Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk Exp $ */ |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 2 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 5 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 6 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * The authentication agent program. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
| 14 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 39 | RCSID("$OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk Exp $"); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 40 | |
Damien Miller | ec52d7c | 2002-01-22 23:52:17 +1100 | [diff] [blame^] | 41 | #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) |
| 42 | #include <sys/queue.h> |
| 43 | #else |
| 44 | #include "openbsd-compat/fake-queue.h" |
| 45 | #endif |
| 46 | |
| 47 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include <openssl/evp.h> |
| 49 | #include <openssl/md5.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | |
| 51 | #include "ssh.h" |
| 52 | #include "rsa.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | #include "buffer.h" |
| 54 | #include "bufaux.h" |
| 55 | #include "xmalloc.h" |
| 56 | #include "packet.h" |
| 57 | #include "getput.h" |
| 58 | #include "mpaux.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 59 | #include "key.h" |
| 60 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 61 | #include "cipher.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 62 | #include "kex.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 63 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 64 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 66 | #ifdef SMARTCARD |
| 67 | #include <openssl/engine.h> |
| 68 | #include "scard.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 69 | #endif |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 70 | |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 71 | typedef enum { |
| 72 | AUTH_UNUSED, |
| 73 | AUTH_SOCKET, |
| 74 | AUTH_CONNECTION |
| 75 | } sock_type; |
| 76 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 77 | typedef struct { |
| 78 | int fd; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 79 | sock_type type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | Buffer input; |
| 81 | Buffer output; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | } SocketEntry; |
| 83 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 84 | u_int sockets_alloc = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | SocketEntry *sockets = NULL; |
| 86 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 87 | typedef struct identity { |
| 88 | TAILQ_ENTRY(identity) next; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 89 | Key *key; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | char *comment; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | } Identity; |
| 92 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 93 | typedef struct { |
| 94 | int nentries; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 95 | TAILQ_HEAD(idqueue, identity) idlist; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 96 | } Idtab; |
| 97 | |
| 98 | /* private key table, one per protocol version */ |
| 99 | Idtab idtable[3]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
| 101 | int max_fd = 0; |
| 102 | |
| 103 | /* pid of shell == parent of agent */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 104 | pid_t parent_pid = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | |
| 106 | /* pathname and directory for AUTH_SOCKET */ |
| 107 | char socket_name[1024]; |
| 108 | char socket_dir[1024]; |
| 109 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | #ifdef HAVE___PROGNAME |
| 111 | extern char *__progname; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 112 | #else |
| 113 | char *__progname; |
| 114 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 116 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 117 | idtab_init(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 119 | int i; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 120 | for (i = 0; i <=2; i++) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 121 | TAILQ_INIT(&idtable[i].idlist); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 122 | idtable[i].nentries = 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* return private key table for requested protocol version */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 127 | static Idtab * |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 128 | idtab_lookup(int version) |
| 129 | { |
| 130 | if (version < 1 || version > 2) |
| 131 | fatal("internal error, bad protocol version %d", version); |
| 132 | return &idtable[version]; |
| 133 | } |
| 134 | |
| 135 | /* return matching private key for given public key */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 136 | static Identity * |
| 137 | lookup_identity(Key *key, int version) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 138 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 139 | Identity *id; |
| 140 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 141 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 142 | TAILQ_FOREACH(id, &tab->idlist, next) { |
| 143 | if (key_equal(key, id->key)) |
| 144 | return (id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 145 | } |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 146 | return (NULL); |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | free_identity(Identity *id) |
| 151 | { |
| 152 | key_free(id->key); |
| 153 | xfree(id->comment); |
| 154 | xfree(id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* send list of supported public keys to 'client' */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 158 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 159 | process_request_identities(SocketEntry *e, int version) |
| 160 | { |
| 161 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | Buffer msg; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 163 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | buffer_init(&msg); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 166 | buffer_put_char(&msg, (version == 1) ? |
| 167 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 168 | buffer_put_int(&msg, tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 169 | TAILQ_FOREACH(id, &tab->idlist, next) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 170 | if (id->key->type == KEY_RSA1) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 171 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
| 172 | buffer_put_bignum(&msg, id->key->rsa->e); |
| 173 | buffer_put_bignum(&msg, id->key->rsa->n); |
| 174 | } else { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 175 | u_char *blob; |
| 176 | u_int blen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 177 | key_to_blob(id->key, &blob, &blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 178 | buffer_put_string(&msg, blob, blen); |
| 179 | xfree(blob); |
| 180 | } |
| 181 | buffer_put_cstring(&msg, id->comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | } |
| 183 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 184 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 185 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | } |
| 187 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 188 | /* ssh1 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 189 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 190 | process_authentication_challenge1(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 192 | Identity *id; |
| 193 | Key *key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 194 | BIGNUM *challenge; |
| 195 | int i, len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | Buffer msg; |
| 197 | MD5_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 198 | u_char buf[32], mdbuf[16], session_id[16]; |
| 199 | u_int response_type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 200 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | buffer_init(&msg); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 202 | key = key_new(KEY_RSA1); |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 203 | if ((challenge = BN_new()) == NULL) |
| 204 | fatal("process_authentication_challenge1: BN_new failed"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 205 | |
| 206 | buffer_get_int(&e->input); /* ignored */ |
| 207 | buffer_get_bignum(&e->input, key->rsa->e); |
| 208 | buffer_get_bignum(&e->input, key->rsa->n); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | buffer_get_bignum(&e->input, challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 211 | /* Only protocol 1.1 is supported */ |
| 212 | if (buffer_len(&e->input) == 0) |
| 213 | goto failure; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 214 | buffer_get(&e->input, session_id, 16); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 215 | response_type = buffer_get_int(&e->input); |
| 216 | if (response_type != 1) |
| 217 | goto failure; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 219 | id = lookup_identity(key, 1); |
| 220 | if (id != NULL) { |
| 221 | Key *private = id->key; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 222 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 223 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
| 224 | goto failure; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 225 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 226 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 227 | len = BN_num_bytes(challenge); |
| 228 | if (len <= 0 || len > 32) { |
| 229 | log("process_authentication_challenge: bad challenge length %d", len); |
| 230 | goto failure; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 232 | memset(buf, 0, 32); |
| 233 | BN_bn2bin(challenge, buf + 32 - len); |
| 234 | MD5_Init(&md); |
| 235 | MD5_Update(&md, buf, 32); |
| 236 | MD5_Update(&md, session_id, 16); |
| 237 | MD5_Final(mdbuf, &md); |
| 238 | |
| 239 | /* Send the response. */ |
| 240 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 241 | for (i = 0; i < 16; i++) |
| 242 | buffer_put_char(&msg, mdbuf[i]); |
| 243 | goto send; |
| 244 | } |
| 245 | |
| 246 | failure: |
| 247 | /* Unknown identity or protocol error. Send failure. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 248 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 249 | send: |
| 250 | buffer_put_int(&e->output, buffer_len(&msg)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 251 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 252 | key_free(key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 253 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 254 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 255 | } |
| 256 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 257 | /* ssh2 only */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 258 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 259 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 260 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 261 | extern int datafellows; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 262 | Key *key; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 263 | u_char *blob, *data, *signature = NULL; |
| 264 | u_int blen, dlen, slen = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 265 | int flags; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 266 | Buffer msg; |
| 267 | int ok = -1; |
| 268 | |
| 269 | datafellows = 0; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 270 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 271 | blob = buffer_get_string(&e->input, &blen); |
| 272 | data = buffer_get_string(&e->input, &dlen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 273 | |
| 274 | flags = buffer_get_int(&e->input); |
| 275 | if (flags & SSH_AGENT_OLD_SIGNATURE) |
| 276 | datafellows = SSH_BUG_SIGBLOB; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 277 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 278 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 279 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 280 | Identity *id = lookup_identity(key, 2); |
| 281 | if (id != NULL) |
| 282 | ok = key_sign(id->key, &signature, &slen, data, dlen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 283 | } |
| 284 | key_free(key); |
| 285 | buffer_init(&msg); |
| 286 | if (ok == 0) { |
| 287 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); |
| 288 | buffer_put_string(&msg, signature, slen); |
| 289 | } else { |
| 290 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 291 | } |
| 292 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 293 | buffer_append(&e->output, buffer_ptr(&msg), |
| 294 | buffer_len(&msg)); |
| 295 | buffer_free(&msg); |
| 296 | xfree(data); |
| 297 | xfree(blob); |
| 298 | if (signature != NULL) |
| 299 | xfree(signature); |
| 300 | } |
| 301 | |
| 302 | /* shared */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 303 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 304 | process_remove_identity(SocketEntry *e, int version) |
| 305 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 306 | Key *key = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 307 | u_char *blob; |
| 308 | u_int blen; |
| 309 | u_int bits; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 310 | int success = 0; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 311 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 312 | switch (version) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 313 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 314 | key = key_new(KEY_RSA1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 315 | bits = buffer_get_int(&e->input); |
| 316 | buffer_get_bignum(&e->input, key->rsa->e); |
| 317 | buffer_get_bignum(&e->input, key->rsa->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 318 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 319 | if (bits != key_size(key)) |
| 320 | log("Warning: identity keysize mismatch: actual %d, announced %d", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 321 | key_size(key), bits); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 322 | break; |
| 323 | case 2: |
| 324 | blob = buffer_get_string(&e->input, &blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 325 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 326 | xfree(blob); |
| 327 | break; |
| 328 | } |
| 329 | if (key != NULL) { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 330 | Identity *id = lookup_identity(key, version); |
| 331 | if (id != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 332 | /* |
| 333 | * We have this key. Free the old key. Since we |
| 334 | * don\'t want to leave empty slots in the middle of |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 335 | * the array, we actually free the key there and move |
| 336 | * all the entries between the empty slot and the end |
| 337 | * of the array. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 338 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 339 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 340 | if (tab->nentries < 1) |
| 341 | fatal("process_remove_identity: " |
| 342 | "internal error: tab->nentries %d", |
| 343 | tab->nentries); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 344 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 345 | free_identity(id); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 346 | tab->nentries--; |
| 347 | success = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 349 | key_free(key); |
| 350 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 351 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 352 | buffer_put_char(&e->output, |
| 353 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 354 | } |
| 355 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 356 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 357 | process_remove_all_identities(SocketEntry *e, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 358 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 359 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 360 | Identity *id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 361 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 362 | /* Loop over all identities and clear the keys. */ |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 363 | for (id = TAILQ_FIRST(&tab->idlist); id; |
| 364 | id = TAILQ_FIRST(&tab->idlist)) { |
| 365 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 366 | free_identity(id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 367 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 368 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 369 | /* Mark that there are no identities. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 370 | tab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 371 | |
| 372 | /* Send success. */ |
| 373 | buffer_put_int(&e->output, 1); |
| 374 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 375 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 376 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 377 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 378 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 379 | process_add_identity(SocketEntry *e, int version) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 380 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 381 | Key *k = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 382 | char *type_name; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 383 | char *comment; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 384 | int type, success = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 385 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 386 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 387 | switch (version) { |
| 388 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 389 | k = key_new_private(KEY_RSA1); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 390 | buffer_get_int(&e->input); /* ignored */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 391 | buffer_get_bignum(&e->input, k->rsa->n); |
| 392 | buffer_get_bignum(&e->input, k->rsa->e); |
| 393 | buffer_get_bignum(&e->input, k->rsa->d); |
| 394 | buffer_get_bignum(&e->input, k->rsa->iqmp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 396 | /* SSH and SSL have p and q swapped */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 397 | buffer_get_bignum(&e->input, k->rsa->q); /* p */ |
| 398 | buffer_get_bignum(&e->input, k->rsa->p); /* q */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 400 | /* Generate additional parameters */ |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 401 | rsa_generate_additional_parameters(k->rsa); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 402 | break; |
| 403 | case 2: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 404 | type_name = buffer_get_string(&e->input, NULL); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 405 | type = key_type_from_name(type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 406 | xfree(type_name); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 407 | switch (type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 408 | case KEY_DSA: |
| 409 | k = key_new_private(type); |
| 410 | buffer_get_bignum2(&e->input, k->dsa->p); |
| 411 | buffer_get_bignum2(&e->input, k->dsa->q); |
| 412 | buffer_get_bignum2(&e->input, k->dsa->g); |
| 413 | buffer_get_bignum2(&e->input, k->dsa->pub_key); |
| 414 | buffer_get_bignum2(&e->input, k->dsa->priv_key); |
| 415 | break; |
| 416 | case KEY_RSA: |
| 417 | k = key_new_private(type); |
| 418 | buffer_get_bignum2(&e->input, k->rsa->n); |
| 419 | buffer_get_bignum2(&e->input, k->rsa->e); |
| 420 | buffer_get_bignum2(&e->input, k->rsa->d); |
| 421 | buffer_get_bignum2(&e->input, k->rsa->iqmp); |
| 422 | buffer_get_bignum2(&e->input, k->rsa->p); |
| 423 | buffer_get_bignum2(&e->input, k->rsa->q); |
| 424 | |
| 425 | /* Generate additional parameters */ |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 426 | rsa_generate_additional_parameters(k->rsa); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 427 | break; |
| 428 | default: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 429 | buffer_clear(&e->input); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 430 | goto send; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 432 | break; |
| 433 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 434 | comment = buffer_get_string(&e->input, NULL); |
| 435 | if (k == NULL) { |
| 436 | xfree(comment); |
| 437 | goto send; |
| 438 | } |
| 439 | success = 1; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 440 | if (lookup_identity(k, version) == NULL) { |
| 441 | Identity *id = xmalloc(sizeof(Identity)); |
| 442 | id->key = k; |
| 443 | id->comment = comment; |
| 444 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 445 | /* Increment the number of identities. */ |
| 446 | tab->nentries++; |
| 447 | } else { |
| 448 | key_free(k); |
| 449 | xfree(comment); |
| 450 | } |
| 451 | send: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 452 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 453 | buffer_put_char(&e->output, |
| 454 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 455 | } |
| 456 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 457 | |
| 458 | #ifdef SMARTCARD |
| 459 | static void |
| 460 | process_add_smartcard_key (SocketEntry *e) |
| 461 | { |
| 462 | Idtab *tab; |
| 463 | Key *n = NULL, *k = NULL; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 464 | char *sc_reader_id = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 465 | int success = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 466 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 467 | sc_reader_id = buffer_get_string(&e->input, NULL); |
| 468 | k = sc_get_key(sc_reader_id); |
| 469 | xfree(sc_reader_id); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 470 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 471 | if (k == NULL) { |
| 472 | error("sc_get_pubkey failed"); |
| 473 | goto send; |
| 474 | } |
| 475 | success = 1; |
| 476 | |
| 477 | tab = idtab_lookup(1); |
Damien Miller | f3512d9 | 2001-07-14 12:14:27 +1000 | [diff] [blame] | 478 | k->type = KEY_RSA1; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 479 | if (lookup_identity(k, 1) == NULL) { |
| 480 | Identity *id = xmalloc(sizeof(Identity)); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 481 | n = key_new(KEY_RSA1); |
| 482 | BN_copy(n->rsa->n, k->rsa->n); |
| 483 | BN_copy(n->rsa->e, k->rsa->e); |
| 484 | RSA_set_method(n->rsa, sc_get_engine()); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 485 | id->key = n; |
| 486 | id->comment = xstrdup("rsa1 smartcard"); |
| 487 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 488 | tab->nentries++; |
| 489 | } |
Damien Miller | f3512d9 | 2001-07-14 12:14:27 +1000 | [diff] [blame] | 490 | k->type = KEY_RSA; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 491 | tab = idtab_lookup(2); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 492 | if (lookup_identity(k, 2) == NULL) { |
| 493 | Identity *id = xmalloc(sizeof(Identity)); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 494 | n = key_new(KEY_RSA); |
| 495 | BN_copy(n->rsa->n, k->rsa->n); |
| 496 | BN_copy(n->rsa->e, k->rsa->e); |
| 497 | RSA_set_method(n->rsa, sc_get_engine()); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 498 | id->key = n; |
| 499 | id->comment = xstrdup("rsa smartcard"); |
| 500 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 501 | tab->nentries++; |
| 502 | } |
| 503 | key_free(k); |
| 504 | send: |
| 505 | buffer_put_int(&e->output, 1); |
| 506 | buffer_put_char(&e->output, |
| 507 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | process_remove_smartcard_key(SocketEntry *e) |
| 512 | { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 513 | Key *k = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 514 | int success = 0; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 515 | char *sc_reader_id = NULL; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 516 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 517 | sc_reader_id = buffer_get_string(&e->input, NULL); |
| 518 | k = sc_get_key(sc_reader_id); |
| 519 | xfree(sc_reader_id); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 520 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 521 | if (k == NULL) { |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 522 | error("sc_get_pubkey failed"); |
| 523 | } else { |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 524 | Identity *id; |
Damien Miller | 8d4bf17 | 2001-07-14 12:13:49 +1000 | [diff] [blame] | 525 | k->type = KEY_RSA1; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 526 | id = lookup_identity(k, 1); |
| 527 | if (id != NULL) { |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 528 | Idtab *tab = idtab_lookup(1); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 529 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 530 | free_identity(id); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 531 | tab->nentries--; |
| 532 | success = 1; |
| 533 | } |
Damien Miller | 8d4bf17 | 2001-07-14 12:13:49 +1000 | [diff] [blame] | 534 | k->type = KEY_RSA; |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 535 | id = lookup_identity(k, 2); |
| 536 | if (id != NULL) { |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 537 | Idtab *tab = idtab_lookup(2); |
Damien Miller | 1a534ae | 2002-01-22 23:26:13 +1100 | [diff] [blame] | 538 | TAILQ_REMOVE(&tab->idlist, id, next); |
| 539 | free_identity(id); |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 540 | tab->nentries--; |
| 541 | success = 1; |
| 542 | } |
| 543 | key_free(k); |
| 544 | } |
| 545 | |
| 546 | buffer_put_int(&e->output, 1); |
| 547 | buffer_put_char(&e->output, |
| 548 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
| 549 | } |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 550 | #endif /* SMARTCARD */ |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 551 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 552 | /* dispatch incoming messages */ |
| 553 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 554 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | process_message(SocketEntry *e) |
| 556 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 557 | u_int msg_len; |
| 558 | u_int type; |
| 559 | u_char *cp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 560 | if (buffer_len(&e->input) < 5) |
| 561 | return; /* Incomplete message. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 562 | cp = buffer_ptr(&e->input); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 563 | msg_len = GET_32BIT(cp); |
| 564 | if (msg_len > 256 * 1024) { |
| 565 | shutdown(e->fd, SHUT_RDWR); |
| 566 | close(e->fd); |
| 567 | e->type = AUTH_UNUSED; |
| 568 | return; |
| 569 | } |
| 570 | if (buffer_len(&e->input) < msg_len + 4) |
| 571 | return; |
| 572 | buffer_consume(&e->input, 4); |
| 573 | type = buffer_get_char(&e->input); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 574 | |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 575 | debug("type %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 576 | switch (type) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 577 | /* ssh1 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 578 | case SSH_AGENTC_RSA_CHALLENGE: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 579 | process_authentication_challenge1(e); |
| 580 | break; |
| 581 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 582 | process_request_identities(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 583 | break; |
| 584 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 585 | process_add_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 586 | break; |
| 587 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 588 | process_remove_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 589 | break; |
| 590 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 591 | process_remove_all_identities(e, 1); |
| 592 | break; |
| 593 | /* ssh2 */ |
| 594 | case SSH2_AGENTC_SIGN_REQUEST: |
| 595 | process_sign_request2(e); |
| 596 | break; |
| 597 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 598 | process_request_identities(e, 2); |
| 599 | break; |
| 600 | case SSH2_AGENTC_ADD_IDENTITY: |
| 601 | process_add_identity(e, 2); |
| 602 | break; |
| 603 | case SSH2_AGENTC_REMOVE_IDENTITY: |
| 604 | process_remove_identity(e, 2); |
| 605 | break; |
| 606 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
| 607 | process_remove_all_identities(e, 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 608 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 609 | #ifdef SMARTCARD |
| 610 | case SSH_AGENTC_ADD_SMARTCARD_KEY: |
| 611 | process_add_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 612 | break; |
Ben Lindstrom | 3f47163 | 2001-07-04 03:53:15 +0000 | [diff] [blame] | 613 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
| 614 | process_remove_smartcard_key(e); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 615 | break; |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 616 | #endif /* SMARTCARD */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 617 | default: |
| 618 | /* Unknown message. Respond with failure. */ |
| 619 | error("Unknown message %d", type); |
| 620 | buffer_clear(&e->input); |
| 621 | buffer_put_int(&e->output, 1); |
| 622 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 623 | break; |
| 624 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 625 | } |
| 626 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 627 | static void |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 628 | new_socket(sock_type type, int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 629 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 630 | u_int i, old_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 631 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
| 632 | error("fcntl O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 633 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 634 | if (fd > max_fd) |
| 635 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 636 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 637 | for (i = 0; i < sockets_alloc; i++) |
| 638 | if (sockets[i].type == AUTH_UNUSED) { |
| 639 | sockets[i].fd = fd; |
| 640 | sockets[i].type = type; |
| 641 | buffer_init(&sockets[i].input); |
| 642 | buffer_init(&sockets[i].output); |
| 643 | return; |
| 644 | } |
| 645 | old_alloc = sockets_alloc; |
| 646 | sockets_alloc += 10; |
| 647 | if (sockets) |
| 648 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); |
| 649 | else |
| 650 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); |
| 651 | for (i = old_alloc; i < sockets_alloc; i++) |
| 652 | sockets[i].type = AUTH_UNUSED; |
| 653 | sockets[old_alloc].type = type; |
| 654 | sockets[old_alloc].fd = fd; |
| 655 | buffer_init(&sockets[old_alloc].input); |
| 656 | buffer_init(&sockets[old_alloc].output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 657 | } |
| 658 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 659 | static int |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 660 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 661 | { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 662 | u_int i, sz; |
| 663 | int n = 0; |
| 664 | |
| 665 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 666 | switch (sockets[i].type) { |
| 667 | case AUTH_SOCKET: |
| 668 | case AUTH_CONNECTION: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 669 | n = MAX(n, sockets[i].fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 670 | break; |
| 671 | case AUTH_UNUSED: |
| 672 | break; |
| 673 | default: |
| 674 | fatal("Unknown socket type %d", sockets[i].type); |
| 675 | break; |
| 676 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 680 | if (*fdrp == NULL || sz > *nallocp) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 681 | if (*fdrp) |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 682 | xfree(*fdrp); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 683 | if (*fdwp) |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 684 | xfree(*fdwp); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 685 | *fdrp = xmalloc(sz); |
| 686 | *fdwp = xmalloc(sz); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 687 | *nallocp = sz; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 688 | } |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 689 | if (n < *fdl) |
| 690 | debug("XXX shrink: %d < %d", n, *fdl); |
| 691 | *fdl = n; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 692 | memset(*fdrp, 0, sz); |
| 693 | memset(*fdwp, 0, sz); |
| 694 | |
| 695 | for (i = 0; i < sockets_alloc; i++) { |
| 696 | switch (sockets[i].type) { |
| 697 | case AUTH_SOCKET: |
| 698 | case AUTH_CONNECTION: |
| 699 | FD_SET(sockets[i].fd, *fdrp); |
| 700 | if (buffer_len(&sockets[i].output) > 0) |
| 701 | FD_SET(sockets[i].fd, *fdwp); |
| 702 | break; |
| 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 708 | } |
| 709 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 710 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 711 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 712 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 713 | u_int i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 714 | int len, sock; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 715 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 716 | char buf[1024]; |
| 717 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 718 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 719 | for (i = 0; i < sockets_alloc; i++) |
| 720 | switch (sockets[i].type) { |
| 721 | case AUTH_UNUSED: |
| 722 | break; |
| 723 | case AUTH_SOCKET: |
| 724 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 725 | slen = sizeof(sunaddr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 726 | sock = accept(sockets[i].fd, |
| 727 | (struct sockaddr *) &sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 728 | if (sock < 0) { |
| 729 | perror("accept from AUTH_SOCKET"); |
| 730 | break; |
| 731 | } |
| 732 | new_socket(AUTH_CONNECTION, sock); |
| 733 | } |
| 734 | break; |
| 735 | case AUTH_CONNECTION: |
| 736 | if (buffer_len(&sockets[i].output) > 0 && |
| 737 | FD_ISSET(sockets[i].fd, writeset)) { |
Ben Lindstrom | b3144e5 | 2001-03-06 03:31:34 +0000 | [diff] [blame] | 738 | do { |
| 739 | len = write(sockets[i].fd, |
| 740 | buffer_ptr(&sockets[i].output), |
| 741 | buffer_len(&sockets[i].output)); |
| 742 | if (len == -1 && (errno == EAGAIN || |
| 743 | errno == EINTR)) |
| 744 | continue; |
| 745 | break; |
| 746 | } while (1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 747 | if (len <= 0) { |
| 748 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 749 | close(sockets[i].fd); |
| 750 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 751 | buffer_free(&sockets[i].input); |
| 752 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | buffer_consume(&sockets[i].output, len); |
| 756 | } |
| 757 | if (FD_ISSET(sockets[i].fd, readset)) { |
Ben Lindstrom | b3144e5 | 2001-03-06 03:31:34 +0000 | [diff] [blame] | 758 | do { |
| 759 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 760 | if (len == -1 && (errno == EAGAIN || |
| 761 | errno == EINTR)) |
| 762 | continue; |
| 763 | break; |
| 764 | } while (1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 765 | if (len <= 0) { |
| 766 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 767 | close(sockets[i].fd); |
| 768 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 769 | buffer_free(&sockets[i].input); |
| 770 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | buffer_append(&sockets[i].input, buf, len); |
| 774 | process_message(&sockets[i]); |
| 775 | } |
| 776 | break; |
| 777 | default: |
| 778 | fatal("Unknown type %d", sockets[i].type); |
| 779 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 780 | } |
| 781 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 782 | static void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 783 | cleanup_socket(void) |
| 784 | { |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 785 | if (socket_name[0]) |
| 786 | unlink(socket_name); |
| 787 | if (socket_dir[0]) |
| 788 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 789 | } |
| 790 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 791 | static void |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 792 | cleanup_exit(int i) |
| 793 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 794 | cleanup_socket(); |
| 795 | exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 796 | } |
| 797 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 798 | static void |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 799 | cleanup_handler(int sig) |
| 800 | { |
| 801 | cleanup_socket(); |
| 802 | _exit(2); |
| 803 | } |
| 804 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 805 | static void |
Ben Lindstrom | 0250da0 | 2001-07-22 20:44:00 +0000 | [diff] [blame] | 806 | check_parent_exists(int sig) |
| 807 | { |
| 808 | int save_errno = errno; |
| 809 | |
| 810 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
| 811 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
| 812 | cleanup_handler(sig); /* safe */ |
| 813 | } |
| 814 | signal(SIGALRM, check_parent_exists); |
| 815 | alarm(10); |
| 816 | errno = save_errno; |
| 817 | } |
| 818 | |
| 819 | static void |
Ben Lindstrom | 28072eb | 2001-02-10 23:13:41 +0000 | [diff] [blame] | 820 | usage(void) |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 821 | { |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 822 | fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", |
Kevin Steves | 2926586 | 2001-01-24 14:06:28 +0000 | [diff] [blame] | 823 | __progname); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 824 | fprintf(stderr, "Options:\n"); |
| 825 | fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); |
| 826 | fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); |
| 827 | fprintf(stderr, " -k Kill the current agent.\n"); |
| 828 | fprintf(stderr, " -d Debug mode.\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 829 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 830 | } |
| 831 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 832 | int |
| 833 | main(int ac, char **av) |
| 834 | { |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 835 | int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 836 | struct sockaddr_un sunaddr; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 837 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 838 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 839 | #endif |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 840 | #ifdef HAVE_CYGWIN |
| 841 | int prev_mask; |
| 842 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 843 | pid_t pid; |
| 844 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 845 | extern int optind; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 846 | fd_set *readsetp = NULL, *writesetp = NULL; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 847 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 848 | SSLeay_add_all_algorithms(); |
| 849 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 850 | __progname = get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 851 | init_rng(); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 852 | seed_rng(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 853 | |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 854 | #ifdef __GNU_LIBRARY__ |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 855 | while ((ch = getopt(ac, av, "+cdks")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 856 | #else /* __GNU_LIBRARY__ */ |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 857 | while ((ch = getopt(ac, av, "cdks")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 858 | #endif /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 859 | switch (ch) { |
| 860 | case 'c': |
| 861 | if (s_flag) |
| 862 | usage(); |
| 863 | c_flag++; |
| 864 | break; |
| 865 | case 'k': |
| 866 | k_flag++; |
| 867 | break; |
| 868 | case 's': |
| 869 | if (c_flag) |
| 870 | usage(); |
| 871 | s_flag++; |
| 872 | break; |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 873 | case 'd': |
| 874 | if (d_flag) |
| 875 | usage(); |
| 876 | d_flag++; |
| 877 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 878 | default: |
| 879 | usage(); |
| 880 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 881 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 882 | ac -= optind; |
| 883 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 884 | |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 885 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 886 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 887 | |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 888 | if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 889 | shell = getenv("SHELL"); |
| 890 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) |
| 891 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 892 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 893 | if (k_flag) { |
| 894 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 895 | if (pidstr == NULL) { |
| 896 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 897 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | exit(1); |
| 899 | } |
| 900 | pid = atoi(pidstr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 901 | if (pid < 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 902 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 903 | SSH_AGENTPID_ENV_NAME, pidstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 904 | exit(1); |
| 905 | } |
| 906 | if (kill(pid, SIGTERM) == -1) { |
| 907 | perror("kill"); |
| 908 | exit(1); |
| 909 | } |
| 910 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 911 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 912 | printf(format, SSH_AGENTPID_ENV_NAME); |
| 913 | printf("echo Agent pid %d killed;\n", pid); |
| 914 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 915 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 916 | parent_pid = getpid(); |
| 917 | |
| 918 | /* Create private directory for agent socket */ |
| 919 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); |
| 920 | if (mkdtemp(socket_dir) == NULL) { |
| 921 | perror("mkdtemp: private socket dir"); |
| 922 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 923 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 924 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 925 | parent_pid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 926 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 927 | /* |
| 928 | * Create socket early so it will exist before command gets run from |
| 929 | * the parent. |
| 930 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 931 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 932 | if (sock < 0) { |
| 933 | perror("socket"); |
| 934 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 935 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 936 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 937 | sunaddr.sun_family = AF_UNIX; |
| 938 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 939 | #ifdef HAVE_CYGWIN |
| 940 | prev_mask = umask(0177); |
| 941 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 942 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 943 | perror("bind"); |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 944 | #ifdef HAVE_CYGWIN |
| 945 | umask(prev_mask); |
| 946 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 947 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 948 | } |
Ben Lindstrom | 3524d69 | 2001-05-03 22:59:24 +0000 | [diff] [blame] | 949 | #ifdef HAVE_CYGWIN |
| 950 | umask(prev_mask); |
| 951 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 952 | if (listen(sock, 5) < 0) { |
| 953 | perror("listen"); |
| 954 | cleanup_exit(1); |
| 955 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 956 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 957 | /* |
| 958 | * Fork, and have the parent execute the command, if any, or present |
| 959 | * the socket data. The child continues as the authentication agent. |
| 960 | */ |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 961 | if (d_flag) { |
| 962 | log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); |
| 963 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 964 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 965 | SSH_AUTHSOCKET_ENV_NAME); |
| 966 | printf("echo Agent pid %d;\n", parent_pid); |
| 967 | goto skip; |
| 968 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 969 | pid = fork(); |
| 970 | if (pid == -1) { |
| 971 | perror("fork"); |
| 972 | exit(1); |
| 973 | } |
| 974 | if (pid != 0) { /* Parent - execute the given command. */ |
| 975 | close(sock); |
| 976 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); |
| 977 | if (ac == 0) { |
| 978 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 979 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 980 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 982 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 983 | printf("echo Agent pid %d;\n", pid); |
| 984 | exit(0); |
| 985 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 986 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 987 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 988 | perror("setenv"); |
| 989 | exit(1); |
| 990 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 991 | execvp(av[0], av); |
| 992 | perror(av[0]); |
| 993 | exit(1); |
| 994 | } |
Ben Lindstrom | 3fdf876 | 2001-07-22 20:40:24 +0000 | [diff] [blame] | 995 | |
| 996 | if (setsid() == -1) { |
| 997 | perror("setsid"); |
| 998 | cleanup_exit(1); |
| 999 | } |
| 1000 | |
| 1001 | (void)chdir("/"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1002 | close(0); |
| 1003 | close(1); |
| 1004 | close(2); |
| 1005 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1006 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1007 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 1008 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 1009 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { |
| 1010 | perror("setrlimit rlimit_core failed"); |
| 1011 | cleanup_exit(1); |
| 1012 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 1013 | #endif |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1014 | |
| 1015 | skip: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1016 | if (atexit(cleanup_socket) < 0) { |
| 1017 | perror("atexit"); |
| 1018 | cleanup_exit(1); |
| 1019 | } |
| 1020 | new_socket(AUTH_SOCKET, sock); |
| 1021 | if (ac > 0) { |
| 1022 | signal(SIGALRM, check_parent_exists); |
| 1023 | alarm(10); |
| 1024 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1025 | idtab_init(); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1026 | if (!d_flag) |
Ben Lindstrom | d94580c | 2001-07-04 03:48:02 +0000 | [diff] [blame] | 1027 | signal(SIGINT, SIG_IGN); |
Damien Miller | 48bfa9c | 2001-07-14 12:12:55 +1000 | [diff] [blame] | 1028 | signal(SIGPIPE, SIG_IGN); |
Ben Lindstrom | 77808ab | 2001-01-26 05:10:34 +0000 | [diff] [blame] | 1029 | signal(SIGHUP, cleanup_handler); |
| 1030 | signal(SIGTERM, cleanup_handler); |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1031 | nalloc = 0; |
| 1032 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1033 | while (1) { |
Ben Lindstrom | a3d5a4c | 2001-07-18 15:58:08 +0000 | [diff] [blame] | 1034 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1035 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1036 | if (errno == EINTR) |
| 1037 | continue; |
| 1038 | exit(1); |
| 1039 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1040 | after_select(readsetp, writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1041 | } |
| 1042 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1043 | } |