blob: 260c6307a69ab60308f36d9a2e9070e964393a30 [file] [log] [blame]
Damien Millercee85232009-03-06 00:58:22 +11001/* $OpenBSD: sshconnect2.c,v 1.171 2009/03/05 07:18:19 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 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
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000155 session_id2 = kex->session_id;
156 session_id2_len = kex->session_id_len;
157
Damien Miller874d77b2000-10-14 16:23:11 +1100158#ifdef DEBUG_KEXDH
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE);
161 packet_put_cstring("markus");
162 packet_send();
163 packet_write_wait();
164#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000165}
Damien Millerb1715dc2000-05-30 13:44:51 +1000166
Damien Millereba71ba2000-04-29 23:57:08 +1000167/*
168 * Authenticate user
169 */
Damien Miller62cee002000-09-23 17:15:56 +1100170
171typedef struct Authctxt Authctxt;
172typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000173typedef struct identity Identity;
174typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100175
Damien Miller280ecfb2003-05-14 13:46:00 +1000176struct identity {
177 TAILQ_ENTRY(identity) next;
178 AuthenticationConnection *ac; /* set if agent supports key */
179 Key *key; /* public/private key */
180 char *filename; /* comment for agent-only keys */
181 int tried;
182 int isprivate; /* key points to the private key */
183};
184TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100185
186struct Authctxt {
187 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000188 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100189 const char *host;
190 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100191 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100192 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000193 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000194 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000195 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000196 AuthenticationConnection *agent;
197 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000198 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000199 /* kbd-interactive */
200 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000201 /* generic */
202 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100203};
204struct Authmethod {
205 char *name; /* string to compare against server's list */
206 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100207 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100208 int *enabled; /* flag in option struct that enables method */
209 int *batch_flag; /* flag in option struct that disables method */
210};
211
Damien Miller630d6f42002-01-22 23:17:30 +1100212void input_userauth_success(int, u_int32_t, void *);
213void input_userauth_failure(int, u_int32_t, void *);
214void input_userauth_banner(int, u_int32_t, void *);
215void input_userauth_error(int, u_int32_t, void *);
216void input_userauth_info_req(int, u_int32_t, void *);
217void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000218void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100219void input_userauth_jpake_server_step1(int, u_int32_t, void *);
220void input_userauth_jpake_server_step2(int, u_int32_t, void *);
221void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100222
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000223int userauth_none(Authctxt *);
224int userauth_pubkey(Authctxt *);
225int userauth_passwd(Authctxt *);
226int userauth_kbdint(Authctxt *);
227int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100228int userauth_jpake(Authctxt *);
229
230void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100231
Darren Tucker0efd1552003-08-26 11:49:55 +1000232#ifdef GSSAPI
233int userauth_gssapi(Authctxt *authctxt);
234void input_gssapi_response(int type, u_int32_t, void *);
235void input_gssapi_token(int type, u_int32_t, void *);
236void input_gssapi_hash(int type, u_int32_t, void *);
237void input_gssapi_error(int, u_int32_t, void *);
238void input_gssapi_errtok(int, u_int32_t, void *);
239#endif
240
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000241void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000242
Damien Miller280ecfb2003-05-14 13:46:00 +1000243static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000244static void pubkey_prepare(Authctxt *);
245static void pubkey_cleanup(Authctxt *);
246static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static Authmethod *authmethod_get(char *authlist);
249static Authmethod *authmethod_lookup(const char *name);
250static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100251
252Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000253#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100254 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000255 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100256 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000257 &options.gss_authentication,
258 NULL},
259#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000260 {"hostbased",
261 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100262 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000263 &options.hostbased_authentication,
264 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000265 {"publickey",
266 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100267 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000268 &options.pubkey_authentication,
269 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100270#ifdef JPAKE
271 {"jpake-01@openssh.com",
272 userauth_jpake,
273 userauth_jpake_cleanup,
274 &options.zero_knowledge_password_authentication,
275 &options.batch_mode},
276#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100277 {"keyboard-interactive",
278 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100279 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100280 &options.kbd_interactive_authentication,
281 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000282 {"password",
283 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100284 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000285 &options.password_authentication,
286 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100287 {"none",
288 userauth_none,
289 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100290 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100291 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100292 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100293};
294
295void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000296ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000297 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100298{
299 Authctxt authctxt;
300 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100301
Ben Lindstrom551ea372001-06-05 18:56:16 +0000302 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000303 options.kbd_interactive_authentication = 1;
304
Damien Miller62cee002000-09-23 17:15:56 +1100305 packet_start(SSH2_MSG_SERVICE_REQUEST);
306 packet_put_cstring("ssh-userauth");
307 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000308 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100309 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100310 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000311 if (type != SSH2_MSG_SERVICE_ACCEPT)
312 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100313 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100314 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000315 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100316 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100317 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000318 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100319 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100320 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000321 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100322
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000323 if (options.preferred_authentications == NULL)
324 options.preferred_authentications = authmethods_get();
325
Damien Miller62cee002000-09-23 17:15:56 +1100326 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000327 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000328 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100329 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000330 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100331 authctxt.host = host;
332 authctxt.service = "ssh-connection"; /* service name */
333 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100334 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000335 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000336 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000337 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000338 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100339 if (authctxt.method == NULL)
340 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100341
342 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100343 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100344
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000345 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100346 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
347 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000348 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100349 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
350
Damien Miller280ecfb2003-05-14 13:46:00 +1000351 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000352 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
353
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000354 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100355}
Damien Millerf842fcb2003-05-15 12:01:28 +1000356
Damien Miller62cee002000-09-23 17:15:56 +1100357void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000358userauth(Authctxt *authctxt, char *authlist)
359{
Damien Miller01ed2272008-11-05 16:20:46 +1100360 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
361 authctxt->method->cleanup(authctxt);
362
Darren Tucker0efd1552003-08-26 11:49:55 +1000363 if (authctxt->methoddata) {
364 xfree(authctxt->methoddata);
365 authctxt->methoddata = NULL;
366 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000367 if (authlist == NULL) {
368 authlist = authctxt->authlist;
369 } else {
370 if (authctxt->authlist)
371 xfree(authctxt->authlist);
372 authctxt->authlist = authlist;
373 }
374 for (;;) {
375 Authmethod *method = authmethod_get(authlist);
376 if (method == NULL)
377 fatal("Permission denied (%s).", authlist);
378 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000379
380 /* reset the per method handler */
381 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
382 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
383
384 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000385 if (method->userauth(authctxt) != 0) {
386 debug2("we sent a %s packet, wait for reply", method->name);
387 break;
388 } else {
389 debug2("we did not send a packet, disable method");
390 method->enabled = NULL;
391 }
392 }
393}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000394
Damien Millerf7475d72008-11-03 19:26:18 +1100395/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000396void
Damien Miller630d6f42002-01-22 23:17:30 +1100397input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100398{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000399 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000400 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000401}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000402
Damien Millerf7475d72008-11-03 19:26:18 +1100403/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000404void
Damien Miller630d6f42002-01-22 23:17:30 +1100405input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000406{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000407 char *msg, *raw, *lang;
408 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000409
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000410 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000411 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000412 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100413 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000414 if (len > 65536)
415 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100416 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
417 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
Darren Tucker046dff22003-10-08 17:32:02 +1000418 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000419 xfree(msg);
420 }
421 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000422 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100423}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000424
Damien Millerf7475d72008-11-03 19:26:18 +1100425/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100426void
Damien Miller630d6f42002-01-22 23:17:30 +1100427input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100428{
429 Authctxt *authctxt = ctxt;
430 if (authctxt == NULL)
431 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000432 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000433 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000434 authctxt->authlist = NULL;
435 }
436 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000437 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000438 authctxt->methoddata = NULL;
439 }
Damien Miller62cee002000-09-23 17:15:56 +1100440 authctxt->success = 1; /* break out */
441}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000442
Damien Millerf7475d72008-11-03 19:26:18 +1100443/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100444void
Damien Miller630d6f42002-01-22 23:17:30 +1100445input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100446{
Damien Miller62cee002000-09-23 17:15:56 +1100447 Authctxt *authctxt = ctxt;
448 char *authlist = NULL;
449 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100450
451 if (authctxt == NULL)
452 fatal("input_userauth_failure: no authentication context");
453
Damien Miller874d77b2000-10-14 16:23:11 +1100454 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100455 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100456 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100457
458 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000459 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000460 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100461
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000462 userauth(authctxt, authlist);
463}
Damien Millerf7475d72008-11-03 19:26:18 +1100464
465/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000466void
Damien Miller630d6f42002-01-22 23:17:30 +1100467input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000468{
469 Authctxt *authctxt = ctxt;
470 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000471 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000472 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000473 int pktype, sent = 0;
474 u_int alen, blen;
475 char *pkalg, *fp;
476 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000477
478 if (authctxt == NULL)
479 fatal("input_userauth_pk_ok: no authentication context");
480 if (datafellows & SSH_BUG_PKOK) {
481 /* this is similar to SSH_BUG_PKAUTH */
482 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
483 pkblob = packet_get_string(&blen);
484 buffer_init(&b);
485 buffer_append(&b, pkblob, blen);
486 pkalg = buffer_get_string(&b, &alen);
487 buffer_free(&b);
488 } else {
489 pkalg = packet_get_string(&alen);
490 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000491 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100492 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000493
Damien Miller280ecfb2003-05-14 13:46:00 +1000494 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000495
Damien Miller280ecfb2003-05-14 13:46:00 +1000496 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
497 debug("unknown pkalg %s", pkalg);
498 goto done;
499 }
500 if ((key = key_from_blob(pkblob, blen)) == NULL) {
501 debug("no key from blob. pkalg %s", pkalg);
502 goto done;
503 }
504 if (key->type != pktype) {
505 error("input_userauth_pk_ok: type mismatch "
506 "for decoded key (received %d, expected %d)",
507 key->type, pktype);
508 goto done;
509 }
510 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
511 debug2("input_userauth_pk_ok: fp %s", fp);
512 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000513
Darren Tuckerd05b6012003-10-15 15:55:59 +1000514 /*
515 * search keys in the reverse order, because last candidate has been
516 * moved to the end of the queue. this also avoids confusion by
517 * duplicate keys
518 */
Damien Miller0b51a522004-04-20 20:07:19 +1000519 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000520 if (key_equal(key, id->key)) {
521 sent = sign_and_send_pubkey(authctxt, id);
522 break;
523 }
524 }
525done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000526 if (key != NULL)
527 key_free(key);
528 xfree(pkalg);
529 xfree(pkblob);
530
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000531 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000532 if (sent == 0)
533 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100534}
535
Darren Tucker0efd1552003-08-26 11:49:55 +1000536#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100537int
Darren Tucker0efd1552003-08-26 11:49:55 +1000538userauth_gssapi(Authctxt *authctxt)
539{
540 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100541 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000542 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000543 OM_uint32 min;
544 int ok = 0;
545
546 /* Try one GSSAPI method at a time, rather than sending them all at
547 * once. */
548
Darren Tucker56afe142003-11-03 20:06:14 +1100549 if (gss_supported == NULL)
550 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000551
552 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100553 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000554 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100555 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000556 ssh_gssapi_check_mechanism(&gssctxt,
557 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000558 ok = 1; /* Mechanism works */
559 } else {
560 mech++;
561 }
562 }
563
Damien Millera1cb9f32006-08-19 00:33:34 +1000564 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000565 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000566
567 authctxt->methoddata=(void *)gssctxt;
568
569 packet_start(SSH2_MSG_USERAUTH_REQUEST);
570 packet_put_cstring(authctxt->server_user);
571 packet_put_cstring(authctxt->service);
572 packet_put_cstring(authctxt->method->name);
573
574 packet_put_int(1);
575
Darren Tucker655a5e02003-11-03 20:09:03 +1100576 packet_put_int((gss_supported->elements[mech].length) + 2);
577 packet_put_char(SSH_GSS_OIDTYPE);
578 packet_put_char(gss_supported->elements[mech].length);
579 packet_put_raw(gss_supported->elements[mech].elements,
580 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000581
582 packet_send();
583
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
587 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
588
589 mech++; /* Move along to next candidate */
590
591 return 1;
592}
593
Damien Miller91c6aa42003-11-17 21:20:18 +1100594static OM_uint32
595process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
596{
597 Authctxt *authctxt = ctxt;
598 Gssctxt *gssctxt = authctxt->methoddata;
599 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000600 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
601 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100602 OM_uint32 status, ms, flags;
603 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100604
Damien Miller91c6aa42003-11-17 21:20:18 +1100605 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100606 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100607
608 if (send_tok.length > 0) {
609 if (GSS_ERROR(status))
610 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
611 else
612 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100613
Damien Miller91c6aa42003-11-17 21:20:18 +1100614 packet_put_string(send_tok.value, send_tok.length);
615 packet_send();
616 gss_release_buffer(&ms, &send_tok);
617 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100618
Damien Miller91c6aa42003-11-17 21:20:18 +1100619 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100620 /* send either complete or MIC, depending on mechanism */
621 if (!(flags & GSS_C_INTEG_FLAG)) {
622 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
623 packet_send();
624 } else {
625 ssh_gssapi_buildmic(&b, authctxt->server_user,
626 authctxt->service, "gssapi-with-mic");
627
628 gssbuf.value = buffer_ptr(&b);
629 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100630
Damien Miller0425d402003-11-17 22:18:21 +1100631 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100632
Damien Miller0425d402003-11-17 22:18:21 +1100633 if (!GSS_ERROR(status)) {
634 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
635 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100636
Damien Miller0425d402003-11-17 22:18:21 +1100637 packet_send();
638 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100639
Damien Miller0425d402003-11-17 22:18:21 +1100640 buffer_free(&b);
641 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100642 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100643 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100644
Damien Miller91c6aa42003-11-17 21:20:18 +1100645 return status;
646}
647
Damien Millerf7475d72008-11-03 19:26:18 +1100648/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000649void
650input_gssapi_response(int type, u_int32_t plen, void *ctxt)
651{
652 Authctxt *authctxt = ctxt;
653 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000654 int oidlen;
655 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000656
657 if (authctxt == NULL)
658 fatal("input_gssapi_response: no authentication context");
659 gssctxt = authctxt->methoddata;
660
661 /* Setup our OID */
662 oidv = packet_get_string(&oidlen);
663
Darren Tucker655a5e02003-11-03 20:09:03 +1100664 if (oidlen <= 2 ||
665 oidv[0] != SSH_GSS_OIDTYPE ||
666 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100667 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100668 debug("Badly encoded mechanism OID received");
669 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100670 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000671 }
672
Darren Tucker655a5e02003-11-03 20:09:03 +1100673 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
674 fatal("Server returned different OID than expected");
675
Darren Tucker0efd1552003-08-26 11:49:55 +1000676 packet_check_eom();
677
678 xfree(oidv);
679
Damien Miller91c6aa42003-11-17 21:20:18 +1100680 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000681 /* Start again with next method on list */
682 debug("Trying to start again");
683 userauth(authctxt, NULL);
684 return;
685 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000686}
687
Damien Millerf7475d72008-11-03 19:26:18 +1100688/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000689void
690input_gssapi_token(int type, u_int32_t plen, void *ctxt)
691{
692 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000693 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100694 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000695 u_int slen;
696
697 if (authctxt == NULL)
698 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000699
700 recv_tok.value = packet_get_string(&slen);
701 recv_tok.length = slen; /* safe typecast */
702
703 packet_check_eom();
704
Damien Miller91c6aa42003-11-17 21:20:18 +1100705 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000706
707 xfree(recv_tok.value);
708
709 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000710 /* Start again with the next method in the list */
711 userauth(authctxt, NULL);
712 return;
713 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000714}
715
Damien Millerf7475d72008-11-03 19:26:18 +1100716/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000717void
718input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
719{
720 Authctxt *authctxt = ctxt;
721 Gssctxt *gssctxt;
722 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
723 gss_buffer_desc recv_tok;
724 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000725 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000726
727 if (authctxt == NULL)
728 fatal("input_gssapi_response: no authentication context");
729 gssctxt = authctxt->methoddata;
730
Darren Tucker600ad8d2003-08-26 12:10:48 +1000731 recv_tok.value = packet_get_string(&len);
732 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000733
734 packet_check_eom();
735
736 /* Stick it into GSSAPI and see what it says */
737 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000738 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000739
740 xfree(recv_tok.value);
741 gss_release_buffer(&ms, &send_tok);
742
743 /* Server will be returning a failed packet after this one */
744}
745
Damien Millerf7475d72008-11-03 19:26:18 +1100746/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000747void
748input_gssapi_error(int type, u_int32_t plen, void *ctxt)
749{
750 OM_uint32 maj, min;
751 char *msg;
752 char *lang;
753
754 maj=packet_get_int();
755 min=packet_get_int();
756 msg=packet_get_string(NULL);
757 lang=packet_get_string(NULL);
758
759 packet_check_eom();
760
Damien Miller15d72a02005-11-05 15:07:33 +1100761 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000762 xfree(msg);
763 xfree(lang);
764}
765#endif /* GSSAPI */
766
Damien Millereba71ba2000-04-29 23:57:08 +1000767int
Damien Miller874d77b2000-10-14 16:23:11 +1100768userauth_none(Authctxt *authctxt)
769{
770 /* initial userauth request */
771 packet_start(SSH2_MSG_USERAUTH_REQUEST);
772 packet_put_cstring(authctxt->server_user);
773 packet_put_cstring(authctxt->service);
774 packet_put_cstring(authctxt->method->name);
775 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100776 return 1;
777}
778
779int
Damien Miller62cee002000-09-23 17:15:56 +1100780userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000781{
Damien Millere247cc42000-05-07 12:03:14 +1000782 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000783 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000784 char *password;
785
Damien Millerd3a18572000-06-07 19:55:44 +1000786 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000787 return 0;
788
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000789 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000790 error("Permission denied, please try again.");
791
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000792 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100793 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000794 password = read_passphrase(prompt, 0);
795 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100796 packet_put_cstring(authctxt->server_user);
797 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100798 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000799 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000800 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000801 memset(password, 0, strlen(password));
802 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100803 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000804 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000805
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000806 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000807 &input_userauth_passwd_changereq);
808
Damien Millereba71ba2000-04-29 23:57:08 +1000809 return 1;
810}
Damien Millerf7475d72008-11-03 19:26:18 +1100811
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000812/*
813 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
814 */
Damien Millerf7475d72008-11-03 19:26:18 +1100815/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000816void
Tim Ricec8549622002-03-31 12:49:38 -0800817input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000818{
819 Authctxt *authctxt = ctxt;
820 char *info, *lang, *password = NULL, *retype = NULL;
821 char prompt[150];
822
823 debug2("input_userauth_passwd_changereq");
824
825 if (authctxt == NULL)
826 fatal("input_userauth_passwd_changereq: "
827 "no authentication context");
828
829 info = packet_get_string(NULL);
830 lang = packet_get_string(NULL);
831 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000833 xfree(info);
834 xfree(lang);
835 packet_start(SSH2_MSG_USERAUTH_REQUEST);
836 packet_put_cstring(authctxt->server_user);
837 packet_put_cstring(authctxt->service);
838 packet_put_cstring(authctxt->method->name);
839 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000840 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000841 "Enter %.30s@%.128s's old password: ",
842 authctxt->server_user, authctxt->host);
843 password = read_passphrase(prompt, 0);
844 packet_put_cstring(password);
845 memset(password, 0, strlen(password));
846 xfree(password);
847 password = NULL;
848 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000849 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000850 "Enter %.30s@%.128s's new password: ",
851 authctxt->server_user, authctxt->host);
852 password = read_passphrase(prompt, RP_ALLOW_EOF);
853 if (password == NULL) {
854 /* bail out */
855 return;
856 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000857 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000858 "Retype %.30s@%.128s's new password: ",
859 authctxt->server_user, authctxt->host);
860 retype = read_passphrase(prompt, 0);
861 if (strcmp(password, retype) != 0) {
862 memset(password, 0, strlen(password));
863 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000864 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000865 password = NULL;
866 }
867 memset(retype, 0, strlen(retype));
868 xfree(retype);
869 }
870 packet_put_cstring(password);
871 memset(password, 0, strlen(password));
872 xfree(password);
873 packet_add_padding(64);
874 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000875
876 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000877 &input_userauth_passwd_changereq);
878}
Damien Millereba71ba2000-04-29 23:57:08 +1000879
Damien Miller01ed2272008-11-05 16:20:46 +1100880#ifdef JPAKE
881static char *
882pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
883{
884 /* OpenBSD crypt(3) handles all of these */
885 if (strcmp(crypt_scheme, "crypt") == 0 ||
886 strcmp(crypt_scheme, "bcrypt") == 0 ||
887 strcmp(crypt_scheme, "md5crypt") == 0 ||
888 strcmp(crypt_scheme, "crypt-extended") == 0)
889 return xstrdup(crypt(password, salt));
890 error("%s: unsupported password encryption scheme \"%.100s\"",
891 __func__, crypt_scheme);
892 return NULL;
893}
894
895static BIGNUM *
896jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
897 const char *salt)
898{
899 char prompt[256], *password, *crypted;
900 u_char *secret;
901 u_int secret_len;
902 BIGNUM *ret;
903
904 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
905 authctxt->server_user, authctxt->host);
906 password = read_passphrase(prompt, 0);
907
908 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
909 logit("Disabling %s authentication", authctxt->method->name);
910 authctxt->method->enabled = NULL;
911 /* Continue with an empty password to fail gracefully */
912 crypted = xstrdup("");
913 }
914
915#ifdef JPAKE_DEBUG
916 debug3("%s: salt = %s", __func__, salt);
917 debug3("%s: scheme = %s", __func__, crypt_scheme);
918 debug3("%s: crypted = %s", __func__, crypted);
919#endif
920
921 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
922 &secret, &secret_len) != 0)
923 fatal("%s: hash_buffer", __func__);
924
925 bzero(password, strlen(password));
926 bzero(crypted, strlen(crypted));
927 xfree(password);
928 xfree(crypted);
929
930 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
931 fatal("%s: BN_bin2bn (secret)", __func__);
932 bzero(secret, secret_len);
933 xfree(secret);
934
935 return ret;
936}
937
938/* ARGSUSED */
939void
940input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
941{
942 Authctxt *authctxt = ctxt;
943 struct jpake_ctx *pctx = authctxt->methoddata;
944 u_char *x3_proof, *x4_proof, *x2_s_proof;
945 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
946 char *crypt_scheme, *salt;
947
948 /* Disable this message */
949 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
950
951 if ((pctx->g_x3 = BN_new()) == NULL ||
952 (pctx->g_x4 = BN_new()) == NULL)
953 fatal("%s: BN_new", __func__);
954
955 /* Fetch step 1 values */
956 crypt_scheme = packet_get_string(NULL);
957 salt = packet_get_string(NULL);
958 pctx->server_id = packet_get_string(&pctx->server_id_len);
959 packet_get_bignum2(pctx->g_x3);
960 packet_get_bignum2(pctx->g_x4);
961 x3_proof = packet_get_string(&x3_proof_len);
962 x4_proof = packet_get_string(&x4_proof_len);
963 packet_check_eom();
964
965 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
966
967 /* Obtain password and derive secret */
968 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
969 bzero(crypt_scheme, strlen(crypt_scheme));
970 bzero(salt, strlen(salt));
971 xfree(crypt_scheme);
972 xfree(salt);
973 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
974
975 /* Calculate step 2 values */
976 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
977 pctx->g_x3, pctx->g_x4, pctx->x2,
978 pctx->server_id, pctx->server_id_len,
979 pctx->client_id, pctx->client_id_len,
980 x3_proof, x3_proof_len,
981 x4_proof, x4_proof_len,
982 &pctx->a,
983 &x2_s_proof, &x2_s_proof_len);
984
985 bzero(x3_proof, x3_proof_len);
986 bzero(x4_proof, x4_proof_len);
987 xfree(x3_proof);
988 xfree(x4_proof);
989
990 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
991
992 /* Send values for step 2 */
993 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
994 packet_put_bignum2(pctx->a);
995 packet_put_string(x2_s_proof, x2_s_proof_len);
996 packet_send();
997
998 bzero(x2_s_proof, x2_s_proof_len);
999 xfree(x2_s_proof);
1000
1001 /* Expect step 2 packet from peer */
1002 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1003 input_userauth_jpake_server_step2);
1004}
1005
1006/* ARGSUSED */
1007void
1008input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1009{
1010 Authctxt *authctxt = ctxt;
1011 struct jpake_ctx *pctx = authctxt->methoddata;
1012 u_char *x4_s_proof;
1013 u_int x4_s_proof_len;
1014
1015 /* Disable this message */
1016 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1017
1018 if ((pctx->b = BN_new()) == NULL)
1019 fatal("%s: BN_new", __func__);
1020
1021 /* Fetch step 2 values */
1022 packet_get_bignum2(pctx->b);
1023 x4_s_proof = packet_get_string(&x4_s_proof_len);
1024 packet_check_eom();
1025
1026 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1027
1028 /* Derive shared key and calculate confirmation hash */
1029 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1030 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1031 pctx->client_id, pctx->client_id_len,
1032 pctx->server_id, pctx->server_id_len,
1033 session_id2, session_id2_len,
1034 x4_s_proof, x4_s_proof_len,
1035 &pctx->k,
1036 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1037
1038 bzero(x4_s_proof, x4_s_proof_len);
1039 xfree(x4_s_proof);
1040
1041 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1042
1043 /* Send key confirmation proof */
1044 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1045 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1046 packet_send();
1047
1048 /* Expect confirmation from peer */
1049 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1050 input_userauth_jpake_server_confirm);
1051}
1052
1053/* ARGSUSED */
1054void
1055input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1056{
1057 Authctxt *authctxt = ctxt;
1058 struct jpake_ctx *pctx = authctxt->methoddata;
1059
1060 /* Disable this message */
1061 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1062
1063 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1064 packet_check_eom();
1065
1066 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1067
1068 /* Verify expected confirmation hash */
1069 if (jpake_check_confirm(pctx->k,
1070 pctx->server_id, pctx->server_id_len,
1071 session_id2, session_id2_len,
1072 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1073 debug("%s: %s success", __func__, authctxt->method->name);
1074 else {
1075 debug("%s: confirmation mismatch", __func__);
1076 /* XXX stash this so if auth succeeds then we can warn/kill */
1077 }
1078
1079 userauth_jpake_cleanup(authctxt);
1080}
1081#endif /* JPAKE */
1082
Ben Lindstrombba81212001-06-25 05:01:22 +00001083static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001084identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1085 u_char *data, u_int datalen)
1086{
1087 Key *prv;
1088 int ret;
1089
1090 /* the agent supports this key */
1091 if (id->ac)
1092 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1093 data, datalen));
1094 /*
1095 * we have already loaded the private key or
1096 * the private key is stored in external hardware
1097 */
1098 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1099 return (key_sign(id->key, sigp, lenp, data, datalen));
1100 /* load the private key from the file */
1101 if ((prv = load_identity_file(id->filename)) == NULL)
1102 return (-1);
1103 ret = key_sign(prv, sigp, lenp, data, datalen);
1104 key_free(prv);
1105 return (ret);
1106}
1107
1108static int
1109sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001110{
1111 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001112 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001113 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001114 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001115 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001116 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001117
Ben Lindstromd121f612000-12-03 17:00:47 +00001118 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001119
Damien Miller280ecfb2003-05-14 13:46:00 +10001120 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001122 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001123 return 0;
1124 }
Damien Millereba71ba2000-04-29 23:57:08 +10001125 /* data to be signed */
1126 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001127 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001128 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001129 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001130 } else {
1131 buffer_put_string(&b, session_id2, session_id2_len);
1132 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001133 }
Damien Millereba71ba2000-04-29 23:57:08 +10001134 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001135 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001136 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001137 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001138 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001139 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001140 if (datafellows & SSH_BUG_PKAUTH) {
1141 buffer_put_char(&b, have_sig);
1142 } else {
1143 buffer_put_cstring(&b, authctxt->method->name);
1144 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001145 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001146 }
Damien Millereba71ba2000-04-29 23:57:08 +10001147 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001148
1149 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001150 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001151 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001152 if (ret == -1) {
1153 xfree(blob);
1154 buffer_free(&b);
1155 return 0;
1156 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001157#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001158 buffer_dump(&b);
1159#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001160 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001161 buffer_clear(&b);
1162 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001163 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001164 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001165 buffer_put_cstring(&b, authctxt->server_user);
1166 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001167 buffer_put_cstring(&b, authctxt->method->name);
1168 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001169 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001170 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001171 buffer_put_string(&b, blob, bloblen);
1172 }
1173 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001174
Damien Millereba71ba2000-04-29 23:57:08 +10001175 /* append signature */
1176 buffer_put_string(&b, signature, slen);
1177 xfree(signature);
1178
1179 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001180 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001181 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001182 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001183
1184 /* put remaining data from buffer into packet */
1185 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1186 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1187 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001188 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001189
1190 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001191}
1192
Ben Lindstrombba81212001-06-25 05:01:22 +00001193static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001194send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001195{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001196 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001197 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001198
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001199 debug3("send_pubkey_test");
1200
Damien Miller280ecfb2003-05-14 13:46:00 +10001201 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001202 /* we cannot handle this key */
1203 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001204 return 0;
1205 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001206 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001207 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001208
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001209 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1210 packet_put_cstring(authctxt->server_user);
1211 packet_put_cstring(authctxt->service);
1212 packet_put_cstring(authctxt->method->name);
1213 packet_put_char(have_sig);
1214 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001215 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001216 packet_put_string(blob, bloblen);
1217 xfree(blob);
1218 packet_send();
1219 return 1;
1220}
1221
Ben Lindstrombba81212001-06-25 05:01:22 +00001222static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001223load_identity_file(char *filename)
1224{
1225 Key *private;
1226 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +10001227 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001228 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001229
Ben Lindstrom329782e2001-03-10 17:08:59 +00001230 if (stat(filename, &st) < 0) {
1231 debug3("no such identity: %s", filename);
1232 return NULL;
1233 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001234 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1235 if (!perm_ok)
1236 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001237 if (private == NULL) {
1238 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001239 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001240 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001241 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001242 for (i = 0; i < options.number_of_password_prompts; i++) {
1243 passphrase = read_passphrase(prompt, 0);
1244 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001245 private = key_load_private_type(KEY_UNSPEC,
1246 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001247 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001248 } else {
1249 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001250 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001251 }
1252 memset(passphrase, 0, strlen(passphrase));
1253 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001254 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001255 break;
1256 debug2("bad passphrase given, try again...");
1257 }
Damien Miller994cf142000-07-21 10:19:44 +10001258 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001259 return private;
1260}
1261
Damien Miller280ecfb2003-05-14 13:46:00 +10001262/*
1263 * try keys in the following order:
1264 * 1. agent keys that are found in the config file
1265 * 2. other agent keys
1266 * 3. keys that are only listed in the config file
1267 */
1268static void
1269pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001270{
Damien Miller280ecfb2003-05-14 13:46:00 +10001271 Identity *id;
1272 Idlist agent, files, *preferred;
1273 Key *key;
1274 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001275 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001276 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001277
Damien Miller280ecfb2003-05-14 13:46:00 +10001278 TAILQ_INIT(&agent); /* keys from the agent */
1279 TAILQ_INIT(&files); /* keys from the config file */
1280 preferred = &authctxt->keys;
1281 TAILQ_INIT(preferred); /* preferred order of keys */
1282
1283 /* list of keys stored in the filesystem */
1284 for (i = 0; i < options.num_identity_files; i++) {
1285 key = options.identity_keys[i];
1286 if (key && key->type == KEY_RSA1)
1287 continue;
1288 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001289 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001290 id->key = key;
1291 id->filename = xstrdup(options.identity_files[i]);
1292 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001293 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001294 /* list of keys supported by the agent */
1295 if ((ac = ssh_get_authentication_connection())) {
1296 for (key = ssh_get_first_identity(ac, &comment, 2);
1297 key != NULL;
1298 key = ssh_get_next_identity(ac, &comment, 2)) {
1299 found = 0;
1300 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001301 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001302 if (key_equal(key, id->key)) {
1303 key_free(key);
1304 xfree(comment);
1305 TAILQ_REMOVE(&files, id, next);
1306 TAILQ_INSERT_TAIL(preferred, id, next);
1307 id->ac = ac;
1308 found = 1;
1309 break;
1310 }
1311 }
Damien Millerbd394c32004-03-08 23:12:36 +11001312 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001313 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001314 id->key = key;
1315 id->filename = comment;
1316 id->ac = ac;
1317 TAILQ_INSERT_TAIL(&agent, id, next);
1318 }
1319 }
1320 /* append remaining agent keys */
1321 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1322 TAILQ_REMOVE(&agent, id, next);
1323 TAILQ_INSERT_TAIL(preferred, id, next);
1324 }
1325 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001326 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001327 /* append remaining keys from the config file */
1328 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1329 TAILQ_REMOVE(&files, id, next);
1330 TAILQ_INSERT_TAIL(preferred, id, next);
1331 }
1332 TAILQ_FOREACH(id, preferred, next) {
1333 debug2("key: %s (%p)", id->filename, id->key);
1334 }
1335}
1336
1337static void
1338pubkey_cleanup(Authctxt *authctxt)
1339{
1340 Identity *id;
1341
1342 if (authctxt->agent != NULL)
1343 ssh_close_authentication_connection(authctxt->agent);
1344 for (id = TAILQ_FIRST(&authctxt->keys); id;
1345 id = TAILQ_FIRST(&authctxt->keys)) {
1346 TAILQ_REMOVE(&authctxt->keys, id, next);
1347 if (id->key)
1348 key_free(id->key);
1349 if (id->filename)
1350 xfree(id->filename);
1351 xfree(id);
1352 }
Damien Millereba71ba2000-04-29 23:57:08 +10001353}
1354
Damien Miller62cee002000-09-23 17:15:56 +11001355int
1356userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001357{
Damien Miller280ecfb2003-05-14 13:46:00 +10001358 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001359 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001360
Damien Miller280ecfb2003-05-14 13:46:00 +10001361 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1362 if (id->tried++)
1363 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001364 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001365 TAILQ_REMOVE(&authctxt->keys, id, next);
1366 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1367 /*
1368 * send a test message if we have the public key. for
1369 * encrypted keys we cannot do this and have to load the
1370 * private key instead
1371 */
1372 if (id->key && id->key->type != KEY_RSA1) {
1373 debug("Offering public key: %s", id->filename);
1374 sent = send_pubkey_test(authctxt, id);
1375 } else if (id->key == NULL) {
1376 debug("Trying private key: %s", id->filename);
1377 id->key = load_identity_file(id->filename);
1378 if (id->key != NULL) {
1379 id->isprivate = 1;
1380 sent = sign_and_send_pubkey(authctxt, id);
1381 key_free(id->key);
1382 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001383 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001384 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001385 if (sent)
1386 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001387 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001388 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001389}
Damien Millereba71ba2000-04-29 23:57:08 +10001390
Damien Miller874d77b2000-10-14 16:23:11 +11001391/*
1392 * Send userauth request message specifying keyboard-interactive method.
1393 */
1394int
1395userauth_kbdint(Authctxt *authctxt)
1396{
1397 static int attempt = 0;
1398
1399 if (attempt++ >= options.number_of_password_prompts)
1400 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001401 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1402 if (attempt > 1 && !authctxt->info_req_seen) {
1403 debug3("userauth_kbdint: disable: no info_req_seen");
1404 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1405 return 0;
1406 }
Damien Miller874d77b2000-10-14 16:23:11 +11001407
1408 debug2("userauth_kbdint");
1409 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1410 packet_put_cstring(authctxt->server_user);
1411 packet_put_cstring(authctxt->service);
1412 packet_put_cstring(authctxt->method->name);
1413 packet_put_cstring(""); /* lang */
1414 packet_put_cstring(options.kbd_interactive_devices ?
1415 options.kbd_interactive_devices : "");
1416 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001417
1418 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1419 return 1;
1420}
1421
1422/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001423 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001424 */
1425void
Damien Miller630d6f42002-01-22 23:17:30 +11001426input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001427{
1428 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001429 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001430 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001431 int echo = 0;
1432
1433 debug2("input_userauth_info_req");
1434
1435 if (authctxt == NULL)
1436 fatal("input_userauth_info_req: no authentication context");
1437
Ben Lindstrom7d199962001-09-12 18:29:00 +00001438 authctxt->info_req_seen = 1;
1439
Damien Miller874d77b2000-10-14 16:23:11 +11001440 name = packet_get_string(NULL);
1441 inst = packet_get_string(NULL);
1442 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001443 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001444 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001445 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001446 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001447 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001448 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001449 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001450
1451 num_prompts = packet_get_int();
1452 /*
1453 * Begin to build info response packet based on prompts requested.
1454 * We commit to providing the correct number of responses, so if
1455 * further on we run into a problem that prevents this, we have to
1456 * be sure and clean this up and send a correct error response.
1457 */
1458 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1459 packet_put_int(num_prompts);
1460
Ben Lindstrom551ea372001-06-05 18:56:16 +00001461 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001462 for (i = 0; i < num_prompts; i++) {
1463 prompt = packet_get_string(NULL);
1464 echo = packet_get_char();
1465
Ben Lindstrom949974b2001-06-25 05:20:31 +00001466 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001467
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001468 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001469 memset(response, 0, strlen(response));
1470 xfree(response);
1471 xfree(prompt);
1472 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001473 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001474
Damien Miller9f643902001-11-12 11:02:52 +11001475 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001476 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001477}
Damien Miller62cee002000-09-23 17:15:56 +11001478
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001479static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001480ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001481 u_char *data, u_int datalen)
1482{
1483 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001484 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001485 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001486 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001487
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001488 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001489
Ben Lindstrom5206b952002-06-06 19:59:29 +00001490 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1491 error("ssh_keysign: no installed: %s", strerror(errno));
1492 return -1;
1493 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001494 if (fflush(stdout) != 0)
1495 error("ssh_keysign: fflush: %s", strerror(errno));
1496 if (pipe(to) < 0) {
1497 error("ssh_keysign: pipe: %s", strerror(errno));
1498 return -1;
1499 }
1500 if (pipe(from) < 0) {
1501 error("ssh_keysign: pipe: %s", strerror(errno));
1502 return -1;
1503 }
1504 if ((pid = fork()) < 0) {
1505 error("ssh_keysign: fork: %s", strerror(errno));
1506 return -1;
1507 }
1508 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001509 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001510 close(from[0]);
1511 if (dup2(from[1], STDOUT_FILENO) < 0)
1512 fatal("ssh_keysign: dup2: %s", strerror(errno));
1513 close(to[1]);
1514 if (dup2(to[0], STDIN_FILENO) < 0)
1515 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001516 close(from[1]);
1517 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001518 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001519 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1520 strerror(errno));
1521 }
1522 close(from[1]);
1523 close(to[0]);
1524
1525 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001526 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001527 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001528 if (ssh_msg_send(to[1], version, &b) == -1)
1529 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001530
Damien Miller901119b2002-10-04 11:10:04 +10001531 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001532 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001533 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001534 return -1;
1535 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001536 close(from[0]);
1537 close(to[1]);
1538
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001539 while (waitpid(pid, &status, 0) < 0)
1540 if (errno != EINTR)
1541 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001542
Ben Lindstrom5206b952002-06-06 19:59:29 +00001543 if (buffer_get_char(&b) != version) {
1544 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001545 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001546 return -1;
1547 }
1548 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001549 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001550
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001551 return 0;
1552}
1553
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001554int
1555userauth_hostbased(Authctxt *authctxt)
1556{
1557 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001558 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001559 Buffer b;
1560 u_char *signature, *blob;
Darren Tucker26c66622007-05-20 15:09:42 +10001561 char *chost, *pkalg, *p, myname[NI_MAXHOST];
Ben Lindstrom671388f2001-04-19 20:40:45 +00001562 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001563 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001564 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001565
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001566 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001567 for (i = 0; i < sensitive->nkeys; i++) {
1568 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001569 if (private && private->type != KEY_RSA1) {
1570 found = 1;
1571 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001572 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001573 break;
1574 }
1575 }
1576 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001577 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001578 return 0;
1579 }
1580 if (key_to_blob(private, &blob, &blen) == 0) {
1581 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001582 return 0;
1583 }
Damien Miller91c18472001-11-12 11:02:03 +11001584 /* figure out a name for the client host */
Darren Tucker26c66622007-05-20 15:09:42 +10001585 p = NULL;
1586 if (packet_connection_is_on_socket())
1587 p = get_local_name(packet_get_connection_in());
1588 if (p == NULL) {
1589 if (gethostname(myname, sizeof(myname)) == -1) {
1590 verbose("userauth_hostbased: gethostname: %s",
1591 strerror(errno));
1592 } else
1593 p = xstrdup(myname);
1594 }
Damien Miller91c18472001-11-12 11:02:03 +11001595 if (p == NULL) {
1596 error("userauth_hostbased: cannot get local ipaddr/name");
1597 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001598 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001599 return 0;
1600 }
1601 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001602 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001603 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001604 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001605
Ben Lindstrom671388f2001-04-19 20:40:45 +00001606 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1607 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001608 pkalg = xstrdup(key_ssh_name(private));
1609 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001610 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001611 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001612 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1613 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001614 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001615 buffer_put_cstring(&b, authctxt->method->name);
1616 buffer_put_cstring(&b, pkalg);
1617 buffer_put_string(&b, blob, blen);
1618 buffer_put_cstring(&b, chost);
1619 buffer_put_cstring(&b, authctxt->local_user);
1620#ifdef DEBUG_PK
1621 buffer_dump(&b);
1622#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001623 if (sensitive->external_keysign)
1624 ok = ssh_keysign(private, &signature, &slen,
1625 buffer_ptr(&b), buffer_len(&b));
1626 else
1627 ok = key_sign(private, &signature, &slen,
1628 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001629 key_free(private);
1630 buffer_free(&b);
1631 if (ok != 0) {
1632 error("key_sign failed");
1633 xfree(chost);
1634 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001635 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001636 return 0;
1637 }
1638 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1639 packet_put_cstring(authctxt->server_user);
1640 packet_put_cstring(authctxt->service);
1641 packet_put_cstring(authctxt->method->name);
1642 packet_put_cstring(pkalg);
1643 packet_put_string(blob, blen);
1644 packet_put_cstring(chost);
1645 packet_put_cstring(authctxt->local_user);
1646 packet_put_string(signature, slen);
1647 memset(signature, 's', slen);
1648 xfree(signature);
1649 xfree(chost);
1650 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001651 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001652
1653 packet_send();
1654 return 1;
1655}
1656
Damien Miller01ed2272008-11-05 16:20:46 +11001657#ifdef JPAKE
1658int
1659userauth_jpake(Authctxt *authctxt)
1660{
1661 struct jpake_ctx *pctx;
1662 u_char *x1_proof, *x2_proof;
1663 u_int x1_proof_len, x2_proof_len;
1664 static int attempt = 0; /* XXX share with userauth_password's? */
1665
1666 if (attempt++ >= options.number_of_password_prompts)
1667 return 0;
1668 if (attempt != 1)
1669 error("Permission denied, please try again.");
1670
1671 if (authctxt->methoddata != NULL)
1672 fatal("%s: authctxt->methoddata already set (%p)",
1673 __func__, authctxt->methoddata);
1674
1675 authctxt->methoddata = pctx = jpake_new();
1676
1677 /*
1678 * Send request immediately, to get the protocol going while
1679 * we do the initial computations.
1680 */
1681 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1682 packet_put_cstring(authctxt->server_user);
1683 packet_put_cstring(authctxt->service);
1684 packet_put_cstring(authctxt->method->name);
1685 packet_send();
1686 packet_write_wait();
1687
1688 jpake_step1(pctx->grp,
1689 &pctx->client_id, &pctx->client_id_len,
1690 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1691 &x1_proof, &x1_proof_len,
1692 &x2_proof, &x2_proof_len);
1693
1694 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1695
1696 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1697 packet_put_string(pctx->client_id, pctx->client_id_len);
1698 packet_put_bignum2(pctx->g_x1);
1699 packet_put_bignum2(pctx->g_x2);
1700 packet_put_string(x1_proof, x1_proof_len);
1701 packet_put_string(x2_proof, x2_proof_len);
1702 packet_send();
1703
1704 bzero(x1_proof, x1_proof_len);
1705 bzero(x2_proof, x2_proof_len);
1706 xfree(x1_proof);
1707 xfree(x2_proof);
1708
1709 /* Expect step 1 packet from peer */
1710 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1711 input_userauth_jpake_server_step1);
1712
1713 return 1;
1714}
1715
1716void
1717userauth_jpake_cleanup(Authctxt *authctxt)
1718{
1719 debug3("%s: clean up", __func__);
1720 if (authctxt->methoddata != NULL) {
1721 jpake_free(authctxt->methoddata);
1722 authctxt->methoddata = NULL;
1723 }
1724}
1725#endif /* JPAKE */
1726
Damien Miller62cee002000-09-23 17:15:56 +11001727/* find auth method */
1728
Damien Miller62cee002000-09-23 17:15:56 +11001729/*
1730 * given auth method name, if configurable options permit this method fill
1731 * in auth_ident field and return true, otherwise return false.
1732 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001733static int
Damien Miller62cee002000-09-23 17:15:56 +11001734authmethod_is_enabled(Authmethod *method)
1735{
1736 if (method == NULL)
1737 return 0;
1738 /* return false if options indicate this method is disabled */
1739 if (method->enabled == NULL || *method->enabled == 0)
1740 return 0;
1741 /* return false if batch mode is enabled but method needs interactive mode */
1742 if (method->batch_flag != NULL && *method->batch_flag != 0)
1743 return 0;
1744 return 1;
1745}
1746
Ben Lindstrombba81212001-06-25 05:01:22 +00001747static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001748authmethod_lookup(const char *name)
1749{
1750 Authmethod *method = NULL;
1751 if (name != NULL)
1752 for (method = authmethods; method->name != NULL; method++)
1753 if (strcmp(name, method->name) == 0)
1754 return method;
1755 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1756 return NULL;
1757}
1758
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001759/* XXX internal state */
1760static Authmethod *current = NULL;
1761static char *supported = NULL;
1762static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001763
Damien Miller62cee002000-09-23 17:15:56 +11001764/*
1765 * Given the authentication method list sent by the server, return the
1766 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001767 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001768 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001769static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001770authmethod_get(char *authlist)
1771{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001772 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001773 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001774
Damien Miller62cee002000-09-23 17:15:56 +11001775 /* Use a suitable default if we're passed a nil list. */
1776 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001777 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001778
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001779 if (supported == NULL || strcmp(authlist, supported) != 0) {
1780 debug3("start over, passed a different list %s", authlist);
1781 if (supported != NULL)
1782 xfree(supported);
1783 supported = xstrdup(authlist);
1784 preferred = options.preferred_authentications;
1785 debug3("preferred %s", preferred);
1786 current = NULL;
1787 } else if (current != NULL && authmethod_is_enabled(current))
1788 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001789
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001790 for (;;) {
1791 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001792 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001793 current = NULL;
1794 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001795 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001796 preferred += next;
1797 debug3("authmethod_lookup %s", name);
1798 debug3("remaining preferred: %s", preferred);
1799 if ((current = authmethod_lookup(name)) != NULL &&
1800 authmethod_is_enabled(current)) {
1801 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001802 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001803 return current;
1804 }
Damien Millereba71ba2000-04-29 23:57:08 +10001805 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001806}
Damien Miller62cee002000-09-23 17:15:56 +11001807
Ben Lindstrom79073822001-07-04 03:42:30 +00001808static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001809authmethods_get(void)
1810{
1811 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001812 Buffer b;
1813 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001814
Damien Miller0e3b8722002-01-22 23:26:38 +11001815 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001816 for (method = authmethods; method->name != NULL; method++) {
1817 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001818 if (buffer_len(&b) > 0)
1819 buffer_append(&b, ",", 1);
1820 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001821 }
Damien Miller62cee002000-09-23 17:15:56 +11001822 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001823 buffer_append(&b, "\0", 1);
1824 list = xstrdup(buffer_ptr(&b));
1825 buffer_free(&b);
1826 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001827}
Damien Miller01ed2272008-11-05 16:20:46 +11001828