blob: 84cff1282a9a654470cc64f197a7df8c2179bb21 [file] [log] [blame]
Damien Miller1a534ae2002-01-22 23:26:13 +11001/* $OpenBSD: ssh-agent.c,v 1.78 2002/01/13 17:27:07 provos 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 Miller1a534ae2002-01-22 23:26:13 +110039#include <sys/queue.h>
40RCSID("$OpenBSD: ssh-agent.c,v 1.78 2002/01/13 17:27:07 provos Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041
42#include <openssl/evp.h>
43#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
46#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "buffer.h"
48#include "bufaux.h"
49#include "xmalloc.h"
50#include "packet.h"
51#include "getput.h"
52#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100053#include "key.h"
54#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100056#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Ben Lindstrom3f471632001-07-04 03:53:15 +000060#ifdef SMARTCARD
61#include <openssl/engine.h>
62#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000063#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000064
Ben Lindstrom65366a82001-12-06 16:32:47 +000065typedef enum {
66 AUTH_UNUSED,
67 AUTH_SOCKET,
68 AUTH_CONNECTION
69} sock_type;
70
Damien Miller95def091999-11-25 00:26:21 +110071typedef struct {
72 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000073 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110074 Buffer input;
75 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076} SocketEntry;
77
Ben Lindstrom46c16222000-12-22 01:43:59 +000078u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079SocketEntry *sockets = NULL;
80
Damien Miller1a534ae2002-01-22 23:26:13 +110081typedef struct identity {
82 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100083 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110084 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085} Identity;
86
Damien Millerad833b32000-08-23 10:46:23 +100087typedef struct {
88 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110089 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100090} Idtab;
91
92/* private key table, one per protocol version */
93Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
95int max_fd = 0;
96
97/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100098pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
100/* pathname and directory for AUTH_SOCKET */
101char socket_name[1024];
102char socket_dir[1024];
103
Damien Miller95def091999-11-25 00:26:21 +1100104#ifdef HAVE___PROGNAME
105extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000106#else
107char *__progname;
108#endif
Damien Miller95def091999-11-25 00:26:21 +1100109
Ben Lindstrombba81212001-06-25 05:01:22 +0000110static void
Damien Millerad833b32000-08-23 10:46:23 +1000111idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112{
Damien Millerad833b32000-08-23 10:46:23 +1000113 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000114 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100115 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000116 idtable[i].nentries = 0;
117 }
118}
119
120/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000122idtab_lookup(int version)
123{
124 if (version < 1 || version > 2)
125 fatal("internal error, bad protocol version %d", version);
126 return &idtable[version];
127}
128
129/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100130static Identity *
131lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000132{
Damien Miller1a534ae2002-01-22 23:26:13 +1100133 Identity *id;
134
Damien Millerad833b32000-08-23 10:46:23 +1000135 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100136 TAILQ_FOREACH(id, &tab->idlist, next) {
137 if (key_equal(key, id->key))
138 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000139 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100140 return (NULL);
141}
142
143static void
144free_identity(Identity *id)
145{
146 key_free(id->key);
147 xfree(id->comment);
148 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000149}
150
151/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Damien Millerad833b32000-08-23 10:46:23 +1000153process_request_identities(SocketEntry *e, int version)
154{
155 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100156 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100157 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Damien Miller95def091999-11-25 00:26:21 +1100159 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000160 buffer_put_char(&msg, (version == 1) ?
161 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
162 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100163 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000165 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
166 buffer_put_bignum(&msg, id->key->rsa->e);
167 buffer_put_bignum(&msg, id->key->rsa->n);
168 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000169 u_char *blob;
170 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000172 buffer_put_string(&msg, blob, blen);
173 xfree(blob);
174 }
175 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100176 }
177 buffer_put_int(&e->output, buffer_len(&msg));
178 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
179 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Damien Millerad833b32000-08-23 10:46:23 +1000182/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static void
Damien Millerad833b32000-08-23 10:46:23 +1000184process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller1a534ae2002-01-22 23:26:13 +1100186 Identity *id;
187 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000188 BIGNUM *challenge;
189 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100190 Buffer msg;
191 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000192 u_char buf[32], mdbuf[16], session_id[16];
193 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller95def091999-11-25 00:26:21 +1100195 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100197 if ((challenge = BN_new()) == NULL)
198 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000199
200 buffer_get_int(&e->input); /* ignored */
201 buffer_get_bignum(&e->input, key->rsa->e);
202 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100203 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Millerad833b32000-08-23 10:46:23 +1000205 /* Only protocol 1.1 is supported */
206 if (buffer_len(&e->input) == 0)
207 goto failure;
208 buffer_get(&e->input, (char *) session_id, 16);
209 response_type = buffer_get_int(&e->input);
210 if (response_type != 1)
211 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Miller1a534ae2002-01-22 23:26:13 +1100213 id = lookup_identity(key, 1);
214 if (id != NULL) {
215 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000216 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100217 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
218 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100219
Damien Millerad833b32000-08-23 10:46:23 +1000220 /* The response is MD5 of decrypted challenge plus session id. */
221 len = BN_num_bytes(challenge);
222 if (len <= 0 || len > 32) {
223 log("process_authentication_challenge: bad challenge length %d", len);
224 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100225 }
Damien Millerad833b32000-08-23 10:46:23 +1000226 memset(buf, 0, 32);
227 BN_bn2bin(challenge, buf + 32 - len);
228 MD5_Init(&md);
229 MD5_Update(&md, buf, 32);
230 MD5_Update(&md, session_id, 16);
231 MD5_Final(mdbuf, &md);
232
233 /* Send the response. */
234 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
235 for (i = 0; i < 16; i++)
236 buffer_put_char(&msg, mdbuf[i]);
237 goto send;
238 }
239
240failure:
241 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100242 buffer_put_char(&msg, SSH_AGENT_FAILURE);
243send:
244 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000245 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
246 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100247 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000248 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
250
Damien Millerad833b32000-08-23 10:46:23 +1000251/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Millerad833b32000-08-23 10:46:23 +1000253process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254{
Damien Millerad833b32000-08-23 10:46:23 +1000255 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100256 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000257 u_char *blob, *data, *signature = NULL;
258 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100259 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000260 Buffer msg;
261 int ok = -1;
262
263 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000264
Damien Millerad833b32000-08-23 10:46:23 +1000265 blob = buffer_get_string(&e->input, &blen);
266 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100267
268 flags = buffer_get_int(&e->input);
269 if (flags & SSH_AGENT_OLD_SIGNATURE)
270 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000271
Damien Miller0bc1bd82000-11-13 22:57:25 +1100272 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000273 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100274 Identity *id = lookup_identity(key, 2);
275 if (id != NULL)
276 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000277 }
278 key_free(key);
279 buffer_init(&msg);
280 if (ok == 0) {
281 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
282 buffer_put_string(&msg, signature, slen);
283 } else {
284 buffer_put_char(&msg, SSH_AGENT_FAILURE);
285 }
286 buffer_put_int(&e->output, buffer_len(&msg));
287 buffer_append(&e->output, buffer_ptr(&msg),
288 buffer_len(&msg));
289 buffer_free(&msg);
290 xfree(data);
291 xfree(blob);
292 if (signature != NULL)
293 xfree(signature);
294}
295
296/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000297static void
Damien Millerad833b32000-08-23 10:46:23 +1000298process_remove_identity(SocketEntry *e, int version)
299{
Damien Miller1a534ae2002-01-22 23:26:13 +1100300 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000301 u_char *blob;
302 u_int blen;
303 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000304 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100305
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000306 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000307 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100308 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000309 bits = buffer_get_int(&e->input);
310 buffer_get_bignum(&e->input, key->rsa->e);
311 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Millerad833b32000-08-23 10:46:23 +1000313 if (bits != key_size(key))
314 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000315 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000316 break;
317 case 2:
318 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100319 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000320 xfree(blob);
321 break;
322 }
323 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100324 Identity *id = lookup_identity(key, version);
325 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100326 /*
327 * We have this key. Free the old key. Since we
328 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000329 * the array, we actually free the key there and move
330 * all the entries between the empty slot and the end
331 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100332 */
Damien Millerad833b32000-08-23 10:46:23 +1000333 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100334 if (tab->nentries < 1)
335 fatal("process_remove_identity: "
336 "internal error: tab->nentries %d",
337 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100338 TAILQ_REMOVE(&tab->idlist, id, next);
339 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000340 tab->nentries--;
341 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100342 }
Damien Millerad833b32000-08-23 10:46:23 +1000343 key_free(key);
344 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000346 buffer_put_char(&e->output,
347 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
Ben Lindstrombba81212001-06-25 05:01:22 +0000350static void
Damien Millerad833b32000-08-23 10:46:23 +1000351process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Millerad833b32000-08-23 10:46:23 +1000353 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100354 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100357 for (id = TAILQ_FIRST(&tab->idlist); id;
358 id = TAILQ_FIRST(&tab->idlist)) {
359 TAILQ_REMOVE(&tab->idlist, id, next);
360 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller95def091999-11-25 00:26:21 +1100363 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000364 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
366 /* Send success. */
367 buffer_put_int(&e->output, 1);
368 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
369 return;
Damien Miller95def091999-11-25 00:26:21 +1100370}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Ben Lindstrombba81212001-06-25 05:01:22 +0000372static void
Damien Millerad833b32000-08-23 10:46:23 +1000373process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100374{
Damien Millerad833b32000-08-23 10:46:23 +1000375 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000377 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000379 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100380
Damien Millerad833b32000-08-23 10:46:23 +1000381 switch (version) {
382 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000384 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100385 buffer_get_bignum(&e->input, k->rsa->n);
386 buffer_get_bignum(&e->input, k->rsa->e);
387 buffer_get_bignum(&e->input, k->rsa->d);
388 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100389
Damien Millerad833b32000-08-23 10:46:23 +1000390 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100391 buffer_get_bignum(&e->input, k->rsa->q); /* p */
392 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100393
Damien Millerad833b32000-08-23 10:46:23 +1000394 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000395 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000396 break;
397 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100398 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000399 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100400 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000401 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100402 case KEY_DSA:
403 k = key_new_private(type);
404 buffer_get_bignum2(&e->input, k->dsa->p);
405 buffer_get_bignum2(&e->input, k->dsa->q);
406 buffer_get_bignum2(&e->input, k->dsa->g);
407 buffer_get_bignum2(&e->input, k->dsa->pub_key);
408 buffer_get_bignum2(&e->input, k->dsa->priv_key);
409 break;
410 case KEY_RSA:
411 k = key_new_private(type);
412 buffer_get_bignum2(&e->input, k->rsa->n);
413 buffer_get_bignum2(&e->input, k->rsa->e);
414 buffer_get_bignum2(&e->input, k->rsa->d);
415 buffer_get_bignum2(&e->input, k->rsa->iqmp);
416 buffer_get_bignum2(&e->input, k->rsa->p);
417 buffer_get_bignum2(&e->input, k->rsa->q);
418
419 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000420 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100421 break;
422 default:
Damien Millerad833b32000-08-23 10:46:23 +1000423 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000424 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100425 }
Damien Millerad833b32000-08-23 10:46:23 +1000426 break;
427 }
Damien Millerad833b32000-08-23 10:46:23 +1000428 comment = buffer_get_string(&e->input, NULL);
429 if (k == NULL) {
430 xfree(comment);
431 goto send;
432 }
433 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100434 if (lookup_identity(k, version) == NULL) {
435 Identity *id = xmalloc(sizeof(Identity));
436 id->key = k;
437 id->comment = comment;
438 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000439 /* Increment the number of identities. */
440 tab->nentries++;
441 } else {
442 key_free(k);
443 xfree(comment);
444 }
445send:
Damien Miller95def091999-11-25 00:26:21 +1100446 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000447 buffer_put_char(&e->output,
448 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449}
450
Ben Lindstrom3f471632001-07-04 03:53:15 +0000451
452#ifdef SMARTCARD
453static void
454process_add_smartcard_key (SocketEntry *e)
455{
456 Idtab *tab;
457 Key *n = NULL, *k = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000458 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000459 int success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100460
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000461 sc_reader_id = buffer_get_string(&e->input, NULL);
462 k = sc_get_key(sc_reader_id);
463 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000464
Ben Lindstrom3f471632001-07-04 03:53:15 +0000465 if (k == NULL) {
466 error("sc_get_pubkey failed");
467 goto send;
468 }
469 success = 1;
470
471 tab = idtab_lookup(1);
Damien Millerf3512d92001-07-14 12:14:27 +1000472 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100473 if (lookup_identity(k, 1) == NULL) {
474 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000475 n = key_new(KEY_RSA1);
476 BN_copy(n->rsa->n, k->rsa->n);
477 BN_copy(n->rsa->e, k->rsa->e);
478 RSA_set_method(n->rsa, sc_get_engine());
Damien Miller1a534ae2002-01-22 23:26:13 +1100479 id->key = n;
480 id->comment = xstrdup("rsa1 smartcard");
481 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000482 tab->nentries++;
483 }
Damien Millerf3512d92001-07-14 12:14:27 +1000484 k->type = KEY_RSA;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000485 tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100486 if (lookup_identity(k, 2) == NULL) {
487 Identity *id = xmalloc(sizeof(Identity));
Ben Lindstrom3f471632001-07-04 03:53:15 +0000488 n = key_new(KEY_RSA);
489 BN_copy(n->rsa->n, k->rsa->n);
490 BN_copy(n->rsa->e, k->rsa->e);
491 RSA_set_method(n->rsa, sc_get_engine());
Damien Miller1a534ae2002-01-22 23:26:13 +1100492 id->key = n;
493 id->comment = xstrdup("rsa smartcard");
494 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000495 tab->nentries++;
496 }
497 key_free(k);
498send:
499 buffer_put_int(&e->output, 1);
500 buffer_put_char(&e->output,
501 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
502}
503
504static void
505process_remove_smartcard_key(SocketEntry *e)
506{
Damien Miller1a534ae2002-01-22 23:26:13 +1100507 Key *k = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000508 int success = 0;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000509 char *sc_reader_id = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000510
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000511 sc_reader_id = buffer_get_string(&e->input, NULL);
512 k = sc_get_key(sc_reader_id);
513 xfree(sc_reader_id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000514
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000515 if (k == NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000516 error("sc_get_pubkey failed");
517 } else {
Damien Miller1a534ae2002-01-22 23:26:13 +1100518 Identity *id;
Damien Miller8d4bf172001-07-14 12:13:49 +1000519 k->type = KEY_RSA1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100520 id = lookup_identity(k, 1);
521 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000522 Idtab *tab = idtab_lookup(1);
Damien Miller1a534ae2002-01-22 23:26:13 +1100523 TAILQ_REMOVE(&tab->idlist, id, next);
524 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000525 tab->nentries--;
526 success = 1;
527 }
Damien Miller8d4bf172001-07-14 12:13:49 +1000528 k->type = KEY_RSA;
Damien Miller1a534ae2002-01-22 23:26:13 +1100529 id = lookup_identity(k, 2);
530 if (id != NULL) {
Ben Lindstrom3f471632001-07-04 03:53:15 +0000531 Idtab *tab = idtab_lookup(2);
Damien Miller1a534ae2002-01-22 23:26:13 +1100532 TAILQ_REMOVE(&tab->idlist, id, next);
533 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000534 tab->nentries--;
535 success = 1;
536 }
537 key_free(k);
538 }
539
540 buffer_put_int(&e->output, 1);
541 buffer_put_char(&e->output,
542 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
543}
Ben Lindstromffce1472001-08-06 21:57:31 +0000544#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000545
Damien Millerad833b32000-08-23 10:46:23 +1000546/* dispatch incoming messages */
547
Ben Lindstrombba81212001-06-25 05:01:22 +0000548static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549process_message(SocketEntry *e)
550{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000551 u_int msg_len;
552 u_int type;
553 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100554 if (buffer_len(&e->input) < 5)
555 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100556 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100557 msg_len = GET_32BIT(cp);
558 if (msg_len > 256 * 1024) {
559 shutdown(e->fd, SHUT_RDWR);
560 close(e->fd);
561 e->type = AUTH_UNUSED;
562 return;
563 }
564 if (buffer_len(&e->input) < msg_len + 4)
565 return;
566 buffer_consume(&e->input, 4);
567 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Ben Lindstrom3f471632001-07-04 03:53:15 +0000569 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100570 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000571 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100572 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000573 process_authentication_challenge1(e);
574 break;
575 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
576 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100577 break;
578 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000579 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100580 break;
581 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000582 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100583 break;
584 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000585 process_remove_all_identities(e, 1);
586 break;
587 /* ssh2 */
588 case SSH2_AGENTC_SIGN_REQUEST:
589 process_sign_request2(e);
590 break;
591 case SSH2_AGENTC_REQUEST_IDENTITIES:
592 process_request_identities(e, 2);
593 break;
594 case SSH2_AGENTC_ADD_IDENTITY:
595 process_add_identity(e, 2);
596 break;
597 case SSH2_AGENTC_REMOVE_IDENTITY:
598 process_remove_identity(e, 2);
599 break;
600 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
601 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100602 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000603#ifdef SMARTCARD
604 case SSH_AGENTC_ADD_SMARTCARD_KEY:
605 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100606 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
608 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100609 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000610#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100611 default:
612 /* Unknown message. Respond with failure. */
613 error("Unknown message %d", type);
614 buffer_clear(&e->input);
615 buffer_put_int(&e->output, 1);
616 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
617 break;
618 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}
620
Ben Lindstrombba81212001-06-25 05:01:22 +0000621static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000622new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000624 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100625 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
626 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Damien Miller95def091999-11-25 00:26:21 +1100628 if (fd > max_fd)
629 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Damien Miller95def091999-11-25 00:26:21 +1100631 for (i = 0; i < sockets_alloc; i++)
632 if (sockets[i].type == AUTH_UNUSED) {
633 sockets[i].fd = fd;
634 sockets[i].type = type;
635 buffer_init(&sockets[i].input);
636 buffer_init(&sockets[i].output);
637 return;
638 }
639 old_alloc = sockets_alloc;
640 sockets_alloc += 10;
641 if (sockets)
642 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
643 else
644 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
645 for (i = old_alloc; i < sockets_alloc; i++)
646 sockets[i].type = AUTH_UNUSED;
647 sockets[old_alloc].type = type;
648 sockets[old_alloc].fd = fd;
649 buffer_init(&sockets[old_alloc].input);
650 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651}
652
Ben Lindstrombba81212001-06-25 05:01:22 +0000653static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000654prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000656 u_int i, sz;
657 int n = 0;
658
659 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100660 switch (sockets[i].type) {
661 case AUTH_SOCKET:
662 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000663 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100664 break;
665 case AUTH_UNUSED:
666 break;
667 default:
668 fatal("Unknown socket type %d", sockets[i].type);
669 break;
670 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000671 }
672
673 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000674 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000675 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000676 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000677 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000678 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000679 *fdrp = xmalloc(sz);
680 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000681 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000682 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000683 if (n < *fdl)
684 debug("XXX shrink: %d < %d", n, *fdl);
685 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000686 memset(*fdrp, 0, sz);
687 memset(*fdwp, 0, sz);
688
689 for (i = 0; i < sockets_alloc; i++) {
690 switch (sockets[i].type) {
691 case AUTH_SOCKET:
692 case AUTH_CONNECTION:
693 FD_SET(sockets[i].fd, *fdrp);
694 if (buffer_len(&sockets[i].output) > 0)
695 FD_SET(sockets[i].fd, *fdwp);
696 break;
697 default:
698 break;
699 }
700 }
701 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702}
703
Ben Lindstrombba81212001-06-25 05:01:22 +0000704static void
Damien Miller95def091999-11-25 00:26:21 +1100705after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000707 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100708 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100709 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100710 char buf[1024];
711 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller95def091999-11-25 00:26:21 +1100713 for (i = 0; i < sockets_alloc; i++)
714 switch (sockets[i].type) {
715 case AUTH_UNUSED:
716 break;
717 case AUTH_SOCKET:
718 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100719 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000720 sock = accept(sockets[i].fd,
721 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100722 if (sock < 0) {
723 perror("accept from AUTH_SOCKET");
724 break;
725 }
726 new_socket(AUTH_CONNECTION, sock);
727 }
728 break;
729 case AUTH_CONNECTION:
730 if (buffer_len(&sockets[i].output) > 0 &&
731 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000732 do {
733 len = write(sockets[i].fd,
734 buffer_ptr(&sockets[i].output),
735 buffer_len(&sockets[i].output));
736 if (len == -1 && (errno == EAGAIN ||
737 errno == EINTR))
738 continue;
739 break;
740 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100741 if (len <= 0) {
742 shutdown(sockets[i].fd, SHUT_RDWR);
743 close(sockets[i].fd);
744 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000745 buffer_free(&sockets[i].input);
746 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100747 break;
748 }
749 buffer_consume(&sockets[i].output, len);
750 }
751 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000752 do {
753 len = read(sockets[i].fd, buf, sizeof(buf));
754 if (len == -1 && (errno == EAGAIN ||
755 errno == EINTR))
756 continue;
757 break;
758 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100759 if (len <= 0) {
760 shutdown(sockets[i].fd, SHUT_RDWR);
761 close(sockets[i].fd);
762 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000763 buffer_free(&sockets[i].input);
764 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100765 break;
766 }
767 buffer_append(&sockets[i].input, buf, len);
768 process_message(&sockets[i]);
769 }
770 break;
771 default:
772 fatal("Unknown type %d", sockets[i].type);
773 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774}
775
Ben Lindstrombba81212001-06-25 05:01:22 +0000776static void
Damien Miller792c5111999-10-29 09:47:09 +1000777cleanup_socket(void)
778{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000779 if (socket_name[0])
780 unlink(socket_name);
781 if (socket_dir[0])
782 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783}
784
Ben Lindstrombba81212001-06-25 05:01:22 +0000785static void
Damien Miller792c5111999-10-29 09:47:09 +1000786cleanup_exit(int i)
787{
Damien Miller95def091999-11-25 00:26:21 +1100788 cleanup_socket();
789 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000790}
791
Ben Lindstrombba81212001-06-25 05:01:22 +0000792static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000793cleanup_handler(int sig)
794{
795 cleanup_socket();
796 _exit(2);
797}
798
Ben Lindstrombba81212001-06-25 05:01:22 +0000799static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000800check_parent_exists(int sig)
801{
802 int save_errno = errno;
803
804 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
805 /* printf("Parent has died - Authentication agent exiting.\n"); */
806 cleanup_handler(sig); /* safe */
807 }
808 signal(SIGALRM, check_parent_exists);
809 alarm(10);
810 errno = save_errno;
811}
812
813static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000814usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000815{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000816 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000817 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000818 fprintf(stderr, "Options:\n");
819 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
820 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
821 fprintf(stderr, " -k Kill the current agent.\n");
822 fprintf(stderr, " -d Debug mode.\n");
Damien Miller95def091999-11-25 00:26:21 +1100823 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000824}
825
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826int
827main(int ac, char **av)
828{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000829 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100830 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000831#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000832 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000833#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000834#ifdef HAVE_CYGWIN
835 int prev_mask;
836#endif
Damien Miller95def091999-11-25 00:26:21 +1100837 pid_t pid;
838 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000839 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000840 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000841
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000842 SSLeay_add_all_algorithms();
843
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000844 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000845 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100846 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000847
Damien Millerd0cff3e2000-04-20 23:12:58 +1000848#ifdef __GNU_LIBRARY__
Ben Lindstromd94580c2001-07-04 03:48:02 +0000849 while ((ch = getopt(ac, av, "+cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000850#else /* __GNU_LIBRARY__ */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000851 while ((ch = getopt(ac, av, "cdks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000852#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100853 switch (ch) {
854 case 'c':
855 if (s_flag)
856 usage();
857 c_flag++;
858 break;
859 case 'k':
860 k_flag++;
861 break;
862 case 's':
863 if (c_flag)
864 usage();
865 s_flag++;
866 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000867 case 'd':
868 if (d_flag)
869 usage();
870 d_flag++;
871 break;
Damien Miller95def091999-11-25 00:26:21 +1100872 default:
873 usage();
874 }
Damien Miller792c5111999-10-29 09:47:09 +1000875 }
Damien Miller95def091999-11-25 00:26:21 +1100876 ac -= optind;
877 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000878
Ben Lindstromd94580c2001-07-04 03:48:02 +0000879 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100880 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000881
Ben Lindstromd94580c2001-07-04 03:48:02 +0000882 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100883 shell = getenv("SHELL");
884 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
885 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000886 }
Damien Miller95def091999-11-25 00:26:21 +1100887 if (k_flag) {
888 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
889 if (pidstr == NULL) {
890 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000891 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100892 exit(1);
893 }
894 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000895 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100896 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000897 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100898 exit(1);
899 }
900 if (kill(pid, SIGTERM) == -1) {
901 perror("kill");
902 exit(1);
903 }
904 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
905 printf(format, SSH_AUTHSOCKET_ENV_NAME);
906 printf(format, SSH_AGENTPID_ENV_NAME);
907 printf("echo Agent pid %d killed;\n", pid);
908 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000909 }
Damien Miller95def091999-11-25 00:26:21 +1100910 parent_pid = getpid();
911
912 /* Create private directory for agent socket */
913 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
914 if (mkdtemp(socket_dir) == NULL) {
915 perror("mkdtemp: private socket dir");
916 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000917 }
Damien Miller95def091999-11-25 00:26:21 +1100918 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000919 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller5428f641999-11-25 11:54:57 +1100921 /*
922 * Create socket early so it will exist before command gets run from
923 * the parent.
924 */
Damien Miller95def091999-11-25 00:26:21 +1100925 sock = socket(AF_UNIX, SOCK_STREAM, 0);
926 if (sock < 0) {
927 perror("socket");
928 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000929 }
Damien Miller95def091999-11-25 00:26:21 +1100930 memset(&sunaddr, 0, sizeof(sunaddr));
931 sunaddr.sun_family = AF_UNIX;
932 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000933#ifdef HAVE_CYGWIN
934 prev_mask = umask(0177);
935#endif
Damien Miller95def091999-11-25 00:26:21 +1100936 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
937 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000938#ifdef HAVE_CYGWIN
939 umask(prev_mask);
940#endif
Damien Miller95def091999-11-25 00:26:21 +1100941 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000943#ifdef HAVE_CYGWIN
944 umask(prev_mask);
945#endif
Damien Miller95def091999-11-25 00:26:21 +1100946 if (listen(sock, 5) < 0) {
947 perror("listen");
948 cleanup_exit(1);
949 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000950
Damien Miller5428f641999-11-25 11:54:57 +1100951 /*
952 * Fork, and have the parent execute the command, if any, or present
953 * the socket data. The child continues as the authentication agent.
954 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000955 if (d_flag) {
956 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
957 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
958 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
959 SSH_AUTHSOCKET_ENV_NAME);
960 printf("echo Agent pid %d;\n", parent_pid);
961 goto skip;
962 }
Damien Miller95def091999-11-25 00:26:21 +1100963 pid = fork();
964 if (pid == -1) {
965 perror("fork");
966 exit(1);
967 }
968 if (pid != 0) { /* Parent - execute the given command. */
969 close(sock);
970 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
971 if (ac == 0) {
972 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
973 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000974 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100975 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000976 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100977 printf("echo Agent pid %d;\n", pid);
978 exit(0);
979 }
Damien Millere4340be2000-09-16 13:29:08 +1100980 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
981 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
982 perror("setenv");
983 exit(1);
984 }
Damien Miller95def091999-11-25 00:26:21 +1100985 execvp(av[0], av);
986 perror(av[0]);
987 exit(1);
988 }
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000989
990 if (setsid() == -1) {
991 perror("setsid");
992 cleanup_exit(1);
993 }
994
995 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +1100996 close(0);
997 close(1);
998 close(2);
999
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001000#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001001 /* deny core dumps, since memory contains unencrypted private keys */
1002 rlim.rlim_cur = rlim.rlim_max = 0;
1003 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1004 perror("setrlimit rlimit_core failed");
1005 cleanup_exit(1);
1006 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001007#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001008
1009skip:
Damien Miller95def091999-11-25 00:26:21 +11001010 if (atexit(cleanup_socket) < 0) {
1011 perror("atexit");
1012 cleanup_exit(1);
1013 }
1014 new_socket(AUTH_SOCKET, sock);
1015 if (ac > 0) {
1016 signal(SIGALRM, check_parent_exists);
1017 alarm(10);
1018 }
Damien Millerad833b32000-08-23 10:46:23 +10001019 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001020 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001021 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001022 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001023 signal(SIGHUP, cleanup_handler);
1024 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001025 nalloc = 0;
1026
Damien Miller95def091999-11-25 00:26:21 +11001027 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001028 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001029 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001030 if (errno == EINTR)
1031 continue;
1032 exit(1);
1033 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001034 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001035 }
1036 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037}