blob: 13a88afd9cf98ff0a4d93dd4480120d0e92778aa [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"
Damien Millerec52d7c2002-01-22 23:52:17 +110037#include "openbsd-compat/fake-queue.h"
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000038RCSID("$OpenBSD: ssh-agent.c,v 1.87 2002/06/05 16:48:54 markus Exp $");
Damien Millerec52d7c2002-01-22 23:52:17 +110039
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include <openssl/evp.h>
41#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "ssh.h"
44#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "buffer.h"
46#include "bufaux.h"
47#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100049#include "key.h"
50#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Ben Lindstrom3f471632001-07-04 03:53:15 +000054#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000055#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000056#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000057
Ben Lindstrom65366a82001-12-06 16:32:47 +000058typedef enum {
59 AUTH_UNUSED,
60 AUTH_SOCKET,
61 AUTH_CONNECTION
62} sock_type;
63
Damien Miller95def091999-11-25 00:26:21 +110064typedef struct {
65 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000066 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110067 Buffer input;
68 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000069 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070} SocketEntry;
71
Ben Lindstrom46c16222000-12-22 01:43:59 +000072u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073SocketEntry *sockets = NULL;
74
Damien Miller1a534ae2002-01-22 23:26:13 +110075typedef struct identity {
76 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100077 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110078 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079} Identity;
80
Damien Millerad833b32000-08-23 10:46:23 +100081typedef struct {
82 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110083 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100084} Idtab;
85
86/* private key table, one per protocol version */
87Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89int max_fd = 0;
90
91/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100092pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* pathname and directory for AUTH_SOCKET */
95char socket_name[1024];
96char socket_dir[1024];
97
Damien Miller95def091999-11-25 00:26:21 +110098#ifdef HAVE___PROGNAME
99extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000100#else
101char *__progname;
102#endif
Damien Miller95def091999-11-25 00:26:21 +1100103
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static void
Damien Millerad833b32000-08-23 10:46:23 +1000105idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106{
Damien Millerad833b32000-08-23 10:46:23 +1000107 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000108 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100109 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000110 idtable[i].nentries = 0;
111 }
112}
113
114/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000116idtab_lookup(int version)
117{
118 if (version < 1 || version > 2)
119 fatal("internal error, bad protocol version %d", version);
120 return &idtable[version];
121}
122
123/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100124static Identity *
125lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000126{
Damien Miller1a534ae2002-01-22 23:26:13 +1100127 Identity *id;
128
Damien Millerad833b32000-08-23 10:46:23 +1000129 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100130 TAILQ_FOREACH(id, &tab->idlist, next) {
131 if (key_equal(key, id->key))
132 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000133 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100134 return (NULL);
135}
136
137static void
138free_identity(Identity *id)
139{
140 key_free(id->key);
141 xfree(id->comment);
142 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000143}
144
145/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Damien Millerad833b32000-08-23 10:46:23 +1000147process_request_identities(SocketEntry *e, int version)
148{
149 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100150 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100151 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller95def091999-11-25 00:26:21 +1100153 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000154 buffer_put_char(&msg, (version == 1) ?
155 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
156 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100157 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000159 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
160 buffer_put_bignum(&msg, id->key->rsa->e);
161 buffer_put_bignum(&msg, id->key->rsa->n);
162 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163 u_char *blob;
164 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000166 buffer_put_string(&msg, blob, blen);
167 xfree(blob);
168 }
169 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100170 }
171 buffer_put_int(&e->output, buffer_len(&msg));
172 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
173 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Millerad833b32000-08-23 10:46:23 +1000176/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static void
Damien Millerad833b32000-08-23 10:46:23 +1000178process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179{
Damien Miller1a534ae2002-01-22 23:26:13 +1100180 Identity *id;
181 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000182 BIGNUM *challenge;
183 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100184 Buffer msg;
185 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000186 u_char buf[32], mdbuf[16], session_id[16];
187 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100190 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100191 if ((challenge = BN_new()) == NULL)
192 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000193
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000194 buffer_get_int(&e->request); /* ignored */
195 buffer_get_bignum(&e->request, key->rsa->e);
196 buffer_get_bignum(&e->request, key->rsa->n);
197 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Millerad833b32000-08-23 10:46:23 +1000199 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000200 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000201 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000202 buffer_get(&e->request, session_id, 16);
203 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000204 if (response_type != 1)
205 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller1a534ae2002-01-22 23:26:13 +1100207 id = lookup_identity(key, 1);
208 if (id != NULL) {
209 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000210 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100211 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
212 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100213
Damien Millerad833b32000-08-23 10:46:23 +1000214 /* The response is MD5 of decrypted challenge plus session id. */
215 len = BN_num_bytes(challenge);
216 if (len <= 0 || len > 32) {
217 log("process_authentication_challenge: bad challenge length %d", len);
218 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100219 }
Damien Millerad833b32000-08-23 10:46:23 +1000220 memset(buf, 0, 32);
221 BN_bn2bin(challenge, buf + 32 - len);
222 MD5_Init(&md);
223 MD5_Update(&md, buf, 32);
224 MD5_Update(&md, session_id, 16);
225 MD5_Final(mdbuf, &md);
226
227 /* Send the response. */
228 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
229 for (i = 0; i < 16; i++)
230 buffer_put_char(&msg, mdbuf[i]);
231 goto send;
232 }
233
234failure:
235 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100236 buffer_put_char(&msg, SSH_AGENT_FAILURE);
237send:
238 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000239 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
240 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100241 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000242 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
Damien Millerad833b32000-08-23 10:46:23 +1000245/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static void
Damien Millerad833b32000-08-23 10:46:23 +1000247process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Millerad833b32000-08-23 10:46:23 +1000249 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100250 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000251 u_char *blob, *data, *signature = NULL;
252 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100253 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000254 Buffer msg;
255 int ok = -1;
256
257 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000258
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000259 blob = buffer_get_string(&e->request, &blen);
260 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100261
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000262 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100263 if (flags & SSH_AGENT_OLD_SIGNATURE)
264 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000265
Damien Miller0bc1bd82000-11-13 22:57:25 +1100266 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000267 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100268 Identity *id = lookup_identity(key, 2);
269 if (id != NULL)
270 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000271 }
272 key_free(key);
273 buffer_init(&msg);
274 if (ok == 0) {
275 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
276 buffer_put_string(&msg, signature, slen);
277 } else {
278 buffer_put_char(&msg, SSH_AGENT_FAILURE);
279 }
280 buffer_put_int(&e->output, buffer_len(&msg));
281 buffer_append(&e->output, buffer_ptr(&msg),
282 buffer_len(&msg));
283 buffer_free(&msg);
284 xfree(data);
285 xfree(blob);
286 if (signature != NULL)
287 xfree(signature);
288}
289
290/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static void
Damien Millerad833b32000-08-23 10:46:23 +1000292process_remove_identity(SocketEntry *e, int version)
293{
Damien Miller1a534ae2002-01-22 23:26:13 +1100294 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000295 u_char *blob;
296 u_int blen;
297 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000298 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100299
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000300 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000301 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100302 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000303 bits = buffer_get_int(&e->request);
304 buffer_get_bignum(&e->request, key->rsa->e);
305 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Millerad833b32000-08-23 10:46:23 +1000307 if (bits != key_size(key))
308 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000309 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000310 break;
311 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000312 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100313 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000314 xfree(blob);
315 break;
316 }
317 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100318 Identity *id = lookup_identity(key, version);
319 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100320 /*
321 * We have this key. Free the old key. Since we
322 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000323 * the array, we actually free the key there and move
324 * all the entries between the empty slot and the end
325 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100326 */
Damien Millerad833b32000-08-23 10:46:23 +1000327 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100328 if (tab->nentries < 1)
329 fatal("process_remove_identity: "
330 "internal error: tab->nentries %d",
331 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100332 TAILQ_REMOVE(&tab->idlist, id, next);
333 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000334 tab->nentries--;
335 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100336 }
Damien Millerad833b32000-08-23 10:46:23 +1000337 key_free(key);
338 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000340 buffer_put_char(&e->output,
341 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342}
343
Ben Lindstrombba81212001-06-25 05:01:22 +0000344static void
Damien Millerad833b32000-08-23 10:46:23 +1000345process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346{
Damien Millerad833b32000-08-23 10:46:23 +1000347 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100348 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100351 for (id = TAILQ_FIRST(&tab->idlist); id;
352 id = TAILQ_FIRST(&tab->idlist)) {
353 TAILQ_REMOVE(&tab->idlist, id, next);
354 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100355 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller95def091999-11-25 00:26:21 +1100357 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000358 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
360 /* Send success. */
361 buffer_put_int(&e->output, 1);
362 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
363 return;
Damien Miller95def091999-11-25 00:26:21 +1100364}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Ben Lindstrombba81212001-06-25 05:01:22 +0000366static void
Damien Millerad833b32000-08-23 10:46:23 +1000367process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100368{
Damien Millerad833b32000-08-23 10:46:23 +1000369 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100370 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000371 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000373 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100374
Damien Millerad833b32000-08-23 10:46:23 +1000375 switch (version) {
376 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 k = key_new_private(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000378 buffer_get_int(&e->request); /* ignored */
379 buffer_get_bignum(&e->request, k->rsa->n);
380 buffer_get_bignum(&e->request, k->rsa->e);
381 buffer_get_bignum(&e->request, k->rsa->d);
382 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100383
Damien Millerad833b32000-08-23 10:46:23 +1000384 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000385 buffer_get_bignum(&e->request, k->rsa->q); /* p */
386 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100387
Damien Millerad833b32000-08-23 10:46:23 +1000388 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000389 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000390 break;
391 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000392 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000393 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100394 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000395 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 case KEY_DSA:
397 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000398 buffer_get_bignum2(&e->request, k->dsa->p);
399 buffer_get_bignum2(&e->request, k->dsa->q);
400 buffer_get_bignum2(&e->request, k->dsa->g);
401 buffer_get_bignum2(&e->request, k->dsa->pub_key);
402 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100403 break;
404 case KEY_RSA:
405 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000406 buffer_get_bignum2(&e->request, k->rsa->n);
407 buffer_get_bignum2(&e->request, k->rsa->e);
408 buffer_get_bignum2(&e->request, k->rsa->d);
409 buffer_get_bignum2(&e->request, k->rsa->iqmp);
410 buffer_get_bignum2(&e->request, k->rsa->p);
411 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100412
413 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000414 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100415 break;
416 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000417 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000418 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100419 }
Damien Millerad833b32000-08-23 10:46:23 +1000420 break;
421 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000422 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000423 if (k == NULL) {
424 xfree(comment);
425 goto send;
426 }
427 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100428 if (lookup_identity(k, version) == NULL) {
429 Identity *id = xmalloc(sizeof(Identity));
430 id->key = k;
431 id->comment = comment;
432 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000433 /* Increment the number of identities. */
434 tab->nentries++;
435 } else {
436 key_free(k);
437 xfree(comment);
438 }
439send:
Damien Miller95def091999-11-25 00:26:21 +1100440 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000441 buffer_put_char(&e->output,
442 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443}
444
Ben Lindstrom3f471632001-07-04 03:53:15 +0000445
446#ifdef SMARTCARD
447static void
448process_add_smartcard_key (SocketEntry *e)
449{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000450 Identity *id;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000451 Idtab *tab;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000452 Key **keys, *k;
Ben Lindstromba72d302002-03-22 03:51:06 +0000453 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000454 int i, version, success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100455
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 sc_reader_id = buffer_get_string(&e->request, NULL);
457 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000458 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000459 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000460 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000461
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000462 if (keys == NULL || keys[0] == NULL) {
463 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000464 goto send;
465 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000466 for (i = 0; keys[i] != NULL; i++) {
467 k = keys[i];
468 version = k->type == KEY_RSA1 ? 1 : 2;
469 tab = idtab_lookup(version);
470 if (lookup_identity(k, version) == NULL) {
471 id = xmalloc(sizeof(Identity));
472 id->key = k;
473 id->comment = xstrdup("smartcard key");
474 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
475 tab->nentries++;
476 success = 1;
477 } else {
478 key_free(k);
479 }
480 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000481 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000482 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000483send:
484 buffer_put_int(&e->output, 1);
485 buffer_put_char(&e->output,
486 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
487}
488
489static void
490process_remove_smartcard_key(SocketEntry *e)
491{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000492 Identity *id;
493 Idtab *tab;
494 Key **keys, *k = NULL;
Ben Lindstromba72d302002-03-22 03:51:06 +0000495 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000496 int i, version, success = 0;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000497
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000498 sc_reader_id = buffer_get_string(&e->request, NULL);
499 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000500 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000501 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000502 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000503
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000504 if (keys == NULL || keys[0] == NULL) {
505 error("sc_get_keys failed");
506 goto send;
507 }
508 for (i = 0; keys[i] != NULL; i++) {
509 k = keys[i];
510 version = k->type == KEY_RSA1 ? 1 : 2;
511 if ((id = lookup_identity(k, version)) != NULL) {
512 tab = idtab_lookup(version);
513 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000514 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100515 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000516 success = 1;
517 }
518 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000519 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000520 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000521 xfree(keys);
522send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000523 buffer_put_int(&e->output, 1);
524 buffer_put_char(&e->output,
525 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
526}
Ben Lindstromffce1472001-08-06 21:57:31 +0000527#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000528
Damien Millerad833b32000-08-23 10:46:23 +1000529/* dispatch incoming messages */
530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532process_message(SocketEntry *e)
533{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000534 u_int msg_len;
535 u_int type;
536 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100537 if (buffer_len(&e->input) < 5)
538 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100539 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100540 msg_len = GET_32BIT(cp);
541 if (msg_len > 256 * 1024) {
542 shutdown(e->fd, SHUT_RDWR);
543 close(e->fd);
544 e->type = AUTH_UNUSED;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000545 buffer_free(&e->input);
546 buffer_free(&e->output);
547 buffer_free(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100548 return;
549 }
550 if (buffer_len(&e->input) < msg_len + 4)
551 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000552
553 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100554 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000555 buffer_clear(&e->request);
556 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
557 buffer_consume(&e->input, msg_len);
558 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Ben Lindstrom3f471632001-07-04 03:53:15 +0000560 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100561 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000562 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100563 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000564 process_authentication_challenge1(e);
565 break;
566 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
567 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100568 break;
569 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000570 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100571 break;
572 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000573 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100574 break;
575 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000576 process_remove_all_identities(e, 1);
577 break;
578 /* ssh2 */
579 case SSH2_AGENTC_SIGN_REQUEST:
580 process_sign_request2(e);
581 break;
582 case SSH2_AGENTC_REQUEST_IDENTITIES:
583 process_request_identities(e, 2);
584 break;
585 case SSH2_AGENTC_ADD_IDENTITY:
586 process_add_identity(e, 2);
587 break;
588 case SSH2_AGENTC_REMOVE_IDENTITY:
589 process_remove_identity(e, 2);
590 break;
591 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
592 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100593 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000594#ifdef SMARTCARD
595 case SSH_AGENTC_ADD_SMARTCARD_KEY:
596 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100597 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000598 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
599 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100600 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000601#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100602 default:
603 /* Unknown message. Respond with failure. */
604 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000605 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100606 buffer_put_int(&e->output, 1);
607 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
608 break;
609 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610}
611
Ben Lindstrombba81212001-06-25 05:01:22 +0000612static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000613new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000615 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100616 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
617 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Damien Miller95def091999-11-25 00:26:21 +1100619 if (fd > max_fd)
620 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Damien Miller95def091999-11-25 00:26:21 +1100622 for (i = 0; i < sockets_alloc; i++)
623 if (sockets[i].type == AUTH_UNUSED) {
624 sockets[i].fd = fd;
625 sockets[i].type = type;
626 buffer_init(&sockets[i].input);
627 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000628 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100629 return;
630 }
631 old_alloc = sockets_alloc;
632 sockets_alloc += 10;
633 if (sockets)
634 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
635 else
636 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
637 for (i = old_alloc; i < sockets_alloc; i++)
638 sockets[i].type = AUTH_UNUSED;
639 sockets[old_alloc].type = type;
640 sockets[old_alloc].fd = fd;
641 buffer_init(&sockets[old_alloc].input);
642 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000643 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644}
645
Ben Lindstrombba81212001-06-25 05:01:22 +0000646static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000647prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000649 u_int i, sz;
650 int n = 0;
651
652 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100653 switch (sockets[i].type) {
654 case AUTH_SOCKET:
655 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000656 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100657 break;
658 case AUTH_UNUSED:
659 break;
660 default:
661 fatal("Unknown socket type %d", sockets[i].type);
662 break;
663 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000664 }
665
666 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000667 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000668 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000669 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000670 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000671 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000672 *fdrp = xmalloc(sz);
673 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000674 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000675 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000676 if (n < *fdl)
677 debug("XXX shrink: %d < %d", n, *fdl);
678 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000679 memset(*fdrp, 0, sz);
680 memset(*fdwp, 0, sz);
681
682 for (i = 0; i < sockets_alloc; i++) {
683 switch (sockets[i].type) {
684 case AUTH_SOCKET:
685 case AUTH_CONNECTION:
686 FD_SET(sockets[i].fd, *fdrp);
687 if (buffer_len(&sockets[i].output) > 0)
688 FD_SET(sockets[i].fd, *fdwp);
689 break;
690 default:
691 break;
692 }
693 }
694 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695}
696
Ben Lindstrombba81212001-06-25 05:01:22 +0000697static void
Damien Miller95def091999-11-25 00:26:21 +1100698after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000700 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100701 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100702 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100703 char buf[1024];
704 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705
Damien Miller95def091999-11-25 00:26:21 +1100706 for (i = 0; i < sockets_alloc; i++)
707 switch (sockets[i].type) {
708 case AUTH_UNUSED:
709 break;
710 case AUTH_SOCKET:
711 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100712 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000713 sock = accept(sockets[i].fd,
714 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100715 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100716 error("accept from AUTH_SOCKET: %s",
717 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100718 break;
719 }
720 new_socket(AUTH_CONNECTION, sock);
721 }
722 break;
723 case AUTH_CONNECTION:
724 if (buffer_len(&sockets[i].output) > 0 &&
725 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000726 do {
727 len = write(sockets[i].fd,
728 buffer_ptr(&sockets[i].output),
729 buffer_len(&sockets[i].output));
730 if (len == -1 && (errno == EAGAIN ||
731 errno == EINTR))
732 continue;
733 break;
734 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100735 if (len <= 0) {
736 shutdown(sockets[i].fd, SHUT_RDWR);
737 close(sockets[i].fd);
738 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000739 buffer_free(&sockets[i].input);
740 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000741 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100742 break;
743 }
744 buffer_consume(&sockets[i].output, len);
745 }
746 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000747 do {
748 len = read(sockets[i].fd, buf, sizeof(buf));
749 if (len == -1 && (errno == EAGAIN ||
750 errno == EINTR))
751 continue;
752 break;
753 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100754 if (len <= 0) {
755 shutdown(sockets[i].fd, SHUT_RDWR);
756 close(sockets[i].fd);
757 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000758 buffer_free(&sockets[i].input);
759 buffer_free(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000760 buffer_free(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100761 break;
762 }
763 buffer_append(&sockets[i].input, buf, len);
764 process_message(&sockets[i]);
765 }
766 break;
767 default:
768 fatal("Unknown type %d", sockets[i].type);
769 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770}
771
Ben Lindstrombba81212001-06-25 05:01:22 +0000772static void
Damien Millerc653b892002-02-08 22:05:41 +1100773cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000774{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000775 if (socket_name[0])
776 unlink(socket_name);
777 if (socket_dir[0])
778 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779}
780
Ben Lindstrombba81212001-06-25 05:01:22 +0000781static void
Damien Miller792c5111999-10-29 09:47:09 +1000782cleanup_exit(int i)
783{
Damien Millerc653b892002-02-08 22:05:41 +1100784 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100785 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000789cleanup_handler(int sig)
790{
Damien Millerc653b892002-02-08 22:05:41 +1100791 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000792 _exit(2);
793}
794
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000796check_parent_exists(int sig)
797{
798 int save_errno = errno;
799
800 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
801 /* printf("Parent has died - Authentication agent exiting.\n"); */
802 cleanup_handler(sig); /* safe */
803 }
804 signal(SIGALRM, check_parent_exists);
805 alarm(10);
806 errno = save_errno;
807}
808
809static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000810usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000811{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000812 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000813 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000814 fprintf(stderr, "Options:\n");
815 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
816 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
817 fprintf(stderr, " -k Kill the current agent.\n");
818 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000819 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100820 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000821}
822
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823int
824main(int ac, char **av)
825{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000826 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100827 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000828#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000829 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000830#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000831#ifdef HAVE_CYGWIN
832 int prev_mask;
833#endif
Damien Miller95def091999-11-25 00:26:21 +1100834 pid_t pid;
835 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Ben Lindstromb7788f32002-06-06 21:46:08 +0000836 char *agentsocket = NULL;
Damien Miller615f9392000-05-17 22:53:33 +1000837 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000838 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000839
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000840 SSLeay_add_all_algorithms();
841
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000842 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000843 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100844 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000845
Damien Millerd0cff3e2000-04-20 23:12:58 +1000846#ifdef __GNU_LIBRARY__
Ben Lindstromb7788f32002-06-06 21:46:08 +0000847 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000848#else /* __GNU_LIBRARY__ */
Ben Lindstromb7788f32002-06-06 21:46:08 +0000849 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000850#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100851 switch (ch) {
852 case 'c':
853 if (s_flag)
854 usage();
855 c_flag++;
856 break;
857 case 'k':
858 k_flag++;
859 break;
860 case 's':
861 if (c_flag)
862 usage();
863 s_flag++;
864 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000865 case 'd':
866 if (d_flag)
867 usage();
868 d_flag++;
869 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000870 case 'a':
871 agentsocket = optarg;
872 break;
Damien Miller95def091999-11-25 00:26:21 +1100873 default:
874 usage();
875 }
Damien Miller792c5111999-10-29 09:47:09 +1000876 }
Damien Miller95def091999-11-25 00:26:21 +1100877 ac -= optind;
878 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000879
Ben Lindstromd94580c2001-07-04 03:48:02 +0000880 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100881 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000882
Ben Lindstromeecdf232002-04-02 21:03:51 +0000883 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100884 shell = getenv("SHELL");
885 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
886 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000887 }
Damien Miller95def091999-11-25 00:26:21 +1100888 if (k_flag) {
889 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
890 if (pidstr == NULL) {
891 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000892 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100893 exit(1);
894 }
895 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000896 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100897 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000898 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100899 exit(1);
900 }
901 if (kill(pid, SIGTERM) == -1) {
902 perror("kill");
903 exit(1);
904 }
905 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
906 printf(format, SSH_AUTHSOCKET_ENV_NAME);
907 printf(format, SSH_AGENTPID_ENV_NAME);
908 printf("echo Agent pid %d killed;\n", pid);
909 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000910 }
Damien Miller95def091999-11-25 00:26:21 +1100911 parent_pid = getpid();
912
Ben Lindstromb7788f32002-06-06 21:46:08 +0000913 if (agentsocket == NULL) {
914 /* Create private directory for agent socket */
915 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
916 if (mkdtemp(socket_dir) == NULL) {
917 perror("mkdtemp: private socket dir");
918 exit(1);
919 }
920 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
921 parent_pid);
922 } else {
923 /* Try to use specified agent socket */
924 socket_dir[0] = '\0';
925 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +1000926 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
Damien Miller5428f641999-11-25 11:54:57 +1100928 /*
929 * Create socket early so it will exist before command gets run from
930 * the parent.
931 */
Damien Miller95def091999-11-25 00:26:21 +1100932 sock = socket(AF_UNIX, SOCK_STREAM, 0);
933 if (sock < 0) {
934 perror("socket");
935 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000936 }
Damien Miller95def091999-11-25 00:26:21 +1100937 memset(&sunaddr, 0, sizeof(sunaddr));
938 sunaddr.sun_family = AF_UNIX;
939 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000940#ifdef HAVE_CYGWIN
941 prev_mask = umask(0177);
942#endif
Damien Miller95def091999-11-25 00:26:21 +1100943 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
944 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000945#ifdef HAVE_CYGWIN
946 umask(prev_mask);
947#endif
Damien Miller95def091999-11-25 00:26:21 +1100948 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000950#ifdef HAVE_CYGWIN
951 umask(prev_mask);
952#endif
Damien Miller95def091999-11-25 00:26:21 +1100953 if (listen(sock, 5) < 0) {
954 perror("listen");
955 cleanup_exit(1);
956 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000957
Damien Miller5428f641999-11-25 11:54:57 +1100958 /*
959 * Fork, and have the parent execute the command, if any, or present
960 * the socket data. The child continues as the authentication agent.
961 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000962 if (d_flag) {
963 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
964 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
965 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
966 SSH_AUTHSOCKET_ENV_NAME);
967 printf("echo Agent pid %d;\n", parent_pid);
968 goto skip;
969 }
Damien Miller95def091999-11-25 00:26:21 +1100970 pid = fork();
971 if (pid == -1) {
972 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +1100973 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100974 }
975 if (pid != 0) { /* Parent - execute the given command. */
976 close(sock);
977 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
978 if (ac == 0) {
979 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
980 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000981 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100982 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000983 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100984 printf("echo Agent pid %d;\n", pid);
985 exit(0);
986 }
Damien Millere4340be2000-09-16 13:29:08 +1100987 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
988 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
989 perror("setenv");
990 exit(1);
991 }
Damien Miller95def091999-11-25 00:26:21 +1100992 execvp(av[0], av);
993 perror(av[0]);
994 exit(1);
995 }
Damien Millerc653b892002-02-08 22:05:41 +1100996 /* child */
997 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000998
999 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001000 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001001 cleanup_exit(1);
1002 }
1003
1004 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +11001005 close(0);
1006 close(1);
1007 close(2);
1008
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001009#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001010 /* deny core dumps, since memory contains unencrypted private keys */
1011 rlim.rlim_cur = rlim.rlim_max = 0;
1012 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001013 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001014 cleanup_exit(1);
1015 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001016#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001017
1018skip:
Damien Millerc653b892002-02-08 22:05:41 +11001019 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001020 new_socket(AUTH_SOCKET, sock);
1021 if (ac > 0) {
1022 signal(SIGALRM, check_parent_exists);
1023 alarm(10);
1024 }
Damien Millerad833b32000-08-23 10:46:23 +10001025 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001026 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001027 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001028 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001029 signal(SIGHUP, cleanup_handler);
1030 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001031 nalloc = 0;
1032
Damien Miller95def091999-11-25 00:26:21 +11001033 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001034 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001035 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001036 if (errno == EINTR)
1037 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001038 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001039 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001040 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001041 }
1042 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043}