1 /* $NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)misc.c 8.1 (Berkeley) 6/6/93
32 */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "extern.h"
43
44 enum flavor flavor = F_MTREE;
45
46 typedef struct _key {
47 const char *name; /* key name */
48 u_int val; /* value */
49
50 #define NEEDVALUE 0x01
51 u_int flags;
52 } KEY;
53
54 /* NB: the following tables must be sorted lexically. */
55 static KEY keylist[] = {
56 {"cksum", F_CKSUM, NEEDVALUE},
57 {"device", F_DEV, NEEDVALUE},
58 {"flags", F_FLAGS, NEEDVALUE},
59 {"gid", F_GID, NEEDVALUE},
60 {"gname", F_GNAME, NEEDVALUE},
61 {"ignore", F_IGN, 0},
62 {"link", F_SLINK, NEEDVALUE},
63 {"md5", F_MD5, NEEDVALUE},
64 {"md5digest", F_MD5, NEEDVALUE},
65 {"mode", F_MODE, NEEDVALUE},
66 {"nlink", F_NLINK, NEEDVALUE},
67 {"nochange", F_NOCHANGE, 0},
68 {"optional", F_OPT, 0},
69 {"ripemd160digest", F_RMD160, NEEDVALUE},
70 {"rmd160", F_RMD160, NEEDVALUE},
71 {"rmd160digest",F_RMD160, NEEDVALUE},
72 {"sha1", F_SHA1, NEEDVALUE},
73 {"sha1digest", F_SHA1, NEEDVALUE},
74 {"sha256", F_SHA256, NEEDVALUE},
75 {"sha256digest",F_SHA256, NEEDVALUE},
76 {"sha384", F_SHA384, NEEDVALUE},
77 {"sha384digest",F_SHA384, NEEDVALUE},
78 {"sha512", F_SHA512, NEEDVALUE},
79 {"sha512digest",F_SHA512, NEEDVALUE},
80 {"size", F_SIZE, NEEDVALUE},
81 {"tags", F_TAGS, NEEDVALUE},
82 {"time", F_TIME, NEEDVALUE},
83 {"type", F_TYPE, NEEDVALUE},
84 {"uid", F_UID, NEEDVALUE},
85 {"uname", F_UNAME, NEEDVALUE}
86 };
87
88 static KEY typelist[] = {
89 {"block", F_BLOCK, 0},
90 {"char", F_CHAR, 0},
91 {"dir", F_DIR, 0},
92 #ifdef S_IFDOOR
93 {"door", F_DOOR, 0},
94 #endif
95 {"fifo", F_FIFO, 0},
96 {"file", F_FILE, 0},
97 {"link", F_LINK, 0},
98 {"socket", F_SOCK, 0},
99 };
100
101 slist_t excludetags, includetags;
102 int keys = KEYDEFAULT;
103
104
105 int keycompare(const void *, const void *);
106
107 u_int
parsekey(const char * name,int * needvaluep)108 parsekey(const char *name, int *needvaluep)
109 {
110 static int allbits;
111 KEY *k, tmp;
112
113 if (allbits == 0) {
114 size_t i;
115
116 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
117 allbits |= keylist[i].val;
118 }
119 tmp.name = name;
120 if (strcmp(name, "all") == 0)
121 return (allbits);
122 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
123 sizeof(KEY), keycompare);
124 if (k == NULL)
125 mtree_err("unknown keyword `%s'", name);
126
127 if (needvaluep)
128 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
129
130 return (k->val);
131 }
132
133 u_int
parsetype(const char * name)134 parsetype(const char *name)
135 {
136 KEY *k, tmp;
137
138 tmp.name = name;
139 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
140 sizeof(KEY), keycompare);
141 if (k == NULL)
142 mtree_err("unknown file type `%s'", name);
143
144 return (k->val);
145 }
146
147 int
keycompare(const void * a,const void * b)148 keycompare(const void *a, const void *b)
149 {
150
151 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
152 }
153
154 void
mtree_err(const char * fmt,...)155 mtree_err(const char *fmt, ...)
156 {
157 va_list ap;
158
159 va_start(ap, fmt);
160 vwarnx(fmt, ap);
161 va_end(ap);
162 if (mtree_lineno)
163 warnx("failed at line %lu of the specification",
164 (u_long) mtree_lineno);
165 exit(1);
166 /* NOTREACHED */
167 }
168
169 void
addtag(slist_t * list,char * elem)170 addtag(slist_t *list, char *elem)
171 {
172
173 #define TAG_CHUNK 20
174
175 if ((list->count % TAG_CHUNK) == 0) {
176 char **new;
177
178 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
179 * sizeof(char *));
180 if (new == NULL)
181 mtree_err("memory allocation error");
182 list->list = new;
183 }
184 list->list[list->count] = elem;
185 list->count++;
186 }
187
188 void
parsetags(slist_t * list,char * args)189 parsetags(slist_t *list, char *args)
190 {
191 char *p, *e;
192 int len;
193
194 if (args == NULL) {
195 addtag(list, NULL);
196 return;
197 }
198 while ((p = strsep(&args, ",")) != NULL) {
199 if (*p == '\0')
200 continue;
201 len = strlen(p) + 3; /* "," + p + ",\0" */
202 if ((e = malloc(len)) == NULL)
203 mtree_err("memory allocation error");
204 snprintf(e, len, ",%s,", p);
205 addtag(list, e);
206 }
207 }
208
209 /*
210 * matchtags
211 * returns 0 if there's a match from the exclude list in the node's tags,
212 * or there's an include list and no match.
213 * return 1 otherwise.
214 */
215 int
matchtags(NODE * node)216 matchtags(NODE *node)
217 {
218 int i;
219
220 if (node->tags) {
221 for (i = 0; i < excludetags.count; i++)
222 if (strstr(node->tags, excludetags.list[i]))
223 break;
224 if (i < excludetags.count)
225 return (0);
226
227 for (i = 0; i < includetags.count; i++)
228 if (strstr(node->tags, includetags.list[i]))
229 break;
230 if (i > 0 && i == includetags.count)
231 return (0);
232 } else if (includetags.count > 0) {
233 return (0);
234 }
235 return (1);
236 }
237
238 u_int
nodetoino(u_int type)239 nodetoino(u_int type)
240 {
241
242 switch (type) {
243 case F_BLOCK:
244 return S_IFBLK;
245 case F_CHAR:
246 return S_IFCHR;
247 case F_DIR:
248 return S_IFDIR;
249 case F_FIFO:
250 return S_IFIFO;
251 case F_FILE:
252 return S_IFREG;
253 case F_LINK:
254 return S_IFLNK;
255 #ifdef S_IFSOCK
256 case F_SOCK:
257 return S_IFSOCK;
258 #endif
259 default:
260 printf("unknown type %d", type);
261 abort();
262 }
263 /* NOTREACHED */
264 }
265
266 const char *
nodetype(u_int type)267 nodetype(u_int type)
268 {
269
270 return (inotype(nodetoino(type)));
271 }
272
273
274 const char *
inotype(u_int type)275 inotype(u_int type)
276 {
277
278 switch (type & S_IFMT) {
279 case S_IFBLK:
280 return ("block");
281 case S_IFCHR:
282 return ("char");
283 case S_IFDIR:
284 return ("dir");
285 case S_IFIFO:
286 return ("fifo");
287 case S_IFREG:
288 return ("file");
289 case S_IFLNK:
290 return ("link");
291 #ifdef S_IFSOCK
292 case S_IFSOCK:
293 return ("socket");
294 #endif
295 #ifdef S_IFDOOR
296 case S_IFDOOR:
297 return ("door");
298 #endif
299 default:
300 return ("unknown");
301 }
302 /* NOTREACHED */
303 }
304