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