ypdb.c revision 1.6 1 /* $NetBSD: ypdb.c,v 1.6 1999/07/25 09:33:37 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Margo Seltzer.
10 *
11 * This code is derived from ndbm module of BSD4.4 db (hash) by
12 * Mats O Jansson
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
31 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
34 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43 #include <sys/cdefs.h>
44 #ifndef lint
45 __RCSID("$NetBSD: ypdb.c,v 1.6 1999/07/25 09:33:37 lukem Exp $");
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/types.h>
50
51 #include <db.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <string.h>
55
56 #include <rpcsvc/yp.h>
57
58 #include "ypdb.h"
59
60 /*
61 * Returns:
62 * *DBM on success
63 * NULL on failure
64 */
65
66 DBM *
67 ypdb_open(file, flags, mode)
68 const char *file;
69 int flags, mode;
70 {
71 char path[MAXPATHLEN], *cp;
72 DBM *db;
73 BTREEINFO info;
74
75 cp = strrchr(file, '.');
76 snprintf(path, sizeof(path), "%s%s", file,
77 (cp != NULL && strcmp(cp, ".db") == 0) ? "" : YPDB_SUFFIX);
78
79 /* try our btree format first */
80 info.flags = 0;
81 info.cachesize = 0;
82 info.maxkeypage = 0;
83 info.minkeypage = 0;
84 info.psize = 0;
85 info.compare = NULL;
86 info.prefix = NULL;
87 info.lorder = 0;
88 db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
89 if (db != NULL || errno != EFTYPE)
90 return (db);
91
92 /* fallback to standard hash (for sendmail's aliases.db) */
93 db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
94 return (db);
95 }
96
97 void
98 ypdb_close(db)
99 DBM *db;
100 {
101 (void)(db->close)(db);
102 }
103
104 /*
105 * Returns:
106 * DATUM on success
107 * NULL on failure
108 */
109
110 datum
111 ypdb_fetch(db, key)
112 DBM *db;
113 datum key;
114 {
115 datum retkey;
116 DBT nk, nd;
117 int status;
118
119 nk.data = key.dptr;
120 nk.size = (size_t)key.dsize;
121 status = (db->get)(db, &nk, &nd, 0);
122 if (status) {
123 retkey.dptr = NULL;
124 retkey.dsize = 0;
125 } else {
126 retkey.dptr = nd.data;
127 retkey.dsize = (int)nd.size;
128 }
129 return (retkey);
130 }
131
132 /*
133 * Returns:
134 * DATUM on success
135 * NULL on failure
136 */
137
138 datum
139 ypdb_firstkey(db)
140 DBM *db;
141 {
142 int status;
143 datum retkey;
144 DBT nk, nd;
145
146 status = (db->seq)(db, &nk, &nd, R_FIRST);
147 if (status) {
148 retkey.dptr = NULL;
149 retkey.dsize = 0;
150 } else {
151 retkey.dptr = nk.data;
152 retkey.dsize = (int)nk.size;
153 }
154 return (retkey);
155 }
156
157 /*
158 * Returns:
159 * DATUM on success
160 * NULL on failure
161 */
162
163 datum
164 ypdb_nextkey(db)
165 DBM *db;
166 {
167 int status;
168 datum retkey;
169 DBT nk, nd;
170
171 status = (db->seq)(db, &nk, &nd, R_NEXT);
172 if (status) {
173 retkey.dptr = NULL;
174 retkey.dsize = 0;
175 } else {
176 retkey.dptr = nk.data;
177 retkey.dsize = (int)nk.size;
178 }
179 return (retkey);
180 }
181
182 /*
183 * Returns:
184 * DATUM on success
185 * NULL on failure
186 */
187
188 datum
189 ypdb_setkey(db, key)
190 DBM *db;
191 datum key;
192 {
193 int status;
194 DBT nk, nd;
195
196 nk.data = key.dptr;
197 nk.size = (size_t)key.dsize;
198 status = (db->seq)(db, &nk, &nd, R_CURSOR);
199 if (status) {
200 key.dptr = NULL;
201 key.dsize = 0;
202 }
203 return (key);
204 }
205
206 /*
207 * Returns:
208 * 0 on success
209 * <0 failure
210 */
211
212 int
213 ypdb_delete(db, key)
214 DBM *db;
215 datum key;
216 {
217 int status;
218 DBT nk;
219
220 nk.data = key.dptr;
221 nk.size = (size_t)key.dsize;
222 status = (db->del)(db, &nk, 0);
223 if (status)
224 return (-1);
225 else
226 return (0);
227 }
228
229 /*
230 * Returns:
231 * 0 on success
232 * <0 failure
233 * 1 if YPDB_INSERT and entry exists
234 */
235
236 int
237 ypdb_store(db, key, content, flags)
238 DBM *db;
239 datum key, content;
240 int flags;
241 {
242 DBT nk, nd;
243
244 if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
245 return -1;
246 nk.data = key.dptr;
247 nk.size = (size_t)key.dsize;
248 nd.data = content.dptr;
249 nd.size = (size_t)content.dsize;
250 return ((db->put)(db, &nk, &nd,
251 (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
252 }
253