blob: 4421fa424b420d398e50b6e05810ab105feb348a [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: ssh-agent.c,v 1.143 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
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 Millere3b60b52006-07-10 21:08:03 +100041#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110042#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#endif
Damien Miller9b481512002-09-12 10:43:29 +100045#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110046#include <sys/resource.h>
Damien Millere3b60b52006-07-10 21:08:03 +100047
Darren Tucker39972492006-07-12 22:22:46 +100048#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100049#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110050#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110051# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110052#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110053#include <signal.h>
54
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include <openssl/evp.h>
56#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58#include "ssh.h"
59#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#include "buffer.h"
61#include "bufaux.h"
62#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100063#include "key.h"
64#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110065#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110067#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Ben Lindstrom3f471632001-07-04 03:53:15 +000069#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000070#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000071#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000072
Damien Miller6c4914a2004-03-03 11:08:59 +110073#if defined(HAVE_SYS_PRCTL_H)
74#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
75#endif
76
Ben Lindstrom65366a82001-12-06 16:32:47 +000077typedef enum {
78 AUTH_UNUSED,
79 AUTH_SOCKET,
80 AUTH_CONNECTION
81} sock_type;
82
Damien Miller95def091999-11-25 00:26:21 +110083typedef struct {
84 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000085 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110086 Buffer input;
87 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000088 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089} SocketEntry;
90
Ben Lindstrom46c16222000-12-22 01:43:59 +000091u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092SocketEntry *sockets = NULL;
93
Damien Miller1a534ae2002-01-22 23:26:13 +110094typedef struct identity {
95 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100096 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110097 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000098 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110099 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100} Identity;
101
Damien Millerad833b32000-08-23 10:46:23 +1000102typedef struct {
103 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100104 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000105} Idtab;
106
107/* private key table, one per protocol version */
108Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110int max_fd = 0;
111
112/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000113pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
115/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100116char socket_name[MAXPATHLEN];
117char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Ben Lindstrom2f717042002-06-06 21:52:03 +0000119/* locking */
120int locked = 0;
121char *lock_passwd = NULL;
122
Damien Miller95def091999-11-25 00:26:21 +1100123extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100124
Damien Miller53d81482003-01-22 11:47:19 +1100125/* Default lifetime (0 == forever) */
126static int lifetime = 0;
127
Ben Lindstrombba81212001-06-25 05:01:22 +0000128static void
Damien Miller58f34862002-09-04 16:31:21 +1000129close_socket(SocketEntry *e)
130{
Damien Miller58f34862002-09-04 16:31:21 +1000131 close(e->fd);
132 e->fd = -1;
133 e->type = AUTH_UNUSED;
134 buffer_free(&e->input);
135 buffer_free(&e->output);
136 buffer_free(&e->request);
137}
138
139static void
Damien Millerad833b32000-08-23 10:46:23 +1000140idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Millerad833b32000-08-23 10:46:23 +1000142 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000143
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000144 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100145 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000146 idtable[i].nentries = 0;
147 }
148}
149
150/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000152idtab_lookup(int version)
153{
154 if (version < 1 || version > 2)
155 fatal("internal error, bad protocol version %d", version);
156 return &idtable[version];
157}
158
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000159static void
160free_identity(Identity *id)
161{
162 key_free(id->key);
163 xfree(id->comment);
164 xfree(id);
165}
166
Damien Millerad833b32000-08-23 10:46:23 +1000167/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100168static Identity *
169lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000170{
Damien Miller1a534ae2002-01-22 23:26:13 +1100171 Identity *id;
172
Damien Millerad833b32000-08-23 10:46:23 +1000173 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100174 TAILQ_FOREACH(id, &tab->idlist, next) {
175 if (key_equal(key, id->key))
176 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000177 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100178 return (NULL);
179}
180
Damien Miller6c711792003-01-24 11:36:23 +1100181/* Check confirmation of keysign request */
182static int
183confirm_key(Identity *id)
184{
Darren Tuckerce327b62004-11-05 20:38:03 +1100185 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100186 int ret = -1;
187
188 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100189 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
190 id->comment, p))
191 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100192 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100193
Damien Miller6c711792003-01-24 11:36:23 +1100194 return (ret);
195}
196
Damien Millerad833b32000-08-23 10:46:23 +1000197/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static void
Damien Millerad833b32000-08-23 10:46:23 +1000199process_request_identities(SocketEntry *e, int version)
200{
201 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100202 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000203 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller95def091999-11-25 00:26:21 +1100205 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000206 buffer_put_char(&msg, (version == 1) ?
207 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
208 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100209 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100210 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000211 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
212 buffer_put_bignum(&msg, id->key->rsa->e);
213 buffer_put_bignum(&msg, id->key->rsa->n);
214 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000215 u_char *blob;
216 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000218 buffer_put_string(&msg, blob, blen);
219 xfree(blob);
220 }
221 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100222 }
223 buffer_put_int(&e->output, buffer_len(&msg));
224 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
225 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226}
227
Damien Millerad833b32000-08-23 10:46:23 +1000228/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static void
Damien Millerad833b32000-08-23 10:46:23 +1000230process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000232 u_char buf[32], mdbuf[16], session_id[16];
233 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000234 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000235 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000236 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100237 Buffer msg;
238 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000239 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller95def091999-11-25 00:26:21 +1100241 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100242 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100243 if ((challenge = BN_new()) == NULL)
244 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000245
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000246 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000247 buffer_get_bignum(&e->request, key->rsa->e);
248 buffer_get_bignum(&e->request, key->rsa->n);
249 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Millerad833b32000-08-23 10:46:23 +1000251 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000252 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000253 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000254 buffer_get(&e->request, session_id, 16);
255 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000256 if (response_type != 1)
257 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller1a534ae2002-01-22 23:26:13 +1100259 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100260 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100261 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000262 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100263 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
264 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100265
Damien Millerad833b32000-08-23 10:46:23 +1000266 /* The response is MD5 of decrypted challenge plus session id. */
267 len = BN_num_bytes(challenge);
268 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000269 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000270 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100271 }
Damien Millerad833b32000-08-23 10:46:23 +1000272 memset(buf, 0, 32);
273 BN_bn2bin(challenge, buf + 32 - len);
274 MD5_Init(&md);
275 MD5_Update(&md, buf, 32);
276 MD5_Update(&md, session_id, 16);
277 MD5_Final(mdbuf, &md);
278
279 /* Send the response. */
280 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
281 for (i = 0; i < 16; i++)
282 buffer_put_char(&msg, mdbuf[i]);
283 goto send;
284 }
285
286failure:
287 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100288 buffer_put_char(&msg, SSH_AGENT_FAILURE);
289send:
290 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000291 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
292 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100293 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000294 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
296
Damien Millerad833b32000-08-23 10:46:23 +1000297/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000298static void
Damien Millerad833b32000-08-23 10:46:23 +1000299process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000301 u_char *blob, *data, *signature = NULL;
302 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000303 extern int datafellows;
304 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000305 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000306 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000307
308 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000309
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000310 blob = buffer_get_string(&e->request, &blen);
311 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100312
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000313 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100314 if (flags & SSH_AGENT_OLD_SIGNATURE)
315 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000316
Damien Miller0bc1bd82000-11-13 22:57:25 +1100317 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000318 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100319 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100320 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100321 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000322 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000323 }
Damien Millerad833b32000-08-23 10:46:23 +1000324 buffer_init(&msg);
325 if (ok == 0) {
326 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
327 buffer_put_string(&msg, signature, slen);
328 } else {
329 buffer_put_char(&msg, SSH_AGENT_FAILURE);
330 }
331 buffer_put_int(&e->output, buffer_len(&msg));
332 buffer_append(&e->output, buffer_ptr(&msg),
333 buffer_len(&msg));
334 buffer_free(&msg);
335 xfree(data);
336 xfree(blob);
337 if (signature != NULL)
338 xfree(signature);
339}
340
341/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000342static void
Damien Millerad833b32000-08-23 10:46:23 +1000343process_remove_identity(SocketEntry *e, int version)
344{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000345 u_int blen, bits;
346 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100347 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000348 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100349
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000350 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000351 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000353 bits = buffer_get_int(&e->request);
354 buffer_get_bignum(&e->request, key->rsa->e);
355 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Millerad833b32000-08-23 10:46:23 +1000357 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000358 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000359 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000360 break;
361 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000362 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100363 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000364 xfree(blob);
365 break;
366 }
367 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100368 Identity *id = lookup_identity(key, version);
369 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100370 /*
371 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100372 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000373 * the array, we actually free the key there and move
374 * all the entries between the empty slot and the end
375 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100376 */
Damien Millerad833b32000-08-23 10:46:23 +1000377 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 if (tab->nentries < 1)
379 fatal("process_remove_identity: "
380 "internal error: tab->nentries %d",
381 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100382 TAILQ_REMOVE(&tab->idlist, id, next);
383 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000384 tab->nentries--;
385 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100386 }
Damien Millerad833b32000-08-23 10:46:23 +1000387 key_free(key);
388 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000390 buffer_put_char(&e->output,
391 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392}
393
Ben Lindstrombba81212001-06-25 05:01:22 +0000394static void
Damien Millerad833b32000-08-23 10:46:23 +1000395process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396{
Damien Millerad833b32000-08-23 10:46:23 +1000397 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100398 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Damien Miller95def091999-11-25 00:26:21 +1100400 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100401 for (id = TAILQ_FIRST(&tab->idlist); id;
402 id = TAILQ_FIRST(&tab->idlist)) {
403 TAILQ_REMOVE(&tab->idlist, id, next);
404 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100405 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000408 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
410 /* Send success. */
411 buffer_put_int(&e->output, 1);
412 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100413}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Ben Lindstrombba81212001-06-25 05:01:22 +0000415static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000416reaper(void)
417{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000418 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000419 Identity *id, *nxt;
420 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000421 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000422
423 for (version = 1; version < 3; version++) {
424 tab = idtab_lookup(version);
425 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
426 nxt = TAILQ_NEXT(id, next);
427 if (id->death != 0 && now >= id->death) {
428 TAILQ_REMOVE(&tab->idlist, id, next);
429 free_identity(id);
430 tab->nentries--;
431 }
432 }
433 }
434}
435
436static void
Damien Millerad833b32000-08-23 10:46:23 +1000437process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100438{
Damien Millerad833b32000-08-23 10:46:23 +1000439 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100440 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000441 char *type_name, *comment;
442 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100443
Damien Millerad833b32000-08-23 10:46:23 +1000444 switch (version) {
445 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100446 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000447 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000448 buffer_get_bignum(&e->request, k->rsa->n);
449 buffer_get_bignum(&e->request, k->rsa->e);
450 buffer_get_bignum(&e->request, k->rsa->d);
451 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100452
Damien Millerad833b32000-08-23 10:46:23 +1000453 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000454 buffer_get_bignum(&e->request, k->rsa->q); /* p */
455 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100456
Damien Millerad833b32000-08-23 10:46:23 +1000457 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000458 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000459 break;
460 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000461 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000462 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100463 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000464 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 case KEY_DSA:
466 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000467 buffer_get_bignum2(&e->request, k->dsa->p);
468 buffer_get_bignum2(&e->request, k->dsa->q);
469 buffer_get_bignum2(&e->request, k->dsa->g);
470 buffer_get_bignum2(&e->request, k->dsa->pub_key);
471 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 break;
473 case KEY_RSA:
474 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000475 buffer_get_bignum2(&e->request, k->rsa->n);
476 buffer_get_bignum2(&e->request, k->rsa->e);
477 buffer_get_bignum2(&e->request, k->rsa->d);
478 buffer_get_bignum2(&e->request, k->rsa->iqmp);
479 buffer_get_bignum2(&e->request, k->rsa->p);
480 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481
482 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000483 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100484 break;
485 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000486 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000487 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100488 }
Damien Millerad833b32000-08-23 10:46:23 +1000489 break;
490 }
Damien Millerc51d0732003-03-15 11:37:09 +1100491 /* enable blinding */
492 switch (k->type) {
493 case KEY_RSA:
494 case KEY_RSA1:
495 if (RSA_blinding_on(k->rsa, NULL) != 1) {
496 error("process_add_identity: RSA_blinding_on failed");
497 key_free(k);
498 goto send;
499 }
500 break;
501 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000502 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000503 if (k == NULL) {
504 xfree(comment);
505 goto send;
506 }
507 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000508 while (buffer_len(&e->request)) {
509 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000510 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000511 death = time(NULL) + buffer_get_int(&e->request);
512 break;
Damien Miller6c711792003-01-24 11:36:23 +1100513 case SSH_AGENT_CONSTRAIN_CONFIRM:
514 confirm = 1;
515 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000516 default:
517 break;
518 }
519 }
Damien Miller53d81482003-01-22 11:47:19 +1100520 if (lifetime && !death)
521 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000522 if (lookup_identity(k, version) == NULL) {
523 Identity *id = xmalloc(sizeof(Identity));
524 id->key = k;
525 id->comment = comment;
526 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100527 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000528 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
529 /* Increment the number of identities. */
530 tab->nentries++;
531 } else {
532 key_free(k);
533 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000534 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000535send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000536 buffer_put_int(&e->output, 1);
537 buffer_put_char(&e->output,
538 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
539}
540
Ben Lindstrom2f717042002-06-06 21:52:03 +0000541/* XXX todo: encrypt sensitive data with passphrase */
542static void
543process_lock_agent(SocketEntry *e, int lock)
544{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000545 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000546 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000547
548 passwd = buffer_get_string(&e->request, NULL);
549 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
550 locked = 0;
551 memset(lock_passwd, 0, strlen(lock_passwd));
552 xfree(lock_passwd);
553 lock_passwd = NULL;
554 success = 1;
555 } else if (!locked && lock) {
556 locked = 1;
557 lock_passwd = xstrdup(passwd);
558 success = 1;
559 }
560 memset(passwd, 0, strlen(passwd));
561 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000562
Ben Lindstrom2f717042002-06-06 21:52:03 +0000563 buffer_put_int(&e->output, 1);
564 buffer_put_char(&e->output,
565 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000566}
567
568static void
569no_identities(SocketEntry *e, u_int type)
570{
571 Buffer msg;
572
573 buffer_init(&msg);
574 buffer_put_char(&msg,
575 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
576 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
577 buffer_put_int(&msg, 0);
578 buffer_put_int(&e->output, buffer_len(&msg));
579 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
580 buffer_free(&msg);
581}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000582
583#ifdef SMARTCARD
584static void
585process_add_smartcard_key (SocketEntry *e)
586{
Ben Lindstromba72d302002-03-22 03:51:06 +0000587 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000588 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000589 Key **keys, *k;
590 Identity *id;
591 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100592
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000593 sc_reader_id = buffer_get_string(&e->request, NULL);
594 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000595
596 while (buffer_len(&e->request)) {
597 switch (buffer_get_char(&e->request)) {
598 case SSH_AGENT_CONSTRAIN_LIFETIME:
599 death = time(NULL) + buffer_get_int(&e->request);
600 break;
601 case SSH_AGENT_CONSTRAIN_CONFIRM:
602 confirm = 1;
603 break;
604 default:
605 break;
606 }
607 }
608 if (lifetime && !death)
609 death = time(NULL) + lifetime;
610
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000611 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000612 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000613 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000614
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000615 if (keys == NULL || keys[0] == NULL) {
616 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000617 goto send;
618 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000619 for (i = 0; keys[i] != NULL; i++) {
620 k = keys[i];
621 version = k->type == KEY_RSA1 ? 1 : 2;
622 tab = idtab_lookup(version);
623 if (lookup_identity(k, version) == NULL) {
624 id = xmalloc(sizeof(Identity));
625 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000626 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000627 id->death = death;
628 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000629 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
630 tab->nentries++;
631 success = 1;
632 } else {
633 key_free(k);
634 }
635 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000636 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000637 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000638send:
639 buffer_put_int(&e->output, 1);
640 buffer_put_char(&e->output,
641 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
642}
643
644static void
645process_remove_smartcard_key(SocketEntry *e)
646{
Ben Lindstromba72d302002-03-22 03:51:06 +0000647 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000648 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000649 Key **keys, *k = NULL;
650 Identity *id;
651 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000652
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000653 sc_reader_id = buffer_get_string(&e->request, NULL);
654 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000655 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000656 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000657 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000659 if (keys == NULL || keys[0] == NULL) {
660 error("sc_get_keys failed");
661 goto send;
662 }
663 for (i = 0; keys[i] != NULL; i++) {
664 k = keys[i];
665 version = k->type == KEY_RSA1 ? 1 : 2;
666 if ((id = lookup_identity(k, version)) != NULL) {
667 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000668 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100670 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000671 success = 1;
672 }
673 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000674 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000675 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000676 xfree(keys);
677send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000678 buffer_put_int(&e->output, 1);
679 buffer_put_char(&e->output,
680 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
681}
Ben Lindstromffce1472001-08-06 21:57:31 +0000682#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000683
Damien Millerad833b32000-08-23 10:46:23 +1000684/* dispatch incoming messages */
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687process_message(SocketEntry *e)
688{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000689 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000690 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000691
692 /* kill dead keys */
693 reaper();
694
Damien Miller95def091999-11-25 00:26:21 +1100695 if (buffer_len(&e->input) < 5)
696 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100697 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100698 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100699 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000700 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100701 return;
702 }
703 if (buffer_len(&e->input) < msg_len + 4)
704 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000705
706 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100707 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000708 buffer_clear(&e->request);
709 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
710 buffer_consume(&e->input, msg_len);
711 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Ben Lindstrom2f717042002-06-06 21:52:03 +0000713 /* check wheter agent is locked */
714 if (locked && type != SSH_AGENTC_UNLOCK) {
715 buffer_clear(&e->request);
716 switch (type) {
717 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
718 case SSH2_AGENTC_REQUEST_IDENTITIES:
719 /* send empty lists */
720 no_identities(e, type);
721 break;
722 default:
723 /* send a fail message for all other request types */
724 buffer_put_int(&e->output, 1);
725 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
726 }
727 return;
728 }
729
Ben Lindstrom3f471632001-07-04 03:53:15 +0000730 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100731 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000732 case SSH_AGENTC_LOCK:
733 case SSH_AGENTC_UNLOCK:
734 process_lock_agent(e, type == SSH_AGENTC_LOCK);
735 break;
Damien Millerad833b32000-08-23 10:46:23 +1000736 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100737 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000738 process_authentication_challenge1(e);
739 break;
740 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
741 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100742 break;
743 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000744 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000745 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100746 break;
747 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000748 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100749 break;
750 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000751 process_remove_all_identities(e, 1);
752 break;
753 /* ssh2 */
754 case SSH2_AGENTC_SIGN_REQUEST:
755 process_sign_request2(e);
756 break;
757 case SSH2_AGENTC_REQUEST_IDENTITIES:
758 process_request_identities(e, 2);
759 break;
760 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000761 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000762 process_add_identity(e, 2);
763 break;
764 case SSH2_AGENTC_REMOVE_IDENTITY:
765 process_remove_identity(e, 2);
766 break;
767 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
768 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100769 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000770#ifdef SMARTCARD
771 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000772 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000773 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100774 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000775 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
776 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100777 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000778#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100779 default:
780 /* Unknown message. Respond with failure. */
781 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000782 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100783 buffer_put_int(&e->output, 1);
784 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
785 break;
786 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787}
788
Ben Lindstrombba81212001-06-25 05:01:22 +0000789static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000790new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000792 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000793
Damien Miller232711f2004-06-15 10:35:30 +1000794 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795
Damien Miller95def091999-11-25 00:26:21 +1100796 if (fd > max_fd)
797 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller95def091999-11-25 00:26:21 +1100799 for (i = 0; i < sockets_alloc; i++)
800 if (sockets[i].type == AUTH_UNUSED) {
801 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100802 buffer_init(&sockets[i].input);
803 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000804 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000805 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100806 return;
807 }
808 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000809 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100810 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 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,
Damien Miller90967402006-03-26 14:07:26 +1100891 (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
Damien Miller1c13bd82006-03-26 14:28:14 +1100968/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000969static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000970cleanup_handler(int sig)
971{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000972 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000973 _exit(2);
974}
975
Damien Miller1c13bd82006-03-26 14:28:14 +1100976/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000977static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000978check_parent_exists(int sig)
979{
980 int save_errno = errno;
981
982 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
983 /* printf("Parent has died - Authentication agent exiting.\n"); */
984 cleanup_handler(sig); /* safe */
985 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000986 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000987 alarm(10);
988 errno = save_errno;
989}
990
991static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000992usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000993{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000994 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000995 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000996 fprintf(stderr, "Options:\n");
997 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
998 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
999 fprintf(stderr, " -k Kill the current agent.\n");
1000 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001001 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001002 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001003 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001004}
1005
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006int
1007main(int ac, char **av)
1008{
Damien Miller6c711792003-01-24 11:36:23 +11001009 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001010 int sock, fd, ch;
1011 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001012 char *shell, *format, *pidstr, *agentsocket = NULL;
1013 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001014 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001015#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001016 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001017#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001018 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001019 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001020 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001021 pid_t pid;
1022 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001023
Darren Tuckerce321d82005-10-03 18:11:24 +10001024 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1025 sanitise_stdfd();
1026
Damien Miller6cffb9a2002-09-04 16:20:26 +10001027 /* drop */
1028 setegid(getgid());
1029 setgid(getgid());
1030
Damien Miller6c4914a2004-03-03 11:08:59 +11001031#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1032 /* Disable ptrace on Linux without sgid bit */
1033 prctl(PR_SET_DUMPABLE, 0);
1034#endif
1035
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001036 SSLeay_add_all_algorithms();
1037
Damien Miller59d3d5b2003-08-22 09:34:41 +10001038 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001039 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001040 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001041
Damien Miller53d81482003-01-22 11:47:19 +11001042 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001043 switch (ch) {
1044 case 'c':
1045 if (s_flag)
1046 usage();
1047 c_flag++;
1048 break;
1049 case 'k':
1050 k_flag++;
1051 break;
1052 case 's':
1053 if (c_flag)
1054 usage();
1055 s_flag++;
1056 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001057 case 'd':
1058 if (d_flag)
1059 usage();
1060 d_flag++;
1061 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001062 case 'a':
1063 agentsocket = optarg;
1064 break;
Damien Miller53d81482003-01-22 11:47:19 +11001065 case 't':
1066 if ((lifetime = convtime(optarg)) == -1) {
1067 fprintf(stderr, "Invalid lifetime\n");
1068 usage();
1069 }
1070 break;
Damien Miller95def091999-11-25 00:26:21 +11001071 default:
1072 usage();
1073 }
Damien Miller792c5111999-10-29 09:47:09 +10001074 }
Damien Miller95def091999-11-25 00:26:21 +11001075 ac -= optind;
1076 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001077
Ben Lindstromd94580c2001-07-04 03:48:02 +00001078 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001079 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001080
Ben Lindstromeecdf232002-04-02 21:03:51 +00001081 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001082 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001083 if (shell != NULL &&
1084 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001085 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001086 }
Damien Miller95def091999-11-25 00:26:21 +11001087 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001088 const char *errstr = NULL;
1089
Damien Miller95def091999-11-25 00:26:21 +11001090 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1091 if (pidstr == NULL) {
1092 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001093 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001094 exit(1);
1095 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001096 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1097 if (errstr) {
1098 fprintf(stderr,
1099 "%s=\"%s\", which is not a good PID: %s\n",
1100 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001101 exit(1);
1102 }
1103 if (kill(pid, SIGTERM) == -1) {
1104 perror("kill");
1105 exit(1);
1106 }
1107 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1108 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1109 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001110 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001111 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001112 }
Damien Miller95def091999-11-25 00:26:21 +11001113 parent_pid = getpid();
1114
Ben Lindstromb7788f32002-06-06 21:46:08 +00001115 if (agentsocket == NULL) {
1116 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001117 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001118 if (mkdtemp(socket_dir) == NULL) {
1119 perror("mkdtemp: private socket dir");
1120 exit(1);
1121 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001122 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1123 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001124 } else {
1125 /* Try to use specified agent socket */
1126 socket_dir[0] = '\0';
1127 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001128 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
Damien Miller5428f641999-11-25 11:54:57 +11001130 /*
1131 * Create socket early so it will exist before command gets run from
1132 * the parent.
1133 */
Damien Miller95def091999-11-25 00:26:21 +11001134 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1135 if (sock < 0) {
1136 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001137 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001138 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001139 }
Damien Miller95def091999-11-25 00:26:21 +11001140 memset(&sunaddr, 0, sizeof(sunaddr));
1141 sunaddr.sun_family = AF_UNIX;
1142 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001143 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001144 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001145 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001146 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001147 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001148 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001150 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001151 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001152 perror("listen");
1153 cleanup_exit(1);
1154 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001155
Damien Miller5428f641999-11-25 11:54:57 +11001156 /*
1157 * Fork, and have the parent execute the command, if any, or present
1158 * the socket data. The child continues as the authentication agent.
1159 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001160 if (d_flag) {
1161 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1162 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1163 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1164 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001165 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001166 goto skip;
1167 }
Damien Miller95def091999-11-25 00:26:21 +11001168 pid = fork();
1169 if (pid == -1) {
1170 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001171 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001172 }
1173 if (pid != 0) { /* Parent - execute the given command. */
1174 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001175 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001176 if (ac == 0) {
1177 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1178 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001179 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001180 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001181 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001182 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001183 exit(0);
1184 }
Damien Millere4340be2000-09-16 13:29:08 +11001185 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1186 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1187 perror("setenv");
1188 exit(1);
1189 }
Damien Miller95def091999-11-25 00:26:21 +11001190 execvp(av[0], av);
1191 perror(av[0]);
1192 exit(1);
1193 }
Damien Millerc653b892002-02-08 22:05:41 +11001194 /* child */
1195 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001196
1197 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001198 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001199 cleanup_exit(1);
1200 }
1201
1202 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001203 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1204 /* XXX might close listen socket */
1205 (void)dup2(fd, STDIN_FILENO);
1206 (void)dup2(fd, STDOUT_FILENO);
1207 (void)dup2(fd, STDERR_FILENO);
1208 if (fd > 2)
1209 close(fd);
1210 }
Damien Miller95def091999-11-25 00:26:21 +11001211
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001212#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001213 /* deny core dumps, since memory contains unencrypted private keys */
1214 rlim.rlim_cur = rlim.rlim_max = 0;
1215 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001216 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001217 cleanup_exit(1);
1218 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001219#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001220
1221skip:
Damien Miller95def091999-11-25 00:26:21 +11001222 new_socket(AUTH_SOCKET, sock);
1223 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001224 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001225 alarm(10);
1226 }
Damien Millerad833b32000-08-23 10:46:23 +10001227 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001228 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001229 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001230 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001231 signal(SIGHUP, cleanup_handler);
1232 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001233 nalloc = 0;
1234
Damien Miller95def091999-11-25 00:26:21 +11001235 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001236 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001237 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001238 if (errno == EINTR)
1239 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001240 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001241 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001242 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001243 }
1244 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245}