xref: /minix/usr.sbin/services_mkdb/output_db.c (revision 08cbf5a0)
1 /*	$NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg 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 #ifndef lint
34 __RCSID("$NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg Exp $");
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 
39 #include <assert.h>
40 #include <db.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <util.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <stringlist.h>
52 
53 #include "extern.h"
54 
55 static DB *db;
56 
57 static const HASHINFO hinfo = {
58 	.bsize = 256,
59 	.ffactor = 4,
60 	.nelem = 32768,
61 	.cachesize = 1024,
62 	.hash = NULL,
63 	.lorder = 0
64 };
65 
66 static void	store(DBT *, DBT *, int);
67 static void	killproto(DBT *);
68 static const char *mkaliases(StringList *, char *, size_t);
69 
70 int
71 db_open(const char *tname)
72 {
73 	db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
74 	    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
75 
76 	return db != NULL ? 0 : -1;
77 }
78 
79 int
80 db_close(void)
81 {
82 	int rv;
83 
84 	rv = (db->close)(db);
85 	db = NULL;
86 
87 	return rv;
88 }
89 
90 void
91 db_add(StringList *sl, size_t port, const char *proto, size_t *cnt,
92     int warndup)
93 {
94 	size_t i;
95 	char	 keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
96 	DBT	 data, key;
97 	key.data = keyb;
98 	data.data = datab;
99 
100 	/* key `indirect key', data `full line' */
101 	data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
102 	key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
103 	    sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
104 	store(&data, &key, warndup);
105 
106 	/* key `\377port/proto', data = `indirect key' */
107 	key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
108 	    port, proto) + 1;
109 	store(&key, &data, warndup);
110 
111 	/* key `\377port', data = `indirect key' */
112 	killproto(&key);
113 	store(&key, &data, warndup);
114 
115 	/* add references for service and all aliases */
116 	for (i = 0; i < sl->sl_cur; i++) {
117 		/* key `\376service/proto', data = `indirect key' */
118 		key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
119 		    sl->sl_str[i], proto) + 1;
120 		store(&key, &data, warndup);
121 
122 		/* key `\376service', data = `indirect key' */
123 		killproto(&key);
124 		store(&key, &data, warndup);
125 	}
126 	sl_free(sl, 1);
127 }
128 
129 static void
130 killproto(DBT *key)
131 {
132 	char *p, *d = key->data;
133 
134 	if ((p = strchr(d, '/')) == NULL)
135 		abort();
136 	*p++ = '\0';
137 	key->size = p - d;
138 }
139 
140 static void
141 store(DBT *key, DBT *data, int warndup)
142 {
143 #ifdef DEBUG
144 	int k = key->size - 1;
145 	int d = data->size - 1;
146 	(void)printf("store [%*.*s] [%*.*s]\n",
147 		k, k, (char *)key->data + 1,
148 		d, d, (char *)data->data + 1);
149 #endif
150 	switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
151 	case 0:
152 		break;
153 	case 1:
154 		if (warndup)
155 			warnx("duplicate service `%s'",
156 			    &((char *)key->data)[1]);
157 		break;
158 	case -1:
159 		err(1, "put");
160 		break;
161 	default:
162 		abort();
163 		break;
164 	}
165 }
166 
167 static const char *
168 mkaliases(StringList *sl, char *buf, size_t len)
169 {
170 	size_t nc, i, pos;
171 
172 	buf[0] = 0;
173 	for (i = 1, pos = 0; i < sl->sl_cur; i++) {
174 		nc = strlcpy(buf + pos, sl->sl_str[i], len);
175 		if (nc >= len)
176 			goto out;
177 		pos += nc;
178 		len -= nc;
179 		nc = strlcpy(buf + pos, " ", len);
180 		if (nc >= len)
181 			goto out;
182 		pos += nc;
183 		len -= nc;
184 	}
185 	return buf;
186 out:
187 	warn("aliases for `%s' truncated", sl->sl_str[0]);
188 	return buf;
189 }
190