xref: /netbsd/usr.sbin/ypserv/common/ypdb.c (revision c4a72b64)
1 /*	$NetBSD: ypdb.c,v 1.8 2002/07/06 21:39:25 wiz 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.8 2002/07/06 21:39:25 wiz 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(const char *file, int flags, int mode)
68 {
69 	char path[MAXPATHLEN], *cp;
70 	DBM *db;
71 	BTREEINFO info;
72 
73 	cp = strrchr(file, '.');
74 	snprintf(path, sizeof(path), "%s%s", file,
75 	    (cp != NULL && strcmp(cp, ".db") == 0) ? "" : YPDB_SUFFIX);
76 
77 		/* try our btree format first */
78 	info.flags = 0;
79 	info.cachesize = 0;
80 	info.maxkeypage = 0;
81 	info.minkeypage = 0;
82 	info.psize = 0;
83 	info.compare = NULL;
84 	info.prefix = NULL;
85 	info.lorder = 0;
86 	db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
87 	if (db != NULL || errno != EFTYPE)
88 		return (db);
89 
90 		/* fallback to standard hash (for sendmail's aliases.db) */
91 	db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
92 	return (db);
93 }
94 
95 void
96 ypdb_close(DBM *db)
97 {
98 	(void)(db->close)(db);
99 }
100 
101 /*
102  * Returns:
103  *	DATUM on success
104  *	NULL on failure
105  */
106 
107 datum
108 ypdb_fetch(DBM *db, datum key)
109 {
110 	datum retkey;
111 	DBT nk, nd;
112 	int status;
113 
114 	nk.data = key.dptr;
115 	nk.size = key.dsize;
116 	status = (db->get)(db, &nk, &nd, 0);
117 	if (status) {
118 		retkey.dptr = NULL;
119 		retkey.dsize = 0;
120 	} else {
121 		retkey.dptr = nd.data;
122 		retkey.dsize = nd.size;
123 	}
124 	return (retkey);
125 }
126 
127 /*
128  * Returns:
129  *	DATUM on success
130  *	NULL on failure
131  */
132 
133 datum
134 ypdb_firstkey(DBM *db)
135 {
136 	int status;
137 	datum retkey;
138 	DBT nk, nd;
139 
140 	status = (db->seq)(db, &nk, &nd, R_FIRST);
141 	if (status) {
142 		retkey.dptr = NULL;
143 		retkey.dsize = 0;
144 	} else {
145 		retkey.dptr = nk.data;
146 		retkey.dsize = nk.size;
147 	}
148 	return (retkey);
149 }
150 
151 /*
152  * Returns:
153  *	DATUM on success
154  *	NULL on failure
155  */
156 
157 datum
158 ypdb_nextkey(DBM *db)
159 {
160 	int status;
161 	datum retkey;
162 	DBT nk, nd;
163 
164 	status = (db->seq)(db, &nk, &nd, R_NEXT);
165 	if (status) {
166 		retkey.dptr = NULL;
167 		retkey.dsize = 0;
168 	} else {
169 		retkey.dptr = nk.data;
170 		retkey.dsize = nk.size;
171 	}
172 	return (retkey);
173 }
174 
175 /*
176  * Returns:
177  *	DATUM on success
178  *	NULL on failure
179  */
180 
181 datum
182 ypdb_setkey(DBM *db, datum key)
183 {
184 	int status;
185 	DBT nk, nd;
186 
187 	nk.data = key.dptr;
188 	nk.size = key.dsize;
189 	status = (db->seq)(db, &nk, &nd, R_CURSOR);
190 	if (status) {
191 		key.dptr = NULL;
192 		key.dsize = 0;
193 	}
194 	return (key);
195 }
196 
197 /*
198  * Returns:
199  *	 0 on success
200  *	<0 failure
201  */
202 
203 int
204 ypdb_delete(DBM *db, datum key)
205 {
206 	int status;
207 	DBT nk;
208 
209 	nk.data = key.dptr;
210 	nk.size = key.dsize;
211 	status = (db->del)(db, &nk, 0);
212 	if (status)
213 		return (-1);
214 	else
215 		return (0);
216 }
217 
218 /*
219  * Returns:
220  *	 0 on success
221  *	<0 failure
222  *	 1 if YPDB_INSERT and entry exists
223  */
224 
225 int
226 ypdb_store(DBM *db, datum key, datum content, int flags)
227 {
228 	DBT nk, nd;
229 
230 	if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
231 		return -1;
232 	nk.data = key.dptr;
233 	nk.size = key.dsize;
234 	nd.data = content.dptr;
235 	nd.size = content.dsize;
236 	return ((db->put)(db, &nk, &nd,
237 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
238 }
239