blob: d3321478bbf3109ef3ef2e2b81a82f838a7cf570 [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 Lindstromb7788f32002-06-06 21:46:08 +000038RCSID("$OpenBSD: ssh-agent.c,v 1.86 2002/06/05 16:08:07 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069} SocketEntry;
70
Ben Lindstrom46c16222000-12-22 01:43:59 +000071u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072SocketEntry *sockets = NULL;
73
Damien Miller1a534ae2002-01-22 23:26:13 +110074typedef struct identity {
75 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100076 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110077 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078} Identity;
79
Damien Millerad833b32000-08-23 10:46:23 +100080typedef struct {
81 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110082 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100083} Idtab;
84
85/* private key table, one per protocol version */
86Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88int max_fd = 0;
89
90/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100091pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* pathname and directory for AUTH_SOCKET */
94char socket_name[1024];
95char socket_dir[1024];
96
Damien Miller95def091999-11-25 00:26:21 +110097#ifdef HAVE___PROGNAME
98extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000099#else
100char *__progname;
101#endif
Damien Miller95def091999-11-25 00:26:21 +1100102
Ben Lindstrombba81212001-06-25 05:01:22 +0000103static void
Damien Millerad833b32000-08-23 10:46:23 +1000104idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105{
Damien Millerad833b32000-08-23 10:46:23 +1000106 int i;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000107 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100108 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000109 idtable[i].nentries = 0;
110 }
111}
112
113/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000115idtab_lookup(int version)
116{
117 if (version < 1 || version > 2)
118 fatal("internal error, bad protocol version %d", version);
119 return &idtable[version];
120}
121
122/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100123static Identity *
124lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000125{
Damien Miller1a534ae2002-01-22 23:26:13 +1100126 Identity *id;
127
Damien Millerad833b32000-08-23 10:46:23 +1000128 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100129 TAILQ_FOREACH(id, &tab->idlist, next) {
130 if (key_equal(key, id->key))
131 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000132 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100133 return (NULL);
134}
135
136static void
137free_identity(Identity *id)
138{
139 key_free(id->key);
140 xfree(id->comment);
141 xfree(id);
Damien Millerad833b32000-08-23 10:46:23 +1000142}
143
144/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000145static void
Damien Millerad833b32000-08-23 10:46:23 +1000146process_request_identities(SocketEntry *e, int version)
147{
148 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100149 Buffer msg;
Damien Miller1a534ae2002-01-22 23:26:13 +1100150 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000153 buffer_put_char(&msg, (version == 1) ?
154 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
155 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000158 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
159 buffer_put_bignum(&msg, id->key->rsa->e);
160 buffer_put_bignum(&msg, id->key->rsa->n);
161 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000162 u_char *blob;
163 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000165 buffer_put_string(&msg, blob, blen);
166 xfree(blob);
167 }
168 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100169 }
170 buffer_put_int(&e->output, buffer_len(&msg));
171 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
172 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Millerad833b32000-08-23 10:46:23 +1000175/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000176static void
Damien Millerad833b32000-08-23 10:46:23 +1000177process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178{
Damien Miller1a534ae2002-01-22 23:26:13 +1100179 Identity *id;
180 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000181 BIGNUM *challenge;
182 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100183 Buffer msg;
184 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000185 u_char buf[32], mdbuf[16], session_id[16];
186 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Miller95def091999-11-25 00:26:21 +1100188 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100189 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100190 if ((challenge = BN_new()) == NULL)
191 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000192
193 buffer_get_int(&e->input); /* ignored */
194 buffer_get_bignum(&e->input, key->rsa->e);
195 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100196 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Damien Millerad833b32000-08-23 10:46:23 +1000198 /* Only protocol 1.1 is supported */
199 if (buffer_len(&e->input) == 0)
200 goto failure;
Damien Miller4a8ed542002-01-22 23:33:31 +1100201 buffer_get(&e->input, session_id, 16);
Damien Millerad833b32000-08-23 10:46:23 +1000202 response_type = buffer_get_int(&e->input);
203 if (response_type != 1)
204 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller1a534ae2002-01-22 23:26:13 +1100206 id = lookup_identity(key, 1);
207 if (id != NULL) {
208 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000209 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100210 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
211 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100212
Damien Millerad833b32000-08-23 10:46:23 +1000213 /* The response is MD5 of decrypted challenge plus session id. */
214 len = BN_num_bytes(challenge);
215 if (len <= 0 || len > 32) {
216 log("process_authentication_challenge: bad challenge length %d", len);
217 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100218 }
Damien Millerad833b32000-08-23 10:46:23 +1000219 memset(buf, 0, 32);
220 BN_bn2bin(challenge, buf + 32 - len);
221 MD5_Init(&md);
222 MD5_Update(&md, buf, 32);
223 MD5_Update(&md, session_id, 16);
224 MD5_Final(mdbuf, &md);
225
226 /* Send the response. */
227 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
228 for (i = 0; i < 16; i++)
229 buffer_put_char(&msg, mdbuf[i]);
230 goto send;
231 }
232
233failure:
234 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100235 buffer_put_char(&msg, SSH_AGENT_FAILURE);
236send:
237 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000238 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
239 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100240 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000241 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Damien Millerad833b32000-08-23 10:46:23 +1000244/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static void
Damien Millerad833b32000-08-23 10:46:23 +1000246process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247{
Damien Millerad833b32000-08-23 10:46:23 +1000248 extern int datafellows;
Damien Miller1a534ae2002-01-22 23:26:13 +1100249 Key *key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000250 u_char *blob, *data, *signature = NULL;
251 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100252 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000253 Buffer msg;
254 int ok = -1;
255
256 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000257
Damien Millerad833b32000-08-23 10:46:23 +1000258 blob = buffer_get_string(&e->input, &blen);
259 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100260
261 flags = buffer_get_int(&e->input);
262 if (flags & SSH_AGENT_OLD_SIGNATURE)
263 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000264
Damien Miller0bc1bd82000-11-13 22:57:25 +1100265 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000266 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100267 Identity *id = lookup_identity(key, 2);
268 if (id != NULL)
269 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000270 }
271 key_free(key);
272 buffer_init(&msg);
273 if (ok == 0) {
274 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
275 buffer_put_string(&msg, signature, slen);
276 } else {
277 buffer_put_char(&msg, SSH_AGENT_FAILURE);
278 }
279 buffer_put_int(&e->output, buffer_len(&msg));
280 buffer_append(&e->output, buffer_ptr(&msg),
281 buffer_len(&msg));
282 buffer_free(&msg);
283 xfree(data);
284 xfree(blob);
285 if (signature != NULL)
286 xfree(signature);
287}
288
289/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000290static void
Damien Millerad833b32000-08-23 10:46:23 +1000291process_remove_identity(SocketEntry *e, int version)
292{
Damien Miller1a534ae2002-01-22 23:26:13 +1100293 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000294 u_char *blob;
295 u_int blen;
296 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000297 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100298
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000299 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000300 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100301 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000302 bits = buffer_get_int(&e->input);
303 buffer_get_bignum(&e->input, key->rsa->e);
304 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Millerad833b32000-08-23 10:46:23 +1000306 if (bits != key_size(key))
307 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000308 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000309 break;
310 case 2:
311 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100312 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000313 xfree(blob);
314 break;
315 }
316 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100317 Identity *id = lookup_identity(key, version);
318 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100319 /*
320 * We have this key. Free the old key. Since we
321 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000322 * the array, we actually free the key there and move
323 * all the entries between the empty slot and the end
324 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100325 */
Damien Millerad833b32000-08-23 10:46:23 +1000326 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100327 if (tab->nentries < 1)
328 fatal("process_remove_identity: "
329 "internal error: tab->nentries %d",
330 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100331 TAILQ_REMOVE(&tab->idlist, id, next);
332 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000333 tab->nentries--;
334 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerad833b32000-08-23 10:46:23 +1000336 key_free(key);
337 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000339 buffer_put_char(&e->output,
340 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static void
Damien Millerad833b32000-08-23 10:46:23 +1000344process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345{
Damien Millerad833b32000-08-23 10:46:23 +1000346 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100347 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller95def091999-11-25 00:26:21 +1100349 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100350 for (id = TAILQ_FIRST(&tab->idlist); id;
351 id = TAILQ_FIRST(&tab->idlist)) {
352 TAILQ_REMOVE(&tab->idlist, id, next);
353 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100354 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000357 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
359 /* Send success. */
360 buffer_put_int(&e->output, 1);
361 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
362 return;
Damien Miller95def091999-11-25 00:26:21 +1100363}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Millerad833b32000-08-23 10:46:23 +1000366process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100367{
Damien Millerad833b32000-08-23 10:46:23 +1000368 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000370 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100371 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000372 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100373
Damien Millerad833b32000-08-23 10:46:23 +1000374 switch (version) {
375 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000377 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 buffer_get_bignum(&e->input, k->rsa->n);
379 buffer_get_bignum(&e->input, k->rsa->e);
380 buffer_get_bignum(&e->input, k->rsa->d);
381 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100382
Damien Millerad833b32000-08-23 10:46:23 +1000383 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 buffer_get_bignum(&e->input, k->rsa->q); /* p */
385 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100386
Damien Millerad833b32000-08-23 10:46:23 +1000387 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000388 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000389 break;
390 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100391 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000392 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000394 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 case KEY_DSA:
396 k = key_new_private(type);
397 buffer_get_bignum2(&e->input, k->dsa->p);
398 buffer_get_bignum2(&e->input, k->dsa->q);
399 buffer_get_bignum2(&e->input, k->dsa->g);
400 buffer_get_bignum2(&e->input, k->dsa->pub_key);
401 buffer_get_bignum2(&e->input, k->dsa->priv_key);
402 break;
403 case KEY_RSA:
404 k = key_new_private(type);
405 buffer_get_bignum2(&e->input, k->rsa->n);
406 buffer_get_bignum2(&e->input, k->rsa->e);
407 buffer_get_bignum2(&e->input, k->rsa->d);
408 buffer_get_bignum2(&e->input, k->rsa->iqmp);
409 buffer_get_bignum2(&e->input, k->rsa->p);
410 buffer_get_bignum2(&e->input, k->rsa->q);
411
412 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000413 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100414 break;
415 default:
Damien Millerad833b32000-08-23 10:46:23 +1000416 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000417 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100418 }
Damien Millerad833b32000-08-23 10:46:23 +1000419 break;
420 }
Damien Millerad833b32000-08-23 10:46:23 +1000421 comment = buffer_get_string(&e->input, NULL);
422 if (k == NULL) {
423 xfree(comment);
424 goto send;
425 }
426 success = 1;
Damien Miller1a534ae2002-01-22 23:26:13 +1100427 if (lookup_identity(k, version) == NULL) {
428 Identity *id = xmalloc(sizeof(Identity));
429 id->key = k;
430 id->comment = comment;
431 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000432 /* Increment the number of identities. */
433 tab->nentries++;
434 } else {
435 key_free(k);
436 xfree(comment);
437 }
438send:
Damien Miller95def091999-11-25 00:26:21 +1100439 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000440 buffer_put_char(&e->output,
441 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442}
443
Ben Lindstrom3f471632001-07-04 03:53:15 +0000444
445#ifdef SMARTCARD
446static void
447process_add_smartcard_key (SocketEntry *e)
448{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000449 Identity *id;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000450 Idtab *tab;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000451 Key **keys, *k;
Ben Lindstromba72d302002-03-22 03:51:06 +0000452 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000453 int i, version, success = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100454
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000455 sc_reader_id = buffer_get_string(&e->input, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000456 pin = buffer_get_string(&e->input, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000457 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000458 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000459 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000460
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000461 if (keys == NULL || keys[0] == NULL) {
462 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000463 goto send;
464 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000465 for (i = 0; keys[i] != NULL; i++) {
466 k = keys[i];
467 version = k->type == KEY_RSA1 ? 1 : 2;
468 tab = idtab_lookup(version);
469 if (lookup_identity(k, version) == NULL) {
470 id = xmalloc(sizeof(Identity));
471 id->key = k;
472 id->comment = xstrdup("smartcard key");
473 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
474 tab->nentries++;
475 success = 1;
476 } else {
477 key_free(k);
478 }
479 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000480 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000481 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000482send:
483 buffer_put_int(&e->output, 1);
484 buffer_put_char(&e->output,
485 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
486}
487
488static void
489process_remove_smartcard_key(SocketEntry *e)
490{
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000491 Identity *id;
492 Idtab *tab;
493 Key **keys, *k = NULL;
Ben Lindstromba72d302002-03-22 03:51:06 +0000494 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000495 int i, version, success = 0;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000496
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000497 sc_reader_id = buffer_get_string(&e->input, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000498 pin = buffer_get_string(&e->input, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000499 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000500 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000501 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000502
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000503 if (keys == NULL || keys[0] == NULL) {
504 error("sc_get_keys failed");
505 goto send;
506 }
507 for (i = 0; keys[i] != NULL; i++) {
508 k = keys[i];
509 version = k->type == KEY_RSA1 ? 1 : 2;
510 if ((id = lookup_identity(k, version)) != NULL) {
511 tab = idtab_lookup(version);
512 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000513 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100514 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000515 success = 1;
516 }
517 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000518 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000519 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000520 xfree(keys);
521send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000522 buffer_put_int(&e->output, 1);
523 buffer_put_char(&e->output,
524 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
525}
Ben Lindstromffce1472001-08-06 21:57:31 +0000526#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000527
Damien Millerad833b32000-08-23 10:46:23 +1000528/* dispatch incoming messages */
529
Ben Lindstrombba81212001-06-25 05:01:22 +0000530static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531process_message(SocketEntry *e)
532{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000533 u_int msg_len;
534 u_int type;
535 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100536 if (buffer_len(&e->input) < 5)
537 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100538 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100539 msg_len = GET_32BIT(cp);
540 if (msg_len > 256 * 1024) {
541 shutdown(e->fd, SHUT_RDWR);
542 close(e->fd);
543 e->type = AUTH_UNUSED;
544 return;
545 }
546 if (buffer_len(&e->input) < msg_len + 4)
547 return;
548 buffer_consume(&e->input, 4);
549 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Ben Lindstrom3f471632001-07-04 03:53:15 +0000551 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100552 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000553 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100554 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000555 process_authentication_challenge1(e);
556 break;
557 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
558 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100559 break;
560 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000561 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100562 break;
563 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000564 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100565 break;
566 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000567 process_remove_all_identities(e, 1);
568 break;
569 /* ssh2 */
570 case SSH2_AGENTC_SIGN_REQUEST:
571 process_sign_request2(e);
572 break;
573 case SSH2_AGENTC_REQUEST_IDENTITIES:
574 process_request_identities(e, 2);
575 break;
576 case SSH2_AGENTC_ADD_IDENTITY:
577 process_add_identity(e, 2);
578 break;
579 case SSH2_AGENTC_REMOVE_IDENTITY:
580 process_remove_identity(e, 2);
581 break;
582 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
583 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100584 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000585#ifdef SMARTCARD
586 case SSH_AGENTC_ADD_SMARTCARD_KEY:
587 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000589 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
590 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000592#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100593 default:
594 /* Unknown message. Respond with failure. */
595 error("Unknown message %d", type);
596 buffer_clear(&e->input);
597 buffer_put_int(&e->output, 1);
598 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
599 break;
600 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
602
Ben Lindstrombba81212001-06-25 05:01:22 +0000603static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000604new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000606 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100607 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
608 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Damien Miller95def091999-11-25 00:26:21 +1100610 if (fd > max_fd)
611 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Damien Miller95def091999-11-25 00:26:21 +1100613 for (i = 0; i < sockets_alloc; i++)
614 if (sockets[i].type == AUTH_UNUSED) {
615 sockets[i].fd = fd;
616 sockets[i].type = type;
617 buffer_init(&sockets[i].input);
618 buffer_init(&sockets[i].output);
619 return;
620 }
621 old_alloc = sockets_alloc;
622 sockets_alloc += 10;
623 if (sockets)
624 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
625 else
626 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
627 for (i = old_alloc; i < sockets_alloc; i++)
628 sockets[i].type = AUTH_UNUSED;
629 sockets[old_alloc].type = type;
630 sockets[old_alloc].fd = fd;
631 buffer_init(&sockets[old_alloc].input);
632 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633}
634
Ben Lindstrombba81212001-06-25 05:01:22 +0000635static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000636prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000638 u_int i, sz;
639 int n = 0;
640
641 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100642 switch (sockets[i].type) {
643 case AUTH_SOCKET:
644 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000645 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100646 break;
647 case AUTH_UNUSED:
648 break;
649 default:
650 fatal("Unknown socket type %d", sockets[i].type);
651 break;
652 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000653 }
654
655 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000656 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000657 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000658 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000659 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000660 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000661 *fdrp = xmalloc(sz);
662 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000663 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000664 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000665 if (n < *fdl)
666 debug("XXX shrink: %d < %d", n, *fdl);
667 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000668 memset(*fdrp, 0, sz);
669 memset(*fdwp, 0, sz);
670
671 for (i = 0; i < sockets_alloc; i++) {
672 switch (sockets[i].type) {
673 case AUTH_SOCKET:
674 case AUTH_CONNECTION:
675 FD_SET(sockets[i].fd, *fdrp);
676 if (buffer_len(&sockets[i].output) > 0)
677 FD_SET(sockets[i].fd, *fdwp);
678 break;
679 default:
680 break;
681 }
682 }
683 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684}
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Damien Miller95def091999-11-25 00:26:21 +1100687after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000689 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100690 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100691 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100692 char buf[1024];
693 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller95def091999-11-25 00:26:21 +1100695 for (i = 0; i < sockets_alloc; i++)
696 switch (sockets[i].type) {
697 case AUTH_UNUSED:
698 break;
699 case AUTH_SOCKET:
700 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100701 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000702 sock = accept(sockets[i].fd,
703 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100704 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100705 error("accept from AUTH_SOCKET: %s",
706 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100707 break;
708 }
709 new_socket(AUTH_CONNECTION, sock);
710 }
711 break;
712 case AUTH_CONNECTION:
713 if (buffer_len(&sockets[i].output) > 0 &&
714 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000715 do {
716 len = write(sockets[i].fd,
717 buffer_ptr(&sockets[i].output),
718 buffer_len(&sockets[i].output));
719 if (len == -1 && (errno == EAGAIN ||
720 errno == EINTR))
721 continue;
722 break;
723 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100724 if (len <= 0) {
725 shutdown(sockets[i].fd, SHUT_RDWR);
726 close(sockets[i].fd);
727 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000728 buffer_free(&sockets[i].input);
729 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100730 break;
731 }
732 buffer_consume(&sockets[i].output, len);
733 }
734 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000735 do {
736 len = read(sockets[i].fd, buf, sizeof(buf));
737 if (len == -1 && (errno == EAGAIN ||
738 errno == EINTR))
739 continue;
740 break;
741 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100742 if (len <= 0) {
743 shutdown(sockets[i].fd, SHUT_RDWR);
744 close(sockets[i].fd);
745 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000746 buffer_free(&sockets[i].input);
747 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100748 break;
749 }
750 buffer_append(&sockets[i].input, buf, len);
751 process_message(&sockets[i]);
752 }
753 break;
754 default:
755 fatal("Unknown type %d", sockets[i].type);
756 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757}
758
Ben Lindstrombba81212001-06-25 05:01:22 +0000759static void
Damien Millerc653b892002-02-08 22:05:41 +1100760cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000761{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000762 if (socket_name[0])
763 unlink(socket_name);
764 if (socket_dir[0])
765 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766}
767
Ben Lindstrombba81212001-06-25 05:01:22 +0000768static void
Damien Miller792c5111999-10-29 09:47:09 +1000769cleanup_exit(int i)
770{
Damien Millerc653b892002-02-08 22:05:41 +1100771 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100772 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000773}
774
Ben Lindstrombba81212001-06-25 05:01:22 +0000775static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000776cleanup_handler(int sig)
777{
Damien Millerc653b892002-02-08 22:05:41 +1100778 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000779 _exit(2);
780}
781
Ben Lindstrombba81212001-06-25 05:01:22 +0000782static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000783check_parent_exists(int sig)
784{
785 int save_errno = errno;
786
787 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
788 /* printf("Parent has died - Authentication agent exiting.\n"); */
789 cleanup_handler(sig); /* safe */
790 }
791 signal(SIGALRM, check_parent_exists);
792 alarm(10);
793 errno = save_errno;
794}
795
796static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000797usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000798{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000799 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000800 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000801 fprintf(stderr, "Options:\n");
802 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
803 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
804 fprintf(stderr, " -k Kill the current agent.\n");
805 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000806 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller95def091999-11-25 00:26:21 +1100807 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000808}
809
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810int
811main(int ac, char **av)
812{
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000813 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
Damien Miller95def091999-11-25 00:26:21 +1100814 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000815#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000816 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000817#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000818#ifdef HAVE_CYGWIN
819 int prev_mask;
820#endif
Damien Miller95def091999-11-25 00:26:21 +1100821 pid_t pid;
822 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Ben Lindstromb7788f32002-06-06 21:46:08 +0000823 char *agentsocket = NULL;
Damien Miller615f9392000-05-17 22:53:33 +1000824 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000825 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000826
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000827 SSLeay_add_all_algorithms();
828
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000829 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000830 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100831 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000832
Damien Millerd0cff3e2000-04-20 23:12:58 +1000833#ifdef __GNU_LIBRARY__
Ben Lindstromb7788f32002-06-06 21:46:08 +0000834 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000835#else /* __GNU_LIBRARY__ */
Ben Lindstromb7788f32002-06-06 21:46:08 +0000836 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000837#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100838 switch (ch) {
839 case 'c':
840 if (s_flag)
841 usage();
842 c_flag++;
843 break;
844 case 'k':
845 k_flag++;
846 break;
847 case 's':
848 if (c_flag)
849 usage();
850 s_flag++;
851 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +0000852 case 'd':
853 if (d_flag)
854 usage();
855 d_flag++;
856 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +0000857 case 'a':
858 agentsocket = optarg;
859 break;
Damien Miller95def091999-11-25 00:26:21 +1100860 default:
861 usage();
862 }
Damien Miller792c5111999-10-29 09:47:09 +1000863 }
Damien Miller95def091999-11-25 00:26:21 +1100864 ac -= optind;
865 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000866
Ben Lindstromd94580c2001-07-04 03:48:02 +0000867 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +1100868 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000869
Ben Lindstromeecdf232002-04-02 21:03:51 +0000870 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100871 shell = getenv("SHELL");
872 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
873 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000874 }
Damien Miller95def091999-11-25 00:26:21 +1100875 if (k_flag) {
876 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
877 if (pidstr == NULL) {
878 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000879 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100880 exit(1);
881 }
882 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000883 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100884 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000885 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100886 exit(1);
887 }
888 if (kill(pid, SIGTERM) == -1) {
889 perror("kill");
890 exit(1);
891 }
892 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
893 printf(format, SSH_AUTHSOCKET_ENV_NAME);
894 printf(format, SSH_AGENTPID_ENV_NAME);
895 printf("echo Agent pid %d killed;\n", pid);
896 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000897 }
Damien Miller95def091999-11-25 00:26:21 +1100898 parent_pid = getpid();
899
Ben Lindstromb7788f32002-06-06 21:46:08 +0000900 if (agentsocket == NULL) {
901 /* Create private directory for agent socket */
902 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
903 if (mkdtemp(socket_dir) == NULL) {
904 perror("mkdtemp: private socket dir");
905 exit(1);
906 }
907 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
908 parent_pid);
909 } else {
910 /* Try to use specified agent socket */
911 socket_dir[0] = '\0';
912 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +1000913 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller5428f641999-11-25 11:54:57 +1100915 /*
916 * Create socket early so it will exist before command gets run from
917 * the parent.
918 */
Damien Miller95def091999-11-25 00:26:21 +1100919 sock = socket(AF_UNIX, SOCK_STREAM, 0);
920 if (sock < 0) {
921 perror("socket");
922 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000923 }
Damien Miller95def091999-11-25 00:26:21 +1100924 memset(&sunaddr, 0, sizeof(sunaddr));
925 sunaddr.sun_family = AF_UNIX;
926 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000927#ifdef HAVE_CYGWIN
928 prev_mask = umask(0177);
929#endif
Damien Miller95def091999-11-25 00:26:21 +1100930 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
931 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000932#ifdef HAVE_CYGWIN
933 umask(prev_mask);
934#endif
Damien Miller95def091999-11-25 00:26:21 +1100935 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000937#ifdef HAVE_CYGWIN
938 umask(prev_mask);
939#endif
Damien Miller95def091999-11-25 00:26:21 +1100940 if (listen(sock, 5) < 0) {
941 perror("listen");
942 cleanup_exit(1);
943 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000944
Damien Miller5428f641999-11-25 11:54:57 +1100945 /*
946 * Fork, and have the parent execute the command, if any, or present
947 * the socket data. The child continues as the authentication agent.
948 */
Ben Lindstromd94580c2001-07-04 03:48:02 +0000949 if (d_flag) {
950 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
951 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
952 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
953 SSH_AUTHSOCKET_ENV_NAME);
954 printf("echo Agent pid %d;\n", parent_pid);
955 goto skip;
956 }
Damien Miller95def091999-11-25 00:26:21 +1100957 pid = fork();
958 if (pid == -1) {
959 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +1100960 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100961 }
962 if (pid != 0) { /* Parent - execute the given command. */
963 close(sock);
964 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
965 if (ac == 0) {
966 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
967 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000968 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100969 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000970 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100971 printf("echo Agent pid %d;\n", pid);
972 exit(0);
973 }
Damien Millere4340be2000-09-16 13:29:08 +1100974 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
975 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
976 perror("setenv");
977 exit(1);
978 }
Damien Miller95def091999-11-25 00:26:21 +1100979 execvp(av[0], av);
980 perror(av[0]);
981 exit(1);
982 }
Damien Millerc653b892002-02-08 22:05:41 +1100983 /* child */
984 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000985
986 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +1100987 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +0000988 cleanup_exit(1);
989 }
990
991 (void)chdir("/");
Damien Miller95def091999-11-25 00:26:21 +1100992 close(0);
993 close(1);
994 close(2);
995
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000996#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000997 /* deny core dumps, since memory contains unencrypted private keys */
998 rlim.rlim_cur = rlim.rlim_max = 0;
999 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001000 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001001 cleanup_exit(1);
1002 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001003#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001004
1005skip:
Damien Millerc653b892002-02-08 22:05:41 +11001006 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001007 new_socket(AUTH_SOCKET, sock);
1008 if (ac > 0) {
1009 signal(SIGALRM, check_parent_exists);
1010 alarm(10);
1011 }
Damien Millerad833b32000-08-23 10:46:23 +10001012 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001013 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001014 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001015 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001016 signal(SIGHUP, cleanup_handler);
1017 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001018 nalloc = 0;
1019
Damien Miller95def091999-11-25 00:26:21 +11001020 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001021 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001022 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001023 if (errno == EINTR)
1024 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001025 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001026 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001027 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001028 }
1029 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030}