xref: /onnv-gate/usr/src/cmd/ypcmd/ypwhich.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  *
22*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * This is a user command which tells which yp server is being used by a
390Sstevel@tonic-gate  * given machine, or which yp server is the master for a named map.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * Usage is:
420Sstevel@tonic-gate  *	ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
430Sstevel@tonic-gate  *	ypwhich -x
440Sstevel@tonic-gate  * where:  the -d switch can be used to specify a domain other than the
450Sstevel@tonic-gate  * default domain.  -m tells the master of that map.  mname is a mapname
460Sstevel@tonic-gate  * If the -m option is used, ypwhich will act like a vanilla yp client,
470Sstevel@tonic-gate  * and will not attempt to choose a particular yp server.  On the
480Sstevel@tonic-gate  * other hand, if no -m switch is used, ypwhich will talk directly to the yp
490Sstevel@tonic-gate  * bind process on the named host, or to the local ypbind process if no host
500Sstevel@tonic-gate  * name is specified. -t switch inhibits nickname translation of map names.
510Sstevel@tonic-gate  * -x is to dump the nickname translation table from file /var/yp/nicknames.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <stdio.h>
560Sstevel@tonic-gate #include <ctype.h>
570Sstevel@tonic-gate #include <rpc/rpc.h>
580Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
590Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
600Sstevel@tonic-gate #include "yp_b.h"
610Sstevel@tonic-gate #include "ypv2_bind.h"
620Sstevel@tonic-gate #include <string.h>
630Sstevel@tonic-gate #include <netdir.h>
640Sstevel@tonic-gate #include <unistd.h>
650Sstevel@tonic-gate #include <netdb.h>
660Sstevel@tonic-gate #include <arpa/inet.h>
670Sstevel@tonic-gate #include <inet/ip.h>
680Sstevel@tonic-gate #include <inet/ip6.h>
690Sstevel@tonic-gate #include <netinet/ip6.h>
700Sstevel@tonic-gate #include <sys/utsname.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	YPSLEEPTIME 5 /* between two tries of bind */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
750Sstevel@tonic-gate #define	INTER_TRY 10			/* Seconds between tries */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int translate = TRUE;
780Sstevel@tonic-gate static int dodump = FALSE;
790Sstevel@tonic-gate static char *domain = NULL;
800Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
810Sstevel@tonic-gate static char *host = NULL;
820Sstevel@tonic-gate static int vers = YPBINDVERS;
830Sstevel@tonic-gate static char default_host_name[256];
840Sstevel@tonic-gate static bool get_master = FALSE;
850Sstevel@tonic-gate static bool get_server = FALSE;
860Sstevel@tonic-gate static char *map = NULL;
870Sstevel@tonic-gate static char nm[YPMAXMAP+1];
880Sstevel@tonic-gate static struct timeval timeout = {
890Sstevel@tonic-gate 		TIMEOUT,			/* Seconds */
900Sstevel@tonic-gate 		0				/* Microseconds */
910Sstevel@tonic-gate };
920Sstevel@tonic-gate static char nullstring[] = "\000";
930Sstevel@tonic-gate static char err_usage[] =
940Sstevel@tonic-gate "Usage:\n\
950Sstevel@tonic-gate 	ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
960Sstevel@tonic-gate 	ypwhich -x\n\
970Sstevel@tonic-gate where\n\
980Sstevel@tonic-gate 	mname may be either a mapname or a nickname for a map.\n\
990Sstevel@tonic-gate 	host if specified, is the machine whose NIS server is to be found.\n\
1000Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
1010Sstevel@tonic-gate 	-Vn version of ypbind, V3 is default.\n\
1020Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
1030Sstevel@tonic-gate static char err_bad_args[] =
1040Sstevel@tonic-gate "ypwhich:  %s argument is bad.\n";
1050Sstevel@tonic-gate static char err_cant_get_kname[] =
1060Sstevel@tonic-gate "ypwhich:  can't get %s back from system call.\n";
1070Sstevel@tonic-gate static char err_null_kname[] =
1080Sstevel@tonic-gate "ypwhich:  the %s hasn't been set on this machine.\n";
1090Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
1100Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
1110Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static void get_command_line_args();
1140Sstevel@tonic-gate static void getdomain();
1150Sstevel@tonic-gate static void getlochost();
1160Sstevel@tonic-gate static void get_server_name();
1170Sstevel@tonic-gate static int call_binder();
1180Sstevel@tonic-gate static void get_map_master();
1190Sstevel@tonic-gate extern void maketable();
1200Sstevel@tonic-gate extern int getmapname();
1210Sstevel@tonic-gate #ifdef DEBUG
1220Sstevel@tonic-gate static void dump_response();
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate static void dump_ypmaps();
1250Sstevel@tonic-gate static void dumpmaps();
1260Sstevel@tonic-gate 
127*702Sth160488 static bool xdr_yp_inaddr();
128*702Sth160488 static bool xdr_old_ypbind_resp();
1290Sstevel@tonic-gate static bool xdr_old_yp_binding();
1300Sstevel@tonic-gate static int old_call_binder();
1310Sstevel@tonic-gate static void print_server();
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* need these for call to (remote) V2 ypbind */
1340Sstevel@tonic-gate struct old_ypbind_binding {
1350Sstevel@tonic-gate 	struct in_addr ypbind_binding_addr;	/* In network order */
1360Sstevel@tonic-gate 	unsigned short int ypbind_binding_port;	/* In network order */
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate struct old_ypbind_resp {
1400Sstevel@tonic-gate 	enum ypbind_resptype ypbind_status;
1410Sstevel@tonic-gate 	union {
1420Sstevel@tonic-gate 		unsigned long ypbind_error;
1430Sstevel@tonic-gate 		struct old_ypbind_binding ypbind_bindinfo;
1440Sstevel@tonic-gate 	} ypbind_respbody;
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * This is the main line for the ypwhich process.
1490Sstevel@tonic-gate  */
150*702Sth160488 int
main(argc,argv)1510Sstevel@tonic-gate main(argc, argv)
1520Sstevel@tonic-gate char **argv;
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (dodump) {
1570Sstevel@tonic-gate 		maketable(dodump);
1580Sstevel@tonic-gate 		exit(0);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!domain) {
1620Sstevel@tonic-gate 		getdomain();
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (map && translate && (strchr(map, '.') == NULL) &&
1660Sstevel@tonic-gate 		(getmapname(map, nm))) {
1670Sstevel@tonic-gate 		map = nm;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (get_server) {
1710Sstevel@tonic-gate 		if (!host)
1720Sstevel@tonic-gate 			getlochost();
1730Sstevel@tonic-gate 		get_server_name();
1740Sstevel@tonic-gate 	} else {
1750Sstevel@tonic-gate 		if (map)
1760Sstevel@tonic-gate 			get_map_master();
1770Sstevel@tonic-gate 		else
1780Sstevel@tonic-gate 			dump_ypmaps();
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	return (0);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * This does the command line argument processing.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1880Sstevel@tonic-gate get_command_line_args(argc, argv)
1890Sstevel@tonic-gate int argc;
1900Sstevel@tonic-gate char **argv;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	argv++;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (argc == 1) {
1960Sstevel@tonic-gate 		get_server = TRUE;
1970Sstevel@tonic-gate 		return;
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	while (--argc) {
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 			switch ((*argv)[1]) {
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 			case 'V':
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 				vers = atoi(argv[0]+2);
2090Sstevel@tonic-gate 				if (vers <  1) {
2100Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
2110Sstevel@tonic-gate 					exit(1);
2120Sstevel@tonic-gate 				}
2130Sstevel@tonic-gate 				argv++;
2140Sstevel@tonic-gate 				break;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 			case 'm':
2170Sstevel@tonic-gate 				get_master = TRUE;
2180Sstevel@tonic-gate 				argv++;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 				if (argc > 1) {
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 					if ((*(argv))[0] == '-') {
2230Sstevel@tonic-gate 						break;
2240Sstevel@tonic-gate 					}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 					argc--;
2270Sstevel@tonic-gate 					map = *argv;
2280Sstevel@tonic-gate 					argv++;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 					if ((int)strlen(map) > YPMAXMAP) {
2310Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
2320Sstevel@tonic-gate 						    err_bad_mapname);
2330Sstevel@tonic-gate 						exit(1);
2340Sstevel@tonic-gate 					}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 				}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 				break;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			case 'd':
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 				if (argc > 1) {
2430Sstevel@tonic-gate 					argv++;
2440Sstevel@tonic-gate 					argc--;
2450Sstevel@tonic-gate 					domain = *argv;
2460Sstevel@tonic-gate 					argv++;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 					if ((int)strlen(domain) > YPMAXDOMAIN) {
2490Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
2500Sstevel@tonic-gate 				err_bad_domainname);
2510Sstevel@tonic-gate 						exit(1);
2520Sstevel@tonic-gate 					}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 				} else {
2550Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
2560Sstevel@tonic-gate 					exit(1);
2570Sstevel@tonic-gate 				}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 				break;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 			case 't':
2620Sstevel@tonic-gate 				translate = FALSE;
2630Sstevel@tonic-gate 				argv++;
2640Sstevel@tonic-gate 				break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 			case 'x':
2670Sstevel@tonic-gate 				dodump = TRUE;
2680Sstevel@tonic-gate 				argv++;
2690Sstevel@tonic-gate 				break;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 			default:
2720Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
2730Sstevel@tonic-gate 				exit(1);
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		} else {
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 			if (get_server) {
2790Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
2800Sstevel@tonic-gate 				exit(1);
2810Sstevel@tonic-gate 			}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			get_server = TRUE;
2840Sstevel@tonic-gate 			host = *argv;
2850Sstevel@tonic-gate 			argv++;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 			if ((int)strlen(host) > 256) {
2880Sstevel@tonic-gate 				(void) fprintf(stderr,
2890Sstevel@tonic-gate 			err_bad_args, err_bad_hostname);
2900Sstevel@tonic-gate 				exit(1);
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (get_master && get_server) {
2960Sstevel@tonic-gate 		(void) fprintf(stderr, err_usage);
2970Sstevel@tonic-gate 		exit(1);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (!get_master && !get_server) {
3010Sstevel@tonic-gate 		get_server = TRUE;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
3070Sstevel@tonic-gate  * to something reasonable.  domain is set here.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate static void
getdomain()3100Sstevel@tonic-gate getdomain()
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
3130Sstevel@tonic-gate 		domain = default_domain_name;
3140Sstevel@tonic-gate 	} else {
3150Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
3160Sstevel@tonic-gate 		exit(1);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if ((int)strlen(domain) == 0) {
3200Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
3210Sstevel@tonic-gate 		exit(1);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * This gets the local hostname back from the kernel
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate static void
getlochost()3290Sstevel@tonic-gate getlochost()
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	struct utsname utsname;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (uname(&utsname) != -1) {
3340Sstevel@tonic-gate 		strcpy(default_host_name, utsname.nodename);
3350Sstevel@tonic-gate 		host = default_host_name;
3360Sstevel@tonic-gate 	} else {
3370Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
3380Sstevel@tonic-gate 		exit(1);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * This tries to find the name of the server to which the binder in question
3450Sstevel@tonic-gate  * is bound.  If one of the -Vx flags was specified, it will try only for
3460Sstevel@tonic-gate  * that protocol version, otherwise, it will start with the current version,
3470Sstevel@tonic-gate  * then drop back to the previous version.
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static void
get_server_name()3500Sstevel@tonic-gate get_server_name()
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	char *notbound = "Domain %s not bound on %s.\n";
3530Sstevel@tonic-gate 
354*702Sth160488 	if (vers >= 3) {
3550Sstevel@tonic-gate 		if (!call_binder(vers))
3560Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
3570Sstevel@tonic-gate 	} else {
3580Sstevel@tonic-gate 		if (!old_call_binder(vers))
3590Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * This sends a message to the ypbind process on the node with
3670Sstevel@tonic-gate  * the host name
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate static int
call_binder(vers)3700Sstevel@tonic-gate call_binder(vers)
3710Sstevel@tonic-gate int vers;
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	CLIENT *client;
3740Sstevel@tonic-gate 	struct ypbind_resp *response;
3750Sstevel@tonic-gate 	struct ypbind_domain ypbd;
3760Sstevel@tonic-gate 	char errstring[256];
3770Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
3780Sstevel@tonic-gate 	int yperr = 0;
3790Sstevel@tonic-gate 	struct utsname utsname;
3800Sstevel@tonic-gate 	const char *str;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * CAUTION: Do not go to NIS if the host is the same as the local host
3840Sstevel@tonic-gate 	 * XXX: Lots of special magic to distinguish between local and remote
3850Sstevel@tonic-gate 	 * case. We want to make sure the local case doesn't hang.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if ((uname(&utsname) != -1) &&
3890Sstevel@tonic-gate 		(strcmp(host, utsname.nodename) == 0))
3900Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, vers, &yperr);
3910Sstevel@tonic-gate 	else
3920Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG, vers, "netpath");
3930Sstevel@tonic-gate 	if (client == NULL) {
3940Sstevel@tonic-gate 		if (yperr)
3950Sstevel@tonic-gate 			(void) fprintf(stderr,
3960Sstevel@tonic-gate 				"ypwhich: %s\n", yperr_string(yperr));
3970Sstevel@tonic-gate 		else {
3980Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED ||
3990Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_PROGUNAVAIL) {
4000Sstevel@tonic-gate 				(void) fprintf(stderr,
4010Sstevel@tonic-gate 			"ypwhich: %s is not running ypbind\n", host);
4020Sstevel@tonic-gate 			} else if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
4030Sstevel@tonic-gate 				(void) fprintf(stderr,
4040Sstevel@tonic-gate 			"ypwhich: %s is not running rpcbind\n",
4050Sstevel@tonic-gate 					host);
4060Sstevel@tonic-gate 			} else
4070Sstevel@tonic-gate 				(void) clnt_pcreateerror("ypwhich: \
4080Sstevel@tonic-gate clnt_create error");
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		exit(1);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 	ypbd.ypbind_domainname = domain;
4130Sstevel@tonic-gate 	ypbd.ypbind_vers = vers;
4140Sstevel@tonic-gate 	response = ypbindproc_domain_3(&ypbd, client);
4150Sstevel@tonic-gate 
416*702Sth160488 	if (response == NULL) {
4170Sstevel@tonic-gate 		(void) sprintf(errstring,
4180Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
4190Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
4200Sstevel@tonic-gate 		exit(1);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	clnt_destroy(client);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (response->ypbind_status != YPBIND_SUCC_VAL)  {
4260Sstevel@tonic-gate 		return (FALSE);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (response->ypbind_resp_u.ypbind_bindinfo) {
4300Sstevel@tonic-gate 		char *server =
4310Sstevel@tonic-gate 	response->ypbind_resp_u.ypbind_bindinfo->ypbind_servername;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (strcmp(server, nullstring) == 0) {
4340Sstevel@tonic-gate 		/* depends on a hack in ypbind */
4350Sstevel@tonic-gate 			struct nd_hostservlist *nhs = NULL;
4360Sstevel@tonic-gate 			struct netconfig *nconf =
4370Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_nconf;
4380Sstevel@tonic-gate 			struct netbuf *svcaddr =
4390Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_svcaddr;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 			if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
4420Sstevel@tonic-gate 				struct sockaddr_in	*sa4;
4430Sstevel@tonic-gate 				struct sockaddr_in6	*sa6;
4440Sstevel@tonic-gate 				char			buf[INET6_ADDRSTRLEN];
4450Sstevel@tonic-gate 				char			xbuf[IPV6_ADDR_LEN];
4460Sstevel@tonic-gate 				int			af;
4470Sstevel@tonic-gate 				void			*addr;
4480Sstevel@tonic-gate 				XDR			xdrs;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 				sa4 = (struct sockaddr_in *)svcaddr->buf;
4510Sstevel@tonic-gate 				af = ntohs(sa4->sin_family);
4520Sstevel@tonic-gate 				if (af != sa4->sin_family) {
4530Sstevel@tonic-gate 					xdrmem_create(&xdrs,
4540Sstevel@tonic-gate 						(caddr_t)xbuf, IPV6_ADDR_LEN,
4550Sstevel@tonic-gate 						XDR_DECODE);
4560Sstevel@tonic-gate 					if (af == AF_INET6) {
4570Sstevel@tonic-gate 						xdr_opaque(&xdrs,
458*702Sth160488 							(caddr_t)svcaddr->buf,
4590Sstevel@tonic-gate 							IPV6_ADDR_LEN);
4600Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
4610Sstevel@tonic-gate 							xbuf;
4620Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
4630Sstevel@tonic-gate 					} else {
4640Sstevel@tonic-gate 						xdr_opaque(&xdrs,
4650Sstevel@tonic-gate 							(caddr_t)svcaddr->buf,
4660Sstevel@tonic-gate 							IPV4_ADDR_LEN);
4670Sstevel@tonic-gate 						sa4 = (struct sockaddr_in *)
4680Sstevel@tonic-gate 							xbuf;
4690Sstevel@tonic-gate 						addr = &sa4->sin_addr;
4700Sstevel@tonic-gate 					}
4710Sstevel@tonic-gate 				} else {
4720Sstevel@tonic-gate 					if (af == AF_INET6) {
4730Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
4740Sstevel@tonic-gate 							svcaddr->buf;
4750Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
4760Sstevel@tonic-gate 					} else {
4770Sstevel@tonic-gate 						addr = &sa4->sin_addr;
4780Sstevel@tonic-gate 					}
4790Sstevel@tonic-gate 				}
4800Sstevel@tonic-gate 				str = inet_ntop(af, addr, buf, sizeof (buf));
4810Sstevel@tonic-gate 				if (str == NULL)
4820Sstevel@tonic-gate 					perror("inet_ntop");
4830Sstevel@tonic-gate 				else
4840Sstevel@tonic-gate 					fprintf(stdout, "%s\n", str);
4850Sstevel@tonic-gate 			} else {
4860Sstevel@tonic-gate 				str = nhs->h_hostservs->h_host;
4870Sstevel@tonic-gate 				if (str == NULL)
4880Sstevel@tonic-gate 					str = "<unknown>";
4890Sstevel@tonic-gate 				fprintf(stdout, "%s\n", str);
4900Sstevel@tonic-gate 			}
4910Sstevel@tonic-gate 			netdir_free((char *)nhs, ND_HOSTSERVLIST);
4920Sstevel@tonic-gate 		} else {
4930Sstevel@tonic-gate 			fprintf(stdout, "%s\n", server);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate #ifdef DEBUG
4970Sstevel@tonic-gate 	dump_response(response);
4980Sstevel@tonic-gate #endif
4990Sstevel@tonic-gate 	return (TRUE);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * Serializes/deserializes an in_addr struct.
5040Sstevel@tonic-gate  *
5050Sstevel@tonic-gate  * Note:  There is a data coupling between the "definition" of a struct
5060Sstevel@tonic-gate  * in_addr implicit in this xdr routine, and the true data definition in
5070Sstevel@tonic-gate  * <netinet/in.h>.
5080Sstevel@tonic-gate  */
xdr_yp_inaddr(xdrs,ps)5090Sstevel@tonic-gate static bool xdr_yp_inaddr(xdrs, ps)
5100Sstevel@tonic-gate 	XDR * xdrs;
5110Sstevel@tonic-gate 	struct in_addr *ps;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (caddr_t)&ps->s_addr, 4));
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * Serializes/deserializes an old ypbind_binding struct.
5190Sstevel@tonic-gate  */
xdr_old_yp_binding(xdrs,ps)5200Sstevel@tonic-gate static bool xdr_old_yp_binding(xdrs, ps)
5210Sstevel@tonic-gate 	XDR * xdrs;
5220Sstevel@tonic-gate 	struct old_ypbind_binding *ps;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 	return (xdr_yp_inaddr(xdrs, &ps->ypbind_binding_addr) &&
5260Sstevel@tonic-gate 	    xdr_opaque(xdrs, (caddr_t)&ps->ypbind_binding_port, 2));
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate /*
5300Sstevel@tonic-gate  * Serializes/deserializes a ypbind_resp structure.
5310Sstevel@tonic-gate  */
xdr_old_ypbind_resp(xdrs,ps)5320Sstevel@tonic-gate static bool xdr_old_ypbind_resp(xdrs, ps)
5330Sstevel@tonic-gate 	XDR * xdrs;
5340Sstevel@tonic-gate 	struct old_ypbind_resp *ps;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate {
537*702Sth160488 	if (!xdr_enum(xdrs, (enum_t *)&ps->ypbind_status)) {
5380Sstevel@tonic-gate 		return (FALSE);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	switch (ps->ypbind_status) {
5410Sstevel@tonic-gate 	case YPBIND_SUCC_VAL:
5420Sstevel@tonic-gate 		return (xdr_old_yp_binding(xdrs,
5430Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_bindinfo));
5440Sstevel@tonic-gate 	case YPBIND_FAIL_VAL:
5450Sstevel@tonic-gate 		return (xdr_u_long(xdrs,
5460Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_error));
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	return (FALSE);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate /* This sends a message to the old ypbind process on host. */
old_call_binder(vers)5510Sstevel@tonic-gate static int old_call_binder(vers)
5520Sstevel@tonic-gate 	int vers;
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	CLIENT *client;
5550Sstevel@tonic-gate 	struct hostent *hp;
5560Sstevel@tonic-gate 	int sock = RPC_ANYSOCK;
5570Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
5580Sstevel@tonic-gate 	struct old_ypbind_resp response;
5590Sstevel@tonic-gate 	char errstring[256];
5600Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
5610Sstevel@tonic-gate 	struct in_addr *server;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if ((client = clnt_create(host, YPBINDPROG, vers, "udp")) == NULL) {
5640Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) {
5650Sstevel@tonic-gate 			(void) printf("ypwhich: %s is not running ypbind\n",
5660Sstevel@tonic-gate 				host);
5670Sstevel@tonic-gate 			exit(1);
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
5700Sstevel@tonic-gate 		    (void) printf("ypwhich: %s is not running port mapper\n",
5710Sstevel@tonic-gate 				host);
5720Sstevel@tonic-gate 			exit(1);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 		(void) clnt_pcreateerror("ypwhich:  clnt_create error");
5750Sstevel@tonic-gate 		exit(1);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	rpc_stat = clnt_call(client, YPBINDPROC_DOMAIN,
5790Sstevel@tonic-gate 			(xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
5800Sstevel@tonic-gate 			(xdrproc_t)xdr_old_ypbind_resp, (caddr_t)&response,
5810Sstevel@tonic-gate 			timeout);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) &&
5840Sstevel@tonic-gate 	    (rpc_stat != RPC_PROGVERSMISMATCH)) {
5850Sstevel@tonic-gate 		(void) sprintf(errstring,
5860Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
5870Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
5880Sstevel@tonic-gate 		exit(1);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	clnt_destroy(client);
5920Sstevel@tonic-gate 	close(sock);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) ||
5950Sstevel@tonic-gate 	    (response.ypbind_status != YPBIND_SUCC_VAL)) {
5960Sstevel@tonic-gate 		return (FALSE);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	server = &response.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
6000Sstevel@tonic-gate 	print_server  (server);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	return (TRUE);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate  * For old version:
6070Sstevel@tonic-gate  * This translates a server address to a name and prints it.
6080Sstevel@tonic-gate  * We'll get a name by using the standard library routine.
6090Sstevel@tonic-gate  */
print_server(server)6100Sstevel@tonic-gate static void print_server(server)
6110Sstevel@tonic-gate 	struct in_addr *server;
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	char buf[256];
6140Sstevel@tonic-gate 	struct hostent *hp;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	strcpy(buf, inet_ntoa(*server));
617*702Sth160488 	hp = gethostbyaddr((char *)&server->s_addr,
6180Sstevel@tonic-gate 			sizeof (struct in_addr), AF_INET);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	printf("%s\n", hp ? hp->h_name : buf);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate #ifdef DEBUG
6240Sstevel@tonic-gate static void
dump_response(which)6250Sstevel@tonic-gate dump_response(which)
6260Sstevel@tonic-gate ypbind_resp * which;
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	struct netconfig *nc;
6290Sstevel@tonic-gate 	struct netbuf *ua;
6300Sstevel@tonic-gate 	ypbind_binding * b;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	int i;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	{
6350Sstevel@tonic-gate 		b = which->ypbind_resp_u.ypbind_bindinfo;
6360Sstevel@tonic-gate 		if (b == NULL)
6370Sstevel@tonic-gate 			(void) fprintf(stderr, "???NO Binding information\n");
6380Sstevel@tonic-gate 		else {
6390Sstevel@tonic-gate 			(void) fprintf(stderr,
6400Sstevel@tonic-gate 		"server=%s lovers=%ld hivers=%ld\n",
6410Sstevel@tonic-gate 			    b->ypbind_servername,
6420Sstevel@tonic-gate 				b->ypbind_lo_vers, b->ypbind_hi_vers);
6430Sstevel@tonic-gate 			nc = b->ypbind_nconf;
6440Sstevel@tonic-gate 			ua = b->ypbind_svcaddr;
6450Sstevel@tonic-gate 			if (nc == NULL)
6460Sstevel@tonic-gate 				(void) fprintf(stderr,
6470Sstevel@tonic-gate 			"ypwhich: NO netconfig information\n");
6480Sstevel@tonic-gate 			else {
6490Sstevel@tonic-gate 				(void) fprintf(stderr,
6500Sstevel@tonic-gate 		"ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
6510Sstevel@tonic-gate 		nc->nc_netid, nc->nc_device,
652*702Sth160488 		(int)nc->nc_flag, nc->nc_protofmly,
6530Sstevel@tonic-gate 		nc->nc_proto);
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 			if (ua == NULL)
6560Sstevel@tonic-gate 				(void) fprintf(stderr,
6570Sstevel@tonic-gate 		"ypwhich: NO netbuf information available from binder\n");
6580Sstevel@tonic-gate 			else {
6590Sstevel@tonic-gate 				(void) fprintf(stderr,
6600Sstevel@tonic-gate 			"maxlen=%d len=%d\naddr=", ua->maxlen, ua->len);
6610Sstevel@tonic-gate 				for (i = 0; i < ua->len; i++) {
6620Sstevel@tonic-gate 					if (i != (ua->len - 1))
6630Sstevel@tonic-gate 						(void) fprintf(stderr,
6640Sstevel@tonic-gate 					"%d.", ua->buf[i]);
6650Sstevel@tonic-gate 					else
6660Sstevel@tonic-gate 						(void) fprintf(stderr,
6670Sstevel@tonic-gate 					"%d\n", ua->buf[i]);
6680Sstevel@tonic-gate 				}
6690Sstevel@tonic-gate 			}
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate #endif
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * This translates a server address to a name and prints it.  If the address
6780Sstevel@tonic-gate  * is the same as the local address as returned by get_myaddress, the name
6790Sstevel@tonic-gate  * is that retrieved from the kernel.  If it's any other address (including
6800Sstevel@tonic-gate  * another ip address for the local machine), we'll get a name by using the
6810Sstevel@tonic-gate  * standard library routine (which calls the yp).
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate  * This asks any yp server for the map's master.
6860Sstevel@tonic-gate  */
6870Sstevel@tonic-gate static void
get_map_master()6880Sstevel@tonic-gate get_map_master()
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	int err;
6910Sstevel@tonic-gate 	char *master;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	err = __yp_master_rsvdport(domain, map, &master);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (err) {
6960Sstevel@tonic-gate 		(void) fprintf(stderr,
6970Sstevel@tonic-gate 		    "ypwhich:  Can't find the master of %s.  Reason: %s.\n",
6980Sstevel@tonic-gate 		    map, yperr_string(err));
6990Sstevel@tonic-gate 		exit(1);
7000Sstevel@tonic-gate 	} else {
7010Sstevel@tonic-gate 		(void) printf("%s\n", master);
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * This enumerates the entries within map "ypmaps" in the domain at global
7070Sstevel@tonic-gate  * "domain", and prints them out key and value per single line.  dump_ypmaps
7080Sstevel@tonic-gate  * just decides whether we are (probably) able to speak the new YP protocol,
7090Sstevel@tonic-gate  * and dispatches to the appropriate function.
7100Sstevel@tonic-gate  */
7110Sstevel@tonic-gate static void
dump_ypmaps()7120Sstevel@tonic-gate dump_ypmaps()
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	int err;
7150Sstevel@tonic-gate 	struct dom_binding *binding;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	if (err = __yp_dobind(domain, &binding)) {
7180Sstevel@tonic-gate 		(void) fprintf(stderr,
7190Sstevel@tonic-gate 		    "dump_ypmaps: Can't bind for domain %s.  Reason: %s\n",
7200Sstevel@tonic-gate 		    domain, yperr_string(err));
7210Sstevel@tonic-gate 		return;
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	if (binding->dom_binding->ypbind_hi_vers  >= YPVERS) {
7250Sstevel@tonic-gate 		dumpmaps(binding);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate static void
dumpmaps(binding)7300Sstevel@tonic-gate dumpmaps(binding)
7310Sstevel@tonic-gate struct dom_binding *binding;
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
7340Sstevel@tonic-gate 	int err;
7350Sstevel@tonic-gate 	char *master;
7360Sstevel@tonic-gate 	struct ypmaplist *pmpl;
7370Sstevel@tonic-gate 	struct ypresp_maplist maplist;
7380Sstevel@tonic-gate 
739*702Sth160488 	maplist.list = (struct ypmaplist *)NULL;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	rpc_stat = clnt_call(binding->dom_client, YPPROC_MAPLIST,
742*702Sth160488 	    (xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
743*702Sth160488 	    (xdrproc_t)xdr_ypresp_maplist, (caddr_t)&maplist,
7440Sstevel@tonic-gate 	    timeout);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
7470Sstevel@tonic-gate 		(void) clnt_perror(binding->dom_client,
7480Sstevel@tonic-gate 		    "ypwhich(dumpmaps): can't get maplist");
7490Sstevel@tonic-gate 		__yp_rel_binding(binding);
7500Sstevel@tonic-gate 		exit(1);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (maplist.status != YP_TRUE) {
7540Sstevel@tonic-gate 		(void) fprintf(stderr,
7550Sstevel@tonic-gate 		    "ypwhich:  Can't get maplist.  Reason:  %s.\n",
7560Sstevel@tonic-gate 		    yperr_string(ypprot_err(maplist.status)));
7570Sstevel@tonic-gate 		exit(1);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	__yp_rel_binding(binding);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	for (pmpl = maplist.list; pmpl; pmpl = pmpl->ypml_next) {
7620Sstevel@tonic-gate 		(void) printf("%s ", pmpl->ypml_name);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		err = __yp_master_rsvdport(domain, pmpl->ypml_name, &master);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		if (err) {
7670Sstevel@tonic-gate 			(void) printf("????????\n");
7680Sstevel@tonic-gate 			(void) fprintf(stderr,
7690Sstevel@tonic-gate 		"ypwhich:  Can't find the master of %s.  Reason: %s.\n",
7700Sstevel@tonic-gate 		pmpl->ypml_name, yperr_string(err));
7710Sstevel@tonic-gate 		} else {
7720Sstevel@tonic-gate 			(void) printf("%s\n", master);
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate }
776