1 /* $NetBSD: create.c,v 1.44 2003/10/27 00:12:44 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(__RCSID) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 36 #else 37 __RCSID("$NetBSD: create.c,v 1.44 2003/10/27 00:12:44 lukem Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 44 #if ! HAVE_NBTOOL_CONFIG_H 45 #include <dirent.h> 46 #endif 47 48 #include <errno.h> 49 #include <fcntl.h> 50 #include <grp.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdarg.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 #ifndef NO_MD5 60 #include <md5.h> 61 #endif 62 #ifndef NO_RMD160 63 #include <rmd160.h> 64 #endif 65 #ifndef NO_SHA1 66 #include <sha1.h> 67 #endif 68 69 #include "extern.h" 70 71 #define INDENTNAMELEN 15 72 #define MAXLINELEN 80 73 74 static gid_t gid; 75 static uid_t uid; 76 static mode_t mode; 77 static u_long flags; 78 79 static int dsort(const FTSENT **, const FTSENT **); 80 static void output(int *, const char *, ...) 81 __attribute__((__format__(__printf__, 2, 3))); 82 static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *); 83 static void statf(FTSENT *); 84 85 void 86 cwalk(void) 87 { 88 FTS *t; 89 FTSENT *p; 90 time_t clocktime; 91 char host[MAXHOSTNAMELEN + 1]; 92 char *argv[2]; 93 char dot[] = "."; 94 argv[0] = dot; 95 argv[1] = NULL; 96 97 time(&clocktime); 98 gethostname(host, sizeof(host)); 99 host[sizeof(host) - 1] = '\0'; 100 printf( 101 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 102 getlogin(), host, fullpath, ctime(&clocktime)); 103 104 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 105 mtree_err("fts_open: %s", strerror(errno)); 106 while ((p = fts_read(t)) != NULL) { 107 if (check_excludes(p->fts_name, p->fts_path)) { 108 fts_set(t, p, FTS_SKIP); 109 continue; 110 } 111 switch(p->fts_info) { 112 case FTS_D: 113 printf("\n# %s\n", p->fts_path); 114 statd(t, p, &uid, &gid, &mode, &flags); 115 statf(p); 116 break; 117 case FTS_DP: 118 if (p->fts_level > 0) 119 printf("# %s\n..\n\n", p->fts_path); 120 break; 121 case FTS_DNR: 122 case FTS_ERR: 123 case FTS_NS: 124 mtree_err("%s: %s", 125 p->fts_path, strerror(p->fts_errno)); 126 break; 127 default: 128 if (!dflag) 129 statf(p); 130 break; 131 132 } 133 } 134 fts_close(t); 135 if (sflag && keys & F_CKSUM) 136 mtree_err("%s checksum: %u", fullpath, crc_total); 137 } 138 139 static void 140 statf(FTSENT *p) 141 { 142 u_int32_t len, val; 143 int fd, indent; 144 const char *name, *path; 145 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) 146 char digestbuf[41]; /* large enough for {MD5,RMD160,SHA1}File() */ 147 #endif 148 149 path = vispath(p->fts_name); 150 indent = printf("%s%s", 151 S_ISDIR(p->fts_statp->st_mode) ? "" : " ", path); 152 153 if (indent > INDENTNAMELEN) 154 indent = MAXLINELEN; 155 else 156 indent += printf("%*s", INDENTNAMELEN - indent, ""); 157 158 if (!S_ISREG(p->fts_statp->st_mode)) 159 output(&indent, "type=%s", inotype(p->fts_statp->st_mode)); 160 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) { 161 if (keys & F_UNAME && 162 (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL) 163 output(&indent, "uname=%s", name); 164 else /* if (keys & F_UID) */ 165 output(&indent, "uid=%u", p->fts_statp->st_uid); 166 } 167 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) { 168 if (keys & F_GNAME && 169 (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL) 170 output(&indent, "gname=%s", name); 171 else /* if (keys & F_GID) */ 172 output(&indent, "gid=%u", p->fts_statp->st_gid); 173 } 174 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 175 output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS); 176 if (keys & F_DEV && 177 (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode))) 178 output(&indent, "device=%#x", p->fts_statp->st_rdev); 179 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 180 output(&indent, "nlink=%u", p->fts_statp->st_nlink); 181 if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode)) 182 output(&indent, "size=%lld", (long long)p->fts_statp->st_size); 183 #ifdef BSD4_4 184 if (keys & F_TIME) 185 output(&indent, "time=%ld.%ld", 186 (long)p->fts_statp->st_mtimespec.tv_sec, 187 p->fts_statp->st_mtimespec.tv_nsec); 188 #else 189 output(&indent, "time=%ld.%ld", 190 p->fts_statp->st_mtime, 0); 191 #endif 192 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 193 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 194 crc(fd, &val, &len)) 195 mtree_err("%s: %s", p->fts_accpath, strerror(errno)); 196 close(fd); 197 output(&indent, "cksum=%lu", (long)val); 198 } 199 #ifndef NO_MD5 200 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 201 if (MD5File(p->fts_accpath, digestbuf) == NULL) 202 mtree_err("%s: %s", p->fts_accpath, "MD5File"); 203 output(&indent, "md5=%s", digestbuf); 204 } 205 #endif /* ! NO_MD5 */ 206 #ifndef NO_RMD160 207 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { 208 if (RMD160File(p->fts_accpath, digestbuf) == NULL) 209 mtree_err("%s: %s", p->fts_accpath, "RMD160File"); 210 output(&indent, "rmd160=%s", digestbuf); 211 } 212 #endif /* ! NO_RMD160 */ 213 #ifndef NO_SHA1 214 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { 215 if (SHA1File(p->fts_accpath, digestbuf) == NULL) 216 mtree_err("%s: %s", p->fts_accpath, "SHA1File"); 217 output(&indent, "sha1=%s", digestbuf); 218 } 219 #endif /* ! NO_SHA1 */ 220 if (keys & F_SLINK && 221 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 222 output(&indent, "link=%s", rlink(p->fts_accpath)); 223 #if HAVE_STRUCT_STAT_ST_FLAGS 224 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) 225 output(&indent, "flags=%s", 226 flags_to_string(p->fts_statp->st_flags, "none")); 227 #endif 228 putchar('\n'); 229 } 230 231 /* XXX 232 * FLAGS2INDEX will fail once the user and system settable bits need more 233 * than one byte, respectively. 234 */ 235 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff)) 236 237 #define MTREE_MAXGID 5000 238 #define MTREE_MAXUID 5000 239 #define MTREE_MAXMODE (MBITS + 1) 240 #if HAVE_STRUCT_STAT_ST_FLAGS 241 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */ 242 #else 243 #define MTREE_MAXFLAGS 1 244 #endif 245 #define MTREE_MAXS 16 246 247 static int 248 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, 249 u_long *pflags) 250 { 251 FTSENT *p; 252 gid_t sgid; 253 uid_t suid; 254 mode_t smode; 255 u_long sflags = 0; 256 const char *name; 257 gid_t savegid; 258 uid_t saveuid; 259 mode_t savemode; 260 u_long saveflags; 261 u_short maxgid, maxuid, maxmode, maxflags; 262 u_short g[MTREE_MAXGID], u[MTREE_MAXUID], 263 m[MTREE_MAXMODE], f[MTREE_MAXFLAGS]; 264 static int first = 1; 265 266 savegid = *pgid; 267 saveuid = *puid; 268 savemode = *pmode; 269 saveflags = *pflags; 270 if ((p = fts_children(t, 0)) == NULL) { 271 if (errno) 272 mtree_err("%s: %s", RP(parent), strerror(errno)); 273 return (1); 274 } 275 276 memset(g, 0, sizeof(g)); 277 memset(u, 0, sizeof(u)); 278 memset(m, 0, sizeof(m)); 279 memset(f, 0, sizeof(f)); 280 281 maxuid = maxgid = maxmode = maxflags = 0; 282 for (; p; p = p->fts_link) { 283 smode = p->fts_statp->st_mode & MBITS; 284 if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) { 285 savemode = smode; 286 maxmode = m[smode]; 287 } 288 sgid = p->fts_statp->st_gid; 289 if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) { 290 savegid = sgid; 291 maxgid = g[sgid]; 292 } 293 suid = p->fts_statp->st_uid; 294 if (suid < MTREE_MAXUID && ++u[suid] > maxuid) { 295 saveuid = suid; 296 maxuid = u[suid]; 297 } 298 299 #if HAVE_STRUCT_STAT_ST_FLAGS 300 sflags = FLAGS2INDEX(p->fts_statp->st_flags); 301 if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) { 302 saveflags = p->fts_statp->st_flags; 303 maxflags = f[sflags]; 304 } 305 #endif 306 } 307 /* 308 * If the /set record is the same as the last one we do not need to 309 * output a new one. So first we check to see if anything changed. 310 * Note that we always output a /set record for the first directory. 311 */ 312 if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) || 313 ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) || 314 ((keys & F_MODE) && (*pmode != savemode)) || 315 ((keys & F_FLAGS) && (*pflags != saveflags)) || 316 first) { 317 first = 0; 318 printf("/set type=file"); 319 if (keys & (F_UID | F_UNAME)) { 320 if (keys & F_UNAME && 321 (name = user_from_uid(saveuid, 1)) != NULL) 322 printf(" uname=%s", name); 323 else /* if (keys & F_UID) */ 324 printf(" uid=%lu", (u_long)saveuid); 325 } 326 if (keys & (F_GID | F_GNAME)) { 327 if (keys & F_GNAME && 328 (name = group_from_gid(savegid, 1)) != NULL) 329 printf(" gname=%s", name); 330 else /* if (keys & F_UID) */ 331 printf(" gid=%lu", (u_long)savegid); 332 } 333 if (keys & F_MODE) 334 printf(" mode=%#lo", (u_long)savemode); 335 if (keys & F_NLINK) 336 printf(" nlink=1"); 337 if (keys & F_FLAGS) 338 printf(" flags=%s", 339 flags_to_string(saveflags, "none")); 340 printf("\n"); 341 *puid = saveuid; 342 *pgid = savegid; 343 *pmode = savemode; 344 *pflags = saveflags; 345 } 346 return (0); 347 } 348 349 static int 350 dsort(const FTSENT **a, const FTSENT **b) 351 { 352 353 if (S_ISDIR((*a)->fts_statp->st_mode)) { 354 if (!S_ISDIR((*b)->fts_statp->st_mode)) 355 return (1); 356 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 357 return (-1); 358 return (strcmp((*a)->fts_name, (*b)->fts_name)); 359 } 360 361 void 362 output(int *offset, const char *fmt, ...) 363 { 364 va_list ap; 365 char buf[1024]; 366 367 va_start(ap, fmt); 368 vsnprintf(buf, sizeof(buf), fmt, ap); 369 va_end(ap); 370 371 if (*offset + strlen(buf) > MAXLINELEN - 3) { 372 printf(" \\\n%*s", INDENTNAMELEN, ""); 373 *offset = INDENTNAMELEN; 374 } 375 *offset += printf(" %s", buf) + 1; 376 } 377