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 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.3 2000/01/27 23:06:39 jasone Exp $
33 * $DragonFly: src/lib/libc/rpc/key_call.c,v 1.5 2005/01/31 22:29:38 dillon Exp $
36 #ident "@(#)key_call.c 1.25 94/04/24 SMI"
39 * key_call.c, Interface to keyserver
41 * setsecretkey(key) - set your secret key
42 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
43 * decryptsessionkey(agent, deskey) - decrypt ditto
44 * gendeskey(deskey) - generate a secure des key
47 #include "namespace.h"
54 #include <rpc/auth_unix.h>
55 #include <rpc/key_prot.h>
57 #include <sys/utsname.h>
61 #include <sys/fcntl.h>
62 #include "un-namespace.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, char *, xdrproc_t, char * );
90 key_setsecret(secretkey)
91 const char *secretkey;
95 if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey,
96 xdr_keystatus, (char *)&status)) {
99 if (status != KEY_SUCCESS) {
100 debug("set status is nonzero");
107 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
108 * stored for the caller's effective uid; it returns 0 otherwise
110 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
111 * be using it, because it allows them to get the user's secret key.
115 key_secretkey_is_set(void)
117 struct key_netstres kres;
119 memset((void*)&kres, 0, sizeof (kres));
120 if (key_call((u_long) KEY_NET_GET, xdr_void, (char *)NULL,
121 xdr_key_netstres, (char *) &kres) &&
122 (kres.status == KEY_SUCCESS) &&
123 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
124 /* avoid leaving secret key in memory */
125 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
132 key_encryptsession_pk(remotename, remotekey, deskey)
140 arg.remotename = remotename;
141 arg.remotekey = *remotekey;
142 arg.deskey = *deskey;
143 if (!key_call((u_long)KEY_ENCRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
144 xdr_cryptkeyres, (char *)&res)) {
147 if (res.status != KEY_SUCCESS) {
148 debug("encrypt status is nonzero");
151 *deskey = res.cryptkeyres_u.deskey;
156 key_decryptsession_pk(remotename, remotekey, deskey)
164 arg.remotename = remotename;
165 arg.remotekey = *remotekey;
166 arg.deskey = *deskey;
167 if (!key_call((u_long)KEY_DECRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
168 xdr_cryptkeyres, (char *)&res)) {
171 if (res.status != KEY_SUCCESS) {
172 debug("decrypt status is nonzero");
175 *deskey = res.cryptkeyres_u.deskey;
180 key_encryptsession(remotename, deskey)
181 const char *remotename;
187 arg.remotename = (char *) remotename;
188 arg.deskey = *deskey;
189 if (!key_call((u_long)KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg,
190 xdr_cryptkeyres, (char *)&res)) {
193 if (res.status != KEY_SUCCESS) {
194 debug("encrypt status is nonzero");
197 *deskey = res.cryptkeyres_u.deskey;
202 key_decryptsession(remotename, deskey)
203 const char *remotename;
209 arg.remotename = (char *) remotename;
210 arg.deskey = *deskey;
211 if (!key_call((u_long)KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg,
212 xdr_cryptkeyres, (char *)&res)) {
215 if (res.status != KEY_SUCCESS) {
216 debug("decrypt status is nonzero");
219 *deskey = res.cryptkeyres_u.deskey;
227 if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL,
228 xdr_des_block, (char *)key)) {
236 struct netstarg *arg;
241 if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg,
242 xdr_keystatus, (char *) &status)){
246 if (status != KEY_SUCCESS) {
247 debug("key_setnet status is nonzero");
255 key_get_conv(pkey, deskey)
261 if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey,
262 xdr_cryptkeyres, (char *)&res)) {
265 if (res.status != KEY_SUCCESS) {
266 debug("get_conv status is nonzero");
269 *deskey = res.cryptkeyres_u.deskey;
273 struct key_call_private {
274 CLIENT *client; /* Client handle */
275 pid_t pid; /* process-id at moment of creation */
276 uid_t uid; /* user-id at last authorization */
278 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);
295 * Keep the handle cached. This call may be made quite often.
298 getkeyserv_handle(vers)
301 struct key_call_private *kcp = key_call_private_main;
302 struct timeval wait_time;
304 struct sockaddr_un name;
305 int namelen = sizeof(struct sockaddr_un);
307 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
308 #define TOTAL_TRIES 5 /* Number of tries */
310 if (kcp == (struct key_call_private *)NULL) {
311 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
312 if (kcp == (struct key_call_private *)NULL) {
313 return ((CLIENT *) NULL);
315 key_call_private_main = 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 other side closed socket, build handle again */
327 clnt_control(kcp->client, CLGET_FD, (char *)&fd);
328 if (_getpeername(fd,(struct sockaddr *)&name,&namelen) == -1) {
329 auth_destroy(kcp->client->cl_auth);
330 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);
353 if ((kcp->client == (CLIENT *) NULL))
354 /* Use the AF_UNIX transport */
355 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG,
358 if (kcp->client == (CLIENT *) NULL) {
359 return ((CLIENT *) NULL);
361 kcp->uid = geteuid();
363 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
364 if (kcp->client->cl_auth == NULL) {
365 clnt_destroy(kcp->client);
367 return ((CLIENT *) NULL);
370 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
371 wait_time.tv_usec = 0;
372 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
374 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
375 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
377 return (kcp->client);
380 /* returns 0 on failure, 1 on success */
383 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
391 struct timeval wait_time;
393 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
395 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
396 *(cryptkeyres*)rslt = *res;
398 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
400 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
401 *(cryptkeyres*)rslt = *res;
403 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
405 res = (*__key_gendes_LOCAL)(geteuid(), 0);
406 *(des_block*)rslt = *res;
410 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
411 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
412 (proc == KEY_GET_CONV))
413 clnt = getkeyserv_handle(2); /* talk to version 2 */
415 clnt = getkeyserv_handle(1); /* talk to version 1 */
421 wait_time.tv_sec = TOTAL_TIMEOUT;
422 wait_time.tv_usec = 0;
424 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
425 wait_time) == RPC_SUCCESS) {