xref: /onnv-gate/usr/src/cmd/ypcmd/ypset.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 issues a "Set domain binding" command to a
390Sstevel@tonic-gate  * YP binder (ypbind) process
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  *	ypset [-h <host>] [-d <domainname>] server_to_use
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * where host and server_to_use may be either names or internet addresses.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <rpc/rpc.h>
480Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
490Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
500Sstevel@tonic-gate #include "yp_b.h"
510Sstevel@tonic-gate #include <sys/utsname.h>
520Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef NULL
550Sstevel@tonic-gate #undef NULL
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate #define	NULL 0
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static char *pusage;
620Sstevel@tonic-gate static char *domain = NULL;
630Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
640Sstevel@tonic-gate static char default_host_name[256];
650Sstevel@tonic-gate static char *host = NULL;
660Sstevel@tonic-gate static char *server_to_use;
670Sstevel@tonic-gate static struct timeval timeout = {
680Sstevel@tonic-gate 	TIMEOUT,			/* Seconds */
690Sstevel@tonic-gate 	0				/* Microseconds */
700Sstevel@tonic-gate 	};
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static char err_usage_set[] =
730Sstevel@tonic-gate "Usage:\n\
740Sstevel@tonic-gate 	ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
750Sstevel@tonic-gate static char err_bad_args[] =
760Sstevel@tonic-gate 	"Sorry, the %s argument is bad.\n";
770Sstevel@tonic-gate static char err_cant_get_kname[] =
780Sstevel@tonic-gate 	"Sorry, can't get %s back from system call.\n";
790Sstevel@tonic-gate static char err_null_kname[] =
800Sstevel@tonic-gate 	"Sorry, the %s hasn't been set on this machine.\n";
810Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
820Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
830Sstevel@tonic-gate static char err_bad_server[] = "server_to_use";
840Sstevel@tonic-gate static char err_tp_failure[] =
850Sstevel@tonic-gate 	"Sorry, I can't set up a connection to host %s.\n";
860Sstevel@tonic-gate static char err_rpc_failure[] =
870Sstevel@tonic-gate 	"Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
880Sstevel@tonic-gate static char err_access_failure[] =
890Sstevel@tonic-gate 	"ypset: Sorry, ypbind on host %s has rejected your request.\n";
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static void get_command_line_args();
920Sstevel@tonic-gate static void send_message();
930Sstevel@tonic-gate 
940Sstevel@tonic-gate extern void exit();
950Sstevel@tonic-gate extern int getdomainname();
960Sstevel@tonic-gate extern int gethostname();
970Sstevel@tonic-gate extern struct netconfig *getnetconfigent();
980Sstevel@tonic-gate extern unsigned int strlen();
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * This is the mainline for the ypset process.  It pulls whatever arguments
1020Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
105*702Sth160488 int
main(argc,argv)1060Sstevel@tonic-gate main(argc, argv)
1070Sstevel@tonic-gate 	int argc;
1080Sstevel@tonic-gate 	char **argv;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (!domain) {
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
1160Sstevel@tonic-gate 			domain = default_domain_name;
1170Sstevel@tonic-gate 		} else {
1180Sstevel@tonic-gate 			(void) fprintf(stderr,
119*702Sth160488 				err_cant_get_kname,
120*702Sth160488 				err_bad_domainname);
1210Sstevel@tonic-gate 			exit(1);
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 
124*702Sth160488 		if ((int)strlen(domain) == 0) {
1250Sstevel@tonic-gate 			(void) fprintf(stderr,
126*702Sth160488 				err_null_kname,
127*702Sth160488 				err_bad_domainname);
1280Sstevel@tonic-gate 			exit(1);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	send_message();
132*702Sth160488 	return (0);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * This does the command line argument processing.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1390Sstevel@tonic-gate get_command_line_args(argc, argv)
1400Sstevel@tonic-gate 	int argc;
1410Sstevel@tonic-gate 	char **argv;
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	pusage = err_usage_set;
1440Sstevel@tonic-gate 	argv++;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	while (--argc > 1) {
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 			switch ((*argv)[1]) {
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 			case 'h': {
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 				if (argc > 1) {
1550Sstevel@tonic-gate 					struct utsname utsname;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 					argv++;
1580Sstevel@tonic-gate 					argc--;
1590Sstevel@tonic-gate 					(void) uname(&utsname);
1600Sstevel@tonic-gate 					if (strcasecmp(utsname.nodename,
1610Sstevel@tonic-gate 						*argv) != 0) {
1620Sstevel@tonic-gate 						host = *argv;
1630Sstevel@tonic-gate 
164*702Sth160488 						if ((int)strlen(host) > 256) {
1650Sstevel@tonic-gate 							(void) fprintf(stderr,
1660Sstevel@tonic-gate 	err_bad_args,
1670Sstevel@tonic-gate 	err_bad_hostname);
1680Sstevel@tonic-gate 							exit(1);
1690Sstevel@tonic-gate 						}
1700Sstevel@tonic-gate 					}
1710Sstevel@tonic-gate 					argv++;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 				} else {
1740Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
1750Sstevel@tonic-gate 					exit(1);
1760Sstevel@tonic-gate 				}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 				break;
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 			case 'd': {
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 				if (argc > 1) {
1840Sstevel@tonic-gate 					argv++;
1850Sstevel@tonic-gate 					argc--;
1860Sstevel@tonic-gate 					domain = *argv;
1870Sstevel@tonic-gate 					argv++;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 					if (strlen(domain) > YPMAXDOMAIN) {
1900Sstevel@tonic-gate 						(void) fprintf(stderr,
1910Sstevel@tonic-gate 	err_bad_args,
1920Sstevel@tonic-gate 	err_bad_domainname);
1930Sstevel@tonic-gate 						exit(1);
1940Sstevel@tonic-gate 					}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 				} else {
1970Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
1980Sstevel@tonic-gate 					exit(1);
1990Sstevel@tonic-gate 				}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 				break;
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 			default: {
2050Sstevel@tonic-gate 				(void) fprintf(stderr, pusage);
2060Sstevel@tonic-gate 				exit(1);
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 			}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		} else {
2120Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
2130Sstevel@tonic-gate 			exit(1);
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (argc == 1) {
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
2200Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
2210Sstevel@tonic-gate 			exit(1);
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		server_to_use = *argv;
2250Sstevel@tonic-gate 
226*702Sth160488 		if ((int)strlen(server_to_use) > 256) {
2270Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args,
2280Sstevel@tonic-gate 			    err_bad_server);
2290Sstevel@tonic-gate 			exit(1);
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	} else {
2330Sstevel@tonic-gate 		(void) fprintf(stderr, pusage);
2340Sstevel@tonic-gate 		exit(1);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * This takes the name of the YP host of interest, and fires off
2400Sstevel@tonic-gate  * the "set domain binding" message to the ypbind process.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static void
send_message()2440Sstevel@tonic-gate send_message()
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	CLIENT *server, *client;
2470Sstevel@tonic-gate 	struct ypbind_setdom req;
2480Sstevel@tonic-gate 	struct ypbind_binding ypbind_info;
2490Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
2500Sstevel@tonic-gate 	struct netconfig *nconf;
2510Sstevel@tonic-gate 	struct netbuf nbuf;
2520Sstevel@tonic-gate 	int err;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Open up a path to the server
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
2590Sstevel@tonic-gate 	    "datagram_n")) == NULL) {
2600Sstevel@tonic-gate 		(void) fprintf(stderr, err_tp_failure, server_to_use);
2610Sstevel@tonic-gate 		exit(1);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* get nconf, netbuf structures */
2650Sstevel@tonic-gate 	nconf = getnetconfigent(server->cl_netid);
266*702Sth160488 	clnt_control(server, CLGET_SVC_ADDR, (char *)&nbuf);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * Open a path to host
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (!host) {
2730Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
2740Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
2750Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2760Sstevel@tonic-gate 			exit(1);
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 		client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
2790Sstevel@tonic-gate 		if (client->cl_auth == NULL) {
2800Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2810Sstevel@tonic-gate 			exit(1);
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 	} else {
2840Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG,
2850Sstevel@tonic-gate 			YPBINDVERS, "datagram_n");
2860Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
2870Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2880Sstevel@tonic-gate 			exit(1);
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * Load up the message structure and fire it off.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	ypbind_info.ypbind_nconf = nconf;
2960Sstevel@tonic-gate 	ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
2970Sstevel@tonic-gate 	ypbind_info.ypbind_servername = server_to_use;
2980Sstevel@tonic-gate 	ypbind_info.ypbind_hi_vers = YPVERS;
2990Sstevel@tonic-gate 	ypbind_info.ypbind_lo_vers = YPVERS;
3000Sstevel@tonic-gate 	req.ypsetdom_bindinfo = &ypbind_info;
3010Sstevel@tonic-gate 	req.ypsetdom_domain =  domain;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	clnt_stat = (enum clnt_stat) clnt_call(client,
3040Sstevel@tonic-gate 	    YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
3050Sstevel@tonic-gate 	    timeout);
3060Sstevel@tonic-gate 	if (clnt_stat != RPC_SUCCESS) {
3070Sstevel@tonic-gate 		if (clnt_stat == RPC_PROGUNAVAIL)
3080Sstevel@tonic-gate 			(void) fprintf(stderr,
3090Sstevel@tonic-gate 	err_access_failure, host ? host : "localhost");
3100Sstevel@tonic-gate 		else
3110Sstevel@tonic-gate 			(void) fprintf(stderr,
3120Sstevel@tonic-gate 	err_rpc_failure, host ? host : "localhost");
3130Sstevel@tonic-gate 		exit(1);
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 	if (!host)
3160Sstevel@tonic-gate 		auth_destroy((client)->cl_auth);
3170Sstevel@tonic-gate 	(void) clnt_destroy(server);
3180Sstevel@tonic-gate 	(void) clnt_destroy(client);
3190Sstevel@tonic-gate }
320