yp_dbupdate.c revision 30378
190075Sobrien/*
290075Sobrien * Copyright (c) 1996
390075Sobrien *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4169689Skan *
590075Sobrien * Redistribution and use in source and binary forms, with or without
690075Sobrien * modification, are permitted provided that the following conditions
790075Sobrien * are met:
890075Sobrien * 1. Redistributions of source code must retain the above copyright
990075Sobrien *    notice, this list of conditions and the following disclaimer.
1090075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1190075Sobrien *    notice, this list of conditions and the following disclaimer in the
1290075Sobrien *    documentation and/or other materials provided with the distribution.
1390075Sobrien * 3. All advertising materials mentioning features or use of this software
1490075Sobrien *    must display the following acknowledgement:
1590075Sobrien *	This product includes software developed by Bill Paul.
1690075Sobrien * 4. Neither the name of the author nor the names of any co-contributors
1790075Sobrien *    may be used to endorse or promote products derived from this software
1890075Sobrien *    without specific prior written permission.
1990075Sobrien *
20169689Skan * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2290075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30169689Skan * SUCH DAMAGE.
31169689Skan */
32169689Skan
33117395Skan#ifndef lint
34117395Skanstatic const char rcsid[] =
35117395Skan	"$Id$";
3690075Sobrien#endif /* not lint */
3790075Sobrien
3890075Sobrien#include <sys/fcntl.h>
39169689Skan
40169689Skan#include <stdio.h>
41169689Skan#include <string.h>
42169689Skan#include <errno.h>
43169689Skan#include <limits.h>
44169689Skan#include <db.h>
4590075Sobrien#include <unistd.h>
4690075Sobrienstruct dom_binding {};
4790075Sobrien#include <rpcsvc/ypclnt.h>
4890075Sobrien#include <rpcsvc/ypupdate_prot.h>
4990075Sobrien#include "ypxfr_extern.h"
5090075Sobrien#include "ypupdated_extern.h"
5190075Sobrien
5290075Sobrienstatic int yp_domake(map, domain)
5390075Sobrien	char *map;
5490075Sobrien	char *domain;
5590075Sobrien{
5690075Sobrien	int pid;
5790075Sobrien
5890075Sobrien	switch((pid = fork())) {
5990075Sobrien	case 0:
6090075Sobrien		execlp(MAP_UPDATE_PATH, MAP_UPDATE, map, domain, NULL);
6190075Sobrien		yp_error("couldn't exec map update process: %s",
6290075Sobrien						strerror(errno));
6390075Sobrien		exit(1);
6490075Sobrien		break;
6590075Sobrien	case -1:
6690075Sobrien		yp_error("fork() failed: %s", strerror(errno));
6790075Sobrien		return(YPERR_YPERR);
6890075Sobrien		break;
6990075Sobrien	default:
7090075Sobrien		children++;
7190075Sobrien		break;
7290075Sobrien	}
7390075Sobrien
7490075Sobrien	return(0);
7590075Sobrien}
7690075Sobrien
7790075Sobrienint ypmap_update(netname, map, op, keylen, keyval, datlen, datval)
7890075Sobrien	char *netname;
7990075Sobrien	char *map;
8090075Sobrien	unsigned int op;
8190075Sobrien	unsigned int keylen;
8290075Sobrien	char *keyval;
8390075Sobrien	unsigned int datlen;
8490075Sobrien	char *datval;
8590075Sobrien{
8690075Sobrien	DB *dbp;
8790075Sobrien	DBT key = { NULL, 0 }, data = { NULL, 0 };
8890075Sobrien	char *yp_last = "YP_LAST_MODIFIED";
8990075Sobrien	char yplastbuf[YPMAXRECORD];
9090075Sobrien	char *domptr;
9190075Sobrien	int rval = 0;
9290075Sobrien
9390075Sobrien	if ((domptr = strchr(netname, '@')) == NULL)
9490075Sobrien		return(ERR_ACCESS);
9590075Sobrien	domptr++;
9690075Sobrien
9790075Sobrien
9890075Sobrien	dbp = yp_open_db_rw(domptr, map, O_RDWR);
9990075Sobrien	if (dbp == NULL)
10090075Sobrien		return(ERR_DBASE);
101132718Skan
10290075Sobrien	key.data = keyval;
103169689Skan	key.size = keylen;
10490075Sobrien	data.data = datval;
105102780Skan	data.size = datlen;
106102780Skan
107102780Skan	switch(op) {
108102780Skan	case YPOP_DELETE: /* delete this entry */
109102780Skan		rval = yp_del_record(dbp, &key);
110102780Skan		if (rval == YP_TRUE)
11190075Sobrien			rval = 0;
11290075Sobrien		break;
11390075Sobrien	case YPOP_INSERT: /* add, do not change */
11490075Sobrien		rval = yp_put_record(dbp, &key, &data, 0);
11590075Sobrien		if (rval == YP_TRUE)
11690075Sobrien			rval = 0;
11790075Sobrien		break;
11890075Sobrien	case YPOP_STORE: /* add, or change */
11990075Sobrien		rval = yp_put_record(dbp, &key, &data, 1);
12090075Sobrien		if (rval == YP_TRUE)
12190075Sobrien			rval = 0;
12290075Sobrien		break;
12390075Sobrien	case YPOP_CHANGE: /* change, do not add */
12490075Sobrien		if (yp_get_record(domptr, map, &key, &data, 0) != YP_TRUE) {
12590075Sobrien			rval = ERR_KEY;
12690075Sobrien			break;
12790075Sobrien		}
128117395Skan		rval = yp_put_record(dbp, &key, &data, 1);
12990075Sobrien		if (rval == YP_TRUE)
13090075Sobrien			rval = 0;
131119256Skan		break;
132119256Skan	default:
133119256Skan		yp_error("unknown update command: (%d)", op);
13490075Sobrien	}
13590075Sobrien
13690075Sobrien	if (rval) {
13790075Sobrien		(void)(dbp->close)(dbp);
13890075Sobrien		return(rval);
13990075Sobrien	}
14090075Sobrien
14190075Sobrien	snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL));
14290075Sobrien	key.data = yp_last;
14390075Sobrien	key.size = strlen(yp_last);
14490075Sobrien	data.data = (char *)&yplastbuf;
145169689Skan	data.size = strlen(yplastbuf);
14690075Sobrien	if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
14790075Sobrien		yp_error("failed to update timestamp in %s/%s", domptr, map);
148132718Skan		(void)(dbp->close)(dbp);
14990075Sobrien		return(ERR_DBASE);
150132718Skan	}
151169689Skan
152169689Skan	(void)(dbp->close)(dbp);
153169689Skan	return(yp_domake(map, domptr));
154169689Skan}
155169689Skan