xref: /illumos-gate/usr/src/cmd/ypcmd/ypwhich.c (revision f83283b9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
22a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This is a user command which tells which yp server is being used by a
377c478bd9Sstevel@tonic-gate  * given machine, or which yp server is the master for a named map.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * Usage is:
407c478bd9Sstevel@tonic-gate  *	ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
417c478bd9Sstevel@tonic-gate  *	ypwhich -x
427c478bd9Sstevel@tonic-gate  * where:  the -d switch can be used to specify a domain other than the
437c478bd9Sstevel@tonic-gate  * default domain.  -m tells the master of that map.  mname is a mapname
447c478bd9Sstevel@tonic-gate  * If the -m option is used, ypwhich will act like a vanilla yp client,
457c478bd9Sstevel@tonic-gate  * and will not attempt to choose a particular yp server.  On the
467c478bd9Sstevel@tonic-gate  * other hand, if no -m switch is used, ypwhich will talk directly to the yp
477c478bd9Sstevel@tonic-gate  * bind process on the named host, or to the local ypbind process if no host
487c478bd9Sstevel@tonic-gate  * name is specified. -t switch inhibits nickname translation of map names.
497c478bd9Sstevel@tonic-gate  * -x is to dump the nickname translation table from file /var/yp/nicknames.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <ctype.h>
557c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
567c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
577c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
587c478bd9Sstevel@tonic-gate #include "yp_b.h"
597c478bd9Sstevel@tonic-gate #include "ypv2_bind.h"
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <netdir.h>
627c478bd9Sstevel@tonic-gate #include <unistd.h>
637c478bd9Sstevel@tonic-gate #include <netdb.h>
647c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
657c478bd9Sstevel@tonic-gate #include <inet/ip.h>
667c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
677c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
687c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	YPSLEEPTIME 5 /* between two tries of bind */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
737c478bd9Sstevel@tonic-gate #define	INTER_TRY 10			/* Seconds between tries */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int translate = TRUE;
767c478bd9Sstevel@tonic-gate static int dodump = FALSE;
777c478bd9Sstevel@tonic-gate static char *domain = NULL;
787c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
797c478bd9Sstevel@tonic-gate static char *host = NULL;
807c478bd9Sstevel@tonic-gate static int vers = YPBINDVERS;
817c478bd9Sstevel@tonic-gate static char default_host_name[256];
827c478bd9Sstevel@tonic-gate static bool get_master = FALSE;
837c478bd9Sstevel@tonic-gate static bool get_server = FALSE;
847c478bd9Sstevel@tonic-gate static char *map = NULL;
857c478bd9Sstevel@tonic-gate static char nm[YPMAXMAP+1];
867c478bd9Sstevel@tonic-gate static struct timeval timeout = {
877c478bd9Sstevel@tonic-gate 		TIMEOUT,			/* Seconds */
887c478bd9Sstevel@tonic-gate 		0				/* Microseconds */
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate static char nullstring[] = "\000";
917c478bd9Sstevel@tonic-gate static char err_usage[] =
927c478bd9Sstevel@tonic-gate "Usage:\n\
937c478bd9Sstevel@tonic-gate 	ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
947c478bd9Sstevel@tonic-gate 	ypwhich -x\n\
957c478bd9Sstevel@tonic-gate where\n\
967c478bd9Sstevel@tonic-gate 	mname may be either a mapname or a nickname for a map.\n\
977c478bd9Sstevel@tonic-gate 	host if specified, is the machine whose NIS server is to be found.\n\
987c478bd9Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
997c478bd9Sstevel@tonic-gate 	-Vn version of ypbind, V3 is default.\n\
1007c478bd9Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
1017c478bd9Sstevel@tonic-gate static char err_bad_args[] =
1027c478bd9Sstevel@tonic-gate "ypwhich:  %s argument is bad.\n";
1037c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
1047c478bd9Sstevel@tonic-gate "ypwhich:  can't get %s back from system call.\n";
1057c478bd9Sstevel@tonic-gate static char err_null_kname[] =
1067c478bd9Sstevel@tonic-gate "ypwhich:  the %s hasn't been set on this machine.\n";
1077c478bd9Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
1087c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
1097c478bd9Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void get_command_line_args();
1127c478bd9Sstevel@tonic-gate static void getdomain();
1137c478bd9Sstevel@tonic-gate static void getlochost();
1147c478bd9Sstevel@tonic-gate static void get_server_name();
1157c478bd9Sstevel@tonic-gate static int call_binder();
1167c478bd9Sstevel@tonic-gate static void get_map_master();
1177c478bd9Sstevel@tonic-gate extern void maketable();
1187c478bd9Sstevel@tonic-gate extern int getmapname();
1197c478bd9Sstevel@tonic-gate #ifdef DEBUG
1207c478bd9Sstevel@tonic-gate static void dump_response();
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate static void dump_ypmaps();
1237c478bd9Sstevel@tonic-gate static void dumpmaps();
1247c478bd9Sstevel@tonic-gate 
125a506a34cSth static bool xdr_yp_inaddr();
126a506a34cSth static bool xdr_old_ypbind_resp();
1277c478bd9Sstevel@tonic-gate static bool xdr_old_yp_binding();
1287c478bd9Sstevel@tonic-gate static int old_call_binder();
1297c478bd9Sstevel@tonic-gate static void print_server();
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* need these for call to (remote) V2 ypbind */
1327c478bd9Sstevel@tonic-gate struct old_ypbind_binding {
1337c478bd9Sstevel@tonic-gate 	struct in_addr ypbind_binding_addr;	/* In network order */
1347c478bd9Sstevel@tonic-gate 	unsigned short int ypbind_binding_port;	/* In network order */
1357c478bd9Sstevel@tonic-gate };
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate struct old_ypbind_resp {
1387c478bd9Sstevel@tonic-gate 	enum ypbind_resptype ypbind_status;
1397c478bd9Sstevel@tonic-gate 	union {
1407c478bd9Sstevel@tonic-gate 		unsigned long ypbind_error;
1417c478bd9Sstevel@tonic-gate 		struct old_ypbind_binding ypbind_bindinfo;
1427c478bd9Sstevel@tonic-gate 	} ypbind_respbody;
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * This is the main line for the ypwhich process.
1477c478bd9Sstevel@tonic-gate  */
148a506a34cSth int
main(int argc,char ** argv)149*f83283b9SToomas Soome main(int argc, char **argv)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (dodump) {
1547c478bd9Sstevel@tonic-gate 		maketable(dodump);
1557c478bd9Sstevel@tonic-gate 		exit(0);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (!domain) {
1597c478bd9Sstevel@tonic-gate 		getdomain();
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (map && translate && (strchr(map, '.') == NULL) &&
1637c478bd9Sstevel@tonic-gate 		(getmapname(map, nm))) {
1647c478bd9Sstevel@tonic-gate 		map = nm;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (get_server) {
1687c478bd9Sstevel@tonic-gate 		if (!host)
1697c478bd9Sstevel@tonic-gate 			getlochost();
1707c478bd9Sstevel@tonic-gate 		get_server_name();
1717c478bd9Sstevel@tonic-gate 	} else {
1727c478bd9Sstevel@tonic-gate 		if (map)
1737c478bd9Sstevel@tonic-gate 			get_map_master();
1747c478bd9Sstevel@tonic-gate 		else
1757c478bd9Sstevel@tonic-gate 			dump_ypmaps();
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1857c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
1867c478bd9Sstevel@tonic-gate int argc;
1877c478bd9Sstevel@tonic-gate char **argv;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	argv++;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1937c478bd9Sstevel@tonic-gate 		get_server = TRUE;
1947c478bd9Sstevel@tonic-gate 		return;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	while (--argc) {
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			switch ((*argv)[1]) {
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 			case 'V':
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 				vers = atoi(argv[0]+2);
2067c478bd9Sstevel@tonic-gate 				if (vers <  1) {
2077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
2087c478bd9Sstevel@tonic-gate 					exit(1);
2097c478bd9Sstevel@tonic-gate 				}
2107c478bd9Sstevel@tonic-gate 				argv++;
2117c478bd9Sstevel@tonic-gate 				break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 			case 'm':
2147c478bd9Sstevel@tonic-gate 				get_master = TRUE;
2157c478bd9Sstevel@tonic-gate 				argv++;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 				if (argc > 1) {
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 					if ((*(argv))[0] == '-') {
2207c478bd9Sstevel@tonic-gate 						break;
2217c478bd9Sstevel@tonic-gate 					}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 					argc--;
2247c478bd9Sstevel@tonic-gate 					map = *argv;
2257c478bd9Sstevel@tonic-gate 					argv++;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 					if ((int)strlen(map) > YPMAXMAP) {
2287c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
2297c478bd9Sstevel@tonic-gate 						    err_bad_mapname);
2307c478bd9Sstevel@tonic-gate 						exit(1);
2317c478bd9Sstevel@tonic-gate 					}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 				break;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 			case 'd':
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 				if (argc > 1) {
2407c478bd9Sstevel@tonic-gate 					argv++;
2417c478bd9Sstevel@tonic-gate 					argc--;
2427c478bd9Sstevel@tonic-gate 					domain = *argv;
2437c478bd9Sstevel@tonic-gate 					argv++;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 					if ((int)strlen(domain) > YPMAXDOMAIN) {
2467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
2477c478bd9Sstevel@tonic-gate 				err_bad_domainname);
2487c478bd9Sstevel@tonic-gate 						exit(1);
2497c478bd9Sstevel@tonic-gate 					}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 				} else {
2527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
2537c478bd9Sstevel@tonic-gate 					exit(1);
2547c478bd9Sstevel@tonic-gate 				}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 				break;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			case 't':
2597c478bd9Sstevel@tonic-gate 				translate = FALSE;
2607c478bd9Sstevel@tonic-gate 				argv++;
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			case 'x':
2647c478bd9Sstevel@tonic-gate 				dodump = TRUE;
2657c478bd9Sstevel@tonic-gate 				argv++;
2667c478bd9Sstevel@tonic-gate 				break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			default:
2697c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
2707c478bd9Sstevel@tonic-gate 				exit(1);
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			if (get_server) {
2767c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
2777c478bd9Sstevel@tonic-gate 				exit(1);
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			get_server = TRUE;
2817c478bd9Sstevel@tonic-gate 			host = *argv;
2827c478bd9Sstevel@tonic-gate 			argv++;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 			if ((int)strlen(host) > 256) {
2857c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2867c478bd9Sstevel@tonic-gate 			err_bad_args, err_bad_hostname);
2877c478bd9Sstevel@tonic-gate 				exit(1);
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (get_master && get_server) {
2937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_usage);
2947c478bd9Sstevel@tonic-gate 		exit(1);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (!get_master && !get_server) {
2987c478bd9Sstevel@tonic-gate 		get_server = TRUE;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
3047c478bd9Sstevel@tonic-gate  * to something reasonable.  domain is set here.
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate static void
getdomain()3077c478bd9Sstevel@tonic-gate getdomain()
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
3107c478bd9Sstevel@tonic-gate 		domain = default_domain_name;
3117c478bd9Sstevel@tonic-gate 	} else {
3127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
3137c478bd9Sstevel@tonic-gate 		exit(1);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if ((int)strlen(domain) == 0) {
3177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
3187c478bd9Sstevel@tonic-gate 		exit(1);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * This gets the local hostname back from the kernel
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate static void
getlochost()3267c478bd9Sstevel@tonic-gate getlochost()
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	struct utsname utsname;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (uname(&utsname) != -1) {
3317c478bd9Sstevel@tonic-gate 		strcpy(default_host_name, utsname.nodename);
3327c478bd9Sstevel@tonic-gate 		host = default_host_name;
3337c478bd9Sstevel@tonic-gate 	} else {
3347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
3357c478bd9Sstevel@tonic-gate 		exit(1);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * This tries to find the name of the server to which the binder in question
3427c478bd9Sstevel@tonic-gate  * is bound.  If one of the -Vx flags was specified, it will try only for
3437c478bd9Sstevel@tonic-gate  * that protocol version, otherwise, it will start with the current version,
3447c478bd9Sstevel@tonic-gate  * then drop back to the previous version.
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate static void
get_server_name()3477c478bd9Sstevel@tonic-gate get_server_name()
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	char *notbound = "Domain %s not bound on %s.\n";
3507c478bd9Sstevel@tonic-gate 
351a506a34cSth 	if (vers >= 3) {
3527c478bd9Sstevel@tonic-gate 		if (!call_binder(vers))
3537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
3547c478bd9Sstevel@tonic-gate 	} else {
3557c478bd9Sstevel@tonic-gate 		if (!old_call_binder(vers))
3567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * This sends a message to the ypbind process on the node with
3647c478bd9Sstevel@tonic-gate  * the host name
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate static int
call_binder(vers)3677c478bd9Sstevel@tonic-gate call_binder(vers)
3687c478bd9Sstevel@tonic-gate int vers;
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	CLIENT *client;
3717c478bd9Sstevel@tonic-gate 	struct ypbind_resp *response;
3727c478bd9Sstevel@tonic-gate 	struct ypbind_domain ypbd;
3737c478bd9Sstevel@tonic-gate 	char errstring[256];
3747c478bd9Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
3757c478bd9Sstevel@tonic-gate 	int yperr = 0;
3767c478bd9Sstevel@tonic-gate 	struct utsname utsname;
3777c478bd9Sstevel@tonic-gate 	const char *str;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * CAUTION: Do not go to NIS if the host is the same as the local host
3817c478bd9Sstevel@tonic-gate 	 * XXX: Lots of special magic to distinguish between local and remote
3827c478bd9Sstevel@tonic-gate 	 * case. We want to make sure the local case doesn't hang.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if ((uname(&utsname) != -1) &&
3867c478bd9Sstevel@tonic-gate 		(strcmp(host, utsname.nodename) == 0))
3877c478bd9Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, vers, &yperr);
3887c478bd9Sstevel@tonic-gate 	else
3897c478bd9Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG, vers, "netpath");
3907c478bd9Sstevel@tonic-gate 	if (client == NULL) {
3917c478bd9Sstevel@tonic-gate 		if (yperr)
3927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3937c478bd9Sstevel@tonic-gate 				"ypwhich: %s\n", yperr_string(yperr));
3947c478bd9Sstevel@tonic-gate 		else {
3957c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED ||
3967c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_PROGUNAVAIL) {
3977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3987c478bd9Sstevel@tonic-gate 			"ypwhich: %s is not running ypbind\n", host);
3997c478bd9Sstevel@tonic-gate 			} else if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
4007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4017c478bd9Sstevel@tonic-gate 			"ypwhich: %s is not running rpcbind\n",
4027c478bd9Sstevel@tonic-gate 					host);
4037c478bd9Sstevel@tonic-gate 			} else
4047c478bd9Sstevel@tonic-gate 				(void) clnt_pcreateerror("ypwhich: \
4057c478bd9Sstevel@tonic-gate clnt_create error");
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 		exit(1);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	ypbd.ypbind_domainname = domain;
4107c478bd9Sstevel@tonic-gate 	ypbd.ypbind_vers = vers;
4117c478bd9Sstevel@tonic-gate 	response = ypbindproc_domain_3(&ypbd, client);
4127c478bd9Sstevel@tonic-gate 
413a506a34cSth 	if (response == NULL) {
4147c478bd9Sstevel@tonic-gate 		(void) sprintf(errstring,
4157c478bd9Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
4167c478bd9Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
4177c478bd9Sstevel@tonic-gate 		exit(1);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (response->ypbind_status != YPBIND_SUCC_VAL)  {
4237c478bd9Sstevel@tonic-gate 		return (FALSE);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (response->ypbind_resp_u.ypbind_bindinfo) {
4277c478bd9Sstevel@tonic-gate 		char *server =
4287c478bd9Sstevel@tonic-gate 	response->ypbind_resp_u.ypbind_bindinfo->ypbind_servername;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if (strcmp(server, nullstring) == 0) {
4317c478bd9Sstevel@tonic-gate 		/* depends on a hack in ypbind */
4327c478bd9Sstevel@tonic-gate 			struct nd_hostservlist *nhs = NULL;
4337c478bd9Sstevel@tonic-gate 			struct netconfig *nconf =
4347c478bd9Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_nconf;
4357c478bd9Sstevel@tonic-gate 			struct netbuf *svcaddr =
4367c478bd9Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_svcaddr;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 			if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
4397c478bd9Sstevel@tonic-gate 				struct sockaddr_in	*sa4;
4407c478bd9Sstevel@tonic-gate 				struct sockaddr_in6	*sa6;
4417c478bd9Sstevel@tonic-gate 				char			buf[INET6_ADDRSTRLEN];
4427c478bd9Sstevel@tonic-gate 				char			xbuf[IPV6_ADDR_LEN];
4437c478bd9Sstevel@tonic-gate 				int			af;
4447c478bd9Sstevel@tonic-gate 				void			*addr;
4457c478bd9Sstevel@tonic-gate 				XDR			xdrs;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 				sa4 = (struct sockaddr_in *)svcaddr->buf;
4487c478bd9Sstevel@tonic-gate 				af = ntohs(sa4->sin_family);
4497c478bd9Sstevel@tonic-gate 				if (af != sa4->sin_family) {
4507c478bd9Sstevel@tonic-gate 					xdrmem_create(&xdrs,
4517c478bd9Sstevel@tonic-gate 						(caddr_t)xbuf, IPV6_ADDR_LEN,
4527c478bd9Sstevel@tonic-gate 						XDR_DECODE);
4537c478bd9Sstevel@tonic-gate 					if (af == AF_INET6) {
4547c478bd9Sstevel@tonic-gate 						xdr_opaque(&xdrs,
455a506a34cSth 							(caddr_t)svcaddr->buf,
4567c478bd9Sstevel@tonic-gate 							IPV6_ADDR_LEN);
4577c478bd9Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
4587c478bd9Sstevel@tonic-gate 							xbuf;
4597c478bd9Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
4607c478bd9Sstevel@tonic-gate 					} else {
4617c478bd9Sstevel@tonic-gate 						xdr_opaque(&xdrs,
4627c478bd9Sstevel@tonic-gate 							(caddr_t)svcaddr->buf,
4637c478bd9Sstevel@tonic-gate 							IPV4_ADDR_LEN);
4647c478bd9Sstevel@tonic-gate 						sa4 = (struct sockaddr_in *)
4657c478bd9Sstevel@tonic-gate 							xbuf;
4667c478bd9Sstevel@tonic-gate 						addr = &sa4->sin_addr;
4677c478bd9Sstevel@tonic-gate 					}
4687c478bd9Sstevel@tonic-gate 				} else {
4697c478bd9Sstevel@tonic-gate 					if (af == AF_INET6) {
4707c478bd9Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
4717c478bd9Sstevel@tonic-gate 							svcaddr->buf;
4727c478bd9Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
4737c478bd9Sstevel@tonic-gate 					} else {
4747c478bd9Sstevel@tonic-gate 						addr = &sa4->sin_addr;
4757c478bd9Sstevel@tonic-gate 					}
4767c478bd9Sstevel@tonic-gate 				}
4777c478bd9Sstevel@tonic-gate 				str = inet_ntop(af, addr, buf, sizeof (buf));
4787c478bd9Sstevel@tonic-gate 				if (str == NULL)
4797c478bd9Sstevel@tonic-gate 					perror("inet_ntop");
4807c478bd9Sstevel@tonic-gate 				else
4817c478bd9Sstevel@tonic-gate 					fprintf(stdout, "%s\n", str);
4827c478bd9Sstevel@tonic-gate 			} else {
4837c478bd9Sstevel@tonic-gate 				str = nhs->h_hostservs->h_host;
4847c478bd9Sstevel@tonic-gate 				if (str == NULL)
4857c478bd9Sstevel@tonic-gate 					str = "<unknown>";
4867c478bd9Sstevel@tonic-gate 				fprintf(stdout, "%s\n", str);
4877c478bd9Sstevel@tonic-gate 			}
4887c478bd9Sstevel@tonic-gate 			netdir_free((char *)nhs, ND_HOSTSERVLIST);
4897c478bd9Sstevel@tonic-gate 		} else {
4907c478bd9Sstevel@tonic-gate 			fprintf(stdout, "%s\n", server);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate #ifdef DEBUG
4947c478bd9Sstevel@tonic-gate 	dump_response(response);
4957c478bd9Sstevel@tonic-gate #endif
4967c478bd9Sstevel@tonic-gate 	return (TRUE);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * Serializes/deserializes an in_addr struct.
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  * Note:  There is a data coupling between the "definition" of a struct
5037c478bd9Sstevel@tonic-gate  * in_addr implicit in this xdr routine, and the true data definition in
5047c478bd9Sstevel@tonic-gate  * <netinet/in.h>.
5057c478bd9Sstevel@tonic-gate  */
xdr_yp_inaddr(xdrs,ps)5067c478bd9Sstevel@tonic-gate static bool xdr_yp_inaddr(xdrs, ps)
5077c478bd9Sstevel@tonic-gate 	XDR * xdrs;
5087c478bd9Sstevel@tonic-gate 	struct in_addr *ps;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (caddr_t)&ps->s_addr, 4));
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Serializes/deserializes an old ypbind_binding struct.
5167c478bd9Sstevel@tonic-gate  */
xdr_old_yp_binding(xdrs,ps)5177c478bd9Sstevel@tonic-gate static bool xdr_old_yp_binding(xdrs, ps)
5187c478bd9Sstevel@tonic-gate 	XDR * xdrs;
5197c478bd9Sstevel@tonic-gate 	struct old_ypbind_binding *ps;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	return (xdr_yp_inaddr(xdrs, &ps->ypbind_binding_addr) &&
5237c478bd9Sstevel@tonic-gate 	    xdr_opaque(xdrs, (caddr_t)&ps->ypbind_binding_port, 2));
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * Serializes/deserializes a ypbind_resp structure.
5287c478bd9Sstevel@tonic-gate  */
xdr_old_ypbind_resp(xdrs,ps)5297c478bd9Sstevel@tonic-gate static bool xdr_old_ypbind_resp(xdrs, ps)
5307c478bd9Sstevel@tonic-gate 	XDR * xdrs;
5317c478bd9Sstevel@tonic-gate 	struct old_ypbind_resp *ps;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate {
534a506a34cSth 	if (!xdr_enum(xdrs, (enum_t *)&ps->ypbind_status)) {
5357c478bd9Sstevel@tonic-gate 		return (FALSE);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 	switch (ps->ypbind_status) {
5387c478bd9Sstevel@tonic-gate 	case YPBIND_SUCC_VAL:
5397c478bd9Sstevel@tonic-gate 		return (xdr_old_yp_binding(xdrs,
5407c478bd9Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_bindinfo));
5417c478bd9Sstevel@tonic-gate 	case YPBIND_FAIL_VAL:
5427c478bd9Sstevel@tonic-gate 		return (xdr_u_long(xdrs,
5437c478bd9Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_error));
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	return (FALSE);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate /* This sends a message to the old ypbind process on host. */
old_call_binder(vers)5487c478bd9Sstevel@tonic-gate static int old_call_binder(vers)
5497c478bd9Sstevel@tonic-gate 	int vers;
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	CLIENT *client;
5527c478bd9Sstevel@tonic-gate 	struct hostent *hp;
5537c478bd9Sstevel@tonic-gate 	int sock = RPC_ANYSOCK;
5547c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
5557c478bd9Sstevel@tonic-gate 	struct old_ypbind_resp response;
5567c478bd9Sstevel@tonic-gate 	char errstring[256];
5577c478bd9Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
5587c478bd9Sstevel@tonic-gate 	struct in_addr *server;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if ((client = clnt_create(host, YPBINDPROG, vers, "udp")) == NULL) {
5617c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) {
5627c478bd9Sstevel@tonic-gate 			(void) printf("ypwhich: %s is not running ypbind\n",
5637c478bd9Sstevel@tonic-gate 				host);
5647c478bd9Sstevel@tonic-gate 			exit(1);
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
5677c478bd9Sstevel@tonic-gate 		    (void) printf("ypwhich: %s is not running port mapper\n",
5687c478bd9Sstevel@tonic-gate 				host);
5697c478bd9Sstevel@tonic-gate 			exit(1);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		(void) clnt_pcreateerror("ypwhich:  clnt_create error");
5727c478bd9Sstevel@tonic-gate 		exit(1);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(client, YPBINDPROC_DOMAIN,
5767c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
5777c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_old_ypbind_resp, (caddr_t)&response,
5787c478bd9Sstevel@tonic-gate 			timeout);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) &&
5817c478bd9Sstevel@tonic-gate 	    (rpc_stat != RPC_PROGVERSMISMATCH)) {
5827c478bd9Sstevel@tonic-gate 		(void) sprintf(errstring,
5837c478bd9Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
5847c478bd9Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
5857c478bd9Sstevel@tonic-gate 		exit(1);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
5897c478bd9Sstevel@tonic-gate 	close(sock);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) ||
5927c478bd9Sstevel@tonic-gate 	    (response.ypbind_status != YPBIND_SUCC_VAL)) {
5937c478bd9Sstevel@tonic-gate 		return (FALSE);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	server = &response.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
5977c478bd9Sstevel@tonic-gate 	print_server  (server);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	return (TRUE);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * For old version:
6047c478bd9Sstevel@tonic-gate  * This translates a server address to a name and prints it.
6057c478bd9Sstevel@tonic-gate  * We'll get a name by using the standard library routine.
6067c478bd9Sstevel@tonic-gate  */
print_server(server)6077c478bd9Sstevel@tonic-gate static void print_server(server)
6087c478bd9Sstevel@tonic-gate 	struct in_addr *server;
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	char buf[256];
6117c478bd9Sstevel@tonic-gate 	struct hostent *hp;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	strcpy(buf, inet_ntoa(*server));
614a506a34cSth 	hp = gethostbyaddr((char *)&server->s_addr,
6157c478bd9Sstevel@tonic-gate 			sizeof (struct in_addr), AF_INET);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	printf("%s\n", hp ? hp->h_name : buf);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate #ifdef DEBUG
6217c478bd9Sstevel@tonic-gate static void
dump_response(which)6227c478bd9Sstevel@tonic-gate dump_response(which)
6237c478bd9Sstevel@tonic-gate ypbind_resp * which;
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	struct netconfig *nc;
6267c478bd9Sstevel@tonic-gate 	struct netbuf *ua;
6277c478bd9Sstevel@tonic-gate 	ypbind_binding * b;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	int i;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	{
6327c478bd9Sstevel@tonic-gate 		b = which->ypbind_resp_u.ypbind_bindinfo;
6337c478bd9Sstevel@tonic-gate 		if (b == NULL)
6347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "???NO Binding information\n");
6357c478bd9Sstevel@tonic-gate 		else {
6367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6377c478bd9Sstevel@tonic-gate 		"server=%s lovers=%ld hivers=%ld\n",
6387c478bd9Sstevel@tonic-gate 			    b->ypbind_servername,
6397c478bd9Sstevel@tonic-gate 				b->ypbind_lo_vers, b->ypbind_hi_vers);
6407c478bd9Sstevel@tonic-gate 			nc = b->ypbind_nconf;
6417c478bd9Sstevel@tonic-gate 			ua = b->ypbind_svcaddr;
6427c478bd9Sstevel@tonic-gate 			if (nc == NULL)
6437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6447c478bd9Sstevel@tonic-gate 			"ypwhich: NO netconfig information\n");
6457c478bd9Sstevel@tonic-gate 			else {
6467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6477c478bd9Sstevel@tonic-gate 		"ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
6487c478bd9Sstevel@tonic-gate 		nc->nc_netid, nc->nc_device,
649a506a34cSth 		(int)nc->nc_flag, nc->nc_protofmly,
6507c478bd9Sstevel@tonic-gate 		nc->nc_proto);
6517c478bd9Sstevel@tonic-gate 			}
6527c478bd9Sstevel@tonic-gate 			if (ua == NULL)
6537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6547c478bd9Sstevel@tonic-gate 		"ypwhich: NO netbuf information available from binder\n");
6557c478bd9Sstevel@tonic-gate 			else {
6567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6577c478bd9Sstevel@tonic-gate 			"maxlen=%d len=%d\naddr=", ua->maxlen, ua->len);
6587c478bd9Sstevel@tonic-gate 				for (i = 0; i < ua->len; i++) {
6597c478bd9Sstevel@tonic-gate 					if (i != (ua->len - 1))
6607c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6617c478bd9Sstevel@tonic-gate 					"%d.", ua->buf[i]);
6627c478bd9Sstevel@tonic-gate 					else
6637c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6647c478bd9Sstevel@tonic-gate 					"%d\n", ua->buf[i]);
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 			}
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate #endif
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * This translates a server address to a name and prints it.  If the address
6757c478bd9Sstevel@tonic-gate  * is the same as the local address as returned by get_myaddress, the name
6767c478bd9Sstevel@tonic-gate  * is that retrieved from the kernel.  If it's any other address (including
6777c478bd9Sstevel@tonic-gate  * another ip address for the local machine), we'll get a name by using the
6787c478bd9Sstevel@tonic-gate  * standard library routine (which calls the yp).
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * This asks any yp server for the map's master.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate static void
get_map_master()6857c478bd9Sstevel@tonic-gate get_map_master()
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	int err;
6887c478bd9Sstevel@tonic-gate 	char *master;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	err = __yp_master_rsvdport(domain, map, &master);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (err) {
6937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6947c478bd9Sstevel@tonic-gate 		    "ypwhich:  Can't find the master of %s.  Reason: %s.\n",
6957c478bd9Sstevel@tonic-gate 		    map, yperr_string(err));
6967c478bd9Sstevel@tonic-gate 		exit(1);
6977c478bd9Sstevel@tonic-gate 	} else {
6987c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", master);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * This enumerates the entries within map "ypmaps" in the domain at global
7047c478bd9Sstevel@tonic-gate  * "domain", and prints them out key and value per single line.  dump_ypmaps
7057c478bd9Sstevel@tonic-gate  * just decides whether we are (probably) able to speak the new YP protocol,
7067c478bd9Sstevel@tonic-gate  * and dispatches to the appropriate function.
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate static void
dump_ypmaps()7097c478bd9Sstevel@tonic-gate dump_ypmaps()
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	int err;
7127c478bd9Sstevel@tonic-gate 	struct dom_binding *binding;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (err = __yp_dobind(domain, &binding)) {
7157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7167c478bd9Sstevel@tonic-gate 		    "dump_ypmaps: Can't bind for domain %s.  Reason: %s\n",
7177c478bd9Sstevel@tonic-gate 		    domain, yperr_string(err));
7187c478bd9Sstevel@tonic-gate 		return;
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (binding->dom_binding->ypbind_hi_vers  >= YPVERS) {
7227c478bd9Sstevel@tonic-gate 		dumpmaps(binding);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate static void
dumpmaps(binding)7277c478bd9Sstevel@tonic-gate dumpmaps(binding)
7287c478bd9Sstevel@tonic-gate struct dom_binding *binding;
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
7317c478bd9Sstevel@tonic-gate 	int err;
7327c478bd9Sstevel@tonic-gate 	char *master;
7337c478bd9Sstevel@tonic-gate 	struct ypmaplist *pmpl;
7347c478bd9Sstevel@tonic-gate 	struct ypresp_maplist maplist;
7357c478bd9Sstevel@tonic-gate 
736a506a34cSth 	maplist.list = (struct ypmaplist *)NULL;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(binding->dom_client, YPPROC_MAPLIST,
739a506a34cSth 	    (xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
740a506a34cSth 	    (xdrproc_t)xdr_ypresp_maplist, (caddr_t)&maplist,
7417c478bd9Sstevel@tonic-gate 	    timeout);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
7447c478bd9Sstevel@tonic-gate 		(void) clnt_perror(binding->dom_client,
7457c478bd9Sstevel@tonic-gate 		    "ypwhich(dumpmaps): can't get maplist");
7467c478bd9Sstevel@tonic-gate 		__yp_rel_binding(binding);
7477c478bd9Sstevel@tonic-gate 		exit(1);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (maplist.status != YP_TRUE) {
7517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7527c478bd9Sstevel@tonic-gate 		    "ypwhich:  Can't get maplist.  Reason:  %s.\n",
7537c478bd9Sstevel@tonic-gate 		    yperr_string(ypprot_err(maplist.status)));
7547c478bd9Sstevel@tonic-gate 		exit(1);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	__yp_rel_binding(binding);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	for (pmpl = maplist.list; pmpl; pmpl = pmpl->ypml_next) {
7597c478bd9Sstevel@tonic-gate 		(void) printf("%s ", pmpl->ypml_name);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		err = __yp_master_rsvdport(domain, pmpl->ypml_name, &master);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if (err) {
7647c478bd9Sstevel@tonic-gate 			(void) printf("????????\n");
7657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7667c478bd9Sstevel@tonic-gate 		"ypwhich:  Can't find the master of %s.  Reason: %s.\n",
7677c478bd9Sstevel@tonic-gate 		pmpl->ypml_name, yperr_string(err));
7687c478bd9Sstevel@tonic-gate 		} else {
7697c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", master);
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate }
773