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