2 * Copyright (c) 2010, Oracle America, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * The original source is from the RPCSRC 4.0 package from Sun Microsystems.
33 * The Interface to keyserver protocoll 2, RPC over AF_UNIX and Linux/doors
34 * was added by Thorsten Kukuk <kukuk@suse.de>
35 * Since the Linux/doors project was stopped, I doubt that this code will
36 * ever be useful <kukuk@suse.de>.
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <rpc/key_prot.h>
51 #include <libc-lock.h>
52 #include <shlib-compat.h>
54 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
55 #define KEY_NRETRY 12 /* number of retries */
57 #define debug(msg) /* turn off debugging */
60 extern int _openchild (const char *command
, FILE **fto
, FILE **ffrom
);
63 static int key_call (u_long
, xdrproc_t xdr_arg
, char *,
64 xdrproc_t xdr_rslt
, char *) internal_function
;
66 static const struct timeval trytimeout
= {KEY_TIMEOUT
, 0};
67 static const struct timeval tottimeout
= {KEY_TIMEOUT
*KEY_NRETRY
, 0};
70 key_setsecret (char *secretkey
)
74 if (!key_call ((u_long
) KEY_SET
, (xdrproc_t
) xdr_keybuf
, secretkey
,
75 (xdrproc_t
) xdr_keystatus
, (char *) &status
))
77 if (status
!= KEY_SUCCESS
)
79 debug ("set status is nonzero");
84 libc_hidden_nolink_sunrpc (key_setsecret
, GLIBC_2_1
)
86 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
87 * stored for the caller's effective uid; it returns 0 otherwise
89 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
90 * be using it, because it allows them to get the user's secret key.
93 key_secretkey_is_set (void)
95 struct key_netstres kres
;
97 memset (&kres
, 0, sizeof (kres
));
98 if (key_call ((u_long
) KEY_NET_GET
, (xdrproc_t
) xdr_void
,
99 (char *) NULL
, (xdrproc_t
) xdr_key_netstres
,
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
);
110 #ifdef EXPORT_RPC_SYMBOLS
111 libc_hidden_def (key_secretkey_is_set
)
113 libc_hidden_nolink_sunrpc (key_secretkey_is_set
, GLIBC_2_1
)
117 key_encryptsession (char *remotename
, des_block
*deskey
)
122 arg
.remotename
= remotename
;
123 arg
.deskey
= *deskey
;
124 if (!key_call ((u_long
) KEY_ENCRYPT
, (xdrproc_t
) xdr_cryptkeyarg
,
125 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
,
129 if (res
.status
!= KEY_SUCCESS
)
131 debug ("encrypt status is nonzero");
134 *deskey
= res
.cryptkeyres_u
.deskey
;
137 libc_hidden_nolink_sunrpc (key_encryptsession
, GLIBC_2_1
)
140 key_decryptsession (char *remotename
, des_block
*deskey
)
145 arg
.remotename
= remotename
;
146 arg
.deskey
= *deskey
;
147 if (!key_call ((u_long
) KEY_DECRYPT
, (xdrproc_t
) xdr_cryptkeyarg
,
148 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
,
151 if (res
.status
!= KEY_SUCCESS
)
153 debug ("decrypt status is nonzero");
156 *deskey
= res
.cryptkeyres_u
.deskey
;
159 libc_hidden_nolink_sunrpc (key_decryptsession
, GLIBC_2_1
)
162 key_encryptsession_pk (char *remotename
, netobj
*remotekey
,
168 arg
.remotename
= remotename
;
169 arg
.remotekey
= *remotekey
;
170 arg
.deskey
= *deskey
;
171 if (!key_call ((u_long
) KEY_ENCRYPT_PK
, (xdrproc_t
) xdr_cryptkeyarg2
,
172 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
,
176 if (res
.status
!= KEY_SUCCESS
)
178 debug ("encrypt status is nonzero");
181 *deskey
= res
.cryptkeyres_u
.deskey
;
184 libc_hidden_nolink_sunrpc (key_encryptsession_pk
, GLIBC_2_1
)
187 key_decryptsession_pk (char *remotename
, netobj
*remotekey
,
193 arg
.remotename
= remotename
;
194 arg
.remotekey
= *remotekey
;
195 arg
.deskey
= *deskey
;
196 if (!key_call ((u_long
) KEY_DECRYPT_PK
, (xdrproc_t
) xdr_cryptkeyarg2
,
197 (char *) &arg
, (xdrproc_t
) xdr_cryptkeyres
,
201 if (res
.status
!= KEY_SUCCESS
)
203 debug ("decrypt status is nonzero");
206 *deskey
= res
.cryptkeyres_u
.deskey
;
209 libc_hidden_nolink_sunrpc (key_decryptsession_pk
, GLIBC_2_1
)
212 key_gendes (des_block
*key
)
214 struct sockaddr_in sin
;
219 sin
.sin_family
= AF_INET
;
221 sin
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
222 memset (sin
.sin_zero
, 0, sizeof (sin
.sin_zero
));
223 socket
= RPC_ANYSOCK
;
224 client
= clntudp_bufcreate (&sin
, (u_long
) KEY_PROG
, (u_long
) KEY_VERS
,
225 trytimeout
, &socket
, RPCSMALLMSGSIZE
,
230 stat
= clnt_call (client
, KEY_GEN
, (xdrproc_t
) xdr_void
, NULL
,
231 (xdrproc_t
) xdr_des_block
, (caddr_t
) key
,
233 clnt_destroy (client
);
235 if (stat
!= RPC_SUCCESS
)
240 #ifdef EXPORT_RPC_SYMBOLS
241 libc_hidden_def (key_gendes
)
243 libc_hidden_nolink_sunrpc (key_gendes
, GLIBC_2_1
)
247 key_setnet (struct key_netstarg
*arg
)
251 if (!key_call ((u_long
) KEY_NET_PUT
, (xdrproc_t
) xdr_key_netstarg
,
252 (char *) arg
,(xdrproc_t
) xdr_keystatus
,
256 if (status
!= KEY_SUCCESS
)
258 debug ("key_setnet status is nonzero");
263 libc_hidden_nolink_sunrpc (key_setnet
, GLIBC_2_1
)
266 key_get_conv (char *pkey
, des_block
*deskey
)
270 if (!key_call ((u_long
) KEY_GET_CONV
, (xdrproc_t
) xdr_keybuf
, pkey
,
271 (xdrproc_t
) xdr_cryptkeyres
, (char *) &res
))
274 if (res
.status
!= KEY_SUCCESS
)
276 debug ("get_conv status is nonzero");
279 *deskey
= res
.cryptkeyres_u
.deskey
;
282 libc_hidden_nolink_sunrpc (key_get_conv
, GLIBC_2_1
)
285 * Hack to allow the keyserver to use AUTH_DES (for authenticated
286 * NIS+ calls, for example). The only functions that get called
287 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
289 * The approach is to have the keyserver fill in pointers to local
290 * implementations of these functions, and to call those in key_call().
293 cryptkeyres
*(*__key_encryptsession_pk_LOCAL
) (uid_t
, char *);
294 cryptkeyres
*(*__key_decryptsession_pk_LOCAL
) (uid_t
, char *);
295 des_block
*(*__key_gendes_LOCAL
) (uid_t
, char *);
300 key_call_keyenvoy (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
301 xdrproc_t xdr_rslt
, char *rslt
)
307 sigset_t oldmask
, mask
;
313 static const char MESSENGER
[] = "/usr/etc/keyenvoy";
317 sigaddset (&mask
, SIGCHLD
);
318 __sigprocmask (SIG_BLOCK
, &mask
, &oldmask
);
321 * We are going to exec a set-uid program which makes our effective uid
322 * zero, and authenticates us with our real uid. We need to make the
323 * effective uid be the real uid for the setuid program, and
324 * the real uid be the effective uid so that we can change things back.
328 __setreuid (euid
, ruid
);
329 pid
= _openchild (MESSENGER
, &fargs
, &frslt
);
330 __setreuid (ruid
, euid
);
333 debug ("open_streams");
334 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
337 xdrstdio_create (&xdrargs
, fargs
, XDR_ENCODE
);
338 xdrstdio_create (&xdrrslt
, frslt
, XDR_DECODE
);
340 if (!xdr_u_long (&xdrargs
, &proc
) || !(*xdr_arg
) (&xdrargs
, arg
))
347 if (success
&& !(*xdr_rslt
) (&xdrrslt
, rslt
))
355 if (__wait4 (pid
, &status
, 0, NULL
) < 0)
360 if (errno
== ECHILD
|| errno
== ESRCH
)
371 __sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
377 struct key_call_private
{
378 CLIENT
*client
; /* Client handle */
379 pid_t pid
; /* process-id at moment of creation */
380 uid_t uid
; /* user-id at last authorization */
382 #ifdef _RPC_THREAD_SAFE_
383 #define key_call_private_main RPC_THREAD_VARIABLE(key_call_private_s)
385 static struct key_call_private
*key_call_private_main
;
387 __libc_lock_define_initialized (static, keycall_lock
)
390 * Keep the handle cached. This call may be made quite often.
393 getkeyserv_handle (int vers
)
395 struct key_call_private
*kcp
= key_call_private_main
;
396 struct timeval wait_time
;
398 struct sockaddr_un name
;
399 socklen_t namelen
= sizeof(struct sockaddr_un
);
401 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
402 #define TOTAL_TRIES 5 /* Number of tries */
404 if (kcp
== (struct key_call_private
*)NULL
)
406 kcp
= (struct key_call_private
*)malloc (sizeof (*kcp
));
407 if (kcp
== (struct key_call_private
*)NULL
)
408 return (CLIENT
*) NULL
;
410 key_call_private_main
= kcp
;
414 /* if pid has changed, destroy client and rebuild */
415 if (kcp
->client
!= NULL
&& kcp
->pid
!= __getpid ())
417 auth_destroy (kcp
->client
->cl_auth
);
418 clnt_destroy (kcp
->client
);
422 if (kcp
->client
!= NULL
)
424 /* if other side closed socket, build handle again */
425 clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
);
426 if (__getpeername (fd
,(struct sockaddr
*)&name
,&namelen
) == -1)
428 auth_destroy (kcp
->client
->cl_auth
);
429 clnt_destroy (kcp
->client
);
434 if (kcp
->client
!= NULL
)
436 /* if uid has changed, build client handle again */
437 if (kcp
->uid
!= __geteuid ())
439 kcp
->uid
= __geteuid ();
440 auth_destroy (kcp
->client
->cl_auth
);
441 kcp
->client
->cl_auth
=
442 authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
443 if (kcp
->client
->cl_auth
== NULL
)
445 clnt_destroy (kcp
->client
);
447 return ((CLIENT
*) NULL
);
450 /* Change the version number to the new one */
451 clnt_control (kcp
->client
, CLSET_VERS
, (void *)&vers
);
455 if ((kcp
->client
== (CLIENT
*) NULL
))
456 /* Use the AF_UNIX transport */
457 kcp
->client
= clnt_create ("/var/run/keyservsock", KEY_PROG
, vers
, "unix");
459 if (kcp
->client
== (CLIENT
*) NULL
)
460 return (CLIENT
*) NULL
;
462 kcp
->uid
= __geteuid ();
463 kcp
->pid
= __getpid ();
464 kcp
->client
->cl_auth
= authunix_create ((char *)"", kcp
->uid
, 0, 0, NULL
);
465 if (kcp
->client
->cl_auth
== NULL
)
467 clnt_destroy (kcp
->client
);
469 return (CLIENT
*) NULL
;
472 wait_time
.tv_sec
= TOTAL_TIMEOUT
/TOTAL_TRIES
;
473 wait_time
.tv_usec
= 0;
474 clnt_control (kcp
->client
, CLSET_RETRY_TIMEOUT
,
476 if (clnt_control (kcp
->client
, CLGET_FD
, (char *)&fd
))
477 __fcntl (fd
, F_SETFD
, FD_CLOEXEC
); /* make it "close on exec" */
482 /* returns 0 on failure, 1 on success */
485 key_call_socket (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
486 xdrproc_t xdr_rslt
, char *rslt
)
489 struct timeval wait_time
;
492 __libc_lock_lock (keycall_lock
);
493 if ((proc
== KEY_ENCRYPT_PK
) || (proc
== KEY_DECRYPT_PK
) ||
494 (proc
== KEY_NET_GET
) || (proc
== KEY_NET_PUT
) ||
495 (proc
== KEY_GET_CONV
))
496 clnt
= getkeyserv_handle(2); /* talk to version 2 */
498 clnt
= getkeyserv_handle(1); /* talk to version 1 */
502 wait_time
.tv_sec
= TOTAL_TIMEOUT
;
503 wait_time
.tv_usec
= 0;
505 if (clnt_call (clnt
, proc
, xdr_arg
, arg
, xdr_rslt
, rslt
,
506 wait_time
) == RPC_SUCCESS
)
510 __libc_lock_unlock (keycall_lock
);
516 /* returns 0 on failure, 1 on success */
519 key_call (u_long proc
, xdrproc_t xdr_arg
, char *arg
,
520 xdrproc_t xdr_rslt
, char *rslt
)
523 static int use_keyenvoy
;
526 if (proc
== KEY_ENCRYPT_PK
&& __key_encryptsession_pk_LOCAL
)
529 res
= (*__key_encryptsession_pk_LOCAL
) (__geteuid (), arg
);
530 *(cryptkeyres
*) rslt
= *res
;
533 else if (proc
== KEY_DECRYPT_PK
&& __key_decryptsession_pk_LOCAL
)
536 res
= (*__key_decryptsession_pk_LOCAL
) (__geteuid (), arg
);
537 *(cryptkeyres
*) rslt
= *res
;
540 else if (proc
== KEY_GEN
&& __key_gendes_LOCAL
)
543 res
= (*__key_gendes_LOCAL
) (__geteuid (), 0);
544 *(des_block
*) rslt
= *res
;
549 return key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);
553 if (key_call_socket (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
))
557 return key_call_keyenvoy (proc
, xdr_arg
, arg
, xdr_rslt
, rslt
);
561 #ifdef _RPC_THREAD_SAFE_
563 __rpc_thread_key_cleanup (void)
565 struct key_call_private
*kcp
= RPC_THREAD_VARIABLE(key_call_private_s
);
569 if (kcp
->client
->cl_auth
)
570 auth_destroy (kcp
->client
->cl_auth
);
571 clnt_destroy(kcp
->client
);
576 #endif /* _RPC_THREAD_SAFE_ */