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