Update.
[glibc.git] / sunrpc / key_call.c
blob0aa49cc78a6ce6863bafd1d46b52d2b4f9da6b26
1 /*
2 * Copyright (c) 1988 by Sun Microsystems, Inc.
3 */
4 /*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
34 * The original source is from the RPCSRC 4.0 package from Sun Microsystems.
35 * The Interface to keyserver protocoll 2, RPC over AF_UNIX und Linux/doors
36 * was added by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <rpc/auth.h>
47 #include <sys/wait.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <rpc/key_prot.h>
51 #include <bits/libc-lock.h>
53 #ifdef HAVE_DOORS
54 # include "door/door.h"
55 #endif
57 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
58 #define KEY_NRETRY 12 /* number of retries */
60 #define debug(msg) /* turn off debugging */
62 extern int _openchild (char *command, FILE **fto, FILE **ffrom);
65 static int key_call (u_long, xdrproc_t xdr_arg, char *,
66 xdrproc_t xdr_rslt, char *) internal_function;
68 static struct timeval trytimeout = {KEY_TIMEOUT, 0};
69 static struct timeval tottimeout = {KEY_TIMEOUT *KEY_NRETRY, 0};
71 int
72 key_setsecret (char *secretkey)
74 keystatus status;
76 if (!key_call ((u_long) KEY_SET, (xdrproc_t) xdr_keybuf, secretkey,
77 (xdrproc_t) xdr_keystatus, (char *) &status))
78 return -1;
79 if (status != KEY_SUCCESS)
81 debug ("set status is nonzero");
82 return -1;
84 return 0;
87 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
88 * stored for the caller's effective uid; it returns 0 otherwise
90 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
91 * be using it, because it allows them to get the user's secret key.
93 int
94 key_secretkey_is_set (void)
96 struct key_netstres kres;
98 memset (&kres, 0, sizeof (kres));
99 if (key_call ((u_long) KEY_NET_GET, (xdrproc_t) xdr_void, (char *) NULL,
100 (xdrproc_t) xdr_key_netstres, (char *) &kres) &&
101 (kres.status == KEY_SUCCESS) &&
102 (kres.key_netstres_u.knet.st_priv_key[0] != 0))
104 /* avoid leaving secret key in memory */
105 memset (kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
106 return 1;
108 return 0;
112 key_encryptsession (char *remotename, des_block *deskey)
114 cryptkeyarg arg;
115 cryptkeyres res;
117 arg.remotename = remotename;
118 arg.deskey = *deskey;
119 if (!key_call ((u_long) KEY_ENCRYPT, (xdrproc_t) xdr_cryptkeyarg,
120 (char *) &arg, (xdrproc_t) xdr_cryptkeyres, (char *) &res))
121 return -1;
123 if (res.status != KEY_SUCCESS)
125 debug ("encrypt status is nonzero");
126 return -1;
128 *deskey = res.cryptkeyres_u.deskey;
129 return 0;
133 key_decryptsession (char *remotename, des_block *deskey)
135 cryptkeyarg arg;
136 cryptkeyres res;
138 arg.remotename = remotename;
139 arg.deskey = *deskey;
140 if (!key_call ((u_long) KEY_DECRYPT, (xdrproc_t) xdr_cryptkeyarg,
141 (char *) &arg, (xdrproc_t) xdr_cryptkeyres, (char *) &res))
142 return -1;
143 if (res.status != KEY_SUCCESS)
145 debug ("decrypt status is nonzero");
146 return -1;
148 *deskey = res.cryptkeyres_u.deskey;
149 return 0;
153 key_encryptsession_pk (char *remotename, netobj *remotekey,
154 des_block *deskey)
156 cryptkeyarg2 arg;
157 cryptkeyres res;
159 arg.remotename = remotename;
160 arg.remotekey = *remotekey;
161 arg.deskey = *deskey;
162 if (!key_call ((u_long) KEY_ENCRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
163 (char *) &arg, (xdrproc_t) xdr_cryptkeyres, (char *) &res))
164 return -1;
166 if (res.status != KEY_SUCCESS)
168 debug ("encrypt status is nonzero");
169 return -1;
171 *deskey = res.cryptkeyres_u.deskey;
172 return 0;
176 key_decryptsession_pk (char *remotename, netobj *remotekey,
177 des_block *deskey)
179 cryptkeyarg2 arg;
180 cryptkeyres res;
182 arg.remotename = remotename;
183 arg.remotekey = *remotekey;
184 arg.deskey = *deskey;
185 if (!key_call ((u_long) KEY_DECRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
186 (char *) &arg, (xdrproc_t) xdr_cryptkeyres, (char *) &res))
187 return -1;
189 if (res.status != KEY_SUCCESS)
191 debug ("decrypt status is nonzero");
192 return -1;
194 *deskey = res.cryptkeyres_u.deskey;
195 return 0;
199 key_gendes (des_block *key)
201 struct sockaddr_in sin;
202 CLIENT *client;
203 int socket;
204 enum clnt_stat stat;
206 sin.sin_family = AF_INET;
207 sin.sin_port = 0;
208 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
209 __bzero (sin.sin_zero, sizeof (sin.sin_zero));
210 socket = RPC_ANYSOCK;
211 client = clntudp_bufcreate (&sin, (u_long) KEY_PROG, (u_long) KEY_VERS,
212 trytimeout, &socket, RPCSMALLMSGSIZE,
213 RPCSMALLMSGSIZE);
214 if (client == NULL)
215 return -1;
217 stat = clnt_call (client, KEY_GEN, (xdrproc_t) xdr_void, NULL,
218 (xdrproc_t) xdr_des_block, (caddr_t) key, tottimeout);
219 clnt_destroy (client);
220 __close (socket);
221 if (stat != RPC_SUCCESS)
222 return -1;
224 return 0;
228 key_setnet (struct key_netstarg *arg)
230 keystatus status;
232 if (!key_call ((u_long) KEY_NET_PUT, (xdrproc_t) xdr_key_netstarg,
233 (char *) arg,(xdrproc_t) xdr_keystatus, (char *) &status))
234 return -1;
236 if (status != KEY_SUCCESS)
238 debug ("key_setnet status is nonzero");
239 return -1;
241 return 1;
245 key_get_conv (char *pkey, des_block *deskey)
247 cryptkeyres res;
249 if (!key_call ((u_long) KEY_GET_CONV, (xdrproc_t) xdr_keybuf, pkey,
250 (xdrproc_t) xdr_cryptkeyres, (char *) &res))
251 return -1;
253 if (res.status != KEY_SUCCESS)
255 debug ("get_conv status is nonzero");
256 return -1;
258 *deskey = res.cryptkeyres_u.deskey;
259 return 0;
263 * Hack to allow the keyserver to use AUTH_DES (for authenticated
264 * NIS+ calls, for example). The only functions that get called
265 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
267 * The approach is to have the keyserver fill in pointers to local
268 * implementations of these functions, and to call those in key_call().
271 cryptkeyres *(*__key_encryptsession_pk_LOCAL) (uid_t, char *) = 0;
272 cryptkeyres *(*__key_decryptsession_pk_LOCAL) (uid_t, char *) = 0;
273 des_block *(*__key_gendes_LOCAL) (uid_t, char *) = 0;
275 static int
276 internal_function
277 key_call_keyenvoy (u_long proc, xdrproc_t xdr_arg, char *arg,
278 xdrproc_t xdr_rslt, char *rslt)
280 XDR xdrargs;
281 XDR xdrrslt;
282 FILE *fargs;
283 FILE *frslt;
284 sigset_t oldmask, mask;
285 union wait status;
286 int pid;
287 int success;
288 uid_t ruid;
289 uid_t euid;
290 static char MESSENGER[] = "/usr/etc/keyenvoy";
292 success = 1;
293 sigemptyset (&mask);
294 sigaddset (&mask, SIGCHLD);
295 __sigprocmask (SIG_BLOCK, &mask, &oldmask);
298 * We are going to exec a set-uid program which makes our effective uid
299 * zero, and authenticates us with our real uid. We need to make the
300 * effective uid be the real uid for the setuid program, and
301 * the real uid be the effective uid so that we can change things back.
303 euid = __geteuid ();
304 ruid = __getuid ();
305 __setreuid (euid, ruid);
306 pid = _openchild (MESSENGER, &fargs, &frslt);
307 __setreuid (ruid, euid);
308 if (pid < 0)
310 debug ("open_streams");
311 __sigprocmask (SIG_SETMASK, &oldmask, NULL);
312 return (0);
314 xdrstdio_create (&xdrargs, fargs, XDR_ENCODE);
315 xdrstdio_create (&xdrrslt, frslt, XDR_DECODE);
317 if (!xdr_u_long (&xdrargs, &proc) || !(*xdr_arg) (&xdrargs, arg))
319 debug ("xdr args");
320 success = 0;
322 fclose (fargs);
324 if (success && !(*xdr_rslt) (&xdrrslt, rslt))
326 debug ("xdr rslt");
327 success = 0;
329 fclose(frslt);
331 wait_again:
332 if (__wait4 (pid, &status, 0, NULL) < 0)
334 if (errno == EINTR)
335 goto wait_again;
336 debug ("wait4");
337 if (errno == ECHILD || errno == ESRCH)
338 perror ("wait");
339 else
340 success = 0;
342 else
343 if (status.w_retcode)
345 debug ("wait4 1");
346 success = 0;
348 __sigprocmask (SIG_SETMASK, &oldmask, NULL);
350 return success;
353 struct key_call_private {
354 CLIENT *client; /* Client handle */
355 pid_t pid; /* process-id at moment of creation */
356 uid_t uid; /* user-id at last authorization */
358 static struct key_call_private *key_call_private_main = NULL;
359 __libc_lock_define_initialized (static, keycall_lock)
362 * Keep the handle cached. This call may be made quite often.
364 static CLIENT *
365 getkeyserv_handle (int vers)
367 struct key_call_private *kcp = key_call_private_main;
368 struct timeval wait_time;
369 int fd;
370 struct sockaddr_un name;
371 int namelen = sizeof(struct sockaddr_un);
373 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
374 #define TOTAL_TRIES 5 /* Number of tries */
376 if (kcp == (struct key_call_private *)NULL)
378 kcp = (struct key_call_private *)malloc (sizeof (*kcp));
379 if (kcp == (struct key_call_private *)NULL)
380 return (CLIENT *) NULL;
382 key_call_private_main = kcp;
383 kcp->client = NULL;
386 /* if pid has changed, destroy client and rebuild */
387 if (kcp->client != NULL && kcp->pid != __getpid ())
389 clnt_destroy (kcp->client);
390 kcp->client = NULL;
393 if (kcp->client != NULL)
395 /* if other side closed socket, build handle again */
396 clnt_control (kcp->client, CLGET_FD, (char *)&fd);
397 if (getpeername (fd,(struct sockaddr *)&name,&namelen) == -1)
399 auth_destroy (kcp->client->cl_auth);
400 clnt_destroy (kcp->client);
401 kcp->client = NULL;
405 if (kcp->client != NULL)
407 /* if uid has changed, build client handle again */
408 if (kcp->uid != __geteuid ())
410 kcp->uid = __geteuid ();
411 auth_destroy (kcp->client->cl_auth);
412 kcp->client->cl_auth =
413 authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
414 if (kcp->client->cl_auth == NULL)
416 clnt_destroy (kcp->client);
417 kcp->client = NULL;
418 return ((CLIENT *) NULL);
421 /* Change the version number to the new one */
422 clnt_control (kcp->client, CLSET_VERS, (void *)&vers);
423 return kcp->client;
426 if ((kcp->client == (CLIENT *) NULL))
427 /* Use the AF_UNIX transport */
428 kcp->client = clnt_create ("/var/run/keyservsock", KEY_PROG, vers, "unix");
430 if (kcp->client == (CLIENT *) NULL)
431 return (CLIENT *) NULL;
433 kcp->uid = __geteuid ();
434 kcp->pid = __getpid ();
435 kcp->client->cl_auth = authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
436 if (kcp->client->cl_auth == NULL)
438 clnt_destroy (kcp->client);
439 kcp->client = NULL;
440 return (CLIENT *) NULL;
443 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
444 wait_time.tv_usec = 0;
445 clnt_control (kcp->client, CLSET_RETRY_TIMEOUT,
446 (char *)&wait_time);
447 if (clnt_control (kcp->client, CLGET_FD, (char *)&fd))
448 fcntl (fd, F_SETFD, 1); /* make it "close on exec" */
450 return kcp->client;
453 /* returns 0 on failure, 1 on success */
454 static int
455 internal_function
456 key_call_socket (u_long proc, xdrproc_t xdr_arg, char *arg,
457 xdrproc_t xdr_rslt, char *rslt)
459 CLIENT *clnt;
460 struct timeval wait_time;
461 int result = 0;
463 __libc_lock_lock (keycall_lock);
464 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
465 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
466 (proc == KEY_GET_CONV))
467 clnt = getkeyserv_handle(2); /* talk to version 2 */
468 else
469 clnt = getkeyserv_handle(1); /* talk to version 1 */
471 if (clnt != NULL)
473 wait_time.tv_sec = TOTAL_TIMEOUT;
474 wait_time.tv_usec = 0;
476 if (clnt_call (clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
477 wait_time) == RPC_SUCCESS)
478 result = 1;
481 __libc_lock_unlock (keycall_lock);
483 return result;
486 #ifdef HAVE_DOORS
487 /* returns 0 on failure, 1 on success */
488 static int
489 internal_function
490 key_call_door (u_long proc, xdrproc_t xdr_arg, char *arg,
491 xdrproc_t xdr_rslt, char *rslt)
493 XDR xdrs;
494 int fd;
495 door_arg_t args;
496 char *data_ptr;
497 u_long data_len = 0;
498 char res[255];
500 if ((fd = open("/var/run/keyservdoor", O_RDONLY)) < 0)
501 return 0;
502 res[0] = 0;
504 data_len = xdr_sizeof (xdr_arg, arg);
505 data_ptr = calloc (1, data_len + 2 * sizeof (u_long));
506 if (data_ptr == NULL)
507 return 0;
509 xdrmem_create (&xdrs, &data_ptr[2 * sizeof (u_long)], data_len, XDR_ENCODE);
510 if (!xdr_arg (&xdrs, arg))
512 xdr_destroy (&xdrs);
513 free (data_ptr);
514 return 0;
516 xdr_destroy (&xdrs);
518 memcpy (data_ptr, &proc, sizeof (u_long));
519 memcpy (&data_ptr[sizeof (proc)], &data_len, sizeof (u_long));
521 args.data_ptr = data_ptr;
522 args.data_size = data_len + 2 * sizeof (u_long);
523 args.desc_ptr = NULL;
524 args.desc_num = 0;
525 args.rbuf = res;
526 args.rsize = sizeof (res);
528 if (__door_call (fd, &args) < 0)
529 return 0;
531 free (data_ptr);
532 close (fd);
534 memcpy (&data_len, args.data_ptr, sizeof (u_long));
535 if (data_len != 0)
536 return 0;
538 memcpy (&data_len, &args.data_ptr[sizeof (u_long)], sizeof (u_long));
539 xdrmem_create (&xdrs, &args.data_ptr[2 * sizeof (u_long)],
540 data_len, XDR_DECODE);
541 if (!xdr_rslt (&xdrs, rslt))
543 xdr_destroy (&xdrs);
544 return 0;
546 xdr_destroy (&xdrs);
548 return 1;
550 #endif
552 /* returns 0 on failure, 1 on success */
553 static int
554 internal_function
555 key_call (u_long proc, xdrproc_t xdr_arg, char *arg,
556 xdrproc_t xdr_rslt, char *rslt)
558 static int use_keyenvoy = 0;
559 #ifdef HAVE_DOORS
560 static int use_doors = 1;
561 #endif
563 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL)
565 cryptkeyres *res;
566 res = (*__key_encryptsession_pk_LOCAL) (__geteuid (), arg);
567 *(cryptkeyres *) rslt = *res;
568 return 1;
570 else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL)
572 cryptkeyres *res;
573 res = (*__key_decryptsession_pk_LOCAL) (__geteuid (), arg);
574 *(cryptkeyres *) rslt = *res;
575 return 1;
577 else if (proc == KEY_GEN && __key_gendes_LOCAL)
579 des_block *res;
580 res = (*__key_gendes_LOCAL) (__geteuid (), 0);
581 *(des_block *) rslt = *res;
582 return 1;
585 #ifdef HAVE_DOORS
586 if (use_doors)
588 if (key_call_door (proc, xdr_arg, arg, xdr_rslt, rslt))
589 return 1;
590 use_doors = 0;
592 #endif
593 if (!use_keyenvoy)
595 if (key_call_socket (proc, xdr_arg, arg, xdr_rslt, rslt))
596 return 1;
597 use_keyenvoy = 1;
599 return key_call_keyenvoy (proc, xdr_arg, arg, xdr_rslt, rslt);