blob: 41768bf0549849fbf74c0b34adc6e1664745f6cb [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Damien Millera5539d22003-04-09 20:50:06 +100026RCSID("$OpenBSD: sshconnect2.c,v 1.115 2003/04/02 09:48:07 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100030#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "buffer.h"
32#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "kex.h"
37#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "sshconnect.h"
39#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000040#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100041#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
43#include "readconf.h"
44#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000045#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000046#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000047#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000048#include "msg.h"
49#include "pathnames.h"
Damien Miller874d77b2000-10-14 16:23:11 +110050
Damien Millereba71ba2000-04-29 23:57:08 +100051/* import */
52extern char *client_version_string;
53extern char *server_version_string;
54extern Options options;
55
56/*
57 * SSH2 key exchange
58 */
59
Ben Lindstrom46c16222000-12-22 01:43:59 +000060u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100061int session_id2_len = 0;
62
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000063char *xxx_host;
64struct sockaddr *xxx_hostaddr;
65
Ben Lindstromf28f6342001-04-04 02:03:04 +000066Kex *xxx_kex = NULL;
67
Ben Lindstrombba81212001-06-25 05:01:22 +000068static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000069verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000070{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000071 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100072 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000073 return 0;
74}
75
Damien Millereba71ba2000-04-29 23:57:08 +100076void
Damien Miller874d77b2000-10-14 16:23:11 +110077ssh_kex2(char *host, struct sockaddr *hostaddr)
78{
Damien Miller874d77b2000-10-14 16:23:11 +110079 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000080
81 xxx_host = host;
82 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110083
Damien Millere39cacc2000-11-29 12:18:44 +110084 if (options.ciphers == (char *)-1) {
85 log("No valid ciphers for protocol version 2 given, using defaults.");
86 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110087 }
88 if (options.ciphers != NULL) {
89 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
90 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
91 }
Damien Millera0ff4662001-03-30 10:49:35 +100092 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
93 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
94 myproposal[PROPOSAL_ENC_ALGS_STOC] =
95 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +110096 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000097 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +000098 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +110099 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000100 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000101 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100102 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103 if (options.macs != NULL) {
104 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
105 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
106 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000107 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100108 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000109 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100110
Damien Millera5539d22003-04-09 20:50:06 +1000111 if (options.rekey_limit)
112 packet_set_rekey_limit(options.rekey_limit);
113
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000114 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000115 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100116 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
117 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000118 kex->client_version_string=client_version_string;
119 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000120 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100121
Ben Lindstromf28f6342001-04-04 02:03:04 +0000122 xxx_kex = kex;
123
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000124 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100125
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000126 session_id2 = kex->session_id;
127 session_id2_len = kex->session_id_len;
128
Damien Miller874d77b2000-10-14 16:23:11 +1100129#ifdef DEBUG_KEXDH
130 /* send 1st encrypted/maced/compressed message */
131 packet_start(SSH2_MSG_IGNORE);
132 packet_put_cstring("markus");
133 packet_send();
134 packet_write_wait();
135#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000136}
Damien Millerb1715dc2000-05-30 13:44:51 +1000137
Damien Millereba71ba2000-04-29 23:57:08 +1000138/*
139 * Authenticate user
140 */
Damien Miller62cee002000-09-23 17:15:56 +1100141
142typedef struct Authctxt Authctxt;
143typedef struct Authmethod Authmethod;
144
145typedef int sign_cb_fn(
146 Authctxt *authctxt, Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000147 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100148
149struct Authctxt {
150 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000151 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100152 const char *host;
153 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100154 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100155 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000156 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000157 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000158 Key *last_key;
159 sign_cb_fn *last_key_sign;
160 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000161 AuthenticationConnection *agent;
162 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000163 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000164 /* kbd-interactive */
165 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100166};
167struct Authmethod {
168 char *name; /* string to compare against server's list */
169 int (*userauth)(Authctxt *authctxt);
170 int *enabled; /* flag in option struct that enables method */
171 int *batch_flag; /* flag in option struct that disables method */
172};
173
Damien Miller630d6f42002-01-22 23:17:30 +1100174void input_userauth_success(int, u_int32_t, void *);
175void input_userauth_failure(int, u_int32_t, void *);
176void input_userauth_banner(int, u_int32_t, void *);
177void input_userauth_error(int, u_int32_t, void *);
178void input_userauth_info_req(int, u_int32_t, void *);
179void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000180void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100181
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000182int userauth_none(Authctxt *);
183int userauth_pubkey(Authctxt *);
184int userauth_passwd(Authctxt *);
185int userauth_kbdint(Authctxt *);
186int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100187
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000188void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
191static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static Authmethod *authmethod_get(char *authlist);
194static Authmethod *authmethod_lookup(const char *name);
195static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100196
197Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000198 {"hostbased",
199 userauth_hostbased,
200 &options.hostbased_authentication,
201 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000202 {"publickey",
203 userauth_pubkey,
204 &options.pubkey_authentication,
205 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100206 {"keyboard-interactive",
207 userauth_kbdint,
208 &options.kbd_interactive_authentication,
209 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000210 {"password",
211 userauth_passwd,
212 &options.password_authentication,
213 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100214 {"none",
215 userauth_none,
216 NULL,
217 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100218 {NULL, NULL, NULL, NULL}
219};
220
221void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000222ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000223 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100224{
225 Authctxt authctxt;
226 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100227
Ben Lindstrom551ea372001-06-05 18:56:16 +0000228 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000229 options.kbd_interactive_authentication = 1;
230
Damien Miller62cee002000-09-23 17:15:56 +1100231 packet_start(SSH2_MSG_SERVICE_REQUEST);
232 packet_put_cstring("ssh-userauth");
233 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000234 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100235 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100236 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000237 if (type != SSH2_MSG_SERVICE_ACCEPT)
238 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100239 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100240 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000241 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100242 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100243 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000244 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100245 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100246 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000247 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100248
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000249 if (options.preferred_authentications == NULL)
250 options.preferred_authentications = authmethods_get();
251
Damien Miller62cee002000-09-23 17:15:56 +1100252 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000253 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100254 authctxt.agent = ssh_get_authentication_connection();
255 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000256 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100257 authctxt.host = host;
258 authctxt.service = "ssh-connection"; /* service name */
259 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100260 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000261 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000262 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000263 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100264 if (authctxt.method == NULL)
265 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100266
267 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100268 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100269
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000270 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100271 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
272 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000273 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100274 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
275
276 if (authctxt.agent != NULL)
277 ssh_close_authentication_connection(authctxt.agent);
278
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000279 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100280}
281void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000282userauth(Authctxt *authctxt, char *authlist)
283{
284 if (authlist == NULL) {
285 authlist = authctxt->authlist;
286 } else {
287 if (authctxt->authlist)
288 xfree(authctxt->authlist);
289 authctxt->authlist = authlist;
290 }
291 for (;;) {
292 Authmethod *method = authmethod_get(authlist);
293 if (method == NULL)
294 fatal("Permission denied (%s).", authlist);
295 authctxt->method = method;
296 if (method->userauth(authctxt) != 0) {
297 debug2("we sent a %s packet, wait for reply", method->name);
298 break;
299 } else {
300 debug2("we did not send a packet, disable method");
301 method->enabled = NULL;
302 }
303 }
304}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000305
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000306void
Damien Miller630d6f42002-01-22 23:17:30 +1100307input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100308{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000309 fatal("input_userauth_error: bad message during authentication: "
310 "type %d", type);
311}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000312
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000313void
Damien Miller630d6f42002-01-22 23:17:30 +1100314input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000315{
316 char *msg, *lang;
317 debug3("input_userauth_banner");
318 msg = packet_get_string(NULL);
319 lang = packet_get_string(NULL);
320 fprintf(stderr, "%s", msg);
321 xfree(msg);
322 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100323}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000324
Damien Miller62cee002000-09-23 17:15:56 +1100325void
Damien Miller630d6f42002-01-22 23:17:30 +1100326input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100327{
328 Authctxt *authctxt = ctxt;
329 if (authctxt == NULL)
330 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000331 if (authctxt->authlist)
332 xfree(authctxt->authlist);
333 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100334 authctxt->success = 1; /* break out */
335}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000336
Damien Miller62cee002000-09-23 17:15:56 +1100337void
Damien Miller630d6f42002-01-22 23:17:30 +1100338input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100339{
Damien Miller62cee002000-09-23 17:15:56 +1100340 Authctxt *authctxt = ctxt;
341 char *authlist = NULL;
342 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100343
344 if (authctxt == NULL)
345 fatal("input_userauth_failure: no authentication context");
346
Damien Miller874d77b2000-10-14 16:23:11 +1100347 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100348 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100349 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100350
351 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000352 log("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000353 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100354
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000355 clear_auth_state(authctxt);
356 userauth(authctxt, authlist);
357}
358void
Damien Miller630d6f42002-01-22 23:17:30 +1100359input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000360{
361 Authctxt *authctxt = ctxt;
362 Key *key = NULL;
363 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000364 int pktype, sent = 0;
365 u_int alen, blen;
366 char *pkalg, *fp;
367 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000368
369 if (authctxt == NULL)
370 fatal("input_userauth_pk_ok: no authentication context");
371 if (datafellows & SSH_BUG_PKOK) {
372 /* this is similar to SSH_BUG_PKAUTH */
373 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
374 pkblob = packet_get_string(&blen);
375 buffer_init(&b);
376 buffer_append(&b, pkblob, blen);
377 pkalg = buffer_get_string(&b, &alen);
378 buffer_free(&b);
379 } else {
380 pkalg = packet_get_string(&alen);
381 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000382 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100383 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000384
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000385 debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000386 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
387
388 do {
389 if (authctxt->last_key == NULL ||
390 authctxt->last_key_sign == NULL) {
391 debug("no last key or no sign cb");
392 break;
393 }
Damien Miller68f45982002-02-05 12:23:32 +1100394 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000395 debug("unknown pkalg %s", pkalg);
396 break;
397 }
398 if ((key = key_from_blob(pkblob, blen)) == NULL) {
399 debug("no key from blob. pkalg %s", pkalg);
400 break;
401 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000402 if (key->type != pktype) {
Damien Miller68f45982002-02-05 12:23:32 +1100403 error("input_userauth_pk_ok: type mismatch "
404 "for decoded key (received %d, expected %d)",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000405 key->type, pktype);
Damien Miller68f45982002-02-05 12:23:32 +1100406 break;
407 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000408 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
409 debug2("input_userauth_pk_ok: fp %s", fp);
410 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000411 if (!key_equal(key, authctxt->last_key)) {
412 debug("key != last_key");
413 break;
414 }
415 sent = sign_and_send_pubkey(authctxt, key,
416 authctxt->last_key_sign);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000417 } while (0);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000418
419 if (key != NULL)
420 key_free(key);
421 xfree(pkalg);
422 xfree(pkblob);
423
424 /* unregister */
425 clear_auth_state(authctxt);
426 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
427
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000428 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000429 if (sent == 0)
430 userauth(authctxt, NULL);
431
Damien Miller62cee002000-09-23 17:15:56 +1100432}
433
Damien Millereba71ba2000-04-29 23:57:08 +1000434int
Damien Miller874d77b2000-10-14 16:23:11 +1100435userauth_none(Authctxt *authctxt)
436{
437 /* initial userauth request */
438 packet_start(SSH2_MSG_USERAUTH_REQUEST);
439 packet_put_cstring(authctxt->server_user);
440 packet_put_cstring(authctxt->service);
441 packet_put_cstring(authctxt->method->name);
442 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100443 return 1;
444}
445
446int
Damien Miller62cee002000-09-23 17:15:56 +1100447userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000448{
Damien Millere247cc42000-05-07 12:03:14 +1000449 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000450 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000451 char *password;
452
Damien Millerd3a18572000-06-07 19:55:44 +1000453 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000454 return 0;
455
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000456 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000457 error("Permission denied, please try again.");
458
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000459 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100460 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 password = read_passphrase(prompt, 0);
462 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100463 packet_put_cstring(authctxt->server_user);
464 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100465 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000466 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000467 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000468 memset(password, 0, strlen(password));
469 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100470 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000471 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000472
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000473 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000474 &input_userauth_passwd_changereq);
475
Damien Millereba71ba2000-04-29 23:57:08 +1000476 return 1;
477}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000478/*
479 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
480 */
481void
Tim Ricec8549622002-03-31 12:49:38 -0800482input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000483{
484 Authctxt *authctxt = ctxt;
485 char *info, *lang, *password = NULL, *retype = NULL;
486 char prompt[150];
487
488 debug2("input_userauth_passwd_changereq");
489
490 if (authctxt == NULL)
491 fatal("input_userauth_passwd_changereq: "
492 "no authentication context");
493
494 info = packet_get_string(NULL);
495 lang = packet_get_string(NULL);
496 if (strlen(info) > 0)
497 log("%s", info);
498 xfree(info);
499 xfree(lang);
500 packet_start(SSH2_MSG_USERAUTH_REQUEST);
501 packet_put_cstring(authctxt->server_user);
502 packet_put_cstring(authctxt->service);
503 packet_put_cstring(authctxt->method->name);
504 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000505 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000506 "Enter %.30s@%.128s's old password: ",
507 authctxt->server_user, authctxt->host);
508 password = read_passphrase(prompt, 0);
509 packet_put_cstring(password);
510 memset(password, 0, strlen(password));
511 xfree(password);
512 password = NULL;
513 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000514 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000515 "Enter %.30s@%.128s's new password: ",
516 authctxt->server_user, authctxt->host);
517 password = read_passphrase(prompt, RP_ALLOW_EOF);
518 if (password == NULL) {
519 /* bail out */
520 return;
521 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000522 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000523 "Retype %.30s@%.128s's new password: ",
524 authctxt->server_user, authctxt->host);
525 retype = read_passphrase(prompt, 0);
526 if (strcmp(password, retype) != 0) {
527 memset(password, 0, strlen(password));
528 xfree(password);
529 log("Mismatch; try again, EOF to quit.");
530 password = NULL;
531 }
532 memset(retype, 0, strlen(retype));
533 xfree(retype);
534 }
535 packet_put_cstring(password);
536 memset(password, 0, strlen(password));
537 xfree(password);
538 packet_add_padding(64);
539 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000540
541 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000542 &input_userauth_passwd_changereq);
543}
Damien Millereba71ba2000-04-29 23:57:08 +1000544
Ben Lindstrom79073822001-07-04 03:42:30 +0000545static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000546clear_auth_state(Authctxt *authctxt)
547{
548 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000549 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
550
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
552 debug3("clear_auth_state: key_free %p", authctxt->last_key);
553 key_free(authctxt->last_key);
554 }
555 authctxt->last_key = NULL;
556 authctxt->last_key_hint = -2;
557 authctxt->last_key_sign = NULL;
558}
559
Ben Lindstrombba81212001-06-25 05:01:22 +0000560static int
Damien Miller62cee002000-09-23 17:15:56 +1100561sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000562{
563 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000564 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000565 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000566 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000567 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100568 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000569
Ben Lindstromd121f612000-12-03 17:00:47 +0000570 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000571
Damien Miller0bc1bd82000-11-13 22:57:25 +1100572 if (key_to_blob(k, &blob, &bloblen) == 0) {
573 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000574 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100575 return 0;
576 }
Damien Millereba71ba2000-04-29 23:57:08 +1000577 /* data to be signed */
578 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100579 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000580 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100582 } else {
583 buffer_put_string(&b, session_id2, session_id2_len);
584 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000585 }
Damien Millereba71ba2000-04-29 23:57:08 +1000586 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100587 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000588 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000589 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000590 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100591 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000592 if (datafellows & SSH_BUG_PKAUTH) {
593 buffer_put_char(&b, have_sig);
594 } else {
595 buffer_put_cstring(&b, authctxt->method->name);
596 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000598 }
Damien Millereba71ba2000-04-29 23:57:08 +1000599 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000600
601 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000602 ret = (*sign_callback)(authctxt, k, &signature, &slen,
603 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000604 if (ret == -1) {
605 xfree(blob);
606 buffer_free(&b);
607 return 0;
608 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100609#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000610 buffer_dump(&b);
611#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000612 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000613 buffer_clear(&b);
614 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000615 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000616 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100617 buffer_put_cstring(&b, authctxt->server_user);
618 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100619 buffer_put_cstring(&b, authctxt->method->name);
620 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000621 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000622 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000623 buffer_put_string(&b, blob, bloblen);
624 }
625 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000626
Damien Millereba71ba2000-04-29 23:57:08 +1000627 /* append signature */
628 buffer_put_string(&b, signature, slen);
629 xfree(signature);
630
631 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000632 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100633 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000634 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000635
636 /* put remaining data from buffer into packet */
637 packet_start(SSH2_MSG_USERAUTH_REQUEST);
638 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
639 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000640 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000641
642 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000643}
644
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000646send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
647 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000648{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000649 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000650 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000651
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000652 debug3("send_pubkey_test");
653
654 if (key_to_blob(k, &blob, &bloblen) == 0) {
655 /* we cannot handle this key */
656 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000657 return 0;
658 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000659 /* register callback for USERAUTH_PK_OK message */
660 authctxt->last_key_sign = sign_callback;
661 authctxt->last_key_hint = hint;
662 authctxt->last_key = k;
663 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000664
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000665 packet_start(SSH2_MSG_USERAUTH_REQUEST);
666 packet_put_cstring(authctxt->server_user);
667 packet_put_cstring(authctxt->service);
668 packet_put_cstring(authctxt->method->name);
669 packet_put_char(have_sig);
670 if (!(datafellows & SSH_BUG_PKAUTH))
671 packet_put_cstring(key_ssh_name(k));
672 packet_put_string(blob, bloblen);
673 xfree(blob);
674 packet_send();
675 return 1;
676}
677
Ben Lindstrombba81212001-06-25 05:01:22 +0000678static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000679load_identity_file(char *filename)
680{
681 Key *private;
682 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000683 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000684 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000685
Ben Lindstrom329782e2001-03-10 17:08:59 +0000686 if (stat(filename, &st) < 0) {
687 debug3("no such identity: %s", filename);
688 return NULL;
689 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000690 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
691 if (private == NULL) {
692 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000693 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000694 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100695 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100696 for (i = 0; i < options.number_of_password_prompts; i++) {
697 passphrase = read_passphrase(prompt, 0);
698 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000699 private = key_load_private_type(KEY_UNSPEC, filename,
700 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000701 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100702 } else {
703 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000704 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100705 }
706 memset(passphrase, 0, strlen(passphrase));
707 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000708 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100709 break;
710 debug2("bad passphrase given, try again...");
711 }
Damien Miller994cf142000-07-21 10:19:44 +1000712 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000713 return private;
714}
715
Ben Lindstrombba81212001-06-25 05:01:22 +0000716static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000717identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
718 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000719{
720 Key *private;
721 int idx, ret;
722
723 idx = authctxt->last_key_hint;
724 if (idx < 0)
725 return -1;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000726
727 /* private key is stored in external hardware */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100728 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000729 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
730
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000731 private = load_identity_file(options.identity_files[idx]);
732 if (private == NULL)
733 return -1;
734 ret = key_sign(private, sigp, lenp, data, datalen);
735 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000736 return ret;
737}
738
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000740agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
741 u_char *data, u_int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000742{
Damien Miller62cee002000-09-23 17:15:56 +1100743 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000744}
745
Ben Lindstrombba81212001-06-25 05:01:22 +0000746static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000747key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
748 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000749{
Ben Lindstroma3700052001-04-05 23:26:32 +0000750 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000751}
752
Ben Lindstrombba81212001-06-25 05:01:22 +0000753static int
Damien Miller62cee002000-09-23 17:15:56 +1100754userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000755{
756 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000758 char *comment;
759 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000760
761 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100762 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
763 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100764 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000765 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100767 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768 debug2("userauth_pubkey_agent: no more keys");
769 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000770 debug("Offering agent key: %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000772 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
773 if (ret == 0)
774 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100775 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100776 if (ret == 0)
777 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000778 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000779}
780
Damien Miller62cee002000-09-23 17:15:56 +1100781int
782userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000783{
Damien Miller62cee002000-09-23 17:15:56 +1100784 static int idx = 0;
785 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000786 Key *key;
787 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000788
Damien Miller0bc1bd82000-11-13 22:57:25 +1100789 if (authctxt->agent != NULL) {
790 do {
791 sent = userauth_pubkey_agent(authctxt);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000792 } while (!sent && authctxt->agent->howmany > 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793 }
794 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000795 key = options.identity_keys[idx];
796 filename = options.identity_files[idx];
797 if (key == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000798 debug("Trying private key: %s", filename);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000799 key = load_identity_file(filename);
800 if (key != NULL) {
801 sent = sign_and_send_pubkey(authctxt, key,
802 key_sign_cb);
803 key_free(key);
804 }
805 } else if (key->type != KEY_RSA1) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000806 debug("Offering public key: %s", filename);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000807 sent = send_pubkey_test(authctxt, key,
808 identity_sign_cb, idx);
809 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100810 idx++;
811 }
Damien Miller62cee002000-09-23 17:15:56 +1100812 return sent;
813}
Damien Millereba71ba2000-04-29 23:57:08 +1000814
Damien Miller874d77b2000-10-14 16:23:11 +1100815/*
816 * Send userauth request message specifying keyboard-interactive method.
817 */
818int
819userauth_kbdint(Authctxt *authctxt)
820{
821 static int attempt = 0;
822
823 if (attempt++ >= options.number_of_password_prompts)
824 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000825 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
826 if (attempt > 1 && !authctxt->info_req_seen) {
827 debug3("userauth_kbdint: disable: no info_req_seen");
828 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
829 return 0;
830 }
Damien Miller874d77b2000-10-14 16:23:11 +1100831
832 debug2("userauth_kbdint");
833 packet_start(SSH2_MSG_USERAUTH_REQUEST);
834 packet_put_cstring(authctxt->server_user);
835 packet_put_cstring(authctxt->service);
836 packet_put_cstring(authctxt->method->name);
837 packet_put_cstring(""); /* lang */
838 packet_put_cstring(options.kbd_interactive_devices ?
839 options.kbd_interactive_devices : "");
840 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100841
842 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
843 return 1;
844}
845
846/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000847 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100848 */
849void
Damien Miller630d6f42002-01-22 23:17:30 +1100850input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100851{
852 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000853 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000854 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100855 int echo = 0;
856
857 debug2("input_userauth_info_req");
858
859 if (authctxt == NULL)
860 fatal("input_userauth_info_req: no authentication context");
861
Ben Lindstrom7d199962001-09-12 18:29:00 +0000862 authctxt->info_req_seen = 1;
863
Damien Miller874d77b2000-10-14 16:23:11 +1100864 name = packet_get_string(NULL);
865 inst = packet_get_string(NULL);
866 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100867 if (strlen(name) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000868 log("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100869 if (strlen(inst) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000870 log("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000871 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100872 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000873 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100874
875 num_prompts = packet_get_int();
876 /*
877 * Begin to build info response packet based on prompts requested.
878 * We commit to providing the correct number of responses, so if
879 * further on we run into a problem that prevents this, we have to
880 * be sure and clean this up and send a correct error response.
881 */
882 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
883 packet_put_int(num_prompts);
884
Ben Lindstrom551ea372001-06-05 18:56:16 +0000885 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100886 for (i = 0; i < num_prompts; i++) {
887 prompt = packet_get_string(NULL);
888 echo = packet_get_char();
889
Ben Lindstrom949974b2001-06-25 05:20:31 +0000890 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100891
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000892 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100893 memset(response, 0, strlen(response));
894 xfree(response);
895 xfree(prompt);
896 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100897 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100898
Damien Miller9f643902001-11-12 11:02:52 +1100899 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100900 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100901}
Damien Miller62cee002000-09-23 17:15:56 +1100902
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000903static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000904ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000905 u_char *data, u_int datalen)
906{
907 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000908 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000909 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000910 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000911
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000912 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000913
Ben Lindstrom5206b952002-06-06 19:59:29 +0000914 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
915 error("ssh_keysign: no installed: %s", strerror(errno));
916 return -1;
917 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000918 if (fflush(stdout) != 0)
919 error("ssh_keysign: fflush: %s", strerror(errno));
920 if (pipe(to) < 0) {
921 error("ssh_keysign: pipe: %s", strerror(errno));
922 return -1;
923 }
924 if (pipe(from) < 0) {
925 error("ssh_keysign: pipe: %s", strerror(errno));
926 return -1;
927 }
928 if ((pid = fork()) < 0) {
929 error("ssh_keysign: fork: %s", strerror(errno));
930 return -1;
931 }
932 if (pid == 0) {
933 seteuid(getuid());
934 setuid(getuid());
935 close(from[0]);
936 if (dup2(from[1], STDOUT_FILENO) < 0)
937 fatal("ssh_keysign: dup2: %s", strerror(errno));
938 close(to[1]);
939 if (dup2(to[0], STDIN_FILENO) < 0)
940 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000941 close(from[1]);
942 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +0000943 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000944 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
945 strerror(errno));
946 }
947 close(from[1]);
948 close(to[0]);
949
950 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000951 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000952 buffer_put_string(&b, data, datalen);
Damien Miller901119b2002-10-04 11:10:04 +1000953 ssh_msg_send(to[1], version, &b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000954
Damien Miller901119b2002-10-04 11:10:04 +1000955 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +0000956 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000957 buffer_clear(&b);
958 return -1;
959 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000960 close(from[0]);
961 close(to[1]);
962
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000963 while (waitpid(pid, &status, 0) < 0)
964 if (errno != EINTR)
965 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000966
Ben Lindstrom5206b952002-06-06 19:59:29 +0000967 if (buffer_get_char(&b) != version) {
968 error("ssh_keysign: bad version");
969 buffer_clear(&b);
970 return -1;
971 }
972 *sigp = buffer_get_string(&b, lenp);
973 buffer_clear(&b);
974
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000975 return 0;
976}
977
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000978int
979userauth_hostbased(Authctxt *authctxt)
980{
981 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000982 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000983 Buffer b;
984 u_char *signature, *blob;
985 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000986 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000987 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000988 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000989
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000990 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000991 for (i = 0; i < sensitive->nkeys; i++) {
992 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000993 if (private && private->type != KEY_RSA1) {
994 found = 1;
995 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000996 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000997 break;
998 }
999 }
1000 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001001 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001002 return 0;
1003 }
1004 if (key_to_blob(private, &blob, &blen) == 0) {
1005 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001006 return 0;
1007 }
Damien Miller91c18472001-11-12 11:02:03 +11001008 /* figure out a name for the client host */
1009 p = get_local_name(packet_get_connection_in());
1010 if (p == NULL) {
1011 error("userauth_hostbased: cannot get local ipaddr/name");
1012 key_free(private);
1013 return 0;
1014 }
1015 len = strlen(p) + 2;
1016 chost = xmalloc(len);
1017 strlcpy(chost, p, len);
1018 strlcat(chost, ".", len);
1019 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001020 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001021
Ben Lindstrom671388f2001-04-19 20:40:45 +00001022 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1023 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001024 pkalg = xstrdup(key_ssh_name(private));
1025 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001026 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001027 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001028 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1029 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001030 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001031 buffer_put_cstring(&b, authctxt->method->name);
1032 buffer_put_cstring(&b, pkalg);
1033 buffer_put_string(&b, blob, blen);
1034 buffer_put_cstring(&b, chost);
1035 buffer_put_cstring(&b, authctxt->local_user);
1036#ifdef DEBUG_PK
1037 buffer_dump(&b);
1038#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001039 if (sensitive->external_keysign)
1040 ok = ssh_keysign(private, &signature, &slen,
1041 buffer_ptr(&b), buffer_len(&b));
1042 else
1043 ok = key_sign(private, &signature, &slen,
1044 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001045 key_free(private);
1046 buffer_free(&b);
1047 if (ok != 0) {
1048 error("key_sign failed");
1049 xfree(chost);
1050 xfree(pkalg);
1051 return 0;
1052 }
1053 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1054 packet_put_cstring(authctxt->server_user);
1055 packet_put_cstring(authctxt->service);
1056 packet_put_cstring(authctxt->method->name);
1057 packet_put_cstring(pkalg);
1058 packet_put_string(blob, blen);
1059 packet_put_cstring(chost);
1060 packet_put_cstring(authctxt->local_user);
1061 packet_put_string(signature, slen);
1062 memset(signature, 's', slen);
1063 xfree(signature);
1064 xfree(chost);
1065 xfree(pkalg);
1066
1067 packet_send();
1068 return 1;
1069}
1070
Damien Miller62cee002000-09-23 17:15:56 +11001071/* find auth method */
1072
Damien Miller62cee002000-09-23 17:15:56 +11001073/*
1074 * given auth method name, if configurable options permit this method fill
1075 * in auth_ident field and return true, otherwise return false.
1076 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001077static int
Damien Miller62cee002000-09-23 17:15:56 +11001078authmethod_is_enabled(Authmethod *method)
1079{
1080 if (method == NULL)
1081 return 0;
1082 /* return false if options indicate this method is disabled */
1083 if (method->enabled == NULL || *method->enabled == 0)
1084 return 0;
1085 /* return false if batch mode is enabled but method needs interactive mode */
1086 if (method->batch_flag != NULL && *method->batch_flag != 0)
1087 return 0;
1088 return 1;
1089}
1090
Ben Lindstrombba81212001-06-25 05:01:22 +00001091static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001092authmethod_lookup(const char *name)
1093{
1094 Authmethod *method = NULL;
1095 if (name != NULL)
1096 for (method = authmethods; method->name != NULL; method++)
1097 if (strcmp(name, method->name) == 0)
1098 return method;
1099 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1100 return NULL;
1101}
1102
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001103/* XXX internal state */
1104static Authmethod *current = NULL;
1105static char *supported = NULL;
1106static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001107
Damien Miller62cee002000-09-23 17:15:56 +11001108/*
1109 * Given the authentication method list sent by the server, return the
1110 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001111 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001112 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001113static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001114authmethod_get(char *authlist)
1115{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001116 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001117 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001118
Damien Miller62cee002000-09-23 17:15:56 +11001119 /* Use a suitable default if we're passed a nil list. */
1120 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001121 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001122
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001123 if (supported == NULL || strcmp(authlist, supported) != 0) {
1124 debug3("start over, passed a different list %s", authlist);
1125 if (supported != NULL)
1126 xfree(supported);
1127 supported = xstrdup(authlist);
1128 preferred = options.preferred_authentications;
1129 debug3("preferred %s", preferred);
1130 current = NULL;
1131 } else if (current != NULL && authmethod_is_enabled(current))
1132 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001133
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001134 for (;;) {
1135 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001136 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001137 current = NULL;
1138 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001139 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001140 preferred += next;
1141 debug3("authmethod_lookup %s", name);
1142 debug3("remaining preferred: %s", preferred);
1143 if ((current = authmethod_lookup(name)) != NULL &&
1144 authmethod_is_enabled(current)) {
1145 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001146 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001147 return current;
1148 }
Damien Millereba71ba2000-04-29 23:57:08 +10001149 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001150}
Damien Miller62cee002000-09-23 17:15:56 +11001151
Ben Lindstrom79073822001-07-04 03:42:30 +00001152static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001153authmethods_get(void)
1154{
1155 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001156 Buffer b;
1157 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001158
Damien Miller0e3b8722002-01-22 23:26:38 +11001159 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001160 for (method = authmethods; method->name != NULL; method++) {
1161 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001162 if (buffer_len(&b) > 0)
1163 buffer_append(&b, ",", 1);
1164 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001165 }
Damien Miller62cee002000-09-23 17:15:56 +11001166 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001167 buffer_append(&b, "\0", 1);
1168 list = xstrdup(buffer_ptr(&b));
1169 buffer_free(&b);
1170 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001171}