1 |
|
|
/* $OpenBSD: ypserv_db.c,v 1.30 2015/12/08 07:16:33 mmcc Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se> |
5 |
|
|
* Copyright (c) 1996 Charles D. Cranor |
6 |
|
|
* All rights reserved. |
7 |
|
|
* |
8 |
|
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
|
* modification, are permitted provided that the following conditions |
10 |
|
|
* are met: |
11 |
|
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
|
* documentation and/or other materials provided with the distribution. |
16 |
|
|
* |
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
18 |
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 |
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
21 |
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
|
* SUCH DAMAGE. |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
/* |
31 |
|
|
* major revision/cleanup of Mats' version |
32 |
|
|
* done by Chuck Cranor <chuck@ccrc.wustl.edu> |
33 |
|
|
* Jan 1996. |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
#include <rpc/rpc.h> |
37 |
|
|
#include <rpcsvc/yp.h> |
38 |
|
|
#include <rpcsvc/ypclnt.h> |
39 |
|
|
#include <sys/stat.h> |
40 |
|
|
#include <fcntl.h> |
41 |
|
|
#include <string.h> |
42 |
|
|
#include <stdio.h> |
43 |
|
|
#include <stdlib.h> |
44 |
|
|
#include <netdb.h> |
45 |
|
|
#include <arpa/nameser.h> |
46 |
|
|
#include <resolv.h> |
47 |
|
|
#include <sys/types.h> |
48 |
|
|
#include <sys/socket.h> |
49 |
|
|
#include <sys/queue.h> |
50 |
|
|
#include <netinet/in.h> |
51 |
|
|
#include <arpa/inet.h> |
52 |
|
|
#include <syslog.h> |
53 |
|
|
#include <errno.h> |
54 |
|
|
#include "yplog.h" |
55 |
|
|
#include "ypdb.h" |
56 |
|
|
#include "ypdef.h" |
57 |
|
|
#include "ypserv.h" |
58 |
|
|
|
59 |
|
|
LIST_HEAD(domainlist, opt_domain); /* LIST of domains */ |
60 |
|
|
LIST_HEAD(maplist, opt_map); /* LIST of maps (in a domain) */ |
61 |
|
|
TAILQ_HEAD(mapq, opt_map); /* TAILQ of maps (LRU) */ |
62 |
|
|
|
63 |
|
|
struct opt_map { |
64 |
|
|
mapname map; /* map name (malloc'd) */ |
65 |
|
|
DBM *db; /* database */ |
66 |
|
|
struct opt_domain *dom; /* back ptr to our domain */ |
67 |
|
|
int host_lookup; /* host lookup */ |
68 |
|
|
int secure; /* secure map? */ |
69 |
|
|
TAILQ_ENTRY(opt_map) mapsq; /* map queue pointers */ |
70 |
|
|
LIST_ENTRY(opt_map) mapsl; /* map list pointers */ |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
struct opt_domain { |
74 |
|
|
domainname domain; /* domain name (malloc'd) */ |
75 |
|
|
struct maplist dmaps; /* the domain's active maps */ |
76 |
|
|
LIST_ENTRY(opt_domain) domsl; /* global linked list of domains */ |
77 |
|
|
}; |
78 |
|
|
|
79 |
|
|
struct domainlist doms; /* global list of domains */ |
80 |
|
|
struct mapq maps; /* global queue of maps (LRU) */ |
81 |
|
|
|
82 |
|
|
extern int usedns; |
83 |
|
|
|
84 |
|
|
/* |
85 |
|
|
* ypdb_init: init the queues and lists |
86 |
|
|
*/ |
87 |
|
|
void |
88 |
|
|
ypdb_init(void) |
89 |
|
|
{ |
90 |
|
|
LIST_INIT(&doms); |
91 |
|
|
TAILQ_INIT(&maps); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
* yp_private: |
96 |
|
|
* Check if key is a YP private key. Return TRUE if it is and |
97 |
|
|
* ypprivate is FALSE. |
98 |
|
|
*/ |
99 |
|
|
static int |
100 |
|
|
yp_private(datum key, int ypprivate) |
101 |
|
|
{ |
102 |
|
|
if (ypprivate) |
103 |
|
|
return (FALSE); |
104 |
|
|
|
105 |
|
|
if (key.dsize == 0 || key.dptr == NULL) |
106 |
|
|
return (FALSE); |
107 |
|
|
|
108 |
|
|
if (key.dsize == YP_LAST_LEN && |
109 |
|
|
strncmp(key.dptr, YP_LAST_KEY, YP_LAST_LEN) == 0) |
110 |
|
|
return(TRUE); |
111 |
|
|
if (key.dsize == YP_INPUT_LEN && |
112 |
|
|
strncmp(key.dptr, YP_INPUT_KEY, YP_INPUT_LEN) == 0) |
113 |
|
|
return(TRUE); |
114 |
|
|
if (key.dsize == YP_OUTPUT_LEN && |
115 |
|
|
strncmp(key.dptr, YP_OUTPUT_KEY, YP_OUTPUT_LEN) == 0) |
116 |
|
|
return(TRUE); |
117 |
|
|
if (key.dsize == YP_MASTER_LEN && |
118 |
|
|
strncmp(key.dptr, YP_MASTER_KEY, YP_MASTER_LEN) == 0) |
119 |
|
|
return(TRUE); |
120 |
|
|
if (key.dsize == YP_DOMAIN_LEN && |
121 |
|
|
strncmp(key.dptr, YP_DOMAIN_KEY, YP_DOMAIN_LEN) == 0) |
122 |
|
|
return(TRUE); |
123 |
|
|
if (key.dsize == YP_INTERDOMAIN_LEN && |
124 |
|
|
strncmp(key.dptr, YP_INTERDOMAIN_KEY, YP_INTERDOMAIN_LEN) == 0) |
125 |
|
|
return(TRUE); |
126 |
|
|
if (key.dsize == YP_SECURE_LEN && |
127 |
|
|
strncmp(key.dptr, YP_SECURE_KEY, YP_SECURE_LEN) == 0) |
128 |
|
|
return(TRUE); |
129 |
|
|
return(FALSE); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
/* |
133 |
|
|
* Close least recent used map. This routine is called when we have |
134 |
|
|
* no more file descripotors free, or we want to close all maps. |
135 |
|
|
*/ |
136 |
|
|
static void |
137 |
|
|
ypdb_close_last(void) |
138 |
|
|
{ |
139 |
|
|
struct opt_map *last; |
140 |
|
|
|
141 |
|
|
if (TAILQ_EMPTY(&maps)) { |
142 |
|
|
yplog(" ypdb_close_last: LRU list is empty!"); |
143 |
|
|
return; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
last = TAILQ_LAST(&maps, mapq); |
147 |
|
|
|
148 |
|
|
TAILQ_REMOVE(&maps, last, mapsq); /* remove from LRU circleq */ |
149 |
|
|
LIST_REMOVE(last, mapsl); /* remove from domain list */ |
150 |
|
|
|
151 |
|
|
#ifdef DEBUG |
152 |
|
|
yplog(" ypdb_close_last: closing map %s in domain %s [db=0x%x]", |
153 |
|
|
last->map, last->dom->domain, last->db); |
154 |
|
|
#endif |
155 |
|
|
|
156 |
|
|
ypdb_close(last->db); /* close DB */ |
157 |
|
|
free(last->map); /* free map name */ |
158 |
|
|
free(last); /* free map */ |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/* |
162 |
|
|
* Close all open maps. |
163 |
|
|
*/ |
164 |
|
|
void |
165 |
|
|
ypdb_close_all(void) |
166 |
|
|
{ |
167 |
|
|
|
168 |
|
|
#ifdef DEBUG |
169 |
|
|
yplog(" ypdb_close_all(): start"); |
170 |
|
|
#endif |
171 |
|
|
while (!TAILQ_EMPTY(&maps)) |
172 |
|
|
ypdb_close_last(); |
173 |
|
|
#ifdef DEBUG |
174 |
|
|
yplog(" ypdb_close_all(): done"); |
175 |
|
|
#endif |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/* |
179 |
|
|
* Close Database if Open/Close Optimization isn't turned on. |
180 |
|
|
*/ |
181 |
|
|
static void |
182 |
|
|
ypdb_close_db(DBM *db) |
183 |
|
|
{ |
184 |
|
|
#ifdef DEBUG |
185 |
|
|
yplog(" ypdb_close_db(0x%x)", db); |
186 |
|
|
#endif |
187 |
|
|
#ifndef OPTDB |
188 |
|
|
ypdb_close_all(); |
189 |
|
|
#endif |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
/* |
193 |
|
|
* ypdb_open_db |
194 |
|
|
*/ |
195 |
|
|
DBM * |
196 |
|
|
ypdb_open_db(domainname domain, mapname map, ypstat *status, |
197 |
|
|
struct opt_map **map_info) |
198 |
|
|
{ |
199 |
|
|
char map_path[PATH_MAX]; |
200 |
|
|
static char *domain_key = YP_INTERDOMAIN_KEY; |
201 |
|
|
static char *secure_key = YP_SECURE_KEY; |
202 |
|
|
DBM *db; |
203 |
|
|
struct opt_domain *d = NULL; |
204 |
|
|
struct opt_map *m = NULL; |
205 |
|
|
datum k, v; |
206 |
|
|
#ifdef OPTDB |
207 |
|
|
int i; |
208 |
|
|
#endif |
209 |
|
|
|
210 |
|
|
/* |
211 |
|
|
* check for preloaded domain, map |
212 |
|
|
*/ |
213 |
|
|
LIST_FOREACH(d, &doms, domsl) { |
214 |
|
|
if (strcmp(domain, d->domain) == 0) |
215 |
|
|
break; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
if (d) { |
219 |
|
|
LIST_FOREACH(m, &d->dmaps, mapsl) |
220 |
|
|
if (strcmp(map, m->map) == 0) |
221 |
|
|
break; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
/* |
225 |
|
|
* map found open? |
226 |
|
|
*/ |
227 |
|
|
if (m) { |
228 |
|
|
#ifdef DEBUG |
229 |
|
|
yplog(" ypdb_open_db: cached open: domain=%s, map=%s, db=0x%x", |
230 |
|
|
domain, map, m->db); |
231 |
|
|
#endif |
232 |
|
|
TAILQ_REMOVE(&maps, m, mapsq); /* adjust LRU queue */ |
233 |
|
|
TAILQ_INSERT_HEAD(&maps, m, mapsq); |
234 |
|
|
*status = YP_TRUE; |
235 |
|
|
if (map_info) |
236 |
|
|
*map_info = m; |
237 |
|
|
return(m->db); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
/* Check for illegal charcaters */ |
241 |
|
|
|
242 |
|
|
if (strchr(domain, '/')) { |
243 |
|
|
*status = YP_NODOM; |
244 |
|
|
return (NULL); |
245 |
|
|
} |
246 |
|
|
if (strchr(map, '/')) { |
247 |
|
|
*status = YP_NOMAP; |
248 |
|
|
return (NULL); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
/* |
252 |
|
|
* open map |
253 |
|
|
*/ |
254 |
|
|
#ifdef OPTDB |
255 |
|
|
i = 0; |
256 |
|
|
while (i == 0) { |
257 |
|
|
#endif |
258 |
|
|
snprintf(map_path, sizeof(map_path), "%s/%s/%s", YP_DB_PATH, |
259 |
|
|
domain, map); |
260 |
|
|
db = ypdb_open(map_path, O_RDONLY, 0444); |
261 |
|
|
#ifdef OPTDB |
262 |
|
|
if (db == NULL) { |
263 |
|
|
#ifdef DEBUG |
264 |
|
|
yplog(" ypdb_open_db: errno %d (%s)", |
265 |
|
|
errno, sys_errlist[errno]); |
266 |
|
|
#endif |
267 |
|
|
if (errno == ENFILE || errno == EMFILE) { |
268 |
|
|
ypdb_close_last(); |
269 |
|
|
} else { |
270 |
|
|
i = errno; |
271 |
|
|
} |
272 |
|
|
} else { |
273 |
|
|
i = 4711; |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
#endif |
277 |
|
|
*status = YP_NOMAP; /* see note below */ |
278 |
|
|
if (db == NULL) { |
279 |
|
|
if (errno == ENOENT) { |
280 |
|
|
#ifdef DEBUG |
281 |
|
|
yplog(" ypdb_open_db: no map %s (domain=%s)", |
282 |
|
|
map, domain); |
283 |
|
|
#endif |
284 |
|
|
return(NULL); |
285 |
|
|
} |
286 |
|
|
#ifdef DEBUG |
287 |
|
|
yplog(" ypdb_open_db: ypdb_open FAILED: map %s (domain=%s)", |
288 |
|
|
map, domain); |
289 |
|
|
#endif |
290 |
|
|
return(NULL); |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
/* |
294 |
|
|
* note: status now YP_NOMAP |
295 |
|
|
*/ |
296 |
|
|
|
297 |
|
|
if (d == NULL) { /* allocate new domain? */ |
298 |
|
|
d = malloc(sizeof(*d)); |
299 |
|
|
if (d) |
300 |
|
|
d->domain = strdup(domain); |
301 |
|
|
if (d == NULL || d->domain == NULL) { |
302 |
|
|
yplog(" ypdb_open_db: MALLOC failed"); |
303 |
|
|
ypdb_close(db); |
304 |
|
|
free(d); |
305 |
|
|
return(NULL); |
306 |
|
|
} |
307 |
|
|
LIST_INIT(&d->dmaps); |
308 |
|
|
LIST_INSERT_HEAD(&doms, d, domsl); |
309 |
|
|
#ifdef DEBUG |
310 |
|
|
yplog(" ypdb_open_db: NEW DOMAIN %s", domain); |
311 |
|
|
#endif |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
/* |
315 |
|
|
* m must be NULL since we couldn't find a map. allocate new one |
316 |
|
|
*/ |
317 |
|
|
|
318 |
|
|
m = malloc(sizeof(*m)); |
319 |
|
|
if (m) |
320 |
|
|
m->map = strdup(map); |
321 |
|
|
if (m == NULL || m->map == NULL) { |
322 |
|
|
free(m); |
323 |
|
|
yplog(" ypdb_open_db: MALLOC failed"); |
324 |
|
|
ypdb_close(db); |
325 |
|
|
return(NULL); |
326 |
|
|
} |
327 |
|
|
m->db = db; |
328 |
|
|
m->dom = d; |
329 |
|
|
m->host_lookup = FALSE; |
330 |
|
|
TAILQ_INSERT_HEAD(&maps, m, mapsq); |
331 |
|
|
LIST_INSERT_HEAD(&d->dmaps, m, mapsl); |
332 |
|
|
if (strcmp(map, YP_HOSTNAME) == 0 || strcmp(map, YP_HOSTADDR) == 0) { |
333 |
|
|
if (!usedns) { |
334 |
|
|
k.dptr = domain_key; |
335 |
|
|
k.dsize = YP_INTERDOMAIN_LEN; |
336 |
|
|
v = ypdb_fetch(db, k); |
337 |
|
|
if (v.dptr) |
338 |
|
|
m->host_lookup = TRUE; |
339 |
|
|
} else |
340 |
|
|
m->host_lookup = TRUE; |
341 |
|
|
} |
342 |
|
|
m->secure = FALSE; |
343 |
|
|
k.dptr = secure_key; |
344 |
|
|
k.dsize = YP_SECURE_LEN; |
345 |
|
|
v = ypdb_fetch(db, k); |
346 |
|
|
if (v.dptr) |
347 |
|
|
m->secure = TRUE; |
348 |
|
|
*status = YP_TRUE; |
349 |
|
|
if (map_info) |
350 |
|
|
*map_info = m; |
351 |
|
|
#ifdef DEBUG |
352 |
|
|
yplog(" ypdb_open_db: NEW MAP domain=%s, map=%s, hl=%d, s=%d, db=0x%x", |
353 |
|
|
domain, map, m->host_lookup, m->secure, m->db); |
354 |
|
|
#endif |
355 |
|
|
return(m->db); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
/* |
359 |
|
|
* lookup host |
360 |
|
|
*/ |
361 |
|
|
static ypstat |
362 |
|
|
lookup_host(int nametable, int host_lookup, DBM *db, char *keystr, |
363 |
|
|
ypresp_val *result) |
364 |
|
|
{ |
365 |
|
|
struct hostent *host; |
366 |
|
|
struct in_addr *addr_name; |
367 |
|
|
struct in_addr addr_addr; |
368 |
|
|
static char val[BUFSIZ+1]; /* match libc */ |
369 |
|
|
static char hostname[HOST_NAME_MAX+1]; |
370 |
|
|
char tmpbuf[HOST_NAME_MAX+1 + 20], *v, *ptr; |
371 |
|
|
size_t len; |
372 |
|
|
int l; |
373 |
|
|
|
374 |
|
|
if (!host_lookup) |
375 |
|
|
return(YP_NOKEY); |
376 |
|
|
|
377 |
|
|
if ((_res.options & RES_INIT) == 0) |
378 |
|
|
res_init(); |
379 |
|
|
bcopy("b", _res.lookups, sizeof("b")); |
380 |
|
|
|
381 |
|
|
if (nametable) { |
382 |
|
|
host = gethostbyname(keystr); |
383 |
|
|
if (host == NULL || host->h_addrtype != AF_INET) |
384 |
|
|
return(YP_NOKEY); |
385 |
|
|
addr_name = (struct in_addr *) *host->h_addr_list; |
386 |
|
|
v = val; |
387 |
|
|
for (; host->h_addr_list[0] != NULL; host->h_addr_list++) { |
388 |
|
|
addr_name = (struct in_addr *)host->h_addr_list[0]; |
389 |
|
|
snprintf(tmpbuf, sizeof(tmpbuf), "%s %s\n", |
390 |
|
|
inet_ntoa(*addr_name), host->h_name); |
391 |
|
|
len = strlcat(val, tmpbuf, sizeof(val)); |
392 |
|
|
if (len >= sizeof(val)) |
393 |
|
|
break; |
394 |
|
|
v = val + len; |
395 |
|
|
} |
396 |
|
|
result->val.valdat_val = val; |
397 |
|
|
result->val.valdat_len = v - val; |
398 |
|
|
return(YP_TRUE); |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
inet_aton(keystr, &addr_addr); |
402 |
|
|
host = gethostbyaddr((char *)&addr_addr, sizeof(addr_addr), AF_INET); |
403 |
|
|
if (host == NULL) return(YP_NOKEY); |
404 |
|
|
|
405 |
|
|
strncpy((char *)hostname, host->h_name, sizeof(hostname) - 1); |
406 |
|
|
hostname[sizeof(hostname) - 1] = '\0'; |
407 |
|
|
host = gethostbyname((char *)hostname); |
408 |
|
|
if (host == NULL) |
409 |
|
|
return(YP_NOKEY); |
410 |
|
|
|
411 |
|
|
l = 0; |
412 |
|
|
for (; host->h_addr_list[0] != NULL; host->h_addr_list++) |
413 |
|
|
if (!bcmp(host->h_addr_list[0], &addr_addr, sizeof(addr_addr))) |
414 |
|
|
l++; |
415 |
|
|
if (l == 0) { |
416 |
|
|
yplog("lookup_host: address %s not listed for host %s\n", |
417 |
|
|
inet_ntoa(addr_addr), hostname); |
418 |
|
|
syslog(LOG_NOTICE, |
419 |
|
|
"ypserv: address %s not listed for host %s\n", |
420 |
|
|
inet_ntoa(addr_addr), hostname); |
421 |
|
|
return(YP_NOKEY); |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
len = snprintf(val, sizeof(val), "%s %s", keystr, host->h_name); |
425 |
|
|
if (len == (size_t)-1 || len >= sizeof(val)) |
426 |
|
|
return(YP_YPERR); |
427 |
|
|
v = val + len; |
428 |
|
|
while ((ptr = *(host->h_aliases)) != NULL) { |
429 |
|
|
strlcat(val, " ", sizeof(val)); |
430 |
|
|
len = strlcat(val, ptr, sizeof(val)); |
431 |
|
|
if (len >= sizeof(val)) |
432 |
|
|
break; |
433 |
|
|
v = val + len; |
434 |
|
|
host->h_aliases++; |
435 |
|
|
} |
436 |
|
|
result->val.valdat_val = val; |
437 |
|
|
result->val.valdat_len = v - val; |
438 |
|
|
|
439 |
|
|
return(YP_TRUE); |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
ypresp_val |
443 |
|
|
ypdb_get_record(domainname domain, mapname map, keydat key, int ypprivate) |
444 |
|
|
{ |
445 |
|
|
static ypresp_val res; |
446 |
|
|
static char keystr[YPMAXRECORD+1]; |
447 |
|
|
DBM *db; |
448 |
|
|
datum k, v; |
449 |
|
|
int host_lookup = 0, hn; |
450 |
|
|
struct opt_map *map_info = NULL; |
451 |
|
|
|
452 |
|
|
bzero(&res, sizeof(res)); |
453 |
|
|
|
454 |
|
|
db = ypdb_open_db(domain, map, &res.stat, &map_info); |
455 |
|
|
if (!db || res.stat < 0) |
456 |
|
|
return(res); |
457 |
|
|
if (map_info) |
458 |
|
|
host_lookup = map_info->host_lookup; |
459 |
|
|
|
460 |
|
|
k.dptr = key.keydat_val; |
461 |
|
|
k.dsize = key.keydat_len; |
462 |
|
|
|
463 |
|
|
if (yp_private(k, ypprivate)) { |
464 |
|
|
res.stat = YP_NOKEY; |
465 |
|
|
goto done; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
v = ypdb_fetch(db, k); |
469 |
|
|
|
470 |
|
|
if (v.dptr == NULL) { |
471 |
|
|
res.stat = YP_NOKEY; |
472 |
|
|
if ((hn = strcmp(map, YP_HOSTNAME)) != 0 && |
473 |
|
|
strcmp(map, YP_HOSTADDR) != 0) |
474 |
|
|
goto done; |
475 |
|
|
/* note: lookup_host needs null terminated string */ |
476 |
|
|
strncpy(keystr, key.keydat_val, key.keydat_len); |
477 |
|
|
keystr[key.keydat_len] = '\0'; |
478 |
|
|
res.stat = lookup_host((hn == 0) ? TRUE : FALSE, |
479 |
|
|
host_lookup, db, keystr, &res); |
480 |
|
|
} else { |
481 |
|
|
res.val.valdat_val = v.dptr; |
482 |
|
|
res.val.valdat_len = v.dsize; |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
done: |
486 |
|
|
ypdb_close_db(db); |
487 |
|
|
return(res); |
488 |
|
|
} |
489 |
|
|
|
490 |
|
|
ypresp_key_val |
491 |
|
|
ypdb_get_first(domainname domain, mapname map, int ypprivate) |
492 |
|
|
{ |
493 |
|
|
static ypresp_key_val res; |
494 |
|
|
DBM *db; |
495 |
|
|
datum k, v; |
496 |
|
|
|
497 |
|
|
bzero(&res, sizeof(res)); |
498 |
|
|
|
499 |
|
|
db = ypdb_open_db(domain, map, &res.stat, NULL); |
500 |
|
|
|
501 |
|
|
if (res.stat >= 0) { |
502 |
|
|
k = ypdb_firstkey(db); |
503 |
|
|
|
504 |
|
|
while (yp_private(k, ypprivate)) |
505 |
|
|
k = ypdb_nextkey(db); |
506 |
|
|
|
507 |
|
|
if (k.dptr == NULL) { |
508 |
|
|
res.stat = YP_NOKEY; |
509 |
|
|
} else { |
510 |
|
|
res.key.keydat_val = k.dptr; |
511 |
|
|
res.key.keydat_len = k.dsize; |
512 |
|
|
v = ypdb_fetch(db, k); |
513 |
|
|
if (v.dptr == NULL) { |
514 |
|
|
res.stat = YP_NOKEY; |
515 |
|
|
} else { |
516 |
|
|
res.val.valdat_val = v.dptr; |
517 |
|
|
res.val.valdat_len = v.dsize; |
518 |
|
|
} |
519 |
|
|
} |
520 |
|
|
} |
521 |
|
|
ypdb_close_db(db); |
522 |
|
|
return (res); |
523 |
|
|
} |
524 |
|
|
|
525 |
|
|
ypresp_key_val |
526 |
|
|
ypdb_get_next(domainname domain, mapname map, keydat key, int ypprivate) |
527 |
|
|
{ |
528 |
|
|
static ypresp_key_val res; |
529 |
|
|
DBM *db; |
530 |
|
|
datum k, v, n; |
531 |
|
|
|
532 |
|
|
bzero(&res, sizeof(res)); |
533 |
|
|
db = ypdb_open_db(domain, map, &res.stat, NULL); |
534 |
|
|
|
535 |
|
|
if (res.stat >= 0) { |
536 |
|
|
n.dptr = key.keydat_val; |
537 |
|
|
n.dsize = key.keydat_len; |
538 |
|
|
v.dptr = NULL; |
539 |
|
|
v.dsize = 0; |
540 |
|
|
k.dptr = NULL; |
541 |
|
|
k.dsize = 0; |
542 |
|
|
|
543 |
|
|
n = ypdb_setkey(db, n); |
544 |
|
|
|
545 |
|
|
if (n.dptr != NULL) |
546 |
|
|
k = ypdb_nextkey(db); |
547 |
|
|
else |
548 |
|
|
k.dptr = NULL; |
549 |
|
|
|
550 |
|
|
if (k.dptr != NULL) { |
551 |
|
|
while (yp_private(k, ypprivate)) |
552 |
|
|
k = ypdb_nextkey(db); |
553 |
|
|
} |
554 |
|
|
|
555 |
|
|
if (k.dptr == NULL) { |
556 |
|
|
res.stat = YP_NOMORE; |
557 |
|
|
} else { |
558 |
|
|
res.key.keydat_val = k.dptr; |
559 |
|
|
res.key.keydat_len = k.dsize; |
560 |
|
|
v = ypdb_fetch(db, k); |
561 |
|
|
if (v.dptr == NULL) { |
562 |
|
|
res.stat = YP_NOMORE; |
563 |
|
|
} else { |
564 |
|
|
res.val.valdat_val = v.dptr; |
565 |
|
|
res.val.valdat_len = v.dsize; |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
} |
569 |
|
|
ypdb_close_db(db); |
570 |
|
|
return (res); |
571 |
|
|
} |
572 |
|
|
|
573 |
|
|
ypresp_order |
574 |
|
|
ypdb_get_order(domainname domain, mapname map) |
575 |
|
|
{ |
576 |
|
|
static ypresp_order res; |
577 |
|
|
static char *order_key = YP_LAST_KEY; |
578 |
|
|
char order[MAX_LAST_LEN+1]; |
579 |
|
|
DBM *db; |
580 |
|
|
datum k, v; |
581 |
|
|
|
582 |
|
|
bzero(&res, sizeof(res)); |
583 |
|
|
db = ypdb_open_db(domain, map, &res.stat, NULL); |
584 |
|
|
|
585 |
|
|
if (res.stat >= 0) { |
586 |
|
|
k.dptr = order_key; |
587 |
|
|
k.dsize = YP_LAST_LEN; |
588 |
|
|
|
589 |
|
|
v = ypdb_fetch(db, k); |
590 |
|
|
if (v.dptr == NULL) { |
591 |
|
|
res.stat = YP_NOKEY; |
592 |
|
|
} else { |
593 |
|
|
strncpy(order, v.dptr, v.dsize); |
594 |
|
|
order[v.dsize] = '\0'; |
595 |
|
|
res.ordernum = (u_int32_t)atol(order); |
596 |
|
|
} |
597 |
|
|
} |
598 |
|
|
ypdb_close_db(db); |
599 |
|
|
return (res); |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
ypresp_master |
603 |
|
|
ypdb_get_master(domainname domain, mapname map) |
604 |
|
|
{ |
605 |
|
|
static ypresp_master res; |
606 |
|
|
static char *master_key = YP_MASTER_KEY; |
607 |
|
|
static char master[MAX_MASTER_LEN+1]; |
608 |
|
|
DBM *db; |
609 |
|
|
datum k, v; |
610 |
|
|
|
611 |
|
|
bzero(&res, sizeof(res)); |
612 |
|
|
db = ypdb_open_db(domain, map, &res.stat, NULL); |
613 |
|
|
|
614 |
|
|
if (res.stat >= 0) { |
615 |
|
|
k.dptr = master_key; |
616 |
|
|
k.dsize = YP_MASTER_LEN; |
617 |
|
|
|
618 |
|
|
v = ypdb_fetch(db, k); |
619 |
|
|
if (v.dptr == NULL) { |
620 |
|
|
res.stat = YP_NOKEY; |
621 |
|
|
} else { |
622 |
|
|
strncpy(master, v.dptr, v.dsize); |
623 |
|
|
master[v.dsize] = '\0'; |
624 |
|
|
res.peer = (peername) &master; |
625 |
|
|
} |
626 |
|
|
} |
627 |
|
|
ypdb_close_db(db); |
628 |
|
|
return (res); |
629 |
|
|
} |
630 |
|
|
|
631 |
|
|
bool_t |
632 |
|
|
ypdb_xdr_get_all(XDR *xdrs, ypreq_nokey *req) |
633 |
|
|
{ |
634 |
|
|
static ypresp_all resp; |
635 |
|
|
DBM *db; |
636 |
|
|
datum k, v; |
637 |
|
|
|
638 |
|
|
bzero(&resp, sizeof(resp)); |
639 |
|
|
|
640 |
|
|
/* |
641 |
|
|
* open db, and advance past any private keys we may see |
642 |
|
|
*/ |
643 |
|
|
db = ypdb_open_db(req->domain, req->map, |
644 |
|
|
&resp.ypresp_all_u.val.stat, NULL); |
645 |
|
|
if (!db || resp.ypresp_all_u.val.stat < 0) |
646 |
|
|
return(FALSE); |
647 |
|
|
k = ypdb_firstkey(db); |
648 |
|
|
while (yp_private(k, FALSE)) |
649 |
|
|
k = ypdb_nextkey(db); |
650 |
|
|
|
651 |
|
|
while (1) { |
652 |
|
|
if (k.dptr == NULL) |
653 |
|
|
break; |
654 |
|
|
|
655 |
|
|
v = ypdb_fetch(db, k); |
656 |
|
|
if (v.dptr == NULL) |
657 |
|
|
break; |
658 |
|
|
|
659 |
|
|
resp.more = TRUE; |
660 |
|
|
resp.ypresp_all_u.val.stat = YP_TRUE; |
661 |
|
|
resp.ypresp_all_u.val.key.keydat_val = k.dptr; |
662 |
|
|
resp.ypresp_all_u.val.key.keydat_len = k.dsize; |
663 |
|
|
resp.ypresp_all_u.val.val.valdat_val = v.dptr; |
664 |
|
|
resp.ypresp_all_u.val.val.valdat_len = v.dsize; |
665 |
|
|
|
666 |
|
|
if (!xdr_ypresp_all(xdrs, &resp)) { |
667 |
|
|
#ifdef DEBUG |
668 |
|
|
yplog(" ypdb_xdr_get_all: xdr_ypresp_all failed"); |
669 |
|
|
#endif |
670 |
|
|
return(FALSE); |
671 |
|
|
} |
672 |
|
|
|
673 |
|
|
/* advance past private keys */ |
674 |
|
|
k = ypdb_nextkey(db); |
675 |
|
|
while (yp_private(k, FALSE)) |
676 |
|
|
k = ypdb_nextkey(db); |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
bzero(&resp, sizeof(resp)); |
680 |
|
|
resp.ypresp_all_u.val.stat = YP_NOKEY; |
681 |
|
|
resp.more = FALSE; |
682 |
|
|
|
683 |
|
|
if (!xdr_ypresp_all(xdrs, &resp)) { |
684 |
|
|
#ifdef DEBUG |
685 |
|
|
yplog(" ypdb_xdr_get_all: final xdr_ypresp_all failed"); |
686 |
|
|
#endif |
687 |
|
|
return(FALSE); |
688 |
|
|
} |
689 |
|
|
ypdb_close_db(db); |
690 |
|
|
return (TRUE); |
691 |
|
|
} |
692 |
|
|
|
693 |
|
|
int |
694 |
|
|
ypdb_secure(domainname domain, mapname map) |
695 |
|
|
{ |
696 |
|
|
static ypresp_val res; |
697 |
|
|
DBM *db; |
698 |
|
|
int secure; |
699 |
|
|
struct opt_map *map_info = NULL; |
700 |
|
|
|
701 |
|
|
bzero(&res, sizeof(res)); |
702 |
|
|
secure = FALSE; |
703 |
|
|
|
704 |
|
|
db = ypdb_open_db(domain, map, &res.stat, &map_info); |
705 |
|
|
if (!db || res.stat < 0) |
706 |
|
|
return(secure); /* ? */ |
707 |
|
|
if (map_info) |
708 |
|
|
secure = map_info->secure; |
709 |
|
|
|
710 |
|
|
ypdb_close_db(db); |
711 |
|
|
return(secure); |
712 |
|
|
} |