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