blob: f7be488f8e32e9a187f45c5b23290eaa562804c4 [file] [log] [blame]
Damien Millere4340be2000-09-16 13:29:08 +11001/* $OpenBSD: ssh-agent.c,v 1.36 2000/09/15 07:13:49 deraadt Exp $ */
Damien Miller792c5111999-10-29 09:47:09 +10002
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11007 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
Damien Millerad833b32000-08-23 10:46:23 +100015 * SSH2 implementation,
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Millere4340be2000-09-16 13:29:08 +110040RCSID("$OpenBSD: ssh-agent.c,v 1.36 2000/09/15 07:13:49 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "ssh.h"
43#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "buffer.h"
45#include "bufaux.h"
46#include "xmalloc.h"
47#include "packet.h"
48#include "getput.h"
49#include "mpaux.h"
50
Damien Millerad833b32000-08-23 10:46:23 +100051#include <openssl/evp.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include <openssl/md5.h>
Damien Miller994cf142000-07-21 10:19:44 +100053#include <openssl/dsa.h>
54#include <openssl/rsa.h>
55#include "key.h"
56#include "authfd.h"
Damien Millerad833b32000-08-23 10:46:23 +100057#include "dsa.h"
58#include "kex.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller95def091999-11-25 00:26:21 +110060typedef struct {
61 int fd;
62 enum {
63 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
64 } type;
65 Buffer input;
66 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067} SocketEntry;
68
69unsigned int sockets_alloc = 0;
70SocketEntry *sockets = NULL;
71
Damien Miller95def091999-11-25 00:26:21 +110072typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100073 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110074 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075} Identity;
76
Damien Millerad833b32000-08-23 10:46:23 +100077typedef struct {
78 int nentries;
79 Identity *identities;
80} Idtab;
81
82/* private key table, one per protocol version */
83Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85int max_fd = 0;
86
87/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100088pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* pathname and directory for AUTH_SOCKET */
91char socket_name[1024];
92char socket_dir[1024];
93
Damien Miller95def091999-11-25 00:26:21 +110094#ifdef HAVE___PROGNAME
95extern char *__progname;
96#else /* HAVE___PROGNAME */
Damien Miller70fb6712000-05-01 20:59:50 +100097static const char *__progname = "ssh-agent";
Damien Miller95def091999-11-25 00:26:21 +110098#endif /* HAVE___PROGNAME */
99
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100void
Damien Millerad833b32000-08-23 10:46:23 +1000101idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102{
Damien Millerad833b32000-08-23 10:46:23 +1000103 int i;
104 for (i = 0; i <=2; i++){
105 idtable[i].identities = NULL;
106 idtable[i].nentries = 0;
107 }
108}
109
110/* return private key table for requested protocol version */
111Idtab *
112idtab_lookup(int version)
113{
114 if (version < 1 || version > 2)
115 fatal("internal error, bad protocol version %d", version);
116 return &idtable[version];
117}
118
119/* return matching private key for given public key */
120Key *
121lookup_private_key(Key *key, int *idx, int version)
122{
123 int i;
124 Idtab *tab = idtab_lookup(version);
125 for (i = 0; i < tab->nentries; i++) {
126 if (key_equal(key, tab->identities[i].key)) {
127 if (idx != NULL)
128 *idx = i;
129 return tab->identities[i].key;
130 }
131 }
132 return NULL;
133}
134
135/* send list of supported public keys to 'client' */
136void
137process_request_identities(SocketEntry *e, int version)
138{
139 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100140 Buffer msg;
141 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller95def091999-11-25 00:26:21 +1100143 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000144 buffer_put_char(&msg, (version == 1) ?
145 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
146 buffer_put_int(&msg, tab->nentries);
147 for (i = 0; i < tab->nentries; i++) {
148 Identity *id = &tab->identities[i];
149 if (id->key->type == KEY_RSA) {
150 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
151 buffer_put_bignum(&msg, id->key->rsa->e);
152 buffer_put_bignum(&msg, id->key->rsa->n);
153 } else {
154 unsigned char *blob;
155 unsigned int blen;
156 dsa_make_key_blob(id->key, &blob, &blen);
157 buffer_put_string(&msg, blob, blen);
158 xfree(blob);
159 }
160 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100161 }
162 buffer_put_int(&e->output, buffer_len(&msg));
163 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
164 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Damien Millerad833b32000-08-23 10:46:23 +1000167/* ssh1 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168void
Damien Millerad833b32000-08-23 10:46:23 +1000169process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Millerad833b32000-08-23 10:46:23 +1000171 Key *key, *private;
172 BIGNUM *challenge;
173 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100174 Buffer msg;
175 MD5_CTX md;
176 unsigned char buf[32], mdbuf[16], session_id[16];
177 unsigned int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller95def091999-11-25 00:26:21 +1100179 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000180 key = key_new(KEY_RSA);
Damien Miller95def091999-11-25 00:26:21 +1100181 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000182
183 buffer_get_int(&e->input); /* ignored */
184 buffer_get_bignum(&e->input, key->rsa->e);
185 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100186 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Millerad833b32000-08-23 10:46:23 +1000188 /* Only protocol 1.1 is supported */
189 if (buffer_len(&e->input) == 0)
190 goto failure;
191 buffer_get(&e->input, (char *) session_id, 16);
192 response_type = buffer_get_int(&e->input);
193 if (response_type != 1)
194 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Millerad833b32000-08-23 10:46:23 +1000196 private = lookup_private_key(key, NULL, 1);
197 if (private != NULL) {
198 /* Decrypt the challenge using the private key. */
199 rsa_private_decrypt(challenge, challenge, private->rsa);
Damien Millerfd7c9111999-11-08 16:15:55 +1100200
Damien Millerad833b32000-08-23 10:46:23 +1000201 /* The response is MD5 of decrypted challenge plus session id. */
202 len = BN_num_bytes(challenge);
203 if (len <= 0 || len > 32) {
204 log("process_authentication_challenge: bad challenge length %d", len);
205 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100206 }
Damien Millerad833b32000-08-23 10:46:23 +1000207 memset(buf, 0, 32);
208 BN_bn2bin(challenge, buf + 32 - len);
209 MD5_Init(&md);
210 MD5_Update(&md, buf, 32);
211 MD5_Update(&md, session_id, 16);
212 MD5_Final(mdbuf, &md);
213
214 /* Send the response. */
215 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
216 for (i = 0; i < 16; i++)
217 buffer_put_char(&msg, mdbuf[i]);
218 goto send;
219 }
220
221failure:
222 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100223 buffer_put_char(&msg, SSH_AGENT_FAILURE);
224send:
225 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000226 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
227 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100228 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000229 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Millerad833b32000-08-23 10:46:23 +1000232/* ssh2 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233void
Damien Millerad833b32000-08-23 10:46:23 +1000234process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Millerad833b32000-08-23 10:46:23 +1000236 extern int datafellows;
237 Key *key, *private;
238 unsigned char *blob, *data, *signature = NULL;
239 unsigned int blen, dlen, slen = 0;
240 Buffer msg;
241 int ok = -1;
242
243 datafellows = 0;
244
245 blob = buffer_get_string(&e->input, &blen);
246 data = buffer_get_string(&e->input, &dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100247 buffer_get_int(&e->input); /* flags, unused */
Damien Millerad833b32000-08-23 10:46:23 +1000248
249 key = dsa_key_from_blob(blob, blen);
250 if (key != NULL) {
251 private = lookup_private_key(key, NULL, 2);
252 if (private != NULL)
253 ok = dsa_sign(private, &signature, &slen, data, dlen);
254 }
255 key_free(key);
256 buffer_init(&msg);
257 if (ok == 0) {
258 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
259 buffer_put_string(&msg, signature, slen);
260 } else {
261 buffer_put_char(&msg, SSH_AGENT_FAILURE);
262 }
263 buffer_put_int(&e->output, buffer_len(&msg));
264 buffer_append(&e->output, buffer_ptr(&msg),
265 buffer_len(&msg));
266 buffer_free(&msg);
267 xfree(data);
268 xfree(blob);
269 if (signature != NULL)
270 xfree(signature);
271}
272
273/* shared */
274void
275process_remove_identity(SocketEntry *e, int version)
276{
277 Key *key = NULL, *private;
278 unsigned char *blob;
279 unsigned int blen;
Damien Miller95def091999-11-25 00:26:21 +1100280 unsigned int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000281 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100282
Damien Millerad833b32000-08-23 10:46:23 +1000283 switch(version){
284 case 1:
285 key = key_new(KEY_RSA);
286 bits = buffer_get_int(&e->input);
287 buffer_get_bignum(&e->input, key->rsa->e);
288 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289
Damien Millerad833b32000-08-23 10:46:23 +1000290 if (bits != key_size(key))
291 log("Warning: identity keysize mismatch: actual %d, announced %d",
292 key_size(key), bits);
293 break;
294 case 2:
295 blob = buffer_get_string(&e->input, &blen);
296 key = dsa_key_from_blob(blob, blen);
297 xfree(blob);
298 break;
299 }
300 if (key != NULL) {
301 int idx;
302 private = lookup_private_key(key, &idx, version);
303 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100304 /*
305 * We have this key. Free the old key. Since we
306 * don\'t want to leave empty slots in the middle of
307 * the array, we actually free the key there and copy
308 * data from the last entry.
309 */
Damien Millerad833b32000-08-23 10:46:23 +1000310 Idtab *tab = idtab_lookup(version);
311 key_free(tab->identities[idx].key);
312 xfree(tab->identities[idx].comment);
313 if (idx != tab->nentries)
314 tab->identities[idx] = tab->identities[tab->nentries];
315 tab->nentries--;
316 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100317 }
Damien Millerad833b32000-08-23 10:46:23 +1000318 key_free(key);
319 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000321 buffer_put_char(&e->output,
322 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
324
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325void
Damien Millerad833b32000-08-23 10:46:23 +1000326process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327{
Damien Miller95def091999-11-25 00:26:21 +1100328 unsigned int i;
Damien Millerad833b32000-08-23 10:46:23 +1000329 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller95def091999-11-25 00:26:21 +1100331 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000332 for (i = 0; i < tab->nentries; i++) {
333 key_free(tab->identities[i].key);
334 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller95def091999-11-25 00:26:21 +1100337 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000338 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
340 /* Send success. */
341 buffer_put_int(&e->output, 1);
342 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
343 return;
Damien Miller95def091999-11-25 00:26:21 +1100344}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller95def091999-11-25 00:26:21 +1100346void
Damien Millerad833b32000-08-23 10:46:23 +1000347process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100348{
Damien Millerad833b32000-08-23 10:46:23 +1000349 Key *k = NULL;
350 RSA *rsa;
Damien Miller95def091999-11-25 00:26:21 +1100351 BIGNUM *aux;
352 BN_CTX *ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000353 char *type;
354 char *comment;
355 int success = 0;
356 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100357
Damien Millerad833b32000-08-23 10:46:23 +1000358 switch (version) {
359 case 1:
360 k = key_new(KEY_RSA);
361 rsa = k->rsa;
Damien Miller95def091999-11-25 00:26:21 +1100362
Damien Millerad833b32000-08-23 10:46:23 +1000363 /* allocate mem for private key */
364 /* XXX rsa->n and rsa->e are already allocated */
365 rsa->d = BN_new();
366 rsa->iqmp = BN_new();
367 rsa->q = BN_new();
368 rsa->p = BN_new();
369 rsa->dmq1 = BN_new();
370 rsa->dmp1 = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100371
Damien Millerad833b32000-08-23 10:46:23 +1000372 buffer_get_int(&e->input); /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100373
Damien Millerad833b32000-08-23 10:46:23 +1000374 buffer_get_bignum(&e->input, rsa->n);
375 buffer_get_bignum(&e->input, rsa->e);
376 buffer_get_bignum(&e->input, rsa->d);
377 buffer_get_bignum(&e->input, rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100378
Damien Millerad833b32000-08-23 10:46:23 +1000379 /* SSH and SSL have p and q swapped */
380 buffer_get_bignum(&e->input, rsa->q); /* p */
381 buffer_get_bignum(&e->input, rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100382
Damien Millerad833b32000-08-23 10:46:23 +1000383 /* Generate additional parameters */
384 aux = BN_new();
385 ctx = BN_CTX_new();
Damien Miller95def091999-11-25 00:26:21 +1100386
Damien Millerad833b32000-08-23 10:46:23 +1000387 BN_sub(aux, rsa->q, BN_value_one());
388 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
Damien Miller95def091999-11-25 00:26:21 +1100389
Damien Millerad833b32000-08-23 10:46:23 +1000390 BN_sub(aux, rsa->p, BN_value_one());
391 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
Damien Miller95def091999-11-25 00:26:21 +1100392
Damien Millerad833b32000-08-23 10:46:23 +1000393 BN_clear_free(aux);
394 BN_CTX_free(ctx);
395
396 break;
397 case 2:
398 type = buffer_get_string(&e->input, NULL);
399 if (strcmp(type, KEX_DSS)) {
400 buffer_clear(&e->input);
401 xfree(type);
402 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100403 }
Damien Millerad833b32000-08-23 10:46:23 +1000404 xfree(type);
Damien Miller95def091999-11-25 00:26:21 +1100405
Damien Millerad833b32000-08-23 10:46:23 +1000406 k = key_new(KEY_DSA);
407
408 /* allocate mem for private key */
409 k->dsa->priv_key = BN_new();
410
411 buffer_get_bignum2(&e->input, k->dsa->p);
412 buffer_get_bignum2(&e->input, k->dsa->q);
413 buffer_get_bignum2(&e->input, k->dsa->g);
414 buffer_get_bignum2(&e->input, k->dsa->pub_key);
415 buffer_get_bignum2(&e->input, k->dsa->priv_key);
416
417 break;
418 }
419
420 comment = buffer_get_string(&e->input, NULL);
421 if (k == NULL) {
422 xfree(comment);
423 goto send;
424 }
425 success = 1;
426 if (lookup_private_key(k, NULL, version) == NULL) {
427 if (tab->nentries == 0)
428 tab->identities = xmalloc(sizeof(Identity));
429 else
430 tab->identities = xrealloc(tab->identities,
431 (tab->nentries + 1) * sizeof(Identity));
432 tab->identities[tab->nentries].key = k;
433 tab->identities[tab->nentries].comment = comment;
434 /* Increment the number of identities. */
435 tab->nentries++;
436 } else {
437 key_free(k);
438 xfree(comment);
439 }
440send:
Damien Miller95def091999-11-25 00:26:21 +1100441 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000442 buffer_put_char(&e->output,
443 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444}
445
Damien Millerad833b32000-08-23 10:46:23 +1000446/* dispatch incoming messages */
447
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448void
449process_message(SocketEntry *e)
450{
Damien Miller95def091999-11-25 00:26:21 +1100451 unsigned int msg_len;
452 unsigned int type;
453 unsigned char *cp;
454 if (buffer_len(&e->input) < 5)
455 return; /* Incomplete message. */
456 cp = (unsigned char *) buffer_ptr(&e->input);
457 msg_len = GET_32BIT(cp);
458 if (msg_len > 256 * 1024) {
459 shutdown(e->fd, SHUT_RDWR);
460 close(e->fd);
461 e->type = AUTH_UNUSED;
462 return;
463 }
464 if (buffer_len(&e->input) < msg_len + 4)
465 return;
466 buffer_consume(&e->input, 4);
467 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000470 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100471 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000472 process_authentication_challenge1(e);
473 break;
474 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
475 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100476 break;
477 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000478 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100479 break;
480 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000481 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100482 break;
483 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000484 process_remove_all_identities(e, 1);
485 break;
486 /* ssh2 */
487 case SSH2_AGENTC_SIGN_REQUEST:
488 process_sign_request2(e);
489 break;
490 case SSH2_AGENTC_REQUEST_IDENTITIES:
491 process_request_identities(e, 2);
492 break;
493 case SSH2_AGENTC_ADD_IDENTITY:
494 process_add_identity(e, 2);
495 break;
496 case SSH2_AGENTC_REMOVE_IDENTITY:
497 process_remove_identity(e, 2);
498 break;
499 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
500 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100501 break;
502 default:
503 /* Unknown message. Respond with failure. */
504 error("Unknown message %d", type);
505 buffer_clear(&e->input);
506 buffer_put_int(&e->output, 1);
507 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
508 break;
509 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510}
511
512void
513new_socket(int type, int fd)
514{
Damien Miller95def091999-11-25 00:26:21 +1100515 unsigned int i, old_alloc;
516 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
517 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 if (fd > max_fd)
520 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller95def091999-11-25 00:26:21 +1100522 for (i = 0; i < sockets_alloc; i++)
523 if (sockets[i].type == AUTH_UNUSED) {
524 sockets[i].fd = fd;
525 sockets[i].type = type;
526 buffer_init(&sockets[i].input);
527 buffer_init(&sockets[i].output);
528 return;
529 }
530 old_alloc = sockets_alloc;
531 sockets_alloc += 10;
532 if (sockets)
533 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
534 else
535 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
536 for (i = old_alloc; i < sockets_alloc; i++)
537 sockets[i].type = AUTH_UNUSED;
538 sockets[old_alloc].type = type;
539 sockets[old_alloc].fd = fd;
540 buffer_init(&sockets[old_alloc].input);
541 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542}
543
544void
545prepare_select(fd_set *readset, fd_set *writeset)
546{
Damien Miller95def091999-11-25 00:26:21 +1100547 unsigned int i;
548 for (i = 0; i < sockets_alloc; i++)
549 switch (sockets[i].type) {
550 case AUTH_SOCKET:
551 case AUTH_CONNECTION:
552 FD_SET(sockets[i].fd, readset);
553 if (buffer_len(&sockets[i].output) > 0)
554 FD_SET(sockets[i].fd, writeset);
555 break;
556 case AUTH_UNUSED:
557 break;
558 default:
559 fatal("Unknown socket type %d", sockets[i].type);
560 break;
561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562}
563
Damien Miller4af51302000-04-16 11:18:38 +1000564void
Damien Miller95def091999-11-25 00:26:21 +1100565after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566{
Damien Miller95def091999-11-25 00:26:21 +1100567 unsigned int i;
568 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100569 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100570 char buf[1024];
571 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 for (i = 0; i < sockets_alloc; i++)
574 switch (sockets[i].type) {
575 case AUTH_UNUSED:
576 break;
577 case AUTH_SOCKET:
578 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100579 slen = sizeof(sunaddr);
580 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100581 if (sock < 0) {
582 perror("accept from AUTH_SOCKET");
583 break;
584 }
585 new_socket(AUTH_CONNECTION, sock);
586 }
587 break;
588 case AUTH_CONNECTION:
589 if (buffer_len(&sockets[i].output) > 0 &&
590 FD_ISSET(sockets[i].fd, writeset)) {
591 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
592 buffer_len(&sockets[i].output));
593 if (len <= 0) {
594 shutdown(sockets[i].fd, SHUT_RDWR);
595 close(sockets[i].fd);
596 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000597 buffer_free(&sockets[i].input);
598 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100599 break;
600 }
601 buffer_consume(&sockets[i].output, len);
602 }
603 if (FD_ISSET(sockets[i].fd, readset)) {
604 len = read(sockets[i].fd, buf, sizeof(buf));
605 if (len <= 0) {
606 shutdown(sockets[i].fd, SHUT_RDWR);
607 close(sockets[i].fd);
608 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000609 buffer_free(&sockets[i].input);
610 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100611 break;
612 }
613 buffer_append(&sockets[i].input, buf, len);
614 process_message(&sockets[i]);
615 }
616 break;
617 default:
618 fatal("Unknown type %d", sockets[i].type);
619 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620}
621
622void
623check_parent_exists(int sig)
624{
Damien Miller166fca82000-04-20 07:42:21 +1000625 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100626 /* printf("Parent has died - Authentication agent exiting.\n"); */
627 exit(1);
628 }
629 signal(SIGALRM, check_parent_exists);
630 alarm(10);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631}
632
Damien Miller792c5111999-10-29 09:47:09 +1000633void
634cleanup_socket(void)
635{
Damien Miller95def091999-11-25 00:26:21 +1100636 remove(socket_name);
637 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638}
639
Damien Miller792c5111999-10-29 09:47:09 +1000640void
641cleanup_exit(int i)
642{
Damien Miller95def091999-11-25 00:26:21 +1100643 cleanup_socket();
644 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000645}
646
647void
648usage()
649{
Damien Miller95def091999-11-25 00:26:21 +1100650 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
651 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
652 __progname);
653 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000654}
655
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656int
657main(int ac, char **av)
658{
Damien Miller95def091999-11-25 00:26:21 +1100659 fd_set readset, writeset;
660 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
661 struct sockaddr_un sunaddr;
662 pid_t pid;
663 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000664 extern int optind;
Damien Millerad833b32000-08-23 10:46:23 +1000665
Damien Millerf9b625c2000-07-09 22:42:32 +1000666 init_rng();
Damien Millerad833b32000-08-23 10:46:23 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 /* check if RSA support exists */
669 if (rsa_alive() == 0) {
670 fprintf(stderr,
671 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
672 __progname);
673 exit(1);
674 }
Damien Millerd0cff3e2000-04-20 23:12:58 +1000675#ifdef __GNU_LIBRARY__
676 while ((ch = getopt(ac, av, "+cks")) != -1) {
677#else /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100678 while ((ch = getopt(ac, av, "cks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000679#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100680 switch (ch) {
681 case 'c':
682 if (s_flag)
683 usage();
684 c_flag++;
685 break;
686 case 'k':
687 k_flag++;
688 break;
689 case 's':
690 if (c_flag)
691 usage();
692 s_flag++;
693 break;
694 default:
695 usage();
696 }
Damien Miller792c5111999-10-29 09:47:09 +1000697 }
Damien Miller95def091999-11-25 00:26:21 +1100698 ac -= optind;
699 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000700
Damien Miller95def091999-11-25 00:26:21 +1100701 if (ac > 0 && (c_flag || k_flag || s_flag))
702 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000703
Damien Miller95def091999-11-25 00:26:21 +1100704 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
705 shell = getenv("SHELL");
706 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
707 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000708 }
Damien Miller95def091999-11-25 00:26:21 +1100709 if (k_flag) {
710 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
711 if (pidstr == NULL) {
712 fprintf(stderr, "%s not set, cannot kill agent\n",
713 SSH_AGENTPID_ENV_NAME);
714 exit(1);
715 }
716 pid = atoi(pidstr);
717 if (pid < 1) { /* XXX PID_MAX check too */
Damien Miller166fca82000-04-20 07:42:21 +1000718 /* Yes, PID_MAX check please */
Damien Miller95def091999-11-25 00:26:21 +1100719 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
720 SSH_AGENTPID_ENV_NAME, pidstr);
721 exit(1);
722 }
723 if (kill(pid, SIGTERM) == -1) {
724 perror("kill");
725 exit(1);
726 }
727 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
728 printf(format, SSH_AUTHSOCKET_ENV_NAME);
729 printf(format, SSH_AGENTPID_ENV_NAME);
730 printf("echo Agent pid %d killed;\n", pid);
731 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000732 }
Damien Miller95def091999-11-25 00:26:21 +1100733 parent_pid = getpid();
734
735 /* Create private directory for agent socket */
736 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
737 if (mkdtemp(socket_dir) == NULL) {
738 perror("mkdtemp: private socket dir");
739 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000740 }
Damien Miller95def091999-11-25 00:26:21 +1100741 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
742 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743
Damien Miller5428f641999-11-25 11:54:57 +1100744 /*
745 * Create socket early so it will exist before command gets run from
746 * the parent.
747 */
Damien Miller95def091999-11-25 00:26:21 +1100748 sock = socket(AF_UNIX, SOCK_STREAM, 0);
749 if (sock < 0) {
750 perror("socket");
751 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000752 }
Damien Miller95def091999-11-25 00:26:21 +1100753 memset(&sunaddr, 0, sizeof(sunaddr));
754 sunaddr.sun_family = AF_UNIX;
755 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
756 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
757 perror("bind");
758 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759 }
Damien Miller95def091999-11-25 00:26:21 +1100760 if (listen(sock, 5) < 0) {
761 perror("listen");
762 cleanup_exit(1);
763 }
Damien Miller5428f641999-11-25 11:54:57 +1100764 /*
765 * Fork, and have the parent execute the command, if any, or present
766 * the socket data. The child continues as the authentication agent.
767 */
Damien Miller95def091999-11-25 00:26:21 +1100768 pid = fork();
769 if (pid == -1) {
770 perror("fork");
771 exit(1);
772 }
773 if (pid != 0) { /* Parent - execute the given command. */
774 close(sock);
775 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
776 if (ac == 0) {
777 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
778 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
779 SSH_AUTHSOCKET_ENV_NAME);
780 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
781 SSH_AGENTPID_ENV_NAME);
782 printf("echo Agent pid %d;\n", pid);
783 exit(0);
784 }
Damien Millere4340be2000-09-16 13:29:08 +1100785 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
786 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
787 perror("setenv");
788 exit(1);
789 }
Damien Miller95def091999-11-25 00:26:21 +1100790 execvp(av[0], av);
791 perror(av[0]);
792 exit(1);
793 }
794 close(0);
795 close(1);
796 close(2);
797
798 if (setsid() == -1) {
799 perror("setsid");
800 cleanup_exit(1);
801 }
802 if (atexit(cleanup_socket) < 0) {
803 perror("atexit");
804 cleanup_exit(1);
805 }
806 new_socket(AUTH_SOCKET, sock);
807 if (ac > 0) {
808 signal(SIGALRM, check_parent_exists);
809 alarm(10);
810 }
Damien Millerad833b32000-08-23 10:46:23 +1000811 idtab_init();
Damien Miller95def091999-11-25 00:26:21 +1100812 signal(SIGINT, SIG_IGN);
813 signal(SIGPIPE, SIG_IGN);
Damien Miller4af51302000-04-16 11:18:38 +1000814 signal(SIGHUP, cleanup_exit);
815 signal(SIGTERM, cleanup_exit);
Damien Miller95def091999-11-25 00:26:21 +1100816 while (1) {
817 FD_ZERO(&readset);
818 FD_ZERO(&writeset);
819 prepare_select(&readset, &writeset);
820 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
821 if (errno == EINTR)
822 continue;
823 exit(1);
824 }
825 after_select(&readset, &writeset);
826 }
827 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828}