blob: 6fe356ccaa9eaf3e3ce0eb790076d764ed2e15df [file] [log] [blame]
Damien Millerd5f62bf2010-09-24 22:11:14 +10001/* $OpenBSD: sshconnect2.c,v 1.185 2010/09/22 05:01:29 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
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 Millereba71ba2000-04-29 23:57:08 +100014 *
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 Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller2e28d862008-07-17 19:15:43 +100043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100065#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000066#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000068#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000069#include "msg.h"
70#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100071#include "uidswap.h"
Damien Millercee85232009-03-06 00:58:22 +110072#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110073#include "jpake.h"
Damien Miller874d77b2000-10-14 16:23:11 +110074
Darren Tucker0efd1552003-08-26 11:49:55 +100075#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78
Damien Millereba71ba2000-04-29 23:57:08 +100079/* import */
80extern char *client_version_string;
81extern char *server_version_string;
82extern Options options;
83
84/*
85 * SSH2 key exchange
86 */
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100089u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100090
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000091char *xxx_host;
92struct sockaddr *xxx_hostaddr;
93
Ben Lindstromf28f6342001-04-04 02:03:04 +000094Kex *xxx_kex = NULL;
95
Ben Lindstrombba81212001-06-25 05:01:22 +000096static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000098{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000099 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000100 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000101 return 0;
102}
103
Damien Millereba71ba2000-04-29 23:57:08 +1000104void
Damien Miller874d77b2000-10-14 16:23:11 +1100105ssh_kex2(char *host, struct sockaddr *hostaddr)
106{
Damien Miller874d77b2000-10-14 16:23:11 +1100107 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000108
109 xxx_host = host;
110 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100111
Damien Millere39cacc2000-11-29 12:18:44 +1100112 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000113 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100114 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100115 }
116 if (options.ciphers != NULL) {
117 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
118 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
119 }
Damien Millera0ff4662001-03-30 10:49:35 +1000120 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
121 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
122 myproposal[PROPOSAL_ENC_ALGS_STOC] =
123 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100124 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000125 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000126 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100127 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000128 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000129 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100130 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000131 if (options.macs != NULL) {
132 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
133 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
134 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000135 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100136 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000137 options.hostkeyalgorithms;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000138 if (options.kex_algorithms != NULL)
139 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100140
Damien Millera5539d22003-04-09 20:50:06 +1000141 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100142 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000143
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000144 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000145 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100146 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000147 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100148 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100149 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000150 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000151 kex->client_version_string=client_version_string;
152 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000153 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100154
Ben Lindstromf28f6342001-04-04 02:03:04 +0000155 xxx_kex = kex;
156
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000157 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100158
Darren Tucker36331b52010-01-08 16:50:41 +1100159 if (options.use_roaming && !kex->roaming) {
160 debug("Roaming not allowed by server");
161 options.use_roaming = 0;
162 }
163
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000164 session_id2 = kex->session_id;
165 session_id2_len = kex->session_id_len;
166
Damien Miller874d77b2000-10-14 16:23:11 +1100167#ifdef DEBUG_KEXDH
168 /* send 1st encrypted/maced/compressed message */
169 packet_start(SSH2_MSG_IGNORE);
170 packet_put_cstring("markus");
171 packet_send();
172 packet_write_wait();
173#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000174}
Damien Millerb1715dc2000-05-30 13:44:51 +1000175
Damien Millereba71ba2000-04-29 23:57:08 +1000176/*
177 * Authenticate user
178 */
Damien Miller62cee002000-09-23 17:15:56 +1100179
180typedef struct Authctxt Authctxt;
181typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000182typedef struct identity Identity;
183typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100184
Damien Miller280ecfb2003-05-14 13:46:00 +1000185struct identity {
186 TAILQ_ENTRY(identity) next;
187 AuthenticationConnection *ac; /* set if agent supports key */
188 Key *key; /* public/private key */
189 char *filename; /* comment for agent-only keys */
190 int tried;
191 int isprivate; /* key points to the private key */
192};
193TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100194
195struct Authctxt {
196 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000197 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100198 const char *host;
199 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100200 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000201 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000202 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000203 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000204 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000205 AuthenticationConnection *agent;
206 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000207 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000208 /* kbd-interactive */
209 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000210 /* generic */
211 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100212};
213struct Authmethod {
214 char *name; /* string to compare against server's list */
215 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100216 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100217 int *enabled; /* flag in option struct that enables method */
218 int *batch_flag; /* flag in option struct that disables method */
219};
220
Damien Miller630d6f42002-01-22 23:17:30 +1100221void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100222void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100223void input_userauth_failure(int, u_int32_t, void *);
224void input_userauth_banner(int, u_int32_t, void *);
225void input_userauth_error(int, u_int32_t, void *);
226void input_userauth_info_req(int, u_int32_t, void *);
227void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000228void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100229void input_userauth_jpake_server_step1(int, u_int32_t, void *);
230void input_userauth_jpake_server_step2(int, u_int32_t, void *);
231void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100232
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000233int userauth_none(Authctxt *);
234int userauth_pubkey(Authctxt *);
235int userauth_passwd(Authctxt *);
236int userauth_kbdint(Authctxt *);
237int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100238int userauth_jpake(Authctxt *);
239
240void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100241
Darren Tucker0efd1552003-08-26 11:49:55 +1000242#ifdef GSSAPI
243int userauth_gssapi(Authctxt *authctxt);
244void input_gssapi_response(int type, u_int32_t, void *);
245void input_gssapi_token(int type, u_int32_t, void *);
246void input_gssapi_hash(int type, u_int32_t, void *);
247void input_gssapi_error(int, u_int32_t, void *);
248void input_gssapi_errtok(int, u_int32_t, void *);
249#endif
250
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000251void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000252
Damien Miller280ecfb2003-05-14 13:46:00 +1000253static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000254static void pubkey_prepare(Authctxt *);
255static void pubkey_cleanup(Authctxt *);
256static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000257
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static Authmethod *authmethod_get(char *authlist);
259static Authmethod *authmethod_lookup(const char *name);
260static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100261
262Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000263#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100264 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000265 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100266 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000267 &options.gss_authentication,
268 NULL},
269#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000270 {"hostbased",
271 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100272 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000273 &options.hostbased_authentication,
274 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000275 {"publickey",
276 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100277 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000278 &options.pubkey_authentication,
279 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100280#ifdef JPAKE
281 {"jpake-01@openssh.com",
282 userauth_jpake,
283 userauth_jpake_cleanup,
284 &options.zero_knowledge_password_authentication,
285 &options.batch_mode},
286#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100287 {"keyboard-interactive",
288 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100289 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100290 &options.kbd_interactive_authentication,
291 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000292 {"password",
293 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100294 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000295 &options.password_authentication,
296 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100297 {"none",
298 userauth_none,
299 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100300 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100301 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100302 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100303};
304
305void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000306ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000307 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100308{
309 Authctxt authctxt;
310 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100311
Ben Lindstrom551ea372001-06-05 18:56:16 +0000312 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000313 options.kbd_interactive_authentication = 1;
314
Damien Miller62cee002000-09-23 17:15:56 +1100315 packet_start(SSH2_MSG_SERVICE_REQUEST);
316 packet_put_cstring("ssh-userauth");
317 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000318 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100319 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100320 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000321 if (type != SSH2_MSG_SERVICE_ACCEPT)
322 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100323 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100324 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000325 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100326 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100327 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000328 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100329 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100330 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000331 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100332
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000333 if (options.preferred_authentications == NULL)
334 options.preferred_authentications = authmethods_get();
335
Damien Miller62cee002000-09-23 17:15:56 +1100336 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000337 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000338 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100339 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000340 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100341 authctxt.host = host;
342 authctxt.service = "ssh-connection"; /* service name */
343 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100344 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000345 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000346 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000347 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000348 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100349 if (authctxt.method == NULL)
350 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100351
352 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100353 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100354
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000355 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100356 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
357 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000358 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100359 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
360
Damien Miller280ecfb2003-05-14 13:46:00 +1000361 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000362 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
363
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000364 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100365}
Damien Millerf842fcb2003-05-15 12:01:28 +1000366
Damien Miller62cee002000-09-23 17:15:56 +1100367void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000368userauth(Authctxt *authctxt, char *authlist)
369{
Damien Miller01ed2272008-11-05 16:20:46 +1100370 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
371 authctxt->method->cleanup(authctxt);
372
Darren Tucker0efd1552003-08-26 11:49:55 +1000373 if (authctxt->methoddata) {
374 xfree(authctxt->methoddata);
375 authctxt->methoddata = NULL;
376 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000377 if (authlist == NULL) {
378 authlist = authctxt->authlist;
379 } else {
380 if (authctxt->authlist)
381 xfree(authctxt->authlist);
382 authctxt->authlist = authlist;
383 }
384 for (;;) {
385 Authmethod *method = authmethod_get(authlist);
386 if (method == NULL)
387 fatal("Permission denied (%s).", authlist);
388 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000389
390 /* reset the per method handler */
391 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
392 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
393
394 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000395 if (method->userauth(authctxt) != 0) {
396 debug2("we sent a %s packet, wait for reply", method->name);
397 break;
398 } else {
399 debug2("we did not send a packet, disable method");
400 method->enabled = NULL;
401 }
402 }
403}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000404
Damien Millerf7475d72008-11-03 19:26:18 +1100405/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000406void
Damien Miller630d6f42002-01-22 23:17:30 +1100407input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100408{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000409 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000410 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000411}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000412
Damien Millerf7475d72008-11-03 19:26:18 +1100413/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000414void
Damien Miller630d6f42002-01-22 23:17:30 +1100415input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000416{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000417 char *msg, *raw, *lang;
418 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000419
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000420 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000421 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000422 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100423 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000424 if (len > 65536)
425 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100426 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100427 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000428 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000429 xfree(msg);
430 }
431 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000432 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100433}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000434
Damien Millerf7475d72008-11-03 19:26:18 +1100435/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100436void
Damien Miller630d6f42002-01-22 23:17:30 +1100437input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100438{
439 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100440
Damien Miller62cee002000-09-23 17:15:56 +1100441 if (authctxt == NULL)
442 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000443 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000444 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000445 authctxt->authlist = NULL;
446 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100447 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
448 authctxt->method->cleanup(authctxt);
Darren Tucker79644822003-10-08 17:37:58 +1000449 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000450 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000451 authctxt->methoddata = NULL;
452 }
Damien Miller62cee002000-09-23 17:15:56 +1100453 authctxt->success = 1; /* break out */
454}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000455
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100456void
457input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
458{
459 Authctxt *authctxt = ctxt;
460
461 if (authctxt == NULL)
462 fatal("%s: no authentication context", __func__);
463
464 fatal("Unexpected authentication success during %s.",
465 authctxt->method->name);
466}
467
Damien Millerf7475d72008-11-03 19:26:18 +1100468/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100469void
Damien Miller630d6f42002-01-22 23:17:30 +1100470input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100471{
Damien Miller62cee002000-09-23 17:15:56 +1100472 Authctxt *authctxt = ctxt;
473 char *authlist = NULL;
474 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100475
476 if (authctxt == NULL)
477 fatal("input_userauth_failure: no authentication context");
478
Damien Miller874d77b2000-10-14 16:23:11 +1100479 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100480 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100481 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100482
483 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000484 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000485 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100486
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000487 userauth(authctxt, authlist);
488}
Damien Millerf7475d72008-11-03 19:26:18 +1100489
490/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000491void
Damien Miller630d6f42002-01-22 23:17:30 +1100492input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000493{
494 Authctxt *authctxt = ctxt;
495 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000496 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000497 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000498 int pktype, sent = 0;
499 u_int alen, blen;
500 char *pkalg, *fp;
501 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000502
503 if (authctxt == NULL)
504 fatal("input_userauth_pk_ok: no authentication context");
505 if (datafellows & SSH_BUG_PKOK) {
506 /* this is similar to SSH_BUG_PKAUTH */
507 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
508 pkblob = packet_get_string(&blen);
509 buffer_init(&b);
510 buffer_append(&b, pkblob, blen);
511 pkalg = buffer_get_string(&b, &alen);
512 buffer_free(&b);
513 } else {
514 pkalg = packet_get_string(&alen);
515 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000516 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100517 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000518
Damien Miller280ecfb2003-05-14 13:46:00 +1000519 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000520
Damien Miller280ecfb2003-05-14 13:46:00 +1000521 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
522 debug("unknown pkalg %s", pkalg);
523 goto done;
524 }
525 if ((key = key_from_blob(pkblob, blen)) == NULL) {
526 debug("no key from blob. pkalg %s", pkalg);
527 goto done;
528 }
529 if (key->type != pktype) {
530 error("input_userauth_pk_ok: type mismatch "
531 "for decoded key (received %d, expected %d)",
532 key->type, pktype);
533 goto done;
534 }
535 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
536 debug2("input_userauth_pk_ok: fp %s", fp);
537 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000538
Darren Tuckerd05b6012003-10-15 15:55:59 +1000539 /*
540 * search keys in the reverse order, because last candidate has been
541 * moved to the end of the queue. this also avoids confusion by
542 * duplicate keys
543 */
Damien Miller0b51a522004-04-20 20:07:19 +1000544 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000545 if (key_equal(key, id->key)) {
546 sent = sign_and_send_pubkey(authctxt, id);
547 break;
548 }
549 }
550done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551 if (key != NULL)
552 key_free(key);
553 xfree(pkalg);
554 xfree(pkblob);
555
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000556 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000557 if (sent == 0)
558 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100559}
560
Darren Tucker0efd1552003-08-26 11:49:55 +1000561#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100562int
Darren Tucker0efd1552003-08-26 11:49:55 +1000563userauth_gssapi(Authctxt *authctxt)
564{
565 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100566 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000567 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000568 OM_uint32 min;
569 int ok = 0;
570
571 /* Try one GSSAPI method at a time, rather than sending them all at
572 * once. */
573
Darren Tucker56afe142003-11-03 20:06:14 +1100574 if (gss_supported == NULL)
575 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000576
577 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100578 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000579 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100580 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000581 ssh_gssapi_check_mechanism(&gssctxt,
582 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000583 ok = 1; /* Mechanism works */
584 } else {
585 mech++;
586 }
587 }
588
Damien Millera1cb9f32006-08-19 00:33:34 +1000589 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000590 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000591
592 authctxt->methoddata=(void *)gssctxt;
593
594 packet_start(SSH2_MSG_USERAUTH_REQUEST);
595 packet_put_cstring(authctxt->server_user);
596 packet_put_cstring(authctxt->service);
597 packet_put_cstring(authctxt->method->name);
598
599 packet_put_int(1);
600
Darren Tucker655a5e02003-11-03 20:09:03 +1100601 packet_put_int((gss_supported->elements[mech].length) + 2);
602 packet_put_char(SSH_GSS_OIDTYPE);
603 packet_put_char(gss_supported->elements[mech].length);
604 packet_put_raw(gss_supported->elements[mech].elements,
605 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000606
607 packet_send();
608
609 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
610 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
611 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
612 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
613
614 mech++; /* Move along to next candidate */
615
616 return 1;
617}
618
Damien Miller91c6aa42003-11-17 21:20:18 +1100619static OM_uint32
620process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
621{
622 Authctxt *authctxt = ctxt;
623 Gssctxt *gssctxt = authctxt->methoddata;
624 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000625 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
626 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100627 OM_uint32 status, ms, flags;
628 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100629
Damien Miller91c6aa42003-11-17 21:20:18 +1100630 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100631 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100632
633 if (send_tok.length > 0) {
634 if (GSS_ERROR(status))
635 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
636 else
637 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100638
Damien Miller91c6aa42003-11-17 21:20:18 +1100639 packet_put_string(send_tok.value, send_tok.length);
640 packet_send();
641 gss_release_buffer(&ms, &send_tok);
642 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100643
Damien Miller91c6aa42003-11-17 21:20:18 +1100644 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100645 /* send either complete or MIC, depending on mechanism */
646 if (!(flags & GSS_C_INTEG_FLAG)) {
647 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
648 packet_send();
649 } else {
650 ssh_gssapi_buildmic(&b, authctxt->server_user,
651 authctxt->service, "gssapi-with-mic");
652
653 gssbuf.value = buffer_ptr(&b);
654 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100655
Damien Miller0425d402003-11-17 22:18:21 +1100656 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100657
Damien Miller0425d402003-11-17 22:18:21 +1100658 if (!GSS_ERROR(status)) {
659 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
660 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100661
Damien Miller0425d402003-11-17 22:18:21 +1100662 packet_send();
663 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100664
Damien Miller0425d402003-11-17 22:18:21 +1100665 buffer_free(&b);
666 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100667 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100668 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100669
Damien Miller91c6aa42003-11-17 21:20:18 +1100670 return status;
671}
672
Damien Millerf7475d72008-11-03 19:26:18 +1100673/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000674void
675input_gssapi_response(int type, u_int32_t plen, void *ctxt)
676{
677 Authctxt *authctxt = ctxt;
678 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000679 int oidlen;
680 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000681
682 if (authctxt == NULL)
683 fatal("input_gssapi_response: no authentication context");
684 gssctxt = authctxt->methoddata;
685
686 /* Setup our OID */
687 oidv = packet_get_string(&oidlen);
688
Darren Tucker655a5e02003-11-03 20:09:03 +1100689 if (oidlen <= 2 ||
690 oidv[0] != SSH_GSS_OIDTYPE ||
691 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100692 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100693 debug("Badly encoded mechanism OID received");
694 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100695 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000696 }
697
Darren Tucker655a5e02003-11-03 20:09:03 +1100698 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
699 fatal("Server returned different OID than expected");
700
Darren Tucker0efd1552003-08-26 11:49:55 +1000701 packet_check_eom();
702
703 xfree(oidv);
704
Damien Miller91c6aa42003-11-17 21:20:18 +1100705 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000706 /* Start again with next method on list */
707 debug("Trying to start again");
708 userauth(authctxt, NULL);
709 return;
710 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000711}
712
Damien Millerf7475d72008-11-03 19:26:18 +1100713/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000714void
715input_gssapi_token(int type, u_int32_t plen, void *ctxt)
716{
717 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000718 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100719 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000720 u_int slen;
721
722 if (authctxt == NULL)
723 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000724
725 recv_tok.value = packet_get_string(&slen);
726 recv_tok.length = slen; /* safe typecast */
727
728 packet_check_eom();
729
Damien Miller91c6aa42003-11-17 21:20:18 +1100730 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000731
732 xfree(recv_tok.value);
733
734 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000735 /* Start again with the next method in the list */
736 userauth(authctxt, NULL);
737 return;
738 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000739}
740
Damien Millerf7475d72008-11-03 19:26:18 +1100741/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000742void
743input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
744{
745 Authctxt *authctxt = ctxt;
746 Gssctxt *gssctxt;
747 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
748 gss_buffer_desc recv_tok;
749 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000750 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000751
752 if (authctxt == NULL)
753 fatal("input_gssapi_response: no authentication context");
754 gssctxt = authctxt->methoddata;
755
Darren Tucker600ad8d2003-08-26 12:10:48 +1000756 recv_tok.value = packet_get_string(&len);
757 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000758
759 packet_check_eom();
760
761 /* Stick it into GSSAPI and see what it says */
762 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000763 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000764
765 xfree(recv_tok.value);
766 gss_release_buffer(&ms, &send_tok);
767
768 /* Server will be returning a failed packet after this one */
769}
770
Damien Millerf7475d72008-11-03 19:26:18 +1100771/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000772void
773input_gssapi_error(int type, u_int32_t plen, void *ctxt)
774{
775 OM_uint32 maj, min;
776 char *msg;
777 char *lang;
778
779 maj=packet_get_int();
780 min=packet_get_int();
781 msg=packet_get_string(NULL);
782 lang=packet_get_string(NULL);
783
784 packet_check_eom();
785
Damien Miller15d72a02005-11-05 15:07:33 +1100786 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000787 xfree(msg);
788 xfree(lang);
789}
790#endif /* GSSAPI */
791
Damien Millereba71ba2000-04-29 23:57:08 +1000792int
Damien Miller874d77b2000-10-14 16:23:11 +1100793userauth_none(Authctxt *authctxt)
794{
795 /* initial userauth request */
796 packet_start(SSH2_MSG_USERAUTH_REQUEST);
797 packet_put_cstring(authctxt->server_user);
798 packet_put_cstring(authctxt->service);
799 packet_put_cstring(authctxt->method->name);
800 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100801 return 1;
802}
803
804int
Damien Miller62cee002000-09-23 17:15:56 +1100805userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000806{
Damien Millere247cc42000-05-07 12:03:14 +1000807 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000808 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000809 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100810 const char *host = options.host_key_alias ? options.host_key_alias :
811 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000812
Damien Millerd3a18572000-06-07 19:55:44 +1000813 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000814 return 0;
815
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000816 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000817 error("Permission denied, please try again.");
818
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000819 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100820 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000821 password = read_passphrase(prompt, 0);
822 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100823 packet_put_cstring(authctxt->server_user);
824 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100825 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000826 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000827 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000828 memset(password, 0, strlen(password));
829 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100830 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000831 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000832
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000833 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000834 &input_userauth_passwd_changereq);
835
Damien Millereba71ba2000-04-29 23:57:08 +1000836 return 1;
837}
Damien Millerf7475d72008-11-03 19:26:18 +1100838
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000839/*
840 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
841 */
Damien Millerf7475d72008-11-03 19:26:18 +1100842/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000843void
Tim Ricec8549622002-03-31 12:49:38 -0800844input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000845{
846 Authctxt *authctxt = ctxt;
847 char *info, *lang, *password = NULL, *retype = NULL;
848 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100849 const char *host = options.host_key_alias ? options.host_key_alias :
850 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000851
852 debug2("input_userauth_passwd_changereq");
853
854 if (authctxt == NULL)
855 fatal("input_userauth_passwd_changereq: "
856 "no authentication context");
857
858 info = packet_get_string(NULL);
859 lang = packet_get_string(NULL);
860 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000861 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000862 xfree(info);
863 xfree(lang);
864 packet_start(SSH2_MSG_USERAUTH_REQUEST);
865 packet_put_cstring(authctxt->server_user);
866 packet_put_cstring(authctxt->service);
867 packet_put_cstring(authctxt->method->name);
868 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000869 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000870 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100871 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000872 password = read_passphrase(prompt, 0);
873 packet_put_cstring(password);
874 memset(password, 0, strlen(password));
875 xfree(password);
876 password = NULL;
877 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000878 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000879 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100880 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000881 password = read_passphrase(prompt, RP_ALLOW_EOF);
882 if (password == NULL) {
883 /* bail out */
884 return;
885 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000886 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000887 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100888 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000889 retype = read_passphrase(prompt, 0);
890 if (strcmp(password, retype) != 0) {
891 memset(password, 0, strlen(password));
892 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000893 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000894 password = NULL;
895 }
896 memset(retype, 0, strlen(retype));
897 xfree(retype);
898 }
899 packet_put_cstring(password);
900 memset(password, 0, strlen(password));
901 xfree(password);
902 packet_add_padding(64);
903 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000904
905 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000906 &input_userauth_passwd_changereq);
907}
Damien Millereba71ba2000-04-29 23:57:08 +1000908
Damien Miller01ed2272008-11-05 16:20:46 +1100909#ifdef JPAKE
910static char *
911pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
912{
913 /* OpenBSD crypt(3) handles all of these */
914 if (strcmp(crypt_scheme, "crypt") == 0 ||
915 strcmp(crypt_scheme, "bcrypt") == 0 ||
916 strcmp(crypt_scheme, "md5crypt") == 0 ||
917 strcmp(crypt_scheme, "crypt-extended") == 0)
918 return xstrdup(crypt(password, salt));
919 error("%s: unsupported password encryption scheme \"%.100s\"",
920 __func__, crypt_scheme);
921 return NULL;
922}
923
924static BIGNUM *
925jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
926 const char *salt)
927{
928 char prompt[256], *password, *crypted;
929 u_char *secret;
930 u_int secret_len;
931 BIGNUM *ret;
932
933 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
934 authctxt->server_user, authctxt->host);
935 password = read_passphrase(prompt, 0);
936
937 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
938 logit("Disabling %s authentication", authctxt->method->name);
939 authctxt->method->enabled = NULL;
940 /* Continue with an empty password to fail gracefully */
941 crypted = xstrdup("");
942 }
943
944#ifdef JPAKE_DEBUG
945 debug3("%s: salt = %s", __func__, salt);
946 debug3("%s: scheme = %s", __func__, crypt_scheme);
947 debug3("%s: crypted = %s", __func__, crypted);
948#endif
949
950 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
951 &secret, &secret_len) != 0)
952 fatal("%s: hash_buffer", __func__);
953
954 bzero(password, strlen(password));
955 bzero(crypted, strlen(crypted));
956 xfree(password);
957 xfree(crypted);
958
959 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
960 fatal("%s: BN_bin2bn (secret)", __func__);
961 bzero(secret, secret_len);
962 xfree(secret);
963
964 return ret;
965}
966
967/* ARGSUSED */
968void
969input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
970{
971 Authctxt *authctxt = ctxt;
972 struct jpake_ctx *pctx = authctxt->methoddata;
973 u_char *x3_proof, *x4_proof, *x2_s_proof;
974 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
975 char *crypt_scheme, *salt;
976
977 /* Disable this message */
978 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
979
980 if ((pctx->g_x3 = BN_new()) == NULL ||
981 (pctx->g_x4 = BN_new()) == NULL)
982 fatal("%s: BN_new", __func__);
983
984 /* Fetch step 1 values */
985 crypt_scheme = packet_get_string(NULL);
986 salt = packet_get_string(NULL);
987 pctx->server_id = packet_get_string(&pctx->server_id_len);
988 packet_get_bignum2(pctx->g_x3);
989 packet_get_bignum2(pctx->g_x4);
990 x3_proof = packet_get_string(&x3_proof_len);
991 x4_proof = packet_get_string(&x4_proof_len);
992 packet_check_eom();
993
994 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
995
996 /* Obtain password and derive secret */
997 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
998 bzero(crypt_scheme, strlen(crypt_scheme));
999 bzero(salt, strlen(salt));
1000 xfree(crypt_scheme);
1001 xfree(salt);
1002 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1003
1004 /* Calculate step 2 values */
1005 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1006 pctx->g_x3, pctx->g_x4, pctx->x2,
1007 pctx->server_id, pctx->server_id_len,
1008 pctx->client_id, pctx->client_id_len,
1009 x3_proof, x3_proof_len,
1010 x4_proof, x4_proof_len,
1011 &pctx->a,
1012 &x2_s_proof, &x2_s_proof_len);
1013
1014 bzero(x3_proof, x3_proof_len);
1015 bzero(x4_proof, x4_proof_len);
1016 xfree(x3_proof);
1017 xfree(x4_proof);
1018
1019 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1020
1021 /* Send values for step 2 */
1022 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1023 packet_put_bignum2(pctx->a);
1024 packet_put_string(x2_s_proof, x2_s_proof_len);
1025 packet_send();
1026
1027 bzero(x2_s_proof, x2_s_proof_len);
1028 xfree(x2_s_proof);
1029
1030 /* Expect step 2 packet from peer */
1031 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1032 input_userauth_jpake_server_step2);
1033}
1034
1035/* ARGSUSED */
1036void
1037input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1038{
1039 Authctxt *authctxt = ctxt;
1040 struct jpake_ctx *pctx = authctxt->methoddata;
1041 u_char *x4_s_proof;
1042 u_int x4_s_proof_len;
1043
1044 /* Disable this message */
1045 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1046
1047 if ((pctx->b = BN_new()) == NULL)
1048 fatal("%s: BN_new", __func__);
1049
1050 /* Fetch step 2 values */
1051 packet_get_bignum2(pctx->b);
1052 x4_s_proof = packet_get_string(&x4_s_proof_len);
1053 packet_check_eom();
1054
1055 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1056
1057 /* Derive shared key and calculate confirmation hash */
1058 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1059 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1060 pctx->client_id, pctx->client_id_len,
1061 pctx->server_id, pctx->server_id_len,
1062 session_id2, session_id2_len,
1063 x4_s_proof, x4_s_proof_len,
1064 &pctx->k,
1065 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1066
1067 bzero(x4_s_proof, x4_s_proof_len);
1068 xfree(x4_s_proof);
1069
1070 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1071
1072 /* Send key confirmation proof */
1073 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1074 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1075 packet_send();
1076
1077 /* Expect confirmation from peer */
1078 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1079 input_userauth_jpake_server_confirm);
1080}
1081
1082/* ARGSUSED */
1083void
1084input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1085{
1086 Authctxt *authctxt = ctxt;
1087 struct jpake_ctx *pctx = authctxt->methoddata;
1088
1089 /* Disable this message */
1090 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1091
1092 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1093 packet_check_eom();
1094
1095 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1096
1097 /* Verify expected confirmation hash */
1098 if (jpake_check_confirm(pctx->k,
1099 pctx->server_id, pctx->server_id_len,
1100 session_id2, session_id2_len,
1101 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1102 debug("%s: %s success", __func__, authctxt->method->name);
1103 else {
1104 debug("%s: confirmation mismatch", __func__);
1105 /* XXX stash this so if auth succeeds then we can warn/kill */
1106 }
1107
1108 userauth_jpake_cleanup(authctxt);
1109}
1110#endif /* JPAKE */
1111
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001113identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1114 u_char *data, u_int datalen)
1115{
1116 Key *prv;
1117 int ret;
1118
1119 /* the agent supports this key */
1120 if (id->ac)
1121 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1122 data, datalen));
1123 /*
1124 * we have already loaded the private key or
1125 * the private key is stored in external hardware
1126 */
1127 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1128 return (key_sign(id->key, sigp, lenp, data, datalen));
1129 /* load the private key from the file */
1130 if ((prv = load_identity_file(id->filename)) == NULL)
1131 return (-1);
1132 ret = key_sign(prv, sigp, lenp, data, datalen);
1133 key_free(prv);
1134 return (ret);
1135}
1136
1137static int
1138sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001139{
1140 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001141 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001142 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001143 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001144 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001145 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001146 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001147
Damien Miller4e270b02010-04-16 15:56:21 +10001148 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1149 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1150 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001151
Damien Miller280ecfb2003-05-14 13:46:00 +10001152 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001153 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001154 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155 return 0;
1156 }
Damien Millereba71ba2000-04-29 23:57:08 +10001157 /* data to be signed */
1158 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001159 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001160 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001161 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001162 } else {
1163 buffer_put_string(&b, session_id2, session_id2_len);
1164 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001165 }
Damien Millereba71ba2000-04-29 23:57:08 +10001166 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001167 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001168 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001169 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001170 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001171 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001172 if (datafellows & SSH_BUG_PKAUTH) {
1173 buffer_put_char(&b, have_sig);
1174 } else {
1175 buffer_put_cstring(&b, authctxt->method->name);
1176 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001177 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001178 }
Damien Millereba71ba2000-04-29 23:57:08 +10001179 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001180
1181 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001182 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001183 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001184 if (ret == -1) {
1185 xfree(blob);
1186 buffer_free(&b);
1187 return 0;
1188 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001189#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001190 buffer_dump(&b);
1191#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001192 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001193 buffer_clear(&b);
1194 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001195 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001196 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001197 buffer_put_cstring(&b, authctxt->server_user);
1198 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001199 buffer_put_cstring(&b, authctxt->method->name);
1200 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001201 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001202 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001203 buffer_put_string(&b, blob, bloblen);
1204 }
1205 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001206
Damien Millereba71ba2000-04-29 23:57:08 +10001207 /* append signature */
1208 buffer_put_string(&b, signature, slen);
1209 xfree(signature);
1210
1211 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001212 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001213 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001214 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001215
1216 /* put remaining data from buffer into packet */
1217 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1218 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1219 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001220 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001221
1222 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001223}
1224
Ben Lindstrombba81212001-06-25 05:01:22 +00001225static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001226send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001227{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001228 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001229 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001230
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001231 debug3("send_pubkey_test");
1232
Damien Miller280ecfb2003-05-14 13:46:00 +10001233 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001234 /* we cannot handle this key */
1235 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001236 return 0;
1237 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001238 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001239 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001240
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001241 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1242 packet_put_cstring(authctxt->server_user);
1243 packet_put_cstring(authctxt->service);
1244 packet_put_cstring(authctxt->method->name);
1245 packet_put_char(have_sig);
1246 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001247 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001248 packet_put_string(blob, bloblen);
1249 xfree(blob);
1250 packet_send();
1251 return 1;
1252}
1253
Ben Lindstrombba81212001-06-25 05:01:22 +00001254static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001255load_identity_file(char *filename)
1256{
1257 Key *private;
1258 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001259 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001260 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001261
Ben Lindstrom329782e2001-03-10 17:08:59 +00001262 if (stat(filename, &st) < 0) {
1263 debug3("no such identity: %s", filename);
1264 return NULL;
1265 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001266 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1267 if (!perm_ok)
1268 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001269 if (private == NULL) {
1270 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001271 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001272 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001273 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001274 for (i = 0; i < options.number_of_password_prompts; i++) {
1275 passphrase = read_passphrase(prompt, 0);
1276 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001277 private = key_load_private_type(KEY_UNSPEC,
1278 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001279 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001280 } else {
1281 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001282 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001283 }
1284 memset(passphrase, 0, strlen(passphrase));
1285 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001286 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001287 break;
1288 debug2("bad passphrase given, try again...");
1289 }
Damien Miller994cf142000-07-21 10:19:44 +10001290 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001291 return private;
1292}
1293
Damien Miller280ecfb2003-05-14 13:46:00 +10001294/*
1295 * try keys in the following order:
1296 * 1. agent keys that are found in the config file
1297 * 2. other agent keys
1298 * 3. keys that are only listed in the config file
1299 */
1300static void
1301pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001302{
Damien Miller280ecfb2003-05-14 13:46:00 +10001303 Identity *id;
1304 Idlist agent, files, *preferred;
1305 Key *key;
1306 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001307 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001308 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001309
Damien Miller280ecfb2003-05-14 13:46:00 +10001310 TAILQ_INIT(&agent); /* keys from the agent */
1311 TAILQ_INIT(&files); /* keys from the config file */
1312 preferred = &authctxt->keys;
1313 TAILQ_INIT(preferred); /* preferred order of keys */
1314
1315 /* list of keys stored in the filesystem */
1316 for (i = 0; i < options.num_identity_files; i++) {
1317 key = options.identity_keys[i];
1318 if (key && key->type == KEY_RSA1)
1319 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001320 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1321 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001322 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001323 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001324 id->key = key;
1325 id->filename = xstrdup(options.identity_files[i]);
1326 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001327 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001328 /* list of keys supported by the agent */
1329 if ((ac = ssh_get_authentication_connection())) {
1330 for (key = ssh_get_first_identity(ac, &comment, 2);
1331 key != NULL;
1332 key = ssh_get_next_identity(ac, &comment, 2)) {
1333 found = 0;
1334 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001335 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001336 if (key_equal(key, id->key)) {
1337 key_free(key);
1338 xfree(comment);
1339 TAILQ_REMOVE(&files, id, next);
1340 TAILQ_INSERT_TAIL(preferred, id, next);
1341 id->ac = ac;
1342 found = 1;
1343 break;
1344 }
1345 }
Damien Millerbd394c32004-03-08 23:12:36 +11001346 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001347 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001348 id->key = key;
1349 id->filename = comment;
1350 id->ac = ac;
1351 TAILQ_INSERT_TAIL(&agent, id, next);
1352 }
1353 }
1354 /* append remaining agent keys */
1355 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1356 TAILQ_REMOVE(&agent, id, next);
1357 TAILQ_INSERT_TAIL(preferred, id, next);
1358 }
1359 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001360 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001361 /* append remaining keys from the config file */
1362 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1363 TAILQ_REMOVE(&files, id, next);
1364 TAILQ_INSERT_TAIL(preferred, id, next);
1365 }
1366 TAILQ_FOREACH(id, preferred, next) {
1367 debug2("key: %s (%p)", id->filename, id->key);
1368 }
1369}
1370
1371static void
1372pubkey_cleanup(Authctxt *authctxt)
1373{
1374 Identity *id;
1375
1376 if (authctxt->agent != NULL)
1377 ssh_close_authentication_connection(authctxt->agent);
1378 for (id = TAILQ_FIRST(&authctxt->keys); id;
1379 id = TAILQ_FIRST(&authctxt->keys)) {
1380 TAILQ_REMOVE(&authctxt->keys, id, next);
1381 if (id->key)
1382 key_free(id->key);
1383 if (id->filename)
1384 xfree(id->filename);
1385 xfree(id);
1386 }
Damien Millereba71ba2000-04-29 23:57:08 +10001387}
1388
Damien Miller62cee002000-09-23 17:15:56 +11001389int
1390userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001391{
Damien Miller280ecfb2003-05-14 13:46:00 +10001392 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001393 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001394
Damien Miller280ecfb2003-05-14 13:46:00 +10001395 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1396 if (id->tried++)
1397 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001398 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001399 TAILQ_REMOVE(&authctxt->keys, id, next);
1400 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1401 /*
1402 * send a test message if we have the public key. for
1403 * encrypted keys we cannot do this and have to load the
1404 * private key instead
1405 */
1406 if (id->key && id->key->type != KEY_RSA1) {
Damien Miller88680652010-04-16 15:53:43 +10001407 debug("Offering %s public key: %s", key_type(id->key),
1408 id->filename);
Damien Miller280ecfb2003-05-14 13:46:00 +10001409 sent = send_pubkey_test(authctxt, id);
1410 } else if (id->key == NULL) {
1411 debug("Trying private key: %s", id->filename);
1412 id->key = load_identity_file(id->filename);
1413 if (id->key != NULL) {
1414 id->isprivate = 1;
1415 sent = sign_and_send_pubkey(authctxt, id);
1416 key_free(id->key);
1417 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001418 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001419 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001420 if (sent)
1421 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001422 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001423 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001424}
Damien Millereba71ba2000-04-29 23:57:08 +10001425
Damien Miller874d77b2000-10-14 16:23:11 +11001426/*
1427 * Send userauth request message specifying keyboard-interactive method.
1428 */
1429int
1430userauth_kbdint(Authctxt *authctxt)
1431{
1432 static int attempt = 0;
1433
1434 if (attempt++ >= options.number_of_password_prompts)
1435 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001436 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1437 if (attempt > 1 && !authctxt->info_req_seen) {
1438 debug3("userauth_kbdint: disable: no info_req_seen");
1439 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1440 return 0;
1441 }
Damien Miller874d77b2000-10-14 16:23:11 +11001442
1443 debug2("userauth_kbdint");
1444 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1445 packet_put_cstring(authctxt->server_user);
1446 packet_put_cstring(authctxt->service);
1447 packet_put_cstring(authctxt->method->name);
1448 packet_put_cstring(""); /* lang */
1449 packet_put_cstring(options.kbd_interactive_devices ?
1450 options.kbd_interactive_devices : "");
1451 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001452
1453 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1454 return 1;
1455}
1456
1457/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001458 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001459 */
1460void
Damien Miller630d6f42002-01-22 23:17:30 +11001461input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001462{
1463 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001464 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001465 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001466 int echo = 0;
1467
1468 debug2("input_userauth_info_req");
1469
1470 if (authctxt == NULL)
1471 fatal("input_userauth_info_req: no authentication context");
1472
Ben Lindstrom7d199962001-09-12 18:29:00 +00001473 authctxt->info_req_seen = 1;
1474
Damien Miller874d77b2000-10-14 16:23:11 +11001475 name = packet_get_string(NULL);
1476 inst = packet_get_string(NULL);
1477 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001478 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001479 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001480 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001481 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001482 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001483 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001484 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001485
1486 num_prompts = packet_get_int();
1487 /*
1488 * Begin to build info response packet based on prompts requested.
1489 * We commit to providing the correct number of responses, so if
1490 * further on we run into a problem that prevents this, we have to
1491 * be sure and clean this up and send a correct error response.
1492 */
1493 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1494 packet_put_int(num_prompts);
1495
Ben Lindstrom551ea372001-06-05 18:56:16 +00001496 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001497 for (i = 0; i < num_prompts; i++) {
1498 prompt = packet_get_string(NULL);
1499 echo = packet_get_char();
1500
Ben Lindstrom949974b2001-06-25 05:20:31 +00001501 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001502
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001503 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001504 memset(response, 0, strlen(response));
1505 xfree(response);
1506 xfree(prompt);
1507 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001508 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001509
Damien Miller9f643902001-11-12 11:02:52 +11001510 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001511 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001512}
Damien Miller62cee002000-09-23 17:15:56 +11001513
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001514static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001515ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001516 u_char *data, u_int datalen)
1517{
1518 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001519 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001520 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001521 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001522
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001523 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001524
Ben Lindstrom5206b952002-06-06 19:59:29 +00001525 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001526 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001527 return -1;
1528 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001529 if (fflush(stdout) != 0)
1530 error("ssh_keysign: fflush: %s", strerror(errno));
1531 if (pipe(to) < 0) {
1532 error("ssh_keysign: pipe: %s", strerror(errno));
1533 return -1;
1534 }
1535 if (pipe(from) < 0) {
1536 error("ssh_keysign: pipe: %s", strerror(errno));
1537 return -1;
1538 }
1539 if ((pid = fork()) < 0) {
1540 error("ssh_keysign: fork: %s", strerror(errno));
1541 return -1;
1542 }
1543 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001544 /* keep the socket on exec */
1545 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001546 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001547 close(from[0]);
1548 if (dup2(from[1], STDOUT_FILENO) < 0)
1549 fatal("ssh_keysign: dup2: %s", strerror(errno));
1550 close(to[1]);
1551 if (dup2(to[0], STDIN_FILENO) < 0)
1552 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001553 close(from[1]);
1554 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001555 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001556 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1557 strerror(errno));
1558 }
1559 close(from[1]);
1560 close(to[0]);
1561
1562 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001563 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001564 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001565 if (ssh_msg_send(to[1], version, &b) == -1)
1566 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001567
Damien Miller901119b2002-10-04 11:10:04 +10001568 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001569 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001570 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001571 return -1;
1572 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001573 close(from[0]);
1574 close(to[1]);
1575
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001576 while (waitpid(pid, &status, 0) < 0)
1577 if (errno != EINTR)
1578 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001579
Ben Lindstrom5206b952002-06-06 19:59:29 +00001580 if (buffer_get_char(&b) != version) {
1581 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001582 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001583 return -1;
1584 }
1585 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001586 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001587
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001588 return 0;
1589}
1590
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001591int
1592userauth_hostbased(Authctxt *authctxt)
1593{
1594 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001595 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001596 Buffer b;
1597 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001598 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001599 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001600 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001601 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001602
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001603 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001604 for (i = 0; i < sensitive->nkeys; i++) {
1605 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001606 if (private && private->type != KEY_RSA1) {
1607 found = 1;
1608 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001609 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001610 break;
1611 }
1612 }
1613 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001614 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001615 return 0;
1616 }
1617 if (key_to_blob(private, &blob, &blen) == 0) {
1618 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001619 return 0;
1620 }
Damien Miller91c18472001-11-12 11:02:03 +11001621 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001622 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001623 if (p == NULL) {
1624 error("userauth_hostbased: cannot get local ipaddr/name");
1625 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001626 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001627 return 0;
1628 }
Damien Miller07d86be2006-03-26 14:19:21 +11001629 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001630 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001631 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001632
Ben Lindstrom671388f2001-04-19 20:40:45 +00001633 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1634 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001635 pkalg = xstrdup(key_ssh_name(private));
1636 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001637 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001638 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001639 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1640 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001641 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001642 buffer_put_cstring(&b, authctxt->method->name);
1643 buffer_put_cstring(&b, pkalg);
1644 buffer_put_string(&b, blob, blen);
1645 buffer_put_cstring(&b, chost);
1646 buffer_put_cstring(&b, authctxt->local_user);
1647#ifdef DEBUG_PK
1648 buffer_dump(&b);
1649#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001650 if (sensitive->external_keysign)
1651 ok = ssh_keysign(private, &signature, &slen,
1652 buffer_ptr(&b), buffer_len(&b));
1653 else
1654 ok = key_sign(private, &signature, &slen,
1655 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001656 key_free(private);
1657 buffer_free(&b);
1658 if (ok != 0) {
1659 error("key_sign failed");
1660 xfree(chost);
1661 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001662 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001663 return 0;
1664 }
1665 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1666 packet_put_cstring(authctxt->server_user);
1667 packet_put_cstring(authctxt->service);
1668 packet_put_cstring(authctxt->method->name);
1669 packet_put_cstring(pkalg);
1670 packet_put_string(blob, blen);
1671 packet_put_cstring(chost);
1672 packet_put_cstring(authctxt->local_user);
1673 packet_put_string(signature, slen);
1674 memset(signature, 's', slen);
1675 xfree(signature);
1676 xfree(chost);
1677 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001678 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001679
1680 packet_send();
1681 return 1;
1682}
1683
Damien Miller01ed2272008-11-05 16:20:46 +11001684#ifdef JPAKE
1685int
1686userauth_jpake(Authctxt *authctxt)
1687{
1688 struct jpake_ctx *pctx;
1689 u_char *x1_proof, *x2_proof;
1690 u_int x1_proof_len, x2_proof_len;
1691 static int attempt = 0; /* XXX share with userauth_password's? */
1692
1693 if (attempt++ >= options.number_of_password_prompts)
1694 return 0;
1695 if (attempt != 1)
1696 error("Permission denied, please try again.");
1697
1698 if (authctxt->methoddata != NULL)
1699 fatal("%s: authctxt->methoddata already set (%p)",
1700 __func__, authctxt->methoddata);
1701
1702 authctxt->methoddata = pctx = jpake_new();
1703
1704 /*
1705 * Send request immediately, to get the protocol going while
1706 * we do the initial computations.
1707 */
1708 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1709 packet_put_cstring(authctxt->server_user);
1710 packet_put_cstring(authctxt->service);
1711 packet_put_cstring(authctxt->method->name);
1712 packet_send();
1713 packet_write_wait();
1714
1715 jpake_step1(pctx->grp,
1716 &pctx->client_id, &pctx->client_id_len,
1717 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1718 &x1_proof, &x1_proof_len,
1719 &x2_proof, &x2_proof_len);
1720
1721 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1722
1723 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1724 packet_put_string(pctx->client_id, pctx->client_id_len);
1725 packet_put_bignum2(pctx->g_x1);
1726 packet_put_bignum2(pctx->g_x2);
1727 packet_put_string(x1_proof, x1_proof_len);
1728 packet_put_string(x2_proof, x2_proof_len);
1729 packet_send();
1730
1731 bzero(x1_proof, x1_proof_len);
1732 bzero(x2_proof, x2_proof_len);
1733 xfree(x1_proof);
1734 xfree(x2_proof);
1735
1736 /* Expect step 1 packet from peer */
1737 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1738 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001739 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1740 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001741
1742 return 1;
1743}
1744
1745void
1746userauth_jpake_cleanup(Authctxt *authctxt)
1747{
1748 debug3("%s: clean up", __func__);
1749 if (authctxt->methoddata != NULL) {
1750 jpake_free(authctxt->methoddata);
1751 authctxt->methoddata = NULL;
1752 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001753 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001754}
1755#endif /* JPAKE */
1756
Damien Miller62cee002000-09-23 17:15:56 +11001757/* find auth method */
1758
Damien Miller62cee002000-09-23 17:15:56 +11001759/*
1760 * given auth method name, if configurable options permit this method fill
1761 * in auth_ident field and return true, otherwise return false.
1762 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001763static int
Damien Miller62cee002000-09-23 17:15:56 +11001764authmethod_is_enabled(Authmethod *method)
1765{
1766 if (method == NULL)
1767 return 0;
1768 /* return false if options indicate this method is disabled */
1769 if (method->enabled == NULL || *method->enabled == 0)
1770 return 0;
1771 /* return false if batch mode is enabled but method needs interactive mode */
1772 if (method->batch_flag != NULL && *method->batch_flag != 0)
1773 return 0;
1774 return 1;
1775}
1776
Ben Lindstrombba81212001-06-25 05:01:22 +00001777static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001778authmethod_lookup(const char *name)
1779{
1780 Authmethod *method = NULL;
1781 if (name != NULL)
1782 for (method = authmethods; method->name != NULL; method++)
1783 if (strcmp(name, method->name) == 0)
1784 return method;
1785 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1786 return NULL;
1787}
1788
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001789/* XXX internal state */
1790static Authmethod *current = NULL;
1791static char *supported = NULL;
1792static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001793
Damien Miller62cee002000-09-23 17:15:56 +11001794/*
1795 * Given the authentication method list sent by the server, return the
1796 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001797 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001798 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001799static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001800authmethod_get(char *authlist)
1801{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001802 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001803 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001804
Damien Miller62cee002000-09-23 17:15:56 +11001805 /* Use a suitable default if we're passed a nil list. */
1806 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001807 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001808
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001809 if (supported == NULL || strcmp(authlist, supported) != 0) {
1810 debug3("start over, passed a different list %s", authlist);
1811 if (supported != NULL)
1812 xfree(supported);
1813 supported = xstrdup(authlist);
1814 preferred = options.preferred_authentications;
1815 debug3("preferred %s", preferred);
1816 current = NULL;
1817 } else if (current != NULL && authmethod_is_enabled(current))
1818 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001819
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001820 for (;;) {
1821 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001822 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001823 current = NULL;
1824 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001825 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001826 preferred += next;
1827 debug3("authmethod_lookup %s", name);
1828 debug3("remaining preferred: %s", preferred);
1829 if ((current = authmethod_lookup(name)) != NULL &&
1830 authmethod_is_enabled(current)) {
1831 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001832 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001833 return current;
1834 }
Damien Millereba71ba2000-04-29 23:57:08 +10001835 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001836}
Damien Miller62cee002000-09-23 17:15:56 +11001837
Ben Lindstrom79073822001-07-04 03:42:30 +00001838static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001839authmethods_get(void)
1840{
1841 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001842 Buffer b;
1843 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001844
Damien Miller0e3b8722002-01-22 23:26:38 +11001845 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001846 for (method = authmethods; method->name != NULL; method++) {
1847 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001848 if (buffer_len(&b) > 0)
1849 buffer_append(&b, ",", 1);
1850 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001851 }
Damien Miller62cee002000-09-23 17:15:56 +11001852 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001853 buffer_append(&b, "\0", 1);
1854 list = xstrdup(buffer_ptr(&b));
1855 buffer_free(&b);
1856 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001857}
Damien Miller01ed2272008-11-05 16:20:46 +11001858