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 (_yp_dobind(indomain, &ysd) != 0) {
696 return (YPERR_DOMAIN);
699 tv.tv_sec = _yplib_timeout;
702 bzero((char *)&yprv, sizeof yprv);
704 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
705 (xdrproc_t)xdr_ypreq_key, &yprk,
706 (xdrproc_t)xdr_ypresp_val, &yprv, tv);
707 if (r != RPC_SUCCESS) {
708 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
713 if (!(r = ypprot_err(yprv.stat))) {
714 *outvallen = yprv.val.valdat_len;
715 *outval = (char *)malloc(*outvallen+1);
716 bcopy(yprv.val.valdat_val, *outval, *outvallen);
717 (*outval)[*outvallen] = '\0';
719 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
723 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
729 yp_get_default_domain_locked(char **domp)
732 if (_yp_domain[0] == '\0')
733 if (getdomainname(_yp_domain, sizeof _yp_domain))
734 return (YPERR_NODOM);
740 yp_get_default_domain(char **domp)
745 r = yp_get_default_domain_locked(domp);
751 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
752 char **outval, int *outvallen)
754 struct ypresp_key_val yprkv;
755 struct ypreq_nokey yprnk;
756 struct dom_binding *ysd;
762 if (indomain == NULL || !strlen(indomain) ||
763 inmap == NULL || !strlen(inmap))
764 return (YPERR_BADARGS);
766 *outkey = *outval = NULL;
767 *outkeylen = *outvallen = 0;
771 if (_yp_dobind(indomain, &ysd) != 0) {
773 return (YPERR_DOMAIN);
776 tv.tv_sec = _yplib_timeout;
779 yprnk.domain = indomain;
781 bzero((char *)&yprkv, sizeof yprkv);
783 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
784 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
785 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
786 if (r != RPC_SUCCESS) {
787 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
791 if (!(r = ypprot_err(yprkv.stat))) {
792 *outkeylen = yprkv.key.keydat_len;
793 *outkey = (char *)malloc(*outkeylen+1);
794 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
795 (*outkey)[*outkeylen] = '\0';
796 *outvallen = yprkv.val.valdat_len;
797 *outval = (char *)malloc(*outvallen+1);
798 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
799 (*outval)[*outvallen] = '\0';
802 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
808 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
809 char **outkey, int *outkeylen, char **outval, int *outvallen)
811 struct ypresp_key_val yprkv;
812 struct ypreq_key yprk;
813 struct dom_binding *ysd;
819 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
820 inmap == NULL || !strlen(inmap) ||
821 indomain == NULL || !strlen(indomain))
822 return (YPERR_BADARGS);
824 *outkey = *outval = NULL;
825 *outkeylen = *outvallen = 0;
829 if (_yp_dobind(indomain, &ysd) != 0) {
831 return (YPERR_DOMAIN);
834 tv.tv_sec = _yplib_timeout;
837 yprk.domain = indomain;
839 yprk.key.keydat_val = inkey;
840 yprk.key.keydat_len = inkeylen;
841 bzero((char *)&yprkv, sizeof yprkv);
843 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
844 (xdrproc_t)xdr_ypreq_key, &yprk,
845 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
846 if (r != RPC_SUCCESS) {
847 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
851 if (!(r = ypprot_err(yprkv.stat))) {
852 *outkeylen = yprkv.key.keydat_len;
853 *outkey = (char *)malloc(*outkeylen+1);
854 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
855 (*outkey)[*outkeylen] = '\0';
856 *outvallen = yprkv.val.valdat_len;
857 *outval = (char *)malloc(*outvallen+1);
858 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
859 (*outval)[*outvallen] = '\0';
862 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
868 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
870 struct ypreq_nokey yprnk;
871 struct dom_binding *ysd;
873 struct sockaddr_in clnt_sin;
875 u_long status, savstat;
880 if (indomain == NULL || !strlen(indomain) ||
881 inmap == NULL || !strlen(inmap))
882 return (YPERR_BADARGS);
887 if (_yp_dobind(indomain, &ysd) != 0) {
889 return (YPERR_DOMAIN);
892 tv.tv_sec = _yplib_timeout;
895 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
897 clnt_sock = RPC_ANYSOCK;
898 clnt_sin = ysd->dom_server_addr;
899 clnt_sin.sin_port = 0;
900 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
903 printf("clnttcp_create failed\n");
907 yprnk.domain = indomain;
909 ypresp_allfn = incallback->foreach;
910 ypresp_data = (void *)incallback->data;
912 if (clnt_call(clnt, YPPROC_ALL,
913 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
914 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
915 clnt_perror(ysd->dom_client, "yp_all: clnt_call");
923 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */
925 if (savstat != YP_NOMORE)
926 return (ypprot_err(savstat));
931 yp_order(char *indomain, char *inmap, int *outorder)
933 struct dom_binding *ysd;
934 struct ypresp_order ypro;
935 struct ypreq_nokey yprnk;
941 if (indomain == NULL || !strlen(indomain) ||
942 inmap == NULL || !strlen(inmap))
943 return (YPERR_BADARGS);
947 if (_yp_dobind(indomain, &ysd) != 0) {
949 return (YPERR_DOMAIN);
952 tv.tv_sec = _yplib_timeout;
955 yprnk.domain = indomain;
958 bzero((char *)(char *)&ypro, sizeof ypro);
960 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
961 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
962 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
965 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
968 if (r == RPC_PROCUNAVAIL) {
973 if (r != RPC_SUCCESS) {
974 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
979 if (!(r = ypprot_err(ypro.stat))) {
980 *outorder = ypro.ordernum;
983 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
989 yp_master(char *indomain, char *inmap, char **outname)
991 struct dom_binding *ysd;
992 struct ypresp_master yprm;
993 struct ypreq_nokey yprnk;
999 if (indomain == NULL || !strlen(indomain) ||
1000 inmap == NULL || !strlen(inmap))
1001 return (YPERR_BADARGS);
1004 if (_yp_dobind(indomain, &ysd) != 0) {
1006 return (YPERR_DOMAIN);
1009 tv.tv_sec = _yplib_timeout;
1012 yprnk.domain = indomain;
1015 bzero((char *)&yprm, sizeof yprm);
1017 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1018 (xdrproc_t)xdr_ypreq_nokey, &yprnk,
1019 (xdrproc_t)xdr_ypresp_master, &yprm, tv);
1020 if (r != RPC_SUCCESS) {
1021 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
1026 if (!(r = ypprot_err(yprm.stat))) {
1027 *outname = (char *)strdup(yprm.peer);
1030 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1036 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1038 struct dom_binding *ysd;
1039 struct ypresp_maplist ypml;
1045 if (indomain == NULL || !strlen(indomain))
1046 return (YPERR_BADARGS);
1050 if (_yp_dobind(indomain, &ysd) != 0) {
1052 return (YPERR_DOMAIN);
1055 tv.tv_sec = _yplib_timeout;
1058 bzero((char *)&ypml, sizeof ypml);
1060 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1061 (xdrproc_t)xdr_domainname, &indomain,
1062 (xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1063 if (r != RPC_SUCCESS) {
1064 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1068 if (!(r = ypprot_err(ypml.stat))) {
1069 *outmaplist = ypml.maps;
1072 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1078 yperr_string(int incode)
1080 static char err[80];
1086 return ("Request arguments bad");
1088 return ("RPC failure");
1090 return ("Can't bind to server which serves this domain");
1092 return ("No such map in server's domain");
1094 return ("No such key in map");
1096 return ("YP server error");
1098 return ("Local resource allocation failure");
1100 return ("No more records in map database");
1102 return ("Can't communicate with portmapper");
1104 return ("Can't communicate with ypbind");
1106 return ("Can't communicate with ypserv");
1108 return ("Local domain name not set");
1110 return ("Server data base is bad");
1112 return ("YP server version mismatch - server can't supply service.");
1114 return ("Access violation");
1116 return ("Database is busy");
1118 sprintf(err, "YP unknown error %d\n", incode);
1123 ypprot_err(unsigned int incode)
1129 return (YPERR_YPBIND);
1131 return (YPERR_NOMORE);
1135 return (YPERR_DOMAIN);
1139 return (YPERR_YPERR);
1141 return (YPERR_BADDB);
1143 return (YPERR_YPERR);
1145 return (YPERR_BADARGS);
1147 return (YPERR_VERS);
1149 return (YPERR_YPERR);
1153 _yp_check(char **dom)
1158 if (_yp_domain[0]=='\0')
1159 if (yp_get_default_domain_locked(&unused)) {
1167 if (yp_bind_locked(_yp_domain) == 0) {
1168 yp_unbind_locked(_yp_domain);