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