2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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.
100 * Dynamically allocate an entry for the circular queue.
101 * Return a NULL pointer on failure.
103 static struct circleq_entry *
106 register struct circleq_entry *q;
108 q = malloc(sizeof(struct circleq_entry));
110 yp_error("failed to malloc() circleq entry");
113 bzero((char *)q, sizeof(struct circleq_entry));
114 q->dbptr = malloc(sizeof(struct dbent));
115 if (q->dbptr == NULL) {
116 yp_error("failed to malloc() circleq entry");
120 bzero((char *)q->dbptr, sizeof(struct dbent));
126 * Free a previously allocated circular queue
130 yp_free_qent(struct circleq_entry *q)
133 * First, close the database. In theory, this is also
134 * supposed to free the resources allocated by the DB
135 * package, including the memory pointed to by q->dbptr->key.
136 * This means we don't have to free q->dbptr->key here.
139 (void)(q->dbptr->dbp->close)(q->dbptr->dbp);
140 q->dbptr->dbp = NULL;
143 * Then free the database name, which was strdup()'ed.
145 free(q->dbptr->name);
148 * Free the rest of the dbent struct.
154 * Free the circleq struct.
163 * Zorch a single entry in the dbent queue and release
164 * all its resources. (This always removes the last entry
170 register struct circleq_entry *qptr;
172 qptr = TAILQ_LAST(&qhead, circlehead);
173 TAILQ_REMOVE(&qhead, qptr, links);
181 * Close all databases, erase all database names and empty the queue.
186 register struct circleq_entry *qptr;
188 while (!TAILQ_EMPTY(&qhead)) {
189 qptr = TAILQ_FIRST(&qhead); /* save this */
190 TAILQ_REMOVE(&qhead, qptr, links);
198 static char *inter_string = "YP_INTERDOMAIN";
199 static char *secure_string = "YP_SECURE";
200 static int inter_sz = sizeof("YP_INTERDOMAIN") - 1;
201 static int secure_sz = sizeof("YP_SECURE") - 1;
206 DBT key = { NULL, 0 }, data = { NULL, 0 };
209 key.data = inter_string;
212 if (!(dbp->get)(dbp, &key, &data, 0))
213 flags |= YP_INTERDOMAIN;
215 key.data = secure_string;
216 key.size = secure_sz;
218 if (!(dbp->get)(dbp, &key, &data, 0))
225 yp_testflag(char *map, char *domain, int flag)
227 char buf[MAXPATHLEN + 2];
228 register struct circleq_entry *qptr;
230 if (map == NULL || domain == NULL)
237 TAILQ_FOREACH(qptr, &qhead, links) {
238 if (!strcmp(qptr->dbptr->name, buf)) {
239 if (qptr->dbptr->flags & flag)
246 if (yp_open_db_cache(domain, map, NULL, 0) == NULL)
249 if (TAILQ_FIRST(&qhead)->dbptr->flags & flag)
256 * Add a DB handle and database name to the cache. We only maintain
257 * fixed number of entries in the cache, so if we're asked to store
258 * a new entry when all our slots are already filled, we have to kick
259 * out the entry in the last slot to make room.
262 yp_cache_db(DB *dbp, char *name, int 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.
321 yp_find_db(const char *name, const char *key, int size)
323 register struct circleq_entry *qptr;
325 TAILQ_FOREACH(qptr, &qhead, links) {
326 if (!strcmp(qptr->dbptr->name, name)) {
328 if (size != qptr->dbptr->size ||
329 strncmp(qptr->dbptr->key, key, size))
332 if (qptr->dbptr->size)
335 if (qptr != TAILQ_FIRST(&qhead)) {
336 TAILQ_REMOVE(&qhead, qptr, links);
337 TAILQ_INSERT_HEAD(&qhead, qptr, links);
339 return(qptr->dbptr->dbp);
347 * Open a DB database and cache the handle for later use. We first
348 * check the cache to see if the required database is already open.
349 * If so, we fetch the handle from the cache. If not, we try to open
350 * the database and save the handle in the cache for later use.
353 yp_open_db_cache(const char *domain, const char *map, const char *key,
357 char buf[MAXPATHLEN + 2];
359 snprintf(buf, sizeof(buf), "%s/%s", domain, map);
367 if ((dbp = yp_find_db(buf, key, size)) != NULL) {
370 if ((dbp = yp_open_db(domain, map)) != NULL) {
371 if (yp_cache_db(dbp, buf, size)) {
372 (void)(dbp->close)(dbp);
384 * Open a DB database.
387 yp_open_db(const char *domain, const char *map)
390 char buf[MAXPATHLEN + 2];
394 if (map[0] == '.' || strchr(map, '/')) {
395 yp_errno = YP_BADARGS;
400 if (yp_validdomain(domain)) {
405 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
410 dbp = dbopen(buf, O_RDONLY, PERM_SECURE, DB_HASH, NULL);
417 * We ran out of file descriptors. Nuke an
418 * open one and try again.
420 yp_error("ran out of file descriptors");
441 * Database access routines.
443 * - yp_get_record(): retrieve an arbitrary key/data pair given one key
446 * - yp_first_record(): retrieve first key/data base in a database.
448 * - yp_next_record(): retrieve key/data pair that sequentially follows
449 * the supplied key value in the database.
454 yp_get_record(DB *dbp, const DBT *key, DBT *data, int allow)
457 yp_get_record(const char *domain, const char *map,
458 const DBT *key, DBT *data, int allow)
466 static unsigned char buf[YPMAXRECORD];
470 yp_error("looking up key [%.*s]",
471 (int)key->size, (char *)key->data);
474 * Avoid passing back magic "YP_*" entries unless
475 * the caller specifically requested them by setting
478 if (!allow && !strncmp(key->data, "YP_", 3))
482 if ((dbp = yp_open_db(domain, map)) == NULL) {
487 if ((rval = (dbp->get)(dbp, key, data, 0)) != 0) {
489 TAILQ_FIRST(&qhead)->dbptr->size = 0;
491 (void)(dbp->close)(dbp);
500 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
501 (int)key->size, (char *)key->data,
502 (int)data->size, (char *)data->data);
505 if (TAILQ_FIRST(&qhead)->dbptr->size) {
506 TAILQ_FIRST(&qhead)->dbptr->key = "";
507 TAILQ_FIRST(&qhead)->dbptr->size = 0;
510 bcopy(data->data, &buf, data->size);
512 (void)(dbp->close)(dbp);
519 yp_first_record(const DB *dbp, DBT *key, DBT *data, int allow)
523 static unsigned char buf[YPMAXRECORD];
527 yp_error("retrieving first key in map");
529 if ((rval = (dbp->seq)(dbp,key,data,R_FIRST)) != 0) {
531 TAILQ_FIRST(&qhead)->dbptr->size = 0;
539 /* Avoid passing back magic "YP_*" records. */
540 while (!strncmp(key->data, "YP_", 3) && !allow) {
541 if ((rval = (dbp->seq)(dbp,key,data,R_NEXT)) != 0) {
543 TAILQ_FIRST(&qhead)->dbptr->size = 0;
553 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
554 (int)key->size, (char *)key->data,
555 (int)data->size, (char *)data->data);
558 if (TAILQ_FIRST(&qhead)->dbptr->size) {
559 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
560 TAILQ_FIRST(&qhead)->dbptr->size = key->size;
563 bcopy(data->data, &buf, data->size);
571 yp_next_record(const DB *dbp, DBT *key, DBT *data, int all, int allow)
573 static DBT lkey = { NULL, 0 };
574 static DBT ldata = { NULL, 0 };
577 static unsigned char keybuf[YPMAXRECORD];
578 static unsigned char datbuf[YPMAXRECORD];
581 if (key == NULL || !key->size || key->data == NULL) {
582 rval = yp_first_record(dbp,key,data,allow);
583 if (rval == YP_NOKEY)
587 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
588 TAILQ_FIRST(&qhead)->dbptr->size = key->size;
595 yp_error("retrieving next key, previous was: [%.*s]",
596 (int)key->size, (char *)key->data);
600 if (TAILQ_FIRST(&qhead)->dbptr->key == NULL) {
602 (dbp->seq)(dbp,&lkey,&ldata,R_FIRST);
603 while (key->size != lkey.size ||
604 strncmp(key->data, lkey.data,
606 if ((dbp->seq)(dbp,&lkey,&ldata,R_NEXT)) {
608 TAILQ_FIRST(&qhead)->dbptr->size = 0;
618 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
620 TAILQ_FIRST(&qhead)->dbptr->size = 0;
625 /* Avoid passing back magic "YP_*" records. */
626 while (!strncmp(key->data, "YP_", 3) && !allow)
627 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
629 TAILQ_FIRST(&qhead)->dbptr->size = 0;
635 yp_error("result of lookup: key: [%.*s] data: [%.*s]",
636 (int)key->size, (char *)key->data,
637 (int)data->size, (char *)data->data);
640 if (TAILQ_FIRST(&qhead)->dbptr->size) {
641 TAILQ_FIRST(&qhead)->dbptr->key = key->data;
642 TAILQ_FIRST(&qhead)->dbptr->size = key->size;
645 bcopy(key->data, &keybuf, key->size);
647 lkey.size = key->size;
648 bcopy(data->data, &datbuf, data->size);
649 data->data = &datbuf;
657 * Database glue functions.
660 static DB *yp_currmap_db = NULL;
661 static int yp_allow_db = 0;
664 yp_select_map(char *map, char *domain, keydat *key, int allow)
667 yp_currmap_db = yp_open_db_cache(domain, map, NULL, 0);
669 yp_currmap_db = yp_open_db_cache(domain, map,
678 yp_getbykey(keydat *key, valdat *val)
680 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
683 db_key.data = key->keydat_val;
684 db_key.size = key->keydat_len;
686 rval = yp_get_record(yp_currmap_db,
687 &db_key, &db_val, yp_allow_db);
689 if (rval == YP_TRUE) {
690 val->valdat_val = db_val.data;
691 val->valdat_len = db_val.size;
698 yp_firstbykey(keydat *key, valdat *val)
700 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
703 rval = yp_first_record(yp_currmap_db, &db_key, &db_val, yp_allow_db);
705 if (rval == YP_TRUE) {
706 key->keydat_val = db_key.data;
707 key->keydat_len = db_key.size;
708 val->valdat_val = db_val.data;
709 val->valdat_len = db_val.size;
716 yp_nextbykey(keydat *key, valdat *val)
718 DBT db_key = { NULL, 0 }, db_val = { NULL, 0 };
721 db_key.data = key->keydat_val;
722 db_key.size = key->keydat_len;
724 rval = yp_next_record(yp_currmap_db, &db_key, &db_val, 0, yp_allow_db);
726 if (rval == YP_TRUE) {
727 key->keydat_val = db_key.data;
728 key->keydat_len = db_key.size;
729 val->valdat_val = db_val.data;
730 val->valdat_len = db_val.size;