blob: 5620b6b90569ebac160ac033a45ab33f57d60d7e [file] [log] [blame]
Damien Millerda755162002-01-22 23:09:22 +11001/* $OpenBSD: ssh-agent.c,v 1.76 2001/12/27 18:22:16 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 *
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Millerda755162002-01-22 23:09:22 +110039RCSID("$OpenBSD: ssh-agent.c,v 1.76 2001/12/27 18:22:16 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040
41#include <openssl/evp.h>
42#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "ssh.h"
45#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "buffer.h"
47#include "bufaux.h"
48#include "xmalloc.h"
49#include "packet.h"
50#include "getput.h"
51#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100052#include "key.h"
53#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100055#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstrom3f471632001-07-04 03:53:15 +000059#ifdef SMARTCARD
60#include <openssl/engine.h>
61#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000062#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000063
Ben Lindstrom65366a82001-12-06 16:32:47 +000064typedef enum {
65 AUTH_UNUSED,
66 AUTH_SOCKET,
67 AUTH_CONNECTION
68} sock_type;
69
Damien Miller95def091999-11-25 00:26:21 +110070typedef struct {
71 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000072 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110073 Buffer input;
74 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075} SocketEntry;
76
Ben Lindstrom46c16222000-12-22 01:43:59 +000077u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078SocketEntry *sockets = NULL;
79
Damien Miller95def091999-11-25 00:26:21 +110080typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100081 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110082 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083} Identity;
84
Damien Millerad833b32000-08-23 10:46:23 +100085typedef struct {
86 int nentries;
87 Identity *identities;
88} Idtab;
89
90/* private key table, one per protocol version */
91Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93int max_fd = 0;
94
95/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100096pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* pathname and directory for AUTH_SOCKET */
99char socket_name[1024];
100char socket_dir[1024];
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 Millerad833b32000-08-23 10:46:23 +1000113 idtable[i].identities = NULL;
114 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 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000128static Key *
Damien Millerad833b32000-08-23 10:46:23 +1000129lookup_private_key(Key *key, int *idx, int version)
130{
131 int i;
132 Idtab *tab = idtab_lookup(version);
133 for (i = 0; i < tab->nentries; i++) {
134 if (key_equal(key, tab->identities[i].key)) {
135 if (idx != NULL)
136 *idx = i;
137 return tab->identities[i].key;
138 }
139 }
140 return NULL;
141}
142
143/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static void
Damien Millerad833b32000-08-23 10:46:23 +1000145process_request_identities(SocketEntry *e, int version)
146{
147 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100148 Buffer msg;
149 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000152 buffer_put_char(&msg, (version == 1) ?
153 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
154 buffer_put_int(&msg, tab->nentries);
155 for (i = 0; i < tab->nentries; i++) {
156 Identity *id = &tab->identities[i];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000158 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
159 buffer_put_bignum(&msg, id->key->rsa->e);
160 buffer_put_bignum(&msg, id->key->rsa->n);
161 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000162 u_char *blob;
163 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000165 buffer_put_string(&msg, blob, blen);
166 xfree(blob);
167 }
168 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100169 }
170 buffer_put_int(&e->output, buffer_len(&msg));
171 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
172 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Millerad833b32000-08-23 10:46:23 +1000175/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000176static void
Damien Millerad833b32000-08-23 10:46:23 +1000177process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178{
Damien Millerad833b32000-08-23 10:46:23 +1000179 Key *key, *private;
180 BIGNUM *challenge;
181 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100182 Buffer msg;
183 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000184 u_char buf[32], mdbuf[16], session_id[16];
185 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186
Damien Miller95def091999-11-25 00:26:21 +1100187 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100188 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100189 if ((challenge = BN_new()) == NULL)
190 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000191
192 buffer_get_int(&e->input); /* ignored */
193 buffer_get_bignum(&e->input, key->rsa->e);
194 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100195 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Millerad833b32000-08-23 10:46:23 +1000197 /* Only protocol 1.1 is supported */
198 if (buffer_len(&e->input) == 0)
199 goto failure;
200 buffer_get(&e->input, (char *) session_id, 16);
201 response_type = buffer_get_int(&e->input);
202 if (response_type != 1)
203 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Millerad833b32000-08-23 10:46:23 +1000205 private = lookup_private_key(key, NULL, 1);
206 if (private != NULL) {
207 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100208 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
209 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100210
Damien Millerad833b32000-08-23 10:46:23 +1000211 /* The response is MD5 of decrypted challenge plus session id. */
212 len = BN_num_bytes(challenge);
213 if (len <= 0 || len > 32) {
214 log("process_authentication_challenge: bad challenge length %d", len);
215 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100216 }
Damien Millerad833b32000-08-23 10:46:23 +1000217 memset(buf, 0, 32);
218 BN_bn2bin(challenge, buf + 32 - len);
219 MD5_Init(&md);
220 MD5_Update(&md, buf, 32);
221 MD5_Update(&md, session_id, 16);
222 MD5_Final(mdbuf, &md);
223
224 /* Send the response. */
225 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
226 for (i = 0; i < 16; i++)
227 buffer_put_char(&msg, mdbuf[i]);
228 goto send;
229 }
230
231failure:
232 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100233 buffer_put_char(&msg, SSH_AGENT_FAILURE);
234send:
235 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000236 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
237 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100238 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000239 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Millerad833b32000-08-23 10:46:23 +1000242/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Millerad833b32000-08-23 10:46:23 +1000244process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245{
Damien Millerad833b32000-08-23 10:46:23 +1000246 extern int datafellows;
247 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000248 u_char *blob, *data, *signature = NULL;
249 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100250 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000251 Buffer msg;
252 int ok = -1;
253
254 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000255
Damien Millerad833b32000-08-23 10:46:23 +1000256 blob = buffer_get_string(&e->input, &blen);
257 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100258
259 flags = buffer_get_int(&e->input);
260 if (flags & SSH_AGENT_OLD_SIGNATURE)
261 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000262
Damien Miller0bc1bd82000-11-13 22:57:25 +1100263 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000264 if (key != NULL) {
265 private = lookup_private_key(key, NULL, 2);
266 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100267 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000268 }
269 key_free(key);
270 buffer_init(&msg);
271 if (ok == 0) {
272 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
273 buffer_put_string(&msg, signature, slen);
274 } else {
275 buffer_put_char(&msg, SSH_AGENT_FAILURE);
276 }
277 buffer_put_int(&e->output, buffer_len(&msg));
278 buffer_append(&e->output, buffer_ptr(&msg),
279 buffer_len(&msg));
280 buffer_free(&msg);
281 xfree(data);
282 xfree(blob);
283 if (signature != NULL)
284 xfree(signature);
285}
286
287/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static void
Damien Millerad833b32000-08-23 10:46:23 +1000289process_remove_identity(SocketEntry *e, int version)
290{
291 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000292 u_char *blob;
293 u_int blen;
294 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000295 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100296
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000297 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000298 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100299 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000300 bits = buffer_get_int(&e->input);
301 buffer_get_bignum(&e->input, key->rsa->e);
302 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Millerad833b32000-08-23 10:46:23 +1000304 if (bits != key_size(key))
305 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000306 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000307 break;
308 case 2:
309 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100310 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000311 xfree(blob);
312 break;
313 }
314 if (key != NULL) {
315 int idx;
316 private = lookup_private_key(key, &idx, version);
317 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100318 /*
319 * We have this key. Free the old key. Since we
320 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000321 * the array, we actually free the key there and move
322 * all the entries between the empty slot and the end
323 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100324 */
Damien Millerad833b32000-08-23 10:46:23 +1000325 Idtab *tab = idtab_lookup(version);
326 key_free(tab->identities[idx].key);
327 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100328 if (tab->nentries < 1)
329 fatal("process_remove_identity: "
330 "internal error: tab->nentries %d",
331 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000332 if (idx != tab->nentries - 1) {
333 int i;
334 for (i = idx; i < tab->nentries - 1; i++)
335 tab->identities[i] = tab->identities[i+1];
336 }
337 tab->identities[tab->nentries - 1].key = NULL;
338 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000339 tab->nentries--;
340 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100341 }
Damien Millerad833b32000-08-23 10:46:23 +1000342 key_free(key);
343 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000345 buffer_put_char(&e->output,
346 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static void
Damien Millerad833b32000-08-23 10:46:23 +1000350process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000352 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000353 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000356 for (i = 0; i < tab->nentries; i++) {
357 key_free(tab->identities[i].key);
358 xfree(tab->identities[i].comment);
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 Lindstrom226cfa02001-01-22 05:34:40 +0000382 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 buffer_get_bignum(&e->input, k->rsa->n);
384 buffer_get_bignum(&e->input, k->rsa->e);
385 buffer_get_bignum(&e->input, k->rsa->d);
386 buffer_get_bignum(&e->input, 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 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 buffer_get_bignum(&e->input, k->rsa->q); /* p */
390 buffer_get_bignum(&e->input, 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:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 type_name = buffer_get_string(&e->input, 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);
402 buffer_get_bignum2(&e->input, k->dsa->p);
403 buffer_get_bignum2(&e->input, k->dsa->q);
404 buffer_get_bignum2(&e->input, k->dsa->g);
405 buffer_get_bignum2(&e->input, k->dsa->pub_key);
406 buffer_get_bignum2(&e->input, k->dsa->priv_key);
407 break;
408 case KEY_RSA:
409 k = key_new_private(type);
410 buffer_get_bignum2(&e->input, k->rsa->n);
411 buffer_get_bignum2(&e->input, k->rsa->e);
412 buffer_get_bignum2(&e->input, k->rsa->d);
413 buffer_get_bignum2(&e->input, k->rsa->iqmp);
414 buffer_get_bignum2(&e->input, k->rsa->p);
415 buffer_get_bignum2(&e->input, k->rsa->q);
416
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:
Damien Millerad833b32000-08-23 10:46:23 +1000421 buffer_clear(&e->input);
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 }
Damien Millerad833b32000-08-23 10:46:23 +1000426 comment = buffer_get_string(&e->input, NULL);
427 if (k == NULL) {
428 xfree(comment);
429 goto send;
430 }
431 success = 1;
432 if (lookup_private_key(k, NULL, version) == NULL) {
433 if (tab->nentries == 0)
434 tab->identities = xmalloc(sizeof(Identity));
435 else
436 tab->identities = xrealloc(tab->identities,
437 (tab->nentries + 1) * sizeof(Identity));
438 tab->identities[tab->nentries].key = k;
439 tab->identities[tab->nentries].comment = comment;
440 /* Increment the number of identities. */
441 tab->nentries++;
442 } else {
443 key_free(k);
444 xfree(comment);
445 }
446send:
Damien Miller95def091999-11-25 00:26:21 +1100447 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000448 buffer_put_char(&e->output,
449 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
451
Ben Lindstrom3f471632001-07-04 03:53:15 +0000452
453#ifdef SMARTCARD
454static void
455process_add_smartcard_key (SocketEntry *e)
456{
457 Idtab *tab;
458 Key *n = NULL, *k = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000459 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000460 int success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100461
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000462 sc_reader_id = buffer_get_string(&e->input, NULL);
463 k = sc_get_key(sc_reader_id);
464 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000465
Ben Lindstrom3f471632001-07-04 03:53:15 +0000466 if (k == NULL) {
467 error("sc_get_pubkey failed");
468 goto send;
469 }
470 success = 1;
471
472 tab = idtab_lookup(1);
Damien Millerf3512d92001-07-14 12:14:27 +1000473 k->type = KEY_RSA1;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000474 if (lookup_private_key(k, NULL, 1) == NULL) {
475 if (tab->nentries == 0)
476 tab->identities = xmalloc(sizeof(Identity));
477 else
478 tab->identities = xrealloc(tab->identities,
479 (tab->nentries + 1) * sizeof(Identity));
480 n = key_new(KEY_RSA1);
481 BN_copy(n->rsa->n, k->rsa->n);
482 BN_copy(n->rsa->e, k->rsa->e);
483 RSA_set_method(n->rsa, sc_get_engine());
484 tab->identities[tab->nentries].key = n;
485 tab->identities[tab->nentries].comment =
486 xstrdup("rsa1 smartcard");
487 tab->nentries++;
488 }
Damien Millerf3512d92001-07-14 12:14:27 +1000489 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000490 tab = idtab_lookup(2);
491 if (lookup_private_key(k, NULL, 2) == NULL) {
492 if (tab->nentries == 0)
493 tab->identities = xmalloc(sizeof(Identity));
494 else
495 tab->identities = xrealloc(tab->identities,
496 (tab->nentries + 1) * sizeof(Identity));
497 n = key_new(KEY_RSA);
498 BN_copy(n->rsa->n, k->rsa->n);
499 BN_copy(n->rsa->e, k->rsa->e);
500 RSA_set_method(n->rsa, sc_get_engine());
501 tab->identities[tab->nentries].key = n;
502 tab->identities[tab->nentries].comment =
503 xstrdup("rsa smartcard");
504 tab->nentries++;
505 }
506 key_free(k);
507send:
508 buffer_put_int(&e->output, 1);
509 buffer_put_char(&e->output,
510 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
511}
512
513static void
514process_remove_smartcard_key(SocketEntry *e)
515{
516 Key *k = NULL, *private;
517 int idx;
518 int success = 0;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000519 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000520
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000521 sc_reader_id = buffer_get_string(&e->input, NULL);
522 k = sc_get_key(sc_reader_id);
523 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000524
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000525 if (k == NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000526 error("sc_get_pubkey failed");
527 } else {
Damien Miller8d4bf172001-07-14 12:13:49 +1000528 k->type = KEY_RSA1;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000529 private = lookup_private_key(k, &idx, 1);
530 if (private != NULL) {
531 Idtab *tab = idtab_lookup(1);
532 key_free(tab->identities[idx].key);
533 xfree(tab->identities[idx].comment);
534 if (idx != tab->nentries)
535 tab->identities[idx] = tab->identities[tab->nentries];
536 tab->nentries--;
537 success = 1;
538 }
Damien Miller8d4bf172001-07-14 12:13:49 +1000539 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000540 private = lookup_private_key(k, &idx, 2);
541 if (private != NULL) {
542 Idtab *tab = idtab_lookup(2);
543 key_free(tab->identities[idx].key);
544 xfree(tab->identities[idx].comment);
545 if (idx != tab->nentries)
546 tab->identities[idx] = tab->identities[tab->nentries];
547 tab->nentries--;
548 success = 1;
549 }
550 key_free(k);
551 }
552
553 buffer_put_int(&e->output, 1);
554 buffer_put_char(&e->output,
555 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
556}
Ben Lindstromffce1472001-08-06 21:57:31 +0000557#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000558
Damien Millerad833b32000-08-23 10:46:23 +1000559/* dispatch incoming messages */
560
Ben Lindstrombba81212001-06-25 05:01:22 +0000561static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562process_message(SocketEntry *e)
563{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000564 u_int msg_len;
565 u_int type;
566 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100567 if (buffer_len(&e->input) < 5)
568 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000569 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100570 msg_len = GET_32BIT(cp);
571 if (msg_len > 256 * 1024) {
572 shutdown(e->fd, SHUT_RDWR);
573 close(e->fd);
574 e->type = AUTH_UNUSED;
575 return;
576 }
577 if (buffer_len(&e->input) < msg_len + 4)
578 return;
579 buffer_consume(&e->input, 4);
580 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Ben Lindstrom3f471632001-07-04 03:53:15 +0000582 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100583 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000584 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100585 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000586 process_authentication_challenge1(e);
587 break;
588 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
589 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100590 break;
591 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000592 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100593 break;
594 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000595 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100596 break;
597 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000598 process_remove_all_identities(e, 1);
599 break;
600 /* ssh2 */
601 case SSH2_AGENTC_SIGN_REQUEST:
602 process_sign_request2(e);
603 break;
604 case SSH2_AGENTC_REQUEST_IDENTITIES:
605 process_request_identities(e, 2);
606 break;
607 case SSH2_AGENTC_ADD_IDENTITY:
608 process_add_identity(e, 2);
609 break;
610 case SSH2_AGENTC_REMOVE_IDENTITY:
611 process_remove_identity(e, 2);
612 break;
613 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
614 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100615 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000616#ifdef SMARTCARD
617 case SSH_AGENTC_ADD_SMARTCARD_KEY:
618 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100619 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000620 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
621 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100622 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000623#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100624 default:
625 /* Unknown message. Respond with failure. */
626 error("Unknown message %d", type);
627 buffer_clear(&e->input);
628 buffer_put_int(&e->output, 1);
629 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
630 break;
631 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632}
633
Ben Lindstrombba81212001-06-25 05:01:22 +0000634static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000635new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000637 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100638 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
639 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Miller95def091999-11-25 00:26:21 +1100641 if (fd > max_fd)
642 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Damien Miller95def091999-11-25 00:26:21 +1100644 for (i = 0; i < sockets_alloc; i++)
645 if (sockets[i].type == AUTH_UNUSED) {
646 sockets[i].fd = fd;
647 sockets[i].type = type;
648 buffer_init(&sockets[i].input);
649 buffer_init(&sockets[i].output);
650 return;
651 }
652 old_alloc = sockets_alloc;
653 sockets_alloc += 10;
654 if (sockets)
655 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
656 else
657 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
658 for (i = old_alloc; i < sockets_alloc; i++)
659 sockets[i].type = AUTH_UNUSED;
660 sockets[old_alloc].type = type;
661 sockets[old_alloc].fd = fd;
662 buffer_init(&sockets[old_alloc].input);
663 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664}
665
Ben Lindstrombba81212001-06-25 05:01:22 +0000666static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000667prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000669 u_int i, sz;
670 int n = 0;
671
672 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100673 switch (sockets[i].type) {
674 case AUTH_SOCKET:
675 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000676 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100677 break;
678 case AUTH_UNUSED:
679 break;
680 default:
681 fatal("Unknown socket type %d", sockets[i].type);
682 break;
683 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000684 }
685
686 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000687 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000688 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000689 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000690 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000691 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000692 *fdrp = xmalloc(sz);
693 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000694 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000695 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000696 if (n < *fdl)
697 debug("XXX shrink: %d < %d", n, *fdl);
698 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000699 memset(*fdrp, 0, sz);
700 memset(*fdwp, 0, sz);
701
702 for (i = 0; i < sockets_alloc; i++) {
703 switch (sockets[i].type) {
704 case AUTH_SOCKET:
705 case AUTH_CONNECTION:
706 FD_SET(sockets[i].fd, *fdrp);
707 if (buffer_len(&sockets[i].output) > 0)
708 FD_SET(sockets[i].fd, *fdwp);
709 break;
710 default:
711 break;
712 }
713 }
714 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
Ben Lindstrombba81212001-06-25 05:01:22 +0000717static void
Damien Miller95def091999-11-25 00:26:21 +1100718after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000720 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100721 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100722 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100723 char buf[1024];
724 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller95def091999-11-25 00:26:21 +1100726 for (i = 0; i < sockets_alloc; i++)
727 switch (sockets[i].type) {
728 case AUTH_UNUSED:
729 break;
730 case AUTH_SOCKET:
731 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100732 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000733 sock = accept(sockets[i].fd,
734 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100735 if (sock < 0) {
736 perror("accept from AUTH_SOCKET");
737 break;
738 }
739 new_socket(AUTH_CONNECTION, sock);
740 }
741 break;
742 case AUTH_CONNECTION:
743 if (buffer_len(&sockets[i].output) > 0 &&
744 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000745 do {
746 len = write(sockets[i].fd,
747 buffer_ptr(&sockets[i].output),
748 buffer_len(&sockets[i].output));
749 if (len == -1 && (errno == EAGAIN ||
750 errno == EINTR))
751 continue;
752 break;
753 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100754 if (len <= 0) {
755 shutdown(sockets[i].fd, SHUT_RDWR);
756 close(sockets[i].fd);
757 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000758 buffer_free(&sockets[i].input);
759 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100760 break;
761 }
762 buffer_consume(&sockets[i].output, len);
763 }
764 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000765 do {
766 len = read(sockets[i].fd, buf, sizeof(buf));
767 if (len == -1 && (errno == EAGAIN ||
768 errno == EINTR))
769 continue;
770 break;
771 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100772 if (len <= 0) {
773 shutdown(sockets[i].fd, SHUT_RDWR);
774 close(sockets[i].fd);
775 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000776 buffer_free(&sockets[i].input);
777 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100778 break;
779 }
780 buffer_append(&sockets[i].input, buf, len);
781 process_message(&sockets[i]);
782 }
783 break;
784 default:
785 fatal("Unknown type %d", sockets[i].type);
786 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787}
788
Ben Lindstrombba81212001-06-25 05:01:22 +0000789static void
Damien Miller792c5111999-10-29 09:47:09 +1000790cleanup_socket(void)
791{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000792 if (socket_name[0])
793 unlink(socket_name);
794 if (socket_dir[0])
795 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796}
797
Ben Lindstrombba81212001-06-25 05:01:22 +0000798static void
Damien Miller792c5111999-10-29 09:47:09 +1000799cleanup_exit(int i)
800{
Damien Miller95def091999-11-25 00:26:21 +1100801 cleanup_socket();
802 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000803}
804
Ben Lindstrombba81212001-06-25 05:01:22 +0000805static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000806cleanup_handler(int sig)
807{
808 cleanup_socket();
809 _exit(2);
810}
811
Ben Lindstrombba81212001-06-25 05:01:22 +0000812static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000813check_parent_exists(int sig)
814{
815 int save_errno = errno;
816
817 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
818 /* printf("Parent has died - Authentication agent exiting.\n"); */
819 cleanup_handler(sig); /* safe */
820 }
821 signal(SIGALRM, check_parent_exists);
822 alarm(10);
823 errno = save_errno;
824}
825
826static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000827usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000828{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000829 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000830 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000831 fprintf(stderr, "Options:\n");
832 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
833 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
834 fprintf(stderr, " -k Kill the current agent.\n");
835 fprintf(stderr, " -d Debug mode.\n");
Damien Miller95def091999-11-25 00:26:21 +1100836 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000837}
838
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839int
840main(int ac, char **av)
841{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000842 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100843 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000844#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000845 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000846#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000847#ifdef HAVE_CYGWIN
848 int prev_mask;
849#endif
Damien Miller95def091999-11-25 00:26:21 +1100850 pid_t pid;
851 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000852 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000853 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000854
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000855 SSLeay_add_all_algorithms();
856
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000857 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000858 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100859 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000860
Damien Millerd0cff3e2000-04-20 23:12:58 +1000861#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000862 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000863#else /* __GNU_LIBRARY__ */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000864 while ((ch = getopt(ac, av, "cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000865#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100866 switch (ch) {
867 case 'c':
868 if (s_flag)
869 usage();
870 c_flag++;
871 break;
872 case 'k':
873 k_flag++;
874 break;
875 case 's':
876 if (c_flag)
877 usage();
878 s_flag++;
879 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000880 case 'd':
881 if (d_flag)
882 usage();
883 d_flag++;
884 break;
Damien Miller95def091999-11-25 00:26:21 +1100885 default:
886 usage();
887 }
Damien Miller792c5111999-10-29 09:47:09 +1000888 }
Damien Miller95def091999-11-25 00:26:21 +1100889 ac -= optind;
890 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000891
Ben Lindstromd94580c2001-07-04 03:48:02 +0000892 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100893 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000894
Ben Lindstromd94580c2001-07-04 03:48:02 +0000895 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100896 shell = getenv("SHELL");
897 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
898 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 if (k_flag) {
901 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
902 if (pidstr == NULL) {
903 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000904 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100905 exit(1);
906 }
907 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000908 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100909 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000910 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100911 exit(1);
912 }
913 if (kill(pid, SIGTERM) == -1) {
914 perror("kill");
915 exit(1);
916 }
917 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
918 printf(format, SSH_AUTHSOCKET_ENV_NAME);
919 printf(format, SSH_AGENTPID_ENV_NAME);
920 printf("echo Agent pid %d killed;\n", pid);
921 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000922 }
Damien Miller95def091999-11-25 00:26:21 +1100923 parent_pid = getpid();
924
925 /* Create private directory for agent socket */
926 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
927 if (mkdtemp(socket_dir) == NULL) {
928 perror("mkdtemp: private socket dir");
929 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000930 }
Damien Miller95def091999-11-25 00:26:21 +1100931 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000932 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller5428f641999-11-25 11:54:57 +1100934 /*
935 * Create socket early so it will exist before command gets run from
936 * the parent.
937 */
Damien Miller95def091999-11-25 00:26:21 +1100938 sock = socket(AF_UNIX, SOCK_STREAM, 0);
939 if (sock < 0) {
940 perror("socket");
941 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000942 }
Damien Miller95def091999-11-25 00:26:21 +1100943 memset(&sunaddr, 0, sizeof(sunaddr));
944 sunaddr.sun_family = AF_UNIX;
945 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000946#ifdef HAVE_CYGWIN
947 prev_mask = umask(0177);
948#endif
Damien Miller95def091999-11-25 00:26:21 +1100949 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
950 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000951#ifdef HAVE_CYGWIN
952 umask(prev_mask);
953#endif
Damien Miller95def091999-11-25 00:26:21 +1100954 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000956#ifdef HAVE_CYGWIN
957 umask(prev_mask);
958#endif
Damien Miller95def091999-11-25 00:26:21 +1100959 if (listen(sock, 5) < 0) {
960 perror("listen");
961 cleanup_exit(1);
962 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000963
Damien Miller5428f641999-11-25 11:54:57 +1100964 /*
965 * Fork, and have the parent execute the command, if any, or present
966 * the socket data. The child continues as the authentication agent.
967 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000968 if (d_flag) {
969 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
970 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
971 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
972 SSH_AUTHSOCKET_ENV_NAME);
973 printf("echo Agent pid %d;\n", parent_pid);
974 goto skip;
975 }
Damien Miller95def091999-11-25 00:26:21 +1100976 pid = fork();
977 if (pid == -1) {
978 perror("fork");
979 exit(1);
980 }
981 if (pid != 0) { /* Parent - execute the given command. */
982 close(sock);
983 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
984 if (ac == 0) {
985 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
986 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000987 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100988 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000989 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100990 printf("echo Agent pid %d;\n", pid);
991 exit(0);
992 }
Damien Millere4340be2000-09-16 13:29:08 +1100993 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
994 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
995 perror("setenv");
996 exit(1);
997 }
Damien Miller95def091999-11-25 00:26:21 +1100998 execvp(av[0], av);
999 perror(av[0]);
1000 exit(1);
1001 }
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001002
1003 if (setsid() == -1) {
1004 perror("setsid");
1005 cleanup_exit(1);
1006 }
1007
1008 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001009 close(0);
1010 close(1);
1011 close(2);
1012
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001014 /* deny core dumps, since memory contains unencrypted private keys */
1015 rlim.rlim_cur = rlim.rlim_max = 0;
1016 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1017 perror("setrlimit rlimit_core failed");
1018 cleanup_exit(1);
1019 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001020#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001021
1022skip:
Damien Miller95def091999-11-25 00:26:21 +11001023 if (atexit(cleanup_socket) < 0) {
1024 perror("atexit");
1025 cleanup_exit(1);
1026 }
1027 new_socket(AUTH_SOCKET, sock);
1028 if (ac > 0) {
1029 signal(SIGALRM, check_parent_exists);
1030 alarm(10);
1031 }
Damien Millerad833b32000-08-23 10:46:23 +10001032 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001033 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001034 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001035 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001036 signal(SIGHUP, cleanup_handler);
1037 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001038 nalloc = 0;
1039
Damien Miller95def091999-11-25 00:26:21 +11001040 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001041 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001042 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001043 if (errno == EINTR)
1044 continue;
1045 exit(1);
1046 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001047 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001048 }
1049 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050}