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