xref: /freebsd/libexec/ypxfr/yp_dbwrite.c (revision df57947f083046d50552e99b91074927d2458708)
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>
3622e9bc15SDavid E. O'Brien __FBSDID("$FreeBSD$");
3727eed7e3SPhilippe Charnier 
3827eed7e3SPhilippe Charnier #include <db.h>
3927eed7e3SPhilippe Charnier #include <errno.h>
4027eed7e3SPhilippe Charnier #include <fcntl.h>
4127eed7e3SPhilippe Charnier #include <limits.h>
4227eed7e3SPhilippe Charnier #include <paths.h>
4327eed7e3SPhilippe Charnier #include <stdio.h>
4427eed7e3SPhilippe Charnier #include <stdlib.h>
4527eed7e3SPhilippe Charnier #include <string.h>
4627eed7e3SPhilippe Charnier #include <unistd.h>
4727eed7e3SPhilippe Charnier #include <sys/stat.h>
4827eed7e3SPhilippe Charnier #include <rpcsvc/yp.h>
4927eed7e3SPhilippe Charnier #include "ypxfr_extern.h"
50665823d0SBill Paul 
51665823d0SBill Paul #define PERM_SECURE (S_IRUSR|S_IWUSR)
52665823d0SBill Paul 
53665823d0SBill Paul /*
54665823d0SBill Paul  * Open a DB database read/write
55665823d0SBill Paul  */
56dc584ddbSDag-Erling Smørgrav DB *
57dc584ddbSDag-Erling Smørgrav yp_open_db_rw(const char *domain, const char *map, const int flags)
58665823d0SBill Paul {
59665823d0SBill Paul 	DB *dbp;
60665823d0SBill Paul 	char buf[1025];
61665823d0SBill Paul 
62665823d0SBill Paul 
63db83a391SDimitry Andric 	yp_errno = YP_TRUE;
64665823d0SBill Paul 
65665823d0SBill Paul 	if (map[0] == '.' || strchr(map, '/')) {
66db83a391SDimitry Andric 		yp_errno = YP_BADARGS;
67665823d0SBill Paul 		return (NULL);
68665823d0SBill Paul 	}
69665823d0SBill Paul 
7016deb43aSBill Paul #define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT
71665823d0SBill Paul 
7216deb43aSBill Paul 	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
7316deb43aSBill Paul 	dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);
74665823d0SBill Paul 
75665823d0SBill Paul 	if (dbp == NULL) {
76665823d0SBill Paul 		switch (errno) {
77665823d0SBill Paul 		case ENOENT:
78db83a391SDimitry Andric 			yp_errno = YP_NOMAP;
79665823d0SBill Paul 			break;
80665823d0SBill Paul 		case EFTYPE:
81db83a391SDimitry Andric 			yp_errno = YP_BADDB;
82665823d0SBill Paul 			break;
83665823d0SBill Paul 		default:
84db83a391SDimitry Andric 			yp_errno = YP_YPERR;
85665823d0SBill Paul 			break;
86665823d0SBill Paul 		}
87665823d0SBill Paul 	}
88665823d0SBill Paul 
89665823d0SBill Paul 	return (dbp);
90665823d0SBill Paul }
91665823d0SBill Paul 
92dc584ddbSDag-Erling Smørgrav int
93dc584ddbSDag-Erling Smørgrav yp_put_record(DB *dbp, DBT *key, DBT *data, int allow_overwrite)
94665823d0SBill Paul {
95b95c787eSBill Paul 	int rval;
96665823d0SBill Paul 
9716deb43aSBill Paul 	if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :
9816deb43aSBill Paul 							R_NOOVERWRITE))) {
99b95c787eSBill Paul 		switch (rval) {
100b95c787eSBill Paul 		case 1:
101b95c787eSBill Paul 			return(YP_FALSE);
102b95c787eSBill Paul 			break;
103b95c787eSBill Paul 		case -1:
104b95c787eSBill Paul 		default:
105665823d0SBill Paul 			(void)(dbp->close)(dbp);
106665823d0SBill Paul 			return(YP_BADDB);
107b95c787eSBill Paul 			break;
108b95c787eSBill Paul 		}
109665823d0SBill Paul 	}
110665823d0SBill Paul 
111665823d0SBill Paul 	return(YP_TRUE);
112665823d0SBill Paul }
113