1 /* $NetBSD: spec.c,v 1.41 2001/11/09 06:55:56 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 #ifndef 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.41 2001/11/09 06:55:56 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 <fts.h> 87 #include <grp.h> 88 #include <pwd.h> 89 #include <stdio.h> 90 #include <stdlib.h> 91 #include <string.h> 92 #include <unistd.h> 93 #include <util.h> 94 #include <vis.h> 95 96 #include "mtree.h" 97 #include "extern.h" 98 #include "pack_dev.h" 99 100 size_t mtree_lineno; /* Current spec line number */ 101 int Wflag; /* Don't "whack" permissions */ 102 103 static dev_t parsedev(char *); 104 static void set(char *, NODE *); 105 static void unset(char *, NODE *); 106 107 NODE * 108 spec(FILE *fp) 109 { 110 NODE *centry, *last, *pathparent, *cur; 111 char *p, *e, *next; 112 NODE ginfo, *root; 113 char *buf, *tname; 114 size_t tnamelen, plen; 115 116 root = NULL; 117 centry = last = NULL; 118 tname = NULL; 119 tnamelen = 0; 120 memset(&ginfo, 0, sizeof(ginfo)); 121 for (mtree_lineno = 0; 122 (buf = fparseln(fp, NULL, &mtree_lineno, NULL, 123 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC)); 124 free(buf)) { 125 /* Skip leading whitespace. */ 126 for (p = buf; *p && isspace((unsigned char)*p); ++p) 127 continue; 128 129 /* If nothing but whitespace, continue. */ 130 if (!*p) 131 continue; 132 133 #ifdef DEBUG 134 fprintf(stderr, "line %lu: {%s}\n", 135 (u_long)mtree_lineno, p); 136 #endif 137 /* Grab file name, "$", "set", or "unset". */ 138 next = buf; 139 while ((p = strsep(&next, " \t")) != NULL && *p == '\0') 140 continue; 141 if (p == NULL) 142 mtree_err("missing field"); 143 144 if (p[0] == '/') { 145 if (strcmp(p + 1, "set") == 0) 146 set(next, &ginfo); 147 else if (strcmp(p + 1, "unset") == 0) 148 unset(next, &ginfo); 149 else 150 mtree_err("invalid specification `%s'", p); 151 continue; 152 } 153 154 if (strcmp(p, "..") == 0) { 155 /* Don't go up, if haven't gone down. */ 156 if (root == NULL) 157 goto noparent; 158 if (last->type != F_DIR || last->flags & F_DONE) { 159 if (last == root) 160 goto noparent; 161 last = last->parent; 162 } 163 last->flags |= F_DONE; 164 continue; 165 166 noparent: mtree_err("no parent node"); 167 } 168 169 plen = strlen(p) + 1; 170 if (plen > tnamelen) { 171 tnamelen = plen; 172 if ((tname = realloc(tname, tnamelen)) == NULL) 173 mtree_err("realloc: %s", strerror(errno)); 174 } 175 if (strunvis(tname, p) == -1) 176 mtree_err("strunvis failed on `%s'", p); 177 p = tname; 178 179 pathparent = NULL; 180 if (strchr(p, '/') != NULL) { 181 cur = root; 182 for (; (e = strchr(p, '/')) != NULL; p = e+1) { 183 if (p == e) 184 continue; /* handle // */ 185 *e = '\0'; 186 if (strcmp(p, ".") != 0) { 187 while (cur && strcmp(cur->name, p)) { 188 cur = cur->next; 189 } 190 } 191 if (cur == NULL || cur->type != F_DIR) { 192 mtree_err("%s: %s", tname, 193 strerror(ENOENT)); 194 } 195 *e = '/'; 196 pathparent = cur; 197 cur = cur->child; 198 } 199 if (*p == '\0') 200 mtree_err("%s: empty leaf element", tname); 201 for (; cur != NULL; cur = cur->next) { 202 if (strcmp(cur->name, p) == 0) 203 mtree_err("%s: %s", p, 204 strerror(EEXIST)); 205 } 206 } 207 208 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 209 mtree_err("%s", strerror(errno)); 210 *centry = ginfo; 211 centry->lineno = mtree_lineno; 212 strcpy(centry->name, p); 213 #define MAGIC "?*[" 214 if (strpbrk(p, MAGIC)) 215 centry->flags |= F_MAGIC; 216 set(next, centry); 217 218 if (root == NULL) { 219 if (strcmp(centry->name, ".") || centry->type != F_DIR) 220 mtree_err( 221 "root node must be the directory `.'"); 222 last = root = centry; 223 root->parent = root; 224 } else if (pathparent != NULL) { 225 centry->parent = pathparent; 226 cur = pathparent->child; 227 if (cur == NULL) 228 pathparent->child = centry; 229 else { 230 while (cur->next != NULL) 231 cur = cur->next; 232 cur->next = centry; 233 centry->prev = cur; 234 } 235 last = centry; 236 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 237 centry->parent = last; 238 last = last->child = centry; 239 } else { 240 centry->parent = last->parent; 241 centry->prev = last; 242 last = last->next = centry; 243 } 244 } 245 return (root); 246 } 247 248 /* 249 * dump_nodes -- 250 * dump the NODEs from `cur', based in the directory `dir' 251 */ 252 void 253 dump_nodes(const char *dir, NODE *root) 254 { 255 NODE *cur; 256 char path[MAXPATHLEN]; 257 const char *name; 258 259 for (cur = root; cur != NULL; cur = cur->next) { 260 if (cur->type != F_DIR && !matchtags(cur)) 261 continue; 262 263 if (snprintf(path, sizeof(path), "%s%s%s", 264 dir, *dir ? "/" : "", cur->name) 265 >= sizeof(path)) 266 mtree_err("Pathname too long."); 267 268 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 269 if (MATCHFLAG(F_TYPE)) 270 printf("type=%s ", nodetype(cur->type)); 271 if (MATCHFLAG(F_UID | F_UNAME)) { 272 if (keys & F_UNAME && 273 (name = user_from_uid(cur->st_uid, 1)) != NULL) 274 printf("uname=%s ", name); 275 else 276 printf("uid=%u ", cur->st_uid); 277 } 278 if (MATCHFLAG(F_GID | F_GNAME)) { 279 if (keys & F_GNAME && 280 (name = group_from_gid(cur->st_gid, 1)) != NULL) 281 printf("gname=%s ", name); 282 else 283 printf("gid=%u ", cur->st_gid); 284 } 285 if (MATCHFLAG(F_MODE)) 286 printf("mode=%#o ", cur->st_mode); 287 if (MATCHFLAG(F_DEV) && 288 (cur->type == F_BLOCK || cur->type == F_CHAR)) 289 printf("device=%#x ", cur->st_rdev); 290 if (MATCHFLAG(F_NLINK)) 291 printf("nlink=%d ", cur->st_nlink); 292 if (MATCHFLAG(F_SLINK)) 293 printf("link=%s ", cur->slink); 294 if (MATCHFLAG(F_SIZE)) 295 printf("size=%lld ", (long long)cur->st_size); 296 if (MATCHFLAG(F_TIME)) 297 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec, 298 cur->st_mtimespec.tv_nsec); 299 if (MATCHFLAG(F_CKSUM)) 300 printf("cksum=%lu ", cur->cksum); 301 if (MATCHFLAG(F_MD5)) 302 printf("md5=%s ", cur->md5digest); 303 if (MATCHFLAG(F_RMD160)) 304 printf("rmd160=%s ", cur->rmd160digest); 305 if (MATCHFLAG(F_SHA1)) 306 printf("sha1=%s ", cur->sha1digest); 307 if (MATCHFLAG(F_FLAGS)) 308 printf("flags=%s ", 309 flags_to_string(cur->st_flags, "none")); 310 if (MATCHFLAG(F_IGN)) 311 printf("ignore "); 312 if (MATCHFLAG(F_OPT)) 313 printf("optional "); 314 if (MATCHFLAG(F_TAGS)) 315 printf("tags=%s ", cur->tags); 316 puts(path); 317 318 if (cur->child) 319 dump_nodes(path, cur->child); 320 } 321 } 322 323 static dev_t 324 parsedev(char *arg) 325 { 326 #define MAX_PACK_ARGS 3 327 u_long numbers[MAX_PACK_ARGS]; 328 char *p, *ep, *dev; 329 int argc; 330 pack_t *pack; 331 dev_t result; 332 333 if ((dev = strchr(arg, ',')) != NULL) { 334 *dev++='\0'; 335 if ((pack = pack_find(arg)) == NULL) 336 mtree_err("unknown format `%s'", arg); 337 argc = 0; 338 while ((p = strsep(&dev, ",")) != NULL) { 339 if (*p == '\0') 340 mtree_err("missing number"); 341 numbers[argc++] = strtoul(p, &ep, 0); 342 if (*ep != '\0') 343 mtree_err("invalid number `%s'", 344 p); 345 if (argc > MAX_PACK_ARGS) 346 mtree_err("too many arguments"); 347 } 348 if (argc < 2) 349 mtree_err("not enough arguments"); 350 result = (*pack)(argc, numbers); 351 } else { 352 result = (dev_t)strtoul(arg, &ep, 0); 353 if (*ep != '\0') 354 mtree_err("invalid device `%s'", arg); 355 } 356 return (result); 357 } 358 359 static void 360 set(char *t, NODE *ip) 361 { 362 int type, value, len; 363 gid_t gid; 364 uid_t uid; 365 char *kw, *val, *md, *ep; 366 void *m; 367 368 val = NULL; 369 while ((kw = strsep(&t, "= \t")) != NULL) { 370 if (*kw == '\0') 371 continue; 372 if (strcmp(kw, "all") == 0) 373 mtree_err("invalid keyword `all'"); 374 ip->flags |= type = parsekey(kw, &value); 375 if (value) { 376 while ((val = strsep(&t, " \t")) != NULL && 377 *val == '\0') 378 continue; 379 if (val == NULL) 380 mtree_err("missing value"); 381 } 382 switch(type) { 383 case F_CKSUM: 384 ip->cksum = strtoul(val, &ep, 10); 385 if (*ep) 386 mtree_err("invalid checksum `%s'", val); 387 break; 388 case F_DEV: 389 ip->st_rdev = parsedev(val); 390 break; 391 case F_FLAGS: 392 if (strcmp("none", val) == 0) 393 ip->st_flags = 0; 394 else if (string_to_flags(&val, &ip->st_flags, NULL) 395 != 0) 396 mtree_err("invalid flag `%s'", val); 397 break; 398 case F_GID: 399 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 400 if (*ep) 401 mtree_err("invalid gid `%s'", val); 402 break; 403 case F_GNAME: 404 if (Wflag) /* don't parse if whacking */ 405 break; 406 if (gid_from_group(val, &gid) == -1) 407 mtree_err("unknown group `%s'", val); 408 ip->st_gid = gid; 409 break; 410 case F_IGN: 411 /* just set flag bit */ 412 break; 413 case F_MD5: 414 if (val[0]=='0' && val[1]=='x') 415 md=&val[2]; 416 else 417 md=val; 418 if ((ip->md5digest = strdup(md)) == NULL) 419 mtree_err("memory allocation error"); 420 break; 421 case F_MODE: 422 if ((m = setmode(val)) == NULL) 423 mtree_err("invalid file mode `%s'", val); 424 ip->st_mode = getmode(m, 0); 425 free(m); 426 break; 427 case F_NLINK: 428 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 429 if (*ep) 430 mtree_err("invalid link count `%s'", val); 431 break; 432 case F_OPT: 433 /* just set flag bit */ 434 break; 435 case F_RMD160: 436 if (val[0]=='0' && val[1]=='x') 437 md=&val[2]; 438 else 439 md=val; 440 if ((ip->rmd160digest = strdup(md)) == NULL) 441 mtree_err("memory allocation error"); 442 break; 443 case F_SHA1: 444 if (val[0]=='0' && val[1]=='x') 445 md=&val[2]; 446 else 447 md=val; 448 if ((ip->sha1digest = strdup(md)) == NULL) 449 mtree_err("memory allocation error"); 450 break; 451 case F_SIZE: 452 ip->st_size = (off_t)strtoll(val, &ep, 10); 453 if (*ep) 454 mtree_err("invalid size `%s'", val); 455 break; 456 case F_SLINK: 457 if ((ip->slink = strdup(val)) == NULL) 458 mtree_err("memory allocation error"); 459 break; 460 case F_TAGS: 461 len = strlen(val) + 3; /* "," + str + ",\0" */ 462 if ((ip->tags = malloc(len)) == NULL) 463 mtree_err("memory allocation error"); 464 snprintf(ip->tags, len, ",%s,", val); 465 break; 466 case F_TIME: 467 ip->st_mtimespec.tv_sec = 468 (time_t)strtoul(val, &ep, 10); 469 if (*ep != '.') 470 mtree_err("invalid time `%s'", val); 471 val = ep + 1; 472 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 473 if (*ep) 474 mtree_err("invalid time `%s'", val); 475 break; 476 case F_TYPE: 477 ip->type = parsetype(val); 478 break; 479 case F_UID: 480 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 481 if (*ep) 482 mtree_err("invalid uid `%s'", val); 483 break; 484 case F_UNAME: 485 if (Wflag) /* don't parse if whacking */ 486 break; 487 if (uid_from_user(val, &uid) == -1) 488 mtree_err("unknown user `%s'", val); 489 ip->st_uid = uid; 490 break; 491 default: 492 mtree_err( 493 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 494 type); 495 /* NOTREACHED */ 496 } 497 } 498 } 499 500 static void 501 unset(char *t, NODE *ip) 502 { 503 char *p; 504 505 while ((p = strsep(&t, " \t")) != NULL) { 506 if (*p == '\0') 507 continue; 508 if (strcmp(p, "all") == 0) 509 mtree_err("invalid keyword `all'"); 510 ip->flags &= ~parsekey(p, NULL); 511 } 512 } 513