cmd-inet/usr.sbin: remove -Wno-implicit-function-declaration
[unleashed.git] / usr / src / cmd / ypcmd / ypset.c
blob73205b3f487357a7dd2977d8a379bf33de036944
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.
36 * This is a user command which issues a "Set domain binding" command to a
37 * YP binder (ypbind) process
39 * ypset [-h <host>] [-d <domainname>] server_to_use
41 * where host and server_to_use may be either names or internet addresses.
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();
52 #define TIMEOUT 30 /* Total seconds for timeout */
54 static char *pusage;
55 static char *domain = NULL;
56 static char default_domain_name[YPMAXDOMAIN];
57 static char default_host_name[256];
58 static char *host = NULL;
59 static char *server_to_use;
60 static struct timeval timeout = {
61 TIMEOUT, /* Seconds */
62 0 /* Microseconds */
65 static char err_usage_set[] =
66 "Usage:\n\
67 ypset [ -h host ] [ -d domainname ] server_to_use\n\n";
68 static char err_bad_args[] =
69 "Sorry, the %s argument is bad.\n";
70 static char err_cant_get_kname[] =
71 "Sorry, can't get %s back from system call.\n";
72 static char err_null_kname[] =
73 "Sorry, the %s hasn't been set on this machine.\n";
74 static char err_bad_hostname[] = "hostname";
75 static char err_bad_domainname[] = "domainname";
76 static char err_bad_server[] = "server_to_use";
77 static char err_tp_failure[] =
78 "Sorry, I can't set up a connection to host %s.\n";
79 static char err_rpc_failure[] =
80 "Sorry, I couldn't send my rpc message to ypbind on host %s.\n";
81 static char err_access_failure[] =
82 "ypset: Sorry, ypbind on host %s has rejected your request.\n";
84 static void get_command_line_args();
85 static void send_message();
87 extern void exit();
88 extern int getdomainname();
89 extern int gethostname();
90 extern struct netconfig *getnetconfigent();
91 extern unsigned int strlen();
94 * This is the mainline for the ypset process. It pulls whatever arguments
95 * have been passed from the command line, and uses defaults for the rest.
98 int
99 main(argc, argv)
100 int argc;
101 char **argv;
104 get_command_line_args(argc, argv);
106 if (!domain) {
108 if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
109 domain = default_domain_name;
110 } else {
111 (void) fprintf(stderr,
112 err_cant_get_kname,
113 err_bad_domainname);
114 exit(1);
117 if ((int)strlen(domain) == 0) {
118 (void) fprintf(stderr,
119 err_null_kname,
120 err_bad_domainname);
121 exit(1);
124 send_message();
125 return (0);
129 * This does the command line argument processing.
131 static void
132 get_command_line_args(argc, argv)
133 int argc;
134 char **argv;
136 pusage = err_usage_set;
137 argv++;
139 while (--argc > 1) {
141 if ((*argv)[0] == '-') {
143 switch ((*argv)[1]) {
145 case 'h': {
147 if (argc > 1) {
148 struct utsname utsname;
150 argv++;
151 argc--;
152 (void) uname(&utsname);
153 if (strcasecmp(utsname.nodename,
154 *argv) != 0) {
155 host = *argv;
157 if ((int)strlen(host) > 256) {
158 (void) fprintf(stderr,
159 err_bad_args,
160 err_bad_hostname);
161 exit(1);
164 argv++;
166 } else {
167 (void) fprintf(stderr, pusage);
168 exit(1);
171 break;
174 case 'd': {
176 if (argc > 1) {
177 argv++;
178 argc--;
179 domain = *argv;
180 argv++;
182 if (strlen(domain) > YPMAXDOMAIN) {
183 (void) fprintf(stderr,
184 err_bad_args,
185 err_bad_domainname);
186 exit(1);
189 } else {
190 (void) fprintf(stderr, pusage);
191 exit(1);
194 break;
197 default: {
198 (void) fprintf(stderr, pusage);
199 exit(1);
204 } else {
205 (void) fprintf(stderr, pusage);
206 exit(1);
210 if (argc == 1) {
212 if ((*argv)[0] == '-') {
213 (void) fprintf(stderr, pusage);
214 exit(1);
217 server_to_use = *argv;
219 if ((int)strlen(server_to_use) > 256) {
220 (void) fprintf(stderr, err_bad_args,
221 err_bad_server);
222 exit(1);
225 } else {
226 (void) fprintf(stderr, pusage);
227 exit(1);
232 * This takes the name of the YP host of interest, and fires off
233 * the "set domain binding" message to the ypbind process.
236 static void
237 send_message()
239 CLIENT *server, *client;
240 struct ypbind_setdom req;
241 struct ypbind_binding ypbind_info;
242 enum clnt_stat clnt_stat;
243 struct netconfig *nconf;
244 struct netbuf nbuf;
245 int err;
248 * Open up a path to the server
251 if ((server = clnt_create(server_to_use, YPPROG, YPVERS,
252 "datagram_n")) == NULL) {
253 (void) fprintf(stderr, err_tp_failure, server_to_use);
254 exit(1);
257 /* get nconf, netbuf structures */
258 nconf = getnetconfigent(server->cl_netid);
259 clnt_control(server, CLGET_SVC_ADDR, (char *)&nbuf);
262 * Open a path to host
265 if (!host) {
266 client = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
267 if (client == (CLIENT *)NULL) {
268 clnt_pcreateerror("ypset: clnt_create");
269 exit(1);
271 client->cl_auth = authsys_create("", geteuid(), 0, 0, NULL);
272 if (client->cl_auth == NULL) {
273 clnt_pcreateerror("ypset: clnt_create");
274 exit(1);
276 } else {
277 client = clnt_create(host, YPBINDPROG,
278 YPBINDVERS, "datagram_n");
279 if (client == (CLIENT *)NULL) {
280 clnt_pcreateerror("ypset: clnt_create");
281 exit(1);
286 * Load up the message structure and fire it off.
288 ypbind_info.ypbind_nconf = nconf;
289 ypbind_info.ypbind_svcaddr = (struct netbuf *)(&nbuf);
290 ypbind_info.ypbind_servername = server_to_use;
291 ypbind_info.ypbind_hi_vers = YPVERS;
292 ypbind_info.ypbind_lo_vers = YPVERS;
293 req.ypsetdom_bindinfo = &ypbind_info;
294 req.ypsetdom_domain = domain;
296 clnt_stat = (enum clnt_stat) clnt_call(client,
297 YPBINDPROC_SETDOM, xdr_ypbind_setdom, (char *)&req, xdr_void, 0,
298 timeout);
299 if (clnt_stat != RPC_SUCCESS) {
300 if (clnt_stat == RPC_PROGUNAVAIL)
301 (void) fprintf(stderr,
302 err_access_failure, host ? host : "localhost");
303 else
304 (void) fprintf(stderr,
305 err_rpc_failure, host ? host : "localhost");
306 exit(1);
308 if (!host)
309 auth_destroy((client)->cl_auth);
310 (void) clnt_destroy(server);
311 (void) clnt_destroy(client);