Remove _THREAD_SAFE depenendancies. Create weakly associated stubs for
[dragonfly.git] / lib / libc / rpc / key_call.c
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.
12  * 
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.
16  * 
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.
20  * 
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.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * Copyright (c) 1986-1991 by Sun Microsystems Inc. 
31  *
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 $
34  */
35
36 #ident  "@(#)key_call.c 1.25    94/04/24 SMI"
37
38 /*
39  * key_call.c, Interface to keyserver
40  *
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
45  */
46
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"
63
64
65 #define KEY_TIMEOUT     5       /* per-try timeout in seconds */
66 #define KEY_NRETRY      12      /* number of retries */
67
68 #ifdef DEBUG
69 #define debug(msg)      (void) fprintf(stderr, "%s\n", msg);
70 #else
71 #define debug(msg)
72 #endif /* DEBUG */
73
74 /*
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.
78  *
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().
81  */
82
83 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
84 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
85 des_block *(*__key_gendes_LOCAL)() = 0;
86
87 static int key_call ( u_long, xdrproc_t, char *, xdrproc_t, char * );
88
89 int
90 key_setsecret(secretkey)
91         const char *secretkey;
92 {
93         keystatus status;
94
95         if (!key_call((u_long) KEY_SET, xdr_keybuf, (char *) secretkey,
96                         xdr_keystatus, (char *)&status)) {
97                 return (-1);
98         }
99         if (status != KEY_SUCCESS) {
100                 debug("set status is nonzero");
101                 return (-1);
102         }
103         return (0);
104 }
105
106
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
109  *
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.
112  */
113
114 int
115 key_secretkey_is_set(void)
116 {
117         struct key_netstres     kres;
118
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);
126                 return (1);
127         }
128         return (0);
129 }
130
131 int
132 key_encryptsession_pk(remotename, remotekey, deskey)
133         char *remotename;
134         netobj *remotekey;
135         des_block *deskey;
136 {
137         cryptkeyarg2 arg;
138         cryptkeyres res;
139
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)) {
145                 return (-1);
146         }
147         if (res.status != KEY_SUCCESS) {
148                 debug("encrypt status is nonzero");
149                 return (-1);
150         }
151         *deskey = res.cryptkeyres_u.deskey;
152         return (0);
153 }
154
155 int
156 key_decryptsession_pk(remotename, remotekey, deskey)
157         char *remotename;
158         netobj *remotekey;
159         des_block *deskey;
160 {
161         cryptkeyarg2 arg;
162         cryptkeyres res;
163
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)) {
169                 return (-1);
170         }
171         if (res.status != KEY_SUCCESS) {
172                 debug("decrypt status is nonzero");
173                 return (-1);
174         }
175         *deskey = res.cryptkeyres_u.deskey;
176         return (0);
177 }
178
179 int
180 key_encryptsession(remotename, deskey)
181         const char *remotename;
182         des_block *deskey;
183 {
184         cryptkeyarg arg;
185         cryptkeyres res;
186
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)) {
191                 return (-1);
192         }
193         if (res.status != KEY_SUCCESS) {
194                 debug("encrypt status is nonzero");
195                 return (-1);
196         }
197         *deskey = res.cryptkeyres_u.deskey;
198         return (0);
199 }
200
201 int
202 key_decryptsession(remotename, deskey)
203         const char *remotename;
204         des_block *deskey;
205 {
206         cryptkeyarg arg;
207         cryptkeyres res;
208
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)) {
213                 return (-1);
214         }
215         if (res.status != KEY_SUCCESS) {
216                 debug("decrypt status is nonzero");
217                 return (-1);
218         }
219         *deskey = res.cryptkeyres_u.deskey;
220         return (0);
221 }
222
223 int
224 key_gendes(key)
225         des_block *key;
226 {
227         if (!key_call((u_long)KEY_GEN, xdr_void, (char *)NULL,
228                         xdr_des_block, (char *)key)) {
229                 return (-1);
230         }
231         return (0);
232 }
233
234 int
235 key_setnet(arg)
236 struct netstarg *arg;
237 {
238         keystatus status;
239
240
241         if (!key_call((u_long) KEY_NET_PUT, xdr_key_netstarg, (char *) arg,
242                 xdr_keystatus, (char *) &status)){
243                 return (-1);
244         }
245
246         if (status != KEY_SUCCESS) {
247                 debug("key_setnet status is nonzero");
248                 return (-1);
249         }
250         return (1);
251 }
252
253
254 int
255 key_get_conv(pkey, deskey)
256         char *pkey;
257         des_block *deskey;
258 {
259         cryptkeyres res;
260
261         if (!key_call((u_long) KEY_GET_CONV, xdr_keybuf, pkey,
262                 xdr_cryptkeyres, (char *)&res)) {
263                 return (-1);
264         }
265         if (res.status != KEY_SUCCESS) {
266                 debug("get_conv status is nonzero");
267                 return (-1);
268         }
269         *deskey = res.cryptkeyres_u.deskey;
270         return (0);
271 }
272
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 */
277 };
278 static struct key_call_private *key_call_private_main = NULL;
279
280 #ifdef foo
281 static void
282 key_call_destroy(void *vp)
283 {
284         struct key_call_private *kcp = (struct key_call_private *)vp;
285
286         if (kcp) {
287                 if (kcp->client)
288                         clnt_destroy(kcp->client);
289                 free(kcp);
290         }
291 }
292 #endif
293
294 /*
295  * Keep the handle cached.  This call may be made quite often.
296  */
297 static CLIENT *
298 getkeyserv_handle(vers)
299 int     vers;
300 {
301         struct key_call_private *kcp = key_call_private_main;
302         struct timeval wait_time;
303         int fd;
304         struct sockaddr_un name;
305         int namelen = sizeof(struct sockaddr_un);
306
307 #define TOTAL_TIMEOUT   30      /* total timeout talking to keyserver */
308 #define TOTAL_TRIES     5       /* Number of tries */
309
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);
314                 }
315                 key_call_private_main = kcp;
316                 kcp->client = NULL;
317         }
318
319         /* if pid has changed, destroy client and rebuild */
320         if (kcp->client != NULL && kcp->pid != getpid()) {
321                 clnt_destroy(kcp->client);
322                 kcp->client = NULL;
323         }
324
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);
331                         kcp->client = NULL;
332                 }
333         }
334
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);
344                                 kcp->client = NULL;
345                                 return ((CLIENT *) NULL);
346                         }
347                 }
348                 /* Change the version number to the new one */
349                 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
350                 return (kcp->client);
351         }
352
353         if ((kcp->client == (CLIENT *) NULL))
354                 /* Use the AF_UNIX transport */
355                 kcp->client = clnt_create("/var/run/keyservsock", KEY_PROG,
356                                                         vers, "unix");
357
358         if (kcp->client == (CLIENT *) NULL) {
359                 return ((CLIENT *) NULL);
360         }
361         kcp->uid = geteuid();
362         kcp->pid = getpid();
363         kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
364         if (kcp->client->cl_auth == NULL) {
365                 clnt_destroy(kcp->client);
366                 kcp->client = NULL;
367                 return ((CLIENT *) NULL);
368         }
369
370         wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
371         wait_time.tv_usec = 0;
372         (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
373                 (char *)&wait_time);
374         if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
375                 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
376
377         return (kcp->client);
378 }
379
380 /* returns  0 on failure, 1 on success */
381
382 static int
383 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
384         u_long proc;
385         xdrproc_t xdr_arg;
386         char *arg;
387         xdrproc_t xdr_rslt;
388         char *rslt;
389 {
390         CLIENT *clnt;
391         struct timeval wait_time;
392
393         if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
394                 cryptkeyres *res;
395                 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
396                 *(cryptkeyres*)rslt = *res;
397                 return (1);
398         } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
399                 cryptkeyres *res;
400                 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
401                 *(cryptkeyres*)rslt = *res;
402                 return (1);
403         } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
404                 des_block *res;
405                 res = (*__key_gendes_LOCAL)(geteuid(), 0);
406                 *(des_block*)rslt = *res;
407                 return (1);
408         }
409
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 */
414         else
415                 clnt = getkeyserv_handle(1); /* talk to version 1 */
416
417         if (clnt == NULL) {
418                 return (0);
419         }
420
421         wait_time.tv_sec = TOTAL_TIMEOUT;
422         wait_time.tv_usec = 0;
423
424         if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
425                 wait_time) == RPC_SUCCESS) {
426                 return (1);
427         } else {
428                 return (0);
429         }
430 }