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 "namespace.h"
34 #include "reentrant.h"
35 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <arpa/inet.h>
47 #include <rpcsvc/yp.h>
48 #include "un-namespace.h"
49 #include "libc_private.h"
52 * We have to define these here due to clashes between yp_prot.h and
64 struct ypmatch_ent *ypc_next;
66 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
67 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
71 struct dom_binding *dom_pnext;
72 char dom_domain[YPMAXDOMAIN + 1];
73 struct sockaddr_in dom_server_addr;
74 u_short dom_server_port;
77 u_short dom_local_port; /* now I finally know what this is for. */
80 struct ypmatch_ent *cache;
85 #include <rpcsvc/ypclnt.h>
88 #define BINDINGDIR "/var/yp/binding"
90 #define MAX_RETRIES 20
92 bool_t xdr_ypresp_all_seq(XDR *xdrs, u_long *objp);
94 int (*ypresp_allfn)(unsigned long, char *, int, char *, int, void *);
97 static void _yp_unbind(struct dom_binding *);
98 struct dom_binding *_ypbindlist;
99 static char _yp_domain[MAXHOSTNAMELEN];
100 int _yplib_timeout = 20;
102 static mutex_t _ypmutex = MUTEX_INITIALIZER;
103 #define YPLOCK() mutex_lock(&_ypmutex);
104 #define YPUNLOCK() mutex_unlock(&_ypmutex);
108 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
109 struct ypmatch_ent *cur)
112 ypdb->cache = cur->ypc_next;
114 prev->ypc_next = cur->ypc_next;
117 free(cur->ypc_key.keydat_val);
118 free(cur->ypc_val.valdat_val);
121 ypdb->ypmatch_cachecnt--;
127 ypmatch_cache_flush(struct dom_binding *ypdb)
129 struct ypmatch_ent *n, *c = ypdb->cache;
133 ypmatch_cache_delete(ypdb, NULL, c);
141 ypmatch_cache_expire(struct dom_binding *ypdb)
143 struct ypmatch_ent *c = ypdb->cache;
144 struct ypmatch_ent *n, *p = NULL;
150 if (t >= c->ypc_expire_t) {
152 ypmatch_cache_delete(ypdb, p, c);
164 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
167 struct ypmatch_ent *new;
169 /* Do an expire run to maybe open up a slot. */
170 if (ypdb->ypmatch_cachecnt)
171 ypmatch_cache_expire(ypdb);
174 * If there are no slots free, then force an expire of
175 * the least recently used entry.
177 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
178 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
184 if (c->ypc_expire_t < oldest) {
185 oldest = c->ypc_expire_t;
194 ypmatch_cache_expire(ypdb);
197 new = malloc(sizeof(struct ypmatch_ent));
201 new->ypc_map = strdup(map);
202 if (new->ypc_map == NULL) {
206 new->ypc_key.keydat_val = malloc(key->keydat_len);
207 if (new->ypc_key.keydat_val == NULL) {
212 new->ypc_val.valdat_val = malloc(val->valdat_len);
213 if (new->ypc_val.valdat_val == NULL) {
214 free(new->ypc_val.valdat_val);
220 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
221 new->ypc_key.keydat_len = key->keydat_len;
222 new->ypc_val.valdat_len = val->valdat_len;
223 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
224 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
226 new->ypc_next = ypdb->cache;
229 ypdb->ypmatch_cachecnt++;
235 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
238 struct ypmatch_ent *c;
240 ypmatch_cache_expire(ypdb);
242 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
243 if (strcmp(map, c->ypc_map))
245 if (key->keydat_len != c->ypc_key.keydat_len)
247 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
255 val->valdat_len = c->ypc_val.valdat_len;
256 val->valdat_val = c->ypc_val.valdat_val;
263 ypbinderr_string(int incode)
270 return ("Internal ypbind error");
271 case YPBIND_ERR_NOSERV:
272 return ("Domain not bound");
273 case YPBIND_ERR_RESC:
274 return ("System resource allocation failure");
276 sprintf(err, "Unknown ypbind error: #%d\n", incode);
281 _yp_dobind(char *dom, struct dom_binding **ypdb)
283 static pid_t pid = -1;
284 char path[MAXPATHLEN];
285 struct dom_binding *ysd, *ysd2;
286 struct ypbind_resp ypbr;
288 struct sockaddr_in clnt_sin;
294 struct sockaddr_in check;
295 socklen_t checklen = sizeof(struct sockaddr_in);
297 /* Not allowed; bad doggie. Bad. */
298 if (strchr(dom, '/') != NULL)
299 return(YPERR_BADARGS);
302 if (!(pid == -1 || pid == gpid)) {
305 if (ysd->dom_client != NULL)
307 ysd2 = ysd->dom_pnext;
318 if (dom == NULL || strlen(dom) == 0)
319 return (YPERR_BADARGS);
321 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
322 if (strcmp(dom, ysd->dom_domain) == 0)
327 ysd = (struct dom_binding *)malloc(sizeof *ysd);
329 return (YPERR_RESRC);
330 bzero((char *)ysd, sizeof *ysd);
331 ysd->dom_socket = -1;
335 /* Check the socket -- may have been hosed by the caller. */
336 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
337 &checklen) == -1 || check.sin_family != AF_INET ||
338 check.sin_port != ysd->dom_local_port) {
339 /* Socket became bogus somehow... need to rebind. */
342 sock = ysd->dom_socket;
343 save = _dup(ysd->dom_socket);
344 if (ysd->dom_client != NULL)
345 clnt_destroy(ysd->dom_client);
347 ysd->dom_client = NULL;
348 sock = _dup2(save, sock);
355 if (retries > MAX_RETRIES) {
358 return(YPERR_YPBIND);
361 if (ysd->dom_vers == 0) {
363 * We're trying to make a new binding: zorch the
364 * existing handle now (if any).
366 if (ysd->dom_client != NULL) {
367 clnt_destroy(ysd->dom_client);
368 ysd->dom_client = NULL;
369 ysd->dom_socket = -1;
371 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
372 if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
373 /* no binding file, YP is dead. */
374 /* Try to bring it back to life. */
378 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
380 struct ypbind_resp ybr;
383 iov[0].iov_base = (caddr_t)&ypb_port;
384 iov[0].iov_len = sizeof ypb_port;
385 iov[1].iov_base = (caddr_t)&ybr;
386 iov[1].iov_len = sizeof ybr;
388 r = _readv(fd, iov, 2);
389 if (r != iov[0].iov_len + iov[1].iov_len) {
395 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
396 ysd->dom_server_addr.sin_family = AF_INET;
397 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
398 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
399 &ysd->dom_server_addr.sin_addr.s_addr,
400 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
401 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
402 &ysd->dom_server_addr.sin_port,
403 sizeof(ysd->dom_server_addr.sin_port));
405 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
409 /* no lock on binding file, YP is dead. */
410 /* Try to bring it back to life. */
417 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
419 * We're trying to make a new binding: zorch the
420 * existing handle now (if any).
422 if (ysd->dom_client != NULL) {
423 clnt_destroy(ysd->dom_client);
424 ysd->dom_client = NULL;
425 ysd->dom_socket = -1;
427 bzero((char *)&clnt_sin, sizeof clnt_sin);
428 clnt_sin.sin_family = AF_INET;
429 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
431 clnt_sock = RPC_ANYSOCK;
432 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
434 if (client == NULL) {
436 * These conditions indicate ypbind just isn't
437 * alive -- we probably don't want to shoot our
438 * mouth off in this case; instead generate error
439 * messages only for really exotic problems.
441 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
442 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
443 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
444 clnt_pcreateerror("clnttcp_create");
447 return (YPERR_YPBIND);
451 * Check the port number -- should be < IPPORT_RESERVED.
452 * If not, it's possible someone has registered a bogus
453 * ypbind with the portmapper and is trying to trick us.
455 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
457 clnt_destroy(client);
460 return(YPERR_YPBIND);
462 tv.tv_sec = _yplib_timeout/2;
464 r = clnt_call(client, YPBINDPROC_DOMAIN,
465 (xdrproc_t)xdr_domainname, &dom,
466 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
467 if (r != RPC_SUCCESS) {
468 clnt_destroy(client);
470 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
473 return(YPERR_YPBIND);
476 "YP: server for domain %s not responding, retrying\n", dom);
479 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
480 struct timespec time_to_sleep, time_remaining;
482 clnt_destroy(client);
485 time_to_sleep.tv_sec = _yplib_timeout/2;
486 time_to_sleep.tv_nsec = 0;
487 _nanosleep(&time_to_sleep,
492 clnt_destroy(client);
494 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
495 ysd->dom_server_addr.sin_family = AF_INET;
496 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
497 &ysd->dom_server_addr.sin_port,
498 sizeof(ysd->dom_server_addr.sin_port));
499 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
500 &ysd->dom_server_addr.sin_addr.s_addr,
501 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
504 * We could do a reserved port check here too, but this
505 * could pose compatibility problems. The local ypbind is
506 * supposed to decide whether or not to trust yp servers
507 * on insecure ports. For now, we trust its judgement.
509 ysd->dom_server_port =
510 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
512 ysd->dom_vers = YPVERS;
513 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
516 /* Don't rebuild the connection to the server unless we have to. */
517 if (ysd->dom_client == NULL) {
518 tv.tv_sec = _yplib_timeout/2;
520 ysd->dom_socket = RPC_ANYSOCK;
521 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
522 YPPROG, YPVERS, tv, &ysd->dom_socket, 65507, 65507);
523 if (ysd->dom_client == NULL) {
524 clnt_pcreateerror("clntudp_create");
528 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
529 perror("fcntl: F_SETFD");
531 * We want a port number associated with this socket
532 * so that we can check its authenticity later.
534 checklen = sizeof(struct sockaddr_in);
535 bzero((char *)&check, checklen);
536 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
537 check.sin_family = AF_INET;
538 if (!_getsockname(ysd->dom_socket,
539 (struct sockaddr *)&check, &checklen)) {
540 ysd->dom_local_port = check.sin_port;
542 clnt_destroy(ysd->dom_client);
545 return(YPERR_YPBIND);
550 ysd->dom_pnext = _ypbindlist;
555 * Set low retry timeout to realistically handle UDP packet
556 * loss for YP packet bursts.
560 clnt_control(ysd->dom_client, CLSET_RETRY_TIMEOUT, (char*)&tv);
568 _yp_unbind(struct dom_binding *ypb)
570 struct sockaddr_in check;
571 socklen_t checklen = sizeof(struct sockaddr_in);
573 if (ypb->dom_client != NULL) {
574 /* Check the socket -- may have been hosed by the caller. */
575 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
576 &checklen) == -1 || check.sin_family != AF_INET ||
577 check.sin_port != ypb->dom_local_port) {
580 sock = ypb->dom_socket;
581 save = _dup(ypb->dom_socket);
582 clnt_destroy(ypb->dom_client);
583 sock = _dup2(save, sock);
586 clnt_destroy(ypb->dom_client);
589 ypb->dom_client = NULL;
590 ypb->dom_socket = -1;
593 ypmatch_cache_flush(ypb);
598 yp_bind_locked(char *dom)
600 return (_yp_dobind(dom, NULL));
609 r = yp_bind_locked(dom);
615 yp_unbind_locked(char *dom)
617 struct dom_binding *ypb, *ypbp;
620 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
621 if (strcmp(dom, ypb->dom_domain) == 0) {
624 ypbp->dom_pnext = ypb->dom_pnext;
626 _ypbindlist = ypb->dom_pnext;
639 yp_unbind_locked(dom);
644 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
645 char **outval, int *outvallen)
647 struct dom_binding *ysd;
648 struct ypresp_val yprv;
650 struct ypreq_key yprk;
658 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
659 inmap == NULL || !strlen(inmap) ||
660 indomain == NULL || !strlen(indomain))
661 return (YPERR_BADARGS);
664 if (_yp_dobind(indomain, &ysd) != 0) {
666 return(YPERR_DOMAIN);
669 yprk.domain = indomain;
671 yprk.key.keydat_val = (char *)inkey;
672 yprk.key.keydat_len = inkeylen;
675 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
677 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
678 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
680 *outvallen = yprv.val.valdat_len;
681 *outval = (char *)malloc(*outvallen+1);
682 if (*outval == NULL) {
686 return (YPERR_RESRC);
688 bcopy(yprv.val.valdat_val, *outval, *outvallen);
689 (*outval)[*outvallen] = '\0';
697 if (retries > MAX_RETRIES) {
702 if (_yp_dobind(indomain, &ysd) != 0) {
704 return (YPERR_DOMAIN);
707 tv.tv_sec = _yplib_timeout;
710 bzero((char *)&yprv, sizeof yprv);
712 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
713 (xdrproc_t)xdr_ypreq_key, &yprk,
714 (xdrproc_t)xdr_ypresp_val, &yprv, tv);
715 if (r != RPC_SUCCESS) {
716 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
722 if (!(r = ypprot_err(yprv.stat))) {
723 *outvallen = yprv.val.valdat_len;
724 *outval = (char *)malloc(*outvallen+1);
725 if (*outval == NULL) {
728 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
730 return (YPERR_RESRC);
732 bcopy(yprv.val.valdat_val, *outval, *outvallen);
733 (*outval)[*outvallen] = '\0';
735 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
739 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
745 yp_get_default_domain_locked(char **domp)
748 if (_yp_domain[0] == '\0')
749 if (getdomainname(_yp_domain, sizeof _yp_domain))
750 return (YPERR_NODOM);
756 yp_get_default_domain(char **domp)
761 r = yp_get_default_domain_locked(domp);
767 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
768 char **outval, int *outvallen)
770 struct ypresp_key_val yprkv;
771 struct ypreq_nokey yprnk;
772 struct dom_binding *ysd;
778 if (indomain == NULL || !strlen(indomain) ||
779 inmap == NULL || !strlen(inmap))
780 return (YPERR_BADARGS);
782 *outkey = *outval = NULL;
783 *outkeylen = *outvallen = 0;
787 if (retries > MAX_RETRIES) {
792 if (_yp_dobind(indomain, &ysd) != 0) {
794 return (YPERR_DOMAIN);
797 tv.tv_sec = _yplib_timeout;
800 yprnk.domain = indomain;
802 bzero((char *)&yprkv, sizeof yprkv);
804 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
805 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
806 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
807 if (r != RPC_SUCCESS) {
808 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
813 if (!(r = ypprot_err(yprkv.stat))) {
814 *outkeylen = yprkv.key.keydat_len;
815 *outkey = (char *)malloc(*outkeylen+1);
816 if (*outkey == NULL) {
819 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
821 return (YPERR_RESRC);
823 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
824 (*outkey)[*outkeylen] = '\0';
825 *outvallen = yprkv.val.valdat_len;
826 *outval = (char *)malloc(*outvallen+1);
827 if (*outval == NULL) {
830 *outkeylen = *outvallen = 0;
831 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
833 return (YPERR_RESRC);
835 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
836 (*outval)[*outvallen] = '\0';
839 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
845 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
846 char **outkey, int *outkeylen, char **outval, int *outvallen)
848 struct ypresp_key_val yprkv;
849 struct ypreq_key yprk;
850 struct dom_binding *ysd;
856 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
857 inmap == NULL || !strlen(inmap) ||
858 indomain == NULL || !strlen(indomain))
859 return (YPERR_BADARGS);
861 *outkey = *outval = NULL;
862 *outkeylen = *outvallen = 0;
866 if (retries > MAX_RETRIES) {
871 if (_yp_dobind(indomain, &ysd) != 0) {
873 return (YPERR_DOMAIN);
876 tv.tv_sec = _yplib_timeout;
879 yprk.domain = indomain;
881 yprk.key.keydat_val = inkey;
882 yprk.key.keydat_len = inkeylen;
883 bzero((char *)&yprkv, sizeof yprkv);
885 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
886 (xdrproc_t)xdr_ypreq_key, &yprk,
887 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
888 if (r != RPC_SUCCESS) {
889 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
894 if (!(r = ypprot_err(yprkv.stat))) {
895 *outkeylen = yprkv.key.keydat_len;
896 *outkey = (char *)malloc(*outkeylen+1);
897 if (*outkey == NULL) {
900 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
902 return (YPERR_RESRC);
904 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
905 (*outkey)[*outkeylen] = '\0';
906 *outvallen = yprkv.val.valdat_len;
907 *outval = (char *)malloc(*outvallen+1);
908 if (*outval == NULL) {
911 *outkeylen = *outvallen = 0;
912 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
914 return (YPERR_RESRC);
916 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
917 (*outval)[*outvallen] = '\0';
920 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
926 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
928 struct ypreq_nokey yprnk;
929 struct dom_binding *ysd;
931 struct sockaddr_in clnt_sin;
933 u_long status, savstat;
938 if (indomain == NULL || !strlen(indomain) ||
939 inmap == NULL || !strlen(inmap))
940 return (YPERR_BADARGS);
944 if (retries > MAX_RETRIES) {
949 if (_yp_dobind(indomain, &ysd) != 0) {
951 return (YPERR_DOMAIN);
954 tv.tv_sec = _yplib_timeout;
957 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
959 clnt_sock = RPC_ANYSOCK;
960 clnt_sin = ysd->dom_server_addr;
961 clnt_sin.sin_port = 0;
962 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
965 printf("clnttcp_create failed\n");
969 yprnk.domain = indomain;
971 ypresp_allfn = incallback->foreach;
972 ypresp_data = (void *)incallback->data;
974 if (clnt_call(clnt, YPPROC_ALL,
975 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
976 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
977 clnt_perror(clnt, "yp_all: clnt_call");
986 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */
988 if (savstat != YP_NOMORE)
989 return (ypprot_err(savstat));
994 yp_order(char *indomain, char *inmap, int *outorder)
996 struct dom_binding *ysd;
997 struct ypresp_order ypro;
998 struct ypreq_nokey yprnk;
1004 if (indomain == NULL || !strlen(indomain) ||
1005 inmap == NULL || !strlen(inmap))
1006 return (YPERR_BADARGS);
1010 if (_yp_dobind(indomain, &ysd) != 0) {
1012 return (YPERR_DOMAIN);
1015 tv.tv_sec = _yplib_timeout;
1018 yprnk.domain = indomain;
1021 bzero((char *)(char *)&ypro, sizeof ypro);
1023 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
1024 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1025 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
1028 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
1031 if (r == RPC_PROCUNAVAIL) {
1033 return(YPERR_YPERR);
1036 if (r != RPC_SUCCESS) {
1037 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
1042 if (!(r = ypprot_err(ypro.stat))) {
1043 *outorder = ypro.ordernum;
1046 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
1052 yp_master(char *indomain, char *inmap, char **outname)
1054 struct dom_binding *ysd;
1055 struct ypresp_master yprm;
1056 struct ypreq_nokey yprnk;
1062 if (indomain == NULL || !strlen(indomain) ||
1063 inmap == NULL || !strlen(inmap))
1064 return (YPERR_BADARGS);
1067 if (_yp_dobind(indomain, &ysd) != 0) {
1069 return (YPERR_DOMAIN);
1072 tv.tv_sec = _yplib_timeout;
1075 yprnk.domain = indomain;
1078 bzero((char *)&yprm, sizeof yprm);
1080 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1081 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1082 (xdrproc_t)xdr_ypresp_master, &yprm, tv);
1083 if (r != RPC_SUCCESS) {
1084 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
1089 if (!(r = ypprot_err(yprm.stat))) {
1090 *outname = (char *)strdup(yprm.peer);
1093 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1099 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1101 struct dom_binding *ysd;
1102 struct ypresp_maplist ypml;
1108 if (indomain == NULL || !strlen(indomain))
1109 return (YPERR_BADARGS);
1113 if (_yp_dobind(indomain, &ysd) != 0) {
1115 return (YPERR_DOMAIN);
1118 tv.tv_sec = _yplib_timeout;
1121 bzero((char *)&ypml, sizeof ypml);
1123 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1124 (xdrproc_t)xdr_domainname, &indomain,
1125 (xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1126 if (r != RPC_SUCCESS) {
1127 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1131 if (!(r = ypprot_err(ypml.stat))) {
1132 *outmaplist = ypml.maps;
1135 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1141 yperr_string(int incode)
1143 static char err[80];
1149 return ("Request arguments bad");
1151 return ("RPC failure");
1153 return ("Can't bind to server which serves this domain");
1155 return ("No such map in server's domain");
1157 return ("No such key in map");
1159 return ("YP server error");
1161 return ("Local resource allocation failure");
1163 return ("No more records in map database");
1165 return ("Can't communicate with portmapper");
1167 return ("Can't communicate with ypbind");
1169 return ("Can't communicate with ypserv");
1171 return ("Local domain name not set");
1173 return ("Server data base is bad");
1175 return ("YP server version mismatch - server can't supply service.");
1177 return ("Access violation");
1179 return ("Database is busy");
1181 sprintf(err, "YP unknown error %d\n", incode);
1186 ypprot_err(unsigned int incode)
1192 return (YPERR_YPBIND);
1194 return (YPERR_NOMORE);
1198 return (YPERR_DOMAIN);
1202 return (YPERR_YPERR);
1204 return (YPERR_BADDB);
1206 return (YPERR_YPERR);
1208 return (YPERR_BADARGS);
1210 return (YPERR_VERS);
1212 return (YPERR_YPERR);
1216 _yp_check(char **dom)
1221 if (_yp_domain[0]=='\0')
1222 if (yp_get_default_domain_locked(&unused)) {
1230 if (yp_bind_locked(_yp_domain) == 0) {
1231 yp_unbind_locked(_yp_domain);