ypdb.c revision 1.13 1 1.13 mrg /* $NetBSD: ypdb.c,v 1.13 2023/08/01 08:47:25 mrg Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1990, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej * All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * This code is derived from software contributed to Berkeley by
9 1.1 thorpej * Margo Seltzer.
10 1.1 thorpej *
11 1.1 thorpej * This code is derived from ndbm module of BSD4.4 db (hash) by
12 1.1 thorpej * Mats O Jansson
13 1.1 thorpej *
14 1.1 thorpej * Redistribution and use in source and binary forms, with or without
15 1.1 thorpej * modification, are permitted provided that the following conditions
16 1.1 thorpej * are met:
17 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.1 thorpej * notice, this list of conditions and the following disclaimer.
19 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.1 thorpej * documentation and/or other materials provided with the distribution.
22 1.9 agc * 3. Neither the name of the University nor the names of its contributors
23 1.1 thorpej * may be used to endorse or promote products derived from this software
24 1.1 thorpej * without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
27 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
30 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 thorpej * SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.4 lukem #include <sys/cdefs.h>
40 1.4 lukem #ifndef lint
41 1.13 mrg __RCSID("$NetBSD: ypdb.c,v 1.13 2023/08/01 08:47:25 mrg Exp $");
42 1.4 lukem #endif
43 1.4 lukem
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/types.h>
46 1.12 christos #include <sys/stat.h>
47 1.4 lukem
48 1.4 lukem #include <db.h>
49 1.10 lukem #include <err.h>
50 1.4 lukem #include <errno.h>
51 1.11 lukem #include <fcntl.h>
52 1.1 thorpej #include <stdio.h>
53 1.11 lukem #include <stdlib.h>
54 1.1 thorpej #include <string.h>
55 1.11 lukem #include <unistd.h>
56 1.1 thorpej
57 1.5 lukem #include <rpcsvc/yp.h>
58 1.5 lukem
59 1.1 thorpej #include "ypdb.h"
60 1.1 thorpej
61 1.11 lukem static DBM *_ypdb_dbopen(const char *, int, mode_t);
62 1.11 lukem
63 1.1 thorpej /*
64 1.10 lukem * ypdb_open --
65 1.11 lukem * dbopen(3) file, read-only.
66 1.10 lukem * First ensure that file has a suffix of YPDB_SUFFIX.
67 1.10 lukem * Try opening as a DB_BTREE first, then DB_HASH.
68 1.10 lukem *
69 1.1 thorpej * Returns:
70 1.11 lukem * *DBM on success
71 1.1 thorpej * NULL on failure
72 1.1 thorpej */
73 1.1 thorpej
74 1.3 lukem DBM *
75 1.11 lukem ypdb_open(const char *file)
76 1.1 thorpej {
77 1.10 lukem char path[MAXPATHLEN];
78 1.10 lukem const char *cp, *suffix;
79 1.1 thorpej
80 1.4 lukem cp = strrchr(file, '.');
81 1.10 lukem if (cp != NULL && strcmp(cp, YPDB_SUFFIX) == 0)
82 1.10 lukem suffix = "";
83 1.10 lukem else
84 1.10 lukem suffix = YPDB_SUFFIX;
85 1.13 mrg if ((size_t)snprintf(path, sizeof(path), "%s%s", file, suffix) >
86 1.13 mrg sizeof(path)) {
87 1.10 lukem warnx("File name `%s' is too long", file);
88 1.13 mrg return NULL;
89 1.10 lukem }
90 1.11 lukem return _ypdb_dbopen(path, O_RDONLY, 0444);
91 1.11 lukem }
92 1.11 lukem
93 1.11 lukem /*
94 1.11 lukem * ypdb_mktemp --
95 1.11 lukem * Create a temporary file using mkstemp(3) based on the
96 1.11 lukem * template provided in file.
97 1.11 lukem * dbopen(3) file, read-write, 0644 (modified by umask(2)).
98 1.11 lukem * Try opening as a DB_BTREE first, then DB_HASH.
99 1.11 lukem * file won't have YPDB_SUFFIX.
100 1.11 lukem *
101 1.11 lukem * Returns:
102 1.11 lukem * *DBM on success; file now exists.
103 1.11 lukem * NULL on failure
104 1.11 lukem */
105 1.11 lukem
106 1.11 lukem DBM *
107 1.11 lukem ypdb_mktemp(char *file)
108 1.11 lukem {
109 1.11 lukem int fd = -1;
110 1.11 lukem DBM *db = NULL;
111 1.11 lukem mode_t myumask;
112 1.11 lukem int save_errno;
113 1.11 lukem
114 1.11 lukem if ((fd = mkstemp(file)) == -1)
115 1.11 lukem return NULL;
116 1.11 lukem
117 1.11 lukem myumask = umask(0);
118 1.11 lukem (void)umask(myumask);
119 1.11 lukem if (fchmod(fd, 0644 & ~myumask) == -1)
120 1.11 lukem goto bad;
121 1.11 lukem
122 1.11 lukem (void) close(fd);
123 1.11 lukem fd = -1;
124 1.11 lukem
125 1.11 lukem if ((db = _ypdb_dbopen(file, O_RDWR, 0644)) == NULL)
126 1.11 lukem goto bad;
127 1.11 lukem
128 1.11 lukem return db;
129 1.11 lukem
130 1.11 lukem bad:
131 1.11 lukem save_errno = errno;
132 1.11 lukem if (fd != 1)
133 1.11 lukem (void) close(fd);
134 1.11 lukem (void) unlink(file);
135 1.11 lukem errno = save_errno;
136 1.11 lukem return NULL;
137 1.11 lukem }
138 1.11 lukem
139 1.11 lukem /*
140 1.11 lukem * _ypdb_dbopen --
141 1.11 lukem * dbopen(3) path with the flags & mode.
142 1.11 lukem * Try opening as a DB_BTREE first, then DB_HASH.
143 1.11 lukem */
144 1.11 lukem
145 1.11 lukem static DBM *
146 1.11 lukem _ypdb_dbopen(const char *path, int flags, mode_t mode)
147 1.11 lukem {
148 1.11 lukem DBM *db;
149 1.11 lukem BTREEINFO info;
150 1.4 lukem
151 1.4 lukem /* try our btree format first */
152 1.1 thorpej info.flags = 0;
153 1.1 thorpej info.cachesize = 0;
154 1.1 thorpej info.maxkeypage = 0;
155 1.1 thorpej info.minkeypage = 0;
156 1.1 thorpej info.psize = 0;
157 1.1 thorpej info.compare = NULL;
158 1.1 thorpej info.prefix = NULL;
159 1.1 thorpej info.lorder = 0;
160 1.4 lukem db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
161 1.4 lukem if (db != NULL || errno != EFTYPE)
162 1.4 lukem return (db);
163 1.4 lukem
164 1.4 lukem /* fallback to standard hash (for sendmail's aliases.db) */
165 1.4 lukem db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
166 1.4 lukem return (db);
167 1.1 thorpej }
168 1.1 thorpej
169 1.11 lukem /*
170 1.11 lukem * ypdb_close --
171 1.11 lukem * Close the db
172 1.11 lukem */
173 1.11 lukem
174 1.3 lukem void
175 1.8 wiz ypdb_close(DBM *db)
176 1.1 thorpej {
177 1.1 thorpej (void)(db->close)(db);
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej /*
181 1.1 thorpej * Returns:
182 1.1 thorpej * DATUM on success
183 1.1 thorpej * NULL on failure
184 1.1 thorpej */
185 1.1 thorpej
186 1.3 lukem datum
187 1.8 wiz ypdb_fetch(DBM *db, datum key)
188 1.1 thorpej {
189 1.6 lukem datum retkey;
190 1.6 lukem DBT nk, nd;
191 1.1 thorpej int status;
192 1.1 thorpej
193 1.6 lukem nk.data = key.dptr;
194 1.7 lukem nk.size = key.dsize;
195 1.6 lukem status = (db->get)(db, &nk, &nd, 0);
196 1.1 thorpej if (status) {
197 1.6 lukem retkey.dptr = NULL;
198 1.6 lukem retkey.dsize = 0;
199 1.6 lukem } else {
200 1.6 lukem retkey.dptr = nd.data;
201 1.7 lukem retkey.dsize = nd.size;
202 1.1 thorpej }
203 1.6 lukem return (retkey);
204 1.1 thorpej }
205 1.1 thorpej
206 1.1 thorpej /*
207 1.1 thorpej * Returns:
208 1.1 thorpej * DATUM on success
209 1.1 thorpej * NULL on failure
210 1.1 thorpej */
211 1.1 thorpej
212 1.3 lukem datum
213 1.8 wiz ypdb_firstkey(DBM *db)
214 1.1 thorpej {
215 1.1 thorpej int status;
216 1.6 lukem datum retkey;
217 1.6 lukem DBT nk, nd;
218 1.1 thorpej
219 1.6 lukem status = (db->seq)(db, &nk, &nd, R_FIRST);
220 1.6 lukem if (status) {
221 1.1 thorpej retkey.dptr = NULL;
222 1.6 lukem retkey.dsize = 0;
223 1.6 lukem } else {
224 1.6 lukem retkey.dptr = nk.data;
225 1.7 lukem retkey.dsize = nk.size;
226 1.6 lukem }
227 1.1 thorpej return (retkey);
228 1.1 thorpej }
229 1.1 thorpej
230 1.1 thorpej /*
231 1.1 thorpej * Returns:
232 1.1 thorpej * DATUM on success
233 1.1 thorpej * NULL on failure
234 1.1 thorpej */
235 1.1 thorpej
236 1.3 lukem datum
237 1.8 wiz ypdb_nextkey(DBM *db)
238 1.1 thorpej {
239 1.1 thorpej int status;
240 1.6 lukem datum retkey;
241 1.6 lukem DBT nk, nd;
242 1.1 thorpej
243 1.6 lukem status = (db->seq)(db, &nk, &nd, R_NEXT);
244 1.6 lukem if (status) {
245 1.1 thorpej retkey.dptr = NULL;
246 1.6 lukem retkey.dsize = 0;
247 1.6 lukem } else {
248 1.6 lukem retkey.dptr = nk.data;
249 1.7 lukem retkey.dsize = nk.size;
250 1.6 lukem }
251 1.1 thorpej return (retkey);
252 1.1 thorpej }
253 1.1 thorpej
254 1.1 thorpej /*
255 1.1 thorpej * Returns:
256 1.1 thorpej * DATUM on success
257 1.1 thorpej * NULL on failure
258 1.1 thorpej */
259 1.1 thorpej
260 1.3 lukem datum
261 1.8 wiz ypdb_setkey(DBM *db, datum key)
262 1.1 thorpej {
263 1.1 thorpej int status;
264 1.6 lukem DBT nk, nd;
265 1.6 lukem
266 1.6 lukem nk.data = key.dptr;
267 1.7 lukem nk.size = key.dsize;
268 1.6 lukem status = (db->seq)(db, &nk, &nd, R_CURSOR);
269 1.6 lukem if (status) {
270 1.1 thorpej key.dptr = NULL;
271 1.6 lukem key.dsize = 0;
272 1.6 lukem }
273 1.1 thorpej return (key);
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej /*
277 1.1 thorpej * Returns:
278 1.1 thorpej * 0 on success
279 1.1 thorpej * <0 failure
280 1.1 thorpej */
281 1.1 thorpej
282 1.1 thorpej int
283 1.8 wiz ypdb_delete(DBM *db, datum key)
284 1.1 thorpej {
285 1.1 thorpej int status;
286 1.6 lukem DBT nk;
287 1.1 thorpej
288 1.6 lukem nk.data = key.dptr;
289 1.7 lukem nk.size = key.dsize;
290 1.6 lukem status = (db->del)(db, &nk, 0);
291 1.1 thorpej if (status)
292 1.1 thorpej return (-1);
293 1.1 thorpej else
294 1.1 thorpej return (0);
295 1.1 thorpej }
296 1.1 thorpej
297 1.1 thorpej /*
298 1.1 thorpej * Returns:
299 1.1 thorpej * 0 on success
300 1.1 thorpej * <0 failure
301 1.1 thorpej * 1 if YPDB_INSERT and entry exists
302 1.1 thorpej */
303 1.1 thorpej
304 1.1 thorpej int
305 1.8 wiz ypdb_store(DBM *db, datum key, datum content, int flags)
306 1.1 thorpej {
307 1.6 lukem DBT nk, nd;
308 1.6 lukem
309 1.5 lukem if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
310 1.5 lukem return -1;
311 1.6 lukem nk.data = key.dptr;
312 1.7 lukem nk.size = key.dsize;
313 1.6 lukem nd.data = content.dptr;
314 1.7 lukem nd.size = content.dsize;
315 1.6 lukem return ((db->put)(db, &nk, &nd,
316 1.1 thorpej (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
317 1.1 thorpej }
318