Home | History | Annotate | Line # | Download | only in common
ypdb.c revision 1.11
      1  1.11    lukem /*	$NetBSD: ypdb.c,v 1.11 2008/02/29 03:00:47 lukem Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 1990, 1993
      5   1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6   1.1  thorpej  * All rights reserved.
      7   1.1  thorpej  *
      8   1.1  thorpej  * This code is derived from software contributed to Berkeley by
      9   1.1  thorpej  * Margo Seltzer.
     10   1.1  thorpej  *
     11   1.1  thorpej  * This code is derived from ndbm module of BSD4.4 db (hash) by
     12   1.1  thorpej  * Mats O Jansson
     13   1.1  thorpej  *
     14   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15   1.1  thorpej  * modification, are permitted provided that the following conditions
     16   1.1  thorpej  * are met:
     17   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22   1.9      agc  * 3. Neither the name of the University nor the names of its contributors
     23   1.1  thorpej  *    may be used to endorse or promote products derived from this software
     24   1.1  thorpej  *    without specific prior written permission.
     25   1.1  thorpej  *
     26   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     27   1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     28   1.1  thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     30   1.1  thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1  thorpej  * SUCH DAMAGE.
     37   1.1  thorpej  */
     38   1.1  thorpej 
     39   1.4    lukem #include <sys/cdefs.h>
     40   1.4    lukem #ifndef lint
     41  1.11    lukem __RCSID("$NetBSD: ypdb.c,v 1.11 2008/02/29 03:00:47 lukem Exp $");
     42   1.4    lukem #endif
     43   1.4    lukem 
     44   1.1  thorpej #include <sys/param.h>
     45   1.1  thorpej #include <sys/types.h>
     46   1.4    lukem 
     47   1.4    lukem #include <db.h>
     48  1.10    lukem #include <err.h>
     49   1.4    lukem #include <errno.h>
     50  1.11    lukem #include <fcntl.h>
     51   1.1  thorpej #include <stdio.h>
     52  1.11    lukem #include <stdlib.h>
     53   1.1  thorpej #include <string.h>
     54  1.11    lukem #include <unistd.h>
     55   1.1  thorpej 
     56   1.5    lukem #include <rpcsvc/yp.h>
     57   1.5    lukem 
     58   1.1  thorpej #include "ypdb.h"
     59   1.1  thorpej 
     60  1.11    lukem static DBM	*_ypdb_dbopen(const char *, int, mode_t);
     61  1.11    lukem 
     62   1.1  thorpej /*
     63  1.10    lukem  * ypdb_open --
     64  1.11    lukem  *	dbopen(3) file, read-only.
     65  1.10    lukem  *	First ensure that file has a suffix of YPDB_SUFFIX.
     66  1.10    lukem  *	Try opening as a DB_BTREE first, then DB_HASH.
     67  1.10    lukem  *
     68   1.1  thorpej  * Returns:
     69  1.11    lukem  *	*DBM on success
     70   1.1  thorpej  *	 NULL on failure
     71   1.1  thorpej  */
     72   1.1  thorpej 
     73   1.3    lukem DBM *
     74  1.11    lukem ypdb_open(const char *file)
     75   1.1  thorpej {
     76  1.10    lukem 	char path[MAXPATHLEN];
     77  1.10    lukem 	const char *cp, *suffix;
     78   1.1  thorpej 
     79   1.4    lukem 	cp = strrchr(file, '.');
     80  1.10    lukem 	if (cp != NULL && strcmp(cp, YPDB_SUFFIX) == 0)
     81  1.10    lukem 		suffix = "";
     82  1.10    lukem 	else
     83  1.10    lukem 		suffix = YPDB_SUFFIX;
     84  1.10    lukem 	if (strlen(file) + strlen(suffix) > (sizeof(path) - 1)) {
     85  1.10    lukem 		warnx("File name `%s' is too long", file);
     86  1.10    lukem 		return (NULL);
     87  1.10    lukem 	}
     88  1.10    lukem 	snprintf(path, sizeof(path), "%s%s", file, suffix);
     89  1.11    lukem 	return _ypdb_dbopen(path, O_RDONLY, 0444);
     90  1.11    lukem }
     91  1.11    lukem 
     92  1.11    lukem /*
     93  1.11    lukem  * ypdb_mktemp --
     94  1.11    lukem  *	Create a temporary file using mkstemp(3) based on the
     95  1.11    lukem  *	template provided in file.
     96  1.11    lukem  *	dbopen(3) file, read-write, 0644 (modified by umask(2)).
     97  1.11    lukem  *	Try opening as a DB_BTREE first, then DB_HASH.
     98  1.11    lukem  *	file won't have YPDB_SUFFIX.
     99  1.11    lukem  *
    100  1.11    lukem  * Returns:
    101  1.11    lukem  *	*DBM on success; file now exists.
    102  1.11    lukem  *	 NULL on failure
    103  1.11    lukem  */
    104  1.11    lukem 
    105  1.11    lukem DBM *
    106  1.11    lukem ypdb_mktemp(char *file)
    107  1.11    lukem {
    108  1.11    lukem 	int fd = -1;
    109  1.11    lukem 	DBM *db = NULL;
    110  1.11    lukem 	mode_t myumask;
    111  1.11    lukem 	int save_errno;
    112  1.11    lukem 
    113  1.11    lukem 	if ((fd = mkstemp(file)) == -1)
    114  1.11    lukem 		return NULL;
    115  1.11    lukem 
    116  1.11    lukem 	myumask = umask(0);
    117  1.11    lukem 	(void)umask(myumask);
    118  1.11    lukem 	if (fchmod(fd, 0644 & ~myumask) == -1)
    119  1.11    lukem 		goto bad;
    120  1.11    lukem 
    121  1.11    lukem 	(void) close(fd);
    122  1.11    lukem 	fd = -1;
    123  1.11    lukem 
    124  1.11    lukem 	if ((db = _ypdb_dbopen(file, O_RDWR, 0644)) == NULL)
    125  1.11    lukem 		goto bad;
    126  1.11    lukem 
    127  1.11    lukem 	return db;
    128  1.11    lukem 
    129  1.11    lukem  bad:
    130  1.11    lukem 	save_errno = errno;
    131  1.11    lukem 	if (fd != 1)
    132  1.11    lukem 		(void) close(fd);
    133  1.11    lukem 	(void) unlink(file);
    134  1.11    lukem 	errno = save_errno;
    135  1.11    lukem 	return NULL;
    136  1.11    lukem }
    137  1.11    lukem 
    138  1.11    lukem /*
    139  1.11    lukem  * _ypdb_dbopen --
    140  1.11    lukem  *	dbopen(3) path with the flags & mode.
    141  1.11    lukem  *	Try opening as a DB_BTREE first, then DB_HASH.
    142  1.11    lukem  */
    143  1.11    lukem 
    144  1.11    lukem static DBM *
    145  1.11    lukem _ypdb_dbopen(const char *path, int flags, mode_t mode)
    146  1.11    lukem {
    147  1.11    lukem 	DBM *db;
    148  1.11    lukem 	BTREEINFO info;
    149   1.4    lukem 
    150   1.4    lukem 		/* try our btree format first */
    151   1.1  thorpej 	info.flags = 0;
    152   1.1  thorpej 	info.cachesize = 0;
    153   1.1  thorpej 	info.maxkeypage = 0;
    154   1.1  thorpej 	info.minkeypage = 0;
    155   1.1  thorpej 	info.psize = 0;
    156   1.1  thorpej 	info.compare = NULL;
    157   1.1  thorpej 	info.prefix = NULL;
    158   1.1  thorpej 	info.lorder = 0;
    159   1.4    lukem 	db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
    160   1.4    lukem 	if (db != NULL || errno != EFTYPE)
    161   1.4    lukem 		return (db);
    162   1.4    lukem 
    163   1.4    lukem 		/* fallback to standard hash (for sendmail's aliases.db) */
    164   1.4    lukem 	db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
    165   1.4    lukem 	return (db);
    166   1.1  thorpej }
    167   1.1  thorpej 
    168  1.11    lukem /*
    169  1.11    lukem  * ypdb_close --
    170  1.11    lukem  *	Close the db
    171  1.11    lukem  */
    172  1.11    lukem 
    173   1.3    lukem void
    174   1.8      wiz ypdb_close(DBM *db)
    175   1.1  thorpej {
    176   1.1  thorpej 	(void)(db->close)(db);
    177   1.1  thorpej }
    178   1.1  thorpej 
    179   1.1  thorpej /*
    180   1.1  thorpej  * Returns:
    181   1.1  thorpej  *	DATUM on success
    182   1.1  thorpej  *	NULL on failure
    183   1.1  thorpej  */
    184   1.1  thorpej 
    185   1.3    lukem datum
    186   1.8      wiz ypdb_fetch(DBM *db, datum key)
    187   1.1  thorpej {
    188   1.6    lukem 	datum retkey;
    189   1.6    lukem 	DBT nk, nd;
    190   1.1  thorpej 	int status;
    191   1.1  thorpej 
    192   1.6    lukem 	nk.data = key.dptr;
    193   1.7    lukem 	nk.size = key.dsize;
    194   1.6    lukem 	status = (db->get)(db, &nk, &nd, 0);
    195   1.1  thorpej 	if (status) {
    196   1.6    lukem 		retkey.dptr = NULL;
    197   1.6    lukem 		retkey.dsize = 0;
    198   1.6    lukem 	} else {
    199   1.6    lukem 		retkey.dptr = nd.data;
    200   1.7    lukem 		retkey.dsize = nd.size;
    201   1.1  thorpej 	}
    202   1.6    lukem 	return (retkey);
    203   1.1  thorpej }
    204   1.1  thorpej 
    205   1.1  thorpej /*
    206   1.1  thorpej  * Returns:
    207   1.1  thorpej  *	DATUM on success
    208   1.1  thorpej  *	NULL on failure
    209   1.1  thorpej  */
    210   1.1  thorpej 
    211   1.3    lukem datum
    212   1.8      wiz ypdb_firstkey(DBM *db)
    213   1.1  thorpej {
    214   1.1  thorpej 	int status;
    215   1.6    lukem 	datum retkey;
    216   1.6    lukem 	DBT nk, nd;
    217   1.1  thorpej 
    218   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_FIRST);
    219   1.6    lukem 	if (status) {
    220   1.1  thorpej 		retkey.dptr = NULL;
    221   1.6    lukem 		retkey.dsize = 0;
    222   1.6    lukem 	} else {
    223   1.6    lukem 		retkey.dptr = nk.data;
    224   1.7    lukem 		retkey.dsize = nk.size;
    225   1.6    lukem 	}
    226   1.1  thorpej 	return (retkey);
    227   1.1  thorpej }
    228   1.1  thorpej 
    229   1.1  thorpej /*
    230   1.1  thorpej  * Returns:
    231   1.1  thorpej  *	DATUM on success
    232   1.1  thorpej  *	NULL on failure
    233   1.1  thorpej  */
    234   1.1  thorpej 
    235   1.3    lukem datum
    236   1.8      wiz ypdb_nextkey(DBM *db)
    237   1.1  thorpej {
    238   1.1  thorpej 	int status;
    239   1.6    lukem 	datum retkey;
    240   1.6    lukem 	DBT nk, nd;
    241   1.1  thorpej 
    242   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_NEXT);
    243   1.6    lukem 	if (status) {
    244   1.1  thorpej 		retkey.dptr = NULL;
    245   1.6    lukem 		retkey.dsize = 0;
    246   1.6    lukem 	} else {
    247   1.6    lukem 		retkey.dptr = nk.data;
    248   1.7    lukem 		retkey.dsize = nk.size;
    249   1.6    lukem 	}
    250   1.1  thorpej 	return (retkey);
    251   1.1  thorpej }
    252   1.1  thorpej 
    253   1.1  thorpej /*
    254   1.1  thorpej  * Returns:
    255   1.1  thorpej  *	DATUM on success
    256   1.1  thorpej  *	NULL on failure
    257   1.1  thorpej  */
    258   1.1  thorpej 
    259   1.3    lukem datum
    260   1.8      wiz ypdb_setkey(DBM *db, datum key)
    261   1.1  thorpej {
    262   1.1  thorpej 	int status;
    263   1.6    lukem 	DBT nk, nd;
    264   1.6    lukem 
    265   1.6    lukem 	nk.data = key.dptr;
    266   1.7    lukem 	nk.size = key.dsize;
    267   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_CURSOR);
    268   1.6    lukem 	if (status) {
    269   1.1  thorpej 		key.dptr = NULL;
    270   1.6    lukem 		key.dsize = 0;
    271   1.6    lukem 	}
    272   1.1  thorpej 	return (key);
    273   1.1  thorpej }
    274   1.1  thorpej 
    275   1.1  thorpej /*
    276   1.1  thorpej  * Returns:
    277   1.1  thorpej  *	 0 on success
    278   1.1  thorpej  *	<0 failure
    279   1.1  thorpej  */
    280   1.1  thorpej 
    281   1.1  thorpej int
    282   1.8      wiz ypdb_delete(DBM *db, datum key)
    283   1.1  thorpej {
    284   1.1  thorpej 	int status;
    285   1.6    lukem 	DBT nk;
    286   1.1  thorpej 
    287   1.6    lukem 	nk.data = key.dptr;
    288   1.7    lukem 	nk.size = key.dsize;
    289   1.6    lukem 	status = (db->del)(db, &nk, 0);
    290   1.1  thorpej 	if (status)
    291   1.1  thorpej 		return (-1);
    292   1.1  thorpej 	else
    293   1.1  thorpej 		return (0);
    294   1.1  thorpej }
    295   1.1  thorpej 
    296   1.1  thorpej /*
    297   1.1  thorpej  * Returns:
    298   1.1  thorpej  *	 0 on success
    299   1.1  thorpej  *	<0 failure
    300   1.1  thorpej  *	 1 if YPDB_INSERT and entry exists
    301   1.1  thorpej  */
    302   1.1  thorpej 
    303   1.1  thorpej int
    304   1.8      wiz ypdb_store(DBM *db, datum key, datum content, int flags)
    305   1.1  thorpej {
    306   1.6    lukem 	DBT nk, nd;
    307   1.6    lukem 
    308   1.5    lukem 	if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
    309   1.5    lukem 		return -1;
    310   1.6    lukem 	nk.data = key.dptr;
    311   1.7    lukem 	nk.size = key.dsize;
    312   1.6    lukem 	nd.data = content.dptr;
    313   1.7    lukem 	nd.size = content.dsize;
    314   1.6    lukem 	return ((db->put)(db, &nk, &nd,
    315   1.1  thorpej 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
    316   1.1  thorpej }
    317