Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.57
      1 /*	$NetBSD: ypbind.c,v 1.57 2007/07/07 22:33:57 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef LINT
     31 __RCSID("$NetBSD: ypbind.c,v 1.57 2007/07/07 22:33:57 christos Exp $");
     32 #endif
     33 
     34 #include <sys/param.h>
     35 #include <sys/types.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/signal.h>
     38 #include <sys/socket.h>
     39 #include <sys/file.h>
     40 #include <sys/uio.h>
     41 #include <sys/syslog.h>
     42 #include <sys/stat.h>
     43 #include <fcntl.h>
     44 #include <limits.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <errno.h>
     48 #include <syslog.h>
     49 #include <stdarg.h>
     50 #include <ctype.h>
     51 #include <dirent.h>
     52 #include <netdb.h>
     53 #include <string.h>
     54 #include <err.h>
     55 #include <rpc/rpc.h>
     56 #include <rpc/xdr.h>
     57 #include <net/if.h>
     58 #include <arpa/inet.h>
     59 #include <rpc/pmap_clnt.h>
     60 #include <rpc/pmap_prot.h>
     61 #include <rpc/pmap_rmt.h>
     62 #include <unistd.h>
     63 #include <util.h>
     64 #include <rpcsvc/yp_prot.h>
     65 #include <rpcsvc/ypclnt.h>
     66 #include <ifaddrs.h>
     67 
     68 #include "pathnames.h"
     69 
     70 #ifndef O_SHLOCK
     71 #define O_SHLOCK 0
     72 #endif
     73 
     74 #define BUFSIZE		1400
     75 
     76 #define YPSERVERSSUFF	".ypservers"
     77 #define BINDINGDIR	(_PATH_VAR_YP "binding")
     78 
     79 struct _dom_binding {
     80 	struct _dom_binding *dom_pnext;
     81 	char dom_domain[YPMAXDOMAIN + 1];
     82 	struct sockaddr_in dom_server_addr;
     83 	int dom_socket;
     84 	CLIENT *dom_client;
     85 	long dom_vers;
     86 	time_t dom_check_t;
     87 	time_t dom_ask_t;
     88 	int dom_lockfd;
     89 	int dom_alive;
     90 	u_int32_t dom_xid;
     91 };
     92 
     93 static char *domainname;
     94 
     95 static struct _dom_binding *ypbindlist;
     96 static int check;
     97 
     98 typedef enum {
     99 	YPBIND_DIRECT, YPBIND_BROADCAST, YPBIND_SETLOCAL, YPBIND_SETALL
    100 } ypbind_mode_t;
    101 
    102 ypbind_mode_t ypbindmode;
    103 
    104 /*
    105  * If ypbindmode is YPBIND_SETLOCAL or YPBIND_SETALL, this indicates
    106  * whether or not we've been "ypset".  If we haven't, we behave like
    107  * YPBIND_BROADCAST.  If we have, we behave like YPBIND_DIRECT.
    108  */
    109 int been_ypset;
    110 
    111 #ifdef DEBUG
    112 static int debug;
    113 #endif
    114 
    115 static int insecure;
    116 static int rpcsock, pingsock;
    117 static struct rmtcallargs rmtca;
    118 static struct rmtcallres rmtcr;
    119 static bool_t rmtcr_outval;
    120 static u_long rmtcr_port;
    121 static SVCXPRT *udptransp, *tcptransp;
    122 
    123 int	_yp_invalid_domain(const char *);		/* from libc */
    124 int	main(int, char *[]);
    125 
    126 static void usage(void);
    127 static void yp_log(int, const char *, ...)
    128 	__attribute__((__format__(__printf__, 2, 3)));
    129 static struct _dom_binding *makebinding(const char *);
    130 static int makelock(struct _dom_binding *);
    131 static void removelock(struct _dom_binding *);
    132 static void *ypbindproc_null_2(SVCXPRT *, void *);
    133 static void *ypbindproc_domain_2(SVCXPRT *, void *);
    134 static void *ypbindproc_setdom_2(SVCXPRT *, void *);
    135 static void ypbindprog_2(struct svc_req *, SVCXPRT *);
    136 static void checkwork(void);
    137 static int ping(struct _dom_binding *);
    138 static int nag_servers(struct _dom_binding *);
    139 static enum clnt_stat handle_replies(void);
    140 static enum clnt_stat handle_ping(void);
    141 static void rpc_received(char *, struct sockaddr_in *, int);
    142 static struct _dom_binding *xid2ypdb(u_int32_t);
    143 static u_int32_t unique_xid(struct _dom_binding *);
    144 static int broadcast(char *, int);
    145 static int direct(char *, int);
    146 static int direct_set(char *, int, struct _dom_binding *);
    147 
    148 static void
    149 usage(void)
    150 {
    151 	const char *opt = "";
    152 #ifdef DEBUG
    153 	opt = " [-d]";
    154 #endif
    155 
    156 	(void)fprintf(stderr,
    157 	    "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
    158 	    getprogname(), opt);
    159 	exit(1);
    160 }
    161 
    162 static void
    163 yp_log(int pri, const char *fmt, ...)
    164 {
    165 	va_list ap;
    166 
    167 	va_start(ap, fmt);
    168 
    169 #if defined(DEBUG)
    170 	if (debug)
    171 		(void)vprintf(fmt, ap);
    172 	else
    173 #endif
    174 		vsyslog(pri, fmt, ap);
    175 	va_end(ap);
    176 }
    177 
    178 static struct _dom_binding *
    179 makebinding(const char *dm)
    180 {
    181 	struct _dom_binding *ypdb;
    182 
    183 	if ((ypdb = (struct _dom_binding *)malloc(sizeof *ypdb)) == NULL) {
    184 		yp_log(LOG_ERR, "makebinding");
    185 		exit(1);
    186 	}
    187 
    188 	(void)memset(ypdb, 0, sizeof *ypdb);
    189 	(void)strlcpy(ypdb->dom_domain, dm, sizeof ypdb->dom_domain);
    190 	return ypdb;
    191 }
    192 
    193 static int
    194 makelock(struct _dom_binding *ypdb)
    195 {
    196 	int fd;
    197 	char path[MAXPATHLEN];
    198 
    199 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    200 	    ypdb->dom_domain, ypdb->dom_vers);
    201 
    202 	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    203 		(void)mkdir(BINDINGDIR, 0755);
    204 		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
    205 			return -1;
    206 	}
    207 
    208 #if O_SHLOCK == 0
    209 	(void)flock(fd, LOCK_SH);
    210 #endif
    211 	return fd;
    212 }
    213 
    214 static void
    215 removelock(struct _dom_binding *ypdb)
    216 {
    217 	char path[MAXPATHLEN];
    218 
    219 	(void)snprintf(path, sizeof(path), "%s/%s.%ld",
    220 	    BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
    221 	(void)unlink(path);
    222 }
    223 
    224 static void *
    225 /*ARGSUSED*/
    226 ypbindproc_null_2(SVCXPRT *transp, void *argp)
    227 {
    228 	static char res;
    229 
    230 #ifdef DEBUG
    231 	if (debug)
    232 		(void)printf("ypbindproc_null_2\n");
    233 #endif
    234 	(void)memset(&res, 0, sizeof(res));
    235 	return (void *)&res;
    236 }
    237 
    238 static void *
    239 /*ARGSUSED*/
    240 ypbindproc_domain_2(SVCXPRT *transp, void *argp)
    241 {
    242 	static struct ypbind_resp res;
    243 	struct _dom_binding *ypdb;
    244 	char *arg = *(char **) argp;
    245 	time_t now;
    246 	int count;
    247 
    248 #ifdef DEBUG
    249 	if (debug)
    250 		(void)printf("ypbindproc_domain_2 %s\n", arg);
    251 #endif
    252 	if (_yp_invalid_domain(arg))
    253 		return NULL;
    254 
    255 	(void)memset(&res, 0, sizeof res);
    256 	res.ypbind_status = YPBIND_FAIL_VAL;
    257 
    258 	for (count = 0, ypdb = ypbindlist;
    259 	    ypdb != NULL;
    260 	    ypdb = ypdb->dom_pnext, count++) {
    261 		if (count > 100)
    262 			return NULL;		/* prevent denial of service */
    263 		if (!strcmp(ypdb->dom_domain, arg))
    264 			break;
    265 	}
    266 
    267 	if (ypdb == NULL) {
    268 		ypdb = makebinding(arg);
    269 		ypdb->dom_vers = YPVERS;
    270 		ypdb->dom_alive = 0;
    271 		ypdb->dom_lockfd = -1;
    272 		removelock(ypdb);
    273 		ypdb->dom_xid = unique_xid(ypdb);
    274 		ypdb->dom_pnext = ypbindlist;
    275 		ypbindlist = ypdb;
    276 		check++;
    277 #ifdef DEBUG
    278 		if (debug)
    279 			(void)printf("unknown domain %s\n", arg);
    280 #endif
    281 		return NULL;
    282 	}
    283 
    284 	if (ypdb->dom_alive == 0) {
    285 #ifdef DEBUG
    286 		if (debug)
    287 			(void)printf("dead domain %s\n", arg);
    288 #endif
    289 		return NULL;
    290 	}
    291 
    292 #ifdef HEURISTIC
    293 	(void)time(&now);
    294 	if (now < ypdb->dom_ask_t + 5) {
    295 		/*
    296 		 * Hmm. More than 2 requests in 5 seconds have indicated
    297 		 * that my binding is possibly incorrect.
    298 		 * Ok, do an immediate poll of the server.
    299 		 */
    300 		if (ypdb->dom_check_t >= now) {
    301 			/* don't flood it */
    302 			ypdb->dom_check_t = 0;
    303 			check++;
    304 		}
    305 	}
    306 	ypdb->dom_ask_t = now;
    307 #endif
    308 
    309 	res.ypbind_status = YPBIND_SUCC_VAL;
    310 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    311 		ypdb->dom_server_addr.sin_addr.s_addr;
    312 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    313 		ypdb->dom_server_addr.sin_port;
    314 #ifdef DEBUG
    315 	if (debug)
    316 		(void)printf("domain %s at %s/%d\n", ypdb->dom_domain,
    317 		    inet_ntoa(ypdb->dom_server_addr.sin_addr),
    318 		    ntohs(ypdb->dom_server_addr.sin_port));
    319 #endif
    320 	return &res;
    321 }
    322 
    323 static void *
    324 ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
    325 {
    326 	struct ypbind_setdom *sd = argp;
    327 	struct sockaddr_in *fromsin, bindsin;
    328 	static bool_t res;
    329 
    330 #ifdef DEBUG
    331 	if (debug)
    332 		(void)printf("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr));
    333 #endif
    334 	(void)memset(&res, 0, sizeof(res));
    335 	fromsin = svc_getcaller(transp);
    336 
    337 	switch (ypbindmode) {
    338 	case YPBIND_SETLOCAL:
    339 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
    340 #ifdef DEBUG
    341 			if (debug)
    342 				(void)printf("ypset from %s denied\n",
    343 				    inet_ntoa(fromsin->sin_addr));
    344 #endif
    345 			return NULL;
    346 		}
    347 		/* FALLTHROUGH */
    348 
    349 	case YPBIND_SETALL:
    350 		been_ypset = 1;
    351 		break;
    352 
    353 	case YPBIND_DIRECT:
    354 	case YPBIND_BROADCAST:
    355 	default:
    356 #ifdef DEBUG
    357 		if (debug)
    358 			(void)printf("ypset denied\n");
    359 #endif
    360 		return NULL;
    361 	}
    362 
    363 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
    364 #ifdef DEBUG
    365 		if (debug)
    366 			(void)printf("ypset from unprivileged port denied\n");
    367 #endif
    368 		return &res;
    369 	}
    370 
    371 	if (sd->ypsetdom_vers != YPVERS) {
    372 #ifdef DEBUG
    373 		if (debug)
    374 			(void)printf("ypset with wrong version denied\n");
    375 #endif
    376 		return &res;
    377 	}
    378 
    379 	(void)memset(&bindsin, 0, sizeof bindsin);
    380 	bindsin.sin_family = AF_INET;
    381 	bindsin.sin_len = sizeof(bindsin);
    382 	bindsin.sin_addr = sd->ypsetdom_addr;
    383 	bindsin.sin_port = sd->ypsetdom_port;
    384 	rpc_received(sd->ypsetdom_domain, &bindsin, 1);
    385 
    386 #ifdef DEBUG
    387 	if (debug)
    388 		(void)printf("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr));
    389 #endif
    390 	res = 1;
    391 	return &res;
    392 }
    393 
    394 static void
    395 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
    396 {
    397 	union {
    398 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    399 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    400 		void *alignment;
    401 	} argument;
    402 	struct authunix_parms *creds;
    403 	char *result;
    404 	xdrproc_t xdr_argument, xdr_result;
    405 	void *(*local)(SVCXPRT *, void *);
    406 
    407 	switch (rqstp->rq_proc) {
    408 	case YPBINDPROC_NULL:
    409 		xdr_argument = xdr_void;
    410 		xdr_result = xdr_void;
    411 		local = ypbindproc_null_2;
    412 		break;
    413 
    414 	case YPBINDPROC_DOMAIN:
    415 		xdr_argument = xdr_ypdomain_wrap_string;
    416 		xdr_result = xdr_ypbind_resp;
    417 		local = ypbindproc_domain_2;
    418 		break;
    419 
    420 	case YPBINDPROC_SETDOM:
    421 		switch (rqstp->rq_cred.oa_flavor) {
    422 		case AUTH_UNIX:
    423 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    424 			if (creds->aup_uid != 0) {
    425 				svcerr_auth(transp, AUTH_BADCRED);
    426 				return;
    427 			}
    428 			break;
    429 		default:
    430 			svcerr_auth(transp, AUTH_TOOWEAK);
    431 			return;
    432 		}
    433 
    434 		xdr_argument = xdr_ypbind_setdom;
    435 		xdr_result = xdr_void;
    436 		local = ypbindproc_setdom_2;
    437 		break;
    438 
    439 	default:
    440 		svcerr_noproc(transp);
    441 		return;
    442 	}
    443 	(void)memset(&argument, 0, sizeof(argument));
    444 	if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
    445 		svcerr_decode(transp);
    446 		return;
    447 	}
    448 	result = (*local)(transp, &argument);
    449 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    450 		svcerr_systemerr(transp);
    451 	}
    452 	return;
    453 }
    454 
    455 int
    456 main(int argc, char *argv[])
    457 {
    458 	struct timeval tv;
    459 	fd_set fdsr;
    460 	int width, lockfd;
    461 	int evil = 0, one;
    462 	char pathname[MAXPATHLEN];
    463 	struct stat st;
    464 
    465 	setprogname(argv[0]);
    466 	(void)yp_get_default_domain(&domainname);
    467 	if (domainname[0] == '\0')
    468 		errx(1, "Domainname not set. Aborting.");
    469 
    470 	/*
    471 	 * Per traditional ypbind(8) semantics, if a ypservers
    472 	 * file does not exist, we default to broadcast mode.
    473 	 * If the file does exist, we default to direct mode.
    474 	 * Note that we can still override direct mode by passing
    475 	 * the -broadcast flag.
    476 	 */
    477 	(void)snprintf(pathname, sizeof(pathname), "%s/%s%s", BINDINGDIR,
    478 	    domainname, YPSERVERSSUFF);
    479 	if (stat(pathname, &st) < 0) {
    480 #ifdef DEBUG
    481 		if (debug)
    482 			(void)printf("%s does not exist, defaulting to "
    483 			    "broadcast\n", pathname);
    484 #endif
    485 		ypbindmode = YPBIND_BROADCAST;
    486 	} else
    487 		ypbindmode = YPBIND_DIRECT;
    488 
    489 	while (--argc) {
    490 		++argv;
    491 		if (!strcmp("-insecure", *argv))
    492 			insecure = 1;
    493 		else if (!strcmp("-ypset", *argv))
    494 			ypbindmode = YPBIND_SETALL;
    495 		else if (!strcmp("-ypsetme", *argv))
    496 			ypbindmode = YPBIND_SETLOCAL;
    497 		else if (!strcmp("-broadcast", *argv))
    498 			ypbindmode = YPBIND_BROADCAST;
    499 #ifdef DEBUG
    500 		else if (!strcmp("-d", *argv))
    501 			debug++;
    502 #endif
    503 		else
    504 			usage();
    505 	}
    506 
    507 	/* initialise syslog */
    508 	openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
    509 
    510 	/* blow away everything in BINDINGDIR */
    511 
    512 	lockfd = open(_PATH_YPBIND_LOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644);
    513 	if (lockfd == -1)
    514 		err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
    515 
    516 #if O_SHLOCK == 0
    517 	(void)flock(lockfd, LOCK_SH);
    518 #endif
    519 
    520 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
    521 
    522 	udptransp = svcudp_create(RPC_ANYSOCK);
    523 	if (udptransp == NULL)
    524 		errx(1, "Cannot create udp service.");
    525 
    526 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    527 	    IPPROTO_UDP))
    528 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
    529 
    530 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    531 	if (tcptransp == NULL)
    532 		errx(1, "Cannot create tcp service.");
    533 
    534 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    535 	    IPPROTO_TCP))
    536 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    537 
    538 	/* XXX use SOCK_STREAM for direct queries? */
    539 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    540 		err(1, "rpc socket");
    541 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    542 		err(1, "ping socket");
    543 
    544 	(void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    545 	(void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
    546 
    547 	one = 1;
    548 	(void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
    549 	    (socklen_t)sizeof(one));
    550 	rmtca.prog = YPPROG;
    551 	rmtca.vers = YPVERS;
    552 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    553 	rmtca.xdr_args = NULL;		/* set at call time */
    554 	rmtca.args_ptr = NULL;		/* set at call time */
    555 	rmtcr.port_ptr = &rmtcr_port;
    556 	rmtcr.xdr_results = xdr_bool;
    557 	rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
    558 
    559 	if (_yp_invalid_domain(domainname))
    560 		errx(1, "bad domainname: %s", domainname);
    561 
    562 	/* build initial domain binding, make it "unsuccessful" */
    563 	ypbindlist = makebinding(domainname);
    564 	ypbindlist->dom_vers = YPVERS;
    565 	ypbindlist->dom_alive = 0;
    566 	ypbindlist->dom_lockfd = -1;
    567 	removelock(ypbindlist);
    568 
    569 	checkwork();
    570 
    571 	for (;;) {
    572 		width = svc_maxfd;
    573 		if (rpcsock > width)
    574 			width = rpcsock;
    575 		if (pingsock > width)
    576 			width = pingsock;
    577 		width++;
    578 		fdsr = svc_fdset;
    579 		FD_SET(rpcsock, &fdsr);
    580 		FD_SET(pingsock, &fdsr);
    581 		tv.tv_sec = 1;
    582 		tv.tv_usec = 0;
    583 
    584 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
    585 		case 0:
    586 			checkwork();
    587 			break;
    588 		case -1:
    589 			yp_log(LOG_WARNING, "select: %m");
    590 			break;
    591 		default:
    592 			if (FD_ISSET(rpcsock, &fdsr))
    593 				(void)handle_replies();
    594 			if (FD_ISSET(pingsock, &fdsr))
    595 				(void)handle_ping();
    596 			svc_getreqset(&fdsr);
    597 			if (check)
    598 				checkwork();
    599 			break;
    600 		}
    601 
    602 		if (!evil && ypbindlist->dom_alive) {
    603 			evil = 1;
    604 #ifdef DEBUG
    605 			if (!debug)
    606 #endif
    607 				(void)daemon(0, 0);
    608 			(void)pidfile(NULL);
    609 		}
    610 	}
    611 }
    612 
    613 /*
    614  * State transition is done like this:
    615  *
    616  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
    617  * no binding	timeout		broadcast 		no binding	5 sec
    618  * no binding	answer		--			binding		60 sec
    619  * binding	timeout		ping server		checking	5 sec
    620  * checking	timeout		ping server + broadcast	checking	5 sec
    621  * checking	answer		--			binding		60 sec
    622  */
    623 void
    624 checkwork(void)
    625 {
    626 	struct _dom_binding *ypdb;
    627 	time_t t;
    628 
    629 	check = 0;
    630 
    631 	(void)time(&t);
    632 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
    633 		if (ypdb->dom_check_t < t) {
    634 			if (ypdb->dom_alive == 1)
    635 				(void)ping(ypdb);
    636 			else
    637 				(void)nag_servers(ypdb);
    638 			(void)time(&t);
    639 			ypdb->dom_check_t = t + 5;
    640 		}
    641 	}
    642 }
    643 
    644 int
    645 ping(struct _dom_binding *ypdb)
    646 {
    647 	char *dom = ypdb->dom_domain;
    648 	struct rpc_msg msg;
    649 	char buf[BUFSIZE];
    650 	enum clnt_stat st;
    651 	int outlen;
    652 	AUTH *rpcua;
    653 	XDR xdr;
    654 
    655 	(void)memset(&xdr, 0, sizeof xdr);
    656 	(void)memset(&msg, 0, sizeof msg);
    657 
    658 	rpcua = authunix_create_default();
    659 	if (rpcua == NULL) {
    660 #ifdef DEBUG
    661 		if (debug)
    662 			(void)printf("cannot get unix auth\n");
    663 #endif
    664 		return RPC_SYSTEMERROR;
    665 	}
    666 
    667 	msg.rm_direction = CALL;
    668 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    669 	msg.rm_call.cb_prog = YPPROG;
    670 	msg.rm_call.cb_vers = YPVERS;
    671 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
    672 	msg.rm_call.cb_cred = rpcua->ah_cred;
    673 	msg.rm_call.cb_verf = rpcua->ah_verf;
    674 
    675 	msg.rm_xid = ypdb->dom_xid;
    676 	xdrmem_create(&xdr, buf, (u_int)sizeof(buf), XDR_ENCODE);
    677 	if (!xdr_callmsg(&xdr, &msg)) {
    678 		st = RPC_CANTENCODEARGS;
    679 		AUTH_DESTROY(rpcua);
    680 		return st;
    681 	}
    682 	if (!xdr_ypdomain_wrap_string(&xdr, &dom)) {
    683 		st = RPC_CANTENCODEARGS;
    684 		AUTH_DESTROY(rpcua);
    685 		return st;
    686 	}
    687 	outlen = (int)xdr_getpos(&xdr);
    688 	xdr_destroy(&xdr);
    689 	if (outlen < 1) {
    690 		st = RPC_CANTENCODEARGS;
    691 		AUTH_DESTROY(rpcua);
    692 		return st;
    693 	}
    694 	AUTH_DESTROY(rpcua);
    695 
    696 	ypdb->dom_alive = 2;
    697 #ifdef DEBUG
    698 	if (debug)
    699 		(void)printf("ping %x\n",
    700 		    ypdb->dom_server_addr.sin_addr.s_addr);
    701 #endif
    702 	if (sendto(pingsock, buf, outlen, 0,
    703 	    (struct sockaddr *)(void *)&ypdb->dom_server_addr,
    704 	    (socklen_t)sizeof ypdb->dom_server_addr) == -1)
    705 		yp_log(LOG_WARNING, "ping: sendto: %m");
    706 	return 0;
    707 
    708 }
    709 
    710 static int
    711 nag_servers(struct _dom_binding *ypdb)
    712 {
    713 	char *dom = ypdb->dom_domain;
    714 	struct rpc_msg msg;
    715 	char buf[BUFSIZE];
    716 	enum clnt_stat st;
    717 	int outlen;
    718 	AUTH *rpcua;
    719 	XDR xdr;
    720 
    721 #ifdef DEBUG
    722 	if (debug)
    723 		(void)printf("nag_servers\n");
    724 #endif
    725 	rmtca.xdr_args = xdr_ypdomain_wrap_string;
    726 	rmtca.args_ptr = (caddr_t)(void *)&dom;
    727 
    728 	(void)memset(&xdr, 0, sizeof xdr);
    729 	(void)memset(&msg, 0, sizeof msg);
    730 
    731 	rpcua = authunix_create_default();
    732 	if (rpcua == NULL) {
    733 #ifdef DEBUG
    734 		if (debug)
    735 			(void)printf("cannot get unix auth\n");
    736 #endif
    737 		return RPC_SYSTEMERROR;
    738 	}
    739 	msg.rm_direction = CALL;
    740 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    741 	msg.rm_call.cb_prog = PMAPPROG;
    742 	msg.rm_call.cb_vers = PMAPVERS;
    743 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
    744 	msg.rm_call.cb_cred = rpcua->ah_cred;
    745 	msg.rm_call.cb_verf = rpcua->ah_verf;
    746 
    747 	msg.rm_xid = ypdb->dom_xid;
    748 	xdrmem_create(&xdr, buf, (u_int)sizeof(buf), XDR_ENCODE);
    749 	if (!xdr_callmsg(&xdr, &msg)) {
    750 		st = RPC_CANTENCODEARGS;
    751 		AUTH_DESTROY(rpcua);
    752 		return st;
    753 	}
    754 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
    755 		st = RPC_CANTENCODEARGS;
    756 		AUTH_DESTROY(rpcua);
    757 		return st;
    758 	}
    759 	outlen = (int)xdr_getpos(&xdr);
    760 	xdr_destroy(&xdr);
    761 	if (outlen < 1) {
    762 		st = RPC_CANTENCODEARGS;
    763 		AUTH_DESTROY(rpcua);
    764 		return st;
    765 	}
    766 	AUTH_DESTROY(rpcua);
    767 
    768 	if (ypdb->dom_lockfd != -1) {
    769 		(void)close(ypdb->dom_lockfd);
    770 		ypdb->dom_lockfd = -1;
    771 		removelock(ypdb);
    772 	}
    773 
    774 	if (ypdb->dom_alive == 2) {
    775 		/*
    776 		 * This resolves the following situation:
    777 		 * ypserver on other subnet was once bound,
    778 		 * but rebooted and is now using a different port
    779 		 */
    780 		struct sockaddr_in bindsin;
    781 
    782 		(void)memset(&bindsin, 0, sizeof bindsin);
    783 		bindsin.sin_family = AF_INET;
    784 		bindsin.sin_len = sizeof(bindsin);
    785 		bindsin.sin_port = htons(PMAPPORT);
    786 		bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
    787 
    788 		if (sendto(rpcsock, buf, outlen, 0,
    789 		    (struct sockaddr *)(void *)&bindsin,
    790 		    (socklen_t)sizeof bindsin) == -1)
    791 			yp_log(LOG_WARNING, "nag_servers: sendto: %m");
    792 	}
    793 
    794 	switch (ypbindmode) {
    795 	case YPBIND_SETALL:
    796 	case YPBIND_SETLOCAL:
    797 		if (been_ypset)
    798 			return direct_set(buf, outlen, ypdb);
    799 		/* FALLTHROUGH */
    800 
    801 	case YPBIND_BROADCAST:
    802 		return broadcast(buf, outlen);
    803 
    804 	case YPBIND_DIRECT:
    805 		return direct(buf, outlen);
    806 	}
    807 	/*NOTREACHED*/
    808 	return -1;
    809 }
    810 
    811 static int
    812 broadcast(char *buf, int outlen)
    813 {
    814 	struct ifaddrs *ifap, *ifa;
    815 	struct sockaddr_in bindsin;
    816 	struct in_addr in;
    817 
    818 	(void)memset(&bindsin, 0, sizeof bindsin);
    819 	bindsin.sin_family = AF_INET;
    820 	bindsin.sin_len = sizeof(bindsin);
    821 	bindsin.sin_port = htons(PMAPPORT);
    822 
    823 	if (getifaddrs(&ifap) != 0) {
    824 		yp_log(LOG_WARNING, "broadcast: getifaddrs: %m");
    825 		return (-1);
    826 	}
    827 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    828 		if (ifa->ifa_addr->sa_family != AF_INET)
    829 			continue;
    830 		if ((ifa->ifa_flags & IFF_UP) == 0)
    831 			continue;
    832 
    833 		switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
    834 		case IFF_BROADCAST:
    835 			if (!ifa->ifa_broadaddr)
    836 				continue;
    837 			if (ifa->ifa_broadaddr->sa_family != AF_INET)
    838 				continue;
    839 			in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
    840 			break;
    841 		case IFF_LOOPBACK:
    842 			in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
    843 			break;
    844 		default:
    845 			continue;
    846 		}
    847 
    848 		bindsin.sin_addr = in;
    849 #ifdef DEBUG
    850 		if (debug)
    851 			(void)printf("broadcast %x\n",
    852 			    bindsin.sin_addr.s_addr);
    853 #endif
    854 		if (sendto(rpcsock, buf, outlen, 0,
    855 		    (struct sockaddr *)(void *)&bindsin,
    856 		    (socklen_t)bindsin.sin_len) == -1)
    857 			yp_log(LOG_WARNING, "broadcast: sendto: %m");
    858 	}
    859 	freeifaddrs(ifap);
    860 	return (0);
    861 }
    862 
    863 static int
    864 direct(char *buf, int outlen)
    865 {
    866 	static FILE *df;
    867 	static char ypservers_path[MAXPATHLEN];
    868 	char line[_POSIX2_LINE_MAX];
    869 	char *p;
    870 	struct hostent *hp;
    871 	struct sockaddr_in bindsin;
    872 	int i, count = 0;
    873 
    874 	if (df)
    875 		rewind(df);
    876 	else {
    877 		(void)snprintf(ypservers_path, sizeof(ypservers_path),
    878 		    "%s/%s%s", BINDINGDIR, domainname, YPSERVERSSUFF);
    879 		df = fopen(ypservers_path, "r");
    880 		if (df == NULL) {
    881 			yp_log(LOG_ERR, "%s: ", ypservers_path);
    882 			exit(1);
    883 		}
    884 	}
    885 
    886 	(void)memset(&bindsin, 0, sizeof bindsin);
    887 	bindsin.sin_family = AF_INET;
    888 	bindsin.sin_len = sizeof(bindsin);
    889 	bindsin.sin_port = htons(PMAPPORT);
    890 
    891 	while(fgets(line, (int)sizeof(line), df) != NULL) {
    892 		/* skip lines that are too big */
    893 		p = strchr(line, '\n');
    894 		if (p == NULL) {
    895 			int c;
    896 
    897 			while ((c = getc(df)) != '\n' && c != EOF)
    898 				;
    899 			continue;
    900 		}
    901 		*p = '\0';
    902 		p = line;
    903 		while (isspace((unsigned char)*p))
    904 			p++;
    905 		if (*p == '#')
    906 			continue;
    907 		hp = gethostbyname(p);
    908 		if (!hp) {
    909 			yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
    910 			continue;
    911 		}
    912 		/* step through all addresses in case first is unavailable */
    913 		for (i = 0; hp->h_addr_list[i]; i++) {
    914 			(void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
    915 			    hp->h_length);
    916 			if (sendto(rpcsock, buf, outlen, 0,
    917 			    (struct sockaddr *)(void *)&bindsin,
    918 			    (socklen_t)sizeof(bindsin)) < 0) {
    919 				yp_log(LOG_WARNING, "direct: sendto: %m");
    920 				continue;
    921 			} else
    922 				count++;
    923 		}
    924 	}
    925 	if (!count) {
    926 		yp_log(LOG_WARNING, "no contactable servers found in %s",
    927 		    ypservers_path);
    928 		return -1;
    929 	}
    930 	return 0;
    931 }
    932 
    933 static int
    934 direct_set(char *buf, int outlen, struct _dom_binding *ypdb)
    935 {
    936 	struct sockaddr_in bindsin;
    937 	char path[MAXPATHLEN];
    938 	struct iovec iov[2];
    939 	struct ypbind_resp ybr;
    940 	SVCXPRT dummy_svc;
    941 	int fd;
    942 	ssize_t bytes;
    943 
    944 	/*
    945 	 * Gack, we lose if binding file went away.  We reset
    946 	 * "been_set" if this happens, otherwise we'll never
    947 	 * bind again.
    948 	 */
    949 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    950 	    ypdb->dom_domain, ypdb->dom_vers);
    951 
    952 	if ((fd = open(path, O_SHLOCK|O_RDONLY, 0644)) == -1) {
    953 		yp_log(LOG_WARNING, "%s: %m", path);
    954 		been_ypset = 0;
    955 		return -1;
    956 	}
    957 
    958 #if O_SHLOCK == 0
    959 	(void)flock(fd, LOCK_SH);
    960 #endif
    961 
    962 	/* Read the binding file... */
    963 	iov[0].iov_base = &(dummy_svc.xp_port);
    964 	iov[0].iov_len = sizeof(dummy_svc.xp_port);
    965 	iov[1].iov_base = &ybr;
    966 	iov[1].iov_len = sizeof(ybr);
    967 	bytes = readv(fd, iov, 2);
    968 	(void)close(fd);
    969 	if (bytes != (iov[0].iov_len + iov[1].iov_len)) {
    970 		/* Binding file corrupt? */
    971 		yp_log(LOG_WARNING, "%s: %m", path);
    972 		been_ypset = 0;
    973 		return -1;
    974 	}
    975 
    976 	bindsin.sin_addr =
    977 	    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
    978 
    979 	if (sendto(rpcsock, buf, outlen, 0,
    980 	    (struct sockaddr *)(void *)&bindsin,
    981 	    (socklen_t)sizeof(bindsin)) < 0) {
    982 		yp_log(LOG_WARNING, "direct_set: sendto: %m");
    983 		return -1;
    984 	}
    985 
    986 	return 0;
    987 }
    988 
    989 static enum clnt_stat
    990 handle_replies(void)
    991 {
    992 	char buf[BUFSIZE];
    993 	socklen_t fromlen;
    994 	ssize_t inlen;
    995 	struct _dom_binding *ypdb;
    996 	struct sockaddr_in raddr;
    997 	struct rpc_msg msg;
    998 	XDR xdr;
    999 
   1000 recv_again:
   1001 #ifdef DEBUG
   1002 	if (debug)
   1003 		printf("handle_replies receiving\n");
   1004 #endif
   1005 	(void)memset(&xdr, 0, sizeof(xdr));
   1006 	(void)memset(&msg, 0, sizeof(msg));
   1007 	msg.acpted_rply.ar_verf = _null_auth;
   1008 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
   1009 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
   1010 
   1011 try_again:
   1012 	fromlen = sizeof(struct sockaddr);
   1013 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
   1014 		(struct sockaddr *)(void *)&raddr, &fromlen);
   1015 	if (inlen < 0) {
   1016 		if (errno == EINTR)
   1017 			goto try_again;
   1018 #ifdef DEBUG
   1019 		if (debug)
   1020 			printf("handle_replies: recvfrom failed (%s)\n",
   1021 			    strerror(errno));
   1022 #endif
   1023 		return RPC_CANTRECV;
   1024 	}
   1025 	if (inlen < sizeof(u_int32_t))
   1026 		goto recv_again;
   1027 
   1028 	/*
   1029 	 * see if reply transaction id matches sent id.
   1030 	 * If so, decode the results.
   1031 	 */
   1032 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
   1033 	if (xdr_replymsg(&xdr, &msg)) {
   1034 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
   1035 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
   1036 			raddr.sin_port = htons((u_short)rmtcr_port);
   1037 			ypdb = xid2ypdb(msg.rm_xid);
   1038 			if (ypdb != NULL)
   1039 				rpc_received(ypdb->dom_domain, &raddr, 0);
   1040 		}
   1041 	}
   1042 	xdr.x_op = XDR_FREE;
   1043 	msg.acpted_rply.ar_results.proc = xdr_void;
   1044 	xdr_destroy(&xdr);
   1045 
   1046 	return RPC_SUCCESS;
   1047 }
   1048 
   1049 static enum clnt_stat
   1050 handle_ping(void)
   1051 {
   1052 	char buf[BUFSIZE];
   1053 	socklen_t fromlen;
   1054 	ssize_t inlen;
   1055 	struct _dom_binding *ypdb;
   1056 	struct sockaddr_in raddr;
   1057 	struct rpc_msg msg;
   1058 	XDR xdr;
   1059 	bool_t res;
   1060 
   1061 recv_again:
   1062 #ifdef DEBUG
   1063 	if (debug)
   1064 		printf("handle_ping receiving\n");
   1065 #endif
   1066 	(void)memset(&xdr, 0, sizeof(xdr));
   1067 	(void)memset(&msg, 0, sizeof(msg));
   1068 	msg.acpted_rply.ar_verf = _null_auth;
   1069 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
   1070 	msg.acpted_rply.ar_results.proc = xdr_bool;
   1071 
   1072 try_again:
   1073 	fromlen = sizeof (struct sockaddr);
   1074 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
   1075 	    (struct sockaddr *)(void *)&raddr, &fromlen);
   1076 	if (inlen < 0) {
   1077 		if (errno == EINTR)
   1078 			goto try_again;
   1079 #ifdef DEBUG
   1080 		if (debug)
   1081 			printf("handle_ping: recvfrom failed (%s)\n",
   1082 			    strerror(errno));
   1083 #endif
   1084 		return RPC_CANTRECV;
   1085 	}
   1086 	if (inlen < sizeof(u_int32_t))
   1087 		goto recv_again;
   1088 
   1089 	/*
   1090 	 * see if reply transaction id matches sent id.
   1091 	 * If so, decode the results.
   1092 	 */
   1093 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
   1094 	if (xdr_replymsg(&xdr, &msg)) {
   1095 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
   1096 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
   1097 			ypdb = xid2ypdb(msg.rm_xid);
   1098 			if (ypdb != NULL)
   1099 				rpc_received(ypdb->dom_domain, &raddr, 0);
   1100 		}
   1101 	}
   1102 	xdr.x_op = XDR_FREE;
   1103 	msg.acpted_rply.ar_results.proc = xdr_void;
   1104 	xdr_destroy(&xdr);
   1105 
   1106 	return RPC_SUCCESS;
   1107 }
   1108 
   1109 /*
   1110  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
   1111  */
   1112 void
   1113 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
   1114 {
   1115 	struct _dom_binding *ypdb;
   1116 	struct iovec iov[2];
   1117 	struct ypbind_resp ybr;
   1118 	int fd;
   1119 
   1120 #ifdef DEBUG
   1121 	if (debug)
   1122 		(void)printf("returned from %s about %s\n",
   1123 		    inet_ntoa(raddrp->sin_addr), dom);
   1124 #endif
   1125 
   1126 	if (dom == NULL)
   1127 		return;
   1128 
   1129 	if (_yp_invalid_domain(dom))
   1130 		return;
   1131 
   1132 		/* don't support insecure servers by default */
   1133 	if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
   1134 		return;
   1135 
   1136 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
   1137 		if (!strcmp(ypdb->dom_domain, dom))
   1138 			break;
   1139 
   1140 	if (ypdb == NULL) {
   1141 		if (force == 0)
   1142 			return;
   1143 		ypdb = makebinding(dom);
   1144 		ypdb->dom_lockfd = -1;
   1145 		ypdb->dom_pnext = ypbindlist;
   1146 		ypbindlist = ypdb;
   1147 	}
   1148 
   1149 	/* soft update, alive */
   1150 	if (ypdb->dom_alive == 1 && force == 0) {
   1151 		if (!memcmp(&ypdb->dom_server_addr, raddrp,
   1152 			    sizeof ypdb->dom_server_addr)) {
   1153 			ypdb->dom_alive = 1;
   1154 			/* recheck binding in 60 sec */
   1155 			ypdb->dom_check_t = time(NULL) + 60;
   1156 		}
   1157 		return;
   1158 	}
   1159 
   1160 	(void)memcpy(&ypdb->dom_server_addr, raddrp,
   1161 	    sizeof ypdb->dom_server_addr);
   1162 	/* recheck binding in 60 seconds */
   1163 	ypdb->dom_check_t = time(NULL) + 60;
   1164 	ypdb->dom_vers = YPVERS;
   1165 	ypdb->dom_alive = 1;
   1166 
   1167 	if (ypdb->dom_lockfd != -1)
   1168 		(void)close(ypdb->dom_lockfd);
   1169 
   1170 	if ((fd = makelock(ypdb)) == -1)
   1171 		return;
   1172 
   1173 	/*
   1174 	 * ok, if BINDINGDIR exists, and we can create the binding file,
   1175 	 * then write to it..
   1176 	 */
   1177 	ypdb->dom_lockfd = fd;
   1178 
   1179 	iov[0].iov_base = &(udptransp->xp_port);
   1180 	iov[0].iov_len = sizeof udptransp->xp_port;
   1181 	iov[1].iov_base = &ybr;
   1182 	iov[1].iov_len = sizeof ybr;
   1183 
   1184 	(void)memset(&ybr, 0, sizeof ybr);
   1185 	ybr.ypbind_status = YPBIND_SUCC_VAL;
   1186 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
   1187 	    raddrp->sin_addr;
   1188 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
   1189 	    raddrp->sin_port;
   1190 
   1191 	if (writev(ypdb->dom_lockfd, iov, 2) !=
   1192 	    iov[0].iov_len + iov[1].iov_len) {
   1193 		yp_log(LOG_WARNING, "writev: %m");
   1194 		(void)close(ypdb->dom_lockfd);
   1195 		removelock(ypdb);
   1196 		ypdb->dom_lockfd = -1;
   1197 	}
   1198 }
   1199 
   1200 static struct _dom_binding *
   1201 xid2ypdb(u_int32_t xid)
   1202 {
   1203 	struct _dom_binding *ypdb;
   1204 
   1205 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
   1206 		if (ypdb->dom_xid == xid)
   1207 			break;
   1208 	return (ypdb);
   1209 }
   1210 
   1211 static u_int32_t
   1212 unique_xid(struct _dom_binding *ypdb)
   1213 {
   1214 	u_int32_t tmp_xid;
   1215 
   1216 	tmp_xid = ((u_int32_t)(u_long)ypdb) & 0xffffffff;
   1217 	while (xid2ypdb(tmp_xid) != NULL)
   1218 		tmp_xid++;
   1219 
   1220 	return tmp_xid;
   1221 }
   1222