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
33114626Sobrien#include <sys/cdefs.h>
34114626Sobrien__FBSDID("$FreeBSD$");
3531626Scharnier
3631626Scharnier#include <db.h>
3731626Scharnier#include <errno.h>
3831626Scharnier#include <fcntl.h>
3931626Scharnier#include <limits.h>
4031626Scharnier#include <paths.h>
4113007Swpaul#include <stdio.h>
4213007Swpaul#include <stdlib.h>
4313007Swpaul#include <string.h>
4413007Swpaul#include <unistd.h>
4513007Swpaul#include <sys/stat.h>
4615420Swpaul#include <rpcsvc/yp.h>
4713007Swpaul#include "ypxfr_extern.h"
4813007Swpaul
4913007Swpaul#define PERM_SECURE (S_IRUSR|S_IWUSR)
5013007Swpaul
5113007Swpaul/*
5213007Swpaul * Open a DB database read/write
5313007Swpaul */
5490298SdesDB *
5590298Sdesyp_open_db_rw(const char *domain, const char *map, const int flags)
5613007Swpaul{
5713007Swpaul	DB *dbp;
5813007Swpaul	char buf[1025];
5913007Swpaul
6013007Swpaul
61228599Sdim	yp_errno = YP_TRUE;
6213007Swpaul
6313007Swpaul	if (map[0] == '.' || strchr(map, '/')) {
64228599Sdim		yp_errno = YP_BADARGS;
6513007Swpaul		return (NULL);
6613007Swpaul	}
6713007Swpaul
6816132Swpaul#define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT
6916132Swpaul
7013007Swpaul	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);
7116132Swpaul	dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);
7213007Swpaul
7313007Swpaul	if (dbp == NULL) {
7490297Sdes		switch (errno) {
7513007Swpaul		case ENOENT:
76228599Sdim			yp_errno = YP_NOMAP;
7713007Swpaul			break;
7813007Swpaul		case EFTYPE:
79228599Sdim			yp_errno = YP_BADDB;
8013007Swpaul			break;
8113007Swpaul		default:
82228599Sdim			yp_errno = YP_YPERR;
8313007Swpaul			break;
8413007Swpaul		}
8513007Swpaul	}
8613007Swpaul
8713007Swpaul	return (dbp);
8813007Swpaul}
8913007Swpaul
9090298Sdesint
9190298Sdesyp_put_record(DB *dbp, DBT *key, DBT *data, int allow_overwrite)
9213007Swpaul{
9315420Swpaul	int rval;
9413007Swpaul
9516132Swpaul	if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :
9616132Swpaul							R_NOOVERWRITE))) {
9790297Sdes		switch (rval) {
9815420Swpaul		case 1:
9915420Swpaul			return(YP_FALSE);
10015420Swpaul			break;
10115420Swpaul		case -1:
10215420Swpaul		default:
10315420Swpaul			(void)(dbp->close)(dbp);
10415420Swpaul			return(YP_BADDB);
10515420Swpaul			break;
10615420Swpaul		}
10713007Swpaul	}
10813007Swpaul
10913007Swpaul	return(YP_TRUE);
11013007Swpaul}
111