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