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
30 * $FreeBSD: src/lib/libc/yp/yplib.c,v 1.34.2.2 2002/02/15 00:46:53 des Exp $
31 * $DragonFly: src/lib/libcr/yp/Attic/yplib.c,v 1.3 2004/07/05 17:31:02 eirikn Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
46 #include <rpcsvc/yp.h>
49 * We have to define these here due to clashes between yp_prot.h and
61 struct ypmatch_ent *ypc_next;
63 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
64 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
68 struct dom_binding *dom_pnext;
69 char dom_domain[YPMAXDOMAIN + 1];
70 struct sockaddr_in dom_server_addr;
71 u_short dom_server_port;
74 u_short dom_local_port; /* now I finally know what this is for. */
77 struct ypmatch_ent *cache;
82 #include <rpcsvc/ypclnt.h>
85 #define BINDINGDIR "/var/yp/binding"
87 #define MAX_RETRIES 20
89 extern bool_t xdr_domainname(), xdr_ypbind_resp();
90 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
91 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
92 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
93 extern bool_t xdr_ypresp_master();
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 = 10;
105 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
106 struct ypmatch_ent *cur)
109 ypdb->cache = cur->ypc_next;
111 prev->ypc_next = cur->ypc_next;
114 free(cur->ypc_key.keydat_val);
115 free(cur->ypc_val.valdat_val);
118 ypdb->ypmatch_cachecnt--;
124 ypmatch_cache_flush(struct dom_binding *ypdb)
126 struct ypmatch_ent *n, *c = ypdb->cache;
130 ypmatch_cache_delete(ypdb, NULL, c);
138 ypmatch_cache_expire(struct dom_binding *ypdb)
140 struct ypmatch_ent *c = ypdb->cache;
141 struct ypmatch_ent *n, *p = NULL;
147 if (t >= c->ypc_expire_t) {
149 ypmatch_cache_delete(ypdb, p, c);
161 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
164 struct ypmatch_ent *new;
166 /* Do an expire run to maybe open up a slot. */
167 if (ypdb->ypmatch_cachecnt)
168 ypmatch_cache_expire(ypdb);
171 * If there are no slots free, then force an expire of
172 * the least recently used entry.
174 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
175 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
181 if (c->ypc_expire_t < oldest) {
182 oldest = c->ypc_expire_t;
191 ypmatch_cache_expire(ypdb);
194 new = malloc(sizeof(struct ypmatch_ent));
198 new->ypc_map = strdup(map);
199 if (new->ypc_map == NULL) {
203 new->ypc_key.keydat_val = malloc(key->keydat_len);
204 if (new->ypc_key.keydat_val == NULL) {
209 new->ypc_val.valdat_val = malloc(val->valdat_len);
210 if (new->ypc_val.valdat_val == NULL) {
211 free(new->ypc_val.valdat_val);
217 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
218 new->ypc_key.keydat_len = key->keydat_len;
219 new->ypc_val.valdat_len = val->valdat_len;
220 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
221 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
223 new->ypc_next = ypdb->cache;
226 ypdb->ypmatch_cachecnt++;
232 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
235 struct ypmatch_ent *c = ypdb->cache;
237 ypmatch_cache_expire(ypdb);
239 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
240 if (strcmp(map, c->ypc_map))
242 if (key->keydat_len != c->ypc_key.keydat_len)
244 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
252 val->valdat_len = c->ypc_val.valdat_len;
253 val->valdat_val = c->ypc_val.valdat_val;
260 ypbinderr_string(int incode)
267 return ("Internal ypbind error");
268 case YPBIND_ERR_NOSERV:
269 return ("Domain not bound");
270 case YPBIND_ERR_RESC:
271 return ("System resource allocation failure");
273 sprintf(err, "Unknown ypbind error: #%d\n", incode);
278 _yp_dobind(char *dom, struct dom_binding **ypdb)
280 static pid_t pid = -1;
281 char path[MAXPATHLEN];
282 struct dom_binding *ysd, *ysd2;
283 struct ypbind_resp ypbr;
285 struct sockaddr_in clnt_sin;
291 struct sockaddr_in check;
292 int checklen = sizeof(struct sockaddr_in);
294 /* Not allowed; bad doggie. Bad. */
295 if (strchr(dom, '/') != NULL)
296 return(YPERR_BADARGS);
299 if (!(pid == -1 || pid == gpid)) {
302 if (ysd->dom_client != NULL)
304 ysd2 = ysd->dom_pnext;
315 if (dom == NULL || strlen(dom) == 0)
316 return (YPERR_BADARGS);
318 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
319 if (strcmp(dom, ysd->dom_domain) == 0)
324 ysd = (struct dom_binding *)malloc(sizeof *ysd);
325 bzero((char *)ysd, sizeof *ysd);
326 ysd->dom_socket = -1;
330 /* Check the socket -- may have been hosed by the caller. */
331 if (getsockname(ysd->dom_socket, (struct sockaddr *)&check,
332 &checklen) == -1 || check.sin_family != AF_INET ||
333 check.sin_port != ysd->dom_local_port) {
334 /* Socket became bogus somehow... need to rebind. */
337 sock = ysd->dom_socket;
338 save = dup(ysd->dom_socket);
339 if (ysd->dom_client != NULL)
340 clnt_destroy(ysd->dom_client);
342 ysd->dom_client = NULL;
343 sock = dup2(save, sock);
350 if (retries > MAX_RETRIES) {
353 return(YPERR_YPBIND);
356 if (ysd->dom_vers == 0) {
358 * We're trying to make a new binding: zorch the
359 * existing handle now (if any).
361 if (ysd->dom_client != NULL) {
362 clnt_destroy(ysd->dom_client);
363 ysd->dom_client = NULL;
364 ysd->dom_socket = -1;
366 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
367 if ((fd = _open(path, O_RDONLY)) == -1) {
368 /* no binding file, YP is dead. */
369 /* Try to bring it back to life. */
373 if (flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
375 struct ypbind_resp ybr;
378 iov[0].iov_base = (caddr_t)&ypb_port;
379 iov[0].iov_len = sizeof ypb_port;
380 iov[1].iov_base = (caddr_t)&ybr;
381 iov[1].iov_len = sizeof ybr;
383 r = readv(fd, iov, 2);
384 if (r != iov[0].iov_len + iov[1].iov_len) {
390 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
391 ysd->dom_server_addr.sin_family = AF_INET;
392 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
393 ysd->dom_server_addr.sin_addr.s_addr =
394 *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
395 ysd->dom_server_addr.sin_port =
396 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
398 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
402 /* no lock on binding file, YP is dead. */
403 /* Try to bring it back to life. */
410 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
412 * We're trying to make a new binding: zorch the
413 * existing handle now (if any).
415 if (ysd->dom_client != NULL) {
416 clnt_destroy(ysd->dom_client);
417 ysd->dom_client = NULL;
418 ysd->dom_socket = -1;
420 bzero((char *)&clnt_sin, sizeof clnt_sin);
421 clnt_sin.sin_family = AF_INET;
422 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
424 clnt_sock = RPC_ANYSOCK;
425 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
427 if (client == NULL) {
429 * These conditions indicate ypbind just isn't
430 * alive -- we probably don't want to shoot our
431 * mouth off in this case; instead generate error
432 * messages only for really exotic problems.
434 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
435 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
436 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
437 clnt_pcreateerror("clnttcp_create");
440 return (YPERR_YPBIND);
444 * Check the port number -- should be < IPPORT_RESERVED.
445 * If not, it's possible someone has registered a bogus
446 * ypbind with the portmapper and is trying to trick us.
448 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
450 clnt_destroy(client);
453 return(YPERR_YPBIND);
455 tv.tv_sec = _yplib_timeout/2;
457 r = clnt_call(client, YPBINDPROC_DOMAIN,
458 xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv);
459 if (r != RPC_SUCCESS) {
460 clnt_destroy(client);
462 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
465 return(YPERR_YPBIND);
468 "YP: server for domain %s not responding, retrying\n", dom);
471 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
472 struct timespec time_to_sleep, time_remaining;
474 clnt_destroy(client);
477 time_to_sleep.tv_sec = _yplib_timeout/2;
478 time_to_sleep.tv_nsec = 0;
479 _nanosleep(&time_to_sleep,
484 clnt_destroy(client);
486 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
487 ysd->dom_server_addr.sin_family = AF_INET;
488 ysd->dom_server_addr.sin_port =
489 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
490 ysd->dom_server_addr.sin_addr.s_addr =
491 *(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
494 * We could do a reserved port check here too, but this
495 * could pose compatibility problems. The local ypbind is
496 * supposed to decide whether or not to trust yp servers
497 * on insecure ports. For now, we trust its judgement.
499 ysd->dom_server_port =
500 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
502 ysd->dom_vers = YPVERS;
503 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
506 /* Don't rebuild the connection to the server unless we have to. */
507 if (ysd->dom_client == NULL) {
508 tv.tv_sec = _yplib_timeout/2;
510 ysd->dom_socket = RPC_ANYSOCK;
511 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
512 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
513 if (ysd->dom_client == NULL) {
514 clnt_pcreateerror("clntudp_create");
518 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
519 perror("fcntl: F_SETFD");
521 * We want a port number associated with this socket
522 * so that we can check its authenticity later.
524 checklen = sizeof(struct sockaddr_in);
525 bzero((char *)&check, checklen);
526 bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
527 check.sin_family = AF_INET;
528 if (!getsockname(ysd->dom_socket,
529 (struct sockaddr *)&check, &checklen)) {
530 ysd->dom_local_port = check.sin_port;
532 clnt_destroy(ysd->dom_client);
535 return(YPERR_YPBIND);
540 ysd->dom_pnext = _ypbindlist;
550 _yp_unbind(struct dom_binding *ypb)
552 struct sockaddr_in check;
553 int checklen = sizeof(struct sockaddr_in);
555 if (ypb->dom_client != NULL) {
556 /* Check the socket -- may have been hosed by the caller. */
557 if (getsockname(ypb->dom_socket, (struct sockaddr *)&check,
558 &checklen) == -1 || check.sin_family != AF_INET ||
559 check.sin_port != ypb->dom_local_port) {
562 sock = ypb->dom_socket;
563 save = dup(ypb->dom_socket);
564 clnt_destroy(ypb->dom_client);
565 sock = dup2(save, sock);
568 clnt_destroy(ypb->dom_client);
571 ypb->dom_client = NULL;
572 ypb->dom_socket = -1;
575 ypmatch_cache_flush(ypb);
582 return (_yp_dobind(dom, NULL));
588 struct dom_binding *ypb, *ypbp;
591 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
592 if (strcmp(dom, ypb->dom_domain) == 0) {
595 ypbp->dom_pnext = ypb->dom_pnext;
597 _ypbindlist = ypb->dom_pnext;
607 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
608 char **outval, int *outvallen)
610 struct dom_binding *ysd;
611 struct ypresp_val yprv;
613 struct ypreq_key yprk;
621 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
622 inmap == NULL || !strlen(inmap) ||
623 indomain == NULL || !strlen(indomain))
624 return (YPERR_BADARGS);
626 if (_yp_dobind(indomain, &ysd) != 0)
627 return(YPERR_DOMAIN);
629 yprk.domain = indomain;
631 yprk.key.keydat_val = (char *)inkey;
632 yprk.key.keydat_len = inkeylen;
635 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
637 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
638 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
640 *outvallen = yprv.val.valdat_len;
641 *outval = (char *)malloc(*outvallen+1);
642 bcopy(yprv.val.valdat_val, *outval, *outvallen);
643 (*outval)[*outvallen] = '\0';
649 if (_yp_dobind(indomain, &ysd) != 0)
650 return (YPERR_DOMAIN);
652 tv.tv_sec = _yplib_timeout;
655 bzero((char *)&yprv, sizeof yprv);
657 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
658 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
659 if (r != RPC_SUCCESS) {
660 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
665 if (!(r = ypprot_err(yprv.stat))) {
666 *outvallen = yprv.val.valdat_len;
667 *outval = (char *)malloc(*outvallen+1);
668 bcopy(yprv.val.valdat_val, *outval, *outvallen);
669 (*outval)[*outvallen] = '\0';
671 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
675 xdr_free(xdr_ypresp_val, (char *)&yprv);
680 yp_get_default_domain(char **domp)
683 if (_yp_domain[0] == '\0')
684 if (getdomainname(_yp_domain, sizeof _yp_domain))
685 return (YPERR_NODOM);
691 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
692 char **outval, int *outvallen)
694 struct ypresp_key_val yprkv;
695 struct ypreq_nokey yprnk;
696 struct dom_binding *ysd;
702 if (indomain == NULL || !strlen(indomain) ||
703 inmap == NULL || !strlen(inmap))
704 return (YPERR_BADARGS);
706 *outkey = *outval = NULL;
707 *outkeylen = *outvallen = 0;
710 if (_yp_dobind(indomain, &ysd) != 0)
711 return (YPERR_DOMAIN);
713 tv.tv_sec = _yplib_timeout;
716 yprnk.domain = indomain;
718 bzero((char *)&yprkv, sizeof yprkv);
720 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
721 xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
722 if (r != RPC_SUCCESS) {
723 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
727 if (!(r = ypprot_err(yprkv.stat))) {
728 *outkeylen = yprkv.key.keydat_len;
729 *outkey = (char *)malloc(*outkeylen+1);
730 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
731 (*outkey)[*outkeylen] = '\0';
732 *outvallen = yprkv.val.valdat_len;
733 *outval = (char *)malloc(*outvallen+1);
734 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
735 (*outval)[*outvallen] = '\0';
738 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
743 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
744 char **outkey, int *outkeylen, char **outval, int *outvallen)
746 struct ypresp_key_val yprkv;
747 struct ypreq_key yprk;
748 struct dom_binding *ysd;
754 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
755 inmap == NULL || !strlen(inmap) ||
756 indomain == NULL || !strlen(indomain))
757 return (YPERR_BADARGS);
759 *outkey = *outval = NULL;
760 *outkeylen = *outvallen = 0;
763 if (_yp_dobind(indomain, &ysd) != 0)
764 return (YPERR_DOMAIN);
766 tv.tv_sec = _yplib_timeout;
769 yprk.domain = indomain;
771 yprk.key.keydat_val = inkey;
772 yprk.key.keydat_len = inkeylen;
773 bzero((char *)&yprkv, sizeof yprkv);
775 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
776 xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
777 if (r != RPC_SUCCESS) {
778 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
782 if (!(r = ypprot_err(yprkv.stat))) {
783 *outkeylen = yprkv.key.keydat_len;
784 *outkey = (char *)malloc(*outkeylen+1);
785 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
786 (*outkey)[*outkeylen] = '\0';
787 *outvallen = yprkv.val.valdat_len;
788 *outval = (char *)malloc(*outvallen+1);
789 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
790 (*outval)[*outvallen] = '\0';
793 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
798 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
800 struct ypreq_nokey yprnk;
801 struct dom_binding *ysd;
803 struct sockaddr_in clnt_sin;
805 u_long status, savstat;
810 if (indomain == NULL || !strlen(indomain) ||
811 inmap == NULL || !strlen(inmap))
812 return (YPERR_BADARGS);
816 if (_yp_dobind(indomain, &ysd) != 0)
817 return (YPERR_DOMAIN);
819 tv.tv_sec = _yplib_timeout;
822 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
824 clnt_sock = RPC_ANYSOCK;
825 clnt_sin = ysd->dom_server_addr;
826 clnt_sin.sin_port = 0;
827 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
829 printf("clnttcp_create failed\n");
833 yprnk.domain = indomain;
835 ypresp_allfn = incallback->foreach;
836 ypresp_data = (void *)incallback->data;
838 if (clnt_call(clnt, YPPROC_ALL,
839 xdr_ypreq_nokey, &yprnk,
840 xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
841 clnt_perror(ysd->dom_client, "yp_all: clnt_call");
849 xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */
850 if (savstat != YP_NOMORE)
851 return (ypprot_err(savstat));
856 yp_order(char *indomain, char *inmap, int *outorder)
858 struct dom_binding *ysd;
859 struct ypresp_order ypro;
860 struct ypreq_nokey yprnk;
866 if (indomain == NULL || !strlen(indomain) ||
867 inmap == NULL || !strlen(inmap))
868 return (YPERR_BADARGS);
871 if (_yp_dobind(indomain, &ysd) != 0)
872 return (YPERR_DOMAIN);
874 tv.tv_sec = _yplib_timeout;
877 yprnk.domain = indomain;
880 bzero((char *)(char *)&ypro, sizeof ypro);
882 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
883 xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
886 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
889 if (r == RPC_PROCUNAVAIL) {
893 if (r != RPC_SUCCESS) {
894 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
899 if (!(r = ypprot_err(ypro.stat))) {
900 *outorder = ypro.ordernum;
903 xdr_free(xdr_ypresp_order, (char *)&ypro);
908 yp_master(char *indomain, char *inmap, char **outname)
910 struct dom_binding *ysd;
911 struct ypresp_master yprm;
912 struct ypreq_nokey yprnk;
918 if (indomain == NULL || !strlen(indomain) ||
919 inmap == NULL || !strlen(inmap))
920 return (YPERR_BADARGS);
922 if (_yp_dobind(indomain, &ysd) != 0)
923 return (YPERR_DOMAIN);
925 tv.tv_sec = _yplib_timeout;
928 yprnk.domain = indomain;
931 bzero((char *)&yprm, sizeof yprm);
933 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
934 xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
935 if (r != RPC_SUCCESS) {
936 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
941 if (!(r = ypprot_err(yprm.stat))) {
942 *outname = (char *)strdup(yprm.peer);
945 xdr_free(xdr_ypresp_master, (char *)&yprm);
950 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
952 struct dom_binding *ysd;
953 struct ypresp_maplist ypml;
959 if (indomain == NULL || !strlen(indomain))
960 return (YPERR_BADARGS);
963 if (_yp_dobind(indomain, &ysd) != 0)
964 return (YPERR_DOMAIN);
966 tv.tv_sec = _yplib_timeout;
969 bzero((char *)&ypml, sizeof ypml);
971 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
972 xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv);
973 if (r != RPC_SUCCESS) {
974 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
978 if (!(r = ypprot_err(ypml.stat))) {
979 *outmaplist = ypml.maps;
982 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
987 yperr_string(int incode)
995 return ("Request arguments bad");
997 return ("RPC failure");
999 return ("Can't bind to server which serves this domain");
1001 return ("No such map in server's domain");
1003 return ("No such key in map");
1005 return ("YP server error");
1007 return ("Local resource allocation failure");
1009 return ("No more records in map database");
1011 return ("Can't communicate with portmapper");
1013 return ("Can't communicate with ypbind");
1015 return ("Can't communicate with ypserv");
1017 return ("Local domain name not set");
1019 return ("Server data base is bad");
1021 return ("YP server version mismatch - server can't supply service.");
1023 return ("Access violation");
1025 return ("Database is busy");
1027 sprintf(err, "YP unknown error %d\n", incode);
1032 ypprot_err(unsigned int incode)
1038 return (YPERR_YPBIND);
1040 return (YPERR_NOMORE);
1044 return (YPERR_DOMAIN);
1048 return (YPERR_YPERR);
1050 return (YPERR_BADDB);
1052 return (YPERR_YPERR);
1054 return (YPERR_BADARGS);
1056 return (YPERR_VERS);
1058 return (YPERR_YPERR);
1062 _yp_check(char **dom)
1066 if (_yp_domain[0]=='\0')
1067 if (yp_get_default_domain(&unused))
1073 if (yp_bind(_yp_domain) == 0) {
1074 yp_unbind(_yp_domain);