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"
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
50 #include <rpc/auth_unix.h>
51 #include <rpc/key_prot.h>
53 #include <sys/utsname.h>
57 #include <sys/fcntl.h>
60 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
61 #define KEY_NRETRY 12 /* number of retries */
64 #define debug(msg) (void) fprintf(stderr, "%s\n", msg);
70 * Hack to allow the keyserver to use AUTH_DES (for authenticated
71 * NIS+ calls, for example). The only functions that get called
72 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
74 * The approach is to have the keyserver fill in pointers to local
75 * implementations of these functions, and to call those in key_call().
78 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
79 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
80 des_block *(*__key_gendes_LOCAL)() = 0;
82 static int key_call __P(( u_long, xdrproc_t, char *, xdrproc_t, char * ));
85 key_setsecret(secretkey)
86 const char *secretkey;
90 if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey,
91 xdr_keystatus, (char *)&status)) {
94 if (status != KEY_SUCCESS) {
95 debug("set status is nonzero");
102 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
103 * stored for the caller's effective uid; it returns 0 otherwise
105 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
106 * be using it, because it allows them to get the user's secret key.
110 key_secretkey_is_set(void)
112 struct key_netstres kres;
114 memset((void*)&kres, 0, sizeof (kres));
115 if (key_call((u_long) KEY_NET_GET, xdr_void, (char *)NULL,
116 xdr_key_netstres, (char *) &kres) &&
117 (kres.status == KEY_SUCCESS) &&
118 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
119 /* avoid leaving secret key in memory */
120 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
127 key_encryptsession_pk(remotename, remotekey, deskey)
135 arg.remotename = remotename;
136 arg.remotekey = *remotekey;
137 arg.deskey = *deskey;
138 if (!key_call((u_long)KEY_ENCRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
139 xdr_cryptkeyres, (char *)&res)) {
142 if (res.status != KEY_SUCCESS) {
143 debug("encrypt status is nonzero");
146 *deskey = res.cryptkeyres_u.deskey;
151 key_decryptsession_pk(remotename, remotekey, deskey)
159 arg.remotename = remotename;
160 arg.remotekey = *remotekey;
161 arg.deskey = *deskey;
162 if (!key_call((u_long)KEY_DECRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
163 xdr_cryptkeyres, (char *)&res)) {
166 if (res.status != KEY_SUCCESS) {
167 debug("decrypt status is nonzero");
170 *deskey = res.cryptkeyres_u.deskey;
175 key_encryptsession(remotename, deskey)
176 const char *remotename;
182 arg.remotename = (char *) remotename;
183 arg.deskey = *deskey;
184 if (!key_call((u_long)KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg,
185 xdr_cryptkeyres, (char *)&res)) {
188 if (res.status != KEY_SUCCESS) {
189 debug("encrypt status is nonzero");
192 *deskey = res.cryptkeyres_u.deskey;
197 key_decryptsession(remotename, deskey)
198 const char *remotename;
204 arg.remotename = (char *) remotename;
205 arg.deskey = *deskey;
206 if (!key_call((u_long)KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg,
207 xdr_cryptkeyres, (char *)&res)) {
210 if (res.status != KEY_SUCCESS) {
211 debug("decrypt status is nonzero");
214 *deskey = res.cryptkeyres_u.deskey;
222 if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL,
223 xdr_des_block, (char *)key)) {
231 struct netstarg *arg;
236 if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg,
237 xdr_keystatus, (char *) &status)){
241 if (status != KEY_SUCCESS) {
242 debug("key_setnet status is nonzero");
250 key_get_conv(pkey, deskey)
256 if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey,
257 xdr_cryptkeyres, (char *)&res)) {
260 if (res.status != KEY_SUCCESS) {
261 debug("get_conv status is nonzero");
264 *deskey = res.cryptkeyres_u.deskey;
268 struct key_call_private {
269 CLIENT *client; /* Client handle */
270 pid_t pid; /* process-id at moment of creation */
271 uid_t uid; /* user-id at last authorization */
273 static struct key_call_private *key_call_private_main = NULL;
277 key_call_destroy(void *vp)
279 register struct key_call_private *kcp = (struct key_call_private *)vp;
283 clnt_destroy(kcp->client);
290 * Keep the handle cached. This call may be made quite often.
293 getkeyserv_handle(vers)
296 struct key_call_private *kcp = key_call_private_main;
297 struct timeval wait_time;
299 struct sockaddr_un name;
300 int namelen = sizeof(struct sockaddr_un);
302 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
303 #define TOTAL_TRIES 5 /* Number of tries */
305 if (kcp == (struct key_call_private *)NULL) {
306 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
307 if (kcp == (struct key_call_private *)NULL) {
308 return ((CLIENT *) NULL);
310 key_call_private_main = kcp;
314 /* if pid has changed, destroy client and rebuild */
315 if (kcp->client != NULL && kcp->pid != getpid()) {
316 clnt_destroy(kcp->client);
320 if (kcp->client != NULL) {
321 /* if other side closed socket, build handle again */
322 clnt_control(kcp->client, CLGET_FD, (char *)&fd);
323 if (getpeername(fd,(struct sockaddr *)&name,&namelen) == -1) {
324 auth_destroy(kcp->client->cl_auth);
325 clnt_destroy(kcp->client);
330 if (kcp->client != NULL) {
331 /* if uid has changed, build client handle again */
332 if (kcp->uid != geteuid()) {
333 kcp->uid = geteuid();
334 auth_destroy(kcp->client->cl_auth);
335 kcp->client->cl_auth =
336 authsys_create("", kcp->uid, 0, 0, NULL);
337 if (kcp->client->cl_auth == NULL) {
338 clnt_destroy(kcp->client);
340 return ((CLIENT *) NULL);
343 /* Change the version number to the new one */
344 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
345 return (kcp->client);
348 if ((kcp->client == (CLIENT *) NULL))
349 /* Use the AF_UNIX transport */
350 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG,
353 if (kcp->client == (CLIENT *) NULL) {
354 return ((CLIENT *) NULL);
356 kcp->uid = geteuid();
358 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
359 if (kcp->client->cl_auth == NULL) {
360 clnt_destroy(kcp->client);
362 return ((CLIENT *) NULL);
365 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
366 wait_time.tv_usec = 0;
367 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
369 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
370 fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
372 return (kcp->client);
375 /* returns 0 on failure, 1 on success */
378 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
386 struct timeval wait_time;
388 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
390 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
391 *(cryptkeyres*)rslt = *res;
393 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
395 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
396 *(cryptkeyres*)rslt = *res;
398 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
400 res = (*__key_gendes_LOCAL)(geteuid(), 0);
401 *(des_block*)rslt = *res;
405 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
406 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
407 (proc == KEY_GET_CONV))
408 clnt = getkeyserv_handle(2); /* talk to version 2 */
410 clnt = getkeyserv_handle(1); /* talk to version 1 */
416 wait_time.tv_sec = TOTAL_TIMEOUT;
417 wait_time.tv_usec = 0;
419 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
420 wait_time) == RPC_SUCCESS) {