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