8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / ypcmd / ypset.c
blobb44d9c422d06b241c0b56b1e0edb900018540719
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
32 * California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * This is a user command which issues a "Set domain binding" command to a
39 * YP binder (ypbind) process
41 * ypset [-h <host>] [-d <domainname>] server_to_use
43 * where host and server_to_use may be either names or internet addresses.
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <rpc/rpc.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yp_prot.h>
50 #include "yp_b.h"
51 #include <sys/utsname.h>
52 extern CLIENT *__clnt_create_loopback();
54 #ifdef NULL
55 #undef NULL
56 #endif
57 #define NULL 0
59 #define TIMEOUT 30 /* Total seconds for timeout */
61 static char *pusage;
62 static char *domain = NULL;
63 static char default_domain_name[YPMAXDOMAIN];
64 static char default_host_name[256];
65 static char *host = NULL;
66 static char *server_to_use;
67 static struct timeval timeout = {
68 TIMEOUT, /* Seconds */
69 0 /* Microseconds */
72 static char err_usage_set[] =
73 "Usage:\n\
74 ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
75 static char err_bad_args[] =
76 "Sorry, the %s argument is bad.\n";
77 static char err_cant_get_kname[] =
78 "Sorry, can't get %s back from system call.\n";
79 static char err_null_kname[] =
80 "Sorry, the %s hasn't been set on this machine.\n";
81 static char err_bad_hostname[] = "hostname";
82 static char err_bad_domainname[] = "domainname";
83 static char err_bad_server[] = "server_to_use";
84 static char err_tp_failure[] =
85 "Sorry, I can't set up a connection to host %s.\n";
86 static char err_rpc_failure[] =
87 "Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
88 static char err_access_failure[] =
89 "ypset: Sorry, ypbind on host %s has rejected your request.\n";
91 static void get_command_line_args();
92 static void send_message();
94 extern void exit();
95 extern int getdomainname();
96 extern int gethostname();
97 extern struct netconfig *getnetconfigent();
98 extern unsigned int strlen();
101 * This is the mainline for the ypset process. It pulls whatever arguments
102 * have been passed from the command line, and uses defaults for the rest.
106 main(argc, argv)
107 int argc;
108 char **argv;
111 get_command_line_args(argc, argv);
113 if (!domain) {
115 if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
116 domain = default_domain_name;
117 } else {
118 (void) fprintf(stderr,
119 err_cant_get_kname,
120 err_bad_domainname);
121 exit(1);
124 if ((int)strlen(domain) == 0) {
125 (void) fprintf(stderr,
126 err_null_kname,
127 err_bad_domainname);
128 exit(1);
131 send_message();
132 return (0);
136 * This does the command line argument processing.
138 static void
139 get_command_line_args(argc, argv)
140 int argc;
141 char **argv;
143 pusage = err_usage_set;
144 argv++;
146 while (--argc > 1) {
148 if ((*argv)[0] == '-') {
150 switch ((*argv)[1]) {
152 case 'h': {
154 if (argc > 1) {
155 struct utsname utsname;
157 argv++;
158 argc--;
159 (void) uname(&utsname);
160 if (strcasecmp(utsname.nodename,
161 *argv) != 0) {
162 host = *argv;
164 if ((int)strlen(host) > 256) {
165 (void) fprintf(stderr,
166 err_bad_args,
167 err_bad_hostname);
168 exit(1);
171 argv++;
173 } else {
174 (void) fprintf(stderr, pusage);
175 exit(1);
178 break;
181 case 'd': {
183 if (argc > 1) {
184 argv++;
185 argc--;
186 domain = *argv;
187 argv++;
189 if (strlen(domain) > YPMAXDOMAIN) {
190 (void) fprintf(stderr,
191 err_bad_args,
192 err_bad_domainname);
193 exit(1);
196 } else {
197 (void) fprintf(stderr, pusage);
198 exit(1);
201 break;
204 default: {
205 (void) fprintf(stderr, pusage);
206 exit(1);
211 } else {
212 (void) fprintf(stderr, pusage);
213 exit(1);
217 if (argc == 1) {
219 if ((*argv)[0] == '-') {
220 (void) fprintf(stderr, pusage);
221 exit(1);
224 server_to_use = *argv;
226 if ((int)strlen(server_to_use) > 256) {
227 (void) fprintf(stderr, err_bad_args,
228 err_bad_server);
229 exit(1);
232 } else {
233 (void) fprintf(stderr, pusage);
234 exit(1);
239 * This takes the name of the YP host of interest, and fires off
240 * the "set domain binding" message to the ypbind process.
243 static void
244 send_message()
246 CLIENT *server, *client;
247 struct ypbind_setdom req;
248 struct ypbind_binding ypbind_info;
249 enum clnt_stat clnt_stat;
250 struct netconfig *nconf;
251 struct netbuf nbuf;
252 int err;
255 * Open up a path to the server
258 if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
259 "datagram_n")) == NULL) {
260 (void) fprintf(stderr, err_tp_failure, server_to_use);
261 exit(1);
264 /* get nconf, netbuf structures */
265 nconf = getnetconfigent(server->cl_netid);
266 clnt_control(server, CLGET_SVC_ADDR, (char *)&nbuf);
269 * Open a path to host
272 if (!host) {
273 client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
274 if (client == (CLIENT *)NULL) {
275 clnt_pcreateerror("ypset: clnt_create");
276 exit(1);
278 client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
279 if (client->cl_auth == NULL) {
280 clnt_pcreateerror("ypset: clnt_create");
281 exit(1);
283 } else {
284 client = clnt_create(host, YPBINDPROG,
285 YPBINDVERS, "datagram_n");
286 if (client == (CLIENT *)NULL) {
287 clnt_pcreateerror("ypset: clnt_create");
288 exit(1);
293 * Load up the message structure and fire it off.
295 ypbind_info.ypbind_nconf = nconf;
296 ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
297 ypbind_info.ypbind_servername = server_to_use;
298 ypbind_info.ypbind_hi_vers = YPVERS;
299 ypbind_info.ypbind_lo_vers = YPVERS;
300 req.ypsetdom_bindinfo = &ypbind_info;
301 req.ypsetdom_domain = domain;
303 clnt_stat = (enum clnt_stat) clnt_call(client,
304 YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
305 timeout);
306 if (clnt_stat != RPC_SUCCESS) {
307 if (clnt_stat == RPC_PROGUNAVAIL)
308 (void) fprintf(stderr,
309 err_access_failure, host ? host : "localhost");
310 else
311 (void) fprintf(stderr,
312 err_rpc_failure, host ? host : "localhost");
313 exit(1);
315 if (!host)
316 auth_destroy((client)->cl_auth);
317 (void) clnt_destroy(server);
318 (void) clnt_destroy(client);