2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 //#include <sys/cdefs.h>
36 * key_call.c, Interface to keyserver
38 * setsecretkey(key) - set your secret key
39 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
40 * decryptsessionkey(agent, deskey) - decrypt ditto
41 * gendeskey(deskey) - generate a secure des key
47 #include <reentrant.h>
54 #include <rpc/auth_unix.h>
55 #include <rpc/key_prot.h>
57 #include <netconfig.h>
58 #include <sys/utsname.h>
62 #include <sys/fcntl.h>
65 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
66 #define KEY_NRETRY 12 /* number of retries */
69 #define debug(msg) (void) fprintf(stderr, "%s\n", msg);
75 * Hack to allow the keyserver to use AUTH_DES (for authenticated
76 * NIS+ calls, for example). The only functions that get called
77 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
79 * The approach is to have the keyserver fill in pointers to local
80 * implementations of these functions, and to call those in key_call().
83 cryptkeyres
*(*__key_encryptsession_pk_LOCAL
)() = 0;
84 cryptkeyres
*(*__key_decryptsession_pk_LOCAL
)() = 0;
85 des_block
*(*__key_gendes_LOCAL
)() = 0;
87 static int key_call( u_long
, xdrproc_t
, void *, xdrproc_t
, void *);
90 key_setsecret(secretkey
)
91 const char *secretkey
;
95 if (!key_call((u_long
) KEY_SET
, (xdrproc_t
)xdr_keybuf
,
97 (xdrproc_t
)xdr_keystatus
, &status
)) {
100 if (status
!= KEY_SUCCESS
) {
101 debug("set status is nonzero");
108 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
109 * stored for the caller's effective uid; it returns 0 otherwise
111 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
112 * be using it, because it allows them to get the user's secret key.
116 key_secretkey_is_set(void)
118 struct key_netstres kres
;
120 memset((void*)&kres
, 0, sizeof (kres
));
121 if (key_call((u_long
) KEY_NET_GET
, (xdrproc_t
)xdr_void
, NULL
,
122 (xdrproc_t
)xdr_key_netstres
, &kres
) &&
123 (kres
.status
== KEY_SUCCESS
) &&
124 (kres
.key_netstres_u
.knet
.st_priv_key
[0] != 0)) {
125 /* avoid leaving secret key in memory */
126 memset(kres
.key_netstres_u
.knet
.st_priv_key
, 0, HEXKEYBYTES
);
133 key_encryptsession_pk(remotename
, remotekey
, deskey
)
141 arg
.remotename
= remotename
;
142 arg
.remotekey
= *remotekey
;
143 arg
.deskey
= *deskey
;
144 if (!key_call((u_long
)KEY_ENCRYPT_PK
, (xdrproc_t
)xdr_cryptkeyarg2
, &arg
,
145 (xdrproc_t
)xdr_cryptkeyres
, &res
)) {
148 if (res
.status
!= KEY_SUCCESS
) {
149 debug("encrypt status is nonzero");
152 *deskey
= res
.cryptkeyres_u
.deskey
;
157 key_decryptsession_pk(remotename
, remotekey
, deskey
)
165 arg
.remotename
= remotename
;
166 arg
.remotekey
= *remotekey
;
167 arg
.deskey
= *deskey
;
168 if (!key_call((u_long
)KEY_DECRYPT_PK
, (xdrproc_t
)xdr_cryptkeyarg2
, &arg
,
169 (xdrproc_t
)xdr_cryptkeyres
, &res
)) {
172 if (res
.status
!= KEY_SUCCESS
) {
173 debug("decrypt status is nonzero");
176 *deskey
= res
.cryptkeyres_u
.deskey
;
181 key_encryptsession(remotename
, deskey
)
182 const char *remotename
;
188 arg
.remotename
= (char *) remotename
;
189 arg
.deskey
= *deskey
;
190 if (!key_call((u_long
)KEY_ENCRYPT
, (xdrproc_t
)xdr_cryptkeyarg
, &arg
,
191 (xdrproc_t
)xdr_cryptkeyres
, &res
)) {
194 if (res
.status
!= KEY_SUCCESS
) {
195 debug("encrypt status is nonzero");
198 *deskey
= res
.cryptkeyres_u
.deskey
;
203 key_decryptsession(remotename
, deskey
)
204 const char *remotename
;
210 arg
.remotename
= (char *) remotename
;
211 arg
.deskey
= *deskey
;
212 if (!key_call((u_long
)KEY_DECRYPT
, (xdrproc_t
)xdr_cryptkeyarg
, &arg
,
213 (xdrproc_t
)xdr_cryptkeyres
, &res
)) {
216 if (res
.status
!= KEY_SUCCESS
) {
217 debug("decrypt status is nonzero");
220 *deskey
= res
.cryptkeyres_u
.deskey
;
228 if (!key_call((u_long
)KEY_GEN
, (xdrproc_t
)xdr_void
, NULL
,
229 (xdrproc_t
)xdr_des_block
, key
)) {
237 struct key_netstarg
*arg
;
242 if (!key_call((u_long
) KEY_NET_PUT
, (xdrproc_t
)xdr_key_netstarg
, arg
,
243 (xdrproc_t
)xdr_keystatus
, &status
)){
247 if (status
!= KEY_SUCCESS
) {
248 debug("key_setnet status is nonzero");
256 key_get_conv(pkey
, deskey
)
262 if (!key_call((u_long
) KEY_GET_CONV
, (xdrproc_t
)xdr_keybuf
, pkey
,
263 (xdrproc_t
)xdr_cryptkeyres
, &res
)) {
266 if (res
.status
!= KEY_SUCCESS
) {
267 debug("get_conv status is nonzero");
270 *deskey
= res
.cryptkeyres_u
.deskey
;
274 struct key_call_private
{
275 CLIENT
*client
; /* Client handle */
276 pid_t pid
; /* process-id at moment of creation */
277 uid_t uid
; /* user-id at last authorization */
279 static struct key_call_private
*key_call_private_main
= NULL
;
282 key_call_destroy(void *vp
)
284 struct key_call_private
*kcp
= (struct key_call_private
*)vp
;
288 clnt_destroy(kcp
->client
);
294 * Keep the handle cached. This call may be made quite often.
297 getkeyserv_handle(vers
)
301 struct netconfig
*nconf
;
302 struct netconfig
*tpconf
;
303 struct key_call_private
*kcp
= key_call_private_main
;
304 struct timeval wait_time
;
307 extern thread_key_t key_call_key
;
308 extern mutex_t tsd_lock
;
310 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
311 #define TOTAL_TRIES 5 /* Number of tries */
313 if (key_call_key
== -1) {
314 mutex_lock(&tsd_lock
);
315 if (key_call_key
== -1)
316 thr_keycreate(&key_call_key
, key_call_destroy
);
317 mutex_unlock(&tsd_lock
);
319 kcp
= (struct key_call_private
*)thr_getspecific(key_call_key
);
320 if (kcp
== (struct key_call_private
*)NULL
) {
321 kcp
= (struct key_call_private
*)malloc(sizeof (*kcp
));
322 if (kcp
== (struct key_call_private
*)NULL
) {
323 return ((CLIENT
*) NULL
);
325 thr_setspecific(key_call_key
, (void *) kcp
);
329 /* if pid has changed, destroy client and rebuild */
330 if (kcp
->client
!= NULL
&& kcp
->pid
!= getpid()) {
331 clnt_destroy(kcp
->client
);
335 if (kcp
->client
!= NULL
) {
336 /* if uid has changed, build client handle again */
337 if (kcp
->uid
!= geteuid()) {
338 kcp
->uid
= geteuid();
339 auth_destroy(kcp
->client
->cl_auth
);
340 kcp
->client
->cl_auth
=
341 authsys_create("", kcp
->uid
, 0, 0, NULL
);
342 if (kcp
->client
->cl_auth
== NULL
) {
343 clnt_destroy(kcp
->client
);
345 return ((CLIENT
*) NULL
);
348 /* Change the version number to the new one */
349 clnt_control(kcp
->client
, CLSET_VERS
, (void *)&vers
);
350 return (kcp
->client
);
352 if (!(localhandle
= setnetconfig())) {
353 return ((CLIENT
*) NULL
);
356 #if defined(__FreeBSD__)
364 #error Unknown architecture!
368 endnetconfig(localhandle
);
369 return ((CLIENT
*) NULL
);
371 while ((nconf
= getnetconfig(localhandle
)) != NULL
) {
372 if (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0) {
374 * We use COTS_ORD here so that the caller can
375 * find out immediately if the server is dead.
377 if (nconf
->nc_semantics
== NC_TPI_COTS_ORD
) {
378 kcp
->client
= clnt_tp_create(u
.nodename
,
379 KEY_PROG
, vers
, nconf
);
387 if ((kcp
->client
== (CLIENT
*) NULL
) && (tpconf
))
388 /* Now, try the CLTS or COTS loopback transport */
389 kcp
->client
= clnt_tp_create(u
.nodename
,
390 KEY_PROG
, vers
, tpconf
);
391 endnetconfig(localhandle
);
393 if (kcp
->client
== (CLIENT
*) NULL
) {
394 return ((CLIENT
*) NULL
);
396 kcp
->uid
= geteuid();
398 kcp
->client
->cl_auth
= authsys_create("", kcp
->uid
, 0, 0, NULL
);
399 if (kcp
->client
->cl_auth
== NULL
) {
400 clnt_destroy(kcp
->client
);
402 return ((CLIENT
*) NULL
);
405 wait_time
.tv_sec
= TOTAL_TIMEOUT
/TOTAL_TRIES
;
406 wait_time
.tv_usec
= 0;
407 (void) clnt_control(kcp
->client
, CLSET_RETRY_TIMEOUT
,
409 if (clnt_control(kcp
->client
, CLGET_FD
, (char *)&fd
))
410 fcntl(fd
, F_SETFD
, 1); /* make it "close on exec" */
412 return (kcp
->client
);
415 /* returns 0 on failure, 1 on success */
418 key_call(proc
, xdr_arg
, arg
, xdr_rslt
, rslt
)
426 struct timeval wait_time
;
428 if (proc
== KEY_ENCRYPT_PK
&& __key_encryptsession_pk_LOCAL
) {
430 res
= (*__key_encryptsession_pk_LOCAL
)(geteuid(), arg
);
431 *(cryptkeyres
*)rslt
= *res
;
433 } else if (proc
== KEY_DECRYPT_PK
&& __key_decryptsession_pk_LOCAL
) {
435 res
= (*__key_decryptsession_pk_LOCAL
)(geteuid(), arg
);
436 *(cryptkeyres
*)rslt
= *res
;
438 } else if (proc
== KEY_GEN
&& __key_gendes_LOCAL
) {
440 res
= (*__key_gendes_LOCAL
)(geteuid(), 0);
441 *(des_block
*)rslt
= *res
;
445 if ((proc
== KEY_ENCRYPT_PK
) || (proc
== KEY_DECRYPT_PK
) ||
446 (proc
== KEY_NET_GET
) || (proc
== KEY_NET_PUT
) ||
447 (proc
== KEY_GET_CONV
))
448 clnt
= getkeyserv_handle(2); /* talk to version 2 */
450 clnt
= getkeyserv_handle(1); /* talk to version 1 */
456 wait_time
.tv_sec
= TOTAL_TIMEOUT
;
457 wait_time
.tv_usec
= 0;
459 if (clnt_call(clnt
, proc
, xdr_arg
, arg
, xdr_rslt
, rslt
,
460 wait_time
) == RPC_SUCCESS
) {