MFC r1.9:
[dragonfly.git] / lib / libc / rpc / key_call.c
blob9409561bbb0b52e354a1798fb65ce31e6b82b38f
1 /*
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.
8 *
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.
26 * 2550 Garcia Avenue
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.6 2005/11/13 12:27:04 swildner 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"
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <rpc/rpc.h>
53 #include <rpc/auth.h>
54 #include <rpc/auth_unix.h>
55 #include <rpc/key_prot.h>
56 #include <string.h>
57 #include <sys/utsname.h>
58 #include <stdlib.h>
59 #include <signal.h>
60 #include <sys/wait.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 */
68 #ifdef DEBUG
69 #define debug(msg) fprintf(stderr, "%s\n", msg);
70 #else
71 #define debug(msg)
72 #endif /* DEBUG */
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 * );
89 int
90 key_setsecret(const char *secretkey)
92 keystatus status;
94 if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey,
95 xdr_keystatus, (char *)&status)) {
96 return (-1);
98 if (status != KEY_SUCCESS) {
99 debug("set status is nonzero");
100 return (-1);
102 return (0);
106 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
107 * stored for the caller's effective uid; it returns 0 otherwise
109 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
110 * be using it, because it allows them to get the user's secret key.
114 key_secretkey_is_set(void)
116 struct key_netstres kres;
118 memset((void*)&kres, 0, sizeof (kres));
119 if (key_call((u_long) KEY_NET_GET, xdr_void, (char *)NULL,
120 xdr_key_netstres, (char *) &kres) &&
121 (kres.status == KEY_SUCCESS) &&
122 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
123 /* avoid leaving secret key in memory */
124 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
125 return (1);
127 return (0);
131 key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
133 cryptkeyarg2 arg;
134 cryptkeyres res;
136 arg.remotename = remotename;
137 arg.remotekey = *remotekey;
138 arg.deskey = *deskey;
139 if (!key_call((u_long)KEY_ENCRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
140 xdr_cryptkeyres, (char *)&res)) {
141 return (-1);
143 if (res.status != KEY_SUCCESS) {
144 debug("encrypt status is nonzero");
145 return (-1);
147 *deskey = res.cryptkeyres_u.deskey;
148 return (0);
152 key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
154 cryptkeyarg2 arg;
155 cryptkeyres res;
157 arg.remotename = remotename;
158 arg.remotekey = *remotekey;
159 arg.deskey = *deskey;
160 if (!key_call((u_long)KEY_DECRYPT_PK, xdr_cryptkeyarg2, (char *)&arg,
161 xdr_cryptkeyres, (char *)&res)) {
162 return (-1);
164 if (res.status != KEY_SUCCESS) {
165 debug("decrypt status is nonzero");
166 return (-1);
168 *deskey = res.cryptkeyres_u.deskey;
169 return (0);
173 key_encryptsession(const char *remotename, des_block *deskey)
175 cryptkeyarg arg;
176 cryptkeyres res;
178 arg.remotename = (char *) remotename;
179 arg.deskey = *deskey;
180 if (!key_call((u_long)KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg,
181 xdr_cryptkeyres, (char *)&res)) {
182 return (-1);
184 if (res.status != KEY_SUCCESS) {
185 debug("encrypt status is nonzero");
186 return (-1);
188 *deskey = res.cryptkeyres_u.deskey;
189 return (0);
193 key_decryptsession(const char *remotename, des_block *deskey)
195 cryptkeyarg arg;
196 cryptkeyres res;
198 arg.remotename = (char *) remotename;
199 arg.deskey = *deskey;
200 if (!key_call((u_long)KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg,
201 xdr_cryptkeyres, (char *)&res)) {
202 return (-1);
204 if (res.status != KEY_SUCCESS) {
205 debug("decrypt status is nonzero");
206 return (-1);
208 *deskey = res.cryptkeyres_u.deskey;
209 return (0);
213 key_gendes(des_block *key)
215 if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL,
216 xdr_des_block, (char *)key)) {
217 return (-1);
219 return (0);
223 key_setnet(struct netstarg *arg)
225 keystatus status;
228 if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg,
229 xdr_keystatus, (char *) &status)){
230 return (-1);
233 if (status != KEY_SUCCESS) {
234 debug("key_setnet status is nonzero");
235 return (-1);
237 return (1);
242 key_get_conv(char *pkey, des_block *deskey)
244 cryptkeyres res;
246 if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey,
247 xdr_cryptkeyres, (char *)&res)) {
248 return (-1);
250 if (res.status != KEY_SUCCESS) {
251 debug("get_conv status is nonzero");
252 return (-1);
254 *deskey = res.cryptkeyres_u.deskey;
255 return (0);
258 struct key_call_private {
259 CLIENT *client; /* Client handle */
260 pid_t pid; /* process-id at moment of creation */
261 uid_t uid; /* user-id at last authorization */
263 static struct key_call_private *key_call_private_main = NULL;
265 #ifdef foo
266 static void
267 key_call_destroy(void *vp)
269 struct key_call_private *kcp = (struct key_call_private *)vp;
271 if (kcp) {
272 if (kcp->client)
273 clnt_destroy(kcp->client);
274 free(kcp);
277 #endif
280 * Keep the handle cached. This call may be made quite often.
282 static CLIENT *
283 getkeyserv_handle(int vers)
285 struct key_call_private *kcp = key_call_private_main;
286 struct timeval wait_time;
287 int fd;
288 struct sockaddr_un name;
289 int namelen = sizeof(struct sockaddr_un);
291 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
292 #define TOTAL_TRIES 5 /* Number of tries */
294 if (kcp == (struct key_call_private *)NULL) {
295 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
296 if (kcp == (struct key_call_private *)NULL) {
297 return ((CLIENT *) NULL);
299 key_call_private_main = kcp;
300 kcp->client = NULL;
303 /* if pid has changed, destroy client and rebuild */
304 if (kcp->client != NULL && kcp->pid != getpid()) {
305 clnt_destroy(kcp->client);
306 kcp->client = NULL;
309 if (kcp->client != NULL) {
310 /* if other side closed socket, build handle again */
311 clnt_control(kcp->client, CLGET_FD, (char *)&fd);
312 if (_getpeername(fd,(struct sockaddr *)&name,&namelen) == -1) {
313 auth_destroy(kcp->client->cl_auth);
314 clnt_destroy(kcp->client);
315 kcp->client = NULL;
319 if (kcp->client != NULL) {
320 /* if uid has changed, build client handle again */
321 if (kcp->uid != geteuid()) {
322 kcp->uid = geteuid();
323 auth_destroy(kcp->client->cl_auth);
324 kcp->client->cl_auth =
325 authsys_create("", kcp->uid, 0, 0, NULL);
326 if (kcp->client->cl_auth == NULL) {
327 clnt_destroy(kcp->client);
328 kcp->client = NULL;
329 return ((CLIENT *) NULL);
332 /* Change the version number to the new one */
333 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
334 return (kcp->client);
337 if ((kcp->client == (CLIENT *) NULL))
338 /* Use the AF_UNIX transport */
339 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG,
340 vers, "unix");
342 if (kcp->client == (CLIENT *) NULL) {
343 return ((CLIENT *) NULL);
345 kcp->uid = geteuid();
346 kcp->pid = getpid();
347 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
348 if (kcp->client->cl_auth == NULL) {
349 clnt_destroy(kcp->client);
350 kcp->client = NULL;
351 return ((CLIENT *) NULL);
354 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
355 wait_time.tv_usec = 0;
356 clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
357 (char *)&wait_time);
358 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
359 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
361 return (kcp->client);
364 /* returns 0 on failure, 1 on success */
366 static int
367 key_call(u_long proc, xdrproc_t xdr_arg, char *arg, xdrproc_t xdr_rslt,
368 char *rslt)
370 CLIENT *clnt;
371 struct timeval wait_time;
373 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
374 cryptkeyres *res;
375 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
376 *(cryptkeyres*)rslt = *res;
377 return (1);
378 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
379 cryptkeyres *res;
380 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
381 *(cryptkeyres*)rslt = *res;
382 return (1);
383 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
384 des_block *res;
385 res = (*__key_gendes_LOCAL)(geteuid(), 0);
386 *(des_block*)rslt = *res;
387 return (1);
390 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
391 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
392 (proc == KEY_GET_CONV))
393 clnt = getkeyserv_handle(2); /* talk to version 2 */
394 else
395 clnt = getkeyserv_handle(1); /* talk to version 1 */
397 if (clnt == NULL) {
398 return (0);
401 wait_time.tv_sec = TOTAL_TIMEOUT;
402 wait_time.tv_usec = 0;
404 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
405 wait_time) == RPC_SUCCESS) {
406 return (1);
407 } else {
408 return (0);