yplib.c revision 1.21 1 /* $NetBSD: yplib.c,v 1.21 1996/05/15 05:27:53 jtc 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.21 1996/05/15 05:27:53 jtc 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 #define YPMATCHCACHE
56
57 struct dom_binding *_ypbindlist;
58 char _yp_domain[MAXHOSTNAMELEN];
59 int _yplib_timeout = 10;
60
61 static bool_t ypmatch_add __P((const char *, const char *, int, char *, int));
62 static bool_t ypmatch_find __P((const char *, const char *, int, const char **,
63 int *));
64 void _yp_unbind __P((struct dom_binding *));
65
66 int
67 _yp_dobind(dom, ypdb)
68 const char *dom;
69 struct dom_binding **ypdb;
70 {
71 static int pid = -1;
72 char path[MAXPATHLEN];
73 struct dom_binding *ysd, *ysd2;
74 struct ypbind_resp ypbr;
75 struct timeval tv;
76 struct sockaddr_in clnt_sin;
77 int clnt_sock, fd, gpid;
78 CLIENT *client;
79 int new = 0, r;
80 int count = 0;
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 if (dom == NULL || strlen(dom) == 0)
111 return YPERR_BADARGS;
112
113 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
114 if (strcmp(dom, ysd->dom_domain) == 0)
115 break;
116 if (ysd == NULL) {
117 if ((ysd = malloc(sizeof *ysd)) == NULL)
118 return YPERR_YPERR;
119 (void)memset(ysd, 0, sizeof *ysd);
120 ysd->dom_socket = -1;
121 ysd->dom_vers = 0;
122 new = 1;
123 }
124 again:
125 if (ysd->dom_vers == 0) {
126 (void) snprintf(path, sizeof(path), "%s/%s.%d",
127 BINDINGDIR, dom, 2);
128 if ((fd = open(path, O_RDONLY)) == -1) {
129 /*
130 * no binding file, YP is dead, or not yet fully
131 * alive.
132 */
133 goto trynet;
134 }
135 if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
136 errno == EWOULDBLOCK) {
137 struct iovec iov[2];
138 struct ypbind_resp ybr;
139 u_short ypb_port;
140 struct ypbind_binding *bn;
141
142 iov[0].iov_base = (caddr_t) & ypb_port;
143 iov[0].iov_len = sizeof ypb_port;
144 iov[1].iov_base = (caddr_t) & ybr;
145 iov[1].iov_len = sizeof ybr;
146
147 r = readv(fd, iov, 2);
148 if (r != iov[0].iov_len + iov[1].iov_len) {
149 (void)close(fd);
150 ysd->dom_vers = -1;
151 goto again;
152 }
153 (void)memset(&ysd->dom_server_addr, 0,
154 sizeof ysd->dom_server_addr);
155 ysd->dom_server_addr.sin_len =
156 sizeof(struct sockaddr_in);
157 ysd->dom_server_addr.sin_family = AF_INET;
158 bn = &ybr.ypbind_respbody.ypbind_bindinfo;
159 ysd->dom_server_addr.sin_port =
160 bn->ypbind_binding_port;
161
162 ysd->dom_server_addr.sin_addr =
163 bn->ypbind_binding_addr;
164
165 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
166 (void)close(fd);
167 goto gotit;
168 } else {
169 /* no lock on binding file, YP is dead. */
170 (void)close(fd);
171 if (new)
172 free(ysd);
173 return YPERR_YPBIND;
174 }
175 }
176 trynet:
177 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
178 struct ypbind_binding *bn;
179 (void)memset(&clnt_sin, 0, sizeof clnt_sin);
180 clnt_sin.sin_len = sizeof(struct sockaddr_in);
181 clnt_sin.sin_family = AF_INET;
182 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
183
184 clnt_sock = RPC_ANYSOCK;
185 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
186 &clnt_sock, 0, 0);
187 if (client == NULL) {
188 clnt_pcreateerror("clnttcp_create");
189 if (new)
190 free(ysd);
191 return YPERR_YPBIND;
192 }
193 tv.tv_sec = _yplib_timeout;
194 tv.tv_usec = 0;
195 r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname,
196 dom, xdr_ypbind_resp, &ypbr, tv);
197 if (r != RPC_SUCCESS) {
198 if (new == 0 || count)
199 fprintf(stderr,
200 "YP server for domain %s not responding, still trying\n",
201 dom);
202 count++;
203 clnt_destroy(client);
204 ysd->dom_vers = -1;
205 goto again;
206 }
207 clnt_destroy(client);
208
209 (void)memset(&ysd->dom_server_addr, 0,
210 sizeof ysd->dom_server_addr);
211 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
212 ysd->dom_server_addr.sin_family = AF_INET;
213 bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
214 ysd->dom_server_addr.sin_port =
215 bn->ypbind_binding_port;
216 ysd->dom_server_addr.sin_addr.s_addr =
217 bn->ypbind_binding_addr.s_addr;
218 ysd->dom_server_port =
219 bn->ypbind_binding_port;
220 gotit:
221 ysd->dom_vers = YPVERS;
222 (void)strcpy(ysd->dom_domain, dom);
223 }
224 tv.tv_sec = _yplib_timeout / 2;
225 tv.tv_usec = 0;
226 if (ysd->dom_client)
227 clnt_destroy(ysd->dom_client);
228 ysd->dom_socket = RPC_ANYSOCK;
229 ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
230 YPPROG, YPVERS, tv, &ysd->dom_socket);
231 if (ysd->dom_client == NULL) {
232 clnt_pcreateerror("clntudp_create");
233 ysd->dom_vers = -1;
234 goto again;
235 }
236 if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
237 perror("fcntl: F_SETFD");
238
239 if (new) {
240 ysd->dom_pnext = _ypbindlist;
241 _ypbindlist = ysd;
242 }
243 if (ypdb != NULL)
244 *ypdb = ysd;
245 return 0;
246 }
247
248 void
249 _yp_unbind(ypb)
250 struct dom_binding *ypb;
251 {
252 clnt_destroy(ypb->dom_client);
253 ypb->dom_client = NULL;
254 ypb->dom_socket = -1;
255 }
256
257 int
258 yp_bind(dom)
259 const char *dom;
260 {
261 return _yp_dobind(dom, NULL);
262 }
263
264 void
265 yp_unbind(dom)
266 const char *dom;
267 {
268 struct dom_binding *ypb, *ypbp;
269
270 ypbp = NULL;
271 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
272 if (strcmp(dom, ypb->dom_domain) == 0) {
273 clnt_destroy(ypb->dom_client);
274 if (ypbp)
275 ypbp->dom_pnext = ypb->dom_pnext;
276 else
277 _ypbindlist = ypb->dom_pnext;
278 free(ypb);
279 return;
280 }
281 ypbp = ypb;
282 }
283 return;
284 }
285
286 int
287 yp_get_default_domain(domp)
288 char **domp;
289 {
290 *domp = NULL;
291 if (_yp_domain[0] == '\0')
292 if (getdomainname(_yp_domain, sizeof _yp_domain))
293 return YPERR_NODOM;
294 *domp = _yp_domain;
295 return 0;
296 }
297
298 int
299 yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
300 const char *indomain;
301 const char *inmap;
302 char **outkey;
303 int *outkeylen;
304 char **outval;
305 int *outvallen;
306 {
307 struct ypresp_key_val yprkv;
308 struct ypreq_nokey yprnk;
309 struct dom_binding *ysd;
310 struct timeval tv;
311 int r;
312
313 *outkey = *outval = NULL;
314 *outkeylen = *outvallen = 0;
315
316 again:
317 if (_yp_dobind(indomain, &ysd) != 0)
318 return YPERR_DOMAIN;
319
320 tv.tv_sec = _yplib_timeout;
321 tv.tv_usec = 0;
322
323 yprnk.domain = indomain;
324 yprnk.map = inmap;
325 (void)memset(&yprkv, 0, sizeof yprkv);
326
327 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
328 xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
329 if (r != RPC_SUCCESS) {
330 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
331 ysd->dom_vers = -1;
332 goto again;
333 }
334 if (!(r = ypprot_err(yprkv.status))) {
335 *outkeylen = yprkv.keydat.dsize;
336 if ((*outkey = malloc(*outkeylen + 1)) == NULL)
337 r = RPC_SYSTEMERROR;
338 else {
339 (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
340 (*outkey)[*outkeylen] = '\0';
341 }
342 *outvallen = yprkv.valdat.dsize;
343 if ((*outval = malloc(*outvallen + 1)) == NULL)
344 r = RPC_SYSTEMERROR;
345 else {
346 (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
347 (*outval)[*outvallen] = '\0';
348 }
349 }
350 xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
351 _yp_unbind(ysd);
352 return r;
353 }
354
355 int
356 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
357 const char *indomain;
358 const char *inmap;
359 const char *inkey;
360 int inkeylen;
361 char **outkey;
362 int *outkeylen;
363 char **outval;
364 int *outvallen;
365 {
366 struct ypresp_key_val yprkv;
367 struct ypreq_key yprk;
368 struct dom_binding *ysd;
369 struct timeval tv;
370 int r;
371
372 *outkey = *outval = NULL;
373 *outkeylen = *outvallen = 0;
374
375 again:
376 if (_yp_dobind(indomain, &ysd) != 0)
377 return YPERR_DOMAIN;
378
379 tv.tv_sec = _yplib_timeout;
380 tv.tv_usec = 0;
381
382 yprk.domain = indomain;
383 yprk.map = inmap;
384 yprk.keydat.dptr = inkey;
385 yprk.keydat.dsize = inkeylen;
386 (void)memset(&yprkv, 0, sizeof yprkv);
387
388 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
389 xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
390 if (r != RPC_SUCCESS) {
391 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
392 ysd->dom_vers = -1;
393 goto again;
394 }
395 if (!(r = ypprot_err(yprkv.status))) {
396 *outkeylen = yprkv.keydat.dsize;
397 if ((*outkey = malloc(*outkeylen + 1)) == NULL)
398 r = RPC_SYSTEMERROR;
399 else {
400 (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
401 (*outkey)[*outkeylen] = '\0';
402 }
403 *outvallen = yprkv.valdat.dsize;
404 if ((*outval = malloc(*outvallen + 1)) == NULL)
405 r = RPC_SYSTEMERROR;
406 else {
407 (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
408 (*outval)[*outvallen] = '\0';
409 }
410 }
411 xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
412 _yp_unbind(ysd);
413 return r;
414 }
415
416 int
417 yp_maplist(indomain, outmaplist)
418 const char *indomain;
419 struct ypmaplist **outmaplist;
420 {
421 struct dom_binding *ysd;
422 struct ypresp_maplist ypml;
423 struct timeval tv;
424 int r;
425
426 again:
427 if (_yp_dobind(indomain, &ysd) != 0)
428 return YPERR_DOMAIN;
429
430 tv.tv_sec = _yplib_timeout;
431 tv.tv_usec = 0;
432
433 memset(&ypml, 0, sizeof ypml);
434
435 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
436 xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
437 if (r != RPC_SUCCESS) {
438 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
439 ysd->dom_vers = -1;
440 goto again;
441 }
442 *outmaplist = ypml.list;
443 /* NO: xdr_free(xdr_ypresp_maplist, &ypml); */
444 _yp_unbind(ysd);
445 return ypprot_err(ypml.status);
446 }
447
448 int
449 ypprot_err(incode)
450 unsigned int incode;
451 {
452 switch (incode) {
453 case YP_TRUE:
454 return 0;
455 case YP_FALSE:
456 return YPERR_YPBIND;
457 case YP_NOMORE:
458 return YPERR_NOMORE;
459 case YP_NOMAP:
460 return YPERR_MAP;
461 case YP_NODOM:
462 return YPERR_NODOM;
463 case YP_NOKEY:
464 return YPERR_KEY;
465 case YP_BADOP:
466 return YPERR_YPERR;
467 case YP_BADDB:
468 return YPERR_BADDB;
469 case YP_YPERR:
470 return YPERR_YPERR;
471 case YP_BADARGS:
472 return YPERR_BADARGS;
473 case YP_VERS:
474 return YPERR_VERS;
475 }
476 return YPERR_YPERR;
477 }
478
479 int
480 _yp_check(dom)
481 char **dom;
482 {
483 char *unused;
484
485 if (_yp_domain[0] == '\0')
486 if (yp_get_default_domain(&unused))
487 return 0;
488
489 if (dom)
490 *dom = _yp_domain;
491
492 if (yp_bind(_yp_domain) == 0)
493 return 1;
494 return 0;
495 }
496