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