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/libcr/rpc/Attic/key_call.c,v 1.4 2004/10/25 19:38:25 drhodus 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
53 #include <rpc/auth_unix.h>
54 #include <rpc/key_prot.h>
56 #include <sys/utsname.h>
60 #include <sys/fcntl.h>
63 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
64 #define KEY_NRETRY 12 /* number of retries */
67 #define debug(msg) (void) fprintf(stderr, "%s\n", msg);
73 * Hack to allow the keyserver to use AUTH_DES (for authenticated
74 * NIS+ calls, for example). The only functions that get called
75 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
77 * The approach is to have the keyserver fill in pointers to local
78 * implementations of these functions, and to call those in key_call().
81 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
82 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
83 des_block *(*__key_gendes_LOCAL)() = 0;
85 static int key_call ( u_long, xdrproc_t, char *, xdrproc_t, char * );
88 key_setsecret(secretkey)
89 const char *secretkey;
93 if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey,
94 xdr_keystatus, (char *)&status)) {
97 if (status != KEY_SUCCESS) {
98 debug("set status is nonzero");
105 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
106 * stored for the caller's effective uid; it returns 0 otherwise
108 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
109 * be using it, because it allows them to get the user's secret key.
113 key_secretkey_is_set(void)
115 struct key_netstres kres;
117 memset((void*)&kres, 0, sizeof (kres));
118 if (key_call((u_long) KEY_NET_GET, xdr_void, (char *)NULL,
119 xdr_key_netstres, (char *) &kres) &&
120 (kres.status == KEY_SUCCESS) &&
121 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
122 /* avoid leaving secret key in memory */
123 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
130 key_encryptsession_pk(remotename, remotekey, deskey)
138 arg.remotename = remotename;
139 arg.remotekey = *remotekey;
140 arg.deskey = *deskey;
141 if (!key_call((u_long)KEY_ENCRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
142 xdr_cryptkeyres, (char *)&res)) {
145 if (res.status != KEY_SUCCESS) {
146 debug("encrypt status is nonzero");
149 *deskey = res.cryptkeyres_u.deskey;
154 key_decryptsession_pk(remotename, remotekey, deskey)
162 arg.remotename = remotename;
163 arg.remotekey = *remotekey;
164 arg.deskey = *deskey;
165 if (!key_call((u_long)KEY_DECRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
166 xdr_cryptkeyres, (char *)&res)) {
169 if (res.status != KEY_SUCCESS) {
170 debug("decrypt status is nonzero");
173 *deskey = res.cryptkeyres_u.deskey;
178 key_encryptsession(remotename, deskey)
179 const char *remotename;
185 arg.remotename = (char *) remotename;
186 arg.deskey = *deskey;
187 if (!key_call((u_long)KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg,
188 xdr_cryptkeyres, (char *)&res)) {
191 if (res.status != KEY_SUCCESS) {
192 debug("encrypt status is nonzero");
195 *deskey = res.cryptkeyres_u.deskey;
200 key_decryptsession(remotename, deskey)
201 const char *remotename;
207 arg.remotename = (char *) remotename;
208 arg.deskey = *deskey;
209 if (!key_call((u_long)KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg,
210 xdr_cryptkeyres, (char *)&res)) {
213 if (res.status != KEY_SUCCESS) {
214 debug("decrypt status is nonzero");
217 *deskey = res.cryptkeyres_u.deskey;
225 if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL,
226 xdr_des_block, (char *)key)) {
234 struct netstarg *arg;
239 if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg,
240 xdr_keystatus, (char *) &status)){
244 if (status != KEY_SUCCESS) {
245 debug("key_setnet status is nonzero");
253 key_get_conv(pkey, deskey)
259 if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey,
260 xdr_cryptkeyres, (char *)&res)) {
263 if (res.status != KEY_SUCCESS) {
264 debug("get_conv status is nonzero");
267 *deskey = res.cryptkeyres_u.deskey;
271 struct key_call_private {
272 CLIENT *client; /* Client handle */
273 pid_t pid; /* process-id at moment of creation */
274 uid_t uid; /* user-id at last authorization */
276 static struct key_call_private *key_call_private_main = NULL;
280 key_call_destroy(void *vp)
282 struct key_call_private *kcp = (struct key_call_private *)vp;
286 clnt_destroy(kcp->client);
293 * Keep the handle cached. This call may be made quite often.
296 getkeyserv_handle(vers)
299 struct key_call_private *kcp = key_call_private_main;
300 struct timeval wait_time;
302 struct sockaddr_un name;
303 int namelen = sizeof(struct sockaddr_un);
305 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
306 #define TOTAL_TRIES 5 /* Number of tries */
308 if (kcp == (struct key_call_private *)NULL) {
309 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
310 if (kcp == (struct key_call_private *)NULL) {
311 return ((CLIENT *) NULL);
313 key_call_private_main = kcp;
317 /* if pid has changed, destroy client and rebuild */
318 if (kcp->client != NULL && kcp->pid != getpid()) {
319 clnt_destroy(kcp->client);
323 if (kcp->client != NULL) {
324 /* if other side closed socket, build handle again */
325 clnt_control(kcp->client, CLGET_FD, (char *)&fd);
326 if (getpeername(fd,(struct sockaddr *)&name,&namelen) == -1) {
327 auth_destroy(kcp->client->cl_auth);
328 clnt_destroy(kcp->client);
333 if (kcp->client != NULL) {
334 /* if uid has changed, build client handle again */
335 if (kcp->uid != geteuid()) {
336 kcp->uid = geteuid();
337 auth_destroy(kcp->client->cl_auth);
338 kcp->client->cl_auth =
339 authsys_create("", kcp->uid, 0, 0, NULL);
340 if (kcp->client->cl_auth == NULL) {
341 clnt_destroy(kcp->client);
343 return ((CLIENT *) NULL);
346 /* Change the version number to the new one */
347 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
348 return (kcp->client);
351 if ((kcp->client == (CLIENT *) NULL))
352 /* Use the AF_UNIX transport */
353 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG,
356 if (kcp->client == (CLIENT *) NULL) {
357 return ((CLIENT *) NULL);
359 kcp->uid = geteuid();
361 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
362 if (kcp->client->cl_auth == NULL) {
363 clnt_destroy(kcp->client);
365 return ((CLIENT *) NULL);
368 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
369 wait_time.tv_usec = 0;
370 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
372 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
373 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
375 return (kcp->client);
378 /* returns 0 on failure, 1 on success */
381 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
389 struct timeval wait_time;
391 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
393 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
394 *(cryptkeyres*)rslt = *res;
396 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
398 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
399 *(cryptkeyres*)rslt = *res;
401 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
403 res = (*__key_gendes_LOCAL)(geteuid(), 0);
404 *(des_block*)rslt = *res;
408 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
409 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
410 (proc == KEY_GET_CONV))
411 clnt = getkeyserv_handle(2); /* talk to version 2 */
413 clnt = getkeyserv_handle(1); /* talk to version 1 */
419 wait_time.tv_sec = TOTAL_TIMEOUT;
420 wait_time.tv_usec = 0;
422 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
423 wait_time) == RPC_SUCCESS) {