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