2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "namespace.h"
35 #include "reentrant.h"
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <arpa/inet.h>
49 #include <rpcsvc/yp.h>
50 #include "un-namespace.h"
51 #include "libc_private.h"
54 * We have to define these here due to clashes between yp_prot.h and
66 struct ypmatch_ent *ypc_next;
68 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
69 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
73 struct dom_binding *dom_pnext;
74 char dom_domain[YPMAXDOMAIN + 1];
75 struct sockaddr_in dom_server_addr;
76 u_short dom_server_port;
79 u_short dom_local_port; /* now I finally know what this is for. */
82 struct ypmatch_ent *cache;
87 #include <rpcsvc/ypclnt.h>
90 #define BINDINGDIR "/var/yp/binding"
92 #define MAX_RETRIES 20
94 extern bool_t xdr_domainname(), xdr_ypbind_resp();
95 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
96 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
97 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
98 extern bool_t xdr_ypresp_master();
100 int (*ypresp_allfn)();
103 static void _yp_unbind(struct dom_binding *);
104 struct dom_binding *_ypbindlist;
105 static char _yp_domain[MAXHOSTNAMELEN];
106 int _yplib_timeout = 20;
108 static mutex_t _ypmutex = MUTEX_INITIALIZER;
109 #define YPLOCK() mutex_lock(&_ypmutex);
110 #define YPUNLOCK() mutex_unlock(&_ypmutex);
114 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
115 struct ypmatch_ent *cur)
118 ypdb->cache = cur->ypc_next;
120 prev->ypc_next = cur->ypc_next;
123 free(cur->ypc_key.keydat_val);
124 free(cur->ypc_val.valdat_val);
127 ypdb->ypmatch_cachecnt--;
133 ypmatch_cache_flush(struct dom_binding *ypdb)
135 struct ypmatch_ent *n, *c = ypdb->cache;
139 ypmatch_cache_delete(ypdb, NULL, c);
147 ypmatch_cache_expire(struct dom_binding *ypdb)
149 struct ypmatch_ent *c = ypdb->cache;
150 struct ypmatch_ent *n, *p = NULL;
156 if (t >= c->ypc_expire_t) {
158 ypmatch_cache_delete(ypdb, p, c);
170 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
173 struct ypmatch_ent *new;
175 /* Do an expire run to maybe open up a slot. */
176 if (ypdb->ypmatch_cachecnt)
177 ypmatch_cache_expire(ypdb);
180 * If there are no slots free, then force an expire of
181 * the least recently used entry.
183 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
184 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
190 if (c->ypc_expire_t < oldest) {
191 oldest = c->ypc_expire_t;
200 ypmatch_cache_expire(ypdb);
203 new = malloc(sizeof(struct ypmatch_ent));
207 new->ypc_map = strdup(map);
208 if (new->ypc_map == NULL) {
212 new->ypc_key.keydat_val = malloc(key->keydat_len);
213 if (new->ypc_key.keydat_val == NULL) {
218 new->ypc_val.valdat_val = malloc(val->valdat_len);
219 if (new->ypc_val.valdat_val == NULL) {
220 free(new->ypc_val.valdat_val);
226 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
227 new->ypc_key.keydat_len = key->keydat_len;
228 new->ypc_val.valdat_len = val->valdat_len;
229 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
230 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
232 new->ypc_next = ypdb->cache;
235 ypdb->ypmatch_cachecnt++;
241 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
244 struct ypmatch_ent *c;
246 ypmatch_cache_expire(ypdb);
248 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
249 if (strcmp(map, c->ypc_map))
251 if (key->keydat_len != c->ypc_key.keydat_len)
253 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
261 val->valdat_len = c->ypc_val.valdat_len;
262 val->valdat_val = c->ypc_val.valdat_val;
269 ypbinderr_string(int incode)
276 return ("Internal ypbind error");
277 case YPBIND_ERR_NOSERV:
278 return ("Domain not bound");
279 case YPBIND_ERR_RESC:
280 return ("System resource allocation failure");
282 sprintf(err, "Unknown ypbind error: #%d\n", incode);
287 _yp_dobind(char *dom, struct dom_binding **ypdb)
289 static pid_t pid = -1;
290 char path[MAXPATHLEN];
291 struct dom_binding *ysd, *ysd2;
292 struct ypbind_resp ypbr;
294 struct sockaddr_in clnt_sin;
300 struct sockaddr_in check;
301 socklen_t checklen = sizeof(struct sockaddr_in);
303 /* Not allowed; bad doggie. Bad. */
304 if (strchr(dom, '/') != NULL)
305 return(YPERR_BADARGS);
308 if (!(pid == -1 || pid == gpid)) {
311 if (ysd->dom_client != NULL)
313 ysd2 = ysd->dom_pnext;
324 if (dom == NULL || strlen(dom) == 0)
325 return (YPERR_BADARGS);
327 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
328 if (strcmp(dom, ysd->dom_domain) == 0)
333 ysd = (struct dom_binding *)malloc(sizeof *ysd);
334 bzero((char *)ysd, sizeof *ysd);
335 ysd->dom_socket = -1;
339 /* Check the socket -- may have been hosed by the caller. */
340 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
341 &checklen) == -1 || check.sin_family != AF_INET ||
342 check.sin_port != ysd->dom_local_port) {
343 /* Socket became bogus somehow... need to rebind. */
346 sock = ysd->dom_socket;
347 save = _dup(ysd->dom_socket);
348 if (ysd->dom_client != NULL)
349 clnt_destroy(ysd->dom_client);
351 ysd->dom_client = NULL;
352 sock = _dup2(save, sock);
359 if (retries > MAX_RETRIES) {
362 return(YPERR_YPBIND);
365 if (ysd->dom_vers == 0) {
367 * We're trying to make a new binding: zorch the
368 * existing handle now (if any).
370 if (ysd->dom_client != NULL) {
371 clnt_destroy(ysd->dom_client);
372 ysd->dom_client = NULL;
373 ysd->dom_socket = -1;
375 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
376 if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
377 /* no binding file, YP is dead. */
378 /* Try to bring it back to life. */
382 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
384 struct ypbind_resp ybr;
387 iov[0].iov_base = (caddr_t)&ypb_port;
388 iov[0].iov_len = sizeof ypb_port;
389 iov[1].iov_base = (caddr_t)&ybr;
390 iov[1].iov_len = sizeof ybr;
392 r = _readv(fd, iov, 2);
393 if (r != iov[0].iov_len + iov[1].iov_len) {
399 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
400 ysd->dom_server_addr.sin_family = AF_INET;
401 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
402 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
403 &ysd->dom_server_addr.sin_addr.s_addr,
404 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
405 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
406 &ysd->dom_server_addr.sin_port,
407 sizeof(ysd->dom_server_addr.sin_port));
409 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
413 /* no lock on binding file, YP is dead. */
414 /* Try to bring it back to life. */
421 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
423 * We're trying to make a new binding: zorch the
424 * existing handle now (if any).
426 if (ysd->dom_client != NULL) {
427 clnt_destroy(ysd->dom_client);
428 ysd->dom_client = NULL;
429 ysd->dom_socket = -1;
431 bzero((char *)&clnt_sin, sizeof clnt_sin);
432 clnt_sin.sin_family = AF_INET;
433 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
435 clnt_sock = RPC_ANYSOCK;
436 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
438 if (client == NULL) {
440 * These conditions indicate ypbind just isn't
441 * alive -- we probably don't want to shoot our
442 * mouth off in this case; instead generate error
443 * messages only for really exotic problems.
445 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
446 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
447 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
448 clnt_pcreateerror("clnttcp_create");
451 return (YPERR_YPBIND);
455 * Check the port number -- should be < IPPORT_RESERVED.
456 * If not, it's possible someone has registered a bogus
457 * ypbind with the portmapper and is trying to trick us.
459 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
461 clnt_destroy(client);
464 return(YPERR_YPBIND);
466 tv.tv_sec = _yplib_timeout/2;
468 r = clnt_call(client, YPBINDPROC_DOMAIN,
469 (xdrproc_t)xdr_domainname, &dom,
470 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
471 if (r != RPC_SUCCESS) {
472 clnt_destroy(client);
474 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
477 return(YPERR_YPBIND);
480 "YP: server for domain %s not responding, retrying\n", dom);
483 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
484 struct timespec time_to_sleep, time_remaining;
486 clnt_destroy(client);
489 time_to_sleep.tv_sec = _yplib_timeout/2;
490 time_to_sleep.tv_nsec = 0;
491 _nanosleep(&time_to_sleep,
496 clnt_destroy(client);
498 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
499 ysd->dom_server_addr.sin_family = AF_INET;
500 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
501 &ysd->dom_server_addr.sin_port,
502 sizeof(ysd->dom_server_addr.sin_port));
503 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
504 &ysd->dom_server_addr.sin_addr.s_addr,
505 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
508 * We could do a reserved port check here too, but this
509 * could pose compatibility problems. The local ypbind is
510 * supposed to decide whether or not to trust yp servers
511 * on insecure ports. For now, we trust its judgement.
513 ysd->dom_server_port =
514 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
516 ysd->dom_vers = YPVERS;
517 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
520 /* Don't rebuild the connection to the server unless we have to. */
521 if (ysd->dom_client == NULL) {
522 tv.tv_sec = _yplib_timeout/2;
524 ysd->dom_socket = RPC_ANYSOCK;
525 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
526 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
527 if (ysd->dom_client == NULL) {
528 clnt_pcreateerror("clntudp_create");
532 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
533 perror("fcntl: F_SETFD");
535 * We want a port number associated with this socket
536 * so that we can check its authenticity later.
538 checklen = sizeof(struct sockaddr_in);
539 bzero((char *)&check, checklen);
540 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
541 check.sin_family = AF_INET;
542 if (!_getsockname(ysd->dom_socket,
543 (struct sockaddr *)&check, &checklen)) {
544 ysd->dom_local_port = check.sin_port;
546 clnt_destroy(ysd->dom_client);
549 return(YPERR_YPBIND);
554 ysd->dom_pnext = _ypbindlist;
559 * Set low retry timeout to realistically handle UDP packet
560 * loss for YP packet bursts.
564 clnt_control(ysd->dom_client, CLSET_RETRY_TIMEOUT, (char*)&tv);
572 _yp_unbind(struct dom_binding *ypb)
574 struct sockaddr_in check;
575 socklen_t checklen = sizeof(struct sockaddr_in);
577 if (ypb->dom_client != NULL) {
578 /* Check the socket -- may have been hosed by the caller. */
579 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
580 &checklen) == -1 || check.sin_family != AF_INET ||
581 check.sin_port != ypb->dom_local_port) {
584 sock = ypb->dom_socket;
585 save = _dup(ypb->dom_socket);
586 clnt_destroy(ypb->dom_client);
587 sock = _dup2(save, sock);
590 clnt_destroy(ypb->dom_client);
593 ypb->dom_client = NULL;
594 ypb->dom_socket = -1;
597 ypmatch_cache_flush(ypb);
602 yp_bind_locked(char *dom)
604 return (_yp_dobind(dom, NULL));
613 r = yp_bind_locked(dom);
619 yp_unbind_locked(char *dom)
621 struct dom_binding *ypb, *ypbp;
624 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
625 if (strcmp(dom, ypb->dom_domain) == 0) {
628 ypbp->dom_pnext = ypb->dom_pnext;
630 _ypbindlist = ypb->dom_pnext;
643 yp_unbind_locked(dom);
648 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
649 char **outval, int *outvallen)
651 struct dom_binding *ysd;
652 struct ypresp_val yprv;
654 struct ypreq_key yprk;
662 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
663 inmap == NULL || !strlen(inmap) ||
664 indomain == NULL || !strlen(indomain))
665 return (YPERR_BADARGS);
668 if (_yp_dobind(indomain, &ysd) != 0) {
670 return(YPERR_DOMAIN);
673 yprk.domain = indomain;
675 yprk.key.keydat_val = (char *)inkey;
676 yprk.key.keydat_len = inkeylen;
679 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
681 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
682 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
684 *outvallen = yprv.val.valdat_len;
685 *outval = (char *)malloc(*outvallen+1);
686 bcopy(yprv.val.valdat_val, *outval, *outvallen);
687 (*outval)[*outvallen] = '\0';
694 if (retries > MAX_RETRIES) {
699 if (_yp_dobind(indomain, &ysd) != 0) {
701 return (YPERR_DOMAIN);
704 tv.tv_sec = _yplib_timeout;
707 bzero((char *)&yprv, sizeof yprv);
709 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
710 (xdrproc_t)xdr_ypreq_key, &yprk,
711 (xdrproc_t)xdr_ypresp_val, &yprv, tv);
712 if (r != RPC_SUCCESS) {
713 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
719 if (!(r = ypprot_err(yprv.stat))) {
720 *outvallen = yprv.val.valdat_len;
721 *outval = (char *)malloc(*outvallen+1);
722 bcopy(yprv.val.valdat_val, *outval, *outvallen);
723 (*outval)[*outvallen] = '\0';
725 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
729 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
735 yp_get_default_domain_locked(char **domp)
738 if (_yp_domain[0] == '\0')
739 if (getdomainname(_yp_domain, sizeof _yp_domain))
740 return (YPERR_NODOM);
746 yp_get_default_domain(char **domp)
751 r = yp_get_default_domain_locked(domp);
757 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
758 char **outval, int *outvallen)
760 struct ypresp_key_val yprkv;
761 struct ypreq_nokey yprnk;
762 struct dom_binding *ysd;
768 if (indomain == NULL || !strlen(indomain) ||
769 inmap == NULL || !strlen(inmap))
770 return (YPERR_BADARGS);
772 *outkey = *outval = NULL;
773 *outkeylen = *outvallen = 0;
777 if (retries > MAX_RETRIES) {
782 if (_yp_dobind(indomain, &ysd) != 0) {
784 return (YPERR_DOMAIN);
787 tv.tv_sec = _yplib_timeout;
790 yprnk.domain = indomain;
792 bzero((char *)&yprkv, sizeof yprkv);
794 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
795 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
796 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
797 if (r != RPC_SUCCESS) {
798 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
803 if (!(r = ypprot_err(yprkv.stat))) {
804 *outkeylen = yprkv.key.keydat_len;
805 *outkey = (char *)malloc(*outkeylen+1);
806 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
807 (*outkey)[*outkeylen] = '\0';
808 *outvallen = yprkv.val.valdat_len;
809 *outval = (char *)malloc(*outvallen+1);
810 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
811 (*outval)[*outvallen] = '\0';
814 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
820 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
821 char **outkey, int *outkeylen, char **outval, int *outvallen)
823 struct ypresp_key_val yprkv;
824 struct ypreq_key yprk;
825 struct dom_binding *ysd;
831 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
832 inmap == NULL || !strlen(inmap) ||
833 indomain == NULL || !strlen(indomain))
834 return (YPERR_BADARGS);
836 *outkey = *outval = NULL;
837 *outkeylen = *outvallen = 0;
841 if (retries > MAX_RETRIES) {
846 if (_yp_dobind(indomain, &ysd) != 0) {
848 return (YPERR_DOMAIN);
851 tv.tv_sec = _yplib_timeout;
854 yprk.domain = indomain;
856 yprk.key.keydat_val = inkey;
857 yprk.key.keydat_len = inkeylen;
858 bzero((char *)&yprkv, sizeof yprkv);
860 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
861 (xdrproc_t)xdr_ypreq_key, &yprk,
862 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
863 if (r != RPC_SUCCESS) {
864 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
869 if (!(r = ypprot_err(yprkv.stat))) {
870 *outkeylen = yprkv.key.keydat_len;
871 *outkey = (char *)malloc(*outkeylen+1);
872 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
873 (*outkey)[*outkeylen] = '\0';
874 *outvallen = yprkv.val.valdat_len;
875 *outval = (char *)malloc(*outvallen+1);
876 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
877 (*outval)[*outvallen] = '\0';
880 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
886 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
888 struct ypreq_nokey yprnk;
889 struct dom_binding *ysd;
891 struct sockaddr_in clnt_sin;
893 u_long status, savstat;
898 if (indomain == NULL || !strlen(indomain) ||
899 inmap == NULL || !strlen(inmap))
900 return (YPERR_BADARGS);
904 if (retries > MAX_RETRIES) {
909 if (_yp_dobind(indomain, &ysd) != 0) {
911 return (YPERR_DOMAIN);
914 tv.tv_sec = _yplib_timeout;
917 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
919 clnt_sock = RPC_ANYSOCK;
920 clnt_sin = ysd->dom_server_addr;
921 clnt_sin.sin_port = 0;
922 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
925 printf("clnttcp_create failed\n");
929 yprnk.domain = indomain;
931 ypresp_allfn = incallback->foreach;
932 ypresp_data = (void *)incallback->data;
934 if (clnt_call(clnt, YPPROC_ALL,
935 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
936 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
937 clnt_perror(clnt, "yp_all: clnt_call");
946 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */
948 if (savstat != YP_NOMORE)
949 return (ypprot_err(savstat));
954 yp_order(char *indomain, char *inmap, int *outorder)
956 struct dom_binding *ysd;
957 struct ypresp_order ypro;
958 struct ypreq_nokey yprnk;
964 if (indomain == NULL || !strlen(indomain) ||
965 inmap == NULL || !strlen(inmap))
966 return (YPERR_BADARGS);
970 if (_yp_dobind(indomain, &ysd) != 0) {
972 return (YPERR_DOMAIN);
975 tv.tv_sec = _yplib_timeout;
978 yprnk.domain = indomain;
981 bzero((char *)(char *)&ypro, sizeof ypro);
983 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
984 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
985 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
988 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
991 if (r == RPC_PROCUNAVAIL) {
996 if (r != RPC_SUCCESS) {
997 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
1002 if (!(r = ypprot_err(ypro.stat))) {
1003 *outorder = ypro.ordernum;
1006 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
1012 yp_master(char *indomain, char *inmap, char **outname)
1014 struct dom_binding *ysd;
1015 struct ypresp_master yprm;
1016 struct ypreq_nokey yprnk;
1022 if (indomain == NULL || !strlen(indomain) ||
1023 inmap == NULL || !strlen(inmap))
1024 return (YPERR_BADARGS);
1027 if (_yp_dobind(indomain, &ysd) != 0) {
1029 return (YPERR_DOMAIN);
1032 tv.tv_sec = _yplib_timeout;
1035 yprnk.domain = indomain;
1038 bzero((char *)&yprm, sizeof yprm);
1040 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1041 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1042 (xdrproc_t)xdr_ypresp_master, &yprm, tv);
1043 if (r != RPC_SUCCESS) {
1044 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
1049 if (!(r = ypprot_err(yprm.stat))) {
1050 *outname = (char *)strdup(yprm.peer);
1053 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1059 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1061 struct dom_binding *ysd;
1062 struct ypresp_maplist ypml;
1068 if (indomain == NULL || !strlen(indomain))
1069 return (YPERR_BADARGS);
1073 if (_yp_dobind(indomain, &ysd) != 0) {
1075 return (YPERR_DOMAIN);
1078 tv.tv_sec = _yplib_timeout;
1081 bzero((char *)&ypml, sizeof ypml);
1083 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1084 (xdrproc_t)xdr_domainname, &indomain,
1085 (xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1086 if (r != RPC_SUCCESS) {
1087 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1091 if (!(r = ypprot_err(ypml.stat))) {
1092 *outmaplist = ypml.maps;
1095 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1101 yperr_string(int incode)
1103 static char err[80];
1109 return ("Request arguments bad");
1111 return ("RPC failure");
1113 return ("Can't bind to server which serves this domain");
1115 return ("No such map in server's domain");
1117 return ("No such key in map");
1119 return ("YP server error");
1121 return ("Local resource allocation failure");
1123 return ("No more records in map database");
1125 return ("Can't communicate with portmapper");
1127 return ("Can't communicate with ypbind");
1129 return ("Can't communicate with ypserv");
1131 return ("Local domain name not set");
1133 return ("Server data base is bad");
1135 return ("YP server version mismatch - server can't supply service.");
1137 return ("Access violation");
1139 return ("Database is busy");
1141 sprintf(err, "YP unknown error %d\n", incode);
1146 ypprot_err(unsigned int incode)
1152 return (YPERR_YPBIND);
1154 return (YPERR_NOMORE);
1158 return (YPERR_DOMAIN);
1162 return (YPERR_YPERR);
1164 return (YPERR_BADDB);
1166 return (YPERR_YPERR);
1168 return (YPERR_BADARGS);
1170 return (YPERR_VERS);
1172 return (YPERR_YPERR);
1176 _yp_check(char **dom)
1181 if (_yp_domain[0]=='\0')
1182 if (yp_get_default_domain_locked(&unused)) {
1190 if (yp_bind_locked(_yp_domain) == 0) {
1191 yp_unbind_locked(_yp_domain);