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/libc/yp/yplib.c,v 1.4 2005/01/31 22:29:47 dillon Exp $
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
47 #include <rpcsvc/yp.h>
48 #include "un-namespace.h"
51 * We have to define these here due to clashes between yp_prot.h and
63 struct ypmatch_ent *ypc_next;
65 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
66 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
70 struct dom_binding *dom_pnext;
71 char dom_domain[YPMAXDOMAIN + 1];
72 struct sockaddr_in dom_server_addr;
73 u_short dom_server_port;
76 u_short dom_local_port; /* now I finally know what this is for. */
79 struct ypmatch_ent *cache;
84 #include <rpcsvc/ypclnt.h>
87 #define BINDINGDIR "/var/yp/binding"
89 #define MAX_RETRIES 20
91 extern bool_t xdr_domainname(), xdr_ypbind_resp();
92 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
93 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
94 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
95 extern bool_t xdr_ypresp_master();
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 = 10;
107 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
108 struct ypmatch_ent *cur)
111 ypdb->cache = cur->ypc_next;
113 prev->ypc_next = cur->ypc_next;
116 free(cur->ypc_key.keydat_val);
117 free(cur->ypc_val.valdat_val);
120 ypdb->ypmatch_cachecnt--;
126 ypmatch_cache_flush(struct dom_binding *ypdb)
128 struct ypmatch_ent *n, *c = ypdb->cache;
132 ypmatch_cache_delete(ypdb, NULL, c);
140 ypmatch_cache_expire(struct dom_binding *ypdb)
142 struct ypmatch_ent *c = ypdb->cache;
143 struct ypmatch_ent *n, *p = NULL;
149 if (t >= c->ypc_expire_t) {
151 ypmatch_cache_delete(ypdb, p, c);
163 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
166 struct ypmatch_ent *new;
168 /* Do an expire run to maybe open up a slot. */
169 if (ypdb->ypmatch_cachecnt)
170 ypmatch_cache_expire(ypdb);
173 * If there are no slots free, then force an expire of
174 * the least recently used entry.
176 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
177 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
183 if (c->ypc_expire_t < oldest) {
184 oldest = c->ypc_expire_t;
193 ypmatch_cache_expire(ypdb);
196 new = malloc(sizeof(struct ypmatch_ent));
200 new->ypc_map = strdup(map);
201 if (new->ypc_map == NULL) {
205 new->ypc_key.keydat_val = malloc(key->keydat_len);
206 if (new->ypc_key.keydat_val == NULL) {
211 new->ypc_val.valdat_val = malloc(val->valdat_len);
212 if (new->ypc_val.valdat_val == NULL) {
213 free(new->ypc_val.valdat_val);
219 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
220 new->ypc_key.keydat_len = key->keydat_len;
221 new->ypc_val.valdat_len = val->valdat_len;
222 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
223 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
225 new->ypc_next = ypdb->cache;
228 ypdb->ypmatch_cachecnt++;
234 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
237 struct ypmatch_ent *c = ypdb->cache;
239 ypmatch_cache_expire(ypdb);
241 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
242 if (strcmp(map, c->ypc_map))
244 if (key->keydat_len != c->ypc_key.keydat_len)
246 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
254 val->valdat_len = c->ypc_val.valdat_len;
255 val->valdat_val = c->ypc_val.valdat_val;
262 ypbinderr_string(int incode)
269 return ("Internal ypbind error");
270 case YPBIND_ERR_NOSERV:
271 return ("Domain not bound");
272 case YPBIND_ERR_RESC:
273 return ("System resource allocation failure");
275 sprintf(err, "Unknown ypbind error: #%d\n", incode);
280 _yp_dobind(char *dom, struct dom_binding **ypdb)
282 static pid_t pid = -1;
283 char path[MAXPATHLEN];
284 struct dom_binding *ysd, *ysd2;
285 struct ypbind_resp ypbr;
287 struct sockaddr_in clnt_sin;
293 struct sockaddr_in check;
294 int checklen = sizeof(struct sockaddr_in);
296 /* Not allowed; bad doggie. Bad. */
297 if (strchr(dom, '/') != NULL)
298 return(YPERR_BADARGS);
301 if (!(pid == -1 || pid == gpid)) {
304 if (ysd->dom_client != NULL)
306 ysd2 = ysd->dom_pnext;
317 if (dom == NULL || strlen(dom) == 0)
318 return (YPERR_BADARGS);
320 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
321 if (strcmp(dom, ysd->dom_domain) == 0)
326 ysd = (struct dom_binding *)malloc(sizeof *ysd);
327 bzero((char *)ysd, sizeof *ysd);
328 ysd->dom_socket = -1;
332 /* Check the socket -- may have been hosed by the caller. */
333 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
334 &checklen) == -1 || check.sin_family != AF_INET ||
335 check.sin_port != ysd->dom_local_port) {
336 /* Socket became bogus somehow... need to rebind. */
339 sock = ysd->dom_socket;
340 save = _dup(ysd->dom_socket);
341 if (ysd->dom_client != NULL)
342 clnt_destroy(ysd->dom_client);
344 ysd->dom_client = NULL;
345 sock = _dup2(save, sock);
352 if (retries > MAX_RETRIES) {
355 return(YPERR_YPBIND);
358 if (ysd->dom_vers == 0) {
360 * We're trying to make a new binding: zorch the
361 * existing handle now (if any).
363 if (ysd->dom_client != NULL) {
364 clnt_destroy(ysd->dom_client);
365 ysd->dom_client = NULL;
366 ysd->dom_socket = -1;
368 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
369 if ((fd = _open(path, O_RDONLY)) == -1) {
370 /* no binding file, YP is dead. */
371 /* Try to bring it back to life. */
375 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
377 struct ypbind_resp ybr;
380 iov[0].iov_base = (caddr_t)&ypb_port;
381 iov[0].iov_len = sizeof ypb_port;
382 iov[1].iov_base = (caddr_t)&ybr;
383 iov[1].iov_len = sizeof ybr;
385 r = _readv(fd, iov, 2);
386 if (r != iov[0].iov_len + iov[1].iov_len) {
392 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
393 ysd->dom_server_addr.sin_family = AF_INET;
394 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
395 ysd->dom_server_addr.sin_addr.s_addr =
396 *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
397 ysd->dom_server_addr.sin_port =
398 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
400 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
404 /* no lock on binding file, YP is dead. */
405 /* Try to bring it back to life. */
412 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
414 * We're trying to make a new binding: zorch the
415 * existing handle now (if any).
417 if (ysd->dom_client != NULL) {
418 clnt_destroy(ysd->dom_client);
419 ysd->dom_client = NULL;
420 ysd->dom_socket = -1;
422 bzero((char *)&clnt_sin, sizeof clnt_sin);
423 clnt_sin.sin_family = AF_INET;
424 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
426 clnt_sock = RPC_ANYSOCK;
427 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
429 if (client == NULL) {
431 * These conditions indicate ypbind just isn't
432 * alive -- we probably don't want to shoot our
433 * mouth off in this case; instead generate error
434 * messages only for really exotic problems.
436 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
437 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
438 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
439 clnt_pcreateerror("clnttcp_create");
442 return (YPERR_YPBIND);
446 * Check the port number -- should be < IPPORT_RESERVED.
447 * If not, it's possible someone has registered a bogus
448 * ypbind with the portmapper and is trying to trick us.
450 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
452 clnt_destroy(client);
455 return(YPERR_YPBIND);
457 tv.tv_sec = _yplib_timeout/2;
459 r = clnt_call(client, YPBINDPROC_DOMAIN,
460 xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv);
461 if (r != RPC_SUCCESS) {
462 clnt_destroy(client);
464 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
467 return(YPERR_YPBIND);
470 "YP: server for domain %s not responding, retrying\n", dom);
473 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
474 struct timespec time_to_sleep, time_remaining;
476 clnt_destroy(client);
479 time_to_sleep.tv_sec = _yplib_timeout/2;
480 time_to_sleep.tv_nsec = 0;
481 _nanosleep(&time_to_sleep,
486 clnt_destroy(client);
488 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
489 ysd->dom_server_addr.sin_family = AF_INET;
490 ysd->dom_server_addr.sin_port =
491 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
492 ysd->dom_server_addr.sin_addr.s_addr =
493 *(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
496 * We could do a reserved port check here too, but this
497 * could pose compatibility problems. The local ypbind is
498 * supposed to decide whether or not to trust yp servers
499 * on insecure ports. For now, we trust its judgement.
501 ysd->dom_server_port =
502 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
504 ysd->dom_vers = YPVERS;
505 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
508 /* Don't rebuild the connection to the server unless we have to. */
509 if (ysd->dom_client == NULL) {
510 tv.tv_sec = _yplib_timeout/2;
512 ysd->dom_socket = RPC_ANYSOCK;
513 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
514 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
515 if (ysd->dom_client == NULL) {
516 clnt_pcreateerror("clntudp_create");
520 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
521 perror("fcntl: F_SETFD");
523 * We want a port number associated with this socket
524 * so that we can check its authenticity later.
526 checklen = sizeof(struct sockaddr_in);
527 bzero((char *)&check, checklen);
528 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
529 check.sin_family = AF_INET;
530 if (!_getsockname(ysd->dom_socket,
531 (struct sockaddr *)&check, &checklen)) {
532 ysd->dom_local_port = check.sin_port;
534 clnt_destroy(ysd->dom_client);
537 return(YPERR_YPBIND);
542 ysd->dom_pnext = _ypbindlist;
552 _yp_unbind(struct dom_binding *ypb)
554 struct sockaddr_in check;
555 int checklen = sizeof(struct sockaddr_in);
557 if (ypb->dom_client != NULL) {
558 /* Check the socket -- may have been hosed by the caller. */
559 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
560 &checklen) == -1 || check.sin_family != AF_INET ||
561 check.sin_port != ypb->dom_local_port) {
564 sock = ypb->dom_socket;
565 save = _dup(ypb->dom_socket);
566 clnt_destroy(ypb->dom_client);
567 sock = _dup2(save, sock);
570 clnt_destroy(ypb->dom_client);
573 ypb->dom_client = NULL;
574 ypb->dom_socket = -1;
577 ypmatch_cache_flush(ypb);
584 return (_yp_dobind(dom, NULL));
590 struct dom_binding *ypb, *ypbp;
593 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
594 if (strcmp(dom, ypb->dom_domain) == 0) {
597 ypbp->dom_pnext = ypb->dom_pnext;
599 _ypbindlist = ypb->dom_pnext;
609 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
610 char **outval, int *outvallen)
612 struct dom_binding *ysd;
613 struct ypresp_val yprv;
615 struct ypreq_key yprk;
623 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
624 inmap == NULL || !strlen(inmap) ||
625 indomain == NULL || !strlen(indomain))
626 return (YPERR_BADARGS);
628 if (_yp_dobind(indomain, &ysd) != 0)
629 return(YPERR_DOMAIN);
631 yprk.domain = indomain;
633 yprk.key.keydat_val = (char *)inkey;
634 yprk.key.keydat_len = inkeylen;
637 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
639 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
640 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
642 *outvallen = yprv.val.valdat_len;
643 *outval = (char *)malloc(*outvallen+1);
644 bcopy(yprv.val.valdat_val, *outval, *outvallen);
645 (*outval)[*outvallen] = '\0';
651 if (_yp_dobind(indomain, &ysd) != 0)
652 return (YPERR_DOMAIN);
654 tv.tv_sec = _yplib_timeout;
657 bzero((char *)&yprv, sizeof yprv);
659 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
660 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
661 if (r != RPC_SUCCESS) {
662 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
667 if (!(r = ypprot_err(yprv.stat))) {
668 *outvallen = yprv.val.valdat_len;
669 *outval = (char *)malloc(*outvallen+1);
670 bcopy(yprv.val.valdat_val, *outval, *outvallen);
671 (*outval)[*outvallen] = '\0';
673 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
677 xdr_free(xdr_ypresp_val, (char *)&yprv);
682 yp_get_default_domain(char **domp)
685 if (_yp_domain[0] == '\0')
686 if (getdomainname(_yp_domain, sizeof _yp_domain))
687 return (YPERR_NODOM);
693 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
694 char **outval, int *outvallen)
696 struct ypresp_key_val yprkv;
697 struct ypreq_nokey yprnk;
698 struct dom_binding *ysd;
704 if (indomain == NULL || !strlen(indomain) ||
705 inmap == NULL || !strlen(inmap))
706 return (YPERR_BADARGS);
708 *outkey = *outval = NULL;
709 *outkeylen = *outvallen = 0;
712 if (_yp_dobind(indomain, &ysd) != 0)
713 return (YPERR_DOMAIN);
715 tv.tv_sec = _yplib_timeout;
718 yprnk.domain = indomain;
720 bzero((char *)&yprkv, sizeof yprkv);
722 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
723 xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
724 if (r != RPC_SUCCESS) {
725 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
729 if (!(r = ypprot_err(yprkv.stat))) {
730 *outkeylen = yprkv.key.keydat_len;
731 *outkey = (char *)malloc(*outkeylen+1);
732 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
733 (*outkey)[*outkeylen] = '\0';
734 *outvallen = yprkv.val.valdat_len;
735 *outval = (char *)malloc(*outvallen+1);
736 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
737 (*outval)[*outvallen] = '\0';
740 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
745 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
746 char **outkey, int *outkeylen, char **outval, int *outvallen)
748 struct ypresp_key_val yprkv;
749 struct ypreq_key yprk;
750 struct dom_binding *ysd;
756 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
757 inmap == NULL || !strlen(inmap) ||
758 indomain == NULL || !strlen(indomain))
759 return (YPERR_BADARGS);
761 *outkey = *outval = NULL;
762 *outkeylen = *outvallen = 0;
765 if (_yp_dobind(indomain, &ysd) != 0)
766 return (YPERR_DOMAIN);
768 tv.tv_sec = _yplib_timeout;
771 yprk.domain = indomain;
773 yprk.key.keydat_val = inkey;
774 yprk.key.keydat_len = inkeylen;
775 bzero((char *)&yprkv, sizeof yprkv);
777 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
778 xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
779 if (r != RPC_SUCCESS) {
780 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
784 if (!(r = ypprot_err(yprkv.stat))) {
785 *outkeylen = yprkv.key.keydat_len;
786 *outkey = (char *)malloc(*outkeylen+1);
787 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
788 (*outkey)[*outkeylen] = '\0';
789 *outvallen = yprkv.val.valdat_len;
790 *outval = (char *)malloc(*outvallen+1);
791 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
792 (*outval)[*outvallen] = '\0';
795 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
800 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
802 struct ypreq_nokey yprnk;
803 struct dom_binding *ysd;
805 struct sockaddr_in clnt_sin;
807 u_long status, savstat;
812 if (indomain == NULL || !strlen(indomain) ||
813 inmap == NULL || !strlen(inmap))
814 return (YPERR_BADARGS);
818 if (_yp_dobind(indomain, &ysd) != 0)
819 return (YPERR_DOMAIN);
821 tv.tv_sec = _yplib_timeout;
824 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
826 clnt_sock = RPC_ANYSOCK;
827 clnt_sin = ysd->dom_server_addr;
828 clnt_sin.sin_port = 0;
829 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
831 printf("clnttcp_create failed\n");
835 yprnk.domain = indomain;
837 ypresp_allfn = incallback->foreach;
838 ypresp_data = (void *)incallback->data;
840 if (clnt_call(clnt, YPPROC_ALL,
841 xdr_ypreq_nokey, &yprnk,
842 xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
843 clnt_perror(ysd->dom_client, "yp_all: clnt_call");
851 xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */
852 if (savstat != YP_NOMORE)
853 return (ypprot_err(savstat));
858 yp_order(char *indomain, char *inmap, int *outorder)
860 struct dom_binding *ysd;
861 struct ypresp_order ypro;
862 struct ypreq_nokey yprnk;
868 if (indomain == NULL || !strlen(indomain) ||
869 inmap == NULL || !strlen(inmap))
870 return (YPERR_BADARGS);
873 if (_yp_dobind(indomain, &ysd) != 0)
874 return (YPERR_DOMAIN);
876 tv.tv_sec = _yplib_timeout;
879 yprnk.domain = indomain;
882 bzero((char *)(char *)&ypro, sizeof ypro);
884 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
885 xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
888 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
891 if (r == RPC_PROCUNAVAIL) {
895 if (r != RPC_SUCCESS) {
896 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
901 if (!(r = ypprot_err(ypro.stat))) {
902 *outorder = ypro.ordernum;
905 xdr_free(xdr_ypresp_order, (char *)&ypro);
910 yp_master(char *indomain, char *inmap, char **outname)
912 struct dom_binding *ysd;
913 struct ypresp_master yprm;
914 struct ypreq_nokey yprnk;
920 if (indomain == NULL || !strlen(indomain) ||
921 inmap == NULL || !strlen(inmap))
922 return (YPERR_BADARGS);
924 if (_yp_dobind(indomain, &ysd) != 0)
925 return (YPERR_DOMAIN);
927 tv.tv_sec = _yplib_timeout;
930 yprnk.domain = indomain;
933 bzero((char *)&yprm, sizeof yprm);
935 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
936 xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
937 if (r != RPC_SUCCESS) {
938 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
943 if (!(r = ypprot_err(yprm.stat))) {
944 *outname = (char *)strdup(yprm.peer);
947 xdr_free(xdr_ypresp_master, (char *)&yprm);
952 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
954 struct dom_binding *ysd;
955 struct ypresp_maplist ypml;
961 if (indomain == NULL || !strlen(indomain))
962 return (YPERR_BADARGS);
965 if (_yp_dobind(indomain, &ysd) != 0)
966 return (YPERR_DOMAIN);
968 tv.tv_sec = _yplib_timeout;
971 bzero((char *)&ypml, sizeof ypml);
973 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
974 xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv);
975 if (r != RPC_SUCCESS) {
976 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
980 if (!(r = ypprot_err(ypml.stat))) {
981 *outmaplist = ypml.maps;
984 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
989 yperr_string(int incode)
997 return ("Request arguments bad");
999 return ("RPC failure");
1001 return ("Can't bind to server which serves this domain");
1003 return ("No such map in server's domain");
1005 return ("No such key in map");
1007 return ("YP server error");
1009 return ("Local resource allocation failure");
1011 return ("No more records in map database");
1013 return ("Can't communicate with portmapper");
1015 return ("Can't communicate with ypbind");
1017 return ("Can't communicate with ypserv");
1019 return ("Local domain name not set");
1021 return ("Server data base is bad");
1023 return ("YP server version mismatch - server can't supply service.");
1025 return ("Access violation");
1027 return ("Database is busy");
1029 sprintf(err, "YP unknown error %d\n", incode);
1034 ypprot_err(unsigned int incode)
1040 return (YPERR_YPBIND);
1042 return (YPERR_NOMORE);
1046 return (YPERR_DOMAIN);
1050 return (YPERR_YPERR);
1052 return (YPERR_BADDB);
1054 return (YPERR_YPERR);
1056 return (YPERR_BADARGS);
1058 return (YPERR_VERS);
1060 return (YPERR_YPERR);
1064 _yp_check(char **dom)
1068 if (_yp_domain[0]=='\0')
1069 if (yp_get_default_domain(&unused))
1075 if (yp_bind(_yp_domain) == 0) {
1076 yp_unbind(_yp_domain);