Home | History | Annotate | Line # | Download | only in ypxfr
ypxfr.c revision 1.15.10.1
      1  1.15.10.1      jym /*	$NetBSD: ypxfr.c,v 1.15.10.1 2009/05/13 19:20:45 jym 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.15.10.1      jym __RCSID("$NetBSD: ypxfr.c,v 1.15.10.1 2009/05/13 19:20:45 jym Exp $");
     37        1.6    lukem #endif
     38        1.1  thorpej 
     39       1.15    lukem #include <sys/param.h>
     40        1.1  thorpej #include <sys/types.h>
     41        1.1  thorpej #include <sys/stat.h>
     42        1.1  thorpej #include <sys/socket.h>
     43        1.1  thorpej 
     44        1.1  thorpej #include <netinet/in.h>
     45        1.1  thorpej #include <arpa/inet.h>
     46        1.1  thorpej 
     47        1.4  thorpej #include <err.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.8  thorpej #include <syslog.h>
     53        1.1  thorpej #include <unistd.h>
     54        1.1  thorpej 
     55        1.1  thorpej #include <rpc/rpc.h>
     56        1.1  thorpej #include <rpc/xdr.h>
     57        1.1  thorpej #include <rpcsvc/yp_prot.h>
     58        1.1  thorpej #include <rpcsvc/ypclnt.h>
     59        1.1  thorpej 
     60        1.1  thorpej #include "yplib_host.h"
     61        1.1  thorpej #include "ypdb.h"
     62        1.1  thorpej #include "ypdef.h"
     63        1.1  thorpej 
     64        1.1  thorpej DBM	*db;
     65        1.1  thorpej 
     66       1.12      wiz static	int ypxfr_foreach(int, char *, int, char *, int, char *);
     67        1.1  thorpej 
     68       1.12      wiz int	main(int, char *[]);
     69       1.12      wiz int	get_local_ordernum(char *, char *, u_int *);
     70       1.12      wiz int	get_remote_ordernum(CLIENT *, char *, char *, u_int, u_int *);
     71       1.12      wiz void	get_map(CLIENT *, char *, char *, struct ypall_callback *);
     72       1.15    lukem DBM	*create_db(char *, char *, char *, size_t);
     73       1.12      wiz int	install_db(char *, char *, char *);
     74       1.12      wiz int	unlink_db(char *, char *, char *);
     75       1.12      wiz int	add_order(DBM *, u_int);
     76       1.12      wiz int	add_master(CLIENT *, char *, char *, DBM *);
     77       1.12      wiz int	add_interdomain(CLIENT *, char *, char *, DBM *);
     78       1.12      wiz int	add_secure(CLIENT *, char *, char *, DBM *);
     79       1.12      wiz int	send_clear(CLIENT *);
     80       1.12      wiz int	send_reply(CLIENT *, int, int);
     81       1.12      wiz 
     82       1.12      wiz int
     83       1.12      wiz main(int argc, char **argv)
     84        1.1  thorpej {
     85        1.1  thorpej 	int need_usage = 0, cflag = 0, fflag = 0, Cflag = 0;
     86        1.1  thorpej 	int ch;
     87        1.1  thorpej 	char *domain;
     88        1.1  thorpej 	char *host = NULL;
     89        1.1  thorpej 	char *srcdomain = NULL;
     90        1.1  thorpej 	char *tid = NULL;
     91        1.1  thorpej 	char *prog = NULL;
     92        1.1  thorpej 	char *ipadd = NULL;
     93        1.1  thorpej 	char *port = NULL;
     94        1.1  thorpej 	char *map = NULL;
     95        1.1  thorpej 	u_int ordernum, new_ordernum;
     96        1.1  thorpej 	struct ypall_callback callback;
     97        1.1  thorpej 	CLIENT *client;
     98       1.15    lukem 	char temp_map[MAXPATHLEN];
     99        1.1  thorpej 	int status, xfr_status;
    100        1.1  thorpej 
    101        1.1  thorpej 	status = YPPUSH_SUCC;
    102        1.1  thorpej 	client = NULL;
    103        1.1  thorpej 
    104        1.1  thorpej 	if (yp_get_default_domain(&domain))
    105        1.1  thorpej 		errx(1, "can't get YP domain name");
    106        1.1  thorpej 
    107        1.1  thorpej 	while ((ch = getopt(argc, argv, "cd:fh:s:C:")) != -1) {
    108        1.1  thorpej 		switch (ch) {
    109        1.1  thorpej 		case 'c':
    110        1.1  thorpej 			cflag = 1;
    111        1.1  thorpej 			break;
    112        1.1  thorpej 
    113        1.1  thorpej 		case 'd':
    114        1.1  thorpej 			domain = optarg;
    115        1.1  thorpej 			break;
    116        1.1  thorpej 
    117        1.1  thorpej 		case 'f':
    118        1.1  thorpej 			fflag = 1;
    119        1.1  thorpej 			break;
    120        1.1  thorpej 
    121        1.1  thorpej 		case 'h':
    122        1.1  thorpej 			host = optarg;
    123        1.1  thorpej 			break;
    124        1.1  thorpej 
    125        1.1  thorpej 		case 's':
    126        1.1  thorpej 			srcdomain = optarg;
    127        1.1  thorpej 			break;
    128        1.1  thorpej 
    129        1.1  thorpej 		case 'C':
    130        1.2    chuck 			if (optind + 3 >= argc) {
    131        1.2    chuck 				need_usage = 1;
    132        1.2    chuck 				optind = argc;
    133        1.2    chuck 				break;
    134        1.2    chuck 			}
    135        1.1  thorpej 			Cflag = 1;
    136        1.1  thorpej 			tid = optarg;
    137        1.1  thorpej 			prog = argv[optind++];
    138        1.1  thorpej 			ipadd = argv[optind++];
    139        1.1  thorpej 			port = argv[optind++];
    140        1.1  thorpej 			break;
    141        1.1  thorpej 
    142        1.1  thorpej 		default:
    143        1.1  thorpej 			need_usage = 1;
    144        1.1  thorpej 		}
    145        1.1  thorpej 	}
    146        1.1  thorpej 	argc -= optind; argv += optind;
    147        1.1  thorpej 
    148        1.1  thorpej 	if (argc != 1)
    149        1.1  thorpej 		need_usage = 1;
    150        1.1  thorpej 
    151        1.1  thorpej 	map = argv[0];
    152        1.1  thorpej 
    153        1.1  thorpej 	if (need_usage) {
    154        1.1  thorpej 		status = YPPUSH_BADARGS;
    155        1.1  thorpej 		fprintf(stderr, "usage: %s [-cf] [-d domain] [-h host] %s\n",
    156       1.11      cgd 		   getprogname(),
    157       1.11      cgd 		   "[-s domain] [-C tid prog ipadd port] mapname");
    158        1.1  thorpej 		goto punt;
    159        1.1  thorpej 	}
    160        1.1  thorpej 
    161        1.8  thorpej #ifdef DEBUG
    162       1.10    lukem 	openlog("ypxfr", LOG_PID, LOG_DAEMON);
    163        1.1  thorpej 
    164        1.8  thorpej 	syslog(LOG_DEBUG, "ypxfr: Arguments:");
    165        1.8  thorpej 	syslog(LOG_DEBUG, "YP clear to local: %s", (cflag) ? "no" : "yes");
    166        1.8  thorpej 	syslog(LOG_DEBUG, "   Force transfer: %s", (fflag) ? "yes" : "no");
    167        1.8  thorpej 	syslog(LOG_DEBUG, "           domain: %s", domain);
    168        1.8  thorpej 	syslog(LOG_DEBUG, "             host: %s", host);
    169        1.8  thorpej 	syslog(LOG_DEBUG, "    source domain: %s", srcdomain);
    170        1.8  thorpej 	syslog(LOG_DEBUG, "          transid: %s", tid);
    171        1.8  thorpej 	syslog(LOG_DEBUG, "             prog: %s", prog);
    172        1.8  thorpej 	syslog(LOG_DEBUG, "             port: %s", port);
    173        1.8  thorpej 	syslog(LOG_DEBUG, "            ipadd: %s", ipadd);
    174        1.8  thorpej 	syslog(LOG_DEBUG, "              map: %s", map);
    175        1.8  thorpej #endif
    176        1.1  thorpej 
    177        1.1  thorpej 	if (fflag != 0)
    178        1.1  thorpej 		ordernum = 0;
    179        1.1  thorpej 	else {
    180        1.1  thorpej 		status = get_local_ordernum(domain, map, &ordernum);
    181        1.1  thorpej 		if (status < 0)
    182        1.1  thorpej 			goto punt;
    183        1.1  thorpej 	}
    184        1.1  thorpej 
    185       1.10    lukem #ifdef DEBUG
    186        1.8  thorpej         syslog(LOG_DEBUG, "Get Master");
    187       1.10    lukem #endif
    188        1.1  thorpej 
    189        1.1  thorpej 	if (host == NULL) {
    190        1.1  thorpej 		if (srcdomain == NULL)
    191        1.5    lukem 			status = yp_master(domain, map, &host);
    192        1.1  thorpej 	        else
    193        1.5    lukem 			status = yp_master(srcdomain, map, &host);
    194        1.1  thorpej 
    195        1.1  thorpej 		if (status == 0)
    196        1.1  thorpej 			status = YPPUSH_SUCC;
    197        1.1  thorpej 		else {
    198        1.1  thorpej 			status = -status;
    199        1.1  thorpej 			goto punt;
    200        1.1  thorpej 		}
    201        1.1  thorpej 	}
    202        1.1  thorpej 
    203       1.10    lukem #ifdef DEBUG
    204        1.8  thorpej         syslog(LOG_DEBUG, "Connect host: %s", host);
    205       1.10    lukem #endif
    206        1.1  thorpej 
    207        1.5    lukem 	client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
    208        1.1  thorpej 
    209        1.5    lukem 	status = get_remote_ordernum(client, domain, map, ordernum,
    210        1.5    lukem 	    &new_ordernum);
    211        1.1  thorpej 
    212        1.1  thorpej 
    213        1.1  thorpej 	if (status == YPPUSH_SUCC) {
    214        1.1  thorpej 		/* Create temporary db */
    215       1.15    lukem 		db = create_db(domain, map, temp_map, sizeof(temp_map));
    216        1.1  thorpej 		if (db == NULL)
    217        1.1  thorpej 			status = YPPUSH_DBM;
    218        1.1  thorpej 
    219        1.1  thorpej 	  	/* Add ORDER */
    220        1.1  thorpej 		if (status > 0)
    221        1.1  thorpej 			status = add_order(db, new_ordernum);
    222        1.1  thorpej 
    223        1.1  thorpej 		/* Add MASTER */
    224        1.1  thorpej 		if (status > 0)
    225        1.1  thorpej 			status = add_master(client, domain, map, db);
    226        1.1  thorpej 
    227        1.1  thorpej 	        /* Add INTERDOMAIN */
    228        1.1  thorpej 		if (status > 0)
    229        1.1  thorpej 			status = add_interdomain(client, domain, map, db);
    230        1.1  thorpej 
    231        1.1  thorpej 	        /* Add SECURE */
    232        1.1  thorpej 		if (status > 0)
    233        1.1  thorpej 			status = add_secure(client, domain, map, db);
    234        1.1  thorpej 
    235        1.1  thorpej 		if (status > 0) {
    236        1.4  thorpej 			callback.foreach = ypxfr_foreach;
    237        1.1  thorpej 			get_map(client, domain, map, &callback);
    238        1.1  thorpej 		}
    239        1.1  thorpej 
    240        1.1  thorpej 		/* Close db */
    241        1.1  thorpej 		if (db != NULL)
    242        1.1  thorpej 			ypdb_close(db);
    243        1.1  thorpej 
    244        1.1  thorpej 		/* Rename db */
    245        1.1  thorpej 		if (status > 0)
    246       1.15    lukem 			status = install_db(domain, map, temp_map);
    247        1.1  thorpej 		else
    248       1.15    lukem 			status = unlink_db(domain, map, temp_map);
    249        1.1  thorpej 	}
    250        1.1  thorpej 
    251        1.1  thorpej  punt:
    252        1.1  thorpej 	xfr_status = status;
    253        1.1  thorpej 
    254        1.1  thorpej 	if (client != NULL)
    255        1.1  thorpej 		clnt_destroy(client);
    256        1.1  thorpej 
    257        1.1  thorpej 	/* YP_CLEAR */
    258        1.1  thorpej 	if (!cflag) {
    259        1.1  thorpej 		client = yp_bind_local(YPPROG, YPVERS);
    260        1.1  thorpej 		status = send_clear(client);
    261        1.1  thorpej 		clnt_destroy(client);
    262        1.1  thorpej 	}
    263        1.1  thorpej 
    264        1.1  thorpej 	if (Cflag > 0) {
    265        1.1  thorpej 		/* Send Response */
    266        1.1  thorpej 		client = yp_bind_host(ipadd, atoi(prog), 1, atoi(port), 0);
    267        1.1  thorpej 		status = send_reply(client, xfr_status, atoi(tid));
    268        1.1  thorpej 		clnt_destroy(client);
    269        1.1  thorpej 	}
    270        1.1  thorpej 
    271        1.1  thorpej 	exit (0);
    272        1.1  thorpej }
    273        1.1  thorpej 
    274        1.1  thorpej static int
    275       1.12      wiz ypxfr_foreach(int status, char *keystr, int keylen, char *valstr,
    276       1.12      wiz 	      int vallen, char *data)
    277        1.1  thorpej {
    278        1.1  thorpej 	datum key, val;
    279        1.1  thorpej 
    280        1.1  thorpej 	if (status == YP_NOMORE)
    281        1.1  thorpej 		return (0);
    282        1.1  thorpej 
    283        1.1  thorpej 	keystr[keylen] = '\0';
    284        1.1  thorpej 	valstr[vallen] = '\0';
    285        1.1  thorpej 
    286        1.1  thorpej 	key.dptr = keystr;
    287        1.1  thorpej 	key.dsize = strlen(keystr);
    288        1.1  thorpej 
    289        1.1  thorpej 	val.dptr = valstr;
    290        1.1  thorpej 	val.dsize = strlen(valstr);
    291        1.1  thorpej 
    292        1.1  thorpej 	ypdb_store(db, key, val, YPDB_INSERT);
    293        1.1  thorpej 
    294        1.1  thorpej 	return (0);
    295        1.1  thorpej }
    296        1.1  thorpej 
    297        1.1  thorpej int
    298       1.12      wiz get_local_ordernum(char *domain, char *map, u_int *lordernum)
    299        1.1  thorpej {
    300        1.1  thorpej 	char map_path[1024];
    301        1.1  thorpej 	char order_key[] = YP_LAST_KEY;
    302        1.1  thorpej 	char order[MAX_LAST_LEN+1];
    303        1.1  thorpej 	struct stat finfo;
    304  1.15.10.1      jym 	DBM *ldb;
    305        1.5    lukem 	datum k, v;
    306  1.15.10.1      jym 	unsigned int status;
    307        1.1  thorpej 
    308        1.1  thorpej 	status = YPPUSH_SUCC;
    309        1.1  thorpej 
    310        1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s", YP_DB_PATH, domain);
    311        1.1  thorpej 
    312        1.1  thorpej 	/* Make sure we serve the domain. */
    313        1.1  thorpej 	if ((stat(map_path, &finfo)) != 0 ||
    314        1.1  thorpej 	    (S_ISDIR(finfo.st_mode) == 0)) {
    315        1.4  thorpej 		warnx("domain `%s' not found locally", domain);
    316        1.1  thorpej 		status = YPPUSH_NODOM;
    317        1.1  thorpej 		goto out;
    318        1.1  thorpej 	}
    319        1.1  thorpej 
    320        1.1  thorpej 	/* Make sure we serve the map. */
    321        1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s/%s%s",
    322        1.1  thorpej 	    YP_DB_PATH, domain, map, YPDB_SUFFIX);
    323        1.1  thorpej 	if (stat(map_path, &finfo) != 0) {
    324        1.1  thorpej 		status = YPPUSH_NOMAP;
    325        1.1  thorpej 		goto out;
    326        1.1  thorpej 	}
    327        1.1  thorpej 
    328        1.1  thorpej 	/* Open the map file. */
    329        1.1  thorpej 	snprintf(map_path, sizeof(map_path), "%s/%s/%s",
    330        1.1  thorpej 	    YP_DB_PATH, domain, map);
    331  1.15.10.1      jym 	ldb = ypdb_open(map_path);
    332  1.15.10.1      jym 	if (ldb == NULL) {
    333        1.1  thorpej 		status = YPPUSH_DBM;
    334        1.1  thorpej 		goto out;
    335        1.1  thorpej 	}
    336        1.1  thorpej 
    337        1.1  thorpej 	k.dptr = (char *)&order_key;
    338        1.1  thorpej 	k.dsize = YP_LAST_LEN;
    339        1.1  thorpej 
    340  1.15.10.1      jym 	v = ypdb_fetch(ldb, k);
    341        1.1  thorpej 
    342        1.1  thorpej 	if (v.dptr == NULL)
    343        1.1  thorpej 		*lordernum = 0;
    344        1.1  thorpej 	else {
    345        1.1  thorpej 		strncpy(order, v.dptr, v.dsize);
    346        1.1  thorpej 		order[v.dsize] = '\0';
    347        1.1  thorpej 		*lordernum = (u_int)atoi((char *)&order);
    348        1.1  thorpej 	}
    349  1.15.10.1      jym 	ypdb_close(ldb);
    350        1.1  thorpej 
    351        1.1  thorpej  out:
    352        1.1  thorpej 	if ((status == YPPUSH_NOMAP) || (status == YPPUSH_DBM)) {
    353        1.1  thorpej 		*lordernum = 0;
    354        1.1  thorpej 		status = YPPUSH_SUCC;
    355        1.1  thorpej 	}
    356        1.1  thorpej 
    357        1.1  thorpej 	return (status);
    358        1.1  thorpej }
    359        1.1  thorpej 
    360        1.1  thorpej int
    361       1.12      wiz get_remote_ordernum(CLIENT *client, char *domain, char *map,
    362       1.12      wiz 		    u_int lordernum, u_int *rordernum)
    363        1.1  thorpej {
    364        1.1  thorpej 	int status;
    365        1.1  thorpej 
    366        1.1  thorpej 	status = yp_order_host(client, domain, map, (int *)rordernum);
    367        1.1  thorpej 
    368        1.7     ross 	if (status == 0) {
    369        1.1  thorpej 		if (*rordernum <= lordernum)
    370        1.1  thorpej 			status = YPPUSH_AGE;
    371        1.1  thorpej 		else
    372        1.1  thorpej 			status = YPPUSH_SUCC;
    373        1.7     ross 	}
    374        1.1  thorpej 
    375        1.1  thorpej 	return status;
    376        1.1  thorpej }
    377        1.1  thorpej 
    378        1.1  thorpej void
    379       1.12      wiz get_map(CLIENT *client, char *domain, char *map,
    380       1.12      wiz 	struct ypall_callback *incallback)
    381        1.1  thorpej {
    382        1.1  thorpej 
    383        1.1  thorpej 	(void)yp_all_host(client, domain, map, incallback);
    384        1.1  thorpej }
    385        1.1  thorpej 
    386        1.1  thorpej DBM *
    387       1.15    lukem create_db(char *domain, char *map, char *db_temp, size_t db_temp_len)
    388        1.1  thorpej {
    389       1.15    lukem 	static const char template[] = "ypdbXXXXXX";
    390  1.15.10.1      jym 	DBM *ldb;
    391        1.1  thorpej 
    392       1.15    lukem 	snprintf(db_temp, db_temp_len, "%s/%s/%s",
    393       1.15    lukem 	    YP_DB_PATH, domain, template);
    394        1.1  thorpej 
    395  1.15.10.1      jym 	ldb = ypdb_mktemp(db_temp);
    396        1.1  thorpej 
    397  1.15.10.1      jym 	return ldb;
    398        1.1  thorpej }
    399        1.1  thorpej 
    400        1.1  thorpej int
    401       1.15    lukem install_db(char *domain, char *map, char *db_temp)
    402        1.1  thorpej {
    403       1.15    lukem 	char db_name[MAXPATHLEN];
    404        1.1  thorpej 
    405        1.1  thorpej 	snprintf(db_name, sizeof(db_name), "%s/%s/%s%s",
    406        1.1  thorpej 	    YP_DB_PATH, domain, map, YPDB_SUFFIX);
    407        1.1  thorpej 
    408        1.1  thorpej 	if (rename(db_temp, db_name)) {
    409        1.1  thorpej 		warn("can't rename `%s' -> `%s'", db_temp, db_name);
    410        1.1  thorpej 		return YPPUSH_YPERR;
    411        1.1  thorpej 	}
    412        1.1  thorpej 
    413        1.1  thorpej 	return YPPUSH_SUCC;
    414        1.1  thorpej }
    415        1.1  thorpej 
    416        1.1  thorpej int
    417       1.15    lukem unlink_db(char *domain, char *map, char *db_temp)
    418        1.1  thorpej {
    419        1.1  thorpej 
    420        1.1  thorpej 	if (unlink(db_temp)) {
    421        1.1  thorpej 		warn("can't unlink `%s'", db_temp);
    422        1.1  thorpej 		return YPPUSH_YPERR;
    423        1.1  thorpej 	}
    424        1.1  thorpej 
    425        1.1  thorpej 	return YPPUSH_SUCC;
    426        1.1  thorpej }
    427        1.1  thorpej 
    428        1.1  thorpej int
    429  1.15.10.1      jym add_order(DBM *ldb, u_int ordernum)
    430        1.1  thorpej {
    431        1.1  thorpej 	char datestr[11];
    432        1.1  thorpej 	datum key, val;
    433        1.1  thorpej 	char keystr[] = YP_LAST_KEY;
    434        1.1  thorpej 	int status;
    435        1.1  thorpej 
    436        1.1  thorpej 	snprintf(datestr, sizeof(datestr), "%010d", ordernum);
    437        1.1  thorpej 
    438        1.1  thorpej 	key.dptr = keystr;
    439        1.1  thorpej 	key.dsize = strlen(keystr);
    440        1.1  thorpej 
    441        1.1  thorpej 	val.dptr = datestr;
    442        1.1  thorpej 	val.dsize = strlen(datestr);
    443        1.1  thorpej 
    444  1.15.10.1      jym 	status = ypdb_store(ldb, key, val, YPDB_INSERT);
    445        1.1  thorpej 	if(status >= 0)
    446        1.1  thorpej 		status = YPPUSH_SUCC;
    447        1.1  thorpej 	else
    448        1.1  thorpej 		status = YPPUSH_DBM;
    449        1.1  thorpej 
    450        1.1  thorpej 	return (status);
    451        1.1  thorpej }
    452        1.1  thorpej 
    453        1.1  thorpej int
    454  1.15.10.1      jym add_master(CLIENT *client, char *domain, char *map, DBM *ldb)
    455        1.1  thorpej {
    456        1.1  thorpej 	char keystr[] = YP_MASTER_KEY;
    457        1.1  thorpej 	char *master;
    458        1.1  thorpej 	int status;
    459        1.5    lukem 	datum key, val;
    460        1.1  thorpej 
    461        1.1  thorpej 	master = NULL;
    462        1.1  thorpej 
    463        1.1  thorpej 	/* Get MASTER */
    464        1.1  thorpej 	status = yp_master_host(client, domain, map, &master);
    465        1.1  thorpej 
    466        1.1  thorpej 	if (master != NULL) {
    467        1.1  thorpej 		key.dptr = keystr;
    468        1.1  thorpej 		key.dsize = strlen(keystr);
    469        1.1  thorpej 
    470        1.1  thorpej 		val.dptr = master;
    471        1.1  thorpej 		val.dsize = strlen(master);
    472        1.1  thorpej 
    473  1.15.10.1      jym 		status = ypdb_store(ldb, key, val, YPDB_INSERT);
    474        1.1  thorpej 		if (status >= 0)
    475        1.1  thorpej 			status = YPPUSH_SUCC;
    476        1.1  thorpej 		else
    477        1.1  thorpej 			status = YPPUSH_DBM;
    478        1.1  thorpej 	}
    479        1.1  thorpej 
    480        1.1  thorpej 	return status;
    481        1.1  thorpej }
    482        1.1  thorpej 
    483        1.1  thorpej int
    484  1.15.10.1      jym add_interdomain(CLIENT *client, char *domain, char *map, DBM *ldb)
    485        1.1  thorpej {
    486        1.1  thorpej 	char keystr[] = YP_INTERDOMAIN_KEY;
    487        1.1  thorpej 	char *value;
    488        1.1  thorpej 	int vallen;
    489        1.1  thorpej 	int status;
    490        1.1  thorpej 	datum k, v;
    491        1.1  thorpej 
    492        1.1  thorpej 	/* Get INTERDOMAIN */
    493        1.1  thorpej 	k.dptr = keystr;
    494        1.1  thorpej 	k.dsize = strlen(keystr);
    495        1.1  thorpej 
    496        1.1  thorpej 	status = yp_match_host(client, domain, map,
    497        1.1  thorpej 	    k.dptr, k.dsize, &value, &vallen);
    498        1.1  thorpej 
    499        1.1  thorpej 	if (status == 0 && value) {
    500        1.1  thorpej 		v.dptr = value;
    501        1.1  thorpej 		v.dsize = vallen;
    502        1.1  thorpej 
    503        1.1  thorpej 		if (v.dptr != NULL) {
    504  1.15.10.1      jym 			status = ypdb_store(ldb, k, v, 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 
    512        1.1  thorpej 	return status;
    513        1.1  thorpej }
    514        1.1  thorpej 
    515        1.1  thorpej int
    516  1.15.10.1      jym add_secure(CLIENT *client, char *domain, char *map, DBM *ldb)
    517        1.1  thorpej {
    518        1.1  thorpej 	char keystr[] = YP_SECURE_KEY;
    519        1.1  thorpej 	char *value;
    520        1.1  thorpej 	int vallen;
    521        1.1  thorpej 	int status;
    522        1.1  thorpej 	datum k, v;
    523        1.1  thorpej 
    524        1.1  thorpej 	/* Get SECURE */
    525        1.1  thorpej 	k.dptr = keystr;
    526        1.1  thorpej 	k.dsize = strlen(keystr);
    527        1.1  thorpej 
    528        1.1  thorpej 	status = yp_match_host(client, domain, map,
    529        1.1  thorpej 	    k.dptr, k.dsize, &value, &vallen);
    530        1.1  thorpej 
    531  1.15.10.1      jym 	if (status == 0 && value != 0) {
    532        1.1  thorpej 		v.dptr = value;
    533        1.1  thorpej 		v.dsize = vallen;
    534        1.1  thorpej 
    535        1.1  thorpej 		if (v.dptr != NULL) {
    536  1.15.10.1      jym 			status = ypdb_store(ldb, k, v, YPDB_INSERT);
    537        1.1  thorpej 			if (status >= 0)
    538        1.1  thorpej 				status = YPPUSH_SUCC;
    539        1.1  thorpej 			else
    540        1.1  thorpej 				status = YPPUSH_DBM;
    541        1.1  thorpej 		}
    542        1.1  thorpej 	}
    543        1.1  thorpej 
    544        1.1  thorpej 	return status;
    545        1.1  thorpej }
    546        1.1  thorpej 
    547        1.1  thorpej int
    548       1.12      wiz send_clear(CLIENT *client)
    549        1.1  thorpej {
    550        1.1  thorpej 	struct timeval tv;
    551        1.1  thorpej 	int r;
    552        1.1  thorpej 	int status;
    553        1.1  thorpej 
    554        1.1  thorpej 	status = YPPUSH_SUCC;
    555        1.1  thorpej 
    556        1.1  thorpej 	tv.tv_sec = 10;
    557        1.1  thorpej 	tv.tv_usec = 0;
    558        1.1  thorpej 
    559        1.1  thorpej 	/* Send CLEAR */
    560        1.1  thorpej 	r = clnt_call(client, YPPROC_CLEAR, xdr_void, 0, xdr_void, 0, tv);
    561        1.1  thorpej 	if (r != RPC_SUCCESS) {
    562        1.1  thorpej 		clnt_perror(client, "yp_clear: clnt_call");
    563        1.1  thorpej 		status = YPPUSH_RPC;
    564        1.1  thorpej 	}
    565        1.1  thorpej 
    566        1.1  thorpej 	return status;
    567        1.1  thorpej }
    568        1.1  thorpej 
    569        1.1  thorpej int
    570       1.12      wiz send_reply(CLIENT *client, int status, int tid)
    571        1.1  thorpej {
    572        1.1  thorpej 	struct timeval tv;
    573        1.1  thorpej 	struct ypresp_xfr resp;
    574        1.1  thorpej 	int r;
    575        1.1  thorpej 
    576        1.1  thorpej 	tv.tv_sec = 10;
    577        1.1  thorpej 	tv.tv_usec = 0;
    578        1.1  thorpej 
    579        1.1  thorpej 	resp.transid = tid;
    580        1.1  thorpej 	resp.xfrstat = status;
    581        1.1  thorpej 
    582        1.1  thorpej 	/* Send XFRRESP */
    583        1.1  thorpej 	r = clnt_call(client, YPPUSHPROC_XFRRESP, xdr_ypresp_xfr, &resp,
    584        1.1  thorpej 	    xdr_void, 0, tv);
    585        1.1  thorpej 	if (r != RPC_SUCCESS) {
    586        1.1  thorpej 		clnt_perror(client, "yppushresp_xdr: clnt_call");
    587        1.1  thorpej 		status = YPPUSH_RPC;
    588        1.1  thorpej 	}
    589        1.1  thorpej 
    590        1.1  thorpej 	return status;
    591        1.1  thorpej }
    592