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