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[] =
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 TAILQ_HEAD(circlehead, circleq_entry) qhead;
84 struct circleq_entry {
86 TAILQ_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 = TAILQ_LAST(&qhead, circlehead);
170 TAILQ_REMOVE(&qhead, qptr, links);
178 * Close all databases, erase all database names and empty the queue.
182 register struct circleq_entry *qptr;
184 while(!TAILQ_EMPTY(&qhead)) {
185 qptr = TAILQ_FIRST(&qhead); /* save this */
186 TAILQ_REMOVE(&qhead, qptr, 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 TAILQ_FOREACH(qptr, &qhead, links) {
236 if (!strcmp(qptr->dbptr->name, buf)) {
237 if (qptr->dbptr->flags & flag)
244 if (yp_open_db_cache(domain, map, NULL, 0) == NULL)
247 if (TAILQ_FIRST(&qhead)->dbptr->flags & flag)
254 * Add a DB handle and database name to the cache. We only maintain
255 * fixed number of entries in the cache, so if we're asked to store
256 * a new entry when all our slots are already filled, we have to kick
257 * out the entry in the last slot to make room.
259 static int yp_cache_db(dbp, name, size)
264 register struct circleq_entry *qptr;
266 if (numdbs == MAXDBS) {
268 yp_error("queue overflow -- releasing last slot");
273 * Allocate a new queue entry.
276 if ((qptr = yp_malloc_qent()) == NULL) {
277 yp_error("failed to allocate a new cache entry");
281 qptr->dbptr->dbp = dbp;
282 qptr->dbptr->name = strdup(name);
283 qptr->dbptr->size = size;
284 qptr->dbptr->key = NULL;
286 qptr->dbptr->flags = yp_setflags(dbp);
288 TAILQ_INSERT_HEAD(&qhead, qptr, links);
295 * Search the list for a database matching 'name.' If we find it,
296 * move it to the head of the list and return its DB handle. If
297 * not, just fail: yp_open_db_cache() will subsequently try to open
298 * the database itself and call yp_cache_db() to add it to the
301 * The search works like this:
303 * - The caller specifies the name of a database to locate. We try to
304 * find an entry in our queue with a matching name.
306 * - If the caller doesn't specify a key or size, we assume that the
307 * first entry that we encounter with a matching name is returned.
308 * This will result in matches regardless of the key/size values
309 * stored in the queue entry.
311 * - If the caller also specifies a key and length, we check to see
312 * if the key and length saved in the queue entry also matches.
313 * This lets us return a DB handle that's already positioned at the
314 * correct location within a database.
316 * - Once we have a match, it gets migrated to the top of the queue
317 * so that it will be easier to find if another request for
318 * the same database comes in later.
320 static DB *yp_find_db(name, key, size)
325 register struct circleq_entry *qptr;
327 TAILQ_FOREACH(qptr, &qhead, links) {
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 != TAILQ_FIRST(&qhead)) {
338 TAILQ_REMOVE(&qhead, qptr, links);
339 TAILQ_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 TAILQ_FIRST(&qhead)->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 (TAILQ_FIRST(&qhead)->dbptr->size) {
513 TAILQ_FIRST(&qhead)->dbptr->key = "";
514 TAILQ_FIRST(&qhead)->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 TAILQ_FIRST(&qhead)->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 TAILQ_FIRST(&qhead)->dbptr->size = 0;
563 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
564 key->size, key->data, data->size, data->data);
567 if (TAILQ_FIRST(&qhead)->dbptr->size) {
568 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
569 TAILQ_FIRST(&qhead)->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 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
601 TAILQ_FIRST(&qhead)->dbptr->size = key->size;
608 yp_error("retrieving next key, previous was: [%.*s]",
609 key->size, key->data);
613 if (TAILQ_FIRST(&qhead)->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 TAILQ_FIRST(&qhead)->dbptr->size = 0;
631 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
633 TAILQ_FIRST(&qhead)->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 TAILQ_FIRST(&qhead)->dbptr->size = 0;
648 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
649 key->size, key->data, data->size, data->data);
652 if (TAILQ_FIRST(&qhead)->dbptr->size) {
653 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
654 TAILQ_FIRST(&qhead)->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;