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 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.3 2000/01/27 23:06:39 jasone Exp $ 33 * $DragonFly: src/lib/libc/rpc/key_call.c,v 1.4 2004/10/25 19:38:01 drhodus Exp $ 34 */ 35 36 #ident "@(#)key_call.c 1.25 94/04/24 SMI" 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 <stdio.h> 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include <errno.h> 51 #include <rpc/rpc.h> 52 #include <rpc/auth.h> 53 #include <rpc/auth_unix.h> 54 #include <rpc/key_prot.h> 55 #include <string.h> 56 #include <sys/utsname.h> 57 #include <stdlib.h> 58 #include <signal.h> 59 #include <sys/wait.h> 60 #include <sys/fcntl.h> 61 62 63 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */ 64 #define KEY_NRETRY 12 /* number of retries */ 65 66 #ifdef DEBUG 67 #define debug(msg) (void) fprintf(stderr, "%s\n", msg); 68 #else 69 #define debug(msg) 70 #endif /* DEBUG */ 71 72 /* 73 * Hack to allow the keyserver to use AUTH_DES (for authenticated 74 * NIS+ calls, for example). The only functions that get called 75 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes. 76 * 77 * The approach is to have the keyserver fill in pointers to local 78 * implementations of these functions, and to call those in key_call(). 79 */ 80 81 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0; 82 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0; 83 des_block *(*__key_gendes_LOCAL)() = 0; 84 85 static int key_call ( u_long, xdrproc_t, char *, xdrproc_t, char * ); 86 87 int 88 key_setsecret(secretkey) 89 const char *secretkey; 90 { 91 keystatus status; 92 93 if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey, 94 xdr_keystatus, (char *)&status)) { 95 return (-1); 96 } 97 if (status != KEY_SUCCESS) { 98 debug("set status is nonzero"); 99 return (-1); 100 } 101 return (0); 102 } 103 104 105 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key 106 * stored for the caller's effective uid; it returns 0 otherwise 107 * 108 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't 109 * be using it, because it allows them to get the user's secret key. 110 */ 111 112 int 113 key_secretkey_is_set(void) 114 { 115 struct key_netstres kres; 116 117 memset((void*)&kres, 0, sizeof (kres)); 118 if (key_call((u_long) KEY_NET_GET, xdr_void, (char *)NULL, 119 xdr_key_netstres, (char *) &kres) && 120 (kres.status == KEY_SUCCESS) && 121 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) { 122 /* avoid leaving secret key in memory */ 123 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES); 124 return (1); 125 } 126 return (0); 127 } 128 129 int 130 key_encryptsession_pk(remotename, remotekey, deskey) 131 char *remotename; 132 netobj *remotekey; 133 des_block *deskey; 134 { 135 cryptkeyarg2 arg; 136 cryptkeyres res; 137 138 arg.remotename = remotename; 139 arg.remotekey = *remotekey; 140 arg.deskey = *deskey; 141 if (!key_call((u_long)KEY_ENCRYPT_PK, xdr_cryptkeyarg2, (char *)&arg, 142 xdr_cryptkeyres, (char *)&res)) { 143 return (-1); 144 } 145 if (res.status != KEY_SUCCESS) { 146 debug("encrypt status is nonzero"); 147 return (-1); 148 } 149 *deskey = res.cryptkeyres_u.deskey; 150 return (0); 151 } 152 153 int 154 key_decryptsession_pk(remotename, remotekey, deskey) 155 char *remotename; 156 netobj *remotekey; 157 des_block *deskey; 158 { 159 cryptkeyarg2 arg; 160 cryptkeyres res; 161 162 arg.remotename = remotename; 163 arg.remotekey = *remotekey; 164 arg.deskey = *deskey; 165 if (!key_call((u_long)KEY_DECRYPT_PK, xdr_cryptkeyarg2, (char *)&arg, 166 xdr_cryptkeyres, (char *)&res)) { 167 return (-1); 168 } 169 if (res.status != KEY_SUCCESS) { 170 debug("decrypt status is nonzero"); 171 return (-1); 172 } 173 *deskey = res.cryptkeyres_u.deskey; 174 return (0); 175 } 176 177 int 178 key_encryptsession(remotename, deskey) 179 const char *remotename; 180 des_block *deskey; 181 { 182 cryptkeyarg arg; 183 cryptkeyres res; 184 185 arg.remotename = (char *) remotename; 186 arg.deskey = *deskey; 187 if (!key_call((u_long)KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg, 188 xdr_cryptkeyres, (char *)&res)) { 189 return (-1); 190 } 191 if (res.status != KEY_SUCCESS) { 192 debug("encrypt status is nonzero"); 193 return (-1); 194 } 195 *deskey = res.cryptkeyres_u.deskey; 196 return (0); 197 } 198 199 int 200 key_decryptsession(remotename, deskey) 201 const char *remotename; 202 des_block *deskey; 203 { 204 cryptkeyarg arg; 205 cryptkeyres res; 206 207 arg.remotename = (char *) remotename; 208 arg.deskey = *deskey; 209 if (!key_call((u_long)KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg, 210 xdr_cryptkeyres, (char *)&res)) { 211 return (-1); 212 } 213 if (res.status != KEY_SUCCESS) { 214 debug("decrypt status is nonzero"); 215 return (-1); 216 } 217 *deskey = res.cryptkeyres_u.deskey; 218 return (0); 219 } 220 221 int 222 key_gendes(key) 223 des_block *key; 224 { 225 if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL, 226 xdr_des_block, (char *)key)) { 227 return (-1); 228 } 229 return (0); 230 } 231 232 int 233 key_setnet(arg) 234 struct netstarg *arg; 235 { 236 keystatus status; 237 238 239 if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg, 240 xdr_keystatus, (char *) &status)){ 241 return (-1); 242 } 243 244 if (status != KEY_SUCCESS) { 245 debug("key_setnet status is nonzero"); 246 return (-1); 247 } 248 return (1); 249 } 250 251 252 int 253 key_get_conv(pkey, deskey) 254 char *pkey; 255 des_block *deskey; 256 { 257 cryptkeyres res; 258 259 if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey, 260 xdr_cryptkeyres, (char *)&res)) { 261 return (-1); 262 } 263 if (res.status != KEY_SUCCESS) { 264 debug("get_conv status is nonzero"); 265 return (-1); 266 } 267 *deskey = res.cryptkeyres_u.deskey; 268 return (0); 269 } 270 271 struct key_call_private { 272 CLIENT *client; /* Client handle */ 273 pid_t pid; /* process-id at moment of creation */ 274 uid_t uid; /* user-id at last authorization */ 275 }; 276 static struct key_call_private *key_call_private_main = NULL; 277 278 #ifdef foo 279 static void 280 key_call_destroy(void *vp) 281 { 282 struct key_call_private *kcp = (struct key_call_private *)vp; 283 284 if (kcp) { 285 if (kcp->client) 286 clnt_destroy(kcp->client); 287 free(kcp); 288 } 289 } 290 #endif 291 292 /* 293 * Keep the handle cached. This call may be made quite often. 294 */ 295 static CLIENT * 296 getkeyserv_handle(vers) 297 int vers; 298 { 299 struct key_call_private *kcp = key_call_private_main; 300 struct timeval wait_time; 301 int fd; 302 struct sockaddr_un name; 303 int namelen = sizeof(struct sockaddr_un); 304 305 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */ 306 #define TOTAL_TRIES 5 /* Number of tries */ 307 308 if (kcp == (struct key_call_private *)NULL) { 309 kcp = (struct key_call_private *)malloc(sizeof (*kcp)); 310 if (kcp == (struct key_call_private *)NULL) { 311 return ((CLIENT *) NULL); 312 } 313 key_call_private_main = kcp; 314 kcp->client = NULL; 315 } 316 317 /* if pid has changed, destroy client and rebuild */ 318 if (kcp->client != NULL && kcp->pid != getpid()) { 319 clnt_destroy(kcp->client); 320 kcp->client = NULL; 321 } 322 323 if (kcp->client != NULL) { 324 /* if other side closed socket, build handle again */ 325 clnt_control(kcp->client, CLGET_FD, (char *)&fd); 326 if (getpeername(fd,(struct sockaddr *)&name,&namelen) == -1) { 327 auth_destroy(kcp->client->cl_auth); 328 clnt_destroy(kcp->client); 329 kcp->client = NULL; 330 } 331 } 332 333 if (kcp->client != NULL) { 334 /* if uid has changed, build client handle again */ 335 if (kcp->uid != geteuid()) { 336 kcp->uid = geteuid(); 337 auth_destroy(kcp->client->cl_auth); 338 kcp->client->cl_auth = 339 authsys_create("", kcp->uid, 0, 0, NULL); 340 if (kcp->client->cl_auth == NULL) { 341 clnt_destroy(kcp->client); 342 kcp->client = NULL; 343 return ((CLIENT *) NULL); 344 } 345 } 346 /* Change the version number to the new one */ 347 clnt_control(kcp->client, CLSET_VERS, (void *)&vers); 348 return (kcp->client); 349 } 350 351 if ((kcp->client == (CLIENT *) NULL)) 352 /* Use the AF_UNIX transport */ 353 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG, 354 vers, "unix"); 355 356 if (kcp->client == (CLIENT *) NULL) { 357 return ((CLIENT *) NULL); 358 } 359 kcp->uid = geteuid(); 360 kcp->pid = getpid(); 361 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL); 362 if (kcp->client->cl_auth == NULL) { 363 clnt_destroy(kcp->client); 364 kcp->client = NULL; 365 return ((CLIENT *) NULL); 366 } 367 368 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES; 369 wait_time.tv_usec = 0; 370 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT, 371 (char *)&wait_time); 372 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd)) 373 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 374 375 return (kcp->client); 376 } 377 378 /* returns 0 on failure, 1 on success */ 379 380 static int 381 key_call(proc, xdr_arg, arg, xdr_rslt, rslt) 382 u_long proc; 383 xdrproc_t xdr_arg; 384 char *arg; 385 xdrproc_t xdr_rslt; 386 char *rslt; 387 { 388 CLIENT *clnt; 389 struct timeval wait_time; 390 391 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) { 392 cryptkeyres *res; 393 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg); 394 *(cryptkeyres*)rslt = *res; 395 return (1); 396 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) { 397 cryptkeyres *res; 398 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg); 399 *(cryptkeyres*)rslt = *res; 400 return (1); 401 } else if (proc == KEY_GEN && __key_gendes_LOCAL) { 402 des_block *res; 403 res = (*__key_gendes_LOCAL)(geteuid(), 0); 404 *(des_block*)rslt = *res; 405 return (1); 406 } 407 408 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) || 409 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) || 410 (proc == KEY_GET_CONV)) 411 clnt = getkeyserv_handle(2); /* talk to version 2 */ 412 else 413 clnt = getkeyserv_handle(1); /* talk to version 1 */ 414 415 if (clnt == NULL) { 416 return (0); 417 } 418 419 wait_time.tv_sec = TOTAL_TIMEOUT; 420 wait_time.tv_usec = 0; 421 422 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt, 423 wait_time) == RPC_SUCCESS) { 424 return (1); 425 } else { 426 return (0); 427 } 428 } 429