xref: /freebsd/lib/libc/rpc/key_call.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
12e322d37SHiroki Sato /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
42e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
52e322d37SHiroki Sato  * All rights reserved.
6e8636dfdSBill Paul  *
72e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
82e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
92e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
102e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
112e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
132e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
142e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
152e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
162e322d37SHiroki Sato  *   from this software without specific prior written permission.
17e8636dfdSBill Paul  *
182e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
192e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
222e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
29e8636dfdSBill Paul  */
30e8636dfdSBill Paul /*
31e8636dfdSBill Paul  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32e8636dfdSBill Paul  */
33e8636dfdSBill Paul 
34e8636dfdSBill Paul #ident	"@(#)key_call.c	1.25	94/04/24 SMI"
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"
65235baf26SDaniel Eischen #include "mt_misc.h"
66e8636dfdSBill Paul 
67e8636dfdSBill Paul 
68e8636dfdSBill Paul #define	KEY_TIMEOUT	5	/* per-try timeout in seconds */
69e8636dfdSBill Paul #define	KEY_NRETRY	12	/* number of retries */
70e8636dfdSBill Paul 
71e8636dfdSBill Paul #ifdef DEBUG
72e8636dfdSBill Paul #define	debug(msg)	(void) fprintf(stderr, "%s\n", msg);
73e8636dfdSBill Paul #else
74e8636dfdSBill Paul #define	debug(msg)
75e8636dfdSBill Paul #endif /* DEBUG */
76e8636dfdSBill Paul 
77e8636dfdSBill Paul /*
78e8636dfdSBill Paul  * Hack to allow the keyserver to use AUTH_DES (for authenticated
79e8636dfdSBill Paul  * NIS+ calls, for example).  The only functions that get called
80e8636dfdSBill Paul  * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
81e8636dfdSBill Paul  *
82e8636dfdSBill Paul  * The approach is to have the keyserver fill in pointers to local
83e8636dfdSBill Paul  * implementations of these functions, and to call those in key_call().
84e8636dfdSBill Paul  */
85e8636dfdSBill Paul 
8667903135SCraig Rodrigues cryptkeyres *(*__key_encryptsession_pk_LOCAL)(uid_t, void *arg) = 0;
8767903135SCraig Rodrigues cryptkeyres *(*__key_decryptsession_pk_LOCAL)(uid_t, void *arg) = 0;
8867903135SCraig Rodrigues des_block *(*__key_gendes_LOCAL)(uid_t, void *) = 0;
89e8636dfdSBill Paul 
90f249dbccSDag-Erling Smørgrav static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
91e8636dfdSBill Paul 
92e8636dfdSBill Paul int
9368895e38SCraig Rodrigues key_setsecret(const char *secretkey)
94e8636dfdSBill Paul {
95e8636dfdSBill Paul 	keystatus status;
96e8636dfdSBill Paul 
97c549fd46SAlfred Perlstein 	if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
98c549fd46SAlfred Perlstein 			(void *)secretkey,
99f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_keystatus, &status)) {
100e8636dfdSBill Paul 		return (-1);
101e8636dfdSBill Paul 	}
102e8636dfdSBill Paul 	if (status != KEY_SUCCESS) {
103e8636dfdSBill Paul 		debug("set status is nonzero");
104e8636dfdSBill Paul 		return (-1);
105e8636dfdSBill Paul 	}
106e8636dfdSBill Paul 	return (0);
107e8636dfdSBill Paul }
108e8636dfdSBill Paul 
109e8636dfdSBill Paul 
110e8636dfdSBill Paul /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
111e8636dfdSBill Paul  * stored for the caller's effective uid; it returns 0 otherwise
112e8636dfdSBill Paul  *
113e8636dfdSBill Paul  * N.B.:  The KEY_NET_GET key call is undocumented.  Applications shouldn't
114e8636dfdSBill Paul  * be using it, because it allows them to get the user's secret key.
115e8636dfdSBill Paul  */
116e8636dfdSBill Paul 
117e8636dfdSBill Paul int
118e8636dfdSBill Paul key_secretkey_is_set(void)
119e8636dfdSBill Paul {
120e8636dfdSBill Paul 	struct key_netstres 	kres;
121e8636dfdSBill Paul 
122e8636dfdSBill Paul 	memset((void*)&kres, 0, sizeof (kres));
123f249dbccSDag-Erling Smørgrav 	if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
124f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_key_netstres, &kres) &&
125e8636dfdSBill Paul 	    (kres.status == KEY_SUCCESS) &&
126e8636dfdSBill Paul 	    (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
127e8636dfdSBill Paul 		/* avoid leaving secret key in memory */
128e8636dfdSBill Paul 		memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
129e8636dfdSBill Paul 		return (1);
130e8636dfdSBill Paul 	}
131e8636dfdSBill Paul 	return (0);
132e8636dfdSBill Paul }
133e8636dfdSBill Paul 
134e8636dfdSBill Paul int
13568895e38SCraig Rodrigues key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
136e8636dfdSBill Paul {
137e8636dfdSBill Paul 	cryptkeyarg2 arg;
138e8636dfdSBill Paul 	cryptkeyres res;
139e8636dfdSBill Paul 
140e8636dfdSBill Paul 	arg.remotename = remotename;
141e8636dfdSBill Paul 	arg.remotekey = *remotekey;
142e8636dfdSBill Paul 	arg.deskey = *deskey;
143f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
144f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
145e8636dfdSBill Paul 		return (-1);
146e8636dfdSBill Paul 	}
147e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
148e8636dfdSBill Paul 		debug("encrypt status is nonzero");
149e8636dfdSBill Paul 		return (-1);
150e8636dfdSBill Paul 	}
151e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
152e8636dfdSBill Paul 	return (0);
153e8636dfdSBill Paul }
154e8636dfdSBill Paul 
155e8636dfdSBill Paul int
15668895e38SCraig Rodrigues key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
157e8636dfdSBill Paul {
158e8636dfdSBill Paul 	cryptkeyarg2 arg;
159e8636dfdSBill Paul 	cryptkeyres res;
160e8636dfdSBill Paul 
161e8636dfdSBill Paul 	arg.remotename = remotename;
162e8636dfdSBill Paul 	arg.remotekey = *remotekey;
163e8636dfdSBill Paul 	arg.deskey = *deskey;
164f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
165f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
166e8636dfdSBill Paul 		return (-1);
167e8636dfdSBill Paul 	}
168e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
169e8636dfdSBill Paul 		debug("decrypt status is nonzero");
170e8636dfdSBill Paul 		return (-1);
171e8636dfdSBill Paul 	}
172e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
173e8636dfdSBill Paul 	return (0);
174e8636dfdSBill Paul }
175e8636dfdSBill Paul 
176e8636dfdSBill Paul int
17768895e38SCraig Rodrigues key_encryptsession(const char *remotename, des_block *deskey)
178e8636dfdSBill Paul {
179e8636dfdSBill Paul 	cryptkeyarg arg;
180e8636dfdSBill Paul 	cryptkeyres res;
181e8636dfdSBill Paul 
182e8636dfdSBill Paul 	arg.remotename = (char *) remotename;
183e8636dfdSBill Paul 	arg.deskey = *deskey;
184f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
185f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
186e8636dfdSBill Paul 		return (-1);
187e8636dfdSBill Paul 	}
188e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
189e8636dfdSBill Paul 		debug("encrypt status is nonzero");
190e8636dfdSBill Paul 		return (-1);
191e8636dfdSBill Paul 	}
192e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
193e8636dfdSBill Paul 	return (0);
194e8636dfdSBill Paul }
195e8636dfdSBill Paul 
196e8636dfdSBill Paul int
19768895e38SCraig Rodrigues key_decryptsession(const char *remotename, des_block *deskey)
198e8636dfdSBill Paul {
199e8636dfdSBill Paul 	cryptkeyarg arg;
200e8636dfdSBill Paul 	cryptkeyres res;
201e8636dfdSBill Paul 
202e8636dfdSBill Paul 	arg.remotename = (char *) remotename;
203e8636dfdSBill Paul 	arg.deskey = *deskey;
204f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
205f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
206e8636dfdSBill Paul 		return (-1);
207e8636dfdSBill Paul 	}
208e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
209e8636dfdSBill Paul 		debug("decrypt status is nonzero");
210e8636dfdSBill Paul 		return (-1);
211e8636dfdSBill Paul 	}
212e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
213e8636dfdSBill Paul 	return (0);
214e8636dfdSBill Paul }
215e8636dfdSBill Paul 
216e8636dfdSBill Paul int
21768895e38SCraig Rodrigues key_gendes(des_block *key)
218e8636dfdSBill Paul {
219f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
220f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_des_block, key)) {
221e8636dfdSBill Paul 		return (-1);
222e8636dfdSBill Paul 	}
223e8636dfdSBill Paul 	return (0);
224e8636dfdSBill Paul }
225e8636dfdSBill Paul 
226e8636dfdSBill Paul int
22768895e38SCraig Rodrigues key_setnet(struct key_netstarg *arg)
228e8636dfdSBill Paul {
229e8636dfdSBill Paul 	keystatus status;
230e8636dfdSBill Paul 
231e8636dfdSBill Paul 
232f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
233f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_keystatus, &status)){
234e8636dfdSBill Paul 		return (-1);
235e8636dfdSBill Paul 	}
236e8636dfdSBill Paul 
237e8636dfdSBill Paul 	if (status != KEY_SUCCESS) {
238e8636dfdSBill Paul 		debug("key_setnet status is nonzero");
239e8636dfdSBill Paul 		return (-1);
240e8636dfdSBill Paul 	}
241e8636dfdSBill Paul 	return (1);
242e8636dfdSBill Paul }
243e8636dfdSBill Paul 
244e8636dfdSBill Paul 
245e8636dfdSBill Paul int
24668895e38SCraig Rodrigues key_get_conv(char *pkey, des_block *deskey)
247e8636dfdSBill Paul {
248e8636dfdSBill Paul 	cryptkeyres res;
249e8636dfdSBill Paul 
250f249dbccSDag-Erling Smørgrav 	if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
251f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_cryptkeyres, &res)) {
252e8636dfdSBill Paul 		return (-1);
253e8636dfdSBill Paul 	}
254e8636dfdSBill Paul 	if (res.status != KEY_SUCCESS) {
255e8636dfdSBill Paul 		debug("get_conv status is nonzero");
256e8636dfdSBill Paul 		return (-1);
257e8636dfdSBill Paul 	}
258e8636dfdSBill Paul 	*deskey = res.cryptkeyres_u.deskey;
259e8636dfdSBill Paul 	return (0);
260e8636dfdSBill Paul }
261e8636dfdSBill Paul 
262e8636dfdSBill Paul struct  key_call_private {
263e8636dfdSBill Paul 	CLIENT	*client;	/* Client handle */
264e8636dfdSBill Paul 	pid_t	pid;		/* process-id at moment of creation */
265e8636dfdSBill Paul 	uid_t	uid;		/* user-id at last authorization */
266e8636dfdSBill Paul };
267e8636dfdSBill Paul static struct key_call_private *key_call_private_main = NULL;
2686f88d2a8SJohn Baldwin static thread_key_t key_call_key;
2696f88d2a8SJohn Baldwin static once_t key_call_once = ONCE_INITIALIZER;
2706f88d2a8SJohn Baldwin static int key_call_key_error;
271e8636dfdSBill Paul 
272e8636dfdSBill Paul static void
273e8636dfdSBill Paul key_call_destroy(void *vp)
274e8636dfdSBill Paul {
2758fb3f3f6SDavid E. O'Brien 	struct key_call_private *kcp = (struct key_call_private *)vp;
276e8636dfdSBill Paul 
277e8636dfdSBill Paul 	if (kcp) {
278e8636dfdSBill Paul 		if (kcp->client)
279e8636dfdSBill Paul 			clnt_destroy(kcp->client);
280e8636dfdSBill Paul 		free(kcp);
281e8636dfdSBill Paul 	}
282e8636dfdSBill Paul }
283e8636dfdSBill Paul 
2846f88d2a8SJohn Baldwin static void
2856f88d2a8SJohn Baldwin key_call_init(void)
2866f88d2a8SJohn Baldwin {
2876f88d2a8SJohn Baldwin 
2886f88d2a8SJohn Baldwin 	key_call_key_error = thr_keycreate(&key_call_key, key_call_destroy);
2896f88d2a8SJohn Baldwin }
2906f88d2a8SJohn Baldwin 
291e8636dfdSBill Paul /*
292e8636dfdSBill Paul  * Keep the handle cached.  This call may be made quite often.
293e8636dfdSBill Paul  */
294e8636dfdSBill Paul static CLIENT *
29568895e38SCraig Rodrigues getkeyserv_handle(int vers)
296e8636dfdSBill Paul {
2978360efbdSAlfred Perlstein 	void *localhandle;
2988360efbdSAlfred Perlstein 	struct netconfig *nconf;
2998360efbdSAlfred Perlstein 	struct netconfig *tpconf;
3000c0349bfSGarrett Wollman 	struct key_call_private *kcp;
301e8636dfdSBill Paul 	struct timeval wait_time;
3028360efbdSAlfred Perlstein 	struct utsname u;
3038360efbdSAlfred Perlstein 	int main_thread;
304e8636dfdSBill Paul 	int fd;
305e8636dfdSBill Paul 
306e8636dfdSBill Paul #define	TOTAL_TIMEOUT	30	/* total timeout talking to keyserver */
307e8636dfdSBill Paul #define	TOTAL_TRIES	5	/* Number of tries */
308e8636dfdSBill Paul 
3098360efbdSAlfred Perlstein 	if ((main_thread = thr_main())) {
3108360efbdSAlfred Perlstein 		kcp = key_call_private_main;
3118360efbdSAlfred Perlstein 	} else {
3126f88d2a8SJohn Baldwin 		if (thr_once(&key_call_once, key_call_init) != 0 ||
3136f88d2a8SJohn Baldwin 		    key_call_key_error != 0)
3146f88d2a8SJohn Baldwin 			return ((CLIENT *) NULL);
3158360efbdSAlfred Perlstein 		kcp = (struct key_call_private *)thr_getspecific(key_call_key);
3168360efbdSAlfred Perlstein 	}
317e8636dfdSBill Paul 	if (kcp == (struct key_call_private *)NULL) {
318e8636dfdSBill Paul 		kcp = (struct key_call_private *)malloc(sizeof (*kcp));
319e8636dfdSBill Paul 		if (kcp == (struct key_call_private *)NULL) {
320e8636dfdSBill Paul 			return ((CLIENT *) NULL);
321e8636dfdSBill Paul 		}
3228360efbdSAlfred Perlstein                 if (main_thread)
323e8636dfdSBill Paul                         key_call_private_main = kcp;
3248360efbdSAlfred Perlstein                 else
3258360efbdSAlfred Perlstein                         thr_setspecific(key_call_key, (void *) kcp);
326e8636dfdSBill Paul 		kcp->client = NULL;
327e8636dfdSBill Paul 	}
328e8636dfdSBill Paul 
329e8636dfdSBill Paul 	/* if pid has changed, destroy client and rebuild */
330e8636dfdSBill Paul 	if (kcp->client != NULL && kcp->pid != getpid()) {
331e8636dfdSBill Paul 		clnt_destroy(kcp->client);
332e8636dfdSBill Paul 		kcp->client = NULL;
333e8636dfdSBill Paul 	}
334e8636dfdSBill Paul 
335e8636dfdSBill Paul 	if (kcp->client != NULL) {
336e8636dfdSBill Paul 		/* if uid has changed, build client handle again */
337e8636dfdSBill Paul 		if (kcp->uid != geteuid()) {
338e8636dfdSBill Paul 			kcp->uid = geteuid();
339e8636dfdSBill Paul 			auth_destroy(kcp->client->cl_auth);
340e8636dfdSBill Paul 			kcp->client->cl_auth =
341e8636dfdSBill Paul 				authsys_create("", kcp->uid, 0, 0, NULL);
342e8636dfdSBill Paul 			if (kcp->client->cl_auth == NULL) {
343e8636dfdSBill Paul 				clnt_destroy(kcp->client);
344e8636dfdSBill Paul 				kcp->client = NULL;
345e8636dfdSBill Paul 				return ((CLIENT *) NULL);
346e8636dfdSBill Paul 			}
347e8636dfdSBill Paul 		}
348e8636dfdSBill Paul 		/* Change the version number to the new one */
349e8636dfdSBill Paul 		clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
350e8636dfdSBill Paul 		return (kcp->client);
351e8636dfdSBill Paul 	}
3528360efbdSAlfred Perlstein 	if (!(localhandle = setnetconfig())) {
3538360efbdSAlfred Perlstein 		return ((CLIENT *) NULL);
3548360efbdSAlfred Perlstein 	}
3558360efbdSAlfred Perlstein         tpconf = NULL;
35606fa7267SDavid E. O'Brien #if defined(__FreeBSD__)
3578360efbdSAlfred Perlstein 	if (uname(&u) == -1)
35806fa7267SDavid E. O'Brien #else
35906fa7267SDavid E. O'Brien #if defined(i386)
36006fa7267SDavid E. O'Brien 	if (_nuname(&u) == -1)
36106fa7267SDavid E. O'Brien #elif defined(sparc)
36206fa7267SDavid E. O'Brien 	if (_uname(&u) == -1)
36306fa7267SDavid E. O'Brien #else
36406fa7267SDavid E. O'Brien #error Unknown architecture!
36506fa7267SDavid E. O'Brien #endif
36606fa7267SDavid E. O'Brien #endif
3678360efbdSAlfred Perlstein 	{
3688360efbdSAlfred Perlstein 		endnetconfig(localhandle);
3698360efbdSAlfred Perlstein 		return ((CLIENT *) NULL);
3708360efbdSAlfred Perlstein         }
3718d630135SAlfred Perlstein 	while ((nconf = getnetconfig(localhandle)) != NULL) {
3728360efbdSAlfred Perlstein 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
3738360efbdSAlfred Perlstein 			/*
3748360efbdSAlfred Perlstein 			 * We use COTS_ORD here so that the caller can
3758360efbdSAlfred Perlstein 			 * find out immediately if the server is dead.
3768360efbdSAlfred Perlstein 			 */
3778360efbdSAlfred Perlstein 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
3788360efbdSAlfred Perlstein 				kcp->client = clnt_tp_create(u.nodename,
3798360efbdSAlfred Perlstein 					KEY_PROG, vers, nconf);
3808360efbdSAlfred Perlstein 				if (kcp->client)
3818360efbdSAlfred Perlstein 					break;
3828360efbdSAlfred Perlstein 			} else {
3838360efbdSAlfred Perlstein 				tpconf = nconf;
3848360efbdSAlfred Perlstein 			}
3858360efbdSAlfred Perlstein 		}
3868360efbdSAlfred Perlstein 	}
3878360efbdSAlfred Perlstein 	if ((kcp->client == (CLIENT *) NULL) && (tpconf))
3888360efbdSAlfred Perlstein 		/* Now, try the CLTS or COTS loopback transport */
3898360efbdSAlfred Perlstein 		kcp->client = clnt_tp_create(u.nodename,
3908360efbdSAlfred Perlstein 			KEY_PROG, vers, tpconf);
3918360efbdSAlfred Perlstein 	endnetconfig(localhandle);
392e8636dfdSBill Paul 
393e8636dfdSBill Paul 	if (kcp->client == (CLIENT *) NULL) {
394e8636dfdSBill Paul 		return ((CLIENT *) NULL);
395e8636dfdSBill Paul         }
396e8636dfdSBill Paul 	kcp->uid = geteuid();
397e8636dfdSBill Paul 	kcp->pid = getpid();
398e8636dfdSBill Paul 	kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
399e8636dfdSBill Paul 	if (kcp->client->cl_auth == NULL) {
400e8636dfdSBill Paul 		clnt_destroy(kcp->client);
401e8636dfdSBill Paul 		kcp->client = NULL;
402e8636dfdSBill Paul 		return ((CLIENT *) NULL);
403e8636dfdSBill Paul 	}
404e8636dfdSBill Paul 
405e8636dfdSBill Paul 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
406e8636dfdSBill Paul 	wait_time.tv_usec = 0;
407e8636dfdSBill Paul 	(void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
408e8636dfdSBill Paul 		(char *)&wait_time);
409e8636dfdSBill Paul 	if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
4109233c4d9SJason Evans 		_fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
411e8636dfdSBill Paul 
412e8636dfdSBill Paul 	return (kcp->client);
413e8636dfdSBill Paul }
414e8636dfdSBill Paul 
415e8636dfdSBill Paul /* returns  0 on failure, 1 on success */
416e8636dfdSBill Paul 
417e8636dfdSBill Paul static int
41868895e38SCraig Rodrigues key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt,
41968895e38SCraig Rodrigues     void *rslt)
420e8636dfdSBill Paul {
421e8636dfdSBill Paul 	CLIENT *clnt;
422e8636dfdSBill Paul 	struct timeval wait_time;
423e8636dfdSBill Paul 
424e8636dfdSBill Paul 	if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
425e8636dfdSBill Paul 		cryptkeyres *res;
426e8636dfdSBill Paul 		res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
427e8636dfdSBill Paul 		*(cryptkeyres*)rslt = *res;
428e8636dfdSBill Paul 		return (1);
429e8636dfdSBill Paul 	} else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
430e8636dfdSBill Paul 		cryptkeyres *res;
431e8636dfdSBill Paul 		res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
432e8636dfdSBill Paul 		*(cryptkeyres*)rslt = *res;
433e8636dfdSBill Paul 		return (1);
434e8636dfdSBill Paul 	} else if (proc == KEY_GEN && __key_gendes_LOCAL) {
435e8636dfdSBill Paul 		des_block *res;
436e8636dfdSBill Paul 		res = (*__key_gendes_LOCAL)(geteuid(), 0);
437e8636dfdSBill Paul 		*(des_block*)rslt = *res;
438e8636dfdSBill Paul 		return (1);
439e8636dfdSBill Paul 	}
440e8636dfdSBill Paul 
441e8636dfdSBill Paul 	if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
442e8636dfdSBill Paul 	    (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
443e8636dfdSBill Paul 	    (proc == KEY_GET_CONV))
444e8636dfdSBill Paul 		clnt = getkeyserv_handle(2); /* talk to version 2 */
445e8636dfdSBill Paul 	else
446e8636dfdSBill Paul 		clnt = getkeyserv_handle(1); /* talk to version 1 */
447e8636dfdSBill Paul 
448e8636dfdSBill Paul 	if (clnt == NULL) {
449e8636dfdSBill Paul 		return (0);
450e8636dfdSBill Paul 	}
451e8636dfdSBill Paul 
452e8636dfdSBill Paul 	wait_time.tv_sec = TOTAL_TIMEOUT;
453e8636dfdSBill Paul 	wait_time.tv_usec = 0;
454e8636dfdSBill Paul 
455e8636dfdSBill Paul 	if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
456e8636dfdSBill Paul 		wait_time) == RPC_SUCCESS) {
457e8636dfdSBill Paul 		return (1);
458e8636dfdSBill Paul 	} else {
459e8636dfdSBill Paul 		return (0);
460e8636dfdSBill Paul 	}
461e8636dfdSBill Paul }
462