blob: 536db2de00fadca267c5eb436767a85c07ac363a [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 Lindstrom2b266b72002-06-21 00:08:39 +000038RCSID("$OpenBSD: ssh-agent.c,v 1.94 2002/06/15 01:27:48 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 Lindstrom1c37c6a2001-12-06 18:00:18 +0000113 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100114 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000115 idtable[i].nentries = 0;
116 }
117}
118
119/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000121idtab_lookup(int version)
122{
123 if (version < 1 || version > 2)
124 fatal("internal error, bad protocol version %d", version);
125 return &idtable[version];
126}
127
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000128static void
129free_identity(Identity *id)
130{
131 key_free(id->key);
132 xfree(id->comment);
133 xfree(id);
134}
135
Damien Millerad833b32000-08-23 10:46:23 +1000136/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100137static Identity *
138lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000139{
Damien Miller1a534ae2002-01-22 23:26:13 +1100140 Identity *id;
141
Damien Millerad833b32000-08-23 10:46:23 +1000142 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100143 TAILQ_FOREACH(id, &tab->idlist, next) {
144 if (key_equal(key, id->key))
145 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000146 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100147 return (NULL);
148}
149
Damien Millerad833b32000-08-23 10:46:23 +1000150/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Millerad833b32000-08-23 10:46:23 +1000152process_request_identities(SocketEntry *e, int version)
153{
154 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100155 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000159 buffer_put_char(&msg, (version == 1) ?
160 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
161 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100162 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000164 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
165 buffer_put_bignum(&msg, id->key->rsa->e);
166 buffer_put_bignum(&msg, id->key->rsa->n);
167 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000168 u_char *blob;
169 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000171 buffer_put_string(&msg, blob, blen);
172 xfree(blob);
173 }
174 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100175 }
176 buffer_put_int(&e->output, buffer_len(&msg));
177 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
178 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Damien Millerad833b32000-08-23 10:46:23 +1000181/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static void
Damien Millerad833b32000-08-23 10:46:23 +1000183process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 Identity *id;
186 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000187 BIGNUM *challenge;
188 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100189 Buffer msg;
190 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000191 u_char buf[32], mdbuf[16], session_id[16];
192 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Miller95def091999-11-25 00:26:21 +1100194 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100195 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100196 if ((challenge = BN_new()) == NULL)
197 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000198
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000199 buffer_get_int(&e->request); /* ignored */
200 buffer_get_bignum(&e->request, key->rsa->e);
201 buffer_get_bignum(&e->request, key->rsa->n);
202 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Millerad833b32000-08-23 10:46:23 +1000204 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000205 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000206 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000207 buffer_get(&e->request, session_id, 16);
208 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000209 if (response_type != 1)
210 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller1a534ae2002-01-22 23:26:13 +1100212 id = lookup_identity(key, 1);
213 if (id != NULL) {
214 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000215 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100216 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
217 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100218
Damien Millerad833b32000-08-23 10:46:23 +1000219 /* The response is MD5 of decrypted challenge plus session id. */
220 len = BN_num_bytes(challenge);
221 if (len <= 0 || len > 32) {
222 log("process_authentication_challenge: bad challenge length %d", len);
223 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100224 }
Damien Millerad833b32000-08-23 10:46:23 +1000225 memset(buf, 0, 32);
226 BN_bn2bin(challenge, buf + 32 - len);
227 MD5_Init(&md);
228 MD5_Update(&md, buf, 32);
229 MD5_Update(&md, session_id, 16);
230 MD5_Final(mdbuf, &md);
231
232 /* Send the response. */
233 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
234 for (i = 0; i < 16; i++)
235 buffer_put_char(&msg, mdbuf[i]);
236 goto send;
237 }
238
239failure:
240 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100241 buffer_put_char(&msg, SSH_AGENT_FAILURE);
242send:
243 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000244 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
245 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100246 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000247 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248}
249
Damien Millerad833b32000-08-23 10:46:23 +1000250/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000251static void
Damien Millerad833b32000-08-23 10:46:23 +1000252process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Damien Millerad833b32000-08-23 10:46:23 +1000254 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100255 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000256 u_char *blob, *data, *signature = NULL;
257 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100258 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000259 Buffer msg;
260 int ok = -1;
261
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{
Damien Miller1a534ae2002-01-22 23:26:13 +1100299 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_char *blob;
301 u_int blen;
302 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000303 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100304
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000305 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000306 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100307 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000308 bits = buffer_get_int(&e->request);
309 buffer_get_bignum(&e->request, key->rsa->e);
310 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Millerad833b32000-08-23 10:46:23 +1000312 if (bits != key_size(key))
313 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000314 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000315 break;
316 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000317 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000319 xfree(blob);
320 break;
321 }
322 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100323 Identity *id = lookup_identity(key, version);
324 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100325 /*
326 * We have this key. Free the old key. Since we
327 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000328 * the array, we actually free the key there and move
329 * all the entries between the empty slot and the end
330 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100331 */
Damien Millerad833b32000-08-23 10:46:23 +1000332 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333 if (tab->nentries < 1)
334 fatal("process_remove_identity: "
335 "internal error: tab->nentries %d",
336 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100337 TAILQ_REMOVE(&tab->idlist, id, next);
338 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000339 tab->nentries--;
340 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100341 }
Damien Millerad833b32000-08-23 10:46:23 +1000342 key_free(key);
343 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000345 buffer_put_char(&e->output,
346 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static void
Damien Millerad833b32000-08-23 10:46:23 +1000350process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Damien Millerad833b32000-08-23 10:46:23 +1000352 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100353 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100356 for (id = TAILQ_FIRST(&tab->idlist); id;
357 id = TAILQ_FIRST(&tab->idlist)) {
358 TAILQ_REMOVE(&tab->idlist, id, next);
359 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Damien Miller95def091999-11-25 00:26:21 +1100362 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000363 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
365 /* Send success. */
366 buffer_put_int(&e->output, 1);
367 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
368 return;
Damien Miller95def091999-11-25 00:26:21 +1100369}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000372reaper(void)
373{
374 Idtab *tab;
375 Identity *id, *nxt;
376 int version;
377 u_int now = time(NULL);
378
379 for (version = 1; version < 3; version++) {
380 tab = idtab_lookup(version);
381 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
382 nxt = TAILQ_NEXT(id, next);
383 if (id->death != 0 && now >= id->death) {
384 TAILQ_REMOVE(&tab->idlist, id, next);
385 free_identity(id);
386 tab->nentries--;
387 }
388 }
389 }
390}
391
392static void
Damien Millerad833b32000-08-23 10:46:23 +1000393process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100394{
Damien Millerad833b32000-08-23 10:46:23 +1000395 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000397 char *comment;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000398 int type, success = 0, death = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000399 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100400
Damien Millerad833b32000-08-23 10:46:23 +1000401 switch (version) {
402 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100403 k = key_new_private(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000404 buffer_get_int(&e->request); /* ignored */
405 buffer_get_bignum(&e->request, k->rsa->n);
406 buffer_get_bignum(&e->request, k->rsa->e);
407 buffer_get_bignum(&e->request, k->rsa->d);
408 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100409
Damien Millerad833b32000-08-23 10:46:23 +1000410 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000411 buffer_get_bignum(&e->request, k->rsa->q); /* p */
412 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100413
Damien Millerad833b32000-08-23 10:46:23 +1000414 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000415 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000416 break;
417 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000418 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000419 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100420 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000421 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100422 case KEY_DSA:
423 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000424 buffer_get_bignum2(&e->request, k->dsa->p);
425 buffer_get_bignum2(&e->request, k->dsa->q);
426 buffer_get_bignum2(&e->request, k->dsa->g);
427 buffer_get_bignum2(&e->request, k->dsa->pub_key);
428 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100429 break;
430 case KEY_RSA:
431 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000432 buffer_get_bignum2(&e->request, k->rsa->n);
433 buffer_get_bignum2(&e->request, k->rsa->e);
434 buffer_get_bignum2(&e->request, k->rsa->d);
435 buffer_get_bignum2(&e->request, k->rsa->iqmp);
436 buffer_get_bignum2(&e->request, k->rsa->p);
437 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438
439 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000440 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 break;
442 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000443 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000444 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100445 }
Damien Millerad833b32000-08-23 10:46:23 +1000446 break;
447 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000448 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000449 if (k == NULL) {
450 xfree(comment);
451 goto send;
452 }
453 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000454 while (buffer_len(&e->request)) {
455 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000456 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000457 death = time(NULL) + buffer_get_int(&e->request);
458 break;
459 default:
460 break;
461 }
462 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000463 if (lookup_identity(k, version) == NULL) {
464 Identity *id = xmalloc(sizeof(Identity));
465 id->key = k;
466 id->comment = comment;
467 id->death = death;
468 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
469 /* Increment the number of identities. */
470 tab->nentries++;
471 } else {
472 key_free(k);
473 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000474 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000475send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000476 buffer_put_int(&e->output, 1);
477 buffer_put_char(&e->output,
478 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
479}
480
Ben Lindstrom2f717042002-06-06 21:52:03 +0000481/* XXX todo: encrypt sensitive data with passphrase */
482static void
483process_lock_agent(SocketEntry *e, int lock)
484{
485 char *passwd;
486 int success = 0;
487
488 passwd = buffer_get_string(&e->request, NULL);
489 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
490 locked = 0;
491 memset(lock_passwd, 0, strlen(lock_passwd));
492 xfree(lock_passwd);
493 lock_passwd = NULL;
494 success = 1;
495 } else if (!locked && lock) {
496 locked = 1;
497 lock_passwd = xstrdup(passwd);
498 success = 1;
499 }
500 memset(passwd, 0, strlen(passwd));
501 xfree(passwd);
502
503 buffer_put_int(&e->output, 1);
504 buffer_put_char(&e->output,
505 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
506 return;
507}
508
509static void
510no_identities(SocketEntry *e, u_int type)
511{
512 Buffer msg;
513
514 buffer_init(&msg);
515 buffer_put_char(&msg,
516 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
517 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
518 buffer_put_int(&msg, 0);
519 buffer_put_int(&e->output, buffer_len(&msg));
520 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
521 buffer_free(&msg);
522}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000523
524#ifdef SMARTCARD
525static void
526process_add_smartcard_key (SocketEntry *e)
527{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000528 Identity *id;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000529 Idtab *tab;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000530 Key **keys, *k;
Ben Lindstromba72d302002-03-22 03:51:06 +0000531 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000532 int i, version, success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100533
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000534 sc_reader_id = buffer_get_string(&e->request, NULL);
535 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000536 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000537 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000538 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000539
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000540 if (keys == NULL || keys[0] == NULL) {
541 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000542 goto send;
543 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000544 for (i = 0; keys[i] != NULL; i++) {
545 k = keys[i];
546 version = k->type == KEY_RSA1 ? 1 : 2;
547 tab = idtab_lookup(version);
548 if (lookup_identity(k, version) == NULL) {
549 id = xmalloc(sizeof(Identity));
550 id->key = k;
551 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000552 id->death = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000553 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
554 tab->nentries++;
555 success = 1;
556 } else {
557 key_free(k);
558 }
559 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000560 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000561 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000562send:
563 buffer_put_int(&e->output, 1);
564 buffer_put_char(&e->output,
565 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
566}
567
568static void
569process_remove_smartcard_key(SocketEntry *e)
570{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000571 Identity *id;
572 Idtab *tab;
573 Key **keys, *k = NULL;
Ben Lindstromba72d302002-03-22 03:51:06 +0000574 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000575 int i, version, success = 0;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000576
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000577 sc_reader_id = buffer_get_string(&e->request, NULL);
578 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000579 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000580 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000581 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000582
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000583 if (keys == NULL || keys[0] == NULL) {
584 error("sc_get_keys failed");
585 goto send;
586 }
587 for (i = 0; keys[i] != NULL; i++) {
588 k = keys[i];
589 version = k->type == KEY_RSA1 ? 1 : 2;
590 if ((id = lookup_identity(k, version)) != NULL) {
591 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000592 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000593 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100594 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000595 success = 1;
596 }
597 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000598 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000599 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000600 xfree(keys);
601send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000602 buffer_put_int(&e->output, 1);
603 buffer_put_char(&e->output,
604 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
605}
Ben Lindstromffce1472001-08-06 21:57:31 +0000606#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607
Damien Millerad833b32000-08-23 10:46:23 +1000608/* dispatch incoming messages */
609
Ben Lindstrombba81212001-06-25 05:01:22 +0000610static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611process_message(SocketEntry *e)
612{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000613 u_int msg_len;
614 u_int type;
615 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000616
617 /* kill dead keys */
618 reaper();
619
Damien Miller95def091999-11-25 00:26:21 +1100620 if (buffer_len(&e->input) < 5)
621 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100622 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100623 msg_len = GET_32BIT(cp);
624 if (msg_len > 256 * 1024) {
625 shutdown(e->fd, SHUT_RDWR);
626 close(e->fd);
627 e->type = AUTH_UNUSED;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000628 buffer_free(&e->input);
629 buffer_free(&e->output);
630 buffer_free(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100631 return;
632 }
633 if (buffer_len(&e->input) < msg_len + 4)
634 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000635
636 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100637 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000638 buffer_clear(&e->request);
639 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
640 buffer_consume(&e->input, msg_len);
641 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Ben Lindstrom2f717042002-06-06 21:52:03 +0000643 /* check wheter agent is locked */
644 if (locked && type != SSH_AGENTC_UNLOCK) {
645 buffer_clear(&e->request);
646 switch (type) {
647 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
648 case SSH2_AGENTC_REQUEST_IDENTITIES:
649 /* send empty lists */
650 no_identities(e, type);
651 break;
652 default:
653 /* send a fail message for all other request types */
654 buffer_put_int(&e->output, 1);
655 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
656 }
657 return;
658 }
659
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100661 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000662 case SSH_AGENTC_LOCK:
663 case SSH_AGENTC_UNLOCK:
664 process_lock_agent(e, type == SSH_AGENTC_LOCK);
665 break;
Damien Millerad833b32000-08-23 10:46:23 +1000666 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100667 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000668 process_authentication_challenge1(e);
669 break;
670 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
671 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100672 break;
673 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000674 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000675 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100676 break;
677 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000678 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100679 break;
680 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000681 process_remove_all_identities(e, 1);
682 break;
683 /* ssh2 */
684 case SSH2_AGENTC_SIGN_REQUEST:
685 process_sign_request2(e);
686 break;
687 case SSH2_AGENTC_REQUEST_IDENTITIES:
688 process_request_identities(e, 2);
689 break;
690 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000691 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000692 process_add_identity(e, 2);
693 break;
694 case SSH2_AGENTC_REMOVE_IDENTITY:
695 process_remove_identity(e, 2);
696 break;
697 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
698 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100699 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700#ifdef SMARTCARD
701 case SSH_AGENTC_ADD_SMARTCARD_KEY:
702 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100703 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000704 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
705 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100706 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000707#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100708 default:
709 /* Unknown message. Respond with failure. */
710 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000711 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100712 buffer_put_int(&e->output, 1);
713 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
714 break;
715 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716}
717
Ben Lindstrombba81212001-06-25 05:01:22 +0000718static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000719new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000721 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100722 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
723 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller95def091999-11-25 00:26:21 +1100725 if (fd > max_fd)
726 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727
Damien Miller95def091999-11-25 00:26:21 +1100728 for (i = 0; i < sockets_alloc; i++)
729 if (sockets[i].type == AUTH_UNUSED) {
730 sockets[i].fd = fd;
731 sockets[i].type = type;
732 buffer_init(&sockets[i].input);
733 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000734 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100735 return;
736 }
737 old_alloc = sockets_alloc;
738 sockets_alloc += 10;
739 if (sockets)
740 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
741 else
742 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
743 for (i = old_alloc; i < sockets_alloc; i++)
744 sockets[i].type = AUTH_UNUSED;
745 sockets[old_alloc].type = type;
746 sockets[old_alloc].fd = fd;
747 buffer_init(&sockets[old_alloc].input);
748 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000749 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750}
751
Ben Lindstrombba81212001-06-25 05:01:22 +0000752static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000753prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000755 u_int i, sz;
756 int n = 0;
757
758 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100759 switch (sockets[i].type) {
760 case AUTH_SOCKET:
761 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000762 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100763 break;
764 case AUTH_UNUSED:
765 break;
766 default:
767 fatal("Unknown socket type %d", sockets[i].type);
768 break;
769 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000770 }
771
772 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000773 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000774 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000775 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000776 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000777 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000778 *fdrp = xmalloc(sz);
779 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000780 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000781 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000782 if (n < *fdl)
783 debug("XXX shrink: %d < %d", n, *fdl);
784 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000785 memset(*fdrp, 0, sz);
786 memset(*fdwp, 0, sz);
787
788 for (i = 0; i < sockets_alloc; i++) {
789 switch (sockets[i].type) {
790 case AUTH_SOCKET:
791 case AUTH_CONNECTION:
792 FD_SET(sockets[i].fd, *fdrp);
793 if (buffer_len(&sockets[i].output) > 0)
794 FD_SET(sockets[i].fd, *fdwp);
795 break;
796 default:
797 break;
798 }
799 }
800 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801}
802
Ben Lindstrombba81212001-06-25 05:01:22 +0000803static void
Damien Miller95def091999-11-25 00:26:21 +1100804after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000806 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100807 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100808 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100809 char buf[1024];
810 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811
Damien Miller95def091999-11-25 00:26:21 +1100812 for (i = 0; i < sockets_alloc; i++)
813 switch (sockets[i].type) {
814 case AUTH_UNUSED:
815 break;
816 case AUTH_SOCKET:
817 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100818 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000819 sock = accept(sockets[i].fd,
820 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100821 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100822 error("accept from AUTH_SOCKET: %s",
823 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100824 break;
825 }
826 new_socket(AUTH_CONNECTION, sock);
827 }
828 break;
829 case AUTH_CONNECTION:
830 if (buffer_len(&sockets[i].output) > 0 &&
831 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000832 do {
833 len = write(sockets[i].fd,
834 buffer_ptr(&sockets[i].output),
835 buffer_len(&sockets[i].output));
836 if (len == -1 && (errno == EAGAIN ||
837 errno == EINTR))
838 continue;
839 break;
840 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100841 if (len <= 0) {
842 shutdown(sockets[i].fd, SHUT_RDWR);
843 close(sockets[i].fd);
844 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000845 buffer_free(&sockets[i].input);
846 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000847 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100848 break;
849 }
850 buffer_consume(&sockets[i].output, len);
851 }
852 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000853 do {
854 len = read(sockets[i].fd, buf, sizeof(buf));
855 if (len == -1 && (errno == EAGAIN ||
856 errno == EINTR))
857 continue;
858 break;
859 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100860 if (len <= 0) {
861 shutdown(sockets[i].fd, SHUT_RDWR);
862 close(sockets[i].fd);
863 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000864 buffer_free(&sockets[i].input);
865 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000866 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100867 break;
868 }
869 buffer_append(&sockets[i].input, buf, len);
870 process_message(&sockets[i]);
871 }
872 break;
873 default:
874 fatal("Unknown type %d", sockets[i].type);
875 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876}
877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static void
Damien Millerc653b892002-02-08 22:05:41 +1100879cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000880{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000881 if (socket_name[0])
882 unlink(socket_name);
883 if (socket_dir[0])
884 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885}
886
Ben Lindstrombba81212001-06-25 05:01:22 +0000887static void
Damien Miller792c5111999-10-29 09:47:09 +1000888cleanup_exit(int i)
889{
Damien Millerc653b892002-02-08 22:05:41 +1100890 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100891 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000892}
893
Ben Lindstrombba81212001-06-25 05:01:22 +0000894static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000895cleanup_handler(int sig)
896{
Damien Millerc653b892002-02-08 22:05:41 +1100897 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000898 _exit(2);
899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000902check_parent_exists(int sig)
903{
904 int save_errno = errno;
905
906 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
907 /* printf("Parent has died - Authentication agent exiting.\n"); */
908 cleanup_handler(sig); /* safe */
909 }
910 signal(SIGALRM, check_parent_exists);
911 alarm(10);
912 errno = save_errno;
913}
914
915static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000916usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000917{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000918 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000919 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000920 fprintf(stderr, "Options:\n");
921 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
922 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
923 fprintf(stderr, " -k Kill the current agent.\n");
924 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000925 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100926 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000927}
928
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929int
930main(int ac, char **av)
931{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000932 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100933 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000934#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000935 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000936#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000937#ifdef HAVE_CYGWIN
938 int prev_mask;
939#endif
Damien Miller95def091999-11-25 00:26:21 +1100940 pid_t pid;
941 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Ben Lindstromb7788f32002-06-06 21:46:08 +0000942 char *agentsocket = NULL;
Damien Miller615f9392000-05-17 22:53:33 +1000943 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000944 fd_set *readsetp = NULL, *writesetp = NULL;
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
Damien Millerd0cff3e2000-04-20 23:12:58 +1000952#ifdef __GNU_LIBRARY__
Ben Lindstromb7788f32002-06-06 21:46:08 +0000953 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000954#else /* __GNU_LIBRARY__ */
Ben Lindstromb7788f32002-06-06 21:46:08 +0000955 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000956#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100957 switch (ch) {
958 case 'c':
959 if (s_flag)
960 usage();
961 c_flag++;
962 break;
963 case 'k':
964 k_flag++;
965 break;
966 case 's':
967 if (c_flag)
968 usage();
969 s_flag++;
970 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000971 case 'd':
972 if (d_flag)
973 usage();
974 d_flag++;
975 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000976 case 'a':
977 agentsocket = optarg;
978 break;
Damien Miller95def091999-11-25 00:26:21 +1100979 default:
980 usage();
981 }
Damien Miller792c5111999-10-29 09:47:09 +1000982 }
Damien Miller95def091999-11-25 00:26:21 +1100983 ac -= optind;
984 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000985
Ben Lindstromd94580c2001-07-04 03:48:02 +0000986 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100987 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000988
Ben Lindstromeecdf232002-04-02 21:03:51 +0000989 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100990 shell = getenv("SHELL");
991 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
992 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000993 }
Damien Miller95def091999-11-25 00:26:21 +1100994 if (k_flag) {
995 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
996 if (pidstr == NULL) {
997 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000998 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100999 exit(1);
1000 }
1001 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001002 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001003 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001004 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001005 exit(1);
1006 }
1007 if (kill(pid, SIGTERM) == -1) {
1008 perror("kill");
1009 exit(1);
1010 }
1011 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1012 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1013 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001014 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001015 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001016 }
Damien Miller95def091999-11-25 00:26:21 +11001017 parent_pid = getpid();
1018
Ben Lindstromb7788f32002-06-06 21:46:08 +00001019 if (agentsocket == NULL) {
1020 /* Create private directory for agent socket */
1021 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1022 if (mkdtemp(socket_dir) == NULL) {
1023 perror("mkdtemp: private socket dir");
1024 exit(1);
1025 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001026 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1027 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001028 } else {
1029 /* Try to use specified agent socket */
1030 socket_dir[0] = '\0';
1031 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001032 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033
Damien Miller5428f641999-11-25 11:54:57 +11001034 /*
1035 * Create socket early so it will exist before command gets run from
1036 * the parent.
1037 */
Damien Miller95def091999-11-25 00:26:21 +11001038 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1039 if (sock < 0) {
1040 perror("socket");
1041 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001042 }
Damien Miller95def091999-11-25 00:26:21 +11001043 memset(&sunaddr, 0, sizeof(sunaddr));
1044 sunaddr.sun_family = AF_UNIX;
1045 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001046#ifdef HAVE_CYGWIN
1047 prev_mask = umask(0177);
1048#endif
Damien Miller95def091999-11-25 00:26:21 +11001049 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1050 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001051#ifdef HAVE_CYGWIN
1052 umask(prev_mask);
1053#endif
Damien Miller95def091999-11-25 00:26:21 +11001054 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001056#ifdef HAVE_CYGWIN
1057 umask(prev_mask);
1058#endif
Damien Miller95def091999-11-25 00:26:21 +11001059 if (listen(sock, 5) < 0) {
1060 perror("listen");
1061 cleanup_exit(1);
1062 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001063
Damien Miller5428f641999-11-25 11:54:57 +11001064 /*
1065 * Fork, and have the parent execute the command, if any, or present
1066 * the socket data. The child continues as the authentication agent.
1067 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001068 if (d_flag) {
1069 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1070 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1071 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1072 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001073 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001074 goto skip;
1075 }
Damien Miller95def091999-11-25 00:26:21 +11001076 pid = fork();
1077 if (pid == -1) {
1078 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001079 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001080 }
1081 if (pid != 0) { /* Parent - execute the given command. */
1082 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001083 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001084 if (ac == 0) {
1085 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1086 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001087 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001088 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001089 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001090 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001091 exit(0);
1092 }
Damien Millere4340be2000-09-16 13:29:08 +11001093 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1094 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1095 perror("setenv");
1096 exit(1);
1097 }
Damien Miller95def091999-11-25 00:26:21 +11001098 execvp(av[0], av);
1099 perror(av[0]);
1100 exit(1);
1101 }
Damien Millerc653b892002-02-08 22:05:41 +11001102 /* child */
1103 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001104
1105 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001106 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001107 cleanup_exit(1);
1108 }
1109
1110 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001111 close(0);
1112 close(1);
1113 close(2);
1114
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001115#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001116 /* deny core dumps, since memory contains unencrypted private keys */
1117 rlim.rlim_cur = rlim.rlim_max = 0;
1118 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001119 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001120 cleanup_exit(1);
1121 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001122#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001123
1124skip:
Damien Millerc653b892002-02-08 22:05:41 +11001125 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001126 new_socket(AUTH_SOCKET, sock);
1127 if (ac > 0) {
1128 signal(SIGALRM, check_parent_exists);
1129 alarm(10);
1130 }
Damien Millerad833b32000-08-23 10:46:23 +10001131 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001132 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001133 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001134 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001135 signal(SIGHUP, cleanup_handler);
1136 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001137 nalloc = 0;
1138
Damien Miller95def091999-11-25 00:26:21 +11001139 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001140 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001141 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001142 if (errno == EINTR)
1143 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001144 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001145 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001146 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001147 }
1148 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149}