1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 */ 29 /* 30 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 31 */ 32 33 #ident "@(#)key_call.c 1.25 94/04/24 SMI" 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 /* 39 * key_call.c, Interface to keyserver 40 * 41 * setsecretkey(key) - set your secret key 42 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent 43 * decryptsessionkey(agent, deskey) - decrypt ditto 44 * gendeskey(deskey) - generate a secure des key 45 */ 46 47 #include "namespace.h" 48 #include "reentrant.h" 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <unistd.h> 52 #include <errno.h> 53 #include <rpc/rpc.h> 54 #include <rpc/auth.h> 55 #include <rpc/auth_unix.h> 56 #include <rpc/key_prot.h> 57 #include <string.h> 58 #include <netconfig.h> 59 #include <sys/utsname.h> 60 #include <stdlib.h> 61 #include <signal.h> 62 #include <sys/wait.h> 63 #include <sys/fcntl.h> 64 #include "un-namespace.h" 65 66 67 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */ 68 #define KEY_NRETRY 12 /* number of retries */ 69 70 #ifdef DEBUG 71 #define debug(msg) (void) fprintf(stderr, "%s\n", msg); 72 #else 73 #define debug(msg) 74 #endif /* DEBUG */ 75 76 /* 77 * Hack to allow the keyserver to use AUTH_DES (for authenticated 78 * NIS+ calls, for example). The only functions that get called 79 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes. 80 * 81 * The approach is to have the keyserver fill in pointers to local 82 * implementations of these functions, and to call those in key_call(). 83 */ 84 85 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0; 86 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0; 87 des_block *(*__key_gendes_LOCAL)() = 0; 88 89 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *); 90 91 int 92 key_setsecret(secretkey) 93 const char *secretkey; 94 { 95 keystatus status; 96 97 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf, 98 (void *)secretkey, 99 (xdrproc_t)xdr_keystatus, &status)) { 100 return (-1); 101 } 102 if (status != KEY_SUCCESS) { 103 debug("set status is nonzero"); 104 return (-1); 105 } 106 return (0); 107 } 108 109 110 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key 111 * stored for the caller's effective uid; it returns 0 otherwise 112 * 113 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't 114 * be using it, because it allows them to get the user's secret key. 115 */ 116 117 int 118 key_secretkey_is_set(void) 119 { 120 struct key_netstres kres; 121 122 memset((void*)&kres, 0, sizeof (kres)); 123 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL, 124 (xdrproc_t)xdr_key_netstres, &kres) && 125 (kres.status == KEY_SUCCESS) && 126 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) { 127 /* avoid leaving secret key in memory */ 128 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES); 129 return (1); 130 } 131 return (0); 132 } 133 134 int 135 key_encryptsession_pk(remotename, remotekey, deskey) 136 char *remotename; 137 netobj *remotekey; 138 des_block *deskey; 139 { 140 cryptkeyarg2 arg; 141 cryptkeyres res; 142 143 arg.remotename = remotename; 144 arg.remotekey = *remotekey; 145 arg.deskey = *deskey; 146 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 147 (xdrproc_t)xdr_cryptkeyres, &res)) { 148 return (-1); 149 } 150 if (res.status != KEY_SUCCESS) { 151 debug("encrypt status is nonzero"); 152 return (-1); 153 } 154 *deskey = res.cryptkeyres_u.deskey; 155 return (0); 156 } 157 158 int 159 key_decryptsession_pk(remotename, remotekey, deskey) 160 char *remotename; 161 netobj *remotekey; 162 des_block *deskey; 163 { 164 cryptkeyarg2 arg; 165 cryptkeyres res; 166 167 arg.remotename = remotename; 168 arg.remotekey = *remotekey; 169 arg.deskey = *deskey; 170 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 171 (xdrproc_t)xdr_cryptkeyres, &res)) { 172 return (-1); 173 } 174 if (res.status != KEY_SUCCESS) { 175 debug("decrypt status is nonzero"); 176 return (-1); 177 } 178 *deskey = res.cryptkeyres_u.deskey; 179 return (0); 180 } 181 182 int 183 key_encryptsession(remotename, deskey) 184 const char *remotename; 185 des_block *deskey; 186 { 187 cryptkeyarg arg; 188 cryptkeyres res; 189 190 arg.remotename = (char *) remotename; 191 arg.deskey = *deskey; 192 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 193 (xdrproc_t)xdr_cryptkeyres, &res)) { 194 return (-1); 195 } 196 if (res.status != KEY_SUCCESS) { 197 debug("encrypt status is nonzero"); 198 return (-1); 199 } 200 *deskey = res.cryptkeyres_u.deskey; 201 return (0); 202 } 203 204 int 205 key_decryptsession(remotename, deskey) 206 const char *remotename; 207 des_block *deskey; 208 { 209 cryptkeyarg arg; 210 cryptkeyres res; 211 212 arg.remotename = (char *) remotename; 213 arg.deskey = *deskey; 214 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 215 (xdrproc_t)xdr_cryptkeyres, &res)) { 216 return (-1); 217 } 218 if (res.status != KEY_SUCCESS) { 219 debug("decrypt status is nonzero"); 220 return (-1); 221 } 222 *deskey = res.cryptkeyres_u.deskey; 223 return (0); 224 } 225 226 int 227 key_gendes(key) 228 des_block *key; 229 { 230 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL, 231 (xdrproc_t)xdr_des_block, key)) { 232 return (-1); 233 } 234 return (0); 235 } 236 237 int 238 key_setnet(arg) 239 struct key_netstarg *arg; 240 { 241 keystatus status; 242 243 244 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg, 245 (xdrproc_t)xdr_keystatus, &status)){ 246 return (-1); 247 } 248 249 if (status != KEY_SUCCESS) { 250 debug("key_setnet status is nonzero"); 251 return (-1); 252 } 253 return (1); 254 } 255 256 257 int 258 key_get_conv(pkey, deskey) 259 char *pkey; 260 des_block *deskey; 261 { 262 cryptkeyres res; 263 264 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey, 265 (xdrproc_t)xdr_cryptkeyres, &res)) { 266 return (-1); 267 } 268 if (res.status != KEY_SUCCESS) { 269 debug("get_conv status is nonzero"); 270 return (-1); 271 } 272 *deskey = res.cryptkeyres_u.deskey; 273 return (0); 274 } 275 276 struct key_call_private { 277 CLIENT *client; /* Client handle */ 278 pid_t pid; /* process-id at moment of creation */ 279 uid_t uid; /* user-id at last authorization */ 280 }; 281 static struct key_call_private *key_call_private_main = NULL; 282 283 static void 284 key_call_destroy(void *vp) 285 { 286 struct key_call_private *kcp = (struct key_call_private *)vp; 287 288 if (kcp) { 289 if (kcp->client) 290 clnt_destroy(kcp->client); 291 free(kcp); 292 } 293 } 294 295 /* 296 * Keep the handle cached. This call may be made quite often. 297 */ 298 static CLIENT * 299 getkeyserv_handle(vers) 300 int vers; 301 { 302 void *localhandle; 303 struct netconfig *nconf; 304 struct netconfig *tpconf; 305 struct key_call_private *kcp = key_call_private_main; 306 struct timeval wait_time; 307 struct utsname u; 308 int main_thread; 309 int fd; 310 static thread_key_t key_call_key; 311 extern mutex_t tsd_lock; 312 313 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */ 314 #define TOTAL_TRIES 5 /* Number of tries */ 315 316 if ((main_thread = thr_main())) { 317 kcp = key_call_private_main; 318 } else { 319 if (key_call_key == 0) { 320 mutex_lock(&tsd_lock); 321 if (key_call_key == 0) 322 thr_keycreate(&key_call_key, key_call_destroy); 323 mutex_unlock(&tsd_lock); 324 } 325 kcp = (struct key_call_private *)thr_getspecific(key_call_key); 326 } 327 if (kcp == (struct key_call_private *)NULL) { 328 kcp = (struct key_call_private *)malloc(sizeof (*kcp)); 329 if (kcp == (struct key_call_private *)NULL) { 330 return ((CLIENT *) NULL); 331 } 332 if (main_thread) 333 key_call_private_main = kcp; 334 else 335 thr_setspecific(key_call_key, (void *) kcp); 336 kcp->client = NULL; 337 } 338 339 /* if pid has changed, destroy client and rebuild */ 340 if (kcp->client != NULL && kcp->pid != getpid()) { 341 clnt_destroy(kcp->client); 342 kcp->client = NULL; 343 } 344 345 if (kcp->client != NULL) { 346 /* if uid has changed, build client handle again */ 347 if (kcp->uid != geteuid()) { 348 kcp->uid = geteuid(); 349 auth_destroy(kcp->client->cl_auth); 350 kcp->client->cl_auth = 351 authsys_create("", kcp->uid, 0, 0, NULL); 352 if (kcp->client->cl_auth == NULL) { 353 clnt_destroy(kcp->client); 354 kcp->client = NULL; 355 return ((CLIENT *) NULL); 356 } 357 } 358 /* Change the version number to the new one */ 359 clnt_control(kcp->client, CLSET_VERS, (void *)&vers); 360 return (kcp->client); 361 } 362 if (!(localhandle = setnetconfig())) { 363 return ((CLIENT *) NULL); 364 } 365 tpconf = NULL; 366 #if defined(__FreeBSD__) 367 if (uname(&u) == -1) 368 #else 369 #if defined(i386) 370 if (_nuname(&u) == -1) 371 #elif defined(sparc) 372 if (_uname(&u) == -1) 373 #else 374 #error Unknown architecture! 375 #endif 376 #endif 377 { 378 endnetconfig(localhandle); 379 return ((CLIENT *) NULL); 380 } 381 while ((nconf = getnetconfig(localhandle)) != NULL) { 382 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { 383 /* 384 * We use COTS_ORD here so that the caller can 385 * find out immediately if the server is dead. 386 */ 387 if (nconf->nc_semantics == NC_TPI_COTS_ORD) { 388 kcp->client = clnt_tp_create(u.nodename, 389 KEY_PROG, vers, nconf); 390 if (kcp->client) 391 break; 392 } else { 393 tpconf = nconf; 394 } 395 } 396 } 397 if ((kcp->client == (CLIENT *) NULL) && (tpconf)) 398 /* Now, try the CLTS or COTS loopback transport */ 399 kcp->client = clnt_tp_create(u.nodename, 400 KEY_PROG, vers, tpconf); 401 endnetconfig(localhandle); 402 403 if (kcp->client == (CLIENT *) NULL) { 404 return ((CLIENT *) NULL); 405 } 406 kcp->uid = geteuid(); 407 kcp->pid = getpid(); 408 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL); 409 if (kcp->client->cl_auth == NULL) { 410 clnt_destroy(kcp->client); 411 kcp->client = NULL; 412 return ((CLIENT *) NULL); 413 } 414 415 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES; 416 wait_time.tv_usec = 0; 417 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT, 418 (char *)&wait_time); 419 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd)) 420 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 421 422 return (kcp->client); 423 } 424 425 /* returns 0 on failure, 1 on success */ 426 427 static int 428 key_call(proc, xdr_arg, arg, xdr_rslt, rslt) 429 u_long proc; 430 xdrproc_t xdr_arg; 431 void *arg; 432 xdrproc_t xdr_rslt; 433 void *rslt; 434 { 435 CLIENT *clnt; 436 struct timeval wait_time; 437 438 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) { 439 cryptkeyres *res; 440 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg); 441 *(cryptkeyres*)rslt = *res; 442 return (1); 443 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) { 444 cryptkeyres *res; 445 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg); 446 *(cryptkeyres*)rslt = *res; 447 return (1); 448 } else if (proc == KEY_GEN && __key_gendes_LOCAL) { 449 des_block *res; 450 res = (*__key_gendes_LOCAL)(geteuid(), 0); 451 *(des_block*)rslt = *res; 452 return (1); 453 } 454 455 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) || 456 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) || 457 (proc == KEY_GET_CONV)) 458 clnt = getkeyserv_handle(2); /* talk to version 2 */ 459 else 460 clnt = getkeyserv_handle(1); /* talk to version 1 */ 461 462 if (clnt == NULL) { 463 return (0); 464 } 465 466 wait_time.tv_sec = TOTAL_TIMEOUT; 467 wait_time.tv_usec = 0; 468 469 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt, 470 wait_time) == RPC_SUCCESS) { 471 return (1); 472 } else { 473 return (0); 474 } 475 } 476