xref: /freebsd/usr.sbin/rpc.ypupdated/yp_dbupdate.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
440a5f74dSBill Paul  * Copyright (c) 1996
540a5f74dSBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
640a5f74dSBill Paul  *
740a5f74dSBill Paul  * Redistribution and use in source and binary forms, with or without
840a5f74dSBill Paul  * modification, are permitted provided that the following conditions
940a5f74dSBill Paul  * are met:
1040a5f74dSBill Paul  * 1. Redistributions of source code must retain the above copyright
1140a5f74dSBill Paul  *    notice, this list of conditions and the following disclaimer.
1240a5f74dSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1340a5f74dSBill Paul  *    notice, this list of conditions and the following disclaimer in the
1440a5f74dSBill Paul  *    documentation and/or other materials provided with the distribution.
1540a5f74dSBill Paul  * 3. All advertising materials mentioning features or use of this software
1640a5f74dSBill Paul  *    must display the following acknowledgement:
1740a5f74dSBill Paul  *	This product includes software developed by Bill Paul.
1840a5f74dSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1940a5f74dSBill Paul  *    may be used to endorse or promote products derived from this software
2040a5f74dSBill Paul  *    without specific prior written permission.
2140a5f74dSBill Paul  *
2240a5f74dSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2340a5f74dSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2440a5f74dSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2540a5f74dSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2640a5f74dSBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2740a5f74dSBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2840a5f74dSBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2940a5f74dSBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3040a5f74dSBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3140a5f74dSBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3240a5f74dSBill Paul  * SUCH DAMAGE.
3340a5f74dSBill Paul  */
3440a5f74dSBill Paul 
35b728350eSDavid E. O'Brien #include <sys/cdefs.h>
3640a5f74dSBill Paul #include <sys/fcntl.h>
3740a5f74dSBill Paul 
38ae1d1536SDimitry Andric #include <stdint.h>
3940a5f74dSBill Paul #include <stdio.h>
4028e1bf46SStefan Farfeleder #include <stdlib.h>
4140a5f74dSBill Paul #include <string.h>
4240a5f74dSBill Paul #include <errno.h>
4340a5f74dSBill Paul #include <limits.h>
4440a5f74dSBill Paul #include <db.h>
4540a5f74dSBill Paul #include <unistd.h>
4640a5f74dSBill Paul #include <rpcsvc/ypclnt.h>
4740a5f74dSBill Paul #include <rpcsvc/ypupdate_prot.h>
4840a5f74dSBill Paul #include "ypxfr_extern.h"
4940a5f74dSBill Paul #include "ypupdated_extern.h"
5040a5f74dSBill Paul 
51dc584ddbSDag-Erling Smørgrav static int
yp_domake(char * map,char * domain)52dc584ddbSDag-Erling Smørgrav yp_domake(char *map, char *domain)
5340a5f74dSBill Paul {
5440a5f74dSBill Paul 	int pid;
5540a5f74dSBill Paul 
5640a5f74dSBill Paul 	switch ((pid = fork())) {
5740a5f74dSBill Paul 	case 0:
587bc6d015SBrian Somers 		execlp(MAP_UPDATE_PATH, MAP_UPDATE, map, domain, (char *)NULL);
5940a5f74dSBill Paul 		yp_error("couldn't exec map update process: %s",
6040a5f74dSBill Paul 						strerror(errno));
6140a5f74dSBill Paul 		exit(1);
6240a5f74dSBill Paul 		break;
6340a5f74dSBill Paul 	case -1:
6440a5f74dSBill Paul 		yp_error("fork() failed: %s", strerror(errno));
6540a5f74dSBill Paul 		return(YPERR_YPERR);
6640a5f74dSBill Paul 		break;
6740a5f74dSBill Paul 	default:
6840a5f74dSBill Paul 		children++;
6940a5f74dSBill Paul 		break;
7040a5f74dSBill Paul 	}
7140a5f74dSBill Paul 
7240a5f74dSBill Paul 	return(0);
7340a5f74dSBill Paul }
7440a5f74dSBill Paul 
75dc584ddbSDag-Erling Smørgrav int
ypmap_update(char * netname,char * map,unsigned int op,unsigned int keylen,char * keyval,unsigned int datlen,char * datval)76dc584ddbSDag-Erling Smørgrav ypmap_update(char *netname, char *map, unsigned int op, unsigned int keylen,
77dc584ddbSDag-Erling Smørgrav     char *keyval, unsigned int datlen, char *datval)
7840a5f74dSBill Paul {
7940a5f74dSBill Paul 	DB *dbp;
8040a5f74dSBill Paul 	DBT key = { NULL, 0 }, data = { NULL, 0 };
8140a5f74dSBill Paul 	char *yp_last = "YP_LAST_MODIFIED";
82*6b462d27SKonstantin Belousov 	char yplastbuf[32];
8340a5f74dSBill Paul 	char *domptr;
8440a5f74dSBill Paul 	int rval = 0;
8540a5f74dSBill Paul 
8640a5f74dSBill Paul 	if ((domptr = strchr(netname, '@')) == NULL)
8740a5f74dSBill Paul 		return(ERR_ACCESS);
8840a5f74dSBill Paul 	domptr++;
8940a5f74dSBill Paul 
9040a5f74dSBill Paul 
9140a5f74dSBill Paul 	dbp = yp_open_db_rw(domptr, map, O_RDWR);
9240a5f74dSBill Paul 	if (dbp == NULL)
9340a5f74dSBill Paul 		return(ERR_DBASE);
9440a5f74dSBill Paul 
9540a5f74dSBill Paul 	key.data = keyval;
9640a5f74dSBill Paul 	key.size = keylen;
9740a5f74dSBill Paul 	data.data = datval;
9840a5f74dSBill Paul 	data.size = datlen;
9940a5f74dSBill Paul 
10040a5f74dSBill Paul 	switch (op) {
10140a5f74dSBill Paul 	case YPOP_DELETE: /* delete this entry */
10240a5f74dSBill Paul 		rval = yp_del_record(dbp, &key);
10340a5f74dSBill Paul 		if (rval == YP_TRUE)
10440a5f74dSBill Paul 			rval = 0;
10540a5f74dSBill Paul 		break;
10640a5f74dSBill Paul 	case YPOP_INSERT: /* add, do not change */
10740a5f74dSBill Paul 		rval = yp_put_record(dbp, &key, &data, 0);
10840a5f74dSBill Paul 		if (rval == YP_TRUE)
10940a5f74dSBill Paul 			rval = 0;
11040a5f74dSBill Paul 		break;
11140a5f74dSBill Paul 	case YPOP_STORE: /* add, or change */
11240a5f74dSBill Paul 		rval = yp_put_record(dbp, &key, &data, 1);
11340a5f74dSBill Paul 		if (rval == YP_TRUE)
11440a5f74dSBill Paul 			rval = 0;
11540a5f74dSBill Paul 		break;
11640a5f74dSBill Paul 	case YPOP_CHANGE: /* change, do not add */
11740a5f74dSBill Paul 		if (yp_get_record(domptr, map, &key, &data, 0) != YP_TRUE) {
11840a5f74dSBill Paul 			rval = ERR_KEY;
11940a5f74dSBill Paul 			break;
12040a5f74dSBill Paul 		}
12140a5f74dSBill Paul 		rval = yp_put_record(dbp, &key, &data, 1);
12240a5f74dSBill Paul 		if (rval == YP_TRUE)
12340a5f74dSBill Paul 			rval = 0;
12440a5f74dSBill Paul 		break;
12540a5f74dSBill Paul 	default:
12640a5f74dSBill Paul 		yp_error("unknown update command: (%d)", op);
12740a5f74dSBill Paul 	}
12840a5f74dSBill Paul 
12940a5f74dSBill Paul 	if (rval) {
13040a5f74dSBill Paul 		(void)(dbp->close)(dbp);
13140a5f74dSBill Paul 		return(rval);
13240a5f74dSBill Paul 	}
13340a5f74dSBill Paul 
1340af4e80aSDimitry Andric 	snprintf(yplastbuf, sizeof(yplastbuf), "%jd", (intmax_t)time(NULL));
13540a5f74dSBill Paul 	key.data = yp_last;
13640a5f74dSBill Paul 	key.size = strlen(yp_last);
13740a5f74dSBill Paul 	data.data = (char *)&yplastbuf;
13840a5f74dSBill Paul 	data.size = strlen(yplastbuf);
13940a5f74dSBill Paul 	if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
14040a5f74dSBill Paul 		yp_error("failed to update timestamp in %s/%s", domptr, map);
14140a5f74dSBill Paul 		(void)(dbp->close)(dbp);
14240a5f74dSBill Paul 		return(ERR_DBASE);
14340a5f74dSBill Paul 	}
14440a5f74dSBill Paul 
14540a5f74dSBill Paul 	(void)(dbp->close)(dbp);
14640a5f74dSBill Paul 	return(yp_domake(map, domptr));
14740a5f74dSBill Paul }
148