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