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