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