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