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 #pragma ident "%Z%%M% %I% %E% SMI" 36 37 /* 38 * This is a user command which issues a "Set domain binding" command to a 39 * YP binder (ypbind) process 40 * 41 * ypset [-h <host>] [-d <domainname>] server_to_use 42 * 43 * where host and server_to_use may be either names or internet addresses. 44 */ 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(); 53 54 #ifdef NULL 55 #undef NULL 56 #endif 57 #define NULL 0 58 59 #define TIMEOUT 30 /* Total seconds for timeout */ 60 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 */ 70 }; 71 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"; 90 91 static void get_command_line_args(); 92 static void send_message(); 93 94 extern void exit(); 95 extern int getdomainname(); 96 extern int gethostname(); 97 extern struct netconfig *getnetconfigent(); 98 extern unsigned int strlen(); 99 100 /* 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. 103 */ 104 105 int 106 main(argc, argv) 107 int argc; 108 char **argv; 109 110 { 111 get_command_line_args(argc, argv); 112 113 if (!domain) { 114 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); 122 } 123 124 if ((int)strlen(domain) == 0) { 125 (void) fprintf(stderr, 126 err_null_kname, 127 err_bad_domainname); 128 exit(1); 129 } 130 } 131 send_message(); 132 return (0); 133 } 134 135 /* 136 * This does the command line argument processing. 137 */ 138 static void 139 get_command_line_args(argc, argv) 140 int argc; 141 char **argv; 142 { 143 pusage = err_usage_set; 144 argv++; 145 146 while (--argc > 1) { 147 148 if ((*argv)[0] == '-') { 149 150 switch ((*argv)[1]) { 151 152 case 'h': { 153 154 if (argc > 1) { 155 struct utsname utsname; 156 157 argv++; 158 argc--; 159 (void) uname(&utsname); 160 if (strcasecmp(utsname.nodename, 161 *argv) != 0) { 162 host = *argv; 163 164 if ((int)strlen(host) > 256) { 165 (void) fprintf(stderr, 166 err_bad_args, 167 err_bad_hostname); 168 exit(1); 169 } 170 } 171 argv++; 172 173 } else { 174 (void) fprintf(stderr, pusage); 175 exit(1); 176 } 177 178 break; 179 } 180 181 case 'd': { 182 183 if (argc > 1) { 184 argv++; 185 argc--; 186 domain = *argv; 187 argv++; 188 189 if (strlen(domain) > YPMAXDOMAIN) { 190 (void) fprintf(stderr, 191 err_bad_args, 192 err_bad_domainname); 193 exit(1); 194 } 195 196 } else { 197 (void) fprintf(stderr, pusage); 198 exit(1); 199 } 200 201 break; 202 } 203 204 default: { 205 (void) fprintf(stderr, pusage); 206 exit(1); 207 } 208 209 } 210 211 } else { 212 (void) fprintf(stderr, pusage); 213 exit(1); 214 } 215 } 216 217 if (argc == 1) { 218 219 if ((*argv)[0] == '-') { 220 (void) fprintf(stderr, pusage); 221 exit(1); 222 } 223 224 server_to_use = *argv; 225 226 if ((int)strlen(server_to_use) > 256) { 227 (void) fprintf(stderr, err_bad_args, 228 err_bad_server); 229 exit(1); 230 } 231 232 } else { 233 (void) fprintf(stderr, pusage); 234 exit(1); 235 } 236 } 237 238 /* 239 * This takes the name of the YP host of interest, and fires off 240 * the "set domain binding" message to the ypbind process. 241 */ 242 243 static void 244 send_message() 245 { 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; 253 254 /* 255 * Open up a path to the server 256 */ 257 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); 262 } 263 264 /* get nconf, netbuf structures */ 265 nconf = getnetconfigent(server->cl_netid); 266 clnt_control(server, CLGET_SVC_ADDR, (char *)&nbuf); 267 268 /* 269 * Open a path to host 270 */ 271 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); 277 } 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); 282 } 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); 289 } 290 } 291 292 /* 293 * Load up the message structure and fire it off. 294 */ 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; 302 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); 314 } 315 if (!host) 316 auth_destroy((client)->cl_auth); 317 (void) clnt_destroy(server); 318 (void) clnt_destroy(client); 319 } 320