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