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