blob: 1f57c3a9f5da40ce7f602cdc94a08024e4f340a6 [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 Lindstrombba81212001-06-25 05:01:22 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.76 2001/06/23 15:12:21 itojun Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100029#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "uidswap.h"
40#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110048#include "cli.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000049#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100050#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000054#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000055#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000056#include "canohost.h"
Damien Miller874d77b2000-10-14 16:23:11 +110057
Damien Millereba71ba2000-04-29 23:57:08 +100058/* import */
59extern char *client_version_string;
60extern char *server_version_string;
61extern Options options;
62
63/*
64 * SSH2 key exchange
65 */
66
Ben Lindstrom46c16222000-12-22 01:43:59 +000067u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100068int session_id2_len = 0;
69
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000070char *xxx_host;
71struct sockaddr *xxx_hostaddr;
72
Ben Lindstromf28f6342001-04-04 02:03:04 +000073Kex *xxx_kex = NULL;
74
Ben Lindstrombba81212001-06-25 05:01:22 +000075static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000076verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000077{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000078 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
79 fatal("verify_host_key failed");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000080 return 0;
81}
82
Damien Millereba71ba2000-04-29 23:57:08 +100083void
Damien Miller874d77b2000-10-14 16:23:11 +110084ssh_kex2(char *host, struct sockaddr *hostaddr)
85{
Damien Miller874d77b2000-10-14 16:23:11 +110086 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000087
88 xxx_host = host;
89 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110090
Damien Millere39cacc2000-11-29 12:18:44 +110091 if (options.ciphers == (char *)-1) {
92 log("No valid ciphers for protocol version 2 given, using defaults.");
93 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110094 }
95 if (options.ciphers != NULL) {
96 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
97 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
98 }
Damien Millera0ff4662001-03-30 10:49:35 +100099 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
100 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
101 myproposal[PROPOSAL_ENC_ALGS_STOC] =
102 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100103 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000104 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100105 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
106 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000107 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100108 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
109 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000110 if (options.macs != NULL) {
111 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
112 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
113 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000114 if (options.hostkeyalgorithms != NULL)
115 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
116 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100117
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000118 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000119 kex = kex_setup(myproposal);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000120 kex->client_version_string=client_version_string;
121 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000122 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100123
Ben Lindstromf28f6342001-04-04 02:03:04 +0000124 xxx_kex = kex;
125
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000126 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100127
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000128 session_id2 = kex->session_id;
129 session_id2_len = kex->session_id_len;
130
Damien Miller874d77b2000-10-14 16:23:11 +1100131#ifdef DEBUG_KEXDH
132 /* send 1st encrypted/maced/compressed message */
133 packet_start(SSH2_MSG_IGNORE);
134 packet_put_cstring("markus");
135 packet_send();
136 packet_write_wait();
137#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000138 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000139}
Damien Millerb1715dc2000-05-30 13:44:51 +1000140
Damien Millereba71ba2000-04-29 23:57:08 +1000141/*
142 * Authenticate user
143 */
Damien Miller62cee002000-09-23 17:15:56 +1100144
145typedef struct Authctxt Authctxt;
146typedef struct Authmethod Authmethod;
147
148typedef int sign_cb_fn(
149 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000150 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100151
152struct Authctxt {
153 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000154 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100155 const char *host;
156 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100157 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100158 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000159 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000160 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000161 Key *last_key;
162 sign_cb_fn *last_key_sign;
163 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000164 AuthenticationConnection *agent;
165 /* hostbased */
166 Key **keys;
167 int nkeys;
Damien Miller62cee002000-09-23 17:15:56 +1100168};
169struct Authmethod {
170 char *name; /* string to compare against server's list */
171 int (*userauth)(Authctxt *authctxt);
172 int *enabled; /* flag in option struct that enables method */
173 int *batch_flag; /* flag in option struct that disables method */
174};
175
176void input_userauth_success(int type, int plen, void *ctxt);
177void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000178void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100179void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100180void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000181void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100182
183int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100184int userauth_pubkey(Authctxt *authctxt);
185int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100186int userauth_kbdint(Authctxt *authctxt);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000187int userauth_hostbased(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100188
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000189void userauth(Authctxt *authctxt, char *authlist);
190
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
192static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000193
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static Authmethod *authmethod_get(char *authlist);
195static Authmethod *authmethod_lookup(const char *name);
196static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100197
198Authmethod authmethods[] = {
199 {"publickey",
200 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100201 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100202 NULL},
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000203 {"hostbased",
204 userauth_hostbased,
205 &options.hostbased_authentication,
206 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100207 {"password",
208 userauth_passwd,
209 &options.password_authentication,
210 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100211 {"keyboard-interactive",
212 userauth_kbdint,
213 &options.kbd_interactive_authentication,
214 &options.batch_mode},
215 {"none",
216 userauth_none,
217 NULL,
218 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100219 {NULL, NULL, NULL, NULL}
220};
221
222void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000223ssh_userauth2(const char *local_user, const char *server_user, char *host,
224 Key **keys, int nkeys)
Damien Miller62cee002000-09-23 17:15:56 +1100225{
226 Authctxt authctxt;
227 int type;
228 int plen;
229
Ben Lindstrom551ea372001-06-05 18:56:16 +0000230 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000231 options.kbd_interactive_authentication = 1;
232
Damien Miller62cee002000-09-23 17:15:56 +1100233 debug("send SSH2_MSG_SERVICE_REQUEST");
234 packet_start(SSH2_MSG_SERVICE_REQUEST);
235 packet_put_cstring("ssh-userauth");
236 packet_send();
237 packet_write_wait();
238 type = packet_read(&plen);
239 if (type != SSH2_MSG_SERVICE_ACCEPT) {
240 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
241 }
242 if (packet_remaining() > 0) {
243 char *reply = packet_get_string(&plen);
244 debug("service_accept: %s", reply);
245 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100246 } else {
247 debug("buggy server: service_accept w/o service");
248 }
249 packet_done();
250 debug("got SSH2_MSG_SERVICE_ACCEPT");
251
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000252 if (options.preferred_authentications == NULL)
253 options.preferred_authentications = authmethods_get();
254
Damien Miller62cee002000-09-23 17:15:56 +1100255 /* setup authentication context */
256 authctxt.agent = ssh_get_authentication_connection();
257 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000258 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100259 authctxt.host = host;
260 authctxt.service = "ssh-connection"; /* service name */
261 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100262 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000263 authctxt.authlist = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 authctxt.keys = keys;
265 authctxt.nkeys = nkeys;
Damien Miller874d77b2000-10-14 16:23:11 +1100266 if (authctxt.method == NULL)
267 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100268
269 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100270 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100271
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000272 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100273 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
274 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000275 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100276 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
277
278 if (authctxt.agent != NULL)
279 ssh_close_authentication_connection(authctxt.agent);
280
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000281 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100282}
283void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000284userauth(Authctxt *authctxt, char *authlist)
285{
286 if (authlist == NULL) {
287 authlist = authctxt->authlist;
288 } else {
289 if (authctxt->authlist)
290 xfree(authctxt->authlist);
291 authctxt->authlist = authlist;
292 }
293 for (;;) {
294 Authmethod *method = authmethod_get(authlist);
295 if (method == NULL)
296 fatal("Permission denied (%s).", authlist);
297 authctxt->method = method;
298 if (method->userauth(authctxt) != 0) {
299 debug2("we sent a %s packet, wait for reply", method->name);
300 break;
301 } else {
302 debug2("we did not send a packet, disable method");
303 method->enabled = NULL;
304 }
305 }
306}
307void
Damien Miller62cee002000-09-23 17:15:56 +1100308input_userauth_error(int type, int plen, void *ctxt)
309{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000310 fatal("input_userauth_error: bad message during authentication: "
311 "type %d", type);
312}
313void
314input_userauth_banner(int type, int plen, void *ctxt)
315{
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}
324void
325input_userauth_success(int type, int plen, void *ctxt)
326{
327 Authctxt *authctxt = ctxt;
328 if (authctxt == NULL)
329 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000330 if (authctxt->authlist)
331 xfree(authctxt->authlist);
332 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100333 authctxt->success = 1; /* break out */
334}
335void
336input_userauth_failure(int type, int plen, void *ctxt)
337{
Damien Miller62cee002000-09-23 17:15:56 +1100338 Authctxt *authctxt = ctxt;
339 char *authlist = NULL;
340 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100341
342 if (authctxt == NULL)
343 fatal("input_userauth_failure: no authentication context");
344
Damien Miller874d77b2000-10-14 16:23:11 +1100345 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100346 partial = packet_get_char();
347 packet_done();
348
349 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000350 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100351 debug("authentications that can continue: %s", authlist);
352
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000353 clear_auth_state(authctxt);
354 userauth(authctxt, authlist);
355}
356void
357input_userauth_pk_ok(int type, int plen, void *ctxt)
358{
359 Authctxt *authctxt = ctxt;
360 Key *key = NULL;
361 Buffer b;
Ben Lindstrom206941f2001-04-15 14:27:16 +0000362 int alen, blen, sent = 0;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000363 char *pkalg, *pkblob, *fp;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000364
365 if (authctxt == NULL)
366 fatal("input_userauth_pk_ok: no authentication context");
367 if (datafellows & SSH_BUG_PKOK) {
368 /* this is similar to SSH_BUG_PKAUTH */
369 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
370 pkblob = packet_get_string(&blen);
371 buffer_init(&b);
372 buffer_append(&b, pkblob, blen);
373 pkalg = buffer_get_string(&b, &alen);
374 buffer_free(&b);
375 } else {
376 pkalg = packet_get_string(&alen);
377 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000378 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000379 packet_done();
380
381 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
382 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
383
384 do {
385 if (authctxt->last_key == NULL ||
386 authctxt->last_key_sign == NULL) {
387 debug("no last key or no sign cb");
388 break;
389 }
Ben Lindstrom206941f2001-04-15 14:27:16 +0000390 if (key_type_from_name(pkalg) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000391 debug("unknown pkalg %s", pkalg);
392 break;
393 }
394 if ((key = key_from_blob(pkblob, blen)) == NULL) {
395 debug("no key from blob. pkalg %s", pkalg);
396 break;
397 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000398 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
399 debug2("input_userauth_pk_ok: fp %s", fp);
400 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000401 if (!key_equal(key, authctxt->last_key)) {
402 debug("key != last_key");
403 break;
404 }
405 sent = sign_and_send_pubkey(authctxt, key,
406 authctxt->last_key_sign);
407 } while(0);
408
409 if (key != NULL)
410 key_free(key);
411 xfree(pkalg);
412 xfree(pkblob);
413
414 /* unregister */
415 clear_auth_state(authctxt);
416 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
417
418 /* try another method if we did not send a packet*/
419 if (sent == 0)
420 userauth(authctxt, NULL);
421
Damien Miller62cee002000-09-23 17:15:56 +1100422}
423
Damien Millereba71ba2000-04-29 23:57:08 +1000424int
Damien Miller874d77b2000-10-14 16:23:11 +1100425userauth_none(Authctxt *authctxt)
426{
427 /* initial userauth request */
428 packet_start(SSH2_MSG_USERAUTH_REQUEST);
429 packet_put_cstring(authctxt->server_user);
430 packet_put_cstring(authctxt->service);
431 packet_put_cstring(authctxt->method->name);
432 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100433 return 1;
434}
435
436int
Damien Miller62cee002000-09-23 17:15:56 +1100437userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000438{
Damien Millere247cc42000-05-07 12:03:14 +1000439 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000440 char prompt[80];
441 char *password;
442
Damien Millerd3a18572000-06-07 19:55:44 +1000443 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000444 return 0;
445
Damien Millerd3a18572000-06-07 19:55:44 +1000446 if(attempt != 1)
447 error("Permission denied, please try again.");
448
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000449 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100450 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000451 password = read_passphrase(prompt, 0);
452 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100453 packet_put_cstring(authctxt->server_user);
454 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100455 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000456 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000457 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000458 memset(password, 0, strlen(password));
459 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000460 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000462 return 1;
463}
464
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000465void
466clear_auth_state(Authctxt *authctxt)
467{
468 /* XXX clear authentication state */
469 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
470 debug3("clear_auth_state: key_free %p", authctxt->last_key);
471 key_free(authctxt->last_key);
472 }
473 authctxt->last_key = NULL;
474 authctxt->last_key_hint = -2;
475 authctxt->last_key_sign = NULL;
476}
477
Ben Lindstrombba81212001-06-25 05:01:22 +0000478static int
Damien Miller62cee002000-09-23 17:15:56 +1100479sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000480{
481 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000482 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000483 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000484 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000485 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100486 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000487
Ben Lindstromd121f612000-12-03 17:00:47 +0000488 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000489
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 if (key_to_blob(k, &blob, &bloblen) == 0) {
491 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000492 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 return 0;
494 }
Damien Millereba71ba2000-04-29 23:57:08 +1000495 /* data to be signed */
496 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100497 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000498 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000499 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100500 } else {
501 buffer_put_string(&b, session_id2, session_id2_len);
502 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000503 }
Damien Millereba71ba2000-04-29 23:57:08 +1000504 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100505 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000506 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000507 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000508 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100509 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000510 if (datafellows & SSH_BUG_PKAUTH) {
511 buffer_put_char(&b, have_sig);
512 } else {
513 buffer_put_cstring(&b, authctxt->method->name);
514 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000515 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000516 }
Damien Millereba71ba2000-04-29 23:57:08 +1000517 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000518
519 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000520 ret = (*sign_callback)(authctxt, k, &signature, &slen,
521 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000522 if (ret == -1) {
523 xfree(blob);
524 buffer_free(&b);
525 return 0;
526 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100527#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000528 buffer_dump(&b);
529#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000530 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000531 buffer_clear(&b);
532 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000533 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000534 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100535 buffer_put_cstring(&b, authctxt->server_user);
536 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100537 buffer_put_cstring(&b, authctxt->method->name);
538 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000539 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000540 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000541 buffer_put_string(&b, blob, bloblen);
542 }
543 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000544
Damien Millereba71ba2000-04-29 23:57:08 +1000545 /* append signature */
546 buffer_put_string(&b, signature, slen);
547 xfree(signature);
548
549 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000550 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100551 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000552 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000553
554 /* put remaining data from buffer into packet */
555 packet_start(SSH2_MSG_USERAUTH_REQUEST);
556 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
557 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000558 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000559
560 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000561}
562
Ben Lindstrombba81212001-06-25 05:01:22 +0000563static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000564send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
565 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000566{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000567 u_char *blob;
568 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000569
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000570 debug3("send_pubkey_test");
571
572 if (key_to_blob(k, &blob, &bloblen) == 0) {
573 /* we cannot handle this key */
574 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000575 return 0;
576 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000577 /* register callback for USERAUTH_PK_OK message */
578 authctxt->last_key_sign = sign_callback;
579 authctxt->last_key_hint = hint;
580 authctxt->last_key = k;
581 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000582
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000583 packet_start(SSH2_MSG_USERAUTH_REQUEST);
584 packet_put_cstring(authctxt->server_user);
585 packet_put_cstring(authctxt->service);
586 packet_put_cstring(authctxt->method->name);
587 packet_put_char(have_sig);
588 if (!(datafellows & SSH_BUG_PKAUTH))
589 packet_put_cstring(key_ssh_name(k));
590 packet_put_string(blob, bloblen);
591 xfree(blob);
592 packet_send();
593 return 1;
594}
595
Ben Lindstrombba81212001-06-25 05:01:22 +0000596static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000597load_identity_file(char *filename)
598{
599 Key *private;
600 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000601 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000602 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000603
Ben Lindstrom329782e2001-03-10 17:08:59 +0000604 if (stat(filename, &st) < 0) {
605 debug3("no such identity: %s", filename);
606 return NULL;
607 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000608 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
609 if (private == NULL) {
610 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000611 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000612 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100613 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100614 for (i = 0; i < options.number_of_password_prompts; i++) {
615 passphrase = read_passphrase(prompt, 0);
616 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000617 private = key_load_private_type(KEY_UNSPEC, filename,
618 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000619 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100620 } else {
621 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000622 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100623 }
624 memset(passphrase, 0, strlen(passphrase));
625 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000626 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100627 break;
628 debug2("bad passphrase given, try again...");
629 }
Damien Miller994cf142000-07-21 10:19:44 +1000630 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000631 return private;
632}
633
Ben Lindstrombba81212001-06-25 05:01:22 +0000634static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000635identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
636 u_char *data, int datalen)
637{
638 Key *private;
639 int idx, ret;
640
641 idx = authctxt->last_key_hint;
642 if (idx < 0)
643 return -1;
644 private = load_identity_file(options.identity_files[idx]);
645 if (private == NULL)
646 return -1;
647 ret = key_sign(private, sigp, lenp, data, datalen);
648 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000649 return ret;
650}
651
Ben Lindstrombba81212001-06-25 05:01:22 +0000652static int
653agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000654 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000655{
Damien Miller62cee002000-09-23 17:15:56 +1100656 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000657}
658
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static int
660key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000661 u_char *data, int datalen)
662{
Ben Lindstroma3700052001-04-05 23:26:32 +0000663 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000664}
665
Ben Lindstrombba81212001-06-25 05:01:22 +0000666static int
Damien Miller62cee002000-09-23 17:15:56 +1100667userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000668{
669 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100670 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000671 char *comment;
672 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000673
674 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100675 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
676 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100677 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000678 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100680 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 debug2("userauth_pubkey_agent: no more keys");
682 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000683 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100684 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000685 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
686 if (ret == 0)
687 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100688 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100689 if (ret == 0)
690 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000691 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000692}
693
Damien Miller62cee002000-09-23 17:15:56 +1100694int
695userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000696{
Damien Miller62cee002000-09-23 17:15:56 +1100697 static int idx = 0;
698 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000699 Key *key;
700 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000701
Damien Miller0bc1bd82000-11-13 22:57:25 +1100702 if (authctxt->agent != NULL) {
703 do {
704 sent = userauth_pubkey_agent(authctxt);
705 } while(!sent && authctxt->agent->howmany > 0);
706 }
707 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000708 key = options.identity_keys[idx];
709 filename = options.identity_files[idx];
710 if (key == NULL) {
711 debug("try privkey: %s", filename);
712 key = load_identity_file(filename);
713 if (key != NULL) {
714 sent = sign_and_send_pubkey(authctxt, key,
715 key_sign_cb);
716 key_free(key);
717 }
718 } else if (key->type != KEY_RSA1) {
719 debug("try pubkey: %s", filename);
720 sent = send_pubkey_test(authctxt, key,
721 identity_sign_cb, idx);
722 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723 idx++;
724 }
Damien Miller62cee002000-09-23 17:15:56 +1100725 return sent;
726}
Damien Millereba71ba2000-04-29 23:57:08 +1000727
Damien Miller874d77b2000-10-14 16:23:11 +1100728/*
729 * Send userauth request message specifying keyboard-interactive method.
730 */
731int
732userauth_kbdint(Authctxt *authctxt)
733{
734 static int attempt = 0;
735
736 if (attempt++ >= options.number_of_password_prompts)
737 return 0;
738
739 debug2("userauth_kbdint");
740 packet_start(SSH2_MSG_USERAUTH_REQUEST);
741 packet_put_cstring(authctxt->server_user);
742 packet_put_cstring(authctxt->service);
743 packet_put_cstring(authctxt->method->name);
744 packet_put_cstring(""); /* lang */
745 packet_put_cstring(options.kbd_interactive_devices ?
746 options.kbd_interactive_devices : "");
747 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100748
749 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
750 return 1;
751}
752
753/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000754 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100755 */
756void
757input_userauth_info_req(int type, int plen, void *ctxt)
758{
759 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000760 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000761 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100762 int echo = 0;
763
764 debug2("input_userauth_info_req");
765
766 if (authctxt == NULL)
767 fatal("input_userauth_info_req: no authentication context");
768
769 name = packet_get_string(NULL);
770 inst = packet_get_string(NULL);
771 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100772 if (strlen(name) > 0)
773 cli_mesg(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100774 if (strlen(inst) > 0)
775 cli_mesg(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000776 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100777 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000778 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100779
780 num_prompts = packet_get_int();
781 /*
782 * Begin to build info response packet based on prompts requested.
783 * We commit to providing the correct number of responses, so if
784 * further on we run into a problem that prevents this, we have to
785 * be sure and clean this up and send a correct error response.
786 */
787 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
788 packet_put_int(num_prompts);
789
Ben Lindstrom551ea372001-06-05 18:56:16 +0000790 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100791 for (i = 0; i < num_prompts; i++) {
792 prompt = packet_get_string(NULL);
793 echo = packet_get_char();
794
795 response = cli_prompt(prompt, echo);
796
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000797 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100798 memset(response, 0, strlen(response));
799 xfree(response);
800 xfree(prompt);
801 }
802 packet_done(); /* done with parsing incoming message. */
803
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000804 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100805 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100806}
Damien Miller62cee002000-09-23 17:15:56 +1100807
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000808/*
809 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
810 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
811 */
812int
813userauth_hostbased(Authctxt *authctxt)
814{
815 Key *private = NULL;
816 Buffer b;
817 u_char *signature, *blob;
818 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000819 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000820 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000821 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000822
823 p = get_local_name(packet_get_connection_in());
824 if (p == NULL) {
825 error("userauth_hostbased: cannot get local ipaddr/name");
826 return 0;
827 }
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000828 len = strlen(p) + 2;
829 chost = xmalloc(len);
830 strlcpy(chost, p, len);
831 strlcat(chost, ".", len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000832 debug2("userauth_hostbased: chost %s", chost);
833 /* check for a useful key */
834 for (i = 0; i < authctxt->nkeys; i++) {
835 private = authctxt->keys[i];
836 if (private && private->type != KEY_RSA1) {
837 found = 1;
838 /* we take and free the key */
839 authctxt->keys[i] = NULL;
840 break;
841 }
842 }
843 if (!found) {
844 xfree(chost);
845 return 0;
846 }
847 if (key_to_blob(private, &blob, &blen) == 0) {
848 key_free(private);
849 xfree(chost);
850 return 0;
851 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000852 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
853 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000854 pkalg = xstrdup(key_ssh_name(private));
855 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000856 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +0000857 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000858 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
859 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000860 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000861 buffer_put_cstring(&b, authctxt->method->name);
862 buffer_put_cstring(&b, pkalg);
863 buffer_put_string(&b, blob, blen);
864 buffer_put_cstring(&b, chost);
865 buffer_put_cstring(&b, authctxt->local_user);
866#ifdef DEBUG_PK
867 buffer_dump(&b);
868#endif
869 debug2("xxx: chost %s", chost);
870 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
871 key_free(private);
872 buffer_free(&b);
873 if (ok != 0) {
874 error("key_sign failed");
875 xfree(chost);
876 xfree(pkalg);
877 return 0;
878 }
879 packet_start(SSH2_MSG_USERAUTH_REQUEST);
880 packet_put_cstring(authctxt->server_user);
881 packet_put_cstring(authctxt->service);
882 packet_put_cstring(authctxt->method->name);
883 packet_put_cstring(pkalg);
884 packet_put_string(blob, blen);
885 packet_put_cstring(chost);
886 packet_put_cstring(authctxt->local_user);
887 packet_put_string(signature, slen);
888 memset(signature, 's', slen);
889 xfree(signature);
890 xfree(chost);
891 xfree(pkalg);
892
893 packet_send();
894 return 1;
895}
896
Damien Miller62cee002000-09-23 17:15:56 +1100897/* find auth method */
898
Damien Miller62cee002000-09-23 17:15:56 +1100899/*
900 * given auth method name, if configurable options permit this method fill
901 * in auth_ident field and return true, otherwise return false.
902 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000903static int
Damien Miller62cee002000-09-23 17:15:56 +1100904authmethod_is_enabled(Authmethod *method)
905{
906 if (method == NULL)
907 return 0;
908 /* return false if options indicate this method is disabled */
909 if (method->enabled == NULL || *method->enabled == 0)
910 return 0;
911 /* return false if batch mode is enabled but method needs interactive mode */
912 if (method->batch_flag != NULL && *method->batch_flag != 0)
913 return 0;
914 return 1;
915}
916
Ben Lindstrombba81212001-06-25 05:01:22 +0000917static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +1100918authmethod_lookup(const char *name)
919{
920 Authmethod *method = NULL;
921 if (name != NULL)
922 for (method = authmethods; method->name != NULL; method++)
923 if (strcmp(name, method->name) == 0)
924 return method;
925 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
926 return NULL;
927}
928
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000929/* XXX internal state */
930static Authmethod *current = NULL;
931static char *supported = NULL;
932static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100933/*
934 * Given the authentication method list sent by the server, return the
935 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +0000936 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +0000937 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000938static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +1100939authmethod_get(char *authlist)
940{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000941
942 char *name = NULL;
943 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000944
Damien Miller62cee002000-09-23 17:15:56 +1100945 /* Use a suitable default if we're passed a nil list. */
946 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000947 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +1100948
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000949 if (supported == NULL || strcmp(authlist, supported) != 0) {
950 debug3("start over, passed a different list %s", authlist);
951 if (supported != NULL)
952 xfree(supported);
953 supported = xstrdup(authlist);
954 preferred = options.preferred_authentications;
955 debug3("preferred %s", preferred);
956 current = NULL;
957 } else if (current != NULL && authmethod_is_enabled(current))
958 return current;
Damien Millereba71ba2000-04-29 23:57:08 +1000959
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000960 for (;;) {
961 if ((name = match_list(preferred, supported, &next)) == NULL) {
962 debug("no more auth methods to try");
963 current = NULL;
964 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100965 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000966 preferred += next;
967 debug3("authmethod_lookup %s", name);
968 debug3("remaining preferred: %s", preferred);
969 if ((current = authmethod_lookup(name)) != NULL &&
970 authmethod_is_enabled(current)) {
971 debug3("authmethod_is_enabled %s", name);
972 debug("next auth method to try is %s", name);
973 return current;
974 }
Damien Millereba71ba2000-04-29 23:57:08 +1000975 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000976}
Damien Miller62cee002000-09-23 17:15:56 +1100977
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000978
979#define DELIM ","
980char *
981authmethods_get(void)
982{
983 Authmethod *method = NULL;
984 char buf[1024];
985
986 buf[0] = '\0';
987 for (method = authmethods; method->name != NULL; method++) {
988 if (authmethod_is_enabled(method)) {
989 if (buf[0] != '\0')
990 strlcat(buf, DELIM, sizeof buf);
991 strlcat(buf, method->name, sizeof buf);
992 }
Damien Miller62cee002000-09-23 17:15:56 +1100993 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000994 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000995}