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