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