xref: /titanic_51/usr/src/cmd/ypcmd/ypset.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 1997 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 issues a "Set domain binding" command to a
39*7c478bd9Sstevel@tonic-gate  * YP binder (ypbind) process
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  *	ypset [-h <host>] [-d <domainname>] server_to_use
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * where host and server_to_use may be either names or internet addresses.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate #include <stdio.h>
46*7c478bd9Sstevel@tonic-gate #include <ctype.h>
47*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
48*7c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
49*7c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
50*7c478bd9Sstevel@tonic-gate #include "yp_b.h"
51*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
52*7c478bd9Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #ifdef NULL
55*7c478bd9Sstevel@tonic-gate #undef NULL
56*7c478bd9Sstevel@tonic-gate #endif
57*7c478bd9Sstevel@tonic-gate #define	NULL 0
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static char *pusage;
62*7c478bd9Sstevel@tonic-gate static char *domain = NULL;
63*7c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
64*7c478bd9Sstevel@tonic-gate static char default_host_name[256];
65*7c478bd9Sstevel@tonic-gate static char *host = NULL;
66*7c478bd9Sstevel@tonic-gate static char *server_to_use;
67*7c478bd9Sstevel@tonic-gate static struct timeval timeout = {
68*7c478bd9Sstevel@tonic-gate 	TIMEOUT,			/* Seconds */
69*7c478bd9Sstevel@tonic-gate 	0				/* Microseconds */
70*7c478bd9Sstevel@tonic-gate 	};
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static char err_usage_set[] =
73*7c478bd9Sstevel@tonic-gate "Usage:\n\
74*7c478bd9Sstevel@tonic-gate 	ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
75*7c478bd9Sstevel@tonic-gate static char err_bad_args[] =
76*7c478bd9Sstevel@tonic-gate 	"Sorry, the %s argument is bad.\n";
77*7c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
78*7c478bd9Sstevel@tonic-gate 	"Sorry, can't get %s back from system call.\n";
79*7c478bd9Sstevel@tonic-gate static char err_null_kname[] =
80*7c478bd9Sstevel@tonic-gate 	"Sorry, the %s hasn't been set on this machine.\n";
81*7c478bd9Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
82*7c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
83*7c478bd9Sstevel@tonic-gate static char err_bad_server[] = "server_to_use";
84*7c478bd9Sstevel@tonic-gate static char err_tp_failure[] =
85*7c478bd9Sstevel@tonic-gate 	"Sorry, I can't set up a connection to host %s.\n";
86*7c478bd9Sstevel@tonic-gate static char err_rpc_failure[] =
87*7c478bd9Sstevel@tonic-gate 	"Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
88*7c478bd9Sstevel@tonic-gate static char err_access_failure[] =
89*7c478bd9Sstevel@tonic-gate 	"ypset: Sorry, ypbind on host %s has rejected your request.\n";
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate static void get_command_line_args();
92*7c478bd9Sstevel@tonic-gate static void send_message();
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate extern void exit();
95*7c478bd9Sstevel@tonic-gate extern int getdomainname();
96*7c478bd9Sstevel@tonic-gate extern int gethostname();
97*7c478bd9Sstevel@tonic-gate extern struct netconfig *getnetconfigent();
98*7c478bd9Sstevel@tonic-gate extern unsigned int strlen();
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * This is the mainline for the ypset process.  It pulls whatever arguments
102*7c478bd9Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate void
106*7c478bd9Sstevel@tonic-gate main(argc, argv)
107*7c478bd9Sstevel@tonic-gate 	int argc;
108*7c478bd9Sstevel@tonic-gate 	char **argv;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (!domain) {
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
116*7c478bd9Sstevel@tonic-gate 			domain = default_domain_name;
117*7c478bd9Sstevel@tonic-gate 		} else {
118*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
119*7c478bd9Sstevel@tonic-gate 	err_cant_get_kname,
120*7c478bd9Sstevel@tonic-gate 	err_bad_domainname);
121*7c478bd9Sstevel@tonic-gate 			exit(1);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 		if ((int) strlen(domain) == 0) {
125*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
126*7c478bd9Sstevel@tonic-gate 	err_null_kname,
127*7c478bd9Sstevel@tonic-gate 	err_bad_domainname);
128*7c478bd9Sstevel@tonic-gate 			exit(1);
129*7c478bd9Sstevel@tonic-gate 		}
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	send_message();
132*7c478bd9Sstevel@tonic-gate 	exit(0);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
137*7c478bd9Sstevel@tonic-gate  */
138*7c478bd9Sstevel@tonic-gate static void
139*7c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
140*7c478bd9Sstevel@tonic-gate 	int argc;
141*7c478bd9Sstevel@tonic-gate 	char **argv;
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	pusage = err_usage_set;
144*7c478bd9Sstevel@tonic-gate 	argv++;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	while (--argc > 1) {
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 			switch ((*argv)[1]) {
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			case 'h': {
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 				if (argc > 1) {
155*7c478bd9Sstevel@tonic-gate 					struct utsname utsname;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 					argv++;
158*7c478bd9Sstevel@tonic-gate 					argc--;
159*7c478bd9Sstevel@tonic-gate 					(void) uname(&utsname);
160*7c478bd9Sstevel@tonic-gate 					if (strcasecmp(utsname.nodename,
161*7c478bd9Sstevel@tonic-gate 						*argv) != 0) {
162*7c478bd9Sstevel@tonic-gate 						host = *argv;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 						if ((int) strlen(host) > 256) {
165*7c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
166*7c478bd9Sstevel@tonic-gate 	err_bad_args,
167*7c478bd9Sstevel@tonic-gate 	err_bad_hostname);
168*7c478bd9Sstevel@tonic-gate 							exit(1);
169*7c478bd9Sstevel@tonic-gate 						}
170*7c478bd9Sstevel@tonic-gate 					}
171*7c478bd9Sstevel@tonic-gate 					argv++;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 				} else {
174*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
175*7c478bd9Sstevel@tonic-gate 					exit(1);
176*7c478bd9Sstevel@tonic-gate 				}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 				break;
179*7c478bd9Sstevel@tonic-gate 			}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 			case 'd': {
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 				if (argc > 1) {
184*7c478bd9Sstevel@tonic-gate 					argv++;
185*7c478bd9Sstevel@tonic-gate 					argc--;
186*7c478bd9Sstevel@tonic-gate 					domain = *argv;
187*7c478bd9Sstevel@tonic-gate 					argv++;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 					if (strlen(domain) > YPMAXDOMAIN) {
190*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
191*7c478bd9Sstevel@tonic-gate 	err_bad_args,
192*7c478bd9Sstevel@tonic-gate 	err_bad_domainname);
193*7c478bd9Sstevel@tonic-gate 						exit(1);
194*7c478bd9Sstevel@tonic-gate 					}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 				} else {
197*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
198*7c478bd9Sstevel@tonic-gate 					exit(1);
199*7c478bd9Sstevel@tonic-gate 				}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 				break;
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 			default: {
205*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, pusage);
206*7c478bd9Sstevel@tonic-gate 				exit(1);
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 			}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 		} else {
212*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
213*7c478bd9Sstevel@tonic-gate 			exit(1);
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	if (argc == 1) {
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
220*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
221*7c478bd9Sstevel@tonic-gate 			exit(1);
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		server_to_use = *argv;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		if ((int) strlen(server_to_use) > 256) {
227*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args,
228*7c478bd9Sstevel@tonic-gate 			    err_bad_server);
229*7c478bd9Sstevel@tonic-gate 			exit(1);
230*7c478bd9Sstevel@tonic-gate 		}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	} else {
233*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, pusage);
234*7c478bd9Sstevel@tonic-gate 		exit(1);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate  * This takes the name of the YP host of interest, and fires off
240*7c478bd9Sstevel@tonic-gate  * the "set domain binding" message to the ypbind process.
241*7c478bd9Sstevel@tonic-gate  */
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate static void
244*7c478bd9Sstevel@tonic-gate send_message()
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	CLIENT *server, *client;
247*7c478bd9Sstevel@tonic-gate 	struct ypbind_setdom req;
248*7c478bd9Sstevel@tonic-gate 	struct ypbind_binding ypbind_info;
249*7c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
250*7c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
251*7c478bd9Sstevel@tonic-gate 	struct netbuf nbuf;
252*7c478bd9Sstevel@tonic-gate 	int err;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	/*
255*7c478bd9Sstevel@tonic-gate 	 * Open up a path to the server
256*7c478bd9Sstevel@tonic-gate 	 */
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
259*7c478bd9Sstevel@tonic-gate 	    "datagram_n")) == NULL) {
260*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_tp_failure, server_to_use);
261*7c478bd9Sstevel@tonic-gate 		exit(1);
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	/* get nconf, netbuf structures */
265*7c478bd9Sstevel@tonic-gate 	nconf = getnetconfigent(server->cl_netid);
266*7c478bd9Sstevel@tonic-gate 	clnt_control(server, CLGET_SVC_ADDR, (char *) &nbuf);
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	/*
269*7c478bd9Sstevel@tonic-gate 	 * Open a path to host
270*7c478bd9Sstevel@tonic-gate 	 */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	if (!host) {
273*7c478bd9Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
274*7c478bd9Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
275*7c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
276*7c478bd9Sstevel@tonic-gate 			exit(1);
277*7c478bd9Sstevel@tonic-gate 		}
278*7c478bd9Sstevel@tonic-gate 		client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
279*7c478bd9Sstevel@tonic-gate 		if (client->cl_auth == NULL) {
280*7c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
281*7c478bd9Sstevel@tonic-gate 			exit(1);
282*7c478bd9Sstevel@tonic-gate 		}
283*7c478bd9Sstevel@tonic-gate 	} else {
284*7c478bd9Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG,
285*7c478bd9Sstevel@tonic-gate 			YPBINDVERS, "datagram_n");
286*7c478bd9Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
287*7c478bd9Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
288*7c478bd9Sstevel@tonic-gate 			exit(1);
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	/*
293*7c478bd9Sstevel@tonic-gate 	 * Load up the message structure and fire it off.
294*7c478bd9Sstevel@tonic-gate 	 */
295*7c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_nconf = nconf;
296*7c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
297*7c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_servername = server_to_use;
298*7c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_hi_vers = YPVERS;
299*7c478bd9Sstevel@tonic-gate 	ypbind_info.ypbind_lo_vers = YPVERS;
300*7c478bd9Sstevel@tonic-gate 	req.ypsetdom_bindinfo = &ypbind_info;
301*7c478bd9Sstevel@tonic-gate 	req.ypsetdom_domain =  domain;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	clnt_stat = (enum clnt_stat) clnt_call(client,
304*7c478bd9Sstevel@tonic-gate 	    YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
305*7c478bd9Sstevel@tonic-gate 	    timeout);
306*7c478bd9Sstevel@tonic-gate 	if (clnt_stat != RPC_SUCCESS) {
307*7c478bd9Sstevel@tonic-gate 		if (clnt_stat == RPC_PROGUNAVAIL)
308*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
309*7c478bd9Sstevel@tonic-gate 	err_access_failure, host ? host : "localhost");
310*7c478bd9Sstevel@tonic-gate 		else
311*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
312*7c478bd9Sstevel@tonic-gate 	err_rpc_failure, host ? host : "localhost");
313*7c478bd9Sstevel@tonic-gate 		exit(1);
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 	if (!host)
316*7c478bd9Sstevel@tonic-gate 		auth_destroy((client)->cl_auth);
317*7c478bd9Sstevel@tonic-gate 	(void) clnt_destroy(server);
318*7c478bd9Sstevel@tonic-gate 	(void) clnt_destroy(client);
319*7c478bd9Sstevel@tonic-gate }
320