1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #include <libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
27 #include <rpcsvc/ypupd.h>
31 struct dom_binding
*dom_pnext
;
32 char dom_domain
[YPMAXDOMAIN
+ 1];
33 struct sockaddr_in dom_server_addr
;
38 typedef struct dom_binding dom_binding
;
40 static struct timeval TIMEOUT
= {25, 0};
41 static int const MAXTRIES
= 5;
42 static char __ypdomainname
[MAXHOSTNAMELEN
+ 1] = "\0";
43 __libc_lock_define_initialized (static, ypbindlist_lock
)
44 static dom_binding
*__ypbindlist
= NULL
;
48 __yp_bind (const char *domain
, dom_binding
** ypdb
)
50 struct sockaddr_in clnt_saddr
;
51 struct ypbind_resp ypbr
;
61 if ((domain
== NULL
) || (strlen (domain
) == 0))
67 if (strcmp (domain
, ysd
->dom_domain
) == 0)
75 ysd
= (dom_binding
*) malloc (sizeof *ysd
);
76 memset (ysd
, '\0', sizeof *ysd
);
93 if (ysd
->dom_vers
== -1)
97 clnt_destroy(ysd
->dom_client
);
98 ysd
->dom_client
= NULL
;
101 memset (&clnt_saddr
, '\0', sizeof clnt_saddr
);
102 clnt_saddr
.sin_family
= AF_INET
;
103 clnt_saddr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
104 clnt_sock
= RPC_ANYSOCK
;
105 client
= clnttcp_create (&clnt_saddr
, YPBINDPROG
, YPBINDVERS
,
114 ** Check the port number -- should be < IPPORT_RESERVED.
115 ** If not, it's possible someone has registered a bogus
116 ** ypbind with the portmapper and is trying to trick us.
118 if (ntohs(clnt_saddr
.sin_port
) >= IPPORT_RESERVED
)
120 clnt_destroy(client
);
123 return(YPERR_YPBIND
);
126 if (clnt_call (client
, YPBINDPROC_DOMAIN
,
127 (xdrproc_t
) xdr_domainname
, (caddr_t
) &domain
,
128 (xdrproc_t
) xdr_ypbind_resp
,
129 (caddr_t
) &ypbr
, TIMEOUT
) != RPC_SUCCESS
)
131 clnt_destroy (client
);
137 clnt_destroy (client
);
138 if (ypbr
.ypbind_status
!= YPBIND_SUCC_VAL
)
140 switch (ypbr
.ypbind_resp_u
.ypbind_error
)
143 fputs (_("YPBINDPROC_DOMAIN: Internal error\n"), stderr
);
145 case YPBIND_ERR_NOSERV
:
147 _("YPBINDPROC_DOMAIN: No server for domain %s\n"),
150 case YPBIND_ERR_RESC
:
151 fputs (_("YPBINDPROC_DOMAIN: Resource allocation failure\n"),
155 fputs (_("YPBINDPROC_DOMAIN: Unknown error\n"), stderr
);
162 memset (&ysd
->dom_server_addr
, '\0', sizeof ysd
->dom_server_addr
);
163 ysd
->dom_server_addr
.sin_family
= AF_INET
;
164 memcpy (&ysd
->dom_server_addr
.sin_port
,
165 ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
,
166 sizeof (ysd
->dom_server_addr
.sin_port
));
167 memcpy (&ysd
->dom_server_addr
.sin_addr
.s_addr
,
168 ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
,
169 sizeof (ysd
->dom_server_addr
.sin_addr
.s_addr
));
170 ysd
->dom_vers
= YPVERS
;
171 strncpy (ysd
->dom_domain
, domain
, YPMAXDOMAIN
);
172 ysd
->dom_domain
[YPMAXDOMAIN
] = '\0';
176 clnt_destroy (ysd
->dom_client
);
177 ysd
->dom_socket
= RPC_ANYSOCK
;
178 ysd
->dom_client
= clntudp_create (&ysd
->dom_server_addr
, YPPROG
, YPVERS
,
179 TIMEOUT
, &ysd
->dom_socket
);
180 if (ysd
->dom_client
== NULL
)
184 while (ysd
->dom_client
== NULL
);
186 /* If the program exists, close the socket */
187 if (fcntl (ysd
->dom_socket
, F_SETFD
, 1) == -1)
188 perror (_("fcntl: F_SETFD"));
192 ysd
->dom_pnext
= __ypbindlist
;
199 return YPERR_SUCCESS
;
203 __yp_unbind (dom_binding
*ydb
)
205 clnt_destroy (ydb
->dom_client
);
206 ydb
->dom_client
= NULL
;
207 ydb
->dom_socket
= -1;
211 do_ypcall (const char *domain
, u_long prog
, xdrproc_t xargs
,
212 caddr_t req
, xdrproc_t xres
, caddr_t resp
)
214 dom_binding
*ydb
= NULL
;
218 result
= YPERR_YPERR
;
220 while (try < MAXTRIES
&& result
!= RPC_SUCCESS
)
222 __libc_lock_lock (ypbindlist_lock
);
224 if (__yp_bind (domain
, &ydb
) != 0)
226 __libc_lock_unlock (ypbindlist_lock
);
230 result
= clnt_call (ydb
->dom_client
, prog
,
231 xargs
, req
, xres
, resp
, TIMEOUT
);
233 if (result
!= RPC_SUCCESS
)
235 clnt_perror (ydb
->dom_client
, "do_ypcall: clnt_call");
241 __libc_lock_unlock (ypbindlist_lock
);
250 yp_bind (const char *indomain
)
254 __libc_lock_lock (ypbindlist_lock
);
256 status
= __yp_bind (indomain
, NULL
);
258 __libc_lock_unlock (ypbindlist_lock
);
264 yp_unbind (const char *indomain
)
266 dom_binding
*ydbptr
, *ydbptr2
;
268 __libc_lock_lock (ypbindlist_lock
);
271 ydbptr
= __ypbindlist
;
272 while (ydbptr
!= NULL
)
274 if (strcmp (ydbptr
->dom_domain
, indomain
) == 0)
280 __ypbindlist
= __ypbindlist
->dom_pnext
;
282 ydbptr2
= ydbptr
->dom_pnext
;
288 ydbptr
= ydbptr
->dom_pnext
;
291 __libc_lock_unlock (ypbindlist_lock
);
296 __libc_lock_define_initialized (static, domainname_lock
)
299 yp_get_default_domain (char **outdomain
)
301 int result
= YPERR_SUCCESS
;;
304 __libc_lock_lock (domainname_lock
);
306 if (__ypdomainname
[0] == '\0')
308 if (getdomainname (__ypdomainname
, MAXHOSTNAMELEN
))
309 result
= YPERR_NODOM
;
311 *outdomain
= __ypdomainname
;
314 *outdomain
= __ypdomainname
;
316 __libc_lock_unlock (domainname_lock
);
322 __yp_check (char **domain
)
326 if (__ypdomainname
[0] == '\0')
327 if (yp_get_default_domain (&unused
))
329 else if (strcmp (__ypdomainname
, "(none)") == 0)
333 *domain
= __ypdomainname
;
335 if (yp_bind (__ypdomainname
) == 0)
341 yp_match (const char *indomain
, const char *inmap
, const char *inkey
,
342 const int inkeylen
, char **outval
, int *outvallen
)
348 if (indomain
== NULL
|| indomain
[0] == '\0' ||
349 inmap
== NULL
|| inmap
[0] == '\0' ||
350 inkey
== NULL
|| inkey
[0] == '\0' || inkeylen
<= 0)
351 return YPERR_BADARGS
;
353 req
.domain
= (char *) indomain
;
354 req
.map
= (char *) inmap
;
355 req
.key
.keydat_val
= (char *) inkey
;
356 req
.key
.keydat_len
= inkeylen
;
360 memset (&resp
, '\0', sizeof (resp
));
362 result
= do_ypcall (indomain
, YPPROC_MATCH
, (xdrproc_t
) xdr_ypreq_key
,
363 (caddr_t
) & req
, (xdrproc_t
) xdr_ypresp_val
,
366 if (result
!= RPC_SUCCESS
)
368 if (resp
.stat
!= YP_TRUE
)
369 return ypprot_err (resp
.stat
);
371 *outvallen
= resp
.val
.valdat_len
;
372 *outval
= malloc (*outvallen
+ 1);
373 memcpy (*outval
, resp
.val
.valdat_val
, *outvallen
);
374 (*outval
)[*outvallen
] = '\0';
376 xdr_free ((xdrproc_t
) xdr_ypresp_val
, (char *) &resp
);
378 return YPERR_SUCCESS
;
382 yp_first (const char *indomain
, const char *inmap
, char **outkey
,
383 int *outkeylen
, char **outval
, int *outvallen
)
389 if (indomain
== NULL
|| indomain
[0] == '\0' ||
390 inmap
== NULL
|| inmap
[0] == '\0')
391 return YPERR_BADARGS
;
393 req
.domain
= (char *) indomain
;
394 req
.map
= (char *) inmap
;
396 *outkey
= *outval
= NULL
;
397 *outkeylen
= *outvallen
= 0;
398 memset (&resp
, '\0', sizeof (resp
));
400 result
= do_ypcall (indomain
, YPPROC_FIRST
, (xdrproc_t
) xdr_ypreq_nokey
,
401 (caddr_t
) & req
, (xdrproc_t
) xdr_ypresp_key_val
,
404 if (result
!= RPC_SUCCESS
)
406 if (resp
.stat
!= YP_TRUE
)
407 return ypprot_err (resp
.stat
);
409 *outkeylen
= resp
.key
.keydat_len
;
410 *outkey
= malloc (*outkeylen
+ 1);
411 memcpy (*outkey
, resp
.key
.keydat_val
, *outkeylen
);
412 (*outkey
)[*outkeylen
] = '\0';
413 *outvallen
= resp
.val
.valdat_len
;
414 *outval
= malloc (*outvallen
+ 1);
415 memcpy (*outval
, resp
.val
.valdat_val
, *outvallen
);
416 (*outval
)[*outvallen
] = '\0';
418 xdr_free ((xdrproc_t
) xdr_ypresp_key_val
, (char *) &resp
);
420 return YPERR_SUCCESS
;
424 yp_next (const char *indomain
, const char *inmap
, const char *inkey
,
425 const int inkeylen
, char **outkey
, int *outkeylen
, char **outval
,
432 if (indomain
== NULL
|| indomain
[0] == '\0' ||
433 inmap
== NULL
|| inmap
[0] == '\0' ||
434 inkeylen
<= 0 || inkey
== NULL
|| inkey
[0] == '\0')
435 return YPERR_BADARGS
;
437 req
.domain
= (char *) indomain
;
438 req
.map
= (char *) inmap
;
439 req
.key
.keydat_val
= (char *) inkey
;
440 req
.key
.keydat_len
= inkeylen
;
442 *outkey
= *outval
= NULL
;
443 *outkeylen
= *outvallen
= 0;
444 memset (&resp
, '\0', sizeof (resp
));
446 result
= do_ypcall (indomain
, YPPROC_NEXT
, (xdrproc_t
) xdr_ypreq_key
,
447 (caddr_t
) & req
, (xdrproc_t
) xdr_ypresp_key_val
,
450 if (result
!= RPC_SUCCESS
)
452 if (resp
.stat
!= YP_TRUE
)
453 return ypprot_err (resp
.stat
);
455 *outkeylen
= resp
.key
.keydat_len
;
456 *outkey
= malloc (*outkeylen
+ 1);
457 memcpy (*outkey
, resp
.key
.keydat_val
, *outkeylen
);
458 (*outkey
)[*outkeylen
] = '\0';
459 *outvallen
= resp
.val
.valdat_len
;
460 *outval
= malloc (*outvallen
+ 1);
461 memcpy (*outval
, resp
.val
.valdat_val
, *outvallen
);
462 (*outval
)[*outvallen
] = '\0';
464 xdr_free ((xdrproc_t
) xdr_ypresp_key_val
, (char *) &resp
);
466 return YPERR_SUCCESS
;
470 yp_master (const char *indomain
, const char *inmap
, char **outname
)
476 if (indomain
== NULL
|| indomain
[0] == '\0' ||
477 inmap
== NULL
|| inmap
[0] == '\0')
478 return YPERR_BADARGS
;
480 req
.domain
= (char *) indomain
;
481 req
.map
= (char *) inmap
;
483 memset (&resp
, '\0', sizeof (ypresp_master
));
485 result
= do_ypcall (indomain
, YPPROC_MASTER
, (xdrproc_t
) xdr_ypreq_nokey
,
486 (caddr_t
) & req
, (xdrproc_t
) xdr_ypresp_master
, (caddr_t
) & resp
);
488 if (result
!= RPC_SUCCESS
)
490 if (resp
.stat
!= YP_TRUE
)
491 return ypprot_err (resp
.stat
);
493 *outname
= strdup (resp
.peer
);
494 xdr_free ((xdrproc_t
) xdr_ypresp_master
, (char *) &resp
);
496 return YPERR_SUCCESS
;
500 yp_order (const char *indomain
, const char *inmap
, unsigned int *outorder
)
502 struct ypreq_nokey req
;
503 struct ypresp_order resp
;
506 if (indomain
== NULL
|| indomain
[0] == '\0' ||
507 inmap
== NULL
|| inmap
== '\0')
508 return YPERR_BADARGS
;
510 req
.domain
= (char *) indomain
;
511 req
.map
= (char *) inmap
;
513 memset (&resp
, '\0', sizeof (resp
));
515 result
= do_ypcall (indomain
, YPPROC_ORDER
, (xdrproc_t
) xdr_ypreq_nokey
,
516 (caddr_t
) & req
, (xdrproc_t
) xdr_ypresp_order
, (caddr_t
) & resp
);
518 if (result
!= RPC_SUCCESS
)
520 if (resp
.stat
!= YP_TRUE
)
521 return ypprot_err (resp
.stat
);
523 *outorder
= resp
.ordernum
;
524 xdr_free ((xdrproc_t
) xdr_ypresp_order
, (char *) &resp
);
526 return YPERR_SUCCESS
;
529 static void *ypall_data
;
530 static int (*ypall_foreach
) __P ((int status
, char *key
, int keylen
,
531 char *val
, int vallen
, char *data
));
534 __xdr_ypresp_all (XDR
* xdrs
, u_long
* objp
)
538 struct ypresp_all resp
;
540 memset (&resp
, '\0', sizeof (struct ypresp_all
));
541 if (!xdr_ypresp_all (xdrs
, &resp
))
543 xdr_free ((xdrproc_t
) xdr_ypresp_all
, (char *) &resp
);
549 xdr_free ((xdrproc_t
) xdr_ypresp_all
, (char *) &resp
);
554 switch (resp
.ypresp_all_u
.val
.stat
)
558 char key
[resp
.ypresp_all_u
.val
.key
.keydat_len
+ 1];
559 char val
[resp
.ypresp_all_u
.val
.val
.valdat_len
+ 1];
560 int keylen
= resp
.ypresp_all_u
.val
.key
.keydat_len
;
561 int vallen
= resp
.ypresp_all_u
.val
.val
.valdat_len
;
564 memcpy (key
, resp
.ypresp_all_u
.val
.key
.keydat_val
, keylen
);
566 memcpy (val
, resp
.ypresp_all_u
.val
.val
.valdat_val
, vallen
);
568 xdr_free ((xdrproc_t
) xdr_ypresp_all
, (char *) &resp
);
569 if ((*ypall_foreach
) (*objp
, key
, keylen
,
570 val
, vallen
, ypall_data
))
576 xdr_free ((xdrproc_t
) xdr_ypresp_all
, (char *) &resp
);
580 *objp
= resp
.ypresp_all_u
.val
.stat
;
581 xdr_free ((xdrproc_t
) xdr_ypresp_all
, (char *) &resp
);
588 yp_all (const char *indomain
, const char *inmap
,
589 const struct ypall_callback
*incallback
)
591 struct ypreq_nokey req
;
594 struct sockaddr_in clnt_sin
;
596 unsigned long status
;
599 if (indomain
== NULL
|| indomain
[0] == '\0' ||
600 inmap
== NULL
|| inmap
== '\0')
601 return YPERR_BADARGS
;
604 result
= YPERR_YPERR
;
606 while (try < MAXTRIES
&& result
!= RPC_SUCCESS
)
608 __libc_lock_lock (ypbindlist_lock
);
610 if (__yp_bind (indomain
, &ydb
) != 0)
612 __libc_lock_unlock (ypbindlist_lock
);
616 /* YPPROC_ALL get its own TCP channel to ypserv */
617 clnt_sock
= RPC_ANYSOCK
;
618 clnt_sin
= ydb
->dom_server_addr
;
619 clnt_sin
.sin_port
= 0;
620 clnt
= clnttcp_create (&clnt_sin
, YPPROG
, YPVERS
, &clnt_sock
, 0, 0);
623 puts ("yp_all: clnttcp_create failed");
624 __libc_lock_unlock (ypbindlist_lock
);
627 req
.domain
= (char *) indomain
;
628 req
.map
= (char *) inmap
;
630 ypall_foreach
= incallback
->foreach
;
631 ypall_data
= (void *) incallback
->data
;
633 result
= clnt_call (clnt
, YPPROC_ALL
, (xdrproc_t
) xdr_ypreq_nokey
,
634 (caddr_t
) &req
, (xdrproc_t
) __xdr_ypresp_all
,
635 (caddr_t
) &status
, TIMEOUT
);
637 if (result
!= RPC_SUCCESS
)
639 clnt_perror (ydb
->dom_client
, "yp_all: clnt_call");
647 result
= YPERR_SUCCESS
;
650 __libc_lock_unlock (ypbindlist_lock
);
652 if (status
!= YP_NOMORE
)
653 return ypprot_err (status
);
661 yp_maplist (const char *indomain
, struct ypmaplist
**outmaplist
)
663 struct ypresp_maplist resp
;
666 if (indomain
== NULL
|| indomain
[0] == '\0')
667 return YPERR_BADARGS
;
669 memset (&resp
, '\0', sizeof (resp
));
671 result
= do_ypcall (indomain
, YPPROC_MAPLIST
, (xdrproc_t
) xdr_domainname
,
672 (caddr_t
) & indomain
, (xdrproc_t
) xdr_ypresp_maplist
, (caddr_t
) & resp
);
674 if (result
!= RPC_SUCCESS
)
676 if (resp
.stat
!= YP_TRUE
)
677 return ypprot_err (resp
.stat
);
679 *outmaplist
= resp
.maps
;
680 /* We give the list not free, this will be done by ypserv
681 xdr_free((xdrproc_t)xdr_ypresp_maplist, (char *)&resp); */
683 return YPERR_SUCCESS
;
687 yperr_string (const int error
)
694 return _("Request arguments bad");
696 return _("RPC failure on NIS operation");
698 return _("Can't bind to server which serves this domain");
700 return _("No such map in server's domain");
702 return _("No such key in map");
704 return _("Internal NIS error");
706 return _("Local resource allocation failure");
708 return _("No more records in map database");
710 return _("Can't communicate with portmapper");
712 return _("Can't communicate with ypbind");
714 return _("Can't communicate with ypserv");
716 return _("Local domain name not set");
718 return _("NIS map data base is bad");
720 return _("NIS client/server version mismatch - can't supply service");
722 return _("Permission denied");
724 return _("Database is busy");
726 return _("Unknown NIS error code");
730 ypprot_err (const int code
)
735 return YPERR_SUCCESS
;
753 return YPERR_BADARGS
;
761 ypbinderr_string (const int error
)
768 return _("Internal ypbind error");
769 case YPBIND_ERR_NOSERV
:
770 return _("Domain not bound");
771 case YPBIND_ERR_RESC
:
772 return _("System resource allocation failure");
774 return _("Unknown ypbind error");
782 yp_update (char *domain
, char *map
, unsigned ypop
,
783 char *key
, int keylen
, char *data
, int datalen
)
785 #if defined (HAVE_SECURE_RPC)
788 ypupdate_args update_args
;
789 ypdelete_args delete_args
;
792 xdrproc_t xdr_argument
;
796 struct sockaddr saddr
;
797 char servername
[MAXNETNAMELEN
+ 1];
800 if (!domain
|| !map
|| !key
|| (ypop
!= YPOP_DELETE
&& !data
))
801 return YPERR_BADARGS
;
803 args
.update_args
.mapname
= map
;
804 args
.update_args
.key
.yp_buf_len
= keylen
;
805 args
.update_args
.key
.yp_buf_val
= key
;
806 args
.update_args
.datum
.yp_buf_len
= datalen
;
807 args
.update_args
.datum
.yp_buf_val
= data
;
809 if ((r
= yp_master (domain
, map
, &master
)) != 0)
812 if (!host2netname (servername
, master
, domain
))
814 fputs (_("yp_update: cannot convert host to netname\n"), stderr
);
818 if ((clnt
= clnt_create (master
, YPU_PROG
, YPU_VERS
, "tcp")) == NULL
)
820 clnt_pcreateerror ("yp_update: clnt_create");
824 if (!clnt_control (clnt
, CLGET_SERVER_ADDR
, (char *) &saddr
))
826 fputs (_("yp_update: cannot get server address\n"), stderr
);
835 xdr_argument
= (xdrproc_t
) xdr_ypupdate_args
;
838 xdr_argument
= (xdrproc_t
) xdr_ypdelete_args
;
841 return YPERR_BADARGS
;
845 clnt
->cl_auth
= authdes_create (servername
, WINDOW
, &saddr
, NULL
);
847 if (clnt
->cl_auth
== NULL
)
848 clnt
->cl_auth
= authunix_create_default ();
851 r
= clnt_call (clnt
, ypop
, xdr_argument
, (caddr_t
) &args
,
852 (xdrproc_t
) xdr_u_int
, (caddr_t
) &res
, TIMEOUT
);
854 if (r
== RPC_AUTHERROR
)
856 if (clnt
->cl_auth
->ah_cred
.oa_flavor
== AUTH_DES
)
858 clnt
->cl_auth
= authunix_create_default ();
864 if (r
!= RPC_SUCCESS
)
866 clnt_perror (clnt
, "yp_update: clnt_call");