blob: 343ca745916a5f01c382d90443d2347268543384 [file] [log] [blame]
djm@openbsd.org141efe42015-01-14 20:05:27 +00001/* $OpenBSD: sshconnect2.c,v 1.214 2015/01/14 20:05:27 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
144 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
145 if (*first != '\0')
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
147
Darren Tuckera627d422013-06-02 07:31:17 +1000148 free(first);
149 free(last);
150 free(hostname);
151 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100152 free_hostkeys(hostkeys);
153
154 return ret;
155}
156
Damien Millereba71ba2000-04-29 23:57:08 +1000157void
Damien Millerd925dcd2010-12-01 12:21:51 +1100158ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100159{
Damien Miller9235a032014-04-20 13:17:20 +1000160 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
Damien Miller874d77b2000-10-14 16:23:11 +1100161 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000162
163 xxx_host = host;
164 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100165
Damien Millere39cacc2000-11-29 12:18:44 +1100166 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000167 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100168 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100169 }
170 if (options.ciphers != NULL) {
171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
172 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
173 }
Damien Millera0ff4662001-03-30 10:49:35 +1000174 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
176 myproposal[PROPOSAL_ENC_ALGS_STOC] =
177 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100178 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000179 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000180 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100181 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000182 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000183 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100184 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000185 if (options.macs != NULL) {
186 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
187 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
188 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000189 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100191 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 else {
193 /* Prefer algorithms that we already have keys for */
194 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100195 compat_pkalg_proposal(
196 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100197 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000198 if (options.kex_algorithms != NULL)
199 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller9395b282014-04-20 13:25:30 +1000200 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
201 myproposal[PROPOSAL_KEX_ALGS]);
Damien Miller874d77b2000-10-14 16:23:11 +1100202
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000203 if (options.rekey_limit || options.rekey_interval)
204 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
205 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000206
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000207 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000208 kex = kex_setup(myproposal);
Damien Miller1f0311c2014-05-15 14:24:09 +1000209#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100210 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000211 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100212 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100213 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000214 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Damien Miller1f0311c2014-05-15 14:24:09 +1000215#endif
Damien Miller1e124262013-11-04 08:26:52 +1100216 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000217 kex->client_version_string=client_version_string;
218 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000219 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100220
Ben Lindstromf28f6342001-04-04 02:03:04 +0000221 xxx_kex = kex;
222
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000223 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100224
Darren Tucker36331b52010-01-08 16:50:41 +1100225 if (options.use_roaming && !kex->roaming) {
226 debug("Roaming not allowed by server");
227 options.use_roaming = 0;
228 }
229
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000230 session_id2 = kex->session_id;
231 session_id2_len = kex->session_id_len;
232
Damien Miller874d77b2000-10-14 16:23:11 +1100233#ifdef DEBUG_KEXDH
234 /* send 1st encrypted/maced/compressed message */
235 packet_start(SSH2_MSG_IGNORE);
236 packet_put_cstring("markus");
237 packet_send();
238 packet_write_wait();
239#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000240}
Damien Millerb1715dc2000-05-30 13:44:51 +1000241
Damien Millereba71ba2000-04-29 23:57:08 +1000242/*
243 * Authenticate user
244 */
Damien Miller62cee002000-09-23 17:15:56 +1100245
djm@openbsd.org141efe42015-01-14 20:05:27 +0000246typedef struct cauthctxt Authctxt;
247typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000248typedef struct identity Identity;
249typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100250
Damien Miller280ecfb2003-05-14 13:46:00 +1000251struct identity {
252 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000253 int agent_fd; /* >=0 if agent supports key */
254 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000255 char *filename; /* comment for agent-only keys */
256 int tried;
257 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100258 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000259};
260TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100261
djm@openbsd.org141efe42015-01-14 20:05:27 +0000262struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100265 const char *host;
266 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000268 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000269 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000270 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000271 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000272 struct idlist keys;
273 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000274 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000275 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000276 /* kbd-interactive */
277 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000278 /* generic */
279 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100280};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000281
282struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100283 char *name; /* string to compare against server's list */
284 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100285 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100286 int *enabled; /* flag in option struct that enables method */
287 int *batch_flag; /* flag in option struct that disables method */
288};
289
Damien Miller630d6f42002-01-22 23:17:30 +1100290void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100291void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100292void input_userauth_failure(int, u_int32_t, void *);
293void input_userauth_banner(int, u_int32_t, void *);
294void input_userauth_error(int, u_int32_t, void *);
295void input_userauth_info_req(int, u_int32_t, void *);
296void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000297void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100298
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000299int userauth_none(Authctxt *);
300int userauth_pubkey(Authctxt *);
301int userauth_passwd(Authctxt *);
302int userauth_kbdint(Authctxt *);
303int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100304
Darren Tucker0efd1552003-08-26 11:49:55 +1000305#ifdef GSSAPI
306int userauth_gssapi(Authctxt *authctxt);
307void input_gssapi_response(int type, u_int32_t, void *);
308void input_gssapi_token(int type, u_int32_t, void *);
309void input_gssapi_hash(int type, u_int32_t, void *);
310void input_gssapi_error(int, u_int32_t, void *);
311void input_gssapi_errtok(int, u_int32_t, void *);
312#endif
313
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000314void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000315
Damien Miller280ecfb2003-05-14 13:46:00 +1000316static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000317static void pubkey_prepare(Authctxt *);
318static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100319static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000320
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static Authmethod *authmethod_get(char *authlist);
322static Authmethod *authmethod_lookup(const char *name);
323static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100324
325Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000326#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100327 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000328 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100329 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000330 &options.gss_authentication,
331 NULL},
332#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000333 {"hostbased",
334 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100335 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000336 &options.hostbased_authentication,
337 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000338 {"publickey",
339 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100340 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000341 &options.pubkey_authentication,
342 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100343 {"keyboard-interactive",
344 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100345 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100346 &options.kbd_interactive_authentication,
347 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000348 {"password",
349 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100350 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000351 &options.password_authentication,
352 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100353 {"none",
354 userauth_none,
355 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100356 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100357 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100358 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100359};
360
361void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000362ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000363 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100364{
365 Authctxt authctxt;
366 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100367
Ben Lindstrom551ea372001-06-05 18:56:16 +0000368 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000369 options.kbd_interactive_authentication = 1;
370
Damien Miller62cee002000-09-23 17:15:56 +1100371 packet_start(SSH2_MSG_SERVICE_REQUEST);
372 packet_put_cstring("ssh-userauth");
373 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000374 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100375 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100376 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000377 if (type != SSH2_MSG_SERVICE_ACCEPT)
378 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100379 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100380 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000381 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100383 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000384 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100385 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100386 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000387 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100388
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000389 if (options.preferred_authentications == NULL)
390 options.preferred_authentications = authmethods_get();
391
Damien Miller62cee002000-09-23 17:15:56 +1100392 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000393 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000394 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100395 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000396 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100397 authctxt.host = host;
398 authctxt.service = "ssh-connection"; /* service name */
399 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100400 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000401 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000402 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000403 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000404 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100405 if (authctxt.method == NULL)
406 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100407
408 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100409 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100410
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000411 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100412 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
413 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000414 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100415 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
416
Damien Miller280ecfb2003-05-14 13:46:00 +1000417 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000418 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
419
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000420 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100421}
Damien Millerf842fcb2003-05-15 12:01:28 +1000422
Damien Miller62cee002000-09-23 17:15:56 +1100423void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000424userauth(Authctxt *authctxt, char *authlist)
425{
Damien Miller01ed2272008-11-05 16:20:46 +1100426 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
427 authctxt->method->cleanup(authctxt);
428
Darren Tuckera627d422013-06-02 07:31:17 +1000429 free(authctxt->methoddata);
430 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431 if (authlist == NULL) {
432 authlist = authctxt->authlist;
433 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000434 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000435 authctxt->authlist = authlist;
436 }
437 for (;;) {
438 Authmethod *method = authmethod_get(authlist);
439 if (method == NULL)
440 fatal("Permission denied (%s).", authlist);
441 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000442
443 /* reset the per method handler */
444 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
445 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
446
447 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000448 if (method->userauth(authctxt) != 0) {
449 debug2("we sent a %s packet, wait for reply", method->name);
450 break;
451 } else {
452 debug2("we did not send a packet, disable method");
453 method->enabled = NULL;
454 }
455 }
456}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000457
Damien Millerf7475d72008-11-03 19:26:18 +1100458/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000459void
Damien Miller630d6f42002-01-22 23:17:30 +1100460input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100461{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000462 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000463 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000464}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000465
Damien Millerf7475d72008-11-03 19:26:18 +1100466/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000467void
Damien Miller630d6f42002-01-22 23:17:30 +1100468input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000470 char *msg, *raw, *lang;
471 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000472
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000473 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000474 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000475 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100476 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000477 if (len > 65536)
478 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100479 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100480 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000481 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000482 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000483 }
Darren Tuckera627d422013-06-02 07:31:17 +1000484 free(raw);
485 free(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100486}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000487
Damien Millerf7475d72008-11-03 19:26:18 +1100488/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100489void
Damien Miller630d6f42002-01-22 23:17:30 +1100490input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100491{
492 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100493
Damien Miller62cee002000-09-23 17:15:56 +1100494 if (authctxt == NULL)
495 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000496 free(authctxt->authlist);
497 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100498 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
499 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000500 free(authctxt->methoddata);
501 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100502 authctxt->success = 1; /* break out */
503}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000504
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100505void
506input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
507{
508 Authctxt *authctxt = ctxt;
509
510 if (authctxt == NULL)
511 fatal("%s: no authentication context", __func__);
512
513 fatal("Unexpected authentication success during %s.",
514 authctxt->method->name);
515}
516
Damien Millerf7475d72008-11-03 19:26:18 +1100517/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100518void
Damien Miller630d6f42002-01-22 23:17:30 +1100519input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100520{
Damien Miller62cee002000-09-23 17:15:56 +1100521 Authctxt *authctxt = ctxt;
522 char *authlist = NULL;
523 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100524
525 if (authctxt == NULL)
526 fatal("input_userauth_failure: no authentication context");
527
Damien Miller874d77b2000-10-14 16:23:11 +1100528 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100529 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100530 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100531
Damien Miller62e9c4f2013-04-23 15:15:49 +1000532 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000533 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000534 /* reset state */
535 pubkey_cleanup(authctxt);
536 pubkey_prepare(authctxt);
537 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000538 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100539
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000540 userauth(authctxt, authlist);
541}
Damien Millerf7475d72008-11-03 19:26:18 +1100542
543/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000544void
Damien Miller630d6f42002-01-22 23:17:30 +1100545input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000546{
547 Authctxt *authctxt = ctxt;
548 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000549 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000550 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000551 int pktype, sent = 0;
552 u_int alen, blen;
553 char *pkalg, *fp;
554 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000555
556 if (authctxt == NULL)
557 fatal("input_userauth_pk_ok: no authentication context");
558 if (datafellows & SSH_BUG_PKOK) {
559 /* this is similar to SSH_BUG_PKAUTH */
560 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
561 pkblob = packet_get_string(&blen);
562 buffer_init(&b);
563 buffer_append(&b, pkblob, blen);
564 pkalg = buffer_get_string(&b, &alen);
565 buffer_free(&b);
566 } else {
567 pkalg = packet_get_string(&alen);
568 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000569 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100570 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000571
Damien Miller280ecfb2003-05-14 13:46:00 +1000572 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000573
Damien Miller280ecfb2003-05-14 13:46:00 +1000574 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
575 debug("unknown pkalg %s", pkalg);
576 goto done;
577 }
578 if ((key = key_from_blob(pkblob, blen)) == NULL) {
579 debug("no key from blob. pkalg %s", pkalg);
580 goto done;
581 }
582 if (key->type != pktype) {
583 error("input_userauth_pk_ok: type mismatch "
584 "for decoded key (received %d, expected %d)",
585 key->type, pktype);
586 goto done;
587 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000588 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller280ecfb2003-05-14 13:46:00 +1000589 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000590 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000591
Darren Tuckerd05b6012003-10-15 15:55:59 +1000592 /*
593 * search keys in the reverse order, because last candidate has been
594 * moved to the end of the queue. this also avoids confusion by
595 * duplicate keys
596 */
Damien Miller0b51a522004-04-20 20:07:19 +1000597 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000598 if (key_equal(key, id->key)) {
599 sent = sign_and_send_pubkey(authctxt, id);
600 break;
601 }
602 }
603done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000604 if (key != NULL)
605 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000606 free(pkalg);
607 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000608
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000609 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610 if (sent == 0)
611 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100612}
613
Darren Tucker0efd1552003-08-26 11:49:55 +1000614#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100615int
Darren Tucker0efd1552003-08-26 11:49:55 +1000616userauth_gssapi(Authctxt *authctxt)
617{
618 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100619 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000620 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000621 OM_uint32 min;
622 int ok = 0;
623
624 /* Try one GSSAPI method at a time, rather than sending them all at
625 * once. */
626
Darren Tucker56afe142003-11-03 20:06:14 +1100627 if (gss_supported == NULL)
628 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000629
630 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100631 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000632 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100633 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000634 ssh_gssapi_check_mechanism(&gssctxt,
635 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000636 ok = 1; /* Mechanism works */
637 } else {
638 mech++;
639 }
640 }
641
Damien Millera1cb9f32006-08-19 00:33:34 +1000642 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000643 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000644
645 authctxt->methoddata=(void *)gssctxt;
646
647 packet_start(SSH2_MSG_USERAUTH_REQUEST);
648 packet_put_cstring(authctxt->server_user);
649 packet_put_cstring(authctxt->service);
650 packet_put_cstring(authctxt->method->name);
651
652 packet_put_int(1);
653
Darren Tucker655a5e02003-11-03 20:09:03 +1100654 packet_put_int((gss_supported->elements[mech].length) + 2);
655 packet_put_char(SSH_GSS_OIDTYPE);
656 packet_put_char(gss_supported->elements[mech].length);
657 packet_put_raw(gss_supported->elements[mech].elements,
658 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000659
660 packet_send();
661
662 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
663 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
664 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
665 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
666
667 mech++; /* Move along to next candidate */
668
669 return 1;
670}
671
Damien Miller91c6aa42003-11-17 21:20:18 +1100672static OM_uint32
673process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
674{
675 Authctxt *authctxt = ctxt;
676 Gssctxt *gssctxt = authctxt->methoddata;
677 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000678 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
679 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100680 OM_uint32 status, ms, flags;
681 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100682
Damien Miller91c6aa42003-11-17 21:20:18 +1100683 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100684 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100685
686 if (send_tok.length > 0) {
687 if (GSS_ERROR(status))
688 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
689 else
690 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100691
Damien Miller91c6aa42003-11-17 21:20:18 +1100692 packet_put_string(send_tok.value, send_tok.length);
693 packet_send();
694 gss_release_buffer(&ms, &send_tok);
695 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100696
Damien Miller91c6aa42003-11-17 21:20:18 +1100697 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100698 /* send either complete or MIC, depending on mechanism */
699 if (!(flags & GSS_C_INTEG_FLAG)) {
700 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
701 packet_send();
702 } else {
703 ssh_gssapi_buildmic(&b, authctxt->server_user,
704 authctxt->service, "gssapi-with-mic");
705
706 gssbuf.value = buffer_ptr(&b);
707 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100708
Damien Miller0425d402003-11-17 22:18:21 +1100709 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100710
Damien Miller0425d402003-11-17 22:18:21 +1100711 if (!GSS_ERROR(status)) {
712 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
713 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100714
Damien Miller0425d402003-11-17 22:18:21 +1100715 packet_send();
716 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100717
Damien Miller0425d402003-11-17 22:18:21 +1100718 buffer_free(&b);
719 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100720 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100721 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100722
Damien Miller91c6aa42003-11-17 21:20:18 +1100723 return status;
724}
725
Damien Millerf7475d72008-11-03 19:26:18 +1100726/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000727void
728input_gssapi_response(int type, u_int32_t plen, void *ctxt)
729{
730 Authctxt *authctxt = ctxt;
731 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000732 int oidlen;
733 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000734
735 if (authctxt == NULL)
736 fatal("input_gssapi_response: no authentication context");
737 gssctxt = authctxt->methoddata;
738
739 /* Setup our OID */
740 oidv = packet_get_string(&oidlen);
741
Darren Tucker655a5e02003-11-03 20:09:03 +1100742 if (oidlen <= 2 ||
743 oidv[0] != SSH_GSS_OIDTYPE ||
744 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000745 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100746 debug("Badly encoded mechanism OID received");
747 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100748 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000749 }
750
Darren Tucker655a5e02003-11-03 20:09:03 +1100751 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
752 fatal("Server returned different OID than expected");
753
Darren Tucker0efd1552003-08-26 11:49:55 +1000754 packet_check_eom();
755
Darren Tuckera627d422013-06-02 07:31:17 +1000756 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000757
Damien Miller91c6aa42003-11-17 21:20:18 +1100758 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000759 /* Start again with next method on list */
760 debug("Trying to start again");
761 userauth(authctxt, NULL);
762 return;
763 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000764}
765
Damien Millerf7475d72008-11-03 19:26:18 +1100766/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000767void
768input_gssapi_token(int type, u_int32_t plen, void *ctxt)
769{
770 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000771 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100772 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000773 u_int slen;
774
775 if (authctxt == NULL)
776 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000777
778 recv_tok.value = packet_get_string(&slen);
779 recv_tok.length = slen; /* safe typecast */
780
781 packet_check_eom();
782
Damien Miller91c6aa42003-11-17 21:20:18 +1100783 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000784
Darren Tuckera627d422013-06-02 07:31:17 +1000785 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000786
787 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000788 /* Start again with the next method in the list */
789 userauth(authctxt, NULL);
790 return;
791 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000792}
793
Damien Millerf7475d72008-11-03 19:26:18 +1100794/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000795void
796input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
797{
798 Authctxt *authctxt = ctxt;
799 Gssctxt *gssctxt;
800 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
801 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000802 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000803 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000804
805 if (authctxt == NULL)
806 fatal("input_gssapi_response: no authentication context");
807 gssctxt = authctxt->methoddata;
808
Darren Tucker600ad8d2003-08-26 12:10:48 +1000809 recv_tok.value = packet_get_string(&len);
810 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000811
812 packet_check_eom();
813
814 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000815 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000816 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000817
Darren Tuckera627d422013-06-02 07:31:17 +1000818 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000819 gss_release_buffer(&ms, &send_tok);
820
821 /* Server will be returning a failed packet after this one */
822}
823
Damien Millerf7475d72008-11-03 19:26:18 +1100824/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000825void
826input_gssapi_error(int type, u_int32_t plen, void *ctxt)
827{
Darren Tucker0efd1552003-08-26 11:49:55 +1000828 char *msg;
829 char *lang;
830
Damien Millerd677ad12013-04-23 15:18:51 +1000831 /* maj */(void)packet_get_int();
832 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000833 msg=packet_get_string(NULL);
834 lang=packet_get_string(NULL);
835
836 packet_check_eom();
837
Damien Miller15d72a02005-11-05 15:07:33 +1100838 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000839 free(msg);
840 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000841}
842#endif /* GSSAPI */
843
Damien Millereba71ba2000-04-29 23:57:08 +1000844int
Damien Miller874d77b2000-10-14 16:23:11 +1100845userauth_none(Authctxt *authctxt)
846{
847 /* initial userauth request */
848 packet_start(SSH2_MSG_USERAUTH_REQUEST);
849 packet_put_cstring(authctxt->server_user);
850 packet_put_cstring(authctxt->service);
851 packet_put_cstring(authctxt->method->name);
852 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100853 return 1;
854}
855
856int
Damien Miller62cee002000-09-23 17:15:56 +1100857userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000858{
Damien Millere247cc42000-05-07 12:03:14 +1000859 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000860 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000861 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100862 const char *host = options.host_key_alias ? options.host_key_alias :
863 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000864
Damien Millerd3a18572000-06-07 19:55:44 +1000865 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000866 return 0;
867
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000868 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000869 error("Permission denied, please try again.");
870
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000871 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100872 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000873 password = read_passphrase(prompt, 0);
874 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100875 packet_put_cstring(authctxt->server_user);
876 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100877 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000878 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000879 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100880 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100882 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000883 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000884
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000885 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000886 &input_userauth_passwd_changereq);
887
Damien Millereba71ba2000-04-29 23:57:08 +1000888 return 1;
889}
Damien Millerf7475d72008-11-03 19:26:18 +1100890
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000891/*
892 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
893 */
Damien Millerf7475d72008-11-03 19:26:18 +1100894/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000895void
Tim Ricec8549622002-03-31 12:49:38 -0800896input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000897{
898 Authctxt *authctxt = ctxt;
899 char *info, *lang, *password = NULL, *retype = NULL;
900 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100901 const char *host = options.host_key_alias ? options.host_key_alias :
902 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000903
904 debug2("input_userauth_passwd_changereq");
905
906 if (authctxt == NULL)
907 fatal("input_userauth_passwd_changereq: "
908 "no authentication context");
909
910 info = packet_get_string(NULL);
911 lang = packet_get_string(NULL);
912 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000913 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000914 free(info);
915 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000916 packet_start(SSH2_MSG_USERAUTH_REQUEST);
917 packet_put_cstring(authctxt->server_user);
918 packet_put_cstring(authctxt->service);
919 packet_put_cstring(authctxt->method->name);
920 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000921 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000922 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100923 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000924 password = read_passphrase(prompt, 0);
925 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100926 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000927 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000928 password = NULL;
929 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000930 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000931 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100932 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000933 password = read_passphrase(prompt, RP_ALLOW_EOF);
934 if (password == NULL) {
935 /* bail out */
936 return;
937 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000938 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000939 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100940 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000941 retype = read_passphrase(prompt, 0);
942 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100943 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000944 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000945 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000946 password = NULL;
947 }
Damien Millera5103f42014-02-04 11:20:14 +1100948 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000949 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000950 }
951 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100952 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000953 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000954 packet_add_padding(64);
955 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000956
957 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000958 &input_userauth_passwd_changereq);
959}
Damien Millereba71ba2000-04-29 23:57:08 +1000960
Ben Lindstrombba81212001-06-25 05:01:22 +0000961static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000962identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
963 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +1000964{
965 Key *prv;
966 int ret;
967
968 /* the agent supports this key */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000969 if (id->agent_fd)
970 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
971 data, datalen, compat);
972
Damien Miller280ecfb2003-05-14 13:46:00 +1000973 /*
974 * we have already loaded the private key or
975 * the private key is stored in external hardware
976 */
Damien Miller86687062014-07-02 15:28:02 +1000977 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
djm@openbsd.org141efe42015-01-14 20:05:27 +0000978 return (sshkey_sign(id->key, sigp, lenp, data, datalen,
979 compat));
Damien Miller280ecfb2003-05-14 13:46:00 +1000980 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +1100981 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000982 return (-1); /* XXX return decent error code */
983 ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat);
984 sshkey_free(prv);
Damien Miller280ecfb2003-05-14 13:46:00 +1000985 return (ret);
986}
987
988static int
989sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000990{
991 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000992 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000993 u_int bloblen;
994 size_t slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000995 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000996 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100997 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +1000998 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000999
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001000 fp = key_fingerprint(id->key, options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller4e270b02010-04-16 15:56:21 +10001001 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001002 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001003
Damien Miller280ecfb2003-05-14 13:46:00 +10001004 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001005 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001006 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001007 return 0;
1008 }
Damien Millereba71ba2000-04-29 23:57:08 +10001009 /* data to be signed */
1010 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001011 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001012 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001013 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001014 } else {
1015 buffer_put_string(&b, session_id2, session_id2_len);
1016 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001017 }
Damien Millereba71ba2000-04-29 23:57:08 +10001018 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001019 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001020 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001021 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001022 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001023 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001024 if (datafellows & SSH_BUG_PKAUTH) {
1025 buffer_put_char(&b, have_sig);
1026 } else {
1027 buffer_put_cstring(&b, authctxt->method->name);
1028 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001029 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001030 }
Damien Millereba71ba2000-04-29 23:57:08 +10001031 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001032
1033 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001034 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001035 buffer_ptr(&b), buffer_len(&b), datafellows);
1036 if (ret != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001037 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001038 buffer_free(&b);
1039 return 0;
1040 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001041#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001042 buffer_dump(&b);
1043#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001044 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001045 buffer_clear(&b);
1046 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001047 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001048 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001049 buffer_put_cstring(&b, authctxt->server_user);
1050 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001051 buffer_put_cstring(&b, authctxt->method->name);
1052 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001053 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001054 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001055 buffer_put_string(&b, blob, bloblen);
1056 }
Darren Tuckera627d422013-06-02 07:31:17 +10001057 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001058
Damien Millereba71ba2000-04-29 23:57:08 +10001059 /* append signature */
1060 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001061 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001062
1063 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001064 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001065 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001066 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001067
1068 /* put remaining data from buffer into packet */
1069 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1070 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1071 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001072 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001073
1074 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001075}
1076
Ben Lindstrombba81212001-06-25 05:01:22 +00001077static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001078send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001079{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001080 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001081 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001082
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001083 debug3("send_pubkey_test");
1084
Damien Miller280ecfb2003-05-14 13:46:00 +10001085 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001086 /* we cannot handle this key */
1087 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001088 return 0;
1089 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001090 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001091 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001092
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001093 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1094 packet_put_cstring(authctxt->server_user);
1095 packet_put_cstring(authctxt->service);
1096 packet_put_cstring(authctxt->method->name);
1097 packet_put_char(have_sig);
1098 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001099 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001100 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001101 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001102 packet_send();
1103 return 1;
1104}
1105
Ben Lindstrombba81212001-06-25 05:01:22 +00001106static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001107load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001108{
1109 Key *private;
1110 char prompt[300], *passphrase;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001111 int r, perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001112 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001113
Ben Lindstrom329782e2001-03-10 17:08:59 +00001114 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001115 (userprovided ? logit : debug3)("no such identity: %s: %s",
1116 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001117 return NULL;
1118 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001119 snprintf(prompt, sizeof prompt,
1120 "Enter passphrase for key '%.100s': ", filename);
1121 for (i = 0; i <= options.number_of_password_prompts; i++) {
1122 if (i == 0)
1123 passphrase = "";
1124 else {
Damien Miller62cee002000-09-23 17:15:56 +11001125 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001126 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001127 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001128 free(passphrase);
1129 break;
Damien Miller62cee002000-09-23 17:15:56 +11001130 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001131 }
1132 switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename,
1133 passphrase, &private, NULL, &perm_ok))) {
1134 case 0:
1135 break;
1136 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1137 if (options.batch_mode) {
1138 quit = 1;
1139 break;
1140 }
1141 debug2("bad passphrase given, try again...");
1142 break;
1143 case SSH_ERR_SYSTEM_ERROR:
1144 if (errno == ENOENT) {
1145 debug2("Load key \"%s\": %s",
1146 filename, ssh_err(r));
1147 quit = 1;
1148 break;
1149 }
1150 /* FALLTHROUGH */
1151 default:
1152 error("Load key \"%s\": %s", filename, ssh_err(r));
1153 quit = 1;
1154 break;
1155 }
1156 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001157 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001158 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001159 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001160 if (private != NULL || quit)
1161 break;
Damien Miller994cf142000-07-21 10:19:44 +10001162 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001163 return private;
1164}
1165
Damien Miller280ecfb2003-05-14 13:46:00 +10001166/*
1167 * try keys in the following order:
1168 * 1. agent keys that are found in the config file
1169 * 2. other agent keys
1170 * 3. keys that are only listed in the config file
1171 */
1172static void
1173pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001174{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001175 struct identity *id, *id2, *tmp;
1176 struct idlist agent, files, *preferred;
1177 struct sshkey *key;
1178 int agent_fd, i, r, found;
1179 size_t j;
1180 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001181
Damien Miller280ecfb2003-05-14 13:46:00 +10001182 TAILQ_INIT(&agent); /* keys from the agent */
1183 TAILQ_INIT(&files); /* keys from the config file */
1184 preferred = &authctxt->keys;
1185 TAILQ_INIT(preferred); /* preferred order of keys */
1186
Damien Millercb6b68b2012-12-03 09:49:52 +11001187 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001188 for (i = 0; i < options.num_identity_files; i++) {
1189 key = options.identity_keys[i];
1190 if (key && key->type == KEY_RSA1)
1191 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001192 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1193 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001194 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001195 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001196 id->key = key;
1197 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001198 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001199 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001200 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001201 /* Prefer PKCS11 keys that are explicitly listed */
1202 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
Damien Miller86687062014-07-02 15:28:02 +10001203 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001204 continue;
1205 found = 0;
1206 TAILQ_FOREACH(id2, &files, next) {
1207 if (id2->key == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001208 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001209 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001210 if (sshkey_equal(id->key, id2->key)) {
Damien Millercb6b68b2012-12-03 09:49:52 +11001211 TAILQ_REMOVE(&files, id, next);
1212 TAILQ_INSERT_TAIL(preferred, id, next);
1213 found = 1;
1214 break;
1215 }
1216 }
1217 /* If IdentitiesOnly set and key not found then don't use it */
1218 if (!found && options.identities_only) {
1219 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001220 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001221 free(id);
1222 }
1223 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001224 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001225 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1226 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1227 debug("%s: ssh_get_authentication_socket: %s",
1228 __func__, ssh_err(r));
1229 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1230 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1231 debug("%s: ssh_fetch_identitylist: %s",
1232 __func__, ssh_err(r));
1233 } else {
1234 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001235 found = 0;
1236 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001237 /*
1238 * agent keys from the config file are
1239 * preferred
1240 */
1241 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001242 TAILQ_REMOVE(&files, id, next);
1243 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001244 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001245 found = 1;
1246 break;
1247 }
1248 }
Damien Millerbd394c32004-03-08 23:12:36 +11001249 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001250 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001251 /* XXX "steals" key/comment from idlist */
1252 id->key = idlist->keys[j];
1253 id->filename = idlist->comments[j];
1254 idlist->keys[j] = NULL;
1255 idlist->comments[j] = NULL;
1256 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001257 TAILQ_INSERT_TAIL(&agent, id, next);
1258 }
1259 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001260 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001261 /* append remaining agent keys */
1262 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1263 TAILQ_REMOVE(&agent, id, next);
1264 TAILQ_INSERT_TAIL(preferred, id, next);
1265 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001266 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001267 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001268 /* append remaining keys from the config file */
1269 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1270 TAILQ_REMOVE(&files, id, next);
1271 TAILQ_INSERT_TAIL(preferred, id, next);
1272 }
1273 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001274 debug2("key: %s (%p),%s", id->filename, id->key,
1275 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001276 }
1277}
1278
1279static void
1280pubkey_cleanup(Authctxt *authctxt)
1281{
1282 Identity *id;
1283
djm@openbsd.org141efe42015-01-14 20:05:27 +00001284 if (authctxt->agent_fd != -1)
1285 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001286 for (id = TAILQ_FIRST(&authctxt->keys); id;
1287 id = TAILQ_FIRST(&authctxt->keys)) {
1288 TAILQ_REMOVE(&authctxt->keys, id, next);
1289 if (id->key)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001290 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001291 free(id->filename);
1292 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001293 }
Damien Millereba71ba2000-04-29 23:57:08 +10001294}
1295
Damien Miller62cee002000-09-23 17:15:56 +11001296int
1297userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001298{
Damien Miller280ecfb2003-05-14 13:46:00 +10001299 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001300 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001301
Damien Miller280ecfb2003-05-14 13:46:00 +10001302 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1303 if (id->tried++)
1304 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001305 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001306 TAILQ_REMOVE(&authctxt->keys, id, next);
1307 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1308 /*
1309 * send a test message if we have the public key. for
1310 * encrypted keys we cannot do this and have to load the
1311 * private key instead
1312 */
Damien Miller324541e2013-12-31 12:25:40 +11001313 if (id->key != NULL) {
1314 if (key_type_plain(id->key->type) == KEY_RSA &&
1315 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1316 debug("Skipped %s key %s for RSA/MD5 server",
1317 key_type(id->key), id->filename);
1318 } else if (id->key->type != KEY_RSA1) {
1319 debug("Offering %s public key: %s",
1320 key_type(id->key), id->filename);
1321 sent = send_pubkey_test(authctxt, id);
1322 }
1323 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001324 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001325 id->key = load_identity_file(id->filename,
1326 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001327 if (id->key != NULL) {
1328 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001329 if (key_type_plain(id->key->type) == KEY_RSA &&
1330 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1331 debug("Skipped %s key %s for RSA/MD5 "
1332 "server", key_type(id->key),
1333 id->filename);
1334 } else {
1335 sent = sign_and_send_pubkey(
1336 authctxt, id);
1337 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001338 key_free(id->key);
1339 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001340 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001341 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001342 if (sent)
1343 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001344 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001345 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001346}
Damien Millereba71ba2000-04-29 23:57:08 +10001347
Damien Miller874d77b2000-10-14 16:23:11 +11001348/*
1349 * Send userauth request message specifying keyboard-interactive method.
1350 */
1351int
1352userauth_kbdint(Authctxt *authctxt)
1353{
1354 static int attempt = 0;
1355
1356 if (attempt++ >= options.number_of_password_prompts)
1357 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001358 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1359 if (attempt > 1 && !authctxt->info_req_seen) {
1360 debug3("userauth_kbdint: disable: no info_req_seen");
1361 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1362 return 0;
1363 }
Damien Miller874d77b2000-10-14 16:23:11 +11001364
1365 debug2("userauth_kbdint");
1366 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1367 packet_put_cstring(authctxt->server_user);
1368 packet_put_cstring(authctxt->service);
1369 packet_put_cstring(authctxt->method->name);
1370 packet_put_cstring(""); /* lang */
1371 packet_put_cstring(options.kbd_interactive_devices ?
1372 options.kbd_interactive_devices : "");
1373 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001374
1375 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1376 return 1;
1377}
1378
1379/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001380 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001381 */
1382void
Damien Miller630d6f42002-01-22 23:17:30 +11001383input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001384{
1385 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001386 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001387 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001388 int echo = 0;
1389
1390 debug2("input_userauth_info_req");
1391
1392 if (authctxt == NULL)
1393 fatal("input_userauth_info_req: no authentication context");
1394
Ben Lindstrom7d199962001-09-12 18:29:00 +00001395 authctxt->info_req_seen = 1;
1396
Damien Miller874d77b2000-10-14 16:23:11 +11001397 name = packet_get_string(NULL);
1398 inst = packet_get_string(NULL);
1399 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001400 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001401 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001402 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001403 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001404 free(name);
1405 free(inst);
1406 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001407
1408 num_prompts = packet_get_int();
1409 /*
1410 * Begin to build info response packet based on prompts requested.
1411 * We commit to providing the correct number of responses, so if
1412 * further on we run into a problem that prevents this, we have to
1413 * be sure and clean this up and send a correct error response.
1414 */
1415 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1416 packet_put_int(num_prompts);
1417
Ben Lindstrom551ea372001-06-05 18:56:16 +00001418 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001419 for (i = 0; i < num_prompts; i++) {
1420 prompt = packet_get_string(NULL);
1421 echo = packet_get_char();
1422
Ben Lindstrom949974b2001-06-25 05:20:31 +00001423 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001424
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001425 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001426 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001427 free(response);
1428 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001429 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001430 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001431
Damien Miller9f643902001-11-12 11:02:52 +11001432 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001433 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001434}
Damien Miller62cee002000-09-23 17:15:56 +11001435
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001436static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001437ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001438 u_char *data, u_int datalen)
1439{
1440 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001441 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001442 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001443 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001444
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001445 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001446
Ben Lindstrom5206b952002-06-06 19:59:29 +00001447 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001448 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001449 return -1;
1450 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001451 if (fflush(stdout) != 0)
1452 error("ssh_keysign: fflush: %s", strerror(errno));
1453 if (pipe(to) < 0) {
1454 error("ssh_keysign: pipe: %s", strerror(errno));
1455 return -1;
1456 }
1457 if (pipe(from) < 0) {
1458 error("ssh_keysign: pipe: %s", strerror(errno));
1459 return -1;
1460 }
1461 if ((pid = fork()) < 0) {
1462 error("ssh_keysign: fork: %s", strerror(errno));
1463 return -1;
1464 }
1465 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001466 /* keep the socket on exec */
1467 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001468 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001469 close(from[0]);
1470 if (dup2(from[1], STDOUT_FILENO) < 0)
1471 fatal("ssh_keysign: dup2: %s", strerror(errno));
1472 close(to[1]);
1473 if (dup2(to[0], STDIN_FILENO) < 0)
1474 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001475 close(from[1]);
1476 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001477 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001478 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1479 strerror(errno));
1480 }
1481 close(from[1]);
1482 close(to[0]);
1483
1484 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001485 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001486 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001487 if (ssh_msg_send(to[1], version, &b) == -1)
1488 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001489
Damien Miller901119b2002-10-04 11:10:04 +10001490 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001491 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001492 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001493 return -1;
1494 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001495 close(from[0]);
1496 close(to[1]);
1497
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001498 while (waitpid(pid, &status, 0) < 0)
1499 if (errno != EINTR)
1500 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001501
Ben Lindstrom5206b952002-06-06 19:59:29 +00001502 if (buffer_get_char(&b) != version) {
1503 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001504 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001505 return -1;
1506 }
1507 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001508 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001509
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001510 return 0;
1511}
1512
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001513int
1514userauth_hostbased(Authctxt *authctxt)
1515{
1516 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001517 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001518 Buffer b;
1519 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001520 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001521 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001522 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001523 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001524
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001525 /* XXX provide some way to allow user to specify key types attempted */
1526
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001527 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001528 for (i = 0; i < sensitive->nkeys; i++) {
1529 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001530 if (private && private->type != KEY_RSA1) {
1531 found = 1;
1532 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001533 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001534 break;
1535 }
1536 }
1537 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001538 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001539 return 0;
1540 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001541
1542 debug("%s: trying hostkey type %s", __func__, key_type(private));
1543
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001544 if (key_to_blob(private, &blob, &blen) == 0) {
1545 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001546 return 0;
1547 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001548
Damien Miller91c18472001-11-12 11:02:03 +11001549 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001550 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001551 if (p == NULL) {
1552 error("userauth_hostbased: cannot get local ipaddr/name");
1553 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001554 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001555 return 0;
1556 }
Damien Miller07d86be2006-03-26 14:19:21 +11001557 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001558 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001559 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001560
Ben Lindstrom671388f2001-04-19 20:40:45 +00001561 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1562 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001563 pkalg = xstrdup(key_ssh_name(private));
1564 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001565 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001566 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001567 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1568 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001569 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001570 buffer_put_cstring(&b, authctxt->method->name);
1571 buffer_put_cstring(&b, pkalg);
1572 buffer_put_string(&b, blob, blen);
1573 buffer_put_cstring(&b, chost);
1574 buffer_put_cstring(&b, authctxt->local_user);
1575#ifdef DEBUG_PK
1576 buffer_dump(&b);
1577#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001578 if (sensitive->external_keysign)
1579 ok = ssh_keysign(private, &signature, &slen,
1580 buffer_ptr(&b), buffer_len(&b));
1581 else
1582 ok = key_sign(private, &signature, &slen,
1583 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001584 key_free(private);
1585 buffer_free(&b);
1586 if (ok != 0) {
1587 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001588 free(chost);
1589 free(pkalg);
1590 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001591 return 0;
1592 }
1593 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1594 packet_put_cstring(authctxt->server_user);
1595 packet_put_cstring(authctxt->service);
1596 packet_put_cstring(authctxt->method->name);
1597 packet_put_cstring(pkalg);
1598 packet_put_string(blob, blen);
1599 packet_put_cstring(chost);
1600 packet_put_cstring(authctxt->local_user);
1601 packet_put_string(signature, slen);
Damien Millera5103f42014-02-04 11:20:14 +11001602 explicit_bzero(signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001603 free(signature);
1604 free(chost);
1605 free(pkalg);
1606 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001607
1608 packet_send();
1609 return 1;
1610}
1611
Damien Miller62cee002000-09-23 17:15:56 +11001612/* find auth method */
1613
Damien Miller62cee002000-09-23 17:15:56 +11001614/*
1615 * given auth method name, if configurable options permit this method fill
1616 * in auth_ident field and return true, otherwise return false.
1617 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001618static int
Damien Miller62cee002000-09-23 17:15:56 +11001619authmethod_is_enabled(Authmethod *method)
1620{
1621 if (method == NULL)
1622 return 0;
1623 /* return false if options indicate this method is disabled */
1624 if (method->enabled == NULL || *method->enabled == 0)
1625 return 0;
1626 /* return false if batch mode is enabled but method needs interactive mode */
1627 if (method->batch_flag != NULL && *method->batch_flag != 0)
1628 return 0;
1629 return 1;
1630}
1631
Ben Lindstrombba81212001-06-25 05:01:22 +00001632static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001633authmethod_lookup(const char *name)
1634{
1635 Authmethod *method = NULL;
1636 if (name != NULL)
1637 for (method = authmethods; method->name != NULL; method++)
1638 if (strcmp(name, method->name) == 0)
1639 return method;
1640 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1641 return NULL;
1642}
1643
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001644/* XXX internal state */
1645static Authmethod *current = NULL;
1646static char *supported = NULL;
1647static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001648
Damien Miller62cee002000-09-23 17:15:56 +11001649/*
1650 * Given the authentication method list sent by the server, return the
1651 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001652 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001653 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001654static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001655authmethod_get(char *authlist)
1656{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001657 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001658 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001659
Damien Miller62cee002000-09-23 17:15:56 +11001660 /* Use a suitable default if we're passed a nil list. */
1661 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001662 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001663
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001664 if (supported == NULL || strcmp(authlist, supported) != 0) {
1665 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001666 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001667 supported = xstrdup(authlist);
1668 preferred = options.preferred_authentications;
1669 debug3("preferred %s", preferred);
1670 current = NULL;
1671 } else if (current != NULL && authmethod_is_enabled(current))
1672 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001673
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001674 for (;;) {
1675 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001676 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001677 current = NULL;
1678 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001679 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001680 preferred += next;
1681 debug3("authmethod_lookup %s", name);
1682 debug3("remaining preferred: %s", preferred);
1683 if ((current = authmethod_lookup(name)) != NULL &&
1684 authmethod_is_enabled(current)) {
1685 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001686 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001687 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001688 return current;
1689 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001690 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001691 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001692}
Damien Miller62cee002000-09-23 17:15:56 +11001693
Ben Lindstrom79073822001-07-04 03:42:30 +00001694static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001695authmethods_get(void)
1696{
1697 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001698 Buffer b;
1699 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001700
Damien Miller0e3b8722002-01-22 23:26:38 +11001701 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001702 for (method = authmethods; method->name != NULL; method++) {
1703 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001704 if (buffer_len(&b) > 0)
1705 buffer_append(&b, ",", 1);
1706 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001707 }
Damien Miller62cee002000-09-23 17:15:56 +11001708 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001709 buffer_append(&b, "\0", 1);
1710 list = xstrdup(buffer_ptr(&b));
1711 buffer_free(&b);
1712 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001713}
Damien Miller01ed2272008-11-05 16:20:46 +11001714