blob: da1bd384787ac64db85cd10fb8057aefce15895b [file] [log] [blame]
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001/* $OpenBSD: sshconnect2.c,v 1.231 2015/12/04 16:41:28 markus 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"
Darren Tuckere608ca22004-05-13 16:15:47 +100064#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100065#include "readconf.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"
djm@openbsd.org141efe42015-01-14 20:05:27 +000073#include "ssherr.h"
Damien Miller874d77b2000-10-14 16:23:11 +110074
Darren Tucker0efd1552003-08-26 11:49:55 +100075#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78
Damien Millereba71ba2000-04-29 23:57:08 +100079/* import */
80extern char *client_version_string;
81extern char *server_version_string;
82extern Options options;
83
84/*
85 * SSH2 key exchange
86 */
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100089u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100090
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000091char *xxx_host;
92struct sockaddr *xxx_hostaddr;
93
Ben Lindstrombba81212001-06-25 05:01:22 +000094static int
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000095verify_host_key_callback(Key *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000096{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100098 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099 return 0;
100}
101
Damien Millerd925dcd2010-12-01 12:21:51 +1100102static char *
103order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
104{
105 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
106 size_t maxlen;
107 struct hostkeys *hostkeys;
108 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000109 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100110
111 /* Find all hostkeys for this hostname */
112 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
113 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000114 for (i = 0; i < options.num_user_hostfiles; i++)
115 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
116 for (i = 0; i < options.num_system_hostfiles; i++)
117 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100118
119 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
120 maxlen = strlen(avail) + 1;
121 first = xmalloc(maxlen);
122 last = xmalloc(maxlen);
123 *first = *last = '\0';
124
125#define ALG_APPEND(to, from) \
126 do { \
127 if (*to != '\0') \
128 strlcat(to, ",", maxlen); \
129 strlcat(to, from, maxlen); \
130 } while (0)
131
132 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000133 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100134 fatal("%s: unknown alg %s", __func__, alg);
135 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000136 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100137 ALG_APPEND(first, alg);
138 else
139 ALG_APPEND(last, alg);
140 }
141#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000142 xasprintf(&ret, "%s%s%s", first,
143 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100144 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 };
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000160 char *s;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000161 struct kex *kex;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000162 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000163
164 xxx_host = host;
165 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100166
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000167 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
168 fatal("%s: kex_names_cat", __func__);
169 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000171 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000172 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000173 compat_cipher_proposal(options.ciphers);
Damien Miller874d77b2000-10-14 16:23:11 +1100174 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000175 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000176 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100177 } else {
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] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100180 }
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000181 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
182 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
183 if (options.hostkeyalgorithms != NULL) {
184 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
185 &options.hostkeyalgorithms) != 0)
186 fatal("%s: kex_assemble_namelist", __func__);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100187 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100188 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000189 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000190 /* Enforce default */
191 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 /* 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 Miller874d77b2000-10-14 16:23:11 +1100197
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000198 if (options.rekey_limit || options.rekey_interval)
199 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
200 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000201
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000202 /* start key exchange */
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000203 if ((r = kex_setup(active_state, myproposal)) != 0)
204 fatal("kex_setup: %s", ssh_err(r));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000205 kex = active_state->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +1000206#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000208 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100211# ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000212 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# endif
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
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000220 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
Damien Miller874d77b2000-10-14 16:23:11 +1100221
Darren Tucker36331b52010-01-08 16:50:41 +1100222 if (options.use_roaming && !kex->roaming) {
223 debug("Roaming not allowed by server");
224 options.use_roaming = 0;
225 }
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000226 /* remove ext-info from the KEX proposals for rekeying */
227 myproposal[PROPOSAL_KEX_ALGS] =
228 compat_kex_proposal(options.kex_algorithms);
229 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
230 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100231
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000232 session_id2 = kex->session_id;
233 session_id2_len = kex->session_id_len;
234
Damien Miller874d77b2000-10-14 16:23:11 +1100235#ifdef DEBUG_KEXDH
236 /* send 1st encrypted/maced/compressed message */
237 packet_start(SSH2_MSG_IGNORE);
238 packet_put_cstring("markus");
239 packet_send();
240 packet_write_wait();
241#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000242}
Damien Millerb1715dc2000-05-30 13:44:51 +1000243
Damien Millereba71ba2000-04-29 23:57:08 +1000244/*
245 * Authenticate user
246 */
Damien Miller62cee002000-09-23 17:15:56 +1100247
djm@openbsd.org141efe42015-01-14 20:05:27 +0000248typedef struct cauthctxt Authctxt;
249typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000250typedef struct identity Identity;
251typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100252
Damien Miller280ecfb2003-05-14 13:46:00 +1000253struct identity {
254 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000255 int agent_fd; /* >=0 if agent supports key */
256 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000257 char *filename; /* comment for agent-only keys */
258 int tried;
259 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100260 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000261};
262TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100263
djm@openbsd.org141efe42015-01-14 20:05:27 +0000264struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100265 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100267 const char *host;
268 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000269 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000270 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000271 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000272 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000273 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000274 struct idlist keys;
275 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000276 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000277 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000278 char *oktypes, *ktypes;
279 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000280 /* kbd-interactive */
281 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000282 /* generic */
283 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100284};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000285
286struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100287 char *name; /* string to compare against server's list */
288 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100289 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100290 int *enabled; /* flag in option struct that enables method */
291 int *batch_flag; /* flag in option struct that disables method */
292};
293
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000294int input_userauth_service_accept(int, u_int32_t, void *);
295int input_userauth_ext_info(int, u_int32_t, void *);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000296int input_userauth_success(int, u_int32_t, void *);
297int input_userauth_success_unexpected(int, u_int32_t, void *);
298int input_userauth_failure(int, u_int32_t, void *);
299int input_userauth_banner(int, u_int32_t, void *);
300int input_userauth_error(int, u_int32_t, void *);
301int input_userauth_info_req(int, u_int32_t, void *);
302int input_userauth_pk_ok(int, u_int32_t, void *);
303int input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100304
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000305int userauth_none(Authctxt *);
306int userauth_pubkey(Authctxt *);
307int userauth_passwd(Authctxt *);
308int userauth_kbdint(Authctxt *);
309int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100310
Darren Tucker0efd1552003-08-26 11:49:55 +1000311#ifdef GSSAPI
312int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000313int input_gssapi_response(int type, u_int32_t, void *);
314int input_gssapi_token(int type, u_int32_t, void *);
315int input_gssapi_hash(int type, u_int32_t, void *);
316int input_gssapi_error(int, u_int32_t, void *);
317int input_gssapi_errtok(int, u_int32_t, void *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000318#endif
319
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000320void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000321
Damien Miller280ecfb2003-05-14 13:46:00 +1000322static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000323static void pubkey_prepare(Authctxt *);
324static void pubkey_cleanup(Authctxt *);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000325static Key *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000326
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static Authmethod *authmethod_get(char *authlist);
328static Authmethod *authmethod_lookup(const char *name);
329static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100330
331Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000332#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100333 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000334 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100335 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000336 &options.gss_authentication,
337 NULL},
338#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000339 {"hostbased",
340 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100341 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000342 &options.hostbased_authentication,
343 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000344 {"publickey",
345 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100346 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000347 &options.pubkey_authentication,
348 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100349 {"keyboard-interactive",
350 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100351 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100352 &options.kbd_interactive_authentication,
353 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000354 {"password",
355 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100356 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000357 &options.password_authentication,
358 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100359 {"none",
360 userauth_none,
361 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100362 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100363 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100364 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100365};
366
367void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000368ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000369 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100370{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000371 struct ssh *ssh = active_state;
Damien Miller62cee002000-09-23 17:15:56 +1100372 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000373 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100374
Ben Lindstrom551ea372001-06-05 18:56:16 +0000375 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000376 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000377 if (options.preferred_authentications == NULL)
378 options.preferred_authentications = authmethods_get();
379
Damien Miller62cee002000-09-23 17:15:56 +1100380 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000381 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000382 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100383 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000384 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100385 authctxt.host = host;
386 authctxt.service = "ssh-connection"; /* service name */
387 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100388 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000389 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000390 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000391 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000392 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000393 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000394 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100395 if (authctxt.method == NULL)
396 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100397
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000398 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
399 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
400 (r = sshpkt_send(ssh)) != 0)
401 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100402
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000403 ssh_dispatch_init(ssh, &input_userauth_error);
404 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
405 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
406 ssh_dispatch_run(ssh, DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
Damien Miller62cee002000-09-23 17:15:56 +1100407
Damien Miller280ecfb2003-05-14 13:46:00 +1000408 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000409 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000410
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000411 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100412}
Damien Millerf842fcb2003-05-15 12:01:28 +1000413
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000414/* ARGSUSED */
415int
416input_userauth_service_accept(int type, u_int32_t seqnr, void *ctxt)
417{
418 Authctxt *authctxt = ctxt;
419 struct ssh *ssh = active_state;
420 int r;
421
422 if (ssh_packet_remaining(ssh) > 0) {
423 char *reply;
424
425 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
426 goto out;
427 debug2("service_accept: %s", reply);
428 free(reply);
429 } else {
430 debug2("buggy server: service_accept w/o service");
431 }
432 if ((r = sshpkt_get_end(ssh)) != 0)
433 goto out;
434 debug("SSH2_MSG_SERVICE_ACCEPT received");
435
436 /* initial userauth request */
437 userauth_none(authctxt);
438
439 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
440 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
441 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
442 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
443 r = 0;
444 out:
445 return r;
446}
447
448/* ARGSUSED */
449int
450input_userauth_ext_info(int type, u_int32_t seqnr, void *ctxt)
451{
452 return kex_input_ext_info(type, seqnr, active_state);
453}
454
Damien Miller62cee002000-09-23 17:15:56 +1100455void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000456userauth(Authctxt *authctxt, char *authlist)
457{
Damien Miller01ed2272008-11-05 16:20:46 +1100458 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
459 authctxt->method->cleanup(authctxt);
460
Darren Tuckera627d422013-06-02 07:31:17 +1000461 free(authctxt->methoddata);
462 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000463 if (authlist == NULL) {
464 authlist = authctxt->authlist;
465 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000466 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000467 authctxt->authlist = authlist;
468 }
469 for (;;) {
470 Authmethod *method = authmethod_get(authlist);
471 if (method == NULL)
472 fatal("Permission denied (%s).", authlist);
473 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000474
475 /* reset the per method handler */
476 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
477 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
478
479 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000480 if (method->userauth(authctxt) != 0) {
481 debug2("we sent a %s packet, wait for reply", method->name);
482 break;
483 } else {
484 debug2("we did not send a packet, disable method");
485 method->enabled = NULL;
486 }
487 }
488}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000489
Damien Millerf7475d72008-11-03 19:26:18 +1100490/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000491int
Damien Miller630d6f42002-01-22 23:17:30 +1100492input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100493{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000494 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000495 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000496 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000497}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000498
Damien Millerf7475d72008-11-03 19:26:18 +1100499/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000500int
Damien Miller630d6f42002-01-22 23:17:30 +1100501input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000502{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000503 char *msg, *raw, *lang;
504 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000505
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000506 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000507 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000508 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100509 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000510 if (len > 65536)
511 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100512 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100513 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000514 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000515 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000516 }
Darren Tuckera627d422013-06-02 07:31:17 +1000517 free(raw);
518 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000519 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100520}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000521
Damien Millerf7475d72008-11-03 19:26:18 +1100522/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000523int
Damien Miller630d6f42002-01-22 23:17:30 +1100524input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100525{
526 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100527
Damien Miller62cee002000-09-23 17:15:56 +1100528 if (authctxt == NULL)
529 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000530 free(authctxt->authlist);
531 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100532 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
533 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000534 free(authctxt->methoddata);
535 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100536 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000537 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100538}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000539
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000540int
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100541input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
542{
543 Authctxt *authctxt = ctxt;
544
545 if (authctxt == NULL)
546 fatal("%s: no authentication context", __func__);
547
548 fatal("Unexpected authentication success during %s.",
549 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000550 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100551}
552
Damien Millerf7475d72008-11-03 19:26:18 +1100553/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000554int
Damien Miller630d6f42002-01-22 23:17:30 +1100555input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100556{
Damien Miller62cee002000-09-23 17:15:56 +1100557 Authctxt *authctxt = ctxt;
558 char *authlist = NULL;
559 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100560
561 if (authctxt == NULL)
562 fatal("input_userauth_failure: no authentication context");
563
Damien Miller874d77b2000-10-14 16:23:11 +1100564 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100565 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100566 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100567
Damien Miller62e9c4f2013-04-23 15:15:49 +1000568 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000569 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000570 /* reset state */
571 pubkey_cleanup(authctxt);
572 pubkey_prepare(authctxt);
573 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000574 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100575
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000576 userauth(authctxt, authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000577 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000578}
Damien Millerf7475d72008-11-03 19:26:18 +1100579
580/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000581int
Damien Miller630d6f42002-01-22 23:17:30 +1100582input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000583{
584 Authctxt *authctxt = ctxt;
585 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000586 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000587 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000588 int pktype, sent = 0;
589 u_int alen, blen;
590 char *pkalg, *fp;
591 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000592
593 if (authctxt == NULL)
594 fatal("input_userauth_pk_ok: no authentication context");
595 if (datafellows & SSH_BUG_PKOK) {
596 /* this is similar to SSH_BUG_PKAUTH */
597 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
598 pkblob = packet_get_string(&blen);
599 buffer_init(&b);
600 buffer_append(&b, pkblob, blen);
601 pkalg = buffer_get_string(&b, &alen);
602 buffer_free(&b);
603 } else {
604 pkalg = packet_get_string(&alen);
605 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100607 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000608
Damien Miller280ecfb2003-05-14 13:46:00 +1000609 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610
Damien Miller280ecfb2003-05-14 13:46:00 +1000611 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
612 debug("unknown pkalg %s", pkalg);
613 goto done;
614 }
615 if ((key = key_from_blob(pkblob, blen)) == NULL) {
616 debug("no key from blob. pkalg %s", pkalg);
617 goto done;
618 }
619 if (key->type != pktype) {
620 error("input_userauth_pk_ok: type mismatch "
621 "for decoded key (received %d, expected %d)",
622 key->type, pktype);
623 goto done;
624 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000625 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
626 SSH_FP_DEFAULT)) == NULL)
627 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000628 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000629 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000630
Darren Tuckerd05b6012003-10-15 15:55:59 +1000631 /*
632 * search keys in the reverse order, because last candidate has been
633 * moved to the end of the queue. this also avoids confusion by
634 * duplicate keys
635 */
Damien Miller0b51a522004-04-20 20:07:19 +1000636 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000637 if (key_equal(key, id->key)) {
638 sent = sign_and_send_pubkey(authctxt, id);
639 break;
640 }
641 }
642done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643 if (key != NULL)
644 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000645 free(pkalg);
646 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000647
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000648 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000649 if (sent == 0)
650 userauth(authctxt, NULL);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000651 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100652}
653
Darren Tucker0efd1552003-08-26 11:49:55 +1000654#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100655int
Darren Tucker0efd1552003-08-26 11:49:55 +1000656userauth_gssapi(Authctxt *authctxt)
657{
658 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100659 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000660 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000661 OM_uint32 min;
662 int ok = 0;
663
664 /* Try one GSSAPI method at a time, rather than sending them all at
665 * once. */
666
Darren Tucker56afe142003-11-03 20:06:14 +1100667 if (gss_supported == NULL)
668 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000669
670 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100671 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000672 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100673 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000674 ssh_gssapi_check_mechanism(&gssctxt,
675 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000676 ok = 1; /* Mechanism works */
677 } else {
678 mech++;
679 }
680 }
681
Damien Millera1cb9f32006-08-19 00:33:34 +1000682 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000683 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000684
685 authctxt->methoddata=(void *)gssctxt;
686
687 packet_start(SSH2_MSG_USERAUTH_REQUEST);
688 packet_put_cstring(authctxt->server_user);
689 packet_put_cstring(authctxt->service);
690 packet_put_cstring(authctxt->method->name);
691
692 packet_put_int(1);
693
Darren Tucker655a5e02003-11-03 20:09:03 +1100694 packet_put_int((gss_supported->elements[mech].length) + 2);
695 packet_put_char(SSH_GSS_OIDTYPE);
696 packet_put_char(gss_supported->elements[mech].length);
697 packet_put_raw(gss_supported->elements[mech].elements,
698 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000699
700 packet_send();
701
702 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
703 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
704 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
705 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
706
707 mech++; /* Move along to next candidate */
708
709 return 1;
710}
711
Damien Miller91c6aa42003-11-17 21:20:18 +1100712static OM_uint32
713process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
714{
715 Authctxt *authctxt = ctxt;
716 Gssctxt *gssctxt = authctxt->methoddata;
717 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000718 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
719 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100720 OM_uint32 status, ms, flags;
721 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100722
Damien Miller91c6aa42003-11-17 21:20:18 +1100723 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100724 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100725
726 if (send_tok.length > 0) {
727 if (GSS_ERROR(status))
728 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
729 else
730 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100731
Damien Miller91c6aa42003-11-17 21:20:18 +1100732 packet_put_string(send_tok.value, send_tok.length);
733 packet_send();
734 gss_release_buffer(&ms, &send_tok);
735 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100736
Damien Miller91c6aa42003-11-17 21:20:18 +1100737 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100738 /* send either complete or MIC, depending on mechanism */
739 if (!(flags & GSS_C_INTEG_FLAG)) {
740 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
741 packet_send();
742 } else {
743 ssh_gssapi_buildmic(&b, authctxt->server_user,
744 authctxt->service, "gssapi-with-mic");
745
746 gssbuf.value = buffer_ptr(&b);
747 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100748
Damien Miller0425d402003-11-17 22:18:21 +1100749 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100750
Damien Miller0425d402003-11-17 22:18:21 +1100751 if (!GSS_ERROR(status)) {
752 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
753 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100754
Damien Miller0425d402003-11-17 22:18:21 +1100755 packet_send();
756 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100757
Damien Miller0425d402003-11-17 22:18:21 +1100758 buffer_free(&b);
759 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100760 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100761 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100762
Damien Miller91c6aa42003-11-17 21:20:18 +1100763 return status;
764}
765
Damien Millerf7475d72008-11-03 19:26:18 +1100766/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000767int
Darren Tucker0efd1552003-08-26 11:49:55 +1000768input_gssapi_response(int type, u_int32_t plen, void *ctxt)
769{
770 Authctxt *authctxt = ctxt;
771 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000772 int oidlen;
773 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000774
775 if (authctxt == NULL)
776 fatal("input_gssapi_response: no authentication context");
777 gssctxt = authctxt->methoddata;
778
779 /* Setup our OID */
780 oidv = packet_get_string(&oidlen);
781
Darren Tucker655a5e02003-11-03 20:09:03 +1100782 if (oidlen <= 2 ||
783 oidv[0] != SSH_GSS_OIDTYPE ||
784 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000785 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100786 debug("Badly encoded mechanism OID received");
787 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000788 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000789 }
790
Darren Tucker655a5e02003-11-03 20:09:03 +1100791 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
792 fatal("Server returned different OID than expected");
793
Darren Tucker0efd1552003-08-26 11:49:55 +1000794 packet_check_eom();
795
Darren Tuckera627d422013-06-02 07:31:17 +1000796 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000797
Damien Miller91c6aa42003-11-17 21:20:18 +1100798 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000799 /* Start again with next method on list */
800 debug("Trying to start again");
801 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000802 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000803 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000804 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000805}
806
Damien Millerf7475d72008-11-03 19:26:18 +1100807/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000808int
Darren Tucker0efd1552003-08-26 11:49:55 +1000809input_gssapi_token(int type, u_int32_t plen, void *ctxt)
810{
811 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000812 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100813 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000814 u_int slen;
815
816 if (authctxt == NULL)
817 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000818
819 recv_tok.value = packet_get_string(&slen);
820 recv_tok.length = slen; /* safe typecast */
821
822 packet_check_eom();
823
Damien Miller91c6aa42003-11-17 21:20:18 +1100824 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000825
Darren Tuckera627d422013-06-02 07:31:17 +1000826 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000827
828 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000829 /* Start again with the next method in the list */
830 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000831 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000832 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000833 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000834}
835
Damien Millerf7475d72008-11-03 19:26:18 +1100836/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000837int
Darren Tucker0efd1552003-08-26 11:49:55 +1000838input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
839{
840 Authctxt *authctxt = ctxt;
841 Gssctxt *gssctxt;
842 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
843 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000844 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000845 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000846
847 if (authctxt == NULL)
848 fatal("input_gssapi_response: no authentication context");
849 gssctxt = authctxt->methoddata;
850
Darren Tucker600ad8d2003-08-26 12:10:48 +1000851 recv_tok.value = packet_get_string(&len);
852 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000853
854 packet_check_eom();
855
856 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000857 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000858 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000859
Darren Tuckera627d422013-06-02 07:31:17 +1000860 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000861 gss_release_buffer(&ms, &send_tok);
862
863 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000864 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000865}
866
Damien Millerf7475d72008-11-03 19:26:18 +1100867/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000868int
Darren Tucker0efd1552003-08-26 11:49:55 +1000869input_gssapi_error(int type, u_int32_t plen, void *ctxt)
870{
Darren Tucker0efd1552003-08-26 11:49:55 +1000871 char *msg;
872 char *lang;
873
Damien Millerd677ad12013-04-23 15:18:51 +1000874 /* maj */(void)packet_get_int();
875 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000876 msg=packet_get_string(NULL);
877 lang=packet_get_string(NULL);
878
879 packet_check_eom();
880
Damien Miller15d72a02005-11-05 15:07:33 +1100881 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000882 free(msg);
883 free(lang);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000884 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000885}
886#endif /* GSSAPI */
887
Damien Millereba71ba2000-04-29 23:57:08 +1000888int
Damien Miller874d77b2000-10-14 16:23:11 +1100889userauth_none(Authctxt *authctxt)
890{
891 /* initial userauth request */
892 packet_start(SSH2_MSG_USERAUTH_REQUEST);
893 packet_put_cstring(authctxt->server_user);
894 packet_put_cstring(authctxt->service);
895 packet_put_cstring(authctxt->method->name);
896 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100897 return 1;
898}
899
900int
Damien Miller62cee002000-09-23 17:15:56 +1100901userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000902{
Damien Millere247cc42000-05-07 12:03:14 +1000903 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000904 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000905 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100906 const char *host = options.host_key_alias ? options.host_key_alias :
907 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000908
Damien Millerd3a18572000-06-07 19:55:44 +1000909 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000910 return 0;
911
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000912 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000913 error("Permission denied, please try again.");
914
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000915 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100916 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000917 password = read_passphrase(prompt, 0);
918 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100919 packet_put_cstring(authctxt->server_user);
920 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100921 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000922 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000923 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100924 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000925 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100926 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000927 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000928
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000929 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000930 &input_userauth_passwd_changereq);
931
Damien Millereba71ba2000-04-29 23:57:08 +1000932 return 1;
933}
Damien Millerf7475d72008-11-03 19:26:18 +1100934
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000935/*
936 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
937 */
Damien Millerf7475d72008-11-03 19:26:18 +1100938/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000939int
Tim Ricec8549622002-03-31 12:49:38 -0800940input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000941{
942 Authctxt *authctxt = ctxt;
943 char *info, *lang, *password = NULL, *retype = NULL;
944 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100945 const char *host = options.host_key_alias ? options.host_key_alias :
946 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000947
948 debug2("input_userauth_passwd_changereq");
949
950 if (authctxt == NULL)
951 fatal("input_userauth_passwd_changereq: "
952 "no authentication context");
953
954 info = packet_get_string(NULL);
955 lang = packet_get_string(NULL);
956 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000957 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000958 free(info);
959 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000960 packet_start(SSH2_MSG_USERAUTH_REQUEST);
961 packet_put_cstring(authctxt->server_user);
962 packet_put_cstring(authctxt->service);
963 packet_put_cstring(authctxt->method->name);
964 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000965 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000966 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100967 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000968 password = read_passphrase(prompt, 0);
969 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100970 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000971 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000972 password = NULL;
973 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000974 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000975 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100976 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000977 password = read_passphrase(prompt, RP_ALLOW_EOF);
978 if (password == NULL) {
979 /* bail out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000980 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000981 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000982 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000983 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100984 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000985 retype = read_passphrase(prompt, 0);
986 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100987 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000988 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000989 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000990 password = NULL;
991 }
Damien Millera5103f42014-02-04 11:20:14 +1100992 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000993 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000994 }
995 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100996 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000997 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000998 packet_add_padding(64);
999 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001000
1001 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001002 &input_userauth_passwd_changereq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001003 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001004}
Damien Millereba71ba2000-04-29 23:57:08 +10001005
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001006static const char *
1007identity_sign_encode(struct identity *id)
1008{
1009 struct ssh *ssh = active_state;
1010
1011 if (id->key->type == KEY_RSA) {
1012 switch (ssh->kex->rsa_sha2) {
1013 case 256:
1014 return "rsa-sha2-256";
1015 case 512:
1016 return "rsa-sha2-512";
1017 }
1018 }
1019 return key_ssh_name(id->key);
1020}
1021
Ben Lindstrombba81212001-06-25 05:01:22 +00001022static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001023identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1024 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +10001025{
1026 Key *prv;
1027 int ret;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001028 const char *alg;
1029
1030 alg = identity_sign_encode(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001031
1032 /* the agent supports this key */
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001033 if (id->agent_fd != -1)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001034 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001035 data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001036
Damien Miller280ecfb2003-05-14 13:46:00 +10001037 /*
1038 * we have already loaded the private key or
1039 * the private key is stored in external hardware
1040 */
Damien Miller86687062014-07-02 15:28:02 +10001041 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001042 return (sshkey_sign(id->key, sigp, lenp, data, datalen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001043 compat));
Damien Miller280ecfb2003-05-14 13:46:00 +10001044 /* load the private key from the file */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001045 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001046 return (-1); /* XXX return decent error code */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001047 ret = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001048 sshkey_free(prv);
Damien Miller280ecfb2003-05-14 13:46:00 +10001049 return (ret);
1050}
1051
1052static int
1053sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001054{
1055 Buffer b;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001056 Identity *private_id;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001057 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001058 size_t slen;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001059 u_int bloblen, skip = 0;
1060 int matched, ret = -1, have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001061 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001062
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001063 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1064 SSH_FP_DEFAULT)) == NULL)
1065 return 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001066 debug3("%s: %s %s", __func__, key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001067 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001068
Damien Miller280ecfb2003-05-14 13:46:00 +10001069 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001070 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001071 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001072 return 0;
1073 }
Damien Millereba71ba2000-04-29 23:57:08 +10001074 /* data to be signed */
1075 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001076 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001077 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001078 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001079 } else {
1080 buffer_put_string(&b, session_id2, session_id2_len);
1081 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001082 }
Damien Millereba71ba2000-04-29 23:57:08 +10001083 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001084 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001085 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001086 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001087 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001088 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001089 if (datafellows & SSH_BUG_PKAUTH) {
1090 buffer_put_char(&b, have_sig);
1091 } else {
1092 buffer_put_cstring(&b, authctxt->method->name);
1093 buffer_put_char(&b, have_sig);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001094 buffer_put_cstring(&b, identity_sign_encode(id));
Ben Lindstromd121f612000-12-03 17:00:47 +00001095 }
Damien Millereba71ba2000-04-29 23:57:08 +10001096 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001097
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001098 /*
1099 * If the key is an certificate, try to find a matching private key
1100 * and use it to complete the signature.
1101 * If no such private key exists, return failure and continue with
1102 * other methods of authentication.
1103 */
1104 if (key_is_cert(id->key)) {
1105 matched = 0;
1106 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1107 if (sshkey_equal_public(id->key, private_id->key) &&
1108 id->key->type != private_id->key->type) {
1109 id = private_id;
1110 matched = 1;
1111 break;
1112 }
1113 }
1114 if (matched) {
1115 debug2("%s: using private key \"%s\"%s for "
1116 "certificate", __func__, id->filename,
1117 id->agent_fd != -1 ? " from agent" : "");
1118 } else {
1119 /* XXX maybe verbose/error? */
1120 debug("%s: no private key for certificate "
1121 "\"%s\"", __func__, id->filename);
1122 free(blob);
1123 buffer_free(&b);
1124 return 0;
1125 }
1126 }
1127
Damien Millereba71ba2000-04-29 23:57:08 +10001128 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001129 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001130 buffer_ptr(&b), buffer_len(&b), datafellows);
1131 if (ret != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001132 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001133 buffer_free(&b);
1134 return 0;
1135 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001136#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001137 buffer_dump(&b);
1138#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001139 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001140 buffer_clear(&b);
1141 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001142 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001143 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001144 buffer_put_cstring(&b, authctxt->server_user);
1145 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001146 buffer_put_cstring(&b, authctxt->method->name);
1147 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001148 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001149 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001150 buffer_put_string(&b, blob, bloblen);
1151 }
Darren Tuckera627d422013-06-02 07:31:17 +10001152 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001153
Damien Millereba71ba2000-04-29 23:57:08 +10001154 /* append signature */
1155 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001156 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001157
1158 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001159 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001160 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001161 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001162
1163 /* put remaining data from buffer into packet */
1164 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1165 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1166 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001167 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001168
1169 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001170}
1171
Ben Lindstrombba81212001-06-25 05:01:22 +00001172static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001173send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001174{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001175 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001176 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001177
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001178 debug3("send_pubkey_test");
1179
Damien Miller280ecfb2003-05-14 13:46:00 +10001180 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001181 /* we cannot handle this key */
1182 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001183 return 0;
1184 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001185 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001186 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001187
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001188 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1189 packet_put_cstring(authctxt->server_user);
1190 packet_put_cstring(authctxt->service);
1191 packet_put_cstring(authctxt->method->name);
1192 packet_put_char(have_sig);
1193 if (!(datafellows & SSH_BUG_PKAUTH))
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001194 packet_put_cstring(identity_sign_encode(id));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001195 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001196 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001197 packet_send();
1198 return 1;
1199}
1200
Ben Lindstrombba81212001-06-25 05:01:22 +00001201static Key *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001202load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001203{
1204 Key *private;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001205 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001206 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001207 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001208
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001209 if (stat(id->filename, &st) < 0) {
1210 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1211 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001212 return NULL;
1213 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001214 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001215 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001216 for (i = 0; i <= options.number_of_password_prompts; i++) {
1217 if (i == 0)
1218 passphrase = "";
1219 else {
Damien Miller62cee002000-09-23 17:15:56 +11001220 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001221 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001222 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001223 free(passphrase);
1224 break;
Damien Miller62cee002000-09-23 17:15:56 +11001225 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001226 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001227 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1228 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001229 case 0:
1230 break;
1231 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1232 if (options.batch_mode) {
1233 quit = 1;
1234 break;
1235 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001236 if (i != 0)
1237 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001238 break;
1239 case SSH_ERR_SYSTEM_ERROR:
1240 if (errno == ENOENT) {
1241 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001242 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001243 quit = 1;
1244 break;
1245 }
1246 /* FALLTHROUGH */
1247 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001248 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001249 quit = 1;
1250 break;
1251 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001252 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001253 !(id->key && id->isprivate))
1254 maybe_add_key_to_agent(id->filename, private, comment,
1255 passphrase);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001256 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001257 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001258 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001259 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001260 if (comment)
1261 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001262 if (private != NULL || quit)
1263 break;
Damien Miller994cf142000-07-21 10:19:44 +10001264 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001265 return private;
1266}
1267
Damien Miller280ecfb2003-05-14 13:46:00 +10001268/*
1269 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001270 * 1. certificates listed in the config file
1271 * 2. other input certificates
1272 * 3. agent keys that are found in the config file
1273 * 4. other agent keys
1274 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001275 */
1276static void
1277pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001278{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001279 struct identity *id, *id2, *tmp;
1280 struct idlist agent, files, *preferred;
1281 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001282 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001283 size_t j;
1284 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001285
Damien Miller280ecfb2003-05-14 13:46:00 +10001286 TAILQ_INIT(&agent); /* keys from the agent */
1287 TAILQ_INIT(&files); /* keys from the config file */
1288 preferred = &authctxt->keys;
1289 TAILQ_INIT(preferred); /* preferred order of keys */
1290
Damien Millercb6b68b2012-12-03 09:49:52 +11001291 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001292 for (i = 0; i < options.num_identity_files; i++) {
1293 key = options.identity_keys[i];
1294 if (key && key->type == KEY_RSA1)
1295 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001296 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1297 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001298 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001299 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001300 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001301 id->key = key;
1302 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001303 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001304 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001305 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001306 /* Prefer PKCS11 keys that are explicitly listed */
1307 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
Damien Miller86687062014-07-02 15:28:02 +10001308 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001309 continue;
1310 found = 0;
1311 TAILQ_FOREACH(id2, &files, next) {
1312 if (id2->key == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001313 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001314 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001315 if (sshkey_equal(id->key, id2->key)) {
Damien Millercb6b68b2012-12-03 09:49:52 +11001316 TAILQ_REMOVE(&files, id, next);
1317 TAILQ_INSERT_TAIL(preferred, id, next);
1318 found = 1;
1319 break;
1320 }
1321 }
1322 /* If IdentitiesOnly set and key not found then don't use it */
1323 if (!found && options.identities_only) {
1324 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001325 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001326 free(id);
1327 }
1328 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001329 /* list of certificates specified by user */
1330 for (i = 0; i < options.num_certificate_files; i++) {
1331 key = options.certificates[i];
1332 if (!key_is_cert(key) || key->cert == NULL ||
1333 key->cert->type != SSH2_CERT_TYPE_USER)
1334 continue;
1335 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001336 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001337 id->key = key;
1338 id->filename = xstrdup(options.certificate_files[i]);
1339 id->userprovided = options.certificate_file_userprovided[i];
1340 TAILQ_INSERT_TAIL(preferred, id, next);
1341 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001342 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001343 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1344 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1345 debug("%s: ssh_get_authentication_socket: %s",
1346 __func__, ssh_err(r));
1347 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1348 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1349 debug("%s: ssh_fetch_identitylist: %s",
1350 __func__, ssh_err(r));
1351 } else {
1352 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001353 found = 0;
1354 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001355 /*
1356 * agent keys from the config file are
1357 * preferred
1358 */
1359 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001360 TAILQ_REMOVE(&files, id, next);
1361 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001362 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001363 found = 1;
1364 break;
1365 }
1366 }
Damien Millerbd394c32004-03-08 23:12:36 +11001367 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001368 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001369 /* XXX "steals" key/comment from idlist */
1370 id->key = idlist->keys[j];
1371 id->filename = idlist->comments[j];
1372 idlist->keys[j] = NULL;
1373 idlist->comments[j] = NULL;
1374 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001375 TAILQ_INSERT_TAIL(&agent, id, next);
1376 }
1377 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001378 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001379 /* append remaining agent keys */
1380 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1381 TAILQ_REMOVE(&agent, id, next);
1382 TAILQ_INSERT_TAIL(preferred, id, next);
1383 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001384 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001385 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001386 /* append remaining keys from the config file */
1387 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1388 TAILQ_REMOVE(&files, id, next);
1389 TAILQ_INSERT_TAIL(preferred, id, next);
1390 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001391 /* finally, filter by PubkeyAcceptedKeyTypes */
1392 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1393 if (id->key != NULL &&
1394 match_pattern_list(sshkey_ssh_name(id->key),
1395 options.pubkey_key_types, 0) != 1) {
1396 debug("Skipping %s key %s - "
1397 "not in PubkeyAcceptedKeyTypes",
1398 sshkey_ssh_name(id->key), id->filename);
1399 TAILQ_REMOVE(preferred, id, next);
1400 sshkey_free(id->key);
1401 free(id->filename);
1402 memset(id, 0, sizeof(*id));
1403 continue;
1404 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001405 debug2("key: %s (%p)%s%s", id->filename, id->key,
1406 id->userprovided ? ", explicit" : "",
1407 id->agent_fd != -1 ? ", agent" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001408 }
1409}
1410
1411static void
1412pubkey_cleanup(Authctxt *authctxt)
1413{
1414 Identity *id;
1415
djm@openbsd.org141efe42015-01-14 20:05:27 +00001416 if (authctxt->agent_fd != -1)
1417 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001418 for (id = TAILQ_FIRST(&authctxt->keys); id;
1419 id = TAILQ_FIRST(&authctxt->keys)) {
1420 TAILQ_REMOVE(&authctxt->keys, id, next);
1421 if (id->key)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001422 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001423 free(id->filename);
1424 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001425 }
Damien Millereba71ba2000-04-29 23:57:08 +10001426}
1427
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001428static int
1429try_identity(Identity *id)
1430{
1431 if (!id->key)
1432 return (0);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001433 if (key_type_plain(id->key->type) == KEY_RSA &&
1434 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1435 debug("Skipped %s key %s for RSA/MD5 server",
1436 key_type(id->key), id->filename);
1437 return (0);
1438 }
1439 return (id->key->type != KEY_RSA1);
1440}
1441
Damien Miller62cee002000-09-23 17:15:56 +11001442int
1443userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001444{
Damien Miller280ecfb2003-05-14 13:46:00 +10001445 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001446 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001447
Damien Miller280ecfb2003-05-14 13:46:00 +10001448 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1449 if (id->tried++)
1450 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001451 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001452 TAILQ_REMOVE(&authctxt->keys, id, next);
1453 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1454 /*
1455 * send a test message if we have the public key. for
1456 * encrypted keys we cannot do this and have to load the
1457 * private key instead
1458 */
Damien Miller324541e2013-12-31 12:25:40 +11001459 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001460 if (try_identity(id)) {
Damien Miller324541e2013-12-31 12:25:40 +11001461 debug("Offering %s public key: %s",
1462 key_type(id->key), id->filename);
1463 sent = send_pubkey_test(authctxt, id);
1464 }
1465 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001466 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001467 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001468 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001469 if (try_identity(id)) {
1470 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001471 sent = sign_and_send_pubkey(
1472 authctxt, id);
1473 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001474 key_free(id->key);
1475 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001476 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001477 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001478 if (sent)
1479 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001480 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001481 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001482}
Damien Millereba71ba2000-04-29 23:57:08 +10001483
Damien Miller874d77b2000-10-14 16:23:11 +11001484/*
1485 * Send userauth request message specifying keyboard-interactive method.
1486 */
1487int
1488userauth_kbdint(Authctxt *authctxt)
1489{
1490 static int attempt = 0;
1491
1492 if (attempt++ >= options.number_of_password_prompts)
1493 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001494 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1495 if (attempt > 1 && !authctxt->info_req_seen) {
1496 debug3("userauth_kbdint: disable: no info_req_seen");
1497 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1498 return 0;
1499 }
Damien Miller874d77b2000-10-14 16:23:11 +11001500
1501 debug2("userauth_kbdint");
1502 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1503 packet_put_cstring(authctxt->server_user);
1504 packet_put_cstring(authctxt->service);
1505 packet_put_cstring(authctxt->method->name);
1506 packet_put_cstring(""); /* lang */
1507 packet_put_cstring(options.kbd_interactive_devices ?
1508 options.kbd_interactive_devices : "");
1509 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001510
1511 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1512 return 1;
1513}
1514
1515/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001516 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001517 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001518int
Damien Miller630d6f42002-01-22 23:17:30 +11001519input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001520{
1521 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001522 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001523 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001524 int echo = 0;
1525
1526 debug2("input_userauth_info_req");
1527
1528 if (authctxt == NULL)
1529 fatal("input_userauth_info_req: no authentication context");
1530
Ben Lindstrom7d199962001-09-12 18:29:00 +00001531 authctxt->info_req_seen = 1;
1532
Damien Miller874d77b2000-10-14 16:23:11 +11001533 name = packet_get_string(NULL);
1534 inst = packet_get_string(NULL);
1535 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001536 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001537 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001538 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001539 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001540 free(name);
1541 free(inst);
1542 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001543
1544 num_prompts = packet_get_int();
1545 /*
1546 * Begin to build info response packet based on prompts requested.
1547 * We commit to providing the correct number of responses, so if
1548 * further on we run into a problem that prevents this, we have to
1549 * be sure and clean this up and send a correct error response.
1550 */
1551 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1552 packet_put_int(num_prompts);
1553
Ben Lindstrom551ea372001-06-05 18:56:16 +00001554 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001555 for (i = 0; i < num_prompts; i++) {
1556 prompt = packet_get_string(NULL);
1557 echo = packet_get_char();
1558
Ben Lindstrom949974b2001-06-25 05:20:31 +00001559 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001560
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001561 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001562 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001563 free(response);
1564 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001565 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001566 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001567
Damien Miller9f643902001-11-12 11:02:52 +11001568 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001569 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001570 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +11001571}
Damien Miller62cee002000-09-23 17:15:56 +11001572
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001573static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001574ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1575 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001576{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001577 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001578 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001579 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001580 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1581 u_char rversion = 0, version = 2;
1582 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001583
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001584 *sigp = NULL;
1585 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001586
Ben Lindstrom5206b952002-06-06 19:59:29 +00001587 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001588 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001589 return -1;
1590 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001591 if (fflush(stdout) != 0) {
1592 error("%s: fflush: %s", __func__, strerror(errno));
1593 return -1;
1594 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001595 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001596 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001597 return -1;
1598 }
1599 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001600 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001601 return -1;
1602 }
1603 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001604 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001605 return -1;
1606 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001607 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001608 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001609 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001610 fcntl(sock, F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001611 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001612 close(from[0]);
1613 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001614 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001615 close(to[1]);
1616 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001617 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001618 close(from[1]);
1619 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001620 /* Close everything but stdio and the socket */
1621 for (i = STDERR_FILENO + 1; i < sock; i++)
1622 close(i);
1623 closefrom(sock + 1);
1624 debug3("%s: [child] pid=%ld, exec %s",
1625 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001626 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001627 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001628 strerror(errno));
1629 }
1630 close(from[1]);
1631 close(to[0]);
1632
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001633 if ((b = sshbuf_new()) == NULL)
1634 fatal("%s: sshbuf_new failed", __func__);
1635 /* send # of sock, data to be signed */
1636 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1637 (r = sshbuf_put_string(b, data, datalen)) != 0)
1638 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1639 if (ssh_msg_send(to[1], version, b) == -1)
1640 fatal("%s: couldn't send request", __func__);
1641 sshbuf_reset(b);
1642 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001643 close(from[0]);
1644 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001645 if (r < 0) {
1646 error("%s: no reply", __func__);
1647 goto fail;
1648 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001649
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001650 errno = 0;
1651 while (waitpid(pid, &status, 0) < 0) {
1652 if (errno != EINTR) {
1653 error("%s: waitpid %ld: %s",
1654 __func__, (long)pid, strerror(errno));
1655 goto fail;
1656 }
1657 }
1658 if (!WIFEXITED(status)) {
1659 error("%s: exited abnormally", __func__);
1660 goto fail;
1661 }
1662 if (WEXITSTATUS(status) != 0) {
1663 error("%s: exited with status %d",
1664 __func__, WEXITSTATUS(status));
1665 goto fail;
1666 }
1667 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1668 error("%s: buffer error: %s", __func__, ssh_err(r));
1669 goto fail;
1670 }
1671 if (rversion != version) {
1672 error("%s: bad version", __func__);
1673 goto fail;
1674 }
1675 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1676 error("%s: buffer error: %s", __func__, ssh_err(r));
1677 fail:
1678 signal(SIGCHLD, osigchld);
1679 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001680 return -1;
1681 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001682 signal(SIGCHLD, osigchld);
1683 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001684
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001685 return 0;
1686}
1687
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001688int
1689userauth_hostbased(Authctxt *authctxt)
1690{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001691 struct ssh *ssh = active_state;
1692 struct sshkey *private = NULL;
1693 struct sshbuf *b = NULL;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001694 const char *service;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001695 u_char *sig = NULL, *keyblob = NULL;
1696 char *fp = NULL, *chost = NULL, *lname = NULL;
1697 size_t siglen = 0, keylen = 0;
1698 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001699
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001700 if (authctxt->ktypes == NULL) {
1701 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1702 authctxt->ktypes = authctxt->oktypes;
1703 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001704
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001705 /*
1706 * Work through each listed type pattern in HostbasedKeyTypes,
1707 * trying each hostkey that matches the type in turn.
1708 */
1709 for (;;) {
1710 if (authctxt->active_ktype == NULL)
1711 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1712 if (authctxt->active_ktype == NULL ||
1713 *authctxt->active_ktype == '\0')
1714 break;
1715 debug3("%s: trying key type %s", __func__,
1716 authctxt->active_ktype);
1717
1718 /* check for a useful key */
1719 private = NULL;
1720 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1721 if (authctxt->sensitive->keys[i] == NULL ||
1722 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1723 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1724 continue;
1725 if (match_pattern_list(
1726 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001727 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001728 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001729 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001730 private = authctxt->sensitive->keys[i];
1731 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001732 break;
1733 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001734 /* Found one */
1735 if (private != NULL)
1736 break;
1737 /* No more keys of this type; advance */
1738 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001739 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001740 if (private == NULL) {
1741 free(authctxt->oktypes);
1742 authctxt->oktypes = authctxt->ktypes = NULL;
1743 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001744 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001745 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001746 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001747
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001748 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1749 SSH_FP_DEFAULT)) == NULL) {
1750 error("%s: sshkey_fingerprint failed", __func__);
1751 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001752 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001753 debug("%s: trying hostkey %s %s",
1754 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001755
Damien Miller91c18472001-11-12 11:02:03 +11001756 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001757 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1758 error("%s: cannot get local ipaddr/name", __func__);
1759 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001760 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001761
1762 /* XXX sshbuf_put_stringf? */
1763 xasprintf(&chost, "%s.", lname);
1764 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001765
Ben Lindstrom671388f2001-04-19 20:40:45 +00001766 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1767 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001768
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001769 /* construct data */
1770 if ((b = sshbuf_new()) == NULL) {
1771 error("%s: sshbuf_new failed", __func__);
1772 goto out;
1773 }
1774 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1775 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1776 goto out;
1777 }
1778 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1779 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1780 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1781 (r = sshbuf_put_cstring(b, service)) != 0 ||
1782 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1783 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1784 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1785 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1786 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1787 error("%s: buffer error: %s", __func__, ssh_err(r));
1788 goto out;
1789 }
1790
1791#ifdef DEBUG_PK
1792 sshbuf_dump(b, stderr);
1793#endif
1794 if (authctxt->sensitive->external_keysign)
1795 r = ssh_keysign(private, &sig, &siglen,
1796 sshbuf_ptr(b), sshbuf_len(b));
1797 else if ((r = sshkey_sign(private, &sig, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001798 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001799 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1800 if (r != 0) {
1801 error("sign using hostkey %s %s failed",
1802 sshkey_ssh_name(private), fp);
1803 goto out;
1804 }
1805 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1806 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1807 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1808 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1809 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1810 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1811 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1812 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1813 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1814 (r = sshpkt_send(ssh)) != 0) {
1815 error("%s: packet error: %s", __func__, ssh_err(r));
1816 goto out;
1817 }
1818 success = 1;
1819
1820 out:
1821 if (sig != NULL) {
1822 explicit_bzero(sig, siglen);
1823 free(sig);
1824 }
1825 free(keyblob);
1826 free(lname);
1827 free(fp);
1828 free(chost);
1829 sshkey_free(private);
1830 sshbuf_free(b);
1831
1832 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001833}
1834
Damien Miller62cee002000-09-23 17:15:56 +11001835/* find auth method */
1836
Damien Miller62cee002000-09-23 17:15:56 +11001837/*
1838 * given auth method name, if configurable options permit this method fill
1839 * in auth_ident field and return true, otherwise return false.
1840 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001841static int
Damien Miller62cee002000-09-23 17:15:56 +11001842authmethod_is_enabled(Authmethod *method)
1843{
1844 if (method == NULL)
1845 return 0;
1846 /* return false if options indicate this method is disabled */
1847 if (method->enabled == NULL || *method->enabled == 0)
1848 return 0;
1849 /* return false if batch mode is enabled but method needs interactive mode */
1850 if (method->batch_flag != NULL && *method->batch_flag != 0)
1851 return 0;
1852 return 1;
1853}
1854
Ben Lindstrombba81212001-06-25 05:01:22 +00001855static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001856authmethod_lookup(const char *name)
1857{
1858 Authmethod *method = NULL;
1859 if (name != NULL)
1860 for (method = authmethods; method->name != NULL; method++)
1861 if (strcmp(name, method->name) == 0)
1862 return method;
1863 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1864 return NULL;
1865}
1866
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001867/* XXX internal state */
1868static Authmethod *current = NULL;
1869static char *supported = NULL;
1870static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001871
Damien Miller62cee002000-09-23 17:15:56 +11001872/*
1873 * Given the authentication method list sent by the server, return the
1874 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001875 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001876 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001877static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001878authmethod_get(char *authlist)
1879{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001880 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001881 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001882
Damien Miller62cee002000-09-23 17:15:56 +11001883 /* Use a suitable default if we're passed a nil list. */
1884 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001885 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001886
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001887 if (supported == NULL || strcmp(authlist, supported) != 0) {
1888 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001889 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001890 supported = xstrdup(authlist);
1891 preferred = options.preferred_authentications;
1892 debug3("preferred %s", preferred);
1893 current = NULL;
1894 } else if (current != NULL && authmethod_is_enabled(current))
1895 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001896
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001897 for (;;) {
1898 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001899 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001900 current = NULL;
1901 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001902 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001903 preferred += next;
1904 debug3("authmethod_lookup %s", name);
1905 debug3("remaining preferred: %s", preferred);
1906 if ((current = authmethod_lookup(name)) != NULL &&
1907 authmethod_is_enabled(current)) {
1908 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001909 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001910 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001911 return current;
1912 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001913 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001914 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001915}
Damien Miller62cee002000-09-23 17:15:56 +11001916
Ben Lindstrom79073822001-07-04 03:42:30 +00001917static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001918authmethods_get(void)
1919{
1920 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001921 Buffer b;
1922 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001923
Damien Miller0e3b8722002-01-22 23:26:38 +11001924 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001925 for (method = authmethods; method->name != NULL; method++) {
1926 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001927 if (buffer_len(&b) > 0)
1928 buffer_append(&b, ",", 1);
1929 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001930 }
Damien Miller62cee002000-09-23 17:15:56 +11001931 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001932 buffer_append(&b, "\0", 1);
1933 list = xstrdup(buffer_ptr(&b));
1934 buffer_free(&b);
1935 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001936}
Damien Miller01ed2272008-11-05 16:20:46 +11001937