blob: ce283c8ccdb69cca3e5e529c082f0ffd97a6d082 [file] [log] [blame]
Damien Miller4a8ed542002-01-22 23:33:31 +11001/* $OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk 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 Miller4a8ed542002-01-22 23:33:31 +110039RCSID("$OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040
Damien Millerec52d7c2002-01-22 23:52:17 +110041#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
42#include <sys/queue.h>
43#else
44#include "openbsd-compat/fake-queue.h"
45#endif
46
47
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include <openssl/evp.h>
49#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51#include "ssh.h"
52#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "buffer.h"
54#include "bufaux.h"
55#include "xmalloc.h"
56#include "packet.h"
57#include "getput.h"
58#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100059#include "key.h"
60#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110063#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Ben Lindstrom3f471632001-07-04 03:53:15 +000066#ifdef SMARTCARD
67#include <openssl/engine.h>
68#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000069#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000070
Ben Lindstrom65366a82001-12-06 16:32:47 +000071typedef enum {
72 AUTH_UNUSED,
73 AUTH_SOCKET,
74 AUTH_CONNECTION
75} sock_type;
76
Damien Miller95def091999-11-25 00:26:21 +110077typedef struct {
78 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000079 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110080 Buffer input;
81 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082} SocketEntry;
83
Ben Lindstrom46c16222000-12-22 01:43:59 +000084u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085SocketEntry *sockets = NULL;
86
Damien Miller1a534ae2002-01-22 23:26:13 +110087typedef struct identity {
88 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100089 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110090 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091} Identity;
92
Damien Millerad833b32000-08-23 10:46:23 +100093typedef struct {
94 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110095 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100096} Idtab;
97
98/* private key table, one per protocol version */
99Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101int max_fd = 0;
102
103/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000104pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106/* pathname and directory for AUTH_SOCKET */
107char socket_name[1024];
108char socket_dir[1024];
109
Damien Miller95def091999-11-25 00:26:21 +1100110#ifdef HAVE___PROGNAME
111extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000112#else
113char *__progname;
114#endif
Damien Miller95def091999-11-25 00:26:21 +1100115
Ben Lindstrombba81212001-06-25 05:01:22 +0000116static void
Damien Millerad833b32000-08-23 10:46:23 +1000117idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118{
Damien Millerad833b32000-08-23 10:46:23 +1000119 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000120 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100121 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000122 idtable[i].nentries = 0;
123 }
124}
125
126/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000128idtab_lookup(int version)
129{
130 if (version < 1 || version > 2)
131 fatal("internal error, bad protocol version %d", version);
132 return &idtable[version];
133}
134
135/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100136static Identity *
137lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000138{
Damien Miller1a534ae2002-01-22 23:26:13 +1100139 Identity *id;
140
Damien Millerad833b32000-08-23 10:46:23 +1000141 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100142 TAILQ_FOREACH(id, &tab->idlist, next) {
143 if (key_equal(key, id->key))
144 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000145 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100146 return (NULL);
147}
148
149static void
150free_identity(Identity *id)
151{
152 key_free(id->key);
153 xfree(id->comment);
154 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000155}
156
157/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000158static void
Damien Millerad833b32000-08-23 10:46:23 +1000159process_request_identities(SocketEntry *e, int version)
160{
161 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100162 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100163 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller95def091999-11-25 00:26:21 +1100165 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000166 buffer_put_char(&msg, (version == 1) ?
167 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
168 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100169 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000171 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
172 buffer_put_bignum(&msg, id->key->rsa->e);
173 buffer_put_bignum(&msg, id->key->rsa->n);
174 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000175 u_char *blob;
176 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100177 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000178 buffer_put_string(&msg, blob, blen);
179 xfree(blob);
180 }
181 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100182 }
183 buffer_put_int(&e->output, buffer_len(&msg));
184 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
185 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186}
187
Damien Millerad833b32000-08-23 10:46:23 +1000188/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static void
Damien Millerad833b32000-08-23 10:46:23 +1000190process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191{
Damien Miller1a534ae2002-01-22 23:26:13 +1100192 Identity *id;
193 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000194 BIGNUM *challenge;
195 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100196 Buffer msg;
197 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000198 u_char buf[32], mdbuf[16], session_id[16];
199 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Miller95def091999-11-25 00:26:21 +1100201 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100202 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100203 if ((challenge = BN_new()) == NULL)
204 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000205
206 buffer_get_int(&e->input); /* ignored */
207 buffer_get_bignum(&e->input, key->rsa->e);
208 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100209 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Millerad833b32000-08-23 10:46:23 +1000211 /* Only protocol 1.1 is supported */
212 if (buffer_len(&e->input) == 0)
213 goto failure;
Damien Miller4a8ed542002-01-22 23:33:31 +1100214 buffer_get(&e->input, session_id, 16);
Damien Millerad833b32000-08-23 10:46:23 +1000215 response_type = buffer_get_int(&e->input);
216 if (response_type != 1)
217 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller1a534ae2002-01-22 23:26:13 +1100219 id = lookup_identity(key, 1);
220 if (id != NULL) {
221 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000222 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100223 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
224 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100225
Damien Millerad833b32000-08-23 10:46:23 +1000226 /* The response is MD5 of decrypted challenge plus session id. */
227 len = BN_num_bytes(challenge);
228 if (len <= 0 || len > 32) {
229 log("process_authentication_challenge: bad challenge length %d", len);
230 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100231 }
Damien Millerad833b32000-08-23 10:46:23 +1000232 memset(buf, 0, 32);
233 BN_bn2bin(challenge, buf + 32 - len);
234 MD5_Init(&md);
235 MD5_Update(&md, buf, 32);
236 MD5_Update(&md, session_id, 16);
237 MD5_Final(mdbuf, &md);
238
239 /* Send the response. */
240 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
241 for (i = 0; i < 16; i++)
242 buffer_put_char(&msg, mdbuf[i]);
243 goto send;
244 }
245
246failure:
247 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100248 buffer_put_char(&msg, SSH_AGENT_FAILURE);
249send:
250 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000251 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
252 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100253 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000254 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Millerad833b32000-08-23 10:46:23 +1000257/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static void
Damien Millerad833b32000-08-23 10:46:23 +1000259process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Millerad833b32000-08-23 10:46:23 +1000261 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100262 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000263 u_char *blob, *data, *signature = NULL;
264 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100265 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000266 Buffer msg;
267 int ok = -1;
268
269 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000270
Damien Millerad833b32000-08-23 10:46:23 +1000271 blob = buffer_get_string(&e->input, &blen);
272 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100273
274 flags = buffer_get_int(&e->input);
275 if (flags & SSH_AGENT_OLD_SIGNATURE)
276 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000277
Damien Miller0bc1bd82000-11-13 22:57:25 +1100278 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000279 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100280 Identity *id = lookup_identity(key, 2);
281 if (id != NULL)
282 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000283 }
284 key_free(key);
285 buffer_init(&msg);
286 if (ok == 0) {
287 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
288 buffer_put_string(&msg, signature, slen);
289 } else {
290 buffer_put_char(&msg, SSH_AGENT_FAILURE);
291 }
292 buffer_put_int(&e->output, buffer_len(&msg));
293 buffer_append(&e->output, buffer_ptr(&msg),
294 buffer_len(&msg));
295 buffer_free(&msg);
296 xfree(data);
297 xfree(blob);
298 if (signature != NULL)
299 xfree(signature);
300}
301
302/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
Damien Millerad833b32000-08-23 10:46:23 +1000304process_remove_identity(SocketEntry *e, int version)
305{
Damien Miller1a534ae2002-01-22 23:26:13 +1100306 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000307 u_char *blob;
308 u_int blen;
309 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000310 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100311
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000312 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000313 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100314 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000315 bits = buffer_get_int(&e->input);
316 buffer_get_bignum(&e->input, key->rsa->e);
317 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Millerad833b32000-08-23 10:46:23 +1000319 if (bits != key_size(key))
320 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000321 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000322 break;
323 case 2:
324 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100325 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000326 xfree(blob);
327 break;
328 }
329 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100330 Identity *id = lookup_identity(key, version);
331 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100332 /*
333 * We have this key. Free the old key. Since we
334 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000335 * the array, we actually free the key there and move
336 * all the entries between the empty slot and the end
337 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100338 */
Damien Millerad833b32000-08-23 10:46:23 +1000339 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100340 if (tab->nentries < 1)
341 fatal("process_remove_identity: "
342 "internal error: tab->nentries %d",
343 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100344 TAILQ_REMOVE(&tab->idlist, id, next);
345 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000346 tab->nentries--;
347 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100348 }
Damien Millerad833b32000-08-23 10:46:23 +1000349 key_free(key);
350 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000352 buffer_put_char(&e->output,
353 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354}
355
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static void
Damien Millerad833b32000-08-23 10:46:23 +1000357process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Damien Millerad833b32000-08-23 10:46:23 +1000359 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100360 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Damien Miller95def091999-11-25 00:26:21 +1100362 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100363 for (id = TAILQ_FIRST(&tab->idlist); id;
364 id = TAILQ_FIRST(&tab->idlist)) {
365 TAILQ_REMOVE(&tab->idlist, id, next);
366 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100367 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Damien Miller95def091999-11-25 00:26:21 +1100369 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000370 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
372 /* Send success. */
373 buffer_put_int(&e->output, 1);
374 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
375 return;
Damien Miller95def091999-11-25 00:26:21 +1100376}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static void
Damien Millerad833b32000-08-23 10:46:23 +1000379process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100380{
Damien Millerad833b32000-08-23 10:46:23 +1000381 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000383 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000385 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100386
Damien Millerad833b32000-08-23 10:46:23 +1000387 switch (version) {
388 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000390 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100391 buffer_get_bignum(&e->input, k->rsa->n);
392 buffer_get_bignum(&e->input, k->rsa->e);
393 buffer_get_bignum(&e->input, k->rsa->d);
394 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100395
Damien Millerad833b32000-08-23 10:46:23 +1000396 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 buffer_get_bignum(&e->input, k->rsa->q); /* p */
398 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100399
Damien Millerad833b32000-08-23 10:46:23 +1000400 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000401 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000402 break;
403 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100404 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000405 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100406 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000407 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 case KEY_DSA:
409 k = key_new_private(type);
410 buffer_get_bignum2(&e->input, k->dsa->p);
411 buffer_get_bignum2(&e->input, k->dsa->q);
412 buffer_get_bignum2(&e->input, k->dsa->g);
413 buffer_get_bignum2(&e->input, k->dsa->pub_key);
414 buffer_get_bignum2(&e->input, k->dsa->priv_key);
415 break;
416 case KEY_RSA:
417 k = key_new_private(type);
418 buffer_get_bignum2(&e->input, k->rsa->n);
419 buffer_get_bignum2(&e->input, k->rsa->e);
420 buffer_get_bignum2(&e->input, k->rsa->d);
421 buffer_get_bignum2(&e->input, k->rsa->iqmp);
422 buffer_get_bignum2(&e->input, k->rsa->p);
423 buffer_get_bignum2(&e->input, k->rsa->q);
424
425 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000426 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100427 break;
428 default:
Damien Millerad833b32000-08-23 10:46:23 +1000429 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000430 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100431 }
Damien Millerad833b32000-08-23 10:46:23 +1000432 break;
433 }
Damien Millerad833b32000-08-23 10:46:23 +1000434 comment = buffer_get_string(&e->input, NULL);
435 if (k == NULL) {
436 xfree(comment);
437 goto send;
438 }
439 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100440 if (lookup_identity(k, version) == NULL) {
441 Identity *id = xmalloc(sizeof(Identity));
442 id->key = k;
443 id->comment = comment;
444 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000445 /* Increment the number of identities. */
446 tab->nentries++;
447 } else {
448 key_free(k);
449 xfree(comment);
450 }
451send:
Damien Miller95def091999-11-25 00:26:21 +1100452 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000453 buffer_put_char(&e->output,
454 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
456
Ben Lindstrom3f471632001-07-04 03:53:15 +0000457
458#ifdef SMARTCARD
459static void
460process_add_smartcard_key (SocketEntry *e)
461{
462 Idtab *tab;
463 Key *n = NULL, *k = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000464 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000465 int success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100466
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000467 sc_reader_id = buffer_get_string(&e->input, NULL);
468 k = sc_get_key(sc_reader_id);
469 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000470
Ben Lindstrom3f471632001-07-04 03:53:15 +0000471 if (k == NULL) {
472 error("sc_get_pubkey failed");
473 goto send;
474 }
475 success = 1;
476
477 tab = idtab_lookup(1);
Damien Millerf3512d92001-07-14 12:14:27 +1000478 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100479 if (lookup_identity(k, 1) == NULL) {
480 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000481 n = key_new(KEY_RSA1);
482 BN_copy(n->rsa->n, k->rsa->n);
483 BN_copy(n->rsa->e, k->rsa->e);
484 RSA_set_method(n->rsa, sc_get_engine());
Damien Miller1a534ae2002-01-22 23:26:13 +1100485 id->key = n;
486 id->comment = xstrdup("rsa1 smartcard");
487 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000488 tab->nentries++;
489 }
Damien Millerf3512d92001-07-14 12:14:27 +1000490 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000491 tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100492 if (lookup_identity(k, 2) == NULL) {
493 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000494 n = key_new(KEY_RSA);
495 BN_copy(n->rsa->n, k->rsa->n);
496 BN_copy(n->rsa->e, k->rsa->e);
497 RSA_set_method(n->rsa, sc_get_engine());
Damien Miller1a534ae2002-01-22 23:26:13 +1100498 id->key = n;
499 id->comment = xstrdup("rsa smartcard");
500 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000501 tab->nentries++;
502 }
503 key_free(k);
504send:
505 buffer_put_int(&e->output, 1);
506 buffer_put_char(&e->output,
507 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
508}
509
510static void
511process_remove_smartcard_key(SocketEntry *e)
512{
Damien Miller1a534ae2002-01-22 23:26:13 +1100513 Key *k = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000514 int success = 0;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000515 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000516
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000517 sc_reader_id = buffer_get_string(&e->input, NULL);
518 k = sc_get_key(sc_reader_id);
519 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000520
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000521 if (k == NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000522 error("sc_get_pubkey failed");
523 } else {
Damien Miller1a534ae2002-01-22 23:26:13 +1100524 Identity *id;
Damien Miller8d4bf172001-07-14 12:13:49 +1000525 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100526 id = lookup_identity(k, 1);
527 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000528 Idtab *tab = idtab_lookup(1);
Damien Miller1a534ae2002-01-22 23:26:13 +1100529 TAILQ_REMOVE(&tab->idlist, id, next);
530 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000531 tab->nentries--;
532 success = 1;
533 }
Damien Miller8d4bf172001-07-14 12:13:49 +1000534 k->type = KEY_RSA;
Damien Miller1a534ae2002-01-22 23:26:13 +1100535 id = lookup_identity(k, 2);
536 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000537 Idtab *tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100538 TAILQ_REMOVE(&tab->idlist, id, next);
539 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000540 tab->nentries--;
541 success = 1;
542 }
543 key_free(k);
544 }
545
546 buffer_put_int(&e->output, 1);
547 buffer_put_char(&e->output,
548 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
549}
Ben Lindstromffce1472001-08-06 21:57:31 +0000550#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000551
Damien Millerad833b32000-08-23 10:46:23 +1000552/* dispatch incoming messages */
553
Ben Lindstrombba81212001-06-25 05:01:22 +0000554static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555process_message(SocketEntry *e)
556{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000557 u_int msg_len;
558 u_int type;
559 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100560 if (buffer_len(&e->input) < 5)
561 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100562 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100563 msg_len = GET_32BIT(cp);
564 if (msg_len > 256 * 1024) {
565 shutdown(e->fd, SHUT_RDWR);
566 close(e->fd);
567 e->type = AUTH_UNUSED;
568 return;
569 }
570 if (buffer_len(&e->input) < msg_len + 4)
571 return;
572 buffer_consume(&e->input, 4);
573 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Ben Lindstrom3f471632001-07-04 03:53:15 +0000575 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100576 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000577 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100578 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000579 process_authentication_challenge1(e);
580 break;
581 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
582 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100583 break;
584 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000585 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100586 break;
587 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000588 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100589 break;
590 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000591 process_remove_all_identities(e, 1);
592 break;
593 /* ssh2 */
594 case SSH2_AGENTC_SIGN_REQUEST:
595 process_sign_request2(e);
596 break;
597 case SSH2_AGENTC_REQUEST_IDENTITIES:
598 process_request_identities(e, 2);
599 break;
600 case SSH2_AGENTC_ADD_IDENTITY:
601 process_add_identity(e, 2);
602 break;
603 case SSH2_AGENTC_REMOVE_IDENTITY:
604 process_remove_identity(e, 2);
605 break;
606 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
607 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100608 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000609#ifdef SMARTCARD
610 case SSH_AGENTC_ADD_SMARTCARD_KEY:
611 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100612 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000613 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
614 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100615 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000616#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100617 default:
618 /* Unknown message. Respond with failure. */
619 error("Unknown message %d", type);
620 buffer_clear(&e->input);
621 buffer_put_int(&e->output, 1);
622 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
623 break;
624 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625}
626
Ben Lindstrombba81212001-06-25 05:01:22 +0000627static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000628new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000630 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100631 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
632 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
Damien Miller95def091999-11-25 00:26:21 +1100634 if (fd > max_fd)
635 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636
Damien Miller95def091999-11-25 00:26:21 +1100637 for (i = 0; i < sockets_alloc; i++)
638 if (sockets[i].type == AUTH_UNUSED) {
639 sockets[i].fd = fd;
640 sockets[i].type = type;
641 buffer_init(&sockets[i].input);
642 buffer_init(&sockets[i].output);
643 return;
644 }
645 old_alloc = sockets_alloc;
646 sockets_alloc += 10;
647 if (sockets)
648 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
649 else
650 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
651 for (i = old_alloc; i < sockets_alloc; i++)
652 sockets[i].type = AUTH_UNUSED;
653 sockets[old_alloc].type = type;
654 sockets[old_alloc].fd = fd;
655 buffer_init(&sockets[old_alloc].input);
656 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000657}
658
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000660prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000662 u_int i, sz;
663 int n = 0;
664
665 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100666 switch (sockets[i].type) {
667 case AUTH_SOCKET:
668 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000669 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100670 break;
671 case AUTH_UNUSED:
672 break;
673 default:
674 fatal("Unknown socket type %d", sockets[i].type);
675 break;
676 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000677 }
678
679 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000680 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000681 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000682 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000683 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000684 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000685 *fdrp = xmalloc(sz);
686 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000687 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000688 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000689 if (n < *fdl)
690 debug("XXX shrink: %d < %d", n, *fdl);
691 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000692 memset(*fdrp, 0, sz);
693 memset(*fdwp, 0, sz);
694
695 for (i = 0; i < sockets_alloc; i++) {
696 switch (sockets[i].type) {
697 case AUTH_SOCKET:
698 case AUTH_CONNECTION:
699 FD_SET(sockets[i].fd, *fdrp);
700 if (buffer_len(&sockets[i].output) > 0)
701 FD_SET(sockets[i].fd, *fdwp);
702 break;
703 default:
704 break;
705 }
706 }
707 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708}
709
Ben Lindstrombba81212001-06-25 05:01:22 +0000710static void
Damien Miller95def091999-11-25 00:26:21 +1100711after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000713 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100714 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100715 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100716 char buf[1024];
717 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Damien Miller95def091999-11-25 00:26:21 +1100719 for (i = 0; i < sockets_alloc; i++)
720 switch (sockets[i].type) {
721 case AUTH_UNUSED:
722 break;
723 case AUTH_SOCKET:
724 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100725 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000726 sock = accept(sockets[i].fd,
727 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100728 if (sock < 0) {
729 perror("accept from AUTH_SOCKET");
730 break;
731 }
732 new_socket(AUTH_CONNECTION, sock);
733 }
734 break;
735 case AUTH_CONNECTION:
736 if (buffer_len(&sockets[i].output) > 0 &&
737 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000738 do {
739 len = write(sockets[i].fd,
740 buffer_ptr(&sockets[i].output),
741 buffer_len(&sockets[i].output));
742 if (len == -1 && (errno == EAGAIN ||
743 errno == EINTR))
744 continue;
745 break;
746 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100747 if (len <= 0) {
748 shutdown(sockets[i].fd, SHUT_RDWR);
749 close(sockets[i].fd);
750 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000751 buffer_free(&sockets[i].input);
752 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
754 }
755 buffer_consume(&sockets[i].output, len);
756 }
757 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000758 do {
759 len = read(sockets[i].fd, buf, sizeof(buf));
760 if (len == -1 && (errno == EAGAIN ||
761 errno == EINTR))
762 continue;
763 break;
764 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100765 if (len <= 0) {
766 shutdown(sockets[i].fd, SHUT_RDWR);
767 close(sockets[i].fd);
768 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000769 buffer_free(&sockets[i].input);
770 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100771 break;
772 }
773 buffer_append(&sockets[i].input, buf, len);
774 process_message(&sockets[i]);
775 }
776 break;
777 default:
778 fatal("Unknown type %d", sockets[i].type);
779 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780}
781
Ben Lindstrombba81212001-06-25 05:01:22 +0000782static void
Damien Miller792c5111999-10-29 09:47:09 +1000783cleanup_socket(void)
784{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000785 if (socket_name[0])
786 unlink(socket_name);
787 if (socket_dir[0])
788 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Damien Miller792c5111999-10-29 09:47:09 +1000792cleanup_exit(int i)
793{
Damien Miller95def091999-11-25 00:26:21 +1100794 cleanup_socket();
795 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000796}
797
Ben Lindstrombba81212001-06-25 05:01:22 +0000798static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000799cleanup_handler(int sig)
800{
801 cleanup_socket();
802 _exit(2);
803}
804
Ben Lindstrombba81212001-06-25 05:01:22 +0000805static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000806check_parent_exists(int sig)
807{
808 int save_errno = errno;
809
810 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
811 /* printf("Parent has died - Authentication agent exiting.\n"); */
812 cleanup_handler(sig); /* safe */
813 }
814 signal(SIGALRM, check_parent_exists);
815 alarm(10);
816 errno = save_errno;
817}
818
819static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000820usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000821{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000822 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000823 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000824 fprintf(stderr, "Options:\n");
825 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
826 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
827 fprintf(stderr, " -k Kill the current agent.\n");
828 fprintf(stderr, " -d Debug mode.\n");
Damien Miller95def091999-11-25 00:26:21 +1100829 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000830}
831
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832int
833main(int ac, char **av)
834{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000835 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100836 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000837#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000838 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000839#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000840#ifdef HAVE_CYGWIN
841 int prev_mask;
842#endif
Damien Miller95def091999-11-25 00:26:21 +1100843 pid_t pid;
844 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000845 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000847
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000848 SSLeay_add_all_algorithms();
849
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000850 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000851 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100852 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000853
Damien Millerd0cff3e2000-04-20 23:12:58 +1000854#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000855 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000856#else /* __GNU_LIBRARY__ */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000857 while ((ch = getopt(ac, av, "cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000858#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100859 switch (ch) {
860 case 'c':
861 if (s_flag)
862 usage();
863 c_flag++;
864 break;
865 case 'k':
866 k_flag++;
867 break;
868 case 's':
869 if (c_flag)
870 usage();
871 s_flag++;
872 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000873 case 'd':
874 if (d_flag)
875 usage();
876 d_flag++;
877 break;
Damien Miller95def091999-11-25 00:26:21 +1100878 default:
879 usage();
880 }
Damien Miller792c5111999-10-29 09:47:09 +1000881 }
Damien Miller95def091999-11-25 00:26:21 +1100882 ac -= optind;
883 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000884
Ben Lindstromd94580c2001-07-04 03:48:02 +0000885 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100886 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000887
Ben Lindstromd94580c2001-07-04 03:48:02 +0000888 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100889 shell = getenv("SHELL");
890 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
891 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000892 }
Damien Miller95def091999-11-25 00:26:21 +1100893 if (k_flag) {
894 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
895 if (pidstr == NULL) {
896 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000897 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100898 exit(1);
899 }
900 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000901 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100902 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000903 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100904 exit(1);
905 }
906 if (kill(pid, SIGTERM) == -1) {
907 perror("kill");
908 exit(1);
909 }
910 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
911 printf(format, SSH_AUTHSOCKET_ENV_NAME);
912 printf(format, SSH_AGENTPID_ENV_NAME);
913 printf("echo Agent pid %d killed;\n", pid);
914 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000915 }
Damien Miller95def091999-11-25 00:26:21 +1100916 parent_pid = getpid();
917
918 /* Create private directory for agent socket */
919 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
920 if (mkdtemp(socket_dir) == NULL) {
921 perror("mkdtemp: private socket dir");
922 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000923 }
Damien Miller95def091999-11-25 00:26:21 +1100924 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000925 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller5428f641999-11-25 11:54:57 +1100927 /*
928 * Create socket early so it will exist before command gets run from
929 * the parent.
930 */
Damien Miller95def091999-11-25 00:26:21 +1100931 sock = socket(AF_UNIX, SOCK_STREAM, 0);
932 if (sock < 0) {
933 perror("socket");
934 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000935 }
Damien Miller95def091999-11-25 00:26:21 +1100936 memset(&sunaddr, 0, sizeof(sunaddr));
937 sunaddr.sun_family = AF_UNIX;
938 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000939#ifdef HAVE_CYGWIN
940 prev_mask = umask(0177);
941#endif
Damien Miller95def091999-11-25 00:26:21 +1100942 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
943 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000944#ifdef HAVE_CYGWIN
945 umask(prev_mask);
946#endif
Damien Miller95def091999-11-25 00:26:21 +1100947 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000949#ifdef HAVE_CYGWIN
950 umask(prev_mask);
951#endif
Damien Miller95def091999-11-25 00:26:21 +1100952 if (listen(sock, 5) < 0) {
953 perror("listen");
954 cleanup_exit(1);
955 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000956
Damien Miller5428f641999-11-25 11:54:57 +1100957 /*
958 * Fork, and have the parent execute the command, if any, or present
959 * the socket data. The child continues as the authentication agent.
960 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000961 if (d_flag) {
962 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
963 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
964 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
965 SSH_AUTHSOCKET_ENV_NAME);
966 printf("echo Agent pid %d;\n", parent_pid);
967 goto skip;
968 }
Damien Miller95def091999-11-25 00:26:21 +1100969 pid = fork();
970 if (pid == -1) {
971 perror("fork");
972 exit(1);
973 }
974 if (pid != 0) { /* Parent - execute the given command. */
975 close(sock);
976 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
977 if (ac == 0) {
978 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
979 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000980 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100981 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000982 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100983 printf("echo Agent pid %d;\n", pid);
984 exit(0);
985 }
Damien Millere4340be2000-09-16 13:29:08 +1100986 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
987 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
988 perror("setenv");
989 exit(1);
990 }
Damien Miller95def091999-11-25 00:26:21 +1100991 execvp(av[0], av);
992 perror(av[0]);
993 exit(1);
994 }
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000995
996 if (setsid() == -1) {
997 perror("setsid");
998 cleanup_exit(1);
999 }
1000
1001 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001002 close(0);
1003 close(1);
1004 close(2);
1005
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001006#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001007 /* deny core dumps, since memory contains unencrypted private keys */
1008 rlim.rlim_cur = rlim.rlim_max = 0;
1009 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1010 perror("setrlimit rlimit_core failed");
1011 cleanup_exit(1);
1012 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001014
1015skip:
Damien Miller95def091999-11-25 00:26:21 +11001016 if (atexit(cleanup_socket) < 0) {
1017 perror("atexit");
1018 cleanup_exit(1);
1019 }
1020 new_socket(AUTH_SOCKET, sock);
1021 if (ac > 0) {
1022 signal(SIGALRM, check_parent_exists);
1023 alarm(10);
1024 }
Damien Millerad833b32000-08-23 10:46:23 +10001025 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001026 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001027 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001028 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001029 signal(SIGHUP, cleanup_handler);
1030 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001031 nalloc = 0;
1032
Damien Miller95def091999-11-25 00:26:21 +11001033 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001034 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001035 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001036 if (errno == EINTR)
1037 continue;
1038 exit(1);
1039 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001040 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001041 }
1042 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043}