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