lib/libc/yp: Silence warnings.
[dragonfly.git] / lib / libc / yp / yplib.c
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  *    products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD: src/lib/libc/yp/yplib.c,v 1.34.2.2 2002/02/15 00:46:53 des Exp $
31  * $DragonFly: src/lib/libc/yp/yplib.c,v 1.9 2006/08/03 16:40:46 swildner Exp $
32  */
33
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/file.h>
39 #include <sys/uio.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <rpc/rpc.h>
46 #include <rpc/xdr.h>
47 #include <rpcsvc/yp.h>
48 #include "un-namespace.h"
49
50 bool_t  xdr_ypresp_all_seq(XDR *, u_long *);
51 int     _yp_check(char **);
52
53 int (*ypresp_allfn)(unsigned long, char *, int, char *, int, void *);
54
55 /*
56  * We have to define these here due to clashes between yp_prot.h and
57  * yp.h.
58  */
59
60 #define YPMATCHCACHE
61
62 #ifdef YPMATCHCACHE
63 struct ypmatch_ent {
64         char                    *ypc_map;
65         keydat                  ypc_key;
66         valdat                  ypc_val;
67         time_t                  ypc_expire_t;
68         struct ypmatch_ent      *ypc_next;
69 };
70 #define YPLIB_MAXCACHE  5       /* At most 5 entries */
71 #define YPLIB_EXPIRE    5       /* Expire after 5 seconds */
72 #endif
73
74 struct dom_binding {
75         struct dom_binding *dom_pnext;
76         char dom_domain[YPMAXDOMAIN + 1];
77         struct sockaddr_in dom_server_addr;
78         u_short dom_server_port;
79         int dom_socket;
80         CLIENT *dom_client;
81         u_short dom_local_port; /* now I finally know what this is for. */
82         long dom_vers;
83 #ifdef YPMATCHCACHE
84         struct ypmatch_ent *cache;
85         int ypmatch_cachecnt;
86 #endif
87 };
88
89 #include <rpcsvc/yp.h>
90 #include <rpcsvc/ypclnt.h>
91
92 #ifndef BINDINGDIR
93 #define BINDINGDIR "/var/yp/binding"
94 #endif
95 #define MAX_RETRIES 20
96
97 void *ypresp_data;
98
99 static void _yp_unbind(struct dom_binding *);
100 struct dom_binding *_ypbindlist;
101 static char _yp_domain[MAXHOSTNAMELEN];
102 int _yplib_timeout = 10;
103
104 #ifdef YPMATCHCACHE
105 static void
106 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
107     struct ypmatch_ent *cur)
108 {
109         if (prev == NULL)
110                 ypdb->cache = cur->ypc_next;
111         else
112                 prev->ypc_next = cur->ypc_next;
113
114         free(cur->ypc_map);
115         free(cur->ypc_key.keydat_val);
116         free(cur->ypc_val.valdat_val);
117         free(cur);
118
119         ypdb->ypmatch_cachecnt--;
120
121         return;
122 }
123
124 static void
125 ypmatch_cache_flush(struct dom_binding *ypdb)
126 {
127         struct ypmatch_ent      *n, *c = ypdb->cache;
128
129         while (c != NULL) {
130                 n = c->ypc_next;
131                 ypmatch_cache_delete(ypdb, NULL, c);
132                 c = n;
133         }
134
135         return;
136 }
137
138 static void
139 ypmatch_cache_expire(struct dom_binding *ypdb)
140 {
141         struct ypmatch_ent      *c = ypdb->cache;
142         struct ypmatch_ent      *n, *p = NULL;
143         time_t                  t;
144
145         time(&t);
146
147         while (c != NULL) {
148                 if (t >= c->ypc_expire_t) {
149                         n = c->ypc_next;
150                         ypmatch_cache_delete(ypdb, p, c);
151                         c = n;
152                 } else {
153                         p = c;
154                         c = c->ypc_next;
155                 }
156         }
157
158         return;
159 }
160
161 static void
162 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
163     valdat *val)
164 {
165         struct ypmatch_ent      *new;
166
167         /* Do an expire run to maybe open up a slot. */
168         if (ypdb->ypmatch_cachecnt)
169                 ypmatch_cache_expire(ypdb);
170
171         /*
172          * If there are no slots free, then force an expire of
173          * the least recently used entry.
174          */
175         if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
176                 struct ypmatch_ent      *o = NULL, *c = ypdb->cache;
177                 time_t                  oldest = 0;
178
179                 oldest = ~oldest;
180
181                 while (c != NULL) {
182                         if (c->ypc_expire_t < oldest) {
183                                 oldest = c->ypc_expire_t;
184                                 o = c;
185                         }
186                         c = c->ypc_next;
187                 }
188
189                 if (o == NULL)
190                         return;
191                 o->ypc_expire_t = 0;
192                 ypmatch_cache_expire(ypdb);
193         }
194
195         new = malloc(sizeof(struct ypmatch_ent));
196         if (new == NULL)
197                 return;
198
199         new->ypc_map = strdup(map);
200         if (new->ypc_map == NULL) {
201                 free(new);
202                 return;
203         }
204         new->ypc_key.keydat_val = malloc(key->keydat_len);
205         if (new->ypc_key.keydat_val == NULL) {
206                 free(new->ypc_map);
207                 free(new);
208                 return;
209         }
210         new->ypc_val.valdat_val = malloc(val->valdat_len);
211         if (new->ypc_val.valdat_val == NULL) {
212                 free(new->ypc_val.valdat_val);
213                 free(new->ypc_map);
214                 free(new);
215                 return;
216         }
217
218         new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
219         new->ypc_key.keydat_len = key->keydat_len;
220         new->ypc_val.valdat_len = val->valdat_len;
221         bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
222         bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
223
224         new->ypc_next = ypdb->cache;
225         ypdb->cache = new;
226
227         ypdb->ypmatch_cachecnt++;
228
229         return;
230 }
231
232 static bool_t
233 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
234     valdat *val)
235 {
236         struct ypmatch_ent      *c = ypdb->cache;
237
238         ypmatch_cache_expire(ypdb);
239
240         for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
241                 if (strcmp(map, c->ypc_map))
242                         continue;
243                 if (key->keydat_len != c->ypc_key.keydat_len)
244                         continue;
245                 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
246                                 key->keydat_len))
247                         continue;
248         }
249
250         if (c == NULL)
251                 return(FALSE);
252
253         val->valdat_len = c->ypc_val.valdat_len;
254         val->valdat_val = c->ypc_val.valdat_val;
255
256         return(TRUE);
257 }
258 #endif
259
260 char *
261 ypbinderr_string(int incode)
262 {
263         const char *errstr;
264         static char err[80];
265         switch (incode) {
266         case 0:
267                 errstr = "Success";
268                 break;
269         case YPBIND_ERR_ERR:
270                 errstr = "Internal ypbind error";
271                 break;
272         case YPBIND_ERR_NOSERV:
273                 errstr = "Domain not bound";
274                 break;
275         case YPBIND_ERR_RESC:
276                 errstr = "System resource allocation failure";
277                 break;
278         default:
279                 errstr = NULL;
280                 break;
281         }
282         if (errstr != NULL)
283                 strlcpy(err, errstr, sizeof(err));
284         else
285                 snprintf(err, sizeof(err), "Unknown ypbind error: #%d\n", incode);
286         return (err);
287 }
288
289 int
290 _yp_dobind(const char *dom, struct dom_binding **ypdb)
291 {
292         static pid_t pid = -1;
293         char path[MAXPATHLEN];
294         struct dom_binding *ysd, *ysd2;
295         struct ypbind_resp ypbr;
296         struct timeval tv;
297         struct sockaddr_in clnt_sin;
298         int clnt_sock, fd;
299         pid_t gpid;
300         CLIENT *client;
301         int new = 0;
302         ssize_t r;
303         int retries = 0;
304         struct sockaddr_in check;
305         int checklen = sizeof(struct sockaddr_in);
306
307         /* Not allowed; bad doggie. Bad. */
308         if (strchr(dom, '/') != NULL)
309                 return(YPERR_BADARGS);
310
311         gpid = getpid();
312         if (!(pid == -1 || pid == gpid)) {
313                 ysd = _ypbindlist;
314                 while (ysd) {
315                         if (ysd->dom_client != NULL)
316                                 _yp_unbind(ysd);
317                         ysd2 = ysd->dom_pnext;
318                         free(ysd);
319                         ysd = ysd2;
320                 }
321                 _ypbindlist = NULL;
322         }
323         pid = gpid;
324
325         if (ypdb != NULL)
326                 *ypdb = NULL;
327
328         if (dom == NULL || strlen(dom) == 0)
329                 return (YPERR_BADARGS);
330
331         for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
332                 if (strcmp(dom, ysd->dom_domain) == 0)
333                         break;
334
335
336         if (ysd == NULL) {
337                 ysd = (struct dom_binding *)malloc(sizeof *ysd);
338                 bzero((char *)ysd, sizeof *ysd);
339                 ysd->dom_socket = -1;
340                 ysd->dom_vers = 0;
341                 new = 1;
342         } else {
343         /* Check the socket -- may have been hosed by the caller. */
344                 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
345                     &checklen) == -1 || check.sin_family != AF_INET ||
346                     check.sin_port != ysd->dom_local_port) {
347                 /* Socket became bogus somehow... need to rebind. */
348                         int save, sock;
349
350                         sock = ysd->dom_socket;
351                         save = _dup(ysd->dom_socket);
352                         if (ysd->dom_client != NULL)
353                                 clnt_destroy(ysd->dom_client);
354                         ysd->dom_vers = 0;
355                         ysd->dom_client = NULL;
356                         sock = _dup2(save, sock);
357                         _close(save);
358                 }
359         }
360
361 again:
362         retries++;
363         if (retries > MAX_RETRIES) {
364                 if (new)
365                         free(ysd);
366                 return(YPERR_YPBIND);
367         }
368 #ifdef BINDINGDIR
369         if (ysd->dom_vers == 0) {
370                 /*
371                  * We're trying to make a new binding: zorch the
372                  * existing handle now (if any).
373                  */
374                 if (ysd->dom_client != NULL) {
375                         clnt_destroy(ysd->dom_client);
376                         ysd->dom_client = NULL;
377                         ysd->dom_socket = -1;
378                 }
379                 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
380                 if ((fd = _open(path, O_RDONLY)) == -1) {
381                         /* no binding file, YP is dead. */
382                         /* Try to bring it back to life. */
383                         _close(fd);
384                         goto skipit;
385                 }
386                 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
387                         struct iovec iov[2];
388                         struct ypbind_resp ybr;
389                         u_short ypb_port;
390
391                         iov[0].iov_base = (caddr_t)&ypb_port;
392                         iov[0].iov_len = sizeof ypb_port;
393                         iov[1].iov_base = (caddr_t)&ybr;
394                         iov[1].iov_len = sizeof ybr;
395
396                         r = _readv(fd, iov, 2);
397                         if (r != (ssize_t)(iov[0].iov_len + iov[1].iov_len)) {
398                                 _close(fd);
399                                 ysd->dom_vers = -1;
400                                 goto again;
401                         }
402
403                         bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
404                         ysd->dom_server_addr.sin_family = AF_INET;
405                         ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
406                         ysd->dom_server_addr.sin_addr.s_addr =
407                             *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
408                         ysd->dom_server_addr.sin_port =
409                             *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
410
411                         ysd->dom_server_port = ysd->dom_server_addr.sin_port;
412                         _close(fd);
413                         goto gotit;
414                 } else {
415                         /* no lock on binding file, YP is dead. */
416                         /* Try to bring it back to life. */
417                         _close(fd);
418                         goto skipit;
419                 }
420         }
421 skipit:
422 #endif
423         if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
424                 /*
425                  * We're trying to make a new binding: zorch the
426                  * existing handle now (if any).
427                  */
428                 if (ysd->dom_client != NULL) {
429                         clnt_destroy(ysd->dom_client);
430                         ysd->dom_client = NULL;
431                         ysd->dom_socket = -1;
432                 }
433                 bzero((char *)&clnt_sin, sizeof clnt_sin);
434                 clnt_sin.sin_family = AF_INET;
435                 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
436
437                 clnt_sock = RPC_ANYSOCK;
438                 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
439                         0, 0);
440                 if (client == NULL) {
441                         /*
442                          * These conditions indicate ypbind just isn't
443                          * alive -- we probably don't want to shoot our
444                          * mouth off in this case; instead generate error
445                          * messages only for really exotic problems.
446                          */
447                         if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
448                            (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
449                            rpc_createerr.cf_error.re_errno == ECONNREFUSED))
450                                 clnt_pcreateerror("clnttcp_create");
451                         if (new)
452                                 free(ysd);
453                         return (YPERR_YPBIND);
454                 }
455
456                 /*
457                  * Check the port number -- should be < IPPORT_RESERVED.
458                  * If not, it's possible someone has registered a bogus
459                  * ypbind with the portmapper and is trying to trick us.
460                  */
461                 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
462                         if (client != NULL)
463                                 clnt_destroy(client);
464                         if (new)
465                                 free(ysd);
466                         return(YPERR_YPBIND);
467                 }
468                 tv.tv_sec = _yplib_timeout/2;
469                 tv.tv_usec = 0;
470                 r = clnt_call(client, YPBINDPROC_DOMAIN,
471                     (xdrproc_t)xdr_domainname, (char *)&dom,
472                     (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
473                 if (r != RPC_SUCCESS) {
474                         clnt_destroy(client);
475                         ysd->dom_vers = -1;
476                         if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
477                                 if (new)
478                                         free(ysd);
479                                 return(YPERR_YPBIND);
480                         }
481                         fprintf(stderr,
482                         "YP: server for domain %s not responding, retrying\n", dom);
483                         goto again;
484                 } else {
485                         if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
486                                 struct timespec time_to_sleep, time_remaining;
487
488                                 clnt_destroy(client);
489                                 ysd->dom_vers = -1;
490
491                                 time_to_sleep.tv_sec = _yplib_timeout/2;
492                                 time_to_sleep.tv_nsec = 0;
493                                 _nanosleep(&time_to_sleep,
494                                     &time_remaining);
495                                 goto again;
496                         }
497                 }
498                 clnt_destroy(client);
499
500                 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
501                 ysd->dom_server_addr.sin_family = AF_INET;
502                 ysd->dom_server_addr.sin_port =
503                         *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
504                 ysd->dom_server_addr.sin_addr.s_addr =
505                         *(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
506
507                 /*
508                  * We could do a reserved port check here too, but this
509                  * could pose compatibility problems. The local ypbind is
510                  * supposed to decide whether or not to trust yp servers
511                  * on insecure ports. For now, we trust its judgement.
512                  */
513                 ysd->dom_server_port =
514                         *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
515 gotit:
516                 ysd->dom_vers = YPVERS;
517                 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
518         }
519
520         /* Don't rebuild the connection to the server unless we have to. */
521         if (ysd->dom_client == NULL) {
522                 tv.tv_sec = _yplib_timeout/2;
523                 tv.tv_usec = 0;
524                 ysd->dom_socket = RPC_ANYSOCK;
525                 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
526                         YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
527                 if (ysd->dom_client == NULL) {
528                         clnt_pcreateerror("clntudp_create");
529                         ysd->dom_vers = -1;
530                         goto again;
531                 }
532                 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
533                         perror("fcntl: F_SETFD");
534                 /*
535                  * We want a port number associated with this socket
536                  * so that we can check its authenticity later.
537                  */
538                 checklen = sizeof(struct sockaddr_in);
539                 bzero((char *)&check, checklen);
540                 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
541                 check.sin_family = AF_INET;
542                 if (!_getsockname(ysd->dom_socket,
543                     (struct sockaddr *)&check, &checklen)) {
544                         ysd->dom_local_port = check.sin_port;
545                 } else {
546                         clnt_destroy(ysd->dom_client);
547                         if (new)
548                                 free(ysd);
549                         return(YPERR_YPBIND);
550                 }
551         }
552
553         if (new) {
554                 ysd->dom_pnext = _ypbindlist;
555                 _ypbindlist = ysd;
556         }
557
558         if (ypdb != NULL)
559                 *ypdb = ysd;
560         return (0);
561 }
562
563 static void
564 _yp_unbind(struct dom_binding *ypb)
565 {
566         struct sockaddr_in check;
567         int checklen = sizeof(struct sockaddr_in);
568
569         if (ypb->dom_client != NULL) {
570                 /* Check the socket -- may have been hosed by the caller. */
571                 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
572                 &checklen) == -1 || check.sin_family != AF_INET ||
573                 check.sin_port != ypb->dom_local_port) {
574                         int save, sock;
575
576                         sock = ypb->dom_socket;
577                         save = _dup(ypb->dom_socket);
578                         clnt_destroy(ypb->dom_client);
579                         sock = _dup2(save, sock);
580                         _close(save);
581                 } else
582                         clnt_destroy(ypb->dom_client);
583         }
584
585         ypb->dom_client = NULL;
586         ypb->dom_socket = -1;
587         ypb->dom_vers = -1;
588 #ifdef YPMATCHCACHE
589         ypmatch_cache_flush(ypb);
590 #endif
591 }
592
593 int
594 yp_bind(char *dom)
595 {
596         return (_yp_dobind(dom, NULL));
597 }
598
599 void
600 yp_unbind(char *dom)
601 {
602         struct dom_binding *ypb, *ypbp;
603
604         ypbp = NULL;
605         for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
606                 if (strcmp(dom, ypb->dom_domain) == 0) {
607                         _yp_unbind(ypb);
608                         if (ypbp)
609                                 ypbp->dom_pnext = ypb->dom_pnext;
610                         else
611                                 _ypbindlist = ypb->dom_pnext;
612                         free(ypb);
613                         return;
614                 }
615                 ypbp = ypb;
616         }
617         return;
618 }
619
620 int
621 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
622     char **outval, int *outvallen)
623 {
624         struct dom_binding *ysd;
625         struct ypresp_val yprv;
626         struct timeval tv;
627         struct ypreq_key yprk;
628         int r;
629
630         *outval = NULL;
631         *outvallen = 0;
632
633         /* Sanity check */
634
635         if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
636             inmap == NULL || !strlen(inmap) ||
637             indomain == NULL || !strlen(indomain))
638                 return (YPERR_BADARGS);
639
640         if (_yp_dobind(indomain, &ysd) != 0)
641                 return(YPERR_DOMAIN);
642
643         yprk.domain = indomain;
644         yprk.map = inmap;
645         yprk.key.keydat_val = (char *)inkey;
646         yprk.key.keydat_len = inkeylen;
647
648 #ifdef YPMATCHCACHE
649         if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
650 /*
651         if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
652             inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
653 */
654                 *outvallen = yprv.val.valdat_len;
655                 *outval = (char *)malloc(*outvallen+1);
656                 bcopy(yprv.val.valdat_val, *outval, *outvallen);
657                 (*outval)[*outvallen] = '\0';
658                 return (0);
659         }
660 #endif
661
662 again:
663         if (_yp_dobind(indomain, &ysd) != 0)
664                 return (YPERR_DOMAIN);
665
666         tv.tv_sec = _yplib_timeout;
667         tv.tv_usec = 0;
668
669         bzero((char *)&yprv, sizeof yprv);
670
671         r = clnt_call(ysd->dom_client, YPPROC_MATCH,
672             (xdrproc_t)xdr_ypreq_key, &yprk,
673             (xdrproc_t)xdr_ypresp_val, &yprv, tv);
674         if (r != RPC_SUCCESS) {
675                 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
676                 _yp_unbind(ysd);
677                 goto again;
678         }
679
680         if (!(r = ypprot_err(yprv.stat))) {
681                 *outvallen = yprv.val.valdat_len;
682                 *outval = (char *)malloc(*outvallen+1);
683                 bcopy(yprv.val.valdat_val, *outval, *outvallen);
684                 (*outval)[*outvallen] = '\0';
685 #ifdef YPMATCHCACHE
686                 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
687 #endif
688         }
689
690         xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
691         return (r);
692 }
693
694 int
695 yp_get_default_domain(char **domp)
696 {
697         *domp = NULL;
698         if (_yp_domain[0] == '\0')
699                 if (getdomainname(_yp_domain, sizeof _yp_domain))
700                         return (YPERR_NODOM);
701         *domp = _yp_domain;
702         return (0);
703 }
704
705 int
706 yp_first(char *indomain, char *inmap, char **outkey, size_t *outkeylen,
707     char **outval, size_t *outvallen)
708 {
709         struct ypresp_key_val yprkv;
710         struct ypreq_nokey yprnk;
711         struct dom_binding *ysd;
712         struct timeval tv;
713         int r;
714
715         /* Sanity check */
716
717         if (indomain == NULL || !strlen(indomain) ||
718             inmap == NULL || !strlen(inmap))
719                 return (YPERR_BADARGS);
720
721         *outkey = *outval = NULL;
722         *outkeylen = *outvallen = 0;
723
724 again:
725         if (_yp_dobind(indomain, &ysd) != 0)
726                 return (YPERR_DOMAIN);
727
728         tv.tv_sec = _yplib_timeout;
729         tv.tv_usec = 0;
730
731         yprnk.domain = indomain;
732         yprnk.map = inmap;
733         bzero((char *)&yprkv, sizeof yprkv);
734
735         r = clnt_call(ysd->dom_client, YPPROC_FIRST,
736             (xdrproc_t)xdr_ypreq_nokey, &yprnk,
737             (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
738         if (r != RPC_SUCCESS) {
739                 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
740                 _yp_unbind(ysd);
741                 goto again;
742         }
743         if (!(r = ypprot_err(yprkv.stat))) {
744                 *outkeylen = yprkv.key.keydat_len;
745                 *outkey = (char *)malloc(*outkeylen+1);
746                 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
747                 (*outkey)[*outkeylen] = '\0';
748                 *outvallen = yprkv.val.valdat_len;
749                 *outval = (char *)malloc(*outvallen+1);
750                 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
751                 (*outval)[*outvallen] = '\0';
752         }
753
754         xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
755         return (r);
756 }
757
758 int
759 yp_next(char *indomain, char *inmap, char *inkey, size_t inkeylen,
760     char **outkey, size_t *outkeylen, char **outval, size_t *outvallen)
761 {
762         struct ypresp_key_val yprkv;
763         struct ypreq_key yprk;
764         struct dom_binding *ysd;
765         struct timeval tv;
766         int r;
767
768         /* Sanity check */
769
770         if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
771             inmap == NULL || !strlen(inmap) ||
772             indomain == NULL || !strlen(indomain))
773                 return (YPERR_BADARGS);
774
775         *outkey = *outval = NULL;
776         *outkeylen = *outvallen = 0;
777
778 again:
779         if (_yp_dobind(indomain, &ysd) != 0)
780                 return (YPERR_DOMAIN);
781
782         tv.tv_sec = _yplib_timeout;
783         tv.tv_usec = 0;
784
785         yprk.domain = indomain;
786         yprk.map = inmap;
787         yprk.key.keydat_val = inkey;
788         yprk.key.keydat_len = inkeylen;
789         bzero((char *)&yprkv, sizeof yprkv);
790
791         r = clnt_call(ysd->dom_client, YPPROC_NEXT,
792             (xdrproc_t)xdr_ypreq_key, &yprk,
793             (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
794         if (r != RPC_SUCCESS) {
795                 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
796                 _yp_unbind(ysd);
797                 goto again;
798         }
799         if (!(r = ypprot_err(yprkv.stat))) {
800                 *outkeylen = yprkv.key.keydat_len;
801                 *outkey = (char *)malloc(*outkeylen+1);
802                 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
803                 (*outkey)[*outkeylen] = '\0';
804                 *outvallen = yprkv.val.valdat_len;
805                 *outval = (char *)malloc(*outvallen+1);
806                 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
807                 (*outval)[*outvallen] = '\0';
808         }
809
810         xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
811         return (r);
812 }
813
814 int
815 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
816 {
817         struct ypreq_nokey yprnk;
818         struct dom_binding *ysd;
819         struct timeval tv;
820         struct sockaddr_in clnt_sin;
821         CLIENT *clnt;
822         u_long status, savstat;
823         int clnt_sock;
824
825         /* Sanity check */
826
827         if (indomain == NULL || !strlen(indomain) ||
828             inmap == NULL || !strlen(inmap))
829                 return (YPERR_BADARGS);
830
831 again:
832
833         if (_yp_dobind(indomain, &ysd) != 0)
834                 return (YPERR_DOMAIN);
835
836         tv.tv_sec = _yplib_timeout;
837         tv.tv_usec = 0;
838
839         /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
840
841         clnt_sock = RPC_ANYSOCK;
842         clnt_sin = ysd->dom_server_addr;
843         clnt_sin.sin_port = 0;
844         clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
845         if (clnt == NULL) {
846                 printf("clnttcp_create failed\n");
847                 return (YPERR_PMAP);
848         }
849
850         yprnk.domain = indomain;
851         yprnk.map = inmap;
852         ypresp_allfn = incallback->foreach;
853         ypresp_data = (void *)incallback->data;
854
855         if (clnt_call(clnt, YPPROC_ALL,
856                 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
857                 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
858                         clnt_perror(ysd->dom_client, "yp_all: clnt_call");
859                         clnt_destroy(clnt);
860                         _yp_unbind(ysd);
861                         goto again;
862         }
863
864         clnt_destroy(clnt);
865         savstat = status;
866         xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status);       /* not really needed... */
867         if (savstat != YP_NOMORE)
868                 return (ypprot_err(savstat));
869         return (0);
870 }
871
872 int
873 yp_order(char *indomain, char *inmap, int *outorder)
874 {
875         struct dom_binding *ysd;
876         struct ypresp_order ypro;
877         struct ypreq_nokey yprnk;
878         struct timeval tv;
879         int r;
880
881         /* Sanity check */
882
883         if (indomain == NULL || !strlen(indomain) ||
884             inmap == NULL || !strlen(inmap))
885                 return (YPERR_BADARGS);
886
887 again:
888         if (_yp_dobind(indomain, &ysd) != 0)
889                 return (YPERR_DOMAIN);
890
891         tv.tv_sec = _yplib_timeout;
892         tv.tv_usec = 0;
893
894         yprnk.domain = indomain;
895         yprnk.map = inmap;
896
897         bzero((char *)(char *)&ypro, sizeof ypro);
898
899         r = clnt_call(ysd->dom_client, YPPROC_ORDER,
900             (xdrproc_t)xdr_ypreq_nokey, &yprnk,
901             (xdrproc_t)xdr_ypresp_order, &ypro, tv);
902
903         /*
904          * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
905          * procedure.
906          */
907         if (r == RPC_PROCUNAVAIL) {
908                 return(YPERR_YPERR);
909         }
910
911         if (r != RPC_SUCCESS) {
912                 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
913                 _yp_unbind(ysd);
914                 goto again;
915         }
916
917         if (!(r = ypprot_err(ypro.stat))) {
918                 *outorder = ypro.ordernum;
919         }
920
921         xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
922         return (r);
923 }
924
925 int
926 yp_master(char *indomain, char *inmap, char **outname)
927 {
928         struct dom_binding *ysd;
929         struct ypresp_master yprm;
930         struct ypreq_nokey yprnk;
931         struct timeval tv;
932         int r;
933
934         /* Sanity check */
935
936         if (indomain == NULL || !strlen(indomain) ||
937             inmap == NULL || !strlen(inmap))
938                 return (YPERR_BADARGS);
939 again:
940         if (_yp_dobind(indomain, &ysd) != 0)
941                 return (YPERR_DOMAIN);
942
943         tv.tv_sec = _yplib_timeout;
944         tv.tv_usec = 0;
945
946         yprnk.domain = indomain;
947         yprnk.map = inmap;
948
949         bzero((char *)&yprm, sizeof yprm);
950
951         r = clnt_call(ysd->dom_client, YPPROC_MASTER,
952             (xdrproc_t)xdr_ypreq_nokey, &yprnk,
953             (xdrproc_t)xdr_ypresp_master, &yprm, tv);
954         if (r != RPC_SUCCESS) {
955                 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
956                 _yp_unbind(ysd);
957                 goto again;
958         }
959
960         if (!(r = ypprot_err(yprm.stat))) {
961                 *outname = (char *)strdup(yprm.peer);
962         }
963
964         xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
965         return (r);
966 }
967
968 int
969 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
970 {
971         struct dom_binding *ysd;
972         struct ypresp_maplist ypml;
973         struct timeval tv;
974         int r;
975
976         /* Sanity check */
977
978         if (indomain == NULL || !strlen(indomain))
979                 return (YPERR_BADARGS);
980
981 again:
982         if (_yp_dobind(indomain, &ysd) != 0)
983                 return (YPERR_DOMAIN);
984
985         tv.tv_sec = _yplib_timeout;
986         tv.tv_usec = 0;
987
988         bzero((char *)&ypml, sizeof ypml);
989
990         r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
991             (xdrproc_t)xdr_domainname, (char *)&indomain,
992             (xdrproc_t)xdr_ypresp_maplist, &ypml, tv);
993         if (r != RPC_SUCCESS) {
994                 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
995                 _yp_unbind(ysd);
996                 goto again;
997         }
998         if (!(r = ypprot_err(ypml.stat))) {
999                 *outmaplist = ypml.maps;
1000         }
1001
1002         /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
1003         return (r);
1004 }
1005
1006 char *
1007 yperr_string(int incode)
1008 {
1009         const char *errstr;
1010         static char err[80];
1011
1012         switch (incode) {
1013         case 0:
1014                 errstr = "Success";
1015                 break;
1016         case YPERR_BADARGS:
1017                 errstr = "Request arguments bad";
1018                 break;
1019         case YPERR_RPC:
1020                 errstr = "RPC failure";
1021                 break;
1022         case YPERR_DOMAIN:
1023                 errstr = "Can't bind to server which serves this domain";
1024                 break;
1025         case YPERR_MAP:
1026                 errstr = "No such map in server's domain";
1027                 break;
1028         case YPERR_KEY:
1029                 errstr = "No such key in map";
1030                 break;
1031         case YPERR_YPERR:
1032                 errstr = "YP server error";
1033                 break;
1034         case YPERR_RESRC:
1035                 errstr = "Local resource allocation failure";
1036                 break;
1037         case YPERR_NOMORE:
1038                 errstr = "No more records in map database";
1039                 break;
1040         case YPERR_PMAP:
1041                 errstr = "Can't communicate with portmapper";
1042                 break;
1043         case YPERR_YPBIND:
1044                 errstr = "Can't communicate with ypbind";
1045                 break;
1046         case YPERR_YPSERV:
1047                 errstr = "Can't communicate with ypserv";
1048                 break;
1049         case YPERR_NODOM:
1050                 errstr = "Local domain name not set";
1051                 break;
1052         case YPERR_BADDB:
1053                 errstr = "Server data base is bad";
1054                 break;
1055         case YPERR_VERS:
1056                 errstr = "YP server version mismatch - server can't supply service.";
1057                 break;
1058         case YPERR_ACCESS:
1059                 errstr = "Access violation";
1060                 break;
1061         case YPERR_BUSY:
1062                 errstr = "Database is busy";
1063                 break;
1064         default:
1065                 errstr = NULL;
1066                 break;
1067         }
1068         if (errstr != NULL)
1069                 strlcpy(err, errstr, sizeof(err));
1070         else
1071                 snprintf(err, sizeof(err), "YP unknown error %d\n", incode);
1072         return (err);
1073 }
1074
1075 int
1076 ypprot_err(unsigned int incode)
1077 {
1078         switch (incode) {
1079         case YP_TRUE:
1080                 return (0);
1081         case YP_FALSE:
1082                 return (YPERR_YPBIND);
1083         case YP_NOMORE:
1084                 return (YPERR_NOMORE);
1085         case YP_NOMAP:
1086                 return (YPERR_MAP);
1087         case YP_NODOM:
1088                 return (YPERR_DOMAIN);
1089         case YP_NOKEY:
1090                 return (YPERR_KEY);
1091         case YP_BADOP:
1092                 return (YPERR_YPERR);
1093         case YP_BADDB:
1094                 return (YPERR_BADDB);
1095         case YP_YPERR:
1096                 return (YPERR_YPERR);
1097         case YP_BADARGS:
1098                 return (YPERR_BADARGS);
1099         case YP_VERS:
1100                 return (YPERR_VERS);
1101         }
1102         return (YPERR_YPERR);
1103 }
1104
1105 int
1106 _yp_check(char **dom)
1107 {
1108         char *unused;
1109
1110         if (_yp_domain[0]=='\0')
1111                 if (yp_get_default_domain(&unused))
1112                         return (0);
1113
1114         if (dom)
1115                 *dom = _yp_domain;
1116
1117         if (yp_bind(_yp_domain) == 0) {
1118                 yp_unbind(_yp_domain);
1119                 return (1);
1120         }
1121         return (0);
1122 }