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