blob: 8821e00063c4fea0b37b2a10e6f21927a7a6da1e [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 Millercd4223c2006-03-15 11:22:47 +110037RCSID("$OpenBSD: ssh-agent.c,v 1.127 2006/02/08 14:31:30 stevesk Exp $");
38
Damien Miller9b481512002-09-12 10:43:29 +100039#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110040#include <sys/resource.h>
Damien Miller03e20032006-03-15 11:16:59 +110041#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110042# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110043#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include <openssl/evp.h>
45#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
47#include "ssh.h"
48#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "buffer.h"
50#include "bufaux.h"
51#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100053#include "key.h"
54#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110055#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110057#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstrom3f471632001-07-04 03:53:15 +000059#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000060#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000061#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000062
Damien Miller6c4914a2004-03-03 11:08:59 +110063#if defined(HAVE_SYS_PRCTL_H)
64#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
65#endif
66
Ben Lindstrom65366a82001-12-06 16:32:47 +000067typedef enum {
68 AUTH_UNUSED,
69 AUTH_SOCKET,
70 AUTH_CONNECTION
71} sock_type;
72
Damien Miller95def091999-11-25 00:26:21 +110073typedef struct {
74 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000075 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110076 Buffer input;
77 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000078 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079} SocketEntry;
80
Ben Lindstrom46c16222000-12-22 01:43:59 +000081u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082SocketEntry *sockets = NULL;
83
Damien Miller1a534ae2002-01-22 23:26:13 +110084typedef struct identity {
85 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100086 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110087 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000088 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110089 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090} Identity;
91
Damien Millerad833b32000-08-23 10:46:23 +100092typedef struct {
93 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110094 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100095} Idtab;
96
97/* private key table, one per protocol version */
98Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
100int max_fd = 0;
101
102/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000103pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105/* pathname and directory for AUTH_SOCKET */
106char socket_name[1024];
107char socket_dir[1024];
108
Ben Lindstrom2f717042002-06-06 21:52:03 +0000109/* locking */
110int locked = 0;
111char *lock_passwd = NULL;
112
Damien Miller95def091999-11-25 00:26:21 +1100113extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100114
Damien Miller53d81482003-01-22 11:47:19 +1100115/* Default lifetime (0 == forever) */
116static int lifetime = 0;
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void
Damien Miller58f34862002-09-04 16:31:21 +1000119close_socket(SocketEntry *e)
120{
Damien Miller58f34862002-09-04 16:31:21 +1000121 close(e->fd);
122 e->fd = -1;
123 e->type = AUTH_UNUSED;
124 buffer_free(&e->input);
125 buffer_free(&e->output);
126 buffer_free(&e->request);
127}
128
129static void
Damien Millerad833b32000-08-23 10:46:23 +1000130idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131{
Damien Millerad833b32000-08-23 10:46:23 +1000132 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000133
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000134 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100135 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000136 idtable[i].nentries = 0;
137 }
138}
139
140/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000141static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000142idtab_lookup(int version)
143{
144 if (version < 1 || version > 2)
145 fatal("internal error, bad protocol version %d", version);
146 return &idtable[version];
147}
148
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000149static void
150free_identity(Identity *id)
151{
152 key_free(id->key);
153 xfree(id->comment);
154 xfree(id);
155}
156
Damien Millerad833b32000-08-23 10:46:23 +1000157/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100158static Identity *
159lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000160{
Damien Miller1a534ae2002-01-22 23:26:13 +1100161 Identity *id;
162
Damien Millerad833b32000-08-23 10:46:23 +1000163 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100164 TAILQ_FOREACH(id, &tab->idlist, next) {
165 if (key_equal(key, id->key))
166 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000167 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100168 return (NULL);
169}
170
Damien Miller6c711792003-01-24 11:36:23 +1100171/* Check confirmation of keysign request */
172static int
173confirm_key(Identity *id)
174{
Darren Tuckerce327b62004-11-05 20:38:03 +1100175 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100176 int ret = -1;
177
178 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100179 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
180 id->comment, p))
181 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100182 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100183
Damien Miller6c711792003-01-24 11:36:23 +1100184 return (ret);
185}
186
Damien Millerad833b32000-08-23 10:46:23 +1000187/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static void
Damien Millerad833b32000-08-23 10:46:23 +1000189process_request_identities(SocketEntry *e, int version)
190{
191 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100192 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000193 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller95def091999-11-25 00:26:21 +1100195 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000196 buffer_put_char(&msg, (version == 1) ?
197 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
198 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100199 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000201 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
202 buffer_put_bignum(&msg, id->key->rsa->e);
203 buffer_put_bignum(&msg, id->key->rsa->n);
204 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000205 u_char *blob;
206 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000208 buffer_put_string(&msg, blob, blen);
209 xfree(blob);
210 }
211 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100212 }
213 buffer_put_int(&e->output, buffer_len(&msg));
214 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
215 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Millerad833b32000-08-23 10:46:23 +1000218/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static void
Damien Millerad833b32000-08-23 10:46:23 +1000220process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000222 u_char buf[32], mdbuf[16], session_id[16];
223 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000224 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000225 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000226 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100227 Buffer msg;
228 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000229 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100232 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100233 if ((challenge = BN_new()) == NULL)
234 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000235
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000236 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000237 buffer_get_bignum(&e->request, key->rsa->e);
238 buffer_get_bignum(&e->request, key->rsa->n);
239 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Millerad833b32000-08-23 10:46:23 +1000241 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000242 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000243 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000244 buffer_get(&e->request, session_id, 16);
245 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000246 if (response_type != 1)
247 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller1a534ae2002-01-22 23:26:13 +1100249 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100250 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100251 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000252 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100253 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
254 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100255
Damien Millerad833b32000-08-23 10:46:23 +1000256 /* The response is MD5 of decrypted challenge plus session id. */
257 len = BN_num_bytes(challenge);
258 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000259 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000260 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100261 }
Damien Millerad833b32000-08-23 10:46:23 +1000262 memset(buf, 0, 32);
263 BN_bn2bin(challenge, buf + 32 - len);
264 MD5_Init(&md);
265 MD5_Update(&md, buf, 32);
266 MD5_Update(&md, session_id, 16);
267 MD5_Final(mdbuf, &md);
268
269 /* Send the response. */
270 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
271 for (i = 0; i < 16; i++)
272 buffer_put_char(&msg, mdbuf[i]);
273 goto send;
274 }
275
276failure:
277 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100278 buffer_put_char(&msg, SSH_AGENT_FAILURE);
279send:
280 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000281 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
282 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100283 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000284 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285}
286
Damien Millerad833b32000-08-23 10:46:23 +1000287/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static void
Damien Millerad833b32000-08-23 10:46:23 +1000289process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000291 u_char *blob, *data, *signature = NULL;
292 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000293 extern int datafellows;
294 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000295 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000296 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000297
298 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000299
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000300 blob = buffer_get_string(&e->request, &blen);
301 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100302
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000303 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100304 if (flags & SSH_AGENT_OLD_SIGNATURE)
305 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000306
Damien Miller0bc1bd82000-11-13 22:57:25 +1100307 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000308 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100309 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100310 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100311 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000312 }
313 key_free(key);
314 buffer_init(&msg);
315 if (ok == 0) {
316 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
317 buffer_put_string(&msg, signature, slen);
318 } else {
319 buffer_put_char(&msg, SSH_AGENT_FAILURE);
320 }
321 buffer_put_int(&e->output, buffer_len(&msg));
322 buffer_append(&e->output, buffer_ptr(&msg),
323 buffer_len(&msg));
324 buffer_free(&msg);
325 xfree(data);
326 xfree(blob);
327 if (signature != NULL)
328 xfree(signature);
329}
330
331/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static void
Damien Millerad833b32000-08-23 10:46:23 +1000333process_remove_identity(SocketEntry *e, int version)
334{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000335 u_int blen, bits;
336 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100337 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000338 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100339
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000340 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000341 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100342 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000343 bits = buffer_get_int(&e->request);
344 buffer_get_bignum(&e->request, key->rsa->e);
345 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Millerad833b32000-08-23 10:46:23 +1000347 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000348 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000349 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000350 break;
351 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000352 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100353 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000354 xfree(blob);
355 break;
356 }
357 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100358 Identity *id = lookup_identity(key, version);
359 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100360 /*
361 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100362 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000363 * the array, we actually free the key there and move
364 * all the entries between the empty slot and the end
365 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100366 */
Damien Millerad833b32000-08-23 10:46:23 +1000367 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100368 if (tab->nentries < 1)
369 fatal("process_remove_identity: "
370 "internal error: tab->nentries %d",
371 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100372 TAILQ_REMOVE(&tab->idlist, id, next);
373 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000374 tab->nentries--;
375 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100376 }
Damien Millerad833b32000-08-23 10:46:23 +1000377 key_free(key);
378 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000380 buffer_put_char(&e->output,
381 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
Ben Lindstrombba81212001-06-25 05:01:22 +0000384static void
Damien Millerad833b32000-08-23 10:46:23 +1000385process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386{
Damien Millerad833b32000-08-23 10:46:23 +1000387 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100388 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Damien Miller95def091999-11-25 00:26:21 +1100390 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100391 for (id = TAILQ_FIRST(&tab->idlist); id;
392 id = TAILQ_FIRST(&tab->idlist)) {
393 TAILQ_REMOVE(&tab->idlist, id, next);
394 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100395 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller95def091999-11-25 00:26:21 +1100397 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000398 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
400 /* Send success. */
401 buffer_put_int(&e->output, 1);
402 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100403}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Ben Lindstrombba81212001-06-25 05:01:22 +0000405static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000406reaper(void)
407{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000408 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000409 Identity *id, *nxt;
410 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000411 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000412
413 for (version = 1; version < 3; version++) {
414 tab = idtab_lookup(version);
415 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
416 nxt = TAILQ_NEXT(id, next);
417 if (id->death != 0 && now >= id->death) {
418 TAILQ_REMOVE(&tab->idlist, id, next);
419 free_identity(id);
420 tab->nentries--;
421 }
422 }
423 }
424}
425
426static void
Damien Millerad833b32000-08-23 10:46:23 +1000427process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100428{
Damien Millerad833b32000-08-23 10:46:23 +1000429 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100430 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000431 char *type_name, *comment;
432 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100433
Damien Millerad833b32000-08-23 10:46:23 +1000434 switch (version) {
435 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100436 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000437 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000438 buffer_get_bignum(&e->request, k->rsa->n);
439 buffer_get_bignum(&e->request, k->rsa->e);
440 buffer_get_bignum(&e->request, k->rsa->d);
441 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100442
Damien Millerad833b32000-08-23 10:46:23 +1000443 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000444 buffer_get_bignum(&e->request, k->rsa->q); /* p */
445 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100446
Damien Millerad833b32000-08-23 10:46:23 +1000447 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000448 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000449 break;
450 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000451 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000452 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000454 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100455 case KEY_DSA:
456 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000457 buffer_get_bignum2(&e->request, k->dsa->p);
458 buffer_get_bignum2(&e->request, k->dsa->q);
459 buffer_get_bignum2(&e->request, k->dsa->g);
460 buffer_get_bignum2(&e->request, k->dsa->pub_key);
461 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 break;
463 case KEY_RSA:
464 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000465 buffer_get_bignum2(&e->request, k->rsa->n);
466 buffer_get_bignum2(&e->request, k->rsa->e);
467 buffer_get_bignum2(&e->request, k->rsa->d);
468 buffer_get_bignum2(&e->request, k->rsa->iqmp);
469 buffer_get_bignum2(&e->request, k->rsa->p);
470 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471
472 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000473 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 break;
475 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000476 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000477 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100478 }
Damien Millerad833b32000-08-23 10:46:23 +1000479 break;
480 }
Damien Millerc51d0732003-03-15 11:37:09 +1100481 /* enable blinding */
482 switch (k->type) {
483 case KEY_RSA:
484 case KEY_RSA1:
485 if (RSA_blinding_on(k->rsa, NULL) != 1) {
486 error("process_add_identity: RSA_blinding_on failed");
487 key_free(k);
488 goto send;
489 }
490 break;
491 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000492 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000493 if (k == NULL) {
494 xfree(comment);
495 goto send;
496 }
497 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000498 while (buffer_len(&e->request)) {
499 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000500 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000501 death = time(NULL) + buffer_get_int(&e->request);
502 break;
Damien Miller6c711792003-01-24 11:36:23 +1100503 case SSH_AGENT_CONSTRAIN_CONFIRM:
504 confirm = 1;
505 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000506 default:
507 break;
508 }
509 }
Damien Miller53d81482003-01-22 11:47:19 +1100510 if (lifetime && !death)
511 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000512 if (lookup_identity(k, version) == NULL) {
513 Identity *id = xmalloc(sizeof(Identity));
514 id->key = k;
515 id->comment = comment;
516 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100517 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000518 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
519 /* Increment the number of identities. */
520 tab->nentries++;
521 } else {
522 key_free(k);
523 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000524 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000525send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000526 buffer_put_int(&e->output, 1);
527 buffer_put_char(&e->output,
528 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
529}
530
Ben Lindstrom2f717042002-06-06 21:52:03 +0000531/* XXX todo: encrypt sensitive data with passphrase */
532static void
533process_lock_agent(SocketEntry *e, int lock)
534{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000535 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000536 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000537
538 passwd = buffer_get_string(&e->request, NULL);
539 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
540 locked = 0;
541 memset(lock_passwd, 0, strlen(lock_passwd));
542 xfree(lock_passwd);
543 lock_passwd = NULL;
544 success = 1;
545 } else if (!locked && lock) {
546 locked = 1;
547 lock_passwd = xstrdup(passwd);
548 success = 1;
549 }
550 memset(passwd, 0, strlen(passwd));
551 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000552
Ben Lindstrom2f717042002-06-06 21:52:03 +0000553 buffer_put_int(&e->output, 1);
554 buffer_put_char(&e->output,
555 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000556}
557
558static void
559no_identities(SocketEntry *e, u_int type)
560{
561 Buffer msg;
562
563 buffer_init(&msg);
564 buffer_put_char(&msg,
565 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
566 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
567 buffer_put_int(&msg, 0);
568 buffer_put_int(&e->output, buffer_len(&msg));
569 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
570 buffer_free(&msg);
571}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000572
573#ifdef SMARTCARD
574static void
575process_add_smartcard_key (SocketEntry *e)
576{
Ben Lindstromba72d302002-03-22 03:51:06 +0000577 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000578 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000579 Key **keys, *k;
580 Identity *id;
581 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100582
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000583 sc_reader_id = buffer_get_string(&e->request, NULL);
584 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000585
586 while (buffer_len(&e->request)) {
587 switch (buffer_get_char(&e->request)) {
588 case SSH_AGENT_CONSTRAIN_LIFETIME:
589 death = time(NULL) + buffer_get_int(&e->request);
590 break;
591 case SSH_AGENT_CONSTRAIN_CONFIRM:
592 confirm = 1;
593 break;
594 default:
595 break;
596 }
597 }
598 if (lifetime && !death)
599 death = time(NULL) + lifetime;
600
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000601 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000602 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000603 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000604
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000605 if (keys == NULL || keys[0] == NULL) {
606 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607 goto send;
608 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000609 for (i = 0; keys[i] != NULL; i++) {
610 k = keys[i];
611 version = k->type == KEY_RSA1 ? 1 : 2;
612 tab = idtab_lookup(version);
613 if (lookup_identity(k, version) == NULL) {
614 id = xmalloc(sizeof(Identity));
615 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000616 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000617 id->death = death;
618 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000619 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
620 tab->nentries++;
621 success = 1;
622 } else {
623 key_free(k);
624 }
625 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000626 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000627 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000628send:
629 buffer_put_int(&e->output, 1);
630 buffer_put_char(&e->output,
631 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
632}
633
634static void
635process_remove_smartcard_key(SocketEntry *e)
636{
Ben Lindstromba72d302002-03-22 03:51:06 +0000637 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000638 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000639 Key **keys, *k = NULL;
640 Identity *id;
641 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000642
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000643 sc_reader_id = buffer_get_string(&e->request, NULL);
644 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000645 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000646 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000647 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000648
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000649 if (keys == NULL || keys[0] == NULL) {
650 error("sc_get_keys failed");
651 goto send;
652 }
653 for (i = 0; keys[i] != NULL; i++) {
654 k = keys[i];
655 version = k->type == KEY_RSA1 ? 1 : 2;
656 if ((id = lookup_identity(k, version)) != NULL) {
657 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000658 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000659 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100660 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661 success = 1;
662 }
663 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000664 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000665 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000666 xfree(keys);
667send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000668 buffer_put_int(&e->output, 1);
669 buffer_put_char(&e->output,
670 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
671}
Ben Lindstromffce1472001-08-06 21:57:31 +0000672#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000673
Damien Millerad833b32000-08-23 10:46:23 +1000674/* dispatch incoming messages */
675
Ben Lindstrombba81212001-06-25 05:01:22 +0000676static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677process_message(SocketEntry *e)
678{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000679 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000680 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000681
682 /* kill dead keys */
683 reaper();
684
Damien Miller95def091999-11-25 00:26:21 +1100685 if (buffer_len(&e->input) < 5)
686 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100687 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100688 msg_len = GET_32BIT(cp);
689 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000690 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100691 return;
692 }
693 if (buffer_len(&e->input) < msg_len + 4)
694 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000695
696 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100697 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000698 buffer_clear(&e->request);
699 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
700 buffer_consume(&e->input, msg_len);
701 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702
Ben Lindstrom2f717042002-06-06 21:52:03 +0000703 /* check wheter agent is locked */
704 if (locked && type != SSH_AGENTC_UNLOCK) {
705 buffer_clear(&e->request);
706 switch (type) {
707 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
708 case SSH2_AGENTC_REQUEST_IDENTITIES:
709 /* send empty lists */
710 no_identities(e, type);
711 break;
712 default:
713 /* send a fail message for all other request types */
714 buffer_put_int(&e->output, 1);
715 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
716 }
717 return;
718 }
719
Ben Lindstrom3f471632001-07-04 03:53:15 +0000720 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100721 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000722 case SSH_AGENTC_LOCK:
723 case SSH_AGENTC_UNLOCK:
724 process_lock_agent(e, type == SSH_AGENTC_LOCK);
725 break;
Damien Millerad833b32000-08-23 10:46:23 +1000726 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100727 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000728 process_authentication_challenge1(e);
729 break;
730 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
731 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100732 break;
733 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000734 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000735 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100736 break;
737 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000738 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100739 break;
740 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000741 process_remove_all_identities(e, 1);
742 break;
743 /* ssh2 */
744 case SSH2_AGENTC_SIGN_REQUEST:
745 process_sign_request2(e);
746 break;
747 case SSH2_AGENTC_REQUEST_IDENTITIES:
748 process_request_identities(e, 2);
749 break;
750 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000751 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000752 process_add_identity(e, 2);
753 break;
754 case SSH2_AGENTC_REMOVE_IDENTITY:
755 process_remove_identity(e, 2);
756 break;
757 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
758 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100759 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000760#ifdef SMARTCARD
761 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000762 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000763 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100764 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000765 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
766 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100767 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000768#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100769 default:
770 /* Unknown message. Respond with failure. */
771 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000772 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100773 buffer_put_int(&e->output, 1);
774 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
775 break;
776 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777}
778
Ben Lindstrombba81212001-06-25 05:01:22 +0000779static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000780new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000782 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000783
Damien Miller232711f2004-06-15 10:35:30 +1000784 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller95def091999-11-25 00:26:21 +1100786 if (fd > max_fd)
787 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788
Damien Miller95def091999-11-25 00:26:21 +1100789 for (i = 0; i < sockets_alloc; i++)
790 if (sockets[i].type == AUTH_UNUSED) {
791 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100792 buffer_init(&sockets[i].input);
793 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000794 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000795 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100796 return;
797 }
798 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000799 new_alloc = sockets_alloc + 10;
Damien Miller95def091999-11-25 00:26:21 +1100800 if (sockets)
Darren Tuckerfb16b242003-09-22 21:04:23 +1000801 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
Damien Miller95def091999-11-25 00:26:21 +1100802 else
Darren Tuckerfb16b242003-09-22 21:04:23 +1000803 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
804 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100805 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000806 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100807 sockets[old_alloc].fd = fd;
808 buffer_init(&sockets[old_alloc].input);
809 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000810 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812}
813
Ben Lindstrombba81212001-06-25 05:01:22 +0000814static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000815prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000817 u_int i, sz;
818 int n = 0;
819
820 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100821 switch (sockets[i].type) {
822 case AUTH_SOCKET:
823 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000824 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100825 break;
826 case AUTH_UNUSED:
827 break;
828 default:
829 fatal("Unknown socket type %d", sockets[i].type);
830 break;
831 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000832 }
833
834 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000835 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000836 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000837 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000838 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000839 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000840 *fdrp = xmalloc(sz);
841 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000842 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000844 if (n < *fdl)
845 debug("XXX shrink: %d < %d", n, *fdl);
846 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 memset(*fdrp, 0, sz);
848 memset(*fdwp, 0, sz);
849
850 for (i = 0; i < sockets_alloc; i++) {
851 switch (sockets[i].type) {
852 case AUTH_SOCKET:
853 case AUTH_CONNECTION:
854 FD_SET(sockets[i].fd, *fdrp);
855 if (buffer_len(&sockets[i].output) > 0)
856 FD_SET(sockets[i].fd, *fdwp);
857 break;
858 default:
859 break;
860 }
861 }
862 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863}
864
Ben Lindstrombba81212001-06-25 05:01:22 +0000865static void
Damien Miller95def091999-11-25 00:26:21 +1100866after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000868 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100869 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100870 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000871 int len, sock;
872 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000873 uid_t euid;
874 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
Damien Miller95def091999-11-25 00:26:21 +1100876 for (i = 0; i < sockets_alloc; i++)
877 switch (sockets[i].type) {
878 case AUTH_UNUSED:
879 break;
880 case AUTH_SOCKET:
881 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100882 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000883 sock = accept(sockets[i].fd,
884 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100885 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100886 error("accept from AUTH_SOCKET: %s",
887 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100888 break;
889 }
Damien Miller538f1812002-09-12 09:51:10 +1000890 if (getpeereid(sock, &euid, &egid) < 0) {
891 error("getpeereid %d failed: %s",
892 sock, strerror(errno));
893 close(sock);
894 break;
895 }
Damien Milleraf9de382002-10-03 11:54:35 +1000896 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000897 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000898 "peer euid %u != uid %u",
899 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000900 close(sock);
901 break;
902 }
Damien Miller95def091999-11-25 00:26:21 +1100903 new_socket(AUTH_CONNECTION, sock);
904 }
905 break;
906 case AUTH_CONNECTION:
907 if (buffer_len(&sockets[i].output) > 0 &&
908 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000909 do {
910 len = write(sockets[i].fd,
911 buffer_ptr(&sockets[i].output),
912 buffer_len(&sockets[i].output));
913 if (len == -1 && (errno == EAGAIN ||
914 errno == EINTR))
915 continue;
916 break;
917 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100918 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000919 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100920 break;
921 }
922 buffer_consume(&sockets[i].output, len);
923 }
924 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000925 do {
926 len = read(sockets[i].fd, buf, sizeof(buf));
927 if (len == -1 && (errno == EAGAIN ||
928 errno == EINTR))
929 continue;
930 break;
931 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100932 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000933 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100934 break;
935 }
936 buffer_append(&sockets[i].input, buf, len);
937 process_message(&sockets[i]);
938 }
939 break;
940 default:
941 fatal("Unknown type %d", sockets[i].type);
942 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943}
944
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000946cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000947{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000948 if (socket_name[0])
949 unlink(socket_name);
950 if (socket_dir[0])
951 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952}
953
Darren Tucker3e33cec2003-10-02 16:12:36 +1000954void
Damien Miller792c5111999-10-29 09:47:09 +1000955cleanup_exit(int i)
956{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000957 cleanup_socket();
958 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000959}
960
Ben Lindstrombba81212001-06-25 05:01:22 +0000961static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000962cleanup_handler(int sig)
963{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000964 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000965 _exit(2);
966}
967
Ben Lindstrombba81212001-06-25 05:01:22 +0000968static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000969check_parent_exists(int sig)
970{
971 int save_errno = errno;
972
973 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
974 /* printf("Parent has died - Authentication agent exiting.\n"); */
975 cleanup_handler(sig); /* safe */
976 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000977 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000978 alarm(10);
979 errno = save_errno;
980}
981
982static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000983usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000984{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000985 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000986 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000987 fprintf(stderr, "Options:\n");
988 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
989 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
990 fprintf(stderr, " -k Kill the current agent.\n");
991 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000992 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100993 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100994 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000995}
996
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997int
998main(int ac, char **av)
999{
Damien Miller6c711792003-01-24 11:36:23 +11001000 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001001 int sock, fd, ch;
1002 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001003 char *shell, *format, *pidstr, *agentsocket = NULL;
1004 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001005 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001006#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001007 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001008#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001009 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001010 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001011 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001012 pid_t pid;
1013 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001014
Darren Tuckerce321d82005-10-03 18:11:24 +10001015 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1016 sanitise_stdfd();
1017
Damien Miller6cffb9a2002-09-04 16:20:26 +10001018 /* drop */
1019 setegid(getgid());
1020 setgid(getgid());
1021
Damien Miller6c4914a2004-03-03 11:08:59 +11001022#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1023 /* Disable ptrace on Linux without sgid bit */
1024 prctl(PR_SET_DUMPABLE, 0);
1025#endif
1026
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001027 SSLeay_add_all_algorithms();
1028
Damien Miller59d3d5b2003-08-22 09:34:41 +10001029 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001030 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001031 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001032
Damien Miller53d81482003-01-22 11:47:19 +11001033 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001034 switch (ch) {
1035 case 'c':
1036 if (s_flag)
1037 usage();
1038 c_flag++;
1039 break;
1040 case 'k':
1041 k_flag++;
1042 break;
1043 case 's':
1044 if (c_flag)
1045 usage();
1046 s_flag++;
1047 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001048 case 'd':
1049 if (d_flag)
1050 usage();
1051 d_flag++;
1052 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001053 case 'a':
1054 agentsocket = optarg;
1055 break;
Damien Miller53d81482003-01-22 11:47:19 +11001056 case 't':
1057 if ((lifetime = convtime(optarg)) == -1) {
1058 fprintf(stderr, "Invalid lifetime\n");
1059 usage();
1060 }
1061 break;
Damien Miller95def091999-11-25 00:26:21 +11001062 default:
1063 usage();
1064 }
Damien Miller792c5111999-10-29 09:47:09 +10001065 }
Damien Miller95def091999-11-25 00:26:21 +11001066 ac -= optind;
1067 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001068
Ben Lindstromd94580c2001-07-04 03:48:02 +00001069 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001070 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001071
Ben Lindstromeecdf232002-04-02 21:03:51 +00001072 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001073 shell = getenv("SHELL");
1074 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1075 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001076 }
Damien Miller95def091999-11-25 00:26:21 +11001077 if (k_flag) {
1078 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1079 if (pidstr == NULL) {
1080 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001081 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001082 exit(1);
1083 }
1084 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001085 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001086 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001087 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001088 exit(1);
1089 }
1090 if (kill(pid, SIGTERM) == -1) {
1091 perror("kill");
1092 exit(1);
1093 }
1094 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1095 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1096 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001097 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001098 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001099 }
Damien Miller95def091999-11-25 00:26:21 +11001100 parent_pid = getpid();
1101
Ben Lindstromb7788f32002-06-06 21:46:08 +00001102 if (agentsocket == NULL) {
1103 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001104 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001105 if (mkdtemp(socket_dir) == NULL) {
1106 perror("mkdtemp: private socket dir");
1107 exit(1);
1108 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001109 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1110 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001111 } else {
1112 /* Try to use specified agent socket */
1113 socket_dir[0] = '\0';
1114 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116
Damien Miller5428f641999-11-25 11:54:57 +11001117 /*
1118 * Create socket early so it will exist before command gets run from
1119 * the parent.
1120 */
Damien Miller95def091999-11-25 00:26:21 +11001121 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1122 if (sock < 0) {
1123 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001124 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001125 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001126 }
Damien Miller95def091999-11-25 00:26:21 +11001127 memset(&sunaddr, 0, sizeof(sunaddr));
1128 sunaddr.sun_family = AF_UNIX;
1129 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001130 prev_mask = umask(0177);
Damien Miller95def091999-11-25 00:26:21 +11001131 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1132 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001133 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001134 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001135 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001137 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001138 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001139 perror("listen");
1140 cleanup_exit(1);
1141 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001142
Damien Miller5428f641999-11-25 11:54:57 +11001143 /*
1144 * Fork, and have the parent execute the command, if any, or present
1145 * the socket data. The child continues as the authentication agent.
1146 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001147 if (d_flag) {
1148 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1149 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1150 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1151 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001152 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001153 goto skip;
1154 }
Damien Miller95def091999-11-25 00:26:21 +11001155 pid = fork();
1156 if (pid == -1) {
1157 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001158 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001159 }
1160 if (pid != 0) { /* Parent - execute the given command. */
1161 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001162 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001163 if (ac == 0) {
1164 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1165 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001167 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001168 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001169 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001170 exit(0);
1171 }
Damien Millere4340be2000-09-16 13:29:08 +11001172 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1173 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1174 perror("setenv");
1175 exit(1);
1176 }
Damien Miller95def091999-11-25 00:26:21 +11001177 execvp(av[0], av);
1178 perror(av[0]);
1179 exit(1);
1180 }
Damien Millerc653b892002-02-08 22:05:41 +11001181 /* child */
1182 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001183
1184 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001185 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001186 cleanup_exit(1);
1187 }
1188
1189 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001190 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1191 /* XXX might close listen socket */
1192 (void)dup2(fd, STDIN_FILENO);
1193 (void)dup2(fd, STDOUT_FILENO);
1194 (void)dup2(fd, STDERR_FILENO);
1195 if (fd > 2)
1196 close(fd);
1197 }
Damien Miller95def091999-11-25 00:26:21 +11001198
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001199#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001200 /* deny core dumps, since memory contains unencrypted private keys */
1201 rlim.rlim_cur = rlim.rlim_max = 0;
1202 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001203 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001204 cleanup_exit(1);
1205 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001206#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001207
1208skip:
Damien Miller95def091999-11-25 00:26:21 +11001209 new_socket(AUTH_SOCKET, sock);
1210 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001211 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001212 alarm(10);
1213 }
Damien Millerad833b32000-08-23 10:46:23 +10001214 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001215 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001216 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001217 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001218 signal(SIGHUP, cleanup_handler);
1219 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001220 nalloc = 0;
1221
Damien Miller95def091999-11-25 00:26:21 +11001222 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001223 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001224 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001225 if (errno == EINTR)
1226 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001227 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001228 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001229 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001230 }
1231 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232}