1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
4665823d0SBill Paul * Copyright (c) 1995
5665823d0SBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6665823d0SBill Paul *
7665823d0SBill Paul * Redistribution and use in source and binary forms, with or without
8665823d0SBill Paul * modification, are permitted provided that the following conditions
9665823d0SBill Paul * are met:
10665823d0SBill Paul * 1. Redistributions of source code must retain the above copyright
11665823d0SBill Paul * notice, this list of conditions and the following disclaimer.
12665823d0SBill Paul * 2. Redistributions in binary form must reproduce the above copyright
13665823d0SBill Paul * notice, this list of conditions and the following disclaimer in the
14665823d0SBill Paul * documentation and/or other materials provided with the distribution.
15665823d0SBill Paul * 3. All advertising materials mentioning features or use of this software
16665823d0SBill Paul * must display the following acknowledgement:
17665823d0SBill Paul * This product includes software developed by Bill Paul.
18665823d0SBill Paul * 4. Neither the name of the author nor the names of any co-contributors
19665823d0SBill Paul * may be used to endorse or promote products derived from this software
20665823d0SBill Paul * without specific prior written permission.
21665823d0SBill Paul *
22665823d0SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23665823d0SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24665823d0SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25665823d0SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26665823d0SBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27665823d0SBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28665823d0SBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29665823d0SBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30665823d0SBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31665823d0SBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32665823d0SBill Paul * SUCH DAMAGE.
33665823d0SBill Paul */
34665823d0SBill Paul
3522e9bc15SDavid E. O'Brien #include <sys/cdefs.h>
3627eed7e3SPhilippe Charnier #include <db.h>
3727eed7e3SPhilippe Charnier #include <errno.h>
3827eed7e3SPhilippe Charnier #include <fcntl.h>
3927eed7e3SPhilippe Charnier #include <limits.h>
4027eed7e3SPhilippe Charnier #include <paths.h>
4127eed7e3SPhilippe Charnier #include <stdio.h>
4227eed7e3SPhilippe Charnier #include <stdlib.h>
4327eed7e3SPhilippe Charnier #include <string.h>
4427eed7e3SPhilippe Charnier #include <unistd.h>
4527eed7e3SPhilippe Charnier #include <sys/stat.h>
4627eed7e3SPhilippe Charnier #include <rpcsvc/yp.h>
4727eed7e3SPhilippe Charnier #include "ypxfr_extern.h"
48665823d0SBill Paul
49665823d0SBill Paul #define PERM_SECURE (S_IRUSR|S_IWUSR)
50665823d0SBill Paul
51665823d0SBill Paul /*
52665823d0SBill Paul * Open a DB database read/write
53665823d0SBill Paul */
54dc584ddbSDag-Erling Smørgrav DB *
yp_open_db_rw(const char * domain,const char * map,const int flags)55dc584ddbSDag-Erling Smørgrav yp_open_db_rw(const char *domain, const char *map, const int flags)
56665823d0SBill Paul {
57665823d0SBill Paul DB *dbp;
58665823d0SBill Paul char buf[1025];
59665823d0SBill Paul
60665823d0SBill Paul
61db83a391SDimitry Andric yp_errno = YP_TRUE;
62665823d0SBill Paul
63665823d0SBill Paul if (map[0] == '.' || strchr(map, '/')) {
64db83a391SDimitry Andric yp_errno = YP_BADARGS;
65665823d0SBill Paul return (NULL);
66665823d0SBill Paul }
67665823d0SBill Paul
6816deb43aSBill Paul #define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT
69665823d0SBill Paul
7016deb43aSBill Paul snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
7116deb43aSBill Paul dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);
72665823d0SBill Paul
73665823d0SBill Paul if (dbp == NULL) {
74665823d0SBill Paul switch (errno) {
75665823d0SBill Paul case ENOENT:
76db83a391SDimitry Andric yp_errno = YP_NOMAP;
77665823d0SBill Paul break;
78665823d0SBill Paul case EFTYPE:
79db83a391SDimitry Andric yp_errno = YP_BADDB;
80665823d0SBill Paul break;
81665823d0SBill Paul default:
82db83a391SDimitry Andric yp_errno = YP_YPERR;
83665823d0SBill Paul break;
84665823d0SBill Paul }
85665823d0SBill Paul }
86665823d0SBill Paul
87665823d0SBill Paul return (dbp);
88665823d0SBill Paul }
89665823d0SBill Paul
90dc584ddbSDag-Erling Smørgrav int
yp_put_record(DB * dbp,DBT * key,DBT * data,int allow_overwrite)91dc584ddbSDag-Erling Smørgrav yp_put_record(DB *dbp, DBT *key, DBT *data, int allow_overwrite)
92665823d0SBill Paul {
93b95c787eSBill Paul int rval;
94665823d0SBill Paul
9516deb43aSBill Paul if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :
9616deb43aSBill Paul R_NOOVERWRITE))) {
97b95c787eSBill Paul switch (rval) {
98b95c787eSBill Paul case 1:
99b95c787eSBill Paul return(YP_FALSE);
100b95c787eSBill Paul break;
101b95c787eSBill Paul case -1:
102b95c787eSBill Paul default:
103665823d0SBill Paul (void)(dbp->close)(dbp);
104665823d0SBill Paul return(YP_BADDB);
105b95c787eSBill Paul break;
106b95c787eSBill Paul }
107665823d0SBill Paul }
108665823d0SBill Paul
109665823d0SBill Paul return(YP_TRUE);
110665823d0SBill Paul }
111