3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char rcsid[] =
35 "$FreeBSD: src/usr.sbin/ypserv/yp_dblookup.c,v 1.17.2.1 2002/02/15 00:47:00 des Exp $";
48 #include <sys/param.h>
49 #include <rpcsvc/yp.h>
50 #include "yp_extern.h"
53 enum ypstat yp_errno = YP_TRUE;
55 #define PERM_SECURE (S_IRUSR|S_IWUSR)
60 2048 * 512, /* cachesize */
66 #include <sys/queue.h>
72 static int numdbs = 0;
82 static CIRCLEQ_HEAD(circlehead, circleq_entry) qhead;
84 struct circleq_entry {
86 CIRCLEQ_ENTRY(circleq_entry) links;
90 * Initialize the circular queue.
99 * Dynamically allocate an entry for the circular queue.
100 * Return a NULL pointer on failure.
102 static struct circleq_entry *yp_malloc_qent()
104 register struct circleq_entry *q;
106 q = (struct circleq_entry *)malloc(sizeof(struct circleq_entry));
108 yp_error("failed to malloc() circleq entry");
111 bzero((char *)q, sizeof(struct circleq_entry));
112 q->dbptr = (struct dbent *)malloc(sizeof(struct dbent));
113 if (q->dbptr == NULL) {
114 yp_error("failed to malloc() circleq entry");
118 bzero((char *)q->dbptr, sizeof(struct dbent));
124 * Free a previously allocated circular queue
127 static void yp_free_qent(q)
128 struct circleq_entry *q;
131 * First, close the database. In theory, this is also
132 * supposed to free the resources allocated by the DB
133 * package, including the memory pointed to by q->dbptr->key.
134 * This means we don't have to free q->dbptr->key here.
137 (void)(q->dbptr->dbp->close)(q->dbptr->dbp);
138 q->dbptr->dbp = NULL;
141 * Then free the database name, which was strdup()'ed.
143 free(q->dbptr->name);
146 * Free the rest of the dbent struct.
152 * Free the circleq struct.
161 * Zorch a single entry in the dbent queue and release
162 * all its resources. (This always removes the last entry
165 static void yp_flush()
167 register struct circleq_entry *qptr;
169 qptr = qhead.cqh_last;
170 CIRCLEQ_REMOVE(&qhead, qptr, links);
178 * Close all databases, erase all database names and empty the queue.
182 register struct circleq_entry *qptr;
184 while (qhead.cqh_first != (void *)&qhead) {
185 qptr = qhead.cqh_first; /* save this */
186 CIRCLEQ_REMOVE(&qhead, qhead.cqh_first, links);
194 static char *inter_string = "YP_INTERDOMAIN";
195 static char *secure_string = "YP_SECURE";
196 static int inter_sz = sizeof("YP_INTERDOMAIN") - 1;
197 static int secure_sz = sizeof("YP_SECURE") - 1;
199 static int yp_setflags(dbp)
202 DBT key = { NULL, 0 }, data = { NULL, 0 };
205 key.data = inter_string;
208 if (!(dbp->get)(dbp, &key, &data, 0))
209 flags |= YP_INTERDOMAIN;
211 key.data = secure_string;
212 key.size = secure_sz;
214 if (!(dbp->get)(dbp, &key, &data, 0))
220 int yp_testflag(map, domain, flag)
225 char buf[MAXPATHLEN + 2];
226 register struct circleq_entry *qptr;
228 if (map == NULL || domain == NULL)
235 for (qptr = qhead.cqh_first; qptr != (void *)&qhead;
236 qptr = qptr->links.cqe_next) {
237 if (!strcmp(qptr->dbptr->name, buf)) {
238 if (qptr->dbptr->flags & flag)
245 if (yp_open_db_cache(domain, map, NULL, 0) == NULL)
248 if (qhead.cqh_first->dbptr->flags & flag)
255 * Add a DB handle and database name to the cache. We only maintain
256 * fixed number of entries in the cache, so if we're asked to store
257 * a new entry when all our slots are already filled, we have to kick
258 * out the entry in the last slot to make room.
260 static int yp_cache_db(dbp, name, size)
265 register struct circleq_entry *qptr;
267 if (numdbs == MAXDBS) {
269 yp_error("queue overflow -- releasing last slot");
274 * Allocate a new queue entry.
277 if ((qptr = yp_malloc_qent()) == NULL) {
278 yp_error("failed to allocate a new cache entry");
282 qptr->dbptr->dbp = dbp;
283 qptr->dbptr->name = strdup(name);
284 qptr->dbptr->size = size;
285 qptr->dbptr->key = NULL;
287 qptr->dbptr->flags = yp_setflags(dbp);
289 CIRCLEQ_INSERT_HEAD(&qhead, qptr, links);
296 * Search the list for a database matching 'name.' If we find it,
297 * move it to the head of the list and return its DB handle. If
298 * not, just fail: yp_open_db_cache() will subsequently try to open
299 * the database itself and call yp_cache_db() to add it to the
302 * The search works like this:
304 * - The caller specifies the name of a database to locate. We try to
305 * find an entry in our queue with a matching name.
307 * - If the caller doesn't specify a key or size, we assume that the
308 * first entry that we encounter with a matching name is returned.
309 * This will result in matches regardless of the key/size values
310 * stored in the queue entry.
312 * - If the caller also specifies a key and length, we check to see
313 * if the key and length saved in the queue entry also matches.
314 * This lets us return a DB handle that's already positioned at the
315 * correct location within a database.
317 * - Once we have a match, it gets migrated to the top of the queue
318 * so that it will be easier to find if another request for
319 * the same database comes in later.
321 static DB *yp_find_db(name, key, size)
326 register struct circleq_entry *qptr;
328 for (qptr = qhead.cqh_first; qptr != (void *)&qhead;
329 qptr = qptr->links.cqe_next) {
330 if (!strcmp(qptr->dbptr->name, name)) {
332 if (size != qptr->dbptr->size ||
333 strncmp(qptr->dbptr->key, key, size))
336 if (qptr->dbptr->size)
339 if (qptr != qhead.cqh_first) {
340 CIRCLEQ_REMOVE(&qhead, qptr, links);
341 CIRCLEQ_INSERT_HEAD(&qhead, qptr, links);
343 return(qptr->dbptr->dbp);
351 * Open a DB database and cache the handle for later use. We first
352 * check the cache to see if the required database is already open.
353 * If so, we fetch the handle from the cache. If not, we try to open
354 * the database and save the handle in the cache for later use.
356 DB *yp_open_db_cache(domain, map, key, size)
363 char buf[MAXPATHLEN + 2];
365 snprintf(buf, sizeof(buf), "%s/%s", domain, map);
373 if ((dbp = yp_find_db((char *)&buf, key, size)) != NULL) {
376 if ((dbp = yp_open_db(domain, map)) != NULL) {
377 if (yp_cache_db(dbp, (char *)&buf, size)) {
378 (void)(dbp->close)(dbp);
390 * Open a DB database.
392 DB *yp_open_db(domain, map)
397 char buf[MAXPATHLEN + 2];
401 if (map[0] == '.' || strchr(map, '/')) {
402 yp_errno = YP_BADARGS;
407 if (yp_validdomain(domain)) {
412 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
417 dbp = dbopen(buf,O_RDONLY, PERM_SECURE, DB_HASH, NULL);
424 * We ran out of file descriptors. Nuke an
425 * open one and try again.
427 yp_error("ran out of file descriptors");
448 * Database access routines.
450 * - yp_get_record(): retrieve an arbitrary key/data pair given one key
453 * - yp_first_record(): retrieve first key/data base in a database.
455 * - yp_next_record(): retrieve key/data pair that sequentially follows
456 * the supplied key value in the database.
460 int yp_get_record(dbp,key,data,allow)
463 int yp_get_record(domain,map,key,data,allow)
476 static unsigned char buf[YPMAXRECORD];
480 yp_error("looking up key [%.*s]",
481 key->size, key->data);
484 * Avoid passing back magic "YP_*" entries unless
485 * the caller specifically requested them by setting
488 if (!allow && !strncmp(key->data, "YP_", 3))
492 if ((dbp = yp_open_db(domain, map)) == NULL) {
497 if ((rval = (dbp->get)(dbp, key, data, 0)) != 0) {
499 qhead.cqh_first->dbptr->size = 0;
501 (void)(dbp->close)(dbp);
510 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
511 key->size, key->data, data->size, data->data);
514 if (qhead.cqh_first->dbptr->size) {
515 qhead.cqh_first->dbptr->key = "";
516 qhead.cqh_first->dbptr->size = 0;
519 bcopy((char *)data->data, (char *)&buf, data->size);
520 data->data = (void *)&buf;
521 (void)(dbp->close)(dbp);
527 int yp_first_record(dbp,key,data,allow)
535 static unsigned char buf[YPMAXRECORD];
539 yp_error("retrieving first key in map");
541 if ((rval = (dbp->seq)(dbp,key,data,R_FIRST)) != 0) {
543 qhead.cqh_first->dbptr->size = 0;
551 /* Avoid passing back magic "YP_*" records. */
552 while (!strncmp(key->data, "YP_", 3) && !allow) {
553 if ((rval = (dbp->seq)(dbp,key,data,R_NEXT)) != 0) {
555 qhead.cqh_first->dbptr->size = 0;
565 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
566 key->size, key->data, data->size, data->data);
569 if (qhead.cqh_first->dbptr->size) {
570 qhead.cqh_first->dbptr->key = key->data;
571 qhead.cqh_first->dbptr->size = key->size;
574 bcopy((char *)data->data, (char *)&buf, data->size);
575 data->data = (void *)&buf;
581 int yp_next_record(dbp,key,data,all,allow)
588 static DBT lkey = { NULL, 0 };
589 static DBT ldata = { NULL, 0 };
592 static unsigned char keybuf[YPMAXRECORD];
593 static unsigned char datbuf[YPMAXRECORD];
596 if (key == NULL || !key->size || key->data == NULL) {
597 rval = yp_first_record(dbp,key,data,allow);
598 if (rval == YP_NOKEY)
602 qhead.cqh_first->dbptr->key = key->data;
603 qhead.cqh_first->dbptr->size = key->size;
610 yp_error("retrieving next key, previous was: [%.*s]",
611 key->size, key->data);
615 if (qhead.cqh_first->dbptr->key == NULL) {
617 (dbp->seq)(dbp,&lkey,&ldata,R_FIRST);
618 while (key->size != lkey.size ||
619 strncmp((char *)key->data, lkey.data,
621 if ((dbp->seq)(dbp,&lkey,&ldata,R_NEXT)) {
623 qhead.cqh_first->dbptr->size = 0;
633 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
635 qhead.cqh_first->dbptr->size = 0;
640 /* Avoid passing back magic "YP_*" records. */
641 while (!strncmp(key->data, "YP_", 3) && !allow)
642 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
644 qhead.cqh_first->dbptr->size = 0;
650 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
651 key->size, key->data, data->size, data->data);
654 if (qhead.cqh_first->dbptr->size) {
655 qhead.cqh_first->dbptr->key = key->data;
656 qhead.cqh_first->dbptr->size = key->size;
659 bcopy((char *)key->data, (char *)&keybuf, key->size);
660 lkey.data = (void *)&keybuf;
661 lkey.size = key->size;
662 bcopy((char *)data->data, (char *)&datbuf, data->size);
663 data->data = (void *)&datbuf;
671 * Database glue functions.
674 static DB *yp_currmap_db = NULL;
675 static int yp_allow_db = 0;
677 ypstat yp_select_map(map, domain, key, allow)
684 yp_currmap_db = yp_open_db_cache(domain, map, NULL, 0);
686 yp_currmap_db = yp_open_db_cache(domain, map,
694 ypstat yp_getbykey(key, val)
698 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
701 db_key.data = key->keydat_val;
702 db_key.size = key->keydat_len;
704 rval = yp_get_record(yp_currmap_db,
705 &db_key, &db_val, yp_allow_db);
707 if (rval == YP_TRUE) {
708 val->valdat_val = db_val.data;
709 val->valdat_len = db_val.size;
715 ypstat yp_firstbykey(key, val)
719 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
722 rval = yp_first_record(yp_currmap_db, &db_key, &db_val, yp_allow_db);
724 if (rval == YP_TRUE) {
725 key->keydat_val = db_key.data;
726 key->keydat_len = db_key.size;
727 val->valdat_val = db_val.data;
728 val->valdat_len = db_val.size;
734 ypstat yp_nextbykey(key, val)
738 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
741 db_key.data = key->keydat_val;
742 db_key.size = key->keydat_len;
744 rval = yp_next_record(yp_currmap_db, &db_key, &db_val, 0, yp_allow_db);
746 if (rval == YP_TRUE) {
747 key->keydat_val = db_key.data;
748 key->keydat_len = db_key.size;
749 val->valdat_val = db_val.data;
750 val->valdat_len = db_val.size;