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
;
377 extern struct rpc_createerr rpc_createerr
;
379 struct utsname utsname
;
383 * CAUTION: Do not go to NIS if the host is the same as the local host
384 * XXX: Lots of special magic to distinguish between local and remote
385 * case. We want to make sure the local case doesn't hang.
388 if ((uname(&utsname
) != -1) &&
389 (strcmp(host
, utsname
.nodename
) == 0))
390 client
= __clnt_create_loopback(YPBINDPROG
, vers
, &yperr
);
392 client
= clnt_create(host
, YPBINDPROG
, vers
, "netpath");
393 if (client
== NULL
) {
395 (void) fprintf(stderr
,
396 "ypwhich: %s\n", yperr_string(yperr
));
398 if (rpc_createerr
.cf_stat
== RPC_PROGNOTREGISTERED
||
399 rpc_createerr
.cf_stat
== RPC_PROGUNAVAIL
) {
400 (void) fprintf(stderr
,
401 "ypwhich: %s is not running ypbind\n", host
);
402 } else if (rpc_createerr
.cf_stat
== RPC_PMAPFAILURE
) {
403 (void) fprintf(stderr
,
404 "ypwhich: %s is not running rpcbind\n",
407 (void) clnt_pcreateerror("ypwhich: \
412 ypbd
.ypbind_domainname
= domain
;
413 ypbd
.ypbind_vers
= vers
;
414 response
= ypbindproc_domain_3(&ypbd
, client
);
416 if (response
== NULL
) {
417 (void) sprintf(errstring
,
418 "ypwhich: can't call ypbind on %s", host
);
419 (void) clnt_perror(client
, errstring
);
423 clnt_destroy(client
);
425 if (response
->ypbind_status
!= YPBIND_SUCC_VAL
) {
429 if (response
->ypbind_resp_u
.ypbind_bindinfo
) {
431 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_servername
;
433 if (strcmp(server
, nullstring
) == 0) {
434 /* depends on a hack in ypbind */
435 struct nd_hostservlist
*nhs
= NULL
;
436 struct netconfig
*nconf
=
437 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_nconf
;
438 struct netbuf
*svcaddr
=
439 response
->ypbind_resp_u
.ypbind_bindinfo
->ypbind_svcaddr
;
441 if (netdir_getbyaddr(nconf
, &nhs
, svcaddr
) != ND_OK
) {
442 struct sockaddr_in
*sa4
;
443 struct sockaddr_in6
*sa6
;
444 char buf
[INET6_ADDRSTRLEN
];
445 char xbuf
[IPV6_ADDR_LEN
];
450 sa4
= (struct sockaddr_in
*)svcaddr
->buf
;
451 af
= ntohs(sa4
->sin_family
);
452 if (af
!= sa4
->sin_family
) {
454 (caddr_t
)xbuf
, IPV6_ADDR_LEN
,
456 if (af
== AF_INET6
) {
458 (caddr_t
)svcaddr
->buf
,
460 sa6
= (struct sockaddr_in6
*)
462 addr
= &sa6
->sin6_addr
;
465 (caddr_t
)svcaddr
->buf
,
467 sa4
= (struct sockaddr_in
*)
469 addr
= &sa4
->sin_addr
;
472 if (af
== AF_INET6
) {
473 sa6
= (struct sockaddr_in6
*)
475 addr
= &sa6
->sin6_addr
;
477 addr
= &sa4
->sin_addr
;
480 str
= inet_ntop(af
, addr
, buf
, sizeof (buf
));
484 fprintf(stdout
, "%s\n", str
);
486 str
= nhs
->h_hostservs
->h_host
;
489 fprintf(stdout
, "%s\n", str
);
491 netdir_free((char *)nhs
, ND_HOSTSERVLIST
);
493 fprintf(stdout
, "%s\n", server
);
497 dump_response(response
);
503 * Serializes/deserializes an in_addr struct.
505 * Note: There is a data coupling between the "definition" of a struct
506 * in_addr implicit in this xdr routine, and the true data definition in
509 static bool xdr_yp_inaddr(xdrs
, ps
)
514 return (xdr_opaque(xdrs
, (caddr_t
)&ps
->s_addr
, 4));
518 * Serializes/deserializes an old ypbind_binding struct.
520 static bool xdr_old_yp_binding(xdrs
, ps
)
522 struct old_ypbind_binding
*ps
;
525 return (xdr_yp_inaddr(xdrs
, &ps
->ypbind_binding_addr
) &&
526 xdr_opaque(xdrs
, (caddr_t
)&ps
->ypbind_binding_port
, 2));
530 * Serializes/deserializes a ypbind_resp structure.
532 static bool xdr_old_ypbind_resp(xdrs
, ps
)
534 struct old_ypbind_resp
*ps
;
537 if (!xdr_enum(xdrs
, (enum_t
*)&ps
->ypbind_status
)) {
540 switch (ps
->ypbind_status
) {
541 case YPBIND_SUCC_VAL
:
542 return (xdr_old_yp_binding(xdrs
,
543 &ps
->ypbind_respbody
.ypbind_bindinfo
));
544 case YPBIND_FAIL_VAL
:
545 return (xdr_u_long(xdrs
,
546 &ps
->ypbind_respbody
.ypbind_error
));
550 /* This sends a message to the old ypbind process on host. */
551 static int old_call_binder(vers
)
556 int sock
= RPC_ANYSOCK
;
557 enum clnt_stat rpc_stat
;
558 struct old_ypbind_resp response
;
560 extern struct rpc_createerr rpc_createerr
;
561 struct in_addr
*server
;
563 if ((client
= clnt_create(host
, YPBINDPROG
, vers
, "udp")) == NULL
) {
564 if (rpc_createerr
.cf_stat
== RPC_PROGNOTREGISTERED
) {
565 (void) printf("ypwhich: %s is not running ypbind\n",
569 if (rpc_createerr
.cf_stat
== RPC_PMAPFAILURE
) {
570 (void) printf("ypwhich: %s is not running port mapper\n",
574 (void) clnt_pcreateerror("ypwhich: clnt_create error");
578 rpc_stat
= clnt_call(client
, YPBINDPROC_DOMAIN
,
579 (xdrproc_t
)xdr_ypdomain_wrap_string
, (caddr_t
)&domain
,
580 (xdrproc_t
)xdr_old_ypbind_resp
, (caddr_t
)&response
,
583 if ((rpc_stat
!= RPC_SUCCESS
) &&
584 (rpc_stat
!= RPC_PROGVERSMISMATCH
)) {
585 (void) sprintf(errstring
,
586 "ypwhich: can't call ypbind on %s", host
);
587 (void) clnt_perror(client
, errstring
);
591 clnt_destroy(client
);
594 if ((rpc_stat
!= RPC_SUCCESS
) ||
595 (response
.ypbind_status
!= YPBIND_SUCC_VAL
)) {
599 server
= &response
.ypbind_respbody
.ypbind_bindinfo
.ypbind_binding_addr
;
600 print_server (server
);
607 * This translates a server address to a name and prints it.
608 * We'll get a name by using the standard library routine.
610 static void print_server(server
)
611 struct in_addr
*server
;
616 strcpy(buf
, inet_ntoa(*server
));
617 hp
= gethostbyaddr((char *)&server
->s_addr
,
618 sizeof (struct in_addr
), AF_INET
);
620 printf("%s\n", hp
? hp
->h_name
: buf
);
628 struct netconfig
*nc
;
635 b
= which
->ypbind_resp_u
.ypbind_bindinfo
;
637 (void) fprintf(stderr
, "???NO Binding information\n");
639 (void) fprintf(stderr
,
640 "server=%s lovers=%ld hivers=%ld\n",
641 b
->ypbind_servername
,
642 b
->ypbind_lo_vers
, b
->ypbind_hi_vers
);
643 nc
= b
->ypbind_nconf
;
644 ua
= b
->ypbind_svcaddr
;
646 (void) fprintf(stderr
,
647 "ypwhich: NO netconfig information\n");
649 (void) fprintf(stderr
,
650 "ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
651 nc
->nc_netid
, nc
->nc_device
,
652 (int)nc
->nc_flag
, nc
->nc_protofmly
,
656 (void) fprintf(stderr
,
657 "ypwhich: NO netbuf information available from binder\n");
659 (void) fprintf(stderr
,
660 "maxlen=%d len=%d\naddr=", ua
->maxlen
, ua
->len
);
661 for (i
= 0; i
< ua
->len
; i
++) {
662 if (i
!= (ua
->len
- 1))
663 (void) fprintf(stderr
,
666 (void) fprintf(stderr
,
677 * This translates a server address to a name and prints it. If the address
678 * is the same as the local address as returned by get_myaddress, the name
679 * is that retrieved from the kernel. If it's any other address (including
680 * another ip address for the local machine), we'll get a name by using the
681 * standard library routine (which calls the yp).
685 * This asks any yp server for the map's master.
693 err
= __yp_master_rsvdport(domain
, map
, &master
);
696 (void) fprintf(stderr
,
697 "ypwhich: Can't find the master of %s. Reason: %s.\n",
698 map
, yperr_string(err
));
701 (void) printf("%s\n", master
);
706 * This enumerates the entries within map "ypmaps" in the domain at global
707 * "domain", and prints them out key and value per single line. dump_ypmaps
708 * just decides whether we are (probably) able to speak the new YP protocol,
709 * and dispatches to the appropriate function.
715 struct dom_binding
*binding
;
717 if (err
= __yp_dobind(domain
, &binding
)) {
718 (void) fprintf(stderr
,
719 "dump_ypmaps: Can't bind for domain %s. Reason: %s\n",
720 domain
, yperr_string(err
));
724 if (binding
->dom_binding
->ypbind_hi_vers
>= YPVERS
) {
731 struct dom_binding
*binding
;
733 enum clnt_stat rpc_stat
;
736 struct ypmaplist
*pmpl
;
737 struct ypresp_maplist maplist
;
739 maplist
.list
= (struct ypmaplist
*)NULL
;
741 rpc_stat
= clnt_call(binding
->dom_client
, YPPROC_MAPLIST
,
742 (xdrproc_t
)xdr_ypdomain_wrap_string
, (caddr_t
)&domain
,
743 (xdrproc_t
)xdr_ypresp_maplist
, (caddr_t
)&maplist
,
746 if (rpc_stat
!= RPC_SUCCESS
) {
747 (void) clnt_perror(binding
->dom_client
,
748 "ypwhich(dumpmaps): can't get maplist");
749 __yp_rel_binding(binding
);
753 if (maplist
.status
!= YP_TRUE
) {
754 (void) fprintf(stderr
,
755 "ypwhich: Can't get maplist. Reason: %s.\n",
756 yperr_string(ypprot_err(maplist
.status
)));
759 __yp_rel_binding(binding
);
761 for (pmpl
= maplist
.list
; pmpl
; pmpl
= pmpl
->ypml_next
) {
762 (void) printf("%s ", pmpl
->ypml_name
);
764 err
= __yp_master_rsvdport(domain
, pmpl
->ypml_name
, &master
);
767 (void) printf("????????\n");
768 (void) fprintf(stderr
,
769 "ypwhich: Can't find the master of %s. Reason: %s.\n",
770 pmpl
->ypml_name
, yperr_string(err
));
772 (void) printf("%s\n", master
);