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 * @(#)key_call.c 1.25 94/04/24 SMI 33 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.16 2006/02/27 22:10:59 deischen Exp $ 34 * $DragonFly: src/lib/libc/rpc/key_call.c,v 1.6 2005/11/13 12:27:04 swildner Exp $ 35 */ 36 37 /* 38 * key_call.c, Interface to keyserver 39 * 40 * setsecretkey(key) - set your secret key 41 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent 42 * decryptsessionkey(agent, deskey) - decrypt ditto 43 * gendeskey(deskey) - generate a secure des key 44 */ 45 46 #include "namespace.h" 47 #include "reentrant.h" 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <errno.h> 52 #include <rpc/rpc.h> 53 #include <rpc/auth.h> 54 #include <rpc/auth_unix.h> 55 #include <rpc/key_prot.h> 56 #include <string.h> 57 #include <netconfig.h> 58 #include <sys/utsname.h> 59 #include <stdlib.h> 60 #include <signal.h> 61 #include <sys/wait.h> 62 #include <sys/fcntl.h> 63 #include "un-namespace.h" 64 #include "mt_misc.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) 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(const char *secretkey) 93 { 94 keystatus status; 95 96 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf, 97 (void *)secretkey, 98 (xdrproc_t)xdr_keystatus, &status)) { 99 return (-1); 100 } 101 if (status != KEY_SUCCESS) { 102 debug("set status is nonzero"); 103 return (-1); 104 } 105 return (0); 106 } 107 108 109 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key 110 * stored for the caller's effective uid; it returns 0 otherwise 111 * 112 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't 113 * be using it, because it allows them to get the user's secret key. 114 */ 115 116 int 117 key_secretkey_is_set(void) 118 { 119 struct key_netstres kres; 120 121 memset((void*)&kres, 0, sizeof (kres)); 122 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL, 123 (xdrproc_t)xdr_key_netstres, &kres) && 124 (kres.status == KEY_SUCCESS) && 125 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) { 126 /* avoid leaving secret key in memory */ 127 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES); 128 return (1); 129 } 130 return (0); 131 } 132 133 int 134 key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey) 135 { 136 cryptkeyarg2 arg; 137 cryptkeyres res; 138 139 arg.remotename = remotename; 140 arg.remotekey = *remotekey; 141 arg.deskey = *deskey; 142 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 143 (xdrproc_t)xdr_cryptkeyres, &res)) { 144 return (-1); 145 } 146 if (res.status != KEY_SUCCESS) { 147 debug("encrypt status is nonzero"); 148 return (-1); 149 } 150 *deskey = res.cryptkeyres_u.deskey; 151 return (0); 152 } 153 154 int 155 key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey) 156 { 157 cryptkeyarg2 arg; 158 cryptkeyres res; 159 160 arg.remotename = remotename; 161 arg.remotekey = *remotekey; 162 arg.deskey = *deskey; 163 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg, 164 (xdrproc_t)xdr_cryptkeyres, &res)) { 165 return (-1); 166 } 167 if (res.status != KEY_SUCCESS) { 168 debug("decrypt status is nonzero"); 169 return (-1); 170 } 171 *deskey = res.cryptkeyres_u.deskey; 172 return (0); 173 } 174 175 int 176 key_encryptsession(const char *remotename, des_block *deskey) 177 { 178 cryptkeyarg arg; 179 cryptkeyres res; 180 181 arg.remotename = (char *) remotename; 182 arg.deskey = *deskey; 183 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 184 (xdrproc_t)xdr_cryptkeyres, &res)) { 185 return (-1); 186 } 187 if (res.status != KEY_SUCCESS) { 188 debug("encrypt status is nonzero"); 189 return (-1); 190 } 191 *deskey = res.cryptkeyres_u.deskey; 192 return (0); 193 } 194 195 int 196 key_decryptsession(const char *remotename, des_block *deskey) 197 { 198 cryptkeyarg arg; 199 cryptkeyres res; 200 201 arg.remotename = (char *) remotename; 202 arg.deskey = *deskey; 203 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg, 204 (xdrproc_t)xdr_cryptkeyres, &res)) { 205 return (-1); 206 } 207 if (res.status != KEY_SUCCESS) { 208 debug("decrypt status is nonzero"); 209 return (-1); 210 } 211 *deskey = res.cryptkeyres_u.deskey; 212 return (0); 213 } 214 215 int 216 key_gendes(des_block *key) 217 { 218 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL, 219 (xdrproc_t)xdr_des_block, key)) { 220 return (-1); 221 } 222 return (0); 223 } 224 225 int 226 key_setnet(struct key_netstarg *arg) 227 { 228 keystatus status; 229 230 231 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg, 232 (xdrproc_t)xdr_keystatus, &status)){ 233 return (-1); 234 } 235 236 if (status != KEY_SUCCESS) { 237 debug("key_setnet status is nonzero"); 238 return (-1); 239 } 240 return (1); 241 } 242 243 244 int 245 key_get_conv(char *pkey, des_block *deskey) 246 { 247 cryptkeyres res; 248 249 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey, 250 (xdrproc_t)xdr_cryptkeyres, &res)) { 251 return (-1); 252 } 253 if (res.status != KEY_SUCCESS) { 254 debug("get_conv status is nonzero"); 255 return (-1); 256 } 257 *deskey = res.cryptkeyres_u.deskey; 258 return (0); 259 } 260 261 struct key_call_private { 262 CLIENT *client; /* Client handle */ 263 pid_t pid; /* process-id at moment of creation */ 264 uid_t uid; /* user-id at last authorization */ 265 }; 266 static struct key_call_private *key_call_private_main = NULL; 267 268 static void 269 key_call_destroy(void *vp) 270 { 271 struct key_call_private *kcp = (struct key_call_private *)vp; 272 273 if (kcp) { 274 if (kcp->client) 275 clnt_destroy(kcp->client); 276 free(kcp); 277 } 278 } 279 280 /* 281 * Keep the handle cached. This call may be made quite often. 282 */ 283 static CLIENT * 284 getkeyserv_handle(int vers) 285 { 286 void *localhandle; 287 struct netconfig *nconf; 288 struct netconfig *tpconf; 289 struct key_call_private *kcp = key_call_private_main; 290 struct timeval wait_time; 291 struct utsname u; 292 int main_thread; 293 int fd; 294 static thread_key_t key_call_key; 295 296 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */ 297 #define TOTAL_TRIES 5 /* Number of tries */ 298 299 if ((main_thread = thr_main())) { 300 kcp = key_call_private_main; 301 } else { 302 if (key_call_key == 0) { 303 mutex_lock(&tsd_lock); 304 if (key_call_key == 0) 305 thr_keycreate(&key_call_key, key_call_destroy); 306 mutex_unlock(&tsd_lock); 307 } 308 kcp = (struct key_call_private *)thr_getspecific(key_call_key); 309 } 310 if (kcp == NULL) { 311 kcp = (struct key_call_private *)malloc(sizeof (*kcp)); 312 if (kcp == NULL) { 313 return (NULL); 314 } 315 if (main_thread) 316 key_call_private_main = kcp; 317 else 318 thr_setspecific(key_call_key, (void *) kcp); 319 kcp->client = NULL; 320 } 321 322 /* if pid has changed, destroy client and rebuild */ 323 if (kcp->client != NULL && kcp->pid != getpid()) { 324 clnt_destroy(kcp->client); 325 kcp->client = NULL; 326 } 327 328 if (kcp->client != NULL) { 329 /* if uid has changed, build client handle again */ 330 if (kcp->uid != geteuid()) { 331 kcp->uid = geteuid(); 332 auth_destroy(kcp->client->cl_auth); 333 kcp->client->cl_auth = 334 authsys_create("", kcp->uid, 0, 0, NULL); 335 if (kcp->client->cl_auth == NULL) { 336 clnt_destroy(kcp->client); 337 kcp->client = NULL; 338 return (NULL); 339 } 340 } 341 /* Change the version number to the new one */ 342 clnt_control(kcp->client, CLSET_VERS, (void *)&vers); 343 return (kcp->client); 344 } 345 if (!(localhandle = setnetconfig())) { 346 return (NULL); 347 } 348 tpconf = NULL; 349 if (uname(&u) == -1) 350 { 351 endnetconfig(localhandle); 352 return (NULL); 353 } 354 while ((nconf = getnetconfig(localhandle)) != NULL) { 355 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { 356 /* 357 * We use COTS_ORD here so that the caller can 358 * find out immediately if the server is dead. 359 */ 360 if (nconf->nc_semantics == NC_TPI_COTS_ORD) { 361 kcp->client = clnt_tp_create(u.nodename, 362 KEY_PROG, vers, nconf); 363 if (kcp->client) 364 break; 365 } else { 366 tpconf = nconf; 367 } 368 } 369 } 370 if ((kcp->client == NULL) && (tpconf)) 371 /* Now, try the CLTS or COTS loopback transport */ 372 kcp->client = clnt_tp_create(u.nodename, 373 KEY_PROG, vers, tpconf); 374 endnetconfig(localhandle); 375 376 if (kcp->client == NULL) { 377 return (NULL); 378 } 379 kcp->uid = geteuid(); 380 kcp->pid = getpid(); 381 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL); 382 if (kcp->client->cl_auth == NULL) { 383 clnt_destroy(kcp->client); 384 kcp->client = NULL; 385 return (NULL); 386 } 387 388 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES; 389 wait_time.tv_usec = 0; 390 clnt_control(kcp->client, CLSET_RETRY_TIMEOUT, 391 (char *)&wait_time); 392 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd)) 393 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 394 395 return (kcp->client); 396 } 397 398 /* returns 0 on failure, 1 on success */ 399 400 static int 401 key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt, 402 void *rslt) 403 { 404 CLIENT *clnt; 405 struct timeval wait_time; 406 407 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) { 408 cryptkeyres *res; 409 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg); 410 *(cryptkeyres*)rslt = *res; 411 return (1); 412 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) { 413 cryptkeyres *res; 414 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg); 415 *(cryptkeyres*)rslt = *res; 416 return (1); 417 } else if (proc == KEY_GEN && __key_gendes_LOCAL) { 418 des_block *res; 419 res = (*__key_gendes_LOCAL)(geteuid(), 0); 420 *(des_block*)rslt = *res; 421 return (1); 422 } 423 424 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) || 425 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) || 426 (proc == KEY_GET_CONV)) 427 clnt = getkeyserv_handle(2); /* talk to version 2 */ 428 else 429 clnt = getkeyserv_handle(1); /* talk to version 1 */ 430 431 if (clnt == NULL) { 432 return (0); 433 } 434 435 wait_time.tv_sec = TOTAL_TIMEOUT; 436 wait_time.tv_usec = 0; 437 438 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt, 439 wait_time) == RPC_SUCCESS) { 440 return (1); 441 } else { 442 return (0); 443 } 444 } 445