blob: 0b10f9194f8a6412c7c6bc34461bbd3c1675f6f9 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36#include "includes.h"
Damien Miller9b481512002-09-12 10:43:29 +100037#include "openbsd-compat/sys-queue.h"
Damien Miller03e20032006-03-15 11:16:59 +110038RCSID("$OpenBSD: ssh-agent.c,v 1.126 2006/02/08 12:15:27 stevesk Exp $");
Damien Millerec52d7c2002-01-22 23:52:17 +110039
Damien Miller03e20032006-03-15 11:16:59 +110040#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110041# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110042#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include <openssl/evp.h>
44#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46#include "ssh.h"
47#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "buffer.h"
49#include "bufaux.h"
50#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100052#include "key.h"
53#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110056#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Ben Lindstrom3f471632001-07-04 03:53:15 +000058#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000059#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000060#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000061
Damien Miller6c4914a2004-03-03 11:08:59 +110062#if defined(HAVE_SYS_PRCTL_H)
63#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
64#endif
65
Ben Lindstrom65366a82001-12-06 16:32:47 +000066typedef enum {
67 AUTH_UNUSED,
68 AUTH_SOCKET,
69 AUTH_CONNECTION
70} sock_type;
71
Damien Miller95def091999-11-25 00:26:21 +110072typedef struct {
73 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000074 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110075 Buffer input;
76 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000077 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078} SocketEntry;
79
Ben Lindstrom46c16222000-12-22 01:43:59 +000080u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081SocketEntry *sockets = NULL;
82
Damien Miller1a534ae2002-01-22 23:26:13 +110083typedef struct identity {
84 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100085 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110086 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000087 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110088 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089} Identity;
90
Damien Millerad833b32000-08-23 10:46:23 +100091typedef struct {
92 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110093 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100094} Idtab;
95
96/* private key table, one per protocol version */
97Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99int max_fd = 0;
100
101/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000102pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* pathname and directory for AUTH_SOCKET */
105char socket_name[1024];
106char socket_dir[1024];
107
Ben Lindstrom2f717042002-06-06 21:52:03 +0000108/* locking */
109int locked = 0;
110char *lock_passwd = NULL;
111
Damien Miller95def091999-11-25 00:26:21 +1100112extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100113
Damien Miller53d81482003-01-22 11:47:19 +1100114/* Default lifetime (0 == forever) */
115static int lifetime = 0;
116
Ben Lindstrombba81212001-06-25 05:01:22 +0000117static void
Damien Miller58f34862002-09-04 16:31:21 +1000118close_socket(SocketEntry *e)
119{
Damien Miller58f34862002-09-04 16:31:21 +1000120 close(e->fd);
121 e->fd = -1;
122 e->type = AUTH_UNUSED;
123 buffer_free(&e->input);
124 buffer_free(&e->output);
125 buffer_free(&e->request);
126}
127
128static void
Damien Millerad833b32000-08-23 10:46:23 +1000129idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130{
Damien Millerad833b32000-08-23 10:46:23 +1000131 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000132
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000133 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100134 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000135 idtable[i].nentries = 0;
136 }
137}
138
139/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000141idtab_lookup(int version)
142{
143 if (version < 1 || version > 2)
144 fatal("internal error, bad protocol version %d", version);
145 return &idtable[version];
146}
147
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000148static void
149free_identity(Identity *id)
150{
151 key_free(id->key);
152 xfree(id->comment);
153 xfree(id);
154}
155
Damien Millerad833b32000-08-23 10:46:23 +1000156/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100157static Identity *
158lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000159{
Damien Miller1a534ae2002-01-22 23:26:13 +1100160 Identity *id;
161
Damien Millerad833b32000-08-23 10:46:23 +1000162 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100163 TAILQ_FOREACH(id, &tab->idlist, next) {
164 if (key_equal(key, id->key))
165 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000166 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100167 return (NULL);
168}
169
Damien Miller6c711792003-01-24 11:36:23 +1100170/* Check confirmation of keysign request */
171static int
172confirm_key(Identity *id)
173{
Darren Tuckerce327b62004-11-05 20:38:03 +1100174 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100175 int ret = -1;
176
177 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100178 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
179 id->comment, p))
180 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100181 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100182
Damien Miller6c711792003-01-24 11:36:23 +1100183 return (ret);
184}
185
Damien Millerad833b32000-08-23 10:46:23 +1000186/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void
Damien Millerad833b32000-08-23 10:46:23 +1000188process_request_identities(SocketEntry *e, int version)
189{
190 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100191 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000192 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Miller95def091999-11-25 00:26:21 +1100194 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000195 buffer_put_char(&msg, (version == 1) ?
196 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
197 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100198 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100199 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000200 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
201 buffer_put_bignum(&msg, id->key->rsa->e);
202 buffer_put_bignum(&msg, id->key->rsa->n);
203 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000204 u_char *blob;
205 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100206 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000207 buffer_put_string(&msg, blob, blen);
208 xfree(blob);
209 }
210 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100211 }
212 buffer_put_int(&e->output, buffer_len(&msg));
213 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
214 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
Damien Millerad833b32000-08-23 10:46:23 +1000217/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static void
Damien Millerad833b32000-08-23 10:46:23 +1000219process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000221 u_char buf[32], mdbuf[16], session_id[16];
222 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000223 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000224 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000225 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100226 Buffer msg;
227 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000228 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Miller95def091999-11-25 00:26:21 +1100230 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100231 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100232 if ((challenge = BN_new()) == NULL)
233 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000234
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000235 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000236 buffer_get_bignum(&e->request, key->rsa->e);
237 buffer_get_bignum(&e->request, key->rsa->n);
238 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Millerad833b32000-08-23 10:46:23 +1000240 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000241 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000242 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000243 buffer_get(&e->request, session_id, 16);
244 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000245 if (response_type != 1)
246 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller1a534ae2002-01-22 23:26:13 +1100248 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100249 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100250 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000251 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100252 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
253 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100254
Damien Millerad833b32000-08-23 10:46:23 +1000255 /* The response is MD5 of decrypted challenge plus session id. */
256 len = BN_num_bytes(challenge);
257 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000258 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000259 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100260 }
Damien Millerad833b32000-08-23 10:46:23 +1000261 memset(buf, 0, 32);
262 BN_bn2bin(challenge, buf + 32 - len);
263 MD5_Init(&md);
264 MD5_Update(&md, buf, 32);
265 MD5_Update(&md, session_id, 16);
266 MD5_Final(mdbuf, &md);
267
268 /* Send the response. */
269 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
270 for (i = 0; i < 16; i++)
271 buffer_put_char(&msg, mdbuf[i]);
272 goto send;
273 }
274
275failure:
276 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100277 buffer_put_char(&msg, SSH_AGENT_FAILURE);
278send:
279 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000280 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
281 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100282 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000283 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284}
285
Damien Millerad833b32000-08-23 10:46:23 +1000286/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000287static void
Damien Millerad833b32000-08-23 10:46:23 +1000288process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000290 u_char *blob, *data, *signature = NULL;
291 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000292 extern int datafellows;
293 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000294 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000295 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000296
297 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000298
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000299 blob = buffer_get_string(&e->request, &blen);
300 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100301
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000302 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100303 if (flags & SSH_AGENT_OLD_SIGNATURE)
304 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000305
Damien Miller0bc1bd82000-11-13 22:57:25 +1100306 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000307 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100308 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100309 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100310 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000311 }
312 key_free(key);
313 buffer_init(&msg);
314 if (ok == 0) {
315 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
316 buffer_put_string(&msg, signature, slen);
317 } else {
318 buffer_put_char(&msg, SSH_AGENT_FAILURE);
319 }
320 buffer_put_int(&e->output, buffer_len(&msg));
321 buffer_append(&e->output, buffer_ptr(&msg),
322 buffer_len(&msg));
323 buffer_free(&msg);
324 xfree(data);
325 xfree(blob);
326 if (signature != NULL)
327 xfree(signature);
328}
329
330/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static void
Damien Millerad833b32000-08-23 10:46:23 +1000332process_remove_identity(SocketEntry *e, int version)
333{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000334 u_int blen, bits;
335 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000337 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100338
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000339 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000340 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100341 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000342 bits = buffer_get_int(&e->request);
343 buffer_get_bignum(&e->request, key->rsa->e);
344 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Millerad833b32000-08-23 10:46:23 +1000346 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000347 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000348 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000349 break;
350 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000351 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000353 xfree(blob);
354 break;
355 }
356 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100357 Identity *id = lookup_identity(key, version);
358 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100359 /*
360 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100361 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000362 * the array, we actually free the key there and move
363 * all the entries between the empty slot and the end
364 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100365 */
Damien Millerad833b32000-08-23 10:46:23 +1000366 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100367 if (tab->nentries < 1)
368 fatal("process_remove_identity: "
369 "internal error: tab->nentries %d",
370 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100371 TAILQ_REMOVE(&tab->idlist, id, next);
372 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000373 tab->nentries--;
374 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100375 }
Damien Millerad833b32000-08-23 10:46:23 +1000376 key_free(key);
377 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000379 buffer_put_char(&e->output,
380 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381}
382
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static void
Damien Millerad833b32000-08-23 10:46:23 +1000384process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385{
Damien Millerad833b32000-08-23 10:46:23 +1000386 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100387 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Miller95def091999-11-25 00:26:21 +1100389 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100390 for (id = TAILQ_FIRST(&tab->idlist); id;
391 id = TAILQ_FIRST(&tab->idlist)) {
392 TAILQ_REMOVE(&tab->idlist, id, next);
393 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100394 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000397 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
399 /* Send success. */
400 buffer_put_int(&e->output, 1);
401 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100402}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Ben Lindstrombba81212001-06-25 05:01:22 +0000404static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000405reaper(void)
406{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000407 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000408 Identity *id, *nxt;
409 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000410 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000411
412 for (version = 1; version < 3; version++) {
413 tab = idtab_lookup(version);
414 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
415 nxt = TAILQ_NEXT(id, next);
416 if (id->death != 0 && now >= id->death) {
417 TAILQ_REMOVE(&tab->idlist, id, next);
418 free_identity(id);
419 tab->nentries--;
420 }
421 }
422 }
423}
424
425static void
Damien Millerad833b32000-08-23 10:46:23 +1000426process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100427{
Damien Millerad833b32000-08-23 10:46:23 +1000428 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100429 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000430 char *type_name, *comment;
431 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100432
Damien Millerad833b32000-08-23 10:46:23 +1000433 switch (version) {
434 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000436 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000437 buffer_get_bignum(&e->request, k->rsa->n);
438 buffer_get_bignum(&e->request, k->rsa->e);
439 buffer_get_bignum(&e->request, k->rsa->d);
440 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100441
Damien Millerad833b32000-08-23 10:46:23 +1000442 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000443 buffer_get_bignum(&e->request, k->rsa->q); /* p */
444 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100445
Damien Millerad833b32000-08-23 10:46:23 +1000446 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000447 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000448 break;
449 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000450 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000451 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000453 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100454 case KEY_DSA:
455 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 buffer_get_bignum2(&e->request, k->dsa->p);
457 buffer_get_bignum2(&e->request, k->dsa->q);
458 buffer_get_bignum2(&e->request, k->dsa->g);
459 buffer_get_bignum2(&e->request, k->dsa->pub_key);
460 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100461 break;
462 case KEY_RSA:
463 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000464 buffer_get_bignum2(&e->request, k->rsa->n);
465 buffer_get_bignum2(&e->request, k->rsa->e);
466 buffer_get_bignum2(&e->request, k->rsa->d);
467 buffer_get_bignum2(&e->request, k->rsa->iqmp);
468 buffer_get_bignum2(&e->request, k->rsa->p);
469 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470
471 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000472 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 break;
474 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000475 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000476 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100477 }
Damien Millerad833b32000-08-23 10:46:23 +1000478 break;
479 }
Damien Millerc51d0732003-03-15 11:37:09 +1100480 /* enable blinding */
481 switch (k->type) {
482 case KEY_RSA:
483 case KEY_RSA1:
484 if (RSA_blinding_on(k->rsa, NULL) != 1) {
485 error("process_add_identity: RSA_blinding_on failed");
486 key_free(k);
487 goto send;
488 }
489 break;
490 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000491 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000492 if (k == NULL) {
493 xfree(comment);
494 goto send;
495 }
496 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000497 while (buffer_len(&e->request)) {
498 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000499 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000500 death = time(NULL) + buffer_get_int(&e->request);
501 break;
Damien Miller6c711792003-01-24 11:36:23 +1100502 case SSH_AGENT_CONSTRAIN_CONFIRM:
503 confirm = 1;
504 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000505 default:
506 break;
507 }
508 }
Damien Miller53d81482003-01-22 11:47:19 +1100509 if (lifetime && !death)
510 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000511 if (lookup_identity(k, version) == NULL) {
512 Identity *id = xmalloc(sizeof(Identity));
513 id->key = k;
514 id->comment = comment;
515 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100516 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000517 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
518 /* Increment the number of identities. */
519 tab->nentries++;
520 } else {
521 key_free(k);
522 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000523 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000524send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000525 buffer_put_int(&e->output, 1);
526 buffer_put_char(&e->output,
527 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
528}
529
Ben Lindstrom2f717042002-06-06 21:52:03 +0000530/* XXX todo: encrypt sensitive data with passphrase */
531static void
532process_lock_agent(SocketEntry *e, int lock)
533{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000534 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000535 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000536
537 passwd = buffer_get_string(&e->request, NULL);
538 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
539 locked = 0;
540 memset(lock_passwd, 0, strlen(lock_passwd));
541 xfree(lock_passwd);
542 lock_passwd = NULL;
543 success = 1;
544 } else if (!locked && lock) {
545 locked = 1;
546 lock_passwd = xstrdup(passwd);
547 success = 1;
548 }
549 memset(passwd, 0, strlen(passwd));
550 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000551
Ben Lindstrom2f717042002-06-06 21:52:03 +0000552 buffer_put_int(&e->output, 1);
553 buffer_put_char(&e->output,
554 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000555}
556
557static void
558no_identities(SocketEntry *e, u_int type)
559{
560 Buffer msg;
561
562 buffer_init(&msg);
563 buffer_put_char(&msg,
564 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
565 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
566 buffer_put_int(&msg, 0);
567 buffer_put_int(&e->output, buffer_len(&msg));
568 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
569 buffer_free(&msg);
570}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000571
572#ifdef SMARTCARD
573static void
574process_add_smartcard_key (SocketEntry *e)
575{
Ben Lindstromba72d302002-03-22 03:51:06 +0000576 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000577 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000578 Key **keys, *k;
579 Identity *id;
580 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100581
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000582 sc_reader_id = buffer_get_string(&e->request, NULL);
583 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000584
585 while (buffer_len(&e->request)) {
586 switch (buffer_get_char(&e->request)) {
587 case SSH_AGENT_CONSTRAIN_LIFETIME:
588 death = time(NULL) + buffer_get_int(&e->request);
589 break;
590 case SSH_AGENT_CONSTRAIN_CONFIRM:
591 confirm = 1;
592 break;
593 default:
594 break;
595 }
596 }
597 if (lifetime && !death)
598 death = time(NULL) + lifetime;
599
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000600 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000601 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000602 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000603
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000604 if (keys == NULL || keys[0] == NULL) {
605 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000606 goto send;
607 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000608 for (i = 0; keys[i] != NULL; i++) {
609 k = keys[i];
610 version = k->type == KEY_RSA1 ? 1 : 2;
611 tab = idtab_lookup(version);
612 if (lookup_identity(k, version) == NULL) {
613 id = xmalloc(sizeof(Identity));
614 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000615 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000616 id->death = death;
617 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000618 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
619 tab->nentries++;
620 success = 1;
621 } else {
622 key_free(k);
623 }
624 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000625 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000626 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000627send:
628 buffer_put_int(&e->output, 1);
629 buffer_put_char(&e->output,
630 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
631}
632
633static void
634process_remove_smartcard_key(SocketEntry *e)
635{
Ben Lindstromba72d302002-03-22 03:51:06 +0000636 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000637 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000638 Key **keys, *k = NULL;
639 Identity *id;
640 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000641
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000642 sc_reader_id = buffer_get_string(&e->request, NULL);
643 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000645 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000646 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000647
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000648 if (keys == NULL || keys[0] == NULL) {
649 error("sc_get_keys failed");
650 goto send;
651 }
652 for (i = 0; keys[i] != NULL; i++) {
653 k = keys[i];
654 version = k->type == KEY_RSA1 ? 1 : 2;
655 if ((id = lookup_identity(k, version)) != NULL) {
656 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000657 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100659 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660 success = 1;
661 }
662 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000663 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000664 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000665 xfree(keys);
666send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000667 buffer_put_int(&e->output, 1);
668 buffer_put_char(&e->output,
669 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
670}
Ben Lindstromffce1472001-08-06 21:57:31 +0000671#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000672
Damien Millerad833b32000-08-23 10:46:23 +1000673/* dispatch incoming messages */
674
Ben Lindstrombba81212001-06-25 05:01:22 +0000675static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676process_message(SocketEntry *e)
677{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000678 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000679 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000680
681 /* kill dead keys */
682 reaper();
683
Damien Miller95def091999-11-25 00:26:21 +1100684 if (buffer_len(&e->input) < 5)
685 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100686 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100687 msg_len = GET_32BIT(cp);
688 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000689 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100690 return;
691 }
692 if (buffer_len(&e->input) < msg_len + 4)
693 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000694
695 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100696 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000697 buffer_clear(&e->request);
698 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
699 buffer_consume(&e->input, msg_len);
700 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
Ben Lindstrom2f717042002-06-06 21:52:03 +0000702 /* check wheter agent is locked */
703 if (locked && type != SSH_AGENTC_UNLOCK) {
704 buffer_clear(&e->request);
705 switch (type) {
706 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
707 case SSH2_AGENTC_REQUEST_IDENTITIES:
708 /* send empty lists */
709 no_identities(e, type);
710 break;
711 default:
712 /* send a fail message for all other request types */
713 buffer_put_int(&e->output, 1);
714 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
715 }
716 return;
717 }
718
Ben Lindstrom3f471632001-07-04 03:53:15 +0000719 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100720 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000721 case SSH_AGENTC_LOCK:
722 case SSH_AGENTC_UNLOCK:
723 process_lock_agent(e, type == SSH_AGENTC_LOCK);
724 break;
Damien Millerad833b32000-08-23 10:46:23 +1000725 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100726 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000727 process_authentication_challenge1(e);
728 break;
729 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
730 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100731 break;
732 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000733 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000734 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100735 break;
736 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000737 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100738 break;
739 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000740 process_remove_all_identities(e, 1);
741 break;
742 /* ssh2 */
743 case SSH2_AGENTC_SIGN_REQUEST:
744 process_sign_request2(e);
745 break;
746 case SSH2_AGENTC_REQUEST_IDENTITIES:
747 process_request_identities(e, 2);
748 break;
749 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000750 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000751 process_add_identity(e, 2);
752 break;
753 case SSH2_AGENTC_REMOVE_IDENTITY:
754 process_remove_identity(e, 2);
755 break;
756 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
757 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100758 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000759#ifdef SMARTCARD
760 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000761 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000762 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100763 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000764 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
765 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100766 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000767#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100768 default:
769 /* Unknown message. Respond with failure. */
770 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000771 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100772 buffer_put_int(&e->output, 1);
773 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
774 break;
775 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776}
777
Ben Lindstrombba81212001-06-25 05:01:22 +0000778static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000779new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000781 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000782
Damien Miller232711f2004-06-15 10:35:30 +1000783 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784
Damien Miller95def091999-11-25 00:26:21 +1100785 if (fd > max_fd)
786 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787
Damien Miller95def091999-11-25 00:26:21 +1100788 for (i = 0; i < sockets_alloc; i++)
789 if (sockets[i].type == AUTH_UNUSED) {
790 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100791 buffer_init(&sockets[i].input);
792 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000793 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000794 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100795 return;
796 }
797 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000798 new_alloc = sockets_alloc + 10;
Damien Miller95def091999-11-25 00:26:21 +1100799 if (sockets)
Darren Tuckerfb16b242003-09-22 21:04:23 +1000800 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
Damien Miller95def091999-11-25 00:26:21 +1100801 else
Darren Tuckerfb16b242003-09-22 21:04:23 +1000802 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
803 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100804 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000805 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100806 sockets[old_alloc].fd = fd;
807 buffer_init(&sockets[old_alloc].input);
808 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000809 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000810 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}
812
Ben Lindstrombba81212001-06-25 05:01:22 +0000813static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000814prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000816 u_int i, sz;
817 int n = 0;
818
819 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100820 switch (sockets[i].type) {
821 case AUTH_SOCKET:
822 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000823 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100824 break;
825 case AUTH_UNUSED:
826 break;
827 default:
828 fatal("Unknown socket type %d", sockets[i].type);
829 break;
830 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000831 }
832
833 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000834 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000835 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000836 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000837 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000838 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000839 *fdrp = xmalloc(sz);
840 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000841 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000843 if (n < *fdl)
844 debug("XXX shrink: %d < %d", n, *fdl);
845 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 memset(*fdrp, 0, sz);
847 memset(*fdwp, 0, sz);
848
849 for (i = 0; i < sockets_alloc; i++) {
850 switch (sockets[i].type) {
851 case AUTH_SOCKET:
852 case AUTH_CONNECTION:
853 FD_SET(sockets[i].fd, *fdrp);
854 if (buffer_len(&sockets[i].output) > 0)
855 FD_SET(sockets[i].fd, *fdwp);
856 break;
857 default:
858 break;
859 }
860 }
861 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862}
863
Ben Lindstrombba81212001-06-25 05:01:22 +0000864static void
Damien Miller95def091999-11-25 00:26:21 +1100865after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000867 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100868 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100869 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000870 int len, sock;
871 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000872 uid_t euid;
873 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Damien Miller95def091999-11-25 00:26:21 +1100875 for (i = 0; i < sockets_alloc; i++)
876 switch (sockets[i].type) {
877 case AUTH_UNUSED:
878 break;
879 case AUTH_SOCKET:
880 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100881 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000882 sock = accept(sockets[i].fd,
883 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100884 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100885 error("accept from AUTH_SOCKET: %s",
886 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100887 break;
888 }
Damien Miller538f1812002-09-12 09:51:10 +1000889 if (getpeereid(sock, &euid, &egid) < 0) {
890 error("getpeereid %d failed: %s",
891 sock, strerror(errno));
892 close(sock);
893 break;
894 }
Damien Milleraf9de382002-10-03 11:54:35 +1000895 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000896 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000897 "peer euid %u != uid %u",
898 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000899 close(sock);
900 break;
901 }
Damien Miller95def091999-11-25 00:26:21 +1100902 new_socket(AUTH_CONNECTION, sock);
903 }
904 break;
905 case AUTH_CONNECTION:
906 if (buffer_len(&sockets[i].output) > 0 &&
907 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000908 do {
909 len = write(sockets[i].fd,
910 buffer_ptr(&sockets[i].output),
911 buffer_len(&sockets[i].output));
912 if (len == -1 && (errno == EAGAIN ||
913 errno == EINTR))
914 continue;
915 break;
916 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100917 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000918 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100919 break;
920 }
921 buffer_consume(&sockets[i].output, len);
922 }
923 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000924 do {
925 len = read(sockets[i].fd, buf, sizeof(buf));
926 if (len == -1 && (errno == EAGAIN ||
927 errno == EINTR))
928 continue;
929 break;
930 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100931 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000932 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100933 break;
934 }
935 buffer_append(&sockets[i].input, buf, len);
936 process_message(&sockets[i]);
937 }
938 break;
939 default:
940 fatal("Unknown type %d", sockets[i].type);
941 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942}
943
Ben Lindstrombba81212001-06-25 05:01:22 +0000944static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000945cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000946{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000947 if (socket_name[0])
948 unlink(socket_name);
949 if (socket_dir[0])
950 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951}
952
Darren Tucker3e33cec2003-10-02 16:12:36 +1000953void
Damien Miller792c5111999-10-29 09:47:09 +1000954cleanup_exit(int i)
955{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000956 cleanup_socket();
957 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000958}
959
Ben Lindstrombba81212001-06-25 05:01:22 +0000960static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000961cleanup_handler(int sig)
962{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000963 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000964 _exit(2);
965}
966
Ben Lindstrombba81212001-06-25 05:01:22 +0000967static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000968check_parent_exists(int sig)
969{
970 int save_errno = errno;
971
972 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
973 /* printf("Parent has died - Authentication agent exiting.\n"); */
974 cleanup_handler(sig); /* safe */
975 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000976 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000977 alarm(10);
978 errno = save_errno;
979}
980
981static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000982usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000983{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000984 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000985 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000986 fprintf(stderr, "Options:\n");
987 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
988 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
989 fprintf(stderr, " -k Kill the current agent.\n");
990 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000991 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100992 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100993 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000994}
995
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996int
997main(int ac, char **av)
998{
Damien Miller6c711792003-01-24 11:36:23 +1100999 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001000 int sock, fd, ch;
1001 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001002 char *shell, *format, *pidstr, *agentsocket = NULL;
1003 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001004 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001005#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001006 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001007#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001008 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001009 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001010 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001011 pid_t pid;
1012 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001013
Darren Tuckerce321d82005-10-03 18:11:24 +10001014 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1015 sanitise_stdfd();
1016
Damien Miller6cffb9a2002-09-04 16:20:26 +10001017 /* drop */
1018 setegid(getgid());
1019 setgid(getgid());
1020
Damien Miller6c4914a2004-03-03 11:08:59 +11001021#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1022 /* Disable ptrace on Linux without sgid bit */
1023 prctl(PR_SET_DUMPABLE, 0);
1024#endif
1025
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001026 SSLeay_add_all_algorithms();
1027
Damien Miller59d3d5b2003-08-22 09:34:41 +10001028 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001029 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001030 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001031
Damien Miller53d81482003-01-22 11:47:19 +11001032 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001033 switch (ch) {
1034 case 'c':
1035 if (s_flag)
1036 usage();
1037 c_flag++;
1038 break;
1039 case 'k':
1040 k_flag++;
1041 break;
1042 case 's':
1043 if (c_flag)
1044 usage();
1045 s_flag++;
1046 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001047 case 'd':
1048 if (d_flag)
1049 usage();
1050 d_flag++;
1051 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001052 case 'a':
1053 agentsocket = optarg;
1054 break;
Damien Miller53d81482003-01-22 11:47:19 +11001055 case 't':
1056 if ((lifetime = convtime(optarg)) == -1) {
1057 fprintf(stderr, "Invalid lifetime\n");
1058 usage();
1059 }
1060 break;
Damien Miller95def091999-11-25 00:26:21 +11001061 default:
1062 usage();
1063 }
Damien Miller792c5111999-10-29 09:47:09 +10001064 }
Damien Miller95def091999-11-25 00:26:21 +11001065 ac -= optind;
1066 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001067
Ben Lindstromd94580c2001-07-04 03:48:02 +00001068 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001069 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001070
Ben Lindstromeecdf232002-04-02 21:03:51 +00001071 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001072 shell = getenv("SHELL");
1073 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1074 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001075 }
Damien Miller95def091999-11-25 00:26:21 +11001076 if (k_flag) {
1077 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1078 if (pidstr == NULL) {
1079 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001080 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001081 exit(1);
1082 }
1083 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001084 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001085 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001086 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001087 exit(1);
1088 }
1089 if (kill(pid, SIGTERM) == -1) {
1090 perror("kill");
1091 exit(1);
1092 }
1093 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1094 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1095 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001096 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001097 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001098 }
Damien Miller95def091999-11-25 00:26:21 +11001099 parent_pid = getpid();
1100
Ben Lindstromb7788f32002-06-06 21:46:08 +00001101 if (agentsocket == NULL) {
1102 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001103 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001104 if (mkdtemp(socket_dir) == NULL) {
1105 perror("mkdtemp: private socket dir");
1106 exit(1);
1107 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001108 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1109 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001110 } else {
1111 /* Try to use specified agent socket */
1112 socket_dir[0] = '\0';
1113 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Damien Miller5428f641999-11-25 11:54:57 +11001116 /*
1117 * Create socket early so it will exist before command gets run from
1118 * the parent.
1119 */
Damien Miller95def091999-11-25 00:26:21 +11001120 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1121 if (sock < 0) {
1122 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001123 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001124 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001125 }
Damien Miller95def091999-11-25 00:26:21 +11001126 memset(&sunaddr, 0, sizeof(sunaddr));
1127 sunaddr.sun_family = AF_UNIX;
1128 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001129 prev_mask = umask(0177);
Damien Miller95def091999-11-25 00:26:21 +11001130 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1131 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001132 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001133 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001134 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001136 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001137 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001138 perror("listen");
1139 cleanup_exit(1);
1140 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001141
Damien Miller5428f641999-11-25 11:54:57 +11001142 /*
1143 * Fork, and have the parent execute the command, if any, or present
1144 * the socket data. The child continues as the authentication agent.
1145 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001146 if (d_flag) {
1147 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1148 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1149 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1150 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001151 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001152 goto skip;
1153 }
Damien Miller95def091999-11-25 00:26:21 +11001154 pid = fork();
1155 if (pid == -1) {
1156 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001157 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001158 }
1159 if (pid != 0) { /* Parent - execute the given command. */
1160 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001161 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001162 if (ac == 0) {
1163 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1164 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001165 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001166 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001167 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001168 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001169 exit(0);
1170 }
Damien Millere4340be2000-09-16 13:29:08 +11001171 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1172 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1173 perror("setenv");
1174 exit(1);
1175 }
Damien Miller95def091999-11-25 00:26:21 +11001176 execvp(av[0], av);
1177 perror(av[0]);
1178 exit(1);
1179 }
Damien Millerc653b892002-02-08 22:05:41 +11001180 /* child */
1181 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001182
1183 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001184 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001185 cleanup_exit(1);
1186 }
1187
1188 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001189 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1190 /* XXX might close listen socket */
1191 (void)dup2(fd, STDIN_FILENO);
1192 (void)dup2(fd, STDOUT_FILENO);
1193 (void)dup2(fd, STDERR_FILENO);
1194 if (fd > 2)
1195 close(fd);
1196 }
Damien Miller95def091999-11-25 00:26:21 +11001197
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001198#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001199 /* deny core dumps, since memory contains unencrypted private keys */
1200 rlim.rlim_cur = rlim.rlim_max = 0;
1201 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001202 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001203 cleanup_exit(1);
1204 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001205#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001206
1207skip:
Damien Miller95def091999-11-25 00:26:21 +11001208 new_socket(AUTH_SOCKET, sock);
1209 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001210 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001211 alarm(10);
1212 }
Damien Millerad833b32000-08-23 10:46:23 +10001213 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001214 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001215 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001216 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001217 signal(SIGHUP, cleanup_handler);
1218 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001219 nalloc = 0;
1220
Damien Miller95def091999-11-25 00:26:21 +11001221 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001222 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001223 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001224 if (errno == EINTR)
1225 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001226 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001227 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001228 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001229 }
1230 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231}