1 /* $NetBSD: spec.c,v 1.47 2002/02/11 12:43:55 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 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.47 2002/02/11 12:43:55 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 <util.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 */ 279 void 280 dump_nodes(const char *dir, NODE *root) 281 { 282 NODE *cur; 283 char path[MAXPATHLEN]; 284 const char *name; 285 286 for (cur = root; cur != NULL; cur = cur->next) { 287 if (cur->type != F_DIR && !matchtags(cur)) 288 continue; 289 290 if (snprintf(path, sizeof(path), "%s%s%s", 291 dir, *dir ? "/" : "", cur->name) 292 >= sizeof(path)) 293 mtree_err("Pathname too long."); 294 295 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 296 if (MATCHFLAG(F_TYPE)) 297 printf("type=%s ", nodetype(cur->type)); 298 if (MATCHFLAG(F_UID | F_UNAME)) { 299 if (keys & F_UNAME && 300 (name = user_from_uid(cur->st_uid, 1)) != NULL) 301 printf("uname=%s ", name); 302 else 303 printf("uid=%u ", cur->st_uid); 304 } 305 if (MATCHFLAG(F_GID | F_GNAME)) { 306 if (keys & F_GNAME && 307 (name = group_from_gid(cur->st_gid, 1)) != NULL) 308 printf("gname=%s ", name); 309 else 310 printf("gid=%u ", cur->st_gid); 311 } 312 if (MATCHFLAG(F_MODE)) 313 printf("mode=%#o ", cur->st_mode); 314 if (MATCHFLAG(F_DEV) && 315 (cur->type == F_BLOCK || cur->type == F_CHAR)) 316 printf("device=%#x ", cur->st_rdev); 317 if (MATCHFLAG(F_NLINK)) 318 printf("nlink=%d ", cur->st_nlink); 319 if (MATCHFLAG(F_SLINK)) 320 printf("link=%s ", cur->slink); 321 if (MATCHFLAG(F_SIZE)) 322 printf("size=%lld ", (long long)cur->st_size); 323 if (MATCHFLAG(F_TIME)) 324 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec, 325 cur->st_mtimespec.tv_nsec); 326 if (MATCHFLAG(F_CKSUM)) 327 printf("cksum=%lu ", cur->cksum); 328 if (MATCHFLAG(F_MD5)) 329 printf("md5=%s ", cur->md5digest); 330 if (MATCHFLAG(F_RMD160)) 331 printf("rmd160=%s ", cur->rmd160digest); 332 if (MATCHFLAG(F_SHA1)) 333 printf("sha1=%s ", cur->sha1digest); 334 if (MATCHFLAG(F_FLAGS)) 335 printf("flags=%s ", 336 flags_to_string(cur->st_flags, "none")); 337 if (MATCHFLAG(F_IGN)) 338 printf("ignore "); 339 if (MATCHFLAG(F_OPT)) 340 printf("optional "); 341 if (MATCHFLAG(F_TAGS)) 342 printf("tags=%s ", cur->tags); 343 puts(path); 344 345 if (cur->child) 346 dump_nodes(path, cur->child); 347 } 348 } 349 350 static dev_t 351 parsedev(char *arg) 352 { 353 #define MAX_PACK_ARGS 3 354 u_long numbers[MAX_PACK_ARGS]; 355 char *p, *ep, *dev; 356 int argc; 357 pack_t *pack; 358 dev_t result; 359 360 if ((dev = strchr(arg, ',')) != NULL) { 361 *dev++='\0'; 362 if ((pack = pack_find(arg)) == NULL) 363 mtree_err("unknown format `%s'", arg); 364 argc = 0; 365 while ((p = strsep(&dev, ",")) != NULL) { 366 if (*p == '\0') 367 mtree_err("missing number"); 368 numbers[argc++] = strtoul(p, &ep, 0); 369 if (*ep != '\0') 370 mtree_err("invalid number `%s'", 371 p); 372 if (argc > MAX_PACK_ARGS) 373 mtree_err("too many arguments"); 374 } 375 if (argc < 2) 376 mtree_err("not enough arguments"); 377 result = (*pack)(argc, numbers); 378 } else { 379 result = (dev_t)strtoul(arg, &ep, 0); 380 if (*ep != '\0') 381 mtree_err("invalid device `%s'", arg); 382 } 383 return (result); 384 } 385 386 static void 387 replacenode(NODE *cur, NODE *new) 388 { 389 390 if (cur->type != new->type) 391 mtree_err("existing entry type `%s' does not match type `%s'", 392 nodetype(cur->type), nodetype(new->type)); 393 #define REPLACE(x) cur->x = new->x 394 #define REPLACESTR(x) if (cur->x) free(cur->x); cur->x = new->x 395 396 REPLACE(st_size); 397 REPLACE(st_mtimespec); 398 REPLACESTR(slink); 399 REPLACE(st_uid); 400 REPLACE(st_gid); 401 REPLACE(st_mode); 402 REPLACE(st_rdev); 403 REPLACE(st_flags); 404 REPLACE(st_nlink); 405 REPLACE(cksum); 406 REPLACESTR(md5digest); 407 REPLACESTR(rmd160digest); 408 REPLACESTR(sha1digest); 409 REPLACESTR(tags); 410 REPLACE(lineno); 411 REPLACE(flags); 412 free(new); 413 } 414 415 static void 416 set(char *t, NODE *ip) 417 { 418 int type, value, len; 419 gid_t gid; 420 uid_t uid; 421 char *kw, *val, *md, *ep; 422 void *m; 423 424 val = NULL; 425 while ((kw = strsep(&t, "= \t")) != NULL) { 426 if (*kw == '\0') 427 continue; 428 if (strcmp(kw, "all") == 0) 429 mtree_err("invalid keyword `all'"); 430 ip->flags |= type = parsekey(kw, &value); 431 if (value) { 432 while ((val = strsep(&t, " \t")) != NULL && 433 *val == '\0') 434 continue; 435 if (val == NULL) 436 mtree_err("missing value"); 437 } 438 switch(type) { 439 case F_CKSUM: 440 ip->cksum = strtoul(val, &ep, 10); 441 if (*ep) 442 mtree_err("invalid checksum `%s'", val); 443 break; 444 case F_DEV: 445 ip->st_rdev = parsedev(val); 446 break; 447 case F_FLAGS: 448 if (strcmp("none", val) == 0) 449 ip->st_flags = 0; 450 else if (string_to_flags(&val, &ip->st_flags, NULL) 451 != 0) 452 mtree_err("invalid flag `%s'", val); 453 break; 454 case F_GID: 455 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 456 if (*ep) 457 mtree_err("invalid gid `%s'", val); 458 break; 459 case F_GNAME: 460 if (Wflag) /* don't parse if whacking */ 461 break; 462 if (gid_from_group(val, &gid) == -1) 463 mtree_err("unknown group `%s'", val); 464 ip->st_gid = gid; 465 break; 466 case F_IGN: 467 /* just set flag bit */ 468 break; 469 case F_MD5: 470 if (val[0]=='0' && val[1]=='x') 471 md=&val[2]; 472 else 473 md=val; 474 if ((ip->md5digest = strdup(md)) == NULL) 475 mtree_err("memory allocation error"); 476 break; 477 case F_MODE: 478 if ((m = setmode(val)) == NULL) 479 mtree_err("invalid file mode `%s'", val); 480 ip->st_mode = getmode(m, 0); 481 free(m); 482 break; 483 case F_NLINK: 484 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 485 if (*ep) 486 mtree_err("invalid link count `%s'", val); 487 break; 488 case F_OPT: 489 /* just set flag bit */ 490 break; 491 case F_RMD160: 492 if (val[0]=='0' && val[1]=='x') 493 md=&val[2]; 494 else 495 md=val; 496 if ((ip->rmd160digest = strdup(md)) == NULL) 497 mtree_err("memory allocation error"); 498 break; 499 case F_SHA1: 500 if (val[0]=='0' && val[1]=='x') 501 md=&val[2]; 502 else 503 md=val; 504 if ((ip->sha1digest = strdup(md)) == NULL) 505 mtree_err("memory allocation error"); 506 break; 507 case F_SIZE: 508 ip->st_size = (off_t)strtoll(val, &ep, 10); 509 if (*ep) 510 mtree_err("invalid size `%s'", val); 511 break; 512 case F_SLINK: 513 if ((ip->slink = strdup(val)) == NULL) 514 mtree_err("memory allocation error"); 515 break; 516 case F_TAGS: 517 len = strlen(val) + 3; /* "," + str + ",\0" */ 518 if ((ip->tags = malloc(len)) == NULL) 519 mtree_err("memory allocation error"); 520 snprintf(ip->tags, len, ",%s,", val); 521 break; 522 case F_TIME: 523 ip->st_mtimespec.tv_sec = 524 (time_t)strtoul(val, &ep, 10); 525 if (*ep != '.') 526 mtree_err("invalid time `%s'", val); 527 val = ep + 1; 528 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 529 if (*ep) 530 mtree_err("invalid time `%s'", val); 531 break; 532 case F_TYPE: 533 ip->type = parsetype(val); 534 break; 535 case F_UID: 536 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 537 if (*ep) 538 mtree_err("invalid uid `%s'", val); 539 break; 540 case F_UNAME: 541 if (Wflag) /* don't parse if whacking */ 542 break; 543 if (uid_from_user(val, &uid) == -1) 544 mtree_err("unknown user `%s'", val); 545 ip->st_uid = uid; 546 break; 547 default: 548 mtree_err( 549 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 550 type); 551 /* NOTREACHED */ 552 } 553 } 554 } 555 556 static void 557 unset(char *t, NODE *ip) 558 { 559 char *p; 560 561 while ((p = strsep(&t, " \t")) != NULL) { 562 if (*p == '\0') 563 continue; 564 ip->flags &= ~parsekey(p, NULL); 565 } 566 } 567