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
32 * $FreeBSD: src/usr.sbin/ypserv/yp_dblookup.c,v 1.17.2.1 2002/02/15 00:47:00 des Exp $
33 * $DragonFly: src/usr.sbin/ypserv/yp_dblookup.c,v 1.2 2003/06/17 04:30:04 dillon Exp $
46 #include <sys/param.h>
47 #include <rpcsvc/yp.h>
48 #include "yp_extern.h"
51 enum ypstat yp_errno = YP_TRUE;
53 #define PERM_SECURE (S_IRUSR|S_IWUSR)
58 2048 * 512, /* cachesize */
64 #include <sys/queue.h>
70 static int numdbs = 0;
80 static CIRCLEQ_HEAD(circlehead, circleq_entry) qhead;
82 struct circleq_entry {
84 CIRCLEQ_ENTRY(circleq_entry) links;
88 * Initialize the circular queue.
97 * Dynamically allocate an entry for the circular queue.
98 * Return a NULL pointer on failure.
100 static struct circleq_entry *yp_malloc_qent()
102 register struct circleq_entry *q;
104 q = (struct circleq_entry *)malloc(sizeof(struct circleq_entry));
106 yp_error("failed to malloc() circleq entry");
109 bzero((char *)q, sizeof(struct circleq_entry));
110 q->dbptr = (struct dbent *)malloc(sizeof(struct dbent));
111 if (q->dbptr == NULL) {
112 yp_error("failed to malloc() circleq entry");
116 bzero((char *)q->dbptr, sizeof(struct dbent));
122 * Free a previously allocated circular queue
125 static void yp_free_qent(q)
126 struct circleq_entry *q;
129 * First, close the database. In theory, this is also
130 * supposed to free the resources allocated by the DB
131 * package, including the memory pointed to by q->dbptr->key.
132 * This means we don't have to free q->dbptr->key here.
135 (void)(q->dbptr->dbp->close)(q->dbptr->dbp);
136 q->dbptr->dbp = NULL;
139 * Then free the database name, which was strdup()'ed.
141 free(q->dbptr->name);
144 * Free the rest of the dbent struct.
150 * Free the circleq struct.
159 * Zorch a single entry in the dbent queue and release
160 * all its resources. (This always removes the last entry
163 static void yp_flush()
165 register struct circleq_entry *qptr;
167 qptr = qhead.cqh_last;
168 CIRCLEQ_REMOVE(&qhead, qptr, links);
176 * Close all databases, erase all database names and empty the queue.
180 register struct circleq_entry *qptr;
182 while (qhead.cqh_first != (void *)&qhead) {
183 qptr = qhead.cqh_first; /* save this */
184 CIRCLEQ_REMOVE(&qhead, qhead.cqh_first, links);
192 static char *inter_string = "YP_INTERDOMAIN";
193 static char *secure_string = "YP_SECURE";
194 static int inter_sz = sizeof("YP_INTERDOMAIN") - 1;
195 static int secure_sz = sizeof("YP_SECURE") - 1;
197 static int yp_setflags(dbp)
200 DBT key = { NULL, 0 }, data = { NULL, 0 };
203 key.data = inter_string;
206 if (!(dbp->get)(dbp, &key, &data, 0))
207 flags |= YP_INTERDOMAIN;
209 key.data = secure_string;
210 key.size = secure_sz;
212 if (!(dbp->get)(dbp, &key, &data, 0))
218 int yp_testflag(map, domain, flag)
223 char buf[MAXPATHLEN + 2];
224 register struct circleq_entry *qptr;
226 if (map == NULL || domain == NULL)
233 for (qptr = qhead.cqh_first; qptr != (void *)&qhead;
234 qptr = qptr->links.cqe_next) {
235 if (!strcmp(qptr->dbptr->name, buf)) {
236 if (qptr->dbptr->flags & flag)
243 if (yp_open_db_cache(domain, map, NULL, 0) == NULL)
246 if (qhead.cqh_first->dbptr->flags & flag)
253 * Add a DB handle and database name to the cache. We only maintain
254 * fixed number of entries in the cache, so if we're asked to store
255 * a new entry when all our slots are already filled, we have to kick
256 * out the entry in the last slot to make room.
258 static int yp_cache_db(dbp, name, size)
263 register struct circleq_entry *qptr;
265 if (numdbs == MAXDBS) {
267 yp_error("queue overflow -- releasing last slot");
272 * Allocate a new queue entry.
275 if ((qptr = yp_malloc_qent()) == NULL) {
276 yp_error("failed to allocate a new cache entry");
280 qptr->dbptr->dbp = dbp;
281 qptr->dbptr->name = strdup(name);
282 qptr->dbptr->size = size;
283 qptr->dbptr->key = NULL;
285 qptr->dbptr->flags = yp_setflags(dbp);
287 CIRCLEQ_INSERT_HEAD(&qhead, qptr, links);
294 * Search the list for a database matching 'name.' If we find it,
295 * move it to the head of the list and return its DB handle. If
296 * not, just fail: yp_open_db_cache() will subsequently try to open
297 * the database itself and call yp_cache_db() to add it to the
300 * The search works like this:
302 * - The caller specifies the name of a database to locate. We try to
303 * find an entry in our queue with a matching name.
305 * - If the caller doesn't specify a key or size, we assume that the
306 * first entry that we encounter with a matching name is returned.
307 * This will result in matches regardless of the key/size values
308 * stored in the queue entry.
310 * - If the caller also specifies a key and length, we check to see
311 * if the key and length saved in the queue entry also matches.
312 * This lets us return a DB handle that's already positioned at the
313 * correct location within a database.
315 * - Once we have a match, it gets migrated to the top of the queue
316 * so that it will be easier to find if another request for
317 * the same database comes in later.
319 static DB *yp_find_db(name, key, size)
324 register struct circleq_entry *qptr;
326 for (qptr = qhead.cqh_first; qptr != (void *)&qhead;
327 qptr = qptr->links.cqe_next) {
328 if (!strcmp(qptr->dbptr->name, name)) {
330 if (size != qptr->dbptr->size ||
331 strncmp(qptr->dbptr->key, key, size))
334 if (qptr->dbptr->size)
337 if (qptr != qhead.cqh_first) {
338 CIRCLEQ_REMOVE(&qhead, qptr, links);
339 CIRCLEQ_INSERT_HEAD(&qhead, qptr, links);
341 return(qptr->dbptr->dbp);
349 * Open a DB database and cache the handle for later use. We first
350 * check the cache to see if the required database is already open.
351 * If so, we fetch the handle from the cache. If not, we try to open
352 * the database and save the handle in the cache for later use.
354 DB *yp_open_db_cache(domain, map, key, size)
361 char buf[MAXPATHLEN + 2];
363 snprintf(buf, sizeof(buf), "%s/%s", domain, map);
371 if ((dbp = yp_find_db((char *)&buf, key, size)) != NULL) {
374 if ((dbp = yp_open_db(domain, map)) != NULL) {
375 if (yp_cache_db(dbp, (char *)&buf, size)) {
376 (void)(dbp->close)(dbp);
388 * Open a DB database.
390 DB *yp_open_db(domain, map)
395 char buf[MAXPATHLEN + 2];
399 if (map[0] == '.' || strchr(map, '/')) {
400 yp_errno = YP_BADARGS;
405 if (yp_validdomain(domain)) {
410 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
415 dbp = dbopen(buf,O_RDONLY, PERM_SECURE, DB_HASH, NULL);
422 * We ran out of file descriptors. Nuke an
423 * open one and try again.
425 yp_error("ran out of file descriptors");
446 * Database access routines.
448 * - yp_get_record(): retrieve an arbitrary key/data pair given one key
451 * - yp_first_record(): retrieve first key/data base in a database.
453 * - yp_next_record(): retrieve key/data pair that sequentially follows
454 * the supplied key value in the database.
458 int yp_get_record(dbp,key,data,allow)
461 int yp_get_record(domain,map,key,data,allow)
474 static unsigned char buf[YPMAXRECORD];
478 yp_error("looking up key [%.*s]",
479 key->size, key->data);
482 * Avoid passing back magic "YP_*" entries unless
483 * the caller specifically requested them by setting
486 if (!allow && !strncmp(key->data, "YP_", 3))
490 if ((dbp = yp_open_db(domain, map)) == NULL) {
495 if ((rval = (dbp->get)(dbp, key, data, 0)) != 0) {
497 qhead.cqh_first->dbptr->size = 0;
499 (void)(dbp->close)(dbp);
508 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
509 key->size, key->data, data->size, data->data);
512 if (qhead.cqh_first->dbptr->size) {
513 qhead.cqh_first->dbptr->key = "";
514 qhead.cqh_first->dbptr->size = 0;
517 bcopy((char *)data->data, (char *)&buf, data->size);
518 data->data = (void *)&buf;
519 (void)(dbp->close)(dbp);
525 int yp_first_record(dbp,key,data,allow)
533 static unsigned char buf[YPMAXRECORD];
537 yp_error("retrieving first key in map");
539 if ((rval = (dbp->seq)(dbp,key,data,R_FIRST)) != 0) {
541 qhead.cqh_first->dbptr->size = 0;
549 /* Avoid passing back magic "YP_*" records. */
550 while (!strncmp(key->data, "YP_", 3) && !allow) {
551 if ((rval = (dbp->seq)(dbp,key,data,R_NEXT)) != 0) {
553 qhead.cqh_first->dbptr->size = 0;
563 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
564 key->size, key->data, data->size, data->data);
567 if (qhead.cqh_first->dbptr->size) {
568 qhead.cqh_first->dbptr->key = key->data;
569 qhead.cqh_first->dbptr->size = key->size;
572 bcopy((char *)data->data, (char *)&buf, data->size);
573 data->data = (void *)&buf;
579 int yp_next_record(dbp,key,data,all,allow)
586 static DBT lkey = { NULL, 0 };
587 static DBT ldata = { NULL, 0 };
590 static unsigned char keybuf[YPMAXRECORD];
591 static unsigned char datbuf[YPMAXRECORD];
594 if (key == NULL || !key->size || key->data == NULL) {
595 rval = yp_first_record(dbp,key,data,allow);
596 if (rval == YP_NOKEY)
600 qhead.cqh_first->dbptr->key = key->data;
601 qhead.cqh_first->dbptr->size = key->size;
608 yp_error("retrieving next key, previous was: [%.*s]",
609 key->size, key->data);
613 if (qhead.cqh_first->dbptr->key == NULL) {
615 (dbp->seq)(dbp,&lkey,&ldata,R_FIRST);
616 while (key->size != lkey.size ||
617 strncmp((char *)key->data, lkey.data,
619 if ((dbp->seq)(dbp,&lkey,&ldata,R_NEXT)) {
621 qhead.cqh_first->dbptr->size = 0;
631 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
633 qhead.cqh_first->dbptr->size = 0;
638 /* Avoid passing back magic "YP_*" records. */
639 while (!strncmp(key->data, "YP_", 3) && !allow)
640 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
642 qhead.cqh_first->dbptr->size = 0;
648 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
649 key->size, key->data, data->size, data->data);
652 if (qhead.cqh_first->dbptr->size) {
653 qhead.cqh_first->dbptr->key = key->data;
654 qhead.cqh_first->dbptr->size = key->size;
657 bcopy((char *)key->data, (char *)&keybuf, key->size);
658 lkey.data = (void *)&keybuf;
659 lkey.size = key->size;
660 bcopy((char *)data->data, (char *)&datbuf, data->size);
661 data->data = (void *)&datbuf;
669 * Database glue functions.
672 static DB *yp_currmap_db = NULL;
673 static int yp_allow_db = 0;
675 ypstat yp_select_map(map, domain, key, allow)
682 yp_currmap_db = yp_open_db_cache(domain, map, NULL, 0);
684 yp_currmap_db = yp_open_db_cache(domain, map,
692 ypstat yp_getbykey(key, val)
696 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
699 db_key.data = key->keydat_val;
700 db_key.size = key->keydat_len;
702 rval = yp_get_record(yp_currmap_db,
703 &db_key, &db_val, yp_allow_db);
705 if (rval == YP_TRUE) {
706 val->valdat_val = db_val.data;
707 val->valdat_len = db_val.size;
713 ypstat yp_firstbykey(key, val)
717 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
720 rval = yp_first_record(yp_currmap_db, &db_key, &db_val, yp_allow_db);
722 if (rval == YP_TRUE) {
723 key->keydat_val = db_key.data;
724 key->keydat_len = db_key.size;
725 val->valdat_val = db_val.data;
726 val->valdat_len = db_val.size;
732 ypstat yp_nextbykey(key, val)
736 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
739 db_key.data = key->keydat_val;
740 db_key.size = key->keydat_len;
742 rval = yp_next_record(yp_currmap_db, &db_key, &db_val, 0, yp_allow_db);
744 if (rval == YP_TRUE) {
745 key->keydat_val = db_key.data;
746 key->keydat_len = db_key.size;
747 val->valdat_val = db_val.data;
748 val->valdat_len = db_val.size;