blob: cca720ee2717a03606a7884cf2c33d16fab7472a [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36#include "includes.h"
Damien Miller9b481512002-09-12 10:43:29 +100037#include "openbsd-compat/sys-queue.h"
Damien Milleraf9de382002-10-03 11:54:35 +100038RCSID("$OpenBSD: ssh-agent.c,v 1.105 2002/10/01 20:34:12 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 Miller58f34862002-09-04 16:31:21 +1000110close_socket(SocketEntry *e)
111{
Damien Miller58f34862002-09-04 16:31:21 +1000112 close(e->fd);
113 e->fd = -1;
114 e->type = AUTH_UNUSED;
115 buffer_free(&e->input);
116 buffer_free(&e->output);
117 buffer_free(&e->request);
118}
119
120static void
Damien Millerad833b32000-08-23 10:46:23 +1000121idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122{
Damien Millerad833b32000-08-23 10:46:23 +1000123 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000124
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000125 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100126 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000127 idtable[i].nentries = 0;
128 }
129}
130
131/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000133idtab_lookup(int version)
134{
135 if (version < 1 || version > 2)
136 fatal("internal error, bad protocol version %d", version);
137 return &idtable[version];
138}
139
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000140static void
141free_identity(Identity *id)
142{
143 key_free(id->key);
144 xfree(id->comment);
145 xfree(id);
146}
147
Damien Millerad833b32000-08-23 10:46:23 +1000148/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100149static Identity *
150lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000151{
Damien Miller1a534ae2002-01-22 23:26:13 +1100152 Identity *id;
153
Damien Millerad833b32000-08-23 10:46:23 +1000154 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100155 TAILQ_FOREACH(id, &tab->idlist, next) {
156 if (key_equal(key, id->key))
157 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000158 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100159 return (NULL);
160}
161
Damien Millerad833b32000-08-23 10:46:23 +1000162/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static void
Damien Millerad833b32000-08-23 10:46:23 +1000164process_request_identities(SocketEntry *e, int version)
165{
166 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100167 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000168 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller95def091999-11-25 00:26:21 +1100170 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000171 buffer_put_char(&msg, (version == 1) ?
172 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
173 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100174 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100175 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000176 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
177 buffer_put_bignum(&msg, id->key->rsa->e);
178 buffer_put_bignum(&msg, id->key->rsa->n);
179 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000180 u_char *blob;
181 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000183 buffer_put_string(&msg, blob, blen);
184 xfree(blob);
185 }
186 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100187 }
188 buffer_put_int(&e->output, buffer_len(&msg));
189 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
190 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Millerad833b32000-08-23 10:46:23 +1000193/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static void
Damien Millerad833b32000-08-23 10:46:23 +1000195process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000197 u_char buf[32], mdbuf[16], session_id[16];
198 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000199 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000200 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000201 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100202 Buffer msg;
203 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000204 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller95def091999-11-25 00:26:21 +1100206 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100208 if ((challenge = BN_new()) == NULL)
209 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000210
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000211 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000212 buffer_get_bignum(&e->request, key->rsa->e);
213 buffer_get_bignum(&e->request, key->rsa->n);
214 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Millerad833b32000-08-23 10:46:23 +1000216 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000217 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000218 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000219 buffer_get(&e->request, session_id, 16);
220 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000221 if (response_type != 1)
222 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller1a534ae2002-01-22 23:26:13 +1100224 id = lookup_identity(key, 1);
225 if (id != NULL) {
226 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000227 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100228 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
229 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100230
Damien Millerad833b32000-08-23 10:46:23 +1000231 /* The response is MD5 of decrypted challenge plus session id. */
232 len = BN_num_bytes(challenge);
233 if (len <= 0 || len > 32) {
234 log("process_authentication_challenge: bad challenge length %d", len);
235 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100236 }
Damien Millerad833b32000-08-23 10:46:23 +1000237 memset(buf, 0, 32);
238 BN_bn2bin(challenge, buf + 32 - len);
239 MD5_Init(&md);
240 MD5_Update(&md, buf, 32);
241 MD5_Update(&md, session_id, 16);
242 MD5_Final(mdbuf, &md);
243
244 /* Send the response. */
245 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
246 for (i = 0; i < 16; i++)
247 buffer_put_char(&msg, mdbuf[i]);
248 goto send;
249 }
250
251failure:
252 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100253 buffer_put_char(&msg, SSH_AGENT_FAILURE);
254send:
255 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000256 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
257 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100258 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000259 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260}
261
Damien Millerad833b32000-08-23 10:46:23 +1000262/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000263static void
Damien Millerad833b32000-08-23 10:46:23 +1000264process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000266 u_char *blob, *data, *signature = NULL;
267 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000268 extern int datafellows;
269 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000270 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000271 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000272
273 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000274
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000275 blob = buffer_get_string(&e->request, &blen);
276 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100277
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000278 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100279 if (flags & SSH_AGENT_OLD_SIGNATURE)
280 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000281
Damien Miller0bc1bd82000-11-13 22:57:25 +1100282 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000283 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100284 Identity *id = lookup_identity(key, 2);
285 if (id != NULL)
286 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000287 }
288 key_free(key);
289 buffer_init(&msg);
290 if (ok == 0) {
291 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
292 buffer_put_string(&msg, signature, slen);
293 } else {
294 buffer_put_char(&msg, SSH_AGENT_FAILURE);
295 }
296 buffer_put_int(&e->output, buffer_len(&msg));
297 buffer_append(&e->output, buffer_ptr(&msg),
298 buffer_len(&msg));
299 buffer_free(&msg);
300 xfree(data);
301 xfree(blob);
302 if (signature != NULL)
303 xfree(signature);
304}
305
306/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000307static void
Damien Millerad833b32000-08-23 10:46:23 +1000308process_remove_identity(SocketEntry *e, int version)
309{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000310 u_int blen, bits;
311 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100312 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000313 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100314
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000315 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000316 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100317 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000318 bits = buffer_get_int(&e->request);
319 buffer_get_bignum(&e->request, key->rsa->e);
320 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Damien Millerad833b32000-08-23 10:46:23 +1000322 if (bits != key_size(key))
Ben Lindstrom822b6342002-06-23 21:38:49 +0000323 log("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000324 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000325 break;
326 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000327 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100328 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000329 xfree(blob);
330 break;
331 }
332 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100333 Identity *id = lookup_identity(key, version);
334 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100335 /*
336 * We have this key. Free the old key. Since we
337 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000338 * the array, we actually free the key there and move
339 * all the entries between the empty slot and the end
340 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100341 */
Damien Millerad833b32000-08-23 10:46:23 +1000342 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100343 if (tab->nentries < 1)
344 fatal("process_remove_identity: "
345 "internal error: tab->nentries %d",
346 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100347 TAILQ_REMOVE(&tab->idlist, id, next);
348 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000349 tab->nentries--;
350 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100351 }
Damien Millerad833b32000-08-23 10:46:23 +1000352 key_free(key);
353 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000355 buffer_put_char(&e->output,
356 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
Ben Lindstrombba81212001-06-25 05:01:22 +0000359static void
Damien Millerad833b32000-08-23 10:46:23 +1000360process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361{
Damien Millerad833b32000-08-23 10:46:23 +1000362 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100363 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
Damien Miller95def091999-11-25 00:26:21 +1100365 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100366 for (id = TAILQ_FIRST(&tab->idlist); id;
367 id = TAILQ_FIRST(&tab->idlist)) {
368 TAILQ_REMOVE(&tab->idlist, id, next);
369 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100370 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Damien Miller95def091999-11-25 00:26:21 +1100372 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000373 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
375 /* Send success. */
376 buffer_put_int(&e->output, 1);
377 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100378}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000381reaper(void)
382{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000383 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000384 Identity *id, *nxt;
385 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000386 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000387
388 for (version = 1; version < 3; version++) {
389 tab = idtab_lookup(version);
390 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
391 nxt = TAILQ_NEXT(id, next);
392 if (id->death != 0 && now >= id->death) {
393 TAILQ_REMOVE(&tab->idlist, id, next);
394 free_identity(id);
395 tab->nentries--;
396 }
397 }
398 }
399}
400
401static void
Damien Millerad833b32000-08-23 10:46:23 +1000402process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100403{
Damien Millerad833b32000-08-23 10:46:23 +1000404 Idtab *tab = idtab_lookup(version);
Ben Lindstrom822b6342002-06-23 21:38:49 +0000405 int type, success = 0, death = 0;
406 char *type_name, *comment;
407 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100408
Damien Millerad833b32000-08-23 10:46:23 +1000409 switch (version) {
410 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100411 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000412 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000413 buffer_get_bignum(&e->request, k->rsa->n);
414 buffer_get_bignum(&e->request, k->rsa->e);
415 buffer_get_bignum(&e->request, k->rsa->d);
416 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100417
Damien Millerad833b32000-08-23 10:46:23 +1000418 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000419 buffer_get_bignum(&e->request, k->rsa->q); /* p */
420 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100421
Damien Millerad833b32000-08-23 10:46:23 +1000422 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000423 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000424 break;
425 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000426 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000427 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100428 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000429 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100430 case KEY_DSA:
431 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000432 buffer_get_bignum2(&e->request, k->dsa->p);
433 buffer_get_bignum2(&e->request, k->dsa->q);
434 buffer_get_bignum2(&e->request, k->dsa->g);
435 buffer_get_bignum2(&e->request, k->dsa->pub_key);
436 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100437 break;
438 case KEY_RSA:
439 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000440 buffer_get_bignum2(&e->request, k->rsa->n);
441 buffer_get_bignum2(&e->request, k->rsa->e);
442 buffer_get_bignum2(&e->request, k->rsa->d);
443 buffer_get_bignum2(&e->request, k->rsa->iqmp);
444 buffer_get_bignum2(&e->request, k->rsa->p);
445 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100446
447 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000448 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449 break;
450 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000451 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000452 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100453 }
Damien Millerad833b32000-08-23 10:46:23 +1000454 break;
455 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000457 if (k == NULL) {
458 xfree(comment);
459 goto send;
460 }
461 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000462 while (buffer_len(&e->request)) {
463 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000464 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000465 death = time(NULL) + buffer_get_int(&e->request);
466 break;
467 default:
468 break;
469 }
470 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000471 if (lookup_identity(k, version) == NULL) {
472 Identity *id = xmalloc(sizeof(Identity));
473 id->key = k;
474 id->comment = comment;
475 id->death = death;
476 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
477 /* Increment the number of identities. */
478 tab->nentries++;
479 } else {
480 key_free(k);
481 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000482 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000483send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000484 buffer_put_int(&e->output, 1);
485 buffer_put_char(&e->output,
486 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
487}
488
Ben Lindstrom2f717042002-06-06 21:52:03 +0000489/* XXX todo: encrypt sensitive data with passphrase */
490static void
491process_lock_agent(SocketEntry *e, int lock)
492{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000493 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000494 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000495
496 passwd = buffer_get_string(&e->request, NULL);
497 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
498 locked = 0;
499 memset(lock_passwd, 0, strlen(lock_passwd));
500 xfree(lock_passwd);
501 lock_passwd = NULL;
502 success = 1;
503 } else if (!locked && lock) {
504 locked = 1;
505 lock_passwd = xstrdup(passwd);
506 success = 1;
507 }
508 memset(passwd, 0, strlen(passwd));
509 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000510
Ben Lindstrom2f717042002-06-06 21:52:03 +0000511 buffer_put_int(&e->output, 1);
512 buffer_put_char(&e->output,
513 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000514}
515
516static void
517no_identities(SocketEntry *e, u_int type)
518{
519 Buffer msg;
520
521 buffer_init(&msg);
522 buffer_put_char(&msg,
523 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
524 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
525 buffer_put_int(&msg, 0);
526 buffer_put_int(&e->output, buffer_len(&msg));
527 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
528 buffer_free(&msg);
529}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000530
531#ifdef SMARTCARD
532static void
533process_add_smartcard_key (SocketEntry *e)
534{
Ben Lindstromba72d302002-03-22 03:51:06 +0000535 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000536 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000537 Key **keys, *k;
538 Identity *id;
539 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100540
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000541 sc_reader_id = buffer_get_string(&e->request, NULL);
542 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000543 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000544 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000545 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000546
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000547 if (keys == NULL || keys[0] == NULL) {
548 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000549 goto send;
550 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000551 for (i = 0; keys[i] != NULL; i++) {
552 k = keys[i];
553 version = k->type == KEY_RSA1 ? 1 : 2;
554 tab = idtab_lookup(version);
555 if (lookup_identity(k, version) == NULL) {
556 id = xmalloc(sizeof(Identity));
557 id->key = k;
558 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000559 id->death = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000560 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
561 tab->nentries++;
562 success = 1;
563 } else {
564 key_free(k);
565 }
566 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000567 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000568 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000569send:
570 buffer_put_int(&e->output, 1);
571 buffer_put_char(&e->output,
572 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
573}
574
575static void
576process_remove_smartcard_key(SocketEntry *e)
577{
Ben Lindstromba72d302002-03-22 03:51:06 +0000578 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000579 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000580 Key **keys, *k = NULL;
581 Identity *id;
582 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000583
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000584 sc_reader_id = buffer_get_string(&e->request, NULL);
585 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000586 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000587 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000588 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000589
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000590 if (keys == NULL || keys[0] == NULL) {
591 error("sc_get_keys failed");
592 goto send;
593 }
594 for (i = 0; keys[i] != NULL; i++) {
595 k = keys[i];
596 version = k->type == KEY_RSA1 ? 1 : 2;
597 if ((id = lookup_identity(k, version)) != NULL) {
598 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000599 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000600 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100601 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000602 success = 1;
603 }
604 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000605 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000606 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000607 xfree(keys);
608send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000609 buffer_put_int(&e->output, 1);
610 buffer_put_char(&e->output,
611 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
612}
Ben Lindstromffce1472001-08-06 21:57:31 +0000613#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000614
Damien Millerad833b32000-08-23 10:46:23 +1000615/* dispatch incoming messages */
616
Ben Lindstrombba81212001-06-25 05:01:22 +0000617static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618process_message(SocketEntry *e)
619{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000620 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000621 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000622
623 /* kill dead keys */
624 reaper();
625
Damien Miller95def091999-11-25 00:26:21 +1100626 if (buffer_len(&e->input) < 5)
627 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100628 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100629 msg_len = GET_32BIT(cp);
630 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000631 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100632 return;
633 }
634 if (buffer_len(&e->input) < msg_len + 4)
635 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000636
637 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100638 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000639 buffer_clear(&e->request);
640 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
641 buffer_consume(&e->input, msg_len);
642 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Ben Lindstrom2f717042002-06-06 21:52:03 +0000644 /* check wheter agent is locked */
645 if (locked && type != SSH_AGENTC_UNLOCK) {
646 buffer_clear(&e->request);
647 switch (type) {
648 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
649 case SSH2_AGENTC_REQUEST_IDENTITIES:
650 /* send empty lists */
651 no_identities(e, type);
652 break;
653 default:
654 /* send a fail message for all other request types */
655 buffer_put_int(&e->output, 1);
656 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
657 }
658 return;
659 }
660
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100662 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000663 case SSH_AGENTC_LOCK:
664 case SSH_AGENTC_UNLOCK:
665 process_lock_agent(e, type == SSH_AGENTC_LOCK);
666 break;
Damien Millerad833b32000-08-23 10:46:23 +1000667 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100668 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000669 process_authentication_challenge1(e);
670 break;
671 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
672 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100673 break;
674 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000675 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000676 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100677 break;
678 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000679 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100680 break;
681 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000682 process_remove_all_identities(e, 1);
683 break;
684 /* ssh2 */
685 case SSH2_AGENTC_SIGN_REQUEST:
686 process_sign_request2(e);
687 break;
688 case SSH2_AGENTC_REQUEST_IDENTITIES:
689 process_request_identities(e, 2);
690 break;
691 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000692 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000693 process_add_identity(e, 2);
694 break;
695 case SSH2_AGENTC_REMOVE_IDENTITY:
696 process_remove_identity(e, 2);
697 break;
698 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
699 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100700 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000701#ifdef SMARTCARD
702 case SSH_AGENTC_ADD_SMARTCARD_KEY:
703 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100704 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000705 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
706 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100707 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000708#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100709 default:
710 /* Unknown message. Respond with failure. */
711 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000712 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100713 buffer_put_int(&e->output, 1);
714 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
715 break;
716 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717}
718
Ben Lindstrombba81212001-06-25 05:01:22 +0000719static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000720new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000722 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000723
Damien Miller95def091999-11-25 00:26:21 +1100724 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
725 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 if (fd > max_fd)
728 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller95def091999-11-25 00:26:21 +1100730 for (i = 0; i < sockets_alloc; i++)
731 if (sockets[i].type == AUTH_UNUSED) {
732 sockets[i].fd = fd;
733 sockets[i].type = type;
734 buffer_init(&sockets[i].input);
735 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000736 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100737 return;
738 }
739 old_alloc = sockets_alloc;
740 sockets_alloc += 10;
741 if (sockets)
742 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
743 else
744 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
745 for (i = old_alloc; i < sockets_alloc; i++)
746 sockets[i].type = AUTH_UNUSED;
747 sockets[old_alloc].type = type;
748 sockets[old_alloc].fd = fd;
749 buffer_init(&sockets[old_alloc].input);
750 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000751 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752}
753
Ben Lindstrombba81212001-06-25 05:01:22 +0000754static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000755prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000757 u_int i, sz;
758 int n = 0;
759
760 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100761 switch (sockets[i].type) {
762 case AUTH_SOCKET:
763 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000764 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100765 break;
766 case AUTH_UNUSED:
767 break;
768 default:
769 fatal("Unknown socket type %d", sockets[i].type);
770 break;
771 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000772 }
773
774 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000775 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000776 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000777 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000778 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000779 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000780 *fdrp = xmalloc(sz);
781 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000782 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000783 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000784 if (n < *fdl)
785 debug("XXX shrink: %d < %d", n, *fdl);
786 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000787 memset(*fdrp, 0, sz);
788 memset(*fdwp, 0, sz);
789
790 for (i = 0; i < sockets_alloc; i++) {
791 switch (sockets[i].type) {
792 case AUTH_SOCKET:
793 case AUTH_CONNECTION:
794 FD_SET(sockets[i].fd, *fdrp);
795 if (buffer_len(&sockets[i].output) > 0)
796 FD_SET(sockets[i].fd, *fdwp);
797 break;
798 default:
799 break;
800 }
801 }
802 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
804
Ben Lindstrombba81212001-06-25 05:01:22 +0000805static void
Damien Miller95def091999-11-25 00:26:21 +1100806after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000808 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100809 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100810 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000811 int len, sock;
812 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000813 uid_t euid;
814 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815
Damien Miller95def091999-11-25 00:26:21 +1100816 for (i = 0; i < sockets_alloc; i++)
817 switch (sockets[i].type) {
818 case AUTH_UNUSED:
819 break;
820 case AUTH_SOCKET:
821 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100822 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000823 sock = accept(sockets[i].fd,
824 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100825 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100826 error("accept from AUTH_SOCKET: %s",
827 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100828 break;
829 }
Damien Miller538f1812002-09-12 09:51:10 +1000830 if (getpeereid(sock, &euid, &egid) < 0) {
831 error("getpeereid %d failed: %s",
832 sock, strerror(errno));
833 close(sock);
834 break;
835 }
Damien Milleraf9de382002-10-03 11:54:35 +1000836 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000837 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000838 "peer euid %u != uid %u",
839 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000840 close(sock);
841 break;
842 }
Damien Miller95def091999-11-25 00:26:21 +1100843 new_socket(AUTH_CONNECTION, sock);
844 }
845 break;
846 case AUTH_CONNECTION:
847 if (buffer_len(&sockets[i].output) > 0 &&
848 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000849 do {
850 len = write(sockets[i].fd,
851 buffer_ptr(&sockets[i].output),
852 buffer_len(&sockets[i].output));
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_consume(&sockets[i].output, len);
863 }
864 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000865 do {
866 len = read(sockets[i].fd, buf, sizeof(buf));
867 if (len == -1 && (errno == EAGAIN ||
868 errno == EINTR))
869 continue;
870 break;
871 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100872 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000873 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100874 break;
875 }
876 buffer_append(&sockets[i].input, buf, len);
877 process_message(&sockets[i]);
878 }
879 break;
880 default:
881 fatal("Unknown type %d", sockets[i].type);
882 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883}
884
Ben Lindstrombba81212001-06-25 05:01:22 +0000885static void
Damien Millerc653b892002-02-08 22:05:41 +1100886cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000887{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000888 if (socket_name[0])
889 unlink(socket_name);
890 if (socket_dir[0])
891 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892}
893
Ben Lindstrombba81212001-06-25 05:01:22 +0000894static void
Damien Miller792c5111999-10-29 09:47:09 +1000895cleanup_exit(int i)
896{
Damien Millerc653b892002-02-08 22:05:41 +1100897 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100898 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000902cleanup_handler(int sig)
903{
Damien Millerc653b892002-02-08 22:05:41 +1100904 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000905 _exit(2);
906}
907
Ben Lindstrombba81212001-06-25 05:01:22 +0000908static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000909check_parent_exists(int sig)
910{
911 int save_errno = errno;
912
913 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
914 /* printf("Parent has died - Authentication agent exiting.\n"); */
915 cleanup_handler(sig); /* safe */
916 }
917 signal(SIGALRM, check_parent_exists);
918 alarm(10);
919 errno = save_errno;
920}
921
922static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000923usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000924{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000925 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000926 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000927 fprintf(stderr, "Options:\n");
928 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
929 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
930 fprintf(stderr, " -k Kill the current agent.\n");
931 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000932 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100933 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000934}
935
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936int
937main(int ac, char **av)
938{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000939 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000940 char *shell, *format, *pidstr, *agentsocket = NULL;
941 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100942 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000943#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000944 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000945#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000946#ifdef HAVE_CYGWIN
947 int prev_mask;
948#endif
Damien Miller615f9392000-05-17 22:53:33 +1000949 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +0000950 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000951 pid_t pid;
952 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +0000953
Damien Miller6cffb9a2002-09-04 16:20:26 +1000954 /* drop */
955 setegid(getgid());
956 setgid(getgid());
957
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000958 SSLeay_add_all_algorithms();
959
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000960 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000961 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100962 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000963
Ben Lindstromb7788f32002-06-06 21:46:08 +0000964 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100965 switch (ch) {
966 case 'c':
967 if (s_flag)
968 usage();
969 c_flag++;
970 break;
971 case 'k':
972 k_flag++;
973 break;
974 case 's':
975 if (c_flag)
976 usage();
977 s_flag++;
978 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000979 case 'd':
980 if (d_flag)
981 usage();
982 d_flag++;
983 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000984 case 'a':
985 agentsocket = optarg;
986 break;
Damien Miller95def091999-11-25 00:26:21 +1100987 default:
988 usage();
989 }
Damien Miller792c5111999-10-29 09:47:09 +1000990 }
Damien Miller95def091999-11-25 00:26:21 +1100991 ac -= optind;
992 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000993
Ben Lindstromd94580c2001-07-04 03:48:02 +0000994 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100995 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000996
Ben Lindstromeecdf232002-04-02 21:03:51 +0000997 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100998 shell = getenv("SHELL");
999 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1000 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001001 }
Damien Miller95def091999-11-25 00:26:21 +11001002 if (k_flag) {
1003 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1004 if (pidstr == NULL) {
1005 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001006 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001007 exit(1);
1008 }
1009 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001010 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001011 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001012 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001013 exit(1);
1014 }
1015 if (kill(pid, SIGTERM) == -1) {
1016 perror("kill");
1017 exit(1);
1018 }
1019 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1020 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1021 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001022 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001023 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001024 }
Damien Miller95def091999-11-25 00:26:21 +11001025 parent_pid = getpid();
1026
Ben Lindstromb7788f32002-06-06 21:46:08 +00001027 if (agentsocket == NULL) {
1028 /* Create private directory for agent socket */
1029 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1030 if (mkdtemp(socket_dir) == NULL) {
1031 perror("mkdtemp: private socket dir");
1032 exit(1);
1033 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001034 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1035 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001036 } else {
1037 /* Try to use specified agent socket */
1038 socket_dir[0] = '\0';
1039 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001040 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller5428f641999-11-25 11:54:57 +11001042 /*
1043 * Create socket early so it will exist before command gets run from
1044 * the parent.
1045 */
Damien Miller95def091999-11-25 00:26:21 +11001046 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1047 if (sock < 0) {
1048 perror("socket");
1049 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001050 }
Damien Miller95def091999-11-25 00:26:21 +11001051 memset(&sunaddr, 0, sizeof(sunaddr));
1052 sunaddr.sun_family = AF_UNIX;
1053 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001054#ifdef HAVE_CYGWIN
1055 prev_mask = umask(0177);
1056#endif
Damien Miller95def091999-11-25 00:26:21 +11001057 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1058 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001059#ifdef HAVE_CYGWIN
1060 umask(prev_mask);
1061#endif
Damien Miller95def091999-11-25 00:26:21 +11001062 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001064#ifdef HAVE_CYGWIN
1065 umask(prev_mask);
1066#endif
Damien Miller4efdfff2002-09-04 16:28:18 +10001067 if (listen(sock, 128) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001068 perror("listen");
1069 cleanup_exit(1);
1070 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001071
Damien Miller5428f641999-11-25 11:54:57 +11001072 /*
1073 * Fork, and have the parent execute the command, if any, or present
1074 * the socket data. The child continues as the authentication agent.
1075 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001076 if (d_flag) {
1077 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1078 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1079 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1080 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001081 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001082 goto skip;
1083 }
Damien Miller95def091999-11-25 00:26:21 +11001084 pid = fork();
1085 if (pid == -1) {
1086 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001087 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001088 }
1089 if (pid != 0) { /* Parent - execute the given command. */
1090 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001091 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001092 if (ac == 0) {
1093 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1094 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001095 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001096 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001097 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001098 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001099 exit(0);
1100 }
Damien Millere4340be2000-09-16 13:29:08 +11001101 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1102 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1103 perror("setenv");
1104 exit(1);
1105 }
Damien Miller95def091999-11-25 00:26:21 +11001106 execvp(av[0], av);
1107 perror(av[0]);
1108 exit(1);
1109 }
Damien Millerc653b892002-02-08 22:05:41 +11001110 /* child */
1111 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001112
1113 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001114 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001115 cleanup_exit(1);
1116 }
1117
1118 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001119 close(0);
1120 close(1);
1121 close(2);
1122
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001123#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001124 /* deny core dumps, since memory contains unencrypted private keys */
1125 rlim.rlim_cur = rlim.rlim_max = 0;
1126 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001127 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001128 cleanup_exit(1);
1129 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001130#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001131
1132skip:
Damien Millerc653b892002-02-08 22:05:41 +11001133 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001134 new_socket(AUTH_SOCKET, sock);
1135 if (ac > 0) {
1136 signal(SIGALRM, check_parent_exists);
1137 alarm(10);
1138 }
Damien Millerad833b32000-08-23 10:46:23 +10001139 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001140 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001141 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001142 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001143 signal(SIGHUP, cleanup_handler);
1144 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001145 nalloc = 0;
1146
Damien Miller95def091999-11-25 00:26:21 +11001147 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001148 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001149 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001150 if (errno == EINTR)
1151 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001152 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001153 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001154 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001155 }
1156 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157}