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