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