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