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
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]
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
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.
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yp_prot.h>
51 #include <sys/utsname.h>
52 extern CLIENT
*__clnt_create_loopback();
59 #define TIMEOUT 30 /* Total seconds for timeout */
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 */
72 static char err_usage_set
[] =
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();
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.
111 get_command_line_args(argc
, argv
);
115 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
116 domain
= default_domain_name
;
118 (void) fprintf(stderr
,
124 if ((int)strlen(domain
) == 0) {
125 (void) fprintf(stderr
,
136 * This does the command line argument processing.
139 get_command_line_args(argc
, argv
)
143 pusage
= err_usage_set
;
148 if ((*argv
)[0] == '-') {
150 switch ((*argv
)[1]) {
155 struct utsname utsname
;
159 (void) uname(&utsname
);
160 if (strcasecmp(utsname
.nodename
,
164 if ((int)strlen(host
) > 256) {
165 (void) fprintf(stderr
,
174 (void) fprintf(stderr
, pusage
);
189 if (strlen(domain
) > YPMAXDOMAIN
) {
190 (void) fprintf(stderr
,
197 (void) fprintf(stderr
, pusage
);
205 (void) fprintf(stderr
, pusage
);
212 (void) fprintf(stderr
, pusage
);
219 if ((*argv
)[0] == '-') {
220 (void) fprintf(stderr
, pusage
);
224 server_to_use
= *argv
;
226 if ((int)strlen(server_to_use
) > 256) {
227 (void) fprintf(stderr
, err_bad_args
,
233 (void) fprintf(stderr
, pusage
);
239 * This takes the name of the YP host of interest, and fires off
240 * the "set domain binding" message to the ypbind process.
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
;
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
);
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
273 client
= __clnt_create_loopback(YPBINDPROG
, YPBINDVERS
, &err
);
274 if (client
== (CLIENT
*)NULL
) {
275 clnt_pcreateerror("ypset: clnt_create");
278 client
->cl_auth
= authsys_create("", geteuid(), 0, 0, NULL
);
279 if (client
->cl_auth
== NULL
) {
280 clnt_pcreateerror("ypset: clnt_create");
284 client
= clnt_create(host
, YPBINDPROG
,
285 YPBINDVERS
, "datagram_n");
286 if (client
== (CLIENT
*)NULL
) {
287 clnt_pcreateerror("ypset: clnt_create");
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,
306 if (clnt_stat
!= RPC_SUCCESS
) {
307 if (clnt_stat
== RPC_PROGUNAVAIL
)
308 (void) fprintf(stderr
,
309 err_access_failure
, host
? host
: "localhost");
311 (void) fprintf(stderr
,
312 err_rpc_failure
, host
? host
: "localhost");
316 auth_destroy((client
)->cl_auth
);
317 (void) clnt_destroy(server
);
318 (void) clnt_destroy(client
);