Home | History | Annotate | Line # | Download | only in yp
yplib.c revision 1.23
      1 /*	$NetBSD: yplib.c,v 1.23 1996/05/23 13:49:04 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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Theo de Raadt.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 static char rcsid[] = "$NetBSD: yplib.c,v 1.23 1996/05/23 13:49:04 christos Exp $";
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 #include <sys/socket.h>
     41 #include <sys/file.h>
     42 #include <sys/uio.h>
     43 #include <errno.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <rpc/rpc.h>
     49 #include <rpc/xdr.h>
     50 #include <rpcsvc/yp_prot.h>
     51 #include <rpcsvc/ypclnt.h>
     52 
     53 #define BINDINGDIR	"/var/yp/binding"
     54 #define YPBINDLOCK	"/var/run/ypbind.lock"
     55 
     56 struct dom_binding *_ypbindlist;
     57 char _yp_domain[MAXHOSTNAMELEN];
     58 
     59 struct timeval _yplib_timeout = { 10, 0 };
     60 int _yplib_nerrs = 5;
     61 
     62 void _yp_unbind __P((struct dom_binding *));
     63 
     64 int
     65 _yp_dobind(dom, ypdb)
     66 	const char     *dom;
     67 	struct dom_binding **ypdb;
     68 {
     69 	static int      pid = -1;
     70 	char            path[MAXPATHLEN];
     71 	struct dom_binding *ysd, *ysd2;
     72 	struct ypbind_resp ypbr;
     73 	struct sockaddr_in clnt_sin;
     74 	int             clnt_sock, fd, gpid;
     75 	CLIENT         *client;
     76 	int             new = 0, r;
     77 	int             count = 0;
     78 
     79 	if (dom == NULL || *dom == 0)
     80 		return YPERR_BADARGS;
     81 
     82 	/*
     83 	 * test if YP is running or not
     84 	 */
     85 	if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
     86 		return YPERR_YPBIND;
     87 	if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
     88 		(void)close(fd);
     89 		return YPERR_YPBIND;
     90 	}
     91 	(void)close(fd);
     92 
     93 	gpid = getpid();
     94 	if (!(pid == -1 || pid == gpid)) {
     95 		ysd = _ypbindlist;
     96 		while (ysd) {
     97 			if (ysd->dom_client)
     98 				clnt_destroy(ysd->dom_client);
     99 			ysd2 = ysd->dom_pnext;
    100 			free(ysd);
    101 			ysd = ysd2;
    102 		}
    103 		_ypbindlist = NULL;
    104 	}
    105 	pid = gpid;
    106 
    107 	if (ypdb != NULL)
    108 		*ypdb = NULL;
    109 
    110 	for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
    111 		if (strcmp(dom, ysd->dom_domain) == 0)
    112 			break;
    113 	if (ysd == NULL) {
    114 		if ((ysd = malloc(sizeof *ysd)) == NULL)
    115 			return YPERR_YPERR;
    116 		(void)memset(ysd, 0, sizeof *ysd);
    117 		ysd->dom_socket = -1;
    118 		ysd->dom_vers = 0;
    119 		new = 1;
    120 	}
    121 again:
    122 	if (ysd->dom_vers == 0) {
    123 		(void) snprintf(path, sizeof(path), "%s/%s.%d",
    124 				BINDINGDIR, dom, 2);
    125 		if ((fd = open(path, O_RDONLY)) == -1) {
    126 			/*
    127 			 * no binding file, YP is dead, or not yet fully
    128 			 * alive.
    129 			 */
    130 			goto trynet;
    131 		}
    132 		if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
    133 		    errno == EWOULDBLOCK) {
    134 			struct iovec    iov[2];
    135 			struct ypbind_resp ybr;
    136 			u_short         ypb_port;
    137 			struct ypbind_binding *bn;
    138 
    139 			iov[0].iov_base = (caddr_t) & ypb_port;
    140 			iov[0].iov_len = sizeof ypb_port;
    141 			iov[1].iov_base = (caddr_t) & ybr;
    142 			iov[1].iov_len = sizeof ybr;
    143 
    144 			r = readv(fd, iov, 2);
    145 			if (r != iov[0].iov_len + iov[1].iov_len) {
    146 				(void)close(fd);
    147 				ysd->dom_vers = -1;
    148 				goto again;
    149 			}
    150 			(void)memset(&ysd->dom_server_addr, 0,
    151 				     sizeof ysd->dom_server_addr);
    152 			ysd->dom_server_addr.sin_len =
    153 				sizeof(struct sockaddr_in);
    154 			ysd->dom_server_addr.sin_family = AF_INET;
    155 			bn = &ybr.ypbind_respbody.ypbind_bindinfo;
    156 			ysd->dom_server_addr.sin_port =
    157 				bn->ypbind_binding_port;
    158 
    159 			ysd->dom_server_addr.sin_addr =
    160 				bn->ypbind_binding_addr;
    161 
    162 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
    163 			(void)close(fd);
    164 			goto gotit;
    165 		} else {
    166 			/* no lock on binding file, YP is dead. */
    167 			(void)close(fd);
    168 			if (new)
    169 				free(ysd);
    170 			return YPERR_YPBIND;
    171 		}
    172 	}
    173 trynet:
    174 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
    175 		struct ypbind_binding *bn;
    176 		(void)memset(&clnt_sin, 0, sizeof clnt_sin);
    177 		clnt_sin.sin_len = sizeof(struct sockaddr_in);
    178 		clnt_sin.sin_family = AF_INET;
    179 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    180 
    181 		clnt_sock = RPC_ANYSOCK;
    182 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
    183 					&clnt_sock, 0, 0);
    184 		if (client == NULL) {
    185 			clnt_pcreateerror("clnttcp_create");
    186 			if (new)
    187 				free(ysd);
    188 			return YPERR_YPBIND;
    189 		}
    190 		r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname,
    191 			      dom, xdr_ypbind_resp, &ypbr, _yplib_timeout);
    192 		if (r != RPC_SUCCESS) {
    193 			if (new == 0 || count)
    194 				fprintf(stderr,
    195 		    "YP server for domain %s not responding, still trying\n",
    196 					dom);
    197 			count++;
    198 			clnt_destroy(client);
    199 			ysd->dom_vers = -1;
    200 			goto again;
    201 		}
    202 		clnt_destroy(client);
    203 
    204 		(void)memset(&ysd->dom_server_addr, 0,
    205 			     sizeof ysd->dom_server_addr);
    206 		ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
    207 		ysd->dom_server_addr.sin_family = AF_INET;
    208 		bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
    209 		ysd->dom_server_addr.sin_port =
    210 			bn->ypbind_binding_port;
    211 		ysd->dom_server_addr.sin_addr.s_addr =
    212 			bn->ypbind_binding_addr.s_addr;
    213 		ysd->dom_server_port =
    214 			bn->ypbind_binding_port;
    215 gotit:
    216 		ysd->dom_vers = YPVERS;
    217 		(void)strcpy(ysd->dom_domain, dom);
    218 	}
    219 	if (ysd->dom_client)
    220 		clnt_destroy(ysd->dom_client);
    221 	ysd->dom_socket = RPC_ANYSOCK;
    222 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
    223 				      YPPROG, YPVERS, _yplib_timeout,
    224 				      &ysd->dom_socket);
    225 	if (ysd->dom_client == NULL) {
    226 		clnt_pcreateerror("clntudp_create");
    227 		ysd->dom_vers = -1;
    228 		goto again;
    229 	}
    230 	if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
    231 		perror("fcntl: F_SETFD");
    232 
    233 	if (new) {
    234 		ysd->dom_pnext = _ypbindlist;
    235 		_ypbindlist = ysd;
    236 	}
    237 	if (ypdb != NULL)
    238 		*ypdb = ysd;
    239 	return 0;
    240 }
    241 
    242 void
    243 _yp_unbind(ypb)
    244 	struct dom_binding *ypb;
    245 {
    246 	clnt_destroy(ypb->dom_client);
    247 	ypb->dom_client = NULL;
    248 	ypb->dom_socket = -1;
    249 }
    250 
    251 int
    252 yp_bind(dom)
    253 	const char     *dom;
    254 {
    255 	return _yp_dobind(dom, NULL);
    256 }
    257 
    258 void
    259 yp_unbind(dom)
    260 	const char     *dom;
    261 {
    262 	struct dom_binding *ypb, *ypbp;
    263 
    264 	ypbp = NULL;
    265 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
    266 		if (strcmp(dom, ypb->dom_domain) == 0) {
    267 			clnt_destroy(ypb->dom_client);
    268 			if (ypbp)
    269 				ypbp->dom_pnext = ypb->dom_pnext;
    270 			else
    271 				_ypbindlist = ypb->dom_pnext;
    272 			free(ypb);
    273 			return;
    274 		}
    275 		ypbp = ypb;
    276 	}
    277 	return;
    278 }
    279 
    280 int
    281 yp_get_default_domain(domp)
    282 	char          **domp;
    283 {
    284 	*domp = NULL;
    285 	if (_yp_domain[0] == '\0')
    286 		if (getdomainname(_yp_domain, sizeof _yp_domain))
    287 			return YPERR_NODOM;
    288 	*domp = _yp_domain;
    289 	return 0;
    290 }
    291 
    292 int
    293 _yp_check(dom)
    294 	char          **dom;
    295 {
    296 	char           *unused;
    297 
    298 	if (_yp_domain[0] == '\0')
    299 		if (yp_get_default_domain(&unused))
    300 			return 0;
    301 
    302 	if (dom)
    303 		*dom = _yp_domain;
    304 
    305 	if (yp_bind(_yp_domain) == 0)
    306 		return 1;
    307 	return 0;
    308 }
    309