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