/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 19954* Bill Paul <[email protected]>. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. All advertising materials mentioning features or use of this software15* must display the following acknowledgement:16* This product includes software developed by Bill Paul.17* 4. Neither the name of the author nor the names of any co-contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/cdefs.h>35#include <db.h>36#include <errno.h>37#include <fcntl.h>38#include <limits.h>39#include <paths.h>40#include <stdio.h>41#include <stdlib.h>42#include <string.h>43#include <unistd.h>44#include <sys/stat.h>45#include <rpcsvc/yp.h>46#include "ypxfr_extern.h"4748#define PERM_SECURE (S_IRUSR|S_IWUSR)4950/*51* Open a DB database read/write52*/53DB *54yp_open_db_rw(const char *domain, const char *map, const int flags)55{56DB *dbp;57char buf[1025];585960yp_errno = YP_TRUE;6162if (map[0] == '.' || strchr(map, '/')) {63yp_errno = YP_BADARGS;64return (NULL);65}6667#define FLAGS O_RDWR|O_EXLOCK|O_EXCL|O_CREAT6869snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, domain, map);70dbp = dbopen(buf,flags ? flags : FLAGS,PERM_SECURE,DB_HASH,&openinfo);7172if (dbp == NULL) {73switch (errno) {74case ENOENT:75yp_errno = YP_NOMAP;76break;77case EFTYPE:78yp_errno = YP_BADDB;79break;80default:81yp_errno = YP_YPERR;82break;83}84}8586return (dbp);87}8889int90yp_put_record(DB *dbp, DBT *key, DBT *data, int allow_overwrite)91{92int rval;9394if ((rval = (dbp->put)(dbp,key,data, allow_overwrite ? 0 :95R_NOOVERWRITE))) {96switch (rval) {97case 1:98return(YP_FALSE);99break;100case -1:101default:102(void)(dbp->close)(dbp);103return(YP_BADDB);104break;105}106}107108return(YP_TRUE);109}110111112