yp_dbwrite.c revision 90298
113007Swpaul/*
213007Swpaul * Copyright (c) 1995
313007Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
413007Swpaul *
513007Swpaul * Redistribution and use in source and binary forms, with or without
613007Swpaul * modification, are permitted provided that the following conditions
713007Swpaul * are met:
813007Swpaul * 1. Redistributions of source code must retain the above copyright
913007Swpaul *    notice, this list of conditions and the following disclaimer.
1013007Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1113007Swpaul *    notice, this list of conditions and the following disclaimer in the
1213007Swpaul *    documentation and/or other materials provided with the distribution.
1313007Swpaul * 3. All advertising materials mentioning features or use of this software
1413007Swpaul *    must display the following acknowledgement:
1513007Swpaul *	This product includes software developed by Bill Paul.
1613007Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1713007Swpaul *    may be used to endorse or promote products derived from this software
1813007Swpaul *    without specific prior written permission.
1913007Swpaul *
2013007Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2113007Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2213007Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2313007Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2413007Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2513007Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2613007Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2713007Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2813007Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2913007Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3013007Swpaul * SUCH DAMAGE.
3113007Swpaul */
3231626Scharnier
3331626Scharnier#ifndef lint
3431626Scharnierstatic const char rcsid[] =
3550476Speter  "$FreeBSD: head/libexec/ypxfr/yp_dbwrite.c 90298 2002-02-06 15:26:07Z des $";
3631626Scharnier#endif /* not lint */
3731626Scharnier
3831626Scharnier#include <db.h>
3931626Scharnier#include <errno.h>
4031626Scharnier#include <fcntl.h>
4131626Scharnier#include <limits.h>
4231626Scharnier#include <paths.h>
4313007Swpaul#include <stdio.h>
4413007Swpaul#include <stdlib.h>
4513007Swpaul#include <string.h>
4613007Swpaul#include <unistd.h>
4713007Swpaul#include <sys/stat.h>
4815420Swpaul#include <rpcsvc/yp.h>
4913007Swpaul#include "ypxfr_extern.h"
5013007Swpaul
5113007Swpaul#define PERM_SECURE (S_IRUSR|S_IWUSR)
5213007Swpaul
5313007Swpaul/*
5413007Swpaul * Open a DB database read/write
5513007Swpaul */
5690298SdesDB *
5790298Sdesyp_open_db_rw(const char *domain, const char *map, const int flags)
5813007Swpaul{
5913007Swpaul	DB *dbp;
6013007Swpaul	char buf[1025];
6113007Swpaul
6213007Swpaul
6313007Swpaul	yp_errno = YP_TRUE;
6413007Swpaul
6513007Swpaul	if (map[0] == '.' || strchr(map, '/')) {
6613007Swpaul		yp_errno = YP_BADARGS;
6713007Swpaul		return (NULL);
6813007Swpaul	}
6913007Swpaul
7016132Swpaul#define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT
7116132Swpaul
7213007Swpaul	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
7316132Swpaul	dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);
7413007Swpaul
7513007Swpaul	if (dbp == NULL) {
7690297Sdes		switch (errno) {
7713007Swpaul		case ENOENT:
7813007Swpaul			yp_errno = YP_NOMAP;
7913007Swpaul			break;
8013007Swpaul		case EFTYPE:
8113007Swpaul			yp_errno = YP_BADDB;
8213007Swpaul			break;
8313007Swpaul		default:
8413007Swpaul			yp_errno = YP_YPERR;
8513007Swpaul			break;
8613007Swpaul		}
8713007Swpaul	}
8813007Swpaul
8913007Swpaul	return (dbp);
9013007Swpaul}
9113007Swpaul
9290298Sdesint
9390298Sdesyp_put_record(DB *dbp, DBT *key, DBT *data, int allow_overwrite)
9413007Swpaul{
9515420Swpaul	int rval;
9613007Swpaul
9716132Swpaul	if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :
9816132Swpaul							R_NOOVERWRITE))) {
9990297Sdes		switch (rval) {
10015420Swpaul		case 1:
10115420Swpaul			return(YP_FALSE);
10215420Swpaul			break;
10315420Swpaul		case -1:
10415420Swpaul		default:
10515420Swpaul			(void)(dbp->close)(dbp);
10615420Swpaul			return(YP_BADDB);
10715420Swpaul			break;
10815420Swpaul		}
10913007Swpaul	}
11013007Swpaul
11113007Swpaul	return(YP_TRUE);
11213007Swpaul}
113