1 /* $NetBSD: spec.c,v 1.33 2001/10/17 01:19:17 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.33 2001/10/17 01:19:17 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 <string.h> 91 #include <unistd.h> 92 #include <util.h> 93 #include <vis.h> 94 95 #include "mtree.h" 96 #include "extern.h" 97 #include "pack_dev.h" 98 99 size_t lineno; /* Current spec line number. */ 100 101 static dev_t parsedev(char *); 102 static void set(char *, NODE *); 103 static void unset(char *, NODE *); 104 105 NODE * 106 spec(void) 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 (lineno = 0; 120 (buf = fparseln(stdin, NULL, &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 (void)fprintf(stderr, "line %d: {%s}\n", 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 163 noparent: 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 strcpy(centry->name, p); 209 #define MAGIC "?*[" 210 if (strpbrk(p, MAGIC)) 211 centry->flags |= F_MAGIC; 212 set(next, centry); 213 214 if (root == NULL) { 215 last = root = centry; 216 root->parent = root; 217 } else if (pathparent != NULL) { 218 centry->parent = pathparent; 219 cur = pathparent->child; 220 if (cur == NULL) 221 pathparent->child = centry; 222 else { 223 while (cur->next != NULL) 224 cur = cur->next; 225 cur->next = centry; 226 centry->prev = cur; 227 } 228 last = centry; 229 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 230 centry->parent = last; 231 last = last->child = centry; 232 } else { 233 centry->parent = last->parent; 234 centry->prev = last; 235 last = last->next = centry; 236 } 237 } 238 return (root); 239 } 240 241 /* 242 * dump_nodes -- 243 * dump the NODEs from `cur', based in the directory `dir' 244 */ 245 void 246 dump_nodes(const char *dir, NODE *root) 247 { 248 NODE *cur; 249 char path[MAXPATHLEN + 1]; 250 const char *name; 251 252 for (cur = root; cur != NULL; cur = cur->next) { 253 if (cur->type != F_DIR && !matchtags(cur)) 254 continue; 255 256 if (snprintf(path, sizeof(path), "%s%s%s", 257 dir, *dir ? "/" : "", cur->name) 258 >= sizeof(path)) 259 mtree_err("Pathname too long."); 260 261 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 262 if (MATCHFLAG(F_TYPE)) 263 printf("type=%s ", nodetype(cur->type)); 264 if (MATCHFLAG(F_UID | F_UNAME)) { 265 if (keys & F_UNAME && 266 (name = user_from_uid(cur->st_uid, 1)) != NULL) 267 printf("uname=%s ", name); 268 else 269 printf("uid=%u ", cur->st_uid); 270 } 271 if (MATCHFLAG(F_GID | F_GNAME)) { 272 if (keys & F_GNAME && 273 (name = group_from_gid(cur->st_gid, 1)) != NULL) 274 printf("gname=%s ", name); 275 else 276 printf("gid=%u ", cur->st_gid); 277 } 278 if (MATCHFLAG(F_MODE)) 279 printf("mode=%#o ", cur->st_mode); 280 if (MATCHFLAG(F_DEV) && 281 (cur->type == F_BLOCK || cur->type == F_CHAR)) 282 printf("device=%#x ", cur->st_rdev); 283 if (MATCHFLAG(F_NLINK)) 284 printf("nlink=%d ", cur->st_nlink); 285 if (MATCHFLAG(F_SLINK)) 286 printf("link=%s ", cur->slink); 287 if (MATCHFLAG(F_SIZE)) 288 printf("size=%lld ", (long long)cur->st_size); 289 if (MATCHFLAG(F_TIME)) 290 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec, 291 cur->st_mtimespec.tv_nsec); 292 if (MATCHFLAG(F_CKSUM)) 293 printf("cksum=%lu ", cur->cksum); 294 if (MATCHFLAG(F_MD5)) 295 printf("md5=%s ", cur->md5sum); 296 if (MATCHFLAG(F_FLAGS)) 297 printf("flags=%s ", 298 flags_to_string(cur->st_flags, "none")); 299 if (MATCHFLAG(F_IGN)) 300 printf("ignore "); 301 if (MATCHFLAG(F_OPT)) 302 printf("optional "); 303 if (MATCHFLAG(F_TAGS)) 304 printf("tags=%s ", cur->tags); 305 puts(path); 306 307 if (cur->child) 308 dump_nodes(path, cur->child); 309 } 310 } 311 312 static dev_t 313 parsedev(char *arg) 314 { 315 #define MAX_PACK_ARGS 3 316 u_long numbers[MAX_PACK_ARGS]; 317 char *p, *ep, *dev; 318 int argc; 319 pack_t *pack; 320 dev_t result; 321 322 if ((dev = strchr(arg, ',')) != NULL) { 323 *dev++='\0'; 324 if ((pack = pack_find(arg)) == NULL) 325 mtree_err("unknown format `%s'", arg); 326 argc = 0; 327 while ((p = strsep(&dev, ",")) != NULL) { 328 if (*p == '\0') 329 mtree_err("missing number"); 330 numbers[argc++] = strtoul(p, &ep, 0); 331 if (*ep != '\0') 332 mtree_err("invalid number `%s'", 333 p); 334 if (argc > MAX_PACK_ARGS) 335 mtree_err("too many arguments"); 336 } 337 if (argc < 2) 338 mtree_err("not enough arguments"); 339 result = (*pack)(argc, numbers); 340 } else { 341 result = (dev_t)strtoul(arg, &ep, 0); 342 if (*ep != '\0') 343 mtree_err("invalid device `%s'", arg); 344 } 345 return (result); 346 } 347 348 static void 349 set(char *t, NODE *ip) 350 { 351 int type, value, len; 352 gid_t gid; 353 uid_t uid; 354 char *kw, *val, *md, *ep; 355 void *m; 356 357 val = NULL; 358 while ((kw = strsep(&t, "= \t")) != NULL) { 359 if (*kw == '\0') 360 continue; 361 if (strcmp(kw, "all") == 0) 362 mtree_err("invalid keyword `all'"); 363 ip->flags |= type = parsekey(kw, &value); 364 if (value) { 365 while ((val = strsep(&t, " \t")) != NULL && 366 *val == '\0') 367 continue; 368 if (val == NULL) 369 mtree_err("missing value"); 370 } 371 switch(type) { 372 case F_CKSUM: 373 ip->cksum = strtoul(val, &ep, 10); 374 if (*ep) 375 mtree_err("invalid checksum `%s'", val); 376 break; 377 case F_DEV: 378 ip->st_rdev = parsedev(val); 379 break; 380 case F_FLAGS: 381 if (strcmp("none", val) == 0) 382 ip->st_flags = 0; 383 else if (string_to_flags(&val, &ip->st_flags, NULL) 384 != 0) 385 mtree_err("invalid flag `%s'", val); 386 break; 387 case F_GID: 388 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 389 if (*ep) 390 mtree_err("invalid gid `%s'", val); 391 break; 392 case F_GNAME: 393 if (gid_from_group(val, &gid) == -1) 394 mtree_err("unknown group `%s'", val); 395 ip->st_gid = gid; 396 break; 397 case F_IGN: 398 /* just set flag bit */ 399 break; 400 case F_MD5: 401 if (val[0]=='0' && val[1]=='x') 402 md=&val[2]; 403 else 404 md=val; 405 if ((ip->md5sum = strdup(md)) == NULL) 406 mtree_err("memory allocation error"); 407 break; 408 case F_MODE: 409 if ((m = setmode(val)) == NULL) 410 mtree_err("invalid file mode `%s'", val); 411 ip->st_mode = getmode(m, 0); 412 free(m); 413 break; 414 case F_NLINK: 415 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 416 if (*ep) 417 mtree_err("invalid link count `%s'", val); 418 break; 419 case F_OPT: 420 /* just set flag bit */ 421 break; 422 case F_SIZE: 423 ip->st_size = (off_t)strtoq(val, &ep, 10); 424 if (*ep) 425 mtree_err("invalid size `%s'", val); 426 break; 427 case F_SLINK: 428 if ((ip->slink = strdup(val)) == NULL) 429 mtree_err("memory allocation error"); 430 break; 431 case F_TAGS: 432 len = strlen(val) + 3; /* "," + str + ",\0" */ 433 if ((ip->tags = malloc(len)) == NULL) 434 mtree_err("memory allocation error"); 435 snprintf(ip->tags, len, ",%s,", val); 436 break; 437 case F_TIME: 438 ip->st_mtimespec.tv_sec = 439 (time_t)strtoul(val, &ep, 10); 440 if (*ep != '.') 441 mtree_err("invalid time `%s'", val); 442 val = ep + 1; 443 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10); 444 if (*ep) 445 mtree_err("invalid time `%s'", val); 446 break; 447 case F_TYPE: 448 ip->type = parsetype(val); 449 break; 450 case F_UID: 451 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 452 if (*ep) 453 mtree_err("invalid uid `%s'", val); 454 break; 455 case F_UNAME: 456 if (uid_from_user(val, &uid) == -1) 457 mtree_err("unknown user `%s'", val); 458 ip->st_uid = uid; 459 break; 460 default: 461 mtree_err( 462 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 463 type); 464 /* NOTREACHED */ 465 } 466 } 467 } 468 469 static void 470 unset(char *t, NODE *ip) 471 { 472 char *p; 473 474 while ((p = strsep(&t, " \t")) != NULL) { 475 if (*p == '\0') 476 continue; 477 if (strcmp(p, "all") == 0) 478 mtree_err("invalid keyword `all'"); 479 ip->flags &= ~parsekey(p, NULL); 480 } 481 } 482