4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * This is a user command which tells which yp server is being used by a
39 * given machine, or which yp server is the master for a named map.
42 * ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
44 * where: the -d switch can be used to specify a domain other than the
45 * default domain. -m tells the master of that map. mname is a mapname
46 * If the -m option is used, ypwhich will act like a vanilla yp client,
47 * and will not attempt to choose a particular yp server. On the
48 * other hand, if no -m switch is used, ypwhich will talk directly to the yp
49 * bind process on the named host, or to the local ypbind process if no host
50 * name is specified. -t switch inhibits nickname translation of map names.
51 * -x is to dump the nickname translation table from file /var/yp/nicknames.
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
61 #include "ypv2_bind.h"
66 #include <arpa/inet.h>
69 #include <netinet/ip6.h>
70 #include <sys/utsname.h>
72 #define YPSLEEPTIME 5 /* between two tries of bind */
74 #define TIMEOUT 30 /* Total seconds for timeout */
75 #define INTER_TRY 10 /* Seconds between tries */
77 static int translate
= TRUE
;
78 static int dodump
= FALSE
;
79 static char *domain
= NULL
;
80 static char default_domain_name
[YPMAXDOMAIN
];
81 static char *host
= NULL
;
82 static int vers
= YPBINDVERS
;
83 static char default_host_name
[256];
84 static bool get_master
= FALSE
;
85 static bool get_server
= FALSE
;
86 static char *map
= NULL
;
87 static char nm
[YPMAXMAP
+1];
88 static struct timeval timeout
= {
89 TIMEOUT
, /* Seconds */
92 static char nullstring
[] = "\000";
93 static char err_usage
[] =
95 ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
98 mname may be either a mapname or a nickname for a map.\n\
99 host if specified, is the machine whose NIS server is to be found.\n\
100 -t inhibits map nickname translation.\n\
101 -Vn version of ypbind, V3 is default.\n\
102 -x dumps the map nickname translation table.\n";
103 static char err_bad_args
[] =
104 "ypwhich: %s argument is bad.\n";
105 static char err_cant_get_kname
[] =
106 "ypwhich: can't get %s back from system call.\n";
107 static char err_null_kname
[] =
108 "ypwhich: the %s hasn't been set on this machine.\n";
109 static char err_bad_mapname
[] = "mapname";
110 static char err_bad_domainname
[] = "domainname";
111 static char err_bad_hostname
[] = "hostname";
113 static void get_command_line_args();
114 static void getdomain();
115 static void getlochost();
116 static void get_server_name();
117 static int call_binder();
118 static void get_map_master();
119 extern void maketable();
120 extern int getmapname();
122 static void dump_response();
124 static void dump_ypmaps();
125 static void dumpmaps();
127 static bool xdr_yp_inaddr();
128 static bool xdr_old_ypbind_resp();
129 static bool xdr_old_yp_binding();
130 static int old_call_binder();
131 static void print_server();
133 /* need these for call to (remote) V2 ypbind */
134 struct old_ypbind_binding
{
135 struct in_addr ypbind_binding_addr
; /* In network order */
136 unsigned short int ypbind_binding_port
; /* In network order */
139 struct old_ypbind_resp
{
140 enum ypbind_resptype ypbind_status
;
142 unsigned long ypbind_error
;
143 struct old_ypbind_binding ypbind_bindinfo
;
148 * This is the main line for the ypwhich process.
154 get_command_line_args(argc
, argv
);
165 if (map
&& translate
&& (strchr(map
, '.') == NULL
) &&
166 (getmapname(map
, nm
))) {
185 * This does the command line argument processing.
188 get_command_line_args(argc
, argv
)
202 if ((*argv
)[0] == '-') {
204 switch ((*argv
)[1]) {
208 vers
= atoi(argv
[0]+2);
210 (void) fprintf(stderr
, err_usage
);
222 if ((*(argv
))[0] == '-') {
230 if ((int)strlen(map
) > YPMAXMAP
) {
231 (void) fprintf(stderr
, err_bad_args
,
248 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
249 (void) fprintf(stderr
, err_bad_args
,
255 (void) fprintf(stderr
, err_usage
);
272 (void) fprintf(stderr
, err_usage
);
279 (void) fprintf(stderr
, err_usage
);
287 if ((int)strlen(host
) > 256) {
288 (void) fprintf(stderr
,
289 err_bad_args
, err_bad_hostname
);
295 if (get_master
&& get_server
) {
296 (void) fprintf(stderr
, err_usage
);
300 if (!get_master
&& !get_server
) {
306 * This gets the local default domainname, and makes sure that it's set
307 * to something reasonable. domain is set here.
312 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
313 domain
= default_domain_name
;
315 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
319 if ((int)strlen(domain
) == 0) {
320 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);
326 * This gets the local hostname back from the kernel
331 struct utsname utsname
;
333 if (uname(&utsname
) != -1) {
334 strcpy(default_host_name
, utsname
.nodename
);
335 host
= default_host_name
;
337 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_hostname
);
344 * This tries to find the name of the server to which the binder in question
345 * is bound. If one of the -Vx flags was specified, it will try only for
346 * that protocol version, otherwise, it will start with the current version,
347 * then drop back to the previous version.
352 char *notbound
= "Domain %s not bound on %s.\n";
355 if (!call_binder(vers
))
356 (void) fprintf(stderr
, notbound
, domain
, host
);
358 if (!old_call_binder(vers
))
359 (void) fprintf(stderr
, notbound
, domain
, host
);
363 extern CLIENT
*__clnt_create_loopback();
366 * This sends a message to the ypbind process on the node with
374 struct ypbind_resp
*response
;
375 struct ypbind_domain ypbd
;
378 struct utsname utsname
;
382 * CAUTION: Do not go to NIS if the host is the same as the local host
383 * XXX: Lots of special magic to distinguish between local and remote
384 * case. We want to make sure the local case doesn't hang.
387 if ((uname(&utsname
) != -1) &&
388 (strcmp(host
, utsname
.nodename
) == 0))
389 client
= __clnt_create_loopback(YPBINDPROG
, vers
, &yperr
);
391 client
= clnt_create(host
, YPBINDPROG
, vers
, "netpath");
392 if (client
== NULL
) {
394 (void) fprintf(stderr
,
395 "ypwhich: %s\n", yperr_string(yperr
));
397 if (rpc_createerr
.cf_stat
== RPC_PROGNOTREGISTERED
||
398 rpc_createerr
.cf_stat
== RPC_PROGUNAVAIL
) {
399 (void) fprintf(stderr
,
400 "ypwhich: %s is not running ypbind\n", host
);
401 } else if (rpc_createerr
.cf_stat
== RPC_PMAPFAILURE
) {
402 (void) fprintf(stderr
,
403 "ypwhich: %s is not running rpcbind\n",
406 (void) clnt_pcreateerror("ypwhich: \
411 ypbd
.ypbind_domainname
= domain
;
412 ypbd
.ypbind_vers
= vers
;
413 response
= ypbindproc_domain_3(&ypbd
, client
);
415 if (response
== NULL
) {
416 (void) sprintf(errstring
,
417 "ypwhich: can't call ypbind on %s", host
);
418 (void) clnt_perror(client
, errstring
);
422 clnt_destroy(client
);
424 if (response
->ypbind_status
!= YPBIND_SUCC_VAL
) {
428 if (response
->ypbind_resp_u
.ypbind_bindinfo
) {
430 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_servername
;
432 if (strcmp(server
, nullstring
) == 0) {
433 /* depends on a hack in ypbind */
434 struct nd_hostservlist
*nhs
= NULL
;
435 struct netconfig
*nconf
=
436 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_nconf
;
437 struct netbuf
*svcaddr
=
438 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_svcaddr
;
440 if (netdir_getbyaddr(nconf
, &nhs
, svcaddr
) != ND_OK
) {
441 struct sockaddr_in
*sa4
;
442 struct sockaddr_in6
*sa6
;
443 char buf
[INET6_ADDRSTRLEN
];
444 char xbuf
[IPV6_ADDR_LEN
];
449 sa4
= (struct sockaddr_in
*)svcaddr
->buf
;
450 af
= ntohs(sa4
->sin_family
);
451 if (af
!= sa4
->sin_family
) {
453 (caddr_t
)xbuf
, IPV6_ADDR_LEN
,
455 if (af
== AF_INET6
) {
457 (caddr_t
)svcaddr
->buf
,
459 sa6
= (struct sockaddr_in6
*)
461 addr
= &sa6
->sin6_addr
;
464 (caddr_t
)svcaddr
->buf
,
466 sa4
= (struct sockaddr_in
*)
468 addr
= &sa4
->sin_addr
;
471 if (af
== AF_INET6
) {
472 sa6
= (struct sockaddr_in6
*)
474 addr
= &sa6
->sin6_addr
;
476 addr
= &sa4
->sin_addr
;
479 str
= inet_ntop(af
, addr
, buf
, sizeof (buf
));
483 fprintf(stdout
, "%s\n", str
);
485 str
= nhs
->h_hostservs
->h_host
;
488 fprintf(stdout
, "%s\n", str
);
490 netdir_free((char *)nhs
, ND_HOSTSERVLIST
);
492 fprintf(stdout
, "%s\n", server
);
496 dump_response(response
);
502 * Serializes/deserializes an in_addr struct.
504 * Note: There is a data coupling between the "definition" of a struct
505 * in_addr implicit in this xdr routine, and the true data definition in
508 static bool xdr_yp_inaddr(xdrs
, ps
)
513 return (xdr_opaque(xdrs
, (caddr_t
)&ps
->s_addr
, 4));
517 * Serializes/deserializes an old ypbind_binding struct.
519 static bool xdr_old_yp_binding(xdrs
, ps
)
521 struct old_ypbind_binding
*ps
;
524 return (xdr_yp_inaddr(xdrs
, &ps
->ypbind_binding_addr
) &&
525 xdr_opaque(xdrs
, (caddr_t
)&ps
->ypbind_binding_port
, 2));
529 * Serializes/deserializes a ypbind_resp structure.
531 static bool xdr_old_ypbind_resp(xdrs
, ps
)
533 struct old_ypbind_resp
*ps
;
536 if (!xdr_enum(xdrs
, (enum_t
*)&ps
->ypbind_status
)) {
539 switch (ps
->ypbind_status
) {
540 case YPBIND_SUCC_VAL
:
541 return (xdr_old_yp_binding(xdrs
,
542 &ps
->ypbind_respbody
.ypbind_bindinfo
));
543 case YPBIND_FAIL_VAL
:
544 return (xdr_u_long(xdrs
,
545 &ps
->ypbind_respbody
.ypbind_error
));
549 /* This sends a message to the old ypbind process on host. */
550 static int old_call_binder(vers
)
555 int sock
= RPC_ANYSOCK
;
556 enum clnt_stat rpc_stat
;
557 struct old_ypbind_resp response
;
559 struct in_addr
*server
;
561 if ((client
= clnt_create(host
, YPBINDPROG
, vers
, "udp")) == NULL
) {
562 if (rpc_createerr
.cf_stat
== RPC_PROGNOTREGISTERED
) {
563 (void) printf("ypwhich: %s is not running ypbind\n",
567 if (rpc_createerr
.cf_stat
== RPC_PMAPFAILURE
) {
568 (void) printf("ypwhich: %s is not running port mapper\n",
572 (void) clnt_pcreateerror("ypwhich: clnt_create error");
576 rpc_stat
= clnt_call(client
, YPBINDPROC_DOMAIN
,
577 (xdrproc_t
)xdr_ypdomain_wrap_string
, (caddr_t
)&domain
,
578 (xdrproc_t
)xdr_old_ypbind_resp
, (caddr_t
)&response
,
581 if ((rpc_stat
!= RPC_SUCCESS
) &&
582 (rpc_stat
!= RPC_PROGVERSMISMATCH
)) {
583 (void) sprintf(errstring
,
584 "ypwhich: can't call ypbind on %s", host
);
585 (void) clnt_perror(client
, errstring
);
589 clnt_destroy(client
);
592 if ((rpc_stat
!= RPC_SUCCESS
) ||
593 (response
.ypbind_status
!= YPBIND_SUCC_VAL
)) {
597 server
= &response
.ypbind_respbody
.ypbind_bindinfo
.ypbind_binding_addr
;
598 print_server (server
);
605 * This translates a server address to a name and prints it.
606 * We'll get a name by using the standard library routine.
608 static void print_server(server
)
609 struct in_addr
*server
;
614 strcpy(buf
, inet_ntoa(*server
));
615 hp
= gethostbyaddr((char *)&server
->s_addr
,
616 sizeof (struct in_addr
), AF_INET
);
618 printf("%s\n", hp
? hp
->h_name
: buf
);
626 struct netconfig
*nc
;
633 b
= which
->ypbind_resp_u
.ypbind_bindinfo
;
635 (void) fprintf(stderr
, "???NO Binding information\n");
637 (void) fprintf(stderr
,
638 "server=%s lovers=%ld hivers=%ld\n",
639 b
->ypbind_servername
,
640 b
->ypbind_lo_vers
, b
->ypbind_hi_vers
);
641 nc
= b
->ypbind_nconf
;
642 ua
= b
->ypbind_svcaddr
;
644 (void) fprintf(stderr
,
645 "ypwhich: NO netconfig information\n");
647 (void) fprintf(stderr
,
648 "ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
649 nc
->nc_netid
, nc
->nc_device
,
650 (int)nc
->nc_flag
, nc
->nc_protofmly
,
654 (void) fprintf(stderr
,
655 "ypwhich: NO netbuf information available from binder\n");
657 (void) fprintf(stderr
,
658 "maxlen=%d len=%d\naddr=", ua
->maxlen
, ua
->len
);
659 for (i
= 0; i
< ua
->len
; i
++) {
660 if (i
!= (ua
->len
- 1))
661 (void) fprintf(stderr
,
664 (void) fprintf(stderr
,
675 * This translates a server address to a name and prints it. If the address
676 * is the same as the local address as returned by get_myaddress, the name
677 * is that retrieved from the kernel. If it's any other address (including
678 * another ip address for the local machine), we'll get a name by using the
679 * standard library routine (which calls the yp).
683 * This asks any yp server for the map's master.
691 err
= __yp_master_rsvdport(domain
, map
, &master
);
694 (void) fprintf(stderr
,
695 "ypwhich: Can't find the master of %s. Reason: %s.\n",
696 map
, yperr_string(err
));
699 (void) printf("%s\n", master
);
704 * This enumerates the entries within map "ypmaps" in the domain at global
705 * "domain", and prints them out key and value per single line. dump_ypmaps
706 * just decides whether we are (probably) able to speak the new YP protocol,
707 * and dispatches to the appropriate function.
713 struct dom_binding
*binding
;
715 if (err
= __yp_dobind(domain
, &binding
)) {
716 (void) fprintf(stderr
,
717 "dump_ypmaps: Can't bind for domain %s. Reason: %s\n",
718 domain
, yperr_string(err
));
722 if (binding
->dom_binding
->ypbind_hi_vers
>= YPVERS
) {
729 struct dom_binding
*binding
;
731 enum clnt_stat rpc_stat
;
734 struct ypmaplist
*pmpl
;
735 struct ypresp_maplist maplist
;
739 rpc_stat
= clnt_call(binding
->dom_client
, YPPROC_MAPLIST
,
740 (xdrproc_t
)xdr_ypdomain_wrap_string
, (caddr_t
)&domain
,
741 (xdrproc_t
)xdr_ypresp_maplist
, (caddr_t
)&maplist
,
744 if (rpc_stat
!= RPC_SUCCESS
) {
745 (void) clnt_perror(binding
->dom_client
,
746 "ypwhich(dumpmaps): can't get maplist");
747 __yp_rel_binding(binding
);
751 if (maplist
.status
!= YP_TRUE
) {
752 (void) fprintf(stderr
,
753 "ypwhich: Can't get maplist. Reason: %s.\n",
754 yperr_string(ypprot_err(maplist
.status
)));
757 __yp_rel_binding(binding
);
759 for (pmpl
= maplist
.list
; pmpl
; pmpl
= pmpl
->ypml_next
) {
760 (void) printf("%s ", pmpl
->ypml_name
);
762 err
= __yp_master_rsvdport(domain
, pmpl
->ypml_name
, &master
);
765 (void) printf("????????\n");
766 (void) fprintf(stderr
,
767 "ypwhich: Can't find the master of %s. Reason: %s.\n",
768 pmpl
->ypml_name
, yperr_string(err
));
770 (void) printf("%s\n", master
);