spec.c revision 1.49 1 /* $NetBSD: spec.c,v 1.49 2002/12/23 04:40:19 lukem 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 /*-
37 * Copyright (c) 2001-2002 The NetBSD Foundation, Inc.
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Luke Mewburn of Wasabi Systems.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 #include <sys/cdefs.h>
73 #if defined(__RCSID) && !defined(lint)
74 #if 0
75 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
76 #else
77 __RCSID("$NetBSD: spec.c,v 1.49 2002/12/23 04:40:19 lukem Exp $");
78 #endif
79 #endif /* not lint */
80
81 #include <sys/param.h>
82 #include <sys/stat.h>
83
84 #include <ctype.h>
85 #include <errno.h>
86 #include <grp.h>
87 #include <pwd.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <vis.h>
93
94 #include "extern.h"
95 #include "pack_dev.h"
96
97 size_t mtree_lineno; /* Current spec line number */
98 int Wflag; /* Don't "whack" permissions */
99
100 static dev_t parsedev(char *);
101 static void replacenode(NODE *, NODE *);
102 static void set(char *, NODE *);
103 static void unset(char *, NODE *);
104
105 NODE *
106 spec(FILE *fp)
107 {
108 NODE *centry, *last, *pathparent, *cur;
109 char *p, *e, *next;
110 NODE ginfo, *root;
111 char *buf, *tname;
112 size_t tnamelen, plen;
113
114 root = NULL;
115 centry = last = NULL;
116 tname = NULL;
117 tnamelen = 0;
118 memset(&ginfo, 0, sizeof(ginfo));
119 for (mtree_lineno = 0;
120 (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
121 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
122 free(buf)) {
123 /* Skip leading whitespace. */
124 for (p = buf; *p && isspace((unsigned char)*p); ++p)
125 continue;
126
127 /* If nothing but whitespace, continue. */
128 if (!*p)
129 continue;
130
131 #ifdef DEBUG
132 fprintf(stderr, "line %lu: {%s}\n",
133 (u_long)mtree_lineno, p);
134 #endif
135 /* Grab file name, "$", "set", or "unset". */
136 next = buf;
137 while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
138 continue;
139 if (p == NULL)
140 mtree_err("missing field");
141
142 if (p[0] == '/') {
143 if (strcmp(p + 1, "set") == 0)
144 set(next, &ginfo);
145 else if (strcmp(p + 1, "unset") == 0)
146 unset(next, &ginfo);
147 else
148 mtree_err("invalid specification `%s'", p);
149 continue;
150 }
151
152 if (strcmp(p, "..") == 0) {
153 /* Don't go up, if haven't gone down. */
154 if (root == NULL)
155 goto noparent;
156 if (last->type != F_DIR || last->flags & F_DONE) {
157 if (last == root)
158 goto noparent;
159 last = last->parent;
160 }
161 last->flags |= F_DONE;
162 continue;
163
164 noparent: mtree_err("no parent node");
165 }
166
167 plen = strlen(p) + 1;
168 if (plen > tnamelen) {
169 tnamelen = plen;
170 if ((tname = realloc(tname, tnamelen)) == NULL)
171 mtree_err("realloc: %s", strerror(errno));
172 }
173 if (strunvis(tname, p) == -1)
174 mtree_err("strunvis failed on `%s'", p);
175 p = tname;
176
177 pathparent = NULL;
178 if (strchr(p, '/') != NULL) {
179 cur = root;
180 for (; (e = strchr(p, '/')) != NULL; p = e+1) {
181 if (p == e)
182 continue; /* handle // */
183 *e = '\0';
184 if (strcmp(p, ".") != 0) {
185 while (cur &&
186 strcmp(cur->name, p) != 0) {
187 cur = cur->next;
188 }
189 }
190 if (cur == NULL || cur->type != F_DIR) {
191 mtree_err("%s: %s", tname,
192 strerror(ENOENT));
193 }
194 *e = '/';
195 pathparent = cur;
196 cur = cur->child;
197 }
198 if (*p == '\0')
199 mtree_err("%s: empty leaf element", tname);
200 }
201
202 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
203 mtree_err("%s", strerror(errno));
204 *centry = ginfo;
205 centry->lineno = mtree_lineno;
206 strcpy(centry->name, p);
207 #define MAGIC "?*["
208 if (strpbrk(p, MAGIC))
209 centry->flags |= F_MAGIC;
210 set(next, centry);
211
212 if (root == NULL) {
213 /*
214 * empty tree
215 */
216 if (strcmp(centry->name, ".") != 0 ||
217 centry->type != F_DIR)
218 mtree_err(
219 "root node must be the directory `.'");
220 last = root = centry;
221 root->parent = root;
222 } else if (pathparent != NULL) {
223 /*
224 * full path entry
225 */
226 centry->parent = pathparent;
227 cur = pathparent->child;
228 if (cur == NULL) {
229 pathparent->child = centry;
230 last = centry;
231 } else {
232 for (; cur != NULL; cur = cur->next) {
233 if (strcmp(cur->name, centry->name)
234 == 0) {
235 /* existing entry; replace */
236 replacenode(cur, centry);
237 break;
238 }
239 if (cur->next == NULL) {
240 /* last entry; add new */
241 cur->next = centry;
242 centry->prev = cur;
243 break;
244 }
245 }
246 last = cur;
247 while (last->next != NULL)
248 last = last->next;
249 }
250 } else if (strcmp(centry->name, ".") == 0) {
251 /*
252 * duplicate "." entry; always replace
253 */
254 replacenode(root, centry);
255 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
256 /*
257 * new relative child
258 * (no duplicate check)
259 */
260 centry->parent = last;
261 last = last->child = centry;
262 } else {
263 /*
264 * relative entry, up one directory
265 * (no duplicate check)
266 */
267 centry->parent = last->parent;
268 centry->prev = last;
269 last = last->next = centry;
270 }
271 }
272 return (root);
273 }
274
275 /*
276 * dump_nodes --
277 * dump the NODEs from `cur', based in the directory `dir'.
278 * if pathlast is none zero, print the path last, otherwise print
279 * it first.
280 */
281 void
282 dump_nodes(const char *dir, NODE *root, int pathlast)
283 {
284 NODE *cur;
285 char path[MAXPATHLEN];
286 const char *name;
287
288 for (cur = root; cur != NULL; cur = cur->next) {
289 if (cur->type != F_DIR && !matchtags(cur))
290 continue;
291
292 if (snprintf(path, sizeof(path), "%s%s%s",
293 dir, *dir ? "/" : "", cur->name)
294 >= sizeof(path))
295 mtree_err("Pathname too long.");
296
297 if (!pathlast)
298 printf("%s ", vispath(path));
299
300 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
301 if (MATCHFLAG(F_TYPE))
302 printf("type=%s ", nodetype(cur->type));
303 if (MATCHFLAG(F_UID | F_UNAME)) {
304 if (keys & F_UNAME &&
305 (name = user_from_uid(cur->st_uid, 1)) != NULL)
306 printf("uname=%s ", name);
307 else
308 printf("uid=%u ", cur->st_uid);
309 }
310 if (MATCHFLAG(F_GID | F_GNAME)) {
311 if (keys & F_GNAME &&
312 (name = group_from_gid(cur->st_gid, 1)) != NULL)
313 printf("gname=%s ", name);
314 else
315 printf("gid=%u ", cur->st_gid);
316 }
317 if (MATCHFLAG(F_MODE))
318 printf("mode=%#o ", cur->st_mode);
319 if (MATCHFLAG(F_DEV) &&
320 (cur->type == F_BLOCK || cur->type == F_CHAR))
321 printf("device=%#x ", cur->st_rdev);
322 if (MATCHFLAG(F_NLINK))
323 printf("nlink=%d ", cur->st_nlink);
324 if (MATCHFLAG(F_SLINK))
325 printf("link=%s ", cur->slink);
326 if (MATCHFLAG(F_SIZE))
327 printf("size=%lld ", (long long)cur->st_size);
328 if (MATCHFLAG(F_TIME))
329 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
330 cur->st_mtimespec.tv_nsec);
331 if (MATCHFLAG(F_CKSUM))
332 printf("cksum=%lu ", cur->cksum);
333 if (MATCHFLAG(F_MD5))
334 printf("md5=%s ", cur->md5digest);
335 if (MATCHFLAG(F_RMD160))
336 printf("rmd160=%s ", cur->rmd160digest);
337 if (MATCHFLAG(F_SHA1))
338 printf("sha1=%s ", cur->sha1digest);
339 if (MATCHFLAG(F_FLAGS))
340 printf("flags=%s ",
341 flags_to_string(cur->st_flags, "none"));
342 if (MATCHFLAG(F_IGN))
343 printf("ignore ");
344 if (MATCHFLAG(F_OPT))
345 printf("optional ");
346 if (MATCHFLAG(F_TAGS))
347 printf("tags=%s ", cur->tags);
348 puts(pathlast ? vispath(path) : "");
349
350 if (cur->child)
351 dump_nodes(path, cur->child, pathlast);
352 }
353 }
354
355 /*
356 * vispath --
357 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
358 * characters long, and returns a pointer to a static buffer containing
359 * the result.
360 */
361 char *
362 vispath(const char *path)
363 {
364 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
365 static char pathbuf[4*MAXPATHLEN + 1];
366
367 strsvis(pathbuf, path, VIS_CSTYLE, extra);
368 return(pathbuf);
369 }
370
371
372 static dev_t
373 parsedev(char *arg)
374 {
375 #define MAX_PACK_ARGS 3
376 u_long numbers[MAX_PACK_ARGS];
377 char *p, *ep, *dev;
378 int argc;
379 pack_t *pack;
380 dev_t result;
381
382 if ((dev = strchr(arg, ',')) != NULL) {
383 *dev++='\0';
384 if ((pack = pack_find(arg)) == NULL)
385 mtree_err("unknown format `%s'", arg);
386 argc = 0;
387 while ((p = strsep(&dev, ",")) != NULL) {
388 if (*p == '\0')
389 mtree_err("missing number");
390 numbers[argc++] = strtoul(p, &ep, 0);
391 if (*ep != '\0')
392 mtree_err("invalid number `%s'",
393 p);
394 if (argc > MAX_PACK_ARGS)
395 mtree_err("too many arguments");
396 }
397 if (argc < 2)
398 mtree_err("not enough arguments");
399 result = (*pack)(argc, numbers);
400 } else {
401 result = (dev_t)strtoul(arg, &ep, 0);
402 if (*ep != '\0')
403 mtree_err("invalid device `%s'", arg);
404 }
405 return (result);
406 }
407
408 static void
409 replacenode(NODE *cur, NODE *new)
410 {
411
412 if (cur->type != new->type)
413 mtree_err("existing entry type `%s' does not match type `%s'",
414 nodetype(cur->type), nodetype(new->type));
415 #define REPLACE(x) cur->x = new->x
416 #define REPLACESTR(x) if (cur->x) free(cur->x); cur->x = new->x
417
418 REPLACE(st_size);
419 REPLACE(st_mtimespec);
420 REPLACESTR(slink);
421 REPLACE(st_uid);
422 REPLACE(st_gid);
423 REPLACE(st_mode);
424 REPLACE(st_rdev);
425 REPLACE(st_flags);
426 REPLACE(st_nlink);
427 REPLACE(cksum);
428 REPLACESTR(md5digest);
429 REPLACESTR(rmd160digest);
430 REPLACESTR(sha1digest);
431 REPLACESTR(tags);
432 REPLACE(lineno);
433 REPLACE(flags);
434 free(new);
435 }
436
437 static void
438 set(char *t, NODE *ip)
439 {
440 int type, value, len;
441 gid_t gid;
442 uid_t uid;
443 char *kw, *val, *md, *ep;
444 void *m;
445
446 val = NULL;
447 while ((kw = strsep(&t, "= \t")) != NULL) {
448 if (*kw == '\0')
449 continue;
450 if (strcmp(kw, "all") == 0)
451 mtree_err("invalid keyword `all'");
452 ip->flags |= type = parsekey(kw, &value);
453 if (value) {
454 while ((val = strsep(&t, " \t")) != NULL &&
455 *val == '\0')
456 continue;
457 if (val == NULL)
458 mtree_err("missing value");
459 }
460 switch(type) {
461 case F_CKSUM:
462 ip->cksum = strtoul(val, &ep, 10);
463 if (*ep)
464 mtree_err("invalid checksum `%s'", val);
465 break;
466 case F_DEV:
467 ip->st_rdev = parsedev(val);
468 break;
469 case F_FLAGS:
470 if (strcmp("none", val) == 0)
471 ip->st_flags = 0;
472 else if (string_to_flags(&val, &ip->st_flags, NULL)
473 != 0)
474 mtree_err("invalid flag `%s'", val);
475 break;
476 case F_GID:
477 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
478 if (*ep)
479 mtree_err("invalid gid `%s'", val);
480 break;
481 case F_GNAME:
482 if (Wflag) /* don't parse if whacking */
483 break;
484 if (gid_from_group(val, &gid) == -1)
485 mtree_err("unknown group `%s'", val);
486 ip->st_gid = gid;
487 break;
488 case F_IGN:
489 /* just set flag bit */
490 break;
491 case F_MD5:
492 if (val[0]=='0' && val[1]=='x')
493 md=&val[2];
494 else
495 md=val;
496 if ((ip->md5digest = strdup(md)) == NULL)
497 mtree_err("memory allocation error");
498 break;
499 case F_MODE:
500 if ((m = setmode(val)) == NULL)
501 mtree_err("invalid file mode `%s'", val);
502 ip->st_mode = getmode(m, 0);
503 free(m);
504 break;
505 case F_NLINK:
506 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
507 if (*ep)
508 mtree_err("invalid link count `%s'", val);
509 break;
510 case F_OPT:
511 /* just set flag bit */
512 break;
513 case F_RMD160:
514 if (val[0]=='0' && val[1]=='x')
515 md=&val[2];
516 else
517 md=val;
518 if ((ip->rmd160digest = strdup(md)) == NULL)
519 mtree_err("memory allocation error");
520 break;
521 case F_SHA1:
522 if (val[0]=='0' && val[1]=='x')
523 md=&val[2];
524 else
525 md=val;
526 if ((ip->sha1digest = strdup(md)) == NULL)
527 mtree_err("memory allocation error");
528 break;
529 case F_SIZE:
530 ip->st_size = (off_t)strtoll(val, &ep, 10);
531 if (*ep)
532 mtree_err("invalid size `%s'", val);
533 break;
534 case F_SLINK:
535 if ((ip->slink = strdup(val)) == NULL)
536 mtree_err("memory allocation error");
537 break;
538 case F_TAGS:
539 len = strlen(val) + 3; /* "," + str + ",\0" */
540 if ((ip->tags = malloc(len)) == NULL)
541 mtree_err("memory allocation error");
542 snprintf(ip->tags, len, ",%s,", val);
543 break;
544 case F_TIME:
545 ip->st_mtimespec.tv_sec =
546 (time_t)strtoul(val, &ep, 10);
547 if (*ep != '.')
548 mtree_err("invalid time `%s'", val);
549 val = ep + 1;
550 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
551 if (*ep)
552 mtree_err("invalid time `%s'", val);
553 break;
554 case F_TYPE:
555 ip->type = parsetype(val);
556 break;
557 case F_UID:
558 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
559 if (*ep)
560 mtree_err("invalid uid `%s'", val);
561 break;
562 case F_UNAME:
563 if (Wflag) /* don't parse if whacking */
564 break;
565 if (uid_from_user(val, &uid) == -1)
566 mtree_err("unknown user `%s'", val);
567 ip->st_uid = uid;
568 break;
569 default:
570 mtree_err(
571 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
572 type);
573 /* NOTREACHED */
574 }
575 }
576 }
577
578 static void
579 unset(char *t, NODE *ip)
580 {
581 char *p;
582
583 while ((p = strsep(&t, " \t")) != NULL) {
584 if (*p == '\0')
585 continue;
586 ip->flags &= ~parsekey(p, NULL);
587 }
588 }
589