blob: 658398436721d8074f9e4149174073db0919fd5a [file] [log] [blame]
Darren Tucker9f92c532014-06-11 07:57:58 +10001/* $OpenBSD: sshconnect2.c,v 1.208 2014/06/05 22:17:50 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 Miller9235a032014-04-20 13:17:20 +1000159 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
Damien Miller874d77b2000-10-14 16:23:11 +1100160 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000161
162 xxx_host = host;
163 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100164
Damien Millere39cacc2000-11-29 12:18:44 +1100165 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000166 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100167 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100168 }
169 if (options.ciphers != NULL) {
170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
171 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
172 }
Damien Millera0ff4662001-03-30 10:49:35 +1000173 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
174 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
175 myproposal[PROPOSAL_ENC_ALGS_STOC] =
176 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100177 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000178 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000179 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100180 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000181 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000182 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100183 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000184 if (options.macs != NULL) {
185 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
186 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
187 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000188 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100189 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100190 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100191 else {
192 /* Prefer algorithms that we already have keys for */
193 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100194 compat_pkalg_proposal(
195 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100196 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000197 if (options.kex_algorithms != NULL)
198 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller9395b282014-04-20 13:25:30 +1000199 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
200 myproposal[PROPOSAL_KEX_ALGS]);
Damien Miller874d77b2000-10-14 16:23:11 +1100201
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000202 if (options.rekey_limit || options.rekey_interval)
203 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
204 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000205
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000206 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000207 kex = kex_setup(myproposal);
Damien Miller1f0311c2014-05-15 14:24:09 +1000208#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100209 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000210 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100211 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100212 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000213 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Damien Miller1f0311c2014-05-15 14:24:09 +1000214#endif
Damien Miller1e124262013-11-04 08:26:52 +1100215 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000216 kex->client_version_string=client_version_string;
217 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000218 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100219
Ben Lindstromf28f6342001-04-04 02:03:04 +0000220 xxx_kex = kex;
221
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000222 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100223
Darren Tucker36331b52010-01-08 16:50:41 +1100224 if (options.use_roaming && !kex->roaming) {
225 debug("Roaming not allowed by server");
226 options.use_roaming = 0;
227 }
228
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000229 session_id2 = kex->session_id;
230 session_id2_len = kex->session_id_len;
231
Damien Miller874d77b2000-10-14 16:23:11 +1100232#ifdef DEBUG_KEXDH
233 /* send 1st encrypted/maced/compressed message */
234 packet_start(SSH2_MSG_IGNORE);
235 packet_put_cstring("markus");
236 packet_send();
237 packet_write_wait();
238#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000239}
Damien Millerb1715dc2000-05-30 13:44:51 +1000240
Damien Millereba71ba2000-04-29 23:57:08 +1000241/*
242 * Authenticate user
243 */
Damien Miller62cee002000-09-23 17:15:56 +1100244
245typedef struct Authctxt Authctxt;
246typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000247typedef struct identity Identity;
248typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100249
Damien Miller280ecfb2003-05-14 13:46:00 +1000250struct identity {
251 TAILQ_ENTRY(identity) next;
252 AuthenticationConnection *ac; /* set if agent supports key */
253 Key *key; /* public/private key */
254 char *filename; /* comment for agent-only keys */
255 int tried;
256 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100257 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000258};
259TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100260
261struct Authctxt {
262 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100264 const char *host;
265 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100266 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000267 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000268 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000269 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000270 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000271 AuthenticationConnection *agent;
272 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000273 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000274 /* kbd-interactive */
275 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000276 /* generic */
277 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100278};
279struct Authmethod {
280 char *name; /* string to compare against server's list */
281 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100282 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100283 int *enabled; /* flag in option struct that enables method */
284 int *batch_flag; /* flag in option struct that disables method */
285};
286
Damien Miller630d6f42002-01-22 23:17:30 +1100287void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100288void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100289void input_userauth_failure(int, u_int32_t, void *);
290void input_userauth_banner(int, u_int32_t, void *);
291void input_userauth_error(int, u_int32_t, void *);
292void input_userauth_info_req(int, u_int32_t, void *);
293void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000294void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100295
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000296int userauth_none(Authctxt *);
297int userauth_pubkey(Authctxt *);
298int userauth_passwd(Authctxt *);
299int userauth_kbdint(Authctxt *);
300int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100301
Darren Tucker0efd1552003-08-26 11:49:55 +1000302#ifdef GSSAPI
303int userauth_gssapi(Authctxt *authctxt);
304void input_gssapi_response(int type, u_int32_t, void *);
305void input_gssapi_token(int type, u_int32_t, void *);
306void input_gssapi_hash(int type, u_int32_t, void *);
307void input_gssapi_error(int, u_int32_t, void *);
308void input_gssapi_errtok(int, u_int32_t, void *);
309#endif
310
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000311void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000312
Damien Miller280ecfb2003-05-14 13:46:00 +1000313static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000314static void pubkey_prepare(Authctxt *);
315static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100316static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static Authmethod *authmethod_get(char *authlist);
319static Authmethod *authmethod_lookup(const char *name);
320static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100321
322Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000323#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100324 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000325 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100326 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000327 &options.gss_authentication,
328 NULL},
329#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000330 {"hostbased",
331 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100332 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000333 &options.hostbased_authentication,
334 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000335 {"publickey",
336 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100337 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000338 &options.pubkey_authentication,
339 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100340 {"keyboard-interactive",
341 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100342 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100343 &options.kbd_interactive_authentication,
344 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000345 {"password",
346 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100347 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000348 &options.password_authentication,
349 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100350 {"none",
351 userauth_none,
352 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100353 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100354 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100355 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100356};
357
358void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000359ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000360 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100361{
362 Authctxt authctxt;
363 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100364
Ben Lindstrom551ea372001-06-05 18:56:16 +0000365 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000366 options.kbd_interactive_authentication = 1;
367
Damien Miller62cee002000-09-23 17:15:56 +1100368 packet_start(SSH2_MSG_SERVICE_REQUEST);
369 packet_put_cstring("ssh-userauth");
370 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000371 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100372 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100373 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000374 if (type != SSH2_MSG_SERVICE_ACCEPT)
375 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100376 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100377 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000378 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100380 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000381 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100382 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100383 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000384 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100385
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000386 if (options.preferred_authentications == NULL)
387 options.preferred_authentications = authmethods_get();
388
Damien Miller62cee002000-09-23 17:15:56 +1100389 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000390 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000391 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100392 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000393 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100394 authctxt.host = host;
395 authctxt.service = "ssh-connection"; /* service name */
396 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100397 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000398 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000399 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000400 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000401 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100402 if (authctxt.method == NULL)
403 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100404
405 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100406 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100407
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000408 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100409 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
410 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000411 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100412 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
413
Damien Miller280ecfb2003-05-14 13:46:00 +1000414 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000415 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
416
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000417 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100418}
Damien Millerf842fcb2003-05-15 12:01:28 +1000419
Damien Miller62cee002000-09-23 17:15:56 +1100420void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000421userauth(Authctxt *authctxt, char *authlist)
422{
Damien Miller01ed2272008-11-05 16:20:46 +1100423 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
424 authctxt->method->cleanup(authctxt);
425
Darren Tuckera627d422013-06-02 07:31:17 +1000426 free(authctxt->methoddata);
427 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000428 if (authlist == NULL) {
429 authlist = authctxt->authlist;
430 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000431 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000432 authctxt->authlist = authlist;
433 }
434 for (;;) {
435 Authmethod *method = authmethod_get(authlist);
436 if (method == NULL)
437 fatal("Permission denied (%s).", authlist);
438 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000439
440 /* reset the per method handler */
441 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
442 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
443
444 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000445 if (method->userauth(authctxt) != 0) {
446 debug2("we sent a %s packet, wait for reply", method->name);
447 break;
448 } else {
449 debug2("we did not send a packet, disable method");
450 method->enabled = NULL;
451 }
452 }
453}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000454
Damien Millerf7475d72008-11-03 19:26:18 +1100455/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000456void
Damien Miller630d6f42002-01-22 23:17:30 +1100457input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100458{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000459 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000460 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000461}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000462
Damien Millerf7475d72008-11-03 19:26:18 +1100463/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000464void
Damien Miller630d6f42002-01-22 23:17:30 +1100465input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000466{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000467 char *msg, *raw, *lang;
468 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000469
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000470 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000471 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000472 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100473 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000474 if (len > 65536)
475 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100476 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100477 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000478 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000479 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000480 }
Darren Tuckera627d422013-06-02 07:31:17 +1000481 free(raw);
482 free(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100483}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000484
Damien Millerf7475d72008-11-03 19:26:18 +1100485/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100486void
Damien Miller630d6f42002-01-22 23:17:30 +1100487input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100488{
489 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100490
Damien Miller62cee002000-09-23 17:15:56 +1100491 if (authctxt == NULL)
492 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000493 free(authctxt->authlist);
494 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100495 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
496 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000497 free(authctxt->methoddata);
498 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100499 authctxt->success = 1; /* break out */
500}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000501
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100502void
503input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
504{
505 Authctxt *authctxt = ctxt;
506
507 if (authctxt == NULL)
508 fatal("%s: no authentication context", __func__);
509
510 fatal("Unexpected authentication success during %s.",
511 authctxt->method->name);
512}
513
Damien Millerf7475d72008-11-03 19:26:18 +1100514/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100515void
Damien Miller630d6f42002-01-22 23:17:30 +1100516input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100517{
Damien Miller62cee002000-09-23 17:15:56 +1100518 Authctxt *authctxt = ctxt;
519 char *authlist = NULL;
520 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100521
522 if (authctxt == NULL)
523 fatal("input_userauth_failure: no authentication context");
524
Damien Miller874d77b2000-10-14 16:23:11 +1100525 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100526 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100527 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100528
Damien Miller62e9c4f2013-04-23 15:15:49 +1000529 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000530 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000531 /* reset state */
532 pubkey_cleanup(authctxt);
533 pubkey_prepare(authctxt);
534 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000535 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100536
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000537 userauth(authctxt, authlist);
538}
Damien Millerf7475d72008-11-03 19:26:18 +1100539
540/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000541void
Damien Miller630d6f42002-01-22 23:17:30 +1100542input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000543{
544 Authctxt *authctxt = ctxt;
545 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000546 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000547 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000548 int pktype, sent = 0;
549 u_int alen, blen;
550 char *pkalg, *fp;
551 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000552
553 if (authctxt == NULL)
554 fatal("input_userauth_pk_ok: no authentication context");
555 if (datafellows & SSH_BUG_PKOK) {
556 /* this is similar to SSH_BUG_PKAUTH */
557 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
558 pkblob = packet_get_string(&blen);
559 buffer_init(&b);
560 buffer_append(&b, pkblob, blen);
561 pkalg = buffer_get_string(&b, &alen);
562 buffer_free(&b);
563 } else {
564 pkalg = packet_get_string(&alen);
565 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000566 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100567 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000568
Damien Miller280ecfb2003-05-14 13:46:00 +1000569 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000570
Damien Miller280ecfb2003-05-14 13:46:00 +1000571 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
572 debug("unknown pkalg %s", pkalg);
573 goto done;
574 }
575 if ((key = key_from_blob(pkblob, blen)) == NULL) {
576 debug("no key from blob. pkalg %s", pkalg);
577 goto done;
578 }
579 if (key->type != pktype) {
580 error("input_userauth_pk_ok: type mismatch "
581 "for decoded key (received %d, expected %d)",
582 key->type, pktype);
583 goto done;
584 }
585 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
586 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000587 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000588
Darren Tuckerd05b6012003-10-15 15:55:59 +1000589 /*
590 * search keys in the reverse order, because last candidate has been
591 * moved to the end of the queue. this also avoids confusion by
592 * duplicate keys
593 */
Damien Miller0b51a522004-04-20 20:07:19 +1000594 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000595 if (key_equal(key, id->key)) {
596 sent = sign_and_send_pubkey(authctxt, id);
597 break;
598 }
599 }
600done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000601 if (key != NULL)
602 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000603 free(pkalg);
604 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000605
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000606 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000607 if (sent == 0)
608 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100609}
610
Darren Tucker0efd1552003-08-26 11:49:55 +1000611#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100612int
Darren Tucker0efd1552003-08-26 11:49:55 +1000613userauth_gssapi(Authctxt *authctxt)
614{
615 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100616 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000617 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000618 OM_uint32 min;
619 int ok = 0;
620
621 /* Try one GSSAPI method at a time, rather than sending them all at
622 * once. */
623
Darren Tucker56afe142003-11-03 20:06:14 +1100624 if (gss_supported == NULL)
625 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000626
627 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100628 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000629 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100630 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000631 ssh_gssapi_check_mechanism(&gssctxt,
632 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000633 ok = 1; /* Mechanism works */
634 } else {
635 mech++;
636 }
637 }
638
Damien Millera1cb9f32006-08-19 00:33:34 +1000639 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000640 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000641
642 authctxt->methoddata=(void *)gssctxt;
643
644 packet_start(SSH2_MSG_USERAUTH_REQUEST);
645 packet_put_cstring(authctxt->server_user);
646 packet_put_cstring(authctxt->service);
647 packet_put_cstring(authctxt->method->name);
648
649 packet_put_int(1);
650
Darren Tucker655a5e02003-11-03 20:09:03 +1100651 packet_put_int((gss_supported->elements[mech].length) + 2);
652 packet_put_char(SSH_GSS_OIDTYPE);
653 packet_put_char(gss_supported->elements[mech].length);
654 packet_put_raw(gss_supported->elements[mech].elements,
655 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000656
657 packet_send();
658
659 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
660 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
661 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
662 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
663
664 mech++; /* Move along to next candidate */
665
666 return 1;
667}
668
Damien Miller91c6aa42003-11-17 21:20:18 +1100669static OM_uint32
670process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
671{
672 Authctxt *authctxt = ctxt;
673 Gssctxt *gssctxt = authctxt->methoddata;
674 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000675 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
676 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100677 OM_uint32 status, ms, flags;
678 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100679
Damien Miller91c6aa42003-11-17 21:20:18 +1100680 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100681 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100682
683 if (send_tok.length > 0) {
684 if (GSS_ERROR(status))
685 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
686 else
687 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100688
Damien Miller91c6aa42003-11-17 21:20:18 +1100689 packet_put_string(send_tok.value, send_tok.length);
690 packet_send();
691 gss_release_buffer(&ms, &send_tok);
692 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100693
Damien Miller91c6aa42003-11-17 21:20:18 +1100694 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100695 /* send either complete or MIC, depending on mechanism */
696 if (!(flags & GSS_C_INTEG_FLAG)) {
697 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
698 packet_send();
699 } else {
700 ssh_gssapi_buildmic(&b, authctxt->server_user,
701 authctxt->service, "gssapi-with-mic");
702
703 gssbuf.value = buffer_ptr(&b);
704 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100705
Damien Miller0425d402003-11-17 22:18:21 +1100706 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100707
Damien Miller0425d402003-11-17 22:18:21 +1100708 if (!GSS_ERROR(status)) {
709 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
710 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100711
Damien Miller0425d402003-11-17 22:18:21 +1100712 packet_send();
713 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100714
Damien Miller0425d402003-11-17 22:18:21 +1100715 buffer_free(&b);
716 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100717 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100718 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100719
Damien Miller91c6aa42003-11-17 21:20:18 +1100720 return status;
721}
722
Damien Millerf7475d72008-11-03 19:26:18 +1100723/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000724void
725input_gssapi_response(int type, u_int32_t plen, void *ctxt)
726{
727 Authctxt *authctxt = ctxt;
728 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000729 int oidlen;
730 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000731
732 if (authctxt == NULL)
733 fatal("input_gssapi_response: no authentication context");
734 gssctxt = authctxt->methoddata;
735
736 /* Setup our OID */
737 oidv = packet_get_string(&oidlen);
738
Darren Tucker655a5e02003-11-03 20:09:03 +1100739 if (oidlen <= 2 ||
740 oidv[0] != SSH_GSS_OIDTYPE ||
741 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000742 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100743 debug("Badly encoded mechanism OID received");
744 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100745 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000746 }
747
Darren Tucker655a5e02003-11-03 20:09:03 +1100748 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
749 fatal("Server returned different OID than expected");
750
Darren Tucker0efd1552003-08-26 11:49:55 +1000751 packet_check_eom();
752
Darren Tuckera627d422013-06-02 07:31:17 +1000753 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000754
Damien Miller91c6aa42003-11-17 21:20:18 +1100755 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000756 /* Start again with next method on list */
757 debug("Trying to start again");
758 userauth(authctxt, NULL);
759 return;
760 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000761}
762
Damien Millerf7475d72008-11-03 19:26:18 +1100763/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000764void
765input_gssapi_token(int type, u_int32_t plen, void *ctxt)
766{
767 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000768 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100769 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000770 u_int slen;
771
772 if (authctxt == NULL)
773 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000774
775 recv_tok.value = packet_get_string(&slen);
776 recv_tok.length = slen; /* safe typecast */
777
778 packet_check_eom();
779
Damien Miller91c6aa42003-11-17 21:20:18 +1100780 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000781
Darren Tuckera627d422013-06-02 07:31:17 +1000782 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000783
784 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000785 /* Start again with the next method in the list */
786 userauth(authctxt, NULL);
787 return;
788 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000789}
790
Damien Millerf7475d72008-11-03 19:26:18 +1100791/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000792void
793input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
794{
795 Authctxt *authctxt = ctxt;
796 Gssctxt *gssctxt;
797 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
798 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000799 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000800 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000801
802 if (authctxt == NULL)
803 fatal("input_gssapi_response: no authentication context");
804 gssctxt = authctxt->methoddata;
805
Darren Tucker600ad8d2003-08-26 12:10:48 +1000806 recv_tok.value = packet_get_string(&len);
807 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000808
809 packet_check_eom();
810
811 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000812 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000813 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000814
Darren Tuckera627d422013-06-02 07:31:17 +1000815 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000816 gss_release_buffer(&ms, &send_tok);
817
818 /* Server will be returning a failed packet after this one */
819}
820
Damien Millerf7475d72008-11-03 19:26:18 +1100821/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000822void
823input_gssapi_error(int type, u_int32_t plen, void *ctxt)
824{
Darren Tucker0efd1552003-08-26 11:49:55 +1000825 char *msg;
826 char *lang;
827
Damien Millerd677ad12013-04-23 15:18:51 +1000828 /* maj */(void)packet_get_int();
829 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000830 msg=packet_get_string(NULL);
831 lang=packet_get_string(NULL);
832
833 packet_check_eom();
834
Damien Miller15d72a02005-11-05 15:07:33 +1100835 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000836 free(msg);
837 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000838}
839#endif /* GSSAPI */
840
Damien Millereba71ba2000-04-29 23:57:08 +1000841int
Damien Miller874d77b2000-10-14 16:23:11 +1100842userauth_none(Authctxt *authctxt)
843{
844 /* initial userauth request */
845 packet_start(SSH2_MSG_USERAUTH_REQUEST);
846 packet_put_cstring(authctxt->server_user);
847 packet_put_cstring(authctxt->service);
848 packet_put_cstring(authctxt->method->name);
849 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100850 return 1;
851}
852
853int
Damien Miller62cee002000-09-23 17:15:56 +1100854userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000855{
Damien Millere247cc42000-05-07 12:03:14 +1000856 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000857 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000858 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100859 const char *host = options.host_key_alias ? options.host_key_alias :
860 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000861
Damien Millerd3a18572000-06-07 19:55:44 +1000862 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000863 return 0;
864
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000865 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000866 error("Permission denied, please try again.");
867
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000868 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100869 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000870 password = read_passphrase(prompt, 0);
871 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100872 packet_put_cstring(authctxt->server_user);
873 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100874 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000875 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000876 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100877 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000878 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100879 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000880 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000881
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000882 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000883 &input_userauth_passwd_changereq);
884
Damien Millereba71ba2000-04-29 23:57:08 +1000885 return 1;
886}
Damien Millerf7475d72008-11-03 19:26:18 +1100887
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000888/*
889 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
890 */
Damien Millerf7475d72008-11-03 19:26:18 +1100891/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000892void
Tim Ricec8549622002-03-31 12:49:38 -0800893input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000894{
895 Authctxt *authctxt = ctxt;
896 char *info, *lang, *password = NULL, *retype = NULL;
897 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100898 const char *host = options.host_key_alias ? options.host_key_alias :
899 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000900
901 debug2("input_userauth_passwd_changereq");
902
903 if (authctxt == NULL)
904 fatal("input_userauth_passwd_changereq: "
905 "no authentication context");
906
907 info = packet_get_string(NULL);
908 lang = packet_get_string(NULL);
909 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000910 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000911 free(info);
912 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000913 packet_start(SSH2_MSG_USERAUTH_REQUEST);
914 packet_put_cstring(authctxt->server_user);
915 packet_put_cstring(authctxt->service);
916 packet_put_cstring(authctxt->method->name);
917 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000918 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000919 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100920 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000921 password = read_passphrase(prompt, 0);
922 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100923 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000924 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000925 password = NULL;
926 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000927 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000928 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100929 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000930 password = read_passphrase(prompt, RP_ALLOW_EOF);
931 if (password == NULL) {
932 /* bail out */
933 return;
934 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000935 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000936 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100937 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000938 retype = read_passphrase(prompt, 0);
939 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100940 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000942 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000943 password = NULL;
944 }
Damien Millera5103f42014-02-04 11:20:14 +1100945 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000946 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000947 }
948 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100949 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000950 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000951 packet_add_padding(64);
952 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000953
954 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000955 &input_userauth_passwd_changereq);
956}
Damien Millereba71ba2000-04-29 23:57:08 +1000957
Ben Lindstrombba81212001-06-25 05:01:22 +0000958static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000959identity_sign(Identity *id, u_char **sigp, u_int *lenp,
960 u_char *data, u_int datalen)
961{
962 Key *prv;
963 int ret;
964
965 /* the agent supports this key */
966 if (id->ac)
967 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
968 data, datalen));
969 /*
970 * we have already loaded the private key or
971 * the private key is stored in external hardware
972 */
973 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
974 return (key_sign(id->key, sigp, lenp, data, datalen));
975 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +1100976 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
Damien Miller280ecfb2003-05-14 13:46:00 +1000977 return (-1);
978 ret = key_sign(prv, sigp, lenp, data, datalen);
979 key_free(prv);
980 return (ret);
981}
982
983static int
984sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000985{
986 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000987 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000988 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000989 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000990 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100991 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +1000992 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000993
Damien Miller4e270b02010-04-16 15:56:21 +1000994 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
995 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000996 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000997
Damien Miller280ecfb2003-05-14 13:46:00 +1000998 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100999 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001000 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001001 return 0;
1002 }
Damien Millereba71ba2000-04-29 23:57:08 +10001003 /* data to be signed */
1004 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001005 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001006 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001007 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001008 } else {
1009 buffer_put_string(&b, session_id2, session_id2_len);
1010 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001011 }
Damien Millereba71ba2000-04-29 23:57:08 +10001012 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001013 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001014 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001015 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001016 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001017 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001018 if (datafellows & SSH_BUG_PKAUTH) {
1019 buffer_put_char(&b, have_sig);
1020 } else {
1021 buffer_put_cstring(&b, authctxt->method->name);
1022 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001023 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001024 }
Damien Millereba71ba2000-04-29 23:57:08 +10001025 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001026
1027 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001028 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001029 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001030 if (ret == -1) {
Darren Tuckera627d422013-06-02 07:31:17 +10001031 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001032 buffer_free(&b);
1033 return 0;
1034 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001035#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001036 buffer_dump(&b);
1037#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001038 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001039 buffer_clear(&b);
1040 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001041 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001042 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001043 buffer_put_cstring(&b, authctxt->server_user);
1044 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001045 buffer_put_cstring(&b, authctxt->method->name);
1046 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001047 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001048 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001049 buffer_put_string(&b, blob, bloblen);
1050 }
Darren Tuckera627d422013-06-02 07:31:17 +10001051 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001052
Damien Millereba71ba2000-04-29 23:57:08 +10001053 /* append signature */
1054 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001055 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001056
1057 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001058 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001059 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001060 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001061
1062 /* put remaining data from buffer into packet */
1063 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1064 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1065 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001066 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001067
1068 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001069}
1070
Ben Lindstrombba81212001-06-25 05:01:22 +00001071static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001072send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001073{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001074 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001075 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001076
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001077 debug3("send_pubkey_test");
1078
Damien Miller280ecfb2003-05-14 13:46:00 +10001079 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001080 /* we cannot handle this key */
1081 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001082 return 0;
1083 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001084 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001085 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001086
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001087 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1088 packet_put_cstring(authctxt->server_user);
1089 packet_put_cstring(authctxt->service);
1090 packet_put_cstring(authctxt->method->name);
1091 packet_put_char(have_sig);
1092 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001093 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001094 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001095 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001096 packet_send();
1097 return 1;
1098}
1099
Ben Lindstrombba81212001-06-25 05:01:22 +00001100static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001101load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001102{
1103 Key *private;
1104 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001105 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001106 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001107
Ben Lindstrom329782e2001-03-10 17:08:59 +00001108 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001109 (userprovided ? logit : debug3)("no such identity: %s: %s",
1110 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001111 return NULL;
1112 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001113 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
Darren Tuckere52a2602013-06-06 08:22:05 +10001114 if (!perm_ok) {
1115 if (private != NULL)
1116 key_free(private);
Darren Tucker232b76f2006-05-06 17:41:51 +10001117 return NULL;
Darren Tuckere52a2602013-06-06 08:22:05 +10001118 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001119 if (private == NULL) {
1120 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001121 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001122 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001123 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001124 for (i = 0; i < options.number_of_password_prompts; i++) {
1125 passphrase = read_passphrase(prompt, 0);
1126 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001127 private = key_load_private_type(KEY_UNSPEC,
1128 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001129 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001130 } else {
1131 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001132 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001133 }
Damien Millera5103f42014-02-04 11:20:14 +11001134 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001135 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001136 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001137 break;
1138 debug2("bad passphrase given, try again...");
1139 }
Damien Miller994cf142000-07-21 10:19:44 +10001140 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001141 return private;
1142}
1143
Damien Miller280ecfb2003-05-14 13:46:00 +10001144/*
1145 * try keys in the following order:
1146 * 1. agent keys that are found in the config file
1147 * 2. other agent keys
1148 * 3. keys that are only listed in the config file
1149 */
1150static void
1151pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001152{
Damien Millercb6b68b2012-12-03 09:49:52 +11001153 Identity *id, *id2, *tmp;
Damien Miller280ecfb2003-05-14 13:46:00 +10001154 Idlist agent, files, *preferred;
1155 Key *key;
1156 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001157 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001158 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001159
Damien Miller280ecfb2003-05-14 13:46:00 +10001160 TAILQ_INIT(&agent); /* keys from the agent */
1161 TAILQ_INIT(&files); /* keys from the config file */
1162 preferred = &authctxt->keys;
1163 TAILQ_INIT(preferred); /* preferred order of keys */
1164
Damien Millercb6b68b2012-12-03 09:49:52 +11001165 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001166 for (i = 0; i < options.num_identity_files; i++) {
1167 key = options.identity_keys[i];
1168 if (key && key->type == KEY_RSA1)
1169 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001170 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1171 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001172 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001173 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001174 id->key = key;
1175 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001176 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001177 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001178 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001179 /* Prefer PKCS11 keys that are explicitly listed */
1180 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1181 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1182 continue;
1183 found = 0;
1184 TAILQ_FOREACH(id2, &files, next) {
1185 if (id2->key == NULL ||
Darren Tucker9f92c532014-06-11 07:57:58 +10001186 (id2->key->flags & KEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001187 continue;
1188 if (key_equal(id->key, id2->key)) {
1189 TAILQ_REMOVE(&files, id, next);
1190 TAILQ_INSERT_TAIL(preferred, id, next);
1191 found = 1;
1192 break;
1193 }
1194 }
1195 /* If IdentitiesOnly set and key not found then don't use it */
1196 if (!found && options.identities_only) {
1197 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001198 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001199 free(id);
1200 }
1201 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001202 /* list of keys supported by the agent */
1203 if ((ac = ssh_get_authentication_connection())) {
1204 for (key = ssh_get_first_identity(ac, &comment, 2);
1205 key != NULL;
1206 key = ssh_get_next_identity(ac, &comment, 2)) {
1207 found = 0;
1208 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001209 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001210 if (key_equal(key, id->key)) {
1211 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001212 free(comment);
Damien Miller280ecfb2003-05-14 13:46:00 +10001213 TAILQ_REMOVE(&files, id, next);
1214 TAILQ_INSERT_TAIL(preferred, id, next);
1215 id->ac = ac;
1216 found = 1;
1217 break;
1218 }
1219 }
Damien Millerbd394c32004-03-08 23:12:36 +11001220 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001221 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001222 id->key = key;
1223 id->filename = comment;
1224 id->ac = ac;
1225 TAILQ_INSERT_TAIL(&agent, id, next);
1226 }
1227 }
1228 /* append remaining agent keys */
1229 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1230 TAILQ_REMOVE(&agent, id, next);
1231 TAILQ_INSERT_TAIL(preferred, id, next);
1232 }
1233 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001234 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001235 /* append remaining keys from the config file */
1236 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1237 TAILQ_REMOVE(&files, id, next);
1238 TAILQ_INSERT_TAIL(preferred, id, next);
1239 }
1240 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001241 debug2("key: %s (%p),%s", id->filename, id->key,
1242 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001243 }
1244}
1245
1246static void
1247pubkey_cleanup(Authctxt *authctxt)
1248{
1249 Identity *id;
1250
1251 if (authctxt->agent != NULL)
1252 ssh_close_authentication_connection(authctxt->agent);
1253 for (id = TAILQ_FIRST(&authctxt->keys); id;
1254 id = TAILQ_FIRST(&authctxt->keys)) {
1255 TAILQ_REMOVE(&authctxt->keys, id, next);
1256 if (id->key)
1257 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001258 free(id->filename);
1259 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001260 }
Damien Millereba71ba2000-04-29 23:57:08 +10001261}
1262
Damien Miller62cee002000-09-23 17:15:56 +11001263int
1264userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001265{
Damien Miller280ecfb2003-05-14 13:46:00 +10001266 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001267 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001268
Damien Miller280ecfb2003-05-14 13:46:00 +10001269 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1270 if (id->tried++)
1271 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001272 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001273 TAILQ_REMOVE(&authctxt->keys, id, next);
1274 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1275 /*
1276 * send a test message if we have the public key. for
1277 * encrypted keys we cannot do this and have to load the
1278 * private key instead
1279 */
Damien Miller324541e2013-12-31 12:25:40 +11001280 if (id->key != NULL) {
1281 if (key_type_plain(id->key->type) == KEY_RSA &&
1282 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1283 debug("Skipped %s key %s for RSA/MD5 server",
1284 key_type(id->key), id->filename);
1285 } else if (id->key->type != KEY_RSA1) {
1286 debug("Offering %s public key: %s",
1287 key_type(id->key), id->filename);
1288 sent = send_pubkey_test(authctxt, id);
1289 }
1290 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001291 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001292 id->key = load_identity_file(id->filename,
1293 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001294 if (id->key != NULL) {
1295 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001296 if (key_type_plain(id->key->type) == KEY_RSA &&
1297 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1298 debug("Skipped %s key %s for RSA/MD5 "
1299 "server", key_type(id->key),
1300 id->filename);
1301 } else {
1302 sent = sign_and_send_pubkey(
1303 authctxt, id);
1304 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001305 key_free(id->key);
1306 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001307 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001308 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001309 if (sent)
1310 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001311 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001312 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001313}
Damien Millereba71ba2000-04-29 23:57:08 +10001314
Damien Miller874d77b2000-10-14 16:23:11 +11001315/*
1316 * Send userauth request message specifying keyboard-interactive method.
1317 */
1318int
1319userauth_kbdint(Authctxt *authctxt)
1320{
1321 static int attempt = 0;
1322
1323 if (attempt++ >= options.number_of_password_prompts)
1324 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001325 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1326 if (attempt > 1 && !authctxt->info_req_seen) {
1327 debug3("userauth_kbdint: disable: no info_req_seen");
1328 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1329 return 0;
1330 }
Damien Miller874d77b2000-10-14 16:23:11 +11001331
1332 debug2("userauth_kbdint");
1333 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1334 packet_put_cstring(authctxt->server_user);
1335 packet_put_cstring(authctxt->service);
1336 packet_put_cstring(authctxt->method->name);
1337 packet_put_cstring(""); /* lang */
1338 packet_put_cstring(options.kbd_interactive_devices ?
1339 options.kbd_interactive_devices : "");
1340 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001341
1342 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1343 return 1;
1344}
1345
1346/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001347 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001348 */
1349void
Damien Miller630d6f42002-01-22 23:17:30 +11001350input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001351{
1352 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001353 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001354 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001355 int echo = 0;
1356
1357 debug2("input_userauth_info_req");
1358
1359 if (authctxt == NULL)
1360 fatal("input_userauth_info_req: no authentication context");
1361
Ben Lindstrom7d199962001-09-12 18:29:00 +00001362 authctxt->info_req_seen = 1;
1363
Damien Miller874d77b2000-10-14 16:23:11 +11001364 name = packet_get_string(NULL);
1365 inst = packet_get_string(NULL);
1366 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001367 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001368 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001369 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001370 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001371 free(name);
1372 free(inst);
1373 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001374
1375 num_prompts = packet_get_int();
1376 /*
1377 * Begin to build info response packet based on prompts requested.
1378 * We commit to providing the correct number of responses, so if
1379 * further on we run into a problem that prevents this, we have to
1380 * be sure and clean this up and send a correct error response.
1381 */
1382 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1383 packet_put_int(num_prompts);
1384
Ben Lindstrom551ea372001-06-05 18:56:16 +00001385 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001386 for (i = 0; i < num_prompts; i++) {
1387 prompt = packet_get_string(NULL);
1388 echo = packet_get_char();
1389
Ben Lindstrom949974b2001-06-25 05:20:31 +00001390 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001391
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001392 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001393 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(response);
1395 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001396 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001397 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001398
Damien Miller9f643902001-11-12 11:02:52 +11001399 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001400 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001401}
Damien Miller62cee002000-09-23 17:15:56 +11001402
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001403static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001404ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001405 u_char *data, u_int datalen)
1406{
1407 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001408 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001409 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001410 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001411
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001412 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001413
Ben Lindstrom5206b952002-06-06 19:59:29 +00001414 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001415 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001416 return -1;
1417 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001418 if (fflush(stdout) != 0)
1419 error("ssh_keysign: fflush: %s", strerror(errno));
1420 if (pipe(to) < 0) {
1421 error("ssh_keysign: pipe: %s", strerror(errno));
1422 return -1;
1423 }
1424 if (pipe(from) < 0) {
1425 error("ssh_keysign: pipe: %s", strerror(errno));
1426 return -1;
1427 }
1428 if ((pid = fork()) < 0) {
1429 error("ssh_keysign: fork: %s", strerror(errno));
1430 return -1;
1431 }
1432 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001433 /* keep the socket on exec */
1434 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001435 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001436 close(from[0]);
1437 if (dup2(from[1], STDOUT_FILENO) < 0)
1438 fatal("ssh_keysign: dup2: %s", strerror(errno));
1439 close(to[1]);
1440 if (dup2(to[0], STDIN_FILENO) < 0)
1441 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001442 close(from[1]);
1443 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001444 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001445 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1446 strerror(errno));
1447 }
1448 close(from[1]);
1449 close(to[0]);
1450
1451 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001452 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001453 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001454 if (ssh_msg_send(to[1], version, &b) == -1)
1455 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001456
Damien Miller901119b2002-10-04 11:10:04 +10001457 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001458 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001459 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001460 return -1;
1461 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001462 close(from[0]);
1463 close(to[1]);
1464
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001465 while (waitpid(pid, &status, 0) < 0)
1466 if (errno != EINTR)
1467 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001468
Ben Lindstrom5206b952002-06-06 19:59:29 +00001469 if (buffer_get_char(&b) != version) {
1470 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001471 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001472 return -1;
1473 }
1474 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001475 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001476
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001477 return 0;
1478}
1479
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001480int
1481userauth_hostbased(Authctxt *authctxt)
1482{
1483 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001484 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001485 Buffer b;
1486 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001487 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001488 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001489 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001490 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001491
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001492 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001493 for (i = 0; i < sensitive->nkeys; i++) {
1494 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001495 if (private && private->type != KEY_RSA1) {
1496 found = 1;
1497 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001498 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001499 break;
1500 }
1501 }
1502 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001503 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001504 return 0;
1505 }
1506 if (key_to_blob(private, &blob, &blen) == 0) {
1507 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001508 return 0;
1509 }
Damien Miller91c18472001-11-12 11:02:03 +11001510 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001511 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001512 if (p == NULL) {
1513 error("userauth_hostbased: cannot get local ipaddr/name");
1514 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001515 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001516 return 0;
1517 }
Damien Miller07d86be2006-03-26 14:19:21 +11001518 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001519 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001520 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001521
Ben Lindstrom671388f2001-04-19 20:40:45 +00001522 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1523 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001524 pkalg = xstrdup(key_ssh_name(private));
1525 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001526 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001527 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001528 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1529 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001530 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001531 buffer_put_cstring(&b, authctxt->method->name);
1532 buffer_put_cstring(&b, pkalg);
1533 buffer_put_string(&b, blob, blen);
1534 buffer_put_cstring(&b, chost);
1535 buffer_put_cstring(&b, authctxt->local_user);
1536#ifdef DEBUG_PK
1537 buffer_dump(&b);
1538#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001539 if (sensitive->external_keysign)
1540 ok = ssh_keysign(private, &signature, &slen,
1541 buffer_ptr(&b), buffer_len(&b));
1542 else
1543 ok = key_sign(private, &signature, &slen,
1544 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001545 key_free(private);
1546 buffer_free(&b);
1547 if (ok != 0) {
1548 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001549 free(chost);
1550 free(pkalg);
1551 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001552 return 0;
1553 }
1554 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1555 packet_put_cstring(authctxt->server_user);
1556 packet_put_cstring(authctxt->service);
1557 packet_put_cstring(authctxt->method->name);
1558 packet_put_cstring(pkalg);
1559 packet_put_string(blob, blen);
1560 packet_put_cstring(chost);
1561 packet_put_cstring(authctxt->local_user);
1562 packet_put_string(signature, slen);
Damien Millera5103f42014-02-04 11:20:14 +11001563 explicit_bzero(signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001564 free(signature);
1565 free(chost);
1566 free(pkalg);
1567 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001568
1569 packet_send();
1570 return 1;
1571}
1572
Damien Miller62cee002000-09-23 17:15:56 +11001573/* find auth method */
1574
Damien Miller62cee002000-09-23 17:15:56 +11001575/*
1576 * given auth method name, if configurable options permit this method fill
1577 * in auth_ident field and return true, otherwise return false.
1578 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001579static int
Damien Miller62cee002000-09-23 17:15:56 +11001580authmethod_is_enabled(Authmethod *method)
1581{
1582 if (method == NULL)
1583 return 0;
1584 /* return false if options indicate this method is disabled */
1585 if (method->enabled == NULL || *method->enabled == 0)
1586 return 0;
1587 /* return false if batch mode is enabled but method needs interactive mode */
1588 if (method->batch_flag != NULL && *method->batch_flag != 0)
1589 return 0;
1590 return 1;
1591}
1592
Ben Lindstrombba81212001-06-25 05:01:22 +00001593static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001594authmethod_lookup(const char *name)
1595{
1596 Authmethod *method = NULL;
1597 if (name != NULL)
1598 for (method = authmethods; method->name != NULL; method++)
1599 if (strcmp(name, method->name) == 0)
1600 return method;
1601 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1602 return NULL;
1603}
1604
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001605/* XXX internal state */
1606static Authmethod *current = NULL;
1607static char *supported = NULL;
1608static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001609
Damien Miller62cee002000-09-23 17:15:56 +11001610/*
1611 * Given the authentication method list sent by the server, return the
1612 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001613 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001614 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001615static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001616authmethod_get(char *authlist)
1617{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001618 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001619 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001620
Damien Miller62cee002000-09-23 17:15:56 +11001621 /* Use a suitable default if we're passed a nil list. */
1622 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001623 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001624
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001625 if (supported == NULL || strcmp(authlist, supported) != 0) {
1626 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001627 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001628 supported = xstrdup(authlist);
1629 preferred = options.preferred_authentications;
1630 debug3("preferred %s", preferred);
1631 current = NULL;
1632 } else if (current != NULL && authmethod_is_enabled(current))
1633 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001634
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001635 for (;;) {
1636 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001637 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001638 current = NULL;
1639 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001640 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001641 preferred += next;
1642 debug3("authmethod_lookup %s", name);
1643 debug3("remaining preferred: %s", preferred);
1644 if ((current = authmethod_lookup(name)) != NULL &&
1645 authmethod_is_enabled(current)) {
1646 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001647 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001648 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001649 return current;
1650 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001651 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001652 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001653}
Damien Miller62cee002000-09-23 17:15:56 +11001654
Ben Lindstrom79073822001-07-04 03:42:30 +00001655static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001656authmethods_get(void)
1657{
1658 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001659 Buffer b;
1660 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001661
Damien Miller0e3b8722002-01-22 23:26:38 +11001662 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001663 for (method = authmethods; method->name != NULL; method++) {
1664 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001665 if (buffer_len(&b) > 0)
1666 buffer_append(&b, ",", 1);
1667 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001668 }
Damien Miller62cee002000-09-23 17:15:56 +11001669 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001670 buffer_append(&b, "\0", 1);
1671 list = xstrdup(buffer_ptr(&b));
1672 buffer_free(&b);
1673 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001674}
Damien Miller01ed2272008-11-05 16:20:46 +11001675