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