create.c revision 66584
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: head/usr.sbin/mtree/create.c 66584 2000-10-03 13:13:47Z phk $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/stat.h> 44#include <dirent.h> 45#include <err.h> 46#include <errno.h> 47#include <fcntl.h> 48#include <fts.h> 49#include <grp.h> 50#ifdef MD5 51#include <md5.h> 52#endif 53#ifdef SHA1 54#include <sha.h> 55#endif 56#ifdef RMD160 57#include <ripemd.h> 58#endif 59#include <pwd.h> 60#include <stdio.h> 61#include <time.h> 62#include <unistd.h> 63#include <vis.h> 64#include "mtree.h" 65#include "extern.h" 66 67#define INDENTNAMELEN 15 68#define MAXLINELEN 80 69 70extern long int crc_total; 71extern int ftsoptions; 72extern int dflag, iflag, nflag, sflag; 73extern u_int keys; 74extern char fullpath[MAXPATHLEN]; 75extern int lineno; 76 77static gid_t gid; 78static uid_t uid; 79static mode_t mode; 80static u_long flags = 0xffffffff; 81 82static int dsort __P((const FTSENT **, const FTSENT **)); 83static void output __P((int, int *, const char *, ...)); 84static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, 85 u_long *)); 86static void statf __P((int, FTSENT *)); 87 88void 89cwalk() 90{ 91 register FTS *t; 92 register FTSENT *p; 93 time_t clock; 94 char *argv[2], host[MAXHOSTNAMELEN]; 95 int indent = 0; 96 97 (void)time(&clock); 98 (void)gethostname(host, sizeof(host)); 99 (void)printf( 100 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 101 getlogin(), host, fullpath, ctime(&clock)); 102 103 argv[0] = "."; 104 argv[1] = NULL; 105 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 106 err(1, "line %d: fts_open", lineno); 107 while ((p = fts_read(t))) { 108 if (iflag) 109 indent = p->fts_level * 4; 110 if (check_excludes(p->fts_name, p->fts_path)) { 111 fts_set(t, p, FTS_SKIP); 112 continue; 113 } 114 switch(p->fts_info) { 115 case FTS_D: 116 if (!dflag) 117 (void)printf("\n"); 118 if (!nflag) 119 (void)printf("# %s\n", p->fts_path); 120 statd(t, p, &uid, &gid, &mode, &flags); 121 statf(indent, p); 122 break; 123 case FTS_DP: 124 if (!nflag && (p->fts_level > 0)) 125 (void)printf("%*s# %s\n", indent, "", p->fts_path); 126 (void)printf("%*s..\n", indent, ""); 127 if (!dflag) 128 (void)printf("\n"); 129 break; 130 case FTS_DNR: 131 case FTS_ERR: 132 case FTS_NS: 133 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 134 break; 135 default: 136 if (!dflag) 137 statf(indent, p); 138 break; 139 140 } 141 } 142 (void)fts_close(t); 143 if (sflag && keys & F_CKSUM) 144 warnx("%s checksum: %lu", fullpath, crc_total); 145} 146 147static void 148statf(indent, p) 149 int indent; 150 FTSENT *p; 151{ 152 struct group *gr; 153 struct passwd *pw; 154 u_long len, val; 155 int fd, offset; 156 char *fflags; 157 char *escaped_name; 158 159 escaped_name = calloc(1, p->fts_namelen * 4 + 1); 160 if (escaped_name == NULL) 161 errx(1, "statf(): calloc() failed"); 162 strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL); 163 164 if (iflag || S_ISDIR(p->fts_statp->st_mode)) 165 offset = printf("%*s%s", indent, "", escaped_name); 166 else 167 offset = printf("%*s %s", indent, "", escaped_name); 168 169 free(escaped_name); 170 171 if (offset > (INDENTNAMELEN + indent)) 172 offset = MAXLINELEN; 173 else 174 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 175 176 if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 177 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 178 if (p->fts_statp->st_uid != uid) { 179 if (keys & F_UNAME) { 180 if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 181 output(indent, &offset, "uname=%s", pw->pw_name); 182 } else { 183 errx(1, 184 "line %d: could not get uname for uid=%u", 185 lineno, p->fts_statp->st_uid); 186 } 187 } 188 if (keys & F_UID) 189 output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 190 } 191 if (p->fts_statp->st_gid != gid) { 192 if (keys & F_GNAME) { 193 if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 194 output(indent, &offset, "gname=%s", gr->gr_name); 195 } else { 196 errx(1, 197 "line %d: could not get gname for gid=%u", 198 lineno, p->fts_statp->st_gid); 199 } 200 } 201 if (keys & F_GID) 202 output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 203 } 204 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 205 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 206 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 207 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 208 if (keys & F_SIZE) 209 output(indent, &offset, "size=%qd", p->fts_statp->st_size); 210 if (keys & F_TIME) 211 output(indent, &offset, "time=%ld.%ld", 212 p->fts_statp->st_mtimespec.tv_sec, 213 p->fts_statp->st_mtimespec.tv_nsec); 214 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 215 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 216 crc(fd, &val, &len)) 217 err(1, "line %d: %s", lineno, p->fts_accpath); 218 (void)close(fd); 219 output(indent, &offset, "cksum=%lu", val); 220 } 221#ifdef MD5 222 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 223 char *digest, buf[33]; 224 225 digest = MD5File(p->fts_accpath, buf); 226 if (!digest) { 227 err(1, "line %d: %s", lineno, p->fts_accpath); 228 } else { 229 output(indent, &offset, "md5digest=%s", digest); 230 } 231 } 232#endif /* MD5 */ 233#ifdef SHA1 234 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { 235 char *digest, buf[41]; 236 237 digest = SHA1_File(p->fts_accpath, buf); 238 if (!digest) { 239 err(1, "line %d: %s", lineno, p->fts_accpath); 240 } else { 241 output(indent, &offset, "sha1digest=%s", digest); 242 } 243 } 244#endif /* SHA1 */ 245#ifdef RMD160 246 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { 247 char *digest, buf[41]; 248 249 digest = RIPEMD160_File(p->fts_accpath, buf); 250 if (!digest) { 251 err(1, "line %d: %s", lineno, p->fts_accpath); 252 } else { 253 output(indent, &offset, "ripemd160digest=%s", digest); 254 } 255 } 256#endif /* RMD160 */ 257 if (keys & F_SLINK && 258 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 259 output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 260 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) { 261 fflags = flags_to_string(p->fts_statp->st_flags); 262 output(indent, &offset, "flags=%s", fflags); 263 free(fflags); 264 } 265 (void)putchar('\n'); 266} 267 268#define MAXGID 5000 269#define MAXUID 5000 270#define MAXMODE MBITS + 1 271#define MAXFLAGS 256 272#define MAXS 16 273 274static int 275statd(t, parent, puid, pgid, pmode, pflags) 276 FTS *t; 277 FTSENT *parent; 278 uid_t *puid; 279 gid_t *pgid; 280 mode_t *pmode; 281 u_long *pflags; 282{ 283 register FTSENT *p; 284 register gid_t sgid; 285 register uid_t suid; 286 register mode_t smode; 287 register u_long sflags; 288 struct group *gr; 289 struct passwd *pw; 290 gid_t savegid = *pgid; 291 uid_t saveuid = *puid; 292 mode_t savemode = *pmode; 293 u_long saveflags = *pflags; 294 u_short maxgid, maxuid, maxmode, maxflags; 295 u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS]; 296 char *fflags; 297 static int first = 1; 298 299 if ((p = fts_children(t, 0)) == NULL) { 300 if (errno) 301 err(1, "line %d: %s", lineno, RP(parent)); 302 return (1); 303 } 304 305 bzero(g, sizeof(g)); 306 bzero(u, sizeof(u)); 307 bzero(m, sizeof(m)); 308 bzero(f, sizeof(f)); 309 310 maxuid = maxgid = maxmode = maxflags = 0; 311 for (; p; p = p->fts_link) { 312 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 313 smode = p->fts_statp->st_mode & MBITS; 314 if (smode < MAXMODE && ++m[smode] > maxmode) { 315 savemode = smode; 316 maxmode = m[smode]; 317 } 318 sgid = p->fts_statp->st_gid; 319 if (sgid < MAXGID && ++g[sgid] > maxgid) { 320 savegid = sgid; 321 maxgid = g[sgid]; 322 } 323 suid = p->fts_statp->st_uid; 324 if (suid < MAXUID && ++u[suid] > maxuid) { 325 saveuid = suid; 326 maxuid = u[suid]; 327 } 328 329 /* 330 * XXX 331 * note that the below will break when file flags 332 * are extended beyond the first 4 bytes of each 333 * half word of the flags 334 */ 335#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0)) 336 sflags = p->fts_statp->st_flags; 337 if (FLAGS2IDX(sflags) < MAXFLAGS && 338 ++f[FLAGS2IDX(sflags)] > maxflags) { 339 saveflags = sflags; 340 maxflags = f[FLAGS2IDX(sflags)]; 341 } 342 } 343 } 344 /* 345 * If the /set record is the same as the last one we do not need to output 346 * a new one. So first we check to see if anything changed. Note that we 347 * always output a /set record for the first directory. 348 */ 349 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 350 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 351 ((keys & F_MODE) && (*pmode != savemode)) || 352 ((keys & F_FLAGS) && (*pflags != saveflags)) || 353 (first)) { 354 first = 0; 355 if (dflag) 356 (void)printf("/set type=dir"); 357 else 358 (void)printf("/set type=file"); 359 if (keys & F_UNAME) { 360 if ((pw = getpwuid(saveuid)) != NULL) 361 (void)printf(" uname=%s", pw->pw_name); 362 else 363 errx(1, 364 "line %d: could not get uname for uid=%u", 365 lineno, saveuid); 366 } 367 if (keys & F_UID) 368 (void)printf(" uid=%lu", (u_long)saveuid); 369 if (keys & F_GNAME) { 370 if ((gr = getgrgid(savegid)) != NULL) 371 (void)printf(" gname=%s", gr->gr_name); 372 else 373 errx(1, 374 "line %d: could not get gname for gid=%u", 375 lineno, savegid); 376 } 377 if (keys & F_GID) 378 (void)printf(" gid=%lu", (u_long)savegid); 379 if (keys & F_MODE) 380 (void)printf(" mode=%#o", savemode); 381 if (keys & F_NLINK) 382 (void)printf(" nlink=1"); 383 if (keys & F_FLAGS) { 384 fflags = flags_to_string(saveflags); 385 (void)printf(" flags=%s", fflags); 386 free(fflags); 387 } 388 (void)printf("\n"); 389 *puid = saveuid; 390 *pgid = savegid; 391 *pmode = savemode; 392 *pflags = saveflags; 393 } 394 return (0); 395} 396 397static int 398dsort(a, b) 399 const FTSENT **a, **b; 400{ 401 if (S_ISDIR((*a)->fts_statp->st_mode)) { 402 if (!S_ISDIR((*b)->fts_statp->st_mode)) 403 return (1); 404 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 405 return (-1); 406 return (strcmp((*a)->fts_name, (*b)->fts_name)); 407} 408 409#if __STDC__ 410#include <stdarg.h> 411#else 412#include <varargs.h> 413#endif 414 415void 416#if __STDC__ 417output(int indent, int *offset, const char *fmt, ...) 418#else 419output(indent, offset, fmt, va_alist) 420 int indent; 421 int *offset; 422 char *fmt; 423 va_dcl 424#endif 425{ 426 va_list ap; 427 char buf[1024]; 428#if __STDC__ 429 va_start(ap, fmt); 430#else 431 va_start(ap); 432#endif 433 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 434 va_end(ap); 435 436 if (*offset + strlen(buf) > MAXLINELEN - 3) { 437 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 438 *offset = INDENTNAMELEN + indent; 439 } 440 *offset += printf(" %s", buf) + 1; 441} 442