Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/key_call.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28 /*
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
30 */
31
32
33
34 /*
35 * key_call.c, Interface to keyserver
36 *
37 * setsecretkey(key) - set your secret key
38 * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
39 * decryptsessionkey(agent, deskey) - decrypt ditto
40 * gendeskey(deskey) - generate a secure des key
41 */
42
43 #include <pthread.h>
44 #include <reentrant.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <rpc/rpc.h>
50 #include <rpc/auth.h>
51 #include <rpc/auth_unix.h>
52 #include <rpc/key_prot.h>
53 #include <string.h>
54 #include <netconfig.h>
55 #include <sys/utsname.h>
56 #include <stdlib.h>
57 #include <signal.h>
58 #include <sys/wait.h>
59 #include <fcntl.h>
60
61 #include "debug.h"
62
63 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
64 #define KEY_NRETRY 12 /* number of retries */
65
66 /*
67 * Hack to allow the keyserver to use AUTH_DES (for authenticated
68 * NIS+ calls, for example). The only functions that get called
69 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
70 *
71 * The approach is to have the keyserver fill in pointers to local
72 * implementations of these functions, and to call those in key_call().
73 */
74
75 cryptkeyres *(*__key_encryptsession_pk_LOCAL)() = 0;
76 cryptkeyres *(*__key_decryptsession_pk_LOCAL)() = 0;
77 des_block *(*__key_gendes_LOCAL)() = 0;
78
79 static int key_call( u_long, xdrproc_t, void *, xdrproc_t, void *);
80
81 int
82 key_setsecret(secretkey)
83 const char *secretkey;
84 {
85 keystatus status;
86
87 if (!key_call((u_long) KEY_SET, (xdrproc_t)xdr_keybuf,
88 (void *)secretkey,
89 (xdrproc_t)xdr_keystatus, &status)) {
90 return (-1);
91 }
92 if (status != KEY_SUCCESS) {
93 LIBTIRPC_DEBUG(1, ("key_setsecret: set status is nonzero"));
94 return (-1);
95 }
96 return (0);
97 }
98
99
100 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
101 * stored for the caller's effective uid; it returns 0 otherwise
102 *
103 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
104 * be using it, because it allows them to get the user's secret key.
105 */
106
107 int
108 key_secretkey_is_set(void)
109 {
110 struct key_netstres kres;
111
112 memset((void*)&kres, 0, sizeof (kres));
113 if (key_call((u_long) KEY_NET_GET, (xdrproc_t)xdr_void, NULL,
114 (xdrproc_t)xdr_key_netstres, &kres) &&
115 (kres.status == KEY_SUCCESS) &&
116 (kres.key_netstres_u.knet.st_priv_key[0] != 0)) {
117 /* avoid leaving secret key in memory */
118 memset(kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
119 return (1);
120 }
121 return (0);
122 }
123
124 int
125 key_encryptsession_pk(remotename, remotekey, deskey)
126 char *remotename;
127 netobj *remotekey;
128 des_block *deskey;
129 {
130 cryptkeyarg2 arg;
131 cryptkeyres res;
132
133 arg.remotename = remotename;
134 arg.remotekey = *remotekey;
135 arg.deskey = *deskey;
136 if (!key_call((u_long)KEY_ENCRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
137 (xdrproc_t)xdr_cryptkeyres, &res)) {
138 return (-1);
139 }
140 if (res.status != KEY_SUCCESS) {
141 LIBTIRPC_DEBUG(1, ("key_encryptsession_pk: encrypt status is nonzero"));
142 return (-1);
143 }
144 *deskey = res.cryptkeyres_u.deskey;
145 return (0);
146 }
147
148 int
149 key_decryptsession_pk(remotename, remotekey, deskey)
150 char *remotename;
151 netobj *remotekey;
152 des_block *deskey;
153 {
154 cryptkeyarg2 arg;
155 cryptkeyres res;
156
157 arg.remotename = remotename;
158 arg.remotekey = *remotekey;
159 arg.deskey = *deskey;
160 if (!key_call((u_long)KEY_DECRYPT_PK, (xdrproc_t)xdr_cryptkeyarg2, &arg,
161 (xdrproc_t)xdr_cryptkeyres, &res)) {
162 return (-1);
163 }
164 if (res.status != KEY_SUCCESS) {
165 LIBTIRPC_DEBUG(1, ("key_decryptsession_pk: decrypt status is nonzero"));
166 return (-1);
167 }
168 *deskey = res.cryptkeyres_u.deskey;
169 return (0);
170 }
171
172 int
173 key_encryptsession(remotename, deskey)
174 const char *remotename;
175 des_block *deskey;
176 {
177 cryptkeyarg arg;
178 cryptkeyres res;
179
180 arg.remotename = (char *) remotename;
181 arg.deskey = *deskey;
182 if (!key_call((u_long)KEY_ENCRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
183 (xdrproc_t)xdr_cryptkeyres, &res)) {
184 return (-1);
185 }
186 if (res.status != KEY_SUCCESS) {
187 LIBTIRPC_DEBUG(1, ("key_encryptsession: encrypt status is nonzero"));
188 return (-1);
189 }
190 *deskey = res.cryptkeyres_u.deskey;
191 return (0);
192 }
193
194 int
195 key_decryptsession(remotename, deskey)
196 const char *remotename;
197 des_block *deskey;
198 {
199 cryptkeyarg arg;
200 cryptkeyres res;
201
202 arg.remotename = (char *) remotename;
203 arg.deskey = *deskey;
204 if (!key_call((u_long)KEY_DECRYPT, (xdrproc_t)xdr_cryptkeyarg, &arg,
205 (xdrproc_t)xdr_cryptkeyres, &res)) {
206 return (-1);
207 }
208 if (res.status != KEY_SUCCESS) {
209 LIBTIRPC_DEBUG(1, ("key_decryptsession: decrypt status is nonzero"));
210 return (-1);
211 }
212 *deskey = res.cryptkeyres_u.deskey;
213 return (0);
214 }
215
216 int
217 key_gendes(key)
218 des_block *key;
219 {
220 if (!key_call((u_long)KEY_GEN, (xdrproc_t)xdr_void, NULL,
221 (xdrproc_t)xdr_des_block, key)) {
222 return (-1);
223 }
224 return (0);
225 }
226
227 int
228 key_setnet(arg)
229 struct key_netstarg *arg;
230 {
231 keystatus status;
232
233
234 if (!key_call((u_long) KEY_NET_PUT, (xdrproc_t)xdr_key_netstarg, arg,
235 (xdrproc_t)xdr_keystatus, &status)){
236 return (-1);
237 }
238
239 if (status != KEY_SUCCESS) {
240 LIBTIRPC_DEBUG(1, ("key_setnet: key_setnet status is nonzero"));
241 return (-1);
242 }
243 return (1);
244 }
245
246
247 int
248 key_get_conv(pkey, deskey)
249 char *pkey;
250 des_block *deskey;
251 {
252 cryptkeyres res;
253
254 if (!key_call((u_long) KEY_GET_CONV, (xdrproc_t)xdr_keybuf, pkey,
255 (xdrproc_t)xdr_cryptkeyres, &res)) {
256 return (-1);
257 }
258 if (res.status != KEY_SUCCESS) {
259 LIBTIRPC_DEBUG(1, ("key_get_conv: get_conv status is nonzero"));
260 return (-1);
261 }
262 *deskey = res.cryptkeyres_u.deskey;
263 return (0);
264 }
265
266 struct key_call_private {
267 CLIENT *client; /* Client handle */
268 pid_t pid; /* process-id at moment of creation */
269 uid_t uid; /* user-id at last authorization */
270 };
271 static struct key_call_private *key_call_private_main = NULL;
272
273 static void
274 key_call_destroy(void *vp)
275 {
276 struct key_call_private *kcp = (struct key_call_private *)vp;
277
278 if (kcp) {
279 if (kcp->client)
280 clnt_destroy(kcp->client);
281 free(kcp);
282 }
283 }
284
285 /*
286 * Keep the handle cached. This call may be made quite often.
287 */
288 static CLIENT *
289 getkeyserv_handle(vers)
290 int vers;
291 {
292 void *localhandle;
293 struct netconfig *nconf;
294 struct netconfig *tpconf;
295 struct key_call_private *kcp = key_call_private_main;
296 struct timeval wait_time;
297 struct utsname u;
298 int fd;
299 extern thread_key_t key_call_key;
300 extern mutex_t tsd_lock;
301
302 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
303 #define TOTAL_TRIES 5 /* Number of tries */
304
305 if (key_call_key == -1) {
306 mutex_lock(&tsd_lock);
307 if (key_call_key == -1)
308 thr_keycreate(&key_call_key, key_call_destroy);
309 mutex_unlock(&tsd_lock);
310 }
311 kcp = (struct key_call_private *)thr_getspecific(key_call_key);
312 if (kcp == (struct key_call_private *)NULL) {
313 kcp = (struct key_call_private *)malloc(sizeof (*kcp));
314 if (kcp == (struct key_call_private *)NULL) {
315 return ((CLIENT *) NULL);
316 }
317 thr_setspecific(key_call_key, (void *) kcp);
318 kcp->client = NULL;
319 }
320
321 /* if pid has changed, destroy client and rebuild */
322 if (kcp->client != NULL && kcp->pid != getpid()) {
323 clnt_destroy(kcp->client);
324 kcp->client = NULL;
325 }
326
327 if (kcp->client != NULL) {
328 /* if uid has changed, build client handle again */
329 if (kcp->uid != geteuid()) {
330 kcp->uid = geteuid();
331 auth_destroy(kcp->client->cl_auth);
332 kcp->client->cl_auth =
333 authsys_create("", kcp->uid, 0, 0, NULL);
334 if (kcp->client->cl_auth == NULL) {
335 clnt_destroy(kcp->client);
336 kcp->client = NULL;
337 return ((CLIENT *) NULL);
338 }
339 }
340 /* Change the version number to the new one */
341 clnt_control(kcp->client, CLSET_VERS, (void *)&vers);
342 return (kcp->client);
343 }
344 if (!(localhandle = setnetconfig())) {
345 return ((CLIENT *) NULL);
346 }
347 tpconf = NULL;
348 if (uname(&u) == -1) {
349 endnetconfig(localhandle);
350 return ((CLIENT *) NULL);
351 }
352 while ((nconf = getnetconfig(localhandle)) != NULL) {
353 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
354 /*
355 * We use COTS_ORD here so that the caller can
356 * find out immediately if the server is dead.
357 */
358 if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
359 kcp->client = clnt_tp_create(u.nodename,
360 KEY_PROG, vers, nconf);
361 if (kcp->client)
362 break;
363 } else {
364 tpconf = nconf;
365 }
366 }
367 }
368 if ((kcp->client == (CLIENT *) NULL) && (tpconf))
369 /* Now, try the CLTS or COTS loopback transport */
370 kcp->client = clnt_tp_create(u.nodename,
371 KEY_PROG, vers, tpconf);
372 endnetconfig(localhandle);
373
374 if (kcp->client == (CLIENT *) NULL) {
375 return ((CLIENT *) NULL);
376 }
377 kcp->uid = geteuid();
378 kcp->pid = getpid();
379 kcp->client->cl_auth = authsys_create("", kcp->uid, 0, 0, NULL);
380 if (kcp->client->cl_auth == NULL) {
381 clnt_destroy(kcp->client);
382 kcp->client = NULL;
383 return ((CLIENT *) NULL);
384 }
385
386 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
387 wait_time.tv_usec = 0;
388 (void) clnt_control(kcp->client, CLSET_RETRY_TIMEOUT,
389 (char *)&wait_time);
390 if (clnt_control(kcp->client, CLGET_FD, (char *)&fd))
391 fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
392
393 return (kcp->client);
394 }
395
396 /* returns 0 on failure, 1 on success */
397
398 static int
399 key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
400 u_long proc;
401 xdrproc_t xdr_arg;
402 void *arg;
403 xdrproc_t xdr_rslt;
404 void *rslt;
405 {
406 CLIENT *clnt;
407 struct timeval wait_time;
408
409 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL) {
410 cryptkeyres *res;
411 res = (*__key_encryptsession_pk_LOCAL)(geteuid(), arg);
412 *(cryptkeyres*)rslt = *res;
413 return (1);
414 } else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL) {
415 cryptkeyres *res;
416 res = (*__key_decryptsession_pk_LOCAL)(geteuid(), arg);
417 *(cryptkeyres*)rslt = *res;
418 return (1);
419 } else if (proc == KEY_GEN && __key_gendes_LOCAL) {
420 des_block *res;
421 res = (*__key_gendes_LOCAL)(geteuid(), 0);
422 *(des_block*)rslt = *res;
423 return (1);
424 }
425
426 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
427 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
428 (proc == KEY_GET_CONV))
429 clnt = getkeyserv_handle(2); /* talk to version 2 */
430 else
431 clnt = getkeyserv_handle(1); /* talk to version 1 */
432
433 if (clnt == NULL) {
434 return (0);
435 }
436
437 wait_time.tv_sec = TOTAL_TIMEOUT;
438 wait_time.tv_usec = 0;
439
440 if (clnt_call(clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
441 wait_time) == RPC_SUCCESS) {
442 return (1);
443 } else {
444 return (0);
445 }
446 }