1 |
|
|
/* $OpenBSD: ypdb.c,v 1.12 2015/01/16 06:40:22 deraadt 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. Neither the name of the University nor the names of its contributors |
23 |
|
|
* may be used to endorse or promote products derived from this software |
24 |
|
|
* without specific prior written permission. |
25 |
|
|
* |
26 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
27 |
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
28 |
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
29 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
30 |
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
31 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
32 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
34 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
35 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
36 |
|
|
* SUCH DAMAGE. |
37 |
|
|
*/ |
38 |
|
|
|
39 |
|
|
#include <sys/types.h> |
40 |
|
|
#include <stdio.h> |
41 |
|
|
#include <string.h> |
42 |
|
|
|
43 |
|
|
#include <rpcsvc/yp.h> |
44 |
|
|
|
45 |
|
|
#include "ypdb.h" |
46 |
|
|
|
47 |
|
|
/* |
48 |
|
|
* Returns: |
49 |
|
|
* *DBM on success |
50 |
|
|
* NULL on failure |
51 |
|
|
*/ |
52 |
|
|
extern DBM * |
53 |
|
|
ypdb_open(const char *file, int flags, int mode) |
54 |
|
|
{ |
55 |
|
|
BTREEINFO info; |
56 |
|
|
char path[PATH_MAX]; |
57 |
|
|
DBM *db; |
58 |
|
|
|
59 |
|
|
memset(&info, 0, sizeof info); |
60 |
|
|
info.flags = 0; |
61 |
|
|
info.cachesize = 0; |
62 |
|
|
info.maxkeypage = 0; |
63 |
|
|
info.minkeypage = 0; |
64 |
|
|
info.psize = 0; |
65 |
|
|
info.compare = NULL; |
66 |
|
|
info.prefix = NULL; |
67 |
|
|
info.lorder = 0; |
68 |
|
|
snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX); |
69 |
|
|
db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info); |
70 |
|
|
return (db); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/* |
74 |
|
|
* Returns: |
75 |
|
|
* *DBM on success |
76 |
|
|
* NULL on failure |
77 |
|
|
*/ |
78 |
|
|
extern DBM * |
79 |
|
|
ypdb_open_suf(const char *file, int flags, int mode) |
80 |
|
|
{ |
81 |
|
|
BTREEINFO info; |
82 |
|
|
DBM *db; |
83 |
|
|
|
84 |
|
|
memset(&info, 0, sizeof info); |
85 |
|
|
info.flags = 0; |
86 |
|
|
info.cachesize = 0; |
87 |
|
|
info.maxkeypage = 0; |
88 |
|
|
info.minkeypage = 0; |
89 |
|
|
info.psize = 0; |
90 |
|
|
info.compare = NULL; |
91 |
|
|
info.prefix = NULL; |
92 |
|
|
info.lorder = 0; |
93 |
|
|
db = (DBM *)dbopen(file, flags, mode, DB_BTREE, (void *)&info); |
94 |
|
|
return (db); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
extern void |
98 |
|
|
ypdb_close(DBM *db) |
99 |
|
|
{ |
100 |
|
|
(void)(db->close)(db); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/* |
104 |
|
|
* Returns: |
105 |
|
|
* DATUM on success |
106 |
|
|
* NULL on failure |
107 |
|
|
*/ |
108 |
|
|
extern datum |
109 |
|
|
ypdb_fetch(DBM *db, datum key) |
110 |
|
|
{ |
111 |
|
|
datum retval; |
112 |
|
|
DBT nk, nd; |
113 |
|
|
int status; |
114 |
|
|
|
115 |
|
|
nk.data = key.dptr; |
116 |
|
|
nk.size = key.dsize; |
117 |
|
|
|
118 |
|
|
status = (db->get)(db, &nk, &nd, 0); |
119 |
|
|
if (status) { |
120 |
|
|
retval.dptr = NULL; |
121 |
|
|
retval.dsize = 0; |
122 |
|
|
} else { |
123 |
|
|
retval.dptr = nd.data; |
124 |
|
|
retval.dsize = nd.size; |
125 |
|
|
} |
126 |
|
|
return (retval); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/* |
130 |
|
|
* Returns: |
131 |
|
|
* DATUM on success |
132 |
|
|
* NULL on failure |
133 |
|
|
*/ |
134 |
|
|
|
135 |
|
|
extern datum |
136 |
|
|
ypdb_firstkey(DBM *db) |
137 |
|
|
{ |
138 |
|
|
int status; |
139 |
|
|
datum retkey; |
140 |
|
|
DBT nk, nd; |
141 |
|
|
|
142 |
|
|
status = (db->seq)(db, &nk, &nd, R_FIRST); |
143 |
|
|
if (status) { |
144 |
|
|
retkey.dptr = NULL; |
145 |
|
|
retkey.dsize = 0; |
146 |
|
|
} else { |
147 |
|
|
retkey.dptr = nk.data; |
148 |
|
|
retkey.dsize = nk.size; |
149 |
|
|
} |
150 |
|
|
return (retkey); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
/* |
154 |
|
|
* Returns: |
155 |
|
|
* DATUM on success |
156 |
|
|
* NULL on failure |
157 |
|
|
*/ |
158 |
|
|
|
159 |
|
|
extern datum |
160 |
|
|
ypdb_nextkey(DBM *db) |
161 |
|
|
{ |
162 |
|
|
int status; |
163 |
|
|
datum retkey; |
164 |
|
|
DBT nk, nd; |
165 |
|
|
|
166 |
|
|
status = (db->seq)(db, &nk, &nd, R_NEXT); |
167 |
|
|
if (status) { |
168 |
|
|
retkey.dptr = NULL; |
169 |
|
|
retkey.dsize = 0; |
170 |
|
|
} else { |
171 |
|
|
retkey.dptr = nk.data; |
172 |
|
|
retkey.dsize = nk.size; |
173 |
|
|
} |
174 |
|
|
return (retkey); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
/* |
178 |
|
|
* Returns: |
179 |
|
|
* DATUM on success |
180 |
|
|
* NULL on failure |
181 |
|
|
*/ |
182 |
|
|
|
183 |
|
|
extern datum |
184 |
|
|
ypdb_setkey(DBM *db, datum key) |
185 |
|
|
{ |
186 |
|
|
int status; |
187 |
|
|
DBT nk, nd; |
188 |
|
|
|
189 |
|
|
nk.data = key.dptr; |
190 |
|
|
nk.size = key.dsize; |
191 |
|
|
status = (db->seq)(db, &nk, &nd, R_CURSOR); |
192 |
|
|
if (status) { |
193 |
|
|
key.dptr = NULL; |
194 |
|
|
key.dsize = 0; |
195 |
|
|
} |
196 |
|
|
return (key); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/* |
200 |
|
|
* Returns: |
201 |
|
|
* 0 on success |
202 |
|
|
* <0 failure |
203 |
|
|
* 1 if YPDB_INSERT and entry exists |
204 |
|
|
*/ |
205 |
|
|
int |
206 |
|
|
ypdb_store(DBM *db, datum key, datum content, int flags) |
207 |
|
|
{ |
208 |
|
|
DBT nk, nd; |
209 |
|
|
|
210 |
|
|
if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD) |
211 |
|
|
return -1; |
212 |
|
|
nk.data = key.dptr; |
213 |
|
|
nk.size = key.dsize; |
214 |
|
|
nd.data = content.dptr; |
215 |
|
|
nd.size = content.dsize; |
216 |
|
|
return ((db->put)(db, &nk, &nd, |
217 |
|
|
(flags == YPDB_INSERT) ? R_NOOVERWRITE : 0)); |
218 |
|
|
} |