blob: e8018bf3aacbecf1100924927ac42e60ffbaa81f [file] [log] [blame]
Damien Miller9f0f5c62001-12-21 14:45:46 +11001/* $OpenBSD: ssh-agent.c,v 1.75 2001/12/19 07:18:56 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 *
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 Miller9f0f5c62001-12-21 14:45:46 +110039RCSID("$OpenBSD: ssh-agent.c,v 1.75 2001/12/19 07:18:56 deraadt 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 Miller95def091999-11-25 00:26:21 +1100189 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000190
191 buffer_get_int(&e->input); /* ignored */
192 buffer_get_bignum(&e->input, key->rsa->e);
193 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100194 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Millerad833b32000-08-23 10:46:23 +1000196 /* Only protocol 1.1 is supported */
197 if (buffer_len(&e->input) == 0)
198 goto failure;
199 buffer_get(&e->input, (char *) session_id, 16);
200 response_type = buffer_get_int(&e->input);
201 if (response_type != 1)
202 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Millerad833b32000-08-23 10:46:23 +1000204 private = lookup_private_key(key, NULL, 1);
205 if (private != NULL) {
206 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100207 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
208 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100209
Damien Millerad833b32000-08-23 10:46:23 +1000210 /* The response is MD5 of decrypted challenge plus session id. */
211 len = BN_num_bytes(challenge);
212 if (len <= 0 || len > 32) {
213 log("process_authentication_challenge: bad challenge length %d", len);
214 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100215 }
Damien Millerad833b32000-08-23 10:46:23 +1000216 memset(buf, 0, 32);
217 BN_bn2bin(challenge, buf + 32 - len);
218 MD5_Init(&md);
219 MD5_Update(&md, buf, 32);
220 MD5_Update(&md, session_id, 16);
221 MD5_Final(mdbuf, &md);
222
223 /* Send the response. */
224 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
225 for (i = 0; i < 16; i++)
226 buffer_put_char(&msg, mdbuf[i]);
227 goto send;
228 }
229
230failure:
231 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100232 buffer_put_char(&msg, SSH_AGENT_FAILURE);
233send:
234 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000235 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
236 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100237 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000238 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Millerad833b32000-08-23 10:46:23 +1000241/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000242static void
Damien Millerad833b32000-08-23 10:46:23 +1000243process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Damien Millerad833b32000-08-23 10:46:23 +1000245 extern int datafellows;
246 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000247 u_char *blob, *data, *signature = NULL;
248 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100249 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000250 Buffer msg;
251 int ok = -1;
252
253 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000254
Damien Millerad833b32000-08-23 10:46:23 +1000255 blob = buffer_get_string(&e->input, &blen);
256 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100257
258 flags = buffer_get_int(&e->input);
259 if (flags & SSH_AGENT_OLD_SIGNATURE)
260 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000261
Damien Miller0bc1bd82000-11-13 22:57:25 +1100262 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000263 if (key != NULL) {
264 private = lookup_private_key(key, NULL, 2);
265 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100266 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000267 }
268 key_free(key);
269 buffer_init(&msg);
270 if (ok == 0) {
271 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
272 buffer_put_string(&msg, signature, slen);
273 } else {
274 buffer_put_char(&msg, SSH_AGENT_FAILURE);
275 }
276 buffer_put_int(&e->output, buffer_len(&msg));
277 buffer_append(&e->output, buffer_ptr(&msg),
278 buffer_len(&msg));
279 buffer_free(&msg);
280 xfree(data);
281 xfree(blob);
282 if (signature != NULL)
283 xfree(signature);
284}
285
286/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000287static void
Damien Millerad833b32000-08-23 10:46:23 +1000288process_remove_identity(SocketEntry *e, int version)
289{
290 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000291 u_char *blob;
292 u_int blen;
293 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000294 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100295
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000296 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000297 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100298 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000299 bits = buffer_get_int(&e->input);
300 buffer_get_bignum(&e->input, key->rsa->e);
301 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Millerad833b32000-08-23 10:46:23 +1000303 if (bits != key_size(key))
304 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000305 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000306 break;
307 case 2:
308 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100309 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000310 xfree(blob);
311 break;
312 }
313 if (key != NULL) {
314 int idx;
315 private = lookup_private_key(key, &idx, version);
316 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100317 /*
318 * We have this key. Free the old key. Since we
319 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000320 * the array, we actually free the key there and move
321 * all the entries between the empty slot and the end
322 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100323 */
Damien Millerad833b32000-08-23 10:46:23 +1000324 Idtab *tab = idtab_lookup(version);
325 key_free(tab->identities[idx].key);
326 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100327 if (tab->nentries < 1)
328 fatal("process_remove_identity: "
329 "internal error: tab->nentries %d",
330 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000331 if (idx != tab->nentries - 1) {
332 int i;
333 for (i = idx; i < tab->nentries - 1; i++)
334 tab->identities[i] = tab->identities[i+1];
335 }
336 tab->identities[tab->nentries - 1].key = NULL;
337 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000338 tab->nentries--;
339 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100340 }
Damien Millerad833b32000-08-23 10:46:23 +1000341 key_free(key);
342 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000344 buffer_put_char(&e->output,
345 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Damien Millerad833b32000-08-23 10:46:23 +1000349process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000351 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000352 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000355 for (i = 0; i < tab->nentries; i++) {
356 key_free(tab->identities[i].key);
357 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100358 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Miller95def091999-11-25 00:26:21 +1100360 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000361 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
363 /* Send success. */
364 buffer_put_int(&e->output, 1);
365 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
366 return;
Damien Miller95def091999-11-25 00:26:21 +1100367}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static void
Damien Millerad833b32000-08-23 10:46:23 +1000370process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100371{
Damien Millerad833b32000-08-23 10:46:23 +1000372 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100373 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000374 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000376 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100377
Damien Millerad833b32000-08-23 10:46:23 +1000378 switch (version) {
379 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000381 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382 buffer_get_bignum(&e->input, k->rsa->n);
383 buffer_get_bignum(&e->input, k->rsa->e);
384 buffer_get_bignum(&e->input, k->rsa->d);
385 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100386
Damien Millerad833b32000-08-23 10:46:23 +1000387 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100388 buffer_get_bignum(&e->input, k->rsa->q); /* p */
389 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100390
Damien Millerad833b32000-08-23 10:46:23 +1000391 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000392 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000393 break;
394 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000396 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000398 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 case KEY_DSA:
400 k = key_new_private(type);
401 buffer_get_bignum2(&e->input, k->dsa->p);
402 buffer_get_bignum2(&e->input, k->dsa->q);
403 buffer_get_bignum2(&e->input, k->dsa->g);
404 buffer_get_bignum2(&e->input, k->dsa->pub_key);
405 buffer_get_bignum2(&e->input, k->dsa->priv_key);
406 break;
407 case KEY_RSA:
408 k = key_new_private(type);
409 buffer_get_bignum2(&e->input, k->rsa->n);
410 buffer_get_bignum2(&e->input, k->rsa->e);
411 buffer_get_bignum2(&e->input, k->rsa->d);
412 buffer_get_bignum2(&e->input, k->rsa->iqmp);
413 buffer_get_bignum2(&e->input, k->rsa->p);
414 buffer_get_bignum2(&e->input, k->rsa->q);
415
416 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000417 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100418 break;
419 default:
Damien Millerad833b32000-08-23 10:46:23 +1000420 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000421 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100422 }
Damien Millerad833b32000-08-23 10:46:23 +1000423 break;
424 }
Damien Millerad833b32000-08-23 10:46:23 +1000425 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
Ben Lindstrom3f471632001-07-04 03:53:15 +0000451
452#ifdef SMARTCARD
453static void
454process_add_smartcard_key (SocketEntry *e)
455{
456 Idtab *tab;
457 Key *n = NULL, *k = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000458 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000459 int success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100460
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000461 sc_reader_id = buffer_get_string(&e->input, NULL);
462 k = sc_get_key(sc_reader_id);
463 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000464
Ben Lindstrom3f471632001-07-04 03:53:15 +0000465 if (k == NULL) {
466 error("sc_get_pubkey failed");
467 goto send;
468 }
469 success = 1;
470
471 tab = idtab_lookup(1);
Damien Millerf3512d92001-07-14 12:14:27 +1000472 k->type = KEY_RSA1;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000473 if (lookup_private_key(k, NULL, 1) == NULL) {
474 if (tab->nentries == 0)
475 tab->identities = xmalloc(sizeof(Identity));
476 else
477 tab->identities = xrealloc(tab->identities,
478 (tab->nentries + 1) * sizeof(Identity));
479 n = key_new(KEY_RSA1);
480 BN_copy(n->rsa->n, k->rsa->n);
481 BN_copy(n->rsa->e, k->rsa->e);
482 RSA_set_method(n->rsa, sc_get_engine());
483 tab->identities[tab->nentries].key = n;
484 tab->identities[tab->nentries].comment =
485 xstrdup("rsa1 smartcard");
486 tab->nentries++;
487 }
Damien Millerf3512d92001-07-14 12:14:27 +1000488 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000489 tab = idtab_lookup(2);
490 if (lookup_private_key(k, NULL, 2) == NULL) {
491 if (tab->nentries == 0)
492 tab->identities = xmalloc(sizeof(Identity));
493 else
494 tab->identities = xrealloc(tab->identities,
495 (tab->nentries + 1) * sizeof(Identity));
496 n = key_new(KEY_RSA);
497 BN_copy(n->rsa->n, k->rsa->n);
498 BN_copy(n->rsa->e, k->rsa->e);
499 RSA_set_method(n->rsa, sc_get_engine());
500 tab->identities[tab->nentries].key = n;
501 tab->identities[tab->nentries].comment =
502 xstrdup("rsa smartcard");
503 tab->nentries++;
504 }
505 key_free(k);
506send:
507 buffer_put_int(&e->output, 1);
508 buffer_put_char(&e->output,
509 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
510}
511
512static void
513process_remove_smartcard_key(SocketEntry *e)
514{
515 Key *k = NULL, *private;
516 int idx;
517 int success = 0;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000518 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000519
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000520 sc_reader_id = buffer_get_string(&e->input, NULL);
521 k = sc_get_key(sc_reader_id);
522 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000523
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000524 if (k == NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000525 error("sc_get_pubkey failed");
526 } else {
Damien Miller8d4bf172001-07-14 12:13:49 +1000527 k->type = KEY_RSA1;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000528 private = lookup_private_key(k, &idx, 1);
529 if (private != NULL) {
530 Idtab *tab = idtab_lookup(1);
531 key_free(tab->identities[idx].key);
532 xfree(tab->identities[idx].comment);
533 if (idx != tab->nentries)
534 tab->identities[idx] = tab->identities[tab->nentries];
535 tab->nentries--;
536 success = 1;
537 }
Damien Miller8d4bf172001-07-14 12:13:49 +1000538 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000539 private = lookup_private_key(k, &idx, 2);
540 if (private != NULL) {
541 Idtab *tab = idtab_lookup(2);
542 key_free(tab->identities[idx].key);
543 xfree(tab->identities[idx].comment);
544 if (idx != tab->nentries)
545 tab->identities[idx] = tab->identities[tab->nentries];
546 tab->nentries--;
547 success = 1;
548 }
549 key_free(k);
550 }
551
552 buffer_put_int(&e->output, 1);
553 buffer_put_char(&e->output,
554 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
555}
Ben Lindstromffce1472001-08-06 21:57:31 +0000556#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000557
Damien Millerad833b32000-08-23 10:46:23 +1000558/* dispatch incoming messages */
559
Ben Lindstrombba81212001-06-25 05:01:22 +0000560static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561process_message(SocketEntry *e)
562{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000563 u_int msg_len;
564 u_int type;
565 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100566 if (buffer_len(&e->input) < 5)
567 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000568 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100569 msg_len = GET_32BIT(cp);
570 if (msg_len > 256 * 1024) {
571 shutdown(e->fd, SHUT_RDWR);
572 close(e->fd);
573 e->type = AUTH_UNUSED;
574 return;
575 }
576 if (buffer_len(&e->input) < msg_len + 4)
577 return;
578 buffer_consume(&e->input, 4);
579 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Ben Lindstrom3f471632001-07-04 03:53:15 +0000581 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100582 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000583 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100584 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000585 process_authentication_challenge1(e);
586 break;
587 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
588 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100589 break;
590 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000591 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100592 break;
593 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000594 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100595 break;
596 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000597 process_remove_all_identities(e, 1);
598 break;
599 /* ssh2 */
600 case SSH2_AGENTC_SIGN_REQUEST:
601 process_sign_request2(e);
602 break;
603 case SSH2_AGENTC_REQUEST_IDENTITIES:
604 process_request_identities(e, 2);
605 break;
606 case SSH2_AGENTC_ADD_IDENTITY:
607 process_add_identity(e, 2);
608 break;
609 case SSH2_AGENTC_REMOVE_IDENTITY:
610 process_remove_identity(e, 2);
611 break;
612 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
613 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100614 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615#ifdef SMARTCARD
616 case SSH_AGENTC_ADD_SMARTCARD_KEY:
617 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000619 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
620 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100621 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000622#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100623 default:
624 /* Unknown message. Respond with failure. */
625 error("Unknown message %d", type);
626 buffer_clear(&e->input);
627 buffer_put_int(&e->output, 1);
628 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
629 break;
630 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631}
632
Ben Lindstrombba81212001-06-25 05:01:22 +0000633static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000634new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000636 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100637 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
638 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Damien Miller95def091999-11-25 00:26:21 +1100640 if (fd > max_fd)
641 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 for (i = 0; i < sockets_alloc; i++)
644 if (sockets[i].type == AUTH_UNUSED) {
645 sockets[i].fd = fd;
646 sockets[i].type = type;
647 buffer_init(&sockets[i].input);
648 buffer_init(&sockets[i].output);
649 return;
650 }
651 old_alloc = sockets_alloc;
652 sockets_alloc += 10;
653 if (sockets)
654 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
655 else
656 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
657 for (i = old_alloc; i < sockets_alloc; i++)
658 sockets[i].type = AUTH_UNUSED;
659 sockets[old_alloc].type = type;
660 sockets[old_alloc].fd = fd;
661 buffer_init(&sockets[old_alloc].input);
662 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663}
664
Ben Lindstrombba81212001-06-25 05:01:22 +0000665static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000666prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000668 u_int i, sz;
669 int n = 0;
670
671 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100672 switch (sockets[i].type) {
673 case AUTH_SOCKET:
674 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000675 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100676 break;
677 case AUTH_UNUSED:
678 break;
679 default:
680 fatal("Unknown socket type %d", sockets[i].type);
681 break;
682 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000683 }
684
685 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000686 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000687 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000688 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000689 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000690 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000691 *fdrp = xmalloc(sz);
692 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000693 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000694 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000695 if (n < *fdl)
696 debug("XXX shrink: %d < %d", n, *fdl);
697 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000698 memset(*fdrp, 0, sz);
699 memset(*fdwp, 0, sz);
700
701 for (i = 0; i < sockets_alloc; i++) {
702 switch (sockets[i].type) {
703 case AUTH_SOCKET:
704 case AUTH_CONNECTION:
705 FD_SET(sockets[i].fd, *fdrp);
706 if (buffer_len(&sockets[i].output) > 0)
707 FD_SET(sockets[i].fd, *fdwp);
708 break;
709 default:
710 break;
711 }
712 }
713 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714}
715
Ben Lindstrombba81212001-06-25 05:01:22 +0000716static void
Damien Miller95def091999-11-25 00:26:21 +1100717after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000719 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100720 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100721 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100722 char buf[1024];
723 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller95def091999-11-25 00:26:21 +1100725 for (i = 0; i < sockets_alloc; i++)
726 switch (sockets[i].type) {
727 case AUTH_UNUSED:
728 break;
729 case AUTH_SOCKET:
730 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100731 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000732 sock = accept(sockets[i].fd,
733 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100734 if (sock < 0) {
735 perror("accept from AUTH_SOCKET");
736 break;
737 }
738 new_socket(AUTH_CONNECTION, sock);
739 }
740 break;
741 case AUTH_CONNECTION:
742 if (buffer_len(&sockets[i].output) > 0 &&
743 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000744 do {
745 len = write(sockets[i].fd,
746 buffer_ptr(&sockets[i].output),
747 buffer_len(&sockets[i].output));
748 if (len == -1 && (errno == EAGAIN ||
749 errno == EINTR))
750 continue;
751 break;
752 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100753 if (len <= 0) {
754 shutdown(sockets[i].fd, SHUT_RDWR);
755 close(sockets[i].fd);
756 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000757 buffer_free(&sockets[i].input);
758 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100759 break;
760 }
761 buffer_consume(&sockets[i].output, len);
762 }
763 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000764 do {
765 len = read(sockets[i].fd, buf, sizeof(buf));
766 if (len == -1 && (errno == EAGAIN ||
767 errno == EINTR))
768 continue;
769 break;
770 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100771 if (len <= 0) {
772 shutdown(sockets[i].fd, SHUT_RDWR);
773 close(sockets[i].fd);
774 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000775 buffer_free(&sockets[i].input);
776 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100777 break;
778 }
779 buffer_append(&sockets[i].input, buf, len);
780 process_message(&sockets[i]);
781 }
782 break;
783 default:
784 fatal("Unknown type %d", sockets[i].type);
785 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Damien Miller792c5111999-10-29 09:47:09 +1000789cleanup_socket(void)
790{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000791 if (socket_name[0])
792 unlink(socket_name);
793 if (socket_dir[0])
794 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
Ben Lindstrombba81212001-06-25 05:01:22 +0000797static void
Damien Miller792c5111999-10-29 09:47:09 +1000798cleanup_exit(int i)
799{
Damien Miller95def091999-11-25 00:26:21 +1100800 cleanup_socket();
801 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000802}
803
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000805cleanup_handler(int sig)
806{
807 cleanup_socket();
808 _exit(2);
809}
810
Ben Lindstrombba81212001-06-25 05:01:22 +0000811static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000812check_parent_exists(int sig)
813{
814 int save_errno = errno;
815
816 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
817 /* printf("Parent has died - Authentication agent exiting.\n"); */
818 cleanup_handler(sig); /* safe */
819 }
820 signal(SIGALRM, check_parent_exists);
821 alarm(10);
822 errno = save_errno;
823}
824
825static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000826usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000827{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000828 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000829 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000830 fprintf(stderr, "Options:\n");
831 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
832 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
833 fprintf(stderr, " -k Kill the current agent.\n");
834 fprintf(stderr, " -d Debug mode.\n");
Damien Miller95def091999-11-25 00:26:21 +1100835 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000836}
837
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838int
839main(int ac, char **av)
840{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000841 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100842 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000843#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000844 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000845#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000846#ifdef HAVE_CYGWIN
847 int prev_mask;
848#endif
Damien Miller95def091999-11-25 00:26:21 +1100849 pid_t pid;
850 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000851 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000853
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000854 SSLeay_add_all_algorithms();
855
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000856 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000857 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100858 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000859
Damien Millerd0cff3e2000-04-20 23:12:58 +1000860#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000861 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000862#else /* __GNU_LIBRARY__ */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000863 while ((ch = getopt(ac, av, "cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000864#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100865 switch (ch) {
866 case 'c':
867 if (s_flag)
868 usage();
869 c_flag++;
870 break;
871 case 'k':
872 k_flag++;
873 break;
874 case 's':
875 if (c_flag)
876 usage();
877 s_flag++;
878 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000879 case 'd':
880 if (d_flag)
881 usage();
882 d_flag++;
883 break;
Damien Miller95def091999-11-25 00:26:21 +1100884 default:
885 usage();
886 }
Damien Miller792c5111999-10-29 09:47:09 +1000887 }
Damien Miller95def091999-11-25 00:26:21 +1100888 ac -= optind;
889 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000890
Ben Lindstromd94580c2001-07-04 03:48:02 +0000891 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100892 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000893
Ben Lindstromd94580c2001-07-04 03:48:02 +0000894 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100895 shell = getenv("SHELL");
896 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
897 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000898 }
Damien Miller95def091999-11-25 00:26:21 +1100899 if (k_flag) {
900 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
901 if (pidstr == NULL) {
902 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000903 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100904 exit(1);
905 }
906 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000907 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100908 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000909 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100910 exit(1);
911 }
912 if (kill(pid, SIGTERM) == -1) {
913 perror("kill");
914 exit(1);
915 }
916 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
917 printf(format, SSH_AUTHSOCKET_ENV_NAME);
918 printf(format, SSH_AGENTPID_ENV_NAME);
919 printf("echo Agent pid %d killed;\n", pid);
920 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000921 }
Damien Miller95def091999-11-25 00:26:21 +1100922 parent_pid = getpid();
923
924 /* Create private directory for agent socket */
925 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
926 if (mkdtemp(socket_dir) == NULL) {
927 perror("mkdtemp: private socket dir");
928 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000929 }
Damien Miller95def091999-11-25 00:26:21 +1100930 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000931 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932
Damien Miller5428f641999-11-25 11:54:57 +1100933 /*
934 * Create socket early so it will exist before command gets run from
935 * the parent.
936 */
Damien Miller95def091999-11-25 00:26:21 +1100937 sock = socket(AF_UNIX, SOCK_STREAM, 0);
938 if (sock < 0) {
939 perror("socket");
940 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000941 }
Damien Miller95def091999-11-25 00:26:21 +1100942 memset(&sunaddr, 0, sizeof(sunaddr));
943 sunaddr.sun_family = AF_UNIX;
944 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000945#ifdef HAVE_CYGWIN
946 prev_mask = umask(0177);
947#endif
Damien Miller95def091999-11-25 00:26:21 +1100948 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
949 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000950#ifdef HAVE_CYGWIN
951 umask(prev_mask);
952#endif
Damien Miller95def091999-11-25 00:26:21 +1100953 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000955#ifdef HAVE_CYGWIN
956 umask(prev_mask);
957#endif
Damien Miller95def091999-11-25 00:26:21 +1100958 if (listen(sock, 5) < 0) {
959 perror("listen");
960 cleanup_exit(1);
961 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000962
Damien Miller5428f641999-11-25 11:54:57 +1100963 /*
964 * Fork, and have the parent execute the command, if any, or present
965 * the socket data. The child continues as the authentication agent.
966 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000967 if (d_flag) {
968 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
969 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
970 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
971 SSH_AUTHSOCKET_ENV_NAME);
972 printf("echo Agent pid %d;\n", parent_pid);
973 goto skip;
974 }
Damien Miller95def091999-11-25 00:26:21 +1100975 pid = fork();
976 if (pid == -1) {
977 perror("fork");
978 exit(1);
979 }
980 if (pid != 0) { /* Parent - execute the given command. */
981 close(sock);
982 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
983 if (ac == 0) {
984 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
985 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000986 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100987 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000988 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100989 printf("echo Agent pid %d;\n", pid);
990 exit(0);
991 }
Damien Millere4340be2000-09-16 13:29:08 +1100992 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
993 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
994 perror("setenv");
995 exit(1);
996 }
Damien Miller95def091999-11-25 00:26:21 +1100997 execvp(av[0], av);
998 perror(av[0]);
999 exit(1);
1000 }
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001001
1002 if (setsid() == -1) {
1003 perror("setsid");
1004 cleanup_exit(1);
1005 }
1006
1007 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001008 close(0);
1009 close(1);
1010 close(2);
1011
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001012#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001013 /* deny core dumps, since memory contains unencrypted private keys */
1014 rlim.rlim_cur = rlim.rlim_max = 0;
1015 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1016 perror("setrlimit rlimit_core failed");
1017 cleanup_exit(1);
1018 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001019#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001020
1021skip:
Damien Miller95def091999-11-25 00:26:21 +11001022 if (atexit(cleanup_socket) < 0) {
1023 perror("atexit");
1024 cleanup_exit(1);
1025 }
1026 new_socket(AUTH_SOCKET, sock);
1027 if (ac > 0) {
1028 signal(SIGALRM, check_parent_exists);
1029 alarm(10);
1030 }
Damien Millerad833b32000-08-23 10:46:23 +10001031 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001032 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001033 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001034 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001035 signal(SIGHUP, cleanup_handler);
1036 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001037 nalloc = 0;
1038
Damien Miller95def091999-11-25 00:26:21 +11001039 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001040 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001041 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001042 if (errno == EINTR)
1043 continue;
1044 exit(1);
1045 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001046 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001047 }
1048 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049}