blob: b09ce86f29c5d56db8027c100d0c7d6294d992e4 [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 Miller6ff3cad2006-03-15 11:52:09 +110037RCSID("$OpenBSD: ssh-agent.c,v 1.129 2006/02/20 17:02:44 stevesk Exp $");
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller42fb0682006-03-15 14:03:06 +110040#include <sys/stat.h>
Damien Miller574c41f2006-03-15 11:40:10 +110041#ifdef HAVE_SYS_UN_H
42# include <sys/un.h>
43#endif
Damien Miller9b481512002-09-12 10:43:29 +100044#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110045#include <sys/resource.h>
Damien Miller03e20032006-03-15 11:16:59 +110046#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110047# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110048#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110049#include <signal.h>
50
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include <openssl/evp.h>
52#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54#include "ssh.h"
55#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "buffer.h"
57#include "bufaux.h"
58#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100060#include "key.h"
61#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110062#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110064#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Ben Lindstrom3f471632001-07-04 03:53:15 +000066#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000067#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000068#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000069
Damien Miller6c4914a2004-03-03 11:08:59 +110070#if defined(HAVE_SYS_PRCTL_H)
71#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
72#endif
73
Ben Lindstrom65366a82001-12-06 16:32:47 +000074typedef enum {
75 AUTH_UNUSED,
76 AUTH_SOCKET,
77 AUTH_CONNECTION
78} sock_type;
79
Damien Miller95def091999-11-25 00:26:21 +110080typedef struct {
81 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000082 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110083 Buffer input;
84 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000085 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086} SocketEntry;
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089SocketEntry *sockets = NULL;
90
Damien Miller1a534ae2002-01-22 23:26:13 +110091typedef struct identity {
92 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100093 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110094 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000095 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110096 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097} Identity;
98
Damien Millerad833b32000-08-23 10:46:23 +100099typedef struct {
100 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100101 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000102} Idtab;
103
104/* private key table, one per protocol version */
105Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
107int max_fd = 0;
108
109/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000110pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
112/* pathname and directory for AUTH_SOCKET */
113char socket_name[1024];
114char socket_dir[1024];
115
Ben Lindstrom2f717042002-06-06 21:52:03 +0000116/* locking */
117int locked = 0;
118char *lock_passwd = NULL;
119
Damien Miller95def091999-11-25 00:26:21 +1100120extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100121
Damien Miller53d81482003-01-22 11:47:19 +1100122/* Default lifetime (0 == forever) */
123static int lifetime = 0;
124
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static void
Damien Miller58f34862002-09-04 16:31:21 +1000126close_socket(SocketEntry *e)
127{
Damien Miller58f34862002-09-04 16:31:21 +1000128 close(e->fd);
129 e->fd = -1;
130 e->type = AUTH_UNUSED;
131 buffer_free(&e->input);
132 buffer_free(&e->output);
133 buffer_free(&e->request);
134}
135
136static void
Damien Millerad833b32000-08-23 10:46:23 +1000137idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Millerad833b32000-08-23 10:46:23 +1000139 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000140
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000141 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100142 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000143 idtable[i].nentries = 0;
144 }
145}
146
147/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000149idtab_lookup(int version)
150{
151 if (version < 1 || version > 2)
152 fatal("internal error, bad protocol version %d", version);
153 return &idtable[version];
154}
155
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000156static void
157free_identity(Identity *id)
158{
159 key_free(id->key);
160 xfree(id->comment);
161 xfree(id);
162}
163
Damien Millerad833b32000-08-23 10:46:23 +1000164/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100165static Identity *
166lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000167{
Damien Miller1a534ae2002-01-22 23:26:13 +1100168 Identity *id;
169
Damien Millerad833b32000-08-23 10:46:23 +1000170 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100171 TAILQ_FOREACH(id, &tab->idlist, next) {
172 if (key_equal(key, id->key))
173 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000174 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100175 return (NULL);
176}
177
Damien Miller6c711792003-01-24 11:36:23 +1100178/* Check confirmation of keysign request */
179static int
180confirm_key(Identity *id)
181{
Darren Tuckerce327b62004-11-05 20:38:03 +1100182 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100183 int ret = -1;
184
185 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100186 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
187 id->comment, p))
188 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100189 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100190
Damien Miller6c711792003-01-24 11:36:23 +1100191 return (ret);
192}
193
Damien Millerad833b32000-08-23 10:46:23 +1000194/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Damien Millerad833b32000-08-23 10:46:23 +1000196process_request_identities(SocketEntry *e, int version)
197{
198 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100199 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000200 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller95def091999-11-25 00:26:21 +1100202 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000203 buffer_put_char(&msg, (version == 1) ?
204 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
205 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100206 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000208 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
209 buffer_put_bignum(&msg, id->key->rsa->e);
210 buffer_put_bignum(&msg, id->key->rsa->n);
211 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000212 u_char *blob;
213 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100214 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000215 buffer_put_string(&msg, blob, blen);
216 xfree(blob);
217 }
218 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100219 }
220 buffer_put_int(&e->output, buffer_len(&msg));
221 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
222 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223}
224
Damien Millerad833b32000-08-23 10:46:23 +1000225/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static void
Damien Millerad833b32000-08-23 10:46:23 +1000227process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000229 u_char buf[32], mdbuf[16], session_id[16];
230 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000231 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000232 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000233 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100234 Buffer msg;
235 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000236 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller95def091999-11-25 00:26:21 +1100238 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100239 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100240 if ((challenge = BN_new()) == NULL)
241 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000242
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000243 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000244 buffer_get_bignum(&e->request, key->rsa->e);
245 buffer_get_bignum(&e->request, key->rsa->n);
246 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Millerad833b32000-08-23 10:46:23 +1000248 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000249 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000250 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000251 buffer_get(&e->request, session_id, 16);
252 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000253 if (response_type != 1)
254 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller1a534ae2002-01-22 23:26:13 +1100256 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100257 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100258 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000259 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100260 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
261 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100262
Damien Millerad833b32000-08-23 10:46:23 +1000263 /* The response is MD5 of decrypted challenge plus session id. */
264 len = BN_num_bytes(challenge);
265 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000266 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000267 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100268 }
Damien Millerad833b32000-08-23 10:46:23 +1000269 memset(buf, 0, 32);
270 BN_bn2bin(challenge, buf + 32 - len);
271 MD5_Init(&md);
272 MD5_Update(&md, buf, 32);
273 MD5_Update(&md, session_id, 16);
274 MD5_Final(mdbuf, &md);
275
276 /* Send the response. */
277 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
278 for (i = 0; i < 16; i++)
279 buffer_put_char(&msg, mdbuf[i]);
280 goto send;
281 }
282
283failure:
284 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100285 buffer_put_char(&msg, SSH_AGENT_FAILURE);
286send:
287 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000288 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
289 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100290 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000291 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
Damien Millerad833b32000-08-23 10:46:23 +1000294/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000295static void
Damien Millerad833b32000-08-23 10:46:23 +1000296process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000298 u_char *blob, *data, *signature = NULL;
299 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000300 extern int datafellows;
301 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000302 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000303 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000304
305 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000306
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000307 blob = buffer_get_string(&e->request, &blen);
308 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100309
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000310 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100311 if (flags & SSH_AGENT_OLD_SIGNATURE)
312 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000313
Damien Miller0bc1bd82000-11-13 22:57:25 +1100314 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000315 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100316 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100317 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100318 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000319 }
320 key_free(key);
321 buffer_init(&msg);
322 if (ok == 0) {
323 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
324 buffer_put_string(&msg, signature, slen);
325 } else {
326 buffer_put_char(&msg, SSH_AGENT_FAILURE);
327 }
328 buffer_put_int(&e->output, buffer_len(&msg));
329 buffer_append(&e->output, buffer_ptr(&msg),
330 buffer_len(&msg));
331 buffer_free(&msg);
332 xfree(data);
333 xfree(blob);
334 if (signature != NULL)
335 xfree(signature);
336}
337
338/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000339static void
Damien Millerad833b32000-08-23 10:46:23 +1000340process_remove_identity(SocketEntry *e, int version)
341{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000342 u_int blen, bits;
343 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100344 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000345 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100346
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000347 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000348 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100349 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000350 bits = buffer_get_int(&e->request);
351 buffer_get_bignum(&e->request, key->rsa->e);
352 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Millerad833b32000-08-23 10:46:23 +1000354 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000355 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000356 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000357 break;
358 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000359 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100360 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000361 xfree(blob);
362 break;
363 }
364 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100365 Identity *id = lookup_identity(key, version);
366 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100367 /*
368 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100369 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000370 * the array, we actually free the key there and move
371 * all the entries between the empty slot and the end
372 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100373 */
Damien Millerad833b32000-08-23 10:46:23 +1000374 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 if (tab->nentries < 1)
376 fatal("process_remove_identity: "
377 "internal error: tab->nentries %d",
378 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100379 TAILQ_REMOVE(&tab->idlist, id, next);
380 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000381 tab->nentries--;
382 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100383 }
Damien Millerad833b32000-08-23 10:46:23 +1000384 key_free(key);
385 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000387 buffer_put_char(&e->output,
388 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389}
390
Ben Lindstrombba81212001-06-25 05:01:22 +0000391static void
Damien Millerad833b32000-08-23 10:46:23 +1000392process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393{
Damien Millerad833b32000-08-23 10:46:23 +1000394 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100395 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller95def091999-11-25 00:26:21 +1100397 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100398 for (id = TAILQ_FIRST(&tab->idlist); id;
399 id = TAILQ_FIRST(&tab->idlist)) {
400 TAILQ_REMOVE(&tab->idlist, id, next);
401 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100402 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Damien Miller95def091999-11-25 00:26:21 +1100404 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000405 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
407 /* Send success. */
408 buffer_put_int(&e->output, 1);
409 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100410}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000413reaper(void)
414{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000415 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000416 Identity *id, *nxt;
417 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000418 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000419
420 for (version = 1; version < 3; version++) {
421 tab = idtab_lookup(version);
422 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
423 nxt = TAILQ_NEXT(id, next);
424 if (id->death != 0 && now >= id->death) {
425 TAILQ_REMOVE(&tab->idlist, id, next);
426 free_identity(id);
427 tab->nentries--;
428 }
429 }
430 }
431}
432
433static void
Damien Millerad833b32000-08-23 10:46:23 +1000434process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100435{
Damien Millerad833b32000-08-23 10:46:23 +1000436 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100437 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000438 char *type_name, *comment;
439 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100440
Damien Millerad833b32000-08-23 10:46:23 +1000441 switch (version) {
442 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100443 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000444 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000445 buffer_get_bignum(&e->request, k->rsa->n);
446 buffer_get_bignum(&e->request, k->rsa->e);
447 buffer_get_bignum(&e->request, k->rsa->d);
448 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100449
Damien Millerad833b32000-08-23 10:46:23 +1000450 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000451 buffer_get_bignum(&e->request, k->rsa->q); /* p */
452 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100453
Damien Millerad833b32000-08-23 10:46:23 +1000454 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000455 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000456 break;
457 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000458 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000459 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100460 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000461 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 case KEY_DSA:
463 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000464 buffer_get_bignum2(&e->request, k->dsa->p);
465 buffer_get_bignum2(&e->request, k->dsa->q);
466 buffer_get_bignum2(&e->request, k->dsa->g);
467 buffer_get_bignum2(&e->request, k->dsa->pub_key);
468 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 break;
470 case KEY_RSA:
471 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000472 buffer_get_bignum2(&e->request, k->rsa->n);
473 buffer_get_bignum2(&e->request, k->rsa->e);
474 buffer_get_bignum2(&e->request, k->rsa->d);
475 buffer_get_bignum2(&e->request, k->rsa->iqmp);
476 buffer_get_bignum2(&e->request, k->rsa->p);
477 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478
479 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000480 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 break;
482 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000483 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000484 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100485 }
Damien Millerad833b32000-08-23 10:46:23 +1000486 break;
487 }
Damien Millerc51d0732003-03-15 11:37:09 +1100488 /* enable blinding */
489 switch (k->type) {
490 case KEY_RSA:
491 case KEY_RSA1:
492 if (RSA_blinding_on(k->rsa, NULL) != 1) {
493 error("process_add_identity: RSA_blinding_on failed");
494 key_free(k);
495 goto send;
496 }
497 break;
498 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000499 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000500 if (k == NULL) {
501 xfree(comment);
502 goto send;
503 }
504 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000505 while (buffer_len(&e->request)) {
506 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000507 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000508 death = time(NULL) + buffer_get_int(&e->request);
509 break;
Damien Miller6c711792003-01-24 11:36:23 +1100510 case SSH_AGENT_CONSTRAIN_CONFIRM:
511 confirm = 1;
512 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000513 default:
514 break;
515 }
516 }
Damien Miller53d81482003-01-22 11:47:19 +1100517 if (lifetime && !death)
518 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000519 if (lookup_identity(k, version) == NULL) {
520 Identity *id = xmalloc(sizeof(Identity));
521 id->key = k;
522 id->comment = comment;
523 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100524 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000525 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
526 /* Increment the number of identities. */
527 tab->nentries++;
528 } else {
529 key_free(k);
530 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000531 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000532send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000533 buffer_put_int(&e->output, 1);
534 buffer_put_char(&e->output,
535 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
536}
537
Ben Lindstrom2f717042002-06-06 21:52:03 +0000538/* XXX todo: encrypt sensitive data with passphrase */
539static void
540process_lock_agent(SocketEntry *e, int lock)
541{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000542 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000543 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000544
545 passwd = buffer_get_string(&e->request, NULL);
546 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
547 locked = 0;
548 memset(lock_passwd, 0, strlen(lock_passwd));
549 xfree(lock_passwd);
550 lock_passwd = NULL;
551 success = 1;
552 } else if (!locked && lock) {
553 locked = 1;
554 lock_passwd = xstrdup(passwd);
555 success = 1;
556 }
557 memset(passwd, 0, strlen(passwd));
558 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000559
Ben Lindstrom2f717042002-06-06 21:52:03 +0000560 buffer_put_int(&e->output, 1);
561 buffer_put_char(&e->output,
562 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000563}
564
565static void
566no_identities(SocketEntry *e, u_int type)
567{
568 Buffer msg;
569
570 buffer_init(&msg);
571 buffer_put_char(&msg,
572 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
573 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
574 buffer_put_int(&msg, 0);
575 buffer_put_int(&e->output, buffer_len(&msg));
576 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
577 buffer_free(&msg);
578}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000579
580#ifdef SMARTCARD
581static void
582process_add_smartcard_key (SocketEntry *e)
583{
Ben Lindstromba72d302002-03-22 03:51:06 +0000584 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000585 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000586 Key **keys, *k;
587 Identity *id;
588 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000590 sc_reader_id = buffer_get_string(&e->request, NULL);
591 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000592
593 while (buffer_len(&e->request)) {
594 switch (buffer_get_char(&e->request)) {
595 case SSH_AGENT_CONSTRAIN_LIFETIME:
596 death = time(NULL) + buffer_get_int(&e->request);
597 break;
598 case SSH_AGENT_CONSTRAIN_CONFIRM:
599 confirm = 1;
600 break;
601 default:
602 break;
603 }
604 }
605 if (lifetime && !death)
606 death = time(NULL) + lifetime;
607
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000608 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000609 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000610 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000611
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000612 if (keys == NULL || keys[0] == NULL) {
613 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000614 goto send;
615 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000616 for (i = 0; keys[i] != NULL; i++) {
617 k = keys[i];
618 version = k->type == KEY_RSA1 ? 1 : 2;
619 tab = idtab_lookup(version);
620 if (lookup_identity(k, version) == NULL) {
621 id = xmalloc(sizeof(Identity));
622 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000623 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000624 id->death = death;
625 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000626 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
627 tab->nentries++;
628 success = 1;
629 } else {
630 key_free(k);
631 }
632 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000633 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000634 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000635send:
636 buffer_put_int(&e->output, 1);
637 buffer_put_char(&e->output,
638 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
639}
640
641static void
642process_remove_smartcard_key(SocketEntry *e)
643{
Ben Lindstromba72d302002-03-22 03:51:06 +0000644 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000645 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000646 Key **keys, *k = NULL;
647 Identity *id;
648 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000649
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000650 sc_reader_id = buffer_get_string(&e->request, NULL);
651 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000652 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000653 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000654 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000655
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000656 if (keys == NULL || keys[0] == NULL) {
657 error("sc_get_keys failed");
658 goto send;
659 }
660 for (i = 0; keys[i] != NULL; i++) {
661 k = keys[i];
662 version = k->type == KEY_RSA1 ? 1 : 2;
663 if ((id = lookup_identity(k, version)) != NULL) {
664 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000665 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000666 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100667 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000668 success = 1;
669 }
670 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000671 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000672 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000673 xfree(keys);
674send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000675 buffer_put_int(&e->output, 1);
676 buffer_put_char(&e->output,
677 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
678}
Ben Lindstromffce1472001-08-06 21:57:31 +0000679#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000680
Damien Millerad833b32000-08-23 10:46:23 +1000681/* dispatch incoming messages */
682
Ben Lindstrombba81212001-06-25 05:01:22 +0000683static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684process_message(SocketEntry *e)
685{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000686 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000687 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000688
689 /* kill dead keys */
690 reaper();
691
Damien Miller95def091999-11-25 00:26:21 +1100692 if (buffer_len(&e->input) < 5)
693 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100694 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100695 msg_len = GET_32BIT(cp);
696 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000697 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100698 return;
699 }
700 if (buffer_len(&e->input) < msg_len + 4)
701 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000702
703 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100704 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000705 buffer_clear(&e->request);
706 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
707 buffer_consume(&e->input, msg_len);
708 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Ben Lindstrom2f717042002-06-06 21:52:03 +0000710 /* check wheter agent is locked */
711 if (locked && type != SSH_AGENTC_UNLOCK) {
712 buffer_clear(&e->request);
713 switch (type) {
714 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
715 case SSH2_AGENTC_REQUEST_IDENTITIES:
716 /* send empty lists */
717 no_identities(e, type);
718 break;
719 default:
720 /* send a fail message for all other request types */
721 buffer_put_int(&e->output, 1);
722 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
723 }
724 return;
725 }
726
Ben Lindstrom3f471632001-07-04 03:53:15 +0000727 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100728 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000729 case SSH_AGENTC_LOCK:
730 case SSH_AGENTC_UNLOCK:
731 process_lock_agent(e, type == SSH_AGENTC_LOCK);
732 break;
Damien Millerad833b32000-08-23 10:46:23 +1000733 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100734 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000735 process_authentication_challenge1(e);
736 break;
737 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
738 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100739 break;
740 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000741 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000742 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100743 break;
744 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000745 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100746 break;
747 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000748 process_remove_all_identities(e, 1);
749 break;
750 /* ssh2 */
751 case SSH2_AGENTC_SIGN_REQUEST:
752 process_sign_request2(e);
753 break;
754 case SSH2_AGENTC_REQUEST_IDENTITIES:
755 process_request_identities(e, 2);
756 break;
757 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000758 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000759 process_add_identity(e, 2);
760 break;
761 case SSH2_AGENTC_REMOVE_IDENTITY:
762 process_remove_identity(e, 2);
763 break;
764 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
765 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100766 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000767#ifdef SMARTCARD
768 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000769 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000770 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100771 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000772 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
773 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100774 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000775#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100776 default:
777 /* Unknown message. Respond with failure. */
778 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000779 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100780 buffer_put_int(&e->output, 1);
781 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
782 break;
783 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784}
785
Ben Lindstrombba81212001-06-25 05:01:22 +0000786static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000787new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000789 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000790
Damien Miller232711f2004-06-15 10:35:30 +1000791 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792
Damien Miller95def091999-11-25 00:26:21 +1100793 if (fd > max_fd)
794 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795
Damien Miller95def091999-11-25 00:26:21 +1100796 for (i = 0; i < sockets_alloc; i++)
797 if (sockets[i].type == AUTH_UNUSED) {
798 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100799 buffer_init(&sockets[i].input);
800 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000801 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000802 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100803 return;
804 }
805 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000806 new_alloc = sockets_alloc + 10;
Damien Miller95def091999-11-25 00:26:21 +1100807 if (sockets)
Darren Tuckerfb16b242003-09-22 21:04:23 +1000808 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
Damien Miller95def091999-11-25 00:26:21 +1100809 else
Darren Tuckerfb16b242003-09-22 21:04:23 +1000810 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
811 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100812 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100814 sockets[old_alloc].fd = fd;
815 buffer_init(&sockets[old_alloc].input);
816 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000817 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000818 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819}
820
Ben Lindstrombba81212001-06-25 05:01:22 +0000821static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000822prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000824 u_int i, sz;
825 int n = 0;
826
827 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100828 switch (sockets[i].type) {
829 case AUTH_SOCKET:
830 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000831 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100832 break;
833 case AUTH_UNUSED:
834 break;
835 default:
836 fatal("Unknown socket type %d", sockets[i].type);
837 break;
838 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000839 }
840
841 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000842 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000844 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000846 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 *fdrp = xmalloc(sz);
848 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000849 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000851 if (n < *fdl)
852 debug("XXX shrink: %d < %d", n, *fdl);
853 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000854 memset(*fdrp, 0, sz);
855 memset(*fdwp, 0, sz);
856
857 for (i = 0; i < sockets_alloc; i++) {
858 switch (sockets[i].type) {
859 case AUTH_SOCKET:
860 case AUTH_CONNECTION:
861 FD_SET(sockets[i].fd, *fdrp);
862 if (buffer_len(&sockets[i].output) > 0)
863 FD_SET(sockets[i].fd, *fdwp);
864 break;
865 default:
866 break;
867 }
868 }
869 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870}
871
Ben Lindstrombba81212001-06-25 05:01:22 +0000872static void
Damien Miller95def091999-11-25 00:26:21 +1100873after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000875 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100876 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100877 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000878 int len, sock;
879 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000880 uid_t euid;
881 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Damien Miller95def091999-11-25 00:26:21 +1100883 for (i = 0; i < sockets_alloc; i++)
884 switch (sockets[i].type) {
885 case AUTH_UNUSED:
886 break;
887 case AUTH_SOCKET:
888 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100889 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000890 sock = accept(sockets[i].fd,
891 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100892 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100893 error("accept from AUTH_SOCKET: %s",
894 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100895 break;
896 }
Damien Miller538f1812002-09-12 09:51:10 +1000897 if (getpeereid(sock, &euid, &egid) < 0) {
898 error("getpeereid %d failed: %s",
899 sock, strerror(errno));
900 close(sock);
901 break;
902 }
Damien Milleraf9de382002-10-03 11:54:35 +1000903 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000904 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000905 "peer euid %u != uid %u",
906 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000907 close(sock);
908 break;
909 }
Damien Miller95def091999-11-25 00:26:21 +1100910 new_socket(AUTH_CONNECTION, sock);
911 }
912 break;
913 case AUTH_CONNECTION:
914 if (buffer_len(&sockets[i].output) > 0 &&
915 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000916 do {
917 len = write(sockets[i].fd,
918 buffer_ptr(&sockets[i].output),
919 buffer_len(&sockets[i].output));
920 if (len == -1 && (errno == EAGAIN ||
921 errno == EINTR))
922 continue;
923 break;
924 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100925 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000926 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100927 break;
928 }
929 buffer_consume(&sockets[i].output, len);
930 }
931 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000932 do {
933 len = read(sockets[i].fd, buf, sizeof(buf));
934 if (len == -1 && (errno == EAGAIN ||
935 errno == EINTR))
936 continue;
937 break;
938 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100939 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000940 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100941 break;
942 }
943 buffer_append(&sockets[i].input, buf, len);
944 process_message(&sockets[i]);
945 }
946 break;
947 default:
948 fatal("Unknown type %d", sockets[i].type);
949 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950}
951
Ben Lindstrombba81212001-06-25 05:01:22 +0000952static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000953cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000954{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000955 if (socket_name[0])
956 unlink(socket_name);
957 if (socket_dir[0])
958 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959}
960
Darren Tucker3e33cec2003-10-02 16:12:36 +1000961void
Damien Miller792c5111999-10-29 09:47:09 +1000962cleanup_exit(int i)
963{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000964 cleanup_socket();
965 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000966}
967
Ben Lindstrombba81212001-06-25 05:01:22 +0000968static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000969cleanup_handler(int sig)
970{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000971 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000972 _exit(2);
973}
974
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000976check_parent_exists(int sig)
977{
978 int save_errno = errno;
979
980 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
981 /* printf("Parent has died - Authentication agent exiting.\n"); */
982 cleanup_handler(sig); /* safe */
983 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000984 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000985 alarm(10);
986 errno = save_errno;
987}
988
989static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000990usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000991{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000992 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000993 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000994 fprintf(stderr, "Options:\n");
995 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
996 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
997 fprintf(stderr, " -k Kill the current agent.\n");
998 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000999 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001000 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001001 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001002}
1003
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004int
1005main(int ac, char **av)
1006{
Damien Miller6c711792003-01-24 11:36:23 +11001007 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001008 int sock, fd, ch;
1009 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001010 char *shell, *format, *pidstr, *agentsocket = NULL;
1011 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001012 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001014 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001015#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001016 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001017 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001018 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001019 pid_t pid;
1020 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001021
Darren Tuckerce321d82005-10-03 18:11:24 +10001022 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1023 sanitise_stdfd();
1024
Damien Miller6cffb9a2002-09-04 16:20:26 +10001025 /* drop */
1026 setegid(getgid());
1027 setgid(getgid());
1028
Damien Miller6c4914a2004-03-03 11:08:59 +11001029#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1030 /* Disable ptrace on Linux without sgid bit */
1031 prctl(PR_SET_DUMPABLE, 0);
1032#endif
1033
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001034 SSLeay_add_all_algorithms();
1035
Damien Miller59d3d5b2003-08-22 09:34:41 +10001036 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001037 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001038 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001039
Damien Miller53d81482003-01-22 11:47:19 +11001040 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001041 switch (ch) {
1042 case 'c':
1043 if (s_flag)
1044 usage();
1045 c_flag++;
1046 break;
1047 case 'k':
1048 k_flag++;
1049 break;
1050 case 's':
1051 if (c_flag)
1052 usage();
1053 s_flag++;
1054 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001055 case 'd':
1056 if (d_flag)
1057 usage();
1058 d_flag++;
1059 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001060 case 'a':
1061 agentsocket = optarg;
1062 break;
Damien Miller53d81482003-01-22 11:47:19 +11001063 case 't':
1064 if ((lifetime = convtime(optarg)) == -1) {
1065 fprintf(stderr, "Invalid lifetime\n");
1066 usage();
1067 }
1068 break;
Damien Miller95def091999-11-25 00:26:21 +11001069 default:
1070 usage();
1071 }
Damien Miller792c5111999-10-29 09:47:09 +10001072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 ac -= optind;
1074 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001075
Ben Lindstromd94580c2001-07-04 03:48:02 +00001076 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001077 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001078
Ben Lindstromeecdf232002-04-02 21:03:51 +00001079 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001080 shell = getenv("SHELL");
1081 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1082 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001083 }
Damien Miller95def091999-11-25 00:26:21 +11001084 if (k_flag) {
1085 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1086 if (pidstr == NULL) {
1087 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001088 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001089 exit(1);
1090 }
1091 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001092 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001093 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001094 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001095 exit(1);
1096 }
1097 if (kill(pid, SIGTERM) == -1) {
1098 perror("kill");
1099 exit(1);
1100 }
1101 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1102 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1103 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001104 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001105 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001106 }
Damien Miller95def091999-11-25 00:26:21 +11001107 parent_pid = getpid();
1108
Ben Lindstromb7788f32002-06-06 21:46:08 +00001109 if (agentsocket == NULL) {
1110 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001111 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001112 if (mkdtemp(socket_dir) == NULL) {
1113 perror("mkdtemp: private socket dir");
1114 exit(1);
1115 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001116 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1117 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001118 } else {
1119 /* Try to use specified agent socket */
1120 socket_dir[0] = '\0';
1121 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001122 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123
Damien Miller5428f641999-11-25 11:54:57 +11001124 /*
1125 * Create socket early so it will exist before command gets run from
1126 * the parent.
1127 */
Damien Miller95def091999-11-25 00:26:21 +11001128 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1129 if (sock < 0) {
1130 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001131 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001132 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001133 }
Damien Miller95def091999-11-25 00:26:21 +11001134 memset(&sunaddr, 0, sizeof(sunaddr));
1135 sunaddr.sun_family = AF_UNIX;
1136 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001137 prev_mask = umask(0177);
Damien Miller95def091999-11-25 00:26:21 +11001138 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1139 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001140 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001141 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001142 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001144 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001145 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001146 perror("listen");
1147 cleanup_exit(1);
1148 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001149
Damien Miller5428f641999-11-25 11:54:57 +11001150 /*
1151 * Fork, and have the parent execute the command, if any, or present
1152 * the socket data. The child continues as the authentication agent.
1153 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001154 if (d_flag) {
1155 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1156 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1157 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1158 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001159 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001160 goto skip;
1161 }
Damien Miller95def091999-11-25 00:26:21 +11001162 pid = fork();
1163 if (pid == -1) {
1164 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001165 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001166 }
1167 if (pid != 0) { /* Parent - execute the given command. */
1168 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001169 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001170 if (ac == 0) {
1171 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1172 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001173 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001174 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001176 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001177 exit(0);
1178 }
Damien Millere4340be2000-09-16 13:29:08 +11001179 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1180 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1181 perror("setenv");
1182 exit(1);
1183 }
Damien Miller95def091999-11-25 00:26:21 +11001184 execvp(av[0], av);
1185 perror(av[0]);
1186 exit(1);
1187 }
Damien Millerc653b892002-02-08 22:05:41 +11001188 /* child */
1189 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001190
1191 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001192 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001193 cleanup_exit(1);
1194 }
1195
1196 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001197 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1198 /* XXX might close listen socket */
1199 (void)dup2(fd, STDIN_FILENO);
1200 (void)dup2(fd, STDOUT_FILENO);
1201 (void)dup2(fd, STDERR_FILENO);
1202 if (fd > 2)
1203 close(fd);
1204 }
Damien Miller95def091999-11-25 00:26:21 +11001205
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001206#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001207 /* deny core dumps, since memory contains unencrypted private keys */
1208 rlim.rlim_cur = rlim.rlim_max = 0;
1209 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001210 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001211 cleanup_exit(1);
1212 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001213#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001214
1215skip:
Damien Miller95def091999-11-25 00:26:21 +11001216 new_socket(AUTH_SOCKET, sock);
1217 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001218 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001219 alarm(10);
1220 }
Damien Millerad833b32000-08-23 10:46:23 +10001221 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001222 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001223 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001224 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001225 signal(SIGHUP, cleanup_handler);
1226 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001227 nalloc = 0;
1228
Damien Miller95def091999-11-25 00:26:21 +11001229 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001230 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001231 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001232 if (errno == EINTR)
1233 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001234 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001235 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001236 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001237 }
1238 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239}