xref: /onnv-gate/usr/src/cmd/ypcmd/yppoll.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  *
22*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * This is a user command which asks a particular ypserv which version of a
390Sstevel@tonic-gate  * map it is using.  Usage is:
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * yppoll [-h <host>] [-d <domainname>] mapname
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * If the host is ommitted, the local host will be used.  If host is specified
440Sstevel@tonic-gate  * as an internet address, no yp services need to be locally available.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate #include <stdio.h>
480Sstevel@tonic-gate #include <ctype.h>
490Sstevel@tonic-gate #include <rpc/rpc.h>
500Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
510Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
520Sstevel@tonic-gate #include <netdir.h>
530Sstevel@tonic-gate #include <arpa/inet.h>
540Sstevel@tonic-gate #include "yp_b.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifdef NULL
570Sstevel@tonic-gate #undef NULL
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate #define	NULL 0
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int status = 0;				/* exit status */
640Sstevel@tonic-gate static char *domain = NULL;
650Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
660Sstevel@tonic-gate static char *map = NULL;
670Sstevel@tonic-gate static char *host = NULL;
680Sstevel@tonic-gate static char default_host_name[256];
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static char err_usage[] =
710Sstevel@tonic-gate "Usage:\n\
720Sstevel@tonic-gate 	yppoll [ -h host ] [ -d domainname ] mapname\n\n";
730Sstevel@tonic-gate static char err_bad_args[] =
740Sstevel@tonic-gate 	"Bad %s argument.\n";
750Sstevel@tonic-gate static char err_cant_get_kname[] =
760Sstevel@tonic-gate 	"Can't get %s back from system call.\n";
770Sstevel@tonic-gate static char err_null_kname[] =
780Sstevel@tonic-gate 	"%s hasn't been set on this machine.\n";
790Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
800Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
810Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
820Sstevel@tonic-gate static char err_bad_resp[] =
830Sstevel@tonic-gate 	"Ill-formed response returned from ypserv on host %s.\n";
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static void get_command_line_args();
860Sstevel@tonic-gate static void getdomain();
870Sstevel@tonic-gate static void getlochost();
880Sstevel@tonic-gate static void getmapparms();
890Sstevel@tonic-gate static void newresults();
900Sstevel@tonic-gate static void getypserv();
910Sstevel@tonic-gate 
920Sstevel@tonic-gate extern void exit();
930Sstevel@tonic-gate extern int getdomainname();
940Sstevel@tonic-gate extern int gethostname();
950Sstevel@tonic-gate extern unsigned int strlen();
960Sstevel@tonic-gate extern int strcmp();
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * This is the mainline for the yppoll process.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate 
102*702Sth160488 int
main(argc,argv)1030Sstevel@tonic-gate main(argc, argv)
1040Sstevel@tonic-gate 	int argc;
1050Sstevel@tonic-gate 	char **argv;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if (!domain) {
1110Sstevel@tonic-gate 		getdomain();
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (!host) {
1150Sstevel@tonic-gate 		getypserv();
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	getmapparms();
119*702Sth160488 	return (status);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * This does the command line argument processing.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1260Sstevel@tonic-gate get_command_line_args(argc, argv)
1270Sstevel@tonic-gate 	int argc;
1280Sstevel@tonic-gate 	char **argv;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	argv++;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	while (--argc) {
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 			switch ((*argv)[1]) {
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 			case 'h':
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 				if (argc > 1) {
1420Sstevel@tonic-gate 					argv++;
1430Sstevel@tonic-gate 					argc--;
1440Sstevel@tonic-gate 					host = *argv;
1450Sstevel@tonic-gate 					argv++;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 					if ((int)strlen(host) > 256) {
1480Sstevel@tonic-gate 						(void) fprintf(stderr,
1490Sstevel@tonic-gate 						    err_bad_args,
1500Sstevel@tonic-gate 						    err_bad_hostname);
1510Sstevel@tonic-gate 						exit(1);
1520Sstevel@tonic-gate 					}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 				} else {
1550Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
1560Sstevel@tonic-gate 					exit(1);
1570Sstevel@tonic-gate 				}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 				break;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 			case 'd':
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 				if (argc > 1) {
1640Sstevel@tonic-gate 					argv++;
1650Sstevel@tonic-gate 					argc--;
1660Sstevel@tonic-gate 					domain = *argv;
1670Sstevel@tonic-gate 					argv++;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 					if ((int)strlen(domain) > YPMAXDOMAIN) {
1700Sstevel@tonic-gate 						(void) fprintf(stderr,
1710Sstevel@tonic-gate 						    err_bad_args,
1720Sstevel@tonic-gate 						    err_bad_domainname);
1730Sstevel@tonic-gate 						exit(1);
1740Sstevel@tonic-gate 					}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 				} else {
1770Sstevel@tonic-gate 					(void) fprintf(stderr, err_usage);
1780Sstevel@tonic-gate 					exit(1);
1790Sstevel@tonic-gate 				}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 				break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 			default:
1840Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
1850Sstevel@tonic-gate 				exit(1);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		} else {
1900Sstevel@tonic-gate 			if (!map) {
1910Sstevel@tonic-gate 				map = *argv;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 				if ((int)strlen(map) > YPMAXMAP) {
1940Sstevel@tonic-gate 					(void) fprintf(stderr, err_bad_args,
1950Sstevel@tonic-gate 					    err_bad_mapname);
1960Sstevel@tonic-gate 					exit(1);
1970Sstevel@tonic-gate 				}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 			} else {
2000Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
2010Sstevel@tonic-gate 				exit(1);
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (!map) {
2070Sstevel@tonic-gate 		(void) fprintf(stderr, err_usage);
2080Sstevel@tonic-gate 		exit(1);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
2140Sstevel@tonic-gate  * to something reasonable.  domain is set here.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate static void
getdomain()2170Sstevel@tonic-gate getdomain()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
2200Sstevel@tonic-gate 		domain = default_domain_name;
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
2230Sstevel@tonic-gate 		exit(1);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if ((int)strlen(domain) == 0) {
2270Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
2280Sstevel@tonic-gate 		exit(1);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * This gets the local hostname back from the kernel
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate static void
getlochost()2360Sstevel@tonic-gate getlochost()
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (! gethostname(default_host_name, 256)) {
2400Sstevel@tonic-gate 		host = default_host_name;
2410Sstevel@tonic-gate 	} else {
2420Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
2430Sstevel@tonic-gate 		exit(1);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static void
getmapparms()2480Sstevel@tonic-gate getmapparms()
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	CLIENT * map_clnt;
2510Sstevel@tonic-gate 	struct ypresp_order oresp;
2520Sstevel@tonic-gate 	struct ypreq_nokey req;
2530Sstevel@tonic-gate 	struct ypresp_master mresp;
254*702Sth160488 	struct ypresp_master *mresults = (struct ypresp_master *)NULL;
255*702Sth160488 	struct ypresp_order *oresults = (struct ypresp_order *)NULL;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	struct timeval timeout;
2580Sstevel@tonic-gate 	enum clnt_stat s;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if ((map_clnt = clnt_create(host, YPPROG, YPVERS,
2610Sstevel@tonic-gate 	    "netpath"))  == NULL) {
2620Sstevel@tonic-gate 		(void) fprintf(stderr,
2630Sstevel@tonic-gate 		    "Can't create connection to %s.\n", host);
2640Sstevel@tonic-gate 		clnt_pcreateerror("Reason");
2650Sstevel@tonic-gate 		exit(1);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	timeout.tv_sec = TIMEOUT;
2690Sstevel@tonic-gate 	timeout.tv_usec = 0;
2700Sstevel@tonic-gate 	req.domain = domain;
2710Sstevel@tonic-gate 	req.map = map;
2720Sstevel@tonic-gate 	mresp.master = NULL;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (clnt_call(map_clnt, YPPROC_MASTER,  (xdrproc_t)xdr_ypreq_nokey,
275*702Sth160488 		    (caddr_t)&req, (xdrproc_t)xdr_ypresp_master,
276*702Sth160488 		    (caddr_t)&mresp, timeout) == RPC_SUCCESS) {
2770Sstevel@tonic-gate 		mresults = &mresp;
2780Sstevel@tonic-gate 		s = (enum clnt_stat) clnt_call(map_clnt, YPPROC_ORDER,
2790Sstevel@tonic-gate 		    (xdrproc_t)xdr_ypreq_nokey, (char *)&req,
2800Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_order, (char *)&oresp, timeout);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		if (s == RPC_SUCCESS) {
2830Sstevel@tonic-gate 			oresults = &oresp;
2840Sstevel@tonic-gate 			newresults(mresults, oresults);
2850Sstevel@tonic-gate 		} else {
2860Sstevel@tonic-gate 			(void) fprintf(stderr,
2870Sstevel@tonic-gate 		"Can't make YPPROC_ORDER call to ypserv at %s.\n	",
2880Sstevel@tonic-gate 				host);
2890Sstevel@tonic-gate 			clnt_perror(map_clnt, "Reason");
2900Sstevel@tonic-gate 			exit(1);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	} else {
2940Sstevel@tonic-gate 		clnt_destroy(map_clnt);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate static void
newresults(m,o)2990Sstevel@tonic-gate newresults(m, o)
3000Sstevel@tonic-gate 	struct ypresp_master *m;
3010Sstevel@tonic-gate 	struct ypresp_order *o;
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	char *s_domok = "Domain %s is supported.\n";
3040Sstevel@tonic-gate 	char *s_ook = "Map %s has order number %d.\n";
3050Sstevel@tonic-gate 	char *s_mok = "The master server is %s.\n";
3060Sstevel@tonic-gate 	char *s_mbad = "Can't get master for map %s.\n	Reason:  %s\n";
3070Sstevel@tonic-gate 	char *s_obad = "Can't get order number for map %s.\n	Reason:  %s\n";
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (m->status == YP_TRUE && o->status == YP_TRUE) {
3100Sstevel@tonic-gate 		(void) printf(s_domok, domain);
3110Sstevel@tonic-gate 		(void) printf(s_ook, map, o->ordernum);
3120Sstevel@tonic-gate 		(void) printf(s_mok, m->master);
3130Sstevel@tonic-gate 	} else if (o->status == YP_TRUE)  {
3140Sstevel@tonic-gate 		(void) printf(s_domok, domain);
3150Sstevel@tonic-gate 		(void) printf(s_ook, map, o->ordernum);
3160Sstevel@tonic-gate 		(void) fprintf(stderr, s_mbad, map,
3170Sstevel@tonic-gate 		    yperr_string(ypprot_err(m->status)));
3180Sstevel@tonic-gate 		status = 1;
3190Sstevel@tonic-gate 	} else if (m->status == YP_TRUE)  {
3200Sstevel@tonic-gate 		(void) printf(s_domok, domain);
3210Sstevel@tonic-gate 		(void) fprintf(stderr, s_obad, map,
3220Sstevel@tonic-gate 		    yperr_string(ypprot_err(o->status)));
3230Sstevel@tonic-gate 		(void) printf(s_mok, m->master);
3240Sstevel@tonic-gate 		status = 1;
3250Sstevel@tonic-gate 	} else {
3260Sstevel@tonic-gate 		(void) fprintf(stderr,
3270Sstevel@tonic-gate 			"Can't get any map parameter information.\n");
3280Sstevel@tonic-gate 		(void) fprintf(stderr, s_obad, map,
3290Sstevel@tonic-gate 		    yperr_string(ypprot_err(o->status)));
3300Sstevel@tonic-gate 		(void) fprintf(stderr, s_mbad, map,
3310Sstevel@tonic-gate 		    yperr_string(ypprot_err(m->status)));
3320Sstevel@tonic-gate 		status = 1;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static void
getypserv()3370Sstevel@tonic-gate getypserv()
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	struct ypbind_resp response;
3400Sstevel@tonic-gate 	struct ypbind_domain ypdomain;
3410Sstevel@tonic-gate 	struct ypbind_binding *binding;
3420Sstevel@tonic-gate 	static char hostbuf[256];
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	getlochost();
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	(void) memset((char *)&response, 0, sizeof (response));
3470Sstevel@tonic-gate 	ypdomain.ypbind_domainname = domain;
3480Sstevel@tonic-gate 	ypdomain.ypbind_vers = YPBINDVERS;
3490Sstevel@tonic-gate 	(void) rpc_call(host, YPBINDPROG, YPBINDVERS, YPBINDPROC_DOMAIN,
3500Sstevel@tonic-gate 	    xdr_ypbind_domain, (char *)&ypdomain, xdr_ypbind_resp,
3510Sstevel@tonic-gate 	    (char *)&response, "netpath");
3520Sstevel@tonic-gate 	if (response.ypbind_status != YPBIND_SUCC_VAL) {
3530Sstevel@tonic-gate 		(void) fprintf(stderr, "couldn't get yp server - status %u\n",
3540Sstevel@tonic-gate 		    response.ypbind_status);
3550Sstevel@tonic-gate 		exit(1);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 	binding = response.ypbind_resp_u.ypbind_bindinfo;
3580Sstevel@tonic-gate 	host = binding->ypbind_servername;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 *  When ypbind is running in broadcast mode, it sets the
3620Sstevel@tonic-gate 	 *  servername to "".  To get the real name of the server,
3630Sstevel@tonic-gate 	 *  we need to do a host lookup on the svcaddr.  This code
3640Sstevel@tonic-gate 	 *  is similar to code in ypwhich.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	if (strcmp(host, "") == 0) {
3670Sstevel@tonic-gate 		struct nd_hostservlist *nhs;
3680Sstevel@tonic-gate 		struct netconfig *nconf = binding->ypbind_nconf;
3690Sstevel@tonic-gate 		struct netbuf *svcaddr = binding->ypbind_svcaddr;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
3720Sstevel@tonic-gate 			struct sockaddr_in *sa;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 			sa = (struct sockaddr_in *)svcaddr->buf;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 			strcpy(hostbuf, inet_ntoa(sa->sin_addr));
3770Sstevel@tonic-gate 		} else {
3780Sstevel@tonic-gate 			sprintf(hostbuf, "%s", nhs->h_hostservs->h_host);
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 		host = hostbuf;
3810Sstevel@tonic-gate 		netdir_free((char *)nhs, ND_HOSTSERVLIST);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate }
384