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