xref: /freebsd/lib/libc/rpc/key_call.c (revision f249dbcc7149848de00cd8f4e93fe140dfa3f219)
1e8636dfdSBill Paul /*
2e8636dfdSBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3e8636dfdSBill Paul  * unrestricted use provided that this legend is included on all tape
4e8636dfdSBill Paul  * media and as a part of the software program in whole or part.  Users
5e8636dfdSBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
6e8636dfdSBill Paul  * to license or distribute it to anyone else except as part of a product or
7e8636dfdSBill Paul  * program developed by the user.
8e8636dfdSBill Paul  *
9e8636dfdSBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10e8636dfdSBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11e8636dfdSBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12e8636dfdSBill Paul  *
13e8636dfdSBill Paul  * Sun RPC is provided with no support and without any obligation on the
14e8636dfdSBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
15e8636dfdSBill Paul  * modification or enhancement.
16e8636dfdSBill Paul  *
17e8636dfdSBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18e8636dfdSBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19e8636dfdSBill Paul  * OR ANY PART THEREOF.
20e8636dfdSBill Paul  *
21e8636dfdSBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22e8636dfdSBill Paul  * or profits or other special, indirect and consequential damages, even if
23e8636dfdSBill Paul  * Sun has been advised of the possibility of such damages.
24e8636dfdSBill Paul  *
25e8636dfdSBill Paul  * Sun Microsystems, Inc.
26e8636dfdSBill Paul  * 2550 Garcia Avenue
27e8636dfdSBill Paul  * Mountain View, California  94043
28e8636dfdSBill Paul  */
29e8636dfdSBill Paul /*
30e8636dfdSBill Paul  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
31e8636dfdSBill Paul  */
32e8636dfdSBill Paul 
33e8636dfdSBill Paul #ident	"@(#)key_call.c	1.25	94/04/24 SMI"
34e8636dfdSBill Paul 
35d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
36d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
3706fa7267SDavid E. O'Brien 
38e8636dfdSBill Paul /*
39e8636dfdSBill Paul  * key_call.c, Interface to keyserver
40e8636dfdSBill Paul  *
41e8636dfdSBill Paul  * setsecretkey(key) - set your secret key
42e8636dfdSBill Paul  * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
43e8636dfdSBill Paul  * decryptsessionkey(agent, deskey) - decrypt ditto
44e8636dfdSBill Paul  * gendeskey(deskey) - generate a secure des key
45e8636dfdSBill Paul  */
46e8636dfdSBill Paul 
47d201fe46SDaniel Eischen #include "namespace.h"
489f5afc13SIan Dowse #include "reentrant.h"
49e8636dfdSBill Paul #include <stdio.h>
50e8636dfdSBill Paul #include <stdlib.h>
51e8636dfdSBill Paul #include <unistd.h>
52e8636dfdSBill Paul #include <errno.h>
53e8636dfdSBill Paul #include <rpc/rpc.h>
54e8636dfdSBill Paul #include <rpc/auth.h>
55e8636dfdSBill Paul #include <rpc/auth_unix.h>
56e8636dfdSBill Paul #include <rpc/key_prot.h>
57e8636dfdSBill Paul #include <string.h>
588360efbdSAlfred Perlstein #include <netconfig.h>
59e8636dfdSBill Paul #include <sys/utsname.h>
60e8636dfdSBill Paul #include <stdlib.h>
61e8636dfdSBill Paul #include <signal.h>
62e8636dfdSBill Paul #include <sys/wait.h>
63e8636dfdSBill Paul #include <sys/fcntl.h>
64d201fe46SDaniel Eischen #include "un-namespace.h"
65e8636dfdSBill Paul 
66e8636dfdSBill Paul 
67e8636dfdSBill Paul #define	KEY_TIMEOUT	5	/* per-try timeout in seconds */
68e8636dfdSBill Paul #define	KEY_NRETRY	12	/* number of retries */
69e8636dfdSBill Paul 
70e8636dfdSBill Paul #ifdef DEBUG
71e8636dfdSBill Paul #define	debug(msg)	(void) fprintf(stderr, "%s\n", msg);
72e8636dfdSBill Paul #else
73e8636dfdSBill Paul #define	debug(msg)
74e8636dfdSBill Paul #endif /* DEBUG */
75e8636dfdSBill Paul 
76e8636dfdSBill Paul /*
77e8636dfdSBill Paul  * Hack to allow the keyserver to use AUTH_DES (for authenticated
78e8636dfdSBill Paul  * NIS+ calls, for example).  The only functions that get called
79e8636dfdSBill Paul  * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
80e8636dfdSBill Paul  *
81e8636dfdSBill Paul  * The approach is to have the keyserver fill in pointers to local
82e8636dfdSBill Paul  * implementations of these functions, and to call those in key_call().
83e8636dfdSBill Paul  */
84e8636dfdSBill Paul 
85e8636dfdSBill Paul cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
86e8636dfdSBill Paul cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
87e8636dfdSBill Paul des_block *(*__key_gendes_LOCAL)() = 0;
88e8636dfdSBill Paul 
89f249dbccSDag-Erling Smørgrav static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
90e8636dfdSBill Paul 
91e8636dfdSBill Paul int
92e8636dfdSBill Paul key_setsecret(secretkey)
93e8636dfdSBill Paul 	const char *secretkey;
94e8636dfdSBill Paul {
95e8636dfdSBill Paul 	keystatus status;
96e8636dfdSBill Paul 
97f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf, secretkey,
98f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_keystatus, &status)) {
99e8636dfdSBill Paul 		return (-1);
100e8636dfdSBill Paul 	}
101e8636dfdSBill Paul 	if (status != KEY_SUCCESS) {
102e8636dfdSBill Paul 		debug("set status is nonzero");
103e8636dfdSBill Paul 		return (-1);
104e8636dfdSBill Paul 	}
105e8636dfdSBill Paul 	return (0);
106e8636dfdSBill Paul }
107e8636dfdSBill Paul 
108e8636dfdSBill Paul 
109e8636dfdSBill Paul /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
110e8636dfdSBill Paul  * stored for the caller's effective uid; it returns 0 otherwise
111e8636dfdSBill Paul  *
112e8636dfdSBill Paul  * N.B.:  The KEY_NET_GET key call is undocumented.  Applications shouldn't
113e8636dfdSBill Paul  * be using it, because it allows them to get the user's secret key.
114e8636dfdSBill Paul  */
115e8636dfdSBill Paul 
116e8636dfdSBill Paul int
117e8636dfdSBill Paul key_secretkey_is_set(void)
118e8636dfdSBill Paul {
119e8636dfdSBill Paul 	struct key_netstres 	kres;
120e8636dfdSBill Paul 
121e8636dfdSBill Paul 	memset((void*)&kres, 0, sizeof (kres));
122f249dbccSDag-Erling Smørgrav 	if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
123f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_key_netstres, &kres) &&
124e8636dfdSBill Paul 	    (kres.status == KEY_SUCCESS) &&
125e8636dfdSBill Paul 	    (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
126e8636dfdSBill Paul 		/* avoid leaving secret key in memory */
127e8636dfdSBill Paul 		memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
128e8636dfdSBill Paul 		return (1);
129e8636dfdSBill Paul 	}
130e8636dfdSBill Paul 	return (0);
131e8636dfdSBill Paul }
132e8636dfdSBill Paul 
133e8636dfdSBill Paul int
134e8636dfdSBill Paul key_encryptsession_pk(remotename, remotekey, deskey)
135e8636dfdSBill Paul 	char *remotename;
136e8636dfdSBill Paul 	netobj *remotekey;
137e8636dfdSBill Paul 	des_block *deskey;
138e8636dfdSBill Paul {
139e8636dfdSBill Paul 	cryptkeyarg2 arg;
140e8636dfdSBill Paul 	cryptkeyres res;
141e8636dfdSBill Paul 
142e8636dfdSBill Paul 	arg.remotename = remotename;
143e8636dfdSBill Paul 	arg.remotekey = *remotekey;
144e8636dfdSBill Paul 	arg.deskey = *deskey;
145f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
146f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
147e8636dfdSBill Paul 		return (-1);
148e8636dfdSBill Paul 	}
149e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
150e8636dfdSBill Paul 		debug("encrypt status is nonzero");
151e8636dfdSBill Paul 		return (-1);
152e8636dfdSBill Paul 	}
153e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
154e8636dfdSBill Paul 	return (0);
155e8636dfdSBill Paul }
156e8636dfdSBill Paul 
157e8636dfdSBill Paul int
158e8636dfdSBill Paul key_decryptsession_pk(remotename, remotekey, deskey)
159e8636dfdSBill Paul 	char *remotename;
160e8636dfdSBill Paul 	netobj *remotekey;
161e8636dfdSBill Paul 	des_block *deskey;
162e8636dfdSBill Paul {
163e8636dfdSBill Paul 	cryptkeyarg2 arg;
164e8636dfdSBill Paul 	cryptkeyres res;
165e8636dfdSBill Paul 
166e8636dfdSBill Paul 	arg.remotename = remotename;
167e8636dfdSBill Paul 	arg.remotekey = *remotekey;
168e8636dfdSBill Paul 	arg.deskey = *deskey;
169f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
170f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
171e8636dfdSBill Paul 		return (-1);
172e8636dfdSBill Paul 	}
173e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
174e8636dfdSBill Paul 		debug("decrypt status is nonzero");
175e8636dfdSBill Paul 		return (-1);
176e8636dfdSBill Paul 	}
177e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
178e8636dfdSBill Paul 	return (0);
179e8636dfdSBill Paul }
180e8636dfdSBill Paul 
181e8636dfdSBill Paul int
182e8636dfdSBill Paul key_encryptsession(remotename, deskey)
183e8636dfdSBill Paul 	const char *remotename;
184e8636dfdSBill Paul 	des_block *deskey;
185e8636dfdSBill Paul {
186e8636dfdSBill Paul 	cryptkeyarg arg;
187e8636dfdSBill Paul 	cryptkeyres res;
188e8636dfdSBill Paul 
189e8636dfdSBill Paul 	arg.remotename = (char *) remotename;
190e8636dfdSBill Paul 	arg.deskey = *deskey;
191f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
192f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
193e8636dfdSBill Paul 		return (-1);
194e8636dfdSBill Paul 	}
195e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
196e8636dfdSBill Paul 		debug("encrypt status is nonzero");
197e8636dfdSBill Paul 		return (-1);
198e8636dfdSBill Paul 	}
199e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
200e8636dfdSBill Paul 	return (0);
201e8636dfdSBill Paul }
202e8636dfdSBill Paul 
203e8636dfdSBill Paul int
204e8636dfdSBill Paul key_decryptsession(remotename, deskey)
205e8636dfdSBill Paul 	const char *remotename;
206e8636dfdSBill Paul 	des_block *deskey;
207e8636dfdSBill Paul {
208e8636dfdSBill Paul 	cryptkeyarg arg;
209e8636dfdSBill Paul 	cryptkeyres res;
210e8636dfdSBill Paul 
211e8636dfdSBill Paul 	arg.remotename = (char *) remotename;
212e8636dfdSBill Paul 	arg.deskey = *deskey;
213f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
214f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
215e8636dfdSBill Paul 		return (-1);
216e8636dfdSBill Paul 	}
217e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
218e8636dfdSBill Paul 		debug("decrypt status is nonzero");
219e8636dfdSBill Paul 		return (-1);
220e8636dfdSBill Paul 	}
221e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
222e8636dfdSBill Paul 	return (0);
223e8636dfdSBill Paul }
224e8636dfdSBill Paul 
225e8636dfdSBill Paul int
226e8636dfdSBill Paul key_gendes(key)
227e8636dfdSBill Paul 	des_block *key;
228e8636dfdSBill Paul {
229f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
230f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_des_block, key)) {
231e8636dfdSBill Paul 		return (-1);
232e8636dfdSBill Paul 	}
233e8636dfdSBill Paul 	return (0);
234e8636dfdSBill Paul }
235e8636dfdSBill Paul 
236e8636dfdSBill Paul int
237e8636dfdSBill Paul key_setnet(arg)
2388360efbdSAlfred Perlstein struct key_netstarg *arg;
239e8636dfdSBill Paul {
240e8636dfdSBill Paul 	keystatus status;
241e8636dfdSBill Paul 
242e8636dfdSBill Paul 
243f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
244f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_keystatus, &status)){
245e8636dfdSBill Paul 		return (-1);
246e8636dfdSBill Paul 	}
247e8636dfdSBill Paul 
248e8636dfdSBill Paul 	if (status != KEY_SUCCESS) {
249e8636dfdSBill Paul 		debug("key_setnet status is nonzero");
250e8636dfdSBill Paul 		return (-1);
251e8636dfdSBill Paul 	}
252e8636dfdSBill Paul 	return (1);
253e8636dfdSBill Paul }
254e8636dfdSBill Paul 
255e8636dfdSBill Paul 
256e8636dfdSBill Paul int
257e8636dfdSBill Paul key_get_conv(pkey, deskey)
258e8636dfdSBill Paul 	char *pkey;
259e8636dfdSBill Paul 	des_block *deskey;
260e8636dfdSBill Paul {
261e8636dfdSBill Paul 	cryptkeyres res;
262e8636dfdSBill Paul 
263f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
264f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
265e8636dfdSBill Paul 		return (-1);
266e8636dfdSBill Paul 	}
267e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
268e8636dfdSBill Paul 		debug("get_conv status is nonzero");
269e8636dfdSBill Paul 		return (-1);
270e8636dfdSBill Paul 	}
271e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
272e8636dfdSBill Paul 	return (0);
273e8636dfdSBill Paul }
274e8636dfdSBill Paul 
275e8636dfdSBill Paul struct  key_call_private {
276e8636dfdSBill Paul 	CLIENT	*client;	/* Client handle */
277e8636dfdSBill Paul 	pid_t	pid;		/* process-id at moment of creation */
278e8636dfdSBill Paul 	uid_t	uid;		/* user-id at last authorization */
279e8636dfdSBill Paul };
280e8636dfdSBill Paul static struct key_call_private *key_call_private_main = NULL;
281e8636dfdSBill Paul 
282e8636dfdSBill Paul static void
283e8636dfdSBill Paul key_call_destroy(void *vp)
284e8636dfdSBill Paul {
2858fb3f3f6SDavid E. O'Brien 	struct key_call_private *kcp = (struct key_call_private *)vp;
286e8636dfdSBill Paul 
287e8636dfdSBill Paul 	if (kcp) {
288e8636dfdSBill Paul 		if (kcp->client)
289e8636dfdSBill Paul 			clnt_destroy(kcp->client);
290e8636dfdSBill Paul 		free(kcp);
291e8636dfdSBill Paul 	}
292e8636dfdSBill Paul }
293e8636dfdSBill Paul 
294e8636dfdSBill Paul /*
295e8636dfdSBill Paul  * Keep the handle cached.  This call may be made quite often.
296e8636dfdSBill Paul  */
297e8636dfdSBill Paul static CLIENT *
298e8636dfdSBill Paul getkeyserv_handle(vers)
299e8636dfdSBill Paul int	vers;
300e8636dfdSBill Paul {
3018360efbdSAlfred Perlstein 	void *localhandle;
3028360efbdSAlfred Perlstein 	struct netconfig *nconf;
3038360efbdSAlfred Perlstein 	struct netconfig *tpconf;
304e8636dfdSBill Paul 	struct key_call_private *kcp = key_call_private_main;
305e8636dfdSBill Paul 	struct timeval wait_time;
3068360efbdSAlfred Perlstein 	struct utsname u;
3078360efbdSAlfred Perlstein 	int main_thread;
308e8636dfdSBill Paul 	int fd;
3098360efbdSAlfred Perlstein 	static thread_key_t key_call_key;
3108360efbdSAlfred Perlstein 	extern mutex_t tsd_lock;
311e8636dfdSBill Paul 
312e8636dfdSBill Paul #define	TOTAL_TIMEOUT	30	/* total timeout talking to keyserver */
313e8636dfdSBill Paul #define	TOTAL_TRIES	5	/* Number of tries */
314e8636dfdSBill Paul 
3158360efbdSAlfred Perlstein 	if ((main_thread = thr_main())) {
3168360efbdSAlfred Perlstein 		kcp = key_call_private_main;
3178360efbdSAlfred Perlstein 	} else {
3188360efbdSAlfred Perlstein 		if (key_call_key == 0) {
3198360efbdSAlfred Perlstein 			mutex_lock(&tsd_lock);
3208360efbdSAlfred Perlstein 			if (key_call_key == 0)
3218360efbdSAlfred Perlstein 				thr_keycreate(&key_call_key, key_call_destroy);
3228360efbdSAlfred Perlstein 			mutex_unlock(&tsd_lock);
3238360efbdSAlfred Perlstein 		}
3248360efbdSAlfred Perlstein 		kcp = (struct key_call_private *)thr_getspecific(key_call_key);
3258360efbdSAlfred Perlstein 	}
326e8636dfdSBill Paul 	if (kcp == (struct key_call_private *)NULL) {
327e8636dfdSBill Paul 		kcp = (struct key_call_private *)malloc(sizeof (*kcp));
328e8636dfdSBill Paul 		if (kcp == (struct key_call_private *)NULL) {
329e8636dfdSBill Paul 			return ((CLIENT *) NULL);
330e8636dfdSBill Paul 		}
3318360efbdSAlfred Perlstein                 if (main_thread)
332e8636dfdSBill Paul                         key_call_private_main = kcp;
3338360efbdSAlfred Perlstein                 else
3348360efbdSAlfred Perlstein                         thr_setspecific(key_call_key, (void *) kcp);
335e8636dfdSBill Paul 		kcp->client = NULL;
336e8636dfdSBill Paul 	}
337e8636dfdSBill Paul 
338e8636dfdSBill Paul 	/* if pid has changed, destroy client and rebuild */
339e8636dfdSBill Paul 	if (kcp->client != NULL && kcp->pid != getpid()) {
340e8636dfdSBill Paul 		clnt_destroy(kcp->client);
341e8636dfdSBill Paul 		kcp->client = NULL;
342e8636dfdSBill Paul 	}
343e8636dfdSBill Paul 
344e8636dfdSBill Paul 	if (kcp->client != NULL) {
345e8636dfdSBill Paul 		/* if uid has changed, build client handle again */
346e8636dfdSBill Paul 		if (kcp->uid != geteuid()) {
347e8636dfdSBill Paul 			kcp->uid = geteuid();
348e8636dfdSBill Paul 			auth_destroy(kcp->client->cl_auth);
349e8636dfdSBill Paul 			kcp->client->cl_auth =
350e8636dfdSBill Paul 				authsys_create("", kcp->uid, 0, 0, NULL);
351e8636dfdSBill Paul 			if (kcp->client->cl_auth == NULL) {
352e8636dfdSBill Paul 				clnt_destroy(kcp->client);
353e8636dfdSBill Paul 				kcp->client = NULL;
354e8636dfdSBill Paul 				return ((CLIENT *) NULL);
355e8636dfdSBill Paul 			}
356e8636dfdSBill Paul 		}
357e8636dfdSBill Paul 		/* Change the version number to the new one */
358e8636dfdSBill Paul 		clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
359e8636dfdSBill Paul 		return (kcp->client);
360e8636dfdSBill Paul 	}
3618360efbdSAlfred Perlstein 	if (!(localhandle = setnetconfig())) {
3628360efbdSAlfred Perlstein 		return ((CLIENT *) NULL);
3638360efbdSAlfred Perlstein 	}
3648360efbdSAlfred Perlstein         tpconf = NULL;
36506fa7267SDavid E. O'Brien #if defined(__FreeBSD__)
3668360efbdSAlfred Perlstein 	if (uname(&u) == -1)
36706fa7267SDavid E. O'Brien #else
36806fa7267SDavid E. O'Brien #if defined(i386)
36906fa7267SDavid E. O'Brien 	if (_nuname(&u) == -1)
37006fa7267SDavid E. O'Brien #elif defined(sparc)
37106fa7267SDavid E. O'Brien 	if (_uname(&u) == -1)
37206fa7267SDavid E. O'Brien #else
37306fa7267SDavid E. O'Brien #error Unknown architecture!
37406fa7267SDavid E. O'Brien #endif
37506fa7267SDavid E. O'Brien #endif
3768360efbdSAlfred Perlstein 	{
3778360efbdSAlfred Perlstein 		endnetconfig(localhandle);
3788360efbdSAlfred Perlstein 		return ((CLIENT *) NULL);
3798360efbdSAlfred Perlstein         }
3808d630135SAlfred Perlstein 	while ((nconf = getnetconfig(localhandle)) != NULL) {
3818360efbdSAlfred Perlstein 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
3828360efbdSAlfred Perlstein 			/*
3838360efbdSAlfred Perlstein 			 * We use COTS_ORD here so that the caller can
3848360efbdSAlfred Perlstein 			 * find out immediately if the server is dead.
3858360efbdSAlfred Perlstein 			 */
3868360efbdSAlfred Perlstein 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
3878360efbdSAlfred Perlstein 				kcp->client = clnt_tp_create(u.nodename,
3888360efbdSAlfred Perlstein 					KEY_PROG, vers, nconf);
3898360efbdSAlfred Perlstein 				if (kcp->client)
3908360efbdSAlfred Perlstein 					break;
3918360efbdSAlfred Perlstein 			} else {
3928360efbdSAlfred Perlstein 				tpconf = nconf;
3938360efbdSAlfred Perlstein 			}
3948360efbdSAlfred Perlstein 		}
3958360efbdSAlfred Perlstein 	}
3968360efbdSAlfred Perlstein 	if ((kcp->client == (CLIENT *) NULL) && (tpconf))
3978360efbdSAlfred Perlstein 		/* Now, try the CLTS or COTS loopback transport */
3988360efbdSAlfred Perlstein 		kcp->client = clnt_tp_create(u.nodename,
3998360efbdSAlfred Perlstein 			KEY_PROG, vers, tpconf);
4008360efbdSAlfred Perlstein 	endnetconfig(localhandle);
401e8636dfdSBill Paul 
402e8636dfdSBill Paul 	if (kcp->client == (CLIENT *) NULL) {
403e8636dfdSBill Paul 		return ((CLIENT *) NULL);
404e8636dfdSBill Paul         }
405e8636dfdSBill Paul 	kcp->uid = geteuid();
406e8636dfdSBill Paul 	kcp->pid = getpid();
407e8636dfdSBill Paul 	kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
408e8636dfdSBill Paul 	if (kcp->client->cl_auth == NULL) {
409e8636dfdSBill Paul 		clnt_destroy(kcp->client);
410e8636dfdSBill Paul 		kcp->client = NULL;
411e8636dfdSBill Paul 		return ((CLIENT *) NULL);
412e8636dfdSBill Paul 	}
413e8636dfdSBill Paul 
414e8636dfdSBill Paul 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
415e8636dfdSBill Paul 	wait_time.tv_usec = 0;
416e8636dfdSBill Paul 	(void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
417e8636dfdSBill Paul 		(char *)&wait_time);
418e8636dfdSBill Paul 	if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
4199233c4d9SJason Evans 		_fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
420e8636dfdSBill Paul 
421e8636dfdSBill Paul 	return (kcp->client);
422e8636dfdSBill Paul }
423e8636dfdSBill Paul 
424e8636dfdSBill Paul /* returns  0 on failure, 1 on success */
425e8636dfdSBill Paul 
426e8636dfdSBill Paul static int
427e8636dfdSBill Paul key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
428e8636dfdSBill Paul 	u_long proc;
429e8636dfdSBill Paul 	xdrproc_t xdr_arg;
430f249dbccSDag-Erling Smørgrav 	void *arg;
431e8636dfdSBill Paul 	xdrproc_t xdr_rslt;
432f249dbccSDag-Erling Smørgrav 	void *rslt;
433e8636dfdSBill Paul {
434e8636dfdSBill Paul 	CLIENT *clnt;
435e8636dfdSBill Paul 	struct timeval wait_time;
436e8636dfdSBill Paul 
437e8636dfdSBill Paul 	if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
438e8636dfdSBill Paul 		cryptkeyres *res;
439e8636dfdSBill Paul 		res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
440e8636dfdSBill Paul 		*(cryptkeyres*)rslt = *res;
441e8636dfdSBill Paul 		return (1);
442e8636dfdSBill Paul 	} else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
443e8636dfdSBill Paul 		cryptkeyres *res;
444e8636dfdSBill Paul 		res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
445e8636dfdSBill Paul 		*(cryptkeyres*)rslt = *res;
446e8636dfdSBill Paul 		return (1);
447e8636dfdSBill Paul 	} else if (proc == KEY_GEN && __key_gendes_LOCAL) {
448e8636dfdSBill Paul 		des_block *res;
449e8636dfdSBill Paul 		res = (*__key_gendes_LOCAL)(geteuid(), 0);
450e8636dfdSBill Paul 		*(des_block*)rslt = *res;
451e8636dfdSBill Paul 		return (1);
452e8636dfdSBill Paul 	}
453e8636dfdSBill Paul 
454e8636dfdSBill Paul 	if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
455e8636dfdSBill Paul 	    (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
456e8636dfdSBill Paul 	    (proc == KEY_GET_CONV))
457e8636dfdSBill Paul 		clnt = getkeyserv_handle(2); /* talk to version 2 */
458e8636dfdSBill Paul 	else
459e8636dfdSBill Paul 		clnt = getkeyserv_handle(1); /* talk to version 1 */
460e8636dfdSBill Paul 
461e8636dfdSBill Paul 	if (clnt == NULL) {
462e8636dfdSBill Paul 		return (0);
463e8636dfdSBill Paul 	}
464e8636dfdSBill Paul 
465e8636dfdSBill Paul 	wait_time.tv_sec = TOTAL_TIMEOUT;
466e8636dfdSBill Paul 	wait_time.tv_usec = 0;
467e8636dfdSBill Paul 
468e8636dfdSBill Paul 	if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
469e8636dfdSBill Paul 		wait_time) == RPC_SUCCESS) {
470e8636dfdSBill Paul 		return (1);
471e8636dfdSBill Paul 	} else {
472e8636dfdSBill Paul 		return (0);
473e8636dfdSBill Paul 	}
474e8636dfdSBill Paul }
475