blob: 1ee92ab0d740d2d9f507415ef82688a3a7da58a9 [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"
Ben Lindstrom38a69e62002-03-27 17:28:46 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.99 2002/03/26 15:58:46 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"
Damien Miller874d77b2000-10-14 16:23:11 +110048
Damien Millereba71ba2000-04-29 23:57:08 +100049/* import */
50extern char *client_version_string;
51extern char *server_version_string;
52extern Options options;
53
54/*
55 * SSH2 key exchange
56 */
57
Ben Lindstrom46c16222000-12-22 01:43:59 +000058u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100059int session_id2_len = 0;
60
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000061char *xxx_host;
62struct sockaddr *xxx_hostaddr;
63
Ben Lindstromf28f6342001-04-04 02:03:04 +000064Kex *xxx_kex = NULL;
65
Ben Lindstrombba81212001-06-25 05:01:22 +000066static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000067verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000068{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000069 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100070 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000071 return 0;
72}
73
Damien Millereba71ba2000-04-29 23:57:08 +100074void
Damien Miller874d77b2000-10-14 16:23:11 +110075ssh_kex2(char *host, struct sockaddr *hostaddr)
76{
Damien Miller874d77b2000-10-14 16:23:11 +110077 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000078
79 xxx_host = host;
80 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110081
Damien Millere39cacc2000-11-29 12:18:44 +110082 if (options.ciphers == (char *)-1) {
83 log("No valid ciphers for protocol version 2 given, using defaults.");
84 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110085 }
86 if (options.ciphers != NULL) {
87 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
88 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
89 }
Damien Millera0ff4662001-03-30 10:49:35 +100090 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
91 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
92 myproposal[PROPOSAL_ENC_ALGS_STOC] =
93 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +110094 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000095 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110096 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
97 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000098 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110099 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
100 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000101 if (options.macs != NULL) {
102 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
103 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
104 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000105 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100106 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000107 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100108
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000109 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000110 kex = kex_setup(myproposal);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000111 kex->client_version_string=client_version_string;
112 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000113 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100114
Ben Lindstromf28f6342001-04-04 02:03:04 +0000115 xxx_kex = kex;
116
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000117 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100118
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000119 session_id2 = kex->session_id;
120 session_id2_len = kex->session_id_len;
121
Damien Miller874d77b2000-10-14 16:23:11 +1100122#ifdef DEBUG_KEXDH
123 /* send 1st encrypted/maced/compressed message */
124 packet_start(SSH2_MSG_IGNORE);
125 packet_put_cstring("markus");
126 packet_send();
127 packet_write_wait();
128#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000129 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000130}
Damien Millerb1715dc2000-05-30 13:44:51 +1000131
Damien Millereba71ba2000-04-29 23:57:08 +1000132/*
133 * Authenticate user
134 */
Damien Miller62cee002000-09-23 17:15:56 +1100135
136typedef struct Authctxt Authctxt;
137typedef struct Authmethod Authmethod;
138
139typedef int sign_cb_fn(
140 Authctxt *authctxt, Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000141 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100142
143struct Authctxt {
144 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000145 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100146 const char *host;
147 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100148 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100149 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000150 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000151 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000152 Key *last_key;
153 sign_cb_fn *last_key_sign;
154 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000155 AuthenticationConnection *agent;
156 /* hostbased */
157 Key **keys;
158 int nkeys;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000159 /* kbd-interactive */
160 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100161};
162struct Authmethod {
163 char *name; /* string to compare against server's list */
164 int (*userauth)(Authctxt *authctxt);
165 int *enabled; /* flag in option struct that enables method */
166 int *batch_flag; /* flag in option struct that disables method */
167};
168
Damien Miller630d6f42002-01-22 23:17:30 +1100169void input_userauth_success(int, u_int32_t, void *);
170void input_userauth_failure(int, u_int32_t, void *);
171void input_userauth_banner(int, u_int32_t, void *);
172void input_userauth_error(int, u_int32_t, void *);
173void input_userauth_info_req(int, u_int32_t, void *);
174void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000175void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100176
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000177int userauth_none(Authctxt *);
178int userauth_pubkey(Authctxt *);
179int userauth_passwd(Authctxt *);
180int userauth_kbdint(Authctxt *);
181int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100182
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000183void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
186static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static Authmethod *authmethod_get(char *authlist);
189static Authmethod *authmethod_lookup(const char *name);
190static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100191
192Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000193 {"hostbased",
194 userauth_hostbased,
195 &options.hostbased_authentication,
196 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000197 {"publickey",
198 userauth_pubkey,
199 &options.pubkey_authentication,
200 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100201 {"keyboard-interactive",
202 userauth_kbdint,
203 &options.kbd_interactive_authentication,
204 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000205 {"password",
206 userauth_passwd,
207 &options.password_authentication,
208 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100209 {"none",
210 userauth_none,
211 NULL,
212 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100213 {NULL, NULL, NULL, NULL}
214};
215
216void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000217ssh_userauth2(const char *local_user, const char *server_user, char *host,
218 Key **keys, int nkeys)
Damien Miller62cee002000-09-23 17:15:56 +1100219{
220 Authctxt authctxt;
221 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100222
Ben Lindstrom551ea372001-06-05 18:56:16 +0000223 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000224 options.kbd_interactive_authentication = 1;
225
Damien Miller62cee002000-09-23 17:15:56 +1100226 debug("send SSH2_MSG_SERVICE_REQUEST");
227 packet_start(SSH2_MSG_SERVICE_REQUEST);
228 packet_put_cstring("ssh-userauth");
229 packet_send();
230 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100231 type = packet_read();
Damien Miller62cee002000-09-23 17:15:56 +1100232 if (type != SSH2_MSG_SERVICE_ACCEPT) {
233 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
234 }
235 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100236 char *reply = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100237 debug("service_accept: %s", reply);
238 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100239 } else {
240 debug("buggy server: service_accept w/o service");
241 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100242 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100243 debug("got SSH2_MSG_SERVICE_ACCEPT");
244
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000245 if (options.preferred_authentications == NULL)
246 options.preferred_authentications = authmethods_get();
247
Damien Miller62cee002000-09-23 17:15:56 +1100248 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000249 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100250 authctxt.agent = ssh_get_authentication_connection();
251 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000252 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100253 authctxt.host = host;
254 authctxt.service = "ssh-connection"; /* service name */
255 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100256 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000257 authctxt.authlist = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000258 authctxt.keys = keys;
259 authctxt.nkeys = nkeys;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000260 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100261 if (authctxt.method == NULL)
262 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100263
264 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100265 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100266
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000267 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100268 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
269 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000270 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100271 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
272
273 if (authctxt.agent != NULL)
274 ssh_close_authentication_connection(authctxt.agent);
275
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000276 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100277}
278void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000279userauth(Authctxt *authctxt, char *authlist)
280{
281 if (authlist == NULL) {
282 authlist = authctxt->authlist;
283 } else {
284 if (authctxt->authlist)
285 xfree(authctxt->authlist);
286 authctxt->authlist = authlist;
287 }
288 for (;;) {
289 Authmethod *method = authmethod_get(authlist);
290 if (method == NULL)
291 fatal("Permission denied (%s).", authlist);
292 authctxt->method = method;
293 if (method->userauth(authctxt) != 0) {
294 debug2("we sent a %s packet, wait for reply", method->name);
295 break;
296 } else {
297 debug2("we did not send a packet, disable method");
298 method->enabled = NULL;
299 }
300 }
301}
302void
Damien Miller630d6f42002-01-22 23:17:30 +1100303input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100304{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000305 fatal("input_userauth_error: bad message during authentication: "
306 "type %d", type);
307}
308void
Damien Miller630d6f42002-01-22 23:17:30 +1100309input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000310{
311 char *msg, *lang;
312 debug3("input_userauth_banner");
313 msg = packet_get_string(NULL);
314 lang = packet_get_string(NULL);
315 fprintf(stderr, "%s", msg);
316 xfree(msg);
317 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100318}
319void
Damien Miller630d6f42002-01-22 23:17:30 +1100320input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100321{
322 Authctxt *authctxt = ctxt;
323 if (authctxt == NULL)
324 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000325 if (authctxt->authlist)
326 xfree(authctxt->authlist);
327 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100328 authctxt->success = 1; /* break out */
329}
330void
Damien Miller630d6f42002-01-22 23:17:30 +1100331input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100332{
Damien Miller62cee002000-09-23 17:15:56 +1100333 Authctxt *authctxt = ctxt;
334 char *authlist = NULL;
335 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100336
337 if (authctxt == NULL)
338 fatal("input_userauth_failure: no authentication context");
339
Damien Miller874d77b2000-10-14 16:23:11 +1100340 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100341 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100342 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100343
344 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000345 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100346 debug("authentications that can continue: %s", authlist);
347
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000348 clear_auth_state(authctxt);
349 userauth(authctxt, authlist);
350}
351void
Damien Miller630d6f42002-01-22 23:17:30 +1100352input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000353{
354 Authctxt *authctxt = ctxt;
355 Key *key = NULL;
356 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000357 int pktype, sent = 0;
358 u_int alen, blen;
359 char *pkalg, *fp;
360 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000361
362 if (authctxt == NULL)
363 fatal("input_userauth_pk_ok: no authentication context");
364 if (datafellows & SSH_BUG_PKOK) {
365 /* this is similar to SSH_BUG_PKAUTH */
366 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
367 pkblob = packet_get_string(&blen);
368 buffer_init(&b);
369 buffer_append(&b, pkblob, blen);
370 pkalg = buffer_get_string(&b, &alen);
371 buffer_free(&b);
372 } else {
373 pkalg = packet_get_string(&alen);
374 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000375 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100376 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000377
378 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
379 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
380
381 do {
382 if (authctxt->last_key == NULL ||
383 authctxt->last_key_sign == NULL) {
384 debug("no last key or no sign cb");
385 break;
386 }
Damien Miller68f45982002-02-05 12:23:32 +1100387 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000388 debug("unknown pkalg %s", pkalg);
389 break;
390 }
391 if ((key = key_from_blob(pkblob, blen)) == NULL) {
392 debug("no key from blob. pkalg %s", pkalg);
393 break;
394 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000395 if (key->type != pktype) {
Damien Miller68f45982002-02-05 12:23:32 +1100396 error("input_userauth_pk_ok: type mismatch "
397 "for decoded key (received %d, expected %d)",
398 key->type, pktype);
399 break;
400 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000401 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
402 debug2("input_userauth_pk_ok: fp %s", fp);
403 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000404 if (!key_equal(key, authctxt->last_key)) {
405 debug("key != last_key");
406 break;
407 }
408 sent = sign_and_send_pubkey(authctxt, key,
409 authctxt->last_key_sign);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000410 } while (0);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000411
412 if (key != NULL)
413 key_free(key);
414 xfree(pkalg);
415 xfree(pkblob);
416
417 /* unregister */
418 clear_auth_state(authctxt);
419 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
420
421 /* try another method if we did not send a packet*/
422 if (sent == 0)
423 userauth(authctxt, NULL);
424
Damien Miller62cee002000-09-23 17:15:56 +1100425}
426
Damien Millereba71ba2000-04-29 23:57:08 +1000427int
Damien Miller874d77b2000-10-14 16:23:11 +1100428userauth_none(Authctxt *authctxt)
429{
430 /* initial userauth request */
431 packet_start(SSH2_MSG_USERAUTH_REQUEST);
432 packet_put_cstring(authctxt->server_user);
433 packet_put_cstring(authctxt->service);
434 packet_put_cstring(authctxt->method->name);
435 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100436 return 1;
437}
438
439int
Damien Miller62cee002000-09-23 17:15:56 +1100440userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000441{
Damien Millere247cc42000-05-07 12:03:14 +1000442 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000443 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000444 char *password;
445
Damien Millerd3a18572000-06-07 19:55:44 +1000446 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000447 return 0;
448
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000449 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000450 error("Permission denied, please try again.");
451
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000452 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100453 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000454 password = read_passphrase(prompt, 0);
455 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100456 packet_put_cstring(authctxt->server_user);
457 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000459 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000460 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 memset(password, 0, strlen(password));
462 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100463 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000464 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000465
466 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
467 &input_userauth_passwd_changereq);
468
Damien Millereba71ba2000-04-29 23:57:08 +1000469 return 1;
470}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000471/*
472 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
473 */
474void
Tim Ricec8549622002-03-31 12:49:38 -0800475input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000476{
477 Authctxt *authctxt = ctxt;
478 char *info, *lang, *password = NULL, *retype = NULL;
479 char prompt[150];
480
481 debug2("input_userauth_passwd_changereq");
482
483 if (authctxt == NULL)
484 fatal("input_userauth_passwd_changereq: "
485 "no authentication context");
486
487 info = packet_get_string(NULL);
488 lang = packet_get_string(NULL);
489 if (strlen(info) > 0)
490 log("%s", info);
491 xfree(info);
492 xfree(lang);
493 packet_start(SSH2_MSG_USERAUTH_REQUEST);
494 packet_put_cstring(authctxt->server_user);
495 packet_put_cstring(authctxt->service);
496 packet_put_cstring(authctxt->method->name);
497 packet_put_char(1); /* additional info */
498 snprintf(prompt, sizeof(prompt),
499 "Enter %.30s@%.128s's old password: ",
500 authctxt->server_user, authctxt->host);
501 password = read_passphrase(prompt, 0);
502 packet_put_cstring(password);
503 memset(password, 0, strlen(password));
504 xfree(password);
505 password = NULL;
506 while (password == NULL) {
507 snprintf(prompt, sizeof(prompt),
508 "Enter %.30s@%.128s's new password: ",
509 authctxt->server_user, authctxt->host);
510 password = read_passphrase(prompt, RP_ALLOW_EOF);
511 if (password == NULL) {
512 /* bail out */
513 return;
514 }
515 snprintf(prompt, sizeof(prompt),
516 "Retype %.30s@%.128s's new password: ",
517 authctxt->server_user, authctxt->host);
518 retype = read_passphrase(prompt, 0);
519 if (strcmp(password, retype) != 0) {
520 memset(password, 0, strlen(password));
521 xfree(password);
522 log("Mismatch; try again, EOF to quit.");
523 password = NULL;
524 }
525 memset(retype, 0, strlen(retype));
526 xfree(retype);
527 }
528 packet_put_cstring(password);
529 memset(password, 0, strlen(password));
530 xfree(password);
531 packet_add_padding(64);
532 packet_send();
533
534 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
535 &input_userauth_passwd_changereq);
536}
Damien Millereba71ba2000-04-29 23:57:08 +1000537
Ben Lindstrom79073822001-07-04 03:42:30 +0000538static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000539clear_auth_state(Authctxt *authctxt)
540{
541 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000542 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
543
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000544 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
545 debug3("clear_auth_state: key_free %p", authctxt->last_key);
546 key_free(authctxt->last_key);
547 }
548 authctxt->last_key = NULL;
549 authctxt->last_key_hint = -2;
550 authctxt->last_key_sign = NULL;
551}
552
Ben Lindstrombba81212001-06-25 05:01:22 +0000553static int
Damien Miller62cee002000-09-23 17:15:56 +1100554sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000555{
556 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000557 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000558 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000559 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000560 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100561 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000562
Ben Lindstromd121f612000-12-03 17:00:47 +0000563 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000564
Damien Miller0bc1bd82000-11-13 22:57:25 +1100565 if (key_to_blob(k, &blob, &bloblen) == 0) {
566 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000567 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100568 return 0;
569 }
Damien Millereba71ba2000-04-29 23:57:08 +1000570 /* data to be signed */
571 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100572 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000573 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000574 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100575 } else {
576 buffer_put_string(&b, session_id2, session_id2_len);
577 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000578 }
Damien Millereba71ba2000-04-29 23:57:08 +1000579 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100580 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000581 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000582 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000583 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100584 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000585 if (datafellows & SSH_BUG_PKAUTH) {
586 buffer_put_char(&b, have_sig);
587 } else {
588 buffer_put_cstring(&b, authctxt->method->name);
589 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000590 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000591 }
Damien Millereba71ba2000-04-29 23:57:08 +1000592 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000593
594 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000595 ret = (*sign_callback)(authctxt, k, &signature, &slen,
596 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000597 if (ret == -1) {
598 xfree(blob);
599 buffer_free(&b);
600 return 0;
601 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100602#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000603 buffer_dump(&b);
604#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000605 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000606 buffer_clear(&b);
607 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000608 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000609 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100610 buffer_put_cstring(&b, authctxt->server_user);
611 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100612 buffer_put_cstring(&b, authctxt->method->name);
613 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000614 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000615 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000616 buffer_put_string(&b, blob, bloblen);
617 }
618 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000619
Damien Millereba71ba2000-04-29 23:57:08 +1000620 /* append signature */
621 buffer_put_string(&b, signature, slen);
622 xfree(signature);
623
624 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000625 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100626 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000627 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000628
629 /* put remaining data from buffer into packet */
630 packet_start(SSH2_MSG_USERAUTH_REQUEST);
631 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
632 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000633 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000634
635 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000636}
637
Ben Lindstrombba81212001-06-25 05:01:22 +0000638static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000639send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
640 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000641{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000642 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000643 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000644
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000645 debug3("send_pubkey_test");
646
647 if (key_to_blob(k, &blob, &bloblen) == 0) {
648 /* we cannot handle this key */
649 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000650 return 0;
651 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000652 /* register callback for USERAUTH_PK_OK message */
653 authctxt->last_key_sign = sign_callback;
654 authctxt->last_key_hint = hint;
655 authctxt->last_key = k;
656 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000657
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000658 packet_start(SSH2_MSG_USERAUTH_REQUEST);
659 packet_put_cstring(authctxt->server_user);
660 packet_put_cstring(authctxt->service);
661 packet_put_cstring(authctxt->method->name);
662 packet_put_char(have_sig);
663 if (!(datafellows & SSH_BUG_PKAUTH))
664 packet_put_cstring(key_ssh_name(k));
665 packet_put_string(blob, bloblen);
666 xfree(blob);
667 packet_send();
668 return 1;
669}
670
Ben Lindstrombba81212001-06-25 05:01:22 +0000671static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000672load_identity_file(char *filename)
673{
674 Key *private;
675 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000676 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000677 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000678
Ben Lindstrom329782e2001-03-10 17:08:59 +0000679 if (stat(filename, &st) < 0) {
680 debug3("no such identity: %s", filename);
681 return NULL;
682 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000683 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
684 if (private == NULL) {
685 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000686 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000687 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100688 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100689 for (i = 0; i < options.number_of_password_prompts; i++) {
690 passphrase = read_passphrase(prompt, 0);
691 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000692 private = key_load_private_type(KEY_UNSPEC, filename,
693 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000694 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100695 } else {
696 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000697 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100698 }
699 memset(passphrase, 0, strlen(passphrase));
700 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000701 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100702 break;
703 debug2("bad passphrase given, try again...");
704 }
Damien Miller994cf142000-07-21 10:19:44 +1000705 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000706 return private;
707}
708
Ben Lindstrombba81212001-06-25 05:01:22 +0000709static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000710identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
711 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000712{
713 Key *private;
714 int idx, ret;
715
716 idx = authctxt->last_key_hint;
717 if (idx < 0)
718 return -1;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000719
720 /* private key is stored in external hardware */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100721 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000722 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
723
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000724 private = load_identity_file(options.identity_files[idx]);
725 if (private == NULL)
726 return -1;
727 ret = key_sign(private, sigp, lenp, data, datalen);
728 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000729 return ret;
730}
731
Ben Lindstrombba81212001-06-25 05:01:22 +0000732static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000733agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
734 u_char *data, u_int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000735{
Damien Miller62cee002000-09-23 17:15:56 +1100736 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000737}
738
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000740key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
741 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000742{
Ben Lindstroma3700052001-04-05 23:26:32 +0000743 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000744}
745
Ben Lindstrombba81212001-06-25 05:01:22 +0000746static int
Damien Miller62cee002000-09-23 17:15:56 +1100747userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000748{
749 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100750 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000751 char *comment;
752 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000753
754 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100755 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
756 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100757 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000758 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100759 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100760 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100761 debug2("userauth_pubkey_agent: no more keys");
762 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000763 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100764 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000765 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
766 if (ret == 0)
767 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100768 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100769 if (ret == 0)
770 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000771 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000772}
773
Damien Miller62cee002000-09-23 17:15:56 +1100774int
775userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000776{
Damien Miller62cee002000-09-23 17:15:56 +1100777 static int idx = 0;
778 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000779 Key *key;
780 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000781
Damien Miller0bc1bd82000-11-13 22:57:25 +1100782 if (authctxt->agent != NULL) {
783 do {
784 sent = userauth_pubkey_agent(authctxt);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000785 } while (!sent && authctxt->agent->howmany > 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100786 }
787 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000788 key = options.identity_keys[idx];
789 filename = options.identity_files[idx];
790 if (key == NULL) {
791 debug("try privkey: %s", filename);
792 key = load_identity_file(filename);
793 if (key != NULL) {
794 sent = sign_and_send_pubkey(authctxt, key,
795 key_sign_cb);
796 key_free(key);
797 }
798 } else if (key->type != KEY_RSA1) {
799 debug("try pubkey: %s", filename);
800 sent = send_pubkey_test(authctxt, key,
801 identity_sign_cb, idx);
802 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100803 idx++;
804 }
Damien Miller62cee002000-09-23 17:15:56 +1100805 return sent;
806}
Damien Millereba71ba2000-04-29 23:57:08 +1000807
Damien Miller874d77b2000-10-14 16:23:11 +1100808/*
809 * Send userauth request message specifying keyboard-interactive method.
810 */
811int
812userauth_kbdint(Authctxt *authctxt)
813{
814 static int attempt = 0;
815
816 if (attempt++ >= options.number_of_password_prompts)
817 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000818 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
819 if (attempt > 1 && !authctxt->info_req_seen) {
820 debug3("userauth_kbdint: disable: no info_req_seen");
821 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
822 return 0;
823 }
Damien Miller874d77b2000-10-14 16:23:11 +1100824
825 debug2("userauth_kbdint");
826 packet_start(SSH2_MSG_USERAUTH_REQUEST);
827 packet_put_cstring(authctxt->server_user);
828 packet_put_cstring(authctxt->service);
829 packet_put_cstring(authctxt->method->name);
830 packet_put_cstring(""); /* lang */
831 packet_put_cstring(options.kbd_interactive_devices ?
832 options.kbd_interactive_devices : "");
833 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100834
835 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
836 return 1;
837}
838
839/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000840 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100841 */
842void
Damien Miller630d6f42002-01-22 23:17:30 +1100843input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100844{
845 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000846 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000847 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100848 int echo = 0;
849
850 debug2("input_userauth_info_req");
851
852 if (authctxt == NULL)
853 fatal("input_userauth_info_req: no authentication context");
854
Ben Lindstrom7d199962001-09-12 18:29:00 +0000855 authctxt->info_req_seen = 1;
856
Damien Miller874d77b2000-10-14 16:23:11 +1100857 name = packet_get_string(NULL);
858 inst = packet_get_string(NULL);
859 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100860 if (strlen(name) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000861 log("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100862 if (strlen(inst) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000863 log("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000864 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100865 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000866 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100867
868 num_prompts = packet_get_int();
869 /*
870 * Begin to build info response packet based on prompts requested.
871 * We commit to providing the correct number of responses, so if
872 * further on we run into a problem that prevents this, we have to
873 * be sure and clean this up and send a correct error response.
874 */
875 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
876 packet_put_int(num_prompts);
877
Ben Lindstrom551ea372001-06-05 18:56:16 +0000878 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100879 for (i = 0; i < num_prompts; i++) {
880 prompt = packet_get_string(NULL);
881 echo = packet_get_char();
882
Ben Lindstrom949974b2001-06-25 05:20:31 +0000883 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100884
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000885 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100886 memset(response, 0, strlen(response));
887 xfree(response);
888 xfree(prompt);
889 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100890 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100891
Damien Miller9f643902001-11-12 11:02:52 +1100892 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100893 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100894}
Damien Miller62cee002000-09-23 17:15:56 +1100895
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000896/*
897 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
898 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
899 */
900int
901userauth_hostbased(Authctxt *authctxt)
902{
903 Key *private = NULL;
904 Buffer b;
905 u_char *signature, *blob;
906 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000907 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000908 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000909 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000910
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000911 /* check for a useful key */
912 for (i = 0; i < authctxt->nkeys; i++) {
913 private = authctxt->keys[i];
914 if (private && private->type != KEY_RSA1) {
915 found = 1;
916 /* we take and free the key */
917 authctxt->keys[i] = NULL;
918 break;
919 }
920 }
921 if (!found) {
Damien Miller91c18472001-11-12 11:02:03 +1100922 debug("userauth_hostbased: no more client hostkeys");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000923 return 0;
924 }
925 if (key_to_blob(private, &blob, &blen) == 0) {
926 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000927 return 0;
928 }
Damien Miller91c18472001-11-12 11:02:03 +1100929 /* figure out a name for the client host */
930 p = get_local_name(packet_get_connection_in());
931 if (p == NULL) {
932 error("userauth_hostbased: cannot get local ipaddr/name");
933 key_free(private);
934 return 0;
935 }
936 len = strlen(p) + 2;
937 chost = xmalloc(len);
938 strlcpy(chost, p, len);
939 strlcat(chost, ".", len);
940 debug2("userauth_hostbased: chost %s", chost);
941
Ben Lindstrom671388f2001-04-19 20:40:45 +0000942 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
943 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000944 pkalg = xstrdup(key_ssh_name(private));
945 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000946 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +0000947 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000948 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
949 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000950 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000951 buffer_put_cstring(&b, authctxt->method->name);
952 buffer_put_cstring(&b, pkalg);
953 buffer_put_string(&b, blob, blen);
954 buffer_put_cstring(&b, chost);
955 buffer_put_cstring(&b, authctxt->local_user);
956#ifdef DEBUG_PK
957 buffer_dump(&b);
958#endif
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000959 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
960 key_free(private);
961 buffer_free(&b);
962 if (ok != 0) {
963 error("key_sign failed");
964 xfree(chost);
965 xfree(pkalg);
966 return 0;
967 }
968 packet_start(SSH2_MSG_USERAUTH_REQUEST);
969 packet_put_cstring(authctxt->server_user);
970 packet_put_cstring(authctxt->service);
971 packet_put_cstring(authctxt->method->name);
972 packet_put_cstring(pkalg);
973 packet_put_string(blob, blen);
974 packet_put_cstring(chost);
975 packet_put_cstring(authctxt->local_user);
976 packet_put_string(signature, slen);
977 memset(signature, 's', slen);
978 xfree(signature);
979 xfree(chost);
980 xfree(pkalg);
981
982 packet_send();
983 return 1;
984}
985
Damien Miller62cee002000-09-23 17:15:56 +1100986/* find auth method */
987
Damien Miller62cee002000-09-23 17:15:56 +1100988/*
989 * given auth method name, if configurable options permit this method fill
990 * in auth_ident field and return true, otherwise return false.
991 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000992static int
Damien Miller62cee002000-09-23 17:15:56 +1100993authmethod_is_enabled(Authmethod *method)
994{
995 if (method == NULL)
996 return 0;
997 /* return false if options indicate this method is disabled */
998 if (method->enabled == NULL || *method->enabled == 0)
999 return 0;
1000 /* return false if batch mode is enabled but method needs interactive mode */
1001 if (method->batch_flag != NULL && *method->batch_flag != 0)
1002 return 0;
1003 return 1;
1004}
1005
Ben Lindstrombba81212001-06-25 05:01:22 +00001006static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001007authmethod_lookup(const char *name)
1008{
1009 Authmethod *method = NULL;
1010 if (name != NULL)
1011 for (method = authmethods; method->name != NULL; method++)
1012 if (strcmp(name, method->name) == 0)
1013 return method;
1014 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1015 return NULL;
1016}
1017
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001018/* XXX internal state */
1019static Authmethod *current = NULL;
1020static char *supported = NULL;
1021static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001022/*
1023 * Given the authentication method list sent by the server, return the
1024 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001025 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001026 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001027static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001028authmethod_get(char *authlist)
1029{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001030
1031 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001032 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001033
Damien Miller62cee002000-09-23 17:15:56 +11001034 /* Use a suitable default if we're passed a nil list. */
1035 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001036 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001037
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001038 if (supported == NULL || strcmp(authlist, supported) != 0) {
1039 debug3("start over, passed a different list %s", authlist);
1040 if (supported != NULL)
1041 xfree(supported);
1042 supported = xstrdup(authlist);
1043 preferred = options.preferred_authentications;
1044 debug3("preferred %s", preferred);
1045 current = NULL;
1046 } else if (current != NULL && authmethod_is_enabled(current))
1047 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001048
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001049 for (;;) {
1050 if ((name = match_list(preferred, supported, &next)) == NULL) {
1051 debug("no more auth methods to try");
1052 current = NULL;
1053 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001054 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001055 preferred += next;
1056 debug3("authmethod_lookup %s", name);
1057 debug3("remaining preferred: %s", preferred);
1058 if ((current = authmethod_lookup(name)) != NULL &&
1059 authmethod_is_enabled(current)) {
1060 debug3("authmethod_is_enabled %s", name);
1061 debug("next auth method to try is %s", name);
1062 return current;
1063 }
Damien Millereba71ba2000-04-29 23:57:08 +10001064 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001065}
Damien Miller62cee002000-09-23 17:15:56 +11001066
Ben Lindstrom79073822001-07-04 03:42:30 +00001067static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001068authmethods_get(void)
1069{
1070 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001071 Buffer b;
1072 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001073
Damien Miller0e3b8722002-01-22 23:26:38 +11001074 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001075 for (method = authmethods; method->name != NULL; method++) {
1076 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001077 if (buffer_len(&b) > 0)
1078 buffer_append(&b, ",", 1);
1079 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001080 }
Damien Miller62cee002000-09-23 17:15:56 +11001081 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001082 buffer_append(&b, "\0", 1);
1083 list = xstrdup(buffer_ptr(&b));
1084 buffer_free(&b);
1085 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001086}