blob: d9567af5c6ee3b7ba89fc59ab29b10fe5ae36889 [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 Lindstrom2f717042002-06-06 21:52:03 +000038RCSID("$OpenBSD: ssh-agent.c,v 1.88 2002/06/05 19:57: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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079} Identity;
80
Damien Millerad833b32000-08-23 10:46:23 +100081typedef struct {
82 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110083 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100084} Idtab;
85
86/* private key table, one per protocol version */
87Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89int max_fd = 0;
90
91/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100092pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* pathname and directory for AUTH_SOCKET */
95char socket_name[1024];
96char socket_dir[1024];
97
Ben Lindstrom2f717042002-06-06 21:52:03 +000098/* locking */
99int locked = 0;
100char *lock_passwd = NULL;
101
Damien Miller95def091999-11-25 00:26:21 +1100102#ifdef HAVE___PROGNAME
103extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000104#else
105char *__progname;
106#endif
Damien Miller95def091999-11-25 00:26:21 +1100107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static void
Damien Millerad833b32000-08-23 10:46:23 +1000109idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110{
Damien Millerad833b32000-08-23 10:46:23 +1000111 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000112 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100113 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000114 idtable[i].nentries = 0;
115 }
116}
117
118/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000120idtab_lookup(int version)
121{
122 if (version < 1 || version > 2)
123 fatal("internal error, bad protocol version %d", version);
124 return &idtable[version];
125}
126
127/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100128static Identity *
129lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000130{
Damien Miller1a534ae2002-01-22 23:26:13 +1100131 Identity *id;
132
Damien Millerad833b32000-08-23 10:46:23 +1000133 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100134 TAILQ_FOREACH(id, &tab->idlist, next) {
135 if (key_equal(key, id->key))
136 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000137 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100138 return (NULL);
139}
140
141static void
142free_identity(Identity *id)
143{
144 key_free(id->key);
145 xfree(id->comment);
146 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000147}
148
149/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Damien Millerad833b32000-08-23 10:46:23 +1000151process_request_identities(SocketEntry *e, int version)
152{
153 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100154 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100155 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000158 buffer_put_char(&msg, (version == 1) ?
159 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
160 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100161 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100162 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000163 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
164 buffer_put_bignum(&msg, id->key->rsa->e);
165 buffer_put_bignum(&msg, id->key->rsa->n);
166 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000167 u_char *blob;
168 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000170 buffer_put_string(&msg, blob, blen);
171 xfree(blob);
172 }
173 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100174 }
175 buffer_put_int(&e->output, buffer_len(&msg));
176 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
177 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Millerad833b32000-08-23 10:46:23 +1000180/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static void
Damien Millerad833b32000-08-23 10:46:23 +1000182process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183{
Damien Miller1a534ae2002-01-22 23:26:13 +1100184 Identity *id;
185 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000186 BIGNUM *challenge;
187 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100188 Buffer msg;
189 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000190 u_char buf[32], mdbuf[16], session_id[16];
191 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192
Damien Miller95def091999-11-25 00:26:21 +1100193 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100194 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100195 if ((challenge = BN_new()) == NULL)
196 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000197
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000198 buffer_get_int(&e->request); /* ignored */
199 buffer_get_bignum(&e->request, key->rsa->e);
200 buffer_get_bignum(&e->request, key->rsa->n);
201 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Millerad833b32000-08-23 10:46:23 +1000203 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000204 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000205 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000206 buffer_get(&e->request, session_id, 16);
207 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000208 if (response_type != 1)
209 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Miller1a534ae2002-01-22 23:26:13 +1100211 id = lookup_identity(key, 1);
212 if (id != NULL) {
213 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000214 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100215 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
216 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100217
Damien Millerad833b32000-08-23 10:46:23 +1000218 /* The response is MD5 of decrypted challenge plus session id. */
219 len = BN_num_bytes(challenge);
220 if (len <= 0 || len > 32) {
221 log("process_authentication_challenge: bad challenge length %d", len);
222 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100223 }
Damien Millerad833b32000-08-23 10:46:23 +1000224 memset(buf, 0, 32);
225 BN_bn2bin(challenge, buf + 32 - len);
226 MD5_Init(&md);
227 MD5_Update(&md, buf, 32);
228 MD5_Update(&md, session_id, 16);
229 MD5_Final(mdbuf, &md);
230
231 /* Send the response. */
232 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
233 for (i = 0; i < 16; i++)
234 buffer_put_char(&msg, mdbuf[i]);
235 goto send;
236 }
237
238failure:
239 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100240 buffer_put_char(&msg, SSH_AGENT_FAILURE);
241send:
242 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000243 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
244 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100245 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000246 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247}
248
Damien Millerad833b32000-08-23 10:46:23 +1000249/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static void
Damien Millerad833b32000-08-23 10:46:23 +1000251process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Damien Millerad833b32000-08-23 10:46:23 +1000253 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100254 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000255 u_char *blob, *data, *signature = NULL;
256 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100257 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000258 Buffer msg;
259 int ok = -1;
260
261 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000262
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000263 blob = buffer_get_string(&e->request, &blen);
264 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100265
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000266 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100267 if (flags & SSH_AGENT_OLD_SIGNATURE)
268 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000269
Damien Miller0bc1bd82000-11-13 22:57:25 +1100270 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000271 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100272 Identity *id = lookup_identity(key, 2);
273 if (id != NULL)
274 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000275 }
276 key_free(key);
277 buffer_init(&msg);
278 if (ok == 0) {
279 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
280 buffer_put_string(&msg, signature, slen);
281 } else {
282 buffer_put_char(&msg, SSH_AGENT_FAILURE);
283 }
284 buffer_put_int(&e->output, buffer_len(&msg));
285 buffer_append(&e->output, buffer_ptr(&msg),
286 buffer_len(&msg));
287 buffer_free(&msg);
288 xfree(data);
289 xfree(blob);
290 if (signature != NULL)
291 xfree(signature);
292}
293
294/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000295static void
Damien Millerad833b32000-08-23 10:46:23 +1000296process_remove_identity(SocketEntry *e, int version)
297{
Damien Miller1a534ae2002-01-22 23:26:13 +1100298 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000299 u_char *blob;
300 u_int blen;
301 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000302 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100303
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000304 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000305 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100306 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000307 bits = buffer_get_int(&e->request);
308 buffer_get_bignum(&e->request, key->rsa->e);
309 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Millerad833b32000-08-23 10:46:23 +1000311 if (bits != key_size(key))
312 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000313 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000314 break;
315 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000316 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100317 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000318 xfree(blob);
319 break;
320 }
321 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100322 Identity *id = lookup_identity(key, version);
323 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100324 /*
325 * We have this key. Free the old key. Since we
326 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000327 * the array, we actually free the key there and move
328 * all the entries between the empty slot and the end
329 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100330 */
Damien Millerad833b32000-08-23 10:46:23 +1000331 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100332 if (tab->nentries < 1)
333 fatal("process_remove_identity: "
334 "internal error: tab->nentries %d",
335 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 TAILQ_REMOVE(&tab->idlist, id, next);
337 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000338 tab->nentries--;
339 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100340 }
Damien Millerad833b32000-08-23 10:46:23 +1000341 key_free(key);
342 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000344 buffer_put_char(&e->output,
345 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Damien Millerad833b32000-08-23 10:46:23 +1000349process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Millerad833b32000-08-23 10:46:23 +1000351 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100352 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100355 for (id = TAILQ_FIRST(&tab->idlist); id;
356 id = TAILQ_FIRST(&tab->idlist)) {
357 TAILQ_REMOVE(&tab->idlist, id, next);
358 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100359 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000362 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
364 /* Send success. */
365 buffer_put_int(&e->output, 1);
366 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
367 return;
Damien Miller95def091999-11-25 00:26:21 +1100368}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369
Ben Lindstrombba81212001-06-25 05:01:22 +0000370static void
Damien Millerad833b32000-08-23 10:46:23 +1000371process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100372{
Damien Millerad833b32000-08-23 10:46:23 +1000373 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000375 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000377 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100378
Damien Millerad833b32000-08-23 10:46:23 +1000379 switch (version) {
380 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100381 k = key_new_private(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000382 buffer_get_int(&e->request); /* ignored */
383 buffer_get_bignum(&e->request, k->rsa->n);
384 buffer_get_bignum(&e->request, k->rsa->e);
385 buffer_get_bignum(&e->request, k->rsa->d);
386 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100387
Damien Millerad833b32000-08-23 10:46:23 +1000388 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000389 buffer_get_bignum(&e->request, k->rsa->q); /* p */
390 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100391
Damien Millerad833b32000-08-23 10:46:23 +1000392 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000393 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000394 break;
395 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000396 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000397 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100398 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000399 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100400 case KEY_DSA:
401 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000402 buffer_get_bignum2(&e->request, k->dsa->p);
403 buffer_get_bignum2(&e->request, k->dsa->q);
404 buffer_get_bignum2(&e->request, k->dsa->g);
405 buffer_get_bignum2(&e->request, k->dsa->pub_key);
406 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100407 break;
408 case KEY_RSA:
409 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000410 buffer_get_bignum2(&e->request, k->rsa->n);
411 buffer_get_bignum2(&e->request, k->rsa->e);
412 buffer_get_bignum2(&e->request, k->rsa->d);
413 buffer_get_bignum2(&e->request, k->rsa->iqmp);
414 buffer_get_bignum2(&e->request, k->rsa->p);
415 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100416
417 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000418 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100419 break;
420 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000421 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000422 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100423 }
Damien Millerad833b32000-08-23 10:46:23 +1000424 break;
425 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000426 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000427 if (k == NULL) {
428 xfree(comment);
429 goto send;
430 }
431 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100432 if (lookup_identity(k, version) == NULL) {
433 Identity *id = xmalloc(sizeof(Identity));
434 id->key = k;
435 id->comment = comment;
436 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000437 /* Increment the number of identities. */
438 tab->nentries++;
439 } else {
440 key_free(k);
441 xfree(comment);
442 }
443send:
Damien Miller95def091999-11-25 00:26:21 +1100444 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000445 buffer_put_char(&e->output,
446 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
448
Ben Lindstrom2f717042002-06-06 21:52:03 +0000449/* XXX todo: encrypt sensitive data with passphrase */
450static void
451process_lock_agent(SocketEntry *e, int lock)
452{
453 char *passwd;
454 int success = 0;
455
456 passwd = buffer_get_string(&e->request, NULL);
457 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
458 locked = 0;
459 memset(lock_passwd, 0, strlen(lock_passwd));
460 xfree(lock_passwd);
461 lock_passwd = NULL;
462 success = 1;
463 } else if (!locked && lock) {
464 locked = 1;
465 lock_passwd = xstrdup(passwd);
466 success = 1;
467 }
468 memset(passwd, 0, strlen(passwd));
469 xfree(passwd);
470
471 buffer_put_int(&e->output, 1);
472 buffer_put_char(&e->output,
473 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
474 return;
475}
476
477static void
478no_identities(SocketEntry *e, u_int type)
479{
480 Buffer msg;
481
482 buffer_init(&msg);
483 buffer_put_char(&msg,
484 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
485 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
486 buffer_put_int(&msg, 0);
487 buffer_put_int(&e->output, buffer_len(&msg));
488 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
489 buffer_free(&msg);
490}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000491
492#ifdef SMARTCARD
493static void
494process_add_smartcard_key (SocketEntry *e)
495{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000496 Identity *id;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000497 Idtab *tab;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000498 Key **keys, *k;
Ben Lindstromba72d302002-03-22 03:51:06 +0000499 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000500 int i, version, success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100501
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000502 sc_reader_id = buffer_get_string(&e->request, NULL);
503 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000504 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000505 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000506 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000507
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000508 if (keys == NULL || keys[0] == NULL) {
509 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000510 goto send;
511 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000512 for (i = 0; keys[i] != NULL; i++) {
513 k = keys[i];
514 version = k->type == KEY_RSA1 ? 1 : 2;
515 tab = idtab_lookup(version);
516 if (lookup_identity(k, version) == NULL) {
517 id = xmalloc(sizeof(Identity));
518 id->key = k;
519 id->comment = xstrdup("smartcard key");
520 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
521 tab->nentries++;
522 success = 1;
523 } else {
524 key_free(k);
525 }
526 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000527 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000528 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000529send:
530 buffer_put_int(&e->output, 1);
531 buffer_put_char(&e->output,
532 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
533}
534
535static void
536process_remove_smartcard_key(SocketEntry *e)
537{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000538 Identity *id;
539 Idtab *tab;
540 Key **keys, *k = NULL;
Ben Lindstromba72d302002-03-22 03:51:06 +0000541 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000542 int i, version, success = 0;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000543
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000544 sc_reader_id = buffer_get_string(&e->request, NULL);
545 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000546 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000547 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000548 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000549
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000550 if (keys == NULL || keys[0] == NULL) {
551 error("sc_get_keys failed");
552 goto send;
553 }
554 for (i = 0; keys[i] != NULL; i++) {
555 k = keys[i];
556 version = k->type == KEY_RSA1 ? 1 : 2;
557 if ((id = lookup_identity(k, version)) != NULL) {
558 tab = idtab_lookup(version);
559 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000560 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100561 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000562 success = 1;
563 }
564 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000565 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000566 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000567 xfree(keys);
568send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000569 buffer_put_int(&e->output, 1);
570 buffer_put_char(&e->output,
571 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
572}
Ben Lindstromffce1472001-08-06 21:57:31 +0000573#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000574
Damien Millerad833b32000-08-23 10:46:23 +1000575/* dispatch incoming messages */
576
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578process_message(SocketEntry *e)
579{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000580 u_int msg_len;
581 u_int type;
582 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100583 if (buffer_len(&e->input) < 5)
584 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100585 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100586 msg_len = GET_32BIT(cp);
587 if (msg_len > 256 * 1024) {
588 shutdown(e->fd, SHUT_RDWR);
589 close(e->fd);
590 e->type = AUTH_UNUSED;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000591 buffer_free(&e->input);
592 buffer_free(&e->output);
593 buffer_free(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100594 return;
595 }
596 if (buffer_len(&e->input) < msg_len + 4)
597 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000598
599 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100600 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000601 buffer_clear(&e->request);
602 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
603 buffer_consume(&e->input, msg_len);
604 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Ben Lindstrom2f717042002-06-06 21:52:03 +0000606 /* check wheter agent is locked */
607 if (locked && type != SSH_AGENTC_UNLOCK) {
608 buffer_clear(&e->request);
609 switch (type) {
610 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
611 case SSH2_AGENTC_REQUEST_IDENTITIES:
612 /* send empty lists */
613 no_identities(e, type);
614 break;
615 default:
616 /* send a fail message for all other request types */
617 buffer_put_int(&e->output, 1);
618 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
619 }
620 return;
621 }
622
Ben Lindstrom3f471632001-07-04 03:53:15 +0000623 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100624 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000625 case SSH_AGENTC_LOCK:
626 case SSH_AGENTC_UNLOCK:
627 process_lock_agent(e, type == SSH_AGENTC_LOCK);
628 break;
Damien Millerad833b32000-08-23 10:46:23 +1000629 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100630 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000631 process_authentication_challenge1(e);
632 break;
633 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
634 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100635 break;
636 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000637 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100638 break;
639 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000640 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100641 break;
642 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000643 process_remove_all_identities(e, 1);
644 break;
645 /* ssh2 */
646 case SSH2_AGENTC_SIGN_REQUEST:
647 process_sign_request2(e);
648 break;
649 case SSH2_AGENTC_REQUEST_IDENTITIES:
650 process_request_identities(e, 2);
651 break;
652 case SSH2_AGENTC_ADD_IDENTITY:
653 process_add_identity(e, 2);
654 break;
655 case SSH2_AGENTC_REMOVE_IDENTITY:
656 process_remove_identity(e, 2);
657 break;
658 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
659 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100660 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661#ifdef SMARTCARD
662 case SSH_AGENTC_ADD_SMARTCARD_KEY:
663 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100664 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000665 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
666 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100667 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000668#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100669 default:
670 /* Unknown message. Respond with failure. */
671 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000672 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100673 buffer_put_int(&e->output, 1);
674 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
675 break;
676 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677}
678
Ben Lindstrombba81212001-06-25 05:01:22 +0000679static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000680new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000682 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100683 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
684 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685
Damien Miller95def091999-11-25 00:26:21 +1100686 if (fd > max_fd)
687 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688
Damien Miller95def091999-11-25 00:26:21 +1100689 for (i = 0; i < sockets_alloc; i++)
690 if (sockets[i].type == AUTH_UNUSED) {
691 sockets[i].fd = fd;
692 sockets[i].type = type;
693 buffer_init(&sockets[i].input);
694 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000695 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100696 return;
697 }
698 old_alloc = sockets_alloc;
699 sockets_alloc += 10;
700 if (sockets)
701 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
702 else
703 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
704 for (i = old_alloc; i < sockets_alloc; i++)
705 sockets[i].type = AUTH_UNUSED;
706 sockets[old_alloc].type = type;
707 sockets[old_alloc].fd = fd;
708 buffer_init(&sockets[old_alloc].input);
709 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000710 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711}
712
Ben Lindstrombba81212001-06-25 05:01:22 +0000713static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000714prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000716 u_int i, sz;
717 int n = 0;
718
719 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100720 switch (sockets[i].type) {
721 case AUTH_SOCKET:
722 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000723 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100724 break;
725 case AUTH_UNUSED:
726 break;
727 default:
728 fatal("Unknown socket type %d", sockets[i].type);
729 break;
730 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000731 }
732
733 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000734 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000735 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000736 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000737 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000738 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000739 *fdrp = xmalloc(sz);
740 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000741 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000742 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000743 if (n < *fdl)
744 debug("XXX shrink: %d < %d", n, *fdl);
745 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000746 memset(*fdrp, 0, sz);
747 memset(*fdwp, 0, sz);
748
749 for (i = 0; i < sockets_alloc; i++) {
750 switch (sockets[i].type) {
751 case AUTH_SOCKET:
752 case AUTH_CONNECTION:
753 FD_SET(sockets[i].fd, *fdrp);
754 if (buffer_len(&sockets[i].output) > 0)
755 FD_SET(sockets[i].fd, *fdwp);
756 break;
757 default:
758 break;
759 }
760 }
761 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762}
763
Ben Lindstrombba81212001-06-25 05:01:22 +0000764static void
Damien Miller95def091999-11-25 00:26:21 +1100765after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000767 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100768 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100769 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100770 char buf[1024];
771 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772
Damien Miller95def091999-11-25 00:26:21 +1100773 for (i = 0; i < sockets_alloc; i++)
774 switch (sockets[i].type) {
775 case AUTH_UNUSED:
776 break;
777 case AUTH_SOCKET:
778 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100779 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000780 sock = accept(sockets[i].fd,
781 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100782 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100783 error("accept from AUTH_SOCKET: %s",
784 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100785 break;
786 }
787 new_socket(AUTH_CONNECTION, sock);
788 }
789 break;
790 case AUTH_CONNECTION:
791 if (buffer_len(&sockets[i].output) > 0 &&
792 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000793 do {
794 len = write(sockets[i].fd,
795 buffer_ptr(&sockets[i].output),
796 buffer_len(&sockets[i].output));
797 if (len == -1 && (errno == EAGAIN ||
798 errno == EINTR))
799 continue;
800 break;
801 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100802 if (len <= 0) {
803 shutdown(sockets[i].fd, SHUT_RDWR);
804 close(sockets[i].fd);
805 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000806 buffer_free(&sockets[i].input);
807 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000808 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100809 break;
810 }
811 buffer_consume(&sockets[i].output, len);
812 }
813 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000814 do {
815 len = read(sockets[i].fd, buf, sizeof(buf));
816 if (len == -1 && (errno == EAGAIN ||
817 errno == EINTR))
818 continue;
819 break;
820 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100821 if (len <= 0) {
822 shutdown(sockets[i].fd, SHUT_RDWR);
823 close(sockets[i].fd);
824 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000825 buffer_free(&sockets[i].input);
826 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000827 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100828 break;
829 }
830 buffer_append(&sockets[i].input, buf, len);
831 process_message(&sockets[i]);
832 }
833 break;
834 default:
835 fatal("Unknown type %d", sockets[i].type);
836 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837}
838
Ben Lindstrombba81212001-06-25 05:01:22 +0000839static void
Damien Millerc653b892002-02-08 22:05:41 +1100840cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000841{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000842 if (socket_name[0])
843 unlink(socket_name);
844 if (socket_dir[0])
845 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846}
847
Ben Lindstrombba81212001-06-25 05:01:22 +0000848static void
Damien Miller792c5111999-10-29 09:47:09 +1000849cleanup_exit(int i)
850{
Damien Millerc653b892002-02-08 22:05:41 +1100851 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100852 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000853}
854
Ben Lindstrombba81212001-06-25 05:01:22 +0000855static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000856cleanup_handler(int sig)
857{
Damien Millerc653b892002-02-08 22:05:41 +1100858 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000859 _exit(2);
860}
861
Ben Lindstrombba81212001-06-25 05:01:22 +0000862static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000863check_parent_exists(int sig)
864{
865 int save_errno = errno;
866
867 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
868 /* printf("Parent has died - Authentication agent exiting.\n"); */
869 cleanup_handler(sig); /* safe */
870 }
871 signal(SIGALRM, check_parent_exists);
872 alarm(10);
873 errno = save_errno;
874}
875
876static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000877usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000878{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000879 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000880 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000881 fprintf(stderr, "Options:\n");
882 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
883 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
884 fprintf(stderr, " -k Kill the current agent.\n");
885 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000886 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100887 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000888}
889
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890int
891main(int ac, char **av)
892{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000893 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100894 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000895#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000896 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000897#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000898#ifdef HAVE_CYGWIN
899 int prev_mask;
900#endif
Damien Miller95def091999-11-25 00:26:21 +1100901 pid_t pid;
902 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Ben Lindstromb7788f32002-06-06 21:46:08 +0000903 char *agentsocket = NULL;
Damien Miller615f9392000-05-17 22:53:33 +1000904 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000905 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000906
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000907 SSLeay_add_all_algorithms();
908
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000909 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000910 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100911 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000912
Damien Millerd0cff3e2000-04-20 23:12:58 +1000913#ifdef __GNU_LIBRARY__
Ben Lindstromb7788f32002-06-06 21:46:08 +0000914 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000915#else /* __GNU_LIBRARY__ */
Ben Lindstromb7788f32002-06-06 21:46:08 +0000916 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000917#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100918 switch (ch) {
919 case 'c':
920 if (s_flag)
921 usage();
922 c_flag++;
923 break;
924 case 'k':
925 k_flag++;
926 break;
927 case 's':
928 if (c_flag)
929 usage();
930 s_flag++;
931 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000932 case 'd':
933 if (d_flag)
934 usage();
935 d_flag++;
936 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000937 case 'a':
938 agentsocket = optarg;
939 break;
Damien Miller95def091999-11-25 00:26:21 +1100940 default:
941 usage();
942 }
Damien Miller792c5111999-10-29 09:47:09 +1000943 }
Damien Miller95def091999-11-25 00:26:21 +1100944 ac -= optind;
945 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000946
Ben Lindstromd94580c2001-07-04 03:48:02 +0000947 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100948 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000949
Ben Lindstromeecdf232002-04-02 21:03:51 +0000950 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100951 shell = getenv("SHELL");
952 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
953 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000954 }
Damien Miller95def091999-11-25 00:26:21 +1100955 if (k_flag) {
956 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
957 if (pidstr == NULL) {
958 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000959 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100960 exit(1);
961 }
962 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000963 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100964 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000965 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100966 exit(1);
967 }
968 if (kill(pid, SIGTERM) == -1) {
969 perror("kill");
970 exit(1);
971 }
972 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
973 printf(format, SSH_AUTHSOCKET_ENV_NAME);
974 printf(format, SSH_AGENTPID_ENV_NAME);
975 printf("echo Agent pid %d killed;\n", pid);
976 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000977 }
Damien Miller95def091999-11-25 00:26:21 +1100978 parent_pid = getpid();
979
Ben Lindstromb7788f32002-06-06 21:46:08 +0000980 if (agentsocket == NULL) {
981 /* Create private directory for agent socket */
982 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
983 if (mkdtemp(socket_dir) == NULL) {
984 perror("mkdtemp: private socket dir");
985 exit(1);
986 }
987 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
988 parent_pid);
989 } else {
990 /* Try to use specified agent socket */
991 socket_dir[0] = '\0';
992 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +1000993 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller5428f641999-11-25 11:54:57 +1100995 /*
996 * Create socket early so it will exist before command gets run from
997 * the parent.
998 */
Damien Miller95def091999-11-25 00:26:21 +1100999 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1000 if (sock < 0) {
1001 perror("socket");
1002 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001003 }
Damien Miller95def091999-11-25 00:26:21 +11001004 memset(&sunaddr, 0, sizeof(sunaddr));
1005 sunaddr.sun_family = AF_UNIX;
1006 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001007#ifdef HAVE_CYGWIN
1008 prev_mask = umask(0177);
1009#endif
Damien Miller95def091999-11-25 00:26:21 +11001010 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1011 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001012#ifdef HAVE_CYGWIN
1013 umask(prev_mask);
1014#endif
Damien Miller95def091999-11-25 00:26:21 +11001015 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001017#ifdef HAVE_CYGWIN
1018 umask(prev_mask);
1019#endif
Damien Miller95def091999-11-25 00:26:21 +11001020 if (listen(sock, 5) < 0) {
1021 perror("listen");
1022 cleanup_exit(1);
1023 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001024
Damien Miller5428f641999-11-25 11:54:57 +11001025 /*
1026 * Fork, and have the parent execute the command, if any, or present
1027 * the socket data. The child continues as the authentication agent.
1028 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001029 if (d_flag) {
1030 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1031 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1032 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1033 SSH_AUTHSOCKET_ENV_NAME);
1034 printf("echo Agent pid %d;\n", parent_pid);
1035 goto skip;
1036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 pid = fork();
1038 if (pid == -1) {
1039 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001040 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001041 }
1042 if (pid != 0) { /* Parent - execute the given command. */
1043 close(sock);
1044 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
1045 if (ac == 0) {
1046 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1047 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001048 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001049 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001050 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001051 printf("echo Agent pid %d;\n", pid);
1052 exit(0);
1053 }
Damien Millere4340be2000-09-16 13:29:08 +11001054 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1055 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1056 perror("setenv");
1057 exit(1);
1058 }
Damien Miller95def091999-11-25 00:26:21 +11001059 execvp(av[0], av);
1060 perror(av[0]);
1061 exit(1);
1062 }
Damien Millerc653b892002-02-08 22:05:41 +11001063 /* child */
1064 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001065
1066 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001067 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001068 cleanup_exit(1);
1069 }
1070
1071 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001072 close(0);
1073 close(1);
1074 close(2);
1075
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001076#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001077 /* deny core dumps, since memory contains unencrypted private keys */
1078 rlim.rlim_cur = rlim.rlim_max = 0;
1079 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001080 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001081 cleanup_exit(1);
1082 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001083#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001084
1085skip:
Damien Millerc653b892002-02-08 22:05:41 +11001086 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001087 new_socket(AUTH_SOCKET, sock);
1088 if (ac > 0) {
1089 signal(SIGALRM, check_parent_exists);
1090 alarm(10);
1091 }
Damien Millerad833b32000-08-23 10:46:23 +10001092 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001093 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001094 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001095 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001096 signal(SIGHUP, cleanup_handler);
1097 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001098 nalloc = 0;
1099
Damien Miller95def091999-11-25 00:26:21 +11001100 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001101 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001102 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001103 if (errno == EINTR)
1104 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001105 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001106 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001107 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001108 }
1109 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110}