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.
33 #ident "@(#)key_call.c 1.25 94/04/24 SMI"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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) (void) 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(secretkey)
93 const char *secretkey;
97 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
99 (xdrproc_t)xdr_keystatus, &status)) {
102 if (status != KEY_SUCCESS) {
103 debug("set status is nonzero");
110 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
111 * stored for the caller's effective uid; it returns 0 otherwise
113 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
114 * be using it, because it allows them to get the user's secret key.
118 key_secretkey_is_set(void)
120 struct key_netstres kres;
122 memset((void*)&kres, 0, sizeof (kres));
123 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
124 (xdrproc_t)xdr_key_netstres, &kres) &&
125 (kres.status == KEY_SUCCESS) &&
126 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
127 /* avoid leaving secret key in memory */
128 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
135 key_encryptsession_pk(remotename, remotekey, deskey)
143 arg.remotename = remotename;
144 arg.remotekey = *remotekey;
145 arg.deskey = *deskey;
146 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
147 (xdrproc_t)xdr_cryptkeyres, &res)) {
150 if (res.status != KEY_SUCCESS) {
151 debug("encrypt status is nonzero");
154 *deskey = res.cryptkeyres_u.deskey;
159 key_decryptsession_pk(remotename, remotekey, deskey)
167 arg.remotename = remotename;
168 arg.remotekey = *remotekey;
169 arg.deskey = *deskey;
170 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
171 (xdrproc_t)xdr_cryptkeyres, &res)) {
174 if (res.status != KEY_SUCCESS) {
175 debug("decrypt status is nonzero");
178 *deskey = res.cryptkeyres_u.deskey;
183 key_encryptsession(remotename, deskey)
184 const char *remotename;
190 arg.remotename = (char *) remotename;
191 arg.deskey = *deskey;
192 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
193 (xdrproc_t)xdr_cryptkeyres, &res)) {
196 if (res.status != KEY_SUCCESS) {
197 debug("encrypt status is nonzero");
200 *deskey = res.cryptkeyres_u.deskey;
205 key_decryptsession(remotename, deskey)
206 const char *remotename;
212 arg.remotename = (char *) remotename;
213 arg.deskey = *deskey;
214 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
215 (xdrproc_t)xdr_cryptkeyres, &res)) {
218 if (res.status != KEY_SUCCESS) {
219 debug("decrypt status is nonzero");
222 *deskey = res.cryptkeyres_u.deskey;
230 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
231 (xdrproc_t)xdr_des_block, key)) {
239 struct key_netstarg *arg;
244 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
245 (xdrproc_t)xdr_keystatus, &status)){
249 if (status != KEY_SUCCESS) {
250 debug("key_setnet status is nonzero");
258 key_get_conv(pkey, deskey)
264 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
265 (xdrproc_t)xdr_cryptkeyres, &res)) {
268 if (res.status != KEY_SUCCESS) {
269 debug("get_conv status is nonzero");
272 *deskey = res.cryptkeyres_u.deskey;
276 struct key_call_private {
277 CLIENT *client; /* Client handle */
278 pid_t pid; /* process-id at moment of creation */
279 uid_t uid; /* user-id at last authorization */
281 static struct key_call_private *key_call_private_main = NULL;
282 static thread_key_t key_call_key;
283 static once_t key_call_once = ONCE_INITIALIZER;
284 static int key_call_key_error;
287 key_call_destroy(void *vp)
289 struct key_call_private *kcp = (struct key_call_private *)vp;
293 clnt_destroy(kcp->client);
302 key_call_key_error = thr_keycreate(&key_call_key, key_call_destroy);
306 * Keep the handle cached. This call may be made quite often.
309 getkeyserv_handle(vers)
313 struct netconfig *nconf;
314 struct netconfig *tpconf;
315 struct key_call_private *kcp;
316 struct timeval wait_time;
321 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
322 #define TOTAL_TRIES 5 /* Number of tries */
324 if ((main_thread = thr_main())) {
325 kcp = key_call_private_main;
327 if (thr_once(&key_call_once, key_call_init) != 0 ||
328 key_call_key_error != 0)
329 return ((CLIENT *) NULL);
330 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
332 if (kcp == (struct key_call_private *)NULL) {
333 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
334 if (kcp == (struct key_call_private *)NULL) {
335 return ((CLIENT *) NULL);
338 key_call_private_main = kcp;
340 thr_setspecific(key_call_key, (void *) kcp);
344 /* if pid has changed, destroy client and rebuild */
345 if (kcp->client != NULL && kcp->pid != getpid()) {
346 clnt_destroy(kcp->client);
350 if (kcp->client != NULL) {
351 /* if uid has changed, build client handle again */
352 if (kcp->uid != geteuid()) {
353 kcp->uid = geteuid();
354 auth_destroy(kcp->client->cl_auth);
355 kcp->client->cl_auth =
356 authsys_create("", kcp->uid, 0, 0, NULL);
357 if (kcp->client->cl_auth == NULL) {
358 clnt_destroy(kcp->client);
360 return ((CLIENT *) NULL);
363 /* Change the version number to the new one */
364 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
365 return (kcp->client);
367 if (!(localhandle = setnetconfig())) {
368 return ((CLIENT *) NULL);
371 #if defined(__FreeBSD__)
375 if (_nuname(&u) == -1)
377 if (_uname(&u) == -1)
379 #error Unknown architecture!
383 endnetconfig(localhandle);
384 return ((CLIENT *) NULL);
386 while ((nconf = getnetconfig(localhandle)) != NULL) {
387 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
389 * We use COTS_ORD here so that the caller can
390 * find out immediately if the server is dead.
392 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
393 kcp->client = clnt_tp_create(u.nodename,
394 KEY_PROG, vers, nconf);
402 if ((kcp->client == (CLIENT *) NULL) && (tpconf))
403 /* Now, try the CLTS or COTS loopback transport */
404 kcp->client = clnt_tp_create(u.nodename,
405 KEY_PROG, vers, tpconf);
406 endnetconfig(localhandle);
408 if (kcp->client == (CLIENT *) NULL) {
409 return ((CLIENT *) NULL);
411 kcp->uid = geteuid();
413 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
414 if (kcp->client->cl_auth == NULL) {
415 clnt_destroy(kcp->client);
417 return ((CLIENT *) NULL);
420 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
421 wait_time.tv_usec = 0;
422 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
424 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
425 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
427 return (kcp->client);
430 /* returns 0 on failure, 1 on success */
433 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
441 struct timeval wait_time;
443 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
445 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
446 *(cryptkeyres*)rslt = *res;
448 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
450 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
451 *(cryptkeyres*)rslt = *res;
453 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
455 res = (*__key_gendes_LOCAL)(geteuid(), 0);
456 *(des_block*)rslt = *res;
460 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
461 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
462 (proc == KEY_GET_CONV))
463 clnt = getkeyserv_handle(2); /* talk to version 2 */
465 clnt = getkeyserv_handle(1); /* talk to version 1 */
471 wait_time.tv_sec = TOTAL_TIMEOUT;
472 wait_time.tv_usec = 0;
474 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
475 wait_time) == RPC_SUCCESS) {