1 /* $NetBSD: services_mkdb.c,v 1.14 2008/04/28 20:24:17 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Luke Mewburn and Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 38 #include <assert.h> 39 #include <db.h> 40 #include <err.h> 41 #include <fcntl.h> 42 #include <netdb.h> 43 #define _WITH_GETLINE 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <libgen.h> 49 #include <ctype.h> 50 #include <errno.h> 51 #include <stringlist.h> 52 53 #include "extern.h" 54 55 static char tname[MAXPATHLEN]; 56 57 #define PMASK 0xffff 58 #define PROTOMAX 5 59 60 static void add(DB *, StringList *, size_t, const char *, size_t *, int); 61 static StringList ***parseservices(const char *, StringList *); 62 static void cleanup(void); 63 static void store(DB *, DBT *, DBT *, int); 64 static void killproto(DBT *); 65 static char *getstring(const char *, size_t, char **, const char *); 66 static size_t getprotoindex(StringList *, const char *); 67 static const char *getprotostr(StringList *, size_t); 68 static const char *mkaliases(StringList *, char *, size_t); 69 static void usage(void); 70 71 HASHINFO hinfo = { 72 .bsize = 256, 73 .ffactor = 4, 74 .nelem = 32768, 75 .cachesize = 1024, 76 .hash = NULL, 77 .lorder = 0 78 }; 79 80 81 int 82 main(int argc, char *argv[]) 83 { 84 DB *db; 85 int ch; 86 const char *fname = _PATH_SERVICES; 87 const char *dbname = _PATH_SERVICES_DB; 88 int warndup = 1; 89 int unique = 0; 90 int otherflag = 0; 91 int byteorder = 0; 92 size_t cnt = 0; 93 StringList *sl, ***svc; 94 size_t port, proto; 95 char *dbname_dir, *dbname_dirbuf; 96 int dbname_dir_fd = -1; 97 98 setprogname(argv[0]); 99 100 while ((ch = getopt(argc, argv, "blo:qu")) != -1) 101 switch (ch) { 102 case 'b': 103 case 'l': 104 if (byteorder != 0) 105 usage(); 106 byteorder = ch == 'b' ? 4321 : 1234; 107 break; 108 case 'q': 109 otherflag = 1; 110 warndup = 0; 111 break; 112 case 'o': 113 otherflag = 1; 114 dbname = optarg; 115 break; 116 case 'u': 117 unique++; 118 break; 119 case '?': 120 default: 121 usage(); 122 } 123 124 argc -= optind; 125 argv += optind; 126 127 if (argc > 1 || (unique && otherflag)) 128 usage(); 129 if (argc == 1) 130 fname = argv[0]; 131 132 /* Set byte order. */ 133 hinfo.lorder = byteorder; 134 135 if (unique) 136 uniq(fname); 137 138 svc = parseservices(fname, sl = sl_init()); 139 140 if (atexit(cleanup)) 141 err(1, "Cannot install exit handler"); 142 143 (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname); 144 db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL, 145 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo); 146 if (!db) 147 err(1, "Error opening temporary database `%s'", tname); 148 149 150 for (port = 0; port < PMASK + 1; port++) { 151 if (svc[port] == NULL) 152 continue; 153 154 for (proto = 0; proto < PROTOMAX; proto++) { 155 StringList *s; 156 if ((s = svc[port][proto]) == NULL) 157 continue; 158 add(db, s, port, getprotostr(sl, proto), &cnt, warndup); 159 } 160 161 free(svc[port]); 162 } 163 164 free(svc); 165 sl_free(sl, 1); 166 167 if ((db->close)(db)) 168 err(1, "Error closing temporary database `%s'", tname); 169 170 /* 171 * Make sure file is safe on disk. To improve performance we will call 172 * fsync() to the directory where file lies 173 */ 174 if (rename(tname, dbname) == -1 || 175 (dbname_dirbuf = strdup(dbname)) == NULL || 176 (dbname_dir = dirname(dbname_dirbuf)) == NULL || 177 (dbname_dir_fd = open(dbname_dir, O_RDONLY|O_DIRECTORY)) == -1 || 178 fsync(dbname_dir_fd) != 0) { 179 if (dbname_dir_fd != -1) 180 close(dbname_dir_fd); 181 err(1, "Cannot rename `%s' to `%s'", tname, dbname); 182 } 183 184 if (dbname_dir_fd != -1) 185 close(dbname_dir_fd); 186 187 return 0; 188 } 189 190 static void 191 add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt, 192 int warndup) 193 { 194 size_t i; 195 char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ]; 196 DBT data, key; 197 key.data = keyb; 198 data.data = datab; 199 200 #ifdef DEBUG 201 (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto); 202 for (i = 1; i < sl->sl_cur; i++) 203 (void)printf("%s ", sl->sl_str[i]); 204 (void)printf("]\n"); 205 #endif 206 207 /* key `indirect key', data `full line' */ 208 data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1; 209 key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s", 210 sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1; 211 store(db, &data, &key, warndup); 212 213 /* key `\377port/proto', data = `indirect key' */ 214 key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s", 215 port, proto) + 1; 216 store(db, &key, &data, warndup); 217 218 /* key `\377port', data = `indirect key' */ 219 killproto(&key); 220 store(db, &key, &data, warndup); 221 222 /* add references for service and all aliases */ 223 for (i = 0; i < sl->sl_cur; i++) { 224 /* key `\376service/proto', data = `indirect key' */ 225 key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s", 226 sl->sl_str[i], proto) + 1; 227 store(db, &key, &data, warndup); 228 229 /* key `\376service', data = `indirect key' */ 230 killproto(&key); 231 store(db, &key, &data, warndup); 232 } 233 sl_free(sl, 1); 234 } 235 236 static StringList *** 237 parseservices(const char *fname, StringList *sl) 238 { 239 ssize_t len; 240 size_t linecap, line, pindex; 241 FILE *fp; 242 StringList ***svc, *s; 243 char *p, *ep; 244 245 if ((fp = fopen(fname, "r")) == NULL) 246 err(1, "Cannot open `%s'", fname); 247 248 line = linecap = 0; 249 if ((svc = calloc(PMASK + 1, sizeof(StringList **))) == NULL) 250 err(1, "Cannot allocate %zu bytes", (size_t)(PMASK + 1)); 251 252 p = NULL; 253 while ((len = getline(&p, &linecap, fp)) != -1) { 254 char *name, *port, *proto, *aliases, *cp, *alias; 255 unsigned long pnum; 256 257 line++; 258 259 if (len == 0) 260 continue; 261 262 if (p[len - 1] == '\n') 263 p[len - 1] = '\0'; 264 265 for (cp = p; *cp && isspace((unsigned char)*cp); cp++) 266 continue; 267 268 if (*cp == '\0' || *cp == '#') 269 continue; 270 271 if ((name = getstring(fname, line, &cp, "name")) == NULL) 272 continue; 273 274 if ((port = getstring(fname, line, &cp, "port")) == NULL) 275 continue; 276 277 if (cp) { 278 for (aliases = cp; *cp && *cp != '#'; cp++) 279 continue; 280 281 if (*cp) 282 *cp = '\0'; 283 } else 284 aliases = NULL; 285 286 proto = strchr(port, '/'); 287 if (proto == NULL || proto[1] == '\0') { 288 warnx("%s, %zu: no protocol found", fname, line); 289 continue; 290 } 291 *proto++ = '\0'; 292 293 errno = 0; 294 pnum = strtoul(port, &ep, 0); 295 if (*port == '\0' || *ep != '\0') { 296 warnx("%s, %zu: invalid port `%s'", fname, line, port); 297 continue; 298 } 299 if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) { 300 warnx("%s, %zu: port too big `%s'", fname, line, port); 301 continue; 302 } 303 304 if (svc[pnum] == NULL) { 305 svc[pnum] = calloc(PROTOMAX, sizeof(StringList *)); 306 if (svc[pnum] == NULL) 307 err(1, "Cannot allocate %zu bytes", 308 (size_t)PROTOMAX); 309 } 310 311 pindex = getprotoindex(sl, proto); 312 if (svc[pnum][pindex] == NULL) 313 s = svc[pnum][pindex] = sl_init(); 314 else 315 s = svc[pnum][pindex]; 316 317 /* build list of aliases */ 318 if (sl_find(s, name) == NULL) { 319 char *p2; 320 321 if ((p2 = strdup(name)) == NULL) 322 err(1, "Cannot copy string"); 323 (void)sl_add(s, p2); 324 } 325 326 if (aliases) { 327 while ((alias = strsep(&aliases, " \t")) != NULL) { 328 if (alias[0] == '\0') 329 continue; 330 if (sl_find(s, alias) == NULL) { 331 char *p2; 332 333 if ((p2 = strdup(alias)) == NULL) 334 err(1, "Cannot copy string"); 335 (void)sl_add(s, p2); 336 } 337 } 338 } 339 } 340 (void)fclose(fp); 341 return svc; 342 } 343 344 /* 345 * cleanup(): Remove temporary files upon exit 346 */ 347 static void 348 cleanup(void) 349 { 350 if (tname[0]) 351 (void)unlink(tname); 352 } 353 354 static char * 355 getstring(const char *fname, size_t line, char **cp, const char *tag) 356 { 357 char *str; 358 359 while ((str = strsep(cp, " \t")) != NULL && *str == '\0') 360 continue; 361 362 if (str == NULL) 363 warnx("%s, %zu: no %s found", fname, line, tag); 364 365 return str; 366 } 367 368 static void 369 killproto(DBT *key) 370 { 371 char *p, *d = key->data; 372 373 if ((p = strchr(d, '/')) == NULL) 374 abort(); 375 *p++ = '\0'; 376 key->size = p - d; 377 } 378 379 static void 380 store(DB *db, DBT *key, DBT *data, int warndup) 381 { 382 #ifdef DEBUG 383 int k = key->size - 1; 384 int d = data->size - 1; 385 (void)printf("store [%*.*s] [%*.*s]\n", 386 k, k, (char *)key->data + 1, 387 d, d, (char *)data->data + 1); 388 #endif 389 switch ((db->put)(db, key, data, R_NOOVERWRITE)) { 390 case 0: 391 break; 392 case 1: 393 if (warndup) 394 warnx("duplicate service `%s'", 395 &((char *)key->data)[1]); 396 break; 397 case -1: 398 err(1, "put"); 399 break; 400 default: 401 abort(); 402 break; 403 } 404 } 405 406 static size_t 407 getprotoindex(StringList *sl, const char *str) 408 { 409 size_t i; 410 char *p; 411 412 for (i= 0; i < sl->sl_cur; i++) 413 if (strcmp(sl->sl_str[i], str) == 0) 414 return i; 415 416 if (i == PROTOMAX) 417 errx(1, "Ran out of protocols adding `%s';" 418 " recompile with larger PROTOMAX", str); 419 if ((p = strdup(str)) == NULL) 420 err(1, "Cannot copy string"); 421 (void)sl_add(sl, p); 422 return i; 423 } 424 425 static const char * 426 getprotostr(StringList *sl, size_t i) 427 { 428 assert(i < sl->sl_cur); 429 return sl->sl_str[i]; 430 } 431 432 static const char * 433 mkaliases(StringList *sl, char *buf, size_t len) 434 { 435 size_t nc, i, pos; 436 437 buf[0] = 0; 438 for (i = 1, pos = 0; i < sl->sl_cur; i++) { 439 nc = strlcpy(buf + pos, sl->sl_str[i], len); 440 if (nc >= len) 441 goto out; 442 pos += nc; 443 len -= nc; 444 nc = strlcpy(buf + pos, " ", len); 445 if (nc >= len) 446 goto out; 447 pos += nc; 448 len -= nc; 449 } 450 return buf; 451 out: 452 warn("aliases for `%s' truncated", sl->sl_str[0]); 453 return buf; 454 } 455 456 static void 457 usage(void) 458 { 459 (void)fprintf(stderr, 460 "Usage:\t%s [-b | -l] [-q] [-o <db>] [<servicefile>]\n" 461 "\t%s -u [<servicefile>]\n", getprogname(), getprogname()); 462 exit(1); 463 } 464