spec.c revision 121299
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#if 0 35#ifndef lint 36static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93"; 37#endif /* not lint */ 38#endif 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/usr.sbin/mtree/spec.c 121299 2003-10-21 07:58:52Z phk $"); 41 42#include <sys/types.h> 43#include <sys/stat.h> 44#include <ctype.h> 45#include <err.h> 46#include <errno.h> 47#include <fts.h> 48#include <grp.h> 49#include <pwd.h> 50#include <stdio.h> 51#include <unistd.h> 52#include <vis.h> 53#include "mtree.h" 54#include "extern.h" 55 56int lineno; /* Current spec line number. */ 57 58static void set(char *, NODE *); 59static void unset(char *, NODE *); 60 61NODE * 62spec(void) 63{ 64 NODE *centry, *last; 65 char *p; 66 NODE ginfo, *root; 67 int c_cur, c_next; 68 char buf[2048]; 69 70 centry = last = root = NULL; 71 bzero(&ginfo, sizeof(ginfo)); 72 c_cur = c_next = 0; 73 for (lineno = 1; fgets(buf, sizeof(buf), stdin); 74 ++lineno, c_cur = c_next, c_next = 0) { 75 /* Skip empty lines. */ 76 if (buf[0] == '\n') 77 continue; 78 79 /* Find end of line. */ 80 if ((p = index(buf, '\n')) == NULL) 81 errx(1, "line %d too long", lineno); 82 83 /* See if next line is continuation line. */ 84 if (p[-1] == '\\') { 85 --p; 86 c_next = 1; 87 } 88 89 /* Null-terminate the line. */ 90 *p = '\0'; 91 92 /* Skip leading whitespace. */ 93 for (p = buf; *p && isspace(*p); ++p); 94 95 /* If nothing but whitespace or comment char, continue. */ 96 if (!*p || *p == '#') 97 continue; 98 99#ifdef DEBUG 100 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p); 101#endif 102 if (c_cur) { 103 set(p, centry); 104 continue; 105 } 106 107 /* Grab file name, "$", "set", or "unset". */ 108 if ((p = strtok(p, "\n\t ")) == NULL) 109 errx(1, "line %d: missing field", lineno); 110 111 if (p[0] == '/') 112 switch(p[1]) { 113 case 's': 114 if (strcmp(p + 1, "set")) 115 break; 116 set(NULL, &ginfo); 117 continue; 118 case 'u': 119 if (strcmp(p + 1, "unset")) 120 break; 121 unset(NULL, &ginfo); 122 continue; 123 } 124 125 if (index(p, '/')) 126 errx(1, "line %d: slash character in file name", 127 lineno); 128 129 if (!strcmp(p, "..")) { 130 /* Don't go up, if haven't gone down. */ 131 if (!root) 132 goto noparent; 133 if (last->type != F_DIR || last->flags & F_DONE) { 134 if (last == root) 135 goto noparent; 136 last = last->parent; 137 } 138 last->flags |= F_DONE; 139 continue; 140 141noparent: errx(1, "line %d: no parent node", lineno); 142 } 143 144 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 145 errx(1, "calloc"); 146 *centry = ginfo; 147#define MAGIC "?*[" 148 if (strpbrk(p, MAGIC)) 149 centry->flags |= F_MAGIC; 150 if (strunvis(centry->name, p) == -1) { 151 warnx("filename %s is ill-encoded and literally used", 152 p); 153 strcpy(centry->name, p); 154 } 155 set(NULL, centry); 156 157 if (!root) { 158 last = root = centry; 159 root->parent = root; 160 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 161 centry->parent = last; 162 last = last->child = centry; 163 } else { 164 centry->parent = last->parent; 165 centry->prev = last; 166 last = last->next = centry; 167 } 168 } 169 return (root); 170} 171 172static void 173set(char *t, NODE *ip) 174{ 175 int type; 176 char *kw, *val = NULL; 177 struct group *gr; 178 struct passwd *pw; 179 mode_t *m; 180 int value; 181 char *ep; 182 183 for (; (kw = strtok(t, "= \t\n")); t = NULL) { 184 ip->flags |= type = parsekey(kw, &value); 185 if (value && (val = strtok(NULL, " \t\n")) == NULL) 186 errx(1, "line %d: missing value", lineno); 187 switch(type) { 188 case F_CKSUM: 189 ip->cksum = strtoul(val, &ep, 10); 190 if (*ep) 191 errx(1, "line %d: invalid checksum %s", 192 lineno, val); 193 break; 194 case F_MD5: 195 ip->md5digest = strdup(val); 196 if(!ip->md5digest) { 197 errx(1, "strdup"); 198 } 199 break; 200 case F_SHA1: 201 ip->sha1digest = strdup(val); 202 if(!ip->sha1digest) { 203 errx(1, "strdup"); 204 } 205 break; 206 case F_RMD160: 207 ip->rmd160digest = strdup(val); 208 if(!ip->rmd160digest) { 209 errx(1, "strdup"); 210 } 211 break; 212 case F_FLAGS: 213 if (strcmp("none", val) == 0) 214 ip->st_flags = 0; 215 else if (strtofflags(&val, &ip->st_flags, NULL) != 0) 216 errx(1, "line %d: invalid flag %s",lineno, val); 217 break; 218 case F_GID: 219 ip->st_gid = strtoul(val, &ep, 10); 220 if (*ep) 221 errx(1, "line %d: invalid gid %s", lineno, val); 222 break; 223 case F_GNAME: 224 if ((gr = getgrnam(val)) == NULL) 225 errx(1, "line %d: unknown group %s", lineno, val); 226 ip->st_gid = gr->gr_gid; 227 break; 228 case F_IGN: 229 /* just set flag bit */ 230 break; 231 case F_MODE: 232 if ((m = setmode(val)) == NULL) 233 errx(1, "line %d: invalid file mode %s", 234 lineno, val); 235 ip->st_mode = getmode(m, 0); 236 free(m); 237 break; 238 case F_NLINK: 239 ip->st_nlink = strtoul(val, &ep, 10); 240 if (*ep) 241 errx(1, "line %d: invalid link count %s", 242 lineno, val); 243 break; 244 case F_SIZE: 245 ip->st_size = strtoq(val, &ep, 10); 246 if (*ep) 247 errx(1, "line %d: invalid size %s", 248 lineno, val); 249 break; 250 case F_SLINK: 251 if ((ip->slink = strdup(val)) == NULL) 252 errx(1, "strdup"); 253 break; 254 case F_TIME: 255 ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10); 256 if (*ep != '.') 257 errx(1, "line %d: invalid time %s", 258 lineno, val); 259 val = ep + 1; 260 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 261 if (*ep) 262 errx(1, "line %d: invalid time %s", 263 lineno, val); 264 break; 265 case F_TYPE: 266 switch(*val) { 267 case 'b': 268 if (!strcmp(val, "block")) 269 ip->type = F_BLOCK; 270 break; 271 case 'c': 272 if (!strcmp(val, "char")) 273 ip->type = F_CHAR; 274 break; 275 case 'd': 276 if (!strcmp(val, "dir")) 277 ip->type = F_DIR; 278 break; 279 case 'f': 280 if (!strcmp(val, "file")) 281 ip->type = F_FILE; 282 if (!strcmp(val, "fifo")) 283 ip->type = F_FIFO; 284 break; 285 case 'l': 286 if (!strcmp(val, "link")) 287 ip->type = F_LINK; 288 break; 289 case 's': 290 if (!strcmp(val, "socket")) 291 ip->type = F_SOCK; 292 break; 293 default: 294 errx(1, "line %d: unknown file type %s", 295 lineno, val); 296 } 297 break; 298 case F_UID: 299 ip->st_uid = strtoul(val, &ep, 10); 300 if (*ep) 301 errx(1, "line %d: invalid uid %s", lineno, val); 302 break; 303 case F_UNAME: 304 if ((pw = getpwnam(val)) == NULL) 305 errx(1, "line %d: unknown user %s", lineno, val); 306 ip->st_uid = pw->pw_uid; 307 break; 308 } 309 } 310} 311 312static void 313unset(char *t, NODE *ip) 314{ 315 char *p; 316 317 while ((p = strtok(t, "\n\t "))) 318 ip->flags &= ~parsekey(p, NULL); 319} 320