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