blob: cdc236d99cbb30e2d5f4a1b09d281a8222b2d74c [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 Millerec52d7c2002-01-22 23:52:17 +110037#include "openbsd-compat/fake-queue.h"
Damien Miller58f34862002-09-04 16:31:21 +100038RCSID("$OpenBSD: ssh-agent.c,v 1.101 2002/08/22 19:27:53 stevesk 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
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
Damien Miller58f34862002-09-04 16:31:21 +1000110close_socket(SocketEntry *e)
111{
112 shutdown(e->fd, SHUT_RDWR);
113 close(e->fd);
114 e->fd = -1;
115 e->type = AUTH_UNUSED;
116 buffer_free(&e->input);
117 buffer_free(&e->output);
118 buffer_free(&e->request);
119}
120
121static void
Damien Millerad833b32000-08-23 10:46:23 +1000122idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Damien Millerad833b32000-08-23 10:46:23 +1000124 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000125
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000126 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100127 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000128 idtable[i].nentries = 0;
129 }
130}
131
132/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000133static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000134idtab_lookup(int version)
135{
136 if (version < 1 || version > 2)
137 fatal("internal error, bad protocol version %d", version);
138 return &idtable[version];
139}
140
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000141static void
142free_identity(Identity *id)
143{
144 key_free(id->key);
145 xfree(id->comment);
146 xfree(id);
147}
148
Damien Millerad833b32000-08-23 10:46:23 +1000149/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100150static Identity *
151lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000152{
Damien Miller1a534ae2002-01-22 23:26:13 +1100153 Identity *id;
154
Damien Millerad833b32000-08-23 10:46:23 +1000155 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 TAILQ_FOREACH(id, &tab->idlist, next) {
157 if (key_equal(key, id->key))
158 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000159 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100160 return (NULL);
161}
162
Damien Millerad833b32000-08-23 10:46:23 +1000163/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static void
Damien Millerad833b32000-08-23 10:46:23 +1000165process_request_identities(SocketEntry *e, int version)
166{
167 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100168 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000169 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller95def091999-11-25 00:26:21 +1100171 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000172 buffer_put_char(&msg, (version == 1) ?
173 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
174 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100175 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100176 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000177 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
178 buffer_put_bignum(&msg, id->key->rsa->e);
179 buffer_put_bignum(&msg, id->key->rsa->n);
180 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000181 u_char *blob;
182 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100183 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000184 buffer_put_string(&msg, blob, blen);
185 xfree(blob);
186 }
187 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100188 }
189 buffer_put_int(&e->output, buffer_len(&msg));
190 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
191 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Millerad833b32000-08-23 10:46:23 +1000194/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Damien Millerad833b32000-08-23 10:46:23 +1000196process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000198 u_char buf[32], mdbuf[16], session_id[16];
199 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000200 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000201 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000202 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100203 Buffer msg;
204 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000205 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller95def091999-11-25 00:26:21 +1100207 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100208 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100209 if ((challenge = BN_new()) == NULL)
210 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000211
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000212 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000213 buffer_get_bignum(&e->request, key->rsa->e);
214 buffer_get_bignum(&e->request, key->rsa->n);
215 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Millerad833b32000-08-23 10:46:23 +1000217 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000218 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000219 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000220 buffer_get(&e->request, session_id, 16);
221 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000222 if (response_type != 1)
223 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller1a534ae2002-01-22 23:26:13 +1100225 id = lookup_identity(key, 1);
226 if (id != NULL) {
227 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000228 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100229 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
230 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100231
Damien Millerad833b32000-08-23 10:46:23 +1000232 /* The response is MD5 of decrypted challenge plus session id. */
233 len = BN_num_bytes(challenge);
234 if (len <= 0 || len > 32) {
235 log("process_authentication_challenge: bad challenge length %d", len);
236 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100237 }
Damien Millerad833b32000-08-23 10:46:23 +1000238 memset(buf, 0, 32);
239 BN_bn2bin(challenge, buf + 32 - len);
240 MD5_Init(&md);
241 MD5_Update(&md, buf, 32);
242 MD5_Update(&md, session_id, 16);
243 MD5_Final(mdbuf, &md);
244
245 /* Send the response. */
246 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
247 for (i = 0; i < 16; i++)
248 buffer_put_char(&msg, mdbuf[i]);
249 goto send;
250 }
251
252failure:
253 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100254 buffer_put_char(&msg, SSH_AGENT_FAILURE);
255send:
256 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000257 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
258 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100259 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000260 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261}
262
Damien Millerad833b32000-08-23 10:46:23 +1000263/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000264static void
Damien Millerad833b32000-08-23 10:46:23 +1000265process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000267 u_char *blob, *data, *signature = NULL;
268 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000269 extern int datafellows;
270 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000271 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000272 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000273
274 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000275
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000276 blob = buffer_get_string(&e->request, &blen);
277 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100278
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000279 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100280 if (flags & SSH_AGENT_OLD_SIGNATURE)
281 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000282
Damien Miller0bc1bd82000-11-13 22:57:25 +1100283 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000284 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100285 Identity *id = lookup_identity(key, 2);
286 if (id != NULL)
287 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000288 }
289 key_free(key);
290 buffer_init(&msg);
291 if (ok == 0) {
292 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
293 buffer_put_string(&msg, signature, slen);
294 } else {
295 buffer_put_char(&msg, SSH_AGENT_FAILURE);
296 }
297 buffer_put_int(&e->output, buffer_len(&msg));
298 buffer_append(&e->output, buffer_ptr(&msg),
299 buffer_len(&msg));
300 buffer_free(&msg);
301 xfree(data);
302 xfree(blob);
303 if (signature != NULL)
304 xfree(signature);
305}
306
307/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000308static void
Damien Millerad833b32000-08-23 10:46:23 +1000309process_remove_identity(SocketEntry *e, int version)
310{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000311 u_int blen, bits;
312 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100313 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000314 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100315
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000316 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000317 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000319 bits = buffer_get_int(&e->request);
320 buffer_get_bignum(&e->request, key->rsa->e);
321 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Millerad833b32000-08-23 10:46:23 +1000323 if (bits != key_size(key))
Ben Lindstrom822b6342002-06-23 21:38:49 +0000324 log("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000325 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000326 break;
327 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000328 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100329 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000330 xfree(blob);
331 break;
332 }
333 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100334 Identity *id = lookup_identity(key, version);
335 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100336 /*
337 * We have this key. Free the old key. Since we
338 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000339 * the array, we actually free the key there and move
340 * all the entries between the empty slot and the end
341 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100342 */
Damien Millerad833b32000-08-23 10:46:23 +1000343 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100344 if (tab->nentries < 1)
345 fatal("process_remove_identity: "
346 "internal error: tab->nentries %d",
347 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100348 TAILQ_REMOVE(&tab->idlist, id, next);
349 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000350 tab->nentries--;
351 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100352 }
Damien Millerad833b32000-08-23 10:46:23 +1000353 key_free(key);
354 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000356 buffer_put_char(&e->output,
357 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358}
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static void
Damien Millerad833b32000-08-23 10:46:23 +1000361process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Damien Millerad833b32000-08-23 10:46:23 +1000363 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100364 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Damien Miller95def091999-11-25 00:26:21 +1100366 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100367 for (id = TAILQ_FIRST(&tab->idlist); id;
368 id = TAILQ_FIRST(&tab->idlist)) {
369 TAILQ_REMOVE(&tab->idlist, id, next);
370 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100371 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller95def091999-11-25 00:26:21 +1100373 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000374 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375
376 /* Send success. */
377 buffer_put_int(&e->output, 1);
378 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100379}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
Ben Lindstrombba81212001-06-25 05:01:22 +0000381static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000382reaper(void)
383{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000384 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000385 Identity *id, *nxt;
386 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000387 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000388
389 for (version = 1; version < 3; version++) {
390 tab = idtab_lookup(version);
391 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
392 nxt = TAILQ_NEXT(id, next);
393 if (id->death != 0 && now >= id->death) {
394 TAILQ_REMOVE(&tab->idlist, id, next);
395 free_identity(id);
396 tab->nentries--;
397 }
398 }
399 }
400}
401
402static void
Damien Millerad833b32000-08-23 10:46:23 +1000403process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100404{
Damien Millerad833b32000-08-23 10:46:23 +1000405 Idtab *tab = idtab_lookup(version);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000406 int type, success = 0, death = 0;
407 char *type_name, *comment;
408 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100409
Damien Millerad833b32000-08-23 10:46:23 +1000410 switch (version) {
411 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100412 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000413 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000414 buffer_get_bignum(&e->request, k->rsa->n);
415 buffer_get_bignum(&e->request, k->rsa->e);
416 buffer_get_bignum(&e->request, k->rsa->d);
417 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100418
Damien Millerad833b32000-08-23 10:46:23 +1000419 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000420 buffer_get_bignum(&e->request, k->rsa->q); /* p */
421 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100422
Damien Millerad833b32000-08-23 10:46:23 +1000423 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000424 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000425 break;
426 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000427 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000428 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100429 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000430 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100431 case KEY_DSA:
432 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000433 buffer_get_bignum2(&e->request, k->dsa->p);
434 buffer_get_bignum2(&e->request, k->dsa->q);
435 buffer_get_bignum2(&e->request, k->dsa->g);
436 buffer_get_bignum2(&e->request, k->dsa->pub_key);
437 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 break;
439 case KEY_RSA:
440 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000441 buffer_get_bignum2(&e->request, k->rsa->n);
442 buffer_get_bignum2(&e->request, k->rsa->e);
443 buffer_get_bignum2(&e->request, k->rsa->d);
444 buffer_get_bignum2(&e->request, k->rsa->iqmp);
445 buffer_get_bignum2(&e->request, k->rsa->p);
446 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100447
448 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000449 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100450 break;
451 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000452 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000453 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100454 }
Damien Millerad833b32000-08-23 10:46:23 +1000455 break;
456 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000457 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000458 if (k == NULL) {
459 xfree(comment);
460 goto send;
461 }
462 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000463 while (buffer_len(&e->request)) {
464 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000465 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000466 death = time(NULL) + buffer_get_int(&e->request);
467 break;
468 default:
469 break;
470 }
471 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000472 if (lookup_identity(k, version) == NULL) {
473 Identity *id = xmalloc(sizeof(Identity));
474 id->key = k;
475 id->comment = comment;
476 id->death = death;
477 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
478 /* Increment the number of identities. */
479 tab->nentries++;
480 } else {
481 key_free(k);
482 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000483 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000484send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000485 buffer_put_int(&e->output, 1);
486 buffer_put_char(&e->output,
487 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
488}
489
Ben Lindstrom2f717042002-06-06 21:52:03 +0000490/* XXX todo: encrypt sensitive data with passphrase */
491static void
492process_lock_agent(SocketEntry *e, int lock)
493{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000494 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000495 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000496
497 passwd = buffer_get_string(&e->request, NULL);
498 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
499 locked = 0;
500 memset(lock_passwd, 0, strlen(lock_passwd));
501 xfree(lock_passwd);
502 lock_passwd = NULL;
503 success = 1;
504 } else if (!locked && lock) {
505 locked = 1;
506 lock_passwd = xstrdup(passwd);
507 success = 1;
508 }
509 memset(passwd, 0, strlen(passwd));
510 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000511
Ben Lindstrom2f717042002-06-06 21:52:03 +0000512 buffer_put_int(&e->output, 1);
513 buffer_put_char(&e->output,
514 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000515}
516
517static void
518no_identities(SocketEntry *e, u_int type)
519{
520 Buffer msg;
521
522 buffer_init(&msg);
523 buffer_put_char(&msg,
524 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
525 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
526 buffer_put_int(&msg, 0);
527 buffer_put_int(&e->output, buffer_len(&msg));
528 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
529 buffer_free(&msg);
530}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000531
532#ifdef SMARTCARD
533static void
534process_add_smartcard_key (SocketEntry *e)
535{
Ben Lindstromba72d302002-03-22 03:51:06 +0000536 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000537 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000538 Key **keys, *k;
539 Identity *id;
540 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100541
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000542 sc_reader_id = buffer_get_string(&e->request, NULL);
543 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000544 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000545 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000546 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000547
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000548 if (keys == NULL || keys[0] == NULL) {
549 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000550 goto send;
551 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000552 for (i = 0; keys[i] != NULL; i++) {
553 k = keys[i];
554 version = k->type == KEY_RSA1 ? 1 : 2;
555 tab = idtab_lookup(version);
556 if (lookup_identity(k, version) == NULL) {
557 id = xmalloc(sizeof(Identity));
558 id->key = k;
559 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000560 id->death = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000561 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
562 tab->nentries++;
563 success = 1;
564 } else {
565 key_free(k);
566 }
567 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000568 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000569 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000570send:
571 buffer_put_int(&e->output, 1);
572 buffer_put_char(&e->output,
573 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
574}
575
576static void
577process_remove_smartcard_key(SocketEntry *e)
578{
Ben Lindstromba72d302002-03-22 03:51:06 +0000579 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000580 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000581 Key **keys, *k = NULL;
582 Identity *id;
583 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000584
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000585 sc_reader_id = buffer_get_string(&e->request, NULL);
586 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000587 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000588 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000589 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000590
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000591 if (keys == NULL || keys[0] == NULL) {
592 error("sc_get_keys failed");
593 goto send;
594 }
595 for (i = 0; keys[i] != NULL; i++) {
596 k = keys[i];
597 version = k->type == KEY_RSA1 ? 1 : 2;
598 if ((id = lookup_identity(k, version)) != NULL) {
599 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000600 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000601 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100602 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000603 success = 1;
604 }
605 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000606 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000608 xfree(keys);
609send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000610 buffer_put_int(&e->output, 1);
611 buffer_put_char(&e->output,
612 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
613}
Ben Lindstromffce1472001-08-06 21:57:31 +0000614#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615
Damien Millerad833b32000-08-23 10:46:23 +1000616/* dispatch incoming messages */
617
Ben Lindstrombba81212001-06-25 05:01:22 +0000618static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619process_message(SocketEntry *e)
620{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000621 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000622 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000623
624 /* kill dead keys */
625 reaper();
626
Damien Miller95def091999-11-25 00:26:21 +1100627 if (buffer_len(&e->input) < 5)
628 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100629 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100630 msg_len = GET_32BIT(cp);
631 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000632 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100633 return;
634 }
635 if (buffer_len(&e->input) < msg_len + 4)
636 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000637
638 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100639 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000640 buffer_clear(&e->request);
641 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
642 buffer_consume(&e->input, msg_len);
643 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Ben Lindstrom2f717042002-06-06 21:52:03 +0000645 /* check wheter agent is locked */
646 if (locked && type != SSH_AGENTC_UNLOCK) {
647 buffer_clear(&e->request);
648 switch (type) {
649 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
650 case SSH2_AGENTC_REQUEST_IDENTITIES:
651 /* send empty lists */
652 no_identities(e, type);
653 break;
654 default:
655 /* send a fail message for all other request types */
656 buffer_put_int(&e->output, 1);
657 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
658 }
659 return;
660 }
661
Ben Lindstrom3f471632001-07-04 03:53:15 +0000662 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100663 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000664 case SSH_AGENTC_LOCK:
665 case SSH_AGENTC_UNLOCK:
666 process_lock_agent(e, type == SSH_AGENTC_LOCK);
667 break;
Damien Millerad833b32000-08-23 10:46:23 +1000668 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100669 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000670 process_authentication_challenge1(e);
671 break;
672 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
673 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100674 break;
675 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000676 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000677 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100678 break;
679 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000680 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100681 break;
682 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000683 process_remove_all_identities(e, 1);
684 break;
685 /* ssh2 */
686 case SSH2_AGENTC_SIGN_REQUEST:
687 process_sign_request2(e);
688 break;
689 case SSH2_AGENTC_REQUEST_IDENTITIES:
690 process_request_identities(e, 2);
691 break;
692 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000693 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000694 process_add_identity(e, 2);
695 break;
696 case SSH2_AGENTC_REMOVE_IDENTITY:
697 process_remove_identity(e, 2);
698 break;
699 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
700 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100701 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000702#ifdef SMARTCARD
703 case SSH_AGENTC_ADD_SMARTCARD_KEY:
704 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100705 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000706 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
707 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000709#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100710 default:
711 /* Unknown message. Respond with failure. */
712 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000713 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100714 buffer_put_int(&e->output, 1);
715 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
716 break;
717 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718}
719
Ben Lindstrombba81212001-06-25 05:01:22 +0000720static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000721new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000723 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000724
Damien Miller95def091999-11-25 00:26:21 +1100725 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
726 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727
Damien Miller95def091999-11-25 00:26:21 +1100728 if (fd > max_fd)
729 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730
Damien Miller95def091999-11-25 00:26:21 +1100731 for (i = 0; i < sockets_alloc; i++)
732 if (sockets[i].type == AUTH_UNUSED) {
733 sockets[i].fd = fd;
734 sockets[i].type = type;
735 buffer_init(&sockets[i].input);
736 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000737 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100738 return;
739 }
740 old_alloc = sockets_alloc;
741 sockets_alloc += 10;
742 if (sockets)
743 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
744 else
745 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
746 for (i = old_alloc; i < sockets_alloc; i++)
747 sockets[i].type = AUTH_UNUSED;
748 sockets[old_alloc].type = type;
749 sockets[old_alloc].fd = fd;
750 buffer_init(&sockets[old_alloc].input);
751 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000752 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753}
754
Ben Lindstrombba81212001-06-25 05:01:22 +0000755static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000756prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000758 u_int i, sz;
759 int n = 0;
760
761 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100762 switch (sockets[i].type) {
763 case AUTH_SOCKET:
764 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000765 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100766 break;
767 case AUTH_UNUSED:
768 break;
769 default:
770 fatal("Unknown socket type %d", sockets[i].type);
771 break;
772 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000773 }
774
775 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000776 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000777 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000778 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000779 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000780 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000781 *fdrp = xmalloc(sz);
782 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000783 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000784 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000785 if (n < *fdl)
786 debug("XXX shrink: %d < %d", n, *fdl);
787 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000788 memset(*fdrp, 0, sz);
789 memset(*fdwp, 0, sz);
790
791 for (i = 0; i < sockets_alloc; i++) {
792 switch (sockets[i].type) {
793 case AUTH_SOCKET:
794 case AUTH_CONNECTION:
795 FD_SET(sockets[i].fd, *fdrp);
796 if (buffer_len(&sockets[i].output) > 0)
797 FD_SET(sockets[i].fd, *fdwp);
798 break;
799 default:
800 break;
801 }
802 }
803 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804}
805
Ben Lindstrombba81212001-06-25 05:01:22 +0000806static void
Damien Miller95def091999-11-25 00:26:21 +1100807after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000809 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100810 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100811 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000812 int len, sock;
813 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 for (i = 0; i < sockets_alloc; i++)
816 switch (sockets[i].type) {
817 case AUTH_UNUSED:
818 break;
819 case AUTH_SOCKET:
820 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100821 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000822 sock = accept(sockets[i].fd,
823 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100824 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100825 error("accept from AUTH_SOCKET: %s",
826 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100827 break;
828 }
829 new_socket(AUTH_CONNECTION, sock);
830 }
831 break;
832 case AUTH_CONNECTION:
833 if (buffer_len(&sockets[i].output) > 0 &&
834 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000835 do {
836 len = write(sockets[i].fd,
837 buffer_ptr(&sockets[i].output),
838 buffer_len(&sockets[i].output));
839 if (len == -1 && (errno == EAGAIN ||
840 errno == EINTR))
841 continue;
842 break;
843 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100844 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000845 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100846 break;
847 }
848 buffer_consume(&sockets[i].output, len);
849 }
850 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000851 do {
852 len = read(sockets[i].fd, buf, sizeof(buf));
853 if (len == -1 && (errno == EAGAIN ||
854 errno == EINTR))
855 continue;
856 break;
857 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100858 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000859 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100860 break;
861 }
862 buffer_append(&sockets[i].input, buf, len);
863 process_message(&sockets[i]);
864 }
865 break;
866 default:
867 fatal("Unknown type %d", sockets[i].type);
868 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869}
870
Ben Lindstrombba81212001-06-25 05:01:22 +0000871static void
Damien Millerc653b892002-02-08 22:05:41 +1100872cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000873{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000874 if (socket_name[0])
875 unlink(socket_name);
876 if (socket_dir[0])
877 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static void
Damien Miller792c5111999-10-29 09:47:09 +1000881cleanup_exit(int i)
882{
Damien Millerc653b892002-02-08 22:05:41 +1100883 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100884 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000885}
886
Ben Lindstrombba81212001-06-25 05:01:22 +0000887static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000888cleanup_handler(int sig)
889{
Damien Millerc653b892002-02-08 22:05:41 +1100890 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000891 _exit(2);
892}
893
Ben Lindstrombba81212001-06-25 05:01:22 +0000894static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000895check_parent_exists(int sig)
896{
897 int save_errno = errno;
898
899 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
900 /* printf("Parent has died - Authentication agent exiting.\n"); */
901 cleanup_handler(sig); /* safe */
902 }
903 signal(SIGALRM, check_parent_exists);
904 alarm(10);
905 errno = save_errno;
906}
907
908static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000909usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000910{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000911 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000912 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000913 fprintf(stderr, "Options:\n");
914 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
915 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
916 fprintf(stderr, " -k Kill the current agent.\n");
917 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000918 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100919 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000920}
921
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922int
923main(int ac, char **av)
924{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000925 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000926 char *shell, *format, *pidstr, *agentsocket = NULL;
927 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100928 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000929#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000930 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000931#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000932#ifdef HAVE_CYGWIN
933 int prev_mask;
934#endif
Damien Miller615f9392000-05-17 22:53:33 +1000935 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +0000936 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000937 pid_t pid;
938 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +0000939
Damien Miller6cffb9a2002-09-04 16:20:26 +1000940 /* drop */
941 setegid(getgid());
942 setgid(getgid());
943
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000944 SSLeay_add_all_algorithms();
945
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000946 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000947 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100948 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000949
Ben Lindstromb7788f32002-06-06 21:46:08 +0000950 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100951 switch (ch) {
952 case 'c':
953 if (s_flag)
954 usage();
955 c_flag++;
956 break;
957 case 'k':
958 k_flag++;
959 break;
960 case 's':
961 if (c_flag)
962 usage();
963 s_flag++;
964 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000965 case 'd':
966 if (d_flag)
967 usage();
968 d_flag++;
969 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000970 case 'a':
971 agentsocket = optarg;
972 break;
Damien Miller95def091999-11-25 00:26:21 +1100973 default:
974 usage();
975 }
Damien Miller792c5111999-10-29 09:47:09 +1000976 }
Damien Miller95def091999-11-25 00:26:21 +1100977 ac -= optind;
978 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000979
Ben Lindstromd94580c2001-07-04 03:48:02 +0000980 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100981 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000982
Ben Lindstromeecdf232002-04-02 21:03:51 +0000983 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100984 shell = getenv("SHELL");
985 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
986 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000987 }
Damien Miller95def091999-11-25 00:26:21 +1100988 if (k_flag) {
989 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
990 if (pidstr == NULL) {
991 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000992 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100993 exit(1);
994 }
995 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000996 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100997 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000998 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100999 exit(1);
1000 }
1001 if (kill(pid, SIGTERM) == -1) {
1002 perror("kill");
1003 exit(1);
1004 }
1005 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1006 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1007 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001008 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001009 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001010 }
Damien Miller95def091999-11-25 00:26:21 +11001011 parent_pid = getpid();
1012
Ben Lindstromb7788f32002-06-06 21:46:08 +00001013 if (agentsocket == NULL) {
1014 /* Create private directory for agent socket */
1015 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1016 if (mkdtemp(socket_dir) == NULL) {
1017 perror("mkdtemp: private socket dir");
1018 exit(1);
1019 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001020 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1021 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001022 } else {
1023 /* Try to use specified agent socket */
1024 socket_dir[0] = '\0';
1025 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001026 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller5428f641999-11-25 11:54:57 +11001028 /*
1029 * Create socket early so it will exist before command gets run from
1030 * the parent.
1031 */
Damien Miller95def091999-11-25 00:26:21 +11001032 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1033 if (sock < 0) {
1034 perror("socket");
1035 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 memset(&sunaddr, 0, sizeof(sunaddr));
1038 sunaddr.sun_family = AF_UNIX;
1039 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001040#ifdef HAVE_CYGWIN
1041 prev_mask = umask(0177);
1042#endif
Damien Miller95def091999-11-25 00:26:21 +11001043 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1044 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001045#ifdef HAVE_CYGWIN
1046 umask(prev_mask);
1047#endif
Damien Miller95def091999-11-25 00:26:21 +11001048 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001050#ifdef HAVE_CYGWIN
1051 umask(prev_mask);
1052#endif
Damien Miller4efdfff2002-09-04 16:28:18 +10001053 if (listen(sock, 128) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001054 perror("listen");
1055 cleanup_exit(1);
1056 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001057
Damien Miller5428f641999-11-25 11:54:57 +11001058 /*
1059 * Fork, and have the parent execute the command, if any, or present
1060 * the socket data. The child continues as the authentication agent.
1061 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001062 if (d_flag) {
1063 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1064 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1065 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1066 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001067 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001068 goto skip;
1069 }
Damien Miller95def091999-11-25 00:26:21 +11001070 pid = fork();
1071 if (pid == -1) {
1072 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001073 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001074 }
1075 if (pid != 0) { /* Parent - execute the given command. */
1076 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001077 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001078 if (ac == 0) {
1079 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1080 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001081 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001082 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001083 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001084 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001085 exit(0);
1086 }
Damien Millere4340be2000-09-16 13:29:08 +11001087 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1088 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1089 perror("setenv");
1090 exit(1);
1091 }
Damien Miller95def091999-11-25 00:26:21 +11001092 execvp(av[0], av);
1093 perror(av[0]);
1094 exit(1);
1095 }
Damien Millerc653b892002-02-08 22:05:41 +11001096 /* child */
1097 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001098
1099 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001100 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001101 cleanup_exit(1);
1102 }
1103
1104 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001105 close(0);
1106 close(1);
1107 close(2);
1108
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001109#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001110 /* deny core dumps, since memory contains unencrypted private keys */
1111 rlim.rlim_cur = rlim.rlim_max = 0;
1112 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001113 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001114 cleanup_exit(1);
1115 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001116#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001117
1118skip:
Damien Millerc653b892002-02-08 22:05:41 +11001119 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001120 new_socket(AUTH_SOCKET, sock);
1121 if (ac > 0) {
1122 signal(SIGALRM, check_parent_exists);
1123 alarm(10);
1124 }
Damien Millerad833b32000-08-23 10:46:23 +10001125 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001126 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001127 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001128 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001129 signal(SIGHUP, cleanup_handler);
1130 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001131 nalloc = 0;
1132
Damien Miller95def091999-11-25 00:26:21 +11001133 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001134 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001135 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001136 if (errno == EINTR)
1137 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001138 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001139 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001140 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001141 }
1142 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143}