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