yplib.c revision 1.14 1 1.14 cgd /* $NetBSD: yplib.c,v 1.14 1995/02/27 13:00:53 cgd 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.3 deraadt #ifndef LINT
35 1.14 cgd static char *rcsid = "$NetBSD: yplib.c,v 1.14 1995/02/27 13:00:53 cgd 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 extern bool_t xdr_domainname(), xdr_ypbind_resp();
58 1.1 deraadt extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
59 1.1 deraadt extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
60 1.1 deraadt extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
61 1.1 deraadt extern bool_t xdr_ypresp_master();
62 1.1 deraadt
63 1.1 deraadt int (*ypresp_allfn)();
64 1.1 deraadt void *ypresp_data;
65 1.1 deraadt
66 1.1 deraadt struct dom_binding *_ypbindlist;
67 1.1 deraadt static char _yp_domain[MAXHOSTNAMELEN];
68 1.1 deraadt int _yplib_timeout = 10;
69 1.1 deraadt
70 1.1 deraadt #ifdef YPMATCHCACHE
71 1.1 deraadt int _yplib_cache = 5;
72 1.1 deraadt
73 1.1 deraadt static struct ypmatch_ent {
74 1.1 deraadt struct ypmatch_ent *next;
75 1.1 deraadt char *map, *key, *val;
76 1.1 deraadt int keylen, vallen;
77 1.1 deraadt time_t expire_t;
78 1.1 deraadt } *ypmc;
79 1.1 deraadt
80 1.1 deraadt static void
81 1.1 deraadt ypmatch_add(map, key, keylen, val, vallen)
82 1.5 deraadt char *map;
83 1.5 deraadt char *key;
84 1.5 deraadt int keylen;
85 1.5 deraadt char *val;
86 1.5 deraadt int vallen;
87 1.1 deraadt {
88 1.1 deraadt struct ypmatch_ent *ep;
89 1.1 deraadt time_t t;
90 1.1 deraadt
91 1.1 deraadt time(&t);
92 1.1 deraadt
93 1.1 deraadt for(ep=ypmc; ep; ep=ep->next)
94 1.1 deraadt if(ep->expire_t < t)
95 1.1 deraadt break;
96 1.1 deraadt if(ep==NULL) {
97 1.1 deraadt ep = (struct ypmatch_ent *)malloc(sizeof *ep);
98 1.9 jtc memset(ep, 0, sizeof *ep);
99 1.1 deraadt if(ypmc)
100 1.1 deraadt ep->next = ypmc;
101 1.1 deraadt ypmc = ep;
102 1.1 deraadt }
103 1.1 deraadt
104 1.1 deraadt if(ep->key)
105 1.1 deraadt free(ep->key);
106 1.1 deraadt if(ep->val)
107 1.1 deraadt free(ep->val);
108 1.1 deraadt
109 1.1 deraadt ep->key = NULL;
110 1.1 deraadt ep->val = NULL;
111 1.1 deraadt
112 1.1 deraadt ep->key = (char *)malloc(keylen);
113 1.1 deraadt if(ep->key==NULL)
114 1.1 deraadt return;
115 1.1 deraadt
116 1.1 deraadt ep->val = (char *)malloc(vallen);
117 1.1 deraadt if(ep->key==NULL) {
118 1.1 deraadt free(ep->key);
119 1.1 deraadt ep->key = NULL;
120 1.1 deraadt return;
121 1.1 deraadt }
122 1.1 deraadt ep->keylen = keylen;
123 1.1 deraadt ep->vallen = vallen;
124 1.1 deraadt
125 1.9 jtc memcpy(ep->key, key, ep->keylen);
126 1.9 jtc memcpy(ep->val, val, ep->vallen);
127 1.1 deraadt
128 1.1 deraadt if(ep->map) {
129 1.1 deraadt if( strcmp(ep->map, map) ) {
130 1.1 deraadt free(ep->map);
131 1.1 deraadt ep->map = strdup(map);
132 1.1 deraadt }
133 1.1 deraadt } else {
134 1.1 deraadt ep->map = strdup(map);
135 1.1 deraadt }
136 1.1 deraadt
137 1.1 deraadt ep->expire_t = t + _yplib_cache;
138 1.1 deraadt }
139 1.1 deraadt
140 1.1 deraadt static bool_t
141 1.1 deraadt ypmatch_find(map, key, keylen, val, vallen)
142 1.5 deraadt char *map;
143 1.5 deraadt char *key;
144 1.1 deraadt int keylen;
145 1.1 deraadt char **val;
146 1.1 deraadt int *vallen;
147 1.1 deraadt {
148 1.1 deraadt struct ypmatch_ent *ep;
149 1.1 deraadt time_t t;
150 1.1 deraadt
151 1.1 deraadt if(ypmc==NULL)
152 1.1 deraadt return 0;
153 1.1 deraadt
154 1.1 deraadt time(&t);
155 1.1 deraadt
156 1.1 deraadt for(ep=ypmc; ep; ep=ep->next) {
157 1.1 deraadt if(ep->keylen != keylen)
158 1.1 deraadt continue;
159 1.1 deraadt if(strcmp(ep->map, map))
160 1.1 deraadt continue;
161 1.9 jtc if(memcmp(ep->key, key, keylen))
162 1.1 deraadt continue;
163 1.1 deraadt if(t > ep->expire_t)
164 1.1 deraadt continue;
165 1.1 deraadt
166 1.1 deraadt *val = ep->val;
167 1.1 deraadt *vallen = ep->vallen;
168 1.1 deraadt return 1;
169 1.1 deraadt }
170 1.1 deraadt return 0;
171 1.1 deraadt }
172 1.1 deraadt #endif
173 1.1 deraadt
174 1.1 deraadt int
175 1.1 deraadt _yp_dobind(dom, ypdb)
176 1.1 deraadt char *dom;
177 1.1 deraadt struct dom_binding **ypdb;
178 1.1 deraadt {
179 1.1 deraadt static int pid = -1;
180 1.1 deraadt char path[MAXPATHLEN];
181 1.1 deraadt struct dom_binding *ysd, *ysd2;
182 1.1 deraadt struct ypbind_resp ypbr;
183 1.1 deraadt struct timeval tv;
184 1.1 deraadt struct sockaddr_in clnt_sin;
185 1.1 deraadt int clnt_sock, fd, gpid;
186 1.1 deraadt CLIENT *client;
187 1.1 deraadt int new=0, r;
188 1.12 deraadt int count = 0;
189 1.1 deraadt
190 1.13 deraadt /*
191 1.13 deraadt * test if YP is running or not
192 1.13 deraadt */
193 1.13 deraadt if ((fd=open(YPBINDLOCK, O_RDONLY)) == -1)
194 1.13 deraadt return YPERR_YPBIND;
195 1.13 deraadt if( !(flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK)) {
196 1.13 deraadt close(fd);
197 1.13 deraadt return YPERR_YPBIND;
198 1.13 deraadt }
199 1.13 deraadt close(fd);
200 1.13 deraadt
201 1.1 deraadt gpid = getpid();
202 1.1 deraadt if( !(pid==-1 || pid==gpid) ) {
203 1.1 deraadt ysd = _ypbindlist;
204 1.1 deraadt while(ysd) {
205 1.1 deraadt if(ysd->dom_client)
206 1.1 deraadt clnt_destroy(ysd->dom_client);
207 1.1 deraadt ysd2 = ysd->dom_pnext;
208 1.1 deraadt free(ysd);
209 1.1 deraadt ysd = ysd2;
210 1.1 deraadt }
211 1.1 deraadt _ypbindlist = NULL;
212 1.1 deraadt }
213 1.1 deraadt pid = gpid;
214 1.1 deraadt
215 1.1 deraadt if(ypdb!=NULL)
216 1.1 deraadt *ypdb = NULL;
217 1.1 deraadt
218 1.1 deraadt if(dom==NULL || strlen(dom)==0)
219 1.1 deraadt return YPERR_BADARGS;
220 1.1 deraadt
221 1.1 deraadt for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
222 1.1 deraadt if( strcmp(dom, ysd->dom_domain) == 0)
223 1.1 deraadt break;
224 1.1 deraadt if(ysd==NULL) {
225 1.1 deraadt ysd = (struct dom_binding *)malloc(sizeof *ysd);
226 1.9 jtc memset(ysd, 0, sizeof *ysd);
227 1.1 deraadt ysd->dom_socket = -1;
228 1.1 deraadt ysd->dom_vers = 0;
229 1.1 deraadt new = 1;
230 1.1 deraadt }
231 1.1 deraadt again:
232 1.1 deraadt if(ysd->dom_vers==0) {
233 1.1 deraadt sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
234 1.1 deraadt if( (fd=open(path, O_RDONLY)) == -1) {
235 1.11 deraadt /* no binding file, YP is dead, or not yet fully alive. */
236 1.11 deraadt goto trynet;
237 1.1 deraadt }
238 1.1 deraadt if( flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK) {
239 1.7 deraadt struct iovec iov[2];
240 1.7 deraadt struct ypbind_resp ybr;
241 1.7 deraadt u_short ypb_port;
242 1.7 deraadt
243 1.7 deraadt iov[0].iov_base = (caddr_t)&ypb_port;
244 1.7 deraadt iov[0].iov_len = sizeof ypb_port;
245 1.7 deraadt iov[1].iov_base = (caddr_t)&ybr;
246 1.7 deraadt iov[1].iov_len = sizeof ybr;
247 1.7 deraadt
248 1.7 deraadt r = readv(fd, iov, 2);
249 1.7 deraadt if(r != iov[0].iov_len + iov[1].iov_len) {
250 1.1 deraadt close(fd);
251 1.1 deraadt ysd->dom_vers = -1;
252 1.1 deraadt goto again;
253 1.1 deraadt }
254 1.7 deraadt
255 1.9 jtc memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
256 1.7 deraadt ysd->dom_server_addr.sin_family = AF_INET;
257 1.7 deraadt ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
258 1.7 deraadt ysd->dom_server_addr.sin_addr =
259 1.7 deraadt ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
260 1.7 deraadt ysd->dom_server_addr.sin_port =
261 1.7 deraadt ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
262 1.7 deraadt
263 1.1 deraadt ysd->dom_server_port = ysd->dom_server_addr.sin_port;
264 1.1 deraadt close(fd);
265 1.1 deraadt goto gotit;
266 1.1 deraadt } else {
267 1.1 deraadt /* no lock on binding file, YP is dead. */
268 1.1 deraadt close(fd);
269 1.1 deraadt if(new)
270 1.1 deraadt free(ysd);
271 1.1 deraadt return YPERR_YPBIND;
272 1.1 deraadt }
273 1.1 deraadt }
274 1.11 deraadt trynet:
275 1.1 deraadt if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
276 1.9 jtc memset(&clnt_sin, 0, sizeof clnt_sin);
277 1.1 deraadt clnt_sin.sin_family = AF_INET;
278 1.1 deraadt clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
279 1.1 deraadt
280 1.1 deraadt clnt_sock = RPC_ANYSOCK;
281 1.1 deraadt client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
282 1.1 deraadt 0, 0);
283 1.1 deraadt if(client==NULL) {
284 1.1 deraadt clnt_pcreateerror("clnttcp_create");
285 1.1 deraadt if(new)
286 1.1 deraadt free(ysd);
287 1.1 deraadt return YPERR_YPBIND;
288 1.1 deraadt }
289 1.1 deraadt
290 1.1 deraadt tv.tv_sec = _yplib_timeout;
291 1.1 deraadt tv.tv_usec = 0;
292 1.1 deraadt r = clnt_call(client, YPBINDPROC_DOMAIN,
293 1.1 deraadt xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
294 1.1 deraadt if(r != RPC_SUCCESS) {
295 1.12 deraadt if (new==0 || count)
296 1.12 deraadt fprintf(stderr,
297 1.12 deraadt "YP server for domain %s not responding, still trying\n",
298 1.12 deraadt dom);
299 1.12 deraadt count++;
300 1.1 deraadt clnt_destroy(client);
301 1.1 deraadt ysd->dom_vers = -1;
302 1.1 deraadt goto again;
303 1.1 deraadt }
304 1.1 deraadt clnt_destroy(client);
305 1.1 deraadt
306 1.9 jtc memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
307 1.1 deraadt ysd->dom_server_addr.sin_family = AF_INET;
308 1.1 deraadt ysd->dom_server_addr.sin_port =
309 1.1 deraadt ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
310 1.1 deraadt ysd->dom_server_addr.sin_addr.s_addr =
311 1.1 deraadt ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
312 1.1 deraadt ysd->dom_server_port =
313 1.1 deraadt ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
314 1.1 deraadt gotit:
315 1.1 deraadt ysd->dom_vers = YPVERS;
316 1.1 deraadt strcpy(ysd->dom_domain, dom);
317 1.1 deraadt }
318 1.1 deraadt
319 1.1 deraadt tv.tv_sec = _yplib_timeout/2;
320 1.1 deraadt tv.tv_usec = 0;
321 1.1 deraadt if(ysd->dom_client)
322 1.1 deraadt clnt_destroy(ysd->dom_client);
323 1.1 deraadt ysd->dom_socket = RPC_ANYSOCK;
324 1.1 deraadt ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
325 1.1 deraadt YPPROG, YPVERS, tv, &ysd->dom_socket);
326 1.1 deraadt if(ysd->dom_client==NULL) {
327 1.1 deraadt clnt_pcreateerror("clntudp_create");
328 1.1 deraadt ysd->dom_vers = -1;
329 1.1 deraadt goto again;
330 1.1 deraadt }
331 1.1 deraadt if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
332 1.1 deraadt perror("fcntl: F_SETFD");
333 1.1 deraadt
334 1.1 deraadt if(new) {
335 1.1 deraadt ysd->dom_pnext = _ypbindlist;
336 1.1 deraadt _ypbindlist = ysd;
337 1.1 deraadt }
338 1.1 deraadt
339 1.1 deraadt if(ypdb!=NULL)
340 1.1 deraadt *ypdb = ysd;
341 1.1 deraadt return 0;
342 1.1 deraadt }
343 1.1 deraadt
344 1.1 deraadt static void
345 1.1 deraadt _yp_unbind(ypb)
346 1.1 deraadt struct dom_binding *ypb;
347 1.1 deraadt {
348 1.1 deraadt clnt_destroy(ypb->dom_client);
349 1.1 deraadt ypb->dom_client = NULL;
350 1.1 deraadt ypb->dom_socket = -1;
351 1.1 deraadt }
352 1.1 deraadt
353 1.1 deraadt int
354 1.1 deraadt yp_bind(dom)
355 1.1 deraadt char *dom;
356 1.1 deraadt {
357 1.1 deraadt return _yp_dobind(dom, NULL);
358 1.1 deraadt }
359 1.1 deraadt
360 1.1 deraadt void
361 1.1 deraadt yp_unbind(dom)
362 1.1 deraadt char *dom;
363 1.1 deraadt {
364 1.1 deraadt struct dom_binding *ypb, *ypbp;
365 1.1 deraadt
366 1.1 deraadt ypbp = NULL;
367 1.1 deraadt for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
368 1.1 deraadt if( strcmp(dom, ypb->dom_domain) == 0) {
369 1.1 deraadt clnt_destroy(ypb->dom_client);
370 1.1 deraadt if(ypbp)
371 1.1 deraadt ypbp->dom_pnext = ypb->dom_pnext;
372 1.1 deraadt else
373 1.1 deraadt _ypbindlist = ypb->dom_pnext;
374 1.1 deraadt free(ypb);
375 1.1 deraadt return;
376 1.1 deraadt }
377 1.1 deraadt ypbp = ypb;
378 1.1 deraadt }
379 1.1 deraadt return;
380 1.1 deraadt }
381 1.1 deraadt
382 1.1 deraadt int
383 1.1 deraadt yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
384 1.1 deraadt char *indomain;
385 1.1 deraadt char *inmap;
386 1.5 deraadt const char *inkey;
387 1.1 deraadt int inkeylen;
388 1.1 deraadt char **outval;
389 1.1 deraadt int *outvallen;
390 1.1 deraadt {
391 1.1 deraadt struct dom_binding *ysd;
392 1.1 deraadt struct ypresp_val yprv;
393 1.1 deraadt struct timeval tv;
394 1.1 deraadt struct ypreq_key yprk;
395 1.1 deraadt int r;
396 1.1 deraadt
397 1.1 deraadt *outval = NULL;
398 1.1 deraadt *outvallen = 0;
399 1.1 deraadt
400 1.1 deraadt again:
401 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
402 1.1 deraadt return YPERR_DOMAIN;
403 1.1 deraadt
404 1.1 deraadt #ifdef YPMATCHCACHE
405 1.1 deraadt if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
406 1.1 deraadt inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
407 1.1 deraadt *outvallen = yprv.valdat.dsize;
408 1.1 deraadt *outval = (char *)malloc(*outvallen+1);
409 1.9 jtc memcpy(*outval, yprv.valdat.dptr, *outvallen);
410 1.1 deraadt (*outval)[*outvallen] = '\0';
411 1.1 deraadt return 0;
412 1.1 deraadt }
413 1.1 deraadt #endif
414 1.1 deraadt
415 1.1 deraadt tv.tv_sec = _yplib_timeout;
416 1.1 deraadt tv.tv_usec = 0;
417 1.1 deraadt
418 1.1 deraadt yprk.domain = indomain;
419 1.1 deraadt yprk.map = inmap;
420 1.7 deraadt yprk.keydat.dptr = (char *)inkey;
421 1.1 deraadt yprk.keydat.dsize = inkeylen;
422 1.1 deraadt
423 1.9 jtc memset(&yprv, 0, sizeof yprv);
424 1.1 deraadt
425 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_MATCH,
426 1.1 deraadt xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
427 1.1 deraadt if(r != RPC_SUCCESS) {
428 1.1 deraadt clnt_perror(ysd->dom_client, "yp_match: clnt_call");
429 1.1 deraadt ysd->dom_vers = -1;
430 1.1 deraadt goto again;
431 1.1 deraadt }
432 1.1 deraadt if( !(r=ypprot_err(yprv.status)) ) {
433 1.1 deraadt *outvallen = yprv.valdat.dsize;
434 1.1 deraadt *outval = (char *)malloc(*outvallen+1);
435 1.9 jtc memcpy(*outval, yprv.valdat.dptr, *outvallen);
436 1.1 deraadt (*outval)[*outvallen] = '\0';
437 1.1 deraadt #ifdef YPMATCHCACHE
438 1.1 deraadt if( strcmp(_yp_domain, indomain)==0 )
439 1.1 deraadt ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
440 1.1 deraadt #endif
441 1.1 deraadt }
442 1.4 deraadt xdr_free(xdr_ypresp_val, (char *)&yprv);
443 1.1 deraadt _yp_unbind(ysd);
444 1.1 deraadt return r;
445 1.1 deraadt }
446 1.1 deraadt
447 1.1 deraadt int
448 1.1 deraadt yp_get_default_domain(domp)
449 1.1 deraadt char **domp;
450 1.1 deraadt {
451 1.1 deraadt *domp = NULL;
452 1.1 deraadt if(_yp_domain[0] == '\0')
453 1.1 deraadt if( getdomainname(_yp_domain, sizeof _yp_domain))
454 1.1 deraadt return YPERR_NODOM;
455 1.1 deraadt *domp = _yp_domain;
456 1.1 deraadt return 0;
457 1.1 deraadt }
458 1.1 deraadt
459 1.1 deraadt int
460 1.1 deraadt yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
461 1.1 deraadt char *indomain;
462 1.1 deraadt char *inmap;
463 1.1 deraadt char **outkey;
464 1.1 deraadt int *outkeylen;
465 1.1 deraadt char **outval;
466 1.1 deraadt int *outvallen;
467 1.1 deraadt {
468 1.1 deraadt struct ypresp_key_val yprkv;
469 1.1 deraadt struct ypreq_nokey yprnk;
470 1.1 deraadt struct dom_binding *ysd;
471 1.1 deraadt struct timeval tv;
472 1.1 deraadt int r;
473 1.1 deraadt
474 1.1 deraadt *outkey = *outval = NULL;
475 1.1 deraadt *outkeylen = *outvallen = 0;
476 1.1 deraadt
477 1.1 deraadt again:
478 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
479 1.1 deraadt return YPERR_DOMAIN;
480 1.1 deraadt
481 1.1 deraadt tv.tv_sec = _yplib_timeout;
482 1.1 deraadt tv.tv_usec = 0;
483 1.1 deraadt
484 1.1 deraadt yprnk.domain = indomain;
485 1.1 deraadt yprnk.map = inmap;
486 1.9 jtc memset(&yprkv, 0, sizeof yprkv);
487 1.1 deraadt
488 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_FIRST,
489 1.1 deraadt xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
490 1.1 deraadt if(r != RPC_SUCCESS) {
491 1.1 deraadt clnt_perror(ysd->dom_client, "yp_first: clnt_call");
492 1.1 deraadt ysd->dom_vers = -1;
493 1.1 deraadt goto again;
494 1.1 deraadt }
495 1.1 deraadt if( !(r=ypprot_err(yprkv.status)) ) {
496 1.1 deraadt *outkeylen = yprkv.keydat.dsize;
497 1.1 deraadt *outkey = (char *)malloc(*outkeylen+1);
498 1.10 mycroft memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
499 1.1 deraadt (*outkey)[*outkeylen] = '\0';
500 1.1 deraadt *outvallen = yprkv.valdat.dsize;
501 1.1 deraadt *outval = (char *)malloc(*outvallen+1);
502 1.10 mycroft memcpy(*outval, yprkv.valdat.dptr, *outvallen);
503 1.1 deraadt (*outval)[*outvallen] = '\0';
504 1.1 deraadt }
505 1.4 deraadt xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
506 1.1 deraadt _yp_unbind(ysd);
507 1.1 deraadt return r;
508 1.1 deraadt }
509 1.1 deraadt
510 1.1 deraadt int
511 1.1 deraadt yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
512 1.1 deraadt char *indomain;
513 1.1 deraadt char *inmap;
514 1.1 deraadt char *inkey;
515 1.1 deraadt int inkeylen;
516 1.1 deraadt char **outkey;
517 1.1 deraadt int *outkeylen;
518 1.1 deraadt char **outval;
519 1.1 deraadt int *outvallen;
520 1.1 deraadt {
521 1.1 deraadt struct ypresp_key_val yprkv;
522 1.1 deraadt struct ypreq_key yprk;
523 1.1 deraadt struct dom_binding *ysd;
524 1.1 deraadt struct timeval tv;
525 1.1 deraadt int r;
526 1.1 deraadt
527 1.1 deraadt *outkey = *outval = NULL;
528 1.1 deraadt *outkeylen = *outvallen = 0;
529 1.1 deraadt
530 1.1 deraadt again:
531 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
532 1.1 deraadt return YPERR_DOMAIN;
533 1.1 deraadt
534 1.1 deraadt tv.tv_sec = _yplib_timeout;
535 1.1 deraadt tv.tv_usec = 0;
536 1.1 deraadt
537 1.1 deraadt yprk.domain = indomain;
538 1.1 deraadt yprk.map = inmap;
539 1.1 deraadt yprk.keydat.dptr = inkey;
540 1.1 deraadt yprk.keydat.dsize = inkeylen;
541 1.9 jtc memset(&yprkv, 0, sizeof yprkv);
542 1.1 deraadt
543 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_NEXT,
544 1.1 deraadt xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
545 1.1 deraadt if(r != RPC_SUCCESS) {
546 1.1 deraadt clnt_perror(ysd->dom_client, "yp_next: clnt_call");
547 1.1 deraadt ysd->dom_vers = -1;
548 1.1 deraadt goto again;
549 1.1 deraadt }
550 1.1 deraadt if( !(r=ypprot_err(yprkv.status)) ) {
551 1.1 deraadt *outkeylen = yprkv.keydat.dsize;
552 1.1 deraadt *outkey = (char *)malloc(*outkeylen+1);
553 1.9 jtc memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
554 1.1 deraadt (*outkey)[*outkeylen] = '\0';
555 1.1 deraadt *outvallen = yprkv.valdat.dsize;
556 1.1 deraadt *outval = (char *)malloc(*outvallen+1);
557 1.9 jtc memcpy(*outval, yprkv.valdat.dptr, *outvallen);
558 1.1 deraadt (*outval)[*outvallen] = '\0';
559 1.1 deraadt }
560 1.4 deraadt xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
561 1.1 deraadt _yp_unbind(ysd);
562 1.1 deraadt return r;
563 1.1 deraadt }
564 1.1 deraadt
565 1.1 deraadt int
566 1.1 deraadt yp_all(indomain, inmap, incallback)
567 1.1 deraadt char *indomain;
568 1.1 deraadt char *inmap;
569 1.1 deraadt struct ypall_callback *incallback;
570 1.1 deraadt {
571 1.1 deraadt struct ypreq_nokey yprnk;
572 1.1 deraadt struct dom_binding *ysd;
573 1.1 deraadt struct timeval tv;
574 1.1 deraadt struct sockaddr_in clnt_sin;
575 1.1 deraadt CLIENT *clnt;
576 1.1 deraadt u_long status;
577 1.1 deraadt int clnt_sock;
578 1.1 deraadt
579 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
580 1.1 deraadt return YPERR_DOMAIN;
581 1.1 deraadt
582 1.1 deraadt tv.tv_sec = _yplib_timeout;
583 1.1 deraadt tv.tv_usec = 0;
584 1.1 deraadt clnt_sock = RPC_ANYSOCK;
585 1.1 deraadt clnt_sin = ysd->dom_server_addr;
586 1.1 deraadt clnt_sin.sin_port = 0;
587 1.1 deraadt clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
588 1.1 deraadt if(clnt==NULL) {
589 1.1 deraadt printf("clnttcp_create failed\n");
590 1.1 deraadt return YPERR_PMAP;
591 1.1 deraadt }
592 1.1 deraadt
593 1.1 deraadt yprnk.domain = indomain;
594 1.1 deraadt yprnk.map = inmap;
595 1.1 deraadt ypresp_allfn = incallback->foreach;
596 1.1 deraadt ypresp_data = (void *)incallback->data;
597 1.1 deraadt
598 1.1 deraadt (void) clnt_call(clnt, YPPROC_ALL,
599 1.1 deraadt xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
600 1.1 deraadt clnt_destroy(clnt);
601 1.4 deraadt xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */
602 1.1 deraadt _yp_unbind(ysd);
603 1.1 deraadt
604 1.1 deraadt if(status != YP_FALSE)
605 1.1 deraadt return ypprot_err(status);
606 1.1 deraadt return 0;
607 1.1 deraadt }
608 1.1 deraadt
609 1.1 deraadt int
610 1.1 deraadt yp_order(indomain, inmap, outorder)
611 1.1 deraadt char *indomain;
612 1.1 deraadt char *inmap;
613 1.1 deraadt int *outorder;
614 1.1 deraadt {
615 1.1 deraadt struct dom_binding *ysd;
616 1.1 deraadt struct ypresp_order ypro;
617 1.1 deraadt struct ypreq_nokey yprnk;
618 1.1 deraadt struct timeval tv;
619 1.1 deraadt int r;
620 1.1 deraadt
621 1.1 deraadt again:
622 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
623 1.1 deraadt return YPERR_DOMAIN;
624 1.1 deraadt
625 1.1 deraadt tv.tv_sec = _yplib_timeout;
626 1.1 deraadt tv.tv_usec = 0;
627 1.1 deraadt
628 1.1 deraadt yprnk.domain = indomain;
629 1.1 deraadt yprnk.map = inmap;
630 1.1 deraadt
631 1.9 jtc memset(&ypro, 0, sizeof ypro);
632 1.1 deraadt
633 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_ORDER,
634 1.1 deraadt xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
635 1.1 deraadt if(r != RPC_SUCCESS) {
636 1.1 deraadt clnt_perror(ysd->dom_client, "yp_order: clnt_call");
637 1.1 deraadt ysd->dom_vers = -1;
638 1.1 deraadt goto again;
639 1.1 deraadt }
640 1.1 deraadt
641 1.1 deraadt *outorder = ypro.ordernum;
642 1.4 deraadt xdr_free(xdr_ypresp_order, (char *)&ypro);
643 1.1 deraadt _yp_unbind(ysd);
644 1.1 deraadt return ypprot_err(ypro.status);
645 1.1 deraadt }
646 1.1 deraadt
647 1.5 deraadt int
648 1.1 deraadt yp_master(indomain, inmap, outname)
649 1.1 deraadt char *indomain;
650 1.1 deraadt char *inmap;
651 1.1 deraadt char **outname;
652 1.1 deraadt {
653 1.1 deraadt struct dom_binding *ysd;
654 1.1 deraadt struct ypresp_master yprm;
655 1.1 deraadt struct ypreq_nokey yprnk;
656 1.1 deraadt struct timeval tv;
657 1.1 deraadt int r;
658 1.1 deraadt
659 1.1 deraadt again:
660 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
661 1.1 deraadt return YPERR_DOMAIN;
662 1.1 deraadt
663 1.1 deraadt tv.tv_sec = _yplib_timeout;
664 1.1 deraadt tv.tv_usec = 0;
665 1.1 deraadt
666 1.1 deraadt yprnk.domain = indomain;
667 1.1 deraadt yprnk.map = inmap;
668 1.1 deraadt
669 1.9 jtc memset(&yprm, 0, sizeof yprm);
670 1.1 deraadt
671 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_MASTER,
672 1.1 deraadt xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
673 1.1 deraadt if(r != RPC_SUCCESS) {
674 1.1 deraadt clnt_perror(ysd->dom_client, "yp_master: clnt_call");
675 1.1 deraadt ysd->dom_vers = -1;
676 1.1 deraadt goto again;
677 1.1 deraadt }
678 1.1 deraadt if( !(r=ypprot_err(yprm.status)) ) {
679 1.1 deraadt *outname = (char *)strdup(yprm.master);
680 1.1 deraadt }
681 1.4 deraadt xdr_free(xdr_ypresp_master, (char *)&yprm);
682 1.1 deraadt _yp_unbind(ysd);
683 1.1 deraadt return r;
684 1.1 deraadt }
685 1.1 deraadt
686 1.1 deraadt yp_maplist(indomain, outmaplist)
687 1.1 deraadt char *indomain;
688 1.1 deraadt struct ypmaplist **outmaplist;
689 1.1 deraadt {
690 1.1 deraadt struct dom_binding *ysd;
691 1.1 deraadt struct ypresp_maplist ypml;
692 1.1 deraadt struct timeval tv;
693 1.1 deraadt int r;
694 1.1 deraadt
695 1.1 deraadt again:
696 1.1 deraadt if( _yp_dobind(indomain, &ysd) != 0)
697 1.1 deraadt return YPERR_DOMAIN;
698 1.1 deraadt
699 1.1 deraadt tv.tv_sec = _yplib_timeout;
700 1.1 deraadt tv.tv_usec = 0;
701 1.1 deraadt
702 1.9 jtc memset(&ypml, 0, sizeof ypml);
703 1.1 deraadt
704 1.1 deraadt r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
705 1.1 deraadt xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
706 1.1 deraadt if (r != RPC_SUCCESS) {
707 1.1 deraadt clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
708 1.1 deraadt ysd->dom_vers = -1;
709 1.1 deraadt goto again;
710 1.1 deraadt }
711 1.1 deraadt *outmaplist = ypml.list;
712 1.1 deraadt /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
713 1.1 deraadt _yp_unbind(ysd);
714 1.1 deraadt return ypprot_err(ypml.status);
715 1.1 deraadt }
716 1.1 deraadt
717 1.1 deraadt char *
718 1.1 deraadt yperr_string(incode)
719 1.1 deraadt int incode;
720 1.1 deraadt {
721 1.1 deraadt static char err[80];
722 1.1 deraadt
723 1.1 deraadt switch(incode) {
724 1.1 deraadt case 0:
725 1.1 deraadt return "Success";
726 1.1 deraadt case YPERR_BADARGS:
727 1.1 deraadt return "Request arguments bad";
728 1.1 deraadt case YPERR_RPC:
729 1.1 deraadt return "RPC failure";
730 1.1 deraadt case YPERR_DOMAIN:
731 1.1 deraadt return "Can't bind to server which serves this domain";
732 1.1 deraadt case YPERR_MAP:
733 1.1 deraadt return "No such map in server's domain";
734 1.1 deraadt case YPERR_KEY:
735 1.1 deraadt return "No such key in map";
736 1.1 deraadt case YPERR_YPERR:
737 1.1 deraadt return "YP server error";
738 1.1 deraadt case YPERR_RESRC:
739 1.1 deraadt return "Local resource allocation failure";
740 1.1 deraadt case YPERR_NOMORE:
741 1.1 deraadt return "No more records in map database";
742 1.1 deraadt case YPERR_PMAP:
743 1.1 deraadt return "Can't communicate with portmapper";
744 1.1 deraadt case YPERR_YPBIND:
745 1.1 deraadt return "Can't communicate with ypbind";
746 1.1 deraadt case YPERR_YPSERV:
747 1.1 deraadt return "Can't communicate with ypserv";
748 1.1 deraadt case YPERR_NODOM:
749 1.1 deraadt return "Local domain name not set";
750 1.1 deraadt case YPERR_BADDB:
751 1.1 deraadt return "Server data base is bad";
752 1.1 deraadt case YPERR_VERS:
753 1.1 deraadt return "YP server version mismatch - server can't supply service.";
754 1.1 deraadt case YPERR_ACCESS:
755 1.1 deraadt return "Access violation";
756 1.1 deraadt case YPERR_BUSY:
757 1.1 deraadt return "Database is busy";
758 1.1 deraadt }
759 1.1 deraadt sprintf(err, "YP unknown error %d\n", incode);
760 1.1 deraadt return err;
761 1.1 deraadt }
762 1.1 deraadt
763 1.1 deraadt int
764 1.1 deraadt ypprot_err(incode)
765 1.1 deraadt unsigned int incode;
766 1.1 deraadt {
767 1.1 deraadt switch(incode) {
768 1.1 deraadt case YP_TRUE:
769 1.1 deraadt return 0;
770 1.1 deraadt case YP_FALSE:
771 1.1 deraadt return YPERR_YPBIND;
772 1.1 deraadt case YP_NOMORE:
773 1.1 deraadt return YPERR_NOMORE;
774 1.1 deraadt case YP_NOMAP:
775 1.1 deraadt return YPERR_MAP;
776 1.1 deraadt case YP_NODOM:
777 1.1 deraadt return YPERR_NODOM;
778 1.1 deraadt case YP_NOKEY:
779 1.1 deraadt return YPERR_KEY;
780 1.1 deraadt case YP_BADOP:
781 1.1 deraadt return YPERR_YPERR;
782 1.1 deraadt case YP_BADDB:
783 1.1 deraadt return YPERR_BADDB;
784 1.1 deraadt case YP_YPERR:
785 1.1 deraadt return YPERR_YPERR;
786 1.1 deraadt case YP_BADARGS:
787 1.1 deraadt return YPERR_BADARGS;
788 1.1 deraadt case YP_VERS:
789 1.1 deraadt return YPERR_VERS;
790 1.1 deraadt }
791 1.1 deraadt return YPERR_YPERR;
792 1.1 deraadt }
793 1.1 deraadt
794 1.1 deraadt int
795 1.1 deraadt _yp_check(dom)
796 1.1 deraadt char **dom;
797 1.1 deraadt {
798 1.1 deraadt int use_yp = 0;
799 1.1 deraadt char *unused;
800 1.1 deraadt
801 1.1 deraadt if( _yp_domain[0]=='\0' )
802 1.1 deraadt if( yp_get_default_domain(&unused) )
803 1.1 deraadt return 0;
804 1.1 deraadt
805 1.1 deraadt if(dom)
806 1.1 deraadt *dom = _yp_domain;
807 1.1 deraadt
808 1.1 deraadt if( yp_bind(_yp_domain)==0 )
809 1.1 deraadt return 1;
810 1.1 deraadt return 0;
811 1.1 deraadt }
812