xref: /titanic_44/usr/src/cmd/ypcmd/ypwhich.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
31*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
32*7c478bd9Sstevel@tonic-gate  * California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * This is a user command which tells which yp server is being used by a
39*7c478bd9Sstevel@tonic-gate  * given machine, or which yp server is the master for a named map.
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  * Usage is:
42*7c478bd9Sstevel@tonic-gate  *	ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
43*7c478bd9Sstevel@tonic-gate  *	ypwhich -x
44*7c478bd9Sstevel@tonic-gate  * where:  the -d switch can be used to specify a domain other than the
45*7c478bd9Sstevel@tonic-gate  * default domain.  -m tells the master of that map.  mname is a mapname
46*7c478bd9Sstevel@tonic-gate  * If the -m option is used, ypwhich will act like a vanilla yp client,
47*7c478bd9Sstevel@tonic-gate  * and will not attempt to choose a particular yp server.  On the
48*7c478bd9Sstevel@tonic-gate  * other hand, if no -m switch is used, ypwhich will talk directly to the yp
49*7c478bd9Sstevel@tonic-gate  * bind process on the named host, or to the local ypbind process if no host
50*7c478bd9Sstevel@tonic-gate  * name is specified. -t switch inhibits nickname translation of map names.
51*7c478bd9Sstevel@tonic-gate  * -x is to dump the nickname translation table from file /var/yp/nicknames.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #include <stdio.h>
56*7c478bd9Sstevel@tonic-gate #include <ctype.h>
57*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
58*7c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
59*7c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
60*7c478bd9Sstevel@tonic-gate #include "yp_b.h"
61*7c478bd9Sstevel@tonic-gate #include "ypv2_bind.h"
62*7c478bd9Sstevel@tonic-gate #include <string.h>
63*7c478bd9Sstevel@tonic-gate #include <netdir.h>
64*7c478bd9Sstevel@tonic-gate #include <unistd.h>
65*7c478bd9Sstevel@tonic-gate #include <netdb.h>
66*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
67*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
68*7c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
69*7c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate #define	YPSLEEPTIME 5 /* between two tries of bind */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
75*7c478bd9Sstevel@tonic-gate #define	INTER_TRY 10			/* Seconds between tries */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static int translate = TRUE;
78*7c478bd9Sstevel@tonic-gate static int dodump = FALSE;
79*7c478bd9Sstevel@tonic-gate static char *domain = NULL;
80*7c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
81*7c478bd9Sstevel@tonic-gate static char *host = NULL;
82*7c478bd9Sstevel@tonic-gate static int vers = YPBINDVERS;
83*7c478bd9Sstevel@tonic-gate static char default_host_name[256];
84*7c478bd9Sstevel@tonic-gate static bool get_master = FALSE;
85*7c478bd9Sstevel@tonic-gate static bool get_server = FALSE;
86*7c478bd9Sstevel@tonic-gate static char *map = NULL;
87*7c478bd9Sstevel@tonic-gate static char nm[YPMAXMAP+1];
88*7c478bd9Sstevel@tonic-gate static struct timeval timeout = {
89*7c478bd9Sstevel@tonic-gate 		TIMEOUT,			/* Seconds */
90*7c478bd9Sstevel@tonic-gate 		0				/* Microseconds */
91*7c478bd9Sstevel@tonic-gate };
92*7c478bd9Sstevel@tonic-gate static char nullstring[] = "\000";
93*7c478bd9Sstevel@tonic-gate static char err_usage[] =
94*7c478bd9Sstevel@tonic-gate "Usage:\n\
95*7c478bd9Sstevel@tonic-gate 	ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
96*7c478bd9Sstevel@tonic-gate 	ypwhich -x\n\
97*7c478bd9Sstevel@tonic-gate where\n\
98*7c478bd9Sstevel@tonic-gate 	mname may be either a mapname or a nickname for a map.\n\
99*7c478bd9Sstevel@tonic-gate 	host if specified, is the machine whose NIS server is to be found.\n\
100*7c478bd9Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
101*7c478bd9Sstevel@tonic-gate 	-Vn version of ypbind, V3 is default.\n\
102*7c478bd9Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
103*7c478bd9Sstevel@tonic-gate static char err_bad_args[] =
104*7c478bd9Sstevel@tonic-gate "ypwhich:  %s argument is bad.\n";
105*7c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
106*7c478bd9Sstevel@tonic-gate "ypwhich:  can't get %s back from system call.\n";
107*7c478bd9Sstevel@tonic-gate static char err_null_kname[] =
108*7c478bd9Sstevel@tonic-gate "ypwhich:  the %s hasn't been set on this machine.\n";
109*7c478bd9Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
110*7c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
111*7c478bd9Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static void get_command_line_args();
114*7c478bd9Sstevel@tonic-gate static void getdomain();
115*7c478bd9Sstevel@tonic-gate static void getlochost();
116*7c478bd9Sstevel@tonic-gate static void get_server_name();
117*7c478bd9Sstevel@tonic-gate static int call_binder();
118*7c478bd9Sstevel@tonic-gate static void get_map_master();
119*7c478bd9Sstevel@tonic-gate extern void maketable();
120*7c478bd9Sstevel@tonic-gate extern int getmapname();
121*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
122*7c478bd9Sstevel@tonic-gate static void dump_response();
123*7c478bd9Sstevel@tonic-gate #endif
124*7c478bd9Sstevel@tonic-gate static void dump_ypmaps();
125*7c478bd9Sstevel@tonic-gate static void dumpmaps();
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate static bool xdr_yp_inaddr ();
128*7c478bd9Sstevel@tonic-gate static bool xdr_old_ypbind_resp ();
129*7c478bd9Sstevel@tonic-gate static bool xdr_old_yp_binding();
130*7c478bd9Sstevel@tonic-gate static int old_call_binder();
131*7c478bd9Sstevel@tonic-gate static void print_server();
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /* need these for call to (remote) V2 ypbind */
134*7c478bd9Sstevel@tonic-gate struct old_ypbind_binding {
135*7c478bd9Sstevel@tonic-gate 	struct in_addr ypbind_binding_addr;	/* In network order */
136*7c478bd9Sstevel@tonic-gate 	unsigned short int ypbind_binding_port;	/* In network order */
137*7c478bd9Sstevel@tonic-gate };
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate struct old_ypbind_resp {
140*7c478bd9Sstevel@tonic-gate 	enum ypbind_resptype ypbind_status;
141*7c478bd9Sstevel@tonic-gate 	union {
142*7c478bd9Sstevel@tonic-gate 		unsigned long ypbind_error;
143*7c478bd9Sstevel@tonic-gate 		struct old_ypbind_binding ypbind_bindinfo;
144*7c478bd9Sstevel@tonic-gate 	} ypbind_respbody;
145*7c478bd9Sstevel@tonic-gate };
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * This is the main line for the ypwhich process.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate main(argc, argv)
151*7c478bd9Sstevel@tonic-gate char **argv;
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (dodump) {
156*7c478bd9Sstevel@tonic-gate 		maketable(dodump);
157*7c478bd9Sstevel@tonic-gate 		exit(0);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if (!domain) {
161*7c478bd9Sstevel@tonic-gate 		getdomain();
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if (map && translate && (strchr(map, '.') == NULL) &&
165*7c478bd9Sstevel@tonic-gate 		(getmapname(map, nm))) {
166*7c478bd9Sstevel@tonic-gate 		map = nm;
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (get_server) {
170*7c478bd9Sstevel@tonic-gate 		if (!host)
171*7c478bd9Sstevel@tonic-gate 			getlochost();
172*7c478bd9Sstevel@tonic-gate 		get_server_name();
173*7c478bd9Sstevel@tonic-gate 	} else {
174*7c478bd9Sstevel@tonic-gate 		if (map)
175*7c478bd9Sstevel@tonic-gate 			get_map_master();
176*7c478bd9Sstevel@tonic-gate 		else
177*7c478bd9Sstevel@tonic-gate 			dump_ypmaps();
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	return (0);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate static void
187*7c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
188*7c478bd9Sstevel@tonic-gate int argc;
189*7c478bd9Sstevel@tonic-gate char **argv;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	argv++;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (argc == 1) {
195*7c478bd9Sstevel@tonic-gate 		get_server = TRUE;
196*7c478bd9Sstevel@tonic-gate 		return;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	while (--argc) {
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			switch ((*argv)[1]) {
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 			case 'V':
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 				vers = atoi(argv[0]+2);
208*7c478bd9Sstevel@tonic-gate 				if (vers <  1) {
209*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
210*7c478bd9Sstevel@tonic-gate 					exit(1);
211*7c478bd9Sstevel@tonic-gate 				}
212*7c478bd9Sstevel@tonic-gate 				argv++;
213*7c478bd9Sstevel@tonic-gate 				break;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 			case 'm':
216*7c478bd9Sstevel@tonic-gate 				get_master = TRUE;
217*7c478bd9Sstevel@tonic-gate 				argv++;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 				if (argc > 1) {
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 					if ((*(argv))[0] == '-') {
222*7c478bd9Sstevel@tonic-gate 						break;
223*7c478bd9Sstevel@tonic-gate 					}
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 					argc--;
226*7c478bd9Sstevel@tonic-gate 					map = *argv;
227*7c478bd9Sstevel@tonic-gate 					argv++;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 					if ((int)strlen(map) > YPMAXMAP) {
230*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
231*7c478bd9Sstevel@tonic-gate 						    err_bad_mapname);
232*7c478bd9Sstevel@tonic-gate 						exit(1);
233*7c478bd9Sstevel@tonic-gate 					}
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 				}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 				break;
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 			case 'd':
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 				if (argc > 1) {
242*7c478bd9Sstevel@tonic-gate 					argv++;
243*7c478bd9Sstevel@tonic-gate 					argc--;
244*7c478bd9Sstevel@tonic-gate 					domain = *argv;
245*7c478bd9Sstevel@tonic-gate 					argv++;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 					if ((int)strlen(domain) > YPMAXDOMAIN) {
248*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_bad_args,
249*7c478bd9Sstevel@tonic-gate 				err_bad_domainname);
250*7c478bd9Sstevel@tonic-gate 						exit(1);
251*7c478bd9Sstevel@tonic-gate 					}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 				} else {
254*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
255*7c478bd9Sstevel@tonic-gate 					exit(1);
256*7c478bd9Sstevel@tonic-gate 				}
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 				break;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 			case 't':
261*7c478bd9Sstevel@tonic-gate 				translate = FALSE;
262*7c478bd9Sstevel@tonic-gate 				argv++;
263*7c478bd9Sstevel@tonic-gate 				break;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 			case 'x':
266*7c478bd9Sstevel@tonic-gate 				dodump = TRUE;
267*7c478bd9Sstevel@tonic-gate 				argv++;
268*7c478bd9Sstevel@tonic-gate 				break;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 			default:
271*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
272*7c478bd9Sstevel@tonic-gate 				exit(1);
273*7c478bd9Sstevel@tonic-gate 			}
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 		} else {
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 			if (get_server) {
278*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
279*7c478bd9Sstevel@tonic-gate 				exit(1);
280*7c478bd9Sstevel@tonic-gate 			}
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 			get_server = TRUE;
283*7c478bd9Sstevel@tonic-gate 			host = *argv;
284*7c478bd9Sstevel@tonic-gate 			argv++;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 			if ((int)strlen(host) > 256) {
287*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
288*7c478bd9Sstevel@tonic-gate 			err_bad_args, err_bad_hostname);
289*7c478bd9Sstevel@tonic-gate 				exit(1);
290*7c478bd9Sstevel@tonic-gate 			}
291*7c478bd9Sstevel@tonic-gate 		}
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if (get_master && get_server) {
295*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_usage);
296*7c478bd9Sstevel@tonic-gate 		exit(1);
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	if (!get_master && !get_server) {
300*7c478bd9Sstevel@tonic-gate 		get_server = TRUE;
301*7c478bd9Sstevel@tonic-gate 	}
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate /*
305*7c478bd9Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
306*7c478bd9Sstevel@tonic-gate  * to something reasonable.  domain is set here.
307*7c478bd9Sstevel@tonic-gate  */
308*7c478bd9Sstevel@tonic-gate static void
309*7c478bd9Sstevel@tonic-gate getdomain()
310*7c478bd9Sstevel@tonic-gate {
311*7c478bd9Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
312*7c478bd9Sstevel@tonic-gate 		domain = default_domain_name;
313*7c478bd9Sstevel@tonic-gate 	} else {
314*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
315*7c478bd9Sstevel@tonic-gate 		exit(1);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if ((int)strlen(domain) == 0) {
319*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
320*7c478bd9Sstevel@tonic-gate 		exit(1);
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate /*
325*7c478bd9Sstevel@tonic-gate  * This gets the local hostname back from the kernel
326*7c478bd9Sstevel@tonic-gate  */
327*7c478bd9Sstevel@tonic-gate static void
328*7c478bd9Sstevel@tonic-gate getlochost()
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate 	struct utsname utsname;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	if (uname(&utsname) != -1) {
333*7c478bd9Sstevel@tonic-gate 		strcpy(default_host_name, utsname.nodename);
334*7c478bd9Sstevel@tonic-gate 		host = default_host_name;
335*7c478bd9Sstevel@tonic-gate 	} else {
336*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
337*7c478bd9Sstevel@tonic-gate 		exit(1);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /*
343*7c478bd9Sstevel@tonic-gate  * This tries to find the name of the server to which the binder in question
344*7c478bd9Sstevel@tonic-gate  * is bound.  If one of the -Vx flags was specified, it will try only for
345*7c478bd9Sstevel@tonic-gate  * that protocol version, otherwise, it will start with the current version,
346*7c478bd9Sstevel@tonic-gate  * then drop back to the previous version.
347*7c478bd9Sstevel@tonic-gate  */
348*7c478bd9Sstevel@tonic-gate static void
349*7c478bd9Sstevel@tonic-gate get_server_name()
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate 	char *notbound = "Domain %s not bound on %s.\n";
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if (vers >= 3){
354*7c478bd9Sstevel@tonic-gate 		if (!call_binder(vers))
355*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
356*7c478bd9Sstevel@tonic-gate 	} else {
357*7c478bd9Sstevel@tonic-gate 		if (!old_call_binder(vers))
358*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, notbound, domain, host);
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * This sends a message to the ypbind process on the node with
366*7c478bd9Sstevel@tonic-gate  * the host name
367*7c478bd9Sstevel@tonic-gate  */
368*7c478bd9Sstevel@tonic-gate static int
369*7c478bd9Sstevel@tonic-gate call_binder(vers)
370*7c478bd9Sstevel@tonic-gate int vers;
371*7c478bd9Sstevel@tonic-gate {
372*7c478bd9Sstevel@tonic-gate 	CLIENT *client;
373*7c478bd9Sstevel@tonic-gate 	struct ypbind_resp *response;
374*7c478bd9Sstevel@tonic-gate 	struct ypbind_domain ypbd;
375*7c478bd9Sstevel@tonic-gate 	char errstring[256];
376*7c478bd9Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
377*7c478bd9Sstevel@tonic-gate 	int yperr = 0;
378*7c478bd9Sstevel@tonic-gate 	struct utsname utsname;
379*7c478bd9Sstevel@tonic-gate 	const char *str;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	/*
382*7c478bd9Sstevel@tonic-gate 	 * CAUTION: Do not go to NIS if the host is the same as the local host
383*7c478bd9Sstevel@tonic-gate 	 * XXX: Lots of special magic to distinguish between local and remote
384*7c478bd9Sstevel@tonic-gate 	 * case. We want to make sure the local case doesn't hang.
385*7c478bd9Sstevel@tonic-gate 	 */
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	if ((uname(&utsname) != -1) &&
388*7c478bd9Sstevel@tonic-gate 		(strcmp(host, utsname.nodename) == 0))
389*7c478bd9Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, vers, &yperr);
390*7c478bd9Sstevel@tonic-gate 	else
391*7c478bd9Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG, vers, "netpath");
392*7c478bd9Sstevel@tonic-gate 	if (client == NULL) {
393*7c478bd9Sstevel@tonic-gate 		if (yperr)
394*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
395*7c478bd9Sstevel@tonic-gate 				"ypwhich: %s\n", yperr_string(yperr));
396*7c478bd9Sstevel@tonic-gate 		else {
397*7c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED ||
398*7c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_PROGUNAVAIL) {
399*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
400*7c478bd9Sstevel@tonic-gate 			"ypwhich: %s is not running ypbind\n", host);
401*7c478bd9Sstevel@tonic-gate 			} else if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
402*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
403*7c478bd9Sstevel@tonic-gate 			"ypwhich: %s is not running rpcbind\n",
404*7c478bd9Sstevel@tonic-gate 					host);
405*7c478bd9Sstevel@tonic-gate 			} else
406*7c478bd9Sstevel@tonic-gate 				(void) clnt_pcreateerror("ypwhich: \
407*7c478bd9Sstevel@tonic-gate clnt_create error");
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 		exit(1);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 	ypbd.ypbind_domainname = domain;
412*7c478bd9Sstevel@tonic-gate 	ypbd.ypbind_vers = vers;
413*7c478bd9Sstevel@tonic-gate 	response = ypbindproc_domain_3(&ypbd, client);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	if (response == NULL){
416*7c478bd9Sstevel@tonic-gate 		(void) sprintf(errstring,
417*7c478bd9Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
418*7c478bd9Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
419*7c478bd9Sstevel@tonic-gate 		exit(1);
420*7c478bd9Sstevel@tonic-gate 	}
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	if (response->ypbind_status != YPBIND_SUCC_VAL)  {
425*7c478bd9Sstevel@tonic-gate 		return (FALSE);
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	if (response->ypbind_resp_u.ypbind_bindinfo) {
429*7c478bd9Sstevel@tonic-gate 		char *server =
430*7c478bd9Sstevel@tonic-gate 	response->ypbind_resp_u.ypbind_bindinfo->ypbind_servername;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 		if (strcmp(server, nullstring) == 0) {
433*7c478bd9Sstevel@tonic-gate 		/* depends on a hack in ypbind */
434*7c478bd9Sstevel@tonic-gate 			struct nd_hostservlist *nhs = NULL;
435*7c478bd9Sstevel@tonic-gate 			struct netconfig *nconf =
436*7c478bd9Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_nconf;
437*7c478bd9Sstevel@tonic-gate 			struct netbuf *svcaddr =
438*7c478bd9Sstevel@tonic-gate 		response->ypbind_resp_u.ypbind_bindinfo->ypbind_svcaddr;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 			if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
441*7c478bd9Sstevel@tonic-gate 				struct sockaddr_in	*sa4;
442*7c478bd9Sstevel@tonic-gate 				struct sockaddr_in6	*sa6;
443*7c478bd9Sstevel@tonic-gate 				char			buf[INET6_ADDRSTRLEN];
444*7c478bd9Sstevel@tonic-gate 				char			xbuf[IPV6_ADDR_LEN];
445*7c478bd9Sstevel@tonic-gate 				int			af;
446*7c478bd9Sstevel@tonic-gate 				void			*addr;
447*7c478bd9Sstevel@tonic-gate 				XDR			xdrs;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 				sa4 = (struct sockaddr_in *)svcaddr->buf;
450*7c478bd9Sstevel@tonic-gate 				af = ntohs(sa4->sin_family);
451*7c478bd9Sstevel@tonic-gate 				if (af != sa4->sin_family) {
452*7c478bd9Sstevel@tonic-gate 					xdrmem_create(&xdrs,
453*7c478bd9Sstevel@tonic-gate 						(caddr_t)xbuf, IPV6_ADDR_LEN,
454*7c478bd9Sstevel@tonic-gate 						XDR_DECODE);
455*7c478bd9Sstevel@tonic-gate 					if (af == AF_INET6) {
456*7c478bd9Sstevel@tonic-gate 						xdr_opaque(&xdrs,
457*7c478bd9Sstevel@tonic-gate 							(caddr_t)svcaddr->buf,
458*7c478bd9Sstevel@tonic-gate 							IPV6_ADDR_LEN);
459*7c478bd9Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
460*7c478bd9Sstevel@tonic-gate 							xbuf;
461*7c478bd9Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
462*7c478bd9Sstevel@tonic-gate 					} else {
463*7c478bd9Sstevel@tonic-gate 						xdr_opaque(&xdrs,
464*7c478bd9Sstevel@tonic-gate 							(caddr_t)svcaddr->buf,
465*7c478bd9Sstevel@tonic-gate 							IPV4_ADDR_LEN);
466*7c478bd9Sstevel@tonic-gate 						sa4 = (struct sockaddr_in *)
467*7c478bd9Sstevel@tonic-gate 							xbuf;
468*7c478bd9Sstevel@tonic-gate 						addr = &sa4->sin_addr;
469*7c478bd9Sstevel@tonic-gate 					}
470*7c478bd9Sstevel@tonic-gate 				} else {
471*7c478bd9Sstevel@tonic-gate 					if (af == AF_INET6) {
472*7c478bd9Sstevel@tonic-gate 						sa6 = (struct sockaddr_in6 *)
473*7c478bd9Sstevel@tonic-gate 							svcaddr->buf;
474*7c478bd9Sstevel@tonic-gate 						addr = &sa6->sin6_addr;
475*7c478bd9Sstevel@tonic-gate 					} else {
476*7c478bd9Sstevel@tonic-gate 						addr = &sa4->sin_addr;
477*7c478bd9Sstevel@tonic-gate 					}
478*7c478bd9Sstevel@tonic-gate 				}
479*7c478bd9Sstevel@tonic-gate 				str = inet_ntop(af, addr, buf, sizeof (buf));
480*7c478bd9Sstevel@tonic-gate 				if (str == NULL)
481*7c478bd9Sstevel@tonic-gate 					perror("inet_ntop");
482*7c478bd9Sstevel@tonic-gate 				else
483*7c478bd9Sstevel@tonic-gate 					fprintf(stdout, "%s\n", str);
484*7c478bd9Sstevel@tonic-gate 			} else {
485*7c478bd9Sstevel@tonic-gate 				str = nhs->h_hostservs->h_host;
486*7c478bd9Sstevel@tonic-gate 				if (str == NULL)
487*7c478bd9Sstevel@tonic-gate 					str = "<unknown>";
488*7c478bd9Sstevel@tonic-gate 				fprintf(stdout, "%s\n", str);
489*7c478bd9Sstevel@tonic-gate 			}
490*7c478bd9Sstevel@tonic-gate 			netdir_free((char *)nhs, ND_HOSTSERVLIST);
491*7c478bd9Sstevel@tonic-gate 		} else {
492*7c478bd9Sstevel@tonic-gate 			fprintf(stdout, "%s\n", server);
493*7c478bd9Sstevel@tonic-gate 		}
494*7c478bd9Sstevel@tonic-gate 	}
495*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
496*7c478bd9Sstevel@tonic-gate 	dump_response(response);
497*7c478bd9Sstevel@tonic-gate #endif
498*7c478bd9Sstevel@tonic-gate 	return (TRUE);
499*7c478bd9Sstevel@tonic-gate }
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate /*
502*7c478bd9Sstevel@tonic-gate  * Serializes/deserializes an in_addr struct.
503*7c478bd9Sstevel@tonic-gate  *
504*7c478bd9Sstevel@tonic-gate  * Note:  There is a data coupling between the "definition" of a struct
505*7c478bd9Sstevel@tonic-gate  * in_addr implicit in this xdr routine, and the true data definition in
506*7c478bd9Sstevel@tonic-gate  * <netinet/in.h>.
507*7c478bd9Sstevel@tonic-gate  */
508*7c478bd9Sstevel@tonic-gate static bool xdr_yp_inaddr(xdrs, ps)
509*7c478bd9Sstevel@tonic-gate 	XDR * xdrs;
510*7c478bd9Sstevel@tonic-gate 	struct in_addr *ps;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate {
513*7c478bd9Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (caddr_t)&ps->s_addr, 4));
514*7c478bd9Sstevel@tonic-gate }
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate /*
517*7c478bd9Sstevel@tonic-gate  * Serializes/deserializes an old ypbind_binding struct.
518*7c478bd9Sstevel@tonic-gate  */
519*7c478bd9Sstevel@tonic-gate static bool xdr_old_yp_binding(xdrs, ps)
520*7c478bd9Sstevel@tonic-gate 	XDR * xdrs;
521*7c478bd9Sstevel@tonic-gate 	struct old_ypbind_binding *ps;
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate {
524*7c478bd9Sstevel@tonic-gate 	return (xdr_yp_inaddr(xdrs, &ps->ypbind_binding_addr) &&
525*7c478bd9Sstevel@tonic-gate 	    xdr_opaque(xdrs, (caddr_t)&ps->ypbind_binding_port, 2));
526*7c478bd9Sstevel@tonic-gate }
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate /*
529*7c478bd9Sstevel@tonic-gate  * Serializes/deserializes a ypbind_resp structure.
530*7c478bd9Sstevel@tonic-gate  */
531*7c478bd9Sstevel@tonic-gate static bool xdr_old_ypbind_resp(xdrs, ps)
532*7c478bd9Sstevel@tonic-gate 	XDR * xdrs;
533*7c478bd9Sstevel@tonic-gate 	struct old_ypbind_resp *ps;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate {
536*7c478bd9Sstevel@tonic-gate 	if (!xdr_enum(xdrs, (enum_t*)&ps->ypbind_status)) {
537*7c478bd9Sstevel@tonic-gate 		return (FALSE);
538*7c478bd9Sstevel@tonic-gate 	}
539*7c478bd9Sstevel@tonic-gate 	switch (ps->ypbind_status) {
540*7c478bd9Sstevel@tonic-gate 	case YPBIND_SUCC_VAL:
541*7c478bd9Sstevel@tonic-gate 		return (xdr_old_yp_binding(xdrs,
542*7c478bd9Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_bindinfo));
543*7c478bd9Sstevel@tonic-gate 	case YPBIND_FAIL_VAL:
544*7c478bd9Sstevel@tonic-gate 		return (xdr_u_long(xdrs,
545*7c478bd9Sstevel@tonic-gate 				&ps->ypbind_respbody.ypbind_error));
546*7c478bd9Sstevel@tonic-gate 	}
547*7c478bd9Sstevel@tonic-gate 	return (FALSE);
548*7c478bd9Sstevel@tonic-gate }
549*7c478bd9Sstevel@tonic-gate /* This sends a message to the old ypbind process on host. */
550*7c478bd9Sstevel@tonic-gate static int old_call_binder(vers)
551*7c478bd9Sstevel@tonic-gate 	int vers;
552*7c478bd9Sstevel@tonic-gate {
553*7c478bd9Sstevel@tonic-gate 	CLIENT *client;
554*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
555*7c478bd9Sstevel@tonic-gate 	int sock = RPC_ANYSOCK;
556*7c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
557*7c478bd9Sstevel@tonic-gate 	struct old_ypbind_resp response;
558*7c478bd9Sstevel@tonic-gate 	char errstring[256];
559*7c478bd9Sstevel@tonic-gate 	extern struct rpc_createerr rpc_createerr;
560*7c478bd9Sstevel@tonic-gate 	struct in_addr *server;
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	if ((client = clnt_create(host, YPBINDPROG, vers, "udp")) == NULL) {
563*7c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) {
564*7c478bd9Sstevel@tonic-gate 			(void) printf("ypwhich: %s is not running ypbind\n",
565*7c478bd9Sstevel@tonic-gate 				host);
566*7c478bd9Sstevel@tonic-gate 			exit(1);
567*7c478bd9Sstevel@tonic-gate 		}
568*7c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
569*7c478bd9Sstevel@tonic-gate 		    (void) printf("ypwhich: %s is not running port mapper\n",
570*7c478bd9Sstevel@tonic-gate 				host);
571*7c478bd9Sstevel@tonic-gate 			exit(1);
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 		(void) clnt_pcreateerror("ypwhich:  clnt_create error");
574*7c478bd9Sstevel@tonic-gate 		exit(1);
575*7c478bd9Sstevel@tonic-gate 	}
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(client, YPBINDPROC_DOMAIN,
578*7c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
579*7c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_old_ypbind_resp, (caddr_t)&response,
580*7c478bd9Sstevel@tonic-gate 			timeout);
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) &&
583*7c478bd9Sstevel@tonic-gate 	    (rpc_stat != RPC_PROGVERSMISMATCH)) {
584*7c478bd9Sstevel@tonic-gate 		(void) sprintf(errstring,
585*7c478bd9Sstevel@tonic-gate 		    "ypwhich: can't call ypbind on %s", host);
586*7c478bd9Sstevel@tonic-gate 		(void) clnt_perror(client, errstring);
587*7c478bd9Sstevel@tonic-gate 		exit(1);
588*7c478bd9Sstevel@tonic-gate 	}
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
591*7c478bd9Sstevel@tonic-gate 	close(sock);
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	if ((rpc_stat != RPC_SUCCESS) ||
594*7c478bd9Sstevel@tonic-gate 	    (response.ypbind_status != YPBIND_SUCC_VAL)) {
595*7c478bd9Sstevel@tonic-gate 		return (FALSE);
596*7c478bd9Sstevel@tonic-gate 	}
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	server = &response.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
599*7c478bd9Sstevel@tonic-gate 	print_server  (server);
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	return (TRUE);
602*7c478bd9Sstevel@tonic-gate }
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate /*
605*7c478bd9Sstevel@tonic-gate  * For old version:
606*7c478bd9Sstevel@tonic-gate  * This translates a server address to a name and prints it.
607*7c478bd9Sstevel@tonic-gate  * We'll get a name by using the standard library routine.
608*7c478bd9Sstevel@tonic-gate  */
609*7c478bd9Sstevel@tonic-gate static void print_server(server)
610*7c478bd9Sstevel@tonic-gate 	struct in_addr *server;
611*7c478bd9Sstevel@tonic-gate {
612*7c478bd9Sstevel@tonic-gate 	char buf[256];
613*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	strcpy(buf, inet_ntoa(*server));
616*7c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr((char*)&server->s_addr,
617*7c478bd9Sstevel@tonic-gate 			sizeof (struct in_addr), AF_INET);
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	printf("%s\n", hp ? hp->h_name : buf);
620*7c478bd9Sstevel@tonic-gate }
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
623*7c478bd9Sstevel@tonic-gate static void
624*7c478bd9Sstevel@tonic-gate dump_response(which)
625*7c478bd9Sstevel@tonic-gate ypbind_resp * which;
626*7c478bd9Sstevel@tonic-gate {
627*7c478bd9Sstevel@tonic-gate 	struct netconfig *nc;
628*7c478bd9Sstevel@tonic-gate 	struct netbuf *ua;
629*7c478bd9Sstevel@tonic-gate 	ypbind_binding * b;
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 	int i;
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	{
634*7c478bd9Sstevel@tonic-gate 		b = which->ypbind_resp_u.ypbind_bindinfo;
635*7c478bd9Sstevel@tonic-gate 		if (b == NULL)
636*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "???NO Binding information\n");
637*7c478bd9Sstevel@tonic-gate 		else {
638*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
639*7c478bd9Sstevel@tonic-gate 		"server=%s lovers=%ld hivers=%ld\n",
640*7c478bd9Sstevel@tonic-gate 			    b->ypbind_servername,
641*7c478bd9Sstevel@tonic-gate 				b->ypbind_lo_vers, b->ypbind_hi_vers);
642*7c478bd9Sstevel@tonic-gate 			nc = b->ypbind_nconf;
643*7c478bd9Sstevel@tonic-gate 			ua = b->ypbind_svcaddr;
644*7c478bd9Sstevel@tonic-gate 			if (nc == NULL)
645*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
646*7c478bd9Sstevel@tonic-gate 			"ypwhich: NO netconfig information\n");
647*7c478bd9Sstevel@tonic-gate 			else {
648*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
649*7c478bd9Sstevel@tonic-gate 		"ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
650*7c478bd9Sstevel@tonic-gate 		nc->nc_netid, nc->nc_device,
651*7c478bd9Sstevel@tonic-gate 		(int) nc->nc_flag, nc->nc_protofmly,
652*7c478bd9Sstevel@tonic-gate 		nc->nc_proto);
653*7c478bd9Sstevel@tonic-gate 			}
654*7c478bd9Sstevel@tonic-gate 			if (ua == NULL)
655*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
656*7c478bd9Sstevel@tonic-gate 		"ypwhich: NO netbuf information available from binder\n");
657*7c478bd9Sstevel@tonic-gate 			else {
658*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
659*7c478bd9Sstevel@tonic-gate 			"maxlen=%d len=%d\naddr=", ua->maxlen, ua->len);
660*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < ua->len; i++) {
661*7c478bd9Sstevel@tonic-gate 					if (i != (ua->len - 1))
662*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
663*7c478bd9Sstevel@tonic-gate 					"%d.", ua->buf[i]);
664*7c478bd9Sstevel@tonic-gate 					else
665*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
666*7c478bd9Sstevel@tonic-gate 					"%d\n", ua->buf[i]);
667*7c478bd9Sstevel@tonic-gate 				}
668*7c478bd9Sstevel@tonic-gate 			}
669*7c478bd9Sstevel@tonic-gate 		}
670*7c478bd9Sstevel@tonic-gate 	}
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate }
673*7c478bd9Sstevel@tonic-gate #endif
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate /*
676*7c478bd9Sstevel@tonic-gate  * This translates a server address to a name and prints it.  If the address
677*7c478bd9Sstevel@tonic-gate  * is the same as the local address as returned by get_myaddress, the name
678*7c478bd9Sstevel@tonic-gate  * is that retrieved from the kernel.  If it's any other address (including
679*7c478bd9Sstevel@tonic-gate  * another ip address for the local machine), we'll get a name by using the
680*7c478bd9Sstevel@tonic-gate  * standard library routine (which calls the yp).
681*7c478bd9Sstevel@tonic-gate  */
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate /*
684*7c478bd9Sstevel@tonic-gate  * This asks any yp server for the map's master.
685*7c478bd9Sstevel@tonic-gate  */
686*7c478bd9Sstevel@tonic-gate static void
687*7c478bd9Sstevel@tonic-gate get_map_master()
688*7c478bd9Sstevel@tonic-gate {
689*7c478bd9Sstevel@tonic-gate 	int err;
690*7c478bd9Sstevel@tonic-gate 	char *master;
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 	err = __yp_master_rsvdport(domain, map, &master);
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	if (err) {
695*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
696*7c478bd9Sstevel@tonic-gate 		    "ypwhich:  Can't find the master of %s.  Reason: %s.\n",
697*7c478bd9Sstevel@tonic-gate 		    map, yperr_string(err));
698*7c478bd9Sstevel@tonic-gate 		exit(1);
699*7c478bd9Sstevel@tonic-gate 	} else {
700*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", master);
701*7c478bd9Sstevel@tonic-gate 	}
702*7c478bd9Sstevel@tonic-gate }
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate /*
705*7c478bd9Sstevel@tonic-gate  * This enumerates the entries within map "ypmaps" in the domain at global
706*7c478bd9Sstevel@tonic-gate  * "domain", and prints them out key and value per single line.  dump_ypmaps
707*7c478bd9Sstevel@tonic-gate  * just decides whether we are (probably) able to speak the new YP protocol,
708*7c478bd9Sstevel@tonic-gate  * and dispatches to the appropriate function.
709*7c478bd9Sstevel@tonic-gate  */
710*7c478bd9Sstevel@tonic-gate static void
711*7c478bd9Sstevel@tonic-gate dump_ypmaps()
712*7c478bd9Sstevel@tonic-gate {
713*7c478bd9Sstevel@tonic-gate 	int err;
714*7c478bd9Sstevel@tonic-gate 	struct dom_binding *binding;
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	if (err = __yp_dobind(domain, &binding)) {
717*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
718*7c478bd9Sstevel@tonic-gate 		    "dump_ypmaps: Can't bind for domain %s.  Reason: %s\n",
719*7c478bd9Sstevel@tonic-gate 		    domain, yperr_string(err));
720*7c478bd9Sstevel@tonic-gate 		return;
721*7c478bd9Sstevel@tonic-gate 	}
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	if (binding->dom_binding->ypbind_hi_vers  >= YPVERS) {
724*7c478bd9Sstevel@tonic-gate 		dumpmaps(binding);
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate }
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate static void
729*7c478bd9Sstevel@tonic-gate dumpmaps(binding)
730*7c478bd9Sstevel@tonic-gate struct dom_binding *binding;
731*7c478bd9Sstevel@tonic-gate {
732*7c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
733*7c478bd9Sstevel@tonic-gate 	int err;
734*7c478bd9Sstevel@tonic-gate 	char *master;
735*7c478bd9Sstevel@tonic-gate 	struct ypmaplist *pmpl;
736*7c478bd9Sstevel@tonic-gate 	struct ypresp_maplist maplist;
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	maplist.list = (struct ypmaplist *) NULL;
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(binding->dom_client, YPPROC_MAPLIST,
741*7c478bd9Sstevel@tonic-gate 	    (xdrproc_t) xdr_ypdomain_wrap_string, (caddr_t) &domain,
742*7c478bd9Sstevel@tonic-gate 	    (xdrproc_t) xdr_ypresp_maplist, (caddr_t) &maplist,
743*7c478bd9Sstevel@tonic-gate 	    timeout);
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
746*7c478bd9Sstevel@tonic-gate 		(void) clnt_perror(binding->dom_client,
747*7c478bd9Sstevel@tonic-gate 		    "ypwhich(dumpmaps): can't get maplist");
748*7c478bd9Sstevel@tonic-gate 		__yp_rel_binding(binding);
749*7c478bd9Sstevel@tonic-gate 		exit(1);
750*7c478bd9Sstevel@tonic-gate 	}
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate 	if (maplist.status != YP_TRUE) {
753*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
754*7c478bd9Sstevel@tonic-gate 		    "ypwhich:  Can't get maplist.  Reason:  %s.\n",
755*7c478bd9Sstevel@tonic-gate 		    yperr_string(ypprot_err(maplist.status)));
756*7c478bd9Sstevel@tonic-gate 		exit(1);
757*7c478bd9Sstevel@tonic-gate 	}
758*7c478bd9Sstevel@tonic-gate 	__yp_rel_binding(binding);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	for (pmpl = maplist.list; pmpl; pmpl = pmpl->ypml_next) {
761*7c478bd9Sstevel@tonic-gate 		(void) printf("%s ", pmpl->ypml_name);
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 		err = __yp_master_rsvdport(domain, pmpl->ypml_name, &master);
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 		if (err) {
766*7c478bd9Sstevel@tonic-gate 			(void) printf("????????\n");
767*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
768*7c478bd9Sstevel@tonic-gate 		"ypwhich:  Can't find the master of %s.  Reason: %s.\n",
769*7c478bd9Sstevel@tonic-gate 		pmpl->ypml_name, yperr_string(err));
770*7c478bd9Sstevel@tonic-gate 		} else {
771*7c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", master);
772*7c478bd9Sstevel@tonic-gate 		}
773*7c478bd9Sstevel@tonic-gate 	}
774*7c478bd9Sstevel@tonic-gate }
775