mk-amd-map.c revision 119682
1/* 2 * Copyright (c) 1997-2003 Erez Zadok 3 * Copyright (c) 1990 Jan-Simon Pendry 4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry at Imperial College, London. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgment: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * %W% (Berkeley) %G% 40 * 41 * $Id: mk-amd-map.c,v 1.5.2.5 2003/06/09 16:57:46 ezk Exp $ 42 * $FreeBSD: head/contrib/amd/mk-amd-map/mk-amd-map.c 119682 2003-09-02 15:42:57Z mbr $ 43 */ 44 45/* 46 * Convert a file map into an ndbm map 47 */ 48 49#ifdef HAVE_CONFIG_H 50# include <config.h> 51#endif /* HAVE_CONFIG_H */ 52#include <am_defs.h> 53 54/* (libdb version 2) uses .db extensions but an old dbm API */ 55/* check for libgdbm to distinguish it from linux systems */ 56#if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) 57# define HAVE_DB_SUFFIX 58#endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */ 59 60#ifdef HAVE_MAP_NDBM 61 62static int 63store_data(voidp db, char *k, char *v) 64{ 65 datum key, val; 66 67 key.dptr = k; 68 val.dptr = v; 69 key.dsize = strlen(k) + 1; 70 val.dsize = strlen(v) + 1; 71 return dbm_store((DBM *) db, key, val, DBM_INSERT); 72} 73 74 75/* 76 * Read one line from file. 77 */ 78static int 79read_line(char *buf, int size, FILE *fp) 80{ 81 int done = 0; 82 83 do { 84 while (fgets(buf, size, fp)) { 85 int len = strlen(buf); 86 87 done += len; 88 if (len > 1 && buf[len - 2] == '\\' && buf[len - 1] == '\n') { 89 int ch; 90 buf += len - 2; 91 size -= len - 2; 92 *buf = '\n'; 93 buf[1] = '\0'; 94 95 /* 96 * Skip leading white space on next line 97 */ 98 while ((ch = getc(fp)) != EOF && isascii(ch) && isspace(ch)) ; 99 (void) ungetc(ch, fp); 100 } else { 101 return done; 102 } 103 } 104 } while (size > 0 && !feof(fp)); 105 106 return done; 107} 108 109 110/* 111 * Read through a map. 112 */ 113static int 114read_file(FILE *fp, char *map, voidp db) 115{ 116 char key_val[2048]; 117 int chuck = 0; 118 int line_no = 0; 119 int errs = 0; 120 121 while (read_line(key_val, 2048, fp)) { 122 char *kp; 123 char *cp; 124 char *hash; 125 int len = strlen(key_val); 126 127 line_no++; 128 129 /* 130 * Make sure we got the whole line 131 */ 132 if (key_val[len - 1] != '\n') { 133 fprintf(stderr, "line %d in \"%s\" is too long", line_no, map); 134 chuck = 1; 135 } else { 136 key_val[len - 1] = '\0'; 137 } 138 139 /* 140 * Strip comments 141 */ 142 hash = strchr(key_val, '#'); 143 if (hash) 144 *hash = '\0'; 145 146 /* 147 * Find start of key 148 */ 149 for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ; 150 151 /* 152 * Ignore blank lines 153 */ 154 if (!*kp) 155 goto again; 156 157 /* 158 * Find end of key 159 */ 160 for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ; 161 162 /* 163 * Check whether key matches, or whether 164 * the entry is a wildcard entry. 165 */ 166 if (*cp) 167 *cp++ = '\0'; 168 while (*cp && isascii(*cp) && isspace((int)*cp)) 169 cp++; 170 if (*kp == '+') { 171 fprintf(stderr, "Can't interpolate %s\n", kp); 172 errs++; 173 } else if (*cp) { 174 if (db) { 175 if (store_data(db, kp, cp) < 0) { 176 fprintf(stderr, "Could store %s -> %s\n", kp, cp); 177 errs++; 178 } 179 } else { 180 printf("%s\t%s\n", kp, cp); 181 } 182 } else { 183 fprintf(stderr, "%s: line %d has no value field", map, line_no); 184 errs++; 185 } 186 187 again: 188 /* 189 * If the last read didn't get a whole line then 190 * throw away the remainder before continuing... 191 */ 192 if (chuck) { 193 while (fgets(key_val, sizeof(key_val), fp) && 194 !strchr(key_val, '\n')) ; 195 chuck = 0; 196 } 197 } 198 return errs; 199} 200 201 202static int 203remove_file(char *f) 204{ 205 if (unlink(f) < 0 && errno != ENOENT) 206 return -1; 207 208 return 0; 209} 210 211 212int 213main(int argc, char *argv[]) 214{ 215 FILE *mapf; /* the input file to read from */ 216 int error; 217 char *mapsrc; 218 DBM *db = NULL; 219 static char maptmp[] = "dbmXXXXXX"; 220#ifdef HAVE_DB_SUFFIX 221 char maptdb[16]; 222 char *map_name_db = (char *) NULL; 223#else /* not HAVE_DB_SUFFIX */ 224 char maptpag[16], maptdir[16]; 225 char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL; 226#endif /* not HAVE_DB_SUFFIX */ 227 int len; 228 char *sl; 229 int printit = 0; 230 int usage = 0; 231 int ch; 232 extern int optind; 233 234 /* test options */ 235 while ((ch = getopt(argc, argv, "p")) != -1) 236 switch (ch) { 237 case 'p': 238 printit = 1; 239 break; 240 default: 241 usage++; 242 break; 243 } 244 245 if (usage || optind != (argc - 1)) { 246 fputs("Usage: mk-amd-map [-p] file-map\n", stderr); 247 exit(1); 248 } 249 mapsrc = argv[optind]; 250 251 /* test if can get to the map directory */ 252 sl = strrchr(mapsrc, '/'); 253 if (sl) { 254 *sl = '\0'; 255 if (chdir(mapsrc) < 0) { 256 fputs("Can't chdir to ", stderr); 257 perror(mapsrc); 258 exit(1); 259 } 260 mapsrc = sl + 1; 261 } 262 263 /* open source file */ 264 mapf = fopen(mapsrc, "r"); 265 if (!mapf) { 266 fprintf(stderr, "cannot open source file "); 267 perror(mapsrc); 268 exit(1); 269 } 270 271#ifndef DEBUG 272 signal(SIGINT, SIG_IGN); 273#endif /* DEBUG */ 274 275 if (!printit) { 276 len = strlen(mapsrc); 277#ifdef HAVE_DB_SUFFIX 278 map_name_db = (char *) malloc(len + 4); 279 error = (map_name_db == NULL); 280#else /* not HAVE_DB_SUFFIX */ 281 map_name_pag = (char *) malloc(len + 5); 282 map_name_dir = (char *) malloc(len + 5); 283 error = (map_name_pag == NULL || map_name_dir == NULL); 284#endif /* not HAVE_DB_SUFFIX */ 285 if (error) { 286 perror("mk-amd-map: malloc"); 287 exit(1); 288 } 289 290#ifdef HAVE_MKSTEMP 291 { 292 /* 293 * XXX: hack to avoid compiler complaints about mktemp not being 294 * secure, since we have to do a dbm_open on this anyway. So use 295 * mkstemp if you can, and then close the fd, but we get a safe 296 * and unique file name. 297 */ 298 int dummyfd; 299 dummyfd = mkstemp(maptmp); 300 if (dummyfd >= 0) 301 close(dummyfd); 302 } 303#else /* not HAVE_MKSTEMP */ 304 mktemp(maptmp); 305#endif /* not HAVE_MKSTEMP */ 306 307 /* remove existing temps (if any) */ 308#ifdef HAVE_DB_SUFFIX 309 sprintf(maptdb, "%s.db", maptmp); 310 if (remove_file(maptdb) < 0) { 311 fprintf(stderr, "Can't remove existing temporary file; "); 312 perror(maptdb); 313 exit(1); 314 } 315#else /* not HAVE_DB_SUFFIX */ 316 sprintf(maptpag, "%s.pag", maptmp); 317 sprintf(maptdir, "%s.dir", maptmp); 318 if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) { 319 fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag); 320 perror(maptdir); 321 exit(1); 322 } 323#endif /* not HAVE_DB_SUFFIX */ 324 325 db = dbm_open(maptmp, O_RDWR|O_CREAT|O_EXCL, 0444); 326 if (!db) { 327 fprintf(stderr, "cannot initialize temporary database: %s", maptmp); 328 exit(1); 329 } 330 } 331 332 /* print db to stdout or to temp database */ 333 error = read_file(mapf, mapsrc, db); 334 fclose(mapf); 335 if (error) { 336 if (printit) 337 fprintf(stderr, "Error reading source file %s\n", mapsrc); 338 else 339 fprintf(stderr, "Error creating database map for %s\n", mapsrc); 340 exit(1); 341 } 342 343 if (printit) 344 exit(0); /* nothing more to do */ 345 346 /* if gets here, we wrote to a database */ 347 348 dbm_close(db); 349 /* all went well */ 350 351#ifdef HAVE_DB_SUFFIX 352 sprintf(map_name_db, "%s.db", mapsrc); 353 if (rename(maptdb, map_name_db) < 0) { 354 fprintf(stderr, "Couldn't rename %s to ", maptdb); 355 perror(map_name_db); 356 /* Throw away the temporary map */ 357 unlink(maptdb); 358 exit(1); 359 } 360#else /* not HAVE_DB_SUFFIX */ 361 sprintf(map_name_pag, "%s.pag", mapsrc); 362 sprintf(map_name_dir, "%s.dir", mapsrc); 363 if (rename(maptpag, map_name_pag) < 0) { 364 fprintf(stderr, "Couldn't rename %s to ", maptpag); 365 perror(map_name_pag); 366 /* Throw away the temporary map */ 367 unlink(maptpag); 368 unlink(maptdir); 369 exit(1); 370 } 371 if (rename(maptdir, map_name_dir) < 0) { 372 fprintf(stderr, "Couldn't rename %s to ", maptdir); 373 perror(map_name_dir); 374 /* remove the (presumably bad) .pag file */ 375 unlink(map_name_pag); 376 /* throw away remaining part of original map */ 377 unlink(map_name_dir); 378 /* throw away the temporary map */ 379 unlink(maptdir); 380 fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n", 381 mapsrc); 382 exit(1); 383 } 384#endif /* not HAVE_DB_SUFFIX */ 385 386 exit(0); 387} 388 389#else /* not HAVE_MAP_NDBM */ 390 391int 392main() 393{ 394 fputs("mk-amd-map: This system does not support hashed database files\n", stderr); 395 exit(1); 396} 397 398#endif /* not HAVE_MAP_NDBM */ 399