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 #ifdef _RPC_THREAD_SAFE_
363 #define key_call_private_main ((struct key_call_private *)RPC_THREAD_VARIABLE(key_call_private_s))
365 static struct key_call_private
*key_call_private_main
;
367 __libc_lock_define_initialized (static, keycall_lock
)
370 * Keep the handle cached. This call may be made quite often.
373 getkeyserv_handle (int vers
)
375 struct key_call_private
*kcp
= key_call_private_main
;
376 struct timeval wait_time
;
378 struct sockaddr_un name
;
379 int namelen
= sizeof(struct sockaddr_un
);
381 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
382 #define TOTAL_TRIES 5 /* Number of tries */
384 if (kcp
== (struct key_call_private
*)NULL
)
386 kcp
= (struct key_call_private
*)malloc (sizeof (*kcp
));
387 if (kcp
== (struct key_call_private
*)NULL
)
388 return (CLIENT
*) NULL
;
390 key_call_private_main
= kcp
;
394 /* if pid has changed, destroy client and rebuild */
395 if (kcp
->client
!= NULL
&& kcp
->pid
!= __getpid ())
397 clnt_destroy (kcp
->client
);
401 if (kcp
->client
!= NULL
)
403 /* if other side closed socket, build handle again */
404 clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
);
405 if (__getpeername (fd
,(struct sockaddr
*)&name
,&namelen
) == -1)
407 auth_destroy (kcp
->client
->cl_auth
);
408 clnt_destroy (kcp
->client
);
413 if (kcp
->client
!= NULL
)
415 /* if uid has changed, build client handle again */
416 if (kcp
->uid
!= __geteuid ())
418 kcp
->uid
= __geteuid ();
419 auth_destroy (kcp
->client
->cl_auth
);
420 kcp
->client
->cl_auth
=
421 authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
422 if (kcp
->client
->cl_auth
== NULL
)
424 clnt_destroy (kcp
->client
);
426 return ((CLIENT
*) NULL
);
429 /* Change the version number to the new one */
430 clnt_control (kcp
->client
, CLSET_VERS
, (void *)&vers
);
434 if ((kcp
->client
== (CLIENT
*) NULL
))
435 /* Use the AF_UNIX transport */
436 kcp
->client
= clnt_create ("/var/run/keyservsock", KEY_PROG
, vers
, "unix");
438 if (kcp
->client
== (CLIENT
*) NULL
)
439 return (CLIENT
*) NULL
;
441 kcp
->uid
= __geteuid ();
442 kcp
->pid
= __getpid ();
443 kcp
->client
->cl_auth
= authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
444 if (kcp
->client
->cl_auth
== NULL
)
446 clnt_destroy (kcp
->client
);
448 return (CLIENT
*) NULL
;
451 wait_time
.tv_sec
= TOTAL_TIMEOUT
/TOTAL_TRIES
;
452 wait_time
.tv_usec
= 0;
453 clnt_control (kcp
->client
, CLSET_RETRY_TIMEOUT
,
455 if (clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
))
456 __fcntl (fd
, F_SETFD
, 1); /* make it "close on exec" */
461 /* returns 0 on failure, 1 on success */
464 key_call_socket (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
465 xdrproc_t xdr_rslt
, char *rslt
)
468 struct timeval wait_time
;
471 __libc_lock_lock (keycall_lock
);
472 if ((proc
== KEY_ENCRYPT_PK
) || (proc
== KEY_DECRYPT_PK
) ||
473 (proc
== KEY_NET_GET
) || (proc
== KEY_NET_PUT
) ||
474 (proc
== KEY_GET_CONV
))
475 clnt
= getkeyserv_handle(2); /* talk to version 2 */
477 clnt
= getkeyserv_handle(1); /* talk to version 1 */
481 wait_time
.tv_sec
= TOTAL_TIMEOUT
;
482 wait_time
.tv_usec
= 0;
484 if (clnt_call (clnt
, proc
, xdr_arg
, arg
, xdr_rslt
, rslt
,
485 wait_time
) == RPC_SUCCESS
)
489 __libc_lock_unlock (keycall_lock
);
495 /* returns 0 on failure, 1 on success */
498 key_call_door (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
499 xdrproc_t xdr_rslt
, char *rslt
)
508 if ((fd
= open("/var/run/keyservdoor", O_RDONLY
)) < 0)
512 data_len
= xdr_sizeof (xdr_arg
, arg
);
513 data_ptr
= calloc (1, data_len
+ 2 * sizeof (u_long
));
514 if (data_ptr
== NULL
)
517 xdrmem_create (&xdrs
, &data_ptr
[2 * sizeof (u_long
)], data_len
, XDR_ENCODE
);
518 if (!xdr_arg (&xdrs
, arg
))
526 memcpy (data_ptr
, &proc
, sizeof (u_long
));
527 memcpy (&data_ptr
[sizeof (proc
)], &data_len
, sizeof (u_long
));
529 args
.data_ptr
= data_ptr
;
530 args
.data_size
= data_len
+ 2 * sizeof (u_long
);
531 args
.desc_ptr
= NULL
;
534 args
.rsize
= sizeof (res
);
536 ret
= __door_call (fd
, &args
);
543 memcpy (&data_len
, args
.data_ptr
, sizeof (u_long
));
547 memcpy (&data_len
, &args
.data_ptr
[sizeof (u_long
)], sizeof (u_long
));
548 xdrmem_create (&xdrs
, &args
.data_ptr
[2 * sizeof (u_long
)],
549 data_len
, XDR_DECODE
);
550 if (!xdr_rslt (&xdrs
, rslt
))
561 /* returns 0 on failure, 1 on success */
564 key_call (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
565 xdrproc_t xdr_rslt
, char *rslt
)
568 static int use_keyenvoy
;
571 static int not_use_doors
;
574 if (proc
== KEY_ENCRYPT_PK
&& __key_encryptsession_pk_LOCAL
)
577 res
= (*__key_encryptsession_pk_LOCAL
) (__geteuid (), arg
);
578 *(cryptkeyres
*) rslt
= *res
;
581 else if (proc
== KEY_DECRYPT_PK
&& __key_decryptsession_pk_LOCAL
)
584 res
= (*__key_decryptsession_pk_LOCAL
) (__geteuid (), arg
);
585 *(cryptkeyres
*) rslt
= *res
;
588 else if (proc
== KEY_GEN
&& __key_gendes_LOCAL
)
591 res
= (*__key_gendes_LOCAL
) (__geteuid (), 0);
592 *(des_block
*) rslt
= *res
;
599 if (key_call_door (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
606 return key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);
610 if (key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
614 return key_call_keyenvoy (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);
618 #ifdef _RPC_THREAD_SAFE_
620 __rpc_thread_key_cleanup (void)
622 struct key_call_private
*kcp
= RPC_THREAD_VARIABLE(key_call_private_s
);
626 clnt_destroy(kcp
->client
);
630 #endif /* _RPC_THREAD_SAFE_ */