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
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.
46 #include <rpcsvc/ypclnt.h>
47 #include <rpcsvc/yp_prot.h>
49 #include <sys/utsname.h>
50 extern CLIENT
*__clnt_create_loopback();
52 #define TIMEOUT 30 /* Total seconds for timeout */
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 */
65 static char err_usage_set
[] =
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();
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.
104 get_command_line_args(argc
, argv
);
108 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
109 domain
= default_domain_name
;
111 (void) fprintf(stderr
,
117 if ((int)strlen(domain
) == 0) {
118 (void) fprintf(stderr
,
129 * This does the command line argument processing.
132 get_command_line_args(argc
, argv
)
136 pusage
= err_usage_set
;
141 if ((*argv
)[0] == '-') {
143 switch ((*argv
)[1]) {
148 struct utsname utsname
;
152 (void) uname(&utsname
);
153 if (strcasecmp(utsname
.nodename
,
157 if ((int)strlen(host
) > 256) {
158 (void) fprintf(stderr
,
167 (void) fprintf(stderr
, pusage
);
182 if (strlen(domain
) > YPMAXDOMAIN
) {
183 (void) fprintf(stderr
,
190 (void) fprintf(stderr
, pusage
);
198 (void) fprintf(stderr
, pusage
);
205 (void) fprintf(stderr
, pusage
);
212 if ((*argv
)[0] == '-') {
213 (void) fprintf(stderr
, pusage
);
217 server_to_use
= *argv
;
219 if ((int)strlen(server_to_use
) > 256) {
220 (void) fprintf(stderr
, err_bad_args
,
226 (void) fprintf(stderr
, pusage
);
232 * This takes the name of the YP host of interest, and fires off
233 * the "set domain binding" message to the ypbind process.
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
;
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
);
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
266 client
= __clnt_create_loopback(YPBINDPROG
, YPBINDVERS
, &err
);
267 if (client
== (CLIENT
*)NULL
) {
268 clnt_pcreateerror("ypset: clnt_create");
271 client
->cl_auth
= authsys_create("", geteuid(), 0, 0, NULL
);
272 if (client
->cl_auth
== NULL
) {
273 clnt_pcreateerror("ypset: clnt_create");
277 client
= clnt_create(host
, YPBINDPROG
,
278 YPBINDVERS
, "datagram_n");
279 if (client
== (CLIENT
*)NULL
) {
280 clnt_pcreateerror("ypset: clnt_create");
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,
299 if (clnt_stat
!= RPC_SUCCESS
) {
300 if (clnt_stat
== RPC_PROGUNAVAIL
)
301 (void) fprintf(stderr
,
302 err_access_failure
, host
? host
: "localhost");
304 (void) fprintf(stderr
,
305 err_rpc_failure
, host
? host
: "localhost");
309 auth_destroy((client
)->cl_auth
);
310 (void) clnt_destroy(server
);
311 (void) clnt_destroy(client
);