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