xref: /onnv-gate/usr/src/cmd/ypcmd/ypset.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*0Sstevel@tonic-gate /*	  All Rights Reserved   */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
31*0Sstevel@tonic-gate  * under license from the Regents of the University of
32*0Sstevel@tonic-gate  * California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * This is a user command which issues a "Set domain binding" command to a
39*0Sstevel@tonic-gate  * YP binder (ypbind) process
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  *	ypset [-h <host>] [-d <domainname>] server_to_use
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * where host and server_to_use may be either names or internet addresses.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <ctype.h>
47*0Sstevel@tonic-gate #include <rpc/rpc.h>
48*0Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
49*0Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
50*0Sstevel@tonic-gate #include "yp_b.h"
51*0Sstevel@tonic-gate #include <sys/utsname.h>
52*0Sstevel@tonic-gate extern CLIENT *__clnt_create_loopback();
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #ifdef NULL
55*0Sstevel@tonic-gate #undef NULL
56*0Sstevel@tonic-gate #endif
57*0Sstevel@tonic-gate #define	NULL 0
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	TIMEOUT 30			/* Total seconds for timeout */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static char *pusage;
62*0Sstevel@tonic-gate static char *domain = NULL;
63*0Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
64*0Sstevel@tonic-gate static char default_host_name[256];
65*0Sstevel@tonic-gate static char *host = NULL;
66*0Sstevel@tonic-gate static char *server_to_use;
67*0Sstevel@tonic-gate static struct timeval timeout = {
68*0Sstevel@tonic-gate 	TIMEOUT,			/* Seconds */
69*0Sstevel@tonic-gate 	0				/* Microseconds */
70*0Sstevel@tonic-gate 	};
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static char err_usage_set[] =
73*0Sstevel@tonic-gate "Usage:\n\
74*0Sstevel@tonic-gate 	ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
75*0Sstevel@tonic-gate static char err_bad_args[] =
76*0Sstevel@tonic-gate 	"Sorry, the %s argument is bad.\n";
77*0Sstevel@tonic-gate static char err_cant_get_kname[] =
78*0Sstevel@tonic-gate 	"Sorry, can't get %s back from system call.\n";
79*0Sstevel@tonic-gate static char err_null_kname[] =
80*0Sstevel@tonic-gate 	"Sorry, the %s hasn't been set on this machine.\n";
81*0Sstevel@tonic-gate static char err_bad_hostname[] = "hostname";
82*0Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
83*0Sstevel@tonic-gate static char err_bad_server[] = "server_to_use";
84*0Sstevel@tonic-gate static char err_tp_failure[] =
85*0Sstevel@tonic-gate 	"Sorry, I can't set up a connection to host %s.\n";
86*0Sstevel@tonic-gate static char err_rpc_failure[] =
87*0Sstevel@tonic-gate 	"Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
88*0Sstevel@tonic-gate static char err_access_failure[] =
89*0Sstevel@tonic-gate 	"ypset: Sorry, ypbind on host %s has rejected your request.\n";
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static void get_command_line_args();
92*0Sstevel@tonic-gate static void send_message();
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate extern void exit();
95*0Sstevel@tonic-gate extern int getdomainname();
96*0Sstevel@tonic-gate extern int gethostname();
97*0Sstevel@tonic-gate extern struct netconfig *getnetconfigent();
98*0Sstevel@tonic-gate extern unsigned int strlen();
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * This is the mainline for the ypset process.  It pulls whatever arguments
102*0Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
103*0Sstevel@tonic-gate  */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate void
106*0Sstevel@tonic-gate main(argc, argv)
107*0Sstevel@tonic-gate 	int argc;
108*0Sstevel@tonic-gate 	char **argv;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	get_command_line_args(argc, argv);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (!domain) {
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
116*0Sstevel@tonic-gate 			domain = default_domain_name;
117*0Sstevel@tonic-gate 		} else {
118*0Sstevel@tonic-gate 			(void) fprintf(stderr,
119*0Sstevel@tonic-gate 	err_cant_get_kname,
120*0Sstevel@tonic-gate 	err_bad_domainname);
121*0Sstevel@tonic-gate 			exit(1);
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		if ((int) strlen(domain) == 0) {
125*0Sstevel@tonic-gate 			(void) fprintf(stderr,
126*0Sstevel@tonic-gate 	err_null_kname,
127*0Sstevel@tonic-gate 	err_bad_domainname);
128*0Sstevel@tonic-gate 			exit(1);
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	send_message();
132*0Sstevel@tonic-gate 	exit(0);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate  * This does the command line argument processing.
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate static void
139*0Sstevel@tonic-gate get_command_line_args(argc, argv)
140*0Sstevel@tonic-gate 	int argc;
141*0Sstevel@tonic-gate 	char **argv;
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	pusage = err_usage_set;
144*0Sstevel@tonic-gate 	argv++;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	while (--argc > 1) {
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 			switch ((*argv)[1]) {
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 			case 'h': {
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 				if (argc > 1) {
155*0Sstevel@tonic-gate 					struct utsname utsname;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 					argv++;
158*0Sstevel@tonic-gate 					argc--;
159*0Sstevel@tonic-gate 					(void) uname(&utsname);
160*0Sstevel@tonic-gate 					if (strcasecmp(utsname.nodename,
161*0Sstevel@tonic-gate 						*argv) != 0) {
162*0Sstevel@tonic-gate 						host = *argv;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 						if ((int) strlen(host) > 256) {
165*0Sstevel@tonic-gate 							(void) fprintf(stderr,
166*0Sstevel@tonic-gate 	err_bad_args,
167*0Sstevel@tonic-gate 	err_bad_hostname);
168*0Sstevel@tonic-gate 							exit(1);
169*0Sstevel@tonic-gate 						}
170*0Sstevel@tonic-gate 					}
171*0Sstevel@tonic-gate 					argv++;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 				} else {
174*0Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
175*0Sstevel@tonic-gate 					exit(1);
176*0Sstevel@tonic-gate 				}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 				break;
179*0Sstevel@tonic-gate 			}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 			case 'd': {
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 				if (argc > 1) {
184*0Sstevel@tonic-gate 					argv++;
185*0Sstevel@tonic-gate 					argc--;
186*0Sstevel@tonic-gate 					domain = *argv;
187*0Sstevel@tonic-gate 					argv++;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 					if (strlen(domain) > YPMAXDOMAIN) {
190*0Sstevel@tonic-gate 						(void) fprintf(stderr,
191*0Sstevel@tonic-gate 	err_bad_args,
192*0Sstevel@tonic-gate 	err_bad_domainname);
193*0Sstevel@tonic-gate 						exit(1);
194*0Sstevel@tonic-gate 					}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 				} else {
197*0Sstevel@tonic-gate 					(void) fprintf(stderr, pusage);
198*0Sstevel@tonic-gate 					exit(1);
199*0Sstevel@tonic-gate 				}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 				break;
202*0Sstevel@tonic-gate 			}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 			default: {
205*0Sstevel@tonic-gate 				(void) fprintf(stderr, pusage);
206*0Sstevel@tonic-gate 				exit(1);
207*0Sstevel@tonic-gate 			}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 			}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		} else {
212*0Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
213*0Sstevel@tonic-gate 			exit(1);
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (argc == 1) {
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
220*0Sstevel@tonic-gate 			(void) fprintf(stderr, pusage);
221*0Sstevel@tonic-gate 			exit(1);
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		server_to_use = *argv;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		if ((int) strlen(server_to_use) > 256) {
227*0Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args,
228*0Sstevel@tonic-gate 			    err_bad_server);
229*0Sstevel@tonic-gate 			exit(1);
230*0Sstevel@tonic-gate 		}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	} else {
233*0Sstevel@tonic-gate 		(void) fprintf(stderr, pusage);
234*0Sstevel@tonic-gate 		exit(1);
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * This takes the name of the YP host of interest, and fires off
240*0Sstevel@tonic-gate  * the "set domain binding" message to the ypbind process.
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate static void
244*0Sstevel@tonic-gate send_message()
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	CLIENT *server, *client;
247*0Sstevel@tonic-gate 	struct ypbind_setdom req;
248*0Sstevel@tonic-gate 	struct ypbind_binding ypbind_info;
249*0Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
250*0Sstevel@tonic-gate 	struct netconfig *nconf;
251*0Sstevel@tonic-gate 	struct netbuf nbuf;
252*0Sstevel@tonic-gate 	int err;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/*
255*0Sstevel@tonic-gate 	 * Open up a path to the server
256*0Sstevel@tonic-gate 	 */
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
259*0Sstevel@tonic-gate 	    "datagram_n")) == NULL) {
260*0Sstevel@tonic-gate 		(void) fprintf(stderr, err_tp_failure, server_to_use);
261*0Sstevel@tonic-gate 		exit(1);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/* get nconf, netbuf structures */
265*0Sstevel@tonic-gate 	nconf = getnetconfigent(server->cl_netid);
266*0Sstevel@tonic-gate 	clnt_control(server, CLGET_SVC_ADDR, (char *) &nbuf);
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/*
269*0Sstevel@tonic-gate 	 * Open a path to host
270*0Sstevel@tonic-gate 	 */
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	if (!host) {
273*0Sstevel@tonic-gate 		client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
274*0Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
275*0Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
276*0Sstevel@tonic-gate 			exit(1);
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 		client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
279*0Sstevel@tonic-gate 		if (client->cl_auth == NULL) {
280*0Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
281*0Sstevel@tonic-gate 			exit(1);
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 	} else {
284*0Sstevel@tonic-gate 		client = clnt_create(host, YPBINDPROG,
285*0Sstevel@tonic-gate 			YPBINDVERS, "datagram_n");
286*0Sstevel@tonic-gate 		if (client == (CLIENT *)NULL) {
287*0Sstevel@tonic-gate 			clnt_pcreateerror("ypset: clnt_create");
288*0Sstevel@tonic-gate 			exit(1);
289*0Sstevel@tonic-gate 		}
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/*
293*0Sstevel@tonic-gate 	 * Load up the message structure and fire it off.
294*0Sstevel@tonic-gate 	 */
295*0Sstevel@tonic-gate 	ypbind_info.ypbind_nconf = nconf;
296*0Sstevel@tonic-gate 	ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
297*0Sstevel@tonic-gate 	ypbind_info.ypbind_servername = server_to_use;
298*0Sstevel@tonic-gate 	ypbind_info.ypbind_hi_vers = YPVERS;
299*0Sstevel@tonic-gate 	ypbind_info.ypbind_lo_vers = YPVERS;
300*0Sstevel@tonic-gate 	req.ypsetdom_bindinfo = &ypbind_info;
301*0Sstevel@tonic-gate 	req.ypsetdom_domain =  domain;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	clnt_stat = (enum clnt_stat) clnt_call(client,
304*0Sstevel@tonic-gate 	    YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
305*0Sstevel@tonic-gate 	    timeout);
306*0Sstevel@tonic-gate 	if (clnt_stat != RPC_SUCCESS) {
307*0Sstevel@tonic-gate 		if (clnt_stat == RPC_PROGUNAVAIL)
308*0Sstevel@tonic-gate 			(void) fprintf(stderr,
309*0Sstevel@tonic-gate 	err_access_failure, host ? host : "localhost");
310*0Sstevel@tonic-gate 		else
311*0Sstevel@tonic-gate 			(void) fprintf(stderr,
312*0Sstevel@tonic-gate 	err_rpc_failure, host ? host : "localhost");
313*0Sstevel@tonic-gate 		exit(1);
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 	if (!host)
316*0Sstevel@tonic-gate 		auth_destroy((client)->cl_auth);
317*0Sstevel@tonic-gate 	(void) clnt_destroy(server);
318*0Sstevel@tonic-gate 	(void) clnt_destroy(client);
319*0Sstevel@tonic-gate }
320