1 /* $NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * This code is derived from ndbm module of BSD4.4 db (hash) by
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $");
44 #include <sys/param.h>
45 #include <sys/types.h>
56 #include <rpcsvc/yp.h>
60 static DBM
*_ypdb_dbopen(const char *, int, mode_t
);
64 * dbopen(3) file, read-only.
65 * First ensure that file has a suffix of YPDB_SUFFIX.
66 * Try opening as a DB_BTREE first, then DB_HASH.
74 ypdb_open(const char *file
)
76 char path
[MAXPATHLEN
];
77 const char *cp
, *suffix
;
79 cp
= strrchr(file
, '.');
80 if (cp
!= NULL
&& strcmp(cp
, YPDB_SUFFIX
) == 0)
84 if (strlen(file
) + strlen(suffix
) > (sizeof(path
) - 1)) {
85 warnx("File name `%s' is too long", file
);
88 snprintf(path
, sizeof(path
), "%s%s", file
, suffix
);
89 return _ypdb_dbopen(path
, O_RDONLY
, 0444);
94 * Create a temporary file using mkstemp(3) based on the
95 * template provided in file.
96 * dbopen(3) file, read-write, 0644 (modified by umask(2)).
97 * Try opening as a DB_BTREE first, then DB_HASH.
98 * file won't have YPDB_SUFFIX.
101 * *DBM on success; file now exists.
106 ypdb_mktemp(char *file
)
113 if ((fd
= mkstemp(file
)) == -1)
117 (void)umask(myumask
);
118 if (fchmod(fd
, 0644 & ~myumask
) == -1)
124 if ((db
= _ypdb_dbopen(file
, O_RDWR
, 0644)) == NULL
)
140 * dbopen(3) path with the flags & mode.
141 * Try opening as a DB_BTREE first, then DB_HASH.
145 _ypdb_dbopen(const char *path
, int flags
, mode_t mode
)
150 /* try our btree format first */
159 db
= (DBM
*)dbopen(path
, flags
, mode
, DB_BTREE
, (void *)&info
);
160 if (db
!= NULL
|| errno
!= EFTYPE
)
163 /* fallback to standard hash (for sendmail's aliases.db) */
164 db
= (DBM
*)dbopen(path
, flags
, mode
, DB_HASH
, NULL
);
176 (void)(db
->close
)(db
);
186 ypdb_fetch(DBM
*db
, datum key
)
194 status
= (db
->get
)(db
, &nk
, &nd
, 0);
199 retkey
.dptr
= nd
.data
;
200 retkey
.dsize
= nd
.size
;
212 ypdb_firstkey(DBM
*db
)
218 status
= (db
->seq
)(db
, &nk
, &nd
, R_FIRST
);
223 retkey
.dptr
= nk
.data
;
224 retkey
.dsize
= nk
.size
;
236 ypdb_nextkey(DBM
*db
)
242 status
= (db
->seq
)(db
, &nk
, &nd
, R_NEXT
);
247 retkey
.dptr
= nk
.data
;
248 retkey
.dsize
= nk
.size
;
260 ypdb_setkey(DBM
*db
, datum key
)
267 status
= (db
->seq
)(db
, &nk
, &nd
, R_CURSOR
);
282 ypdb_delete(DBM
*db
, datum key
)
289 status
= (db
->del
)(db
, &nk
, 0);
300 * 1 if YPDB_INSERT and entry exists
304 ypdb_store(DBM
*db
, datum key
, datum content
, int flags
)
308 if (key
.dsize
> YPMAXRECORD
|| content
.dsize
> YPMAXRECORD
)
312 nd
.data
= content
.dptr
;
313 nd
.size
= content
.dsize
;
314 return ((db
->put
)(db
, &nk
, &nd
,
315 (flags
== YPDB_INSERT
) ? R_NOOVERWRITE
: 0));