Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "includes.h" |
Ben Lindstrom | 4887da2 | 2002-06-06 20:05:57 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.102 2002/05/25 08:50:39 markus Exp $"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 27 | |
| 28 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 29 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 30 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 31 | #include "buffer.h" |
| 32 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 33 | #include "compat.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 35 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 36 | #include "kex.h" |
| 37 | #include "myproposal.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 38 | #include "sshconnect.h" |
| 39 | #include "authfile.h" |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 40 | #include "dh.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 41 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "log.h" |
| 43 | #include "readconf.h" |
| 44 | #include "readpass.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 45 | #include "match.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 46 | #include "dispatch.h" |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 47 | #include "canohost.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 48 | #include "msg.h" |
| 49 | #include "pathnames.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 50 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 51 | /* import */ |
| 52 | extern char *client_version_string; |
| 53 | extern char *server_version_string; |
| 54 | extern Options options; |
| 55 | |
| 56 | /* |
| 57 | * SSH2 key exchange |
| 58 | */ |
| 59 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 60 | u_char *session_id2 = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 61 | int session_id2_len = 0; |
| 62 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 63 | char *xxx_host; |
| 64 | struct sockaddr *xxx_hostaddr; |
| 65 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 66 | Kex *xxx_kex = NULL; |
| 67 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 68 | static int |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 69 | verify_host_key_callback(Key *hostkey) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 70 | { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 71 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 72 | fatal("Host key verification failed."); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 76 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 77 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
| 78 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 79 | Kex *kex; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 80 | |
| 81 | xxx_host = host; |
| 82 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 83 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 84 | if (options.ciphers == (char *)-1) { |
| 85 | log("No valid ciphers for protocol version 2 given, using defaults."); |
| 86 | options.ciphers = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 87 | } |
| 88 | if (options.ciphers != NULL) { |
| 89 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 90 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 91 | } |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 92 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 93 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); |
| 94 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
| 95 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 96 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 97 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 98 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; |
| 99 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 100 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 101 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; |
| 102 | } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 103 | if (options.macs != NULL) { |
| 104 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 105 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 106 | } |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 107 | if (options.hostkeyalgorithms != NULL) |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 108 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 109 | options.hostkeyalgorithms; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 110 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 111 | /* start key exchange */ |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 112 | kex = kex_setup(myproposal); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 113 | kex->client_version_string=client_version_string; |
| 114 | kex->server_version_string=server_version_string; |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 115 | kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 116 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 117 | xxx_kex = kex; |
| 118 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 119 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 120 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 121 | session_id2 = kex->session_id; |
| 122 | session_id2_len = kex->session_id_len; |
| 123 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 124 | #ifdef DEBUG_KEXDH |
| 125 | /* send 1st encrypted/maced/compressed message */ |
| 126 | packet_start(SSH2_MSG_IGNORE); |
| 127 | packet_put_cstring("markus"); |
| 128 | packet_send(); |
| 129 | packet_write_wait(); |
| 130 | #endif |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 131 | debug("done: ssh_kex2."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 132 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 133 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 134 | /* |
| 135 | * Authenticate user |
| 136 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 137 | |
| 138 | typedef struct Authctxt Authctxt; |
| 139 | typedef struct Authmethod Authmethod; |
| 140 | |
| 141 | typedef int sign_cb_fn( |
| 142 | Authctxt *authctxt, Key *key, |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 143 | u_char **sigp, u_int *lenp, u_char *data, u_int datalen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 144 | |
| 145 | struct Authctxt { |
| 146 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 147 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 148 | const char *host; |
| 149 | const char *service; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 150 | Authmethod *method; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 151 | int success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 152 | char *authlist; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 153 | /* pubkey */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 154 | Key *last_key; |
| 155 | sign_cb_fn *last_key_sign; |
| 156 | int last_key_hint; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 157 | AuthenticationConnection *agent; |
| 158 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 159 | Sensitive *sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 160 | /* kbd-interactive */ |
| 161 | int info_req_seen; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 162 | }; |
| 163 | struct Authmethod { |
| 164 | char *name; /* string to compare against server's list */ |
| 165 | int (*userauth)(Authctxt *authctxt); |
| 166 | int *enabled; /* flag in option struct that enables method */ |
| 167 | int *batch_flag; /* flag in option struct that disables method */ |
| 168 | }; |
| 169 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 170 | void input_userauth_success(int, u_int32_t, void *); |
| 171 | void input_userauth_failure(int, u_int32_t, void *); |
| 172 | void input_userauth_banner(int, u_int32_t, void *); |
| 173 | void input_userauth_error(int, u_int32_t, void *); |
| 174 | void input_userauth_info_req(int, u_int32_t, void *); |
| 175 | void input_userauth_pk_ok(int, u_int32_t, void *); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 176 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 177 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 178 | int userauth_none(Authctxt *); |
| 179 | int userauth_pubkey(Authctxt *); |
| 180 | int userauth_passwd(Authctxt *); |
| 181 | int userauth_kbdint(Authctxt *); |
| 182 | int userauth_hostbased(Authctxt *); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 183 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 184 | void userauth(Authctxt *, char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 185 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 186 | static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *); |
| 187 | static void clear_auth_state(Authctxt *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 188 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 189 | static Authmethod *authmethod_get(char *authlist); |
| 190 | static Authmethod *authmethod_lookup(const char *name); |
| 191 | static char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 192 | |
| 193 | Authmethod authmethods[] = { |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 194 | {"hostbased", |
| 195 | userauth_hostbased, |
| 196 | &options.hostbased_authentication, |
| 197 | NULL}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 198 | {"publickey", |
| 199 | userauth_pubkey, |
| 200 | &options.pubkey_authentication, |
| 201 | NULL}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 202 | {"keyboard-interactive", |
| 203 | userauth_kbdint, |
| 204 | &options.kbd_interactive_authentication, |
| 205 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 206 | {"password", |
| 207 | userauth_passwd, |
| 208 | &options.password_authentication, |
| 209 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 210 | {"none", |
| 211 | userauth_none, |
| 212 | NULL, |
| 213 | NULL}, |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 214 | {NULL, NULL, NULL, NULL} |
| 215 | }; |
| 216 | |
| 217 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 218 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 219 | Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 220 | { |
| 221 | Authctxt authctxt; |
| 222 | int type; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 223 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 224 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 225 | options.kbd_interactive_authentication = 1; |
| 226 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 227 | debug("send SSH2_MSG_SERVICE_REQUEST"); |
| 228 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 229 | packet_put_cstring("ssh-userauth"); |
| 230 | packet_send(); |
| 231 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 232 | type = packet_read(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 233 | if (type != SSH2_MSG_SERVICE_ACCEPT) { |
| 234 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); |
| 235 | } |
| 236 | if (packet_remaining() > 0) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 237 | char *reply = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 238 | debug("service_accept: %s", reply); |
| 239 | xfree(reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 240 | } else { |
| 241 | debug("buggy server: service_accept w/o service"); |
| 242 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 243 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 244 | debug("got SSH2_MSG_SERVICE_ACCEPT"); |
| 245 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 246 | if (options.preferred_authentications == NULL) |
| 247 | options.preferred_authentications = authmethods_get(); |
| 248 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 249 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 250 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 251 | authctxt.agent = ssh_get_authentication_connection(); |
| 252 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 253 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 254 | authctxt.host = host; |
| 255 | authctxt.service = "ssh-connection"; /* service name */ |
| 256 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 257 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 258 | authctxt.authlist = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 259 | authctxt.sensitive = sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 260 | authctxt.info_req_seen = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 261 | if (authctxt.method == NULL) |
| 262 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 263 | |
| 264 | /* initial userauth request */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 265 | userauth_none(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 266 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 267 | dispatch_init(&input_userauth_error); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 268 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 269 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 270 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 271 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
| 272 | |
| 273 | if (authctxt.agent != NULL) |
| 274 | ssh_close_authentication_connection(authctxt.agent); |
| 275 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 276 | debug("ssh-userauth2 successful: method %s", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 277 | } |
| 278 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 279 | userauth(Authctxt *authctxt, char *authlist) |
| 280 | { |
| 281 | if (authlist == NULL) { |
| 282 | authlist = authctxt->authlist; |
| 283 | } else { |
| 284 | if (authctxt->authlist) |
| 285 | xfree(authctxt->authlist); |
| 286 | authctxt->authlist = authlist; |
| 287 | } |
| 288 | for (;;) { |
| 289 | Authmethod *method = authmethod_get(authlist); |
| 290 | if (method == NULL) |
| 291 | fatal("Permission denied (%s).", authlist); |
| 292 | authctxt->method = method; |
| 293 | if (method->userauth(authctxt) != 0) { |
| 294 | debug2("we sent a %s packet, wait for reply", method->name); |
| 295 | break; |
| 296 | } else { |
| 297 | debug2("we did not send a packet, disable method"); |
| 298 | method->enabled = NULL; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 303 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 304 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 305 | fatal("input_userauth_error: bad message during authentication: " |
| 306 | "type %d", type); |
| 307 | } |
| 308 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 309 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 310 | { |
| 311 | char *msg, *lang; |
| 312 | debug3("input_userauth_banner"); |
| 313 | msg = packet_get_string(NULL); |
| 314 | lang = packet_get_string(NULL); |
| 315 | fprintf(stderr, "%s", msg); |
| 316 | xfree(msg); |
| 317 | xfree(lang); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 318 | } |
| 319 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 320 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 321 | { |
| 322 | Authctxt *authctxt = ctxt; |
| 323 | if (authctxt == NULL) |
| 324 | fatal("input_userauth_success: no authentication context"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 325 | if (authctxt->authlist) |
| 326 | xfree(authctxt->authlist); |
| 327 | clear_auth_state(authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 328 | authctxt->success = 1; /* break out */ |
| 329 | } |
| 330 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 331 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 332 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 333 | Authctxt *authctxt = ctxt; |
| 334 | char *authlist = NULL; |
| 335 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 336 | |
| 337 | if (authctxt == NULL) |
| 338 | fatal("input_userauth_failure: no authentication context"); |
| 339 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 340 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 341 | partial = packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 342 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 343 | |
| 344 | if (partial != 0) |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 345 | log("Authenticated with partial success."); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 346 | debug("authentications that can continue: %s", authlist); |
| 347 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 348 | clear_auth_state(authctxt); |
| 349 | userauth(authctxt, authlist); |
| 350 | } |
| 351 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 352 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 353 | { |
| 354 | Authctxt *authctxt = ctxt; |
| 355 | Key *key = NULL; |
| 356 | Buffer b; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 357 | int pktype, sent = 0; |
| 358 | u_int alen, blen; |
| 359 | char *pkalg, *fp; |
| 360 | u_char *pkblob; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 361 | |
| 362 | if (authctxt == NULL) |
| 363 | fatal("input_userauth_pk_ok: no authentication context"); |
| 364 | if (datafellows & SSH_BUG_PKOK) { |
| 365 | /* this is similar to SSH_BUG_PKAUTH */ |
| 366 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 367 | pkblob = packet_get_string(&blen); |
| 368 | buffer_init(&b); |
| 369 | buffer_append(&b, pkblob, blen); |
| 370 | pkalg = buffer_get_string(&b, &alen); |
| 371 | buffer_free(&b); |
| 372 | } else { |
| 373 | pkalg = packet_get_string(&alen); |
| 374 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 375 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 376 | packet_check_eom(); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 377 | |
| 378 | debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d", |
| 379 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); |
| 380 | |
| 381 | do { |
| 382 | if (authctxt->last_key == NULL || |
| 383 | authctxt->last_key_sign == NULL) { |
| 384 | debug("no last key or no sign cb"); |
| 385 | break; |
| 386 | } |
Damien Miller | 68f4598 | 2002-02-05 12:23:32 +1100 | [diff] [blame] | 387 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 388 | debug("unknown pkalg %s", pkalg); |
| 389 | break; |
| 390 | } |
| 391 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 392 | debug("no key from blob. pkalg %s", pkalg); |
| 393 | break; |
| 394 | } |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 395 | if (key->type != pktype) { |
Damien Miller | 68f4598 | 2002-02-05 12:23:32 +1100 | [diff] [blame] | 396 | error("input_userauth_pk_ok: type mismatch " |
| 397 | "for decoded key (received %d, expected %d)", |
| 398 | key->type, pktype); |
| 399 | break; |
| 400 | } |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 401 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 402 | debug2("input_userauth_pk_ok: fp %s", fp); |
| 403 | xfree(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 404 | if (!key_equal(key, authctxt->last_key)) { |
| 405 | debug("key != last_key"); |
| 406 | break; |
| 407 | } |
| 408 | sent = sign_and_send_pubkey(authctxt, key, |
| 409 | authctxt->last_key_sign); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 410 | } while (0); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 411 | |
| 412 | if (key != NULL) |
| 413 | key_free(key); |
| 414 | xfree(pkalg); |
| 415 | xfree(pkblob); |
| 416 | |
| 417 | /* unregister */ |
| 418 | clear_auth_state(authctxt); |
| 419 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); |
| 420 | |
| 421 | /* try another method if we did not send a packet*/ |
| 422 | if (sent == 0) |
| 423 | userauth(authctxt, NULL); |
| 424 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 425 | } |
| 426 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 427 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 428 | userauth_none(Authctxt *authctxt) |
| 429 | { |
| 430 | /* initial userauth request */ |
| 431 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 432 | packet_put_cstring(authctxt->server_user); |
| 433 | packet_put_cstring(authctxt->service); |
| 434 | packet_put_cstring(authctxt->method->name); |
| 435 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 440 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 441 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 442 | static int attempt = 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 443 | char prompt[150]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 444 | char *password; |
| 445 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 446 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 447 | return 0; |
| 448 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 449 | if (attempt != 1) |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 450 | error("Permission denied, please try again."); |
| 451 | |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 452 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 453 | authctxt->server_user, authctxt->host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 454 | password = read_passphrase(prompt, 0); |
| 455 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 456 | packet_put_cstring(authctxt->server_user); |
| 457 | packet_put_cstring(authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 458 | packet_put_cstring(authctxt->method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 459 | packet_put_char(0); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 460 | packet_put_cstring(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 461 | memset(password, 0, strlen(password)); |
| 462 | xfree(password); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 463 | packet_add_padding(64); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 464 | packet_send(); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 465 | |
| 466 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
| 467 | &input_userauth_passwd_changereq); |
| 468 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 469 | return 1; |
| 470 | } |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 471 | /* |
| 472 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
| 473 | */ |
| 474 | void |
Tim Rice | c854962 | 2002-03-31 12:49:38 -0800 | [diff] [blame] | 475 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 476 | { |
| 477 | Authctxt *authctxt = ctxt; |
| 478 | char *info, *lang, *password = NULL, *retype = NULL; |
| 479 | char prompt[150]; |
| 480 | |
| 481 | debug2("input_userauth_passwd_changereq"); |
| 482 | |
| 483 | if (authctxt == NULL) |
| 484 | fatal("input_userauth_passwd_changereq: " |
| 485 | "no authentication context"); |
| 486 | |
| 487 | info = packet_get_string(NULL); |
| 488 | lang = packet_get_string(NULL); |
| 489 | if (strlen(info) > 0) |
| 490 | log("%s", info); |
| 491 | xfree(info); |
| 492 | xfree(lang); |
| 493 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 494 | packet_put_cstring(authctxt->server_user); |
| 495 | packet_put_cstring(authctxt->service); |
| 496 | packet_put_cstring(authctxt->method->name); |
| 497 | packet_put_char(1); /* additional info */ |
| 498 | snprintf(prompt, sizeof(prompt), |
| 499 | "Enter %.30s@%.128s's old password: ", |
| 500 | authctxt->server_user, authctxt->host); |
| 501 | password = read_passphrase(prompt, 0); |
| 502 | packet_put_cstring(password); |
| 503 | memset(password, 0, strlen(password)); |
| 504 | xfree(password); |
| 505 | password = NULL; |
| 506 | while (password == NULL) { |
| 507 | snprintf(prompt, sizeof(prompt), |
| 508 | "Enter %.30s@%.128s's new password: ", |
| 509 | authctxt->server_user, authctxt->host); |
| 510 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
| 511 | if (password == NULL) { |
| 512 | /* bail out */ |
| 513 | return; |
| 514 | } |
| 515 | snprintf(prompt, sizeof(prompt), |
| 516 | "Retype %.30s@%.128s's new password: ", |
| 517 | authctxt->server_user, authctxt->host); |
| 518 | retype = read_passphrase(prompt, 0); |
| 519 | if (strcmp(password, retype) != 0) { |
| 520 | memset(password, 0, strlen(password)); |
| 521 | xfree(password); |
| 522 | log("Mismatch; try again, EOF to quit."); |
| 523 | password = NULL; |
| 524 | } |
| 525 | memset(retype, 0, strlen(retype)); |
| 526 | xfree(retype); |
| 527 | } |
| 528 | packet_put_cstring(password); |
| 529 | memset(password, 0, strlen(password)); |
| 530 | xfree(password); |
| 531 | packet_add_padding(64); |
| 532 | packet_send(); |
| 533 | |
| 534 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
| 535 | &input_userauth_passwd_changereq); |
| 536 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 537 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 538 | static void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 539 | clear_auth_state(Authctxt *authctxt) |
| 540 | { |
| 541 | /* XXX clear authentication state */ |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 542 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL); |
| 543 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 544 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { |
| 545 | debug3("clear_auth_state: key_free %p", authctxt->last_key); |
| 546 | key_free(authctxt->last_key); |
| 547 | } |
| 548 | authctxt->last_key = NULL; |
| 549 | authctxt->last_key_hint = -2; |
| 550 | authctxt->last_key_sign = NULL; |
| 551 | } |
| 552 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 553 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 554 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 555 | { |
| 556 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 557 | u_char *blob, *signature; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 558 | u_int bloblen, slen; |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 559 | int skip = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 560 | int ret = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 561 | int have_sig = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 562 | |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 563 | debug3("sign_and_send_pubkey"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 564 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 565 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
| 566 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 567 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 568 | return 0; |
| 569 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 570 | /* data to be signed */ |
| 571 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 572 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 573 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 574 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 575 | } else { |
| 576 | buffer_put_string(&b, session_id2, session_id2_len); |
| 577 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 578 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 579 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 580 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 581 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 582 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 583 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 584 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 585 | if (datafellows & SSH_BUG_PKAUTH) { |
| 586 | buffer_put_char(&b, have_sig); |
| 587 | } else { |
| 588 | buffer_put_cstring(&b, authctxt->method->name); |
| 589 | buffer_put_char(&b, have_sig); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 590 | buffer_put_cstring(&b, key_ssh_name(k)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 591 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 592 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 593 | |
| 594 | /* generate signature */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 595 | ret = (*sign_callback)(authctxt, k, &signature, &slen, |
| 596 | buffer_ptr(&b), buffer_len(&b)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 597 | if (ret == -1) { |
| 598 | xfree(blob); |
| 599 | buffer_free(&b); |
| 600 | return 0; |
| 601 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 602 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 603 | buffer_dump(&b); |
| 604 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 605 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 606 | buffer_clear(&b); |
| 607 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 608 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 609 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 610 | buffer_put_cstring(&b, authctxt->server_user); |
| 611 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 612 | buffer_put_cstring(&b, authctxt->method->name); |
| 613 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 614 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 615 | buffer_put_cstring(&b, key_ssh_name(k)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 616 | buffer_put_string(&b, blob, bloblen); |
| 617 | } |
| 618 | xfree(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 619 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 620 | /* append signature */ |
| 621 | buffer_put_string(&b, signature, slen); |
| 622 | xfree(signature); |
| 623 | |
| 624 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 625 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 626 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 627 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 628 | |
| 629 | /* put remaining data from buffer into packet */ |
| 630 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 631 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 632 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 633 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 634 | |
| 635 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 636 | } |
| 637 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 638 | static int |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 639 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, |
| 640 | int hint) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 641 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 642 | u_char *blob; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 643 | u_int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 644 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 645 | debug3("send_pubkey_test"); |
| 646 | |
| 647 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
| 648 | /* we cannot handle this key */ |
| 649 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 650 | return 0; |
| 651 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 652 | /* register callback for USERAUTH_PK_OK message */ |
| 653 | authctxt->last_key_sign = sign_callback; |
| 654 | authctxt->last_key_hint = hint; |
| 655 | authctxt->last_key = k; |
| 656 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 657 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 658 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 659 | packet_put_cstring(authctxt->server_user); |
| 660 | packet_put_cstring(authctxt->service); |
| 661 | packet_put_cstring(authctxt->method->name); |
| 662 | packet_put_char(have_sig); |
| 663 | if (!(datafellows & SSH_BUG_PKAUTH)) |
| 664 | packet_put_cstring(key_ssh_name(k)); |
| 665 | packet_put_string(blob, bloblen); |
| 666 | xfree(blob); |
| 667 | packet_send(); |
| 668 | return 1; |
| 669 | } |
| 670 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 671 | static Key * |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 672 | load_identity_file(char *filename) |
| 673 | { |
| 674 | Key *private; |
| 675 | char prompt[300], *passphrase; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 676 | int quit, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 677 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 678 | |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 679 | if (stat(filename, &st) < 0) { |
| 680 | debug3("no such identity: %s", filename); |
| 681 | return NULL; |
| 682 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 683 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); |
| 684 | if (private == NULL) { |
| 685 | if (options.batch_mode) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 686 | return NULL; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 687 | snprintf(prompt, sizeof prompt, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 688 | "Enter passphrase for key '%.100s': ", filename); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 689 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 690 | passphrase = read_passphrase(prompt, 0); |
| 691 | if (strcmp(passphrase, "") != 0) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 692 | private = key_load_private_type(KEY_UNSPEC, filename, |
| 693 | passphrase, NULL); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 694 | quit = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 695 | } else { |
| 696 | debug2("no passphrase given, try next key"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 697 | quit = 1; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 698 | } |
| 699 | memset(passphrase, 0, strlen(passphrase)); |
| 700 | xfree(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 701 | if (private != NULL || quit) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 702 | break; |
| 703 | debug2("bad passphrase given, try again..."); |
| 704 | } |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 705 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 706 | return private; |
| 707 | } |
| 708 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 709 | static int |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 710 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
| 711 | u_char *data, u_int datalen) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 712 | { |
| 713 | Key *private; |
| 714 | int idx, ret; |
| 715 | |
| 716 | idx = authctxt->last_key_hint; |
| 717 | if (idx < 0) |
| 718 | return -1; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 719 | |
| 720 | /* private key is stored in external hardware */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 721 | if (options.identity_keys[idx]->flags & KEY_FLAG_EXT) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 722 | return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen); |
| 723 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 724 | private = load_identity_file(options.identity_files[idx]); |
| 725 | if (private == NULL) |
| 726 | return -1; |
| 727 | ret = key_sign(private, sigp, lenp, data, datalen); |
| 728 | key_free(private); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 729 | return ret; |
| 730 | } |
| 731 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 732 | static int |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 733 | agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
| 734 | u_char *data, u_int datalen) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 735 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 736 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 737 | } |
| 738 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 739 | static int |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 740 | key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
| 741 | u_char *data, u_int datalen) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 742 | { |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 743 | return key_sign(key, sigp, lenp, data, datalen); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 746 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 747 | userauth_pubkey_agent(Authctxt *authctxt) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 748 | { |
| 749 | static int called = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 750 | int ret = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 751 | char *comment; |
| 752 | Key *k; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 753 | |
| 754 | if (called == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 755 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) |
| 756 | debug2("userauth_pubkey_agent: no keys at all"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 757 | called = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 758 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 759 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 760 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 761 | debug2("userauth_pubkey_agent: no more keys"); |
| 762 | } else { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 763 | debug("userauth_pubkey_agent: testing agent key %s", comment); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 764 | xfree(comment); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 765 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); |
| 766 | if (ret == 0) |
| 767 | key_free(k); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 768 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 769 | if (ret == 0) |
| 770 | debug2("userauth_pubkey_agent: no message sent"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 771 | return ret; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 772 | } |
| 773 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 774 | int |
| 775 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 776 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 777 | static int idx = 0; |
| 778 | int sent = 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 779 | Key *key; |
| 780 | char *filename; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 781 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 782 | if (authctxt->agent != NULL) { |
| 783 | do { |
| 784 | sent = userauth_pubkey_agent(authctxt); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 785 | } while (!sent && authctxt->agent->howmany > 0); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 786 | } |
| 787 | while (!sent && idx < options.num_identity_files) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 788 | key = options.identity_keys[idx]; |
| 789 | filename = options.identity_files[idx]; |
| 790 | if (key == NULL) { |
| 791 | debug("try privkey: %s", filename); |
| 792 | key = load_identity_file(filename); |
| 793 | if (key != NULL) { |
| 794 | sent = sign_and_send_pubkey(authctxt, key, |
| 795 | key_sign_cb); |
| 796 | key_free(key); |
| 797 | } |
| 798 | } else if (key->type != KEY_RSA1) { |
| 799 | debug("try pubkey: %s", filename); |
| 800 | sent = send_pubkey_test(authctxt, key, |
| 801 | identity_sign_cb, idx); |
| 802 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 803 | idx++; |
| 804 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 805 | return sent; |
| 806 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 807 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 808 | /* |
| 809 | * Send userauth request message specifying keyboard-interactive method. |
| 810 | */ |
| 811 | int |
| 812 | userauth_kbdint(Authctxt *authctxt) |
| 813 | { |
| 814 | static int attempt = 0; |
| 815 | |
| 816 | if (attempt++ >= options.number_of_password_prompts) |
| 817 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 818 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 819 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 820 | debug3("userauth_kbdint: disable: no info_req_seen"); |
| 821 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
| 822 | return 0; |
| 823 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 824 | |
| 825 | debug2("userauth_kbdint"); |
| 826 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 827 | packet_put_cstring(authctxt->server_user); |
| 828 | packet_put_cstring(authctxt->service); |
| 829 | packet_put_cstring(authctxt->method->name); |
| 830 | packet_put_cstring(""); /* lang */ |
| 831 | packet_put_cstring(options.kbd_interactive_devices ? |
| 832 | options.kbd_interactive_devices : ""); |
| 833 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 834 | |
| 835 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 836 | return 1; |
| 837 | } |
| 838 | |
| 839 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 840 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 841 | */ |
| 842 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 843 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 844 | { |
| 845 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 846 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 847 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 848 | int echo = 0; |
| 849 | |
| 850 | debug2("input_userauth_info_req"); |
| 851 | |
| 852 | if (authctxt == NULL) |
| 853 | fatal("input_userauth_info_req: no authentication context"); |
| 854 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 855 | authctxt->info_req_seen = 1; |
| 856 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 857 | name = packet_get_string(NULL); |
| 858 | inst = packet_get_string(NULL); |
| 859 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 860 | if (strlen(name) > 0) |
Ben Lindstrom | 126c56a | 2001-06-25 05:22:53 +0000 | [diff] [blame] | 861 | log("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 862 | if (strlen(inst) > 0) |
Ben Lindstrom | 126c56a | 2001-06-25 05:22:53 +0000 | [diff] [blame] | 863 | log("%s", inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 864 | xfree(name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 865 | xfree(inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 866 | xfree(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 867 | |
| 868 | num_prompts = packet_get_int(); |
| 869 | /* |
| 870 | * Begin to build info response packet based on prompts requested. |
| 871 | * We commit to providing the correct number of responses, so if |
| 872 | * further on we run into a problem that prevents this, we have to |
| 873 | * be sure and clean this up and send a correct error response. |
| 874 | */ |
| 875 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 876 | packet_put_int(num_prompts); |
| 877 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 878 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 879 | for (i = 0; i < num_prompts; i++) { |
| 880 | prompt = packet_get_string(NULL); |
| 881 | echo = packet_get_char(); |
| 882 | |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 883 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 884 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 885 | packet_put_cstring(response); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 886 | memset(response, 0, strlen(response)); |
| 887 | xfree(response); |
| 888 | xfree(prompt); |
| 889 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 890 | packet_check_eom(); /* done with parsing incoming message. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 891 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 892 | packet_add_padding(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 893 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 894 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 895 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 896 | static int |
| 897 | ssh_keysign( |
| 898 | Key *key, |
| 899 | u_char **sigp, u_int *lenp, |
| 900 | u_char *data, u_int datalen) |
| 901 | { |
| 902 | Buffer b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 903 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 904 | pid_t pid; |
| 905 | int to[2], from[2], status, version = 1; |
| 906 | |
| 907 | debug("ssh_keysign called"); |
| 908 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 909 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
| 910 | error("ssh_keysign: no installed: %s", strerror(errno)); |
| 911 | return -1; |
| 912 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 913 | if (fflush(stdout) != 0) |
| 914 | error("ssh_keysign: fflush: %s", strerror(errno)); |
| 915 | if (pipe(to) < 0) { |
| 916 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 917 | return -1; |
| 918 | } |
| 919 | if (pipe(from) < 0) { |
| 920 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 921 | return -1; |
| 922 | } |
| 923 | if ((pid = fork()) < 0) { |
| 924 | error("ssh_keysign: fork: %s", strerror(errno)); |
| 925 | return -1; |
| 926 | } |
| 927 | if (pid == 0) { |
| 928 | seteuid(getuid()); |
| 929 | setuid(getuid()); |
| 930 | close(from[0]); |
| 931 | if (dup2(from[1], STDOUT_FILENO) < 0) |
| 932 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
| 933 | close(to[1]); |
| 934 | if (dup2(to[0], STDIN_FILENO) < 0) |
| 935 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
Ben Lindstrom | 4887da2 | 2002-06-06 20:05:57 +0000 | [diff] [blame] | 936 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 937 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, |
| 938 | strerror(errno)); |
| 939 | } |
| 940 | close(from[1]); |
| 941 | close(to[0]); |
| 942 | |
| 943 | buffer_init(&b); |
| 944 | buffer_put_string(&b, data, datalen); |
| 945 | msg_send(to[1], version, &b); |
| 946 | |
| 947 | if (msg_recv(from[0], &b) < 0) { |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 948 | error("ssh_keysign: no reply"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 949 | buffer_clear(&b); |
| 950 | return -1; |
| 951 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 952 | close(from[0]); |
| 953 | close(to[1]); |
| 954 | |
| 955 | while (waitpid(pid, &status, 0) < 0) |
| 956 | if (errno != EINTR) |
| 957 | break; |
| 958 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 959 | if (buffer_get_char(&b) != version) { |
| 960 | error("ssh_keysign: bad version"); |
| 961 | buffer_clear(&b); |
| 962 | return -1; |
| 963 | } |
| 964 | *sigp = buffer_get_string(&b, lenp); |
| 965 | buffer_clear(&b); |
| 966 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 970 | int |
| 971 | userauth_hostbased(Authctxt *authctxt) |
| 972 | { |
| 973 | Key *private = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 974 | Sensitive *sensitive = authctxt->sensitive; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 975 | Buffer b; |
| 976 | u_char *signature, *blob; |
| 977 | char *chost, *pkalg, *p; |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 978 | const char *service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 979 | u_int blen, slen; |
Ben Lindstrom | 2bffd6f | 2001-04-19 20:35:40 +0000 | [diff] [blame] | 980 | int ok, i, len, found = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 981 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 982 | /* check for a useful key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 983 | for (i = 0; i < sensitive->nkeys; i++) { |
| 984 | private = sensitive->keys[i]; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 985 | if (private && private->type != KEY_RSA1) { |
| 986 | found = 1; |
| 987 | /* we take and free the key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 988 | sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 989 | break; |
| 990 | } |
| 991 | } |
| 992 | if (!found) { |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 993 | debug("userauth_hostbased: no more client hostkeys"); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | if (key_to_blob(private, &blob, &blen) == 0) { |
| 997 | key_free(private); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 998 | return 0; |
| 999 | } |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1000 | /* figure out a name for the client host */ |
| 1001 | p = get_local_name(packet_get_connection_in()); |
| 1002 | if (p == NULL) { |
| 1003 | error("userauth_hostbased: cannot get local ipaddr/name"); |
| 1004 | key_free(private); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | len = strlen(p) + 2; |
| 1008 | chost = xmalloc(len); |
| 1009 | strlcpy(chost, p, len); |
| 1010 | strlcat(chost, ".", len); |
| 1011 | debug2("userauth_hostbased: chost %s", chost); |
| 1012 | |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1013 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 1014 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1015 | pkalg = xstrdup(key_ssh_name(private)); |
| 1016 | buffer_init(&b); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1017 | /* construct data */ |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1018 | buffer_put_string(&b, session_id2, session_id2_len); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1019 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 1020 | buffer_put_cstring(&b, authctxt->server_user); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1021 | buffer_put_cstring(&b, service); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1022 | buffer_put_cstring(&b, authctxt->method->name); |
| 1023 | buffer_put_cstring(&b, pkalg); |
| 1024 | buffer_put_string(&b, blob, blen); |
| 1025 | buffer_put_cstring(&b, chost); |
| 1026 | buffer_put_cstring(&b, authctxt->local_user); |
| 1027 | #ifdef DEBUG_PK |
| 1028 | buffer_dump(&b); |
| 1029 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1030 | if (sensitive->external_keysign) |
| 1031 | ok = ssh_keysign(private, &signature, &slen, |
| 1032 | buffer_ptr(&b), buffer_len(&b)); |
| 1033 | else |
| 1034 | ok = key_sign(private, &signature, &slen, |
| 1035 | buffer_ptr(&b), buffer_len(&b)); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1036 | key_free(private); |
| 1037 | buffer_free(&b); |
| 1038 | if (ok != 0) { |
| 1039 | error("key_sign failed"); |
| 1040 | xfree(chost); |
| 1041 | xfree(pkalg); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1045 | packet_put_cstring(authctxt->server_user); |
| 1046 | packet_put_cstring(authctxt->service); |
| 1047 | packet_put_cstring(authctxt->method->name); |
| 1048 | packet_put_cstring(pkalg); |
| 1049 | packet_put_string(blob, blen); |
| 1050 | packet_put_cstring(chost); |
| 1051 | packet_put_cstring(authctxt->local_user); |
| 1052 | packet_put_string(signature, slen); |
| 1053 | memset(signature, 's', slen); |
| 1054 | xfree(signature); |
| 1055 | xfree(chost); |
| 1056 | xfree(pkalg); |
| 1057 | |
| 1058 | packet_send(); |
| 1059 | return 1; |
| 1060 | } |
| 1061 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1062 | /* find auth method */ |
| 1063 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1064 | /* |
| 1065 | * given auth method name, if configurable options permit this method fill |
| 1066 | * in auth_ident field and return true, otherwise return false. |
| 1067 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1068 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1069 | authmethod_is_enabled(Authmethod *method) |
| 1070 | { |
| 1071 | if (method == NULL) |
| 1072 | return 0; |
| 1073 | /* return false if options indicate this method is disabled */ |
| 1074 | if (method->enabled == NULL || *method->enabled == 0) |
| 1075 | return 0; |
| 1076 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1077 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1078 | return 0; |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1082 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1083 | authmethod_lookup(const char *name) |
| 1084 | { |
| 1085 | Authmethod *method = NULL; |
| 1086 | if (name != NULL) |
| 1087 | for (method = authmethods; method->name != NULL; method++) |
| 1088 | if (strcmp(name, method->name) == 0) |
| 1089 | return method; |
| 1090 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1094 | /* XXX internal state */ |
| 1095 | static Authmethod *current = NULL; |
| 1096 | static char *supported = NULL; |
| 1097 | static char *preferred = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1098 | /* |
| 1099 | * Given the authentication method list sent by the server, return the |
| 1100 | * next method we should try. If the server initially sends a nil list, |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1101 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1102 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1103 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1104 | authmethod_get(char *authlist) |
| 1105 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1106 | |
| 1107 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1108 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1109 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1110 | /* Use a suitable default if we're passed a nil list. */ |
| 1111 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1112 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1113 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1114 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1115 | debug3("start over, passed a different list %s", authlist); |
| 1116 | if (supported != NULL) |
| 1117 | xfree(supported); |
| 1118 | supported = xstrdup(authlist); |
| 1119 | preferred = options.preferred_authentications; |
| 1120 | debug3("preferred %s", preferred); |
| 1121 | current = NULL; |
| 1122 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1123 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1124 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1125 | for (;;) { |
| 1126 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
| 1127 | debug("no more auth methods to try"); |
| 1128 | current = NULL; |
| 1129 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1130 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1131 | preferred += next; |
| 1132 | debug3("authmethod_lookup %s", name); |
| 1133 | debug3("remaining preferred: %s", preferred); |
| 1134 | if ((current = authmethod_lookup(name)) != NULL && |
| 1135 | authmethod_is_enabled(current)) { |
| 1136 | debug3("authmethod_is_enabled %s", name); |
| 1137 | debug("next auth method to try is %s", name); |
| 1138 | return current; |
| 1139 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1140 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1141 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1142 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 1143 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1144 | authmethods_get(void) |
| 1145 | { |
| 1146 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1147 | Buffer b; |
| 1148 | char *list; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1149 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1150 | buffer_init(&b); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1151 | for (method = authmethods; method->name != NULL; method++) { |
| 1152 | if (authmethod_is_enabled(method)) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1153 | if (buffer_len(&b) > 0) |
| 1154 | buffer_append(&b, ",", 1); |
| 1155 | buffer_append(&b, method->name, strlen(method->name)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1156 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1157 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1158 | buffer_append(&b, "\0", 1); |
| 1159 | list = xstrdup(buffer_ptr(&b)); |
| 1160 | buffer_free(&b); |
| 1161 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1162 | } |