2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
5 * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
33 #include <sys/cdefs.h>
34 #include "namespace.h"
35 #include "reentrant.h"
36 #include <sys/param.h>
37 #include <sys/socket.h>
40 #include <arpa/inet.h>
48 #include <rpcsvc/yp.h>
49 #include "un-namespace.h"
50 #include "libc_private.h"
53 * We have to define these here due to clashes between yp_prot.h and
65 struct ypmatch_ent *ypc_next;
67 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
68 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
72 struct dom_binding *dom_pnext;
73 char dom_domain[YPMAXDOMAIN + 1];
74 struct sockaddr_in dom_server_addr;
75 u_short dom_server_port;
78 u_short dom_local_port; /* now I finally know what this is for. */
81 struct ypmatch_ent *cache;
86 #include <rpcsvc/ypclnt.h>
89 #define BINDINGDIR "/var/yp/binding"
91 #define MAX_RETRIES 20
93 bool_t xdr_ypresp_all_seq(XDR *xdrs, u_long *objp);
95 int (*ypresp_allfn)();
98 static void _yp_unbind(struct dom_binding *);
99 struct dom_binding *_ypbindlist;
100 static char _yp_domain[MAXHOSTNAMELEN];
101 int _yplib_timeout = 20;
103 static mutex_t _ypmutex = MUTEX_INITIALIZER;
104 #define YPLOCK() mutex_lock(&_ypmutex);
105 #define YPUNLOCK() mutex_unlock(&_ypmutex);
109 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
110 struct ypmatch_ent *cur)
113 ypdb->cache = cur->ypc_next;
115 prev->ypc_next = cur->ypc_next;
118 free(cur->ypc_key.keydat_val);
119 free(cur->ypc_val.valdat_val);
122 ypdb->ypmatch_cachecnt--;
128 ypmatch_cache_flush(struct dom_binding *ypdb)
130 struct ypmatch_ent *n, *c = ypdb->cache;
134 ypmatch_cache_delete(ypdb, NULL, c);
142 ypmatch_cache_expire(struct dom_binding *ypdb)
144 struct ypmatch_ent *c = ypdb->cache;
145 struct ypmatch_ent *n, *p = NULL;
151 if (t >= c->ypc_expire_t) {
153 ypmatch_cache_delete(ypdb, p, c);
165 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
168 struct ypmatch_ent *new;
170 /* Do an expire run to maybe open up a slot. */
171 if (ypdb->ypmatch_cachecnt)
172 ypmatch_cache_expire(ypdb);
175 * If there are no slots free, then force an expire of
176 * the least recently used entry.
178 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
179 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
185 if (c->ypc_expire_t < oldest) {
186 oldest = c->ypc_expire_t;
195 ypmatch_cache_expire(ypdb);
198 new = malloc(sizeof(struct ypmatch_ent));
202 new->ypc_map = strdup(map);
203 if (new->ypc_map == NULL) {
207 new->ypc_key.keydat_val = malloc(key->keydat_len);
208 if (new->ypc_key.keydat_val == NULL) {
213 new->ypc_val.valdat_val = malloc(val->valdat_len);
214 if (new->ypc_val.valdat_val == NULL) {
215 free(new->ypc_val.valdat_val);
221 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
222 new->ypc_key.keydat_len = key->keydat_len;
223 new->ypc_val.valdat_len = val->valdat_len;
224 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
225 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
227 new->ypc_next = ypdb->cache;
230 ypdb->ypmatch_cachecnt++;
236 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
239 struct ypmatch_ent *c;
241 ypmatch_cache_expire(ypdb);
243 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
244 if (strcmp(map, c->ypc_map))
246 if (key->keydat_len != c->ypc_key.keydat_len)
248 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
256 val->valdat_len = c->ypc_val.valdat_len;
257 val->valdat_val = c->ypc_val.valdat_val;
264 ypbinderr_string(int incode)
271 return ("Internal ypbind error");
272 case YPBIND_ERR_NOSERV:
273 return ("Domain not bound");
274 case YPBIND_ERR_RESC:
275 return ("System resource allocation failure");
277 sprintf(err, "Unknown ypbind error: #%d\n", incode);
282 _yp_dobind(char *dom, struct dom_binding **ypdb)
284 static pid_t pid = -1;
285 char path[MAXPATHLEN];
286 struct dom_binding *ysd, *ysd2;
287 struct ypbind_resp ypbr;
289 struct sockaddr_in clnt_sin;
295 struct sockaddr_in check;
296 socklen_t checklen = sizeof(struct sockaddr_in);
298 /* Not allowed; bad doggie. Bad. */
299 if (strchr(dom, '/') != NULL)
300 return(YPERR_BADARGS);
303 if (!(pid == -1 || pid == gpid)) {
306 if (ysd->dom_client != NULL)
308 ysd2 = ysd->dom_pnext;
319 if (dom == NULL || strlen(dom) == 0)
320 return (YPERR_BADARGS);
322 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
323 if (strcmp(dom, ysd->dom_domain) == 0)
328 ysd = (struct dom_binding *)malloc(sizeof *ysd);
330 return (YPERR_RESRC);
331 bzero((char *)ysd, sizeof *ysd);
332 ysd->dom_socket = -1;
336 /* Check the socket -- may have been hosed by the caller. */
337 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
338 &checklen) == -1 || check.sin_family != AF_INET ||
339 check.sin_port != ysd->dom_local_port) {
340 /* Socket became bogus somehow... need to rebind. */
343 sock = ysd->dom_socket;
344 save = _dup(ysd->dom_socket);
345 if (ysd->dom_client != NULL)
346 clnt_destroy(ysd->dom_client);
348 ysd->dom_client = NULL;
349 sock = _dup2(save, sock);
356 if (retries > MAX_RETRIES) {
359 return(YPERR_YPBIND);
362 if (ysd->dom_vers == 0) {
364 * We're trying to make a new binding: zorch the
365 * existing handle now (if any).
367 if (ysd->dom_client != NULL) {
368 clnt_destroy(ysd->dom_client);
369 ysd->dom_client = NULL;
370 ysd->dom_socket = -1;
372 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
373 if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
374 /* no binding file, YP is dead. */
375 /* Try to bring it back to life. */
379 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
381 struct ypbind_resp ybr;
384 iov[0].iov_base = (caddr_t)&ypb_port;
385 iov[0].iov_len = sizeof ypb_port;
386 iov[1].iov_base = (caddr_t)&ybr;
387 iov[1].iov_len = sizeof ybr;
389 r = _readv(fd, iov, 2);
390 if (r != iov[0].iov_len + iov[1].iov_len) {
396 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
397 ysd->dom_server_addr.sin_family = AF_INET;
398 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
399 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
400 &ysd->dom_server_addr.sin_addr.s_addr,
401 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
402 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
403 &ysd->dom_server_addr.sin_port,
404 sizeof(ysd->dom_server_addr.sin_port));
406 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
410 /* no lock on binding file, YP is dead. */
411 /* Try to bring it back to life. */
418 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
420 * We're trying to make a new binding: zorch the
421 * existing handle now (if any).
423 if (ysd->dom_client != NULL) {
424 clnt_destroy(ysd->dom_client);
425 ysd->dom_client = NULL;
426 ysd->dom_socket = -1;
428 bzero((char *)&clnt_sin, sizeof clnt_sin);
429 clnt_sin.sin_family = AF_INET;
430 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
432 clnt_sock = RPC_ANYSOCK;
433 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
435 if (client == NULL) {
437 * These conditions indicate ypbind just isn't
438 * alive -- we probably don't want to shoot our
439 * mouth off in this case; instead generate error
440 * messages only for really exotic problems.
442 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
443 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
444 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
445 clnt_pcreateerror("clnttcp_create");
448 return (YPERR_YPBIND);
452 * Check the port number -- should be < IPPORT_RESERVED.
453 * If not, it's possible someone has registered a bogus
454 * ypbind with the portmapper and is trying to trick us.
456 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
458 clnt_destroy(client);
461 return(YPERR_YPBIND);
463 tv.tv_sec = _yplib_timeout/2;
465 r = clnt_call(client, YPBINDPROC_DOMAIN,
466 (xdrproc_t)xdr_domainname, &dom,
467 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
468 if (r != RPC_SUCCESS) {
469 clnt_destroy(client);
471 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
474 return(YPERR_YPBIND);
477 "YP: server for domain %s not responding, retrying\n", dom);
480 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
481 struct timespec time_to_sleep, time_remaining;
483 clnt_destroy(client);
486 time_to_sleep.tv_sec = _yplib_timeout/2;
487 time_to_sleep.tv_nsec = 0;
488 _nanosleep(&time_to_sleep,
493 clnt_destroy(client);
495 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
496 ysd->dom_server_addr.sin_family = AF_INET;
497 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
498 &ysd->dom_server_addr.sin_port,
499 sizeof(ysd->dom_server_addr.sin_port));
500 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
501 &ysd->dom_server_addr.sin_addr.s_addr,
502 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
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.
510 ysd->dom_server_port =
511 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
513 ysd->dom_vers = YPVERS;
514 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
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;
521 ysd->dom_socket = RPC_ANYSOCK;
522 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
523 YPPROG, YPVERS, tv, &ysd->dom_socket, 65507, 65507);
524 if (ysd->dom_client == NULL) {
525 clnt_pcreateerror("clntudp_create");
529 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
530 perror("fcntl: F_SETFD");
532 * We want a port number associated with this socket
533 * so that we can check its authenticity later.
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;
543 clnt_destroy(ysd->dom_client);
546 return(YPERR_YPBIND);
551 ysd->dom_pnext = _ypbindlist;
556 * Set low retry timeout to realistically handle UDP packet
557 * loss for YP packet bursts.
561 clnt_control(ysd->dom_client, CLSET_RETRY_TIMEOUT, (char*)&tv);
569 _yp_unbind(struct dom_binding *ypb)
571 struct sockaddr_in check;
572 socklen_t checklen = sizeof(struct sockaddr_in);
574 if (ypb->dom_client != NULL) {
575 /* Check the socket -- may have been hosed by the caller. */
576 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
577 &checklen) == -1 || check.sin_family != AF_INET ||
578 check.sin_port != ypb->dom_local_port) {
581 sock = ypb->dom_socket;
582 save = _dup(ypb->dom_socket);
583 clnt_destroy(ypb->dom_client);
584 sock = _dup2(save, sock);
587 clnt_destroy(ypb->dom_client);
590 ypb->dom_client = NULL;
591 ypb->dom_socket = -1;
594 ypmatch_cache_flush(ypb);
599 yp_bind_locked(char *dom)
601 return (_yp_dobind(dom, NULL));
610 r = yp_bind_locked(dom);
616 yp_unbind_locked(char *dom)
618 struct dom_binding *ypb, *ypbp;
621 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
622 if (strcmp(dom, ypb->dom_domain) == 0) {
625 ypbp->dom_pnext = ypb->dom_pnext;
627 _ypbindlist = ypb->dom_pnext;
640 yp_unbind_locked(dom);
645 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
646 char **outval, int *outvallen)
648 struct dom_binding *ysd;
649 struct ypresp_val yprv;
651 struct ypreq_key yprk;
659 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
660 inmap == NULL || !strlen(inmap) ||
661 indomain == NULL || !strlen(indomain))
662 return (YPERR_BADARGS);
665 if (_yp_dobind(indomain, &ysd) != 0) {
667 return(YPERR_DOMAIN);
670 yprk.domain = indomain;
672 yprk.key.keydat_val = (char *)inkey;
673 yprk.key.keydat_len = inkeylen;
676 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
678 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
679 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
681 *outvallen = yprv.val.valdat_len;
682 *outval = (char *)malloc(*outvallen+1);
683 if (*outval == NULL) {
687 return (YPERR_RESRC);
689 bcopy(yprv.val.valdat_val, *outval, *outvallen);
690 (*outval)[*outvallen] = '\0';
698 if (retries > MAX_RETRIES) {
703 if (_yp_dobind(indomain, &ysd) != 0) {
705 return (YPERR_DOMAIN);
708 tv.tv_sec = _yplib_timeout;
711 bzero((char *)&yprv, sizeof yprv);
713 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
714 (xdrproc_t)xdr_ypreq_key, &yprk,
715 (xdrproc_t)xdr_ypresp_val, &yprv, tv);
716 if (r != RPC_SUCCESS) {
717 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
723 if (!(r = ypprot_err(yprv.stat))) {
724 *outvallen = yprv.val.valdat_len;
725 *outval = (char *)malloc(*outvallen+1);
726 if (*outval == NULL) {
729 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
731 return (YPERR_RESRC);
733 bcopy(yprv.val.valdat_val, *outval, *outvallen);
734 (*outval)[*outvallen] = '\0';
736 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
740 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
746 yp_get_default_domain_locked(char **domp)
749 if (_yp_domain[0] == '\0')
750 if (getdomainname(_yp_domain, sizeof _yp_domain))
751 return (YPERR_NODOM);
757 yp_get_default_domain(char **domp)
762 r = yp_get_default_domain_locked(domp);
768 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
769 char **outval, int *outvallen)
771 struct ypresp_key_val yprkv;
772 struct ypreq_nokey yprnk;
773 struct dom_binding *ysd;
779 if (indomain == NULL || !strlen(indomain) ||
780 inmap == NULL || !strlen(inmap))
781 return (YPERR_BADARGS);
783 *outkey = *outval = NULL;
784 *outkeylen = *outvallen = 0;
788 if (retries > MAX_RETRIES) {
793 if (_yp_dobind(indomain, &ysd) != 0) {
795 return (YPERR_DOMAIN);
798 tv.tv_sec = _yplib_timeout;
801 yprnk.domain = indomain;
803 bzero((char *)&yprkv, sizeof yprkv);
805 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
806 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
807 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
808 if (r != RPC_SUCCESS) {
809 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
814 if (!(r = ypprot_err(yprkv.stat))) {
815 *outkeylen = yprkv.key.keydat_len;
816 *outkey = (char *)malloc(*outkeylen+1);
817 if (*outkey == NULL) {
820 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
822 return (YPERR_RESRC);
824 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
825 (*outkey)[*outkeylen] = '\0';
826 *outvallen = yprkv.val.valdat_len;
827 *outval = (char *)malloc(*outvallen+1);
828 if (*outval == NULL) {
831 *outkeylen = *outvallen = 0;
832 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
834 return (YPERR_RESRC);
836 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
837 (*outval)[*outvallen] = '\0';
840 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
846 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
847 char **outkey, int *outkeylen, char **outval, int *outvallen)
849 struct ypresp_key_val yprkv;
850 struct ypreq_key yprk;
851 struct dom_binding *ysd;
857 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
858 inmap == NULL || !strlen(inmap) ||
859 indomain == NULL || !strlen(indomain))
860 return (YPERR_BADARGS);
862 *outkey = *outval = NULL;
863 *outkeylen = *outvallen = 0;
867 if (retries > MAX_RETRIES) {
872 if (_yp_dobind(indomain, &ysd) != 0) {
874 return (YPERR_DOMAIN);
877 tv.tv_sec = _yplib_timeout;
880 yprk.domain = indomain;
882 yprk.key.keydat_val = inkey;
883 yprk.key.keydat_len = inkeylen;
884 bzero((char *)&yprkv, sizeof yprkv);
886 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
887 (xdrproc_t)xdr_ypreq_key, &yprk,
888 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
889 if (r != RPC_SUCCESS) {
890 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
895 if (!(r = ypprot_err(yprkv.stat))) {
896 *outkeylen = yprkv.key.keydat_len;
897 *outkey = (char *)malloc(*outkeylen+1);
898 if (*outkey == NULL) {
901 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
903 return (YPERR_RESRC);
905 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
906 (*outkey)[*outkeylen] = '\0';
907 *outvallen = yprkv.val.valdat_len;
908 *outval = (char *)malloc(*outvallen+1);
909 if (*outval == NULL) {
912 *outkeylen = *outvallen = 0;
913 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
915 return (YPERR_RESRC);
917 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
918 (*outval)[*outvallen] = '\0';
921 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
927 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
929 struct ypreq_nokey yprnk;
930 struct dom_binding *ysd;
932 struct sockaddr_in clnt_sin;
934 u_long status, savstat;
939 if (indomain == NULL || !strlen(indomain) ||
940 inmap == NULL || !strlen(inmap))
941 return (YPERR_BADARGS);
945 if (retries > MAX_RETRIES) {
950 if (_yp_dobind(indomain, &ysd) != 0) {
952 return (YPERR_DOMAIN);
955 tv.tv_sec = _yplib_timeout;
958 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
960 clnt_sock = RPC_ANYSOCK;
961 clnt_sin = ysd->dom_server_addr;
962 clnt_sin.sin_port = 0;
963 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
966 printf("clnttcp_create failed\n");
970 yprnk.domain = indomain;
972 ypresp_allfn = incallback->foreach;
973 ypresp_data = (void *)incallback->data;
975 if (clnt_call(clnt, YPPROC_ALL,
976 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
977 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
978 clnt_perror(clnt, "yp_all: clnt_call");
987 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */
989 if (savstat != YP_NOMORE)
990 return (ypprot_err(savstat));
995 yp_order(char *indomain, char *inmap, int *outorder)
997 struct dom_binding *ysd;
998 struct ypresp_order ypro;
999 struct ypreq_nokey yprnk;
1005 if (indomain == NULL || !strlen(indomain) ||
1006 inmap == NULL || !strlen(inmap))
1007 return (YPERR_BADARGS);
1011 if (_yp_dobind(indomain, &ysd) != 0) {
1013 return (YPERR_DOMAIN);
1016 tv.tv_sec = _yplib_timeout;
1019 yprnk.domain = indomain;
1022 bzero((char *)(char *)&ypro, sizeof ypro);
1024 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
1025 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1026 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
1029 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
1032 if (r == RPC_PROCUNAVAIL) {
1034 return(YPERR_YPERR);
1037 if (r != RPC_SUCCESS) {
1038 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
1043 if (!(r = ypprot_err(ypro.stat))) {
1044 *outorder = ypro.ordernum;
1047 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
1053 yp_master(char *indomain, char *inmap, char **outname)
1055 struct dom_binding *ysd;
1056 struct ypresp_master yprm;
1057 struct ypreq_nokey yprnk;
1063 if (indomain == NULL || !strlen(indomain) ||
1064 inmap == NULL || !strlen(inmap))
1065 return (YPERR_BADARGS);
1068 if (_yp_dobind(indomain, &ysd) != 0) {
1070 return (YPERR_DOMAIN);
1073 tv.tv_sec = _yplib_timeout;
1076 yprnk.domain = indomain;
1079 bzero((char *)&yprm, sizeof yprm);
1081 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1082 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1083 (xdrproc_t)xdr_ypresp_master, &yprm, tv);
1084 if (r != RPC_SUCCESS) {
1085 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
1090 if (!(r = ypprot_err(yprm.stat))) {
1091 *outname = (char *)strdup(yprm.peer);
1094 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1100 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1102 struct dom_binding *ysd;
1103 struct ypresp_maplist ypml;
1109 if (indomain == NULL || !strlen(indomain))
1110 return (YPERR_BADARGS);
1114 if (_yp_dobind(indomain, &ysd) != 0) {
1116 return (YPERR_DOMAIN);
1119 tv.tv_sec = _yplib_timeout;
1122 bzero((char *)&ypml, sizeof ypml);
1124 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1125 (xdrproc_t)xdr_domainname, &indomain,
1126 (xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1127 if (r != RPC_SUCCESS) {
1128 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1132 if (!(r = ypprot_err(ypml.stat))) {
1133 *outmaplist = ypml.maps;
1136 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1142 yperr_string(int incode)
1144 static char err[80];
1150 return ("Request arguments bad");
1152 return ("RPC failure");
1154 return ("Can't bind to server which serves this domain");
1156 return ("No such map in server's domain");
1158 return ("No such key in map");
1160 return ("YP server error");
1162 return ("Local resource allocation failure");
1164 return ("No more records in map database");
1166 return ("Can't communicate with portmapper");
1168 return ("Can't communicate with ypbind");
1170 return ("Can't communicate with ypserv");
1172 return ("Local domain name not set");
1174 return ("Server data base is bad");
1176 return ("YP server version mismatch - server can't supply service.");
1178 return ("Access violation");
1180 return ("Database is busy");
1182 sprintf(err, "YP unknown error %d\n", incode);
1187 ypprot_err(unsigned int incode)
1193 return (YPERR_YPBIND);
1195 return (YPERR_NOMORE);
1199 return (YPERR_DOMAIN);
1203 return (YPERR_YPERR);
1205 return (YPERR_BADDB);
1207 return (YPERR_YPERR);
1209 return (YPERR_BADARGS);
1211 return (YPERR_VERS);
1213 return (YPERR_YPERR);
1217 _yp_check(char **dom)
1222 if (_yp_domain[0]=='\0')
1223 if (yp_get_default_domain_locked(&unused)) {
1231 if (yp_bind_locked(_yp_domain) == 0) {
1232 yp_unbind_locked(_yp_domain);