blob: 7f4ff418989c6216d39d7c6c5a7fbe5d86f34b9e [file] [log] [blame]
Damien Millera5103f42014-02-04 11:20:14 +11001/* $OpenBSD: sshconnect2.c,v 1.204 2014/02/02 03:44:32 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 Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
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 Millerd925dcd2010-12-01 12:21:51 +110072#include "hostfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110073
Darren Tucker0efd1552003-08-26 11:49:55 +100074#ifdef GSSAPI
75#include "ssh-gss.h"
76#endif
77
Damien Millereba71ba2000-04-29 23:57:08 +100078/* import */
79extern char *client_version_string;
80extern char *server_version_string;
81extern Options options;
82
83/*
84 * SSH2 key exchange
85 */
86
Ben Lindstrom46c16222000-12-22 01:43:59 +000087u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100088u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100089
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000090char *xxx_host;
91struct sockaddr *xxx_hostaddr;
92
Ben Lindstromf28f6342001-04-04 02:03:04 +000093Kex *xxx_kex = NULL;
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000096verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000097{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100099 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100 return 0;
101}
102
Damien Millerd925dcd2010-12-01 12:21:51 +1100103static char *
104order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
105{
106 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
107 size_t maxlen;
108 struct hostkeys *hostkeys;
109 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000110 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100111
112 /* Find all hostkeys for this hostname */
113 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
114 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000115 for (i = 0; i < options.num_user_hostfiles; i++)
116 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
117 for (i = 0; i < options.num_system_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100119
120 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
121 maxlen = strlen(avail) + 1;
122 first = xmalloc(maxlen);
123 last = xmalloc(maxlen);
124 *first = *last = '\0';
125
126#define ALG_APPEND(to, from) \
127 do { \
128 if (*to != '\0') \
129 strlcat(to, ",", maxlen); \
130 strlcat(to, from, maxlen); \
131 } while (0)
132
133 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
134 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
135 fatal("%s: unknown alg %s", __func__, alg);
136 if (lookup_key_in_hostkeys_by_type(hostkeys,
137 key_type_plain(ktype), NULL))
138 ALG_APPEND(first, alg);
139 else
140 ALG_APPEND(last, alg);
141 }
142#undef ALG_APPEND
143 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
144 if (*first != '\0')
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
146
Darren Tuckera627d422013-06-02 07:31:17 +1000147 free(first);
148 free(last);
149 free(hostname);
150 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100151 free_hostkeys(hostkeys);
152
153 return ret;
154}
155
Damien Millereba71ba2000-04-29 23:57:08 +1000156void
Damien Millerd925dcd2010-12-01 12:21:51 +1100157ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100158{
Damien Miller874d77b2000-10-14 16:23:11 +1100159 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000160
161 xxx_host = host;
162 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100163
Damien Millere39cacc2000-11-29 12:18:44 +1100164 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000165 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100166 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100167 }
168 if (options.ciphers != NULL) {
169 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
170 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
171 }
Damien Millera0ff4662001-03-30 10:49:35 +1000172 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
173 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
174 myproposal[PROPOSAL_ENC_ALGS_STOC] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100176 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000177 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000178 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100179 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000180 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000181 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100182 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000183 if (options.macs != NULL) {
184 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
185 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
186 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000187 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100189 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100190 else {
191 /* Prefer algorithms that we already have keys for */
192 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100193 compat_pkalg_proposal(
194 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100195 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000196 if (options.kex_algorithms != NULL)
197 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100198
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000199 if (options.rekey_limit || options.rekey_interval)
200 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
201 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000202
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000203 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000204 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100205 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000206 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100208 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000209 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Damien Miller1e124262013-11-04 08:26:52 +1100210 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000211 kex->client_version_string=client_version_string;
212 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000213 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100214
Ben Lindstromf28f6342001-04-04 02:03:04 +0000215 xxx_kex = kex;
216
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000217 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100218
Darren Tucker36331b52010-01-08 16:50:41 +1100219 if (options.use_roaming && !kex->roaming) {
220 debug("Roaming not allowed by server");
221 options.use_roaming = 0;
222 }
223
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000224 session_id2 = kex->session_id;
225 session_id2_len = kex->session_id_len;
226
Damien Miller874d77b2000-10-14 16:23:11 +1100227#ifdef DEBUG_KEXDH
228 /* send 1st encrypted/maced/compressed message */
229 packet_start(SSH2_MSG_IGNORE);
230 packet_put_cstring("markus");
231 packet_send();
232 packet_write_wait();
233#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000234}
Damien Millerb1715dc2000-05-30 13:44:51 +1000235
Damien Millereba71ba2000-04-29 23:57:08 +1000236/*
237 * Authenticate user
238 */
Damien Miller62cee002000-09-23 17:15:56 +1100239
240typedef struct Authctxt Authctxt;
241typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000242typedef struct identity Identity;
243typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100244
Damien Miller280ecfb2003-05-14 13:46:00 +1000245struct identity {
246 TAILQ_ENTRY(identity) next;
247 AuthenticationConnection *ac; /* set if agent supports key */
248 Key *key; /* public/private key */
249 char *filename; /* comment for agent-only keys */
250 int tried;
251 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100252 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000253};
254TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100255
256struct Authctxt {
257 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000258 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100259 const char *host;
260 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100261 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000262 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000263 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000265 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266 AuthenticationConnection *agent;
267 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000268 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000269 /* kbd-interactive */
270 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000271 /* generic */
272 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100273};
274struct Authmethod {
275 char *name; /* string to compare against server's list */
276 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100277 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100278 int *enabled; /* flag in option struct that enables method */
279 int *batch_flag; /* flag in option struct that disables method */
280};
281
Damien Miller630d6f42002-01-22 23:17:30 +1100282void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100283void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100284void input_userauth_failure(int, u_int32_t, void *);
285void input_userauth_banner(int, u_int32_t, void *);
286void input_userauth_error(int, u_int32_t, void *);
287void input_userauth_info_req(int, u_int32_t, void *);
288void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000289void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100290
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000291int userauth_none(Authctxt *);
292int userauth_pubkey(Authctxt *);
293int userauth_passwd(Authctxt *);
294int userauth_kbdint(Authctxt *);
295int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100296
Darren Tucker0efd1552003-08-26 11:49:55 +1000297#ifdef GSSAPI
298int userauth_gssapi(Authctxt *authctxt);
299void input_gssapi_response(int type, u_int32_t, void *);
300void input_gssapi_token(int type, u_int32_t, void *);
301void input_gssapi_hash(int type, u_int32_t, void *);
302void input_gssapi_error(int, u_int32_t, void *);
303void input_gssapi_errtok(int, u_int32_t, void *);
304#endif
305
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000306void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000307
Damien Miller280ecfb2003-05-14 13:46:00 +1000308static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000309static void pubkey_prepare(Authctxt *);
310static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100311static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000312
Ben Lindstrombba81212001-06-25 05:01:22 +0000313static Authmethod *authmethod_get(char *authlist);
314static Authmethod *authmethod_lookup(const char *name);
315static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100316
317Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000318#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100319 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000320 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100321 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000322 &options.gss_authentication,
323 NULL},
324#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000325 {"hostbased",
326 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100327 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000328 &options.hostbased_authentication,
329 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000330 {"publickey",
331 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100332 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000333 &options.pubkey_authentication,
334 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100335 {"keyboard-interactive",
336 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100337 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100338 &options.kbd_interactive_authentication,
339 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000340 {"password",
341 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100342 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000343 &options.password_authentication,
344 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100345 {"none",
346 userauth_none,
347 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100348 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100349 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100350 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100351};
352
353void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000354ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000355 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100356{
357 Authctxt authctxt;
358 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100359
Ben Lindstrom551ea372001-06-05 18:56:16 +0000360 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000361 options.kbd_interactive_authentication = 1;
362
Damien Miller62cee002000-09-23 17:15:56 +1100363 packet_start(SSH2_MSG_SERVICE_REQUEST);
364 packet_put_cstring("ssh-userauth");
365 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000366 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100367 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100368 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000369 if (type != SSH2_MSG_SERVICE_ACCEPT)
370 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100371 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100372 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000373 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000374 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100375 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000376 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100377 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100378 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000379 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100380
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000381 if (options.preferred_authentications == NULL)
382 options.preferred_authentications = authmethods_get();
383
Damien Miller62cee002000-09-23 17:15:56 +1100384 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000385 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000386 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100387 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000388 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100389 authctxt.host = host;
390 authctxt.service = "ssh-connection"; /* service name */
391 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100392 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000393 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000394 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000395 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000396 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100397 if (authctxt.method == NULL)
398 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100399
400 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100401 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100402
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000403 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100404 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
405 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000406 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100407 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
408
Damien Miller280ecfb2003-05-14 13:46:00 +1000409 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000410 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
411
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000412 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100413}
Damien Millerf842fcb2003-05-15 12:01:28 +1000414
Damien Miller62cee002000-09-23 17:15:56 +1100415void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000416userauth(Authctxt *authctxt, char *authlist)
417{
Damien Miller01ed2272008-11-05 16:20:46 +1100418 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
419 authctxt->method->cleanup(authctxt);
420
Darren Tuckera627d422013-06-02 07:31:17 +1000421 free(authctxt->methoddata);
422 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000423 if (authlist == NULL) {
424 authlist = authctxt->authlist;
425 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000426 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000427 authctxt->authlist = authlist;
428 }
429 for (;;) {
430 Authmethod *method = authmethod_get(authlist);
431 if (method == NULL)
432 fatal("Permission denied (%s).", authlist);
433 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000434
435 /* reset the per method handler */
436 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
437 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
438
439 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000440 if (method->userauth(authctxt) != 0) {
441 debug2("we sent a %s packet, wait for reply", method->name);
442 break;
443 } else {
444 debug2("we did not send a packet, disable method");
445 method->enabled = NULL;
446 }
447 }
448}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000449
Damien Millerf7475d72008-11-03 19:26:18 +1100450/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000451void
Damien Miller630d6f42002-01-22 23:17:30 +1100452input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100453{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000454 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000455 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000456}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000457
Damien Millerf7475d72008-11-03 19:26:18 +1100458/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000459void
Damien Miller630d6f42002-01-22 23:17:30 +1100460input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000461{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000462 char *msg, *raw, *lang;
463 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000464
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000465 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000466 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000467 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100468 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000469 if (len > 65536)
470 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100471 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100472 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000473 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000474 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000475 }
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(raw);
477 free(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100478}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000479
Damien Millerf7475d72008-11-03 19:26:18 +1100480/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100481void
Damien Miller630d6f42002-01-22 23:17:30 +1100482input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100483{
484 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100485
Damien Miller62cee002000-09-23 17:15:56 +1100486 if (authctxt == NULL)
487 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000488 free(authctxt->authlist);
489 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100490 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
491 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000492 free(authctxt->methoddata);
493 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100494 authctxt->success = 1; /* break out */
495}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000496
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100497void
498input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
499{
500 Authctxt *authctxt = ctxt;
501
502 if (authctxt == NULL)
503 fatal("%s: no authentication context", __func__);
504
505 fatal("Unexpected authentication success during %s.",
506 authctxt->method->name);
507}
508
Damien Millerf7475d72008-11-03 19:26:18 +1100509/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100510void
Damien Miller630d6f42002-01-22 23:17:30 +1100511input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100512{
Damien Miller62cee002000-09-23 17:15:56 +1100513 Authctxt *authctxt = ctxt;
514 char *authlist = NULL;
515 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100516
517 if (authctxt == NULL)
518 fatal("input_userauth_failure: no authentication context");
519
Damien Miller874d77b2000-10-14 16:23:11 +1100520 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100521 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100522 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100523
Damien Miller62e9c4f2013-04-23 15:15:49 +1000524 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000525 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000526 /* reset state */
527 pubkey_cleanup(authctxt);
528 pubkey_prepare(authctxt);
529 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000530 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100531
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000532 userauth(authctxt, authlist);
533}
Damien Millerf7475d72008-11-03 19:26:18 +1100534
535/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000536void
Damien Miller630d6f42002-01-22 23:17:30 +1100537input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000538{
539 Authctxt *authctxt = ctxt;
540 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000541 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000542 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000543 int pktype, sent = 0;
544 u_int alen, blen;
545 char *pkalg, *fp;
546 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000547
548 if (authctxt == NULL)
549 fatal("input_userauth_pk_ok: no authentication context");
550 if (datafellows & SSH_BUG_PKOK) {
551 /* this is similar to SSH_BUG_PKAUTH */
552 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
553 pkblob = packet_get_string(&blen);
554 buffer_init(&b);
555 buffer_append(&b, pkblob, blen);
556 pkalg = buffer_get_string(&b, &alen);
557 buffer_free(&b);
558 } else {
559 pkalg = packet_get_string(&alen);
560 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000561 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100562 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000563
Damien Miller280ecfb2003-05-14 13:46:00 +1000564 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000565
Damien Miller280ecfb2003-05-14 13:46:00 +1000566 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
567 debug("unknown pkalg %s", pkalg);
568 goto done;
569 }
570 if ((key = key_from_blob(pkblob, blen)) == NULL) {
571 debug("no key from blob. pkalg %s", pkalg);
572 goto done;
573 }
574 if (key->type != pktype) {
575 error("input_userauth_pk_ok: type mismatch "
576 "for decoded key (received %d, expected %d)",
577 key->type, pktype);
578 goto done;
579 }
580 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
581 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000582 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000583
Darren Tuckerd05b6012003-10-15 15:55:59 +1000584 /*
585 * search keys in the reverse order, because last candidate has been
586 * moved to the end of the queue. this also avoids confusion by
587 * duplicate keys
588 */
Damien Miller0b51a522004-04-20 20:07:19 +1000589 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000590 if (key_equal(key, id->key)) {
591 sent = sign_and_send_pubkey(authctxt, id);
592 break;
593 }
594 }
595done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596 if (key != NULL)
597 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000598 free(pkalg);
599 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000600
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000601 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000602 if (sent == 0)
603 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100604}
605
Darren Tucker0efd1552003-08-26 11:49:55 +1000606#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100607int
Darren Tucker0efd1552003-08-26 11:49:55 +1000608userauth_gssapi(Authctxt *authctxt)
609{
610 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100611 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000612 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000613 OM_uint32 min;
614 int ok = 0;
615
616 /* Try one GSSAPI method at a time, rather than sending them all at
617 * once. */
618
Darren Tucker56afe142003-11-03 20:06:14 +1100619 if (gss_supported == NULL)
620 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000621
622 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100623 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000624 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100625 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000626 ssh_gssapi_check_mechanism(&gssctxt,
627 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000628 ok = 1; /* Mechanism works */
629 } else {
630 mech++;
631 }
632 }
633
Damien Millera1cb9f32006-08-19 00:33:34 +1000634 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000635 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000636
637 authctxt->methoddata=(void *)gssctxt;
638
639 packet_start(SSH2_MSG_USERAUTH_REQUEST);
640 packet_put_cstring(authctxt->server_user);
641 packet_put_cstring(authctxt->service);
642 packet_put_cstring(authctxt->method->name);
643
644 packet_put_int(1);
645
Darren Tucker655a5e02003-11-03 20:09:03 +1100646 packet_put_int((gss_supported->elements[mech].length) + 2);
647 packet_put_char(SSH_GSS_OIDTYPE);
648 packet_put_char(gss_supported->elements[mech].length);
649 packet_put_raw(gss_supported->elements[mech].elements,
650 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000651
652 packet_send();
653
654 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
655 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
656 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
657 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
658
659 mech++; /* Move along to next candidate */
660
661 return 1;
662}
663
Damien Miller91c6aa42003-11-17 21:20:18 +1100664static OM_uint32
665process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
666{
667 Authctxt *authctxt = ctxt;
668 Gssctxt *gssctxt = authctxt->methoddata;
669 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000670 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
671 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100672 OM_uint32 status, ms, flags;
673 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100674
Damien Miller91c6aa42003-11-17 21:20:18 +1100675 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100676 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100677
678 if (send_tok.length > 0) {
679 if (GSS_ERROR(status))
680 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
681 else
682 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100683
Damien Miller91c6aa42003-11-17 21:20:18 +1100684 packet_put_string(send_tok.value, send_tok.length);
685 packet_send();
686 gss_release_buffer(&ms, &send_tok);
687 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100688
Damien Miller91c6aa42003-11-17 21:20:18 +1100689 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100690 /* send either complete or MIC, depending on mechanism */
691 if (!(flags & GSS_C_INTEG_FLAG)) {
692 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
693 packet_send();
694 } else {
695 ssh_gssapi_buildmic(&b, authctxt->server_user,
696 authctxt->service, "gssapi-with-mic");
697
698 gssbuf.value = buffer_ptr(&b);
699 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100700
Damien Miller0425d402003-11-17 22:18:21 +1100701 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100702
Damien Miller0425d402003-11-17 22:18:21 +1100703 if (!GSS_ERROR(status)) {
704 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
705 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100706
Damien Miller0425d402003-11-17 22:18:21 +1100707 packet_send();
708 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100709
Damien Miller0425d402003-11-17 22:18:21 +1100710 buffer_free(&b);
711 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100712 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100713 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100714
Damien Miller91c6aa42003-11-17 21:20:18 +1100715 return status;
716}
717
Damien Millerf7475d72008-11-03 19:26:18 +1100718/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000719void
720input_gssapi_response(int type, u_int32_t plen, void *ctxt)
721{
722 Authctxt *authctxt = ctxt;
723 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000724 int oidlen;
725 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000726
727 if (authctxt == NULL)
728 fatal("input_gssapi_response: no authentication context");
729 gssctxt = authctxt->methoddata;
730
731 /* Setup our OID */
732 oidv = packet_get_string(&oidlen);
733
Darren Tucker655a5e02003-11-03 20:09:03 +1100734 if (oidlen <= 2 ||
735 oidv[0] != SSH_GSS_OIDTYPE ||
736 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000737 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100738 debug("Badly encoded mechanism OID received");
739 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100740 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000741 }
742
Darren Tucker655a5e02003-11-03 20:09:03 +1100743 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
744 fatal("Server returned different OID than expected");
745
Darren Tucker0efd1552003-08-26 11:49:55 +1000746 packet_check_eom();
747
Darren Tuckera627d422013-06-02 07:31:17 +1000748 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000749
Damien Miller91c6aa42003-11-17 21:20:18 +1100750 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000751 /* Start again with next method on list */
752 debug("Trying to start again");
753 userauth(authctxt, NULL);
754 return;
755 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000756}
757
Damien Millerf7475d72008-11-03 19:26:18 +1100758/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000759void
760input_gssapi_token(int type, u_int32_t plen, void *ctxt)
761{
762 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000763 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100764 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000765 u_int slen;
766
767 if (authctxt == NULL)
768 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000769
770 recv_tok.value = packet_get_string(&slen);
771 recv_tok.length = slen; /* safe typecast */
772
773 packet_check_eom();
774
Damien Miller91c6aa42003-11-17 21:20:18 +1100775 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000776
Darren Tuckera627d422013-06-02 07:31:17 +1000777 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000778
779 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000780 /* Start again with the next method in the list */
781 userauth(authctxt, NULL);
782 return;
783 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000784}
785
Damien Millerf7475d72008-11-03 19:26:18 +1100786/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000787void
788input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
789{
790 Authctxt *authctxt = ctxt;
791 Gssctxt *gssctxt;
792 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
793 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000794 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000795 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000796
797 if (authctxt == NULL)
798 fatal("input_gssapi_response: no authentication context");
799 gssctxt = authctxt->methoddata;
800
Darren Tucker600ad8d2003-08-26 12:10:48 +1000801 recv_tok.value = packet_get_string(&len);
802 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000803
804 packet_check_eom();
805
806 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000807 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000808 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000809
Darren Tuckera627d422013-06-02 07:31:17 +1000810 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000811 gss_release_buffer(&ms, &send_tok);
812
813 /* Server will be returning a failed packet after this one */
814}
815
Damien Millerf7475d72008-11-03 19:26:18 +1100816/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000817void
818input_gssapi_error(int type, u_int32_t plen, void *ctxt)
819{
Darren Tucker0efd1552003-08-26 11:49:55 +1000820 char *msg;
821 char *lang;
822
Damien Millerd677ad12013-04-23 15:18:51 +1000823 /* maj */(void)packet_get_int();
824 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000825 msg=packet_get_string(NULL);
826 lang=packet_get_string(NULL);
827
828 packet_check_eom();
829
Damien Miller15d72a02005-11-05 15:07:33 +1100830 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000831 free(msg);
832 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000833}
834#endif /* GSSAPI */
835
Damien Millereba71ba2000-04-29 23:57:08 +1000836int
Damien Miller874d77b2000-10-14 16:23:11 +1100837userauth_none(Authctxt *authctxt)
838{
839 /* initial userauth request */
840 packet_start(SSH2_MSG_USERAUTH_REQUEST);
841 packet_put_cstring(authctxt->server_user);
842 packet_put_cstring(authctxt->service);
843 packet_put_cstring(authctxt->method->name);
844 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100845 return 1;
846}
847
848int
Damien Miller62cee002000-09-23 17:15:56 +1100849userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000850{
Damien Millere247cc42000-05-07 12:03:14 +1000851 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000852 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000853 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100854 const char *host = options.host_key_alias ? options.host_key_alias :
855 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000856
Damien Millerd3a18572000-06-07 19:55:44 +1000857 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000858 return 0;
859
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000860 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000861 error("Permission denied, please try again.");
862
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000863 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100864 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000865 password = read_passphrase(prompt, 0);
866 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100867 packet_put_cstring(authctxt->server_user);
868 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100869 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000870 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000871 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100872 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000873 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100874 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000875 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000876
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000877 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000878 &input_userauth_passwd_changereq);
879
Damien Millereba71ba2000-04-29 23:57:08 +1000880 return 1;
881}
Damien Millerf7475d72008-11-03 19:26:18 +1100882
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000883/*
884 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
885 */
Damien Millerf7475d72008-11-03 19:26:18 +1100886/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000887void
Tim Ricec8549622002-03-31 12:49:38 -0800888input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000889{
890 Authctxt *authctxt = ctxt;
891 char *info, *lang, *password = NULL, *retype = NULL;
892 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100893 const char *host = options.host_key_alias ? options.host_key_alias :
894 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000895
896 debug2("input_userauth_passwd_changereq");
897
898 if (authctxt == NULL)
899 fatal("input_userauth_passwd_changereq: "
900 "no authentication context");
901
902 info = packet_get_string(NULL);
903 lang = packet_get_string(NULL);
904 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000905 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000906 free(info);
907 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000908 packet_start(SSH2_MSG_USERAUTH_REQUEST);
909 packet_put_cstring(authctxt->server_user);
910 packet_put_cstring(authctxt->service);
911 packet_put_cstring(authctxt->method->name);
912 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000913 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000914 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100915 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000916 password = read_passphrase(prompt, 0);
917 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100918 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000919 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000920 password = NULL;
921 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000922 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000923 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100924 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000925 password = read_passphrase(prompt, RP_ALLOW_EOF);
926 if (password == NULL) {
927 /* bail out */
928 return;
929 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000930 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000931 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100932 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000933 retype = read_passphrase(prompt, 0);
934 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100935 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000937 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000938 password = NULL;
939 }
Damien Millera5103f42014-02-04 11:20:14 +1100940 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000942 }
943 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100944 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000945 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000946 packet_add_padding(64);
947 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000948
949 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000950 &input_userauth_passwd_changereq);
951}
Damien Millereba71ba2000-04-29 23:57:08 +1000952
Ben Lindstrombba81212001-06-25 05:01:22 +0000953static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000954identity_sign(Identity *id, u_char **sigp, u_int *lenp,
955 u_char *data, u_int datalen)
956{
957 Key *prv;
958 int ret;
959
960 /* the agent supports this key */
961 if (id->ac)
962 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
963 data, datalen));
964 /*
965 * we have already loaded the private key or
966 * the private key is stored in external hardware
967 */
968 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
969 return (key_sign(id->key, sigp, lenp, data, datalen));
970 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +1100971 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
Damien Miller280ecfb2003-05-14 13:46:00 +1000972 return (-1);
973 ret = key_sign(prv, sigp, lenp, data, datalen);
974 key_free(prv);
975 return (ret);
976}
977
978static int
979sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000980{
981 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000982 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000983 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000984 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000985 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100986 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +1000987 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000988
Damien Miller4e270b02010-04-16 15:56:21 +1000989 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
990 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000991 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000992
Damien Miller280ecfb2003-05-14 13:46:00 +1000993 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000995 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100996 return 0;
997 }
Damien Millereba71ba2000-04-29 23:57:08 +1000998 /* data to be signed */
999 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001000 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001001 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001002 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001003 } else {
1004 buffer_put_string(&b, session_id2, session_id2_len);
1005 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001006 }
Damien Millereba71ba2000-04-29 23:57:08 +10001007 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001008 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001009 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001010 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001011 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001012 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001013 if (datafellows & SSH_BUG_PKAUTH) {
1014 buffer_put_char(&b, have_sig);
1015 } else {
1016 buffer_put_cstring(&b, authctxt->method->name);
1017 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001018 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001019 }
Damien Millereba71ba2000-04-29 23:57:08 +10001020 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001021
1022 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001023 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001024 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001025 if (ret == -1) {
Darren Tuckera627d422013-06-02 07:31:17 +10001026 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001027 buffer_free(&b);
1028 return 0;
1029 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001030#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001031 buffer_dump(&b);
1032#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001033 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001034 buffer_clear(&b);
1035 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001036 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001037 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001038 buffer_put_cstring(&b, authctxt->server_user);
1039 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001040 buffer_put_cstring(&b, authctxt->method->name);
1041 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001042 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001043 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001044 buffer_put_string(&b, blob, bloblen);
1045 }
Darren Tuckera627d422013-06-02 07:31:17 +10001046 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001047
Damien Millereba71ba2000-04-29 23:57:08 +10001048 /* append signature */
1049 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001050 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001051
1052 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001053 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001054 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001055 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001056
1057 /* put remaining data from buffer into packet */
1058 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1059 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1060 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001061 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001062
1063 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001064}
1065
Ben Lindstrombba81212001-06-25 05:01:22 +00001066static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001067send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001068{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001069 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001070 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001071
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001072 debug3("send_pubkey_test");
1073
Damien Miller280ecfb2003-05-14 13:46:00 +10001074 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001075 /* we cannot handle this key */
1076 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001077 return 0;
1078 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001079 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001080 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001081
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001082 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1083 packet_put_cstring(authctxt->server_user);
1084 packet_put_cstring(authctxt->service);
1085 packet_put_cstring(authctxt->method->name);
1086 packet_put_char(have_sig);
1087 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001088 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001089 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001090 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001091 packet_send();
1092 return 1;
1093}
1094
Ben Lindstrombba81212001-06-25 05:01:22 +00001095static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001096load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001097{
1098 Key *private;
1099 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001100 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001101 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001102
Ben Lindstrom329782e2001-03-10 17:08:59 +00001103 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001104 (userprovided ? logit : debug3)("no such identity: %s: %s",
1105 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001106 return NULL;
1107 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001108 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
Darren Tuckere52a2602013-06-06 08:22:05 +10001109 if (!perm_ok) {
1110 if (private != NULL)
1111 key_free(private);
Darren Tucker232b76f2006-05-06 17:41:51 +10001112 return NULL;
Darren Tuckere52a2602013-06-06 08:22:05 +10001113 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001114 if (private == NULL) {
1115 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001116 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001117 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001118 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001119 for (i = 0; i < options.number_of_password_prompts; i++) {
1120 passphrase = read_passphrase(prompt, 0);
1121 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001122 private = key_load_private_type(KEY_UNSPEC,
1123 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001124 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001125 } else {
1126 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001127 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001128 }
Damien Millera5103f42014-02-04 11:20:14 +11001129 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001130 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001131 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001132 break;
1133 debug2("bad passphrase given, try again...");
1134 }
Damien Miller994cf142000-07-21 10:19:44 +10001135 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001136 return private;
1137}
1138
Damien Miller280ecfb2003-05-14 13:46:00 +10001139/*
1140 * try keys in the following order:
1141 * 1. agent keys that are found in the config file
1142 * 2. other agent keys
1143 * 3. keys that are only listed in the config file
1144 */
1145static void
1146pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001147{
Damien Millercb6b68b2012-12-03 09:49:52 +11001148 Identity *id, *id2, *tmp;
Damien Miller280ecfb2003-05-14 13:46:00 +10001149 Idlist agent, files, *preferred;
1150 Key *key;
1151 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001152 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001153 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001154
Damien Miller280ecfb2003-05-14 13:46:00 +10001155 TAILQ_INIT(&agent); /* keys from the agent */
1156 TAILQ_INIT(&files); /* keys from the config file */
1157 preferred = &authctxt->keys;
1158 TAILQ_INIT(preferred); /* preferred order of keys */
1159
Damien Millercb6b68b2012-12-03 09:49:52 +11001160 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001161 for (i = 0; i < options.num_identity_files; i++) {
1162 key = options.identity_keys[i];
1163 if (key && key->type == KEY_RSA1)
1164 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001165 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1166 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001167 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001168 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001169 id->key = key;
1170 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001171 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001172 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001173 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001174 /* Prefer PKCS11 keys that are explicitly listed */
1175 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1176 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1177 continue;
1178 found = 0;
1179 TAILQ_FOREACH(id2, &files, next) {
1180 if (id2->key == NULL ||
1181 (id2->key->flags & KEY_FLAG_EXT) != 0)
1182 continue;
1183 if (key_equal(id->key, id2->key)) {
1184 TAILQ_REMOVE(&files, id, next);
1185 TAILQ_INSERT_TAIL(preferred, id, next);
1186 found = 1;
1187 break;
1188 }
1189 }
1190 /* If IdentitiesOnly set and key not found then don't use it */
1191 if (!found && options.identities_only) {
1192 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001193 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001194 free(id);
1195 }
1196 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001197 /* list of keys supported by the agent */
1198 if ((ac = ssh_get_authentication_connection())) {
1199 for (key = ssh_get_first_identity(ac, &comment, 2);
1200 key != NULL;
1201 key = ssh_get_next_identity(ac, &comment, 2)) {
1202 found = 0;
1203 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001204 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001205 if (key_equal(key, id->key)) {
1206 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001207 free(comment);
Damien Miller280ecfb2003-05-14 13:46:00 +10001208 TAILQ_REMOVE(&files, id, next);
1209 TAILQ_INSERT_TAIL(preferred, id, next);
1210 id->ac = ac;
1211 found = 1;
1212 break;
1213 }
1214 }
Damien Millerbd394c32004-03-08 23:12:36 +11001215 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001216 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001217 id->key = key;
1218 id->filename = comment;
1219 id->ac = ac;
1220 TAILQ_INSERT_TAIL(&agent, id, next);
1221 }
1222 }
1223 /* append remaining agent keys */
1224 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1225 TAILQ_REMOVE(&agent, id, next);
1226 TAILQ_INSERT_TAIL(preferred, id, next);
1227 }
1228 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001229 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001230 /* append remaining keys from the config file */
1231 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1232 TAILQ_REMOVE(&files, id, next);
1233 TAILQ_INSERT_TAIL(preferred, id, next);
1234 }
1235 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001236 debug2("key: %s (%p),%s", id->filename, id->key,
1237 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001238 }
1239}
1240
1241static void
1242pubkey_cleanup(Authctxt *authctxt)
1243{
1244 Identity *id;
1245
1246 if (authctxt->agent != NULL)
1247 ssh_close_authentication_connection(authctxt->agent);
1248 for (id = TAILQ_FIRST(&authctxt->keys); id;
1249 id = TAILQ_FIRST(&authctxt->keys)) {
1250 TAILQ_REMOVE(&authctxt->keys, id, next);
1251 if (id->key)
1252 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001253 free(id->filename);
1254 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001255 }
Damien Millereba71ba2000-04-29 23:57:08 +10001256}
1257
Damien Miller62cee002000-09-23 17:15:56 +11001258int
1259userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001260{
Damien Miller280ecfb2003-05-14 13:46:00 +10001261 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001262 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001263
Damien Miller280ecfb2003-05-14 13:46:00 +10001264 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1265 if (id->tried++)
1266 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001267 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001268 TAILQ_REMOVE(&authctxt->keys, id, next);
1269 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1270 /*
1271 * send a test message if we have the public key. for
1272 * encrypted keys we cannot do this and have to load the
1273 * private key instead
1274 */
Damien Miller324541e2013-12-31 12:25:40 +11001275 if (id->key != NULL) {
1276 if (key_type_plain(id->key->type) == KEY_RSA &&
1277 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1278 debug("Skipped %s key %s for RSA/MD5 server",
1279 key_type(id->key), id->filename);
1280 } else if (id->key->type != KEY_RSA1) {
1281 debug("Offering %s public key: %s",
1282 key_type(id->key), id->filename);
1283 sent = send_pubkey_test(authctxt, id);
1284 }
1285 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001286 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001287 id->key = load_identity_file(id->filename,
1288 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001289 if (id->key != NULL) {
1290 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001291 if (key_type_plain(id->key->type) == KEY_RSA &&
1292 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1293 debug("Skipped %s key %s for RSA/MD5 "
1294 "server", key_type(id->key),
1295 id->filename);
1296 } else {
1297 sent = sign_and_send_pubkey(
1298 authctxt, id);
1299 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001300 key_free(id->key);
1301 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001302 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001303 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001304 if (sent)
1305 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001306 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001307 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001308}
Damien Millereba71ba2000-04-29 23:57:08 +10001309
Damien Miller874d77b2000-10-14 16:23:11 +11001310/*
1311 * Send userauth request message specifying keyboard-interactive method.
1312 */
1313int
1314userauth_kbdint(Authctxt *authctxt)
1315{
1316 static int attempt = 0;
1317
1318 if (attempt++ >= options.number_of_password_prompts)
1319 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001320 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1321 if (attempt > 1 && !authctxt->info_req_seen) {
1322 debug3("userauth_kbdint: disable: no info_req_seen");
1323 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1324 return 0;
1325 }
Damien Miller874d77b2000-10-14 16:23:11 +11001326
1327 debug2("userauth_kbdint");
1328 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1329 packet_put_cstring(authctxt->server_user);
1330 packet_put_cstring(authctxt->service);
1331 packet_put_cstring(authctxt->method->name);
1332 packet_put_cstring(""); /* lang */
1333 packet_put_cstring(options.kbd_interactive_devices ?
1334 options.kbd_interactive_devices : "");
1335 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001336
1337 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1338 return 1;
1339}
1340
1341/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001342 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001343 */
1344void
Damien Miller630d6f42002-01-22 23:17:30 +11001345input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001346{
1347 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001348 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001349 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001350 int echo = 0;
1351
1352 debug2("input_userauth_info_req");
1353
1354 if (authctxt == NULL)
1355 fatal("input_userauth_info_req: no authentication context");
1356
Ben Lindstrom7d199962001-09-12 18:29:00 +00001357 authctxt->info_req_seen = 1;
1358
Damien Miller874d77b2000-10-14 16:23:11 +11001359 name = packet_get_string(NULL);
1360 inst = packet_get_string(NULL);
1361 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001362 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001363 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001364 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001365 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001366 free(name);
1367 free(inst);
1368 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001369
1370 num_prompts = packet_get_int();
1371 /*
1372 * Begin to build info response packet based on prompts requested.
1373 * We commit to providing the correct number of responses, so if
1374 * further on we run into a problem that prevents this, we have to
1375 * be sure and clean this up and send a correct error response.
1376 */
1377 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1378 packet_put_int(num_prompts);
1379
Ben Lindstrom551ea372001-06-05 18:56:16 +00001380 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001381 for (i = 0; i < num_prompts; i++) {
1382 prompt = packet_get_string(NULL);
1383 echo = packet_get_char();
1384
Ben Lindstrom949974b2001-06-25 05:20:31 +00001385 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001386
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001387 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001388 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001389 free(response);
1390 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001391 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001392 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001393
Damien Miller9f643902001-11-12 11:02:52 +11001394 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001395 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001396}
Damien Miller62cee002000-09-23 17:15:56 +11001397
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001398static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001399ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001400 u_char *data, u_int datalen)
1401{
1402 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001403 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001404 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001405 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001406
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001407 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001408
Ben Lindstrom5206b952002-06-06 19:59:29 +00001409 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001410 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001411 return -1;
1412 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001413 if (fflush(stdout) != 0)
1414 error("ssh_keysign: fflush: %s", strerror(errno));
1415 if (pipe(to) < 0) {
1416 error("ssh_keysign: pipe: %s", strerror(errno));
1417 return -1;
1418 }
1419 if (pipe(from) < 0) {
1420 error("ssh_keysign: pipe: %s", strerror(errno));
1421 return -1;
1422 }
1423 if ((pid = fork()) < 0) {
1424 error("ssh_keysign: fork: %s", strerror(errno));
1425 return -1;
1426 }
1427 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001428 /* keep the socket on exec */
1429 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001430 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001431 close(from[0]);
1432 if (dup2(from[1], STDOUT_FILENO) < 0)
1433 fatal("ssh_keysign: dup2: %s", strerror(errno));
1434 close(to[1]);
1435 if (dup2(to[0], STDIN_FILENO) < 0)
1436 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001437 close(from[1]);
1438 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001439 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001440 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1441 strerror(errno));
1442 }
1443 close(from[1]);
1444 close(to[0]);
1445
1446 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001447 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001448 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001449 if (ssh_msg_send(to[1], version, &b) == -1)
1450 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001451
Damien Miller901119b2002-10-04 11:10:04 +10001452 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001453 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001454 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001455 return -1;
1456 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001457 close(from[0]);
1458 close(to[1]);
1459
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001460 while (waitpid(pid, &status, 0) < 0)
1461 if (errno != EINTR)
1462 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001463
Ben Lindstrom5206b952002-06-06 19:59:29 +00001464 if (buffer_get_char(&b) != version) {
1465 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001466 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001467 return -1;
1468 }
1469 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001470 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001471
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001472 return 0;
1473}
1474
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001475int
1476userauth_hostbased(Authctxt *authctxt)
1477{
1478 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001479 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001480 Buffer b;
1481 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001482 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001483 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001484 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001485 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001486
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001487 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001488 for (i = 0; i < sensitive->nkeys; i++) {
1489 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001490 if (private && private->type != KEY_RSA1) {
1491 found = 1;
1492 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001493 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001494 break;
1495 }
1496 }
1497 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001498 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001499 return 0;
1500 }
1501 if (key_to_blob(private, &blob, &blen) == 0) {
1502 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001503 return 0;
1504 }
Damien Miller91c18472001-11-12 11:02:03 +11001505 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001506 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001507 if (p == NULL) {
1508 error("userauth_hostbased: cannot get local ipaddr/name");
1509 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001510 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001511 return 0;
1512 }
Damien Miller07d86be2006-03-26 14:19:21 +11001513 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001514 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001515 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001516
Ben Lindstrom671388f2001-04-19 20:40:45 +00001517 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1518 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001519 pkalg = xstrdup(key_ssh_name(private));
1520 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001521 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001522 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001523 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1524 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001525 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001526 buffer_put_cstring(&b, authctxt->method->name);
1527 buffer_put_cstring(&b, pkalg);
1528 buffer_put_string(&b, blob, blen);
1529 buffer_put_cstring(&b, chost);
1530 buffer_put_cstring(&b, authctxt->local_user);
1531#ifdef DEBUG_PK
1532 buffer_dump(&b);
1533#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001534 if (sensitive->external_keysign)
1535 ok = ssh_keysign(private, &signature, &slen,
1536 buffer_ptr(&b), buffer_len(&b));
1537 else
1538 ok = key_sign(private, &signature, &slen,
1539 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001540 key_free(private);
1541 buffer_free(&b);
1542 if (ok != 0) {
1543 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001544 free(chost);
1545 free(pkalg);
1546 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001547 return 0;
1548 }
1549 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1550 packet_put_cstring(authctxt->server_user);
1551 packet_put_cstring(authctxt->service);
1552 packet_put_cstring(authctxt->method->name);
1553 packet_put_cstring(pkalg);
1554 packet_put_string(blob, blen);
1555 packet_put_cstring(chost);
1556 packet_put_cstring(authctxt->local_user);
1557 packet_put_string(signature, slen);
Damien Millera5103f42014-02-04 11:20:14 +11001558 explicit_bzero(signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001559 free(signature);
1560 free(chost);
1561 free(pkalg);
1562 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001563
1564 packet_send();
1565 return 1;
1566}
1567
Damien Miller62cee002000-09-23 17:15:56 +11001568/* find auth method */
1569
Damien Miller62cee002000-09-23 17:15:56 +11001570/*
1571 * given auth method name, if configurable options permit this method fill
1572 * in auth_ident field and return true, otherwise return false.
1573 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001574static int
Damien Miller62cee002000-09-23 17:15:56 +11001575authmethod_is_enabled(Authmethod *method)
1576{
1577 if (method == NULL)
1578 return 0;
1579 /* return false if options indicate this method is disabled */
1580 if (method->enabled == NULL || *method->enabled == 0)
1581 return 0;
1582 /* return false if batch mode is enabled but method needs interactive mode */
1583 if (method->batch_flag != NULL && *method->batch_flag != 0)
1584 return 0;
1585 return 1;
1586}
1587
Ben Lindstrombba81212001-06-25 05:01:22 +00001588static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001589authmethod_lookup(const char *name)
1590{
1591 Authmethod *method = NULL;
1592 if (name != NULL)
1593 for (method = authmethods; method->name != NULL; method++)
1594 if (strcmp(name, method->name) == 0)
1595 return method;
1596 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1597 return NULL;
1598}
1599
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001600/* XXX internal state */
1601static Authmethod *current = NULL;
1602static char *supported = NULL;
1603static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001604
Damien Miller62cee002000-09-23 17:15:56 +11001605/*
1606 * Given the authentication method list sent by the server, return the
1607 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001608 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001609 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001610static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001611authmethod_get(char *authlist)
1612{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001613 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001614 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001615
Damien Miller62cee002000-09-23 17:15:56 +11001616 /* Use a suitable default if we're passed a nil list. */
1617 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001618 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001619
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001620 if (supported == NULL || strcmp(authlist, supported) != 0) {
1621 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001622 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001623 supported = xstrdup(authlist);
1624 preferred = options.preferred_authentications;
1625 debug3("preferred %s", preferred);
1626 current = NULL;
1627 } else if (current != NULL && authmethod_is_enabled(current))
1628 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001629
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001630 for (;;) {
1631 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001632 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001633 current = NULL;
1634 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001635 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001636 preferred += next;
1637 debug3("authmethod_lookup %s", name);
1638 debug3("remaining preferred: %s", preferred);
1639 if ((current = authmethod_lookup(name)) != NULL &&
1640 authmethod_is_enabled(current)) {
1641 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001642 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001643 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001644 return current;
1645 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001646 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001647 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001648}
Damien Miller62cee002000-09-23 17:15:56 +11001649
Ben Lindstrom79073822001-07-04 03:42:30 +00001650static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001651authmethods_get(void)
1652{
1653 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001654 Buffer b;
1655 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001656
Damien Miller0e3b8722002-01-22 23:26:38 +11001657 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001658 for (method = authmethods; method->name != NULL; method++) {
1659 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001660 if (buffer_len(&b) > 0)
1661 buffer_append(&b, ",", 1);
1662 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001663 }
Damien Miller62cee002000-09-23 17:15:56 +11001664 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001665 buffer_append(&b, "\0", 1);
1666 list = xstrdup(buffer_ptr(&b));
1667 buffer_free(&b);
1668 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001669}
Damien Miller01ed2272008-11-05 16:20:46 +11001670