xref: /illumos-gate/usr/src/cmd/ypcmd/ypset.c (revision 2a8bcb4e)
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*a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This is a user command which issues a "Set domain binding" command to a
377c478bd9Sstevel@tonic-gate  * YP binder (ypbind) process
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  *	ypset [-h <host>] [-d <domainname>] server_to_use
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * where host and server_to_use may be either names or internet addresses.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
477c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
487c478bd9Sstevel@tonic-gate #include "yp_b.h"
497c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
507c478bd9Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #ifdef NULL
537c478bd9Sstevel@tonic-gate #undef NULL
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate #define	NULL 0
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static char *pusage;
607c478bd9Sstevel@tonic-gate static char *domain = NULL;
617c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
627c478bd9Sstevel@tonic-gate static char default_host_name[256];
637c478bd9Sstevel@tonic-gate static char *host = NULL;
647c478bd9Sstevel@tonic-gate static char *server_to_use;
657c478bd9Sstevel@tonic-gate static struct timeval timeout = {
667c478bd9Sstevel@tonic-gate 	TIMEOUT,			/* Seconds */
677c478bd9Sstevel@tonic-gate 	0				/* Microseconds */
687c478bd9Sstevel@tonic-gate 	};
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static char err_usage_set[] =
717c478bd9Sstevel@tonic-gate "Usage:\n\
727c478bd9Sstevel@tonic-gate 	ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
737c478bd9Sstevel@tonic-gate static char err_bad_args[] =
747c478bd9Sstevel@tonic-gate 	"Sorry, the %s argument is bad.\n";
757c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
767c478bd9Sstevel@tonic-gate 	"Sorry, can't get %s back from system call.\n";
777c478bd9Sstevel@tonic-gate static char err_null_kname[] =
787c478bd9Sstevel@tonic-gate 	"Sorry, the %s hasn't been set on this machine.\n";
797c478bd9Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
807c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
817c478bd9Sstevel@tonic-gate static char err_bad_server[] = "server_to_use";
827c478bd9Sstevel@tonic-gate static char err_tp_failure[] =
837c478bd9Sstevel@tonic-gate 	"Sorry, I can't set up a connection to host %s.\n";
847c478bd9Sstevel@tonic-gate static char err_rpc_failure[] =
857c478bd9Sstevel@tonic-gate 	"Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
867c478bd9Sstevel@tonic-gate static char err_access_failure[] =
877c478bd9Sstevel@tonic-gate 	"ypset: Sorry, ypbind on host %s has rejected your request.\n";
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static void get_command_line_args();
907c478bd9Sstevel@tonic-gate static void send_message();
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate extern void exit();
937c478bd9Sstevel@tonic-gate extern int getdomainname();
947c478bd9Sstevel@tonic-gate extern int gethostname();
957c478bd9Sstevel@tonic-gate extern struct netconfig *getnetconfigent();
967c478bd9Sstevel@tonic-gate extern unsigned int strlen();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * This is the mainline for the ypset process.  It pulls whatever arguments
1007c478bd9Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate 
103*a506a34cSth int
main(argc,argv)1047c478bd9Sstevel@tonic-gate main(argc, argv)
1057c478bd9Sstevel@tonic-gate 	int argc;
1067c478bd9Sstevel@tonic-gate 	char **argv;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (!domain) {
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
1147c478bd9Sstevel@tonic-gate 			domain = default_domain_name;
1157c478bd9Sstevel@tonic-gate 		} else {
1167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
117*a506a34cSth 				err_cant_get_kname,
118*a506a34cSth 				err_bad_domainname);
1197c478bd9Sstevel@tonic-gate 			exit(1);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 
122*a506a34cSth 		if ((int)strlen(domain) == 0) {
1237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
124*a506a34cSth 				err_null_kname,
125*a506a34cSth 				err_bad_domainname);
1267c478bd9Sstevel@tonic-gate 			exit(1);
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	send_message();
130*a506a34cSth 	return (0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1377c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
1387c478bd9Sstevel@tonic-gate 	int argc;
1397c478bd9Sstevel@tonic-gate 	char **argv;
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	pusage = err_usage_set;
1427c478bd9Sstevel@tonic-gate 	argv++;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	while (--argc > 1) {
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 			switch ((*argv)[1]) {
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 			case 'h': {
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 				if (argc > 1) {
1537c478bd9Sstevel@tonic-gate 					struct utsname utsname;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 					argv++;
1567c478bd9Sstevel@tonic-gate 					argc--;
1577c478bd9Sstevel@tonic-gate 					(void) uname(&utsname);
1587c478bd9Sstevel@tonic-gate 					if (strcasecmp(utsname.nodename,
1597c478bd9Sstevel@tonic-gate 						*argv) != 0) {
1607c478bd9Sstevel@tonic-gate 						host = *argv;
1617c478bd9Sstevel@tonic-gate 
162*a506a34cSth 						if ((int)strlen(host) > 256) {
1637c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
1647c478bd9Sstevel@tonic-gate 	err_bad_args,
1657c478bd9Sstevel@tonic-gate 	err_bad_hostname);
1667c478bd9Sstevel@tonic-gate 							exit(1);
1677c478bd9Sstevel@tonic-gate 						}
1687c478bd9Sstevel@tonic-gate 					}
1697c478bd9Sstevel@tonic-gate 					argv++;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 				} else {
1727c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
1737c478bd9Sstevel@tonic-gate 					exit(1);
1747c478bd9Sstevel@tonic-gate 				}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 				break;
1777c478bd9Sstevel@tonic-gate 			}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 			case 'd': {
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 				if (argc > 1) {
1827c478bd9Sstevel@tonic-gate 					argv++;
1837c478bd9Sstevel@tonic-gate 					argc--;
1847c478bd9Sstevel@tonic-gate 					domain = *argv;
1857c478bd9Sstevel@tonic-gate 					argv++;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 					if (strlen(domain) > YPMAXDOMAIN) {
1887c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1897c478bd9Sstevel@tonic-gate 	err_bad_args,
1907c478bd9Sstevel@tonic-gate 	err_bad_domainname);
1917c478bd9Sstevel@tonic-gate 						exit(1);
1927c478bd9Sstevel@tonic-gate 					}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 				} else {
1957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
1967c478bd9Sstevel@tonic-gate 					exit(1);
1977c478bd9Sstevel@tonic-gate 				}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 				break;
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 			default: {
2037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, pusage);
2047c478bd9Sstevel@tonic-gate 				exit(1);
2057c478bd9Sstevel@tonic-gate 			}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		} else {
2107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
2117c478bd9Sstevel@tonic-gate 			exit(1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (argc == 1) {
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
2187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
2197c478bd9Sstevel@tonic-gate 			exit(1);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		server_to_use = *argv;
2237c478bd9Sstevel@tonic-gate 
224*a506a34cSth 		if ((int)strlen(server_to_use) > 256) {
2257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args,
2267c478bd9Sstevel@tonic-gate 			    err_bad_server);
2277c478bd9Sstevel@tonic-gate 			exit(1);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	} else {
2317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, pusage);
2327c478bd9Sstevel@tonic-gate 		exit(1);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * This takes the name of the YP host of interest, and fires off
2387c478bd9Sstevel@tonic-gate  * the "set domain binding" message to the ypbind process.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static void
send_message()2427c478bd9Sstevel@tonic-gate send_message()
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	CLIENT *server, *client;
2457c478bd9Sstevel@tonic-gate 	struct ypbind_setdom req;
2467c478bd9Sstevel@tonic-gate 	struct ypbind_binding ypbind_info;
2477c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
2487c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
2497c478bd9Sstevel@tonic-gate 	struct netbuf nbuf;
2507c478bd9Sstevel@tonic-gate 	int err;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Open up a path to the server
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
2577c478bd9Sstevel@tonic-gate 	    "datagram_n")) == NULL) {
2587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_tp_failure, server_to_use);
2597c478bd9Sstevel@tonic-gate 		exit(1);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/* get nconf, netbuf structures */
2637c478bd9Sstevel@tonic-gate 	nconf = getnetconfigent(server->cl_netid);
264*a506a34cSth 	clnt_control(server, CLGET_SVC_ADDR, (char *)&nbuf);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Open a path to host
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (!host) {
2717c478bd9Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
2727c478bd9Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
2737c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2747c478bd9Sstevel@tonic-gate 			exit(1);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
2777c478bd9Sstevel@tonic-gate 		if (client->cl_auth == NULL) {
2787c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2797c478bd9Sstevel@tonic-gate 			exit(1);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 	} else {
2827c478bd9Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG,
2837c478bd9Sstevel@tonic-gate 			YPBINDVERS, "datagram_n");
2847c478bd9Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
2857c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
2867c478bd9Sstevel@tonic-gate 			exit(1);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Load up the message structure and fire it off.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_nconf = nconf;
2947c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
2957c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_servername = server_to_use;
2967c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_hi_vers = YPVERS;
2977c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_lo_vers = YPVERS;
2987c478bd9Sstevel@tonic-gate 	req.ypsetdom_bindinfo = &ypbind_info;
2997c478bd9Sstevel@tonic-gate 	req.ypsetdom_domain =  domain;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	clnt_stat = (enum clnt_stat) clnt_call(client,
3027c478bd9Sstevel@tonic-gate 	    YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
3037c478bd9Sstevel@tonic-gate 	    timeout);
3047c478bd9Sstevel@tonic-gate 	if (clnt_stat != RPC_SUCCESS) {
3057c478bd9Sstevel@tonic-gate 		if (clnt_stat == RPC_PROGUNAVAIL)
3067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3077c478bd9Sstevel@tonic-gate 	err_access_failure, host ? host : "localhost");
3087c478bd9Sstevel@tonic-gate 		else
3097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3107c478bd9Sstevel@tonic-gate 	err_rpc_failure, host ? host : "localhost");
3117c478bd9Sstevel@tonic-gate 		exit(1);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	if (!host)
3147c478bd9Sstevel@tonic-gate 		auth_destroy((client)->cl_auth);
3157c478bd9Sstevel@tonic-gate 	(void) clnt_destroy(server);
3167c478bd9Sstevel@tonic-gate 	(void) clnt_destroy(client);
3177c478bd9Sstevel@tonic-gate }
318