xref: /dragonfly/usr.bin/cap_mkdb/cap_mkdb.c (revision 279dd846)
1 /*-
2  * Copyright (c) 1992, 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  * @(#) Copyright (c) 1992, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)cap_mkdb.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/cap_mkdb/cap_mkdb.c,v 1.14 2005/02/22 23:29:53 ru Exp $
32  * $DragonFly: src/usr.bin/cap_mkdb/cap_mkdb.c,v 1.5 2007/09/25 04:53:48 pavalos Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 
38 #include <db.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 void	 db_build(char **);
47 void	 dounlink(void);
48 void	 usage(void);
49 
50 DB *capdbp;
51 int verbose;
52 char *capdb, *capname, buf[8 * 1024];
53 
54 HASHINFO openinfo = {
55 	4096,		/* bsize */
56 	0,		/* ffactor */
57 	0,		/* nelem */
58 	0,		/* cachesize */
59 	NULL,		/* hash() */
60 	0		/* lorder */
61 };
62 
63 /*
64  * Mkcapdb creates a capability hash database for quick retrieval of capability
65  * records.  The database contains 2 types of entries: records and references
66  * marked by the first byte in the data.  A record entry contains the actual
67  * capability record whereas a reference contains the name (key) under which
68  * the correct record is stored.
69  */
70 int
71 main(int argc, char **argv)
72 {
73 	int byteorder, c;
74 
75 	capname = NULL;
76 	byteorder = 0;
77 	while ((c = getopt(argc, argv, "bf:lv")) != -1) {
78 		switch(c) {
79 		case 'b':
80 		case 'l':
81 			if (byteorder != 0)
82 				usage();
83 			byteorder = c == 'b' ? 4321 : 1234;
84 			break;
85 		case 'f':
86 			capname = optarg;
87 			break;
88 		case 'v':
89 			verbose = 1;
90 			break;
91 		case '?':
92 		default:
93 			usage();
94 		}
95 	}
96 	argc -= optind;
97 	argv += optind;
98 
99 	if (*argv == NULL)
100 		usage();
101 
102 	/* Set byte order. */
103 	openinfo.lorder = byteorder;
104 
105 	/*
106 	 * The database file is the first argument if no name is specified.
107 	 * Make arrangements to unlink it if exit badly.
108 	 */
109 	(void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
110 	if ((capname = strdup(buf)) == NULL)
111 		errx(1, "strdup failed");
112 	if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
113 	    DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
114 		err(1, "%s", buf);
115 
116 	if (atexit(dounlink))
117 		err(1, "atexit");
118 
119 	db_build(argv);
120 
121 	if (capdbp->close(capdbp) < 0)
122 		err(1, "%s", capname);
123 	capname = NULL;
124 	exit(0);
125 }
126 
127 void
128 dounlink(void)
129 {
130 	if (capname != NULL)
131 		(void)unlink(capname);
132 }
133 
134 /*
135  * Any changes to these definitions should be made also in the getcap(3)
136  * library routines.
137  */
138 #define RECOK	(char)0
139 #define TCERR	(char)1
140 #define SHADOW	(char)2
141 
142 /*
143  * Db_build() builds the name and capability databases according to the
144  * details above.
145  */
146 void
147 db_build(char **ifiles)
148 {
149 	DBT key, data;
150 	recno_t reccnt;
151 	size_t len, bplen;
152 	int st;
153 	char *bp, *p, *t;
154 
155 	data.data = NULL;
156 	key.data = NULL;
157 	for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
158 
159 		/*
160 		 * Allocate enough memory to store record, terminating
161 		 * NULL and one extra byte.
162 		 */
163 		len = strlen(bp);
164 		if (bplen <= len + 2) {
165 			bplen += MAX(256, len + 2);
166 			if ((data.data = realloc(data.data, bplen)) == NULL)
167 				errx(1, "malloc failed");
168 		}
169 
170 		/* Find the end of the name field. */
171 		if ((p = strchr(bp, ':')) == NULL) {
172 			warnx("no name field: %.*s", (int)MIN(len, 20), bp);
173 			continue;
174 		}
175 
176 		/* First byte of stored record indicates status. */
177 		switch(st) {
178 		case 1:
179 			((char *)(data.data))[0] = RECOK;
180 			break;
181 		case 2:
182 			((char *)(data.data))[0] = TCERR;
183 			warnx("record not tc expanded: %.*s", (int)(p - bp),
184 			    bp);
185 			break;
186 		}
187 
188 		/* Create the stored record. */
189 		memmove(&((u_char *)(data.data))[1], bp, len + 1);
190 		data.size = len + 2;
191 
192 		/* Store the record under the name field. */
193 		key.data = bp;
194 		key.size = p - bp;
195 
196 		switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
197 		case -1:
198 			err(1, "put");
199 			/* NOTREACHED */
200 		case 1:
201 			warnx("ignored duplicate: %.*s",
202 			    (int)key.size, (char *)key.data);
203 			continue;
204 		}
205 		++reccnt;
206 
207 		/* If only one name, ignore the rest. */
208 		*p = '\0';
209 		if (strchr(bp, '|') == NULL)
210 			continue;
211 		*p = ':';
212 
213 		/* The rest of the names reference the entire name. */
214 		((char *)(data.data))[0] = SHADOW;
215 		memmove(&((u_char *)(data.data))[1], key.data, key.size);
216 		data.size = key.size + 1;
217 
218 		/* Store references for other names. */
219 		for (p = t = bp;; ++p) {
220 			if (p > t && (*p == ':' || *p == '|')) {
221 				key.size = p - t;
222 				key.data = t;
223 				switch(capdbp->put(capdbp,
224 				    &key, &data, R_NOOVERWRITE)) {
225 				case -1:
226 					err(1, "put");
227 					/* NOTREACHED */
228 				case 1:
229 					warnx("ignored duplicate: %.*s",
230 					    (int)key.size, (char *)key.data);
231 				}
232 				t = p + 1;
233 			}
234 			if (*p == ':')
235 				break;
236 		}
237 	}
238 
239 	switch(st) {
240 	case -1:
241 		err(1, "file argument");
242 		/* NOTREACHED */
243 	case -2:
244 		errx(1, "potential reference loop detected");
245 		/* NOTREACHED */
246 	}
247 
248 	if (verbose)
249 		(void)printf("cap_mkdb: %d capability records\n", reccnt);
250 }
251 
252 void
253 usage(void)
254 {
255 	(void)fprintf(stderr,
256 	    "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");
257 	exit(1);
258 }
259