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 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include "reentrant.h"
38 #include <sys/param.h>
39 #include <sys/socket.h>
42 #include <arpa/inet.h>
50 #include <rpcsvc/yp.h>
51 #include "un-namespace.h"
52 #include "libc_private.h"
55 * We have to define these here due to clashes between yp_prot.h and
67 struct ypmatch_ent *ypc_next;
69 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
70 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
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;
80 u_short dom_local_port; /* now I finally know what this is for. */
83 struct ypmatch_ent *cache;
88 #include <rpcsvc/ypclnt.h>
91 #define BINDINGDIR "/var/yp/binding"
93 #define MAX_RETRIES 20
95 bool_t xdr_ypresp_all_seq(XDR *xdrs, u_long *objp);
97 int (*ypresp_allfn)();
100 static void _yp_unbind(struct dom_binding *);
101 struct dom_binding *_ypbindlist;
102 static char _yp_domain[MAXHOSTNAMELEN];
103 int _yplib_timeout = 20;
105 static mutex_t _ypmutex = MUTEX_INITIALIZER;
106 #define YPLOCK() mutex_lock(&_ypmutex);
107 #define YPUNLOCK() mutex_unlock(&_ypmutex);
111 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
112 struct ypmatch_ent *cur)
115 ypdb->cache = cur->ypc_next;
117 prev->ypc_next = cur->ypc_next;
120 free(cur->ypc_key.keydat_val);
121 free(cur->ypc_val.valdat_val);
124 ypdb->ypmatch_cachecnt--;
130 ypmatch_cache_flush(struct dom_binding *ypdb)
132 struct ypmatch_ent *n, *c = ypdb->cache;
136 ypmatch_cache_delete(ypdb, NULL, c);
144 ypmatch_cache_expire(struct dom_binding *ypdb)
146 struct ypmatch_ent *c = ypdb->cache;
147 struct ypmatch_ent *n, *p = NULL;
153 if (t >= c->ypc_expire_t) {
155 ypmatch_cache_delete(ypdb, p, c);
167 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
170 struct ypmatch_ent *new;
172 /* Do an expire run to maybe open up a slot. */
173 if (ypdb->ypmatch_cachecnt)
174 ypmatch_cache_expire(ypdb);
177 * If there are no slots free, then force an expire of
178 * the least recently used entry.
180 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
181 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
187 if (c->ypc_expire_t < oldest) {
188 oldest = c->ypc_expire_t;
197 ypmatch_cache_expire(ypdb);
200 new = malloc(sizeof(struct ypmatch_ent));
204 new->ypc_map = strdup(map);
205 if (new->ypc_map == NULL) {
209 new->ypc_key.keydat_val = malloc(key->keydat_len);
210 if (new->ypc_key.keydat_val == NULL) {
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);
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);
229 new->ypc_next = ypdb->cache;
232 ypdb->ypmatch_cachecnt++;
238 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
241 struct ypmatch_ent *c;
243 ypmatch_cache_expire(ypdb);
245 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
246 if (strcmp(map, c->ypc_map))
248 if (key->keydat_len != c->ypc_key.keydat_len)
250 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
258 val->valdat_len = c->ypc_val.valdat_len;
259 val->valdat_val = c->ypc_val.valdat_val;
266 ypbinderr_string(int incode)
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");
279 sprintf(err, "Unknown ypbind error: #%d\n", incode);
284 _yp_dobind(char *dom, struct dom_binding **ypdb)
286 static pid_t pid = -1;
287 char path[MAXPATHLEN];
288 struct dom_binding *ysd, *ysd2;
289 struct ypbind_resp ypbr;
291 struct sockaddr_in clnt_sin;
297 struct sockaddr_in check;
298 socklen_t checklen = sizeof(struct sockaddr_in);
300 /* Not allowed; bad doggie. Bad. */
301 if (strchr(dom, '/') != NULL)
302 return(YPERR_BADARGS);
305 if (!(pid == -1 || pid == gpid)) {
308 if (ysd->dom_client != NULL)
310 ysd2 = ysd->dom_pnext;
321 if (dom == NULL || strlen(dom) == 0)
322 return (YPERR_BADARGS);
324 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
325 if (strcmp(dom, ysd->dom_domain) == 0)
330 ysd = (struct dom_binding *)malloc(sizeof *ysd);
332 return (YPERR_RESRC);
333 bzero((char *)ysd, sizeof *ysd);
334 ysd->dom_socket = -1;
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. */
345 sock = ysd->dom_socket;
346 save = _dup(ysd->dom_socket);
347 if (ysd->dom_client != NULL)
348 clnt_destroy(ysd->dom_client);
350 ysd->dom_client = NULL;
351 sock = _dup2(save, sock);
358 if (retries > MAX_RETRIES) {
361 return(YPERR_YPBIND);
364 if (ysd->dom_vers == 0) {
366 * We're trying to make a new binding: zorch the
367 * existing handle now (if any).
369 if (ysd->dom_client != NULL) {
370 clnt_destroy(ysd->dom_client);
371 ysd->dom_client = NULL;
372 ysd->dom_socket = -1;
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. */
381 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
383 struct ypbind_resp ybr;
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;
391 r = _readv(fd, iov, 2);
392 if (r != iov[0].iov_len + iov[1].iov_len) {
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));
408 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
412 /* no lock on binding file, YP is dead. */
413 /* Try to bring it back to life. */
420 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
422 * We're trying to make a new binding: zorch the
423 * existing handle now (if any).
425 if (ysd->dom_client != NULL) {
426 clnt_destroy(ysd->dom_client);
427 ysd->dom_client = NULL;
428 ysd->dom_socket = -1;
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);
434 clnt_sock = RPC_ANYSOCK;
435 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
437 if (client == NULL) {
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.
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");
450 return (YPERR_YPBIND);
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.
458 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
460 clnt_destroy(client);
463 return(YPERR_YPBIND);
465 tv.tv_sec = _yplib_timeout/2;
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);
473 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
476 return(YPERR_YPBIND);
479 "YP: server for domain %s not responding, retrying\n", dom);
482 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
483 struct timespec time_to_sleep, time_remaining;
485 clnt_destroy(client);
488 time_to_sleep.tv_sec = _yplib_timeout/2;
489 time_to_sleep.tv_nsec = 0;
490 _nanosleep(&time_to_sleep,
495 clnt_destroy(client);
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));
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.
512 ysd->dom_server_port =
513 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
515 ysd->dom_vers = YPVERS;
516 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
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;
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");
531 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
532 perror("fcntl: F_SETFD");
534 * We want a port number associated with this socket
535 * so that we can check its authenticity later.
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;
545 clnt_destroy(ysd->dom_client);
548 return(YPERR_YPBIND);
553 ysd->dom_pnext = _ypbindlist;
558 * Set low retry timeout to realistically handle UDP packet
559 * loss for YP packet bursts.
563 clnt_control(ysd->dom_client, CLSET_RETRY_TIMEOUT, (char*)&tv);
571 _yp_unbind(struct dom_binding *ypb)
573 struct sockaddr_in check;
574 socklen_t checklen = sizeof(struct sockaddr_in);
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) {
583 sock = ypb->dom_socket;
584 save = _dup(ypb->dom_socket);
585 clnt_destroy(ypb->dom_client);
586 sock = _dup2(save, sock);
589 clnt_destroy(ypb->dom_client);
592 ypb->dom_client = NULL;
593 ypb->dom_socket = -1;
596 ypmatch_cache_flush(ypb);
601 yp_bind_locked(char *dom)
603 return (_yp_dobind(dom, NULL));
612 r = yp_bind_locked(dom);
618 yp_unbind_locked(char *dom)
620 struct dom_binding *ypb, *ypbp;
623 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
624 if (strcmp(dom, ypb->dom_domain) == 0) {
627 ypbp->dom_pnext = ypb->dom_pnext;
629 _ypbindlist = ypb->dom_pnext;
642 yp_unbind_locked(dom);
647 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
648 char **outval, int *outvallen)
650 struct dom_binding *ysd;
651 struct ypresp_val yprv;
653 struct ypreq_key yprk;
661 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
662 inmap == NULL || !strlen(inmap) ||
663 indomain == NULL || !strlen(indomain))
664 return (YPERR_BADARGS);
667 if (_yp_dobind(indomain, &ysd) != 0) {
669 return(YPERR_DOMAIN);
672 yprk.domain = indomain;
674 yprk.key.keydat_val = (char *)inkey;
675 yprk.key.keydat_len = inkeylen;
678 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
680 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
681 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
683 *outvallen = yprv.val.valdat_len;
684 *outval = (char *)malloc(*outvallen+1);
685 if (*outval == NULL) {
689 return (YPERR_RESRC);
691 bcopy(yprv.val.valdat_val, *outval, *outvallen);
692 (*outval)[*outvallen] = '\0';
700 if (retries > MAX_RETRIES) {
705 if (_yp_dobind(indomain, &ysd) != 0) {
707 return (YPERR_DOMAIN);
710 tv.tv_sec = _yplib_timeout;
713 bzero((char *)&yprv, sizeof yprv);
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");
725 if (!(r = ypprot_err(yprv.stat))) {
726 *outvallen = yprv.val.valdat_len;
727 *outval = (char *)malloc(*outvallen+1);
728 if (*outval == NULL) {
731 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
733 return (YPERR_RESRC);
735 bcopy(yprv.val.valdat_val, *outval, *outvallen);
736 (*outval)[*outvallen] = '\0';
738 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
742 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
748 yp_get_default_domain_locked(char **domp)
751 if (_yp_domain[0] == '\0')
752 if (getdomainname(_yp_domain, sizeof _yp_domain))
753 return (YPERR_NODOM);
759 yp_get_default_domain(char **domp)
764 r = yp_get_default_domain_locked(domp);
770 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
771 char **outval, int *outvallen)
773 struct ypresp_key_val yprkv;
774 struct ypreq_nokey yprnk;
775 struct dom_binding *ysd;
781 if (indomain == NULL || !strlen(indomain) ||
782 inmap == NULL || !strlen(inmap))
783 return (YPERR_BADARGS);
785 *outkey = *outval = NULL;
786 *outkeylen = *outvallen = 0;
790 if (retries > MAX_RETRIES) {
795 if (_yp_dobind(indomain, &ysd) != 0) {
797 return (YPERR_DOMAIN);
800 tv.tv_sec = _yplib_timeout;
803 yprnk.domain = indomain;
805 bzero((char *)&yprkv, sizeof yprkv);
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");
816 if (!(r = ypprot_err(yprkv.stat))) {
817 *outkeylen = yprkv.key.keydat_len;
818 *outkey = (char *)malloc(*outkeylen+1);
819 if (*outkey == NULL) {
822 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
824 return (YPERR_RESRC);
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) {
833 *outkeylen = *outvallen = 0;
834 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
836 return (YPERR_RESRC);
838 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
839 (*outval)[*outvallen] = '\0';
842 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
848 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
849 char **outkey, int *outkeylen, char **outval, int *outvallen)
851 struct ypresp_key_val yprkv;
852 struct ypreq_key yprk;
853 struct dom_binding *ysd;
859 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
860 inmap == NULL || !strlen(inmap) ||
861 indomain == NULL || !strlen(indomain))
862 return (YPERR_BADARGS);
864 *outkey = *outval = NULL;
865 *outkeylen = *outvallen = 0;
869 if (retries > MAX_RETRIES) {
874 if (_yp_dobind(indomain, &ysd) != 0) {
876 return (YPERR_DOMAIN);
879 tv.tv_sec = _yplib_timeout;
882 yprk.domain = indomain;
884 yprk.key.keydat_val = inkey;
885 yprk.key.keydat_len = inkeylen;
886 bzero((char *)&yprkv, sizeof yprkv);
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");
897 if (!(r = ypprot_err(yprkv.stat))) {
898 *outkeylen = yprkv.key.keydat_len;
899 *outkey = (char *)malloc(*outkeylen+1);
900 if (*outkey == NULL) {
903 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
905 return (YPERR_RESRC);
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) {
914 *outkeylen = *outvallen = 0;
915 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
917 return (YPERR_RESRC);
919 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
920 (*outval)[*outvallen] = '\0';
923 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
929 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
931 struct ypreq_nokey yprnk;
932 struct dom_binding *ysd;
934 struct sockaddr_in clnt_sin;
936 u_long status, savstat;
941 if (indomain == NULL || !strlen(indomain) ||
942 inmap == NULL || !strlen(inmap))
943 return (YPERR_BADARGS);
947 if (retries > MAX_RETRIES) {
952 if (_yp_dobind(indomain, &ysd) != 0) {
954 return (YPERR_DOMAIN);
957 tv.tv_sec = _yplib_timeout;
960 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
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);
968 printf("clnttcp_create failed\n");
972 yprnk.domain = indomain;
974 ypresp_allfn = incallback->foreach;
975 ypresp_data = (void *)incallback->data;
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");
989 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */
991 if (savstat != YP_NOMORE)
992 return (ypprot_err(savstat));
997 yp_order(char *indomain, char *inmap, int *outorder)
999 struct dom_binding *ysd;
1000 struct ypresp_order ypro;
1001 struct ypreq_nokey yprnk;
1007 if (indomain == NULL || !strlen(indomain) ||
1008 inmap == NULL || !strlen(inmap))
1009 return (YPERR_BADARGS);
1013 if (_yp_dobind(indomain, &ysd) != 0) {
1015 return (YPERR_DOMAIN);
1018 tv.tv_sec = _yplib_timeout;
1021 yprnk.domain = indomain;
1024 bzero((char *)(char *)&ypro, sizeof ypro);
1026 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
1027 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1028 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
1031 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
1034 if (r == RPC_PROCUNAVAIL) {
1036 return(YPERR_YPERR);
1039 if (r != RPC_SUCCESS) {
1040 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
1045 if (!(r = ypprot_err(ypro.stat))) {
1046 *outorder = ypro.ordernum;
1049 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
1055 yp_master(char *indomain, char *inmap, char **outname)
1057 struct dom_binding *ysd;
1058 struct ypresp_master yprm;
1059 struct ypreq_nokey yprnk;
1065 if (indomain == NULL || !strlen(indomain) ||
1066 inmap == NULL || !strlen(inmap))
1067 return (YPERR_BADARGS);
1070 if (_yp_dobind(indomain, &ysd) != 0) {
1072 return (YPERR_DOMAIN);
1075 tv.tv_sec = _yplib_timeout;
1078 yprnk.domain = indomain;
1081 bzero((char *)&yprm, sizeof yprm);
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");
1092 if (!(r = ypprot_err(yprm.stat))) {
1093 *outname = (char *)strdup(yprm.peer);
1096 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1102 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1104 struct dom_binding *ysd;
1105 struct ypresp_maplist ypml;
1111 if (indomain == NULL || !strlen(indomain))
1112 return (YPERR_BADARGS);
1116 if (_yp_dobind(indomain, &ysd) != 0) {
1118 return (YPERR_DOMAIN);
1121 tv.tv_sec = _yplib_timeout;
1124 bzero((char *)&ypml, sizeof ypml);
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");
1134 if (!(r = ypprot_err(ypml.stat))) {
1135 *outmaplist = ypml.maps;
1138 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1144 yperr_string(int incode)
1146 static char err[80];
1152 return ("Request arguments bad");
1154 return ("RPC failure");
1156 return ("Can't bind to server which serves this domain");
1158 return ("No such map in server's domain");
1160 return ("No such key in map");
1162 return ("YP server error");
1164 return ("Local resource allocation failure");
1166 return ("No more records in map database");
1168 return ("Can't communicate with portmapper");
1170 return ("Can't communicate with ypbind");
1172 return ("Can't communicate with ypserv");
1174 return ("Local domain name not set");
1176 return ("Server data base is bad");
1178 return ("YP server version mismatch - server can't supply service.");
1180 return ("Access violation");
1182 return ("Database is busy");
1184 sprintf(err, "YP unknown error %d\n", incode);
1189 ypprot_err(unsigned int incode)
1195 return (YPERR_YPBIND);
1197 return (YPERR_NOMORE);
1201 return (YPERR_DOMAIN);
1205 return (YPERR_YPERR);
1207 return (YPERR_BADDB);
1209 return (YPERR_YPERR);
1211 return (YPERR_BADARGS);
1213 return (YPERR_VERS);
1215 return (YPERR_YPERR);
1219 _yp_check(char **dom)
1224 if (_yp_domain[0]=='\0')
1225 if (yp_get_default_domain_locked(&unused)) {
1233 if (yp_bind_locked(_yp_domain) == 0) {
1234 yp_unbind_locked(_yp_domain);