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