xref: /openbsd-src/lib/libc/yp/yp_bind.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: yp_bind.c,v 1.16 2007/09/17 07:07:23 moritz Exp $ */
2 /*
3  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/uio.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <rpc/rpc.h>
39 #include <rpc/xdr.h>
40 #include <rpcsvc/yp.h>
41 #include <rpcsvc/ypclnt.h>
42 #include "ypinternal.h"
43 
44 struct dom_binding *_ypbindlist;
45 char _yp_domain[MAXHOSTNAMELEN];
46 int _yplib_timeout = 10;
47 
48 int
49 _yp_dobind(const char *dom, struct dom_binding **ypdb)
50 {
51 	static pid_t	pid = -1;
52 	char            path[MAXPATHLEN];
53 	struct dom_binding *ysd, *ysd2;
54 	struct ypbind_resp ypbr;
55 	struct timeval  tv;
56 	struct sockaddr_in clnt_sin;
57 	struct ypbind_binding *bn;
58 	int             clnt_sock, fd;
59 	pid_t		gpid;
60 	CLIENT         *client;
61 	int             new = 0, r;
62 	int             count = 0;
63 	u_short		port;
64 
65 	/*
66 	 * test if YP is running or not
67 	 */
68 	if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
69 		return YPERR_YPBIND;
70 	if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
71 		(void)close(fd);
72 		return YPERR_YPBIND;
73 	}
74 	(void)close(fd);
75 
76 	gpid = getpid();
77 	if (!(pid == -1 || pid == gpid)) {
78 		ysd = _ypbindlist;
79 		while (ysd) {
80 			if (ysd->dom_client)
81 				clnt_destroy(ysd->dom_client);
82 			ysd2 = ysd->dom_pnext;
83 			free(ysd);
84 			ysd = ysd2;
85 		}
86 		_ypbindlist = NULL;
87 	}
88 	pid = gpid;
89 
90 	if (ypdb != NULL)
91 		*ypdb = NULL;
92 
93 	if (dom == NULL || strlen(dom) == 0)
94 		return YPERR_BADARGS;
95 
96 	for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
97 		if (strcmp(dom, ysd->dom_domain) == 0)
98 			break;
99 	if (ysd == NULL) {
100 		if ((ysd = malloc(sizeof *ysd)) == NULL)
101 			return YPERR_YPERR;
102 		(void)memset(ysd, 0, sizeof *ysd);
103 		ysd->dom_socket = -1;
104 		ysd->dom_vers = 0;
105 		new = 1;
106 	}
107 again:
108 	if (ysd->dom_vers == 0) {
109 		r = snprintf(path, sizeof(path), "%s/%s.%d",
110 		    BINDINGDIR, dom, 2);
111 		if (r < 0 || r >= sizeof(path)) {
112 			if (new)
113 				free(ysd);
114 			return YPERR_BADARGS;
115 		}
116 		if ((fd = open(path, O_RDONLY)) == -1) {
117 			/*
118 			 * no binding file, YP is dead, or not yet fully
119 			 * alive.
120 			 */
121 			goto trynet;
122 		}
123 		if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
124 		    errno == EWOULDBLOCK) {
125 			struct iovec    iov[2];
126 			u_short         ypb_port;
127 
128 			/*
129 			 * we fetch the ypbind port number, but do
130 			 * nothing with it.
131 			 */
132 			iov[0].iov_base = (caddr_t) &ypb_port;
133 			iov[0].iov_len = sizeof ypb_port;
134 			iov[1].iov_base = (caddr_t) &ypbr;
135 			iov[1].iov_len = sizeof ypbr;
136 
137 			r = readv(fd, iov, 2);
138 			if (r != iov[0].iov_len + iov[1].iov_len) {
139 				(void)close(fd);
140 				ysd->dom_vers = -1;
141 				goto again;
142 			}
143 			(void)close(fd);
144 			goto gotdata;
145 		} else {
146 			/* no lock on binding file, YP is dead. */
147 			(void)close(fd);
148 			if (new)
149 				free(ysd);
150 			return YPERR_YPBIND;
151 		}
152 	}
153 trynet:
154 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
155 		(void)memset(&clnt_sin, 0, sizeof clnt_sin);
156 		clnt_sin.sin_len = sizeof(struct sockaddr_in);
157 		clnt_sin.sin_family = AF_INET;
158 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
159 
160 		clnt_sock = RPC_ANYSOCK;
161 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
162 		    &clnt_sock, 0, 0);
163 		if (client == NULL) {
164 			clnt_pcreateerror("clnttcp_create");
165 			if (new)
166 				free(ysd);
167 			return YPERR_YPBIND;
168 		}
169 		if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED ||
170 		    ntohs(clnt_sin.sin_port) == 20) {
171 			/*
172 			 * YP was not running, but someone has registered
173 			 * ypbind with portmap -- this simply means YP is
174 			 * not running.
175 			 */
176 			clnt_destroy(client);
177 			if (new)
178 				free(ysd);
179 			return YPERR_YPBIND;
180 		}
181 		tv.tv_sec = _yplib_timeout;
182 		tv.tv_usec = 0;
183 		r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname,
184 		    &dom, xdr_ypbind_resp, &ypbr, tv);
185 		if (r != RPC_SUCCESS) {
186 			if (new == 0 || count)
187 				fprintf(stderr,
188 		    "YP server for domain %s not responding, still trying\n",
189 				    dom);
190 			count++;
191 			clnt_destroy(client);
192 			ysd->dom_vers = -1;
193 			goto again;
194 		}
195 		clnt_destroy(client);
196 gotdata:
197 		bn = &ypbr.ypbind_resp_u.ypbind_bindinfo;
198 		memcpy(&port, &bn->ypbind_binding_port, sizeof port);
199 		if (ntohs(port) >= IPPORT_RESERVED ||
200 		    ntohs(port) == 20) {
201 			/*
202 			 * This is bullshit -- the ypbind wants me to
203 			 * communicate to an insecure ypserv.  We are
204 			 * within rights to syslog this as an attack,
205 			 * but for now we'll simply ignore it; real YP
206 			 * is obviously not running.
207 			 */
208 			if (new)
209 				free(ysd);
210 			return YPERR_YPBIND;
211 		}
212 		(void)memset(&ysd->dom_server_addr, 0,
213 		    sizeof ysd->dom_server_addr);
214 		ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
215 		ysd->dom_server_addr.sin_family = AF_INET;
216 		memcpy(&ysd->dom_server_addr.sin_port,
217 		    &bn->ypbind_binding_port,
218 		    sizeof(ysd->dom_server_addr.sin_port));
219 		memcpy(&ysd->dom_server_addr.sin_addr.s_addr,
220 		    &bn->ypbind_binding_addr,
221 		    sizeof(ysd->dom_server_addr.sin_addr.s_addr));
222 		ysd->dom_server_port = ysd->dom_server_addr.sin_port;
223 		ysd->dom_vers = YPVERS;
224 		strlcpy(ysd->dom_domain, dom, sizeof ysd->dom_domain);
225 	}
226 	tv.tv_sec = _yplib_timeout / 2;
227 	tv.tv_usec = 0;
228 	if (ysd->dom_client)
229 		clnt_destroy(ysd->dom_client);
230 	ysd->dom_socket = RPC_ANYSOCK;
231 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
232 	    YPPROG, YPVERS, tv, &ysd->dom_socket);
233 	if (ysd->dom_client == NULL) {
234 		clnt_pcreateerror("clntudp_create");
235 		ysd->dom_vers = -1;
236 		goto again;
237 	}
238 	if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
239 		perror("fcntl: F_SETFD");
240 
241 	if (new) {
242 		ysd->dom_pnext = _ypbindlist;
243 		_ypbindlist = ysd;
244 	}
245 	if (ypdb != NULL)
246 		*ypdb = ysd;
247 	return 0;
248 }
249 
250 void
251 _yp_unbind(struct dom_binding *ypb)
252 {
253 	clnt_destroy(ypb->dom_client);
254 	ypb->dom_client = NULL;
255 	ypb->dom_socket = -1;
256 }
257 
258 int
259 yp_bind(const char *dom)
260 {
261 	return _yp_dobind(dom, NULL);
262 }
263 
264 void
265 yp_unbind(const char *dom)
266 {
267 	struct dom_binding *ypb, *ypbp;
268 
269 	ypbp = NULL;
270 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
271 		if (strcmp(dom, ypb->dom_domain) == 0) {
272 			clnt_destroy(ypb->dom_client);
273 			if (ypbp)
274 				ypbp->dom_pnext = ypb->dom_pnext;
275 			else
276 				_ypbindlist = ypb->dom_pnext;
277 			free(ypb);
278 			return;
279 		}
280 		ypbp = ypb;
281 	}
282 }
283