yp_dbwrite.c revision 16132
1217309Snwhitehorn/*
2217309Snwhitehorn * Copyright (c) 1995
3217309Snwhitehorn *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4217309Snwhitehorn *
5217309Snwhitehorn * Redistribution and use in source and binary forms, with or without
6217309Snwhitehorn * modification, are permitted provided that the following conditions
7217309Snwhitehorn * are met:
8217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
13217309Snwhitehorn * 3. All advertising materials mentioning features or use of this software
14217309Snwhitehorn *    must display the following acknowledgement:
15217309Snwhitehorn *	This product includes software developed by Bill Paul.
16217309Snwhitehorn * 4. Neither the name of the author nor the names of any co-contributors
17217309Snwhitehorn *    may be used to endorse or promote products derived from this software
18217309Snwhitehorn *    without specific prior written permission.
19217309Snwhitehorn *
20217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21217309Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22217309Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23217309Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24217309Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25217309Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26217309Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28217309Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29217309Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30217309Snwhitehorn * SUCH DAMAGE.
31217309Snwhitehorn *
32217309Snwhitehorn *	$Id: yp_dbwrite.c,v 1.10 1996/06/03 03:11:25 wpaul Exp $
33217309Snwhitehorn *
34217309Snwhitehorn */
35217309Snwhitehorn#include <stdio.h>
36217309Snwhitehorn#include <stdlib.h>
37217309Snwhitehorn#include <fcntl.h>
38217309Snwhitehorn#include <string.h>
39217309Snwhitehorn#include <limits.h>
40217309Snwhitehorn#include <unistd.h>
41217309Snwhitehorn#include <db.h>
42217309Snwhitehorn#include <sys/stat.h>
43217309Snwhitehorn#include <errno.h>
44217309Snwhitehorn#include <paths.h>
45217309Snwhitehorn#include <rpcsvc/yp.h>
46217309Snwhitehorn#include "ypxfr_extern.h"
47217309Snwhitehorn
48217309Snwhitehorn#ifndef lint
49217309Snwhitehornstatic const char rcsid[] = "$Id: yp_dbwrite.c,v 1.10 1996/06/03 03:11:25 wpaul Exp $";
50217309Snwhitehorn#endif
51217309Snwhitehorn
52217309Snwhitehorn#define PERM_SECURE (S_IRUSR|S_IWUSR)
53217309Snwhitehorn
54217309Snwhitehorn/*
55217309Snwhitehorn * Open a DB database read/write
56217309Snwhitehorn */
57217309SnwhitehornDB *yp_open_db_rw(domain, map, flags)
58217309Snwhitehorn	const char *domain;
59217309Snwhitehorn	const char *map;
60217309Snwhitehorn	const int flags;
61217309Snwhitehorn{
62217309Snwhitehorn	DB *dbp;
63217309Snwhitehorn	char buf[1025];
64217309Snwhitehorn
65217309Snwhitehorn
66217309Snwhitehorn	yp_errno = YP_TRUE;
67217309Snwhitehorn
68217309Snwhitehorn	if (map[0] == '.' || strchr(map, '/')) {
69217309Snwhitehorn		yp_errno = YP_BADARGS;
70217309Snwhitehorn		return (NULL);
71	}
72
73#define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT
74
75	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
76	dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);
77
78	if (dbp == NULL) {
79		switch(errno) {
80		case ENOENT:
81			yp_errno = YP_NOMAP;
82			break;
83		case EFTYPE:
84			yp_errno = YP_BADDB;
85			break;
86		default:
87			yp_errno = YP_YPERR;
88			break;
89		}
90	}
91
92	return (dbp);
93}
94
95int yp_put_record(dbp,key,data,allow_overwrite)
96	DB *dbp;
97	DBT *key;
98	DBT *data;
99	int allow_overwrite;
100{
101	int rval;
102
103	if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :
104							R_NOOVERWRITE))) {
105		switch(rval) {
106		case 1:
107			return(YP_FALSE);
108			break;
109		case -1:
110		default:
111			(void)(dbp->close)(dbp);
112			return(YP_BADDB);
113			break;
114		}
115	}
116
117	return(YP_TRUE);
118}
119