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) 1986-1991 by Sun Microsystems Inc.
32 * @(#)key_call.c 1.25 94/04/24 SMI
33 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.16 2006/02/27 22:10:59 deischen Exp $
34 * $DragonFly: src/lib/libc/rpc/key_call.c,v 1.6 2005/11/13 12:27:04 swildner Exp $
38 * key_call.c, Interface to keyserver
40 * setsecretkey(key) - set your secret key
41 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
42 * decryptsessionkey(agent, deskey) - decrypt ditto
43 * gendeskey(deskey) - generate a secure des key
46 #include "namespace.h"
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>
63 #include "un-namespace.h"
67 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
68 #define KEY_NRETRY 12 /* number of retries */
71 #define debug(msg) fprintf(stderr, "%s\n", msg);
77 * Hack to allow the keyserver to use AUTH_DES (for authenticated
78 * NIS+ calls, for example). The only functions that get called
79 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
81 * The approach is to have the keyserver fill in pointers to local
82 * implementations of these functions, and to call those in key_call().
85 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
86 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
87 des_block *(*__key_gendes_LOCAL)() = 0;
89 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
92 key_setsecret(const char *secretkey)
96 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
98 (xdrproc_t)xdr_keystatus, &status)) {
101 if (status != KEY_SUCCESS) {
102 debug("set status is nonzero");
109 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
110 * stored for the caller's effective uid; it returns 0 otherwise
112 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
113 * be using it, because it allows them to get the user's secret key.
117 key_secretkey_is_set(void)
119 struct key_netstres kres;
121 memset((void*)&kres, 0, sizeof (kres));
122 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
123 (xdrproc_t)xdr_key_netstres, &kres) &&
124 (kres.status == KEY_SUCCESS) &&
125 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
126 /* avoid leaving secret key in memory */
127 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
134 key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
139 arg.remotename = remotename;
140 arg.remotekey = *remotekey;
141 arg.deskey = *deskey;
142 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
143 (xdrproc_t)xdr_cryptkeyres, &res)) {
146 if (res.status != KEY_SUCCESS) {
147 debug("encrypt status is nonzero");
150 *deskey = res.cryptkeyres_u.deskey;
155 key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
160 arg.remotename = remotename;
161 arg.remotekey = *remotekey;
162 arg.deskey = *deskey;
163 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
164 (xdrproc_t)xdr_cryptkeyres, &res)) {
167 if (res.status != KEY_SUCCESS) {
168 debug("decrypt status is nonzero");
171 *deskey = res.cryptkeyres_u.deskey;
176 key_encryptsession(const char *remotename, des_block *deskey)
181 arg.remotename = (char *) remotename;
182 arg.deskey = *deskey;
183 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
184 (xdrproc_t)xdr_cryptkeyres, &res)) {
187 if (res.status != KEY_SUCCESS) {
188 debug("encrypt status is nonzero");
191 *deskey = res.cryptkeyres_u.deskey;
196 key_decryptsession(const char *remotename, des_block *deskey)
201 arg.remotename = (char *) remotename;
202 arg.deskey = *deskey;
203 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
204 (xdrproc_t)xdr_cryptkeyres, &res)) {
207 if (res.status != KEY_SUCCESS) {
208 debug("decrypt status is nonzero");
211 *deskey = res.cryptkeyres_u.deskey;
216 key_gendes(des_block *key)
218 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
219 (xdrproc_t)xdr_des_block, key)) {
226 key_setnet(struct key_netstarg *arg)
231 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
232 (xdrproc_t)xdr_keystatus, &status)){
236 if (status != KEY_SUCCESS) {
237 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, &res)) {
253 if (res.status != KEY_SUCCESS) {
254 debug("get_conv status is nonzero");
257 *deskey = res.cryptkeyres_u.deskey;
261 struct key_call_private {
262 CLIENT *client; /* Client handle */
263 pid_t pid; /* process-id at moment of creation */
264 uid_t uid; /* user-id at last authorization */
266 static struct key_call_private *key_call_private_main = NULL;
269 key_call_destroy(void *vp)
271 struct key_call_private *kcp = (struct key_call_private *)vp;
275 clnt_destroy(kcp->client);
281 * Keep the handle cached. This call may be made quite often.
284 getkeyserv_handle(int vers)
287 struct netconfig *nconf;
288 struct netconfig *tpconf;
289 struct key_call_private *kcp = key_call_private_main;
290 struct timeval wait_time;
294 static thread_key_t key_call_key;
296 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
297 #define TOTAL_TRIES 5 /* Number of tries */
299 if ((main_thread = thr_main())) {
300 kcp = key_call_private_main;
302 if (key_call_key == 0) {
303 mutex_lock(&tsd_lock);
304 if (key_call_key == 0)
305 thr_keycreate(&key_call_key, key_call_destroy);
306 mutex_unlock(&tsd_lock);
308 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
311 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
316 key_call_private_main = kcp;
318 thr_setspecific(key_call_key, (void *) kcp);
322 /* if pid has changed, destroy client and rebuild */
323 if (kcp->client != NULL && kcp->pid != getpid()) {
324 clnt_destroy(kcp->client);
328 if (kcp->client != NULL) {
329 /* if uid has changed, build client handle again */
330 if (kcp->uid != geteuid()) {
331 kcp->uid = geteuid();
332 auth_destroy(kcp->client->cl_auth);
333 kcp->client->cl_auth =
334 authsys_create("", kcp->uid, 0, 0, NULL);
335 if (kcp->client->cl_auth == NULL) {
336 clnt_destroy(kcp->client);
341 /* Change the version number to the new one */
342 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
343 return (kcp->client);
345 if (!(localhandle = setnetconfig())) {
351 endnetconfig(localhandle);
354 while ((nconf = getnetconfig(localhandle)) != NULL) {
355 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
357 * We use COTS_ORD here so that the caller can
358 * find out immediately if the server is dead.
360 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
361 kcp->client = clnt_tp_create(u.nodename,
362 KEY_PROG, vers, nconf);
370 if ((kcp->client == NULL) && (tpconf))
371 /* Now, try the CLTS or COTS loopback transport */
372 kcp->client = clnt_tp_create(u.nodename,
373 KEY_PROG, vers, tpconf);
374 endnetconfig(localhandle);
376 if (kcp->client == NULL) {
379 kcp->uid = geteuid();
381 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
382 if (kcp->client->cl_auth == NULL) {
383 clnt_destroy(kcp->client);
388 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
389 wait_time.tv_usec = 0;
390 clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
392 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
393 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
395 return (kcp->client);
398 /* returns 0 on failure, 1 on success */
401 key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt,
405 struct timeval wait_time;
407 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
409 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
410 *(cryptkeyres*)rslt = *res;
412 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
414 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
415 *(cryptkeyres*)rslt = *res;
417 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
419 res = (*__key_gendes_LOCAL)(geteuid(), 0);
420 *(des_block*)rslt = *res;
424 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
425 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
426 (proc == KEY_GET_CONV))
427 clnt = getkeyserv_handle(2); /* talk to version 2 */
429 clnt = getkeyserv_handle(1); /* talk to version 1 */
435 wait_time.tv_sec = TOTAL_TIMEOUT;
436 wait_time.tv_usec = 0;
438 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
439 wait_time) == RPC_SUCCESS) {