blob: a762eec3bdb4775e77f29f8902033f7e275d7cac [file] [log] [blame]
Damien Miller01ed2272008-11-05 16:20:46 +11001/* $OpenBSD: sshconnect2.c,v 1.170 2008/11/04 08:22:13 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 Miller01ed2272008-11-05 16:20:46 +110071#include "jpake.h"
Damien Miller874d77b2000-10-14 16:23:11 +110072
Darren Tucker0efd1552003-08-26 11:49:55 +100073#ifdef GSSAPI
74#include "ssh-gss.h"
75#endif
76
Damien Millereba71ba2000-04-29 23:57:08 +100077/* import */
78extern char *client_version_string;
79extern char *server_version_string;
80extern Options options;
81
82/*
83 * SSH2 key exchange
84 */
85
Ben Lindstrom46c16222000-12-22 01:43:59 +000086u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100087u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100088
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000089char *xxx_host;
90struct sockaddr *xxx_hostaddr;
91
Ben Lindstromf28f6342001-04-04 02:03:04 +000092Kex *xxx_kex = NULL;
93
Ben Lindstrombba81212001-06-25 05:01:22 +000094static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000095verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000096{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100098 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099 return 0;
100}
101
Damien Millereba71ba2000-04-29 23:57:08 +1000102void
Damien Miller874d77b2000-10-14 16:23:11 +1100103ssh_kex2(char *host, struct sockaddr *hostaddr)
104{
Damien Miller874d77b2000-10-14 16:23:11 +1100105 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000106
107 xxx_host = host;
108 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100109
Damien Millere39cacc2000-11-29 12:18:44 +1100110 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000111 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100112 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100113 }
114 if (options.ciphers != NULL) {
115 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
116 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
117 }
Damien Millera0ff4662001-03-30 10:49:35 +1000118 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
119 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
120 myproposal[PROPOSAL_ENC_ALGS_STOC] =
121 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100122 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000123 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000124 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100125 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000126 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000127 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100128 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000129 if (options.macs != NULL) {
130 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
131 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
132 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000133 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100134 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000135 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100136
Damien Millera5539d22003-04-09 20:50:06 +1000137 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100138 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000139
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000140 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000141 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100142 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000143 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100144 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100145 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000146 kex->client_version_string=client_version_string;
147 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000148 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100149
Ben Lindstromf28f6342001-04-04 02:03:04 +0000150 xxx_kex = kex;
151
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000152 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100153
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000154 session_id2 = kex->session_id;
155 session_id2_len = kex->session_id_len;
156
Damien Miller874d77b2000-10-14 16:23:11 +1100157#ifdef DEBUG_KEXDH
158 /* send 1st encrypted/maced/compressed message */
159 packet_start(SSH2_MSG_IGNORE);
160 packet_put_cstring("markus");
161 packet_send();
162 packet_write_wait();
163#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000164}
Damien Millerb1715dc2000-05-30 13:44:51 +1000165
Damien Millereba71ba2000-04-29 23:57:08 +1000166/*
167 * Authenticate user
168 */
Damien Miller62cee002000-09-23 17:15:56 +1100169
170typedef struct Authctxt Authctxt;
171typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000172typedef struct identity Identity;
173typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100174
Damien Miller280ecfb2003-05-14 13:46:00 +1000175struct identity {
176 TAILQ_ENTRY(identity) next;
177 AuthenticationConnection *ac; /* set if agent supports key */
178 Key *key; /* public/private key */
179 char *filename; /* comment for agent-only keys */
180 int tried;
181 int isprivate; /* key points to the private key */
182};
183TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100184
185struct Authctxt {
186 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000187 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100188 const char *host;
189 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100190 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100191 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000192 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000193 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000194 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000195 AuthenticationConnection *agent;
196 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000197 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000198 /* kbd-interactive */
199 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000200 /* generic */
201 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100202};
203struct Authmethod {
204 char *name; /* string to compare against server's list */
205 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100206 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100207 int *enabled; /* flag in option struct that enables method */
208 int *batch_flag; /* flag in option struct that disables method */
209};
210
Damien Miller630d6f42002-01-22 23:17:30 +1100211void input_userauth_success(int, u_int32_t, void *);
212void input_userauth_failure(int, u_int32_t, void *);
213void input_userauth_banner(int, u_int32_t, void *);
214void input_userauth_error(int, u_int32_t, void *);
215void input_userauth_info_req(int, u_int32_t, void *);
216void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000217void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100218void input_userauth_jpake_server_step1(int, u_int32_t, void *);
219void input_userauth_jpake_server_step2(int, u_int32_t, void *);
220void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100221
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000222int userauth_none(Authctxt *);
223int userauth_pubkey(Authctxt *);
224int userauth_passwd(Authctxt *);
225int userauth_kbdint(Authctxt *);
226int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100227int userauth_jpake(Authctxt *);
228
229void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100230
Darren Tucker0efd1552003-08-26 11:49:55 +1000231#ifdef GSSAPI
232int userauth_gssapi(Authctxt *authctxt);
233void input_gssapi_response(int type, u_int32_t, void *);
234void input_gssapi_token(int type, u_int32_t, void *);
235void input_gssapi_hash(int type, u_int32_t, void *);
236void input_gssapi_error(int, u_int32_t, void *);
237void input_gssapi_errtok(int, u_int32_t, void *);
238#endif
239
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000240void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000241
Damien Miller280ecfb2003-05-14 13:46:00 +1000242static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000243static void pubkey_prepare(Authctxt *);
244static void pubkey_cleanup(Authctxt *);
245static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static Authmethod *authmethod_get(char *authlist);
248static Authmethod *authmethod_lookup(const char *name);
249static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100250
251Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000252#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100253 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000254 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100255 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000256 &options.gss_authentication,
257 NULL},
258#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000259 {"hostbased",
260 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100261 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000262 &options.hostbased_authentication,
263 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000264 {"publickey",
265 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100266 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000267 &options.pubkey_authentication,
268 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100269#ifdef JPAKE
270 {"jpake-01@openssh.com",
271 userauth_jpake,
272 userauth_jpake_cleanup,
273 &options.zero_knowledge_password_authentication,
274 &options.batch_mode},
275#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100276 {"keyboard-interactive",
277 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100278 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100279 &options.kbd_interactive_authentication,
280 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000281 {"password",
282 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100283 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000284 &options.password_authentication,
285 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100286 {"none",
287 userauth_none,
288 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100289 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100290 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100291 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100292};
293
294void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000295ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000296 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100297{
298 Authctxt authctxt;
299 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100300
Ben Lindstrom551ea372001-06-05 18:56:16 +0000301 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000302 options.kbd_interactive_authentication = 1;
303
Damien Miller62cee002000-09-23 17:15:56 +1100304 packet_start(SSH2_MSG_SERVICE_REQUEST);
305 packet_put_cstring("ssh-userauth");
306 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000307 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100308 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100309 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000310 if (type != SSH2_MSG_SERVICE_ACCEPT)
311 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100312 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100313 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000314 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100315 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100316 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000317 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100318 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100319 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000320 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100321
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000322 if (options.preferred_authentications == NULL)
323 options.preferred_authentications = authmethods_get();
324
Damien Miller62cee002000-09-23 17:15:56 +1100325 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000326 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000327 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100328 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000329 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100330 authctxt.host = host;
331 authctxt.service = "ssh-connection"; /* service name */
332 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100333 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000334 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000335 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000336 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000337 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100338 if (authctxt.method == NULL)
339 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100340
341 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100342 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100343
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000344 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100345 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
346 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000347 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100348 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
349
Damien Miller280ecfb2003-05-14 13:46:00 +1000350 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000351 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
352
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000353 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100354}
Damien Millerf842fcb2003-05-15 12:01:28 +1000355
Damien Miller62cee002000-09-23 17:15:56 +1100356void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000357userauth(Authctxt *authctxt, char *authlist)
358{
Damien Miller01ed2272008-11-05 16:20:46 +1100359 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
360 authctxt->method->cleanup(authctxt);
361
Darren Tucker0efd1552003-08-26 11:49:55 +1000362 if (authctxt->methoddata) {
363 xfree(authctxt->methoddata);
364 authctxt->methoddata = NULL;
365 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000366 if (authlist == NULL) {
367 authlist = authctxt->authlist;
368 } else {
369 if (authctxt->authlist)
370 xfree(authctxt->authlist);
371 authctxt->authlist = authlist;
372 }
373 for (;;) {
374 Authmethod *method = authmethod_get(authlist);
375 if (method == NULL)
376 fatal("Permission denied (%s).", authlist);
377 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000378
379 /* reset the per method handler */
380 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
381 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
382
383 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000384 if (method->userauth(authctxt) != 0) {
385 debug2("we sent a %s packet, wait for reply", method->name);
386 break;
387 } else {
388 debug2("we did not send a packet, disable method");
389 method->enabled = NULL;
390 }
391 }
392}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000393
Damien Millerf7475d72008-11-03 19:26:18 +1100394/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000395void
Damien Miller630d6f42002-01-22 23:17:30 +1100396input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100397{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000398 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000399 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000400}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000401
Damien Millerf7475d72008-11-03 19:26:18 +1100402/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000403void
Damien Miller630d6f42002-01-22 23:17:30 +1100404input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000405{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000406 char *msg, *raw, *lang;
407 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000408
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000409 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000410 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000411 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100412 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000413 if (len > 65536)
414 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100415 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
416 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
Darren Tucker046dff22003-10-08 17:32:02 +1000417 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000418 xfree(msg);
419 }
420 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000421 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100422}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000423
Damien Millerf7475d72008-11-03 19:26:18 +1100424/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100425void
Damien Miller630d6f42002-01-22 23:17:30 +1100426input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100427{
428 Authctxt *authctxt = ctxt;
429 if (authctxt == NULL)
430 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000431 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000432 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000433 authctxt->authlist = NULL;
434 }
435 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000436 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000437 authctxt->methoddata = NULL;
438 }
Damien Miller62cee002000-09-23 17:15:56 +1100439 authctxt->success = 1; /* break out */
440}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000441
Damien Millerf7475d72008-11-03 19:26:18 +1100442/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100443void
Damien Miller630d6f42002-01-22 23:17:30 +1100444input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100445{
Damien Miller62cee002000-09-23 17:15:56 +1100446 Authctxt *authctxt = ctxt;
447 char *authlist = NULL;
448 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100449
450 if (authctxt == NULL)
451 fatal("input_userauth_failure: no authentication context");
452
Damien Miller874d77b2000-10-14 16:23:11 +1100453 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100454 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100455 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100456
457 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000458 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000459 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100460
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000461 userauth(authctxt, authlist);
462}
Damien Millerf7475d72008-11-03 19:26:18 +1100463
464/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000465void
Damien Miller630d6f42002-01-22 23:17:30 +1100466input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000467{
468 Authctxt *authctxt = ctxt;
469 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000470 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000471 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000472 int pktype, sent = 0;
473 u_int alen, blen;
474 char *pkalg, *fp;
475 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000476
477 if (authctxt == NULL)
478 fatal("input_userauth_pk_ok: no authentication context");
479 if (datafellows & SSH_BUG_PKOK) {
480 /* this is similar to SSH_BUG_PKAUTH */
481 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
482 pkblob = packet_get_string(&blen);
483 buffer_init(&b);
484 buffer_append(&b, pkblob, blen);
485 pkalg = buffer_get_string(&b, &alen);
486 buffer_free(&b);
487 } else {
488 pkalg = packet_get_string(&alen);
489 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000490 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100491 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000492
Damien Miller280ecfb2003-05-14 13:46:00 +1000493 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000494
Damien Miller280ecfb2003-05-14 13:46:00 +1000495 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
496 debug("unknown pkalg %s", pkalg);
497 goto done;
498 }
499 if ((key = key_from_blob(pkblob, blen)) == NULL) {
500 debug("no key from blob. pkalg %s", pkalg);
501 goto done;
502 }
503 if (key->type != pktype) {
504 error("input_userauth_pk_ok: type mismatch "
505 "for decoded key (received %d, expected %d)",
506 key->type, pktype);
507 goto done;
508 }
509 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
510 debug2("input_userauth_pk_ok: fp %s", fp);
511 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000512
Darren Tuckerd05b6012003-10-15 15:55:59 +1000513 /*
514 * search keys in the reverse order, because last candidate has been
515 * moved to the end of the queue. this also avoids confusion by
516 * duplicate keys
517 */
Damien Miller0b51a522004-04-20 20:07:19 +1000518 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000519 if (key_equal(key, id->key)) {
520 sent = sign_and_send_pubkey(authctxt, id);
521 break;
522 }
523 }
524done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000525 if (key != NULL)
526 key_free(key);
527 xfree(pkalg);
528 xfree(pkblob);
529
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000530 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000531 if (sent == 0)
532 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100533}
534
Darren Tucker0efd1552003-08-26 11:49:55 +1000535#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100536int
Darren Tucker0efd1552003-08-26 11:49:55 +1000537userauth_gssapi(Authctxt *authctxt)
538{
539 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100540 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000541 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000542 OM_uint32 min;
543 int ok = 0;
544
545 /* Try one GSSAPI method at a time, rather than sending them all at
546 * once. */
547
Darren Tucker56afe142003-11-03 20:06:14 +1100548 if (gss_supported == NULL)
549 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000550
551 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100552 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000553 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100554 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000555 ssh_gssapi_check_mechanism(&gssctxt,
556 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000557 ok = 1; /* Mechanism works */
558 } else {
559 mech++;
560 }
561 }
562
Damien Millera1cb9f32006-08-19 00:33:34 +1000563 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000564 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000565
566 authctxt->methoddata=(void *)gssctxt;
567
568 packet_start(SSH2_MSG_USERAUTH_REQUEST);
569 packet_put_cstring(authctxt->server_user);
570 packet_put_cstring(authctxt->service);
571 packet_put_cstring(authctxt->method->name);
572
573 packet_put_int(1);
574
Darren Tucker655a5e02003-11-03 20:09:03 +1100575 packet_put_int((gss_supported->elements[mech].length) + 2);
576 packet_put_char(SSH_GSS_OIDTYPE);
577 packet_put_char(gss_supported->elements[mech].length);
578 packet_put_raw(gss_supported->elements[mech].elements,
579 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000580
581 packet_send();
582
583 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
587
588 mech++; /* Move along to next candidate */
589
590 return 1;
591}
592
Damien Miller91c6aa42003-11-17 21:20:18 +1100593static OM_uint32
594process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
595{
596 Authctxt *authctxt = ctxt;
597 Gssctxt *gssctxt = authctxt->methoddata;
598 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000599 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
600 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100601 OM_uint32 status, ms, flags;
602 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100603
Damien Miller91c6aa42003-11-17 21:20:18 +1100604 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100605 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100606
607 if (send_tok.length > 0) {
608 if (GSS_ERROR(status))
609 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
610 else
611 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100612
Damien Miller91c6aa42003-11-17 21:20:18 +1100613 packet_put_string(send_tok.value, send_tok.length);
614 packet_send();
615 gss_release_buffer(&ms, &send_tok);
616 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100617
Damien Miller91c6aa42003-11-17 21:20:18 +1100618 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100619 /* send either complete or MIC, depending on mechanism */
620 if (!(flags & GSS_C_INTEG_FLAG)) {
621 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
622 packet_send();
623 } else {
624 ssh_gssapi_buildmic(&b, authctxt->server_user,
625 authctxt->service, "gssapi-with-mic");
626
627 gssbuf.value = buffer_ptr(&b);
628 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100629
Damien Miller0425d402003-11-17 22:18:21 +1100630 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100631
Damien Miller0425d402003-11-17 22:18:21 +1100632 if (!GSS_ERROR(status)) {
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
634 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100635
Damien Miller0425d402003-11-17 22:18:21 +1100636 packet_send();
637 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100638
Damien Miller0425d402003-11-17 22:18:21 +1100639 buffer_free(&b);
640 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100641 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100642 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100643
Damien Miller91c6aa42003-11-17 21:20:18 +1100644 return status;
645}
646
Damien Millerf7475d72008-11-03 19:26:18 +1100647/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000648void
649input_gssapi_response(int type, u_int32_t plen, void *ctxt)
650{
651 Authctxt *authctxt = ctxt;
652 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000653 int oidlen;
654 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000655
656 if (authctxt == NULL)
657 fatal("input_gssapi_response: no authentication context");
658 gssctxt = authctxt->methoddata;
659
660 /* Setup our OID */
661 oidv = packet_get_string(&oidlen);
662
Darren Tucker655a5e02003-11-03 20:09:03 +1100663 if (oidlen <= 2 ||
664 oidv[0] != SSH_GSS_OIDTYPE ||
665 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100666 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100667 debug("Badly encoded mechanism OID received");
668 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100669 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000670 }
671
Darren Tucker655a5e02003-11-03 20:09:03 +1100672 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
673 fatal("Server returned different OID than expected");
674
Darren Tucker0efd1552003-08-26 11:49:55 +1000675 packet_check_eom();
676
677 xfree(oidv);
678
Damien Miller91c6aa42003-11-17 21:20:18 +1100679 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000680 /* Start again with next method on list */
681 debug("Trying to start again");
682 userauth(authctxt, NULL);
683 return;
684 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000685}
686
Damien Millerf7475d72008-11-03 19:26:18 +1100687/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000688void
689input_gssapi_token(int type, u_int32_t plen, void *ctxt)
690{
691 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000692 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100693 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000694 u_int slen;
695
696 if (authctxt == NULL)
697 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000698
699 recv_tok.value = packet_get_string(&slen);
700 recv_tok.length = slen; /* safe typecast */
701
702 packet_check_eom();
703
Damien Miller91c6aa42003-11-17 21:20:18 +1100704 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000705
706 xfree(recv_tok.value);
707
708 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000709 /* Start again with the next method in the list */
710 userauth(authctxt, NULL);
711 return;
712 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000713}
714
Damien Millerf7475d72008-11-03 19:26:18 +1100715/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000716void
717input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
718{
719 Authctxt *authctxt = ctxt;
720 Gssctxt *gssctxt;
721 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
722 gss_buffer_desc recv_tok;
723 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000724 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000725
726 if (authctxt == NULL)
727 fatal("input_gssapi_response: no authentication context");
728 gssctxt = authctxt->methoddata;
729
Darren Tucker600ad8d2003-08-26 12:10:48 +1000730 recv_tok.value = packet_get_string(&len);
731 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000732
733 packet_check_eom();
734
735 /* Stick it into GSSAPI and see what it says */
736 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000737 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000738
739 xfree(recv_tok.value);
740 gss_release_buffer(&ms, &send_tok);
741
742 /* Server will be returning a failed packet after this one */
743}
744
Damien Millerf7475d72008-11-03 19:26:18 +1100745/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000746void
747input_gssapi_error(int type, u_int32_t plen, void *ctxt)
748{
749 OM_uint32 maj, min;
750 char *msg;
751 char *lang;
752
753 maj=packet_get_int();
754 min=packet_get_int();
755 msg=packet_get_string(NULL);
756 lang=packet_get_string(NULL);
757
758 packet_check_eom();
759
Damien Miller15d72a02005-11-05 15:07:33 +1100760 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000761 xfree(msg);
762 xfree(lang);
763}
764#endif /* GSSAPI */
765
Damien Millereba71ba2000-04-29 23:57:08 +1000766int
Damien Miller874d77b2000-10-14 16:23:11 +1100767userauth_none(Authctxt *authctxt)
768{
769 /* initial userauth request */
770 packet_start(SSH2_MSG_USERAUTH_REQUEST);
771 packet_put_cstring(authctxt->server_user);
772 packet_put_cstring(authctxt->service);
773 packet_put_cstring(authctxt->method->name);
774 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100775 return 1;
776}
777
778int
Damien Miller62cee002000-09-23 17:15:56 +1100779userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000780{
Damien Millere247cc42000-05-07 12:03:14 +1000781 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000782 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000783 char *password;
784
Damien Millerd3a18572000-06-07 19:55:44 +1000785 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000786 return 0;
787
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000788 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000789 error("Permission denied, please try again.");
790
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000791 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100792 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000793 password = read_passphrase(prompt, 0);
794 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100795 packet_put_cstring(authctxt->server_user);
796 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100797 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000798 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000799 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000800 memset(password, 0, strlen(password));
801 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100802 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000803 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000804
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000805 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000806 &input_userauth_passwd_changereq);
807
Damien Millereba71ba2000-04-29 23:57:08 +1000808 return 1;
809}
Damien Millerf7475d72008-11-03 19:26:18 +1100810
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000811/*
812 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
813 */
Damien Millerf7475d72008-11-03 19:26:18 +1100814/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000815void
Tim Ricec8549622002-03-31 12:49:38 -0800816input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000817{
818 Authctxt *authctxt = ctxt;
819 char *info, *lang, *password = NULL, *retype = NULL;
820 char prompt[150];
821
822 debug2("input_userauth_passwd_changereq");
823
824 if (authctxt == NULL)
825 fatal("input_userauth_passwd_changereq: "
826 "no authentication context");
827
828 info = packet_get_string(NULL);
829 lang = packet_get_string(NULL);
830 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000831 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000832 xfree(info);
833 xfree(lang);
834 packet_start(SSH2_MSG_USERAUTH_REQUEST);
835 packet_put_cstring(authctxt->server_user);
836 packet_put_cstring(authctxt->service);
837 packet_put_cstring(authctxt->method->name);
838 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000839 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000840 "Enter %.30s@%.128s's old password: ",
841 authctxt->server_user, authctxt->host);
842 password = read_passphrase(prompt, 0);
843 packet_put_cstring(password);
844 memset(password, 0, strlen(password));
845 xfree(password);
846 password = NULL;
847 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000848 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000849 "Enter %.30s@%.128s's new password: ",
850 authctxt->server_user, authctxt->host);
851 password = read_passphrase(prompt, RP_ALLOW_EOF);
852 if (password == NULL) {
853 /* bail out */
854 return;
855 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000856 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000857 "Retype %.30s@%.128s's new password: ",
858 authctxt->server_user, authctxt->host);
859 retype = read_passphrase(prompt, 0);
860 if (strcmp(password, retype) != 0) {
861 memset(password, 0, strlen(password));
862 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000863 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000864 password = NULL;
865 }
866 memset(retype, 0, strlen(retype));
867 xfree(retype);
868 }
869 packet_put_cstring(password);
870 memset(password, 0, strlen(password));
871 xfree(password);
872 packet_add_padding(64);
873 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000874
875 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000876 &input_userauth_passwd_changereq);
877}
Damien Millereba71ba2000-04-29 23:57:08 +1000878
Damien Miller01ed2272008-11-05 16:20:46 +1100879#ifdef JPAKE
880static char *
881pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
882{
883 /* OpenBSD crypt(3) handles all of these */
884 if (strcmp(crypt_scheme, "crypt") == 0 ||
885 strcmp(crypt_scheme, "bcrypt") == 0 ||
886 strcmp(crypt_scheme, "md5crypt") == 0 ||
887 strcmp(crypt_scheme, "crypt-extended") == 0)
888 return xstrdup(crypt(password, salt));
889 error("%s: unsupported password encryption scheme \"%.100s\"",
890 __func__, crypt_scheme);
891 return NULL;
892}
893
894static BIGNUM *
895jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
896 const char *salt)
897{
898 char prompt[256], *password, *crypted;
899 u_char *secret;
900 u_int secret_len;
901 BIGNUM *ret;
902
903 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
904 authctxt->server_user, authctxt->host);
905 password = read_passphrase(prompt, 0);
906
907 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
908 logit("Disabling %s authentication", authctxt->method->name);
909 authctxt->method->enabled = NULL;
910 /* Continue with an empty password to fail gracefully */
911 crypted = xstrdup("");
912 }
913
914#ifdef JPAKE_DEBUG
915 debug3("%s: salt = %s", __func__, salt);
916 debug3("%s: scheme = %s", __func__, crypt_scheme);
917 debug3("%s: crypted = %s", __func__, crypted);
918#endif
919
920 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
921 &secret, &secret_len) != 0)
922 fatal("%s: hash_buffer", __func__);
923
924 bzero(password, strlen(password));
925 bzero(crypted, strlen(crypted));
926 xfree(password);
927 xfree(crypted);
928
929 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
930 fatal("%s: BN_bin2bn (secret)", __func__);
931 bzero(secret, secret_len);
932 xfree(secret);
933
934 return ret;
935}
936
937/* ARGSUSED */
938void
939input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
940{
941 Authctxt *authctxt = ctxt;
942 struct jpake_ctx *pctx = authctxt->methoddata;
943 u_char *x3_proof, *x4_proof, *x2_s_proof;
944 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
945 char *crypt_scheme, *salt;
946
947 /* Disable this message */
948 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
949
950 if ((pctx->g_x3 = BN_new()) == NULL ||
951 (pctx->g_x4 = BN_new()) == NULL)
952 fatal("%s: BN_new", __func__);
953
954 /* Fetch step 1 values */
955 crypt_scheme = packet_get_string(NULL);
956 salt = packet_get_string(NULL);
957 pctx->server_id = packet_get_string(&pctx->server_id_len);
958 packet_get_bignum2(pctx->g_x3);
959 packet_get_bignum2(pctx->g_x4);
960 x3_proof = packet_get_string(&x3_proof_len);
961 x4_proof = packet_get_string(&x4_proof_len);
962 packet_check_eom();
963
964 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
965
966 /* Obtain password and derive secret */
967 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
968 bzero(crypt_scheme, strlen(crypt_scheme));
969 bzero(salt, strlen(salt));
970 xfree(crypt_scheme);
971 xfree(salt);
972 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
973
974 /* Calculate step 2 values */
975 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
976 pctx->g_x3, pctx->g_x4, pctx->x2,
977 pctx->server_id, pctx->server_id_len,
978 pctx->client_id, pctx->client_id_len,
979 x3_proof, x3_proof_len,
980 x4_proof, x4_proof_len,
981 &pctx->a,
982 &x2_s_proof, &x2_s_proof_len);
983
984 bzero(x3_proof, x3_proof_len);
985 bzero(x4_proof, x4_proof_len);
986 xfree(x3_proof);
987 xfree(x4_proof);
988
989 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
990
991 /* Send values for step 2 */
992 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
993 packet_put_bignum2(pctx->a);
994 packet_put_string(x2_s_proof, x2_s_proof_len);
995 packet_send();
996
997 bzero(x2_s_proof, x2_s_proof_len);
998 xfree(x2_s_proof);
999
1000 /* Expect step 2 packet from peer */
1001 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1002 input_userauth_jpake_server_step2);
1003}
1004
1005/* ARGSUSED */
1006void
1007input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1008{
1009 Authctxt *authctxt = ctxt;
1010 struct jpake_ctx *pctx = authctxt->methoddata;
1011 u_char *x4_s_proof;
1012 u_int x4_s_proof_len;
1013
1014 /* Disable this message */
1015 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1016
1017 if ((pctx->b = BN_new()) == NULL)
1018 fatal("%s: BN_new", __func__);
1019
1020 /* Fetch step 2 values */
1021 packet_get_bignum2(pctx->b);
1022 x4_s_proof = packet_get_string(&x4_s_proof_len);
1023 packet_check_eom();
1024
1025 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1026
1027 /* Derive shared key and calculate confirmation hash */
1028 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1029 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1030 pctx->client_id, pctx->client_id_len,
1031 pctx->server_id, pctx->server_id_len,
1032 session_id2, session_id2_len,
1033 x4_s_proof, x4_s_proof_len,
1034 &pctx->k,
1035 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1036
1037 bzero(x4_s_proof, x4_s_proof_len);
1038 xfree(x4_s_proof);
1039
1040 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1041
1042 /* Send key confirmation proof */
1043 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1044 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1045 packet_send();
1046
1047 /* Expect confirmation from peer */
1048 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1049 input_userauth_jpake_server_confirm);
1050}
1051
1052/* ARGSUSED */
1053void
1054input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1055{
1056 Authctxt *authctxt = ctxt;
1057 struct jpake_ctx *pctx = authctxt->methoddata;
1058
1059 /* Disable this message */
1060 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1061
1062 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1063 packet_check_eom();
1064
1065 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1066
1067 /* Verify expected confirmation hash */
1068 if (jpake_check_confirm(pctx->k,
1069 pctx->server_id, pctx->server_id_len,
1070 session_id2, session_id2_len,
1071 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1072 debug("%s: %s success", __func__, authctxt->method->name);
1073 else {
1074 debug("%s: confirmation mismatch", __func__);
1075 /* XXX stash this so if auth succeeds then we can warn/kill */
1076 }
1077
1078 userauth_jpake_cleanup(authctxt);
1079}
1080#endif /* JPAKE */
1081
Ben Lindstrombba81212001-06-25 05:01:22 +00001082static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001083identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1084 u_char *data, u_int datalen)
1085{
1086 Key *prv;
1087 int ret;
1088
1089 /* the agent supports this key */
1090 if (id->ac)
1091 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1092 data, datalen));
1093 /*
1094 * we have already loaded the private key or
1095 * the private key is stored in external hardware
1096 */
1097 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1098 return (key_sign(id->key, sigp, lenp, data, datalen));
1099 /* load the private key from the file */
1100 if ((prv = load_identity_file(id->filename)) == NULL)
1101 return (-1);
1102 ret = key_sign(prv, sigp, lenp, data, datalen);
1103 key_free(prv);
1104 return (ret);
1105}
1106
1107static int
1108sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001109{
1110 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001111 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001112 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001113 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001114 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001115 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001116
Ben Lindstromd121f612000-12-03 17:00:47 +00001117 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001118
Damien Miller280ecfb2003-05-14 13:46:00 +10001119 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001120 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001121 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001122 return 0;
1123 }
Damien Millereba71ba2000-04-29 23:57:08 +10001124 /* data to be signed */
1125 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001126 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001127 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001128 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001129 } else {
1130 buffer_put_string(&b, session_id2, session_id2_len);
1131 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001132 }
Damien Millereba71ba2000-04-29 23:57:08 +10001133 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001134 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001135 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001136 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001137 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001138 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001139 if (datafellows & SSH_BUG_PKAUTH) {
1140 buffer_put_char(&b, have_sig);
1141 } else {
1142 buffer_put_cstring(&b, authctxt->method->name);
1143 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001144 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001145 }
Damien Millereba71ba2000-04-29 23:57:08 +10001146 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001147
1148 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001149 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001150 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001151 if (ret == -1) {
1152 xfree(blob);
1153 buffer_free(&b);
1154 return 0;
1155 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001156#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001157 buffer_dump(&b);
1158#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001159 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001160 buffer_clear(&b);
1161 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001162 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001163 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001164 buffer_put_cstring(&b, authctxt->server_user);
1165 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001166 buffer_put_cstring(&b, authctxt->method->name);
1167 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001168 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001169 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001170 buffer_put_string(&b, blob, bloblen);
1171 }
1172 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001173
Damien Millereba71ba2000-04-29 23:57:08 +10001174 /* append signature */
1175 buffer_put_string(&b, signature, slen);
1176 xfree(signature);
1177
1178 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001179 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001180 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001181 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001182
1183 /* put remaining data from buffer into packet */
1184 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1185 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1186 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001187 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001188
1189 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001190}
1191
Ben Lindstrombba81212001-06-25 05:01:22 +00001192static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001193send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001194{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001195 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001196 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001197
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001198 debug3("send_pubkey_test");
1199
Damien Miller280ecfb2003-05-14 13:46:00 +10001200 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001201 /* we cannot handle this key */
1202 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001203 return 0;
1204 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001205 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001206 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001207
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001208 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1209 packet_put_cstring(authctxt->server_user);
1210 packet_put_cstring(authctxt->service);
1211 packet_put_cstring(authctxt->method->name);
1212 packet_put_char(have_sig);
1213 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001214 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215 packet_put_string(blob, bloblen);
1216 xfree(blob);
1217 packet_send();
1218 return 1;
1219}
1220
Ben Lindstrombba81212001-06-25 05:01:22 +00001221static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001222load_identity_file(char *filename)
1223{
1224 Key *private;
1225 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +10001226 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001227 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001228
Ben Lindstrom329782e2001-03-10 17:08:59 +00001229 if (stat(filename, &st) < 0) {
1230 debug3("no such identity: %s", filename);
1231 return NULL;
1232 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001233 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1234 if (!perm_ok)
1235 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001236 if (private == NULL) {
1237 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001238 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001239 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001240 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001241 for (i = 0; i < options.number_of_password_prompts; i++) {
1242 passphrase = read_passphrase(prompt, 0);
1243 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001244 private = key_load_private_type(KEY_UNSPEC,
1245 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001246 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001247 } else {
1248 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001249 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001250 }
1251 memset(passphrase, 0, strlen(passphrase));
1252 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001253 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001254 break;
1255 debug2("bad passphrase given, try again...");
1256 }
Damien Miller994cf142000-07-21 10:19:44 +10001257 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001258 return private;
1259}
1260
Damien Miller280ecfb2003-05-14 13:46:00 +10001261/*
1262 * try keys in the following order:
1263 * 1. agent keys that are found in the config file
1264 * 2. other agent keys
1265 * 3. keys that are only listed in the config file
1266 */
1267static void
1268pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001269{
Damien Miller280ecfb2003-05-14 13:46:00 +10001270 Identity *id;
1271 Idlist agent, files, *preferred;
1272 Key *key;
1273 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001274 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001275 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001276
Damien Miller280ecfb2003-05-14 13:46:00 +10001277 TAILQ_INIT(&agent); /* keys from the agent */
1278 TAILQ_INIT(&files); /* keys from the config file */
1279 preferred = &authctxt->keys;
1280 TAILQ_INIT(preferred); /* preferred order of keys */
1281
1282 /* list of keys stored in the filesystem */
1283 for (i = 0; i < options.num_identity_files; i++) {
1284 key = options.identity_keys[i];
1285 if (key && key->type == KEY_RSA1)
1286 continue;
1287 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001288 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001289 id->key = key;
1290 id->filename = xstrdup(options.identity_files[i]);
1291 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001292 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001293 /* list of keys supported by the agent */
1294 if ((ac = ssh_get_authentication_connection())) {
1295 for (key = ssh_get_first_identity(ac, &comment, 2);
1296 key != NULL;
1297 key = ssh_get_next_identity(ac, &comment, 2)) {
1298 found = 0;
1299 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001300 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001301 if (key_equal(key, id->key)) {
1302 key_free(key);
1303 xfree(comment);
1304 TAILQ_REMOVE(&files, id, next);
1305 TAILQ_INSERT_TAIL(preferred, id, next);
1306 id->ac = ac;
1307 found = 1;
1308 break;
1309 }
1310 }
Damien Millerbd394c32004-03-08 23:12:36 +11001311 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001312 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001313 id->key = key;
1314 id->filename = comment;
1315 id->ac = ac;
1316 TAILQ_INSERT_TAIL(&agent, id, next);
1317 }
1318 }
1319 /* append remaining agent keys */
1320 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1321 TAILQ_REMOVE(&agent, id, next);
1322 TAILQ_INSERT_TAIL(preferred, id, next);
1323 }
1324 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001325 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001326 /* append remaining keys from the config file */
1327 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1328 TAILQ_REMOVE(&files, id, next);
1329 TAILQ_INSERT_TAIL(preferred, id, next);
1330 }
1331 TAILQ_FOREACH(id, preferred, next) {
1332 debug2("key: %s (%p)", id->filename, id->key);
1333 }
1334}
1335
1336static void
1337pubkey_cleanup(Authctxt *authctxt)
1338{
1339 Identity *id;
1340
1341 if (authctxt->agent != NULL)
1342 ssh_close_authentication_connection(authctxt->agent);
1343 for (id = TAILQ_FIRST(&authctxt->keys); id;
1344 id = TAILQ_FIRST(&authctxt->keys)) {
1345 TAILQ_REMOVE(&authctxt->keys, id, next);
1346 if (id->key)
1347 key_free(id->key);
1348 if (id->filename)
1349 xfree(id->filename);
1350 xfree(id);
1351 }
Damien Millereba71ba2000-04-29 23:57:08 +10001352}
1353
Damien Miller62cee002000-09-23 17:15:56 +11001354int
1355userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001356{
Damien Miller280ecfb2003-05-14 13:46:00 +10001357 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001358 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001359
Damien Miller280ecfb2003-05-14 13:46:00 +10001360 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1361 if (id->tried++)
1362 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001363 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001364 TAILQ_REMOVE(&authctxt->keys, id, next);
1365 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1366 /*
1367 * send a test message if we have the public key. for
1368 * encrypted keys we cannot do this and have to load the
1369 * private key instead
1370 */
1371 if (id->key && id->key->type != KEY_RSA1) {
1372 debug("Offering public key: %s", id->filename);
1373 sent = send_pubkey_test(authctxt, id);
1374 } else if (id->key == NULL) {
1375 debug("Trying private key: %s", id->filename);
1376 id->key = load_identity_file(id->filename);
1377 if (id->key != NULL) {
1378 id->isprivate = 1;
1379 sent = sign_and_send_pubkey(authctxt, id);
1380 key_free(id->key);
1381 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001382 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001383 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001384 if (sent)
1385 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001386 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001387 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001388}
Damien Millereba71ba2000-04-29 23:57:08 +10001389
Damien Miller874d77b2000-10-14 16:23:11 +11001390/*
1391 * Send userauth request message specifying keyboard-interactive method.
1392 */
1393int
1394userauth_kbdint(Authctxt *authctxt)
1395{
1396 static int attempt = 0;
1397
1398 if (attempt++ >= options.number_of_password_prompts)
1399 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001400 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1401 if (attempt > 1 && !authctxt->info_req_seen) {
1402 debug3("userauth_kbdint: disable: no info_req_seen");
1403 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1404 return 0;
1405 }
Damien Miller874d77b2000-10-14 16:23:11 +11001406
1407 debug2("userauth_kbdint");
1408 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1409 packet_put_cstring(authctxt->server_user);
1410 packet_put_cstring(authctxt->service);
1411 packet_put_cstring(authctxt->method->name);
1412 packet_put_cstring(""); /* lang */
1413 packet_put_cstring(options.kbd_interactive_devices ?
1414 options.kbd_interactive_devices : "");
1415 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001416
1417 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1418 return 1;
1419}
1420
1421/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001422 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001423 */
1424void
Damien Miller630d6f42002-01-22 23:17:30 +11001425input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001426{
1427 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001428 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001429 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001430 int echo = 0;
1431
1432 debug2("input_userauth_info_req");
1433
1434 if (authctxt == NULL)
1435 fatal("input_userauth_info_req: no authentication context");
1436
Ben Lindstrom7d199962001-09-12 18:29:00 +00001437 authctxt->info_req_seen = 1;
1438
Damien Miller874d77b2000-10-14 16:23:11 +11001439 name = packet_get_string(NULL);
1440 inst = packet_get_string(NULL);
1441 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001442 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001443 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001444 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001445 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001446 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001447 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001448 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001449
1450 num_prompts = packet_get_int();
1451 /*
1452 * Begin to build info response packet based on prompts requested.
1453 * We commit to providing the correct number of responses, so if
1454 * further on we run into a problem that prevents this, we have to
1455 * be sure and clean this up and send a correct error response.
1456 */
1457 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1458 packet_put_int(num_prompts);
1459
Ben Lindstrom551ea372001-06-05 18:56:16 +00001460 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001461 for (i = 0; i < num_prompts; i++) {
1462 prompt = packet_get_string(NULL);
1463 echo = packet_get_char();
1464
Ben Lindstrom949974b2001-06-25 05:20:31 +00001465 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001466
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001467 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001468 memset(response, 0, strlen(response));
1469 xfree(response);
1470 xfree(prompt);
1471 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001472 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001473
Damien Miller9f643902001-11-12 11:02:52 +11001474 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001475 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001476}
Damien Miller62cee002000-09-23 17:15:56 +11001477
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001478static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001479ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001480 u_char *data, u_int datalen)
1481{
1482 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001483 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001484 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001485 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001486
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001487 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001488
Ben Lindstrom5206b952002-06-06 19:59:29 +00001489 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1490 error("ssh_keysign: no installed: %s", strerror(errno));
1491 return -1;
1492 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001493 if (fflush(stdout) != 0)
1494 error("ssh_keysign: fflush: %s", strerror(errno));
1495 if (pipe(to) < 0) {
1496 error("ssh_keysign: pipe: %s", strerror(errno));
1497 return -1;
1498 }
1499 if (pipe(from) < 0) {
1500 error("ssh_keysign: pipe: %s", strerror(errno));
1501 return -1;
1502 }
1503 if ((pid = fork()) < 0) {
1504 error("ssh_keysign: fork: %s", strerror(errno));
1505 return -1;
1506 }
1507 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001508 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001509 close(from[0]);
1510 if (dup2(from[1], STDOUT_FILENO) < 0)
1511 fatal("ssh_keysign: dup2: %s", strerror(errno));
1512 close(to[1]);
1513 if (dup2(to[0], STDIN_FILENO) < 0)
1514 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001515 close(from[1]);
1516 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001517 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001518 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1519 strerror(errno));
1520 }
1521 close(from[1]);
1522 close(to[0]);
1523
1524 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001525 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001526 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001527 if (ssh_msg_send(to[1], version, &b) == -1)
1528 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001529
Damien Miller901119b2002-10-04 11:10:04 +10001530 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001531 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001532 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001533 return -1;
1534 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001535 close(from[0]);
1536 close(to[1]);
1537
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001538 while (waitpid(pid, &status, 0) < 0)
1539 if (errno != EINTR)
1540 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001541
Ben Lindstrom5206b952002-06-06 19:59:29 +00001542 if (buffer_get_char(&b) != version) {
1543 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001544 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001545 return -1;
1546 }
1547 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001548 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001549
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001550 return 0;
1551}
1552
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001553int
1554userauth_hostbased(Authctxt *authctxt)
1555{
1556 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001557 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001558 Buffer b;
1559 u_char *signature, *blob;
Darren Tucker26c66622007-05-20 15:09:42 +10001560 char *chost, *pkalg, *p, myname[NI_MAXHOST];
Ben Lindstrom671388f2001-04-19 20:40:45 +00001561 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001562 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001563 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001564
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001565 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001566 for (i = 0; i < sensitive->nkeys; i++) {
1567 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001568 if (private && private->type != KEY_RSA1) {
1569 found = 1;
1570 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001571 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001572 break;
1573 }
1574 }
1575 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001576 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001577 return 0;
1578 }
1579 if (key_to_blob(private, &blob, &blen) == 0) {
1580 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001581 return 0;
1582 }
Damien Miller91c18472001-11-12 11:02:03 +11001583 /* figure out a name for the client host */
Darren Tucker26c66622007-05-20 15:09:42 +10001584 p = NULL;
1585 if (packet_connection_is_on_socket())
1586 p = get_local_name(packet_get_connection_in());
1587 if (p == NULL) {
1588 if (gethostname(myname, sizeof(myname)) == -1) {
1589 verbose("userauth_hostbased: gethostname: %s",
1590 strerror(errno));
1591 } else
1592 p = xstrdup(myname);
1593 }
Damien Miller91c18472001-11-12 11:02:03 +11001594 if (p == NULL) {
1595 error("userauth_hostbased: cannot get local ipaddr/name");
1596 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001597 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001598 return 0;
1599 }
1600 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001601 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001602 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001603 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001604
Ben Lindstrom671388f2001-04-19 20:40:45 +00001605 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1606 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001607 pkalg = xstrdup(key_ssh_name(private));
1608 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001609 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001610 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001611 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1612 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001613 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001614 buffer_put_cstring(&b, authctxt->method->name);
1615 buffer_put_cstring(&b, pkalg);
1616 buffer_put_string(&b, blob, blen);
1617 buffer_put_cstring(&b, chost);
1618 buffer_put_cstring(&b, authctxt->local_user);
1619#ifdef DEBUG_PK
1620 buffer_dump(&b);
1621#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001622 if (sensitive->external_keysign)
1623 ok = ssh_keysign(private, &signature, &slen,
1624 buffer_ptr(&b), buffer_len(&b));
1625 else
1626 ok = key_sign(private, &signature, &slen,
1627 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001628 key_free(private);
1629 buffer_free(&b);
1630 if (ok != 0) {
1631 error("key_sign failed");
1632 xfree(chost);
1633 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001634 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001635 return 0;
1636 }
1637 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1638 packet_put_cstring(authctxt->server_user);
1639 packet_put_cstring(authctxt->service);
1640 packet_put_cstring(authctxt->method->name);
1641 packet_put_cstring(pkalg);
1642 packet_put_string(blob, blen);
1643 packet_put_cstring(chost);
1644 packet_put_cstring(authctxt->local_user);
1645 packet_put_string(signature, slen);
1646 memset(signature, 's', slen);
1647 xfree(signature);
1648 xfree(chost);
1649 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001650 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001651
1652 packet_send();
1653 return 1;
1654}
1655
Damien Miller01ed2272008-11-05 16:20:46 +11001656#ifdef JPAKE
1657int
1658userauth_jpake(Authctxt *authctxt)
1659{
1660 struct jpake_ctx *pctx;
1661 u_char *x1_proof, *x2_proof;
1662 u_int x1_proof_len, x2_proof_len;
1663 static int attempt = 0; /* XXX share with userauth_password's? */
1664
1665 if (attempt++ >= options.number_of_password_prompts)
1666 return 0;
1667 if (attempt != 1)
1668 error("Permission denied, please try again.");
1669
1670 if (authctxt->methoddata != NULL)
1671 fatal("%s: authctxt->methoddata already set (%p)",
1672 __func__, authctxt->methoddata);
1673
1674 authctxt->methoddata = pctx = jpake_new();
1675
1676 /*
1677 * Send request immediately, to get the protocol going while
1678 * we do the initial computations.
1679 */
1680 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1681 packet_put_cstring(authctxt->server_user);
1682 packet_put_cstring(authctxt->service);
1683 packet_put_cstring(authctxt->method->name);
1684 packet_send();
1685 packet_write_wait();
1686
1687 jpake_step1(pctx->grp,
1688 &pctx->client_id, &pctx->client_id_len,
1689 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1690 &x1_proof, &x1_proof_len,
1691 &x2_proof, &x2_proof_len);
1692
1693 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1694
1695 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1696 packet_put_string(pctx->client_id, pctx->client_id_len);
1697 packet_put_bignum2(pctx->g_x1);
1698 packet_put_bignum2(pctx->g_x2);
1699 packet_put_string(x1_proof, x1_proof_len);
1700 packet_put_string(x2_proof, x2_proof_len);
1701 packet_send();
1702
1703 bzero(x1_proof, x1_proof_len);
1704 bzero(x2_proof, x2_proof_len);
1705 xfree(x1_proof);
1706 xfree(x2_proof);
1707
1708 /* Expect step 1 packet from peer */
1709 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1710 input_userauth_jpake_server_step1);
1711
1712 return 1;
1713}
1714
1715void
1716userauth_jpake_cleanup(Authctxt *authctxt)
1717{
1718 debug3("%s: clean up", __func__);
1719 if (authctxt->methoddata != NULL) {
1720 jpake_free(authctxt->methoddata);
1721 authctxt->methoddata = NULL;
1722 }
1723}
1724#endif /* JPAKE */
1725
Damien Miller62cee002000-09-23 17:15:56 +11001726/* find auth method */
1727
Damien Miller62cee002000-09-23 17:15:56 +11001728/*
1729 * given auth method name, if configurable options permit this method fill
1730 * in auth_ident field and return true, otherwise return false.
1731 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001732static int
Damien Miller62cee002000-09-23 17:15:56 +11001733authmethod_is_enabled(Authmethod *method)
1734{
1735 if (method == NULL)
1736 return 0;
1737 /* return false if options indicate this method is disabled */
1738 if (method->enabled == NULL || *method->enabled == 0)
1739 return 0;
1740 /* return false if batch mode is enabled but method needs interactive mode */
1741 if (method->batch_flag != NULL && *method->batch_flag != 0)
1742 return 0;
1743 return 1;
1744}
1745
Ben Lindstrombba81212001-06-25 05:01:22 +00001746static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001747authmethod_lookup(const char *name)
1748{
1749 Authmethod *method = NULL;
1750 if (name != NULL)
1751 for (method = authmethods; method->name != NULL; method++)
1752 if (strcmp(name, method->name) == 0)
1753 return method;
1754 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1755 return NULL;
1756}
1757
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001758/* XXX internal state */
1759static Authmethod *current = NULL;
1760static char *supported = NULL;
1761static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001762
Damien Miller62cee002000-09-23 17:15:56 +11001763/*
1764 * Given the authentication method list sent by the server, return the
1765 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001766 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001767 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001768static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001769authmethod_get(char *authlist)
1770{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001771 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001772 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001773
Damien Miller62cee002000-09-23 17:15:56 +11001774 /* Use a suitable default if we're passed a nil list. */
1775 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001776 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001777
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001778 if (supported == NULL || strcmp(authlist, supported) != 0) {
1779 debug3("start over, passed a different list %s", authlist);
1780 if (supported != NULL)
1781 xfree(supported);
1782 supported = xstrdup(authlist);
1783 preferred = options.preferred_authentications;
1784 debug3("preferred %s", preferred);
1785 current = NULL;
1786 } else if (current != NULL && authmethod_is_enabled(current))
1787 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001788
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001789 for (;;) {
1790 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001791 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001792 current = NULL;
1793 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001794 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001795 preferred += next;
1796 debug3("authmethod_lookup %s", name);
1797 debug3("remaining preferred: %s", preferred);
1798 if ((current = authmethod_lookup(name)) != NULL &&
1799 authmethod_is_enabled(current)) {
1800 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001801 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001802 return current;
1803 }
Damien Millereba71ba2000-04-29 23:57:08 +10001804 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001805}
Damien Miller62cee002000-09-23 17:15:56 +11001806
Ben Lindstrom79073822001-07-04 03:42:30 +00001807static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001808authmethods_get(void)
1809{
1810 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001811 Buffer b;
1812 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001813
Damien Miller0e3b8722002-01-22 23:26:38 +11001814 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001815 for (method = authmethods; method->name != NULL; method++) {
1816 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001817 if (buffer_len(&b) > 0)
1818 buffer_append(&b, ",", 1);
1819 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001820 }
Damien Miller62cee002000-09-23 17:15:56 +11001821 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001822 buffer_append(&b, "\0", 1);
1823 list = xstrdup(buffer_ptr(&b));
1824 buffer_free(&b);
1825 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001826}
Damien Miller01ed2272008-11-05 16:20:46 +11001827