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