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.
31 * @(#)key_call.c 1.25 94/04/24 SMI
32 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.16 2006/02/27 22:10:59 deischen Exp $
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
44 #include "namespace.h"
45 #include "reentrant.h"
52 #include <rpc/auth_unix.h>
53 #include <rpc/key_prot.h>
55 #include <netconfig.h>
56 #include <sys/utsname.h>
59 #include <sys/fcntl.h>
60 #include "un-namespace.h"
64 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
65 #define KEY_NRETRY 12 /* number of retries */
68 #define debug(msg) fprintf(stderr, "%s\n", msg);
74 * Hack to allow the keyserver to use AUTH_DES (for authenticated
75 * NIS+ calls, for example). The only functions that get called
76 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
78 * The approach is to have the keyserver fill in pointers to local
79 * implementations of these functions, and to call those in key_call().
82 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = NULL;
83 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = NULL;
84 des_block *(*__key_gendes_LOCAL)() = NULL;
86 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
89 key_setsecret(const char *secretkey)
93 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
95 (xdrproc_t)xdr_keystatus, &status)) {
98 if (status != KEY_SUCCESS) {
99 debug("set status is nonzero");
106 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
107 * stored for the caller's effective uid; it returns 0 otherwise
109 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
110 * be using it, because it allows them to get the user's secret key.
114 key_secretkey_is_set(void)
116 struct key_netstres kres;
118 memset((void*)&kres, 0, sizeof (kres));
119 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
120 (xdrproc_t)xdr_key_netstres, &kres) &&
121 (kres.status == KEY_SUCCESS) &&
122 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
123 /* avoid leaving secret key in memory */
124 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
131 key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
136 arg.remotename = remotename;
137 arg.remotekey = *remotekey;
138 arg.deskey = *deskey;
139 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
140 (xdrproc_t)xdr_cryptkeyres, &res)) {
143 if (res.status != KEY_SUCCESS) {
144 debug("encrypt status is nonzero");
147 *deskey = res.cryptkeyres_u.deskey;
152 key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
157 arg.remotename = remotename;
158 arg.remotekey = *remotekey;
159 arg.deskey = *deskey;
160 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
161 (xdrproc_t)xdr_cryptkeyres, &res)) {
164 if (res.status != KEY_SUCCESS) {
165 debug("decrypt status is nonzero");
168 *deskey = res.cryptkeyres_u.deskey;
173 key_encryptsession(const char *remotename, des_block *deskey)
178 arg.remotename = (char *) remotename;
179 arg.deskey = *deskey;
180 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
181 (xdrproc_t)xdr_cryptkeyres, &res)) {
184 if (res.status != KEY_SUCCESS) {
185 debug("encrypt status is nonzero");
188 *deskey = res.cryptkeyres_u.deskey;
193 key_decryptsession(const char *remotename, des_block *deskey)
198 arg.remotename = (char *) remotename;
199 arg.deskey = *deskey;
200 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
201 (xdrproc_t)xdr_cryptkeyres, &res)) {
204 if (res.status != KEY_SUCCESS) {
205 debug("decrypt status is nonzero");
208 *deskey = res.cryptkeyres_u.deskey;
213 key_gendes(des_block *key)
215 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
216 (xdrproc_t)xdr_des_block, key)) {
223 key_setnet(struct key_netstarg *arg)
228 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
229 (xdrproc_t)xdr_keystatus, &status)){
233 if (status != KEY_SUCCESS) {
234 debug("key_setnet status is nonzero");
242 key_get_conv(char *pkey, des_block *deskey)
246 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
247 (xdrproc_t)xdr_cryptkeyres, &res)) {
250 if (res.status != KEY_SUCCESS) {
251 debug("get_conv status is nonzero");
254 *deskey = res.cryptkeyres_u.deskey;
258 struct key_call_private {
259 CLIENT *client; /* Client handle */
260 pid_t pid; /* process-id at moment of creation */
261 uid_t uid; /* user-id at last authorization */
263 static struct key_call_private *key_call_private_main = NULL;
266 key_call_destroy(void *vp)
268 struct key_call_private *kcp = (struct key_call_private *)vp;
272 clnt_destroy(kcp->client);
278 * Keep the handle cached. This call may be made quite often.
281 getkeyserv_handle(int vers)
284 struct netconfig *nconf;
285 struct netconfig *tpconf;
286 struct key_call_private *kcp = key_call_private_main;
287 struct timeval wait_time;
291 static thread_key_t key_call_key;
293 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
294 #define TOTAL_TRIES 5 /* Number of tries */
296 if ((main_thread = thr_main())) {
297 kcp = key_call_private_main;
299 if (key_call_key == 0) {
300 mutex_lock(&tsd_lock);
301 if (key_call_key == 0)
302 thr_keycreate(&key_call_key, key_call_destroy);
303 mutex_unlock(&tsd_lock);
305 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
308 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
313 key_call_private_main = kcp;
315 thr_setspecific(key_call_key, (void *) kcp);
319 /* if pid has changed, destroy client and rebuild */
320 if (kcp->client != NULL && kcp->pid != getpid()) {
321 clnt_destroy(kcp->client);
325 if (kcp->client != NULL) {
326 /* if uid has changed, build client handle again */
327 if (kcp->uid != geteuid()) {
328 kcp->uid = geteuid();
329 auth_destroy(kcp->client->cl_auth);
330 kcp->client->cl_auth =
331 authsys_create("", kcp->uid, 0, 0, NULL);
332 if (kcp->client->cl_auth == NULL) {
333 clnt_destroy(kcp->client);
338 /* Change the version number to the new one */
339 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
340 return (kcp->client);
342 if (!(localhandle = setnetconfig())) {
348 endnetconfig(localhandle);
351 while ((nconf = getnetconfig(localhandle)) != NULL) {
352 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
354 * We use COTS_ORD here so that the caller can
355 * find out immediately if the server is dead.
357 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
358 kcp->client = clnt_tp_create(u.nodename,
359 KEY_PROG, vers, nconf);
367 if ((kcp->client == NULL) && (tpconf))
368 /* Now, try the CLTS or COTS loopback transport */
369 kcp->client = clnt_tp_create(u.nodename,
370 KEY_PROG, vers, tpconf);
371 endnetconfig(localhandle);
373 if (kcp->client == NULL) {
376 kcp->uid = geteuid();
378 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
379 if (kcp->client->cl_auth == NULL) {
380 clnt_destroy(kcp->client);
385 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
386 wait_time.tv_usec = 0;
387 clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
389 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
390 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
392 return (kcp->client);
395 /* returns 0 on failure, 1 on success */
398 key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt,
402 struct timeval wait_time;
404 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
406 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
407 *(cryptkeyres*)rslt = *res;
409 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
411 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
412 *(cryptkeyres*)rslt = *res;
414 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
416 res = (*__key_gendes_LOCAL)(geteuid(), 0);
417 *(des_block*)rslt = *res;
421 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
422 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
423 (proc == KEY_GET_CONV))
424 clnt = getkeyserv_handle(2); /* talk to version 2 */
426 clnt = getkeyserv_handle(1); /* talk to version 1 */
432 wait_time.tv_sec = TOTAL_TIMEOUT;
433 wait_time.tv_usec = 0;
435 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
436 wait_time) == RPC_SUCCESS) {