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