Remove bogus (void *) casts.
[dragonfly.git] / lib / libc / rpc / key_call.c
CommitLineData
984263bc
MD
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.
ce0e08e2 8 *
984263bc
MD
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.
ce0e08e2 12 *
984263bc
MD
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.
ce0e08e2 16 *
984263bc
MD
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.
ce0e08e2 20 *
984263bc
MD
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.
ce0e08e2 24 *
984263bc
MD
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 */
29/*
ce0e08e2 30 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
984263bc 31 *
ce0e08e2
PA
32 * @(#)key_call.c 1.25 94/04/24 SMI
33 * $FreeBSD: src/lib/libc/rpc/key_call.c,v 1.16 2006/02/27 22:10:59 deischen Exp $
984263bc
MD
34 */
35
984263bc
MD
36/*
37 * key_call.c, Interface to keyserver
38 *
39 * setsecretkey(key) - set your secret key
40 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
41 * decryptsessionkey(agent, deskey) - decrypt ditto
42 * gendeskey(deskey) - generate a secure des key
43 */
44
17ea2221 45#include "namespace.h"
ce0e08e2 46#include "reentrant.h"
984263bc
MD
47#include <stdio.h>
48#include <stdlib.h>
49#include <unistd.h>
50#include <errno.h>
51#include <rpc/rpc.h>
52#include <rpc/auth.h>
53#include <rpc/auth_unix.h>
54#include <rpc/key_prot.h>
55#include <string.h>
ce0e08e2 56#include <netconfig.h>
984263bc 57#include <sys/utsname.h>
984263bc
MD
58#include <signal.h>
59#include <sys/wait.h>
60#include <sys/fcntl.h>
17ea2221 61#include "un-namespace.h"
ce0e08e2 62#include "mt_misc.h"
984263bc
MD
63
64
65#define KEY_TIMEOUT 5 /* per-try timeout in seconds */
66#define KEY_NRETRY 12 /* number of retries */
67
68#ifdef DEBUG
c9b6181e 69#define debug(msg) fprintf(stderr, "%s\n", msg);
984263bc
MD
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
678e8cc6
SW
83cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = NULL;
84cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = NULL;
85des_block *(*__key_gendes_LOCAL)() = NULL;
984263bc 86
ce0e08e2 87static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
984263bc
MD
88
89int
c9b6181e 90key_setsecret(const char *secretkey)
984263bc
MD
91{
92 keystatus status;
93
4e1af74f 94 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf, secretkey,
ce0e08e2 95 (xdrproc_t)xdr_keystatus, &status)) {
984263bc
MD
96 return (-1);
97 }
98 if (status != KEY_SUCCESS) {
99 debug("set status is nonzero");
100 return (-1);
101 }
102 return (0);
103}
104
105
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
108 *
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.
111 */
112
113int
114key_secretkey_is_set(void)
115{
116 struct key_netstres kres;
117
118 memset((void*)&kres, 0, sizeof (kres));
ce0e08e2
PA
119 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
120 (xdrproc_t)xdr_key_netstres, &kres) &&
984263bc
MD
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);
126 }
127 return (0);
128}
129
130int
c9b6181e 131key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
984263bc
MD
132{
133 cryptkeyarg2 arg;
134 cryptkeyres res;
135
136 arg.remotename = remotename;
137 arg.remotekey = *remotekey;
138 arg.deskey = *deskey;
ce0e08e2
PA
139 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
140 (xdrproc_t)xdr_cryptkeyres, &res)) {
984263bc
MD
141 return (-1);
142 }
143 if (res.status != KEY_SUCCESS) {
144 debug("encrypt status is nonzero");
145 return (-1);
146 }
147 *deskey = res.cryptkeyres_u.deskey;
148 return (0);
149}
150
151int
c9b6181e 152key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
984263bc
MD
153{
154 cryptkeyarg2 arg;
155 cryptkeyres res;
156
157 arg.remotename = remotename;
158 arg.remotekey = *remotekey;
159 arg.deskey = *deskey;
ce0e08e2
PA
160 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
161 (xdrproc_t)xdr_cryptkeyres, &res)) {
984263bc
MD
162 return (-1);
163 }
164 if (res.status != KEY_SUCCESS) {
165 debug("decrypt status is nonzero");
166 return (-1);
167 }
168 *deskey = res.cryptkeyres_u.deskey;
169 return (0);
170}
171
172int
c9b6181e 173key_encryptsession(const char *remotename, des_block *deskey)
984263bc
MD
174{
175 cryptkeyarg arg;
176 cryptkeyres res;
177
178 arg.remotename = (char *) remotename;
179 arg.deskey = *deskey;
ce0e08e2
PA
180 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
181 (xdrproc_t)xdr_cryptkeyres, &res)) {
984263bc
MD
182 return (-1);
183 }
184 if (res.status != KEY_SUCCESS) {
185 debug("encrypt status is nonzero");
186 return (-1);
187 }
188 *deskey = res.cryptkeyres_u.deskey;
189 return (0);
190}
191
192int
c9b6181e 193key_decryptsession(const char *remotename, des_block *deskey)
984263bc
MD
194{
195 cryptkeyarg arg;
196 cryptkeyres res;
197
198 arg.remotename = (char *) remotename;
199 arg.deskey = *deskey;
ce0e08e2
PA
200 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
201 (xdrproc_t)xdr_cryptkeyres, &res)) {
984263bc
MD
202 return (-1);
203 }
204 if (res.status != KEY_SUCCESS) {
205 debug("decrypt status is nonzero");
206 return (-1);
207 }
208 *deskey = res.cryptkeyres_u.deskey;
209 return (0);
210}
211
212int
c9b6181e 213key_gendes(des_block *key)
984263bc 214{
ce0e08e2
PA
215 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
216 (xdrproc_t)xdr_des_block, key)) {
984263bc
MD
217 return (-1);
218 }
219 return (0);
220}
221
222int
ce0e08e2 223key_setnet(struct key_netstarg *arg)
984263bc
MD
224{
225 keystatus status;
226
227
ce0e08e2
PA
228 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
229 (xdrproc_t)xdr_keystatus, &status)){
984263bc
MD
230 return (-1);
231 }
232
233 if (status != KEY_SUCCESS) {
234 debug("key_setnet status is nonzero");
235 return (-1);
236 }
237 return (1);
238}
239
240
241int
c9b6181e 242key_get_conv(char *pkey, des_block *deskey)
984263bc
MD
243{
244 cryptkeyres res;
245
ce0e08e2
PA
246 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
247 (xdrproc_t)xdr_cryptkeyres, &res)) {
984263bc
MD
248 return (-1);
249 }
250 if (res.status != KEY_SUCCESS) {
251 debug("get_conv status is nonzero");
252 return (-1);
253 }
254 *deskey = res.cryptkeyres_u.deskey;
255 return (0);
256}
257
258struct 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 */
262};
263static struct key_call_private *key_call_private_main = NULL;
264
984263bc
MD
265static void
266key_call_destroy(void *vp)
267{
660c873b 268 struct key_call_private *kcp = (struct key_call_private *)vp;
984263bc
MD
269
270 if (kcp) {
271 if (kcp->client)
272 clnt_destroy(kcp->client);
273 free(kcp);
274 }
275}
984263bc
MD
276
277/*
278 * Keep the handle cached. This call may be made quite often.
279 */
280static CLIENT *
c9b6181e 281getkeyserv_handle(int vers)
984263bc 282{
ce0e08e2
PA
283 void *localhandle;
284 struct netconfig *nconf;
285 struct netconfig *tpconf;
984263bc
MD
286 struct key_call_private *kcp = key_call_private_main;
287 struct timeval wait_time;
ce0e08e2
PA
288 struct utsname u;
289 int main_thread;
984263bc 290 int fd;
ce0e08e2 291 static thread_key_t key_call_key;
984263bc
MD
292
293#define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
294#define TOTAL_TRIES 5 /* Number of tries */
295
ce0e08e2
PA
296 if ((main_thread = thr_main())) {
297 kcp = key_call_private_main;
298 } else {
299 if (key_call_key == 0) {
300 mutex_lock(&tsd_lock);
301 if (key_call_key == 0)
302 thr_keycreate(&key_call_key, key_call_destroy);
303 mutex_unlock(&tsd_lock);
304 }
305 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
306 }
2038fb68 307 if (kcp == NULL) {
984263bc 308 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
2038fb68
SW
309 if (kcp == NULL) {
310 return (NULL);
984263bc 311 }
ce0e08e2
PA
312 if (main_thread)
313 key_call_private_main = kcp;
314 else
315 thr_setspecific(key_call_key, (void *) kcp);
984263bc
MD
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
984263bc
MD
325 if (kcp->client != NULL) {
326 /* if uid has changed, build client handle again */
327 if (kcp->uid != geteuid()) {
328 kcp->uid = geteuid();
329 auth_destroy(kcp->client->cl_auth);
330 kcp->client->cl_auth =
331 authsys_create("", kcp->uid, 0, 0, NULL);
332 if (kcp->client->cl_auth == NULL) {
333 clnt_destroy(kcp->client);
334 kcp->client = NULL;
2038fb68 335 return (NULL);
984263bc
MD
336 }
337 }
338 /* Change the version number to the new one */
339 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
340 return (kcp->client);
341 }
ce0e08e2 342 if (!(localhandle = setnetconfig())) {
2038fb68 343 return (NULL);
ce0e08e2
PA
344 }
345 tpconf = NULL;
346 if (uname(&u) == -1)
347 {
348 endnetconfig(localhandle);
2038fb68 349 return (NULL);
ce0e08e2
PA
350 }
351 while ((nconf = getnetconfig(localhandle)) != NULL) {
352 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
353 /*
354 * We use COTS_ORD here so that the caller can
355 * find out immediately if the server is dead.
356 */
357 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
358 kcp->client = clnt_tp_create(u.nodename,
359 KEY_PROG, vers, nconf);
360 if (kcp->client)
361 break;
362 } else {
363 tpconf = nconf;
364 }
365 }
366 }
2038fb68 367 if ((kcp->client == NULL) && (tpconf))
ce0e08e2
PA
368 /* Now, try the CLTS or COTS loopback transport */
369 kcp->client = clnt_tp_create(u.nodename,
370 KEY_PROG, vers, tpconf);
371 endnetconfig(localhandle);
984263bc 372
2038fb68
SW
373 if (kcp->client == NULL) {
374 return (NULL);
984263bc
MD
375 }
376 kcp->uid = geteuid();
377 kcp->pid = getpid();
378 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
379 if (kcp->client->cl_auth == NULL) {
380 clnt_destroy(kcp->client);
381 kcp->client = NULL;
2038fb68 382 return (NULL);
984263bc
MD
383 }
384
385 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
386 wait_time.tv_usec = 0;
c9b6181e 387 clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
984263bc
MD
388 (char *)&wait_time);
389 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
390 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
391
392 return (kcp->client);
393}
394
395/* returns 0 on failure, 1 on success */
396
397static int
ce0e08e2
PA
398key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt,
399 void *rslt)
984263bc
MD
400{
401 CLIENT *clnt;
402 struct timeval wait_time;
403
404 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
405 cryptkeyres *res;
406 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
407 *(cryptkeyres*)rslt = *res;
408 return (1);
409 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
410 cryptkeyres *res;
411 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
412 *(cryptkeyres*)rslt = *res;
413 return (1);
414 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
415 des_block *res;
416 res = (*__key_gendes_LOCAL)(geteuid(), 0);
417 *(des_block*)rslt = *res;
418 return (1);
419 }
420
421 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
422 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
423 (proc == KEY_GET_CONV))
424 clnt = getkeyserv_handle(2); /* talk to version 2 */
425 else
426 clnt = getkeyserv_handle(1); /* talk to version 1 */
427
428 if (clnt == NULL) {
429 return (0);
430 }
431
432 wait_time.tv_sec = TOTAL_TIMEOUT;
433 wait_time.tv_usec = 0;
434
435 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
436 wait_time) == RPC_SUCCESS) {
437 return (1);
438 } else {
439 return (0);
440 }
441}