Home | History | Annotate | Line # | Download | only in ypxfr
ypxfr.c revision 1.6
      1  1.6    lukem /*	$NetBSD: ypxfr.c,v 1.6 1997/10/13 03:15:30 lukem Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1  thorpej  *    must display the following acknowledgement:
     17  1.1  thorpej  *	This product includes software developed by Mats O Jansson
     18  1.1  thorpej  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  thorpej  *    derived from this software without specific prior written permission.
     20  1.1  thorpej  *
     21  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  1.1  thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  1.1  thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  thorpej  * SUCH DAMAGE.
     32  1.1  thorpej  */
     33  1.6    lukem 
     34  1.6    lukem #include <sys/cdefs.h>
     35  1.6    lukem #ifndef lint
     36  1.6    lukem __RCSID("$NetBSD: ypxfr.c,v 1.6 1997/10/13 03:15:30 lukem Exp $");
     37  1.6    lukem #endif
     38  1.1  thorpej 
     39  1.1  thorpej #include <sys/types.h>
     40  1.1  thorpej #include <sys/stat.h>
     41  1.1  thorpej #include <sys/socket.h>
     42  1.1  thorpej 
     43  1.1  thorpej #include <netinet/in.h>
     44  1.1  thorpej #include <arpa/inet.h>
     45  1.1  thorpej 
     46  1.4  thorpej #include <err.h>
     47  1.1  thorpej #include <fcntl.h>
     48  1.1  thorpej #include <netdb.h>
     49  1.1  thorpej #include <string.h>
     50  1.1  thorpej #include <stdio.h>
     51  1.1  thorpej #include <stdlib.h>
     52  1.1  thorpej #include <unistd.h>
     53  1.1  thorpej 
     54  1.1  thorpej #include <rpc/rpc.h>
     55  1.1  thorpej #include <rpc/xdr.h>
     56  1.1  thorpej #include <rpcsvc/yp_prot.h>
     57  1.1  thorpej #include <rpcsvc/ypclnt.h>
     58  1.1  thorpej 
     59  1.1  thorpej #include "yplib_host.h"
     60  1.1  thorpej #include "yplog.h"
     61  1.1  thorpej #include "ypdb.h"
     62  1.1  thorpej #include "ypdef.h"
     63  1.1  thorpej 
     64  1.1  thorpej extern	char *__progname;		/* from crt0.o */
     65  1.1  thorpej 
     66  1.1  thorpej DBM	*db;
     67  1.1  thorpej 
     68  1.4  thorpej static	int ypxfr_foreach __P((int, char *, int, char *, int, char *));
     69  1.1  thorpej 
     70  1.4  thorpej int	main __P((int, char *[]));
     71  1.1  thorpej int	get_local_ordernum __P((char *, char *, u_int *));
     72  1.1  thorpej int	get_remote_ordernum __P((CLIENT *, char *, char *, u_int, u_int *));
     73  1.1  thorpej void	get_map __P((CLIENT *, char *, char *, struct ypall_callback *));
     74  1.1  thorpej DBM	*create_db __P((char *, char *, char *));
     75  1.1  thorpej int	install_db __P((char *, char *, char *));
     76  1.1  thorpej int	unlink_db __P((char *, char *, char *));
     77  1.1  thorpej int	add_order __P((DBM *, u_int));
     78  1.1  thorpej int	add_master __P((CLIENT *, char *, char *, DBM *));
     79  1.1  thorpej int	add_interdomain __P((CLIENT *, char *, char *, DBM *));
     80  1.1  thorpej int	add_secure __P((CLIENT *, char *, char *, DBM *));
     81  1.1  thorpej int	send_clear __P((CLIENT *));
     82  1.1  thorpej int	send_reply __P((CLIENT *, int, int));
     83  1.1  thorpej 
     84  1.1  thorpej int
     85  1.4  thorpej main(argc, argv)
     86  1.1  thorpej 	int argc;
     87  1.4  thorpej 	char *argv[];
     88  1.1  thorpej {
     89  1.1  thorpej 	int need_usage = 0, cflag = 0, fflag = 0, Cflag = 0;
     90  1.1  thorpej 	int ch;
     91  1.1  thorpej 	char *domain;
     92  1.1  thorpej 	char *host = NULL;
     93  1.1  thorpej 	char *srcdomain = NULL;
     94  1.1  thorpej 	char *tid = NULL;
     95  1.1  thorpej 	char *prog = NULL;
     96  1.1  thorpej 	char *ipadd = NULL;
     97  1.1  thorpej 	char *port = NULL;
     98  1.1  thorpej 	char *map = NULL;
     99  1.1  thorpej 	u_int ordernum, new_ordernum;
    100  1.1  thorpej 	struct ypall_callback callback;
    101  1.1  thorpej 	CLIENT *client;
    102  1.1  thorpej 	char mapname[] = "ypdbXXXXXX";
    103  1.1  thorpej 	int status, xfr_status;
    104  1.1  thorpej 
    105  1.1  thorpej 	status = YPPUSH_SUCC;
    106  1.1  thorpej 	client = NULL;
    107  1.1  thorpej 
    108  1.1  thorpej 	if (yp_get_default_domain(&domain))
    109  1.1  thorpej 		errx(1, "can't get YP domain name");
    110  1.1  thorpej 
    111  1.1  thorpej 	while ((ch = getopt(argc, argv, "cd:fh:s:C:")) != -1) {
    112  1.1  thorpej 		switch (ch) {
    113  1.1  thorpej 		case 'c':
    114  1.1  thorpej 			cflag = 1;
    115  1.1  thorpej 			break;
    116  1.1  thorpej 
    117  1.1  thorpej 		case 'd':
    118  1.1  thorpej 			domain = optarg;
    119  1.1  thorpej 			break;
    120  1.1  thorpej 
    121  1.1  thorpej 		case 'f':
    122  1.1  thorpej 			fflag = 1;
    123  1.1  thorpej 			break;
    124  1.1  thorpej 
    125  1.1  thorpej 		case 'h':
    126  1.1  thorpej 			host = optarg;
    127  1.1  thorpej 			break;
    128  1.1  thorpej 
    129  1.1  thorpej 		case 's':
    130  1.1  thorpej 			srcdomain = optarg;
    131  1.1  thorpej 			break;
    132  1.1  thorpej 
    133  1.1  thorpej 		case 'C':
    134  1.2    chuck 			if (optind + 3 >= argc) {
    135  1.2    chuck 				need_usage = 1;
    136  1.2    chuck 				optind = argc;
    137  1.2    chuck 				break;
    138  1.2    chuck 			}
    139  1.1  thorpej 			Cflag = 1;
    140  1.1  thorpej 			tid = optarg;
    141  1.1  thorpej 			prog = argv[optind++];
    142  1.1  thorpej 			ipadd = argv[optind++];
    143  1.1  thorpej 			port = argv[optind++];
    144  1.1  thorpej 			break;
    145  1.1  thorpej 
    146  1.1  thorpej 		default:
    147  1.1  thorpej 			need_usage = 1;
    148  1.1  thorpej 		}
    149  1.1  thorpej 	}
    150  1.1  thorpej 	argc -= optind; argv += optind;
    151  1.1  thorpej 
    152  1.1  thorpej 	if (argc != 1)
    153  1.1  thorpej 		need_usage = 1;
    154  1.1  thorpej 
    155  1.1  thorpej 	map = argv[0];
    156  1.1  thorpej 
    157  1.1  thorpej 	if (need_usage) {
    158  1.1  thorpej 		status = YPPUSH_BADARGS;
    159  1.1  thorpej 		fprintf(stderr, "usage: %s [-cf] [-d domain] [-h host] %s\n",
    160  1.1  thorpej 		   __progname, "[-s domain] [-C tid prog ipadd port] mapname");
    161  1.1  thorpej 		goto punt;
    162  1.1  thorpej 	}
    163  1.1  thorpej 
    164  1.1  thorpej 	ypopenlog();
    165  1.1  thorpej 
    166  1.1  thorpej 	yplog("ypxfr: Arguments:");
    167  1.1  thorpej 	yplog("YP clear to local: %s", (cflag) ? "no" : "yes");
    168  1.1  thorpej 	yplog("   Force transfer: %s", (fflag) ? "yes" : "no");
    169  1.1  thorpej 	yplog("           domain: %s", domain);
    170  1.1  thorpej 	yplog("             host: %s", host);
    171  1.1  thorpej 	yplog("    source domain: %s", srcdomain);
    172  1.1  thorpej 	yplog("          transid: %s", tid);
    173  1.1  thorpej 	yplog("             prog: %s", prog);
    174  1.1  thorpej 	yplog("             port: %s", port);
    175  1.1  thorpej 	yplog("            ipadd: %s", ipadd);
    176  1.1  thorpej 	yplog("              map: %s", map);
    177  1.1  thorpej 
    178  1.1  thorpej 	if (fflag != 0)
    179  1.1  thorpej 		ordernum = 0;
    180  1.1  thorpej 	else {
    181  1.1  thorpej 		status = get_local_ordernum(domain, map, &ordernum);
    182  1.1  thorpej 		if (status < 0)
    183  1.1  thorpej 			goto punt;
    184  1.1  thorpej 	}
    185  1.1  thorpej 
    186  1.1  thorpej         yplog("Get Master");
    187  1.1  thorpej 
    188  1.1  thorpej 	if (host == NULL) {
    189  1.1  thorpej 		if (srcdomain == NULL)
    190  1.5    lukem 			status = yp_master(domain, map, &host);
    191  1.1  thorpej 	        else
    192  1.5    lukem 			status = yp_master(srcdomain, map, &host);
    193  1.1  thorpej 
    194  1.1  thorpej 		if (status == 0)
    195  1.1  thorpej 			status = YPPUSH_SUCC;
    196  1.1  thorpej 		else {
    197  1.1  thorpej 			status = -status;
    198  1.1  thorpej 			goto punt;
    199  1.1  thorpej 		}
    200  1.1  thorpej 	}
    201  1.1  thorpej 
    202  1.1  thorpej         yplog("Connect host: %s", host);
    203  1.1  thorpej 
    204  1.5    lukem 	client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
    205  1.1  thorpej 
    206  1.5    lukem 	status = get_remote_ordernum(client, domain, map, ordernum,
    207  1.5    lukem 	    &new_ordernum);
    208  1.1  thorpej 
    209  1.1  thorpej 
    210  1.1  thorpej 	if (status == YPPUSH_SUCC) {
    211  1.1  thorpej 		/* Create temporary db */
    212  1.1  thorpej 		mktemp(mapname);
    213  1.5    lukem 		db = create_db(domain, map, mapname);
    214  1.1  thorpej 		if (db == NULL)
    215  1.1  thorpej 			status = YPPUSH_DBM;
    216  1.1  thorpej 
    217  1.1  thorpej 	  	/* Add ORDER */
    218  1.1  thorpej 		if (status > 0)
    219  1.1  thorpej 			status = add_order(db, new_ordernum);
    220  1.1  thorpej 
    221  1.1  thorpej 		/* Add MASTER */
    222  1.1  thorpej 		if (status > 0)
    223  1.1  thorpej 			status = add_master(client, domain, map, db);
    224  1.1  thorpej 
    225  1.1  thorpej 	        /* Add INTERDOMAIN */
    226  1.1  thorpej 		if (status > 0)
    227  1.1  thorpej 			status = add_interdomain(client, domain, map, db);
    228  1.1  thorpej 
    229  1.1  thorpej 	        /* Add SECURE */
    230  1.1  thorpej 		if (status > 0)
    231  1.1  thorpej 			status = add_secure(client, domain, map, db);
    232  1.1  thorpej 
    233  1.1  thorpej 		if (status > 0) {
    234  1.4  thorpej 			callback.foreach = ypxfr_foreach;
    235  1.1  thorpej 			get_map(client, domain, map, &callback);
    236  1.1  thorpej 		}
    237  1.1  thorpej 
    238  1.1  thorpej 		/* Close db */
    239  1.1  thorpej 		if (db != NULL)
    240  1.1  thorpej 			ypdb_close(db);
    241  1.1  thorpej 
    242  1.1  thorpej 		/* Rename db */
    243  1.1  thorpej 		if (status > 0)
    244  1.1  thorpej 			status = install_db(domain, map, mapname);
    245  1.1  thorpej 		else
    246  1.1  thorpej 			status = unlink_db(domain, map, mapname);
    247  1.1  thorpej 	}
    248  1.1  thorpej 
    249  1.1  thorpej  punt:
    250  1.1  thorpej 	xfr_status = status;
    251  1.1  thorpej 
    252  1.1  thorpej 	if (client != NULL)
    253  1.1  thorpej 		clnt_destroy(client);
    254  1.1  thorpej 
    255  1.1  thorpej 	/* YP_CLEAR */
    256  1.1  thorpej 	if (!cflag) {
    257  1.1  thorpej 		client = yp_bind_local(YPPROG, YPVERS);
    258  1.1  thorpej 		status = send_clear(client);
    259  1.1  thorpej 		clnt_destroy(client);
    260  1.1  thorpej 	}
    261  1.1  thorpej 
    262  1.1  thorpej 	if (Cflag > 0) {
    263  1.1  thorpej 		/* Send Response */
    264  1.1  thorpej 		client = yp_bind_host(ipadd, atoi(prog), 1, atoi(port), 0);
    265  1.1  thorpej 		status = send_reply(client, xfr_status, atoi(tid));
    266  1.1  thorpej 		clnt_destroy(client);
    267  1.1  thorpej 	}
    268  1.1  thorpej 
    269  1.1  thorpej 	exit (0);
    270  1.1  thorpej }
    271  1.1  thorpej 
    272  1.1  thorpej static int
    273  1.1  thorpej ypxfr_foreach(status, keystr, keylen, valstr, vallen, data)
    274  1.1  thorpej 	int status;
    275  1.1  thorpej 	char *keystr;
    276  1.1  thorpej 	int keylen;
    277  1.1  thorpej 	char *valstr;
    278  1.1  thorpej 	int vallen;
    279  1.4  thorpej 	char *data;
    280  1.1  thorpej {
    281  1.1  thorpej 	datum key, val;
    282  1.1  thorpej 
    283  1.1  thorpej 	if (status == YP_NOMORE)
    284  1.1  thorpej 		return (0);
    285  1.1  thorpej 
    286  1.1  thorpej 	keystr[keylen] = '\0';
    287  1.1  thorpej 	valstr[vallen] = '\0';
    288  1.1  thorpej 
    289  1.1  thorpej 	key.dptr = keystr;
    290  1.1  thorpej 	key.dsize = strlen(keystr);
    291  1.1  thorpej 
    292  1.1  thorpej 	val.dptr = valstr;
    293  1.1  thorpej 	val.dsize = strlen(valstr);
    294  1.1  thorpej 
    295  1.1  thorpej 	ypdb_store(db, key, val, YPDB_INSERT);
    296  1.1  thorpej 
    297  1.1  thorpej 	return (0);
    298  1.1  thorpej }
    299  1.1  thorpej 
    300  1.1  thorpej int
    301  1.1  thorpej get_local_ordernum(domain, map, lordernum)
    302  1.1  thorpej 	char *domain, *map;
    303  1.1  thorpej 	u_int *lordernum;
    304  1.1  thorpej {
    305  1.1  thorpej 	char map_path[1024];
    306  1.1  thorpej 	char order_key[] = YP_LAST_KEY;
    307  1.1  thorpej 	char order[MAX_LAST_LEN+1];
    308  1.1  thorpej 	struct stat finfo;
    309  1.1  thorpej 	DBM *db;
    310  1.5    lukem 	datum k, v;
    311  1.1  thorpej 	int status;
    312  1.1  thorpej 
    313  1.1  thorpej 	status = YPPUSH_SUCC;
    314  1.1  thorpej 
    315  1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s", YP_DB_PATH, domain);
    316  1.1  thorpej 	map_path[sizeof(map_path) - 1] = '\0';
    317  1.1  thorpej 
    318  1.1  thorpej 	/* Make sure we serve the domain. */
    319  1.1  thorpej 	if ((stat(map_path, &finfo)) != 0 ||
    320  1.1  thorpej 	    (S_ISDIR(finfo.st_mode) == 0)) {
    321  1.4  thorpej 		warnx("domain `%s' not found locally", domain);
    322  1.1  thorpej 		status = YPPUSH_NODOM;
    323  1.1  thorpej 		goto out;
    324  1.1  thorpej 	}
    325  1.1  thorpej 
    326  1.1  thorpej 	/* Make sure we serve the map. */
    327  1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s/%s%s",
    328  1.1  thorpej 	    YP_DB_PATH, domain, map, YPDB_SUFFIX);
    329  1.1  thorpej 	map_path[sizeof(map_path) - 1] = '\0';
    330  1.1  thorpej 	if (stat(map_path, &finfo) != 0) {
    331  1.1  thorpej 		status = YPPUSH_NOMAP;
    332  1.1  thorpej 		goto out;
    333  1.1  thorpej 	}
    334  1.1  thorpej 
    335  1.1  thorpej 	/* Open the map file. */
    336  1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s/%s",
    337  1.1  thorpej 	    YP_DB_PATH, domain, map);
    338  1.1  thorpej 	map_path[sizeof(map_path) - 1] = '\0';
    339  1.1  thorpej 	db = ypdb_open(map_path, O_RDONLY, 0444);
    340  1.1  thorpej 	if (db == NULL) {
    341  1.1  thorpej 		status = YPPUSH_DBM;
    342  1.1  thorpej 		goto out;
    343  1.1  thorpej 	}
    344  1.1  thorpej 
    345  1.1  thorpej 	k.dptr = (char *)&order_key;
    346  1.1  thorpej 	k.dsize = YP_LAST_LEN;
    347  1.1  thorpej 
    348  1.1  thorpej 	v = ypdb_fetch(db, k);
    349  1.1  thorpej 	ypdb_close(db);
    350  1.1  thorpej 
    351  1.1  thorpej 	if (v.dptr == NULL)
    352  1.1  thorpej 		*lordernum = 0;
    353  1.1  thorpej 	else {
    354  1.1  thorpej 		strncpy(order, v.dptr, v.dsize);
    355  1.1  thorpej 		order[v.dsize] = '\0';
    356  1.1  thorpej 		*lordernum = (u_int)atoi((char *)&order);
    357  1.1  thorpej 	}
    358  1.1  thorpej 
    359  1.1  thorpej  out:
    360  1.1  thorpej 	if ((status == YPPUSH_NOMAP) || (status == YPPUSH_DBM)) {
    361  1.1  thorpej 		*lordernum = 0;
    362  1.1  thorpej 		status = YPPUSH_SUCC;
    363  1.1  thorpej 	}
    364  1.1  thorpej 
    365  1.1  thorpej 	return (status);
    366  1.1  thorpej }
    367  1.1  thorpej 
    368  1.1  thorpej int
    369  1.1  thorpej get_remote_ordernum(client, domain, map, lordernum, rordernum)
    370  1.1  thorpej 	CLIENT *client;
    371  1.1  thorpej 	char *domain, *map;
    372  1.1  thorpej 	u_int lordernum, *rordernum;
    373  1.1  thorpej {
    374  1.1  thorpej 	int status;
    375  1.1  thorpej 
    376  1.1  thorpej 	status = yp_order_host(client, domain, map, (int *)rordernum);
    377  1.1  thorpej 
    378  1.1  thorpej 	if (status == 0)
    379  1.1  thorpej 		if (*rordernum <= lordernum)
    380  1.1  thorpej 			status = YPPUSH_AGE;
    381  1.1  thorpej 		else
    382  1.1  thorpej 			status = YPPUSH_SUCC;
    383  1.1  thorpej 
    384  1.1  thorpej 	return status;
    385  1.1  thorpej }
    386  1.1  thorpej 
    387  1.1  thorpej void
    388  1.1  thorpej get_map(client, domain, map, incallback)
    389  1.1  thorpej 	CLIENT *client;
    390  1.1  thorpej 	char *domain, *map;
    391  1.1  thorpej 	struct ypall_callback *incallback;
    392  1.1  thorpej {
    393  1.1  thorpej 
    394  1.1  thorpej 	(void)yp_all_host(client, domain, map, incallback);
    395  1.1  thorpej }
    396  1.1  thorpej 
    397  1.1  thorpej DBM *
    398  1.1  thorpej create_db(domain, map, temp_map)
    399  1.1  thorpej 	char *domain, *map, *temp_map;
    400  1.1  thorpej {
    401  1.1  thorpej 	char db_temp[255];
    402  1.1  thorpej 	DBM *db;
    403  1.1  thorpej 
    404  1.1  thorpej 	snprintf(db_temp, sizeof(db_temp), "%s/%s/%s",
    405  1.1  thorpej 	    YP_DB_PATH, domain, temp_map);
    406  1.1  thorpej 	db_temp[sizeof(db_temp) - 1] = '\0';
    407  1.1  thorpej 
    408  1.3    lukem 	db = ypdb_open(db_temp, O_RDWR|O_CREAT|O_EXCL, 0444);
    409  1.1  thorpej 
    410  1.1  thorpej 	return db;
    411  1.1  thorpej }
    412  1.1  thorpej 
    413  1.1  thorpej int
    414  1.5    lukem install_db(domain, map, temp_map)
    415  1.1  thorpej 	char *domain, *map, *temp_map;
    416  1.1  thorpej {
    417  1.1  thorpej 	char db_name[255], db_temp[255];
    418  1.1  thorpej 
    419  1.1  thorpej 	snprintf(db_name, sizeof(db_name), "%s/%s/%s%s",
    420  1.1  thorpej 	    YP_DB_PATH, domain, map, YPDB_SUFFIX);
    421  1.1  thorpej 	db_name[sizeof(db_name) - 1] = '\0';
    422  1.1  thorpej 
    423  1.1  thorpej 	snprintf(db_temp, sizeof(db_temp), "%s/%s/%s%s",
    424  1.1  thorpej 	    YP_DB_PATH, domain, temp_map, YPDB_SUFFIX);
    425  1.1  thorpej 	db_temp[sizeof(db_temp) - 1] = '\0';
    426  1.1  thorpej 
    427  1.1  thorpej 	if (rename(db_temp, db_name)) {
    428  1.1  thorpej 		warn("can't rename `%s' -> `%s'", db_temp, db_name);
    429  1.1  thorpej 		return YPPUSH_YPERR;
    430  1.1  thorpej 	}
    431  1.1  thorpej 
    432  1.1  thorpej 	return YPPUSH_SUCC;
    433  1.1  thorpej }
    434  1.1  thorpej 
    435  1.1  thorpej int
    436  1.1  thorpej unlink_db(domain, map, temp_map)
    437  1.1  thorpej 	char *domain, *map, *temp_map;
    438  1.1  thorpej {
    439  1.1  thorpej 	char db_temp[255];
    440  1.1  thorpej 
    441  1.1  thorpej 	snprintf(db_temp, sizeof(db_temp), "%s/%s/%s%s",
    442  1.1  thorpej 	    YP_DB_PATH, domain, temp_map, YPDB_SUFFIX);
    443  1.1  thorpej 	db_temp[sizeof(db_temp) - 1] = '\0';
    444  1.1  thorpej 
    445  1.1  thorpej 	if (unlink(db_temp)) {
    446  1.1  thorpej 		warn("can't unlink `%s'", db_temp);
    447  1.1  thorpej 		return YPPUSH_YPERR;
    448  1.1  thorpej 	}
    449  1.1  thorpej 
    450  1.1  thorpej 	return YPPUSH_SUCC;
    451  1.1  thorpej }
    452  1.1  thorpej 
    453  1.1  thorpej int
    454  1.1  thorpej add_order(db, ordernum)
    455  1.1  thorpej 	DBM *db;
    456  1.1  thorpej 	u_int ordernum;
    457  1.1  thorpej {
    458  1.1  thorpej 	char datestr[11];
    459  1.1  thorpej 	datum key, val;
    460  1.1  thorpej 	char keystr[] = YP_LAST_KEY;
    461  1.1  thorpej 	int status;
    462  1.1  thorpej 
    463  1.1  thorpej 	snprintf(datestr, sizeof(datestr), "%010d", ordernum);
    464  1.1  thorpej 	datestr[sizeof(datestr) - 1] = '\0';
    465  1.1  thorpej 
    466  1.1  thorpej 	key.dptr = keystr;
    467  1.1  thorpej 	key.dsize = strlen(keystr);
    468  1.1  thorpej 
    469  1.1  thorpej 	val.dptr = datestr;
    470  1.1  thorpej 	val.dsize = strlen(datestr);
    471  1.1  thorpej 
    472  1.1  thorpej 	status = ypdb_store(db, key, val, YPDB_INSERT);
    473  1.1  thorpej 	if(status >= 0)
    474  1.1  thorpej 		status = YPPUSH_SUCC;
    475  1.1  thorpej 	else
    476  1.1  thorpej 		status = YPPUSH_DBM;
    477  1.1  thorpej 
    478  1.1  thorpej 	return (status);
    479  1.1  thorpej }
    480  1.1  thorpej 
    481  1.1  thorpej int
    482  1.1  thorpej add_master(client, domain, map, db)
    483  1.1  thorpej 	CLIENT *client;
    484  1.1  thorpej 	char *domain, *map;
    485  1.1  thorpej 	DBM *db;
    486  1.1  thorpej {
    487  1.1  thorpej 	char keystr[] = YP_MASTER_KEY;
    488  1.1  thorpej 	char *master;
    489  1.1  thorpej 	int status;
    490  1.5    lukem 	datum key, val;
    491  1.1  thorpej 
    492  1.1  thorpej 	master = NULL;
    493  1.1  thorpej 
    494  1.1  thorpej 	/* Get MASTER */
    495  1.1  thorpej 	status = yp_master_host(client, domain, map, &master);
    496  1.1  thorpej 
    497  1.1  thorpej 	if (master != NULL) {
    498  1.1  thorpej 		key.dptr = keystr;
    499  1.1  thorpej 		key.dsize = strlen(keystr);
    500  1.1  thorpej 
    501  1.1  thorpej 		val.dptr = master;
    502  1.1  thorpej 		val.dsize = strlen(master);
    503  1.1  thorpej 
    504  1.1  thorpej 		status = ypdb_store(db, key, val, YPDB_INSERT);
    505  1.1  thorpej 		if (status >= 0)
    506  1.1  thorpej 			status = YPPUSH_SUCC;
    507  1.1  thorpej 		else
    508  1.1  thorpej 			status = YPPUSH_DBM;
    509  1.1  thorpej 	}
    510  1.1  thorpej 
    511  1.1  thorpej 	return status;
    512  1.1  thorpej }
    513  1.1  thorpej 
    514  1.1  thorpej int
    515  1.1  thorpej add_interdomain(client, domain, map, db)
    516  1.1  thorpej 	CLIENT *client;
    517  1.1  thorpej 	char *domain, *map;
    518  1.1  thorpej 	DBM *db;
    519  1.1  thorpej {
    520  1.1  thorpej 	char keystr[] = YP_INTERDOMAIN_KEY;
    521  1.1  thorpej 	char *value;
    522  1.1  thorpej 	int vallen;
    523  1.1  thorpej 	int status;
    524  1.1  thorpej 	datum k, v;
    525  1.1  thorpej 
    526  1.1  thorpej 	/* Get INTERDOMAIN */
    527  1.1  thorpej 	k.dptr = keystr;
    528  1.1  thorpej 	k.dsize = strlen(keystr);
    529  1.1  thorpej 
    530  1.1  thorpej 	status = yp_match_host(client, domain, map,
    531  1.1  thorpej 	    k.dptr, k.dsize, &value, &vallen);
    532  1.1  thorpej 
    533  1.1  thorpej 	if (status == 0 && value) {
    534  1.1  thorpej 		v.dptr = value;
    535  1.1  thorpej 		v.dsize = vallen;
    536  1.1  thorpej 
    537  1.1  thorpej 		if (v.dptr != NULL) {
    538  1.1  thorpej 			status = ypdb_store(db, k, v, YPDB_INSERT);
    539  1.1  thorpej 			if (status >= 0)
    540  1.1  thorpej 				status = YPPUSH_SUCC;
    541  1.1  thorpej 			else
    542  1.1  thorpej 				status = YPPUSH_DBM;
    543  1.1  thorpej 		}
    544  1.1  thorpej 	}
    545  1.1  thorpej 
    546  1.1  thorpej 	return status;
    547  1.1  thorpej }
    548  1.1  thorpej 
    549  1.1  thorpej int
    550  1.1  thorpej add_secure(client, domain, map, db)
    551  1.1  thorpej 	CLIENT *client;
    552  1.1  thorpej 	char *domain, *map;
    553  1.1  thorpej 	DBM *db;
    554  1.1  thorpej {
    555  1.1  thorpej 	char keystr[] = YP_SECURE_KEY;
    556  1.1  thorpej 	char *value;
    557  1.1  thorpej 	int vallen;
    558  1.1  thorpej 	int status;
    559  1.1  thorpej 	datum k, v;
    560  1.1  thorpej 
    561  1.1  thorpej 	/* Get SECURE */
    562  1.1  thorpej 	k.dptr = keystr;
    563  1.1  thorpej 	k.dsize = strlen(keystr);
    564  1.1  thorpej 
    565  1.1  thorpej 	status = yp_match_host(client, domain, map,
    566  1.1  thorpej 	    k.dptr, k.dsize, &value, &vallen);
    567  1.1  thorpej 
    568  1.1  thorpej 	if (status > 0) {
    569  1.1  thorpej 		v.dptr = value;
    570  1.1  thorpej 		v.dsize = vallen;
    571  1.1  thorpej 
    572  1.1  thorpej 		if (v.dptr != NULL) {
    573  1.1  thorpej 			status = ypdb_store(db, k, v, YPDB_INSERT);
    574  1.1  thorpej 			if (status >= 0)
    575  1.1  thorpej 				status = YPPUSH_SUCC;
    576  1.1  thorpej 			else
    577  1.1  thorpej 				status = YPPUSH_DBM;
    578  1.1  thorpej 		}
    579  1.1  thorpej 	}
    580  1.1  thorpej 
    581  1.1  thorpej 	return status;
    582  1.1  thorpej }
    583  1.1  thorpej 
    584  1.1  thorpej int
    585  1.1  thorpej send_clear(client)
    586  1.1  thorpej 	CLIENT *client;
    587  1.1  thorpej {
    588  1.1  thorpej 	struct timeval tv;
    589  1.1  thorpej 	int r;
    590  1.1  thorpej 	int status;
    591  1.1  thorpej 
    592  1.1  thorpej 	status = YPPUSH_SUCC;
    593  1.1  thorpej 
    594  1.1  thorpej 	tv.tv_sec = 10;
    595  1.1  thorpej 	tv.tv_usec = 0;
    596  1.1  thorpej 
    597  1.1  thorpej 	/* Send CLEAR */
    598  1.1  thorpej 	r = clnt_call(client, YPPROC_CLEAR, xdr_void, 0, xdr_void, 0, tv);
    599  1.1  thorpej 	if (r != RPC_SUCCESS) {
    600  1.1  thorpej 		clnt_perror(client, "yp_clear: clnt_call");
    601  1.1  thorpej 		status = YPPUSH_RPC;
    602  1.1  thorpej 	}
    603  1.1  thorpej 
    604  1.1  thorpej 	return status;
    605  1.1  thorpej }
    606  1.1  thorpej 
    607  1.1  thorpej int
    608  1.1  thorpej send_reply(client, status, tid)
    609  1.1  thorpej 	CLIENT *client;
    610  1.1  thorpej 	int status, tid;
    611  1.1  thorpej {
    612  1.1  thorpej 	struct timeval tv;
    613  1.1  thorpej 	struct ypresp_xfr resp;
    614  1.1  thorpej 	int r;
    615  1.1  thorpej 
    616  1.1  thorpej 	tv.tv_sec = 10;
    617  1.1  thorpej 	tv.tv_usec = 0;
    618  1.1  thorpej 
    619  1.1  thorpej 	resp.transid = tid;
    620  1.1  thorpej 	resp.xfrstat = status;
    621  1.1  thorpej 
    622  1.1  thorpej 	/* Send XFRRESP */
    623  1.1  thorpej 	r = clnt_call(client, YPPUSHPROC_XFRRESP, xdr_ypresp_xfr, &resp,
    624  1.1  thorpej 	    xdr_void, 0, tv);
    625  1.1  thorpej 	if (r != RPC_SUCCESS) {
    626  1.1  thorpej 		clnt_perror(client, "yppushresp_xdr: clnt_call");
    627  1.1  thorpej 		status = YPPUSH_RPC;
    628  1.1  thorpej 	}
    629  1.1  thorpej 
    630  1.1  thorpej 	return status;
    631  1.1  thorpej }
    632