blob: 41dd777cb75473950dbe1ff8f51c5fe1d1572941 [file] [log] [blame]
Ben Lindstrom3f471632001-07-04 03:53:15 +00001/* $OpenBSD: ssh-agent.c,v 1.59 2001/06/26 05:33:34 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"
Ben Lindstrom3f471632001-07-04 03:53:15 +000039RCSID("$OpenBSD: ssh-agent.c,v 1.59 2001/06/26 05:33:34 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"
62#endif
63
Damien Miller95def091999-11-25 00:26:21 +110064typedef struct {
65 int fd;
66 enum {
67 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
68 } type;
69 Buffer input;
70 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071} SocketEntry;
72
Ben Lindstrom46c16222000-12-22 01:43:59 +000073u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074SocketEntry *sockets = NULL;
75
Damien Miller95def091999-11-25 00:26:21 +110076typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100077 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110078 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079} Identity;
80
Damien Millerad833b32000-08-23 10:46:23 +100081typedef struct {
82 int nentries;
83 Identity *identities;
84} Idtab;
85
86/* private key table, one per protocol version */
87Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89int max_fd = 0;
90
91/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100092pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* pathname and directory for AUTH_SOCKET */
95char socket_name[1024];
96char socket_dir[1024];
97
Damien Miller95def091999-11-25 00:26:21 +110098#ifdef HAVE___PROGNAME
99extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000100#else
101char *__progname;
102#endif
Damien Miller95def091999-11-25 00:26:21 +1100103
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static void
Damien Millerad833b32000-08-23 10:46:23 +1000105idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106{
Damien Millerad833b32000-08-23 10:46:23 +1000107 int i;
108 for (i = 0; i <=2; i++){
109 idtable[i].identities = NULL;
110 idtable[i].nentries = 0;
111 }
112}
113
114/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000116idtab_lookup(int version)
117{
118 if (version < 1 || version > 2)
119 fatal("internal error, bad protocol version %d", version);
120 return &idtable[version];
121}
122
123/* return matching private key for given public key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static Key *
Damien Millerad833b32000-08-23 10:46:23 +1000125lookup_private_key(Key *key, int *idx, int version)
126{
127 int i;
128 Idtab *tab = idtab_lookup(version);
129 for (i = 0; i < tab->nentries; i++) {
130 if (key_equal(key, tab->identities[i].key)) {
131 if (idx != NULL)
132 *idx = i;
133 return tab->identities[i].key;
134 }
135 }
136 return NULL;
137}
138
139/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static void
Damien Millerad833b32000-08-23 10:46:23 +1000141process_request_identities(SocketEntry *e, int version)
142{
143 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100144 Buffer msg;
145 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000148 buffer_put_char(&msg, (version == 1) ?
149 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
150 buffer_put_int(&msg, tab->nentries);
151 for (i = 0; i < tab->nentries; i++) {
152 Identity *id = &tab->identities[i];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000154 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
155 buffer_put_bignum(&msg, id->key->rsa->e);
156 buffer_put_bignum(&msg, id->key->rsa->n);
157 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000158 u_char *blob;
159 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000161 buffer_put_string(&msg, blob, blen);
162 xfree(blob);
163 }
164 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100165 }
166 buffer_put_int(&e->output, buffer_len(&msg));
167 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
168 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Damien Millerad833b32000-08-23 10:46:23 +1000171/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Damien Millerad833b32000-08-23 10:46:23 +1000173process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174{
Damien Millerad833b32000-08-23 10:46:23 +1000175 Key *key, *private;
176 BIGNUM *challenge;
177 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100178 Buffer msg;
179 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000180 u_char buf[32], mdbuf[16], session_id[16];
181 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Damien Miller95def091999-11-25 00:26:21 +1100183 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100185 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000186
187 buffer_get_int(&e->input); /* ignored */
188 buffer_get_bignum(&e->input, key->rsa->e);
189 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100190 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191
Damien Millerad833b32000-08-23 10:46:23 +1000192 /* Only protocol 1.1 is supported */
193 if (buffer_len(&e->input) == 0)
194 goto failure;
195 buffer_get(&e->input, (char *) session_id, 16);
196 response_type = buffer_get_int(&e->input);
197 if (response_type != 1)
198 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Millerad833b32000-08-23 10:46:23 +1000200 private = lookup_private_key(key, NULL, 1);
201 if (private != NULL) {
202 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100203 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
204 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100205
Damien Millerad833b32000-08-23 10:46:23 +1000206 /* The response is MD5 of decrypted challenge plus session id. */
207 len = BN_num_bytes(challenge);
208 if (len <= 0 || len > 32) {
209 log("process_authentication_challenge: bad challenge length %d", len);
210 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100211 }
Damien Millerad833b32000-08-23 10:46:23 +1000212 memset(buf, 0, 32);
213 BN_bn2bin(challenge, buf + 32 - len);
214 MD5_Init(&md);
215 MD5_Update(&md, buf, 32);
216 MD5_Update(&md, session_id, 16);
217 MD5_Final(mdbuf, &md);
218
219 /* Send the response. */
220 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
221 for (i = 0; i < 16; i++)
222 buffer_put_char(&msg, mdbuf[i]);
223 goto send;
224 }
225
226failure:
227 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100228 buffer_put_char(&msg, SSH_AGENT_FAILURE);
229send:
230 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000231 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
232 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100233 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000234 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
Damien Millerad833b32000-08-23 10:46:23 +1000237/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000238static void
Damien Millerad833b32000-08-23 10:46:23 +1000239process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Damien Millerad833b32000-08-23 10:46:23 +1000241 extern int datafellows;
242 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000243 u_char *blob, *data, *signature = NULL;
244 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100245 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000246 Buffer msg;
247 int ok = -1;
248
249 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000250
Damien Millerad833b32000-08-23 10:46:23 +1000251 blob = buffer_get_string(&e->input, &blen);
252 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100253
254 flags = buffer_get_int(&e->input);
255 if (flags & SSH_AGENT_OLD_SIGNATURE)
256 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000257
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000259 if (key != NULL) {
260 private = lookup_private_key(key, NULL, 2);
261 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100262 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000263 }
264 key_free(key);
265 buffer_init(&msg);
266 if (ok == 0) {
267 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
268 buffer_put_string(&msg, signature, slen);
269 } else {
270 buffer_put_char(&msg, SSH_AGENT_FAILURE);
271 }
272 buffer_put_int(&e->output, buffer_len(&msg));
273 buffer_append(&e->output, buffer_ptr(&msg),
274 buffer_len(&msg));
275 buffer_free(&msg);
276 xfree(data);
277 xfree(blob);
278 if (signature != NULL)
279 xfree(signature);
280}
281
282/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Damien Millerad833b32000-08-23 10:46:23 +1000284process_remove_identity(SocketEntry *e, int version)
285{
286 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000287 u_char *blob;
288 u_int blen;
289 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000290 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100291
Damien Millerad833b32000-08-23 10:46:23 +1000292 switch(version){
293 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100294 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000295 bits = buffer_get_int(&e->input);
296 buffer_get_bignum(&e->input, key->rsa->e);
297 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Millerad833b32000-08-23 10:46:23 +1000299 if (bits != key_size(key))
300 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000301 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000302 break;
303 case 2:
304 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100305 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000306 xfree(blob);
307 break;
308 }
309 if (key != NULL) {
310 int idx;
311 private = lookup_private_key(key, &idx, version);
312 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100313 /*
314 * We have this key. Free the old key. Since we
315 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000316 * the array, we actually free the key there and move
317 * all the entries between the empty slot and the end
318 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100319 */
Damien Millerad833b32000-08-23 10:46:23 +1000320 Idtab *tab = idtab_lookup(version);
321 key_free(tab->identities[idx].key);
322 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100323 if (tab->nentries < 1)
324 fatal("process_remove_identity: "
325 "internal error: tab->nentries %d",
326 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000327 if (idx != tab->nentries - 1) {
328 int i;
329 for (i = idx; i < tab->nentries - 1; i++)
330 tab->identities[i] = tab->identities[i+1];
331 }
332 tab->identities[tab->nentries - 1].key = NULL;
333 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000334 tab->nentries--;
335 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100336 }
Damien Millerad833b32000-08-23 10:46:23 +1000337 key_free(key);
338 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000340 buffer_put_char(&e->output,
341 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
Ben Lindstrombba81212001-06-25 05:01:22 +0000344static void
Damien Millerad833b32000-08-23 10:46:23 +1000345process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000347 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000348 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000351 for (i = 0; i < tab->nentries; i++) {
352 key_free(tab->identities[i].key);
353 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100354 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000357 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
359 /* Send success. */
360 buffer_put_int(&e->output, 1);
361 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
362 return;
Damien Miller95def091999-11-25 00:26:21 +1100363}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Millerad833b32000-08-23 10:46:23 +1000366process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100367{
Damien Millerad833b32000-08-23 10:46:23 +1000368 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000370 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100371 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000372 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100373
Damien Millerad833b32000-08-23 10:46:23 +1000374 switch (version) {
375 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000377 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 buffer_get_bignum(&e->input, k->rsa->n);
379 buffer_get_bignum(&e->input, k->rsa->e);
380 buffer_get_bignum(&e->input, k->rsa->d);
381 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100382
Damien Millerad833b32000-08-23 10:46:23 +1000383 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 buffer_get_bignum(&e->input, k->rsa->q); /* p */
385 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100386
Damien Millerad833b32000-08-23 10:46:23 +1000387 /* Generate additional parameters */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100388 generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000389 break;
390 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100391 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000392 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 xfree(type_name);
394 switch(type) {
395 case KEY_DSA:
396 k = key_new_private(type);
397 buffer_get_bignum2(&e->input, k->dsa->p);
398 buffer_get_bignum2(&e->input, k->dsa->q);
399 buffer_get_bignum2(&e->input, k->dsa->g);
400 buffer_get_bignum2(&e->input, k->dsa->pub_key);
401 buffer_get_bignum2(&e->input, k->dsa->priv_key);
402 break;
403 case KEY_RSA:
404 k = key_new_private(type);
405 buffer_get_bignum2(&e->input, k->rsa->n);
406 buffer_get_bignum2(&e->input, k->rsa->e);
407 buffer_get_bignum2(&e->input, k->rsa->d);
408 buffer_get_bignum2(&e->input, k->rsa->iqmp);
409 buffer_get_bignum2(&e->input, k->rsa->p);
410 buffer_get_bignum2(&e->input, k->rsa->q);
411
412 /* Generate additional parameters */
413 generate_additional_parameters(k->rsa);
414 break;
415 default:
Damien Millerad833b32000-08-23 10:46:23 +1000416 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000417 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100418 }
Damien Millerad833b32000-08-23 10:46:23 +1000419 break;
420 }
Damien Millerad833b32000-08-23 10:46:23 +1000421 comment = buffer_get_string(&e->input, NULL);
422 if (k == NULL) {
423 xfree(comment);
424 goto send;
425 }
426 success = 1;
427 if (lookup_private_key(k, NULL, version) == NULL) {
428 if (tab->nentries == 0)
429 tab->identities = xmalloc(sizeof(Identity));
430 else
431 tab->identities = xrealloc(tab->identities,
432 (tab->nentries + 1) * sizeof(Identity));
433 tab->identities[tab->nentries].key = k;
434 tab->identities[tab->nentries].comment = comment;
435 /* Increment the number of identities. */
436 tab->nentries++;
437 } else {
438 key_free(k);
439 xfree(comment);
440 }
441send:
Damien Miller95def091999-11-25 00:26:21 +1100442 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000443 buffer_put_char(&e->output,
444 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445}
446
Ben Lindstrom3f471632001-07-04 03:53:15 +0000447
448#ifdef SMARTCARD
449static void
450process_add_smartcard_key (SocketEntry *e)
451{
452 Idtab *tab;
453 Key *n = NULL, *k = NULL;
454 int success = 0;
455 int sc_reader_num = 0;
456
457 sc_reader_num = buffer_get_int(&e->input);
458
459 k = sc_get_key(sc_reader_num);
460 if (k == NULL) {
461 error("sc_get_pubkey failed");
462 goto send;
463 }
464 success = 1;
465
466 tab = idtab_lookup(1);
467 if (lookup_private_key(k, NULL, 1) == NULL) {
468 if (tab->nentries == 0)
469 tab->identities = xmalloc(sizeof(Identity));
470 else
471 tab->identities = xrealloc(tab->identities,
472 (tab->nentries + 1) * sizeof(Identity));
473 n = key_new(KEY_RSA1);
474 BN_copy(n->rsa->n, k->rsa->n);
475 BN_copy(n->rsa->e, k->rsa->e);
476 RSA_set_method(n->rsa, sc_get_engine());
477 tab->identities[tab->nentries].key = n;
478 tab->identities[tab->nentries].comment =
479 xstrdup("rsa1 smartcard");
480 tab->nentries++;
481 }
482 tab = idtab_lookup(2);
483 if (lookup_private_key(k, NULL, 2) == NULL) {
484 if (tab->nentries == 0)
485 tab->identities = xmalloc(sizeof(Identity));
486 else
487 tab->identities = xrealloc(tab->identities,
488 (tab->nentries + 1) * sizeof(Identity));
489 n = key_new(KEY_RSA);
490 BN_copy(n->rsa->n, k->rsa->n);
491 BN_copy(n->rsa->e, k->rsa->e);
492 RSA_set_method(n->rsa, sc_get_engine());
493 tab->identities[tab->nentries].key = n;
494 tab->identities[tab->nentries].comment =
495 xstrdup("rsa smartcard");
496 tab->nentries++;
497 }
498 key_free(k);
499send:
500 buffer_put_int(&e->output, 1);
501 buffer_put_char(&e->output,
502 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
503}
504
505static void
506process_remove_smartcard_key(SocketEntry *e)
507{
508 Key *k = NULL, *private;
509 int idx;
510 int success = 0;
511 int sc_reader_num = 0;
512
513 sc_reader_num = buffer_get_int(&e->input);
514
515 if ((k = sc_get_key(sc_reader_num)) == NULL) {
516 error("sc_get_pubkey failed");
517 } else {
518 private = lookup_private_key(k, &idx, 1);
519 if (private != NULL) {
520 Idtab *tab = idtab_lookup(1);
521 key_free(tab->identities[idx].key);
522 xfree(tab->identities[idx].comment);
523 if (idx != tab->nentries)
524 tab->identities[idx] = tab->identities[tab->nentries];
525 tab->nentries--;
526 success = 1;
527 }
528 private = lookup_private_key(k, &idx, 2);
529 if (private != NULL) {
530 Idtab *tab = idtab_lookup(2);
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 }
538 key_free(k);
539 }
540
541 buffer_put_int(&e->output, 1);
542 buffer_put_char(&e->output,
543 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
544}
545#endif
546
Damien Millerad833b32000-08-23 10:46:23 +1000547/* dispatch incoming messages */
548
Ben Lindstrombba81212001-06-25 05:01:22 +0000549static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550process_message(SocketEntry *e)
551{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000552 u_int msg_len;
553 u_int type;
554 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100555 if (buffer_len(&e->input) < 5)
556 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000557 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100558 msg_len = GET_32BIT(cp);
559 if (msg_len > 256 * 1024) {
560 shutdown(e->fd, SHUT_RDWR);
561 close(e->fd);
562 e->type = AUTH_UNUSED;
563 return;
564 }
565 if (buffer_len(&e->input) < msg_len + 4)
566 return;
567 buffer_consume(&e->input, 4);
568 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Ben Lindstrom3f471632001-07-04 03:53:15 +0000570 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100571 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000572 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100573 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000574 process_authentication_challenge1(e);
575 break;
576 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
577 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100578 break;
579 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000580 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100581 break;
582 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000583 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100584 break;
585 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000586 process_remove_all_identities(e, 1);
587 break;
588 /* ssh2 */
589 case SSH2_AGENTC_SIGN_REQUEST:
590 process_sign_request2(e);
591 break;
592 case SSH2_AGENTC_REQUEST_IDENTITIES:
593 process_request_identities(e, 2);
594 break;
595 case SSH2_AGENTC_ADD_IDENTITY:
596 process_add_identity(e, 2);
597 break;
598 case SSH2_AGENTC_REMOVE_IDENTITY:
599 process_remove_identity(e, 2);
600 break;
601 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
602 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100603 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000604#ifdef SMARTCARD
605 case SSH_AGENTC_ADD_SMARTCARD_KEY:
606 process_add_smartcard_key(e);
607 break;
608 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
609 process_remove_smartcard_key(e);
610 break;
611#endif
Damien Miller95def091999-11-25 00:26:21 +1100612 default:
613 /* Unknown message. Respond with failure. */
614 error("Unknown message %d", type);
615 buffer_clear(&e->input);
616 buffer_put_int(&e->output, 1);
617 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
618 break;
619 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620}
621
Ben Lindstrombba81212001-06-25 05:01:22 +0000622static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623new_socket(int type, int fd)
624{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000625 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100626 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
627 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Damien Miller95def091999-11-25 00:26:21 +1100629 if (fd > max_fd)
630 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller95def091999-11-25 00:26:21 +1100632 for (i = 0; i < sockets_alloc; i++)
633 if (sockets[i].type == AUTH_UNUSED) {
634 sockets[i].fd = fd;
635 sockets[i].type = type;
636 buffer_init(&sockets[i].input);
637 buffer_init(&sockets[i].output);
638 return;
639 }
640 old_alloc = sockets_alloc;
641 sockets_alloc += 10;
642 if (sockets)
643 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
644 else
645 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
646 for (i = old_alloc; i < sockets_alloc; i++)
647 sockets[i].type = AUTH_UNUSED;
648 sockets[old_alloc].type = type;
649 sockets[old_alloc].fd = fd;
650 buffer_init(&sockets[old_alloc].input);
651 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000652}
653
Ben Lindstrombba81212001-06-25 05:01:22 +0000654static int
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000655prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000657 u_int i, sz;
658 int n = 0;
659
660 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100661 switch (sockets[i].type) {
662 case AUTH_SOCKET:
663 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000664 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100665 break;
666 case AUTH_UNUSED:
667 break;
668 default:
669 fatal("Unknown socket type %d", sockets[i].type);
670 break;
671 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000672 }
673
674 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
675 if (*fdrp == NULL || n > *fdl) {
676 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000677 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000678 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000679 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000680 *fdrp = xmalloc(sz);
681 *fdwp = xmalloc(sz);
682 *fdl = n;
683 }
684 memset(*fdrp, 0, sz);
685 memset(*fdwp, 0, sz);
686
687 for (i = 0; i < sockets_alloc; i++) {
688 switch (sockets[i].type) {
689 case AUTH_SOCKET:
690 case AUTH_CONNECTION:
691 FD_SET(sockets[i].fd, *fdrp);
692 if (buffer_len(&sockets[i].output) > 0)
693 FD_SET(sockets[i].fd, *fdwp);
694 break;
695 default:
696 break;
697 }
698 }
699 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700}
701
Ben Lindstrombba81212001-06-25 05:01:22 +0000702static void
Damien Miller95def091999-11-25 00:26:21 +1100703after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000705 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100706 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100707 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100708 char buf[1024];
709 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller95def091999-11-25 00:26:21 +1100711 for (i = 0; i < sockets_alloc; i++)
712 switch (sockets[i].type) {
713 case AUTH_UNUSED:
714 break;
715 case AUTH_SOCKET:
716 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100717 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000718 sock = accept(sockets[i].fd,
719 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100720 if (sock < 0) {
721 perror("accept from AUTH_SOCKET");
722 break;
723 }
724 new_socket(AUTH_CONNECTION, sock);
725 }
726 break;
727 case AUTH_CONNECTION:
728 if (buffer_len(&sockets[i].output) > 0 &&
729 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000730 do {
731 len = write(sockets[i].fd,
732 buffer_ptr(&sockets[i].output),
733 buffer_len(&sockets[i].output));
734 if (len == -1 && (errno == EAGAIN ||
735 errno == EINTR))
736 continue;
737 break;
738 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100739 if (len <= 0) {
740 shutdown(sockets[i].fd, SHUT_RDWR);
741 close(sockets[i].fd);
742 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000743 buffer_free(&sockets[i].input);
744 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100745 break;
746 }
747 buffer_consume(&sockets[i].output, len);
748 }
749 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000750 do {
751 len = read(sockets[i].fd, buf, sizeof(buf));
752 if (len == -1 && (errno == EAGAIN ||
753 errno == EINTR))
754 continue;
755 break;
756 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100757 if (len <= 0) {
758 shutdown(sockets[i].fd, SHUT_RDWR);
759 close(sockets[i].fd);
760 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000761 buffer_free(&sockets[i].input);
762 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100763 break;
764 }
765 buffer_append(&sockets[i].input, buf, len);
766 process_message(&sockets[i]);
767 }
768 break;
769 default:
770 fatal("Unknown type %d", sockets[i].type);
771 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772}
773
Ben Lindstrombba81212001-06-25 05:01:22 +0000774static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775check_parent_exists(int sig)
776{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000777 int save_errno = errno;
778
Damien Miller166fca82000-04-20 07:42:21 +1000779 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100780 /* printf("Parent has died - Authentication agent exiting.\n"); */
781 exit(1);
782 }
783 signal(SIGALRM, check_parent_exists);
784 alarm(10);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000785 errno = save_errno;
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 Lindstrom28072eb2001-02-10 23:13:41 +0000812usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000813{
Damien Miller95def091999-11-25 00:26:21 +1100814 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
Ben Lindstromdb6b2762001-07-04 03:51:35 +0000815 fprintf(stderr, "Usage: %s [-c | -s] [-k] [-d] [command {args...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000816 __progname);
Damien Miller95def091999-11-25 00:26:21 +1100817 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000818}
819
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820int
821main(int ac, char **av)
822{
Ben Lindstromd94580c2001-07-04 03:48:02 +0000823 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch;
Damien Miller95def091999-11-25 00:26:21 +1100824 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000825#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000826 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000827#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000828#ifdef HAVE_CYGWIN
829 int prev_mask;
830#endif
Damien Miller95def091999-11-25 00:26:21 +1100831 pid_t pid;
832 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000833 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000834 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000835
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000836 SSLeay_add_all_algorithms();
837
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000838 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000839 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100840 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000841
Damien Millerd0cff3e2000-04-20 23:12:58 +1000842#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000843 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000844#else /* __GNU_LIBRARY__ */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000845 while ((ch = getopt(ac, av, "cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000846#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100847 switch (ch) {
848 case 'c':
849 if (s_flag)
850 usage();
851 c_flag++;
852 break;
853 case 'k':
854 k_flag++;
855 break;
856 case 's':
857 if (c_flag)
858 usage();
859 s_flag++;
860 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000861 case 'd':
862 if (d_flag)
863 usage();
864 d_flag++;
865 break;
Damien Miller95def091999-11-25 00:26:21 +1100866 default:
867 usage();
868 }
Damien Miller792c5111999-10-29 09:47:09 +1000869 }
Damien Miller95def091999-11-25 00:26:21 +1100870 ac -= optind;
871 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000872
Ben Lindstromd94580c2001-07-04 03:48:02 +0000873 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100874 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000875
Ben Lindstromd94580c2001-07-04 03:48:02 +0000876 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100877 shell = getenv("SHELL");
878 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
879 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000880 }
Damien Miller95def091999-11-25 00:26:21 +1100881 if (k_flag) {
882 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
883 if (pidstr == NULL) {
884 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000885 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100886 exit(1);
887 }
888 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000889 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100890 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000891 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100892 exit(1);
893 }
894 if (kill(pid, SIGTERM) == -1) {
895 perror("kill");
896 exit(1);
897 }
898 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
899 printf(format, SSH_AUTHSOCKET_ENV_NAME);
900 printf(format, SSH_AGENTPID_ENV_NAME);
901 printf("echo Agent pid %d killed;\n", pid);
902 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000903 }
Damien Miller95def091999-11-25 00:26:21 +1100904 parent_pid = getpid();
905
906 /* Create private directory for agent socket */
907 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
908 if (mkdtemp(socket_dir) == NULL) {
909 perror("mkdtemp: private socket dir");
910 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000911 }
Damien Miller95def091999-11-25 00:26:21 +1100912 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000913 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller5428f641999-11-25 11:54:57 +1100915 /*
916 * Create socket early so it will exist before command gets run from
917 * the parent.
918 */
Damien Miller95def091999-11-25 00:26:21 +1100919 sock = socket(AF_UNIX, SOCK_STREAM, 0);
920 if (sock < 0) {
921 perror("socket");
922 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000923 }
Damien Miller95def091999-11-25 00:26:21 +1100924 memset(&sunaddr, 0, sizeof(sunaddr));
925 sunaddr.sun_family = AF_UNIX;
926 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000927#ifdef HAVE_CYGWIN
928 prev_mask = umask(0177);
929#endif
Damien Miller95def091999-11-25 00:26:21 +1100930 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
931 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000932#ifdef HAVE_CYGWIN
933 umask(prev_mask);
934#endif
Damien Miller95def091999-11-25 00:26:21 +1100935 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000937#ifdef HAVE_CYGWIN
938 umask(prev_mask);
939#endif
Damien Miller95def091999-11-25 00:26:21 +1100940 if (listen(sock, 5) < 0) {
941 perror("listen");
942 cleanup_exit(1);
943 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000944
Damien Miller5428f641999-11-25 11:54:57 +1100945 /*
946 * Fork, and have the parent execute the command, if any, or present
947 * the socket data. The child continues as the authentication agent.
948 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000949 if (d_flag) {
950 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
951 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
952 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
953 SSH_AUTHSOCKET_ENV_NAME);
954 printf("echo Agent pid %d;\n", parent_pid);
955 goto skip;
956 }
Damien Miller95def091999-11-25 00:26:21 +1100957 pid = fork();
958 if (pid == -1) {
959 perror("fork");
960 exit(1);
961 }
962 if (pid != 0) { /* Parent - execute the given command. */
963 close(sock);
964 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
965 if (ac == 0) {
966 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
967 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000968 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100969 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000970 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100971 printf("echo Agent pid %d;\n", pid);
972 exit(0);
973 }
Damien Millere4340be2000-09-16 13:29:08 +1100974 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
975 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
976 perror("setenv");
977 exit(1);
978 }
Damien Miller95def091999-11-25 00:26:21 +1100979 execvp(av[0], av);
980 perror(av[0]);
981 exit(1);
982 }
983 close(0);
984 close(1);
985 close(2);
986
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000987#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000988 /* deny core dumps, since memory contains unencrypted private keys */
989 rlim.rlim_cur = rlim.rlim_max = 0;
990 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
991 perror("setrlimit rlimit_core failed");
992 cleanup_exit(1);
993 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000994#endif
Damien Miller95def091999-11-25 00:26:21 +1100995 if (setsid() == -1) {
996 perror("setsid");
997 cleanup_exit(1);
998 }
Ben Lindstromd94580c2001-07-04 03:48:02 +0000999
1000skip:
Damien Miller95def091999-11-25 00:26:21 +11001001 if (atexit(cleanup_socket) < 0) {
1002 perror("atexit");
1003 cleanup_exit(1);
1004 }
1005 new_socket(AUTH_SOCKET, sock);
1006 if (ac > 0) {
1007 signal(SIGALRM, check_parent_exists);
1008 alarm(10);
1009 }
Damien Millerad833b32000-08-23 10:46:23 +10001010 idtab_init();
Ben Lindstromd94580c2001-07-04 03:48:02 +00001011 if (!d_flag) {
1012 signal(SIGINT, SIG_IGN);
1013 signal(SIGPIPE, SIG_IGN);
1014 }
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001015 signal(SIGHUP, cleanup_handler);
1016 signal(SIGTERM, cleanup_handler);
Damien Miller95def091999-11-25 00:26:21 +11001017 while (1) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001018 prepare_select(&readsetp, &writesetp, &max_fd);
1019 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001020 if (errno == EINTR)
1021 continue;
1022 exit(1);
1023 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001024 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001025 }
1026 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027}