Add more ptrace constants for AArch64 and PowerPC.
[glibc.git] / sunrpc / key_call.c
blobf13b0a7e4c3feb6a2446fe6919f92bc811fc43cf
1 /*
2 * Copyright (c) 2010, Oracle America, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * The original source is from the RPCSRC 4.0 package from Sun Microsystems.
33 * The Interface to keyserver protocoll 2, RPC over AF_UNIX and Linux/doors
34 * was added by Thorsten Kukuk <kukuk@suse.de>
35 * Since the Linux/doors project was stopped, I doubt that this code will
36 * ever be useful <kukuk@suse.de>.
39 #include <stdio.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <rpc/auth.h>
47 #include <sys/wait.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <rpc/key_prot.h>
51 #include <libc-lock.h>
52 #include <shlib-compat.h>
54 #define KEY_TIMEOUT 5 /* per-try timeout in seconds */
55 #define KEY_NRETRY 12 /* number of retries */
57 #define debug(msg) /* turn off debugging */
59 #ifndef SO_PASSCRED
60 extern int _openchild (const char *command, FILE **fto, FILE **ffrom);
61 #endif
63 static int key_call (u_long, xdrproc_t xdr_arg, char *,
64 xdrproc_t xdr_rslt, char *);
66 static const struct timeval trytimeout = {KEY_TIMEOUT, 0};
67 static const struct timeval tottimeout = {KEY_TIMEOUT *KEY_NRETRY, 0};
69 int
70 key_setsecret (char *secretkey)
72 keystatus status;
74 if (!key_call ((u_long) KEY_SET, (xdrproc_t) xdr_keybuf, secretkey,
75 (xdrproc_t) xdr_keystatus, (char *) &status))
76 return -1;
77 if (status != KEY_SUCCESS)
79 debug ("set status is nonzero");
80 return -1;
82 return 0;
84 libc_hidden_nolink_sunrpc (key_setsecret, GLIBC_2_1)
86 /* key_secretkey_is_set() returns 1 if the keyserver has a secret key
87 * stored for the caller's effective uid; it returns 0 otherwise
89 * N.B.: The KEY_NET_GET key call is undocumented. Applications shouldn't
90 * be using it, because it allows them to get the user's secret key.
92 int
93 key_secretkey_is_set (void)
95 struct key_netstres kres;
97 memset (&kres, 0, sizeof (kres));
98 if (key_call ((u_long) KEY_NET_GET, (xdrproc_t) xdr_void,
99 (char *) NULL, (xdrproc_t) xdr_key_netstres,
100 (char *) &kres) &&
101 (kres.status == KEY_SUCCESS) &&
102 (kres.key_netstres_u.knet.st_priv_key[0] != 0))
104 /* avoid leaving secret key in memory */
105 memset (kres.key_netstres_u.knet.st_priv_key, 0, HEXKEYBYTES);
106 return 1;
108 return 0;
110 #ifdef EXPORT_RPC_SYMBOLS
111 libc_hidden_def (key_secretkey_is_set)
112 #else
113 libc_hidden_nolink_sunrpc (key_secretkey_is_set, GLIBC_2_1)
114 #endif
117 key_encryptsession (char *remotename, des_block *deskey)
119 cryptkeyarg arg;
120 cryptkeyres res;
122 arg.remotename = remotename;
123 arg.deskey = *deskey;
124 if (!key_call ((u_long) KEY_ENCRYPT, (xdrproc_t) xdr_cryptkeyarg,
125 (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
126 (char *) &res))
127 return -1;
129 if (res.status != KEY_SUCCESS)
131 debug ("encrypt status is nonzero");
132 return -1;
134 *deskey = res.cryptkeyres_u.deskey;
135 return 0;
137 libc_hidden_nolink_sunrpc (key_encryptsession, GLIBC_2_1)
140 key_decryptsession (char *remotename, des_block *deskey)
142 cryptkeyarg arg;
143 cryptkeyres res;
145 arg.remotename = remotename;
146 arg.deskey = *deskey;
147 if (!key_call ((u_long) KEY_DECRYPT, (xdrproc_t) xdr_cryptkeyarg,
148 (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
149 (char *) &res))
150 return -1;
151 if (res.status != KEY_SUCCESS)
153 debug ("decrypt status is nonzero");
154 return -1;
156 *deskey = res.cryptkeyres_u.deskey;
157 return 0;
159 libc_hidden_nolink_sunrpc (key_decryptsession, GLIBC_2_1)
162 key_encryptsession_pk (char *remotename, netobj *remotekey,
163 des_block *deskey)
165 cryptkeyarg2 arg;
166 cryptkeyres res;
168 arg.remotename = remotename;
169 arg.remotekey = *remotekey;
170 arg.deskey = *deskey;
171 if (!key_call ((u_long) KEY_ENCRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
172 (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
173 (char *) &res))
174 return -1;
176 if (res.status != KEY_SUCCESS)
178 debug ("encrypt status is nonzero");
179 return -1;
181 *deskey = res.cryptkeyres_u.deskey;
182 return 0;
184 libc_hidden_nolink_sunrpc (key_encryptsession_pk, GLIBC_2_1)
187 key_decryptsession_pk (char *remotename, netobj *remotekey,
188 des_block *deskey)
190 cryptkeyarg2 arg;
191 cryptkeyres res;
193 arg.remotename = remotename;
194 arg.remotekey = *remotekey;
195 arg.deskey = *deskey;
196 if (!key_call ((u_long) KEY_DECRYPT_PK, (xdrproc_t) xdr_cryptkeyarg2,
197 (char *) &arg, (xdrproc_t) xdr_cryptkeyres,
198 (char *) &res))
199 return -1;
201 if (res.status != KEY_SUCCESS)
203 debug ("decrypt status is nonzero");
204 return -1;
206 *deskey = res.cryptkeyres_u.deskey;
207 return 0;
209 libc_hidden_nolink_sunrpc (key_decryptsession_pk, GLIBC_2_1)
212 key_gendes (des_block *key)
214 struct sockaddr_in sin;
215 CLIENT *client;
216 int socket;
217 enum clnt_stat stat;
219 sin.sin_family = AF_INET;
220 sin.sin_port = 0;
221 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
222 memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
223 socket = RPC_ANYSOCK;
224 client = clntudp_bufcreate (&sin, (u_long) KEY_PROG, (u_long) KEY_VERS,
225 trytimeout, &socket, RPCSMALLMSGSIZE,
226 RPCSMALLMSGSIZE);
227 if (client == NULL)
228 return -1;
230 stat = clnt_call (client, KEY_GEN, (xdrproc_t) xdr_void, NULL,
231 (xdrproc_t) xdr_des_block, (caddr_t) key,
232 tottimeout);
233 clnt_destroy (client);
234 __close (socket);
235 if (stat != RPC_SUCCESS)
236 return -1;
238 return 0;
240 #ifdef EXPORT_RPC_SYMBOLS
241 libc_hidden_def (key_gendes)
242 #else
243 libc_hidden_nolink_sunrpc (key_gendes, GLIBC_2_1)
244 #endif
247 key_setnet (struct key_netstarg *arg)
249 keystatus status;
251 if (!key_call ((u_long) KEY_NET_PUT, (xdrproc_t) xdr_key_netstarg,
252 (char *) arg,(xdrproc_t) xdr_keystatus,
253 (char *) &status))
254 return -1;
256 if (status != KEY_SUCCESS)
258 debug ("key_setnet status is nonzero");
259 return -1;
261 return 1;
263 libc_hidden_nolink_sunrpc (key_setnet, GLIBC_2_1)
266 key_get_conv (char *pkey, des_block *deskey)
268 cryptkeyres res;
270 if (!key_call ((u_long) KEY_GET_CONV, (xdrproc_t) xdr_keybuf, pkey,
271 (xdrproc_t) xdr_cryptkeyres, (char *) &res))
272 return -1;
274 if (res.status != KEY_SUCCESS)
276 debug ("get_conv status is nonzero");
277 return -1;
279 *deskey = res.cryptkeyres_u.deskey;
280 return 0;
282 libc_hidden_nolink_sunrpc (key_get_conv, GLIBC_2_1)
285 * Hack to allow the keyserver to use AUTH_DES (for authenticated
286 * NIS+ calls, for example). The only functions that get called
287 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
289 * The approach is to have the keyserver fill in pointers to local
290 * implementations of these functions, and to call those in key_call().
293 cryptkeyres *(*__key_encryptsession_pk_LOCAL) (uid_t, char *)
294 __attribute__ ((nocommon));
295 cryptkeyres *(*__key_decryptsession_pk_LOCAL) (uid_t, char *)
296 __attribute__ ((nocommon));
297 des_block *(*__key_gendes_LOCAL) (uid_t, char *) __attribute__ ((nocommon));
298 #ifdef SHARED
299 # ifndef EXPORT_RPC_SYMBOLS
300 compat_symbol (libc, __key_encryptsession_pk_LOCAL,
301 __key_encryptsession_pk_LOCAL, GLIBC_2_1);
302 compat_symbol (libc, __key_decryptsession_pk_LOCAL,
303 __key_decryptsession_pk_LOCAL, GLIBC_2_1);
304 compat_symbol (libc, __key_gendes_LOCAL, __key_gendes_LOCAL, GLIBC_2_1);
305 # endif
306 #endif
308 #ifndef SO_PASSCRED
309 static int
310 key_call_keyenvoy (u_long proc, xdrproc_t xdr_arg, char *arg,
311 xdrproc_t xdr_rslt, char *rslt)
313 XDR xdrargs;
314 XDR xdrrslt;
315 FILE *fargs;
316 FILE *frslt;
317 sigset_t oldmask, mask;
318 int status;
319 int pid;
320 int success;
321 uid_t ruid;
322 uid_t euid;
323 static const char MESSENGER[] = "/usr/etc/keyenvoy";
325 success = 1;
326 sigemptyset (&mask);
327 sigaddset (&mask, SIGCHLD);
328 __sigprocmask (SIG_BLOCK, &mask, &oldmask);
331 * We are going to exec a set-uid program which makes our effective uid
332 * zero, and authenticates us with our real uid. We need to make the
333 * effective uid be the real uid for the setuid program, and
334 * the real uid be the effective uid so that we can change things back.
336 euid = __geteuid ();
337 ruid = __getuid ();
338 __setreuid (euid, ruid);
339 pid = _openchild (MESSENGER, &fargs, &frslt);
340 __setreuid (ruid, euid);
341 if (pid < 0)
343 debug ("open_streams");
344 __sigprocmask (SIG_SETMASK, &oldmask, NULL);
345 return (0);
347 xdrstdio_create (&xdrargs, fargs, XDR_ENCODE);
348 xdrstdio_create (&xdrrslt, frslt, XDR_DECODE);
350 if (!xdr_u_long (&xdrargs, &proc) || !(*xdr_arg) (&xdrargs, arg))
352 debug ("xdr args");
353 success = 0;
355 fclose (fargs);
357 if (success && !(*xdr_rslt) (&xdrrslt, rslt))
359 debug ("xdr rslt");
360 success = 0;
362 fclose(frslt);
364 wait_again:
365 if (__wait4 (pid, &status, 0, NULL) < 0)
367 if (errno == EINTR)
368 goto wait_again;
369 debug ("wait4");
370 if (errno == ECHILD || errno == ESRCH)
371 perror ("wait");
372 else
373 success = 0;
375 else
376 if (status != 0)
378 debug ("wait4 1");
379 success = 0;
381 __sigprocmask (SIG_SETMASK, &oldmask, NULL);
383 return success;
385 #endif
387 struct key_call_private {
388 CLIENT *client; /* Client handle */
389 pid_t pid; /* process-id at moment of creation */
390 uid_t uid; /* user-id at last authorization */
392 #define key_call_private_main RPC_THREAD_VARIABLE(key_call_private_s)
393 __libc_lock_define_initialized (static, keycall_lock)
396 * Keep the handle cached. This call may be made quite often.
398 static CLIENT *
399 getkeyserv_handle (int vers)
401 struct key_call_private *kcp = key_call_private_main;
402 struct timeval wait_time;
403 int fd;
404 struct sockaddr_un name;
405 socklen_t namelen = sizeof(struct sockaddr_un);
407 #define TOTAL_TIMEOUT 30 /* total timeout talking to keyserver */
408 #define TOTAL_TRIES 5 /* Number of tries */
410 if (kcp == (struct key_call_private *)NULL)
412 kcp = (struct key_call_private *)malloc (sizeof (*kcp));
413 if (kcp == (struct key_call_private *)NULL)
414 return (CLIENT *) NULL;
416 key_call_private_main = kcp;
417 kcp->client = NULL;
420 /* if pid has changed, destroy client and rebuild */
421 if (kcp->client != NULL && kcp->pid != __getpid ())
423 auth_destroy (kcp->client->cl_auth);
424 clnt_destroy (kcp->client);
425 kcp->client = NULL;
428 if (kcp->client != NULL)
430 /* if other side closed socket, build handle again */
431 clnt_control (kcp->client, CLGET_FD, (char *)&fd);
432 if (__getpeername (fd,(struct sockaddr *)&name,&namelen) == -1)
434 auth_destroy (kcp->client->cl_auth);
435 clnt_destroy (kcp->client);
436 kcp->client = NULL;
440 if (kcp->client != NULL)
442 /* if uid has changed, build client handle again */
443 if (kcp->uid != __geteuid ())
445 kcp->uid = __geteuid ();
446 auth_destroy (kcp->client->cl_auth);
447 kcp->client->cl_auth =
448 authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
449 if (kcp->client->cl_auth == NULL)
451 clnt_destroy (kcp->client);
452 kcp->client = NULL;
453 return ((CLIENT *) NULL);
456 /* Change the version number to the new one */
457 clnt_control (kcp->client, CLSET_VERS, (void *)&vers);
458 return kcp->client;
461 if ((kcp->client == (CLIENT *) NULL))
462 /* Use the AF_UNIX transport */
463 kcp->client = clnt_create ("/var/run/keyservsock", KEY_PROG, vers, "unix");
465 if (kcp->client == (CLIENT *) NULL)
466 return (CLIENT *) NULL;
468 kcp->uid = __geteuid ();
469 kcp->pid = __getpid ();
470 kcp->client->cl_auth = authunix_create ((char *)"", kcp->uid, 0, 0, NULL);
471 if (kcp->client->cl_auth == NULL)
473 clnt_destroy (kcp->client);
474 kcp->client = NULL;
475 return (CLIENT *) NULL;
478 wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
479 wait_time.tv_usec = 0;
480 clnt_control (kcp->client, CLSET_RETRY_TIMEOUT,
481 (char *)&wait_time);
482 if (clnt_control (kcp->client, CLGET_FD, (char *)&fd))
483 __fcntl (fd, F_SETFD, FD_CLOEXEC); /* make it "close on exec" */
485 return kcp->client;
488 /* returns 0 on failure, 1 on success */
489 static int
490 key_call_socket (u_long proc, xdrproc_t xdr_arg, char *arg,
491 xdrproc_t xdr_rslt, char *rslt)
493 CLIENT *clnt;
494 struct timeval wait_time;
495 int result = 0;
497 __libc_lock_lock (keycall_lock);
498 if ((proc == KEY_ENCRYPT_PK) || (proc == KEY_DECRYPT_PK) ||
499 (proc == KEY_NET_GET) || (proc == KEY_NET_PUT) ||
500 (proc == KEY_GET_CONV))
501 clnt = getkeyserv_handle(2); /* talk to version 2 */
502 else
503 clnt = getkeyserv_handle(1); /* talk to version 1 */
505 if (clnt != NULL)
507 wait_time.tv_sec = TOTAL_TIMEOUT;
508 wait_time.tv_usec = 0;
510 if (clnt_call (clnt, proc, xdr_arg, arg, xdr_rslt, rslt,
511 wait_time) == RPC_SUCCESS)
512 result = 1;
515 __libc_lock_unlock (keycall_lock);
517 return result;
521 /* returns 0 on failure, 1 on success */
522 static int
523 key_call (u_long proc, xdrproc_t xdr_arg, char *arg,
524 xdrproc_t xdr_rslt, char *rslt)
526 #ifndef SO_PASSCRED
527 static int use_keyenvoy;
528 #endif
530 if (proc == KEY_ENCRYPT_PK && __key_encryptsession_pk_LOCAL)
532 cryptkeyres *res;
533 res = (*__key_encryptsession_pk_LOCAL) (__geteuid (), arg);
534 *(cryptkeyres *) rslt = *res;
535 return 1;
537 else if (proc == KEY_DECRYPT_PK && __key_decryptsession_pk_LOCAL)
539 cryptkeyres *res;
540 res = (*__key_decryptsession_pk_LOCAL) (__geteuid (), arg);
541 *(cryptkeyres *) rslt = *res;
542 return 1;
544 else if (proc == KEY_GEN && __key_gendes_LOCAL)
546 des_block *res;
547 res = (*__key_gendes_LOCAL) (__geteuid (), 0);
548 *(des_block *) rslt = *res;
549 return 1;
552 #ifdef SO_PASSCRED
553 return key_call_socket (proc, xdr_arg, arg, xdr_rslt, rslt);
554 #else
555 if (!use_keyenvoy)
557 if (key_call_socket (proc, xdr_arg, arg, xdr_rslt, rslt))
558 return 1;
559 use_keyenvoy = 1;
561 return key_call_keyenvoy (proc, xdr_arg, arg, xdr_rslt, rslt);
562 #endif
565 void
566 __rpc_thread_key_cleanup (void)
568 struct key_call_private *kcp = RPC_THREAD_VARIABLE(key_call_private_s);
570 if (kcp) {
571 if (kcp->client) {
572 if (kcp->client->cl_auth)
573 auth_destroy (kcp->client->cl_auth);
574 clnt_destroy(kcp->client);
576 free (kcp);