xref: /netbsd/lib/libc/yp/yplib.c (revision bf9ec67e)
1 /*	$NetBSD: yplib.c,v 1.37 2000/07/06 03:14:05 christos Exp $	 */
2 
3 /*
4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@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 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: yplib.c,v 1.37 2000/07/06 03:14:05 christos Exp $");
37 #endif
38 
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/file.h>
43 #include <sys/uio.h>
44 
45 #include <arpa/nameser.h>
46 
47 #include <assert.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include <rpc/rpc.h>
55 #include <rpc/xdr.h>
56 #include <rpcsvc/yp_prot.h>
57 #include <rpcsvc/ypclnt.h>
58 #include "local.h"
59 
60 #define BINDINGDIR	"/var/yp/binding"
61 #define YPBINDLOCK	"/var/run/ypbind.lock"
62 
63 struct dom_binding *_ypbindlist;
64 char _yp_domain[MAXHOSTNAMELEN];
65 
66 #define YPLIB_TIMEOUT		10
67 #define YPLIB_RPC_RETRIES	4
68 
69 struct timeval _yplib_timeout = { YPLIB_TIMEOUT, 0 };
70 struct timeval _yplib_rpc_timeout = { YPLIB_TIMEOUT / YPLIB_RPC_RETRIES,
71 	1000000 * (YPLIB_TIMEOUT % YPLIB_RPC_RETRIES) / YPLIB_RPC_RETRIES };
72 int _yplib_nerrs = 5;
73 
74 #ifdef __weak_alias
75 __weak_alias(yp_bind, _yp_bind)
76 __weak_alias(yp_unbind, _yp_unbind)
77 __weak_alias(yp_get_default_domain, _yp_get_default_domain)
78 #endif
79 
80 int
81 _yp_dobind(dom, ypdb)
82 	const char     *dom;
83 	struct dom_binding **ypdb;
84 {
85 	static int      pid = -1;
86 	char            path[MAXPATHLEN];
87 	struct dom_binding *ysd, *ysd2;
88 	struct ypbind_resp ypbr;
89 	struct sockaddr_in clnt_sin;
90 	int             clnt_sock, fd, gpid;
91 	CLIENT         *client;
92 	int             new = 0, r;
93 	int             nerrs = 0;
94 
95 	if (dom == NULL || *dom == '\0')
96 		return YPERR_BADARGS;
97 
98 	/*
99 	 * test if YP is running or not
100 	 */
101 	if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
102 		return YPERR_YPBIND;
103 	if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
104 		(void)close(fd);
105 		return YPERR_YPBIND;
106 	}
107 	(void)close(fd);
108 
109 	gpid = getpid();
110 	if (!(pid == -1 || pid == gpid)) {
111 		ysd = _ypbindlist;
112 		while (ysd) {
113 			if (ysd->dom_client)
114 				clnt_destroy(ysd->dom_client);
115 			ysd2 = ysd->dom_pnext;
116 			free(ysd);
117 			ysd = ysd2;
118 		}
119 		_ypbindlist = NULL;
120 	}
121 	pid = gpid;
122 
123 	if (ypdb != NULL)
124 		*ypdb = NULL;
125 
126 	for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
127 		if (strcmp(dom, ysd->dom_domain) == 0)
128 			break;
129 	if (ysd == NULL) {
130 		if ((ysd = malloc(sizeof *ysd)) == NULL)
131 			return YPERR_YPERR;
132 		(void)memset(ysd, 0, sizeof *ysd);
133 		ysd->dom_socket = -1;
134 		ysd->dom_vers = 0;
135 		new = 1;
136 	}
137 again:
138 	if (ysd->dom_vers == 0) {
139 		(void) snprintf(path, sizeof(path), "%s/%s.%d",
140 				BINDINGDIR, dom, 2);
141 		if ((fd = open(path, O_RDONLY)) == -1) {
142 			/*
143 			 * no binding file, YP is dead, or not yet fully
144 			 * alive.
145 			 */
146 			goto trynet;
147 		}
148 		if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
149 		    errno == EWOULDBLOCK) {
150 			struct iovec    iov[2];
151 			struct ypbind_resp ybr;
152 			u_short         ypb_port;
153 			struct ypbind_binding *bn;
154 
155 			iov[0].iov_base = &ypb_port;
156 			iov[0].iov_len = sizeof ypb_port;
157 			iov[1].iov_base = &ybr;
158 			iov[1].iov_len = sizeof ybr;
159 
160 			r = readv(fd, iov, 2);
161 			if (r != iov[0].iov_len + iov[1].iov_len) {
162 				(void)close(fd);
163 				ysd->dom_vers = -1;
164 				goto again;
165 			}
166 			(void)memset(&ysd->dom_server_addr, 0,
167 				     sizeof ysd->dom_server_addr);
168 			ysd->dom_server_addr.sin_len =
169 				sizeof(struct sockaddr_in);
170 			ysd->dom_server_addr.sin_family = AF_INET;
171 			bn = &ybr.ypbind_respbody.ypbind_bindinfo;
172 			ysd->dom_server_addr.sin_port =
173 				bn->ypbind_binding_port;
174 
175 			ysd->dom_server_addr.sin_addr =
176 				bn->ypbind_binding_addr;
177 
178 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
179 			(void)close(fd);
180 			goto gotit;
181 		} else {
182 			/* no lock on binding file, YP is dead. */
183 			(void)close(fd);
184 			if (new)
185 				free(ysd);
186 			return YPERR_YPBIND;
187 		}
188 	}
189 trynet:
190 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
191 		struct ypbind_binding *bn;
192 		(void)memset(&clnt_sin, 0, sizeof clnt_sin);
193 		clnt_sin.sin_len = sizeof(struct sockaddr_in);
194 		clnt_sin.sin_family = AF_INET;
195 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
196 
197 		clnt_sock = RPC_ANYSOCK;
198 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
199 					&clnt_sock, 0, 0);
200 		if (client == NULL) {
201 			clnt_pcreateerror("clnttcp_create");
202 			if (new)
203 				free(ysd);
204 			return YPERR_YPBIND;
205 		}
206 		r = clnt_call(client, (rpcproc_t)YPBINDPROC_DOMAIN,
207 		    (xdrproc_t)xdr_ypdomain_wrap_string, &dom,
208 		    (xdrproc_t)xdr_ypbind_resp, &ypbr, _yplib_timeout);
209 		if (r != RPC_SUCCESS) {
210 			if (new == 0 && ++nerrs == _yplib_nerrs) {
211 				nerrs = 0;
212 				fprintf(stderr,
213 		    "YP server for domain %s not responding, still trying\n",
214 				    dom);
215 			}
216 			clnt_destroy(client);
217 			ysd->dom_vers = -1;
218 			goto again;
219 		}
220 		clnt_destroy(client);
221 
222 		(void)memset(&ysd->dom_server_addr, 0,
223 			     sizeof ysd->dom_server_addr);
224 		ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
225 		ysd->dom_server_addr.sin_family = AF_INET;
226 		bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
227 		ysd->dom_server_addr.sin_port =
228 			bn->ypbind_binding_port;
229 		ysd->dom_server_addr.sin_addr.s_addr =
230 			bn->ypbind_binding_addr.s_addr;
231 		ysd->dom_server_port =
232 			bn->ypbind_binding_port;
233 gotit:
234 		ysd->dom_vers = YPVERS;
235 		(void)strncpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)-1);
236 	}
237 	if (ysd->dom_client)
238 		clnt_destroy(ysd->dom_client);
239 	ysd->dom_socket = RPC_ANYSOCK;
240 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
241 	    YPPROG, YPVERS, _yplib_rpc_timeout, &ysd->dom_socket);
242 	if (ysd->dom_client == NULL) {
243 		clnt_pcreateerror("clntudp_create");
244 		ysd->dom_vers = -1;
245 		goto again;
246 	}
247 	if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
248 		perror("fcntl: F_SETFD");
249 
250 	if (new) {
251 		ysd->dom_pnext = _ypbindlist;
252 		_ypbindlist = ysd;
253 	}
254 	if (ypdb != NULL)
255 		*ypdb = ysd;
256 	return 0;
257 }
258 
259 void
260 __yp_unbind(ypb)
261 	struct dom_binding *ypb;
262 {
263 
264 	_DIAGASSERT(ypb != NULL);
265 
266 	clnt_destroy(ypb->dom_client);
267 	ypb->dom_client = NULL;
268 	ypb->dom_socket = -1;
269 }
270 
271 int
272 yp_bind(dom)
273 	const char     *dom;
274 {
275 	if (_yp_invalid_domain(dom))
276 		return YPERR_BADARGS;
277 
278 	return _yp_dobind(dom, NULL);
279 }
280 
281 void
282 yp_unbind(dom)
283 	const char     *dom;
284 {
285 	struct dom_binding *ypb, *ypbp;
286 
287 	if (_yp_invalid_domain(dom))
288 		return;
289 
290 	ypbp = NULL;
291 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
292 		if (strcmp(dom, ypb->dom_domain) == 0) {
293 			clnt_destroy(ypb->dom_client);
294 			if (ypbp)
295 				ypbp->dom_pnext = ypb->dom_pnext;
296 			else
297 				_ypbindlist = ypb->dom_pnext;
298 			free(ypb);
299 			return;
300 		}
301 		ypbp = ypb;
302 	}
303 	return;
304 }
305 
306 int
307 yp_get_default_domain(domp)
308 	char          **domp;
309 {
310 	if (domp == NULL)
311 		return YPERR_BADARGS;
312 	*domp = NULL;
313 	if (_yp_domain[0] == '\0')
314 		if (getdomainname(_yp_domain, sizeof _yp_domain))
315 			return YPERR_NODOM;
316 	*domp = _yp_domain;
317 	return 0;
318 }
319 
320 int
321 _yp_check(dom)
322 	char          **dom;
323 {
324 	char           *unused;
325 
326 	if (_yp_domain[0] == '\0')
327 		if (yp_get_default_domain(&unused))
328 			return 0;
329 
330 	if (dom)
331 		*dom = _yp_domain;
332 
333 	if (yp_bind(_yp_domain) == 0)
334 		return 1;
335 	return 0;
336 }
337 
338 /*
339  * _yp_invalid_domain: check if given domainname isn't legal.
340  * returns non-zero if invalid
341  */
342 int
343 _yp_invalid_domain(dom)
344 	const char *dom;
345 {
346 	if (dom == NULL || *dom == '\0')
347 		return 1;
348 
349 	if (strlen(dom) > YPMAXDOMAIN)
350 		return 1;
351 
352 	if (strchr(dom, '/') != NULL)
353 		return 1;
354 
355 	return 0;
356 }
357