Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.47 2001/01/21 19:05:56 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, |
| 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 | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 40 | RCSID("$OpenBSD: ssh-agent.c,v 1.47 2001/01/21 19:05:56 markus Exp $"); |
| 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. */ |
| 201 | rsa_private_decrypt(challenge, challenge, private->rsa); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 202 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 203 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 204 | len = BN_num_bytes(challenge); |
| 205 | if (len <= 0 || len > 32) { |
| 206 | log("process_authentication_challenge: bad challenge length %d", len); |
| 207 | goto failure; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 209 | memset(buf, 0, 32); |
| 210 | BN_bn2bin(challenge, buf + 32 - len); |
| 211 | MD5_Init(&md); |
| 212 | MD5_Update(&md, buf, 32); |
| 213 | MD5_Update(&md, session_id, 16); |
| 214 | MD5_Final(mdbuf, &md); |
| 215 | |
| 216 | /* Send the response. */ |
| 217 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 218 | for (i = 0; i < 16; i++) |
| 219 | buffer_put_char(&msg, mdbuf[i]); |
| 220 | goto send; |
| 221 | } |
| 222 | |
| 223 | failure: |
| 224 | /* Unknown identity or protocol error. Send failure. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 226 | send: |
| 227 | buffer_put_int(&e->output, buffer_len(&msg)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 228 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 229 | key_free(key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 231 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | } |
| 233 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 234 | /* ssh2 only */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 235 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 236 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 238 | extern int datafellows; |
| 239 | Key *key, *private; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 240 | u_char *blob, *data, *signature = NULL; |
| 241 | u_int blen, dlen, slen = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 242 | int flags; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 243 | Buffer msg; |
| 244 | int ok = -1; |
| 245 | |
| 246 | datafellows = 0; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 247 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 248 | blob = buffer_get_string(&e->input, &blen); |
| 249 | data = buffer_get_string(&e->input, &dlen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 250 | |
| 251 | flags = buffer_get_int(&e->input); |
| 252 | if (flags & SSH_AGENT_OLD_SIGNATURE) |
| 253 | datafellows = SSH_BUG_SIGBLOB; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 254 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 255 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 256 | if (key != NULL) { |
| 257 | private = lookup_private_key(key, NULL, 2); |
| 258 | if (private != NULL) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 259 | ok = key_sign(private, &signature, &slen, data, dlen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 260 | } |
| 261 | key_free(key); |
| 262 | buffer_init(&msg); |
| 263 | if (ok == 0) { |
| 264 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); |
| 265 | buffer_put_string(&msg, signature, slen); |
| 266 | } else { |
| 267 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 268 | } |
| 269 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 270 | buffer_append(&e->output, buffer_ptr(&msg), |
| 271 | buffer_len(&msg)); |
| 272 | buffer_free(&msg); |
| 273 | xfree(data); |
| 274 | xfree(blob); |
| 275 | if (signature != NULL) |
| 276 | xfree(signature); |
| 277 | } |
| 278 | |
| 279 | /* shared */ |
| 280 | void |
| 281 | process_remove_identity(SocketEntry *e, int version) |
| 282 | { |
| 283 | Key *key = NULL, *private; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 284 | u_char *blob; |
| 285 | u_int blen; |
| 286 | u_int bits; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 287 | int success = 0; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 288 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 289 | switch(version){ |
| 290 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 291 | key = key_new(KEY_RSA1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 292 | bits = buffer_get_int(&e->input); |
| 293 | buffer_get_bignum(&e->input, key->rsa->e); |
| 294 | buffer_get_bignum(&e->input, key->rsa->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 296 | if (bits != key_size(key)) |
| 297 | log("Warning: identity keysize mismatch: actual %d, announced %d", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 298 | key_size(key), bits); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 299 | break; |
| 300 | case 2: |
| 301 | blob = buffer_get_string(&e->input, &blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 302 | key = key_from_blob(blob, blen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 303 | xfree(blob); |
| 304 | break; |
| 305 | } |
| 306 | if (key != NULL) { |
| 307 | int idx; |
| 308 | private = lookup_private_key(key, &idx, version); |
| 309 | if (private != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 310 | /* |
| 311 | * We have this key. Free the old key. Since we |
| 312 | * don\'t want to leave empty slots in the middle of |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 313 | * the array, we actually free the key there and move |
| 314 | * all the entries between the empty slot and the end |
| 315 | * of the array. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 316 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 317 | Idtab *tab = idtab_lookup(version); |
| 318 | key_free(tab->identities[idx].key); |
| 319 | xfree(tab->identities[idx].comment); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 320 | if (tab->nentries < 1) |
| 321 | fatal("process_remove_identity: " |
| 322 | "internal error: tab->nentries %d", |
| 323 | tab->nentries); |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 324 | if (idx != tab->nentries - 1) { |
| 325 | int i; |
| 326 | for (i = idx; i < tab->nentries - 1; i++) |
| 327 | tab->identities[i] = tab->identities[i+1]; |
| 328 | } |
| 329 | tab->identities[tab->nentries - 1].key = NULL; |
| 330 | tab->identities[tab->nentries - 1].comment = NULL; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 331 | tab->nentries--; |
| 332 | success = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 334 | key_free(key); |
| 335 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 336 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 337 | buffer_put_char(&e->output, |
| 338 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 339 | } |
| 340 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 341 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 342 | process_remove_all_identities(SocketEntry *e, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 343 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 344 | u_int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 345 | Idtab *tab = idtab_lookup(version); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 346 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 347 | /* Loop over all identities and clear the keys. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 348 | for (i = 0; i < tab->nentries; i++) { |
| 349 | key_free(tab->identities[i].key); |
| 350 | xfree(tab->identities[i].comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 351 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 352 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | /* Mark that there are no identities. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 354 | tab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 355 | |
| 356 | /* Send success. */ |
| 357 | buffer_put_int(&e->output, 1); |
| 358 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 359 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | } |
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 | void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 363 | generate_additional_parameters(RSA *rsa) |
| 364 | { |
| 365 | BIGNUM *aux; |
| 366 | BN_CTX *ctx; |
| 367 | /* Generate additional parameters */ |
| 368 | aux = BN_new(); |
| 369 | ctx = BN_CTX_new(); |
| 370 | |
| 371 | BN_sub(aux, rsa->q, BN_value_one()); |
| 372 | BN_mod(rsa->dmq1, rsa->d, aux, ctx); |
| 373 | |
| 374 | BN_sub(aux, rsa->p, BN_value_one()); |
| 375 | BN_mod(rsa->dmp1, rsa->d, aux, ctx); |
| 376 | |
| 377 | BN_clear_free(aux); |
| 378 | BN_CTX_free(ctx); |
| 379 | } |
| 380 | |
| 381 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 382 | process_add_identity(SocketEntry *e, int version) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 383 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 384 | Key *k = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 385 | char *type_name; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 386 | char *comment; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 387 | int type, success = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 388 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 390 | switch (version) { |
| 391 | case 1: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 392 | k = key_new_private(KEY_RSA1); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 393 | buffer_get_int(&e->input); /* ignored */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 394 | buffer_get_bignum(&e->input, k->rsa->n); |
| 395 | buffer_get_bignum(&e->input, k->rsa->e); |
| 396 | buffer_get_bignum(&e->input, k->rsa->d); |
| 397 | buffer_get_bignum(&e->input, k->rsa->iqmp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 398 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 399 | /* SSH and SSL have p and q swapped */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 400 | buffer_get_bignum(&e->input, k->rsa->q); /* p */ |
| 401 | buffer_get_bignum(&e->input, k->rsa->p); /* q */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 402 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 403 | /* Generate additional parameters */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 404 | generate_additional_parameters(k->rsa); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 405 | break; |
| 406 | case 2: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 407 | type_name = buffer_get_string(&e->input, NULL); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 408 | type = key_type_from_name(type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 409 | xfree(type_name); |
| 410 | switch(type) { |
| 411 | case KEY_DSA: |
| 412 | k = key_new_private(type); |
| 413 | buffer_get_bignum2(&e->input, k->dsa->p); |
| 414 | buffer_get_bignum2(&e->input, k->dsa->q); |
| 415 | buffer_get_bignum2(&e->input, k->dsa->g); |
| 416 | buffer_get_bignum2(&e->input, k->dsa->pub_key); |
| 417 | buffer_get_bignum2(&e->input, k->dsa->priv_key); |
| 418 | break; |
| 419 | case KEY_RSA: |
| 420 | k = key_new_private(type); |
| 421 | buffer_get_bignum2(&e->input, k->rsa->n); |
| 422 | buffer_get_bignum2(&e->input, k->rsa->e); |
| 423 | buffer_get_bignum2(&e->input, k->rsa->d); |
| 424 | buffer_get_bignum2(&e->input, k->rsa->iqmp); |
| 425 | buffer_get_bignum2(&e->input, k->rsa->p); |
| 426 | buffer_get_bignum2(&e->input, k->rsa->q); |
| 427 | |
| 428 | /* Generate additional parameters */ |
| 429 | generate_additional_parameters(k->rsa); |
| 430 | break; |
| 431 | default: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 432 | buffer_clear(&e->input); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 433 | goto send; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 434 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 435 | break; |
| 436 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 437 | comment = buffer_get_string(&e->input, NULL); |
| 438 | if (k == NULL) { |
| 439 | xfree(comment); |
| 440 | goto send; |
| 441 | } |
| 442 | success = 1; |
| 443 | if (lookup_private_key(k, NULL, version) == NULL) { |
| 444 | if (tab->nentries == 0) |
| 445 | tab->identities = xmalloc(sizeof(Identity)); |
| 446 | else |
| 447 | tab->identities = xrealloc(tab->identities, |
| 448 | (tab->nentries + 1) * sizeof(Identity)); |
| 449 | tab->identities[tab->nentries].key = k; |
| 450 | tab->identities[tab->nentries].comment = comment; |
| 451 | /* Increment the number of identities. */ |
| 452 | tab->nentries++; |
| 453 | } else { |
| 454 | key_free(k); |
| 455 | xfree(comment); |
| 456 | } |
| 457 | send: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 458 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 459 | buffer_put_char(&e->output, |
| 460 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 461 | } |
| 462 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 463 | /* dispatch incoming messages */ |
| 464 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 465 | void |
| 466 | process_message(SocketEntry *e) |
| 467 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 468 | u_int msg_len; |
| 469 | u_int type; |
| 470 | u_char *cp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 471 | if (buffer_len(&e->input) < 5) |
| 472 | return; /* Incomplete message. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 473 | cp = (u_char *) buffer_ptr(&e->input); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 474 | msg_len = GET_32BIT(cp); |
| 475 | if (msg_len > 256 * 1024) { |
| 476 | shutdown(e->fd, SHUT_RDWR); |
| 477 | close(e->fd); |
| 478 | e->type = AUTH_UNUSED; |
| 479 | return; |
| 480 | } |
| 481 | if (buffer_len(&e->input) < msg_len + 4) |
| 482 | return; |
| 483 | buffer_consume(&e->input, 4); |
| 484 | type = buffer_get_char(&e->input); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 485 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 486 | switch (type) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 487 | /* ssh1 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 488 | case SSH_AGENTC_RSA_CHALLENGE: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 489 | process_authentication_challenge1(e); |
| 490 | break; |
| 491 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 492 | process_request_identities(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 493 | break; |
| 494 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 495 | process_add_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 496 | break; |
| 497 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 498 | process_remove_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | break; |
| 500 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 501 | process_remove_all_identities(e, 1); |
| 502 | break; |
| 503 | /* ssh2 */ |
| 504 | case SSH2_AGENTC_SIGN_REQUEST: |
| 505 | process_sign_request2(e); |
| 506 | break; |
| 507 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 508 | process_request_identities(e, 2); |
| 509 | break; |
| 510 | case SSH2_AGENTC_ADD_IDENTITY: |
| 511 | process_add_identity(e, 2); |
| 512 | break; |
| 513 | case SSH2_AGENTC_REMOVE_IDENTITY: |
| 514 | process_remove_identity(e, 2); |
| 515 | break; |
| 516 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
| 517 | process_remove_all_identities(e, 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 518 | break; |
| 519 | default: |
| 520 | /* Unknown message. Respond with failure. */ |
| 521 | error("Unknown message %d", type); |
| 522 | buffer_clear(&e->input); |
| 523 | buffer_put_int(&e->output, 1); |
| 524 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 525 | break; |
| 526 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void |
| 530 | new_socket(int type, int fd) |
| 531 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 532 | u_int i, old_alloc; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 533 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
| 534 | error("fcntl O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 535 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | if (fd > max_fd) |
| 537 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 538 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | for (i = 0; i < sockets_alloc; i++) |
| 540 | if (sockets[i].type == AUTH_UNUSED) { |
| 541 | sockets[i].fd = fd; |
| 542 | sockets[i].type = type; |
| 543 | buffer_init(&sockets[i].input); |
| 544 | buffer_init(&sockets[i].output); |
| 545 | return; |
| 546 | } |
| 547 | old_alloc = sockets_alloc; |
| 548 | sockets_alloc += 10; |
| 549 | if (sockets) |
| 550 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); |
| 551 | else |
| 552 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); |
| 553 | for (i = old_alloc; i < sockets_alloc; i++) |
| 554 | sockets[i].type = AUTH_UNUSED; |
| 555 | sockets[old_alloc].type = type; |
| 556 | sockets[old_alloc].fd = fd; |
| 557 | buffer_init(&sockets[old_alloc].input); |
| 558 | buffer_init(&sockets[old_alloc].output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 559 | } |
| 560 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 561 | int |
| 562 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 563 | { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 564 | u_int i, sz; |
| 565 | int n = 0; |
| 566 | |
| 567 | for (i = 0; i < sockets_alloc; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 568 | switch (sockets[i].type) { |
| 569 | case AUTH_SOCKET: |
| 570 | case AUTH_CONNECTION: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 571 | n = MAX(n, sockets[i].fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 572 | break; |
| 573 | case AUTH_UNUSED: |
| 574 | break; |
| 575 | default: |
| 576 | fatal("Unknown socket type %d", sockets[i].type); |
| 577 | break; |
| 578 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); |
| 582 | if (*fdrp == NULL || n > *fdl) { |
| 583 | if (*fdrp) |
| 584 | free(*fdrp); |
| 585 | if (*fdwp) |
| 586 | free(*fdwp); |
| 587 | *fdrp = xmalloc(sz); |
| 588 | *fdwp = xmalloc(sz); |
| 589 | *fdl = n; |
| 590 | } |
| 591 | memset(*fdrp, 0, sz); |
| 592 | memset(*fdwp, 0, sz); |
| 593 | |
| 594 | for (i = 0; i < sockets_alloc; i++) { |
| 595 | switch (sockets[i].type) { |
| 596 | case AUTH_SOCKET: |
| 597 | case AUTH_CONNECTION: |
| 598 | FD_SET(sockets[i].fd, *fdrp); |
| 599 | if (buffer_len(&sockets[i].output) > 0) |
| 600 | FD_SET(sockets[i].fd, *fdwp); |
| 601 | break; |
| 602 | default: |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 607 | } |
| 608 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 609 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 610 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 611 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 612 | u_int i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 613 | int len, sock; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 614 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 615 | char buf[1024]; |
| 616 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 617 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 618 | for (i = 0; i < sockets_alloc; i++) |
| 619 | switch (sockets[i].type) { |
| 620 | case AUTH_UNUSED: |
| 621 | break; |
| 622 | case AUTH_SOCKET: |
| 623 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 624 | slen = sizeof(sunaddr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 625 | sock = accept(sockets[i].fd, |
| 626 | (struct sockaddr *) &sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 627 | if (sock < 0) { |
| 628 | perror("accept from AUTH_SOCKET"); |
| 629 | break; |
| 630 | } |
| 631 | new_socket(AUTH_CONNECTION, sock); |
| 632 | } |
| 633 | break; |
| 634 | case AUTH_CONNECTION: |
| 635 | if (buffer_len(&sockets[i].output) > 0 && |
| 636 | FD_ISSET(sockets[i].fd, writeset)) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 637 | len = write(sockets[i].fd, |
| 638 | buffer_ptr(&sockets[i].output), |
| 639 | buffer_len(&sockets[i].output)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 640 | if (len <= 0) { |
| 641 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 642 | close(sockets[i].fd); |
| 643 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 644 | buffer_free(&sockets[i].input); |
| 645 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | buffer_consume(&sockets[i].output, len); |
| 649 | } |
| 650 | if (FD_ISSET(sockets[i].fd, readset)) { |
| 651 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 652 | if (len <= 0) { |
| 653 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 654 | close(sockets[i].fd); |
| 655 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 656 | buffer_free(&sockets[i].input); |
| 657 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | buffer_append(&sockets[i].input, buf, len); |
| 661 | process_message(&sockets[i]); |
| 662 | } |
| 663 | break; |
| 664 | default: |
| 665 | fatal("Unknown type %d", sockets[i].type); |
| 666 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | void |
| 670 | check_parent_exists(int sig) |
| 671 | { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 672 | int save_errno = errno; |
| 673 | |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 674 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 675 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
| 676 | exit(1); |
| 677 | } |
| 678 | signal(SIGALRM, check_parent_exists); |
| 679 | alarm(10); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 680 | errno = save_errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 681 | } |
| 682 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 683 | void |
| 684 | cleanup_socket(void) |
| 685 | { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 686 | unlink(socket_name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 687 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 688 | } |
| 689 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 690 | void |
| 691 | cleanup_exit(int i) |
| 692 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 693 | cleanup_socket(); |
| 694 | exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void |
| 698 | usage() |
| 699 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 700 | fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION); |
| 701 | fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 702 | __progname); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 703 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 704 | } |
| 705 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 706 | int |
| 707 | main(int ac, char **av) |
| 708 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 709 | int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch; |
| 710 | struct sockaddr_un sunaddr; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 711 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 712 | struct rlimit rlim; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 713 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 714 | pid_t pid; |
| 715 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 716 | extern int optind; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 717 | fd_set *readsetp = NULL, *writesetp = NULL; |
Kevin Steves | de41bc6 | 2000-12-14 00:04:08 +0000 | [diff] [blame] | 718 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 719 | __progname = get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 720 | init_rng(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 721 | |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 722 | #ifdef __GNU_LIBRARY__ |
| 723 | while ((ch = getopt(ac, av, "+cks")) != -1) { |
| 724 | #else /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 725 | while ((ch = getopt(ac, av, "cks")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 726 | #endif /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 727 | switch (ch) { |
| 728 | case 'c': |
| 729 | if (s_flag) |
| 730 | usage(); |
| 731 | c_flag++; |
| 732 | break; |
| 733 | case 'k': |
| 734 | k_flag++; |
| 735 | break; |
| 736 | case 's': |
| 737 | if (c_flag) |
| 738 | usage(); |
| 739 | s_flag++; |
| 740 | break; |
| 741 | default: |
| 742 | usage(); |
| 743 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 744 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 745 | ac -= optind; |
| 746 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 747 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 748 | if (ac > 0 && (c_flag || k_flag || s_flag)) |
| 749 | usage(); |
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 | if (ac == 0 && !c_flag && !k_flag && !s_flag) { |
| 752 | shell = getenv("SHELL"); |
| 753 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) |
| 754 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 755 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 756 | if (k_flag) { |
| 757 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 758 | if (pidstr == NULL) { |
| 759 | fprintf(stderr, "%s not set, cannot kill agent\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 760 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 761 | exit(1); |
| 762 | } |
| 763 | pid = atoi(pidstr); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 764 | if (pid < 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 765 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 766 | SSH_AGENTPID_ENV_NAME, pidstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 767 | exit(1); |
| 768 | } |
| 769 | if (kill(pid, SIGTERM) == -1) { |
| 770 | perror("kill"); |
| 771 | exit(1); |
| 772 | } |
| 773 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 774 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 775 | printf(format, SSH_AGENTPID_ENV_NAME); |
| 776 | printf("echo Agent pid %d killed;\n", pid); |
| 777 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 778 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 779 | parent_pid = getpid(); |
| 780 | |
| 781 | /* Create private directory for agent socket */ |
| 782 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); |
| 783 | if (mkdtemp(socket_dir) == NULL) { |
| 784 | perror("mkdtemp: private socket dir"); |
| 785 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 786 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 787 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 788 | parent_pid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 789 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 790 | /* |
| 791 | * Create socket early so it will exist before command gets run from |
| 792 | * the parent. |
| 793 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 794 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 795 | if (sock < 0) { |
| 796 | perror("socket"); |
| 797 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 798 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 799 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 800 | sunaddr.sun_family = AF_UNIX; |
| 801 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
| 802 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 803 | perror("bind"); |
| 804 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 805 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 806 | if (listen(sock, 5) < 0) { |
| 807 | perror("listen"); |
| 808 | cleanup_exit(1); |
| 809 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 810 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 811 | /* |
| 812 | * Fork, and have the parent execute the command, if any, or present |
| 813 | * the socket data. The child continues as the authentication agent. |
| 814 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 815 | pid = fork(); |
| 816 | if (pid == -1) { |
| 817 | perror("fork"); |
| 818 | exit(1); |
| 819 | } |
| 820 | if (pid != 0) { /* Parent - execute the given command. */ |
| 821 | close(sock); |
| 822 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); |
| 823 | if (ac == 0) { |
| 824 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 825 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 826 | SSH_AUTHSOCKET_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 828 | SSH_AGENTPID_ENV_NAME); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 829 | printf("echo Agent pid %d;\n", pid); |
| 830 | exit(0); |
| 831 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 832 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
| 833 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { |
| 834 | perror("setenv"); |
| 835 | exit(1); |
| 836 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 837 | execvp(av[0], av); |
| 838 | perror(av[0]); |
| 839 | exit(1); |
| 840 | } |
| 841 | close(0); |
| 842 | close(1); |
| 843 | close(2); |
| 844 | |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 845 | #ifdef HAVE_SETRLIMIT |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 846 | /* deny core dumps, since memory contains unencrypted private keys */ |
| 847 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 848 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { |
| 849 | perror("setrlimit rlimit_core failed"); |
| 850 | cleanup_exit(1); |
| 851 | } |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 852 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 853 | if (setsid() == -1) { |
| 854 | perror("setsid"); |
| 855 | cleanup_exit(1); |
| 856 | } |
| 857 | if (atexit(cleanup_socket) < 0) { |
| 858 | perror("atexit"); |
| 859 | cleanup_exit(1); |
| 860 | } |
| 861 | new_socket(AUTH_SOCKET, sock); |
| 862 | if (ac > 0) { |
| 863 | signal(SIGALRM, check_parent_exists); |
| 864 | alarm(10); |
| 865 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 866 | idtab_init(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 867 | signal(SIGINT, SIG_IGN); |
| 868 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 869 | signal(SIGHUP, cleanup_exit); |
| 870 | signal(SIGTERM, cleanup_exit); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 871 | while (1) { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 872 | prepare_select(&readsetp, &writesetp, &max_fd); |
| 873 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 874 | if (errno == EINTR) |
| 875 | continue; |
| 876 | exit(1); |
| 877 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 878 | after_select(readsetp, writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 879 | } |
| 880 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 881 | } |