blob: ac16bae40aa59307d76a0a0b4600fb6f6d02c19f [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"
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +000038RCSID("$OpenBSD: ssh-agent.c,v 1.97 2002/06/24 14:55:38 markus 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 Millerad833b32000-08-23 10:46:23 +1000110idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Damien Millerad833b32000-08-23 10:46:23 +1000112 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000113
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000114 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100115 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000116 idtable[i].nentries = 0;
117 }
118}
119
120/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000122idtab_lookup(int version)
123{
124 if (version < 1 || version > 2)
125 fatal("internal error, bad protocol version %d", version);
126 return &idtable[version];
127}
128
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000129static void
130free_identity(Identity *id)
131{
132 key_free(id->key);
133 xfree(id->comment);
134 xfree(id);
135}
136
Damien Millerad833b32000-08-23 10:46:23 +1000137/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100138static Identity *
139lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000140{
Damien Miller1a534ae2002-01-22 23:26:13 +1100141 Identity *id;
142
Damien Millerad833b32000-08-23 10:46:23 +1000143 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100144 TAILQ_FOREACH(id, &tab->idlist, next) {
145 if (key_equal(key, id->key))
146 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000147 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100148 return (NULL);
149}
150
Damien Millerad833b32000-08-23 10:46:23 +1000151/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Damien Millerad833b32000-08-23 10:46:23 +1000153process_request_identities(SocketEntry *e, int version)
154{
155 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000157 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Damien Miller95def091999-11-25 00:26:21 +1100159 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000160 buffer_put_char(&msg, (version == 1) ?
161 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
162 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100163 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000165 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
166 buffer_put_bignum(&msg, id->key->rsa->e);
167 buffer_put_bignum(&msg, id->key->rsa->n);
168 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000169 u_char *blob;
170 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000172 buffer_put_string(&msg, blob, blen);
173 xfree(blob);
174 }
175 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100176 }
177 buffer_put_int(&e->output, buffer_len(&msg));
178 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
179 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Damien Millerad833b32000-08-23 10:46:23 +1000182/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static void
Damien Millerad833b32000-08-23 10:46:23 +1000184process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000186 u_char buf[32], mdbuf[16], session_id[16];
187 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000188 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000189 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000190 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100191 Buffer msg;
192 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000193 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller95def091999-11-25 00:26:21 +1100195 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100197 if ((challenge = BN_new()) == NULL)
198 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000199
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000200 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000201 buffer_get_bignum(&e->request, key->rsa->e);
202 buffer_get_bignum(&e->request, key->rsa->n);
203 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Millerad833b32000-08-23 10:46:23 +1000205 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000206 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000207 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000208 buffer_get(&e->request, session_id, 16);
209 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000210 if (response_type != 1)
211 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Miller1a534ae2002-01-22 23:26:13 +1100213 id = lookup_identity(key, 1);
214 if (id != NULL) {
215 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000216 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100217 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
218 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100219
Damien Millerad833b32000-08-23 10:46:23 +1000220 /* The response is MD5 of decrypted challenge plus session id. */
221 len = BN_num_bytes(challenge);
222 if (len <= 0 || len > 32) {
223 log("process_authentication_challenge: bad challenge length %d", len);
224 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100225 }
Damien Millerad833b32000-08-23 10:46:23 +1000226 memset(buf, 0, 32);
227 BN_bn2bin(challenge, buf + 32 - len);
228 MD5_Init(&md);
229 MD5_Update(&md, buf, 32);
230 MD5_Update(&md, session_id, 16);
231 MD5_Final(mdbuf, &md);
232
233 /* Send the response. */
234 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
235 for (i = 0; i < 16; i++)
236 buffer_put_char(&msg, mdbuf[i]);
237 goto send;
238 }
239
240failure:
241 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100242 buffer_put_char(&msg, SSH_AGENT_FAILURE);
243send:
244 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000245 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
246 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100247 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000248 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
250
Damien Millerad833b32000-08-23 10:46:23 +1000251/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Millerad833b32000-08-23 10:46:23 +1000253process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000255 u_char *blob, *data, *signature = NULL;
256 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000257 extern int datafellows;
258 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000259 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000260 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000261
262 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000263
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000264 blob = buffer_get_string(&e->request, &blen);
265 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100266
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000267 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100268 if (flags & SSH_AGENT_OLD_SIGNATURE)
269 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000270
Damien Miller0bc1bd82000-11-13 22:57:25 +1100271 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000272 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100273 Identity *id = lookup_identity(key, 2);
274 if (id != NULL)
275 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000276 }
277 key_free(key);
278 buffer_init(&msg);
279 if (ok == 0) {
280 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
281 buffer_put_string(&msg, signature, slen);
282 } else {
283 buffer_put_char(&msg, SSH_AGENT_FAILURE);
284 }
285 buffer_put_int(&e->output, buffer_len(&msg));
286 buffer_append(&e->output, buffer_ptr(&msg),
287 buffer_len(&msg));
288 buffer_free(&msg);
289 xfree(data);
290 xfree(blob);
291 if (signature != NULL)
292 xfree(signature);
293}
294
295/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
Damien Millerad833b32000-08-23 10:46:23 +1000297process_remove_identity(SocketEntry *e, int version)
298{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000299 u_int blen, bits;
300 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100301 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000302 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100303
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000304 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000305 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100306 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000307 bits = buffer_get_int(&e->request);
308 buffer_get_bignum(&e->request, key->rsa->e);
309 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Millerad833b32000-08-23 10:46:23 +1000311 if (bits != key_size(key))
Ben Lindstrom822b6342002-06-23 21:38:49 +0000312 log("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000313 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000314 break;
315 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000316 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100317 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000318 xfree(blob);
319 break;
320 }
321 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100322 Identity *id = lookup_identity(key, version);
323 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100324 /*
325 * We have this key. Free the old key. Since we
326 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000327 * the array, we actually free the key there and move
328 * all the entries between the empty slot and the end
329 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100330 */
Damien Millerad833b32000-08-23 10:46:23 +1000331 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100332 if (tab->nentries < 1)
333 fatal("process_remove_identity: "
334 "internal error: tab->nentries %d",
335 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 TAILQ_REMOVE(&tab->idlist, id, next);
337 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000338 tab->nentries--;
339 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100340 }
Damien Millerad833b32000-08-23 10:46:23 +1000341 key_free(key);
342 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000344 buffer_put_char(&e->output,
345 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Damien Millerad833b32000-08-23 10:46:23 +1000349process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Millerad833b32000-08-23 10:46:23 +1000351 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100352 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100355 for (id = TAILQ_FIRST(&tab->idlist); id;
356 id = TAILQ_FIRST(&tab->idlist)) {
357 TAILQ_REMOVE(&tab->idlist, id, next);
358 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100359 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000362 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
364 /* Send success. */
365 buffer_put_int(&e->output, 1);
366 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100367}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000370reaper(void)
371{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000372 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000373 Identity *id, *nxt;
374 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000375 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000376
377 for (version = 1; version < 3; version++) {
378 tab = idtab_lookup(version);
379 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
380 nxt = TAILQ_NEXT(id, next);
381 if (id->death != 0 && now >= id->death) {
382 TAILQ_REMOVE(&tab->idlist, id, next);
383 free_identity(id);
384 tab->nentries--;
385 }
386 }
387 }
388}
389
390static void
Damien Millerad833b32000-08-23 10:46:23 +1000391process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100392{
Damien Millerad833b32000-08-23 10:46:23 +1000393 Idtab *tab = idtab_lookup(version);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000394 int type, success = 0, death = 0;
395 char *type_name, *comment;
396 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100397
Damien Millerad833b32000-08-23 10:46:23 +1000398 switch (version) {
399 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100400 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000401 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000402 buffer_get_bignum(&e->request, k->rsa->n);
403 buffer_get_bignum(&e->request, k->rsa->e);
404 buffer_get_bignum(&e->request, k->rsa->d);
405 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100406
Damien Millerad833b32000-08-23 10:46:23 +1000407 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000408 buffer_get_bignum(&e->request, k->rsa->q); /* p */
409 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100410
Damien Millerad833b32000-08-23 10:46:23 +1000411 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000412 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000413 break;
414 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000415 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000416 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100417 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000418 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100419 case KEY_DSA:
420 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000421 buffer_get_bignum2(&e->request, k->dsa->p);
422 buffer_get_bignum2(&e->request, k->dsa->q);
423 buffer_get_bignum2(&e->request, k->dsa->g);
424 buffer_get_bignum2(&e->request, k->dsa->pub_key);
425 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100426 break;
427 case KEY_RSA:
428 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000429 buffer_get_bignum2(&e->request, k->rsa->n);
430 buffer_get_bignum2(&e->request, k->rsa->e);
431 buffer_get_bignum2(&e->request, k->rsa->d);
432 buffer_get_bignum2(&e->request, k->rsa->iqmp);
433 buffer_get_bignum2(&e->request, k->rsa->p);
434 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435
436 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000437 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 break;
439 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000440 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000441 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100442 }
Damien Millerad833b32000-08-23 10:46:23 +1000443 break;
444 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000445 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000446 if (k == NULL) {
447 xfree(comment);
448 goto send;
449 }
450 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000451 while (buffer_len(&e->request)) {
452 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000453 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000454 death = time(NULL) + buffer_get_int(&e->request);
455 break;
456 default:
457 break;
458 }
459 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000460 if (lookup_identity(k, version) == NULL) {
461 Identity *id = xmalloc(sizeof(Identity));
462 id->key = k;
463 id->comment = comment;
464 id->death = death;
465 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
466 /* Increment the number of identities. */
467 tab->nentries++;
468 } else {
469 key_free(k);
470 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000471 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000472send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000473 buffer_put_int(&e->output, 1);
474 buffer_put_char(&e->output,
475 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
476}
477
Ben Lindstrom2f717042002-06-06 21:52:03 +0000478/* XXX todo: encrypt sensitive data with passphrase */
479static void
480process_lock_agent(SocketEntry *e, int lock)
481{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000482 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000483 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000484
485 passwd = buffer_get_string(&e->request, NULL);
486 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
487 locked = 0;
488 memset(lock_passwd, 0, strlen(lock_passwd));
489 xfree(lock_passwd);
490 lock_passwd = NULL;
491 success = 1;
492 } else if (!locked && lock) {
493 locked = 1;
494 lock_passwd = xstrdup(passwd);
495 success = 1;
496 }
497 memset(passwd, 0, strlen(passwd));
498 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000499
Ben Lindstrom2f717042002-06-06 21:52:03 +0000500 buffer_put_int(&e->output, 1);
501 buffer_put_char(&e->output,
502 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000503}
504
505static void
506no_identities(SocketEntry *e, u_int type)
507{
508 Buffer msg;
509
510 buffer_init(&msg);
511 buffer_put_char(&msg,
512 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
513 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
514 buffer_put_int(&msg, 0);
515 buffer_put_int(&e->output, buffer_len(&msg));
516 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
517 buffer_free(&msg);
518}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000519
520#ifdef SMARTCARD
521static void
522process_add_smartcard_key (SocketEntry *e)
523{
Ben Lindstromba72d302002-03-22 03:51:06 +0000524 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000525 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000526 Key **keys, *k;
527 Identity *id;
528 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100529
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000530 sc_reader_id = buffer_get_string(&e->request, NULL);
531 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000532 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000533 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000534 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000535
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000536 if (keys == NULL || keys[0] == NULL) {
537 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000538 goto send;
539 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000540 for (i = 0; keys[i] != NULL; i++) {
541 k = keys[i];
542 version = k->type == KEY_RSA1 ? 1 : 2;
543 tab = idtab_lookup(version);
544 if (lookup_identity(k, version) == NULL) {
545 id = xmalloc(sizeof(Identity));
546 id->key = k;
547 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000548 id->death = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000549 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
550 tab->nentries++;
551 success = 1;
552 } else {
553 key_free(k);
554 }
555 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000556 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000557 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000558send:
559 buffer_put_int(&e->output, 1);
560 buffer_put_char(&e->output,
561 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
562}
563
564static void
565process_remove_smartcard_key(SocketEntry *e)
566{
Ben Lindstromba72d302002-03-22 03:51:06 +0000567 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000568 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000569 Key **keys, *k = NULL;
570 Identity *id;
571 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000572
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000573 sc_reader_id = buffer_get_string(&e->request, NULL);
574 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000575 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000576 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000577 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000578
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000579 if (keys == NULL || keys[0] == NULL) {
580 error("sc_get_keys failed");
581 goto send;
582 }
583 for (i = 0; keys[i] != NULL; i++) {
584 k = keys[i];
585 version = k->type == KEY_RSA1 ? 1 : 2;
586 if ((id = lookup_identity(k, version)) != NULL) {
587 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000588 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000589 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100590 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000591 success = 1;
592 }
593 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000594 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000595 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000596 xfree(keys);
597send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000598 buffer_put_int(&e->output, 1);
599 buffer_put_char(&e->output,
600 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
601}
Ben Lindstromffce1472001-08-06 21:57:31 +0000602#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000603
Damien Millerad833b32000-08-23 10:46:23 +1000604/* dispatch incoming messages */
605
Ben Lindstrombba81212001-06-25 05:01:22 +0000606static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607process_message(SocketEntry *e)
608{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000609 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000610 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000611
612 /* kill dead keys */
613 reaper();
614
Damien Miller95def091999-11-25 00:26:21 +1100615 if (buffer_len(&e->input) < 5)
616 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100617 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100618 msg_len = GET_32BIT(cp);
619 if (msg_len > 256 * 1024) {
620 shutdown(e->fd, SHUT_RDWR);
621 close(e->fd);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000622 e->fd = -1;
Damien Miller95def091999-11-25 00:26:21 +1100623 e->type = AUTH_UNUSED;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000624 buffer_free(&e->input);
625 buffer_free(&e->output);
626 buffer_free(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100627 return;
628 }
629 if (buffer_len(&e->input) < msg_len + 4)
630 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000631
632 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100633 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000634 buffer_clear(&e->request);
635 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
636 buffer_consume(&e->input, msg_len);
637 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638
Ben Lindstrom2f717042002-06-06 21:52:03 +0000639 /* check wheter agent is locked */
640 if (locked && type != SSH_AGENTC_UNLOCK) {
641 buffer_clear(&e->request);
642 switch (type) {
643 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
644 case SSH2_AGENTC_REQUEST_IDENTITIES:
645 /* send empty lists */
646 no_identities(e, type);
647 break;
648 default:
649 /* send a fail message for all other request types */
650 buffer_put_int(&e->output, 1);
651 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
652 }
653 return;
654 }
655
Ben Lindstrom3f471632001-07-04 03:53:15 +0000656 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100657 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000658 case SSH_AGENTC_LOCK:
659 case SSH_AGENTC_UNLOCK:
660 process_lock_agent(e, type == SSH_AGENTC_LOCK);
661 break;
Damien Millerad833b32000-08-23 10:46:23 +1000662 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100663 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000664 process_authentication_challenge1(e);
665 break;
666 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
667 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100668 break;
669 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000670 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000671 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100672 break;
673 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000674 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100675 break;
676 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000677 process_remove_all_identities(e, 1);
678 break;
679 /* ssh2 */
680 case SSH2_AGENTC_SIGN_REQUEST:
681 process_sign_request2(e);
682 break;
683 case SSH2_AGENTC_REQUEST_IDENTITIES:
684 process_request_identities(e, 2);
685 break;
686 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000687 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000688 process_add_identity(e, 2);
689 break;
690 case SSH2_AGENTC_REMOVE_IDENTITY:
691 process_remove_identity(e, 2);
692 break;
693 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
694 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100695 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000696#ifdef SMARTCARD
697 case SSH_AGENTC_ADD_SMARTCARD_KEY:
698 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100699 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
701 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000703#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100704 default:
705 /* Unknown message. Respond with failure. */
706 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000707 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100708 buffer_put_int(&e->output, 1);
709 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
710 break;
711 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712}
713
Ben Lindstrombba81212001-06-25 05:01:22 +0000714static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000715new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000717 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000718
Damien Miller95def091999-11-25 00:26:21 +1100719 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
720 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller95def091999-11-25 00:26:21 +1100722 if (fd > max_fd)
723 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller95def091999-11-25 00:26:21 +1100725 for (i = 0; i < sockets_alloc; i++)
726 if (sockets[i].type == AUTH_UNUSED) {
727 sockets[i].fd = fd;
728 sockets[i].type = type;
729 buffer_init(&sockets[i].input);
730 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000731 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100732 return;
733 }
734 old_alloc = sockets_alloc;
735 sockets_alloc += 10;
736 if (sockets)
737 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
738 else
739 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
740 for (i = old_alloc; i < sockets_alloc; i++)
741 sockets[i].type = AUTH_UNUSED;
742 sockets[old_alloc].type = type;
743 sockets[old_alloc].fd = fd;
744 buffer_init(&sockets[old_alloc].input);
745 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000746 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747}
748
Ben Lindstrombba81212001-06-25 05:01:22 +0000749static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000750prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000752 u_int i, sz;
753 int n = 0;
754
755 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100756 switch (sockets[i].type) {
757 case AUTH_SOCKET:
758 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000759 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100760 break;
761 case AUTH_UNUSED:
762 break;
763 default:
764 fatal("Unknown socket type %d", sockets[i].type);
765 break;
766 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000767 }
768
769 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000770 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000771 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000772 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000773 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000774 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000775 *fdrp = xmalloc(sz);
776 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000777 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000778 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000779 if (n < *fdl)
780 debug("XXX shrink: %d < %d", n, *fdl);
781 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000782 memset(*fdrp, 0, sz);
783 memset(*fdwp, 0, sz);
784
785 for (i = 0; i < sockets_alloc; i++) {
786 switch (sockets[i].type) {
787 case AUTH_SOCKET:
788 case AUTH_CONNECTION:
789 FD_SET(sockets[i].fd, *fdrp);
790 if (buffer_len(&sockets[i].output) > 0)
791 FD_SET(sockets[i].fd, *fdwp);
792 break;
793 default:
794 break;
795 }
796 }
797 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798}
799
Ben Lindstrombba81212001-06-25 05:01:22 +0000800static void
Damien Miller95def091999-11-25 00:26:21 +1100801after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000803 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100804 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100805 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000806 int len, sock;
807 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 for (i = 0; i < sockets_alloc; i++)
810 switch (sockets[i].type) {
811 case AUTH_UNUSED:
812 break;
813 case AUTH_SOCKET:
814 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100815 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000816 sock = accept(sockets[i].fd,
817 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100818 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100819 error("accept from AUTH_SOCKET: %s",
820 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100821 break;
822 }
823 new_socket(AUTH_CONNECTION, sock);
824 }
825 break;
826 case AUTH_CONNECTION:
827 if (buffer_len(&sockets[i].output) > 0 &&
828 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000829 do {
830 len = write(sockets[i].fd,
831 buffer_ptr(&sockets[i].output),
832 buffer_len(&sockets[i].output));
833 if (len == -1 && (errno == EAGAIN ||
834 errno == EINTR))
835 continue;
836 break;
837 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100838 if (len <= 0) {
839 shutdown(sockets[i].fd, SHUT_RDWR);
840 close(sockets[i].fd);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000841 sockets[i].fd = -1;
Damien Miller95def091999-11-25 00:26:21 +1100842 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000843 buffer_free(&sockets[i].input);
844 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000845 buffer_free(&sockets[i].request);
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) {
859 shutdown(sockets[i].fd, SHUT_RDWR);
860 close(sockets[i].fd);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000861 sockets[i].fd = -1;
Damien Miller95def091999-11-25 00:26:21 +1100862 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000863 buffer_free(&sockets[i].input);
864 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000865 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100866 break;
867 }
868 buffer_append(&sockets[i].input, buf, len);
869 process_message(&sockets[i]);
870 }
871 break;
872 default:
873 fatal("Unknown type %d", sockets[i].type);
874 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875}
876
Ben Lindstrombba81212001-06-25 05:01:22 +0000877static void
Damien Millerc653b892002-02-08 22:05:41 +1100878cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000879{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000880 if (socket_name[0])
881 unlink(socket_name);
882 if (socket_dir[0])
883 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884}
885
Ben Lindstrombba81212001-06-25 05:01:22 +0000886static void
Damien Miller792c5111999-10-29 09:47:09 +1000887cleanup_exit(int i)
888{
Damien Millerc653b892002-02-08 22:05:41 +1100889 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100890 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000891}
892
Ben Lindstrombba81212001-06-25 05:01:22 +0000893static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000894cleanup_handler(int sig)
895{
Damien Millerc653b892002-02-08 22:05:41 +1100896 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000897 _exit(2);
898}
899
Ben Lindstrombba81212001-06-25 05:01:22 +0000900static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000901check_parent_exists(int sig)
902{
903 int save_errno = errno;
904
905 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
906 /* printf("Parent has died - Authentication agent exiting.\n"); */
907 cleanup_handler(sig); /* safe */
908 }
909 signal(SIGALRM, check_parent_exists);
910 alarm(10);
911 errno = save_errno;
912}
913
914static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000915usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000916{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000917 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000918 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000919 fprintf(stderr, "Options:\n");
920 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
921 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
922 fprintf(stderr, " -k Kill the current agent.\n");
923 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000924 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100925 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000926}
927
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928int
929main(int ac, char **av)
930{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000931 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000932 char *shell, *format, *pidstr, *agentsocket = NULL;
933 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100934 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000935#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000936 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000937#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000938#ifdef HAVE_CYGWIN
939 int prev_mask;
940#endif
Damien Miller615f9392000-05-17 22:53:33 +1000941 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +0000942 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000943 pid_t pid;
944 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +0000945
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000946 SSLeay_add_all_algorithms();
947
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000948 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000949 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100950 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000951
Ben Lindstromb7788f32002-06-06 21:46:08 +0000952 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100953 switch (ch) {
954 case 'c':
955 if (s_flag)
956 usage();
957 c_flag++;
958 break;
959 case 'k':
960 k_flag++;
961 break;
962 case 's':
963 if (c_flag)
964 usage();
965 s_flag++;
966 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000967 case 'd':
968 if (d_flag)
969 usage();
970 d_flag++;
971 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000972 case 'a':
973 agentsocket = optarg;
974 break;
Damien Miller95def091999-11-25 00:26:21 +1100975 default:
976 usage();
977 }
Damien Miller792c5111999-10-29 09:47:09 +1000978 }
Damien Miller95def091999-11-25 00:26:21 +1100979 ac -= optind;
980 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000981
Ben Lindstromd94580c2001-07-04 03:48:02 +0000982 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100983 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000984
Ben Lindstromeecdf232002-04-02 21:03:51 +0000985 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100986 shell = getenv("SHELL");
987 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
988 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000989 }
Damien Miller95def091999-11-25 00:26:21 +1100990 if (k_flag) {
991 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
992 if (pidstr == NULL) {
993 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000994 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100995 exit(1);
996 }
997 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000998 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100999 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001000 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001001 exit(1);
1002 }
1003 if (kill(pid, SIGTERM) == -1) {
1004 perror("kill");
1005 exit(1);
1006 }
1007 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1008 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1009 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001010 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001011 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001012 }
Damien Miller95def091999-11-25 00:26:21 +11001013 parent_pid = getpid();
1014
Ben Lindstromb7788f32002-06-06 21:46:08 +00001015 if (agentsocket == NULL) {
1016 /* Create private directory for agent socket */
1017 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1018 if (mkdtemp(socket_dir) == NULL) {
1019 perror("mkdtemp: private socket dir");
1020 exit(1);
1021 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001022 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1023 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001024 } else {
1025 /* Try to use specified agent socket */
1026 socket_dir[0] = '\0';
1027 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001028 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Damien Miller5428f641999-11-25 11:54:57 +11001030 /*
1031 * Create socket early so it will exist before command gets run from
1032 * the parent.
1033 */
Damien Miller95def091999-11-25 00:26:21 +11001034 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1035 if (sock < 0) {
1036 perror("socket");
1037 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001038 }
Damien Miller95def091999-11-25 00:26:21 +11001039 memset(&sunaddr, 0, sizeof(sunaddr));
1040 sunaddr.sun_family = AF_UNIX;
1041 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001042#ifdef HAVE_CYGWIN
1043 prev_mask = umask(0177);
1044#endif
Damien Miller95def091999-11-25 00:26:21 +11001045 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1046 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001047#ifdef HAVE_CYGWIN
1048 umask(prev_mask);
1049#endif
Damien Miller95def091999-11-25 00:26:21 +11001050 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001052#ifdef HAVE_CYGWIN
1053 umask(prev_mask);
1054#endif
Damien Miller95def091999-11-25 00:26:21 +11001055 if (listen(sock, 5) < 0) {
1056 perror("listen");
1057 cleanup_exit(1);
1058 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001059
Damien Miller5428f641999-11-25 11:54:57 +11001060 /*
1061 * Fork, and have the parent execute the command, if any, or present
1062 * the socket data. The child continues as the authentication agent.
1063 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001064 if (d_flag) {
1065 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1066 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1067 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1068 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001069 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001070 goto skip;
1071 }
Damien Miller95def091999-11-25 00:26:21 +11001072 pid = fork();
1073 if (pid == -1) {
1074 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001075 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001076 }
1077 if (pid != 0) { /* Parent - execute the given command. */
1078 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001079 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001080 if (ac == 0) {
1081 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1082 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001083 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001084 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001085 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001086 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001087 exit(0);
1088 }
Damien Millere4340be2000-09-16 13:29:08 +11001089 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1090 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1091 perror("setenv");
1092 exit(1);
1093 }
Damien Miller95def091999-11-25 00:26:21 +11001094 execvp(av[0], av);
1095 perror(av[0]);
1096 exit(1);
1097 }
Damien Millerc653b892002-02-08 22:05:41 +11001098 /* child */
1099 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001100
1101 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001102 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001103 cleanup_exit(1);
1104 }
1105
1106 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001107 close(0);
1108 close(1);
1109 close(2);
1110
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001111#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001112 /* deny core dumps, since memory contains unencrypted private keys */
1113 rlim.rlim_cur = rlim.rlim_max = 0;
1114 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001115 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001116 cleanup_exit(1);
1117 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001118#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001119
1120skip:
Damien Millerc653b892002-02-08 22:05:41 +11001121 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001122 new_socket(AUTH_SOCKET, sock);
1123 if (ac > 0) {
1124 signal(SIGALRM, check_parent_exists);
1125 alarm(10);
1126 }
Damien Millerad833b32000-08-23 10:46:23 +10001127 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001128 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001129 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001130 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001131 signal(SIGHUP, cleanup_handler);
1132 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001133 nalloc = 0;
1134
Damien Miller95def091999-11-25 00:26:21 +11001135 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001136 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001137 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001138 if (errno == EINTR)
1139 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001140 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001141 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001142 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001143 }
1144 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145}