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