yplib.c revision 1.20 1 1.20 jtc /* $NetBSD: yplib.c,v 1.20 1996/05/14 23:37:33 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.20 jtc static char rcsid[] = "$NetBSD: yplib.c,v 1.20 1996/05/14 23:37:33 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 #define YPMATCHCACHE
56 1.1 deraadt
57 1.1 deraadt struct dom_binding *_ypbindlist;
58 1.1 deraadt static char _yp_domain[MAXHOSTNAMELEN];
59 1.1 deraadt int _yplib_timeout = 10;
60 1.1 deraadt
61 1.16 christos static bool_t ypmatch_add __P((const char *, const char *, int, char *, int));
62 1.16 christos static bool_t ypmatch_find __P((const char *, const char *, int, const char **,
63 1.16 christos int *));
64 1.20 jtc void _yp_unbind __P((struct dom_binding *));
65 1.16 christos
66 1.1 deraadt #ifdef YPMATCHCACHE
67 1.1 deraadt int _yplib_cache = 5;
68 1.1 deraadt
69 1.1 deraadt static struct ypmatch_ent {
70 1.16 christos struct ypmatch_ent *next;
71 1.16 christos char *map, *key;
72 1.16 christos char *val;
73 1.16 christos int keylen, vallen;
74 1.16 christos time_t expire_t;
75 1.1 deraadt } *ypmc;
76 1.1 deraadt
77 1.16 christos static bool_t
78 1.1 deraadt ypmatch_add(map, key, keylen, val, vallen)
79 1.16 christos const char *map;
80 1.16 christos const char *key;
81 1.16 christos int keylen;
82 1.16 christos char *val;
83 1.16 christos int vallen;
84 1.1 deraadt {
85 1.1 deraadt struct ypmatch_ent *ep;
86 1.1 deraadt time_t t;
87 1.1 deraadt
88 1.16 christos (void)time(&t);
89 1.1 deraadt
90 1.16 christos for (ep = ypmc; ep; ep = ep->next)
91 1.16 christos if (ep->expire_t < t)
92 1.1 deraadt break;
93 1.16 christos if (ep == NULL) {
94 1.16 christos if ((ep = malloc(sizeof *ep)) == NULL)
95 1.16 christos return 0;
96 1.16 christos (void)memset(ep, 0, sizeof *ep);
97 1.16 christos if (ypmc)
98 1.1 deraadt ep->next = ypmc;
99 1.1 deraadt ypmc = ep;
100 1.1 deraadt }
101 1.1 deraadt
102 1.16 christos if (ep->key) {
103 1.1 deraadt free(ep->key);
104 1.16 christos ep->key = NULL;
105 1.16 christos }
106 1.16 christos if (ep->val) {
107 1.1 deraadt free(ep->val);
108 1.16 christos ep->val = NULL;
109 1.16 christos }
110 1.1 deraadt
111 1.16 christos if ((ep->key = malloc(keylen)) == NULL)
112 1.16 christos return 0;
113 1.1 deraadt
114 1.16 christos if ((ep->val = malloc(vallen)) == NULL) {
115 1.1 deraadt free(ep->key);
116 1.1 deraadt ep->key = NULL;
117 1.16 christos return 0;
118 1.1 deraadt }
119 1.16 christos
120 1.1 deraadt ep->keylen = keylen;
121 1.1 deraadt ep->vallen = vallen;
122 1.1 deraadt
123 1.16 christos (void)memcpy(ep->key, key, ep->keylen);
124 1.16 christos (void)memcpy(ep->val, val, ep->vallen);
125 1.1 deraadt
126 1.16 christos if (ep->map) {
127 1.16 christos if (strcmp(ep->map, map)) {
128 1.1 deraadt free(ep->map);
129 1.16 christos if ((ep->map = strdup(map)) == NULL)
130 1.16 christos return 0;
131 1.1 deraadt }
132 1.1 deraadt } else {
133 1.16 christos if ((ep->map = strdup(map)) == NULL)
134 1.16 christos return 0;
135 1.1 deraadt }
136 1.1 deraadt
137 1.1 deraadt ep->expire_t = t + _yplib_cache;
138 1.16 christos return 1;
139 1.1 deraadt }
140 1.1 deraadt
141 1.1 deraadt static bool_t
142 1.1 deraadt ypmatch_find(map, key, keylen, val, vallen)
143 1.16 christos const char *map;
144 1.16 christos const char *key;
145 1.16 christos int keylen;
146 1.16 christos const char **val;
147 1.16 christos int *vallen;
148 1.1 deraadt {
149 1.1 deraadt struct ypmatch_ent *ep;
150 1.16 christos time_t t;
151 1.1 deraadt
152 1.16 christos if (ypmc == NULL)
153 1.1 deraadt return 0;
154 1.1 deraadt
155 1.16 christos (void) time(&t);
156 1.1 deraadt
157 1.16 christos for (ep = ypmc; ep; ep = ep->next) {
158 1.16 christos if (ep->keylen != keylen)
159 1.1 deraadt continue;
160 1.16 christos if (strcmp(ep->map, map))
161 1.1 deraadt continue;
162 1.16 christos if (memcmp(ep->key, key, keylen))
163 1.1 deraadt continue;
164 1.16 christos if (t > ep->expire_t)
165 1.1 deraadt continue;
166 1.1 deraadt
167 1.1 deraadt *val = ep->val;
168 1.1 deraadt *vallen = ep->vallen;
169 1.1 deraadt return 1;
170 1.1 deraadt }
171 1.1 deraadt return 0;
172 1.1 deraadt }
173 1.1 deraadt #endif
174 1.1 deraadt
175 1.1 deraadt int
176 1.1 deraadt _yp_dobind(dom, ypdb)
177 1.16 christos const char *dom;
178 1.16 christos struct dom_binding **ypdb;
179 1.1 deraadt {
180 1.16 christos static int pid = -1;
181 1.16 christos char path[MAXPATHLEN];
182 1.1 deraadt struct dom_binding *ysd, *ysd2;
183 1.1 deraadt struct ypbind_resp ypbr;
184 1.16 christos struct timeval tv;
185 1.1 deraadt struct sockaddr_in clnt_sin;
186 1.16 christos int clnt_sock, fd, gpid;
187 1.16 christos CLIENT *client;
188 1.16 christos int new = 0, r;
189 1.16 christos int count = 0;
190 1.1 deraadt
191 1.13 deraadt /*
192 1.13 deraadt * test if YP is running or not
193 1.13 deraadt */
194 1.16 christos if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
195 1.13 deraadt return YPERR_YPBIND;
196 1.16 christos if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
197 1.16 christos (void)close(fd);
198 1.13 deraadt return YPERR_YPBIND;
199 1.13 deraadt }
200 1.16 christos (void)close(fd);
201 1.13 deraadt
202 1.1 deraadt gpid = getpid();
203 1.16 christos if (!(pid == -1 || pid == gpid)) {
204 1.1 deraadt ysd = _ypbindlist;
205 1.16 christos while (ysd) {
206 1.16 christos if (ysd->dom_client)
207 1.1 deraadt clnt_destroy(ysd->dom_client);
208 1.1 deraadt ysd2 = ysd->dom_pnext;
209 1.1 deraadt free(ysd);
210 1.1 deraadt ysd = ysd2;
211 1.1 deraadt }
212 1.1 deraadt _ypbindlist = NULL;
213 1.1 deraadt }
214 1.1 deraadt pid = gpid;
215 1.1 deraadt
216 1.16 christos if (ypdb != NULL)
217 1.1 deraadt *ypdb = NULL;
218 1.1 deraadt
219 1.16 christos if (dom == NULL || strlen(dom) == 0)
220 1.1 deraadt return YPERR_BADARGS;
221 1.1 deraadt
222 1.16 christos for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
223 1.16 christos if (strcmp(dom, ysd->dom_domain) == 0)
224 1.1 deraadt break;
225 1.16 christos if (ysd == NULL) {
226 1.16 christos if ((ysd = malloc(sizeof *ysd)) == NULL)
227 1.16 christos return YPERR_YPERR;
228 1.16 christos (void)memset(ysd, 0, sizeof *ysd);
229 1.1 deraadt ysd->dom_socket = -1;
230 1.1 deraadt ysd->dom_vers = 0;
231 1.1 deraadt new = 1;
232 1.1 deraadt }
233 1.1 deraadt again:
234 1.16 christos if (ysd->dom_vers == 0) {
235 1.16 christos (void) snprintf(path, sizeof(path), "%s/%s.%d",
236 1.16 christos BINDINGDIR, dom, 2);
237 1.16 christos if ((fd = open(path, O_RDONLY)) == -1) {
238 1.16 christos /*
239 1.16 christos * no binding file, YP is dead, or not yet fully
240 1.16 christos * alive.
241 1.16 christos */
242 1.11 deraadt goto trynet;
243 1.1 deraadt }
244 1.16 christos if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
245 1.16 christos errno == EWOULDBLOCK) {
246 1.16 christos struct iovec iov[2];
247 1.7 deraadt struct ypbind_resp ybr;
248 1.16 christos u_short ypb_port;
249 1.16 christos struct ypbind_binding *bn;
250 1.7 deraadt
251 1.16 christos iov[0].iov_base = (caddr_t) & ypb_port;
252 1.7 deraadt iov[0].iov_len = sizeof ypb_port;
253 1.16 christos iov[1].iov_base = (caddr_t) & ybr;
254 1.7 deraadt iov[1].iov_len = sizeof ybr;
255 1.7 deraadt
256 1.7 deraadt r = readv(fd, iov, 2);
257 1.16 christos if (r != iov[0].iov_len + iov[1].iov_len) {
258 1.16 christos (void)close(fd);
259 1.1 deraadt ysd->dom_vers = -1;
260 1.1 deraadt goto again;
261 1.1 deraadt }
262 1.16 christos (void)memset(&ysd->dom_server_addr, 0,
263 1.16 christos sizeof ysd->dom_server_addr);
264 1.16 christos ysd->dom_server_addr.sin_len =
265 1.16 christos sizeof(struct sockaddr_in);
266 1.7 deraadt ysd->dom_server_addr.sin_family = AF_INET;
267 1.16 christos bn = &ybr.ypbind_respbody.ypbind_bindinfo;
268 1.15 mycroft ysd->dom_server_addr.sin_port =
269 1.16 christos bn->ypbind_binding_port;
270 1.16 christos
271 1.7 deraadt ysd->dom_server_addr.sin_addr =
272 1.16 christos bn->ypbind_binding_addr;
273 1.7 deraadt
274 1.1 deraadt ysd->dom_server_port = ysd->dom_server_addr.sin_port;
275 1.16 christos (void)close(fd);
276 1.1 deraadt goto gotit;
277 1.1 deraadt } else {
278 1.1 deraadt /* no lock on binding file, YP is dead. */
279 1.16 christos (void)close(fd);
280 1.16 christos if (new)
281 1.1 deraadt free(ysd);
282 1.1 deraadt return YPERR_YPBIND;
283 1.1 deraadt }
284 1.1 deraadt }
285 1.11 deraadt trynet:
286 1.16 christos if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
287 1.16 christos struct ypbind_binding *bn;
288 1.16 christos (void)memset(&clnt_sin, 0, sizeof clnt_sin);
289 1.15 mycroft clnt_sin.sin_len = sizeof(struct sockaddr_in);
290 1.1 deraadt clnt_sin.sin_family = AF_INET;
291 1.1 deraadt clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
292 1.1 deraadt
293 1.1 deraadt clnt_sock = RPC_ANYSOCK;
294 1.16 christos client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
295 1.16 christos &clnt_sock, 0, 0);
296 1.16 christos if (client == NULL) {
297 1.1 deraadt clnt_pcreateerror("clnttcp_create");
298 1.16 christos if (new)
299 1.1 deraadt free(ysd);
300 1.1 deraadt return YPERR_YPBIND;
301 1.1 deraadt }
302 1.1 deraadt tv.tv_sec = _yplib_timeout;
303 1.1 deraadt tv.tv_usec = 0;
304 1.16 christos r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname,
305 1.16 christos dom, xdr_ypbind_resp, &ypbr, tv);
306 1.16 christos if (r != RPC_SUCCESS) {
307 1.16 christos if (new == 0 || count)
308 1.12 deraadt fprintf(stderr,
309 1.16 christos "YP server for domain %s not responding, still trying\n",
310 1.16 christos dom);
311 1.12 deraadt count++;
312 1.1 deraadt clnt_destroy(client);
313 1.1 deraadt ysd->dom_vers = -1;
314 1.1 deraadt goto again;
315 1.1 deraadt }
316 1.1 deraadt clnt_destroy(client);
317 1.1 deraadt
318 1.16 christos (void)memset(&ysd->dom_server_addr, 0,
319 1.16 christos sizeof ysd->dom_server_addr);
320 1.15 mycroft ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
321 1.1 deraadt ysd->dom_server_addr.sin_family = AF_INET;
322 1.16 christos bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
323 1.1 deraadt ysd->dom_server_addr.sin_port =
324 1.16 christos bn->ypbind_binding_port;
325 1.1 deraadt ysd->dom_server_addr.sin_addr.s_addr =
326 1.16 christos bn->ypbind_binding_addr.s_addr;
327 1.1 deraadt ysd->dom_server_port =
328 1.16 christos bn->ypbind_binding_port;
329 1.1 deraadt gotit:
330 1.1 deraadt ysd->dom_vers = YPVERS;
331 1.16 christos (void)strcpy(ysd->dom_domain, dom);
332 1.1 deraadt }
333 1.16 christos tv.tv_sec = _yplib_timeout / 2;
334 1.1 deraadt tv.tv_usec = 0;
335 1.16 christos if (ysd->dom_client)
336 1.1 deraadt clnt_destroy(ysd->dom_client);
337 1.1 deraadt ysd->dom_socket = RPC_ANYSOCK;
338 1.1 deraadt ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
339 1.16 christos YPPROG, YPVERS, tv, &ysd->dom_socket);
340 1.16 christos if (ysd->dom_client == NULL) {
341 1.1 deraadt clnt_pcreateerror("clntudp_create");
342 1.1 deraadt ysd->dom_vers = -1;
343 1.1 deraadt goto again;
344 1.1 deraadt }
345 1.16 christos if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
346 1.1 deraadt perror("fcntl: F_SETFD");
347 1.1 deraadt
348 1.16 christos if (new) {
349 1.1 deraadt ysd->dom_pnext = _ypbindlist;
350 1.1 deraadt _ypbindlist = ysd;
351 1.1 deraadt }
352 1.16 christos if (ypdb != NULL)
353 1.1 deraadt *ypdb = ysd;
354 1.1 deraadt return 0;
355 1.1 deraadt }
356 1.1 deraadt
357 1.20 jtc void
358 1.1 deraadt _yp_unbind(ypb)
359 1.16 christos struct dom_binding *ypb;
360 1.1 deraadt {
361 1.1 deraadt clnt_destroy(ypb->dom_client);
362 1.1 deraadt ypb->dom_client = NULL;
363 1.1 deraadt ypb->dom_socket = -1;
364 1.1 deraadt }
365 1.1 deraadt
366 1.1 deraadt int
367 1.1 deraadt yp_bind(dom)
368 1.16 christos const char *dom;
369 1.1 deraadt {
370 1.1 deraadt return _yp_dobind(dom, NULL);
371 1.1 deraadt }
372 1.1 deraadt
373 1.1 deraadt void
374 1.1 deraadt yp_unbind(dom)
375 1.16 christos const char *dom;
376 1.1 deraadt {
377 1.1 deraadt struct dom_binding *ypb, *ypbp;
378 1.1 deraadt
379 1.1 deraadt ypbp = NULL;
380 1.16 christos for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
381 1.16 christos if (strcmp(dom, ypb->dom_domain) == 0) {
382 1.1 deraadt clnt_destroy(ypb->dom_client);
383 1.16 christos if (ypbp)
384 1.1 deraadt ypbp->dom_pnext = ypb->dom_pnext;
385 1.1 deraadt else
386 1.1 deraadt _ypbindlist = ypb->dom_pnext;
387 1.1 deraadt free(ypb);
388 1.1 deraadt return;
389 1.1 deraadt }
390 1.1 deraadt ypbp = ypb;
391 1.1 deraadt }
392 1.1 deraadt return;
393 1.1 deraadt }
394 1.1 deraadt
395 1.1 deraadt int
396 1.1 deraadt yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
397 1.16 christos const char *indomain;
398 1.16 christos const char *inmap;
399 1.16 christos const char *inkey;
400 1.16 christos int inkeylen;
401 1.16 christos char **outval;
402 1.16 christos int *outvallen;
403 1.1 deraadt {
404 1.1 deraadt struct dom_binding *ysd;
405 1.1 deraadt struct ypresp_val yprv;
406 1.16 christos struct timeval tv;
407 1.1 deraadt struct ypreq_key yprk;
408 1.16 christos int r;
409 1.1 deraadt
410 1.1 deraadt *outval = NULL;
411 1.1 deraadt *outvallen = 0;
412 1.1 deraadt
413 1.1 deraadt again:
414 1.16 christos if (_yp_dobind(indomain, &ysd) != 0)
415 1.1 deraadt return YPERR_DOMAIN;
416 1.1 deraadt
417 1.1 deraadt #ifdef YPMATCHCACHE
418 1.16 christos if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
419 1.16 christos inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
420 1.1 deraadt *outvallen = yprv.valdat.dsize;
421 1.16 christos if ((*outval = malloc(*outvallen + 1)) == NULL)
422 1.16 christos return YPERR_YPERR;
423 1.16 christos (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
424 1.1 deraadt (*outval)[*outvallen] = '\0';
425 1.1 deraadt return 0;
426 1.1 deraadt }
427 1.1 deraadt #endif
428 1.1 deraadt
429 1.1 deraadt tv.tv_sec = _yplib_timeout;
430 1.1 deraadt tv.tv_usec = 0;
431 1.1 deraadt
432 1.1 deraadt yprk.domain = indomain;
433 1.1 deraadt yprk.map = inmap;
434 1.16 christos yprk.keydat.dptr = (char *) inkey;
435 1.1 deraadt yprk.keydat.dsize = inkeylen;
436 1.1 deraadt
437 1.9 jtc memset(&yprv, 0, sizeof yprv);
438 1.1 deraadt
439 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_MATCH,
440 1.16 christos xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
441 1.16 christos if (r != RPC_SUCCESS) {
442 1.1 deraadt clnt_perror(ysd->dom_client, "yp_match: clnt_call");
443 1.1 deraadt ysd->dom_vers = -1;
444 1.1 deraadt goto again;
445 1.1 deraadt }
446 1.16 christos if (!(r = ypprot_err(yprv.status))) {
447 1.1 deraadt *outvallen = yprv.valdat.dsize;
448 1.16 christos if ((*outval = malloc(*outvallen + 1)) == NULL)
449 1.16 christos return YPERR_YPERR;
450 1.16 christos (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
451 1.1 deraadt (*outval)[*outvallen] = '\0';
452 1.1 deraadt #ifdef YPMATCHCACHE
453 1.16 christos if (strcmp(_yp_domain, indomain) == 0)
454 1.16 christos if (!ypmatch_add(inmap, inkey, inkeylen,
455 1.16 christos *outval, *outvallen))
456 1.16 christos r = RPC_SYSTEMERROR;
457 1.1 deraadt #endif
458 1.1 deraadt }
459 1.16 christos xdr_free(xdr_ypresp_val, (char *) &yprv);
460 1.1 deraadt _yp_unbind(ysd);
461 1.1 deraadt return r;
462 1.1 deraadt }
463 1.1 deraadt
464 1.1 deraadt int
465 1.1 deraadt yp_get_default_domain(domp)
466 1.16 christos char **domp;
467 1.1 deraadt {
468 1.1 deraadt *domp = NULL;
469 1.16 christos if (_yp_domain[0] == '\0')
470 1.16 christos if (getdomainname(_yp_domain, sizeof _yp_domain))
471 1.1 deraadt return YPERR_NODOM;
472 1.1 deraadt *domp = _yp_domain;
473 1.1 deraadt return 0;
474 1.1 deraadt }
475 1.1 deraadt
476 1.1 deraadt int
477 1.1 deraadt yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
478 1.16 christos const char *indomain;
479 1.16 christos const char *inmap;
480 1.16 christos char **outkey;
481 1.16 christos int *outkeylen;
482 1.16 christos char **outval;
483 1.16 christos int *outvallen;
484 1.1 deraadt {
485 1.1 deraadt struct ypresp_key_val yprkv;
486 1.1 deraadt struct ypreq_nokey yprnk;
487 1.1 deraadt struct dom_binding *ysd;
488 1.16 christos struct timeval tv;
489 1.16 christos int r;
490 1.1 deraadt
491 1.1 deraadt *outkey = *outval = NULL;
492 1.1 deraadt *outkeylen = *outvallen = 0;
493 1.1 deraadt
494 1.1 deraadt again:
495 1.16 christos if (_yp_dobind(indomain, &ysd) != 0)
496 1.1 deraadt return YPERR_DOMAIN;
497 1.1 deraadt
498 1.1 deraadt tv.tv_sec = _yplib_timeout;
499 1.1 deraadt tv.tv_usec = 0;
500 1.1 deraadt
501 1.1 deraadt yprnk.domain = indomain;
502 1.1 deraadt yprnk.map = inmap;
503 1.16 christos (void)memset(&yprkv, 0, sizeof yprkv);
504 1.1 deraadt
505 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_FIRST,
506 1.16 christos xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
507 1.16 christos if (r != RPC_SUCCESS) {
508 1.1 deraadt clnt_perror(ysd->dom_client, "yp_first: clnt_call");
509 1.1 deraadt ysd->dom_vers = -1;
510 1.1 deraadt goto again;
511 1.1 deraadt }
512 1.16 christos if (!(r = ypprot_err(yprkv.status))) {
513 1.1 deraadt *outkeylen = yprkv.keydat.dsize;
514 1.16 christos if ((*outkey = malloc(*outkeylen + 1)) == NULL)
515 1.16 christos r = RPC_SYSTEMERROR;
516 1.16 christos else {
517 1.16 christos (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
518 1.16 christos (*outkey)[*outkeylen] = '\0';
519 1.16 christos }
520 1.1 deraadt *outvallen = yprkv.valdat.dsize;
521 1.16 christos if ((*outval = malloc(*outvallen + 1)) == NULL)
522 1.16 christos r = RPC_SYSTEMERROR;
523 1.16 christos else {
524 1.16 christos (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
525 1.16 christos (*outval)[*outvallen] = '\0';
526 1.16 christos }
527 1.1 deraadt }
528 1.16 christos xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
529 1.1 deraadt _yp_unbind(ysd);
530 1.1 deraadt return r;
531 1.1 deraadt }
532 1.1 deraadt
533 1.1 deraadt int
534 1.1 deraadt yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
535 1.16 christos const char *indomain;
536 1.16 christos const char *inmap;
537 1.16 christos const char *inkey;
538 1.16 christos int inkeylen;
539 1.16 christos char **outkey;
540 1.16 christos int *outkeylen;
541 1.16 christos char **outval;
542 1.16 christos int *outvallen;
543 1.1 deraadt {
544 1.1 deraadt struct ypresp_key_val yprkv;
545 1.1 deraadt struct ypreq_key yprk;
546 1.1 deraadt struct dom_binding *ysd;
547 1.16 christos struct timeval tv;
548 1.16 christos int r;
549 1.1 deraadt
550 1.1 deraadt *outkey = *outval = NULL;
551 1.1 deraadt *outkeylen = *outvallen = 0;
552 1.1 deraadt
553 1.1 deraadt again:
554 1.16 christos if (_yp_dobind(indomain, &ysd) != 0)
555 1.1 deraadt return YPERR_DOMAIN;
556 1.1 deraadt
557 1.1 deraadt tv.tv_sec = _yplib_timeout;
558 1.1 deraadt tv.tv_usec = 0;
559 1.1 deraadt
560 1.1 deraadt yprk.domain = indomain;
561 1.1 deraadt yprk.map = inmap;
562 1.1 deraadt yprk.keydat.dptr = inkey;
563 1.1 deraadt yprk.keydat.dsize = inkeylen;
564 1.16 christos (void)memset(&yprkv, 0, sizeof yprkv);
565 1.1 deraadt
566 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_NEXT,
567 1.16 christos xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
568 1.16 christos if (r != RPC_SUCCESS) {
569 1.1 deraadt clnt_perror(ysd->dom_client, "yp_next: clnt_call");
570 1.1 deraadt ysd->dom_vers = -1;
571 1.1 deraadt goto again;
572 1.1 deraadt }
573 1.16 christos if (!(r = ypprot_err(yprkv.status))) {
574 1.1 deraadt *outkeylen = yprkv.keydat.dsize;
575 1.16 christos if ((*outkey = malloc(*outkeylen + 1)) == NULL)
576 1.16 christos r = RPC_SYSTEMERROR;
577 1.16 christos else {
578 1.16 christos (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
579 1.16 christos (*outkey)[*outkeylen] = '\0';
580 1.16 christos }
581 1.1 deraadt *outvallen = yprkv.valdat.dsize;
582 1.16 christos if ((*outval = malloc(*outvallen + 1)) == NULL)
583 1.16 christos r = RPC_SYSTEMERROR;
584 1.16 christos else {
585 1.16 christos (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
586 1.16 christos (*outval)[*outvallen] = '\0';
587 1.16 christos }
588 1.1 deraadt }
589 1.16 christos xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
590 1.1 deraadt _yp_unbind(ysd);
591 1.1 deraadt return r;
592 1.1 deraadt }
593 1.1 deraadt
594 1.16 christos int
595 1.1 deraadt yp_maplist(indomain, outmaplist)
596 1.16 christos const char *indomain;
597 1.16 christos struct ypmaplist **outmaplist;
598 1.1 deraadt {
599 1.1 deraadt struct dom_binding *ysd;
600 1.1 deraadt struct ypresp_maplist ypml;
601 1.16 christos struct timeval tv;
602 1.16 christos int r;
603 1.1 deraadt
604 1.1 deraadt again:
605 1.16 christos if (_yp_dobind(indomain, &ysd) != 0)
606 1.1 deraadt return YPERR_DOMAIN;
607 1.1 deraadt
608 1.1 deraadt tv.tv_sec = _yplib_timeout;
609 1.1 deraadt tv.tv_usec = 0;
610 1.1 deraadt
611 1.9 jtc memset(&ypml, 0, sizeof ypml);
612 1.1 deraadt
613 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
614 1.16 christos xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
615 1.1 deraadt if (r != RPC_SUCCESS) {
616 1.1 deraadt clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
617 1.1 deraadt ysd->dom_vers = -1;
618 1.1 deraadt goto again;
619 1.1 deraadt }
620 1.1 deraadt *outmaplist = ypml.list;
621 1.16 christos /* NO: xdr_free(xdr_ypresp_maplist, &ypml); */
622 1.1 deraadt _yp_unbind(ysd);
623 1.1 deraadt return ypprot_err(ypml.status);
624 1.1 deraadt }
625 1.1 deraadt
626 1.1 deraadt int
627 1.1 deraadt ypprot_err(incode)
628 1.16 christos unsigned int incode;
629 1.1 deraadt {
630 1.16 christos switch (incode) {
631 1.1 deraadt case YP_TRUE:
632 1.1 deraadt return 0;
633 1.1 deraadt case YP_FALSE:
634 1.1 deraadt return YPERR_YPBIND;
635 1.1 deraadt case YP_NOMORE:
636 1.1 deraadt return YPERR_NOMORE;
637 1.1 deraadt case YP_NOMAP:
638 1.1 deraadt return YPERR_MAP;
639 1.1 deraadt case YP_NODOM:
640 1.1 deraadt return YPERR_NODOM;
641 1.1 deraadt case YP_NOKEY:
642 1.1 deraadt return YPERR_KEY;
643 1.1 deraadt case YP_BADOP:
644 1.1 deraadt return YPERR_YPERR;
645 1.1 deraadt case YP_BADDB:
646 1.1 deraadt return YPERR_BADDB;
647 1.1 deraadt case YP_YPERR:
648 1.1 deraadt return YPERR_YPERR;
649 1.1 deraadt case YP_BADARGS:
650 1.1 deraadt return YPERR_BADARGS;
651 1.1 deraadt case YP_VERS:
652 1.1 deraadt return YPERR_VERS;
653 1.1 deraadt }
654 1.1 deraadt return YPERR_YPERR;
655 1.1 deraadt }
656 1.1 deraadt
657 1.1 deraadt int
658 1.1 deraadt _yp_check(dom)
659 1.16 christos char **dom;
660 1.1 deraadt {
661 1.16 christos char *unused;
662 1.1 deraadt
663 1.16 christos if (_yp_domain[0] == '\0')
664 1.16 christos if (yp_get_default_domain(&unused))
665 1.1 deraadt return 0;
666 1.1 deraadt
667 1.16 christos if (dom)
668 1.1 deraadt *dom = _yp_domain;
669 1.1 deraadt
670 1.16 christos if (yp_bind(_yp_domain) == 0)
671 1.1 deraadt return 1;
672 1.1 deraadt return 0;
673 1.1 deraadt }
674