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