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