blob: 1874eb152860f853bc010fe2239aec6824629dd1 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36#include "includes.h"
Ben Lindstromba72d302002-03-22 03:51:06 +000037RCSID("$OpenBSD: ssh-agent.c,v 1.83 2002/03/21 22:44:05 rees Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038
Damien Millerec52d7c2002-01-22 23:52:17 +110039#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
40#include <sys/queue.h>
41#else
42#include "openbsd-compat/fake-queue.h"
43#endif
44
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include <openssl/evp.h>
46#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
49#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "buffer.h"
51#include "bufaux.h"
52#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100054#include "key.h"
55#include "authfd.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 Miller1a534ae2002-01-22 23:26:13 +110080typedef struct identity {
81 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100082 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110083 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084} Identity;
85
Damien Millerad833b32000-08-23 10:46:23 +100086typedef struct {
87 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110088 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100089} Idtab;
90
91/* private key table, one per protocol version */
92Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94int max_fd = 0;
95
96/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100097pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
99/* pathname and directory for AUTH_SOCKET */
100char socket_name[1024];
101char socket_dir[1024];
102
Damien Miller95def091999-11-25 00:26:21 +1100103#ifdef HAVE___PROGNAME
104extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000105#else
106char *__progname;
107#endif
Damien Miller95def091999-11-25 00:26:21 +1100108
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
Damien Millerad833b32000-08-23 10:46:23 +1000110idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Damien Millerad833b32000-08-23 10:46:23 +1000112 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000113 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100114 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000115 idtable[i].nentries = 0;
116 }
117}
118
119/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000121idtab_lookup(int version)
122{
123 if (version < 1 || version > 2)
124 fatal("internal error, bad protocol version %d", version);
125 return &idtable[version];
126}
127
128/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100129static Identity *
130lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000131{
Damien Miller1a534ae2002-01-22 23:26:13 +1100132 Identity *id;
133
Damien Millerad833b32000-08-23 10:46:23 +1000134 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100135 TAILQ_FOREACH(id, &tab->idlist, next) {
136 if (key_equal(key, id->key))
137 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000138 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100139 return (NULL);
140}
141
142static void
143free_identity(Identity *id)
144{
145 key_free(id->key);
146 xfree(id->comment);
147 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000148}
149
150/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Millerad833b32000-08-23 10:46:23 +1000152process_request_identities(SocketEntry *e, int version)
153{
154 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100155 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000159 buffer_put_char(&msg, (version == 1) ?
160 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
161 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100162 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000164 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
165 buffer_put_bignum(&msg, id->key->rsa->e);
166 buffer_put_bignum(&msg, id->key->rsa->n);
167 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000168 u_char *blob;
169 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000171 buffer_put_string(&msg, blob, blen);
172 xfree(blob);
173 }
174 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100175 }
176 buffer_put_int(&e->output, buffer_len(&msg));
177 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
178 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Damien Millerad833b32000-08-23 10:46:23 +1000181/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static void
Damien Millerad833b32000-08-23 10:46:23 +1000183process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 Identity *id;
186 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000187 BIGNUM *challenge;
188 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100189 Buffer msg;
190 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000191 u_char buf[32], mdbuf[16], session_id[16];
192 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Miller95def091999-11-25 00:26:21 +1100194 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100195 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100196 if ((challenge = BN_new()) == NULL)
197 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000198
199 buffer_get_int(&e->input); /* ignored */
200 buffer_get_bignum(&e->input, key->rsa->e);
201 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100202 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Millerad833b32000-08-23 10:46:23 +1000204 /* Only protocol 1.1 is supported */
205 if (buffer_len(&e->input) == 0)
206 goto failure;
Damien Miller4a8ed542002-01-22 23:33:31 +1100207 buffer_get(&e->input, session_id, 16);
Damien Millerad833b32000-08-23 10:46:23 +1000208 response_type = buffer_get_int(&e->input);
209 if (response_type != 1)
210 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller1a534ae2002-01-22 23:26:13 +1100212 id = lookup_identity(key, 1);
213 if (id != NULL) {
214 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000215 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100216 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
217 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100218
Damien Millerad833b32000-08-23 10:46:23 +1000219 /* The response is MD5 of decrypted challenge plus session id. */
220 len = BN_num_bytes(challenge);
221 if (len <= 0 || len > 32) {
222 log("process_authentication_challenge: bad challenge length %d", len);
223 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100224 }
Damien Millerad833b32000-08-23 10:46:23 +1000225 memset(buf, 0, 32);
226 BN_bn2bin(challenge, buf + 32 - len);
227 MD5_Init(&md);
228 MD5_Update(&md, buf, 32);
229 MD5_Update(&md, session_id, 16);
230 MD5_Final(mdbuf, &md);
231
232 /* Send the response. */
233 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
234 for (i = 0; i < 16; i++)
235 buffer_put_char(&msg, mdbuf[i]);
236 goto send;
237 }
238
239failure:
240 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100241 buffer_put_char(&msg, SSH_AGENT_FAILURE);
242send:
243 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000244 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
245 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100246 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000247 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248}
249
Damien Millerad833b32000-08-23 10:46:23 +1000250/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000251static void
Damien Millerad833b32000-08-23 10:46:23 +1000252process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Damien Millerad833b32000-08-23 10:46:23 +1000254 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100255 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000256 u_char *blob, *data, *signature = NULL;
257 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100258 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000259 Buffer msg;
260 int ok = -1;
261
262 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000263
Damien Millerad833b32000-08-23 10:46:23 +1000264 blob = buffer_get_string(&e->input, &blen);
265 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100266
267 flags = buffer_get_int(&e->input);
268 if (flags & SSH_AGENT_OLD_SIGNATURE)
269 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000270
Damien Miller0bc1bd82000-11-13 22:57:25 +1100271 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000272 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100273 Identity *id = lookup_identity(key, 2);
274 if (id != NULL)
275 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000276 }
277 key_free(key);
278 buffer_init(&msg);
279 if (ok == 0) {
280 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
281 buffer_put_string(&msg, signature, slen);
282 } else {
283 buffer_put_char(&msg, SSH_AGENT_FAILURE);
284 }
285 buffer_put_int(&e->output, buffer_len(&msg));
286 buffer_append(&e->output, buffer_ptr(&msg),
287 buffer_len(&msg));
288 buffer_free(&msg);
289 xfree(data);
290 xfree(blob);
291 if (signature != NULL)
292 xfree(signature);
293}
294
295/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
Damien Millerad833b32000-08-23 10:46:23 +1000297process_remove_identity(SocketEntry *e, int version)
298{
Damien Miller1a534ae2002-01-22 23:26:13 +1100299 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_char *blob;
301 u_int blen;
302 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000303 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100304
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000305 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000306 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100307 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000308 bits = buffer_get_int(&e->input);
309 buffer_get_bignum(&e->input, key->rsa->e);
310 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Millerad833b32000-08-23 10:46:23 +1000312 if (bits != key_size(key))
313 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000314 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000315 break;
316 case 2:
317 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000319 xfree(blob);
320 break;
321 }
322 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100323 Identity *id = lookup_identity(key, version);
324 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100325 /*
326 * We have this key. Free the old key. Since we
327 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000328 * the array, we actually free the key there and move
329 * all the entries between the empty slot and the end
330 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100331 */
Damien Millerad833b32000-08-23 10:46:23 +1000332 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333 if (tab->nentries < 1)
334 fatal("process_remove_identity: "
335 "internal error: tab->nentries %d",
336 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100337 TAILQ_REMOVE(&tab->idlist, id, next);
338 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000339 tab->nentries--;
340 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100341 }
Damien Millerad833b32000-08-23 10:46:23 +1000342 key_free(key);
343 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000345 buffer_put_char(&e->output,
346 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static void
Damien Millerad833b32000-08-23 10:46:23 +1000350process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Damien Millerad833b32000-08-23 10:46:23 +1000352 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100353 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100356 for (id = TAILQ_FIRST(&tab->idlist); id;
357 id = TAILQ_FIRST(&tab->idlist)) {
358 TAILQ_REMOVE(&tab->idlist, id, next);
359 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Damien Miller95def091999-11-25 00:26:21 +1100362 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000363 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
365 /* Send success. */
366 buffer_put_int(&e->output, 1);
367 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
368 return;
Damien Miller95def091999-11-25 00:26:21 +1100369}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static void
Damien Millerad833b32000-08-23 10:46:23 +1000372process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100373{
Damien Millerad833b32000-08-23 10:46:23 +1000374 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000376 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000378 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100379
Damien Millerad833b32000-08-23 10:46:23 +1000380 switch (version) {
381 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000383 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 buffer_get_bignum(&e->input, k->rsa->n);
385 buffer_get_bignum(&e->input, k->rsa->e);
386 buffer_get_bignum(&e->input, k->rsa->d);
387 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100388
Damien Millerad833b32000-08-23 10:46:23 +1000389 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100390 buffer_get_bignum(&e->input, k->rsa->q); /* p */
391 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100392
Damien Millerad833b32000-08-23 10:46:23 +1000393 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000394 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000395 break;
396 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000398 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000400 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 case KEY_DSA:
402 k = key_new_private(type);
403 buffer_get_bignum2(&e->input, k->dsa->p);
404 buffer_get_bignum2(&e->input, k->dsa->q);
405 buffer_get_bignum2(&e->input, k->dsa->g);
406 buffer_get_bignum2(&e->input, k->dsa->pub_key);
407 buffer_get_bignum2(&e->input, k->dsa->priv_key);
408 break;
409 case KEY_RSA:
410 k = key_new_private(type);
411 buffer_get_bignum2(&e->input, k->rsa->n);
412 buffer_get_bignum2(&e->input, k->rsa->e);
413 buffer_get_bignum2(&e->input, k->rsa->d);
414 buffer_get_bignum2(&e->input, k->rsa->iqmp);
415 buffer_get_bignum2(&e->input, k->rsa->p);
416 buffer_get_bignum2(&e->input, k->rsa->q);
417
418 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000419 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100420 break;
421 default:
Damien Millerad833b32000-08-23 10:46:23 +1000422 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000423 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100424 }
Damien Millerad833b32000-08-23 10:46:23 +1000425 break;
426 }
Damien Millerad833b32000-08-23 10:46:23 +1000427 comment = buffer_get_string(&e->input, NULL);
428 if (k == NULL) {
429 xfree(comment);
430 goto send;
431 }
432 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100433 if (lookup_identity(k, version) == NULL) {
434 Identity *id = xmalloc(sizeof(Identity));
435 id->key = k;
436 id->comment = comment;
437 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000438 /* Increment the number of identities. */
439 tab->nentries++;
440 } else {
441 key_free(k);
442 xfree(comment);
443 }
444send:
Damien Miller95def091999-11-25 00:26:21 +1100445 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000446 buffer_put_char(&e->output,
447 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448}
449
Ben Lindstrom3f471632001-07-04 03:53:15 +0000450
451#ifdef SMARTCARD
452static void
453process_add_smartcard_key (SocketEntry *e)
454{
455 Idtab *tab;
456 Key *n = NULL, *k = NULL;
Ben Lindstromba72d302002-03-22 03:51:06 +0000457 char *sc_reader_id = NULL, *pin;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000458 int success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100459
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000460 sc_reader_id = buffer_get_string(&e->input, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000461 pin = buffer_get_string(&e->input, NULL);
462 k = sc_get_key(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000463 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000464 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000465
Ben Lindstrom3f471632001-07-04 03:53:15 +0000466 if (k == NULL) {
467 error("sc_get_pubkey failed");
468 goto send;
469 }
470 success = 1;
471
472 tab = idtab_lookup(1);
Damien Millerf3512d92001-07-14 12:14:27 +1000473 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100474 if (lookup_identity(k, 1) == NULL) {
475 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000476 n = key_new(KEY_RSA1);
477 BN_copy(n->rsa->n, k->rsa->n);
478 BN_copy(n->rsa->e, k->rsa->e);
479 RSA_set_method(n->rsa, sc_get_engine());
Damien Miller1a534ae2002-01-22 23:26:13 +1100480 id->key = n;
481 id->comment = xstrdup("rsa1 smartcard");
482 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000483 tab->nentries++;
484 }
Damien Millerf3512d92001-07-14 12:14:27 +1000485 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000486 tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100487 if (lookup_identity(k, 2) == NULL) {
488 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000489 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());
Damien Miller1a534ae2002-01-22 23:26:13 +1100493 id->key = n;
494 id->comment = xstrdup("rsa smartcard");
495 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000496 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{
Damien Miller1a534ae2002-01-22 23:26:13 +1100508 Key *k = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000509 int success = 0;
Ben Lindstromba72d302002-03-22 03:51:06 +0000510 char *sc_reader_id = NULL, *pin;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000511
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000512 sc_reader_id = buffer_get_string(&e->input, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000513 pin = buffer_get_string(&e->input, NULL);
514 k = sc_get_key(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000515 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000516 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000517
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000518 if (k == NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000519 error("sc_get_pubkey failed");
520 } else {
Damien Miller1a534ae2002-01-22 23:26:13 +1100521 Identity *id;
Damien Miller8d4bf172001-07-14 12:13:49 +1000522 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100523 id = lookup_identity(k, 1);
524 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000525 Idtab *tab = idtab_lookup(1);
Damien Miller1a534ae2002-01-22 23:26:13 +1100526 TAILQ_REMOVE(&tab->idlist, id, next);
527 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000528 tab->nentries--;
529 success = 1;
530 }
Damien Miller8d4bf172001-07-14 12:13:49 +1000531 k->type = KEY_RSA;
Damien Miller1a534ae2002-01-22 23:26:13 +1100532 id = lookup_identity(k, 2);
533 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000534 Idtab *tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100535 TAILQ_REMOVE(&tab->idlist, id, next);
536 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000537 tab->nentries--;
538 success = 1;
539 }
540 key_free(k);
541 }
542
543 buffer_put_int(&e->output, 1);
544 buffer_put_char(&e->output,
545 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
546}
Ben Lindstromffce1472001-08-06 21:57:31 +0000547#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000548
Damien Millerad833b32000-08-23 10:46:23 +1000549/* dispatch incoming messages */
550
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552process_message(SocketEntry *e)
553{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000554 u_int msg_len;
555 u_int type;
556 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100557 if (buffer_len(&e->input) < 5)
558 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100559 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100560 msg_len = GET_32BIT(cp);
561 if (msg_len > 256 * 1024) {
562 shutdown(e->fd, SHUT_RDWR);
563 close(e->fd);
564 e->type = AUTH_UNUSED;
565 return;
566 }
567 if (buffer_len(&e->input) < msg_len + 4)
568 return;
569 buffer_consume(&e->input, 4);
570 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Ben Lindstrom3f471632001-07-04 03:53:15 +0000572 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100573 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000574 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100575 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000576 process_authentication_challenge1(e);
577 break;
578 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
579 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100580 break;
581 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000582 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100583 break;
584 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000585 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100586 break;
587 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000588 process_remove_all_identities(e, 1);
589 break;
590 /* ssh2 */
591 case SSH2_AGENTC_SIGN_REQUEST:
592 process_sign_request2(e);
593 break;
594 case SSH2_AGENTC_REQUEST_IDENTITIES:
595 process_request_identities(e, 2);
596 break;
597 case SSH2_AGENTC_ADD_IDENTITY:
598 process_add_identity(e, 2);
599 break;
600 case SSH2_AGENTC_REMOVE_IDENTITY:
601 process_remove_identity(e, 2);
602 break;
603 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
604 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100605 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000606#ifdef SMARTCARD
607 case SSH_AGENTC_ADD_SMARTCARD_KEY:
608 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100609 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000610 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
611 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100612 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000613#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100614 default:
615 /* Unknown message. Respond with failure. */
616 error("Unknown message %d", type);
617 buffer_clear(&e->input);
618 buffer_put_int(&e->output, 1);
619 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
620 break;
621 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622}
623
Ben Lindstrombba81212001-06-25 05:01:22 +0000624static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000625new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000627 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100628 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
629 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Damien Miller95def091999-11-25 00:26:21 +1100631 if (fd > max_fd)
632 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
Damien Miller95def091999-11-25 00:26:21 +1100634 for (i = 0; i < sockets_alloc; i++)
635 if (sockets[i].type == AUTH_UNUSED) {
636 sockets[i].fd = fd;
637 sockets[i].type = type;
638 buffer_init(&sockets[i].input);
639 buffer_init(&sockets[i].output);
640 return;
641 }
642 old_alloc = sockets_alloc;
643 sockets_alloc += 10;
644 if (sockets)
645 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
646 else
647 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
648 for (i = old_alloc; i < sockets_alloc; i++)
649 sockets[i].type = AUTH_UNUSED;
650 sockets[old_alloc].type = type;
651 sockets[old_alloc].fd = fd;
652 buffer_init(&sockets[old_alloc].input);
653 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654}
655
Ben Lindstrombba81212001-06-25 05:01:22 +0000656static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000657prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000659 u_int i, sz;
660 int n = 0;
661
662 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100663 switch (sockets[i].type) {
664 case AUTH_SOCKET:
665 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000666 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100667 break;
668 case AUTH_UNUSED:
669 break;
670 default:
671 fatal("Unknown socket type %d", sockets[i].type);
672 break;
673 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000674 }
675
676 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000677 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000678 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000679 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000680 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000681 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000682 *fdrp = xmalloc(sz);
683 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000684 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000685 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000686 if (n < *fdl)
687 debug("XXX shrink: %d < %d", n, *fdl);
688 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000689 memset(*fdrp, 0, sz);
690 memset(*fdwp, 0, sz);
691
692 for (i = 0; i < sockets_alloc; i++) {
693 switch (sockets[i].type) {
694 case AUTH_SOCKET:
695 case AUTH_CONNECTION:
696 FD_SET(sockets[i].fd, *fdrp);
697 if (buffer_len(&sockets[i].output) > 0)
698 FD_SET(sockets[i].fd, *fdwp);
699 break;
700 default:
701 break;
702 }
703 }
704 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static void
Damien Miller95def091999-11-25 00:26:21 +1100708after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000710 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100711 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100712 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100713 char buf[1024];
714 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller95def091999-11-25 00:26:21 +1100716 for (i = 0; i < sockets_alloc; i++)
717 switch (sockets[i].type) {
718 case AUTH_UNUSED:
719 break;
720 case AUTH_SOCKET:
721 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100722 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000723 sock = accept(sockets[i].fd,
724 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100725 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100726 error("accept from AUTH_SOCKET: %s",
727 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100728 break;
729 }
730 new_socket(AUTH_CONNECTION, sock);
731 }
732 break;
733 case AUTH_CONNECTION:
734 if (buffer_len(&sockets[i].output) > 0 &&
735 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000736 do {
737 len = write(sockets[i].fd,
738 buffer_ptr(&sockets[i].output),
739 buffer_len(&sockets[i].output));
740 if (len == -1 && (errno == EAGAIN ||
741 errno == EINTR))
742 continue;
743 break;
744 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100745 if (len <= 0) {
746 shutdown(sockets[i].fd, SHUT_RDWR);
747 close(sockets[i].fd);
748 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000749 buffer_free(&sockets[i].input);
750 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100751 break;
752 }
753 buffer_consume(&sockets[i].output, len);
754 }
755 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000756 do {
757 len = read(sockets[i].fd, buf, sizeof(buf));
758 if (len == -1 && (errno == EAGAIN ||
759 errno == EINTR))
760 continue;
761 break;
762 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100763 if (len <= 0) {
764 shutdown(sockets[i].fd, SHUT_RDWR);
765 close(sockets[i].fd);
766 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000767 buffer_free(&sockets[i].input);
768 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100769 break;
770 }
771 buffer_append(&sockets[i].input, buf, len);
772 process_message(&sockets[i]);
773 }
774 break;
775 default:
776 fatal("Unknown type %d", sockets[i].type);
777 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778}
779
Ben Lindstrombba81212001-06-25 05:01:22 +0000780static void
Damien Millerc653b892002-02-08 22:05:41 +1100781cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000782{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000783 if (socket_name[0])
784 unlink(socket_name);
785 if (socket_dir[0])
786 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787}
788
Ben Lindstrombba81212001-06-25 05:01:22 +0000789static void
Damien Miller792c5111999-10-29 09:47:09 +1000790cleanup_exit(int i)
791{
Damien Millerc653b892002-02-08 22:05:41 +1100792 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100793 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000794}
795
Ben Lindstrombba81212001-06-25 05:01:22 +0000796static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000797cleanup_handler(int sig)
798{
Damien Millerc653b892002-02-08 22:05:41 +1100799 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000800 _exit(2);
801}
802
Ben Lindstrombba81212001-06-25 05:01:22 +0000803static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000804check_parent_exists(int sig)
805{
806 int save_errno = errno;
807
808 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
809 /* printf("Parent has died - Authentication agent exiting.\n"); */
810 cleanup_handler(sig); /* safe */
811 }
812 signal(SIGALRM, check_parent_exists);
813 alarm(10);
814 errno = save_errno;
815}
816
817static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000818usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000819{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000820 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000821 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000822 fprintf(stderr, "Options:\n");
823 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
824 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
825 fprintf(stderr, " -k Kill the current agent.\n");
826 fprintf(stderr, " -d Debug mode.\n");
Damien Miller95def091999-11-25 00:26:21 +1100827 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000828}
829
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830int
831main(int ac, char **av)
832{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000833 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100834 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000835#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000836 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000837#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000838#ifdef HAVE_CYGWIN
839 int prev_mask;
840#endif
Damien Miller95def091999-11-25 00:26:21 +1100841 pid_t pid;
842 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000843 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000844 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000845
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000846 SSLeay_add_all_algorithms();
847
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000848 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000849 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100850 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000851
Damien Millerd0cff3e2000-04-20 23:12:58 +1000852#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000853 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000854#else /* __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#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100857 switch (ch) {
858 case 'c':
859 if (s_flag)
860 usage();
861 c_flag++;
862 break;
863 case 'k':
864 k_flag++;
865 break;
866 case 's':
867 if (c_flag)
868 usage();
869 s_flag++;
870 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000871 case 'd':
872 if (d_flag)
873 usage();
874 d_flag++;
875 break;
Damien Miller95def091999-11-25 00:26:21 +1100876 default:
877 usage();
878 }
Damien Miller792c5111999-10-29 09:47:09 +1000879 }
Damien Miller95def091999-11-25 00:26:21 +1100880 ac -= optind;
881 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000882
Ben Lindstromd94580c2001-07-04 03:48:02 +0000883 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100884 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000885
Ben Lindstromd94580c2001-07-04 03:48:02 +0000886 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100887 shell = getenv("SHELL");
888 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
889 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000890 }
Damien Miller95def091999-11-25 00:26:21 +1100891 if (k_flag) {
892 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
893 if (pidstr == NULL) {
894 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000895 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100896 exit(1);
897 }
898 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000899 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100900 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000901 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100902 exit(1);
903 }
904 if (kill(pid, SIGTERM) == -1) {
905 perror("kill");
906 exit(1);
907 }
908 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
909 printf(format, SSH_AUTHSOCKET_ENV_NAME);
910 printf(format, SSH_AGENTPID_ENV_NAME);
911 printf("echo Agent pid %d killed;\n", pid);
912 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000913 }
Damien Miller95def091999-11-25 00:26:21 +1100914 parent_pid = getpid();
915
916 /* Create private directory for agent socket */
917 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
918 if (mkdtemp(socket_dir) == NULL) {
919 perror("mkdtemp: private socket dir");
920 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000921 }
Damien Miller95def091999-11-25 00:26:21 +1100922 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000923 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924
Damien Miller5428f641999-11-25 11:54:57 +1100925 /*
926 * Create socket early so it will exist before command gets run from
927 * the parent.
928 */
Damien Miller95def091999-11-25 00:26:21 +1100929 sock = socket(AF_UNIX, SOCK_STREAM, 0);
930 if (sock < 0) {
931 perror("socket");
932 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000933 }
Damien Miller95def091999-11-25 00:26:21 +1100934 memset(&sunaddr, 0, sizeof(sunaddr));
935 sunaddr.sun_family = AF_UNIX;
936 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000937#ifdef HAVE_CYGWIN
938 prev_mask = umask(0177);
939#endif
Damien Miller95def091999-11-25 00:26:21 +1100940 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
941 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000942#ifdef HAVE_CYGWIN
943 umask(prev_mask);
944#endif
Damien Miller95def091999-11-25 00:26:21 +1100945 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000947#ifdef HAVE_CYGWIN
948 umask(prev_mask);
949#endif
Damien Miller95def091999-11-25 00:26:21 +1100950 if (listen(sock, 5) < 0) {
951 perror("listen");
952 cleanup_exit(1);
953 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954
Damien Miller5428f641999-11-25 11:54:57 +1100955 /*
956 * Fork, and have the parent execute the command, if any, or present
957 * the socket data. The child continues as the authentication agent.
958 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000959 if (d_flag) {
960 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
961 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
962 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
963 SSH_AUTHSOCKET_ENV_NAME);
964 printf("echo Agent pid %d;\n", parent_pid);
965 goto skip;
966 }
Damien Miller95def091999-11-25 00:26:21 +1100967 pid = fork();
968 if (pid == -1) {
969 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +1100970 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100971 }
972 if (pid != 0) { /* Parent - execute the given command. */
973 close(sock);
974 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
975 if (ac == 0) {
976 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
977 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000978 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100979 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000980 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100981 printf("echo Agent pid %d;\n", pid);
982 exit(0);
983 }
Damien Millere4340be2000-09-16 13:29:08 +1100984 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
985 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
986 perror("setenv");
987 exit(1);
988 }
Damien Miller95def091999-11-25 00:26:21 +1100989 execvp(av[0], av);
990 perror(av[0]);
991 exit(1);
992 }
Damien Millerc653b892002-02-08 22:05:41 +1100993 /* child */
994 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000995
996 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +1100997 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000998 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) {
Damien Millerc653b892002-02-08 22:05:41 +11001010 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001011 cleanup_exit(1);
1012 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001014
1015skip:
Damien Millerc653b892002-02-08 22:05:41 +11001016 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001017 new_socket(AUTH_SOCKET, sock);
1018 if (ac > 0) {
1019 signal(SIGALRM, check_parent_exists);
1020 alarm(10);
1021 }
Damien Millerad833b32000-08-23 10:46:23 +10001022 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001023 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001024 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001025 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001026 signal(SIGHUP, cleanup_handler);
1027 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001028 nalloc = 0;
1029
Damien Miller95def091999-11-25 00:26:21 +11001030 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001031 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001032 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001033 if (errno == EINTR)
1034 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001035 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001036 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001037 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001038 }
1039 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040}