blob: 554f8942a67d3392089bbcca698e1244ae09ecbb [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36#include "includes.h"
Damien Miller9b481512002-09-12 10:43:29 +100037#include "openbsd-compat/sys-queue.h"
Damien Miller53d81482003-01-22 11:47:19 +110038RCSID("$OpenBSD: ssh-agent.c,v 1.106 2003/01/21 18:14:36 marc Exp $");
Damien Millerec52d7c2002-01-22 23:52:17 +110039
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include <openssl/evp.h>
41#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "ssh.h"
44#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "buffer.h"
46#include "bufaux.h"
47#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100049#include "key.h"
50#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Ben Lindstrom3f471632001-07-04 03:53:15 +000054#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000055#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000056#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000057
Ben Lindstrom65366a82001-12-06 16:32:47 +000058typedef enum {
59 AUTH_UNUSED,
60 AUTH_SOCKET,
61 AUTH_CONNECTION
62} sock_type;
63
Damien Miller95def091999-11-25 00:26:21 +110064typedef struct {
65 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000066 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110067 Buffer input;
68 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000069 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070} SocketEntry;
71
Ben Lindstrom46c16222000-12-22 01:43:59 +000072u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073SocketEntry *sockets = NULL;
74
Damien Miller1a534ae2002-01-22 23:26:13 +110075typedef struct identity {
76 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100077 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110078 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000079 u_int death;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080} Identity;
81
Damien Millerad833b32000-08-23 10:46:23 +100082typedef struct {
83 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110084 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100085} Idtab;
86
87/* private key table, one per protocol version */
88Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90int max_fd = 0;
91
92/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100093pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
95/* pathname and directory for AUTH_SOCKET */
96char socket_name[1024];
97char socket_dir[1024];
98
Ben Lindstrom2f717042002-06-06 21:52:03 +000099/* locking */
100int locked = 0;
101char *lock_passwd = NULL;
102
Damien Miller95def091999-11-25 00:26:21 +1100103#ifdef HAVE___PROGNAME
104extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000105#else
106char *__progname;
107#endif
Damien Miller95def091999-11-25 00:26:21 +1100108
Damien Miller53d81482003-01-22 11:47:19 +1100109/* Default lifetime (0 == forever) */
110static int lifetime = 0;
111
Ben Lindstrombba81212001-06-25 05:01:22 +0000112static void
Damien Miller58f34862002-09-04 16:31:21 +1000113close_socket(SocketEntry *e)
114{
Damien Miller58f34862002-09-04 16:31:21 +1000115 close(e->fd);
116 e->fd = -1;
117 e->type = AUTH_UNUSED;
118 buffer_free(&e->input);
119 buffer_free(&e->output);
120 buffer_free(&e->request);
121}
122
123static void
Damien Millerad833b32000-08-23 10:46:23 +1000124idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125{
Damien Millerad833b32000-08-23 10:46:23 +1000126 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000127
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000128 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100129 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000130 idtable[i].nentries = 0;
131 }
132}
133
134/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000136idtab_lookup(int version)
137{
138 if (version < 1 || version > 2)
139 fatal("internal error, bad protocol version %d", version);
140 return &idtable[version];
141}
142
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000143static void
144free_identity(Identity *id)
145{
146 key_free(id->key);
147 xfree(id->comment);
148 xfree(id);
149}
150
Damien Millerad833b32000-08-23 10:46:23 +1000151/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100152static Identity *
153lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000154{
Damien Miller1a534ae2002-01-22 23:26:13 +1100155 Identity *id;
156
Damien Millerad833b32000-08-23 10:46:23 +1000157 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100158 TAILQ_FOREACH(id, &tab->idlist, next) {
159 if (key_equal(key, id->key))
160 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000161 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100162 return (NULL);
163}
164
Damien Millerad833b32000-08-23 10:46:23 +1000165/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Damien Millerad833b32000-08-23 10:46:23 +1000167process_request_identities(SocketEntry *e, int version)
168{
169 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100170 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000171 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000174 buffer_put_char(&msg, (version == 1) ?
175 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
176 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100177 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100178 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000179 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
180 buffer_put_bignum(&msg, id->key->rsa->e);
181 buffer_put_bignum(&msg, id->key->rsa->n);
182 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000183 u_char *blob;
184 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100185 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000186 buffer_put_string(&msg, blob, blen);
187 xfree(blob);
188 }
189 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100190 }
191 buffer_put_int(&e->output, buffer_len(&msg));
192 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
193 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194}
195
Damien Millerad833b32000-08-23 10:46:23 +1000196/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Damien Millerad833b32000-08-23 10:46:23 +1000198process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000200 u_char buf[32], mdbuf[16], session_id[16];
201 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000202 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000203 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000204 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100205 Buffer msg;
206 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000207 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Miller95def091999-11-25 00:26:21 +1100209 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100210 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100211 if ((challenge = BN_new()) == NULL)
212 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000213
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000214 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000215 buffer_get_bignum(&e->request, key->rsa->e);
216 buffer_get_bignum(&e->request, key->rsa->n);
217 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Millerad833b32000-08-23 10:46:23 +1000219 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000220 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000221 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000222 buffer_get(&e->request, session_id, 16);
223 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000224 if (response_type != 1)
225 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226
Damien Miller1a534ae2002-01-22 23:26:13 +1100227 id = lookup_identity(key, 1);
228 if (id != NULL) {
229 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000230 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100231 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
232 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100233
Damien Millerad833b32000-08-23 10:46:23 +1000234 /* The response is MD5 of decrypted challenge plus session id. */
235 len = BN_num_bytes(challenge);
236 if (len <= 0 || len > 32) {
237 log("process_authentication_challenge: bad challenge length %d", len);
238 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100239 }
Damien Millerad833b32000-08-23 10:46:23 +1000240 memset(buf, 0, 32);
241 BN_bn2bin(challenge, buf + 32 - len);
242 MD5_Init(&md);
243 MD5_Update(&md, buf, 32);
244 MD5_Update(&md, session_id, 16);
245 MD5_Final(mdbuf, &md);
246
247 /* Send the response. */
248 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
249 for (i = 0; i < 16; i++)
250 buffer_put_char(&msg, mdbuf[i]);
251 goto send;
252 }
253
254failure:
255 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100256 buffer_put_char(&msg, SSH_AGENT_FAILURE);
257send:
258 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000259 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
260 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100261 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000262 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263}
264
Damien Millerad833b32000-08-23 10:46:23 +1000265/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000266static void
Damien Millerad833b32000-08-23 10:46:23 +1000267process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000269 u_char *blob, *data, *signature = NULL;
270 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000271 extern int datafellows;
272 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000273 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000274 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000275
276 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000277
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000278 blob = buffer_get_string(&e->request, &blen);
279 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100280
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000281 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100282 if (flags & SSH_AGENT_OLD_SIGNATURE)
283 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000284
Damien Miller0bc1bd82000-11-13 22:57:25 +1100285 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000286 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100287 Identity *id = lookup_identity(key, 2);
288 if (id != NULL)
289 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000290 }
291 key_free(key);
292 buffer_init(&msg);
293 if (ok == 0) {
294 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
295 buffer_put_string(&msg, signature, slen);
296 } else {
297 buffer_put_char(&msg, SSH_AGENT_FAILURE);
298 }
299 buffer_put_int(&e->output, buffer_len(&msg));
300 buffer_append(&e->output, buffer_ptr(&msg),
301 buffer_len(&msg));
302 buffer_free(&msg);
303 xfree(data);
304 xfree(blob);
305 if (signature != NULL)
306 xfree(signature);
307}
308
309/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000310static void
Damien Millerad833b32000-08-23 10:46:23 +1000311process_remove_identity(SocketEntry *e, int version)
312{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000313 u_int blen, bits;
314 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100315 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000316 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100317
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000318 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000319 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100320 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000321 bits = buffer_get_int(&e->request);
322 buffer_get_bignum(&e->request, key->rsa->e);
323 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Millerad833b32000-08-23 10:46:23 +1000325 if (bits != key_size(key))
Ben Lindstrom822b6342002-06-23 21:38:49 +0000326 log("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000327 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000328 break;
329 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000330 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100331 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000332 xfree(blob);
333 break;
334 }
335 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 Identity *id = lookup_identity(key, version);
337 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100338 /*
339 * We have this key. Free the old key. Since we
340 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000341 * the array, we actually free the key there and move
342 * all the entries between the empty slot and the end
343 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100344 */
Damien Millerad833b32000-08-23 10:46:23 +1000345 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100346 if (tab->nentries < 1)
347 fatal("process_remove_identity: "
348 "internal error: tab->nentries %d",
349 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100350 TAILQ_REMOVE(&tab->idlist, id, next);
351 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000352 tab->nentries--;
353 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100354 }
Damien Millerad833b32000-08-23 10:46:23 +1000355 key_free(key);
356 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000358 buffer_put_char(&e->output,
359 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360}
361
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Damien Millerad833b32000-08-23 10:46:23 +1000363process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Millerad833b32000-08-23 10:46:23 +1000365 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100366 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Damien Miller95def091999-11-25 00:26:21 +1100368 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100369 for (id = TAILQ_FIRST(&tab->idlist); id;
370 id = TAILQ_FIRST(&tab->idlist)) {
371 TAILQ_REMOVE(&tab->idlist, id, next);
372 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100373 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Damien Miller95def091999-11-25 00:26:21 +1100375 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000376 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
378 /* Send success. */
379 buffer_put_int(&e->output, 1);
380 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100381}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000384reaper(void)
385{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000386 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000387 Identity *id, *nxt;
388 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000389 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000390
391 for (version = 1; version < 3; version++) {
392 tab = idtab_lookup(version);
393 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
394 nxt = TAILQ_NEXT(id, next);
395 if (id->death != 0 && now >= id->death) {
396 TAILQ_REMOVE(&tab->idlist, id, next);
397 free_identity(id);
398 tab->nentries--;
399 }
400 }
401 }
402}
403
404static void
Damien Millerad833b32000-08-23 10:46:23 +1000405process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100406{
Damien Millerad833b32000-08-23 10:46:23 +1000407 Idtab *tab = idtab_lookup(version);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000408 int type, success = 0, death = 0;
409 char *type_name, *comment;
410 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100411
Damien Millerad833b32000-08-23 10:46:23 +1000412 switch (version) {
413 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100414 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000415 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000416 buffer_get_bignum(&e->request, k->rsa->n);
417 buffer_get_bignum(&e->request, k->rsa->e);
418 buffer_get_bignum(&e->request, k->rsa->d);
419 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100420
Damien Millerad833b32000-08-23 10:46:23 +1000421 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000422 buffer_get_bignum(&e->request, k->rsa->q); /* p */
423 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100424
Damien Millerad833b32000-08-23 10:46:23 +1000425 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000426 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000427 break;
428 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000429 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000430 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100431 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000432 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100433 case KEY_DSA:
434 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000435 buffer_get_bignum2(&e->request, k->dsa->p);
436 buffer_get_bignum2(&e->request, k->dsa->q);
437 buffer_get_bignum2(&e->request, k->dsa->g);
438 buffer_get_bignum2(&e->request, k->dsa->pub_key);
439 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100440 break;
441 case KEY_RSA:
442 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000443 buffer_get_bignum2(&e->request, k->rsa->n);
444 buffer_get_bignum2(&e->request, k->rsa->e);
445 buffer_get_bignum2(&e->request, k->rsa->d);
446 buffer_get_bignum2(&e->request, k->rsa->iqmp);
447 buffer_get_bignum2(&e->request, k->rsa->p);
448 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449
450 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000451 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 break;
453 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000454 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000455 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100456 }
Damien Millerad833b32000-08-23 10:46:23 +1000457 break;
458 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000459 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000460 if (k == NULL) {
461 xfree(comment);
462 goto send;
463 }
464 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000465 while (buffer_len(&e->request)) {
466 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000467 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000468 death = time(NULL) + buffer_get_int(&e->request);
469 break;
470 default:
471 break;
472 }
473 }
Damien Miller53d81482003-01-22 11:47:19 +1100474 if (lifetime && !death)
475 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000476 if (lookup_identity(k, version) == NULL) {
477 Identity *id = xmalloc(sizeof(Identity));
478 id->key = k;
479 id->comment = comment;
480 id->death = death;
481 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
482 /* Increment the number of identities. */
483 tab->nentries++;
484 } else {
485 key_free(k);
486 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000487 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000488send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000489 buffer_put_int(&e->output, 1);
490 buffer_put_char(&e->output,
491 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
492}
493
Ben Lindstrom2f717042002-06-06 21:52:03 +0000494/* XXX todo: encrypt sensitive data with passphrase */
495static void
496process_lock_agent(SocketEntry *e, int lock)
497{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000498 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000499 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000500
501 passwd = buffer_get_string(&e->request, NULL);
502 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
503 locked = 0;
504 memset(lock_passwd, 0, strlen(lock_passwd));
505 xfree(lock_passwd);
506 lock_passwd = NULL;
507 success = 1;
508 } else if (!locked && lock) {
509 locked = 1;
510 lock_passwd = xstrdup(passwd);
511 success = 1;
512 }
513 memset(passwd, 0, strlen(passwd));
514 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000515
Ben Lindstrom2f717042002-06-06 21:52:03 +0000516 buffer_put_int(&e->output, 1);
517 buffer_put_char(&e->output,
518 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000519}
520
521static void
522no_identities(SocketEntry *e, u_int type)
523{
524 Buffer msg;
525
526 buffer_init(&msg);
527 buffer_put_char(&msg,
528 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
529 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
530 buffer_put_int(&msg, 0);
531 buffer_put_int(&e->output, buffer_len(&msg));
532 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
533 buffer_free(&msg);
534}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000535
536#ifdef SMARTCARD
537static void
538process_add_smartcard_key (SocketEntry *e)
539{
Ben Lindstromba72d302002-03-22 03:51:06 +0000540 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000541 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000542 Key **keys, *k;
543 Identity *id;
544 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100545
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000546 sc_reader_id = buffer_get_string(&e->request, NULL);
547 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000548 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000549 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000550 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000551
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000552 if (keys == NULL || keys[0] == NULL) {
553 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000554 goto send;
555 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000556 for (i = 0; keys[i] != NULL; i++) {
557 k = keys[i];
558 version = k->type == KEY_RSA1 ? 1 : 2;
559 tab = idtab_lookup(version);
560 if (lookup_identity(k, version) == NULL) {
561 id = xmalloc(sizeof(Identity));
562 id->key = k;
563 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000564 id->death = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000565 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
566 tab->nentries++;
567 success = 1;
568 } else {
569 key_free(k);
570 }
571 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000572 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000573 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000574send:
575 buffer_put_int(&e->output, 1);
576 buffer_put_char(&e->output,
577 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
578}
579
580static void
581process_remove_smartcard_key(SocketEntry *e)
582{
Ben Lindstromba72d302002-03-22 03:51:06 +0000583 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000584 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000585 Key **keys, *k = NULL;
586 Identity *id;
587 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000588
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000589 sc_reader_id = buffer_get_string(&e->request, NULL);
590 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000591 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000592 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000593 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000594
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000595 if (keys == NULL || keys[0] == NULL) {
596 error("sc_get_keys failed");
597 goto send;
598 }
599 for (i = 0; keys[i] != NULL; i++) {
600 k = keys[i];
601 version = k->type == KEY_RSA1 ? 1 : 2;
602 if ((id = lookup_identity(k, version)) != NULL) {
603 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000604 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000605 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100606 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607 success = 1;
608 }
609 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000610 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000611 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000612 xfree(keys);
613send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000614 buffer_put_int(&e->output, 1);
615 buffer_put_char(&e->output,
616 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
617}
Ben Lindstromffce1472001-08-06 21:57:31 +0000618#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000619
Damien Millerad833b32000-08-23 10:46:23 +1000620/* dispatch incoming messages */
621
Ben Lindstrombba81212001-06-25 05:01:22 +0000622static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623process_message(SocketEntry *e)
624{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000625 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000626 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000627
628 /* kill dead keys */
629 reaper();
630
Damien Miller95def091999-11-25 00:26:21 +1100631 if (buffer_len(&e->input) < 5)
632 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100633 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100634 msg_len = GET_32BIT(cp);
635 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000636 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100637 return;
638 }
639 if (buffer_len(&e->input) < msg_len + 4)
640 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000641
642 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100643 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000644 buffer_clear(&e->request);
645 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
646 buffer_consume(&e->input, msg_len);
647 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648
Ben Lindstrom2f717042002-06-06 21:52:03 +0000649 /* check wheter agent is locked */
650 if (locked && type != SSH_AGENTC_UNLOCK) {
651 buffer_clear(&e->request);
652 switch (type) {
653 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
654 case SSH2_AGENTC_REQUEST_IDENTITIES:
655 /* send empty lists */
656 no_identities(e, type);
657 break;
658 default:
659 /* send a fail message for all other request types */
660 buffer_put_int(&e->output, 1);
661 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
662 }
663 return;
664 }
665
Ben Lindstrom3f471632001-07-04 03:53:15 +0000666 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100667 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000668 case SSH_AGENTC_LOCK:
669 case SSH_AGENTC_UNLOCK:
670 process_lock_agent(e, type == SSH_AGENTC_LOCK);
671 break;
Damien Millerad833b32000-08-23 10:46:23 +1000672 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100673 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000674 process_authentication_challenge1(e);
675 break;
676 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
677 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100678 break;
679 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000680 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000681 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100682 break;
683 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000684 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100685 break;
686 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000687 process_remove_all_identities(e, 1);
688 break;
689 /* ssh2 */
690 case SSH2_AGENTC_SIGN_REQUEST:
691 process_sign_request2(e);
692 break;
693 case SSH2_AGENTC_REQUEST_IDENTITIES:
694 process_request_identities(e, 2);
695 break;
696 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000697 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000698 process_add_identity(e, 2);
699 break;
700 case SSH2_AGENTC_REMOVE_IDENTITY:
701 process_remove_identity(e, 2);
702 break;
703 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
704 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100705 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000706#ifdef SMARTCARD
707 case SSH_AGENTC_ADD_SMARTCARD_KEY:
708 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100709 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000710 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
711 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100712 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000713#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100714 default:
715 /* Unknown message. Respond with failure. */
716 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000717 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100718 buffer_put_int(&e->output, 1);
719 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
720 break;
721 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722}
723
Ben Lindstrombba81212001-06-25 05:01:22 +0000724static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000725new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000727 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000728
Damien Miller95def091999-11-25 00:26:21 +1100729 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
730 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller95def091999-11-25 00:26:21 +1100732 if (fd > max_fd)
733 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Damien Miller95def091999-11-25 00:26:21 +1100735 for (i = 0; i < sockets_alloc; i++)
736 if (sockets[i].type == AUTH_UNUSED) {
737 sockets[i].fd = fd;
738 sockets[i].type = type;
739 buffer_init(&sockets[i].input);
740 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000741 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100742 return;
743 }
744 old_alloc = sockets_alloc;
745 sockets_alloc += 10;
746 if (sockets)
747 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
748 else
749 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
750 for (i = old_alloc; i < sockets_alloc; i++)
751 sockets[i].type = AUTH_UNUSED;
752 sockets[old_alloc].type = type;
753 sockets[old_alloc].fd = fd;
754 buffer_init(&sockets[old_alloc].input);
755 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000756 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757}
758
Ben Lindstrombba81212001-06-25 05:01:22 +0000759static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000760prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000762 u_int i, sz;
763 int n = 0;
764
765 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100766 switch (sockets[i].type) {
767 case AUTH_SOCKET:
768 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000769 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100770 break;
771 case AUTH_UNUSED:
772 break;
773 default:
774 fatal("Unknown socket type %d", sockets[i].type);
775 break;
776 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000777 }
778
779 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000780 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000781 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000782 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000783 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000784 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000785 *fdrp = xmalloc(sz);
786 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000787 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000788 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000789 if (n < *fdl)
790 debug("XXX shrink: %d < %d", n, *fdl);
791 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000792 memset(*fdrp, 0, sz);
793 memset(*fdwp, 0, sz);
794
795 for (i = 0; i < sockets_alloc; i++) {
796 switch (sockets[i].type) {
797 case AUTH_SOCKET:
798 case AUTH_CONNECTION:
799 FD_SET(sockets[i].fd, *fdrp);
800 if (buffer_len(&sockets[i].output) > 0)
801 FD_SET(sockets[i].fd, *fdwp);
802 break;
803 default:
804 break;
805 }
806 }
807 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808}
809
Ben Lindstrombba81212001-06-25 05:01:22 +0000810static void
Damien Miller95def091999-11-25 00:26:21 +1100811after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000813 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100814 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100815 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000816 int len, sock;
817 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000818 uid_t euid;
819 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820
Damien Miller95def091999-11-25 00:26:21 +1100821 for (i = 0; i < sockets_alloc; i++)
822 switch (sockets[i].type) {
823 case AUTH_UNUSED:
824 break;
825 case AUTH_SOCKET:
826 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100827 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000828 sock = accept(sockets[i].fd,
829 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100830 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100831 error("accept from AUTH_SOCKET: %s",
832 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100833 break;
834 }
Damien Miller538f1812002-09-12 09:51:10 +1000835 if (getpeereid(sock, &euid, &egid) < 0) {
836 error("getpeereid %d failed: %s",
837 sock, strerror(errno));
838 close(sock);
839 break;
840 }
Damien Milleraf9de382002-10-03 11:54:35 +1000841 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000842 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000843 "peer euid %u != uid %u",
844 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000845 close(sock);
846 break;
847 }
Damien Miller95def091999-11-25 00:26:21 +1100848 new_socket(AUTH_CONNECTION, sock);
849 }
850 break;
851 case AUTH_CONNECTION:
852 if (buffer_len(&sockets[i].output) > 0 &&
853 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000854 do {
855 len = write(sockets[i].fd,
856 buffer_ptr(&sockets[i].output),
857 buffer_len(&sockets[i].output));
858 if (len == -1 && (errno == EAGAIN ||
859 errno == EINTR))
860 continue;
861 break;
862 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100863 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000864 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100865 break;
866 }
867 buffer_consume(&sockets[i].output, len);
868 }
869 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000870 do {
871 len = read(sockets[i].fd, buf, sizeof(buf));
872 if (len == -1 && (errno == EAGAIN ||
873 errno == EINTR))
874 continue;
875 break;
876 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100877 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000878 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100879 break;
880 }
881 buffer_append(&sockets[i].input, buf, len);
882 process_message(&sockets[i]);
883 }
884 break;
885 default:
886 fatal("Unknown type %d", sockets[i].type);
887 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888}
889
Ben Lindstrombba81212001-06-25 05:01:22 +0000890static void
Damien Millerc653b892002-02-08 22:05:41 +1100891cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000892{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000893 if (socket_name[0])
894 unlink(socket_name);
895 if (socket_dir[0])
896 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897}
898
Ben Lindstrombba81212001-06-25 05:01:22 +0000899static void
Damien Miller792c5111999-10-29 09:47:09 +1000900cleanup_exit(int i)
901{
Damien Millerc653b892002-02-08 22:05:41 +1100902 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100903 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000904}
905
Ben Lindstrombba81212001-06-25 05:01:22 +0000906static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000907cleanup_handler(int sig)
908{
Damien Millerc653b892002-02-08 22:05:41 +1100909 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000910 _exit(2);
911}
912
Ben Lindstrombba81212001-06-25 05:01:22 +0000913static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000914check_parent_exists(int sig)
915{
916 int save_errno = errno;
917
918 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
919 /* printf("Parent has died - Authentication agent exiting.\n"); */
920 cleanup_handler(sig); /* safe */
921 }
922 signal(SIGALRM, check_parent_exists);
923 alarm(10);
924 errno = save_errno;
925}
926
927static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000928usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000929{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000930 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000931 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000932 fprintf(stderr, "Options:\n");
933 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
934 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
935 fprintf(stderr, " -k Kill the current agent.\n");
936 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000937 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100938 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100939 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000940}
941
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942int
943main(int ac, char **av)
944{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000945 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000946 char *shell, *format, *pidstr, *agentsocket = NULL;
947 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100948 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000949#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000950 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000951#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000952#ifdef HAVE_CYGWIN
953 int prev_mask;
954#endif
Damien Miller615f9392000-05-17 22:53:33 +1000955 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +0000956 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000957 pid_t pid;
958 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +0000959
Damien Miller6cffb9a2002-09-04 16:20:26 +1000960 /* drop */
961 setegid(getgid());
962 setgid(getgid());
963
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000964 SSLeay_add_all_algorithms();
965
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000966 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000967 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100968 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000969
Damien Miller53d81482003-01-22 11:47:19 +1100970 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100971 switch (ch) {
972 case 'c':
973 if (s_flag)
974 usage();
975 c_flag++;
976 break;
977 case 'k':
978 k_flag++;
979 break;
980 case 's':
981 if (c_flag)
982 usage();
983 s_flag++;
984 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000985 case 'd':
986 if (d_flag)
987 usage();
988 d_flag++;
989 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000990 case 'a':
991 agentsocket = optarg;
992 break;
Damien Miller53d81482003-01-22 11:47:19 +1100993 case 't':
994 if ((lifetime = convtime(optarg)) == -1) {
995 fprintf(stderr, "Invalid lifetime\n");
996 usage();
997 }
998 break;
Damien Miller95def091999-11-25 00:26:21 +1100999 default:
1000 usage();
1001 }
Damien Miller792c5111999-10-29 09:47:09 +10001002 }
Damien Miller95def091999-11-25 00:26:21 +11001003 ac -= optind;
1004 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001005
Ben Lindstromd94580c2001-07-04 03:48:02 +00001006 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001007 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001008
Ben Lindstromeecdf232002-04-02 21:03:51 +00001009 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001010 shell = getenv("SHELL");
1011 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1012 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001013 }
Damien Miller95def091999-11-25 00:26:21 +11001014 if (k_flag) {
1015 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1016 if (pidstr == NULL) {
1017 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001018 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001019 exit(1);
1020 }
1021 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001022 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001023 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001024 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001025 exit(1);
1026 }
1027 if (kill(pid, SIGTERM) == -1) {
1028 perror("kill");
1029 exit(1);
1030 }
1031 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1032 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1033 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001034 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001035 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 parent_pid = getpid();
1038
Ben Lindstromb7788f32002-06-06 21:46:08 +00001039 if (agentsocket == NULL) {
1040 /* Create private directory for agent socket */
1041 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1042 if (mkdtemp(socket_dir) == NULL) {
1043 perror("mkdtemp: private socket dir");
1044 exit(1);
1045 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001046 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1047 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001048 } else {
1049 /* Try to use specified agent socket */
1050 socket_dir[0] = '\0';
1051 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001052 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053
Damien Miller5428f641999-11-25 11:54:57 +11001054 /*
1055 * Create socket early so it will exist before command gets run from
1056 * the parent.
1057 */
Damien Miller95def091999-11-25 00:26:21 +11001058 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1059 if (sock < 0) {
1060 perror("socket");
1061 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001062 }
Damien Miller95def091999-11-25 00:26:21 +11001063 memset(&sunaddr, 0, sizeof(sunaddr));
1064 sunaddr.sun_family = AF_UNIX;
1065 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001066#ifdef HAVE_CYGWIN
1067 prev_mask = umask(0177);
1068#endif
Damien Miller95def091999-11-25 00:26:21 +11001069 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1070 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001071#ifdef HAVE_CYGWIN
1072 umask(prev_mask);
1073#endif
Damien Miller95def091999-11-25 00:26:21 +11001074 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001076#ifdef HAVE_CYGWIN
1077 umask(prev_mask);
1078#endif
Damien Miller4efdfff2002-09-04 16:28:18 +10001079 if (listen(sock, 128) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001080 perror("listen");
1081 cleanup_exit(1);
1082 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001083
Damien Miller5428f641999-11-25 11:54:57 +11001084 /*
1085 * Fork, and have the parent execute the command, if any, or present
1086 * the socket data. The child continues as the authentication agent.
1087 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001088 if (d_flag) {
1089 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1090 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1091 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1092 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001093 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001094 goto skip;
1095 }
Damien Miller95def091999-11-25 00:26:21 +11001096 pid = fork();
1097 if (pid == -1) {
1098 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001099 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001100 }
1101 if (pid != 0) { /* Parent - execute the given command. */
1102 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001103 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001104 if (ac == 0) {
1105 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1106 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001107 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001108 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001109 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001110 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001111 exit(0);
1112 }
Damien Millere4340be2000-09-16 13:29:08 +11001113 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1114 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1115 perror("setenv");
1116 exit(1);
1117 }
Damien Miller95def091999-11-25 00:26:21 +11001118 execvp(av[0], av);
1119 perror(av[0]);
1120 exit(1);
1121 }
Damien Millerc653b892002-02-08 22:05:41 +11001122 /* child */
1123 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001124
1125 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001126 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001127 cleanup_exit(1);
1128 }
1129
1130 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001131 close(0);
1132 close(1);
1133 close(2);
1134
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001135#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001136 /* deny core dumps, since memory contains unencrypted private keys */
1137 rlim.rlim_cur = rlim.rlim_max = 0;
1138 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001139 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001140 cleanup_exit(1);
1141 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001142#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001143
1144skip:
Damien Millerc653b892002-02-08 22:05:41 +11001145 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001146 new_socket(AUTH_SOCKET, sock);
1147 if (ac > 0) {
1148 signal(SIGALRM, check_parent_exists);
1149 alarm(10);
1150 }
Damien Millerad833b32000-08-23 10:46:23 +10001151 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001152 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001153 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001154 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001155 signal(SIGHUP, cleanup_handler);
1156 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001157 nalloc = 0;
1158
Damien Miller95def091999-11-25 00:26:21 +11001159 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001160 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001162 if (errno == EINTR)
1163 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001164 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001165 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001166 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001167 }
1168 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169}