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