blob: 479388fab62423ed95593d10195d58185e83988b [file] [log] [blame]
Damien Miller62cee002000-09-23 17:15:56 +11001/* $OpenBSD: ssh-agent.c,v 1.37 2000/09/21 11:07:51 markus 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 Miller62cee002000-09-23 17:15:56 +110040RCSID("$OpenBSD: ssh-agent.c,v 1.37 2000/09/21 11:07:51 markus 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 Miller62cee002000-09-23 17:15:56 +110059#include "compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller95def091999-11-25 00:26:21 +110061typedef struct {
62 int fd;
63 enum {
64 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
65 } type;
66 Buffer input;
67 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068} SocketEntry;
69
70unsigned int sockets_alloc = 0;
71SocketEntry *sockets = NULL;
72
Damien Miller95def091999-11-25 00:26:21 +110073typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100074 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110075 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076} Identity;
77
Damien Millerad833b32000-08-23 10:46:23 +100078typedef struct {
79 int nentries;
80 Identity *identities;
81} Idtab;
82
83/* private key table, one per protocol version */
84Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86int max_fd = 0;
87
88/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100089pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
91/* pathname and directory for AUTH_SOCKET */
92char socket_name[1024];
93char socket_dir[1024];
94
Damien Miller95def091999-11-25 00:26:21 +110095#ifdef HAVE___PROGNAME
96extern char *__progname;
97#else /* HAVE___PROGNAME */
Damien Miller70fb6712000-05-01 20:59:50 +100098static const char *__progname = "ssh-agent";
Damien Miller95def091999-11-25 00:26:21 +110099#endif /* HAVE___PROGNAME */
100
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101void
Damien Millerad833b32000-08-23 10:46:23 +1000102idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103{
Damien Millerad833b32000-08-23 10:46:23 +1000104 int i;
105 for (i = 0; i <=2; i++){
106 idtable[i].identities = NULL;
107 idtable[i].nentries = 0;
108 }
109}
110
111/* return private key table for requested protocol version */
112Idtab *
113idtab_lookup(int version)
114{
115 if (version < 1 || version > 2)
116 fatal("internal error, bad protocol version %d", version);
117 return &idtable[version];
118}
119
120/* return matching private key for given public key */
121Key *
122lookup_private_key(Key *key, int *idx, int version)
123{
124 int i;
125 Idtab *tab = idtab_lookup(version);
126 for (i = 0; i < tab->nentries; i++) {
127 if (key_equal(key, tab->identities[i].key)) {
128 if (idx != NULL)
129 *idx = i;
130 return tab->identities[i].key;
131 }
132 }
133 return NULL;
134}
135
136/* send list of supported public keys to 'client' */
137void
138process_request_identities(SocketEntry *e, int version)
139{
140 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100141 Buffer msg;
142 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller95def091999-11-25 00:26:21 +1100144 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000145 buffer_put_char(&msg, (version == 1) ?
146 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
147 buffer_put_int(&msg, tab->nentries);
148 for (i = 0; i < tab->nentries; i++) {
149 Identity *id = &tab->identities[i];
150 if (id->key->type == KEY_RSA) {
151 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
152 buffer_put_bignum(&msg, id->key->rsa->e);
153 buffer_put_bignum(&msg, id->key->rsa->n);
154 } else {
155 unsigned char *blob;
156 unsigned int blen;
157 dsa_make_key_blob(id->key, &blob, &blen);
158 buffer_put_string(&msg, blob, blen);
159 xfree(blob);
160 }
161 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100162 }
163 buffer_put_int(&e->output, buffer_len(&msg));
164 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
165 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Damien Millerad833b32000-08-23 10:46:23 +1000168/* ssh1 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169void
Damien Millerad833b32000-08-23 10:46:23 +1000170process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171{
Damien Millerad833b32000-08-23 10:46:23 +1000172 Key *key, *private;
173 BIGNUM *challenge;
174 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100175 Buffer msg;
176 MD5_CTX md;
177 unsigned char buf[32], mdbuf[16], session_id[16];
178 unsigned int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller95def091999-11-25 00:26:21 +1100180 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000181 key = key_new(KEY_RSA);
Damien Miller95def091999-11-25 00:26:21 +1100182 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000183
184 buffer_get_int(&e->input); /* ignored */
185 buffer_get_bignum(&e->input, key->rsa->e);
186 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100187 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Millerad833b32000-08-23 10:46:23 +1000189 /* Only protocol 1.1 is supported */
190 if (buffer_len(&e->input) == 0)
191 goto failure;
192 buffer_get(&e->input, (char *) session_id, 16);
193 response_type = buffer_get_int(&e->input);
194 if (response_type != 1)
195 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Millerad833b32000-08-23 10:46:23 +1000197 private = lookup_private_key(key, NULL, 1);
198 if (private != NULL) {
199 /* Decrypt the challenge using the private key. */
200 rsa_private_decrypt(challenge, challenge, private->rsa);
Damien Millerfd7c9111999-11-08 16:15:55 +1100201
Damien Millerad833b32000-08-23 10:46:23 +1000202 /* The response is MD5 of decrypted challenge plus session id. */
203 len = BN_num_bytes(challenge);
204 if (len <= 0 || len > 32) {
205 log("process_authentication_challenge: bad challenge length %d", len);
206 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerad833b32000-08-23 10:46:23 +1000208 memset(buf, 0, 32);
209 BN_bn2bin(challenge, buf + 32 - len);
210 MD5_Init(&md);
211 MD5_Update(&md, buf, 32);
212 MD5_Update(&md, session_id, 16);
213 MD5_Final(mdbuf, &md);
214
215 /* Send the response. */
216 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
217 for (i = 0; i < 16; i++)
218 buffer_put_char(&msg, mdbuf[i]);
219 goto send;
220 }
221
222failure:
223 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100224 buffer_put_char(&msg, SSH_AGENT_FAILURE);
225send:
226 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000227 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
228 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100229 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000230 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
Damien Millerad833b32000-08-23 10:46:23 +1000233/* ssh2 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234void
Damien Millerad833b32000-08-23 10:46:23 +1000235process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Damien Millerad833b32000-08-23 10:46:23 +1000237 extern int datafellows;
238 Key *key, *private;
239 unsigned char *blob, *data, *signature = NULL;
240 unsigned int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100241 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000242 Buffer msg;
243 int ok = -1;
244
245 datafellows = 0;
246
247 blob = buffer_get_string(&e->input, &blen);
248 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100249
250 flags = buffer_get_int(&e->input);
251 if (flags & SSH_AGENT_OLD_SIGNATURE)
252 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000253
254 key = dsa_key_from_blob(blob, blen);
255 if (key != NULL) {
256 private = lookup_private_key(key, NULL, 2);
257 if (private != NULL)
258 ok = dsa_sign(private, &signature, &slen, data, dlen);
259 }
260 key_free(key);
261 buffer_init(&msg);
262 if (ok == 0) {
263 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
264 buffer_put_string(&msg, signature, slen);
265 } else {
266 buffer_put_char(&msg, SSH_AGENT_FAILURE);
267 }
268 buffer_put_int(&e->output, buffer_len(&msg));
269 buffer_append(&e->output, buffer_ptr(&msg),
270 buffer_len(&msg));
271 buffer_free(&msg);
272 xfree(data);
273 xfree(blob);
274 if (signature != NULL)
275 xfree(signature);
276}
277
278/* shared */
279void
280process_remove_identity(SocketEntry *e, int version)
281{
282 Key *key = NULL, *private;
283 unsigned char *blob;
284 unsigned int blen;
Damien Miller95def091999-11-25 00:26:21 +1100285 unsigned int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000286 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100287
Damien Millerad833b32000-08-23 10:46:23 +1000288 switch(version){
289 case 1:
290 key = key_new(KEY_RSA);
291 bits = buffer_get_int(&e->input);
292 buffer_get_bignum(&e->input, key->rsa->e);
293 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Damien Millerad833b32000-08-23 10:46:23 +1000295 if (bits != key_size(key))
296 log("Warning: identity keysize mismatch: actual %d, announced %d",
297 key_size(key), bits);
298 break;
299 case 2:
300 blob = buffer_get_string(&e->input, &blen);
301 key = dsa_key_from_blob(blob, blen);
302 xfree(blob);
303 break;
304 }
305 if (key != NULL) {
306 int idx;
307 private = lookup_private_key(key, &idx, version);
308 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100309 /*
310 * We have this key. Free the old key. Since we
311 * don\'t want to leave empty slots in the middle of
312 * the array, we actually free the key there and copy
313 * data from the last entry.
314 */
Damien Millerad833b32000-08-23 10:46:23 +1000315 Idtab *tab = idtab_lookup(version);
316 key_free(tab->identities[idx].key);
317 xfree(tab->identities[idx].comment);
318 if (idx != tab->nentries)
319 tab->identities[idx] = tab->identities[tab->nentries];
320 tab->nentries--;
321 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100322 }
Damien Millerad833b32000-08-23 10:46:23 +1000323 key_free(key);
324 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000326 buffer_put_char(&e->output,
327 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330void
Damien Millerad833b32000-08-23 10:46:23 +1000331process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332{
Damien Miller95def091999-11-25 00:26:21 +1100333 unsigned int i;
Damien Millerad833b32000-08-23 10:46:23 +1000334 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller95def091999-11-25 00:26:21 +1100336 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000337 for (i = 0; i < tab->nentries; i++) {
338 key_free(tab->identities[i].key);
339 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100340 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Miller95def091999-11-25 00:26:21 +1100342 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000343 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
345 /* Send success. */
346 buffer_put_int(&e->output, 1);
347 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
348 return;
Damien Miller95def091999-11-25 00:26:21 +1100349}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351void
Damien Millerad833b32000-08-23 10:46:23 +1000352process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100353{
Damien Millerad833b32000-08-23 10:46:23 +1000354 Key *k = NULL;
355 RSA *rsa;
Damien Miller95def091999-11-25 00:26:21 +1100356 BIGNUM *aux;
357 BN_CTX *ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000358 char *type;
359 char *comment;
360 int success = 0;
361 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100362
Damien Millerad833b32000-08-23 10:46:23 +1000363 switch (version) {
364 case 1:
365 k = key_new(KEY_RSA);
366 rsa = k->rsa;
Damien Miller95def091999-11-25 00:26:21 +1100367
Damien Millerad833b32000-08-23 10:46:23 +1000368 /* allocate mem for private key */
369 /* XXX rsa->n and rsa->e are already allocated */
370 rsa->d = BN_new();
371 rsa->iqmp = BN_new();
372 rsa->q = BN_new();
373 rsa->p = BN_new();
374 rsa->dmq1 = BN_new();
375 rsa->dmp1 = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100376
Damien Millerad833b32000-08-23 10:46:23 +1000377 buffer_get_int(&e->input); /* ignored */
Damien Miller95def091999-11-25 00:26:21 +1100378
Damien Millerad833b32000-08-23 10:46:23 +1000379 buffer_get_bignum(&e->input, rsa->n);
380 buffer_get_bignum(&e->input, rsa->e);
381 buffer_get_bignum(&e->input, rsa->d);
382 buffer_get_bignum(&e->input, rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100383
Damien Millerad833b32000-08-23 10:46:23 +1000384 /* SSH and SSL have p and q swapped */
385 buffer_get_bignum(&e->input, rsa->q); /* p */
386 buffer_get_bignum(&e->input, rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100387
Damien Millerad833b32000-08-23 10:46:23 +1000388 /* Generate additional parameters */
389 aux = BN_new();
390 ctx = BN_CTX_new();
Damien Miller95def091999-11-25 00:26:21 +1100391
Damien Millerad833b32000-08-23 10:46:23 +1000392 BN_sub(aux, rsa->q, BN_value_one());
393 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
Damien Miller95def091999-11-25 00:26:21 +1100394
Damien Millerad833b32000-08-23 10:46:23 +1000395 BN_sub(aux, rsa->p, BN_value_one());
396 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
Damien Miller95def091999-11-25 00:26:21 +1100397
Damien Millerad833b32000-08-23 10:46:23 +1000398 BN_clear_free(aux);
399 BN_CTX_free(ctx);
400
401 break;
402 case 2:
403 type = buffer_get_string(&e->input, NULL);
404 if (strcmp(type, KEX_DSS)) {
405 buffer_clear(&e->input);
406 xfree(type);
407 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100408 }
Damien Millerad833b32000-08-23 10:46:23 +1000409 xfree(type);
Damien Miller95def091999-11-25 00:26:21 +1100410
Damien Millerad833b32000-08-23 10:46:23 +1000411 k = key_new(KEY_DSA);
412
413 /* allocate mem for private key */
414 k->dsa->priv_key = BN_new();
415
416 buffer_get_bignum2(&e->input, k->dsa->p);
417 buffer_get_bignum2(&e->input, k->dsa->q);
418 buffer_get_bignum2(&e->input, k->dsa->g);
419 buffer_get_bignum2(&e->input, k->dsa->pub_key);
420 buffer_get_bignum2(&e->input, k->dsa->priv_key);
421
422 break;
423 }
424
425 comment = buffer_get_string(&e->input, NULL);
426 if (k == NULL) {
427 xfree(comment);
428 goto send;
429 }
430 success = 1;
431 if (lookup_private_key(k, NULL, version) == NULL) {
432 if (tab->nentries == 0)
433 tab->identities = xmalloc(sizeof(Identity));
434 else
435 tab->identities = xrealloc(tab->identities,
436 (tab->nentries + 1) * sizeof(Identity));
437 tab->identities[tab->nentries].key = k;
438 tab->identities[tab->nentries].comment = comment;
439 /* Increment the number of identities. */
440 tab->nentries++;
441 } else {
442 key_free(k);
443 xfree(comment);
444 }
445send:
Damien Miller95def091999-11-25 00:26:21 +1100446 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000447 buffer_put_char(&e->output,
448 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449}
450
Damien Millerad833b32000-08-23 10:46:23 +1000451/* dispatch incoming messages */
452
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453void
454process_message(SocketEntry *e)
455{
Damien Miller95def091999-11-25 00:26:21 +1100456 unsigned int msg_len;
457 unsigned int type;
458 unsigned char *cp;
459 if (buffer_len(&e->input) < 5)
460 return; /* Incomplete message. */
461 cp = (unsigned char *) buffer_ptr(&e->input);
462 msg_len = GET_32BIT(cp);
463 if (msg_len > 256 * 1024) {
464 shutdown(e->fd, SHUT_RDWR);
465 close(e->fd);
466 e->type = AUTH_UNUSED;
467 return;
468 }
469 if (buffer_len(&e->input) < msg_len + 4)
470 return;
471 buffer_consume(&e->input, 4);
472 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Damien Miller95def091999-11-25 00:26:21 +1100474 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000475 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100476 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000477 process_authentication_challenge1(e);
478 break;
479 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
480 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100481 break;
482 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000483 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100484 break;
485 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000486 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100487 break;
488 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000489 process_remove_all_identities(e, 1);
490 break;
491 /* ssh2 */
492 case SSH2_AGENTC_SIGN_REQUEST:
493 process_sign_request2(e);
494 break;
495 case SSH2_AGENTC_REQUEST_IDENTITIES:
496 process_request_identities(e, 2);
497 break;
498 case SSH2_AGENTC_ADD_IDENTITY:
499 process_add_identity(e, 2);
500 break;
501 case SSH2_AGENTC_REMOVE_IDENTITY:
502 process_remove_identity(e, 2);
503 break;
504 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
505 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100506 break;
507 default:
508 /* Unknown message. Respond with failure. */
509 error("Unknown message %d", type);
510 buffer_clear(&e->input);
511 buffer_put_int(&e->output, 1);
512 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
513 break;
514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515}
516
517void
518new_socket(int type, int fd)
519{
Damien Miller95def091999-11-25 00:26:21 +1100520 unsigned int i, old_alloc;
521 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
522 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Miller95def091999-11-25 00:26:21 +1100524 if (fd > max_fd)
525 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller95def091999-11-25 00:26:21 +1100527 for (i = 0; i < sockets_alloc; i++)
528 if (sockets[i].type == AUTH_UNUSED) {
529 sockets[i].fd = fd;
530 sockets[i].type = type;
531 buffer_init(&sockets[i].input);
532 buffer_init(&sockets[i].output);
533 return;
534 }
535 old_alloc = sockets_alloc;
536 sockets_alloc += 10;
537 if (sockets)
538 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
539 else
540 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
541 for (i = old_alloc; i < sockets_alloc; i++)
542 sockets[i].type = AUTH_UNUSED;
543 sockets[old_alloc].type = type;
544 sockets[old_alloc].fd = fd;
545 buffer_init(&sockets[old_alloc].input);
546 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547}
548
549void
550prepare_select(fd_set *readset, fd_set *writeset)
551{
Damien Miller95def091999-11-25 00:26:21 +1100552 unsigned int i;
553 for (i = 0; i < sockets_alloc; i++)
554 switch (sockets[i].type) {
555 case AUTH_SOCKET:
556 case AUTH_CONNECTION:
557 FD_SET(sockets[i].fd, readset);
558 if (buffer_len(&sockets[i].output) > 0)
559 FD_SET(sockets[i].fd, writeset);
560 break;
561 case AUTH_UNUSED:
562 break;
563 default:
564 fatal("Unknown socket type %d", sockets[i].type);
565 break;
566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567}
568
Damien Miller4af51302000-04-16 11:18:38 +1000569void
Damien Miller95def091999-11-25 00:26:21 +1100570after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571{
Damien Miller95def091999-11-25 00:26:21 +1100572 unsigned int i;
573 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100574 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100575 char buf[1024];
576 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
Damien Miller95def091999-11-25 00:26:21 +1100578 for (i = 0; i < sockets_alloc; i++)
579 switch (sockets[i].type) {
580 case AUTH_UNUSED:
581 break;
582 case AUTH_SOCKET:
583 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100584 slen = sizeof(sunaddr);
585 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100586 if (sock < 0) {
587 perror("accept from AUTH_SOCKET");
588 break;
589 }
590 new_socket(AUTH_CONNECTION, sock);
591 }
592 break;
593 case AUTH_CONNECTION:
594 if (buffer_len(&sockets[i].output) > 0 &&
595 FD_ISSET(sockets[i].fd, writeset)) {
596 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
597 buffer_len(&sockets[i].output));
598 if (len <= 0) {
599 shutdown(sockets[i].fd, SHUT_RDWR);
600 close(sockets[i].fd);
601 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000602 buffer_free(&sockets[i].input);
603 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100604 break;
605 }
606 buffer_consume(&sockets[i].output, len);
607 }
608 if (FD_ISSET(sockets[i].fd, readset)) {
609 len = read(sockets[i].fd, buf, sizeof(buf));
610 if (len <= 0) {
611 shutdown(sockets[i].fd, SHUT_RDWR);
612 close(sockets[i].fd);
613 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000614 buffer_free(&sockets[i].input);
615 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100616 break;
617 }
618 buffer_append(&sockets[i].input, buf, len);
619 process_message(&sockets[i]);
620 }
621 break;
622 default:
623 fatal("Unknown type %d", sockets[i].type);
624 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625}
626
627void
628check_parent_exists(int sig)
629{
Damien Miller166fca82000-04-20 07:42:21 +1000630 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100631 /* printf("Parent has died - Authentication agent exiting.\n"); */
632 exit(1);
633 }
634 signal(SIGALRM, check_parent_exists);
635 alarm(10);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636}
637
Damien Miller792c5111999-10-29 09:47:09 +1000638void
639cleanup_socket(void)
640{
Damien Miller78315eb2000-09-29 23:01:36 +1100641 unlink(socket_name);
Damien Miller95def091999-11-25 00:26:21 +1100642 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643}
644
Damien Miller792c5111999-10-29 09:47:09 +1000645void
646cleanup_exit(int i)
647{
Damien Miller95def091999-11-25 00:26:21 +1100648 cleanup_socket();
649 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000650}
651
652void
653usage()
654{
Damien Miller95def091999-11-25 00:26:21 +1100655 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
656 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
657 __progname);
658 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000659}
660
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661int
662main(int ac, char **av)
663{
Damien Miller95def091999-11-25 00:26:21 +1100664 fd_set readset, writeset;
665 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
666 struct sockaddr_un sunaddr;
667 pid_t pid;
668 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000669 extern int optind;
Damien Millerad833b32000-08-23 10:46:23 +1000670
Damien Millerf9b625c2000-07-09 22:42:32 +1000671 init_rng();
Damien Millerad833b32000-08-23 10:46:23 +1000672
Damien Miller95def091999-11-25 00:26:21 +1100673 /* check if RSA support exists */
674 if (rsa_alive() == 0) {
675 fprintf(stderr,
676 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
677 __progname);
678 exit(1);
679 }
Damien Millerd0cff3e2000-04-20 23:12:58 +1000680#ifdef __GNU_LIBRARY__
681 while ((ch = getopt(ac, av, "+cks")) != -1) {
682#else /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100683 while ((ch = getopt(ac, av, "cks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000684#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100685 switch (ch) {
686 case 'c':
687 if (s_flag)
688 usage();
689 c_flag++;
690 break;
691 case 'k':
692 k_flag++;
693 break;
694 case 's':
695 if (c_flag)
696 usage();
697 s_flag++;
698 break;
699 default:
700 usage();
701 }
Damien Miller792c5111999-10-29 09:47:09 +1000702 }
Damien Miller95def091999-11-25 00:26:21 +1100703 ac -= optind;
704 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000705
Damien Miller95def091999-11-25 00:26:21 +1100706 if (ac > 0 && (c_flag || k_flag || s_flag))
707 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000708
Damien Miller95def091999-11-25 00:26:21 +1100709 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
710 shell = getenv("SHELL");
711 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
712 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000713 }
Damien Miller95def091999-11-25 00:26:21 +1100714 if (k_flag) {
715 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
716 if (pidstr == NULL) {
717 fprintf(stderr, "%s not set, cannot kill agent\n",
718 SSH_AGENTPID_ENV_NAME);
719 exit(1);
720 }
721 pid = atoi(pidstr);
722 if (pid < 1) { /* XXX PID_MAX check too */
Damien Miller166fca82000-04-20 07:42:21 +1000723 /* Yes, PID_MAX check please */
Damien Miller95def091999-11-25 00:26:21 +1100724 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
725 SSH_AGENTPID_ENV_NAME, pidstr);
726 exit(1);
727 }
728 if (kill(pid, SIGTERM) == -1) {
729 perror("kill");
730 exit(1);
731 }
732 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
733 printf(format, SSH_AUTHSOCKET_ENV_NAME);
734 printf(format, SSH_AGENTPID_ENV_NAME);
735 printf("echo Agent pid %d killed;\n", pid);
736 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000737 }
Damien Miller95def091999-11-25 00:26:21 +1100738 parent_pid = getpid();
739
740 /* Create private directory for agent socket */
741 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
742 if (mkdtemp(socket_dir) == NULL) {
743 perror("mkdtemp: private socket dir");
744 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000745 }
Damien Miller95def091999-11-25 00:26:21 +1100746 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
747 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748
Damien Miller5428f641999-11-25 11:54:57 +1100749 /*
750 * Create socket early so it will exist before command gets run from
751 * the parent.
752 */
Damien Miller95def091999-11-25 00:26:21 +1100753 sock = socket(AF_UNIX, SOCK_STREAM, 0);
754 if (sock < 0) {
755 perror("socket");
756 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000757 }
Damien Miller95def091999-11-25 00:26:21 +1100758 memset(&sunaddr, 0, sizeof(sunaddr));
759 sunaddr.sun_family = AF_UNIX;
760 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
761 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
762 perror("bind");
763 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764 }
Damien Miller95def091999-11-25 00:26:21 +1100765 if (listen(sock, 5) < 0) {
766 perror("listen");
767 cleanup_exit(1);
768 }
Damien Miller5428f641999-11-25 11:54:57 +1100769 /*
770 * Fork, and have the parent execute the command, if any, or present
771 * the socket data. The child continues as the authentication agent.
772 */
Damien Miller95def091999-11-25 00:26:21 +1100773 pid = fork();
774 if (pid == -1) {
775 perror("fork");
776 exit(1);
777 }
778 if (pid != 0) { /* Parent - execute the given command. */
779 close(sock);
780 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
781 if (ac == 0) {
782 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
783 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
784 SSH_AUTHSOCKET_ENV_NAME);
785 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
786 SSH_AGENTPID_ENV_NAME);
787 printf("echo Agent pid %d;\n", pid);
788 exit(0);
789 }
Damien Millere4340be2000-09-16 13:29:08 +1100790 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
791 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
792 perror("setenv");
793 exit(1);
794 }
Damien Miller95def091999-11-25 00:26:21 +1100795 execvp(av[0], av);
796 perror(av[0]);
797 exit(1);
798 }
799 close(0);
800 close(1);
801 close(2);
802
803 if (setsid() == -1) {
804 perror("setsid");
805 cleanup_exit(1);
806 }
807 if (atexit(cleanup_socket) < 0) {
808 perror("atexit");
809 cleanup_exit(1);
810 }
811 new_socket(AUTH_SOCKET, sock);
812 if (ac > 0) {
813 signal(SIGALRM, check_parent_exists);
814 alarm(10);
815 }
Damien Millerad833b32000-08-23 10:46:23 +1000816 idtab_init();
Damien Miller95def091999-11-25 00:26:21 +1100817 signal(SIGINT, SIG_IGN);
818 signal(SIGPIPE, SIG_IGN);
Damien Miller4af51302000-04-16 11:18:38 +1000819 signal(SIGHUP, cleanup_exit);
820 signal(SIGTERM, cleanup_exit);
Damien Miller95def091999-11-25 00:26:21 +1100821 while (1) {
822 FD_ZERO(&readset);
823 FD_ZERO(&writeset);
824 prepare_select(&readset, &writeset);
825 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
826 if (errno == EINTR)
827 continue;
828 exit(1);
829 }
830 after_select(&readset, &writeset);
831 }
832 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833}