1 /* $NetBSD: create.c,v 1.46 2004/12/01 10:07:56 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 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if defined(__RCSID) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 40 #else 41 __RCSID("$NetBSD: create.c,v 1.46 2004/12/01 10:07:56 lukem Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 48 #if ! HAVE_NBTOOL_CONFIG_H 49 #include <dirent.h> 50 #endif 51 52 #include <errno.h> 53 #include <fcntl.h> 54 #include <grp.h> 55 #include <pwd.h> 56 #include <stdio.h> 57 #include <stdarg.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <time.h> 61 #include <unistd.h> 62 63 #ifndef NO_MD5 64 #include <md5.h> 65 #endif 66 #ifndef NO_RMD160 67 #include <rmd160.h> 68 #endif 69 #ifndef NO_SHA1 70 #include <sha1.h> 71 #endif 72 73 #include "extern.h" 74 75 #define INDENTNAMELEN 15 76 #define MAXLINELEN 80 77 78 static gid_t gid; 79 static uid_t uid; 80 static mode_t mode; 81 static u_long flags; 82 83 static int dsort(const FTSENT **, const FTSENT **); 84 static void output(int *, const char *, ...) 85 __attribute__((__format__(__printf__, 2, 3))); 86 static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *); 87 static void statf(FTSENT *); 88 89 void 90 cwalk(void) 91 { 92 FTS *t; 93 FTSENT *p; 94 time_t clocktime; 95 char host[MAXHOSTNAMELEN + 1]; 96 char *argv[2]; 97 char dot[] = "."; 98 argv[0] = dot; 99 argv[1] = NULL; 100 101 time(&clocktime); 102 gethostname(host, sizeof(host)); 103 host[sizeof(host) - 1] = '\0'; 104 printf( 105 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 106 getlogin(), host, fullpath, ctime(&clocktime)); 107 108 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 109 mtree_err("fts_open: %s", strerror(errno)); 110 while ((p = fts_read(t)) != NULL) { 111 if (check_excludes(p->fts_name, p->fts_path)) { 112 fts_set(t, p, FTS_SKIP); 113 continue; 114 } 115 switch(p->fts_info) { 116 case FTS_D: 117 printf("\n# %s\n", p->fts_path); 118 statd(t, p, &uid, &gid, &mode, &flags); 119 statf(p); 120 break; 121 case FTS_DP: 122 if (p->fts_level > 0) 123 printf("# %s\n..\n\n", p->fts_path); 124 break; 125 case FTS_DNR: 126 case FTS_ERR: 127 case FTS_NS: 128 mtree_err("%s: %s", 129 p->fts_path, strerror(p->fts_errno)); 130 break; 131 default: 132 if (!dflag) 133 statf(p); 134 break; 135 136 } 137 } 138 fts_close(t); 139 if (sflag && keys & F_CKSUM) 140 mtree_err("%s checksum: %u", fullpath, crc_total); 141 } 142 143 static void 144 statf(FTSENT *p) 145 { 146 u_int32_t len, val; 147 int fd, indent; 148 const char *name; 149 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) 150 char digestbuf[41]; /* large enough for {MD5,RMD160,SHA1}File() */ 151 #endif 152 153 indent = printf("%s%s", 154 S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name)); 155 156 if (indent > INDENTNAMELEN) 157 indent = MAXLINELEN; 158 else 159 indent += printf("%*s", INDENTNAMELEN - indent, ""); 160 161 if (!S_ISREG(p->fts_statp->st_mode)) 162 output(&indent, "type=%s", inotype(p->fts_statp->st_mode)); 163 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) { 164 if (keys & F_UNAME && 165 (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL) 166 output(&indent, "uname=%s", name); 167 else /* if (keys & F_UID) */ 168 output(&indent, "uid=%u", p->fts_statp->st_uid); 169 } 170 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) { 171 if (keys & F_GNAME && 172 (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL) 173 output(&indent, "gname=%s", name); 174 else /* if (keys & F_GID) */ 175 output(&indent, "gid=%u", p->fts_statp->st_gid); 176 } 177 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 178 output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS); 179 if (keys & F_DEV && 180 (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode))) 181 output(&indent, "device=%#x", p->fts_statp->st_rdev); 182 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 183 output(&indent, "nlink=%u", p->fts_statp->st_nlink); 184 if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode)) 185 output(&indent, "size=%lld", (long long)p->fts_statp->st_size); 186 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H) 187 if (keys & F_TIME) 188 output(&indent, "time=%ld.%ld", 189 (long)p->fts_statp->st_mtimespec.tv_sec, 190 p->fts_statp->st_mtimespec.tv_nsec); 191 #else 192 output(&indent, "time=%ld.%ld", 193 p->fts_statp->st_mtime, 0); 194 #endif 195 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 196 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 197 crc(fd, &val, &len)) 198 mtree_err("%s: %s", p->fts_accpath, strerror(errno)); 199 close(fd); 200 output(&indent, "cksum=%lu", (long)val); 201 } 202 #ifndef NO_MD5 203 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 204 if (MD5File(p->fts_accpath, digestbuf) == NULL) 205 mtree_err("%s: %s", p->fts_accpath, "MD5File"); 206 output(&indent, "md5=%s", digestbuf); 207 } 208 #endif /* ! NO_MD5 */ 209 #ifndef NO_RMD160 210 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { 211 if (RMD160File(p->fts_accpath, digestbuf) == NULL) 212 mtree_err("%s: %s", p->fts_accpath, "RMD160File"); 213 output(&indent, "rmd160=%s", digestbuf); 214 } 215 #endif /* ! NO_RMD160 */ 216 #ifndef NO_SHA1 217 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { 218 if (SHA1File(p->fts_accpath, digestbuf) == NULL) 219 mtree_err("%s: %s", p->fts_accpath, "SHA1File"); 220 output(&indent, "sha1=%s", digestbuf); 221 } 222 #endif /* ! NO_SHA1 */ 223 if (keys & F_SLINK && 224 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 225 output(&indent, "link=%s", vispath(rlink(p->fts_accpath))); 226 #if HAVE_STRUCT_STAT_ST_FLAGS 227 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) 228 output(&indent, "flags=%s", 229 flags_to_string(p->fts_statp->st_flags, "none")); 230 #endif 231 putchar('\n'); 232 } 233 234 /* XXX 235 * FLAGS2INDEX will fail once the user and system settable bits need more 236 * than one byte, respectively. 237 */ 238 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff)) 239 240 #define MTREE_MAXGID 5000 241 #define MTREE_MAXUID 5000 242 #define MTREE_MAXMODE (MBITS + 1) 243 #if HAVE_STRUCT_STAT_ST_FLAGS 244 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */ 245 #else 246 #define MTREE_MAXFLAGS 1 247 #endif 248 #define MTREE_MAXS 16 249 250 static int 251 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, 252 u_long *pflags) 253 { 254 FTSENT *p; 255 gid_t sgid; 256 uid_t suid; 257 mode_t smode; 258 u_long sflags = 0; 259 const char *name; 260 gid_t savegid; 261 uid_t saveuid; 262 mode_t savemode; 263 u_long saveflags; 264 u_short maxgid, maxuid, maxmode, maxflags; 265 u_short g[MTREE_MAXGID], u[MTREE_MAXUID], 266 m[MTREE_MAXMODE], f[MTREE_MAXFLAGS]; 267 static int first = 1; 268 269 savegid = *pgid; 270 saveuid = *puid; 271 savemode = *pmode; 272 saveflags = *pflags; 273 if ((p = fts_children(t, 0)) == NULL) { 274 if (errno) 275 mtree_err("%s: %s", RP(parent), strerror(errno)); 276 return (1); 277 } 278 279 memset(g, 0, sizeof(g)); 280 memset(u, 0, sizeof(u)); 281 memset(m, 0, sizeof(m)); 282 memset(f, 0, sizeof(f)); 283 284 maxuid = maxgid = maxmode = maxflags = 0; 285 for (; p; p = p->fts_link) { 286 smode = p->fts_statp->st_mode & MBITS; 287 if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) { 288 savemode = smode; 289 maxmode = m[smode]; 290 } 291 sgid = p->fts_statp->st_gid; 292 if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) { 293 savegid = sgid; 294 maxgid = g[sgid]; 295 } 296 suid = p->fts_statp->st_uid; 297 if (suid < MTREE_MAXUID && ++u[suid] > maxuid) { 298 saveuid = suid; 299 maxuid = u[suid]; 300 } 301 302 #if HAVE_STRUCT_STAT_ST_FLAGS 303 sflags = FLAGS2INDEX(p->fts_statp->st_flags); 304 if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) { 305 saveflags = p->fts_statp->st_flags; 306 maxflags = f[sflags]; 307 } 308 #endif 309 } 310 /* 311 * If the /set record is the same as the last one we do not need to 312 * output a new one. So first we check to see if anything changed. 313 * Note that we always output a /set record for the first directory. 314 */ 315 if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) || 316 ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) || 317 ((keys & F_MODE) && (*pmode != savemode)) || 318 ((keys & F_FLAGS) && (*pflags != saveflags)) || 319 first) { 320 first = 0; 321 printf("/set type=file"); 322 if (keys & (F_UID | F_UNAME)) { 323 if (keys & F_UNAME && 324 (name = user_from_uid(saveuid, 1)) != NULL) 325 printf(" uname=%s", name); 326 else /* if (keys & F_UID) */ 327 printf(" uid=%lu", (u_long)saveuid); 328 } 329 if (keys & (F_GID | F_GNAME)) { 330 if (keys & F_GNAME && 331 (name = group_from_gid(savegid, 1)) != NULL) 332 printf(" gname=%s", name); 333 else /* if (keys & F_UID) */ 334 printf(" gid=%lu", (u_long)savegid); 335 } 336 if (keys & F_MODE) 337 printf(" mode=%#lo", (u_long)savemode); 338 if (keys & F_NLINK) 339 printf(" nlink=1"); 340 if (keys & F_FLAGS) 341 printf(" flags=%s", 342 flags_to_string(saveflags, "none")); 343 printf("\n"); 344 *puid = saveuid; 345 *pgid = savegid; 346 *pmode = savemode; 347 *pflags = saveflags; 348 } 349 return (0); 350 } 351 352 static int 353 dsort(const FTSENT **a, const FTSENT **b) 354 { 355 356 if (S_ISDIR((*a)->fts_statp->st_mode)) { 357 if (!S_ISDIR((*b)->fts_statp->st_mode)) 358 return (1); 359 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 360 return (-1); 361 return (strcmp((*a)->fts_name, (*b)->fts_name)); 362 } 363 364 void 365 output(int *offset, const char *fmt, ...) 366 { 367 va_list ap; 368 char buf[1024]; 369 370 va_start(ap, fmt); 371 vsnprintf(buf, sizeof(buf), fmt, ap); 372 va_end(ap); 373 374 if (*offset + strlen(buf) > MAXLINELEN - 3) { 375 printf(" \\\n%*s", INDENTNAMELEN, ""); 376 *offset = INDENTNAMELEN; 377 } 378 *offset += printf(" %s", buf) + 1; 379 } 380