blob: 937bb773dd647610b7d8a140d05ce5b08b628b95 [file] [log] [blame]
Darren Tucker36331b52010-01-08 16:50:41 +11001/* $OpenBSD: sshconnect2.c,v 1.173 2009/10/24 11:13:54 andreas 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 Tuckerf520ea12007-05-20 15:11:33 +100035#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100036#include <pwd.h>
37#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100038#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100041#include <unistd.h>
Damien Miller2e28d862008-07-17 19:15:43 +100042#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
Damien Miller7ba0ca72008-07-17 18:57:06 +100043#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100044#endif
Darren Tucker39972492006-07-12 22:22:46 +100045
Damien Miller9c617692003-05-14 14:31:11 +100046#include "openbsd-compat/sys-queue.h"
47
Damien Millerd7834352006-08-05 12:39:39 +100048#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100051#include "buffer.h"
52#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100056#include "kex.h"
57#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100058#include "sshconnect.h"
59#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000060#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100061#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
63#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100064#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000065#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000066#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000067#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000068#include "msg.h"
69#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100070#include "uidswap.h"
Damien Millercee85232009-03-06 00:58:22 +110071#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110072#include "jpake.h"
Damien Miller874d77b2000-10-14 16:23:11 +110073
Darren Tucker0efd1552003-08-26 11:49:55 +100074#ifdef GSSAPI
75#include "ssh-gss.h"
76#endif
77
Damien Millereba71ba2000-04-29 23:57:08 +100078/* import */
79extern char *client_version_string;
80extern char *server_version_string;
81extern Options options;
82
83/*
84 * SSH2 key exchange
85 */
86
Ben Lindstrom46c16222000-12-22 01:43:59 +000087u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100088u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100089
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000090char *xxx_host;
91struct sockaddr *xxx_hostaddr;
92
Ben Lindstromf28f6342001-04-04 02:03:04 +000093Kex *xxx_kex = NULL;
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000096verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000097{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100099 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100 return 0;
101}
102
Damien Millereba71ba2000-04-29 23:57:08 +1000103void
Damien Miller874d77b2000-10-14 16:23:11 +1100104ssh_kex2(char *host, struct sockaddr *hostaddr)
105{
Damien Miller874d77b2000-10-14 16:23:11 +1100106 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000107
108 xxx_host = host;
109 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100110
Damien Millere39cacc2000-11-29 12:18:44 +1100111 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000112 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100113 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100114 }
115 if (options.ciphers != NULL) {
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
118 }
Damien Millera0ff4662001-03-30 10:49:35 +1000119 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
121 myproposal[PROPOSAL_ENC_ALGS_STOC] =
122 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100123 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100126 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000127 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000128 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100129 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000130 if (options.macs != NULL) {
131 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
132 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
133 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000134 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000136 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100137
Damien Millera5539d22003-04-09 20:50:06 +1000138 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100139 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000140
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000141 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000142 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100143 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000144 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100145 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100146 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000147 kex->client_version_string=client_version_string;
148 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000149 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100150
Ben Lindstromf28f6342001-04-04 02:03:04 +0000151 xxx_kex = kex;
152
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000153 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100154
Darren Tucker36331b52010-01-08 16:50:41 +1100155 if (options.use_roaming && !kex->roaming) {
156 debug("Roaming not allowed by server");
157 options.use_roaming = 0;
158 }
159
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000160 session_id2 = kex->session_id;
161 session_id2_len = kex->session_id_len;
162
Damien Miller874d77b2000-10-14 16:23:11 +1100163#ifdef DEBUG_KEXDH
164 /* send 1st encrypted/maced/compressed message */
165 packet_start(SSH2_MSG_IGNORE);
166 packet_put_cstring("markus");
167 packet_send();
168 packet_write_wait();
169#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000170}
Damien Millerb1715dc2000-05-30 13:44:51 +1000171
Damien Millereba71ba2000-04-29 23:57:08 +1000172/*
173 * Authenticate user
174 */
Damien Miller62cee002000-09-23 17:15:56 +1100175
176typedef struct Authctxt Authctxt;
177typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000178typedef struct identity Identity;
179typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100180
Damien Miller280ecfb2003-05-14 13:46:00 +1000181struct identity {
182 TAILQ_ENTRY(identity) next;
183 AuthenticationConnection *ac; /* set if agent supports key */
184 Key *key; /* public/private key */
185 char *filename; /* comment for agent-only keys */
186 int tried;
187 int isprivate; /* key points to the private key */
188};
189TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100190
191struct Authctxt {
192 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000193 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100194 const char *host;
195 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100196 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100197 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000198 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000199 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000200 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000201 AuthenticationConnection *agent;
202 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000203 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000204 /* kbd-interactive */
205 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000206 /* generic */
207 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100208};
209struct Authmethod {
210 char *name; /* string to compare against server's list */
211 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100212 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100213 int *enabled; /* flag in option struct that enables method */
214 int *batch_flag; /* flag in option struct that disables method */
215};
216
Damien Miller630d6f42002-01-22 23:17:30 +1100217void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100218void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100219void input_userauth_failure(int, u_int32_t, void *);
220void input_userauth_banner(int, u_int32_t, void *);
221void input_userauth_error(int, u_int32_t, void *);
222void input_userauth_info_req(int, u_int32_t, void *);
223void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000224void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100225void input_userauth_jpake_server_step1(int, u_int32_t, void *);
226void input_userauth_jpake_server_step2(int, u_int32_t, void *);
227void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100228
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000229int userauth_none(Authctxt *);
230int userauth_pubkey(Authctxt *);
231int userauth_passwd(Authctxt *);
232int userauth_kbdint(Authctxt *);
233int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100234int userauth_jpake(Authctxt *);
235
236void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100237
Darren Tucker0efd1552003-08-26 11:49:55 +1000238#ifdef GSSAPI
239int userauth_gssapi(Authctxt *authctxt);
240void input_gssapi_response(int type, u_int32_t, void *);
241void input_gssapi_token(int type, u_int32_t, void *);
242void input_gssapi_hash(int type, u_int32_t, void *);
243void input_gssapi_error(int, u_int32_t, void *);
244void input_gssapi_errtok(int, u_int32_t, void *);
245#endif
246
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000247void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000248
Damien Miller280ecfb2003-05-14 13:46:00 +1000249static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000250static void pubkey_prepare(Authctxt *);
251static void pubkey_cleanup(Authctxt *);
252static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000253
Ben Lindstrombba81212001-06-25 05:01:22 +0000254static Authmethod *authmethod_get(char *authlist);
255static Authmethod *authmethod_lookup(const char *name);
256static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100257
258Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000259#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100260 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000261 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100262 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000263 &options.gss_authentication,
264 NULL},
265#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000266 {"hostbased",
267 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100268 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000269 &options.hostbased_authentication,
270 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000271 {"publickey",
272 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100273 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000274 &options.pubkey_authentication,
275 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100276#ifdef JPAKE
277 {"jpake-01@openssh.com",
278 userauth_jpake,
279 userauth_jpake_cleanup,
280 &options.zero_knowledge_password_authentication,
281 &options.batch_mode},
282#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100283 {"keyboard-interactive",
284 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100285 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100286 &options.kbd_interactive_authentication,
287 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000288 {"password",
289 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100290 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000291 &options.password_authentication,
292 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100293 {"none",
294 userauth_none,
295 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100296 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100297 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100298 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100299};
300
301void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000302ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000303 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100304{
305 Authctxt authctxt;
306 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100307
Ben Lindstrom551ea372001-06-05 18:56:16 +0000308 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000309 options.kbd_interactive_authentication = 1;
310
Damien Miller62cee002000-09-23 17:15:56 +1100311 packet_start(SSH2_MSG_SERVICE_REQUEST);
312 packet_put_cstring("ssh-userauth");
313 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000314 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100315 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100316 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000317 if (type != SSH2_MSG_SERVICE_ACCEPT)
318 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100319 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100320 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000321 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100322 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100323 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000324 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100325 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100326 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000327 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100328
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000329 if (options.preferred_authentications == NULL)
330 options.preferred_authentications = authmethods_get();
331
Damien Miller62cee002000-09-23 17:15:56 +1100332 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000333 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000334 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100335 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000336 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100337 authctxt.host = host;
338 authctxt.service = "ssh-connection"; /* service name */
339 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100340 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000341 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000342 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000343 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000344 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100345 if (authctxt.method == NULL)
346 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100347
348 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100349 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100350
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000351 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100352 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
353 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000354 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100355 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
356
Damien Miller280ecfb2003-05-14 13:46:00 +1000357 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000358 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
359
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000360 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100361}
Damien Millerf842fcb2003-05-15 12:01:28 +1000362
Damien Miller62cee002000-09-23 17:15:56 +1100363void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000364userauth(Authctxt *authctxt, char *authlist)
365{
Damien Miller01ed2272008-11-05 16:20:46 +1100366 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
367 authctxt->method->cleanup(authctxt);
368
Darren Tucker0efd1552003-08-26 11:49:55 +1000369 if (authctxt->methoddata) {
370 xfree(authctxt->methoddata);
371 authctxt->methoddata = NULL;
372 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000373 if (authlist == NULL) {
374 authlist = authctxt->authlist;
375 } else {
376 if (authctxt->authlist)
377 xfree(authctxt->authlist);
378 authctxt->authlist = authlist;
379 }
380 for (;;) {
381 Authmethod *method = authmethod_get(authlist);
382 if (method == NULL)
383 fatal("Permission denied (%s).", authlist);
384 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000385
386 /* reset the per method handler */
387 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
388 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
389
390 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000391 if (method->userauth(authctxt) != 0) {
392 debug2("we sent a %s packet, wait for reply", method->name);
393 break;
394 } else {
395 debug2("we did not send a packet, disable method");
396 method->enabled = NULL;
397 }
398 }
399}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000400
Damien Millerf7475d72008-11-03 19:26:18 +1100401/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000402void
Damien Miller630d6f42002-01-22 23:17:30 +1100403input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100404{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000405 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000406 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000407}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000408
Damien Millerf7475d72008-11-03 19:26:18 +1100409/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000410void
Damien Miller630d6f42002-01-22 23:17:30 +1100411input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000412{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000413 char *msg, *raw, *lang;
414 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000415
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000416 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000417 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000418 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100419 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000420 if (len > 65536)
421 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100422 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
423 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
Darren Tucker046dff22003-10-08 17:32:02 +1000424 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000425 xfree(msg);
426 }
427 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000428 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100429}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000430
Damien Millerf7475d72008-11-03 19:26:18 +1100431/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100432void
Damien Miller630d6f42002-01-22 23:17:30 +1100433input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100434{
435 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100436
Damien Miller62cee002000-09-23 17:15:56 +1100437 if (authctxt == NULL)
438 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000439 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000440 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000441 authctxt->authlist = NULL;
442 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100443 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
444 authctxt->method->cleanup(authctxt);
Darren Tucker79644822003-10-08 17:37:58 +1000445 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000446 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000447 authctxt->methoddata = NULL;
448 }
Damien Miller62cee002000-09-23 17:15:56 +1100449 authctxt->success = 1; /* break out */
450}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000451
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100452void
453input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
454{
455 Authctxt *authctxt = ctxt;
456
457 if (authctxt == NULL)
458 fatal("%s: no authentication context", __func__);
459
460 fatal("Unexpected authentication success during %s.",
461 authctxt->method->name);
462}
463
Damien Millerf7475d72008-11-03 19:26:18 +1100464/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100465void
Damien Miller630d6f42002-01-22 23:17:30 +1100466input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100467{
Damien Miller62cee002000-09-23 17:15:56 +1100468 Authctxt *authctxt = ctxt;
469 char *authlist = NULL;
470 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100471
472 if (authctxt == NULL)
473 fatal("input_userauth_failure: no authentication context");
474
Damien Miller874d77b2000-10-14 16:23:11 +1100475 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100476 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100477 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100478
479 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000480 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000481 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100482
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 userauth(authctxt, authlist);
484}
Damien Millerf7475d72008-11-03 19:26:18 +1100485
486/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000487void
Damien Miller630d6f42002-01-22 23:17:30 +1100488input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000489{
490 Authctxt *authctxt = ctxt;
491 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000492 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000493 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000494 int pktype, sent = 0;
495 u_int alen, blen;
496 char *pkalg, *fp;
497 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000498
499 if (authctxt == NULL)
500 fatal("input_userauth_pk_ok: no authentication context");
501 if (datafellows & SSH_BUG_PKOK) {
502 /* this is similar to SSH_BUG_PKAUTH */
503 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
504 pkblob = packet_get_string(&blen);
505 buffer_init(&b);
506 buffer_append(&b, pkblob, blen);
507 pkalg = buffer_get_string(&b, &alen);
508 buffer_free(&b);
509 } else {
510 pkalg = packet_get_string(&alen);
511 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000512 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100513 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000514
Damien Miller280ecfb2003-05-14 13:46:00 +1000515 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000516
Damien Miller280ecfb2003-05-14 13:46:00 +1000517 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
518 debug("unknown pkalg %s", pkalg);
519 goto done;
520 }
521 if ((key = key_from_blob(pkblob, blen)) == NULL) {
522 debug("no key from blob. pkalg %s", pkalg);
523 goto done;
524 }
525 if (key->type != pktype) {
526 error("input_userauth_pk_ok: type mismatch "
527 "for decoded key (received %d, expected %d)",
528 key->type, pktype);
529 goto done;
530 }
531 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
532 debug2("input_userauth_pk_ok: fp %s", fp);
533 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000534
Darren Tuckerd05b6012003-10-15 15:55:59 +1000535 /*
536 * search keys in the reverse order, because last candidate has been
537 * moved to the end of the queue. this also avoids confusion by
538 * duplicate keys
539 */
Damien Miller0b51a522004-04-20 20:07:19 +1000540 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000541 if (key_equal(key, id->key)) {
542 sent = sign_and_send_pubkey(authctxt, id);
543 break;
544 }
545 }
546done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000547 if (key != NULL)
548 key_free(key);
549 xfree(pkalg);
550 xfree(pkblob);
551
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000552 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000553 if (sent == 0)
554 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100555}
556
Darren Tucker0efd1552003-08-26 11:49:55 +1000557#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100558int
Darren Tucker0efd1552003-08-26 11:49:55 +1000559userauth_gssapi(Authctxt *authctxt)
560{
561 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100562 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000563 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000564 OM_uint32 min;
565 int ok = 0;
566
567 /* Try one GSSAPI method at a time, rather than sending them all at
568 * once. */
569
Darren Tucker56afe142003-11-03 20:06:14 +1100570 if (gss_supported == NULL)
571 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000572
573 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100574 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000575 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100576 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000577 ssh_gssapi_check_mechanism(&gssctxt,
578 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000579 ok = 1; /* Mechanism works */
580 } else {
581 mech++;
582 }
583 }
584
Damien Millera1cb9f32006-08-19 00:33:34 +1000585 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000586 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000587
588 authctxt->methoddata=(void *)gssctxt;
589
590 packet_start(SSH2_MSG_USERAUTH_REQUEST);
591 packet_put_cstring(authctxt->server_user);
592 packet_put_cstring(authctxt->service);
593 packet_put_cstring(authctxt->method->name);
594
595 packet_put_int(1);
596
Darren Tucker655a5e02003-11-03 20:09:03 +1100597 packet_put_int((gss_supported->elements[mech].length) + 2);
598 packet_put_char(SSH_GSS_OIDTYPE);
599 packet_put_char(gss_supported->elements[mech].length);
600 packet_put_raw(gss_supported->elements[mech].elements,
601 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000602
603 packet_send();
604
605 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
606 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
607 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
608 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
609
610 mech++; /* Move along to next candidate */
611
612 return 1;
613}
614
Damien Miller91c6aa42003-11-17 21:20:18 +1100615static OM_uint32
616process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
617{
618 Authctxt *authctxt = ctxt;
619 Gssctxt *gssctxt = authctxt->methoddata;
620 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000621 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
622 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100623 OM_uint32 status, ms, flags;
624 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100625
Damien Miller91c6aa42003-11-17 21:20:18 +1100626 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100627 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100628
629 if (send_tok.length > 0) {
630 if (GSS_ERROR(status))
631 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
632 else
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100634
Damien Miller91c6aa42003-11-17 21:20:18 +1100635 packet_put_string(send_tok.value, send_tok.length);
636 packet_send();
637 gss_release_buffer(&ms, &send_tok);
638 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100639
Damien Miller91c6aa42003-11-17 21:20:18 +1100640 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100641 /* send either complete or MIC, depending on mechanism */
642 if (!(flags & GSS_C_INTEG_FLAG)) {
643 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
644 packet_send();
645 } else {
646 ssh_gssapi_buildmic(&b, authctxt->server_user,
647 authctxt->service, "gssapi-with-mic");
648
649 gssbuf.value = buffer_ptr(&b);
650 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100651
Damien Miller0425d402003-11-17 22:18:21 +1100652 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100653
Damien Miller0425d402003-11-17 22:18:21 +1100654 if (!GSS_ERROR(status)) {
655 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
656 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100657
Damien Miller0425d402003-11-17 22:18:21 +1100658 packet_send();
659 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100660
Damien Miller0425d402003-11-17 22:18:21 +1100661 buffer_free(&b);
662 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100663 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100664 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100665
Damien Miller91c6aa42003-11-17 21:20:18 +1100666 return status;
667}
668
Damien Millerf7475d72008-11-03 19:26:18 +1100669/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000670void
671input_gssapi_response(int type, u_int32_t plen, void *ctxt)
672{
673 Authctxt *authctxt = ctxt;
674 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000675 int oidlen;
676 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000677
678 if (authctxt == NULL)
679 fatal("input_gssapi_response: no authentication context");
680 gssctxt = authctxt->methoddata;
681
682 /* Setup our OID */
683 oidv = packet_get_string(&oidlen);
684
Darren Tucker655a5e02003-11-03 20:09:03 +1100685 if (oidlen <= 2 ||
686 oidv[0] != SSH_GSS_OIDTYPE ||
687 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100688 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100689 debug("Badly encoded mechanism OID received");
690 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100691 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000692 }
693
Darren Tucker655a5e02003-11-03 20:09:03 +1100694 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
695 fatal("Server returned different OID than expected");
696
Darren Tucker0efd1552003-08-26 11:49:55 +1000697 packet_check_eom();
698
699 xfree(oidv);
700
Damien Miller91c6aa42003-11-17 21:20:18 +1100701 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000702 /* Start again with next method on list */
703 debug("Trying to start again");
704 userauth(authctxt, NULL);
705 return;
706 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000707}
708
Damien Millerf7475d72008-11-03 19:26:18 +1100709/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000710void
711input_gssapi_token(int type, u_int32_t plen, void *ctxt)
712{
713 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000714 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100715 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000716 u_int slen;
717
718 if (authctxt == NULL)
719 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000720
721 recv_tok.value = packet_get_string(&slen);
722 recv_tok.length = slen; /* safe typecast */
723
724 packet_check_eom();
725
Damien Miller91c6aa42003-11-17 21:20:18 +1100726 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000727
728 xfree(recv_tok.value);
729
730 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000731 /* Start again with the next method in the list */
732 userauth(authctxt, NULL);
733 return;
734 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000735}
736
Damien Millerf7475d72008-11-03 19:26:18 +1100737/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000738void
739input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
740{
741 Authctxt *authctxt = ctxt;
742 Gssctxt *gssctxt;
743 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
744 gss_buffer_desc recv_tok;
745 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000746 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000747
748 if (authctxt == NULL)
749 fatal("input_gssapi_response: no authentication context");
750 gssctxt = authctxt->methoddata;
751
Darren Tucker600ad8d2003-08-26 12:10:48 +1000752 recv_tok.value = packet_get_string(&len);
753 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000754
755 packet_check_eom();
756
757 /* Stick it into GSSAPI and see what it says */
758 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000759 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000760
761 xfree(recv_tok.value);
762 gss_release_buffer(&ms, &send_tok);
763
764 /* Server will be returning a failed packet after this one */
765}
766
Damien Millerf7475d72008-11-03 19:26:18 +1100767/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000768void
769input_gssapi_error(int type, u_int32_t plen, void *ctxt)
770{
771 OM_uint32 maj, min;
772 char *msg;
773 char *lang;
774
775 maj=packet_get_int();
776 min=packet_get_int();
777 msg=packet_get_string(NULL);
778 lang=packet_get_string(NULL);
779
780 packet_check_eom();
781
Damien Miller15d72a02005-11-05 15:07:33 +1100782 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000783 xfree(msg);
784 xfree(lang);
785}
786#endif /* GSSAPI */
787
Damien Millereba71ba2000-04-29 23:57:08 +1000788int
Damien Miller874d77b2000-10-14 16:23:11 +1100789userauth_none(Authctxt *authctxt)
790{
791 /* initial userauth request */
792 packet_start(SSH2_MSG_USERAUTH_REQUEST);
793 packet_put_cstring(authctxt->server_user);
794 packet_put_cstring(authctxt->service);
795 packet_put_cstring(authctxt->method->name);
796 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100797 return 1;
798}
799
800int
Damien Miller62cee002000-09-23 17:15:56 +1100801userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000802{
Damien Millere247cc42000-05-07 12:03:14 +1000803 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000804 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000805 char *password;
806
Damien Millerd3a18572000-06-07 19:55:44 +1000807 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000808 return 0;
809
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000810 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000811 error("Permission denied, please try again.");
812
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000813 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100814 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000815 password = read_passphrase(prompt, 0);
816 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100817 packet_put_cstring(authctxt->server_user);
818 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100819 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000820 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000821 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000822 memset(password, 0, strlen(password));
823 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100824 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000825 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000826
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000827 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000828 &input_userauth_passwd_changereq);
829
Damien Millereba71ba2000-04-29 23:57:08 +1000830 return 1;
831}
Damien Millerf7475d72008-11-03 19:26:18 +1100832
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000833/*
834 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
835 */
Damien Millerf7475d72008-11-03 19:26:18 +1100836/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000837void
Tim Ricec8549622002-03-31 12:49:38 -0800838input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000839{
840 Authctxt *authctxt = ctxt;
841 char *info, *lang, *password = NULL, *retype = NULL;
842 char prompt[150];
843
844 debug2("input_userauth_passwd_changereq");
845
846 if (authctxt == NULL)
847 fatal("input_userauth_passwd_changereq: "
848 "no authentication context");
849
850 info = packet_get_string(NULL);
851 lang = packet_get_string(NULL);
852 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000853 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000854 xfree(info);
855 xfree(lang);
856 packet_start(SSH2_MSG_USERAUTH_REQUEST);
857 packet_put_cstring(authctxt->server_user);
858 packet_put_cstring(authctxt->service);
859 packet_put_cstring(authctxt->method->name);
860 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000861 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000862 "Enter %.30s@%.128s's old password: ",
863 authctxt->server_user, authctxt->host);
864 password = read_passphrase(prompt, 0);
865 packet_put_cstring(password);
866 memset(password, 0, strlen(password));
867 xfree(password);
868 password = NULL;
869 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000870 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000871 "Enter %.30s@%.128s's new password: ",
872 authctxt->server_user, authctxt->host);
873 password = read_passphrase(prompt, RP_ALLOW_EOF);
874 if (password == NULL) {
875 /* bail out */
876 return;
877 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000878 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000879 "Retype %.30s@%.128s's new password: ",
880 authctxt->server_user, authctxt->host);
881 retype = read_passphrase(prompt, 0);
882 if (strcmp(password, retype) != 0) {
883 memset(password, 0, strlen(password));
884 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000885 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000886 password = NULL;
887 }
888 memset(retype, 0, strlen(retype));
889 xfree(retype);
890 }
891 packet_put_cstring(password);
892 memset(password, 0, strlen(password));
893 xfree(password);
894 packet_add_padding(64);
895 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000896
897 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000898 &input_userauth_passwd_changereq);
899}
Damien Millereba71ba2000-04-29 23:57:08 +1000900
Damien Miller01ed2272008-11-05 16:20:46 +1100901#ifdef JPAKE
902static char *
903pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
904{
905 /* OpenBSD crypt(3) handles all of these */
906 if (strcmp(crypt_scheme, "crypt") == 0 ||
907 strcmp(crypt_scheme, "bcrypt") == 0 ||
908 strcmp(crypt_scheme, "md5crypt") == 0 ||
909 strcmp(crypt_scheme, "crypt-extended") == 0)
910 return xstrdup(crypt(password, salt));
911 error("%s: unsupported password encryption scheme \"%.100s\"",
912 __func__, crypt_scheme);
913 return NULL;
914}
915
916static BIGNUM *
917jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
918 const char *salt)
919{
920 char prompt[256], *password, *crypted;
921 u_char *secret;
922 u_int secret_len;
923 BIGNUM *ret;
924
925 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
926 authctxt->server_user, authctxt->host);
927 password = read_passphrase(prompt, 0);
928
929 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
930 logit("Disabling %s authentication", authctxt->method->name);
931 authctxt->method->enabled = NULL;
932 /* Continue with an empty password to fail gracefully */
933 crypted = xstrdup("");
934 }
935
936#ifdef JPAKE_DEBUG
937 debug3("%s: salt = %s", __func__, salt);
938 debug3("%s: scheme = %s", __func__, crypt_scheme);
939 debug3("%s: crypted = %s", __func__, crypted);
940#endif
941
942 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
943 &secret, &secret_len) != 0)
944 fatal("%s: hash_buffer", __func__);
945
946 bzero(password, strlen(password));
947 bzero(crypted, strlen(crypted));
948 xfree(password);
949 xfree(crypted);
950
951 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
952 fatal("%s: BN_bin2bn (secret)", __func__);
953 bzero(secret, secret_len);
954 xfree(secret);
955
956 return ret;
957}
958
959/* ARGSUSED */
960void
961input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
962{
963 Authctxt *authctxt = ctxt;
964 struct jpake_ctx *pctx = authctxt->methoddata;
965 u_char *x3_proof, *x4_proof, *x2_s_proof;
966 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
967 char *crypt_scheme, *salt;
968
969 /* Disable this message */
970 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
971
972 if ((pctx->g_x3 = BN_new()) == NULL ||
973 (pctx->g_x4 = BN_new()) == NULL)
974 fatal("%s: BN_new", __func__);
975
976 /* Fetch step 1 values */
977 crypt_scheme = packet_get_string(NULL);
978 salt = packet_get_string(NULL);
979 pctx->server_id = packet_get_string(&pctx->server_id_len);
980 packet_get_bignum2(pctx->g_x3);
981 packet_get_bignum2(pctx->g_x4);
982 x3_proof = packet_get_string(&x3_proof_len);
983 x4_proof = packet_get_string(&x4_proof_len);
984 packet_check_eom();
985
986 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
987
988 /* Obtain password and derive secret */
989 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
990 bzero(crypt_scheme, strlen(crypt_scheme));
991 bzero(salt, strlen(salt));
992 xfree(crypt_scheme);
993 xfree(salt);
994 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
995
996 /* Calculate step 2 values */
997 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
998 pctx->g_x3, pctx->g_x4, pctx->x2,
999 pctx->server_id, pctx->server_id_len,
1000 pctx->client_id, pctx->client_id_len,
1001 x3_proof, x3_proof_len,
1002 x4_proof, x4_proof_len,
1003 &pctx->a,
1004 &x2_s_proof, &x2_s_proof_len);
1005
1006 bzero(x3_proof, x3_proof_len);
1007 bzero(x4_proof, x4_proof_len);
1008 xfree(x3_proof);
1009 xfree(x4_proof);
1010
1011 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1012
1013 /* Send values for step 2 */
1014 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1015 packet_put_bignum2(pctx->a);
1016 packet_put_string(x2_s_proof, x2_s_proof_len);
1017 packet_send();
1018
1019 bzero(x2_s_proof, x2_s_proof_len);
1020 xfree(x2_s_proof);
1021
1022 /* Expect step 2 packet from peer */
1023 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1024 input_userauth_jpake_server_step2);
1025}
1026
1027/* ARGSUSED */
1028void
1029input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1030{
1031 Authctxt *authctxt = ctxt;
1032 struct jpake_ctx *pctx = authctxt->methoddata;
1033 u_char *x4_s_proof;
1034 u_int x4_s_proof_len;
1035
1036 /* Disable this message */
1037 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1038
1039 if ((pctx->b = BN_new()) == NULL)
1040 fatal("%s: BN_new", __func__);
1041
1042 /* Fetch step 2 values */
1043 packet_get_bignum2(pctx->b);
1044 x4_s_proof = packet_get_string(&x4_s_proof_len);
1045 packet_check_eom();
1046
1047 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1048
1049 /* Derive shared key and calculate confirmation hash */
1050 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1051 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1052 pctx->client_id, pctx->client_id_len,
1053 pctx->server_id, pctx->server_id_len,
1054 session_id2, session_id2_len,
1055 x4_s_proof, x4_s_proof_len,
1056 &pctx->k,
1057 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1058
1059 bzero(x4_s_proof, x4_s_proof_len);
1060 xfree(x4_s_proof);
1061
1062 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1063
1064 /* Send key confirmation proof */
1065 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1066 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1067 packet_send();
1068
1069 /* Expect confirmation from peer */
1070 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1071 input_userauth_jpake_server_confirm);
1072}
1073
1074/* ARGSUSED */
1075void
1076input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1077{
1078 Authctxt *authctxt = ctxt;
1079 struct jpake_ctx *pctx = authctxt->methoddata;
1080
1081 /* Disable this message */
1082 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1083
1084 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1085 packet_check_eom();
1086
1087 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1088
1089 /* Verify expected confirmation hash */
1090 if (jpake_check_confirm(pctx->k,
1091 pctx->server_id, pctx->server_id_len,
1092 session_id2, session_id2_len,
1093 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1094 debug("%s: %s success", __func__, authctxt->method->name);
1095 else {
1096 debug("%s: confirmation mismatch", __func__);
1097 /* XXX stash this so if auth succeeds then we can warn/kill */
1098 }
1099
1100 userauth_jpake_cleanup(authctxt);
1101}
1102#endif /* JPAKE */
1103
Ben Lindstrombba81212001-06-25 05:01:22 +00001104static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001105identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1106 u_char *data, u_int datalen)
1107{
1108 Key *prv;
1109 int ret;
1110
1111 /* the agent supports this key */
1112 if (id->ac)
1113 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1114 data, datalen));
1115 /*
1116 * we have already loaded the private key or
1117 * the private key is stored in external hardware
1118 */
1119 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1120 return (key_sign(id->key, sigp, lenp, data, datalen));
1121 /* load the private key from the file */
1122 if ((prv = load_identity_file(id->filename)) == NULL)
1123 return (-1);
1124 ret = key_sign(prv, sigp, lenp, data, datalen);
1125 key_free(prv);
1126 return (ret);
1127}
1128
1129static int
1130sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001131{
1132 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001133 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001134 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001135 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001136 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001137 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001138
Ben Lindstromd121f612000-12-03 17:00:47 +00001139 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001140
Damien Miller280ecfb2003-05-14 13:46:00 +10001141 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001143 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001144 return 0;
1145 }
Damien Millereba71ba2000-04-29 23:57:08 +10001146 /* data to be signed */
1147 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001148 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001149 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001150 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001151 } else {
1152 buffer_put_string(&b, session_id2, session_id2_len);
1153 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001154 }
Damien Millereba71ba2000-04-29 23:57:08 +10001155 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001156 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001157 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001158 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001159 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001160 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001161 if (datafellows & SSH_BUG_PKAUTH) {
1162 buffer_put_char(&b, have_sig);
1163 } else {
1164 buffer_put_cstring(&b, authctxt->method->name);
1165 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001166 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001167 }
Damien Millereba71ba2000-04-29 23:57:08 +10001168 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001169
1170 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001171 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001172 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001173 if (ret == -1) {
1174 xfree(blob);
1175 buffer_free(&b);
1176 return 0;
1177 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001178#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001179 buffer_dump(&b);
1180#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001181 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001182 buffer_clear(&b);
1183 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001184 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001185 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001186 buffer_put_cstring(&b, authctxt->server_user);
1187 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001188 buffer_put_cstring(&b, authctxt->method->name);
1189 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001190 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001191 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001192 buffer_put_string(&b, blob, bloblen);
1193 }
1194 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001195
Damien Millereba71ba2000-04-29 23:57:08 +10001196 /* append signature */
1197 buffer_put_string(&b, signature, slen);
1198 xfree(signature);
1199
1200 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001201 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001202 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001203 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001204
1205 /* put remaining data from buffer into packet */
1206 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1207 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1208 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001209 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001210
1211 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001212}
1213
Ben Lindstrombba81212001-06-25 05:01:22 +00001214static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001215send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001216{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001217 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001218 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001219
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001220 debug3("send_pubkey_test");
1221
Damien Miller280ecfb2003-05-14 13:46:00 +10001222 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001223 /* we cannot handle this key */
1224 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001225 return 0;
1226 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001227 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001228 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001229
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001230 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1231 packet_put_cstring(authctxt->server_user);
1232 packet_put_cstring(authctxt->service);
1233 packet_put_cstring(authctxt->method->name);
1234 packet_put_char(have_sig);
1235 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001236 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001237 packet_put_string(blob, bloblen);
1238 xfree(blob);
1239 packet_send();
1240 return 1;
1241}
1242
Ben Lindstrombba81212001-06-25 05:01:22 +00001243static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001244load_identity_file(char *filename)
1245{
1246 Key *private;
1247 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +10001248 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001249 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001250
Ben Lindstrom329782e2001-03-10 17:08:59 +00001251 if (stat(filename, &st) < 0) {
1252 debug3("no such identity: %s", filename);
1253 return NULL;
1254 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001255 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1256 if (!perm_ok)
1257 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001258 if (private == NULL) {
1259 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001260 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001261 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001262 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001263 for (i = 0; i < options.number_of_password_prompts; i++) {
1264 passphrase = read_passphrase(prompt, 0);
1265 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001266 private = key_load_private_type(KEY_UNSPEC,
1267 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001268 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001269 } else {
1270 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001271 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001272 }
1273 memset(passphrase, 0, strlen(passphrase));
1274 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001275 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001276 break;
1277 debug2("bad passphrase given, try again...");
1278 }
Damien Miller994cf142000-07-21 10:19:44 +10001279 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001280 return private;
1281}
1282
Damien Miller280ecfb2003-05-14 13:46:00 +10001283/*
1284 * try keys in the following order:
1285 * 1. agent keys that are found in the config file
1286 * 2. other agent keys
1287 * 3. keys that are only listed in the config file
1288 */
1289static void
1290pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001291{
Damien Miller280ecfb2003-05-14 13:46:00 +10001292 Identity *id;
1293 Idlist agent, files, *preferred;
1294 Key *key;
1295 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001296 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001297 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001298
Damien Miller280ecfb2003-05-14 13:46:00 +10001299 TAILQ_INIT(&agent); /* keys from the agent */
1300 TAILQ_INIT(&files); /* keys from the config file */
1301 preferred = &authctxt->keys;
1302 TAILQ_INIT(preferred); /* preferred order of keys */
1303
1304 /* list of keys stored in the filesystem */
1305 for (i = 0; i < options.num_identity_files; i++) {
1306 key = options.identity_keys[i];
1307 if (key && key->type == KEY_RSA1)
1308 continue;
1309 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001310 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001311 id->key = key;
1312 id->filename = xstrdup(options.identity_files[i]);
1313 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001314 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001315 /* list of keys supported by the agent */
1316 if ((ac = ssh_get_authentication_connection())) {
1317 for (key = ssh_get_first_identity(ac, &comment, 2);
1318 key != NULL;
1319 key = ssh_get_next_identity(ac, &comment, 2)) {
1320 found = 0;
1321 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001322 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001323 if (key_equal(key, id->key)) {
1324 key_free(key);
1325 xfree(comment);
1326 TAILQ_REMOVE(&files, id, next);
1327 TAILQ_INSERT_TAIL(preferred, id, next);
1328 id->ac = ac;
1329 found = 1;
1330 break;
1331 }
1332 }
Damien Millerbd394c32004-03-08 23:12:36 +11001333 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001334 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001335 id->key = key;
1336 id->filename = comment;
1337 id->ac = ac;
1338 TAILQ_INSERT_TAIL(&agent, id, next);
1339 }
1340 }
1341 /* append remaining agent keys */
1342 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1343 TAILQ_REMOVE(&agent, id, next);
1344 TAILQ_INSERT_TAIL(preferred, id, next);
1345 }
1346 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001347 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001348 /* append remaining keys from the config file */
1349 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1350 TAILQ_REMOVE(&files, id, next);
1351 TAILQ_INSERT_TAIL(preferred, id, next);
1352 }
1353 TAILQ_FOREACH(id, preferred, next) {
1354 debug2("key: %s (%p)", id->filename, id->key);
1355 }
1356}
1357
1358static void
1359pubkey_cleanup(Authctxt *authctxt)
1360{
1361 Identity *id;
1362
1363 if (authctxt->agent != NULL)
1364 ssh_close_authentication_connection(authctxt->agent);
1365 for (id = TAILQ_FIRST(&authctxt->keys); id;
1366 id = TAILQ_FIRST(&authctxt->keys)) {
1367 TAILQ_REMOVE(&authctxt->keys, id, next);
1368 if (id->key)
1369 key_free(id->key);
1370 if (id->filename)
1371 xfree(id->filename);
1372 xfree(id);
1373 }
Damien Millereba71ba2000-04-29 23:57:08 +10001374}
1375
Damien Miller62cee002000-09-23 17:15:56 +11001376int
1377userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001378{
Damien Miller280ecfb2003-05-14 13:46:00 +10001379 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001380 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001381
Damien Miller280ecfb2003-05-14 13:46:00 +10001382 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1383 if (id->tried++)
1384 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001385 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001386 TAILQ_REMOVE(&authctxt->keys, id, next);
1387 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1388 /*
1389 * send a test message if we have the public key. for
1390 * encrypted keys we cannot do this and have to load the
1391 * private key instead
1392 */
1393 if (id->key && id->key->type != KEY_RSA1) {
1394 debug("Offering public key: %s", id->filename);
1395 sent = send_pubkey_test(authctxt, id);
1396 } else if (id->key == NULL) {
1397 debug("Trying private key: %s", id->filename);
1398 id->key = load_identity_file(id->filename);
1399 if (id->key != NULL) {
1400 id->isprivate = 1;
1401 sent = sign_and_send_pubkey(authctxt, id);
1402 key_free(id->key);
1403 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001404 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001405 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001406 if (sent)
1407 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001408 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001409 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001410}
Damien Millereba71ba2000-04-29 23:57:08 +10001411
Damien Miller874d77b2000-10-14 16:23:11 +11001412/*
1413 * Send userauth request message specifying keyboard-interactive method.
1414 */
1415int
1416userauth_kbdint(Authctxt *authctxt)
1417{
1418 static int attempt = 0;
1419
1420 if (attempt++ >= options.number_of_password_prompts)
1421 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001422 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1423 if (attempt > 1 && !authctxt->info_req_seen) {
1424 debug3("userauth_kbdint: disable: no info_req_seen");
1425 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1426 return 0;
1427 }
Damien Miller874d77b2000-10-14 16:23:11 +11001428
1429 debug2("userauth_kbdint");
1430 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1431 packet_put_cstring(authctxt->server_user);
1432 packet_put_cstring(authctxt->service);
1433 packet_put_cstring(authctxt->method->name);
1434 packet_put_cstring(""); /* lang */
1435 packet_put_cstring(options.kbd_interactive_devices ?
1436 options.kbd_interactive_devices : "");
1437 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001438
1439 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1440 return 1;
1441}
1442
1443/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001444 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001445 */
1446void
Damien Miller630d6f42002-01-22 23:17:30 +11001447input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001448{
1449 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001450 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001451 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001452 int echo = 0;
1453
1454 debug2("input_userauth_info_req");
1455
1456 if (authctxt == NULL)
1457 fatal("input_userauth_info_req: no authentication context");
1458
Ben Lindstrom7d199962001-09-12 18:29:00 +00001459 authctxt->info_req_seen = 1;
1460
Damien Miller874d77b2000-10-14 16:23:11 +11001461 name = packet_get_string(NULL);
1462 inst = packet_get_string(NULL);
1463 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001464 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001465 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001466 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001467 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001468 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001469 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001470 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001471
1472 num_prompts = packet_get_int();
1473 /*
1474 * Begin to build info response packet based on prompts requested.
1475 * We commit to providing the correct number of responses, so if
1476 * further on we run into a problem that prevents this, we have to
1477 * be sure and clean this up and send a correct error response.
1478 */
1479 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1480 packet_put_int(num_prompts);
1481
Ben Lindstrom551ea372001-06-05 18:56:16 +00001482 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001483 for (i = 0; i < num_prompts; i++) {
1484 prompt = packet_get_string(NULL);
1485 echo = packet_get_char();
1486
Ben Lindstrom949974b2001-06-25 05:20:31 +00001487 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001488
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001489 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001490 memset(response, 0, strlen(response));
1491 xfree(response);
1492 xfree(prompt);
1493 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001494 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001495
Damien Miller9f643902001-11-12 11:02:52 +11001496 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001497 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001498}
Damien Miller62cee002000-09-23 17:15:56 +11001499
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001500static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001501ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001502 u_char *data, u_int datalen)
1503{
1504 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001505 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001506 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001507 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001508
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001509 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001510
Ben Lindstrom5206b952002-06-06 19:59:29 +00001511 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1512 error("ssh_keysign: no installed: %s", strerror(errno));
1513 return -1;
1514 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001515 if (fflush(stdout) != 0)
1516 error("ssh_keysign: fflush: %s", strerror(errno));
1517 if (pipe(to) < 0) {
1518 error("ssh_keysign: pipe: %s", strerror(errno));
1519 return -1;
1520 }
1521 if (pipe(from) < 0) {
1522 error("ssh_keysign: pipe: %s", strerror(errno));
1523 return -1;
1524 }
1525 if ((pid = fork()) < 0) {
1526 error("ssh_keysign: fork: %s", strerror(errno));
1527 return -1;
1528 }
1529 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001530 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001531 close(from[0]);
1532 if (dup2(from[1], STDOUT_FILENO) < 0)
1533 fatal("ssh_keysign: dup2: %s", strerror(errno));
1534 close(to[1]);
1535 if (dup2(to[0], STDIN_FILENO) < 0)
1536 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001537 close(from[1]);
1538 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001539 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001540 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1541 strerror(errno));
1542 }
1543 close(from[1]);
1544 close(to[0]);
1545
1546 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001547 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001548 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001549 if (ssh_msg_send(to[1], version, &b) == -1)
1550 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001551
Damien Miller901119b2002-10-04 11:10:04 +10001552 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001553 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001554 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001555 return -1;
1556 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001557 close(from[0]);
1558 close(to[1]);
1559
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001560 while (waitpid(pid, &status, 0) < 0)
1561 if (errno != EINTR)
1562 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001563
Ben Lindstrom5206b952002-06-06 19:59:29 +00001564 if (buffer_get_char(&b) != version) {
1565 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001566 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001567 return -1;
1568 }
1569 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001570 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001571
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001572 return 0;
1573}
1574
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001575int
1576userauth_hostbased(Authctxt *authctxt)
1577{
1578 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001579 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001580 Buffer b;
1581 u_char *signature, *blob;
Darren Tucker26c66622007-05-20 15:09:42 +10001582 char *chost, *pkalg, *p, myname[NI_MAXHOST];
Ben Lindstrom671388f2001-04-19 20:40:45 +00001583 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001584 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001585 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001586
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001587 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001588 for (i = 0; i < sensitive->nkeys; i++) {
1589 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001590 if (private && private->type != KEY_RSA1) {
1591 found = 1;
1592 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001593 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001594 break;
1595 }
1596 }
1597 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001598 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001599 return 0;
1600 }
1601 if (key_to_blob(private, &blob, &blen) == 0) {
1602 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001603 return 0;
1604 }
Damien Miller91c18472001-11-12 11:02:03 +11001605 /* figure out a name for the client host */
Darren Tucker26c66622007-05-20 15:09:42 +10001606 p = NULL;
1607 if (packet_connection_is_on_socket())
1608 p = get_local_name(packet_get_connection_in());
1609 if (p == NULL) {
1610 if (gethostname(myname, sizeof(myname)) == -1) {
1611 verbose("userauth_hostbased: gethostname: %s",
1612 strerror(errno));
1613 } else
1614 p = xstrdup(myname);
1615 }
Damien Miller91c18472001-11-12 11:02:03 +11001616 if (p == NULL) {
1617 error("userauth_hostbased: cannot get local ipaddr/name");
1618 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001619 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001620 return 0;
1621 }
1622 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001623 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001624 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001625 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001626
Ben Lindstrom671388f2001-04-19 20:40:45 +00001627 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1628 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001629 pkalg = xstrdup(key_ssh_name(private));
1630 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001631 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001632 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001633 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1634 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001635 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001636 buffer_put_cstring(&b, authctxt->method->name);
1637 buffer_put_cstring(&b, pkalg);
1638 buffer_put_string(&b, blob, blen);
1639 buffer_put_cstring(&b, chost);
1640 buffer_put_cstring(&b, authctxt->local_user);
1641#ifdef DEBUG_PK
1642 buffer_dump(&b);
1643#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001644 if (sensitive->external_keysign)
1645 ok = ssh_keysign(private, &signature, &slen,
1646 buffer_ptr(&b), buffer_len(&b));
1647 else
1648 ok = key_sign(private, &signature, &slen,
1649 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001650 key_free(private);
1651 buffer_free(&b);
1652 if (ok != 0) {
1653 error("key_sign failed");
1654 xfree(chost);
1655 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001656 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001657 return 0;
1658 }
1659 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1660 packet_put_cstring(authctxt->server_user);
1661 packet_put_cstring(authctxt->service);
1662 packet_put_cstring(authctxt->method->name);
1663 packet_put_cstring(pkalg);
1664 packet_put_string(blob, blen);
1665 packet_put_cstring(chost);
1666 packet_put_cstring(authctxt->local_user);
1667 packet_put_string(signature, slen);
1668 memset(signature, 's', slen);
1669 xfree(signature);
1670 xfree(chost);
1671 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001672 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001673
1674 packet_send();
1675 return 1;
1676}
1677
Damien Miller01ed2272008-11-05 16:20:46 +11001678#ifdef JPAKE
1679int
1680userauth_jpake(Authctxt *authctxt)
1681{
1682 struct jpake_ctx *pctx;
1683 u_char *x1_proof, *x2_proof;
1684 u_int x1_proof_len, x2_proof_len;
1685 static int attempt = 0; /* XXX share with userauth_password's? */
1686
1687 if (attempt++ >= options.number_of_password_prompts)
1688 return 0;
1689 if (attempt != 1)
1690 error("Permission denied, please try again.");
1691
1692 if (authctxt->methoddata != NULL)
1693 fatal("%s: authctxt->methoddata already set (%p)",
1694 __func__, authctxt->methoddata);
1695
1696 authctxt->methoddata = pctx = jpake_new();
1697
1698 /*
1699 * Send request immediately, to get the protocol going while
1700 * we do the initial computations.
1701 */
1702 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1703 packet_put_cstring(authctxt->server_user);
1704 packet_put_cstring(authctxt->service);
1705 packet_put_cstring(authctxt->method->name);
1706 packet_send();
1707 packet_write_wait();
1708
1709 jpake_step1(pctx->grp,
1710 &pctx->client_id, &pctx->client_id_len,
1711 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1712 &x1_proof, &x1_proof_len,
1713 &x2_proof, &x2_proof_len);
1714
1715 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1716
1717 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1718 packet_put_string(pctx->client_id, pctx->client_id_len);
1719 packet_put_bignum2(pctx->g_x1);
1720 packet_put_bignum2(pctx->g_x2);
1721 packet_put_string(x1_proof, x1_proof_len);
1722 packet_put_string(x2_proof, x2_proof_len);
1723 packet_send();
1724
1725 bzero(x1_proof, x1_proof_len);
1726 bzero(x2_proof, x2_proof_len);
1727 xfree(x1_proof);
1728 xfree(x2_proof);
1729
1730 /* Expect step 1 packet from peer */
1731 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1732 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001733 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1734 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001735
1736 return 1;
1737}
1738
1739void
1740userauth_jpake_cleanup(Authctxt *authctxt)
1741{
1742 debug3("%s: clean up", __func__);
1743 if (authctxt->methoddata != NULL) {
1744 jpake_free(authctxt->methoddata);
1745 authctxt->methoddata = NULL;
1746 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001747 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001748}
1749#endif /* JPAKE */
1750
Damien Miller62cee002000-09-23 17:15:56 +11001751/* find auth method */
1752
Damien Miller62cee002000-09-23 17:15:56 +11001753/*
1754 * given auth method name, if configurable options permit this method fill
1755 * in auth_ident field and return true, otherwise return false.
1756 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001757static int
Damien Miller62cee002000-09-23 17:15:56 +11001758authmethod_is_enabled(Authmethod *method)
1759{
1760 if (method == NULL)
1761 return 0;
1762 /* return false if options indicate this method is disabled */
1763 if (method->enabled == NULL || *method->enabled == 0)
1764 return 0;
1765 /* return false if batch mode is enabled but method needs interactive mode */
1766 if (method->batch_flag != NULL && *method->batch_flag != 0)
1767 return 0;
1768 return 1;
1769}
1770
Ben Lindstrombba81212001-06-25 05:01:22 +00001771static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001772authmethod_lookup(const char *name)
1773{
1774 Authmethod *method = NULL;
1775 if (name != NULL)
1776 for (method = authmethods; method->name != NULL; method++)
1777 if (strcmp(name, method->name) == 0)
1778 return method;
1779 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1780 return NULL;
1781}
1782
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001783/* XXX internal state */
1784static Authmethod *current = NULL;
1785static char *supported = NULL;
1786static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001787
Damien Miller62cee002000-09-23 17:15:56 +11001788/*
1789 * Given the authentication method list sent by the server, return the
1790 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001791 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001792 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001793static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001794authmethod_get(char *authlist)
1795{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001796 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001797 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001798
Damien Miller62cee002000-09-23 17:15:56 +11001799 /* Use a suitable default if we're passed a nil list. */
1800 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001801 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001802
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001803 if (supported == NULL || strcmp(authlist, supported) != 0) {
1804 debug3("start over, passed a different list %s", authlist);
1805 if (supported != NULL)
1806 xfree(supported);
1807 supported = xstrdup(authlist);
1808 preferred = options.preferred_authentications;
1809 debug3("preferred %s", preferred);
1810 current = NULL;
1811 } else if (current != NULL && authmethod_is_enabled(current))
1812 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001813
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001814 for (;;) {
1815 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001816 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001817 current = NULL;
1818 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001819 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001820 preferred += next;
1821 debug3("authmethod_lookup %s", name);
1822 debug3("remaining preferred: %s", preferred);
1823 if ((current = authmethod_lookup(name)) != NULL &&
1824 authmethod_is_enabled(current)) {
1825 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001826 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001827 return current;
1828 }
Damien Millereba71ba2000-04-29 23:57:08 +10001829 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001830}
Damien Miller62cee002000-09-23 17:15:56 +11001831
Ben Lindstrom79073822001-07-04 03:42:30 +00001832static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001833authmethods_get(void)
1834{
1835 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001836 Buffer b;
1837 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001838
Damien Miller0e3b8722002-01-22 23:26:38 +11001839 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001840 for (method = authmethods; method->name != NULL; method++) {
1841 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001842 if (buffer_len(&b) > 0)
1843 buffer_append(&b, ",", 1);
1844 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001845 }
Damien Miller62cee002000-09-23 17:15:56 +11001846 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001847 buffer_append(&b, "\0", 1);
1848 list = xstrdup(buffer_ptr(&b));
1849 buffer_free(&b);
1850 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001851}
Damien Miller01ed2272008-11-05 16:20:46 +11001852