2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 #ident "@(#)key_call.c 1.25 94/04/24 SMI"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * key_call.c, Interface to keyserver
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
45 #include "namespace.h"
46 #include "reentrant.h"
53 #include <rpc/auth_unix.h>
54 #include <rpc/key_prot.h>
56 #include <netconfig.h>
57 #include <sys/utsname.h>
61 #include <sys/fcntl.h>
62 #include "un-namespace.h"
66 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
67 #define KEY_NRETRY 12 /* number of retries */
70 #define debug(msg) (void) fprintf(stderr, "%s\n", msg);
76 * Hack to allow the keyserver to use AUTH_DES (for authenticated
77 * NIS+ calls, for example). The only functions that get called
78 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
80 * The approach is to have the keyserver fill in pointers to local
81 * implementations of these functions, and to call those in key_call().
84 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
85 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
86 des_block *(*__key_gendes_LOCAL)() = 0;
88 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
91 key_setsecret(secretkey)
92 const char *secretkey;
96 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
98 (xdrproc_t)xdr_keystatus, &status)) {
101 if (status != KEY_SUCCESS) {
102 debug("set status is nonzero");
109 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
110 * stored for the caller's effective uid; it returns 0 otherwise
112 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
113 * be using it, because it allows them to get the user's secret key.
117 key_secretkey_is_set(void)
119 struct key_netstres kres;
121 memset((void*)&kres, 0, sizeof (kres));
122 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
123 (xdrproc_t)xdr_key_netstres, &kres) &&
124 (kres.status == KEY_SUCCESS) &&
125 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
126 /* avoid leaving secret key in memory */
127 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
134 key_encryptsession_pk(remotename, remotekey, deskey)
142 arg.remotename = remotename;
143 arg.remotekey = *remotekey;
144 arg.deskey = *deskey;
145 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
146 (xdrproc_t)xdr_cryptkeyres, &res)) {
149 if (res.status != KEY_SUCCESS) {
150 debug("encrypt status is nonzero");
153 *deskey = res.cryptkeyres_u.deskey;
158 key_decryptsession_pk(remotename, remotekey, deskey)
166 arg.remotename = remotename;
167 arg.remotekey = *remotekey;
168 arg.deskey = *deskey;
169 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
170 (xdrproc_t)xdr_cryptkeyres, &res)) {
173 if (res.status != KEY_SUCCESS) {
174 debug("decrypt status is nonzero");
177 *deskey = res.cryptkeyres_u.deskey;
182 key_encryptsession(remotename, deskey)
183 const char *remotename;
189 arg.remotename = (char *) remotename;
190 arg.deskey = *deskey;
191 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
192 (xdrproc_t)xdr_cryptkeyres, &res)) {
195 if (res.status != KEY_SUCCESS) {
196 debug("encrypt status is nonzero");
199 *deskey = res.cryptkeyres_u.deskey;
204 key_decryptsession(remotename, deskey)
205 const char *remotename;
211 arg.remotename = (char *) remotename;
212 arg.deskey = *deskey;
213 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
214 (xdrproc_t)xdr_cryptkeyres, &res)) {
217 if (res.status != KEY_SUCCESS) {
218 debug("decrypt status is nonzero");
221 *deskey = res.cryptkeyres_u.deskey;
229 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
230 (xdrproc_t)xdr_des_block, key)) {
238 struct key_netstarg *arg;
243 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
244 (xdrproc_t)xdr_keystatus, &status)){
248 if (status != KEY_SUCCESS) {
249 debug("key_setnet status is nonzero");
257 key_get_conv(pkey, deskey)
263 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
264 (xdrproc_t)xdr_cryptkeyres, &res)) {
267 if (res.status != KEY_SUCCESS) {
268 debug("get_conv status is nonzero");
271 *deskey = res.cryptkeyres_u.deskey;
275 struct key_call_private {
276 CLIENT *client; /* Client handle */
277 pid_t pid; /* process-id at moment of creation */
278 uid_t uid; /* user-id at last authorization */
280 static struct key_call_private *key_call_private_main = NULL;
281 static thread_key_t key_call_key;
282 static once_t key_call_once = ONCE_INITIALIZER;
283 static int key_call_key_error;
286 key_call_destroy(void *vp)
288 struct key_call_private *kcp = (struct key_call_private *)vp;
292 clnt_destroy(kcp->client);
301 key_call_key_error = thr_keycreate(&key_call_key, key_call_destroy);
305 * Keep the handle cached. This call may be made quite often.
308 getkeyserv_handle(vers)
312 struct netconfig *nconf;
313 struct netconfig *tpconf;
314 struct key_call_private *kcp;
315 struct timeval wait_time;
320 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
321 #define TOTAL_TRIES 5 /* Number of tries */
323 if ((main_thread = thr_main())) {
324 kcp = key_call_private_main;
326 if (thr_once(&key_call_once, key_call_init) != 0 ||
327 key_call_key_error != 0)
328 return ((CLIENT *) NULL);
329 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
331 if (kcp == (struct key_call_private *)NULL) {
332 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
333 if (kcp == (struct key_call_private *)NULL) {
334 return ((CLIENT *) NULL);
337 key_call_private_main = kcp;
339 thr_setspecific(key_call_key, (void *) kcp);
343 /* if pid has changed, destroy client and rebuild */
344 if (kcp->client != NULL && kcp->pid != getpid()) {
345 clnt_destroy(kcp->client);
349 if (kcp->client != NULL) {
350 /* if uid has changed, build client handle again */
351 if (kcp->uid != geteuid()) {
352 kcp->uid = geteuid();
353 auth_destroy(kcp->client->cl_auth);
354 kcp->client->cl_auth =
355 authsys_create("", kcp->uid, 0, 0, NULL);
356 if (kcp->client->cl_auth == NULL) {
357 clnt_destroy(kcp->client);
359 return ((CLIENT *) NULL);
362 /* Change the version number to the new one */
363 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
364 return (kcp->client);
366 if (!(localhandle = setnetconfig())) {
367 return ((CLIENT *) NULL);
370 #if defined(__FreeBSD__)
374 if (_nuname(&u) == -1)
376 if (_uname(&u) == -1)
378 #error Unknown architecture!
382 endnetconfig(localhandle);
383 return ((CLIENT *) NULL);
385 while ((nconf = getnetconfig(localhandle)) != NULL) {
386 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
388 * We use COTS_ORD here so that the caller can
389 * find out immediately if the server is dead.
391 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
392 kcp->client = clnt_tp_create(u.nodename,
393 KEY_PROG, vers, nconf);
401 if ((kcp->client == (CLIENT *) NULL) && (tpconf))
402 /* Now, try the CLTS or COTS loopback transport */
403 kcp->client = clnt_tp_create(u.nodename,
404 KEY_PROG, vers, tpconf);
405 endnetconfig(localhandle);
407 if (kcp->client == (CLIENT *) NULL) {
408 return ((CLIENT *) NULL);
410 kcp->uid = geteuid();
412 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
413 if (kcp->client->cl_auth == NULL) {
414 clnt_destroy(kcp->client);
416 return ((CLIENT *) NULL);
419 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
420 wait_time.tv_usec = 0;
421 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
423 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
424 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
426 return (kcp->client);
429 /* returns 0 on failure, 1 on success */
432 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
440 struct timeval wait_time;
442 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
444 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
445 *(cryptkeyres*)rslt = *res;
447 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
449 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
450 *(cryptkeyres*)rslt = *res;
452 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
454 res = (*__key_gendes_LOCAL)(geteuid(), 0);
455 *(des_block*)rslt = *res;
459 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
460 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
461 (proc == KEY_GET_CONV))
462 clnt = getkeyserv_handle(2); /* talk to version 2 */
464 clnt = getkeyserv_handle(1); /* talk to version 1 */
470 wait_time.tv_sec = TOTAL_TIMEOUT;
471 wait_time.tv_usec = 0;
473 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
474 wait_time) == RPC_SUCCESS) {