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 /*
35e8636dfdSBill Paul * key_call.c, Interface to keyserver
36e8636dfdSBill Paul *
37e8636dfdSBill Paul * setsecretkey(key) - set your secret key
38e8636dfdSBill Paul * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
39e8636dfdSBill Paul * decryptsessionkey(agent, deskey) - decrypt ditto
40e8636dfdSBill Paul * gendeskey(deskey) - generate a secure des key
41e8636dfdSBill Paul */
42e8636dfdSBill Paul
43d201fe46SDaniel Eischen #include "namespace.h"
449f5afc13SIan Dowse #include "reentrant.h"
45e8636dfdSBill Paul #include <stdio.h>
46e8636dfdSBill Paul #include <stdlib.h>
47e8636dfdSBill Paul #include <unistd.h>
48e8636dfdSBill Paul #include <errno.h>
49e8636dfdSBill Paul #include <rpc/rpc.h>
50e8636dfdSBill Paul #include <rpc/auth.h>
51e8636dfdSBill Paul #include <rpc/auth_unix.h>
52e8636dfdSBill Paul #include <rpc/key_prot.h>
53e8636dfdSBill Paul #include <string.h>
548360efbdSAlfred Perlstein #include <netconfig.h>
55e8636dfdSBill Paul #include <sys/utsname.h>
56e8636dfdSBill Paul #include <stdlib.h>
57e8636dfdSBill Paul #include <signal.h>
58e8636dfdSBill Paul #include <sys/wait.h>
59e8636dfdSBill Paul #include <sys/fcntl.h>
60d201fe46SDaniel Eischen #include "un-namespace.h"
61235baf26SDaniel Eischen #include "mt_misc.h"
62e8636dfdSBill Paul
63e8636dfdSBill Paul
64e8636dfdSBill Paul #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
65e8636dfdSBill Paul #define KEY_NRETRY 12 /* number of retries */
66e8636dfdSBill Paul
67e8636dfdSBill Paul #ifdef DEBUG
68e8636dfdSBill Paul #define debug(msg) (void) fprintf(stderr, "%s\n", msg);
69e8636dfdSBill Paul #else
70e8636dfdSBill Paul #define debug(msg)
71e8636dfdSBill Paul #endif /* DEBUG */
72e8636dfdSBill Paul
73e8636dfdSBill Paul /*
74e8636dfdSBill Paul * Hack to allow the keyserver to use AUTH_DES (for authenticated
75e8636dfdSBill Paul * NIS+ calls, for example). The only functions that get called
76e8636dfdSBill Paul * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
77e8636dfdSBill Paul *
78e8636dfdSBill Paul * The approach is to have the keyserver fill in pointers to local
79e8636dfdSBill Paul * implementations of these functions, and to call those in key_call().
80e8636dfdSBill Paul */
81e8636dfdSBill Paul
8267903135SCraig Rodrigues cryptkeyres *(*__key_encryptsession_pk_LOCAL)(uid_t, void *arg) = 0;
8367903135SCraig Rodrigues cryptkeyres *(*__key_decryptsession_pk_LOCAL)(uid_t, void *arg) = 0;
8467903135SCraig Rodrigues des_block *(*__key_gendes_LOCAL)(uid_t, void *) = 0;
85e8636dfdSBill Paul
86f249dbccSDag-Erling Smørgrav static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
87e8636dfdSBill Paul
88e8636dfdSBill Paul int
key_setsecret(const char * secretkey)8968895e38SCraig Rodrigues key_setsecret(const char *secretkey)
90e8636dfdSBill Paul {
91e8636dfdSBill Paul keystatus status;
92e8636dfdSBill Paul
93c549fd46SAlfred Perlstein if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
94c549fd46SAlfred Perlstein (void *)secretkey,
95f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_keystatus, &status)) {
96e8636dfdSBill Paul return (-1);
97e8636dfdSBill Paul }
98e8636dfdSBill Paul if (status != KEY_SUCCESS) {
99e8636dfdSBill Paul debug("set status is nonzero");
100e8636dfdSBill Paul return (-1);
101e8636dfdSBill Paul }
102e8636dfdSBill Paul return (0);
103e8636dfdSBill Paul }
104e8636dfdSBill Paul
105e8636dfdSBill Paul
106e8636dfdSBill Paul /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
107e8636dfdSBill Paul * stored for the caller's effective uid; it returns 0 otherwise
108e8636dfdSBill Paul *
109e8636dfdSBill Paul * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
110e8636dfdSBill Paul * be using it, because it allows them to get the user's secret key.
111e8636dfdSBill Paul */
112e8636dfdSBill Paul
113e8636dfdSBill Paul int
key_secretkey_is_set(void)114e8636dfdSBill Paul key_secretkey_is_set(void)
115e8636dfdSBill Paul {
116e8636dfdSBill Paul struct key_netstres kres;
117e8636dfdSBill Paul
118e8636dfdSBill Paul memset((void*)&kres, 0, sizeof (kres));
119f249dbccSDag-Erling Smørgrav if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
120f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_key_netstres, &kres) &&
121e8636dfdSBill Paul (kres.status == KEY_SUCCESS) &&
122e8636dfdSBill Paul (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
123e8636dfdSBill Paul /* avoid leaving secret key in memory */
124e8636dfdSBill Paul memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
125e8636dfdSBill Paul return (1);
126e8636dfdSBill Paul }
127e8636dfdSBill Paul return (0);
128e8636dfdSBill Paul }
129e8636dfdSBill Paul
130e8636dfdSBill Paul int
key_encryptsession_pk(char * remotename,netobj * remotekey,des_block * deskey)13168895e38SCraig Rodrigues key_encryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
132e8636dfdSBill Paul {
133e8636dfdSBill Paul cryptkeyarg2 arg;
134e8636dfdSBill Paul cryptkeyres res;
135e8636dfdSBill Paul
136e8636dfdSBill Paul arg.remotename = remotename;
137e8636dfdSBill Paul arg.remotekey = *remotekey;
138e8636dfdSBill Paul arg.deskey = *deskey;
139f249dbccSDag-Erling Smørgrav if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
140f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_cryptkeyres, &res)) {
141e8636dfdSBill Paul return (-1);
142e8636dfdSBill Paul }
143e8636dfdSBill Paul if (res.status != KEY_SUCCESS) {
144e8636dfdSBill Paul debug("encrypt status is nonzero");
145e8636dfdSBill Paul return (-1);
146e8636dfdSBill Paul }
147e8636dfdSBill Paul *deskey = res.cryptkeyres_u.deskey;
148e8636dfdSBill Paul return (0);
149e8636dfdSBill Paul }
150e8636dfdSBill Paul
151e8636dfdSBill Paul int
key_decryptsession_pk(char * remotename,netobj * remotekey,des_block * deskey)15268895e38SCraig Rodrigues key_decryptsession_pk(char *remotename, netobj *remotekey, des_block *deskey)
153e8636dfdSBill Paul {
154e8636dfdSBill Paul cryptkeyarg2 arg;
155e8636dfdSBill Paul cryptkeyres res;
156e8636dfdSBill Paul
157e8636dfdSBill Paul arg.remotename = remotename;
158e8636dfdSBill Paul arg.remotekey = *remotekey;
159e8636dfdSBill Paul arg.deskey = *deskey;
160f249dbccSDag-Erling Smørgrav if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
161f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_cryptkeyres, &res)) {
162e8636dfdSBill Paul return (-1);
163e8636dfdSBill Paul }
164e8636dfdSBill Paul if (res.status != KEY_SUCCESS) {
165e8636dfdSBill Paul debug("decrypt status is nonzero");
166e8636dfdSBill Paul return (-1);
167e8636dfdSBill Paul }
168e8636dfdSBill Paul *deskey = res.cryptkeyres_u.deskey;
169e8636dfdSBill Paul return (0);
170e8636dfdSBill Paul }
171e8636dfdSBill Paul
172e8636dfdSBill Paul int
key_encryptsession(const char * remotename,des_block * deskey)17368895e38SCraig Rodrigues key_encryptsession(const char *remotename, des_block *deskey)
174e8636dfdSBill Paul {
175e8636dfdSBill Paul cryptkeyarg arg;
176e8636dfdSBill Paul cryptkeyres res;
177e8636dfdSBill Paul
178e8636dfdSBill Paul arg.remotename = (char *) remotename;
179e8636dfdSBill Paul arg.deskey = *deskey;
180f249dbccSDag-Erling Smørgrav if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
181f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_cryptkeyres, &res)) {
182e8636dfdSBill Paul return (-1);
183e8636dfdSBill Paul }
184e8636dfdSBill Paul if (res.status != KEY_SUCCESS) {
185e8636dfdSBill Paul debug("encrypt status is nonzero");
186e8636dfdSBill Paul return (-1);
187e8636dfdSBill Paul }
188e8636dfdSBill Paul *deskey = res.cryptkeyres_u.deskey;
189e8636dfdSBill Paul return (0);
190e8636dfdSBill Paul }
191e8636dfdSBill Paul
192e8636dfdSBill Paul int
key_decryptsession(const char * remotename,des_block * deskey)19368895e38SCraig Rodrigues key_decryptsession(const char *remotename, des_block *deskey)
194e8636dfdSBill Paul {
195e8636dfdSBill Paul cryptkeyarg arg;
196e8636dfdSBill Paul cryptkeyres res;
197e8636dfdSBill Paul
198e8636dfdSBill Paul arg.remotename = (char *) remotename;
199e8636dfdSBill Paul arg.deskey = *deskey;
200f249dbccSDag-Erling Smørgrav if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
201f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_cryptkeyres, &res)) {
202e8636dfdSBill Paul return (-1);
203e8636dfdSBill Paul }
204e8636dfdSBill Paul if (res.status != KEY_SUCCESS) {
205e8636dfdSBill Paul debug("decrypt status is nonzero");
206e8636dfdSBill Paul return (-1);
207e8636dfdSBill Paul }
208e8636dfdSBill Paul *deskey = res.cryptkeyres_u.deskey;
209e8636dfdSBill Paul return (0);
210e8636dfdSBill Paul }
211e8636dfdSBill Paul
212e8636dfdSBill Paul int
key_gendes(des_block * key)21368895e38SCraig Rodrigues key_gendes(des_block *key)
214e8636dfdSBill Paul {
215f249dbccSDag-Erling Smørgrav if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
216f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_des_block, key)) {
217e8636dfdSBill Paul return (-1);
218e8636dfdSBill Paul }
219e8636dfdSBill Paul return (0);
220e8636dfdSBill Paul }
221e8636dfdSBill Paul
222e8636dfdSBill Paul int
key_setnet(struct key_netstarg * arg)22368895e38SCraig Rodrigues key_setnet(struct key_netstarg *arg)
224e8636dfdSBill Paul {
225e8636dfdSBill Paul keystatus status;
226e8636dfdSBill Paul
227e8636dfdSBill Paul
228f249dbccSDag-Erling Smørgrav if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
229f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_keystatus, &status)){
230e8636dfdSBill Paul return (-1);
231e8636dfdSBill Paul }
232e8636dfdSBill Paul
233e8636dfdSBill Paul if (status != KEY_SUCCESS) {
234e8636dfdSBill Paul debug("key_setnet status is nonzero");
235e8636dfdSBill Paul return (-1);
236e8636dfdSBill Paul }
237e8636dfdSBill Paul return (1);
238e8636dfdSBill Paul }
239e8636dfdSBill Paul
240e8636dfdSBill Paul
241e8636dfdSBill Paul int
key_get_conv(char * pkey,des_block * deskey)24268895e38SCraig Rodrigues key_get_conv(char *pkey, des_block *deskey)
243e8636dfdSBill Paul {
244e8636dfdSBill Paul cryptkeyres res;
245e8636dfdSBill Paul
246f249dbccSDag-Erling Smørgrav if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
247f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_cryptkeyres, &res)) {
248e8636dfdSBill Paul return (-1);
249e8636dfdSBill Paul }
250e8636dfdSBill Paul if (res.status != KEY_SUCCESS) {
251e8636dfdSBill Paul debug("get_conv status is nonzero");
252e8636dfdSBill Paul return (-1);
253e8636dfdSBill Paul }
254e8636dfdSBill Paul *deskey = res.cryptkeyres_u.deskey;
255e8636dfdSBill Paul return (0);
256e8636dfdSBill Paul }
257e8636dfdSBill Paul
258e8636dfdSBill Paul struct key_call_private {
259e8636dfdSBill Paul CLIENT *client; /* Client handle */
260e8636dfdSBill Paul pid_t pid; /* process-id at moment of creation */
261e8636dfdSBill Paul uid_t uid; /* user-id at last authorization */
262e8636dfdSBill Paul };
263e8636dfdSBill Paul static struct key_call_private *key_call_private_main = NULL;
2646f88d2a8SJohn Baldwin static thread_key_t key_call_key;
2656f88d2a8SJohn Baldwin static once_t key_call_once = ONCE_INITIALIZER;
2666f88d2a8SJohn Baldwin static int key_call_key_error;
267e8636dfdSBill Paul
268e8636dfdSBill Paul static void
key_call_destroy(void * vp)269e8636dfdSBill Paul key_call_destroy(void *vp)
270e8636dfdSBill Paul {
2718fb3f3f6SDavid E. O'Brien struct key_call_private *kcp = (struct key_call_private *)vp;
272e8636dfdSBill Paul
273e8636dfdSBill Paul if (kcp) {
274e8636dfdSBill Paul if (kcp->client)
275e8636dfdSBill Paul clnt_destroy(kcp->client);
276e8636dfdSBill Paul free(kcp);
277e8636dfdSBill Paul }
278e8636dfdSBill Paul }
279e8636dfdSBill Paul
2806f88d2a8SJohn Baldwin static void
key_call_init(void)2816f88d2a8SJohn Baldwin key_call_init(void)
2826f88d2a8SJohn Baldwin {
2836f88d2a8SJohn Baldwin
2846f88d2a8SJohn Baldwin key_call_key_error = thr_keycreate(&key_call_key, key_call_destroy);
2856f88d2a8SJohn Baldwin }
2866f88d2a8SJohn Baldwin
287e8636dfdSBill Paul /*
288e8636dfdSBill Paul * Keep the handle cached. This call may be made quite often.
289e8636dfdSBill Paul */
290e8636dfdSBill Paul static CLIENT *
getkeyserv_handle(int vers)29168895e38SCraig Rodrigues getkeyserv_handle(int vers)
292e8636dfdSBill Paul {
2938360efbdSAlfred Perlstein void *localhandle;
2948360efbdSAlfred Perlstein struct netconfig *nconf;
2958360efbdSAlfred Perlstein struct netconfig *tpconf;
2960c0349bfSGarrett Wollman struct key_call_private *kcp;
297e8636dfdSBill Paul struct timeval wait_time;
2988360efbdSAlfred Perlstein struct utsname u;
2998360efbdSAlfred Perlstein int main_thread;
300e8636dfdSBill Paul int fd;
301e8636dfdSBill Paul
302e8636dfdSBill Paul #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
303e8636dfdSBill Paul #define TOTAL_TRIES 5 /* Number of tries */
304e8636dfdSBill Paul
3058360efbdSAlfred Perlstein if ((main_thread = thr_main())) {
3068360efbdSAlfred Perlstein kcp = key_call_private_main;
3078360efbdSAlfred Perlstein } else {
3086f88d2a8SJohn Baldwin if (thr_once(&key_call_once, key_call_init) != 0 ||
3096f88d2a8SJohn Baldwin key_call_key_error != 0)
3106f88d2a8SJohn Baldwin return ((CLIENT *) NULL);
3118360efbdSAlfred Perlstein kcp = (struct key_call_private *)thr_getspecific(key_call_key);
3128360efbdSAlfred Perlstein }
313e8636dfdSBill Paul if (kcp == (struct key_call_private *)NULL) {
314e8636dfdSBill Paul kcp = (struct key_call_private *)malloc(sizeof (*kcp));
315e8636dfdSBill Paul if (kcp == (struct key_call_private *)NULL) {
316e8636dfdSBill Paul return ((CLIENT *) NULL);
317e8636dfdSBill Paul }
3188360efbdSAlfred Perlstein if (main_thread)
319e8636dfdSBill Paul key_call_private_main = kcp;
3208360efbdSAlfred Perlstein else
3218360efbdSAlfred Perlstein thr_setspecific(key_call_key, (void *) kcp);
322e8636dfdSBill Paul kcp->client = NULL;
323e8636dfdSBill Paul }
324e8636dfdSBill Paul
325e8636dfdSBill Paul /* if pid has changed, destroy client and rebuild */
326e8636dfdSBill Paul if (kcp->client != NULL && kcp->pid != getpid()) {
327e8636dfdSBill Paul clnt_destroy(kcp->client);
328e8636dfdSBill Paul kcp->client = NULL;
329e8636dfdSBill Paul }
330e8636dfdSBill Paul
331e8636dfdSBill Paul if (kcp->client != NULL) {
332e8636dfdSBill Paul /* if uid has changed, build client handle again */
333e8636dfdSBill Paul if (kcp->uid != geteuid()) {
334e8636dfdSBill Paul kcp->uid = geteuid();
335e8636dfdSBill Paul auth_destroy(kcp->client->cl_auth);
336e8636dfdSBill Paul kcp->client->cl_auth =
337e8636dfdSBill Paul authsys_create("", kcp->uid, 0, 0, NULL);
338e8636dfdSBill Paul if (kcp->client->cl_auth == NULL) {
339e8636dfdSBill Paul clnt_destroy(kcp->client);
340e8636dfdSBill Paul kcp->client = NULL;
341e8636dfdSBill Paul return ((CLIENT *) NULL);
342e8636dfdSBill Paul }
343e8636dfdSBill Paul }
344e8636dfdSBill Paul /* Change the version number to the new one */
345e8636dfdSBill Paul clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
346e8636dfdSBill Paul return (kcp->client);
347e8636dfdSBill Paul }
3488360efbdSAlfred Perlstein if (!(localhandle = setnetconfig())) {
3498360efbdSAlfred Perlstein return ((CLIENT *) NULL);
3508360efbdSAlfred Perlstein }
3518360efbdSAlfred Perlstein tpconf = NULL;
35206fa7267SDavid E. O'Brien #if defined(__FreeBSD__)
3538360efbdSAlfred Perlstein if (uname(&u) == -1)
35406fa7267SDavid E. O'Brien #else
35506fa7267SDavid E. O'Brien #if defined(i386)
35606fa7267SDavid E. O'Brien if (_nuname(&u) == -1)
35706fa7267SDavid E. O'Brien #elif defined(sparc)
35806fa7267SDavid E. O'Brien if (_uname(&u) == -1)
35906fa7267SDavid E. O'Brien #else
36006fa7267SDavid E. O'Brien #error Unknown architecture!
36106fa7267SDavid E. O'Brien #endif
36206fa7267SDavid E. O'Brien #endif
3638360efbdSAlfred Perlstein {
3648360efbdSAlfred Perlstein endnetconfig(localhandle);
3658360efbdSAlfred Perlstein return ((CLIENT *) NULL);
3668360efbdSAlfred Perlstein }
3678d630135SAlfred Perlstein while ((nconf = getnetconfig(localhandle)) != NULL) {
3688360efbdSAlfred Perlstein if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
3698360efbdSAlfred Perlstein /*
3708360efbdSAlfred Perlstein * We use COTS_ORD here so that the caller can
3718360efbdSAlfred Perlstein * find out immediately if the server is dead.
3728360efbdSAlfred Perlstein */
3738360efbdSAlfred Perlstein if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
3748360efbdSAlfred Perlstein kcp->client = clnt_tp_create(u.nodename,
3758360efbdSAlfred Perlstein KEY_PROG, vers, nconf);
3768360efbdSAlfred Perlstein if (kcp->client)
3778360efbdSAlfred Perlstein break;
3788360efbdSAlfred Perlstein } else {
3798360efbdSAlfred Perlstein tpconf = nconf;
3808360efbdSAlfred Perlstein }
3818360efbdSAlfred Perlstein }
3828360efbdSAlfred Perlstein }
3838360efbdSAlfred Perlstein if ((kcp->client == (CLIENT *) NULL) && (tpconf))
3848360efbdSAlfred Perlstein /* Now, try the CLTS or COTS loopback transport */
3858360efbdSAlfred Perlstein kcp->client = clnt_tp_create(u.nodename,
3868360efbdSAlfred Perlstein KEY_PROG, vers, tpconf);
3878360efbdSAlfred Perlstein endnetconfig(localhandle);
388e8636dfdSBill Paul
389e8636dfdSBill Paul if (kcp->client == (CLIENT *) NULL) {
390e8636dfdSBill Paul return ((CLIENT *) NULL);
391e8636dfdSBill Paul }
392e8636dfdSBill Paul kcp->uid = geteuid();
393e8636dfdSBill Paul kcp->pid = getpid();
394e8636dfdSBill Paul kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
395e8636dfdSBill Paul if (kcp->client->cl_auth == NULL) {
396e8636dfdSBill Paul clnt_destroy(kcp->client);
397e8636dfdSBill Paul kcp->client = NULL;
398e8636dfdSBill Paul return ((CLIENT *) NULL);
399e8636dfdSBill Paul }
400e8636dfdSBill Paul
401e8636dfdSBill Paul wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
402e8636dfdSBill Paul wait_time.tv_usec = 0;
403e8636dfdSBill Paul (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
404e8636dfdSBill Paul (char *)&wait_time);
405e8636dfdSBill Paul if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
4069233c4d9SJason Evans _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
407e8636dfdSBill Paul
408e8636dfdSBill Paul return (kcp->client);
409e8636dfdSBill Paul }
410e8636dfdSBill Paul
411e8636dfdSBill Paul /* returns 0 on failure, 1 on success */
412e8636dfdSBill Paul
413e8636dfdSBill Paul static int
key_call(u_long proc,xdrproc_t xdr_arg,void * arg,xdrproc_t xdr_rslt,void * rslt)41468895e38SCraig Rodrigues key_call(u_long proc, xdrproc_t xdr_arg, void *arg, xdrproc_t xdr_rslt,
41568895e38SCraig Rodrigues void *rslt)
416e8636dfdSBill Paul {
417e8636dfdSBill Paul CLIENT *clnt;
418e8636dfdSBill Paul struct timeval wait_time;
419e8636dfdSBill Paul
420e8636dfdSBill Paul if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
421e8636dfdSBill Paul cryptkeyres *res;
422e8636dfdSBill Paul res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
423e8636dfdSBill Paul *(cryptkeyres*)rslt = *res;
424e8636dfdSBill Paul return (1);
425e8636dfdSBill Paul } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
426e8636dfdSBill Paul cryptkeyres *res;
427e8636dfdSBill Paul res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
428e8636dfdSBill Paul *(cryptkeyres*)rslt = *res;
429e8636dfdSBill Paul return (1);
430e8636dfdSBill Paul } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
431e8636dfdSBill Paul des_block *res;
432e8636dfdSBill Paul res = (*__key_gendes_LOCAL)(geteuid(), 0);
433e8636dfdSBill Paul *(des_block*)rslt = *res;
434e8636dfdSBill Paul return (1);
435e8636dfdSBill Paul }
436e8636dfdSBill Paul
437e8636dfdSBill Paul if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
438e8636dfdSBill Paul (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
439e8636dfdSBill Paul (proc == KEY_GET_CONV))
440e8636dfdSBill Paul clnt = getkeyserv_handle(2); /* talk to version 2 */
441e8636dfdSBill Paul else
442e8636dfdSBill Paul clnt = getkeyserv_handle(1); /* talk to version 1 */
443e8636dfdSBill Paul
444e8636dfdSBill Paul if (clnt == NULL) {
445e8636dfdSBill Paul return (0);
446e8636dfdSBill Paul }
447e8636dfdSBill Paul
448e8636dfdSBill Paul wait_time.tv_sec = TOTAL_TIMEOUT;
449e8636dfdSBill Paul wait_time.tv_usec = 0;
450e8636dfdSBill Paul
451e8636dfdSBill Paul if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
452e8636dfdSBill Paul wait_time) == RPC_SUCCESS) {
453e8636dfdSBill Paul return (1);
454e8636dfdSBill Paul } else {
455e8636dfdSBill Paul return (0);
456e8636dfdSBill Paul }
457e8636dfdSBill Paul }
458