blob: aee49a478ee3cbe91915596b80094ad6f9f43d07 [file] [log] [blame]
djm@openbsd.org35d60222015-01-18 13:33:34 +00001/* $OpenBSD: sshconnect2.c,v 1.216 2015/01/18 13:33:34 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
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 Lindstromf28f6342001-04-04 02:03:04 +000094Kex *xxx_kex = NULL;
95
Ben Lindstrombba81212001-06-25 05:01:22 +000096static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000098{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000099 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000100 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000101 return 0;
102}
103
Damien Millerd925dcd2010-12-01 12:21:51 +1100104static char *
105order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
106{
107 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
108 size_t maxlen;
109 struct hostkeys *hostkeys;
110 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000111 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100112
113 /* Find all hostkeys for this hostname */
114 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
115 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000116 for (i = 0; i < options.num_user_hostfiles; i++)
117 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
118 for (i = 0; i < options.num_system_hostfiles; i++)
119 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100120
121 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
122 maxlen = strlen(avail) + 1;
123 first = xmalloc(maxlen);
124 last = xmalloc(maxlen);
125 *first = *last = '\0';
126
127#define ALG_APPEND(to, from) \
128 do { \
129 if (*to != '\0') \
130 strlcat(to, ",", maxlen); \
131 strlcat(to, from, maxlen); \
132 } while (0)
133
134 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000135 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100136 fatal("%s: unknown alg %s", __func__, alg);
137 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000138 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100139 ALG_APPEND(first, alg);
140 else
141 ALG_APPEND(last, alg);
142 }
143#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000144 xasprintf(&ret, "%s%s%s", first,
145 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100146 if (*first != '\0')
147 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
148
Darren Tuckera627d422013-06-02 07:31:17 +1000149 free(first);
150 free(last);
151 free(hostname);
152 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100153 free_hostkeys(hostkeys);
154
155 return ret;
156}
157
Damien Millereba71ba2000-04-29 23:57:08 +1000158void
Damien Millerd925dcd2010-12-01 12:21:51 +1100159ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100160{
Damien Miller9235a032014-04-20 13:17:20 +1000161 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
Damien Miller874d77b2000-10-14 16:23:11 +1100162 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000163
164 xxx_host = host;
165 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100166
Damien Millere39cacc2000-11-29 12:18:44 +1100167 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000168 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100169 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100170 }
171 if (options.ciphers != NULL) {
172 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
173 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
174 }
Damien Millera0ff4662001-03-30 10:49:35 +1000175 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
176 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
177 myproposal[PROPOSAL_ENC_ALGS_STOC] =
178 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100179 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000180 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000181 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100182 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000183 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000184 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100185 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000186 if (options.macs != NULL) {
187 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
188 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
189 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000190 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100191 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100192 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 else {
194 /* Prefer algorithms that we already have keys for */
195 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100196 compat_pkalg_proposal(
197 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100198 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000199 if (options.kex_algorithms != NULL)
200 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller9395b282014-04-20 13:25:30 +1000201 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
202 myproposal[PROPOSAL_KEX_ALGS]);
Damien Miller874d77b2000-10-14 16:23:11 +1100203
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000204 if (options.rekey_limit || options.rekey_interval)
205 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
206 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000207
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000208 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000209 kex = kex_setup(myproposal);
Damien Miller1f0311c2014-05-15 14:24:09 +1000210#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100211 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000212 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100213 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100214 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000215 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#endif
Damien Miller1e124262013-11-04 08:26:52 +1100217 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000218 kex->client_version_string=client_version_string;
219 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000220 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100221
Ben Lindstromf28f6342001-04-04 02:03:04 +0000222 xxx_kex = kex;
223
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000224 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100225
Darren Tucker36331b52010-01-08 16:50:41 +1100226 if (options.use_roaming && !kex->roaming) {
227 debug("Roaming not allowed by server");
228 options.use_roaming = 0;
229 }
230
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000231 session_id2 = kex->session_id;
232 session_id2_len = kex->session_id_len;
233
Damien Miller874d77b2000-10-14 16:23:11 +1100234#ifdef DEBUG_KEXDH
235 /* send 1st encrypted/maced/compressed message */
236 packet_start(SSH2_MSG_IGNORE);
237 packet_put_cstring("markus");
238 packet_send();
239 packet_write_wait();
240#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000241}
Damien Millerb1715dc2000-05-30 13:44:51 +1000242
Damien Millereba71ba2000-04-29 23:57:08 +1000243/*
244 * Authenticate user
245 */
Damien Miller62cee002000-09-23 17:15:56 +1100246
djm@openbsd.org141efe42015-01-14 20:05:27 +0000247typedef struct cauthctxt Authctxt;
248typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000249typedef struct identity Identity;
250typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100251
Damien Miller280ecfb2003-05-14 13:46:00 +1000252struct identity {
253 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000254 int agent_fd; /* >=0 if agent supports key */
255 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000256 char *filename; /* comment for agent-only keys */
257 int tried;
258 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100259 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000260};
261TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100262
djm@openbsd.org141efe42015-01-14 20:05:27 +0000263struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100264 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000265 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100266 const char *host;
267 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000268 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000269 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000270 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000271 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000272 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000273 struct idlist keys;
274 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000275 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000276 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000277 /* kbd-interactive */
278 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000279 /* generic */
280 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100281};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000282
283struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100284 char *name; /* string to compare against server's list */
285 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100286 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100287 int *enabled; /* flag in option struct that enables method */
288 int *batch_flag; /* flag in option struct that disables method */
289};
290
Damien Miller630d6f42002-01-22 23:17:30 +1100291void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100292void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100293void input_userauth_failure(int, u_int32_t, void *);
294void input_userauth_banner(int, u_int32_t, void *);
295void input_userauth_error(int, u_int32_t, void *);
296void input_userauth_info_req(int, u_int32_t, void *);
297void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000298void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100299
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000300int userauth_none(Authctxt *);
301int userauth_pubkey(Authctxt *);
302int userauth_passwd(Authctxt *);
303int userauth_kbdint(Authctxt *);
304int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100305
Darren Tucker0efd1552003-08-26 11:49:55 +1000306#ifdef GSSAPI
307int userauth_gssapi(Authctxt *authctxt);
308void input_gssapi_response(int type, u_int32_t, void *);
309void input_gssapi_token(int type, u_int32_t, void *);
310void input_gssapi_hash(int type, u_int32_t, void *);
311void input_gssapi_error(int, u_int32_t, void *);
312void input_gssapi_errtok(int, u_int32_t, void *);
313#endif
314
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000315void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000316
Damien Miller280ecfb2003-05-14 13:46:00 +1000317static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000318static void pubkey_prepare(Authctxt *);
319static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100320static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000321
Ben Lindstrombba81212001-06-25 05:01:22 +0000322static Authmethod *authmethod_get(char *authlist);
323static Authmethod *authmethod_lookup(const char *name);
324static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100325
326Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000327#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100328 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000329 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100330 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000331 &options.gss_authentication,
332 NULL},
333#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000334 {"hostbased",
335 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100336 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000337 &options.hostbased_authentication,
338 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000339 {"publickey",
340 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100341 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000342 &options.pubkey_authentication,
343 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100344 {"keyboard-interactive",
345 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100346 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100347 &options.kbd_interactive_authentication,
348 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000349 {"password",
350 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100351 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000352 &options.password_authentication,
353 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100354 {"none",
355 userauth_none,
356 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100357 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100358 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100359 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100360};
361
362void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000363ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000364 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100365{
366 Authctxt authctxt;
367 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100368
Ben Lindstrom551ea372001-06-05 18:56:16 +0000369 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000370 options.kbd_interactive_authentication = 1;
371
Damien Miller62cee002000-09-23 17:15:56 +1100372 packet_start(SSH2_MSG_SERVICE_REQUEST);
373 packet_put_cstring("ssh-userauth");
374 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000375 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100376 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100377 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000378 if (type != SSH2_MSG_SERVICE_ACCEPT)
379 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100380 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100381 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000382 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100384 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000385 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100386 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100387 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000388 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100389
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000390 if (options.preferred_authentications == NULL)
391 options.preferred_authentications = authmethods_get();
392
Damien Miller62cee002000-09-23 17:15:56 +1100393 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000394 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000395 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100396 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000397 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100398 authctxt.host = host;
399 authctxt.service = "ssh-connection"; /* service name */
400 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100401 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000402 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000403 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000404 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000405 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000406 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100407 if (authctxt.method == NULL)
408 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100409
410 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100411 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100412
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000413 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100414 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
415 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000416 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100417 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
418
Damien Miller280ecfb2003-05-14 13:46:00 +1000419 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000420 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
421
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000422 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100423}
Damien Millerf842fcb2003-05-15 12:01:28 +1000424
Damien Miller62cee002000-09-23 17:15:56 +1100425void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000426userauth(Authctxt *authctxt, char *authlist)
427{
Damien Miller01ed2272008-11-05 16:20:46 +1100428 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
429 authctxt->method->cleanup(authctxt);
430
Darren Tuckera627d422013-06-02 07:31:17 +1000431 free(authctxt->methoddata);
432 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000433 if (authlist == NULL) {
434 authlist = authctxt->authlist;
435 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000436 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000437 authctxt->authlist = authlist;
438 }
439 for (;;) {
440 Authmethod *method = authmethod_get(authlist);
441 if (method == NULL)
442 fatal("Permission denied (%s).", authlist);
443 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000444
445 /* reset the per method handler */
446 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
447 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
448
449 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000450 if (method->userauth(authctxt) != 0) {
451 debug2("we sent a %s packet, wait for reply", method->name);
452 break;
453 } else {
454 debug2("we did not send a packet, disable method");
455 method->enabled = NULL;
456 }
457 }
458}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000459
Damien Millerf7475d72008-11-03 19:26:18 +1100460/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000461void
Damien Miller630d6f42002-01-22 23:17:30 +1100462input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100463{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000464 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000465 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000466}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000467
Damien Millerf7475d72008-11-03 19:26:18 +1100468/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469void
Damien Miller630d6f42002-01-22 23:17:30 +1100470input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000471{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000472 char *msg, *raw, *lang;
473 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000474
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000475 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000476 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000477 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100478 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000479 if (len > 65536)
480 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100481 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100482 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000483 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000484 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000485 }
Darren Tuckera627d422013-06-02 07:31:17 +1000486 free(raw);
487 free(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100488}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000489
Damien Millerf7475d72008-11-03 19:26:18 +1100490/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100491void
Damien Miller630d6f42002-01-22 23:17:30 +1100492input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100493{
494 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100495
Damien Miller62cee002000-09-23 17:15:56 +1100496 if (authctxt == NULL)
497 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000498 free(authctxt->authlist);
499 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100500 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
501 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000502 free(authctxt->methoddata);
503 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100504 authctxt->success = 1; /* break out */
505}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000506
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100507void
508input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
509{
510 Authctxt *authctxt = ctxt;
511
512 if (authctxt == NULL)
513 fatal("%s: no authentication context", __func__);
514
515 fatal("Unexpected authentication success during %s.",
516 authctxt->method->name);
517}
518
Damien Millerf7475d72008-11-03 19:26:18 +1100519/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100520void
Damien Miller630d6f42002-01-22 23:17:30 +1100521input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100522{
Damien Miller62cee002000-09-23 17:15:56 +1100523 Authctxt *authctxt = ctxt;
524 char *authlist = NULL;
525 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100526
527 if (authctxt == NULL)
528 fatal("input_userauth_failure: no authentication context");
529
Damien Miller874d77b2000-10-14 16:23:11 +1100530 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100531 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100532 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100533
Damien Miller62e9c4f2013-04-23 15:15:49 +1000534 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000535 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000536 /* reset state */
537 pubkey_cleanup(authctxt);
538 pubkey_prepare(authctxt);
539 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000540 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100541
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000542 userauth(authctxt, authlist);
543}
Damien Millerf7475d72008-11-03 19:26:18 +1100544
545/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000546void
Damien Miller630d6f42002-01-22 23:17:30 +1100547input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000548{
549 Authctxt *authctxt = ctxt;
550 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000551 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000552 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000553 int pktype, sent = 0;
554 u_int alen, blen;
555 char *pkalg, *fp;
556 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000557
558 if (authctxt == NULL)
559 fatal("input_userauth_pk_ok: no authentication context");
560 if (datafellows & SSH_BUG_PKOK) {
561 /* this is similar to SSH_BUG_PKAUTH */
562 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
563 pkblob = packet_get_string(&blen);
564 buffer_init(&b);
565 buffer_append(&b, pkblob, blen);
566 pkalg = buffer_get_string(&b, &alen);
567 buffer_free(&b);
568 } else {
569 pkalg = packet_get_string(&alen);
570 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000571 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100572 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000573
Damien Miller280ecfb2003-05-14 13:46:00 +1000574 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000575
Damien Miller280ecfb2003-05-14 13:46:00 +1000576 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
577 debug("unknown pkalg %s", pkalg);
578 goto done;
579 }
580 if ((key = key_from_blob(pkblob, blen)) == NULL) {
581 debug("no key from blob. pkalg %s", pkalg);
582 goto done;
583 }
584 if (key->type != pktype) {
585 error("input_userauth_pk_ok: type mismatch "
586 "for decoded key (received %d, expected %d)",
587 key->type, pktype);
588 goto done;
589 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000590 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller280ecfb2003-05-14 13:46:00 +1000591 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000592 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000593
Darren Tuckerd05b6012003-10-15 15:55:59 +1000594 /*
595 * search keys in the reverse order, because last candidate has been
596 * moved to the end of the queue. this also avoids confusion by
597 * duplicate keys
598 */
Damien Miller0b51a522004-04-20 20:07:19 +1000599 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000600 if (key_equal(key, id->key)) {
601 sent = sign_and_send_pubkey(authctxt, id);
602 break;
603 }
604 }
605done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000606 if (key != NULL)
607 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000608 free(pkalg);
609 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000611 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000612 if (sent == 0)
613 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100614}
615
Darren Tucker0efd1552003-08-26 11:49:55 +1000616#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100617int
Darren Tucker0efd1552003-08-26 11:49:55 +1000618userauth_gssapi(Authctxt *authctxt)
619{
620 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100621 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000622 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000623 OM_uint32 min;
624 int ok = 0;
625
626 /* Try one GSSAPI method at a time, rather than sending them all at
627 * once. */
628
Darren Tucker56afe142003-11-03 20:06:14 +1100629 if (gss_supported == NULL)
630 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000631
632 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100633 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000634 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100635 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000636 ssh_gssapi_check_mechanism(&gssctxt,
637 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000638 ok = 1; /* Mechanism works */
639 } else {
640 mech++;
641 }
642 }
643
Damien Millera1cb9f32006-08-19 00:33:34 +1000644 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000645 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000646
647 authctxt->methoddata=(void *)gssctxt;
648
649 packet_start(SSH2_MSG_USERAUTH_REQUEST);
650 packet_put_cstring(authctxt->server_user);
651 packet_put_cstring(authctxt->service);
652 packet_put_cstring(authctxt->method->name);
653
654 packet_put_int(1);
655
Darren Tucker655a5e02003-11-03 20:09:03 +1100656 packet_put_int((gss_supported->elements[mech].length) + 2);
657 packet_put_char(SSH_GSS_OIDTYPE);
658 packet_put_char(gss_supported->elements[mech].length);
659 packet_put_raw(gss_supported->elements[mech].elements,
660 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000661
662 packet_send();
663
664 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
665 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
666 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
667 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
668
669 mech++; /* Move along to next candidate */
670
671 return 1;
672}
673
Damien Miller91c6aa42003-11-17 21:20:18 +1100674static OM_uint32
675process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
676{
677 Authctxt *authctxt = ctxt;
678 Gssctxt *gssctxt = authctxt->methoddata;
679 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000680 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
681 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100682 OM_uint32 status, ms, flags;
683 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100684
Damien Miller91c6aa42003-11-17 21:20:18 +1100685 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100686 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100687
688 if (send_tok.length > 0) {
689 if (GSS_ERROR(status))
690 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
691 else
692 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100693
Damien Miller91c6aa42003-11-17 21:20:18 +1100694 packet_put_string(send_tok.value, send_tok.length);
695 packet_send();
696 gss_release_buffer(&ms, &send_tok);
697 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100698
Damien Miller91c6aa42003-11-17 21:20:18 +1100699 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100700 /* send either complete or MIC, depending on mechanism */
701 if (!(flags & GSS_C_INTEG_FLAG)) {
702 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
703 packet_send();
704 } else {
705 ssh_gssapi_buildmic(&b, authctxt->server_user,
706 authctxt->service, "gssapi-with-mic");
707
708 gssbuf.value = buffer_ptr(&b);
709 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100710
Damien Miller0425d402003-11-17 22:18:21 +1100711 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100712
Damien Miller0425d402003-11-17 22:18:21 +1100713 if (!GSS_ERROR(status)) {
714 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
715 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100716
Damien Miller0425d402003-11-17 22:18:21 +1100717 packet_send();
718 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100719
Damien Miller0425d402003-11-17 22:18:21 +1100720 buffer_free(&b);
721 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100722 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100723 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100724
Damien Miller91c6aa42003-11-17 21:20:18 +1100725 return status;
726}
727
Damien Millerf7475d72008-11-03 19:26:18 +1100728/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000729void
730input_gssapi_response(int type, u_int32_t plen, void *ctxt)
731{
732 Authctxt *authctxt = ctxt;
733 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000734 int oidlen;
735 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000736
737 if (authctxt == NULL)
738 fatal("input_gssapi_response: no authentication context");
739 gssctxt = authctxt->methoddata;
740
741 /* Setup our OID */
742 oidv = packet_get_string(&oidlen);
743
Darren Tucker655a5e02003-11-03 20:09:03 +1100744 if (oidlen <= 2 ||
745 oidv[0] != SSH_GSS_OIDTYPE ||
746 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000747 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100748 debug("Badly encoded mechanism OID received");
749 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100750 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000751 }
752
Darren Tucker655a5e02003-11-03 20:09:03 +1100753 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
754 fatal("Server returned different OID than expected");
755
Darren Tucker0efd1552003-08-26 11:49:55 +1000756 packet_check_eom();
757
Darren Tuckera627d422013-06-02 07:31:17 +1000758 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000759
Damien Miller91c6aa42003-11-17 21:20:18 +1100760 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000761 /* Start again with next method on list */
762 debug("Trying to start again");
763 userauth(authctxt, NULL);
764 return;
765 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000766}
767
Damien Millerf7475d72008-11-03 19:26:18 +1100768/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000769void
770input_gssapi_token(int type, u_int32_t plen, void *ctxt)
771{
772 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000773 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100774 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000775 u_int slen;
776
777 if (authctxt == NULL)
778 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000779
780 recv_tok.value = packet_get_string(&slen);
781 recv_tok.length = slen; /* safe typecast */
782
783 packet_check_eom();
784
Damien Miller91c6aa42003-11-17 21:20:18 +1100785 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000786
Darren Tuckera627d422013-06-02 07:31:17 +1000787 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000788
789 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000790 /* Start again with the next method in the list */
791 userauth(authctxt, NULL);
792 return;
793 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000794}
795
Damien Millerf7475d72008-11-03 19:26:18 +1100796/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000797void
798input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
799{
800 Authctxt *authctxt = ctxt;
801 Gssctxt *gssctxt;
802 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
803 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000804 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000805 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000806
807 if (authctxt == NULL)
808 fatal("input_gssapi_response: no authentication context");
809 gssctxt = authctxt->methoddata;
810
Darren Tucker600ad8d2003-08-26 12:10:48 +1000811 recv_tok.value = packet_get_string(&len);
812 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000813
814 packet_check_eom();
815
816 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000817 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000818 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000819
Darren Tuckera627d422013-06-02 07:31:17 +1000820 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000821 gss_release_buffer(&ms, &send_tok);
822
823 /* Server will be returning a failed packet after this one */
824}
825
Damien Millerf7475d72008-11-03 19:26:18 +1100826/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000827void
828input_gssapi_error(int type, u_int32_t plen, void *ctxt)
829{
Darren Tucker0efd1552003-08-26 11:49:55 +1000830 char *msg;
831 char *lang;
832
Damien Millerd677ad12013-04-23 15:18:51 +1000833 /* maj */(void)packet_get_int();
834 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000835 msg=packet_get_string(NULL);
836 lang=packet_get_string(NULL);
837
838 packet_check_eom();
839
Damien Miller15d72a02005-11-05 15:07:33 +1100840 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000841 free(msg);
842 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000843}
844#endif /* GSSAPI */
845
Damien Millereba71ba2000-04-29 23:57:08 +1000846int
Damien Miller874d77b2000-10-14 16:23:11 +1100847userauth_none(Authctxt *authctxt)
848{
849 /* initial userauth request */
850 packet_start(SSH2_MSG_USERAUTH_REQUEST);
851 packet_put_cstring(authctxt->server_user);
852 packet_put_cstring(authctxt->service);
853 packet_put_cstring(authctxt->method->name);
854 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100855 return 1;
856}
857
858int
Damien Miller62cee002000-09-23 17:15:56 +1100859userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000860{
Damien Millere247cc42000-05-07 12:03:14 +1000861 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000862 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000863 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100864 const char *host = options.host_key_alias ? options.host_key_alias :
865 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000866
Damien Millerd3a18572000-06-07 19:55:44 +1000867 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000868 return 0;
869
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000870 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000871 error("Permission denied, please try again.");
872
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000873 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100874 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000875 password = read_passphrase(prompt, 0);
876 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100877 packet_put_cstring(authctxt->server_user);
878 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100879 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000880 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000881 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100882 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000883 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100884 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000885 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000886
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000887 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000888 &input_userauth_passwd_changereq);
889
Damien Millereba71ba2000-04-29 23:57:08 +1000890 return 1;
891}
Damien Millerf7475d72008-11-03 19:26:18 +1100892
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000893/*
894 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
895 */
Damien Millerf7475d72008-11-03 19:26:18 +1100896/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000897void
Tim Ricec8549622002-03-31 12:49:38 -0800898input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000899{
900 Authctxt *authctxt = ctxt;
901 char *info, *lang, *password = NULL, *retype = NULL;
902 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100903 const char *host = options.host_key_alias ? options.host_key_alias :
904 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000905
906 debug2("input_userauth_passwd_changereq");
907
908 if (authctxt == NULL)
909 fatal("input_userauth_passwd_changereq: "
910 "no authentication context");
911
912 info = packet_get_string(NULL);
913 lang = packet_get_string(NULL);
914 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000915 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000916 free(info);
917 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000918 packet_start(SSH2_MSG_USERAUTH_REQUEST);
919 packet_put_cstring(authctxt->server_user);
920 packet_put_cstring(authctxt->service);
921 packet_put_cstring(authctxt->method->name);
922 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000923 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000924 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100925 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000926 password = read_passphrase(prompt, 0);
927 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100928 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000929 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000930 password = NULL;
931 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000932 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000933 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100934 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000935 password = read_passphrase(prompt, RP_ALLOW_EOF);
936 if (password == NULL) {
937 /* bail out */
938 return;
939 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000940 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000941 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100942 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000943 retype = read_passphrase(prompt, 0);
944 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100945 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000946 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000947 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000948 password = NULL;
949 }
Damien Millera5103f42014-02-04 11:20:14 +1100950 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000951 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000952 }
953 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100954 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000955 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000956 packet_add_padding(64);
957 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000958
959 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000960 &input_userauth_passwd_changereq);
961}
Damien Millereba71ba2000-04-29 23:57:08 +1000962
Ben Lindstrombba81212001-06-25 05:01:22 +0000963static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000964identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
965 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +1000966{
967 Key *prv;
968 int ret;
969
970 /* the agent supports this key */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000971 if (id->agent_fd)
972 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
973 data, datalen, compat);
974
Damien Miller280ecfb2003-05-14 13:46:00 +1000975 /*
976 * we have already loaded the private key or
977 * the private key is stored in external hardware
978 */
Damien Miller86687062014-07-02 15:28:02 +1000979 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
djm@openbsd.org141efe42015-01-14 20:05:27 +0000980 return (sshkey_sign(id->key, sigp, lenp, data, datalen,
981 compat));
Damien Miller280ecfb2003-05-14 13:46:00 +1000982 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +1100983 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000984 return (-1); /* XXX return decent error code */
985 ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat);
986 sshkey_free(prv);
Damien Miller280ecfb2003-05-14 13:46:00 +1000987 return (ret);
988}
989
990static int
991sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000992{
993 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000994 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000995 u_int bloblen;
996 size_t slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000997 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000998 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100999 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001000 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001001
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001002 fp = key_fingerprint(id->key, options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller4e270b02010-04-16 15:56:21 +10001003 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001004 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001005
Damien Miller280ecfb2003-05-14 13:46:00 +10001006 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001007 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001008 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001009 return 0;
1010 }
Damien Millereba71ba2000-04-29 23:57:08 +10001011 /* data to be signed */
1012 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001013 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001014 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001015 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001016 } else {
1017 buffer_put_string(&b, session_id2, session_id2_len);
1018 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001019 }
Damien Millereba71ba2000-04-29 23:57:08 +10001020 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001021 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001022 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001023 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001024 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001025 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001026 if (datafellows & SSH_BUG_PKAUTH) {
1027 buffer_put_char(&b, have_sig);
1028 } else {
1029 buffer_put_cstring(&b, authctxt->method->name);
1030 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001031 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001032 }
Damien Millereba71ba2000-04-29 23:57:08 +10001033 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001034
1035 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001036 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001037 buffer_ptr(&b), buffer_len(&b), datafellows);
1038 if (ret != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001039 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001040 buffer_free(&b);
1041 return 0;
1042 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001043#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001044 buffer_dump(&b);
1045#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001046 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001047 buffer_clear(&b);
1048 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001049 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001050 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001051 buffer_put_cstring(&b, authctxt->server_user);
1052 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001053 buffer_put_cstring(&b, authctxt->method->name);
1054 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001055 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001056 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001057 buffer_put_string(&b, blob, bloblen);
1058 }
Darren Tuckera627d422013-06-02 07:31:17 +10001059 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001060
Damien Millereba71ba2000-04-29 23:57:08 +10001061 /* append signature */
1062 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001063 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001064
1065 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001066 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001067 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001068 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001069
1070 /* put remaining data from buffer into packet */
1071 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1072 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1073 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001074 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001075
1076 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001077}
1078
Ben Lindstrombba81212001-06-25 05:01:22 +00001079static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001080send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001081{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001082 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001083 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001084
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001085 debug3("send_pubkey_test");
1086
Damien Miller280ecfb2003-05-14 13:46:00 +10001087 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001088 /* we cannot handle this key */
1089 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001090 return 0;
1091 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001092 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001093 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001094
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001095 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1096 packet_put_cstring(authctxt->server_user);
1097 packet_put_cstring(authctxt->service);
1098 packet_put_cstring(authctxt->method->name);
1099 packet_put_char(have_sig);
1100 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001101 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001102 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001103 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001104 packet_send();
1105 return 1;
1106}
1107
Ben Lindstrombba81212001-06-25 05:01:22 +00001108static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001109load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001110{
1111 Key *private;
1112 char prompt[300], *passphrase;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001113 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001114 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001115
Ben Lindstrom329782e2001-03-10 17:08:59 +00001116 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001117 (userprovided ? logit : debug3)("no such identity: %s: %s",
1118 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001119 return NULL;
1120 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001121 snprintf(prompt, sizeof prompt,
1122 "Enter passphrase for key '%.100s': ", filename);
1123 for (i = 0; i <= options.number_of_password_prompts; i++) {
1124 if (i == 0)
1125 passphrase = "";
1126 else {
Damien Miller62cee002000-09-23 17:15:56 +11001127 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001128 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001129 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001130 free(passphrase);
1131 break;
Damien Miller62cee002000-09-23 17:15:56 +11001132 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001133 }
1134 switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename,
1135 passphrase, &private, NULL, &perm_ok))) {
1136 case 0:
1137 break;
1138 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1139 if (options.batch_mode) {
1140 quit = 1;
1141 break;
1142 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001143 if (i != 0)
1144 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001145 break;
1146 case SSH_ERR_SYSTEM_ERROR:
1147 if (errno == ENOENT) {
1148 debug2("Load key \"%s\": %s",
1149 filename, ssh_err(r));
1150 quit = 1;
1151 break;
1152 }
1153 /* FALLTHROUGH */
1154 default:
1155 error("Load key \"%s\": %s", filename, ssh_err(r));
1156 quit = 1;
1157 break;
1158 }
1159 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001160 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001161 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001162 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001163 if (private != NULL || quit)
1164 break;
Damien Miller994cf142000-07-21 10:19:44 +10001165 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001166 return private;
1167}
1168
Damien Miller280ecfb2003-05-14 13:46:00 +10001169/*
1170 * try keys in the following order:
1171 * 1. agent keys that are found in the config file
1172 * 2. other agent keys
1173 * 3. keys that are only listed in the config file
1174 */
1175static void
1176pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001177{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001178 struct identity *id, *id2, *tmp;
1179 struct idlist agent, files, *preferred;
1180 struct sshkey *key;
1181 int agent_fd, i, r, found;
1182 size_t j;
1183 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001184
Damien Miller280ecfb2003-05-14 13:46:00 +10001185 TAILQ_INIT(&agent); /* keys from the agent */
1186 TAILQ_INIT(&files); /* keys from the config file */
1187 preferred = &authctxt->keys;
1188 TAILQ_INIT(preferred); /* preferred order of keys */
1189
Damien Millercb6b68b2012-12-03 09:49:52 +11001190 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001191 for (i = 0; i < options.num_identity_files; i++) {
1192 key = options.identity_keys[i];
1193 if (key && key->type == KEY_RSA1)
1194 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001195 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1196 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001197 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001198 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001199 id->key = key;
1200 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001201 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001202 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001203 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001204 /* Prefer PKCS11 keys that are explicitly listed */
1205 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
Damien Miller86687062014-07-02 15:28:02 +10001206 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001207 continue;
1208 found = 0;
1209 TAILQ_FOREACH(id2, &files, next) {
1210 if (id2->key == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001211 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001212 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001213 if (sshkey_equal(id->key, id2->key)) {
Damien Millercb6b68b2012-12-03 09:49:52 +11001214 TAILQ_REMOVE(&files, id, next);
1215 TAILQ_INSERT_TAIL(preferred, id, next);
1216 found = 1;
1217 break;
1218 }
1219 }
1220 /* If IdentitiesOnly set and key not found then don't use it */
1221 if (!found && options.identities_only) {
1222 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001223 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001224 free(id);
1225 }
1226 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001227 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001228 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1229 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1230 debug("%s: ssh_get_authentication_socket: %s",
1231 __func__, ssh_err(r));
1232 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1233 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1234 debug("%s: ssh_fetch_identitylist: %s",
1235 __func__, ssh_err(r));
1236 } else {
1237 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001238 found = 0;
1239 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001240 /*
1241 * agent keys from the config file are
1242 * preferred
1243 */
1244 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001245 TAILQ_REMOVE(&files, id, next);
1246 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001247 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001248 found = 1;
1249 break;
1250 }
1251 }
Damien Millerbd394c32004-03-08 23:12:36 +11001252 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001253 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001254 /* XXX "steals" key/comment from idlist */
1255 id->key = idlist->keys[j];
1256 id->filename = idlist->comments[j];
1257 idlist->keys[j] = NULL;
1258 idlist->comments[j] = NULL;
1259 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001260 TAILQ_INSERT_TAIL(&agent, id, next);
1261 }
1262 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001263 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001264 /* append remaining agent keys */
1265 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1266 TAILQ_REMOVE(&agent, id, next);
1267 TAILQ_INSERT_TAIL(preferred, id, next);
1268 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001269 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001270 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001271 /* append remaining keys from the config file */
1272 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1273 TAILQ_REMOVE(&files, id, next);
1274 TAILQ_INSERT_TAIL(preferred, id, next);
1275 }
1276 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001277 debug2("key: %s (%p),%s", id->filename, id->key,
1278 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001279 }
1280}
1281
1282static void
1283pubkey_cleanup(Authctxt *authctxt)
1284{
1285 Identity *id;
1286
djm@openbsd.org141efe42015-01-14 20:05:27 +00001287 if (authctxt->agent_fd != -1)
1288 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001289 for (id = TAILQ_FIRST(&authctxt->keys); id;
1290 id = TAILQ_FIRST(&authctxt->keys)) {
1291 TAILQ_REMOVE(&authctxt->keys, id, next);
1292 if (id->key)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001293 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001294 free(id->filename);
1295 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001296 }
Damien Millereba71ba2000-04-29 23:57:08 +10001297}
1298
Damien Miller62cee002000-09-23 17:15:56 +11001299int
1300userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001301{
Damien Miller280ecfb2003-05-14 13:46:00 +10001302 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001303 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001304
Damien Miller280ecfb2003-05-14 13:46:00 +10001305 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1306 if (id->tried++)
1307 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001308 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001309 TAILQ_REMOVE(&authctxt->keys, id, next);
1310 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1311 /*
1312 * send a test message if we have the public key. for
1313 * encrypted keys we cannot do this and have to load the
1314 * private key instead
1315 */
Damien Miller324541e2013-12-31 12:25:40 +11001316 if (id->key != NULL) {
1317 if (key_type_plain(id->key->type) == KEY_RSA &&
1318 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1319 debug("Skipped %s key %s for RSA/MD5 server",
1320 key_type(id->key), id->filename);
1321 } else if (id->key->type != KEY_RSA1) {
1322 debug("Offering %s public key: %s",
1323 key_type(id->key), id->filename);
1324 sent = send_pubkey_test(authctxt, id);
1325 }
1326 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001327 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001328 id->key = load_identity_file(id->filename,
1329 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001330 if (id->key != NULL) {
1331 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001332 if (key_type_plain(id->key->type) == KEY_RSA &&
1333 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1334 debug("Skipped %s key %s for RSA/MD5 "
1335 "server", key_type(id->key),
1336 id->filename);
1337 } else {
1338 sent = sign_and_send_pubkey(
1339 authctxt, id);
1340 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001341 key_free(id->key);
1342 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001343 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001344 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001345 if (sent)
1346 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001347 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001348 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001349}
Damien Millereba71ba2000-04-29 23:57:08 +10001350
Damien Miller874d77b2000-10-14 16:23:11 +11001351/*
1352 * Send userauth request message specifying keyboard-interactive method.
1353 */
1354int
1355userauth_kbdint(Authctxt *authctxt)
1356{
1357 static int attempt = 0;
1358
1359 if (attempt++ >= options.number_of_password_prompts)
1360 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001361 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1362 if (attempt > 1 && !authctxt->info_req_seen) {
1363 debug3("userauth_kbdint: disable: no info_req_seen");
1364 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1365 return 0;
1366 }
Damien Miller874d77b2000-10-14 16:23:11 +11001367
1368 debug2("userauth_kbdint");
1369 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1370 packet_put_cstring(authctxt->server_user);
1371 packet_put_cstring(authctxt->service);
1372 packet_put_cstring(authctxt->method->name);
1373 packet_put_cstring(""); /* lang */
1374 packet_put_cstring(options.kbd_interactive_devices ?
1375 options.kbd_interactive_devices : "");
1376 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001377
1378 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1379 return 1;
1380}
1381
1382/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001383 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001384 */
1385void
Damien Miller630d6f42002-01-22 23:17:30 +11001386input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001387{
1388 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001389 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001390 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001391 int echo = 0;
1392
1393 debug2("input_userauth_info_req");
1394
1395 if (authctxt == NULL)
1396 fatal("input_userauth_info_req: no authentication context");
1397
Ben Lindstrom7d199962001-09-12 18:29:00 +00001398 authctxt->info_req_seen = 1;
1399
Damien Miller874d77b2000-10-14 16:23:11 +11001400 name = packet_get_string(NULL);
1401 inst = packet_get_string(NULL);
1402 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001403 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001404 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001405 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001406 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001407 free(name);
1408 free(inst);
1409 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001410
1411 num_prompts = packet_get_int();
1412 /*
1413 * Begin to build info response packet based on prompts requested.
1414 * We commit to providing the correct number of responses, so if
1415 * further on we run into a problem that prevents this, we have to
1416 * be sure and clean this up and send a correct error response.
1417 */
1418 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1419 packet_put_int(num_prompts);
1420
Ben Lindstrom551ea372001-06-05 18:56:16 +00001421 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001422 for (i = 0; i < num_prompts; i++) {
1423 prompt = packet_get_string(NULL);
1424 echo = packet_get_char();
1425
Ben Lindstrom949974b2001-06-25 05:20:31 +00001426 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001427
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001428 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001429 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001430 free(response);
1431 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001432 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001433 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001434
Damien Miller9f643902001-11-12 11:02:52 +11001435 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001436 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001437}
Damien Miller62cee002000-09-23 17:15:56 +11001438
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001439static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001440ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001441 u_char *data, u_int datalen)
1442{
1443 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001444 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001445 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001446 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001447
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001448 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001449
Ben Lindstrom5206b952002-06-06 19:59:29 +00001450 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001451 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001452 return -1;
1453 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001454 if (fflush(stdout) != 0)
1455 error("ssh_keysign: fflush: %s", strerror(errno));
1456 if (pipe(to) < 0) {
1457 error("ssh_keysign: pipe: %s", strerror(errno));
1458 return -1;
1459 }
1460 if (pipe(from) < 0) {
1461 error("ssh_keysign: pipe: %s", strerror(errno));
1462 return -1;
1463 }
1464 if ((pid = fork()) < 0) {
1465 error("ssh_keysign: fork: %s", strerror(errno));
1466 return -1;
1467 }
1468 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001469 /* keep the socket on exec */
1470 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001471 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001472 close(from[0]);
1473 if (dup2(from[1], STDOUT_FILENO) < 0)
1474 fatal("ssh_keysign: dup2: %s", strerror(errno));
1475 close(to[1]);
1476 if (dup2(to[0], STDIN_FILENO) < 0)
1477 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001478 close(from[1]);
1479 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001480 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001481 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1482 strerror(errno));
1483 }
1484 close(from[1]);
1485 close(to[0]);
1486
1487 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001488 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001489 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001490 if (ssh_msg_send(to[1], version, &b) == -1)
1491 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001492
Damien Miller901119b2002-10-04 11:10:04 +10001493 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001494 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001495 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001496 return -1;
1497 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001498 close(from[0]);
1499 close(to[1]);
1500
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001501 while (waitpid(pid, &status, 0) < 0)
1502 if (errno != EINTR)
1503 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001504
Ben Lindstrom5206b952002-06-06 19:59:29 +00001505 if (buffer_get_char(&b) != version) {
1506 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001507 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001508 return -1;
1509 }
1510 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001511 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001512
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001513 return 0;
1514}
1515
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001516int
1517userauth_hostbased(Authctxt *authctxt)
1518{
1519 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001520 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001521 Buffer b;
1522 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001523 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001524 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001525 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001526 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001527
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001528 /* XXX provide some way to allow user to specify key types attempted */
1529
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001530 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001531 for (i = 0; i < sensitive->nkeys; i++) {
1532 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001533 if (private && private->type != KEY_RSA1) {
1534 found = 1;
1535 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001536 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001537 break;
1538 }
1539 }
1540 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001541 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001542 return 0;
1543 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001544
1545 debug("%s: trying hostkey type %s", __func__, key_type(private));
1546
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001547 if (key_to_blob(private, &blob, &blen) == 0) {
1548 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001549 return 0;
1550 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001551
Damien Miller91c18472001-11-12 11:02:03 +11001552 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001553 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001554 if (p == NULL) {
1555 error("userauth_hostbased: cannot get local ipaddr/name");
1556 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001557 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001558 return 0;
1559 }
Damien Miller07d86be2006-03-26 14:19:21 +11001560 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001561 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001562 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001563
Ben Lindstrom671388f2001-04-19 20:40:45 +00001564 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1565 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001566 pkalg = xstrdup(key_ssh_name(private));
1567 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001568 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001569 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001570 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1571 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001572 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001573 buffer_put_cstring(&b, authctxt->method->name);
1574 buffer_put_cstring(&b, pkalg);
1575 buffer_put_string(&b, blob, blen);
1576 buffer_put_cstring(&b, chost);
1577 buffer_put_cstring(&b, authctxt->local_user);
1578#ifdef DEBUG_PK
1579 buffer_dump(&b);
1580#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001581 if (sensitive->external_keysign)
1582 ok = ssh_keysign(private, &signature, &slen,
1583 buffer_ptr(&b), buffer_len(&b));
1584 else
1585 ok = key_sign(private, &signature, &slen,
1586 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001587 key_free(private);
1588 buffer_free(&b);
1589 if (ok != 0) {
1590 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001591 free(chost);
1592 free(pkalg);
1593 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001594 return 0;
1595 }
1596 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1597 packet_put_cstring(authctxt->server_user);
1598 packet_put_cstring(authctxt->service);
1599 packet_put_cstring(authctxt->method->name);
1600 packet_put_cstring(pkalg);
1601 packet_put_string(blob, blen);
1602 packet_put_cstring(chost);
1603 packet_put_cstring(authctxt->local_user);
1604 packet_put_string(signature, slen);
Damien Millera5103f42014-02-04 11:20:14 +11001605 explicit_bzero(signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001606 free(signature);
1607 free(chost);
1608 free(pkalg);
1609 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001610
1611 packet_send();
1612 return 1;
1613}
1614
Damien Miller62cee002000-09-23 17:15:56 +11001615/* find auth method */
1616
Damien Miller62cee002000-09-23 17:15:56 +11001617/*
1618 * given auth method name, if configurable options permit this method fill
1619 * in auth_ident field and return true, otherwise return false.
1620 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001621static int
Damien Miller62cee002000-09-23 17:15:56 +11001622authmethod_is_enabled(Authmethod *method)
1623{
1624 if (method == NULL)
1625 return 0;
1626 /* return false if options indicate this method is disabled */
1627 if (method->enabled == NULL || *method->enabled == 0)
1628 return 0;
1629 /* return false if batch mode is enabled but method needs interactive mode */
1630 if (method->batch_flag != NULL && *method->batch_flag != 0)
1631 return 0;
1632 return 1;
1633}
1634
Ben Lindstrombba81212001-06-25 05:01:22 +00001635static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001636authmethod_lookup(const char *name)
1637{
1638 Authmethod *method = NULL;
1639 if (name != NULL)
1640 for (method = authmethods; method->name != NULL; method++)
1641 if (strcmp(name, method->name) == 0)
1642 return method;
1643 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1644 return NULL;
1645}
1646
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001647/* XXX internal state */
1648static Authmethod *current = NULL;
1649static char *supported = NULL;
1650static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001651
Damien Miller62cee002000-09-23 17:15:56 +11001652/*
1653 * Given the authentication method list sent by the server, return the
1654 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001655 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001656 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001657static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001658authmethod_get(char *authlist)
1659{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001660 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001661 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001662
Damien Miller62cee002000-09-23 17:15:56 +11001663 /* Use a suitable default if we're passed a nil list. */
1664 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001665 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001666
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001667 if (supported == NULL || strcmp(authlist, supported) != 0) {
1668 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001669 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001670 supported = xstrdup(authlist);
1671 preferred = options.preferred_authentications;
1672 debug3("preferred %s", preferred);
1673 current = NULL;
1674 } else if (current != NULL && authmethod_is_enabled(current))
1675 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001676
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001677 for (;;) {
1678 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001679 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001680 current = NULL;
1681 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001682 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001683 preferred += next;
1684 debug3("authmethod_lookup %s", name);
1685 debug3("remaining preferred: %s", preferred);
1686 if ((current = authmethod_lookup(name)) != NULL &&
1687 authmethod_is_enabled(current)) {
1688 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001689 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001690 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001691 return current;
1692 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001693 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001694 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001695}
Damien Miller62cee002000-09-23 17:15:56 +11001696
Ben Lindstrom79073822001-07-04 03:42:30 +00001697static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001698authmethods_get(void)
1699{
1700 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001701 Buffer b;
1702 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001703
Damien Miller0e3b8722002-01-22 23:26:38 +11001704 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001705 for (method = authmethods; method->name != NULL; method++) {
1706 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001707 if (buffer_len(&b) > 0)
1708 buffer_append(&b, ",", 1);
1709 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001710 }
Damien Miller62cee002000-09-23 17:15:56 +11001711 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001712 buffer_append(&b, "\0", 1);
1713 list = xstrdup(buffer_ptr(&b));
1714 buffer_free(&b);
1715 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001716}
Damien Miller01ed2272008-11-05 16:20:46 +11001717