Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 1 | /* $OpenBSD: sshconnect2.c,v 1.157 2006/07/20 15:26:15 stevesk Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "includes.h" |
Damien Miller | 2eb6340 | 2006-03-15 11:09:42 +1100 | [diff] [blame] | 27 | |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 30 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 31 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 32 | #include <errno.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 33 | #include <unistd.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 34 | |
Damien Miller | 9c61769 | 2003-05-14 14:31:11 +1000 | [diff] [blame] | 35 | #include "openbsd-compat/sys-queue.h" |
| 36 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 40 | #include "buffer.h" |
| 41 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include "compat.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 43 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 44 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 45 | #include "kex.h" |
| 46 | #include "myproposal.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 47 | #include "sshconnect.h" |
| 48 | #include "authfile.h" |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 49 | #include "dh.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 50 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "log.h" |
| 52 | #include "readconf.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 53 | #include "misc.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 54 | #include "match.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 55 | #include "dispatch.h" |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 56 | #include "canohost.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 57 | #include "msg.h" |
| 58 | #include "pathnames.h" |
Damien Miller | 6b4069a | 2006-06-13 13:05:15 +1000 | [diff] [blame] | 59 | #include "uidswap.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 60 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 61 | #ifdef GSSAPI |
| 62 | #include "ssh-gss.h" |
| 63 | #endif |
| 64 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 65 | /* import */ |
| 66 | extern char *client_version_string; |
| 67 | extern char *server_version_string; |
| 68 | extern Options options; |
| 69 | |
| 70 | /* |
| 71 | * SSH2 key exchange |
| 72 | */ |
| 73 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 74 | u_char *session_id2 = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 75 | u_int session_id2_len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 76 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 77 | char *xxx_host; |
| 78 | struct sockaddr *xxx_hostaddr; |
| 79 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 80 | Kex *xxx_kex = NULL; |
| 81 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 82 | static int |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 83 | verify_host_key_callback(Key *hostkey) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 84 | { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 85 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 86 | fatal("Host key verification failed."); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 90 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 91 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
| 92 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 93 | Kex *kex; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 94 | |
| 95 | xxx_host = host; |
| 96 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 97 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 98 | if (options.ciphers == (char *)-1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 99 | logit("No valid ciphers for protocol version 2 given, using defaults."); |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 100 | options.ciphers = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 101 | } |
| 102 | if (options.ciphers != NULL) { |
| 103 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 104 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 105 | } |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 106 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 107 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); |
| 108 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
| 109 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 110 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 111 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 112 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 113 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 114 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 115 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 116 | } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 117 | if (options.macs != NULL) { |
| 118 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 119 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 120 | } |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 121 | if (options.hostkeyalgorithms != NULL) |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 122 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 123 | options.hostkeyalgorithms; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 124 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 125 | if (options.rekey_limit) |
| 126 | packet_set_rekey_limit(options.rekey_limit); |
| 127 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 128 | /* start key exchange */ |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 129 | kex = kex_setup(myproposal); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 130 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 131 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 132 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 133 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 134 | kex->client_version_string=client_version_string; |
| 135 | kex->server_version_string=server_version_string; |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 136 | kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 137 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 138 | xxx_kex = kex; |
| 139 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 140 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 141 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 142 | session_id2 = kex->session_id; |
| 143 | session_id2_len = kex->session_id_len; |
| 144 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 145 | #ifdef DEBUG_KEXDH |
| 146 | /* send 1st encrypted/maced/compressed message */ |
| 147 | packet_start(SSH2_MSG_IGNORE); |
| 148 | packet_put_cstring("markus"); |
| 149 | packet_send(); |
| 150 | packet_write_wait(); |
| 151 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 152 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 153 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 154 | /* |
| 155 | * Authenticate user |
| 156 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 157 | |
| 158 | typedef struct Authctxt Authctxt; |
| 159 | typedef struct Authmethod Authmethod; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 160 | typedef struct identity Identity; |
| 161 | typedef struct idlist Idlist; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 162 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 163 | struct identity { |
| 164 | TAILQ_ENTRY(identity) next; |
| 165 | AuthenticationConnection *ac; /* set if agent supports key */ |
| 166 | Key *key; /* public/private key */ |
| 167 | char *filename; /* comment for agent-only keys */ |
| 168 | int tried; |
| 169 | int isprivate; /* key points to the private key */ |
| 170 | }; |
| 171 | TAILQ_HEAD(idlist, identity); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 172 | |
| 173 | struct Authctxt { |
| 174 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 175 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 176 | const char *host; |
| 177 | const char *service; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 178 | Authmethod *method; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 179 | int success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 180 | char *authlist; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 181 | /* pubkey */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 182 | Idlist keys; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 183 | AuthenticationConnection *agent; |
| 184 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 185 | Sensitive *sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 186 | /* kbd-interactive */ |
| 187 | int info_req_seen; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 188 | /* generic */ |
| 189 | void *methoddata; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 190 | }; |
| 191 | struct Authmethod { |
| 192 | char *name; /* string to compare against server's list */ |
| 193 | int (*userauth)(Authctxt *authctxt); |
| 194 | int *enabled; /* flag in option struct that enables method */ |
| 195 | int *batch_flag; /* flag in option struct that disables method */ |
| 196 | }; |
| 197 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 198 | void input_userauth_success(int, u_int32_t, void *); |
| 199 | void input_userauth_failure(int, u_int32_t, void *); |
| 200 | void input_userauth_banner(int, u_int32_t, void *); |
| 201 | void input_userauth_error(int, u_int32_t, void *); |
| 202 | void input_userauth_info_req(int, u_int32_t, void *); |
| 203 | void input_userauth_pk_ok(int, u_int32_t, void *); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 204 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 205 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 206 | int userauth_none(Authctxt *); |
| 207 | int userauth_pubkey(Authctxt *); |
| 208 | int userauth_passwd(Authctxt *); |
| 209 | int userauth_kbdint(Authctxt *); |
| 210 | int userauth_hostbased(Authctxt *); |
Damien Miller | 3ab496b | 2003-05-14 13:47:37 +1000 | [diff] [blame] | 211 | int userauth_kerberos(Authctxt *); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 212 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 213 | #ifdef GSSAPI |
| 214 | int userauth_gssapi(Authctxt *authctxt); |
| 215 | void input_gssapi_response(int type, u_int32_t, void *); |
| 216 | void input_gssapi_token(int type, u_int32_t, void *); |
| 217 | void input_gssapi_hash(int type, u_int32_t, void *); |
| 218 | void input_gssapi_error(int, u_int32_t, void *); |
| 219 | void input_gssapi_errtok(int, u_int32_t, void *); |
| 220 | #endif |
| 221 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 222 | void userauth(Authctxt *, char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 223 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 224 | static int sign_and_send_pubkey(Authctxt *, Identity *); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 225 | static void pubkey_prepare(Authctxt *); |
| 226 | static void pubkey_cleanup(Authctxt *); |
| 227 | static Key *load_identity_file(char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 228 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 229 | static Authmethod *authmethod_get(char *authlist); |
| 230 | static Authmethod *authmethod_lookup(const char *name); |
| 231 | static char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 232 | |
| 233 | Authmethod authmethods[] = { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 234 | #ifdef GSSAPI |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 235 | {"gssapi-with-mic", |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 236 | userauth_gssapi, |
| 237 | &options.gss_authentication, |
| 238 | NULL}, |
| 239 | #endif |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 240 | {"hostbased", |
| 241 | userauth_hostbased, |
| 242 | &options.hostbased_authentication, |
| 243 | NULL}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 244 | {"publickey", |
| 245 | userauth_pubkey, |
| 246 | &options.pubkey_authentication, |
| 247 | NULL}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 248 | {"keyboard-interactive", |
| 249 | userauth_kbdint, |
| 250 | &options.kbd_interactive_authentication, |
| 251 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 252 | {"password", |
| 253 | userauth_passwd, |
| 254 | &options.password_authentication, |
| 255 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 256 | {"none", |
| 257 | userauth_none, |
| 258 | NULL, |
| 259 | NULL}, |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 260 | {NULL, NULL, NULL, NULL} |
| 261 | }; |
| 262 | |
| 263 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 264 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 265 | Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 266 | { |
| 267 | Authctxt authctxt; |
| 268 | int type; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 269 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 270 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 271 | options.kbd_interactive_authentication = 1; |
| 272 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 273 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 274 | packet_put_cstring("ssh-userauth"); |
| 275 | packet_send(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 276 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 277 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 278 | type = packet_read(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 279 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
| 280 | fatal("Server denied authentication request: %d", type); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 281 | if (packet_remaining() > 0) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 282 | char *reply = packet_get_string(NULL); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 283 | debug2("service_accept: %s", reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 284 | xfree(reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 285 | } else { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 286 | debug2("buggy server: service_accept w/o service"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 287 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 288 | packet_check_eom(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 289 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 290 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 291 | if (options.preferred_authentications == NULL) |
| 292 | options.preferred_authentications = authmethods_get(); |
| 293 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 294 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 295 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 296 | pubkey_prepare(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 297 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 298 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 299 | authctxt.host = host; |
| 300 | authctxt.service = "ssh-connection"; /* service name */ |
| 301 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 302 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 303 | authctxt.authlist = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 304 | authctxt.methoddata = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 305 | authctxt.sensitive = sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 306 | authctxt.info_req_seen = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 307 | if (authctxt.method == NULL) |
| 308 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 309 | |
| 310 | /* initial userauth request */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 311 | userauth_none(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 312 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 313 | dispatch_init(&input_userauth_error); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 314 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 315 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 316 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 317 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
| 318 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 319 | pubkey_cleanup(&authctxt); |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 320 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); |
| 321 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 322 | debug("Authentication succeeded (%s).", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 323 | } |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 324 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 325 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 326 | userauth(Authctxt *authctxt, char *authlist) |
| 327 | { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 328 | if (authctxt->methoddata) { |
| 329 | xfree(authctxt->methoddata); |
| 330 | authctxt->methoddata = NULL; |
| 331 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 332 | if (authlist == NULL) { |
| 333 | authlist = authctxt->authlist; |
| 334 | } else { |
| 335 | if (authctxt->authlist) |
| 336 | xfree(authctxt->authlist); |
| 337 | authctxt->authlist = authlist; |
| 338 | } |
| 339 | for (;;) { |
| 340 | Authmethod *method = authmethod_get(authlist); |
| 341 | if (method == NULL) |
| 342 | fatal("Permission denied (%s).", authlist); |
| 343 | authctxt->method = method; |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 344 | |
| 345 | /* reset the per method handler */ |
| 346 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, |
| 347 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); |
| 348 | |
| 349 | /* and try new method */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 350 | if (method->userauth(authctxt) != 0) { |
| 351 | debug2("we sent a %s packet, wait for reply", method->name); |
| 352 | break; |
| 353 | } else { |
| 354 | debug2("we did not send a packet, disable method"); |
| 355 | method->enabled = NULL; |
| 356 | } |
| 357 | } |
| 358 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 359 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 360 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 361 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 362 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 363 | fatal("input_userauth_error: bad message during authentication: " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 364 | "type %d", type); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 365 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 366 | |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 367 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 368 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 369 | { |
| 370 | char *msg, *lang; |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 371 | |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 372 | debug3("input_userauth_banner"); |
| 373 | msg = packet_get_string(NULL); |
| 374 | lang = packet_get_string(NULL); |
Darren Tucker | 046dff2 | 2003-10-08 17:32:02 +1000 | [diff] [blame] | 375 | if (options.log_level > SYSLOG_LEVEL_QUIET) |
| 376 | fprintf(stderr, "%s", msg); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 377 | xfree(msg); |
| 378 | xfree(lang); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 379 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 380 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 381 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 382 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 383 | { |
| 384 | Authctxt *authctxt = ctxt; |
| 385 | if (authctxt == NULL) |
| 386 | fatal("input_userauth_success: no authentication context"); |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 387 | if (authctxt->authlist) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 388 | xfree(authctxt->authlist); |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 389 | authctxt->authlist = NULL; |
| 390 | } |
| 391 | if (authctxt->methoddata) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 392 | xfree(authctxt->methoddata); |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 393 | authctxt->methoddata = NULL; |
| 394 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 395 | authctxt->success = 1; /* break out */ |
| 396 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 397 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 398 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 399 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 400 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 401 | Authctxt *authctxt = ctxt; |
| 402 | char *authlist = NULL; |
| 403 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 404 | |
| 405 | if (authctxt == NULL) |
| 406 | fatal("input_userauth_failure: no authentication context"); |
| 407 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 408 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 409 | partial = packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 410 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 411 | |
| 412 | if (partial != 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 413 | logit("Authenticated with partial success."); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 414 | debug("Authentications that can continue: %s", authlist); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 415 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 416 | userauth(authctxt, authlist); |
| 417 | } |
| 418 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 419 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 420 | { |
| 421 | Authctxt *authctxt = ctxt; |
| 422 | Key *key = NULL; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 423 | Identity *id = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 424 | Buffer b; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 425 | int pktype, sent = 0; |
| 426 | u_int alen, blen; |
| 427 | char *pkalg, *fp; |
| 428 | u_char *pkblob; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 429 | |
| 430 | if (authctxt == NULL) |
| 431 | fatal("input_userauth_pk_ok: no authentication context"); |
| 432 | if (datafellows & SSH_BUG_PKOK) { |
| 433 | /* this is similar to SSH_BUG_PKAUTH */ |
| 434 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 435 | pkblob = packet_get_string(&blen); |
| 436 | buffer_init(&b); |
| 437 | buffer_append(&b, pkblob, blen); |
| 438 | pkalg = buffer_get_string(&b, &alen); |
| 439 | buffer_free(&b); |
| 440 | } else { |
| 441 | pkalg = packet_get_string(&alen); |
| 442 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 443 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 444 | packet_check_eom(); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 445 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 446 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 447 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 448 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
| 449 | debug("unknown pkalg %s", pkalg); |
| 450 | goto done; |
| 451 | } |
| 452 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 453 | debug("no key from blob. pkalg %s", pkalg); |
| 454 | goto done; |
| 455 | } |
| 456 | if (key->type != pktype) { |
| 457 | error("input_userauth_pk_ok: type mismatch " |
| 458 | "for decoded key (received %d, expected %d)", |
| 459 | key->type, pktype); |
| 460 | goto done; |
| 461 | } |
| 462 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 463 | debug2("input_userauth_pk_ok: fp %s", fp); |
| 464 | xfree(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 465 | |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 466 | /* |
| 467 | * search keys in the reverse order, because last candidate has been |
| 468 | * moved to the end of the queue. this also avoids confusion by |
| 469 | * duplicate keys |
| 470 | */ |
Damien Miller | 0b51a52 | 2004-04-20 20:07:19 +1000 | [diff] [blame] | 471 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 472 | if (key_equal(key, id->key)) { |
| 473 | sent = sign_and_send_pubkey(authctxt, id); |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | done: |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 478 | if (key != NULL) |
| 479 | key_free(key); |
| 480 | xfree(pkalg); |
| 481 | xfree(pkblob); |
| 482 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 483 | /* try another method if we did not send a packet */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 484 | if (sent == 0) |
| 485 | userauth(authctxt, NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 486 | } |
| 487 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 488 | #ifdef GSSAPI |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 489 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 490 | userauth_gssapi(Authctxt *authctxt) |
| 491 | { |
| 492 | Gssctxt *gssctxt = NULL; |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 493 | static gss_OID_set gss_supported = NULL; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 494 | static u_int mech = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 495 | OM_uint32 min; |
| 496 | int ok = 0; |
| 497 | |
| 498 | /* Try one GSSAPI method at a time, rather than sending them all at |
| 499 | * once. */ |
| 500 | |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 501 | if (gss_supported == NULL) |
| 502 | gss_indicate_mechs(&min, &gss_supported); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 503 | |
| 504 | /* Check to see if the mechanism is usable before we offer it */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 505 | while (mech < gss_supported->count && !ok) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 506 | if (gssctxt) |
| 507 | ssh_gssapi_delete_ctx(&gssctxt); |
| 508 | ssh_gssapi_build_ctx(&gssctxt); |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 509 | ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 510 | |
| 511 | /* My DER encoding requires length<128 */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 512 | if (gss_supported->elements[mech].length < 128 && |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 513 | !GSS_ERROR(ssh_gssapi_import_name(gssctxt, |
| 514 | authctxt->host))) { |
| 515 | ok = 1; /* Mechanism works */ |
| 516 | } else { |
| 517 | mech++; |
| 518 | } |
| 519 | } |
| 520 | |
Damien Miller | 5790b59 | 2006-03-26 13:54:03 +1100 | [diff] [blame] | 521 | if (!ok) { |
| 522 | ssh_gssapi_delete_ctx(&gssctxt); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 523 | return 0; |
Damien Miller | 5790b59 | 2006-03-26 13:54:03 +1100 | [diff] [blame] | 524 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 525 | |
| 526 | authctxt->methoddata=(void *)gssctxt; |
| 527 | |
| 528 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 529 | packet_put_cstring(authctxt->server_user); |
| 530 | packet_put_cstring(authctxt->service); |
| 531 | packet_put_cstring(authctxt->method->name); |
| 532 | |
| 533 | packet_put_int(1); |
| 534 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 535 | packet_put_int((gss_supported->elements[mech].length) + 2); |
| 536 | packet_put_char(SSH_GSS_OIDTYPE); |
| 537 | packet_put_char(gss_supported->elements[mech].length); |
| 538 | packet_put_raw(gss_supported->elements[mech].elements, |
| 539 | gss_supported->elements[mech].length); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 540 | |
| 541 | packet_send(); |
| 542 | |
| 543 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); |
| 544 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); |
| 545 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); |
| 546 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); |
| 547 | |
| 548 | mech++; /* Move along to next candidate */ |
| 549 | |
| 550 | return 1; |
| 551 | } |
| 552 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 553 | static OM_uint32 |
| 554 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) |
| 555 | { |
| 556 | Authctxt *authctxt = ctxt; |
| 557 | Gssctxt *gssctxt = authctxt->methoddata; |
| 558 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
Damien Miller | da9984f | 2005-08-31 19:46:26 +1000 | [diff] [blame] | 559 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
| 560 | gss_buffer_desc gssbuf; |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 561 | OM_uint32 status, ms, flags; |
| 562 | Buffer b; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 563 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 564 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 565 | recv_tok, &send_tok, &flags); |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 566 | |
| 567 | if (send_tok.length > 0) { |
| 568 | if (GSS_ERROR(status)) |
| 569 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); |
| 570 | else |
| 571 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 572 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 573 | packet_put_string(send_tok.value, send_tok.length); |
| 574 | packet_send(); |
| 575 | gss_release_buffer(&ms, &send_tok); |
| 576 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 577 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 578 | if (status == GSS_S_COMPLETE) { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 579 | /* send either complete or MIC, depending on mechanism */ |
| 580 | if (!(flags & GSS_C_INTEG_FLAG)) { |
| 581 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); |
| 582 | packet_send(); |
| 583 | } else { |
| 584 | ssh_gssapi_buildmic(&b, authctxt->server_user, |
| 585 | authctxt->service, "gssapi-with-mic"); |
| 586 | |
| 587 | gssbuf.value = buffer_ptr(&b); |
| 588 | gssbuf.length = buffer_len(&b); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 589 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 590 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 591 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 592 | if (!GSS_ERROR(status)) { |
| 593 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); |
| 594 | packet_put_string(mic.value, mic.length); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 595 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 596 | packet_send(); |
| 597 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 598 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 599 | buffer_free(&b); |
| 600 | gss_release_buffer(&ms, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 601 | } |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 602 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 603 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 604 | return status; |
| 605 | } |
| 606 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 607 | void |
| 608 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
| 609 | { |
| 610 | Authctxt *authctxt = ctxt; |
| 611 | Gssctxt *gssctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 612 | int oidlen; |
| 613 | char *oidv; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 614 | |
| 615 | if (authctxt == NULL) |
| 616 | fatal("input_gssapi_response: no authentication context"); |
| 617 | gssctxt = authctxt->methoddata; |
| 618 | |
| 619 | /* Setup our OID */ |
| 620 | oidv = packet_get_string(&oidlen); |
| 621 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 622 | if (oidlen <= 2 || |
| 623 | oidv[0] != SSH_GSS_OIDTYPE || |
| 624 | oidv[1] != oidlen - 2) { |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 625 | xfree(oidv); |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 626 | debug("Badly encoded mechanism OID received"); |
| 627 | userauth(authctxt, NULL); |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 628 | return; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 629 | } |
| 630 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 631 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
| 632 | fatal("Server returned different OID than expected"); |
| 633 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 634 | packet_check_eom(); |
| 635 | |
| 636 | xfree(oidv); |
| 637 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 638 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 639 | /* Start again with next method on list */ |
| 640 | debug("Trying to start again"); |
| 641 | userauth(authctxt, NULL); |
| 642 | return; |
| 643 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void |
| 647 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
| 648 | { |
| 649 | Authctxt *authctxt = ctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 650 | gss_buffer_desc recv_tok; |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 651 | OM_uint32 status; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 652 | u_int slen; |
| 653 | |
| 654 | if (authctxt == NULL) |
| 655 | fatal("input_gssapi_response: no authentication context"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 656 | |
| 657 | recv_tok.value = packet_get_string(&slen); |
| 658 | recv_tok.length = slen; /* safe typecast */ |
| 659 | |
| 660 | packet_check_eom(); |
| 661 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 662 | status = process_gssapi_token(ctxt, &recv_tok); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 663 | |
| 664 | xfree(recv_tok.value); |
| 665 | |
| 666 | if (GSS_ERROR(status)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 667 | /* Start again with the next method in the list */ |
| 668 | userauth(authctxt, NULL); |
| 669 | return; |
| 670 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void |
| 674 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) |
| 675 | { |
| 676 | Authctxt *authctxt = ctxt; |
| 677 | Gssctxt *gssctxt; |
| 678 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 679 | gss_buffer_desc recv_tok; |
| 680 | OM_uint32 status, ms; |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 681 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 682 | |
| 683 | if (authctxt == NULL) |
| 684 | fatal("input_gssapi_response: no authentication context"); |
| 685 | gssctxt = authctxt->methoddata; |
| 686 | |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 687 | recv_tok.value = packet_get_string(&len); |
| 688 | recv_tok.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 689 | |
| 690 | packet_check_eom(); |
| 691 | |
| 692 | /* Stick it into GSSAPI and see what it says */ |
| 693 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 694 | &recv_tok, &send_tok, NULL); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 695 | |
| 696 | xfree(recv_tok.value); |
| 697 | gss_release_buffer(&ms, &send_tok); |
| 698 | |
| 699 | /* Server will be returning a failed packet after this one */ |
| 700 | } |
| 701 | |
| 702 | void |
| 703 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) |
| 704 | { |
| 705 | OM_uint32 maj, min; |
| 706 | char *msg; |
| 707 | char *lang; |
| 708 | |
| 709 | maj=packet_get_int(); |
| 710 | min=packet_get_int(); |
| 711 | msg=packet_get_string(NULL); |
| 712 | lang=packet_get_string(NULL); |
| 713 | |
| 714 | packet_check_eom(); |
| 715 | |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 716 | debug("Server GSSAPI Error:\n%s", msg); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 717 | xfree(msg); |
| 718 | xfree(lang); |
| 719 | } |
| 720 | #endif /* GSSAPI */ |
| 721 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 722 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 723 | userauth_none(Authctxt *authctxt) |
| 724 | { |
| 725 | /* initial userauth request */ |
| 726 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 727 | packet_put_cstring(authctxt->server_user); |
| 728 | packet_put_cstring(authctxt->service); |
| 729 | packet_put_cstring(authctxt->method->name); |
| 730 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 735 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 736 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 737 | static int attempt = 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 738 | char prompt[150]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 739 | char *password; |
| 740 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 741 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 742 | return 0; |
| 743 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 744 | if (attempt != 1) |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 745 | error("Permission denied, please try again."); |
| 746 | |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 747 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 748 | authctxt->server_user, authctxt->host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 749 | password = read_passphrase(prompt, 0); |
| 750 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 751 | packet_put_cstring(authctxt->server_user); |
| 752 | packet_put_cstring(authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 753 | packet_put_cstring(authctxt->method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 754 | packet_put_char(0); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 755 | packet_put_cstring(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 756 | memset(password, 0, strlen(password)); |
| 757 | xfree(password); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 758 | packet_add_padding(64); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 759 | packet_send(); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 760 | |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 761 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 762 | &input_userauth_passwd_changereq); |
| 763 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 764 | return 1; |
| 765 | } |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 766 | /* |
| 767 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
| 768 | */ |
| 769 | void |
Tim Rice | c854962 | 2002-03-31 12:49:38 -0800 | [diff] [blame] | 770 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 771 | { |
| 772 | Authctxt *authctxt = ctxt; |
| 773 | char *info, *lang, *password = NULL, *retype = NULL; |
| 774 | char prompt[150]; |
| 775 | |
| 776 | debug2("input_userauth_passwd_changereq"); |
| 777 | |
| 778 | if (authctxt == NULL) |
| 779 | fatal("input_userauth_passwd_changereq: " |
| 780 | "no authentication context"); |
| 781 | |
| 782 | info = packet_get_string(NULL); |
| 783 | lang = packet_get_string(NULL); |
| 784 | if (strlen(info) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 785 | logit("%s", info); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 786 | xfree(info); |
| 787 | xfree(lang); |
| 788 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 789 | packet_put_cstring(authctxt->server_user); |
| 790 | packet_put_cstring(authctxt->service); |
| 791 | packet_put_cstring(authctxt->method->name); |
| 792 | packet_put_char(1); /* additional info */ |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 793 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 794 | "Enter %.30s@%.128s's old password: ", |
| 795 | authctxt->server_user, authctxt->host); |
| 796 | password = read_passphrase(prompt, 0); |
| 797 | packet_put_cstring(password); |
| 798 | memset(password, 0, strlen(password)); |
| 799 | xfree(password); |
| 800 | password = NULL; |
| 801 | while (password == NULL) { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 802 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 803 | "Enter %.30s@%.128s's new password: ", |
| 804 | authctxt->server_user, authctxt->host); |
| 805 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
| 806 | if (password == NULL) { |
| 807 | /* bail out */ |
| 808 | return; |
| 809 | } |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 810 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 811 | "Retype %.30s@%.128s's new password: ", |
| 812 | authctxt->server_user, authctxt->host); |
| 813 | retype = read_passphrase(prompt, 0); |
| 814 | if (strcmp(password, retype) != 0) { |
| 815 | memset(password, 0, strlen(password)); |
| 816 | xfree(password); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 817 | logit("Mismatch; try again, EOF to quit."); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 818 | password = NULL; |
| 819 | } |
| 820 | memset(retype, 0, strlen(retype)); |
| 821 | xfree(retype); |
| 822 | } |
| 823 | packet_put_cstring(password); |
| 824 | memset(password, 0, strlen(password)); |
| 825 | xfree(password); |
| 826 | packet_add_padding(64); |
| 827 | packet_send(); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 828 | |
| 829 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 830 | &input_userauth_passwd_changereq); |
| 831 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 832 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 833 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 834 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, |
| 835 | u_char *data, u_int datalen) |
| 836 | { |
| 837 | Key *prv; |
| 838 | int ret; |
| 839 | |
| 840 | /* the agent supports this key */ |
| 841 | if (id->ac) |
| 842 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, |
| 843 | data, datalen)); |
| 844 | /* |
| 845 | * we have already loaded the private key or |
| 846 | * the private key is stored in external hardware |
| 847 | */ |
| 848 | if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) |
| 849 | return (key_sign(id->key, sigp, lenp, data, datalen)); |
| 850 | /* load the private key from the file */ |
| 851 | if ((prv = load_identity_file(id->filename)) == NULL) |
| 852 | return (-1); |
| 853 | ret = key_sign(prv, sigp, lenp, data, datalen); |
| 854 | key_free(prv); |
| 855 | return (ret); |
| 856 | } |
| 857 | |
| 858 | static int |
| 859 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 860 | { |
| 861 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 862 | u_char *blob, *signature; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 863 | u_int bloblen, slen; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 864 | u_int skip = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 865 | int ret = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 866 | int have_sig = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 867 | |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 868 | debug3("sign_and_send_pubkey"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 869 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 870 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 871 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 872 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 873 | return 0; |
| 874 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 875 | /* data to be signed */ |
| 876 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 877 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 878 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 879 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 880 | } else { |
| 881 | buffer_put_string(&b, session_id2, session_id2_len); |
| 882 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 883 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 884 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 885 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 886 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 887 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 888 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 889 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 890 | if (datafellows & SSH_BUG_PKAUTH) { |
| 891 | buffer_put_char(&b, have_sig); |
| 892 | } else { |
| 893 | buffer_put_cstring(&b, authctxt->method->name); |
| 894 | buffer_put_char(&b, have_sig); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 895 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 896 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 897 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 898 | |
| 899 | /* generate signature */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 900 | ret = identity_sign(id, &signature, &slen, |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 901 | buffer_ptr(&b), buffer_len(&b)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 902 | if (ret == -1) { |
| 903 | xfree(blob); |
| 904 | buffer_free(&b); |
| 905 | return 0; |
| 906 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 907 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 908 | buffer_dump(&b); |
| 909 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 910 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 911 | buffer_clear(&b); |
| 912 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 913 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 914 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 915 | buffer_put_cstring(&b, authctxt->server_user); |
| 916 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 917 | buffer_put_cstring(&b, authctxt->method->name); |
| 918 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 919 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 920 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 921 | buffer_put_string(&b, blob, bloblen); |
| 922 | } |
| 923 | xfree(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 924 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 925 | /* append signature */ |
| 926 | buffer_put_string(&b, signature, slen); |
| 927 | xfree(signature); |
| 928 | |
| 929 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 930 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 931 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 932 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 933 | |
| 934 | /* put remaining data from buffer into packet */ |
| 935 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 936 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 937 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 938 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 939 | |
| 940 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 941 | } |
| 942 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 943 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 944 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 945 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 946 | u_char *blob; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 947 | u_int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 948 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 949 | debug3("send_pubkey_test"); |
| 950 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 951 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 952 | /* we cannot handle this key */ |
| 953 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 954 | return 0; |
| 955 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 956 | /* register callback for USERAUTH_PK_OK message */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 957 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 958 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 959 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 960 | packet_put_cstring(authctxt->server_user); |
| 961 | packet_put_cstring(authctxt->service); |
| 962 | packet_put_cstring(authctxt->method->name); |
| 963 | packet_put_char(have_sig); |
| 964 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 965 | packet_put_cstring(key_ssh_name(id->key)); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 966 | packet_put_string(blob, bloblen); |
| 967 | xfree(blob); |
| 968 | packet_send(); |
| 969 | return 1; |
| 970 | } |
| 971 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 972 | static Key * |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 973 | load_identity_file(char *filename) |
| 974 | { |
| 975 | Key *private; |
| 976 | char prompt[300], *passphrase; |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 977 | int perm_ok, quit, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 978 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 979 | |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 980 | if (stat(filename, &st) < 0) { |
| 981 | debug3("no such identity: %s", filename); |
| 982 | return NULL; |
| 983 | } |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 984 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); |
| 985 | if (!perm_ok) |
| 986 | return NULL; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 987 | if (private == NULL) { |
| 988 | if (options.batch_mode) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 989 | return NULL; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 990 | snprintf(prompt, sizeof prompt, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 991 | "Enter passphrase for key '%.100s': ", filename); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 992 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 993 | passphrase = read_passphrase(prompt, 0); |
| 994 | if (strcmp(passphrase, "") != 0) { |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 995 | private = key_load_private_type(KEY_UNSPEC, |
| 996 | filename, passphrase, NULL, NULL); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 997 | quit = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 998 | } else { |
| 999 | debug2("no passphrase given, try next key"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1000 | quit = 1; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1001 | } |
| 1002 | memset(passphrase, 0, strlen(passphrase)); |
| 1003 | xfree(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1004 | if (private != NULL || quit) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1005 | break; |
| 1006 | debug2("bad passphrase given, try again..."); |
| 1007 | } |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1008 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1009 | return private; |
| 1010 | } |
| 1011 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1012 | /* |
| 1013 | * try keys in the following order: |
| 1014 | * 1. agent keys that are found in the config file |
| 1015 | * 2. other agent keys |
| 1016 | * 3. keys that are only listed in the config file |
| 1017 | */ |
| 1018 | static void |
| 1019 | pubkey_prepare(Authctxt *authctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1020 | { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1021 | Identity *id; |
| 1022 | Idlist agent, files, *preferred; |
| 1023 | Key *key; |
| 1024 | AuthenticationConnection *ac; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1025 | char *comment; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1026 | int i, found; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1027 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1028 | TAILQ_INIT(&agent); /* keys from the agent */ |
| 1029 | TAILQ_INIT(&files); /* keys from the config file */ |
| 1030 | preferred = &authctxt->keys; |
| 1031 | TAILQ_INIT(preferred); /* preferred order of keys */ |
| 1032 | |
| 1033 | /* list of keys stored in the filesystem */ |
| 1034 | for (i = 0; i < options.num_identity_files; i++) { |
| 1035 | key = options.identity_keys[i]; |
| 1036 | if (key && key->type == KEY_RSA1) |
| 1037 | continue; |
| 1038 | options.identity_keys[i] = NULL; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1039 | id = xcalloc(1, sizeof(*id)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1040 | id->key = key; |
| 1041 | id->filename = xstrdup(options.identity_files[i]); |
| 1042 | TAILQ_INSERT_TAIL(&files, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1043 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1044 | /* list of keys supported by the agent */ |
| 1045 | if ((ac = ssh_get_authentication_connection())) { |
| 1046 | for (key = ssh_get_first_identity(ac, &comment, 2); |
| 1047 | key != NULL; |
| 1048 | key = ssh_get_next_identity(ac, &comment, 2)) { |
| 1049 | found = 0; |
| 1050 | TAILQ_FOREACH(id, &files, next) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1051 | /* agent keys from the config file are preferred */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1052 | if (key_equal(key, id->key)) { |
| 1053 | key_free(key); |
| 1054 | xfree(comment); |
| 1055 | TAILQ_REMOVE(&files, id, next); |
| 1056 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1057 | id->ac = ac; |
| 1058 | found = 1; |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1062 | if (!found && !options.identities_only) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1063 | id = xcalloc(1, sizeof(*id)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1064 | id->key = key; |
| 1065 | id->filename = comment; |
| 1066 | id->ac = ac; |
| 1067 | TAILQ_INSERT_TAIL(&agent, id, next); |
| 1068 | } |
| 1069 | } |
| 1070 | /* append remaining agent keys */ |
| 1071 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
| 1072 | TAILQ_REMOVE(&agent, id, next); |
| 1073 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1074 | } |
| 1075 | authctxt->agent = ac; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1076 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1077 | /* append remaining keys from the config file */ |
| 1078 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
| 1079 | TAILQ_REMOVE(&files, id, next); |
| 1080 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1081 | } |
| 1082 | TAILQ_FOREACH(id, preferred, next) { |
| 1083 | debug2("key: %s (%p)", id->filename, id->key); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | static void |
| 1088 | pubkey_cleanup(Authctxt *authctxt) |
| 1089 | { |
| 1090 | Identity *id; |
| 1091 | |
| 1092 | if (authctxt->agent != NULL) |
| 1093 | ssh_close_authentication_connection(authctxt->agent); |
| 1094 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
| 1095 | id = TAILQ_FIRST(&authctxt->keys)) { |
| 1096 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1097 | if (id->key) |
| 1098 | key_free(id->key); |
| 1099 | if (id->filename) |
| 1100 | xfree(id->filename); |
| 1101 | xfree(id); |
| 1102 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1103 | } |
| 1104 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1105 | int |
| 1106 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1107 | { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1108 | Identity *id; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1109 | int sent = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1110 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1111 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
| 1112 | if (id->tried++) |
| 1113 | return (0); |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 1114 | /* move key to the end of the queue */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1115 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1116 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); |
| 1117 | /* |
| 1118 | * send a test message if we have the public key. for |
| 1119 | * encrypted keys we cannot do this and have to load the |
| 1120 | * private key instead |
| 1121 | */ |
| 1122 | if (id->key && id->key->type != KEY_RSA1) { |
| 1123 | debug("Offering public key: %s", id->filename); |
| 1124 | sent = send_pubkey_test(authctxt, id); |
| 1125 | } else if (id->key == NULL) { |
| 1126 | debug("Trying private key: %s", id->filename); |
| 1127 | id->key = load_identity_file(id->filename); |
| 1128 | if (id->key != NULL) { |
| 1129 | id->isprivate = 1; |
| 1130 | sent = sign_and_send_pubkey(authctxt, id); |
| 1131 | key_free(id->key); |
| 1132 | id->key = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1133 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1134 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1135 | if (sent) |
| 1136 | return (sent); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1137 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1138 | return (0); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1139 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1140 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1141 | /* |
| 1142 | * Send userauth request message specifying keyboard-interactive method. |
| 1143 | */ |
| 1144 | int |
| 1145 | userauth_kbdint(Authctxt *authctxt) |
| 1146 | { |
| 1147 | static int attempt = 0; |
| 1148 | |
| 1149 | if (attempt++ >= options.number_of_password_prompts) |
| 1150 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1151 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 1152 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 1153 | debug3("userauth_kbdint: disable: no info_req_seen"); |
| 1154 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
| 1155 | return 0; |
| 1156 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1157 | |
| 1158 | debug2("userauth_kbdint"); |
| 1159 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1160 | packet_put_cstring(authctxt->server_user); |
| 1161 | packet_put_cstring(authctxt->service); |
| 1162 | packet_put_cstring(authctxt->method->name); |
| 1163 | packet_put_cstring(""); /* lang */ |
| 1164 | packet_put_cstring(options.kbd_interactive_devices ? |
| 1165 | options.kbd_interactive_devices : ""); |
| 1166 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1167 | |
| 1168 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1173 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1174 | */ |
| 1175 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1176 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1177 | { |
| 1178 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1179 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1180 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1181 | int echo = 0; |
| 1182 | |
| 1183 | debug2("input_userauth_info_req"); |
| 1184 | |
| 1185 | if (authctxt == NULL) |
| 1186 | fatal("input_userauth_info_req: no authentication context"); |
| 1187 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1188 | authctxt->info_req_seen = 1; |
| 1189 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1190 | name = packet_get_string(NULL); |
| 1191 | inst = packet_get_string(NULL); |
| 1192 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1193 | if (strlen(name) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1194 | logit("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1195 | if (strlen(inst) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1196 | logit("%s", inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1197 | xfree(name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1198 | xfree(inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1199 | xfree(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1200 | |
| 1201 | num_prompts = packet_get_int(); |
| 1202 | /* |
| 1203 | * Begin to build info response packet based on prompts requested. |
| 1204 | * We commit to providing the correct number of responses, so if |
| 1205 | * further on we run into a problem that prevents this, we have to |
| 1206 | * be sure and clean this up and send a correct error response. |
| 1207 | */ |
| 1208 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 1209 | packet_put_int(num_prompts); |
| 1210 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1211 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1212 | for (i = 0; i < num_prompts; i++) { |
| 1213 | prompt = packet_get_string(NULL); |
| 1214 | echo = packet_get_char(); |
| 1215 | |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1216 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1217 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1218 | packet_put_cstring(response); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1219 | memset(response, 0, strlen(response)); |
| 1220 | xfree(response); |
| 1221 | xfree(prompt); |
| 1222 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1223 | packet_check_eom(); /* done with parsing incoming message. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1224 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1225 | packet_add_padding(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1226 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1227 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1228 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1229 | static int |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1230 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1231 | u_char *data, u_int datalen) |
| 1232 | { |
| 1233 | Buffer b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1234 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1235 | pid_t pid; |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1236 | int to[2], from[2], status, version = 2; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1237 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1238 | debug2("ssh_keysign called"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1239 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1240 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
| 1241 | error("ssh_keysign: no installed: %s", strerror(errno)); |
| 1242 | return -1; |
| 1243 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1244 | if (fflush(stdout) != 0) |
| 1245 | error("ssh_keysign: fflush: %s", strerror(errno)); |
| 1246 | if (pipe(to) < 0) { |
| 1247 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1248 | return -1; |
| 1249 | } |
| 1250 | if (pipe(from) < 0) { |
| 1251 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | if ((pid = fork()) < 0) { |
| 1255 | error("ssh_keysign: fork: %s", strerror(errno)); |
| 1256 | return -1; |
| 1257 | } |
| 1258 | if (pid == 0) { |
Damien Miller | 2e5fe88 | 2006-06-13 13:10:00 +1000 | [diff] [blame] | 1259 | permanently_drop_suid(getuid()); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1260 | close(from[0]); |
| 1261 | if (dup2(from[1], STDOUT_FILENO) < 0) |
| 1262 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
| 1263 | close(to[1]); |
| 1264 | if (dup2(to[0], STDIN_FILENO) < 0) |
| 1265 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1266 | close(from[1]); |
| 1267 | close(to[0]); |
Ben Lindstrom | 4887da2 | 2002-06-06 20:05:57 +0000 | [diff] [blame] | 1268 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1269 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, |
| 1270 | strerror(errno)); |
| 1271 | } |
| 1272 | close(from[1]); |
| 1273 | close(to[0]); |
| 1274 | |
| 1275 | buffer_init(&b); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1276 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1277 | buffer_put_string(&b, data, datalen); |
Damien Miller | 51bf11f | 2003-11-17 21:20:47 +1100 | [diff] [blame] | 1278 | if (ssh_msg_send(to[1], version, &b) == -1) |
| 1279 | fatal("ssh_keysign: couldn't send request"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1280 | |
Damien Miller | 901119b | 2002-10-04 11:10:04 +1000 | [diff] [blame] | 1281 | if (ssh_msg_recv(from[0], &b) < 0) { |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1282 | error("ssh_keysign: no reply"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1283 | buffer_free(&b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1284 | return -1; |
| 1285 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1286 | close(from[0]); |
| 1287 | close(to[1]); |
| 1288 | |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1289 | while (waitpid(pid, &status, 0) < 0) |
| 1290 | if (errno != EINTR) |
| 1291 | break; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1292 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1293 | if (buffer_get_char(&b) != version) { |
| 1294 | error("ssh_keysign: bad version"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1295 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1296 | return -1; |
| 1297 | } |
| 1298 | *sigp = buffer_get_string(&b, lenp); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1299 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1300 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1301 | return 0; |
| 1302 | } |
| 1303 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1304 | int |
| 1305 | userauth_hostbased(Authctxt *authctxt) |
| 1306 | { |
| 1307 | Key *private = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1308 | Sensitive *sensitive = authctxt->sensitive; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1309 | Buffer b; |
| 1310 | u_char *signature, *blob; |
| 1311 | char *chost, *pkalg, *p; |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1312 | const char *service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1313 | u_int blen, slen; |
Ben Lindstrom | 2bffd6f | 2001-04-19 20:35:40 +0000 | [diff] [blame] | 1314 | int ok, i, len, found = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1315 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1316 | /* check for a useful key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1317 | for (i = 0; i < sensitive->nkeys; i++) { |
| 1318 | private = sensitive->keys[i]; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1319 | if (private && private->type != KEY_RSA1) { |
| 1320 | found = 1; |
| 1321 | /* we take and free the key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1322 | sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | } |
| 1325 | } |
| 1326 | if (!found) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1327 | debug("No more client hostkeys for hostbased authentication."); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | if (key_to_blob(private, &blob, &blen) == 0) { |
| 1331 | key_free(private); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1334 | /* figure out a name for the client host */ |
| 1335 | p = get_local_name(packet_get_connection_in()); |
| 1336 | if (p == NULL) { |
| 1337 | error("userauth_hostbased: cannot get local ipaddr/name"); |
| 1338 | key_free(private); |
Damien Miller | 5790b59 | 2006-03-26 13:54:03 +1100 | [diff] [blame] | 1339 | xfree(blob); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | len = strlen(p) + 2; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1343 | xasprintf(&chost, "%s.", p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1344 | debug2("userauth_hostbased: chost %s", chost); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1345 | xfree(p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1346 | |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1347 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 1348 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1349 | pkalg = xstrdup(key_ssh_name(private)); |
| 1350 | buffer_init(&b); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1351 | /* construct data */ |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1352 | buffer_put_string(&b, session_id2, session_id2_len); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1353 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 1354 | buffer_put_cstring(&b, authctxt->server_user); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1355 | buffer_put_cstring(&b, service); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1356 | buffer_put_cstring(&b, authctxt->method->name); |
| 1357 | buffer_put_cstring(&b, pkalg); |
| 1358 | buffer_put_string(&b, blob, blen); |
| 1359 | buffer_put_cstring(&b, chost); |
| 1360 | buffer_put_cstring(&b, authctxt->local_user); |
| 1361 | #ifdef DEBUG_PK |
| 1362 | buffer_dump(&b); |
| 1363 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1364 | if (sensitive->external_keysign) |
| 1365 | ok = ssh_keysign(private, &signature, &slen, |
| 1366 | buffer_ptr(&b), buffer_len(&b)); |
| 1367 | else |
| 1368 | ok = key_sign(private, &signature, &slen, |
| 1369 | buffer_ptr(&b), buffer_len(&b)); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1370 | key_free(private); |
| 1371 | buffer_free(&b); |
| 1372 | if (ok != 0) { |
| 1373 | error("key_sign failed"); |
| 1374 | xfree(chost); |
| 1375 | xfree(pkalg); |
Damien Miller | 5790b59 | 2006-03-26 13:54:03 +1100 | [diff] [blame] | 1376 | xfree(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1380 | packet_put_cstring(authctxt->server_user); |
| 1381 | packet_put_cstring(authctxt->service); |
| 1382 | packet_put_cstring(authctxt->method->name); |
| 1383 | packet_put_cstring(pkalg); |
| 1384 | packet_put_string(blob, blen); |
| 1385 | packet_put_cstring(chost); |
| 1386 | packet_put_cstring(authctxt->local_user); |
| 1387 | packet_put_string(signature, slen); |
| 1388 | memset(signature, 's', slen); |
| 1389 | xfree(signature); |
| 1390 | xfree(chost); |
| 1391 | xfree(pkalg); |
Damien Miller | 5790b59 | 2006-03-26 13:54:03 +1100 | [diff] [blame] | 1392 | xfree(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1393 | |
| 1394 | packet_send(); |
| 1395 | return 1; |
| 1396 | } |
| 1397 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1398 | /* find auth method */ |
| 1399 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1400 | /* |
| 1401 | * given auth method name, if configurable options permit this method fill |
| 1402 | * in auth_ident field and return true, otherwise return false. |
| 1403 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1404 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1405 | authmethod_is_enabled(Authmethod *method) |
| 1406 | { |
| 1407 | if (method == NULL) |
| 1408 | return 0; |
| 1409 | /* return false if options indicate this method is disabled */ |
| 1410 | if (method->enabled == NULL || *method->enabled == 0) |
| 1411 | return 0; |
| 1412 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1413 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1414 | return 0; |
| 1415 | return 1; |
| 1416 | } |
| 1417 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1418 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1419 | authmethod_lookup(const char *name) |
| 1420 | { |
| 1421 | Authmethod *method = NULL; |
| 1422 | if (name != NULL) |
| 1423 | for (method = authmethods; method->name != NULL; method++) |
| 1424 | if (strcmp(name, method->name) == 0) |
| 1425 | return method; |
| 1426 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1430 | /* XXX internal state */ |
| 1431 | static Authmethod *current = NULL; |
| 1432 | static char *supported = NULL; |
| 1433 | static char *preferred = NULL; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1434 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1435 | /* |
| 1436 | * Given the authentication method list sent by the server, return the |
| 1437 | * 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] | 1438 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1439 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1440 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1441 | authmethod_get(char *authlist) |
| 1442 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1443 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1444 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1445 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1446 | /* Use a suitable default if we're passed a nil list. */ |
| 1447 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1448 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1449 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1450 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1451 | debug3("start over, passed a different list %s", authlist); |
| 1452 | if (supported != NULL) |
| 1453 | xfree(supported); |
| 1454 | supported = xstrdup(authlist); |
| 1455 | preferred = options.preferred_authentications; |
| 1456 | debug3("preferred %s", preferred); |
| 1457 | current = NULL; |
| 1458 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1459 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1460 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1461 | for (;;) { |
| 1462 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1463 | debug("No more authentication methods to try."); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1464 | current = NULL; |
| 1465 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1466 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1467 | preferred += next; |
| 1468 | debug3("authmethod_lookup %s", name); |
| 1469 | debug3("remaining preferred: %s", preferred); |
| 1470 | if ((current = authmethod_lookup(name)) != NULL && |
| 1471 | authmethod_is_enabled(current)) { |
| 1472 | debug3("authmethod_is_enabled %s", name); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1473 | debug("Next authentication method: %s", name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1474 | return current; |
| 1475 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1476 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1477 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1478 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 1479 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1480 | authmethods_get(void) |
| 1481 | { |
| 1482 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1483 | Buffer b; |
| 1484 | char *list; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1485 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1486 | buffer_init(&b); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1487 | for (method = authmethods; method->name != NULL; method++) { |
| 1488 | if (authmethod_is_enabled(method)) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1489 | if (buffer_len(&b) > 0) |
| 1490 | buffer_append(&b, ",", 1); |
| 1491 | buffer_append(&b, method->name, strlen(method->name)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1492 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1493 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1494 | buffer_append(&b, "\0", 1); |
| 1495 | list = xstrdup(buffer_ptr(&b)); |
| 1496 | buffer_free(&b); |
| 1497 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1498 | } |