yp_dbwrite.c revision 16132
1168054Sflz/*
2168054Sflz * Copyright (c) 1995
3168054Sflz *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4168054Sflz *
5168064Sflz * Redistribution and use in source and binary forms, with or without
6168064Sflz * modification, are permitted provided that the following conditions
7168064Sflz * are met:
8168064Sflz * 1. Redistributions of source code must retain the above copyright
9168064Sflz *    notice, this list of conditions and the following disclaimer.
10168064Sflz * 2. Redistributions in binary form must reproduce the above copyright
11168064Sflz *    notice, this list of conditions and the following disclaimer in the
12168064Sflz *    documentation and/or other materials provided with the distribution.
13168064Sflz * 3. All advertising materials mentioning features or use of this software
14168064Sflz *    must display the following acknowledgement:
15168064Sflz *	This product includes software developed by Bill Paul.
16168064Sflz * 4. Neither the name of the author nor the names of any co-contributors
17168064Sflz *    may be used to endorse or promote products derived from this software
18168064Sflz *    without specific prior written permission.
19168054Sflz *
20168054Sflz * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21168064Sflz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22168054Sflz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23168064Sflz * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24168064Sflz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25168054Sflz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26168054Sflz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27168054Sflz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28168054Sflz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29168055Spav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30168059Sgabor * SUCH DAMAGE.
31168054Sflz *
32168059Sgabor *	$Id: yp_dbwrite.c,v 1.10 1996/06/03 03:11:25 wpaul Exp $
33168054Sflz *
34168054Sflz */
35168055Spav#include <stdio.h>
36168055Spav#include <stdlib.h>
37168055Spav#include <fcntl.h>
38168054Sflz#include <string.h>
39168055Spav#include <limits.h>
40168055Spav#include <unistd.h>
41168054Sflz#include <db.h>
42168055Spav#include <sys/stat.h>
43168061Sahze#include <errno.h>
44168054Sflz#include <paths.h>
45168054Sflz#include <rpcsvc/yp.h>
46168064Sflz#include "ypxfr_extern.h"
47168064Sflz
48168054Sflz#ifndef lint
49168055Spavstatic const char rcsid[] = "$Id: yp_dbwrite.c,v 1.10 1996/06/03 03:11:25 wpaul Exp $";
50168055Spav#endif
51168055Spav
52168055Spav#define PERM_SECURE (S_IRUSR|S_IWUSR)
53168055Spav
54168055Spav/*
55168057Sahze * Open a DB database read/write
56168055Spav */
57168059SgaborDB *yp_open_db_rw(domain, map, flags)
58168059Sgabor	const char *domain;
59168054Sflz	const char *map;
60168054Sflz	const int flags;
61168054Sflz{
62168054Sflz	DB *dbp;
63168054Sflz	char buf[1025];
64168055Spav
65168055Spav
66168055Spav	yp_errno = YP_TRUE;
67168054Sflz
68168054Sflz	if (map[0] == '.' || strchr(map, '/')) {
69		yp_errno = YP_BADARGS;
70		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