spec.c revision 99800
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#ifndef lint 35#if 0 36static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: head/usr.sbin/mtree/spec.c 99800 2002-07-11 18:31:16Z alfred $"; 40#endif /* not lint */ 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() 63{ 64 register NODE *centry, *last; 65 register 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(t, ip) 174 char *t; 175 NODE *ip; 176{ 177 register int type; 178 char *kw, *val = NULL; 179 struct group *gr; 180 struct passwd *pw; 181 mode_t *m; 182 int value; 183 char *ep; 184 185 for (; (kw = strtok(t, "= \t\n")); t = NULL) { 186 ip->flags |= type = parsekey(kw, &value); 187 if (value && (val = strtok(NULL, " \t\n")) == NULL) 188 errx(1, "line %d: missing value", lineno); 189 switch(type) { 190 case F_CKSUM: 191 ip->cksum = strtoul(val, &ep, 10); 192 if (*ep) 193 errx(1, "line %d: invalid checksum %s", 194 lineno, val); 195 break; 196 case F_MD5: 197 ip->md5digest = strdup(val); 198 if(!ip->md5digest) { 199 errx(1, "strdup"); 200 } 201 break; 202 case F_SHA1: 203 ip->sha1digest = strdup(val); 204 if(!ip->sha1digest) { 205 errx(1, "strdup"); 206 } 207 break; 208 case F_RMD160: 209 ip->rmd160digest = strdup(val); 210 if(!ip->rmd160digest) { 211 errx(1, "strdup"); 212 } 213 break; 214 case F_FLAGS: 215 if (strcmp("none", val) == 0) 216 ip->st_flags = 0; 217 else if (strtofflags(&val, &ip->st_flags, NULL) != 0) 218 errx(1, "line %d: invalid flag %s",lineno, val); 219 break; 220 case F_GID: 221 ip->st_gid = strtoul(val, &ep, 10); 222 if (*ep) 223 errx(1, "line %d: invalid gid %s", lineno, val); 224 break; 225 case F_GNAME: 226 if ((gr = getgrnam(val)) == NULL) 227 errx(1, "line %d: unknown group %s", lineno, val); 228 ip->st_gid = gr->gr_gid; 229 break; 230 case F_IGN: 231 /* just set flag bit */ 232 break; 233 case F_MODE: 234 if ((m = setmode(val)) == NULL) 235 errx(1, "line %d: invalid file mode %s", 236 lineno, val); 237 ip->st_mode = getmode(m, 0); 238 free(m); 239 break; 240 case F_NLINK: 241 ip->st_nlink = strtoul(val, &ep, 10); 242 if (*ep) 243 errx(1, "line %d: invalid link count %s", 244 lineno, val); 245 break; 246 case F_SIZE: 247 ip->st_size = strtoq(val, &ep, 10); 248 if (*ep) 249 errx(1, "line %d: invalid size %s", 250 lineno, val); 251 break; 252 case F_SLINK: 253 if ((ip->slink = strdup(val)) == NULL) 254 errx(1, "strdup"); 255 break; 256 case F_TIME: 257 ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10); 258 if (*ep != '.') 259 errx(1, "line %d: invalid time %s", 260 lineno, val); 261 val = ep + 1; 262 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 263 if (*ep) 264 errx(1, "line %d: invalid time %s", 265 lineno, val); 266 break; 267 case F_TYPE: 268 switch(*val) { 269 case 'b': 270 if (!strcmp(val, "block")) 271 ip->type = F_BLOCK; 272 break; 273 case 'c': 274 if (!strcmp(val, "char")) 275 ip->type = F_CHAR; 276 break; 277 case 'd': 278 if (!strcmp(val, "dir")) 279 ip->type = F_DIR; 280 break; 281 case 'f': 282 if (!strcmp(val, "file")) 283 ip->type = F_FILE; 284 if (!strcmp(val, "fifo")) 285 ip->type = F_FIFO; 286 break; 287 case 'l': 288 if (!strcmp(val, "link")) 289 ip->type = F_LINK; 290 break; 291 case 's': 292 if (!strcmp(val, "socket")) 293 ip->type = F_SOCK; 294 break; 295 default: 296 errx(1, "line %d: unknown file type %s", 297 lineno, val); 298 } 299 break; 300 case F_UID: 301 ip->st_uid = strtoul(val, &ep, 10); 302 if (*ep) 303 errx(1, "line %d: invalid uid %s", lineno, val); 304 break; 305 case F_UNAME: 306 if ((pw = getpwnam(val)) == NULL) 307 errx(1, "line %d: unknown user %s", lineno, val); 308 ip->st_uid = pw->pw_uid; 309 break; 310 } 311 } 312} 313 314static void 315unset(t, ip) 316 char *t; 317 register NODE *ip; 318{ 319 register char *p; 320 321 while ((p = strtok(t, "\n\t "))) 322 ip->flags &= ~parsekey(p, NULL); 323} 324