2 * Copyright (c) 1988 by Sun Microsystems, Inc.
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.
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>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <rpc/key_prot.h>
51 #include <bits/libc-lock.h>
54 # include "door/door.h"
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};
72 key_setsecret (char *secretkey
)
76 if (!key_call ((u_long
) KEY_SET
, (xdrproc_t
) xdr_keybuf
, secretkey
,
77 (xdrproc_t
) xdr_keystatus
, (char *) &status
))
79 if (status
!= KEY_SUCCESS
)
81 debug ("set status is nonzero");
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.
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
);
112 key_encryptsession (char *remotename
, des_block
*deskey
)
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
))
123 if (res
.status
!= KEY_SUCCESS
)
125 debug ("encrypt status is nonzero");
128 *deskey
= res
.cryptkeyres_u
.deskey
;
133 key_decryptsession (char *remotename
, des_block
*deskey
)
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
))
143 if (res
.status
!= KEY_SUCCESS
)
145 debug ("decrypt status is nonzero");
148 *deskey
= res
.cryptkeyres_u
.deskey
;
153 key_encryptsession_pk (char *remotename
, netobj
*remotekey
,
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
))
166 if (res
.status
!= KEY_SUCCESS
)
168 debug ("encrypt status is nonzero");
171 *deskey
= res
.cryptkeyres_u
.deskey
;
176 key_decryptsession_pk (char *remotename
, netobj
*remotekey
,
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
))
189 if (res
.status
!= KEY_SUCCESS
)
191 debug ("decrypt status is nonzero");
194 *deskey
= res
.cryptkeyres_u
.deskey
;
199 key_gendes (des_block
*key
)
201 struct sockaddr_in sin
;
206 sin
.sin_family
= AF_INET
;
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
,
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
);
221 if (stat
!= RPC_SUCCESS
)
228 key_setnet (struct key_netstarg
*arg
)
232 if (!key_call ((u_long
) KEY_NET_PUT
, (xdrproc_t
) xdr_key_netstarg
,
233 (char *) arg
,(xdrproc_t
) xdr_keystatus
, (char *) &status
))
236 if (status
!= KEY_SUCCESS
)
238 debug ("key_setnet status is nonzero");
245 key_get_conv (char *pkey
, des_block
*deskey
)
249 if (!key_call ((u_long
) KEY_GET_CONV
, (xdrproc_t
) xdr_keybuf
, pkey
,
250 (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
253 if (res
.status
!= KEY_SUCCESS
)
255 debug ("get_conv status is nonzero");
258 *deskey
= res
.cryptkeyres_u
.deskey
;
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;
277 key_call_keyenvoy (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
278 xdrproc_t xdr_rslt
, char *rslt
)
284 sigset_t oldmask
, mask
;
290 static char MESSENGER
[] = "/usr/etc/keyenvoy";
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.
305 __setreuid (euid
, ruid
);
306 pid
= _openchild (MESSENGER
, &fargs
, &frslt
);
307 __setreuid (ruid
, euid
);
310 debug ("open_streams");
311 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
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
))
324 if (success
&& !(*xdr_rslt
) (&xdrrslt
, rslt
))
332 if (__wait4 (pid
, &status
, 0, NULL
) < 0)
337 if (errno
== ECHILD
|| errno
== ESRCH
)
343 if (status
.w_retcode
)
348 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
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.
365 getkeyserv_handle (int vers
)
367 struct key_call_private
*kcp
= key_call_private_main
;
368 struct timeval wait_time
;
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
;
386 /* if pid has changed, destroy client and rebuild */
387 if (kcp
->client
!= NULL
&& kcp
->pid
!= __getpid ())
389 clnt_destroy (kcp
->client
);
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
);
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
);
418 return ((CLIENT
*) NULL
);
421 /* Change the version number to the new one */
422 clnt_control (kcp
->client
, CLSET_VERS
, (void *)&vers
);
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
);
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
,
447 if (clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
))
448 fcntl (fd
, F_SETFD
, 1); /* make it "close on exec" */
453 /* returns 0 on failure, 1 on success */
456 key_call_socket (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
457 xdrproc_t xdr_rslt
, char *rslt
)
460 struct timeval wait_time
;
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 */
469 clnt
= getkeyserv_handle(1); /* talk to version 1 */
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
)
481 __libc_lock_unlock (keycall_lock
);
487 /* returns 0 on failure, 1 on success */
490 key_call_door (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
491 xdrproc_t xdr_rslt
, char *rslt
)
500 if ((fd
= open("/var/run/keyservdoor", O_RDONLY
)) < 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
)
509 xdrmem_create (&xdrs
, &data_ptr
[2 * sizeof (u_long
)], data_len
, XDR_ENCODE
);
510 if (!xdr_arg (&xdrs
, arg
))
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
;
526 args
.rsize
= sizeof (res
);
528 if (__door_call (fd
, &args
) < 0)
534 memcpy (&data_len
, args
.data_ptr
, sizeof (u_long
));
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
))
552 /* returns 0 on failure, 1 on success */
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;
560 static int use_doors
= 1;
563 if (proc
== KEY_ENCRYPT_PK
&& __key_encryptsession_pk_LOCAL
)
566 res
= (*__key_encryptsession_pk_LOCAL
) (__geteuid (), arg
);
567 *(cryptkeyres
*) rslt
= *res
;
570 else if (proc
== KEY_DECRYPT_PK
&& __key_decryptsession_pk_LOCAL
)
573 res
= (*__key_decryptsession_pk_LOCAL
) (__geteuid (), arg
);
574 *(cryptkeyres
*) rslt
= *res
;
577 else if (proc
== KEY_GEN
&& __key_gendes_LOCAL
)
580 res
= (*__key_gendes_LOCAL
) (__geteuid (), 0);
581 *(des_block
*) rslt
= *res
;
588 if (key_call_door (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
595 if (key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
599 return key_call_keyenvoy (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);