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