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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 * Copyright (c) 1988 by Sun Microsystems, Inc.
33 * The original source is from the RPCSRC 4.0 package from Sun Microsystems.
34 * The Interface to keyserver protocoll 2, RPC over AF_UNIX and Linux/doors
35 * was added by Thorsten Kukuk <kukuk@suse.de>
36 * Since the Linux/doors project was stopped, I doubt that this code will
37 * ever be useful <kukuk@suse.de>.
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <rpc/key_prot.h>
52 #include <bits/libc-lock.h>
55 # include "door/door.h"
58 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
59 #define KEY_NRETRY 12 /* number of retries */
61 #define debug(msg) /* turn off debugging */
64 extern int _openchild (const char *command
, FILE **fto
, FILE **ffrom
);
67 static int key_call (u_long
, xdrproc_t xdr_arg
, char *,
68 xdrproc_t xdr_rslt
, char *) internal_function
;
70 static const struct timeval trytimeout
= {KEY_TIMEOUT
, 0};
71 static const struct timeval tottimeout
= {KEY_TIMEOUT
*KEY_NRETRY
, 0};
74 key_setsecret (char *secretkey
)
78 if (!key_call ((u_long
) KEY_SET
, (xdrproc_t
) xdr_keybuf
, secretkey
,
79 (xdrproc_t
) xdr_keystatus
, (char *) &status
))
81 if (status
!= KEY_SUCCESS
)
83 debug ("set status is nonzero");
89 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
90 * stored for the caller's effective uid; it returns 0 otherwise
92 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
93 * be using it, because it allows them to get the user's secret key.
96 key_secretkey_is_set (void)
98 struct key_netstres kres
;
100 memset (&kres
, 0, sizeof (kres
));
101 if (key_call ((u_long
) KEY_NET_GET
, (xdrproc_t
) xdr_void
, (char *) NULL
,
102 (xdrproc_t
) xdr_key_netstres
, (char *) &kres
) &&
103 (kres
.status
== KEY_SUCCESS
) &&
104 (kres
.key_netstres_u
.knet
.st_priv_key
[0] != 0))
106 /* avoid leaving secret key in memory */
107 memset (kres
.key_netstres_u
.knet
.st_priv_key
, 0, HEXKEYBYTES
);
114 key_encryptsession (char *remotename
, des_block
*deskey
)
119 arg
.remotename
= remotename
;
120 arg
.deskey
= *deskey
;
121 if (!key_call ((u_long
) KEY_ENCRYPT
, (xdrproc_t
) xdr_cryptkeyarg
,
122 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
125 if (res
.status
!= KEY_SUCCESS
)
127 debug ("encrypt status is nonzero");
130 *deskey
= res
.cryptkeyres_u
.deskey
;
135 key_decryptsession (char *remotename
, des_block
*deskey
)
140 arg
.remotename
= remotename
;
141 arg
.deskey
= *deskey
;
142 if (!key_call ((u_long
) KEY_DECRYPT
, (xdrproc_t
) xdr_cryptkeyarg
,
143 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
145 if (res
.status
!= KEY_SUCCESS
)
147 debug ("decrypt status is nonzero");
150 *deskey
= res
.cryptkeyres_u
.deskey
;
155 key_encryptsession_pk (char *remotename
, netobj
*remotekey
,
161 arg
.remotename
= remotename
;
162 arg
.remotekey
= *remotekey
;
163 arg
.deskey
= *deskey
;
164 if (!key_call ((u_long
) KEY_ENCRYPT_PK
, (xdrproc_t
) xdr_cryptkeyarg2
,
165 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
168 if (res
.status
!= KEY_SUCCESS
)
170 debug ("encrypt status is nonzero");
173 *deskey
= res
.cryptkeyres_u
.deskey
;
178 key_decryptsession_pk (char *remotename
, netobj
*remotekey
,
184 arg
.remotename
= remotename
;
185 arg
.remotekey
= *remotekey
;
186 arg
.deskey
= *deskey
;
187 if (!key_call ((u_long
) KEY_DECRYPT_PK
, (xdrproc_t
) xdr_cryptkeyarg2
,
188 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
191 if (res
.status
!= KEY_SUCCESS
)
193 debug ("decrypt status is nonzero");
196 *deskey
= res
.cryptkeyres_u
.deskey
;
201 key_gendes (des_block
*key
)
203 struct sockaddr_in sin
;
208 sin
.sin_family
= AF_INET
;
210 sin
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
211 __bzero (sin
.sin_zero
, sizeof (sin
.sin_zero
));
212 socket
= RPC_ANYSOCK
;
213 client
= clntudp_bufcreate (&sin
, (u_long
) KEY_PROG
, (u_long
) KEY_VERS
,
214 trytimeout
, &socket
, RPCSMALLMSGSIZE
,
219 stat
= clnt_call (client
, KEY_GEN
, (xdrproc_t
) xdr_void
, NULL
,
220 (xdrproc_t
) xdr_des_block
, (caddr_t
) key
, tottimeout
);
221 clnt_destroy (client
);
223 if (stat
!= RPC_SUCCESS
)
230 key_setnet (struct key_netstarg
*arg
)
234 if (!key_call ((u_long
) KEY_NET_PUT
, (xdrproc_t
) xdr_key_netstarg
,
235 (char *) arg
,(xdrproc_t
) xdr_keystatus
, (char *) &status
))
238 if (status
!= KEY_SUCCESS
)
240 debug ("key_setnet status is nonzero");
247 key_get_conv (char *pkey
, des_block
*deskey
)
251 if (!key_call ((u_long
) KEY_GET_CONV
, (xdrproc_t
) xdr_keybuf
, pkey
,
252 (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
255 if (res
.status
!= KEY_SUCCESS
)
257 debug ("get_conv status is nonzero");
260 *deskey
= res
.cryptkeyres_u
.deskey
;
265 * Hack to allow the keyserver to use AUTH_DES (for authenticated
266 * NIS+ calls, for example). The only functions that get called
267 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
269 * The approach is to have the keyserver fill in pointers to local
270 * implementations of these functions, and to call those in key_call().
273 cryptkeyres
*(*__key_encryptsession_pk_LOCAL
) (uid_t
, char *);
274 cryptkeyres
*(*__key_decryptsession_pk_LOCAL
) (uid_t
, char *);
275 des_block
*(*__key_gendes_LOCAL
) (uid_t
, char *);
280 key_call_keyenvoy (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
281 xdrproc_t xdr_rslt
, char *rslt
)
287 sigset_t oldmask
, mask
;
293 static const char MESSENGER
[] = "/usr/etc/keyenvoy";
297 sigaddset (&mask
, SIGCHLD
);
298 __sigprocmask (SIG_BLOCK
, &mask
, &oldmask
);
301 * We are going to exec a set-uid program which makes our effective uid
302 * zero, and authenticates us with our real uid. We need to make the
303 * effective uid be the real uid for the setuid program, and
304 * the real uid be the effective uid so that we can change things back.
308 __setreuid (euid
, ruid
);
309 pid
= _openchild (MESSENGER
, &fargs
, &frslt
);
310 __setreuid (ruid
, euid
);
313 debug ("open_streams");
314 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
317 xdrstdio_create (&xdrargs
, fargs
, XDR_ENCODE
);
318 xdrstdio_create (&xdrrslt
, frslt
, XDR_DECODE
);
320 if (!xdr_u_long (&xdrargs
, &proc
) || !(*xdr_arg
) (&xdrargs
, arg
))
327 if (success
&& !(*xdr_rslt
) (&xdrrslt
, rslt
))
335 if (__wait4 (pid
, &status
, 0, NULL
) < 0)
340 if (errno
== ECHILD
|| errno
== ESRCH
)
346 if (status
.w_retcode
)
351 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
357 struct key_call_private
{
358 CLIENT
*client
; /* Client handle */
359 pid_t pid
; /* process-id at moment of creation */
360 uid_t uid
; /* user-id at last authorization */
362 static struct key_call_private
*key_call_private_main
;
363 __libc_lock_define_initialized (static, keycall_lock
)
366 * Keep the handle cached. This call may be made quite often.
369 getkeyserv_handle (int vers
)
371 struct key_call_private
*kcp
= key_call_private_main
;
372 struct timeval wait_time
;
374 struct sockaddr_un name
;
375 int namelen
= sizeof(struct sockaddr_un
);
377 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
378 #define TOTAL_TRIES 5 /* Number of tries */
380 if (kcp
== (struct key_call_private
*)NULL
)
382 kcp
= (struct key_call_private
*)malloc (sizeof (*kcp
));
383 if (kcp
== (struct key_call_private
*)NULL
)
384 return (CLIENT
*) NULL
;
386 key_call_private_main
= kcp
;
390 /* if pid has changed, destroy client and rebuild */
391 if (kcp
->client
!= NULL
&& kcp
->pid
!= __getpid ())
393 clnt_destroy (kcp
->client
);
397 if (kcp
->client
!= NULL
)
399 /* if other side closed socket, build handle again */
400 clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
);
401 if (getpeername (fd
,(struct sockaddr
*)&name
,&namelen
) == -1)
403 auth_destroy (kcp
->client
->cl_auth
);
404 clnt_destroy (kcp
->client
);
409 if (kcp
->client
!= NULL
)
411 /* if uid has changed, build client handle again */
412 if (kcp
->uid
!= __geteuid ())
414 kcp
->uid
= __geteuid ();
415 auth_destroy (kcp
->client
->cl_auth
);
416 kcp
->client
->cl_auth
=
417 authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
418 if (kcp
->client
->cl_auth
== NULL
)
420 clnt_destroy (kcp
->client
);
422 return ((CLIENT
*) NULL
);
425 /* Change the version number to the new one */
426 clnt_control (kcp
->client
, CLSET_VERS
, (void *)&vers
);
430 if ((kcp
->client
== (CLIENT
*) NULL
))
431 /* Use the AF_UNIX transport */
432 kcp
->client
= clnt_create ("/var/run/keyservsock", KEY_PROG
, vers
, "unix");
434 if (kcp
->client
== (CLIENT
*) NULL
)
435 return (CLIENT
*) NULL
;
437 kcp
->uid
= __geteuid ();
438 kcp
->pid
= __getpid ();
439 kcp
->client
->cl_auth
= authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
440 if (kcp
->client
->cl_auth
== NULL
)
442 clnt_destroy (kcp
->client
);
444 return (CLIENT
*) NULL
;
447 wait_time
.tv_sec
= TOTAL_TIMEOUT
/TOTAL_TRIES
;
448 wait_time
.tv_usec
= 0;
449 clnt_control (kcp
->client
, CLSET_RETRY_TIMEOUT
,
451 if (clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
))
452 __fcntl (fd
, F_SETFD
, 1); /* make it "close on exec" */
457 /* returns 0 on failure, 1 on success */
460 key_call_socket (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
461 xdrproc_t xdr_rslt
, char *rslt
)
464 struct timeval wait_time
;
467 __libc_lock_lock (keycall_lock
);
468 if ((proc
== KEY_ENCRYPT_PK
) || (proc
== KEY_DECRYPT_PK
) ||
469 (proc
== KEY_NET_GET
) || (proc
== KEY_NET_PUT
) ||
470 (proc
== KEY_GET_CONV
))
471 clnt
= getkeyserv_handle(2); /* talk to version 2 */
473 clnt
= getkeyserv_handle(1); /* talk to version 1 */
477 wait_time
.tv_sec
= TOTAL_TIMEOUT
;
478 wait_time
.tv_usec
= 0;
480 if (clnt_call (clnt
, proc
, xdr_arg
, arg
, xdr_rslt
, rslt
,
481 wait_time
) == RPC_SUCCESS
)
485 __libc_lock_unlock (keycall_lock
);
491 /* returns 0 on failure, 1 on success */
494 key_call_door (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
495 xdrproc_t xdr_rslt
, char *rslt
)
504 if ((fd
= open("/var/run/keyservdoor", O_RDONLY
)) < 0)
508 data_len
= xdr_sizeof (xdr_arg
, arg
);
509 data_ptr
= calloc (1, data_len
+ 2 * sizeof (u_long
));
510 if (data_ptr
== NULL
)
513 xdrmem_create (&xdrs
, &data_ptr
[2 * sizeof (u_long
)], data_len
, XDR_ENCODE
);
514 if (!xdr_arg (&xdrs
, arg
))
522 memcpy (data_ptr
, &proc
, sizeof (u_long
));
523 memcpy (&data_ptr
[sizeof (proc
)], &data_len
, sizeof (u_long
));
525 args
.data_ptr
= data_ptr
;
526 args
.data_size
= data_len
+ 2 * sizeof (u_long
);
527 args
.desc_ptr
= NULL
;
530 args
.rsize
= sizeof (res
);
532 ret
= __door_call (fd
, &args
);
539 memcpy (&data_len
, args
.data_ptr
, sizeof (u_long
));
543 memcpy (&data_len
, &args
.data_ptr
[sizeof (u_long
)], sizeof (u_long
));
544 xdrmem_create (&xdrs
, &args
.data_ptr
[2 * sizeof (u_long
)],
545 data_len
, XDR_DECODE
);
546 if (!xdr_rslt (&xdrs
, rslt
))
557 /* returns 0 on failure, 1 on success */
560 key_call (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
561 xdrproc_t xdr_rslt
, char *rslt
)
564 static int use_keyenvoy
;
567 static int not_use_doors
;
570 if (proc
== KEY_ENCRYPT_PK
&& __key_encryptsession_pk_LOCAL
)
573 res
= (*__key_encryptsession_pk_LOCAL
) (__geteuid (), arg
);
574 *(cryptkeyres
*) rslt
= *res
;
577 else if (proc
== KEY_DECRYPT_PK
&& __key_decryptsession_pk_LOCAL
)
580 res
= (*__key_decryptsession_pk_LOCAL
) (__geteuid (), arg
);
581 *(cryptkeyres
*) rslt
= *res
;
584 else if (proc
== KEY_GEN
&& __key_gendes_LOCAL
)
587 res
= (*__key_gendes_LOCAL
) (__geteuid (), 0);
588 *(des_block
*) rslt
= *res
;
595 if (key_call_door (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
602 return key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);
606 if (key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
610 return key_call_keyenvoy (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);