blob: 5f4943ba80faadefddb8c0b19f42f46481d00b80 [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 Lindstrom949974b2001-06-25 05:20:31 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.77 2001/06/24 05:35:34 markus 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"
Ben Lindstromdf221392001-03-29 00:36:16 +000048#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100049#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "log.h"
51#include "readconf.h"
52#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000053#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000054#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000055#include "canohost.h"
Damien Miller874d77b2000-10-14 16:23:11 +110056
Damien Millereba71ba2000-04-29 23:57:08 +100057/* import */
58extern char *client_version_string;
59extern char *server_version_string;
60extern Options options;
61
62/*
63 * SSH2 key exchange
64 */
65
Ben Lindstrom46c16222000-12-22 01:43:59 +000066u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100067int session_id2_len = 0;
68
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000069char *xxx_host;
70struct sockaddr *xxx_hostaddr;
71
Ben Lindstromf28f6342001-04-04 02:03:04 +000072Kex *xxx_kex = NULL;
73
Ben Lindstrombba81212001-06-25 05:01:22 +000074static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000075verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000076{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000077 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
78 fatal("verify_host_key failed");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000079 return 0;
80}
81
Damien Millereba71ba2000-04-29 23:57:08 +100082void
Damien Miller874d77b2000-10-14 16:23:11 +110083ssh_kex2(char *host, struct sockaddr *hostaddr)
84{
Damien Miller874d77b2000-10-14 16:23:11 +110085 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000086
87 xxx_host = host;
88 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110089
Damien Millere39cacc2000-11-29 12:18:44 +110090 if (options.ciphers == (char *)-1) {
91 log("No valid ciphers for protocol version 2 given, using defaults.");
92 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110093 }
94 if (options.ciphers != NULL) {
95 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
96 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
97 }
Damien Millera0ff4662001-03-30 10:49:35 +100098 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100102 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100104 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
105 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
108 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000109 if (options.macs != NULL) {
110 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
111 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
112 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000113 if (options.hostkeyalgorithms != NULL)
114 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
115 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100116
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000117 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000118 kex = kex_setup(myproposal);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000119 kex->client_version_string=client_version_string;
120 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000121 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100122
Ben Lindstromf28f6342001-04-04 02:03:04 +0000123 xxx_kex = kex;
124
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000125 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100126
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000127 session_id2 = kex->session_id;
128 session_id2_len = kex->session_id_len;
129
Damien Miller874d77b2000-10-14 16:23:11 +1100130#ifdef DEBUG_KEXDH
131 /* send 1st encrypted/maced/compressed message */
132 packet_start(SSH2_MSG_IGNORE);
133 packet_put_cstring("markus");
134 packet_send();
135 packet_write_wait();
136#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000137 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000138}
Damien Millerb1715dc2000-05-30 13:44:51 +1000139
Damien Millereba71ba2000-04-29 23:57:08 +1000140/*
141 * Authenticate user
142 */
Damien Miller62cee002000-09-23 17:15:56 +1100143
144typedef struct Authctxt Authctxt;
145typedef struct Authmethod Authmethod;
146
147typedef int sign_cb_fn(
148 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000149 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100150
151struct Authctxt {
152 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000153 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100154 const char *host;
155 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100156 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100157 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000158 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000159 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000160 Key *last_key;
161 sign_cb_fn *last_key_sign;
162 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000163 AuthenticationConnection *agent;
164 /* hostbased */
165 Key **keys;
166 int nkeys;
Damien Miller62cee002000-09-23 17:15:56 +1100167};
168struct Authmethod {
169 char *name; /* string to compare against server's list */
170 int (*userauth)(Authctxt *authctxt);
171 int *enabled; /* flag in option struct that enables method */
172 int *batch_flag; /* flag in option struct that disables method */
173};
174
175void input_userauth_success(int type, int plen, void *ctxt);
176void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000177void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100178void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100179void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000180void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100181
182int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100183int userauth_pubkey(Authctxt *authctxt);
184int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100185int userauth_kbdint(Authctxt *authctxt);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000186int userauth_hostbased(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100187
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000188void userauth(Authctxt *authctxt, char *authlist);
189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
191static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static Authmethod *authmethod_get(char *authlist);
194static Authmethod *authmethod_lookup(const char *name);
195static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100196
197Authmethod authmethods[] = {
198 {"publickey",
199 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100201 NULL},
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000202 {"hostbased",
203 userauth_hostbased,
204 &options.hostbased_authentication,
205 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100206 {"password",
207 userauth_passwd,
208 &options.password_authentication,
209 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100210 {"keyboard-interactive",
211 userauth_kbdint,
212 &options.kbd_interactive_authentication,
213 &options.batch_mode},
214 {"none",
215 userauth_none,
216 NULL,
217 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100218 {NULL, NULL, NULL, NULL}
219};
220
221void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000222ssh_userauth2(const char *local_user, const char *server_user, char *host,
223 Key **keys, int nkeys)
Damien Miller62cee002000-09-23 17:15:56 +1100224{
225 Authctxt authctxt;
226 int type;
227 int plen;
228
Ben Lindstrom551ea372001-06-05 18:56:16 +0000229 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000230 options.kbd_interactive_authentication = 1;
231
Damien Miller62cee002000-09-23 17:15:56 +1100232 debug("send SSH2_MSG_SERVICE_REQUEST");
233 packet_start(SSH2_MSG_SERVICE_REQUEST);
234 packet_put_cstring("ssh-userauth");
235 packet_send();
236 packet_write_wait();
237 type = packet_read(&plen);
238 if (type != SSH2_MSG_SERVICE_ACCEPT) {
239 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
240 }
241 if (packet_remaining() > 0) {
242 char *reply = packet_get_string(&plen);
243 debug("service_accept: %s", reply);
244 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100245 } else {
246 debug("buggy server: service_accept w/o service");
247 }
248 packet_done();
249 debug("got SSH2_MSG_SERVICE_ACCEPT");
250
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000251 if (options.preferred_authentications == NULL)
252 options.preferred_authentications = authmethods_get();
253
Damien Miller62cee002000-09-23 17:15:56 +1100254 /* setup authentication context */
255 authctxt.agent = ssh_get_authentication_connection();
256 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000257 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100258 authctxt.host = host;
259 authctxt.service = "ssh-connection"; /* service name */
260 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100261 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000262 authctxt.authlist = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263 authctxt.keys = keys;
264 authctxt.nkeys = nkeys;
Damien Miller874d77b2000-10-14 16:23:11 +1100265 if (authctxt.method == NULL)
266 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100267
268 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100269 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100270
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000271 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100272 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
273 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000274 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100275 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
276
277 if (authctxt.agent != NULL)
278 ssh_close_authentication_connection(authctxt.agent);
279
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000280 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100281}
282void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000283userauth(Authctxt *authctxt, char *authlist)
284{
285 if (authlist == NULL) {
286 authlist = authctxt->authlist;
287 } else {
288 if (authctxt->authlist)
289 xfree(authctxt->authlist);
290 authctxt->authlist = authlist;
291 }
292 for (;;) {
293 Authmethod *method = authmethod_get(authlist);
294 if (method == NULL)
295 fatal("Permission denied (%s).", authlist);
296 authctxt->method = method;
297 if (method->userauth(authctxt) != 0) {
298 debug2("we sent a %s packet, wait for reply", method->name);
299 break;
300 } else {
301 debug2("we did not send a packet, disable method");
302 method->enabled = NULL;
303 }
304 }
305}
306void
Damien Miller62cee002000-09-23 17:15:56 +1100307input_userauth_error(int type, int plen, void *ctxt)
308{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000309 fatal("input_userauth_error: bad message during authentication: "
310 "type %d", type);
311}
312void
313input_userauth_banner(int type, int plen, void *ctxt)
314{
315 char *msg, *lang;
316 debug3("input_userauth_banner");
317 msg = packet_get_string(NULL);
318 lang = packet_get_string(NULL);
319 fprintf(stderr, "%s", msg);
320 xfree(msg);
321 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100322}
323void
324input_userauth_success(int type, int plen, void *ctxt)
325{
326 Authctxt *authctxt = ctxt;
327 if (authctxt == NULL)
328 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000329 if (authctxt->authlist)
330 xfree(authctxt->authlist);
331 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100332 authctxt->success = 1; /* break out */
333}
334void
335input_userauth_failure(int type, int plen, void *ctxt)
336{
Damien Miller62cee002000-09-23 17:15:56 +1100337 Authctxt *authctxt = ctxt;
338 char *authlist = NULL;
339 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100340
341 if (authctxt == NULL)
342 fatal("input_userauth_failure: no authentication context");
343
Damien Miller874d77b2000-10-14 16:23:11 +1100344 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100345 partial = packet_get_char();
346 packet_done();
347
348 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000349 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100350 debug("authentications that can continue: %s", authlist);
351
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000352 clear_auth_state(authctxt);
353 userauth(authctxt, authlist);
354}
355void
356input_userauth_pk_ok(int type, int plen, void *ctxt)
357{
358 Authctxt *authctxt = ctxt;
359 Key *key = NULL;
360 Buffer b;
Ben Lindstrom206941f2001-04-15 14:27:16 +0000361 int alen, blen, sent = 0;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000362 char *pkalg, *pkblob, *fp;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000363
364 if (authctxt == NULL)
365 fatal("input_userauth_pk_ok: no authentication context");
366 if (datafellows & SSH_BUG_PKOK) {
367 /* this is similar to SSH_BUG_PKAUTH */
368 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
369 pkblob = packet_get_string(&blen);
370 buffer_init(&b);
371 buffer_append(&b, pkblob, blen);
372 pkalg = buffer_get_string(&b, &alen);
373 buffer_free(&b);
374 } else {
375 pkalg = packet_get_string(&alen);
376 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000377 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000378 packet_done();
379
380 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
381 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
382
383 do {
384 if (authctxt->last_key == NULL ||
385 authctxt->last_key_sign == NULL) {
386 debug("no last key or no sign cb");
387 break;
388 }
Ben Lindstrom206941f2001-04-15 14:27:16 +0000389 if (key_type_from_name(pkalg) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000390 debug("unknown pkalg %s", pkalg);
391 break;
392 }
393 if ((key = key_from_blob(pkblob, blen)) == NULL) {
394 debug("no key from blob. pkalg %s", pkalg);
395 break;
396 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000397 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
398 debug2("input_userauth_pk_ok: fp %s", fp);
399 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000400 if (!key_equal(key, authctxt->last_key)) {
401 debug("key != last_key");
402 break;
403 }
404 sent = sign_and_send_pubkey(authctxt, key,
405 authctxt->last_key_sign);
406 } while(0);
407
408 if (key != NULL)
409 key_free(key);
410 xfree(pkalg);
411 xfree(pkblob);
412
413 /* unregister */
414 clear_auth_state(authctxt);
415 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
416
417 /* try another method if we did not send a packet*/
418 if (sent == 0)
419 userauth(authctxt, NULL);
420
Damien Miller62cee002000-09-23 17:15:56 +1100421}
422
Damien Millereba71ba2000-04-29 23:57:08 +1000423int
Damien Miller874d77b2000-10-14 16:23:11 +1100424userauth_none(Authctxt *authctxt)
425{
426 /* initial userauth request */
427 packet_start(SSH2_MSG_USERAUTH_REQUEST);
428 packet_put_cstring(authctxt->server_user);
429 packet_put_cstring(authctxt->service);
430 packet_put_cstring(authctxt->method->name);
431 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100432 return 1;
433}
434
435int
Damien Miller62cee002000-09-23 17:15:56 +1100436userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000437{
Damien Millere247cc42000-05-07 12:03:14 +1000438 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000439 char prompt[80];
440 char *password;
441
Damien Millerd3a18572000-06-07 19:55:44 +1000442 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000443 return 0;
444
Damien Millerd3a18572000-06-07 19:55:44 +1000445 if(attempt != 1)
446 error("Permission denied, please try again.");
447
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000448 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100449 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000450 password = read_passphrase(prompt, 0);
451 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100452 packet_put_cstring(authctxt->server_user);
453 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100454 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000455 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000456 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000457 memset(password, 0, strlen(password));
458 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000459 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000460 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000461 return 1;
462}
463
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000464void
465clear_auth_state(Authctxt *authctxt)
466{
467 /* XXX clear authentication state */
468 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
469 debug3("clear_auth_state: key_free %p", authctxt->last_key);
470 key_free(authctxt->last_key);
471 }
472 authctxt->last_key = NULL;
473 authctxt->last_key_hint = -2;
474 authctxt->last_key_sign = NULL;
475}
476
Ben Lindstrombba81212001-06-25 05:01:22 +0000477static int
Damien Miller62cee002000-09-23 17:15:56 +1100478sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000479{
480 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000481 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000482 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000483 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000484 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100485 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000486
Ben Lindstromd121f612000-12-03 17:00:47 +0000487 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000488
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489 if (key_to_blob(k, &blob, &bloblen) == 0) {
490 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000491 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 return 0;
493 }
Damien Millereba71ba2000-04-29 23:57:08 +1000494 /* data to be signed */
495 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100496 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000497 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000498 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100499 } else {
500 buffer_put_string(&b, session_id2, session_id2_len);
501 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000502 }
Damien Millereba71ba2000-04-29 23:57:08 +1000503 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100504 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000505 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000506 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000507 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100508 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000509 if (datafellows & SSH_BUG_PKAUTH) {
510 buffer_put_char(&b, have_sig);
511 } else {
512 buffer_put_cstring(&b, authctxt->method->name);
513 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000514 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000515 }
Damien Millereba71ba2000-04-29 23:57:08 +1000516 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000517
518 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000519 ret = (*sign_callback)(authctxt, k, &signature, &slen,
520 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000521 if (ret == -1) {
522 xfree(blob);
523 buffer_free(&b);
524 return 0;
525 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100526#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000527 buffer_dump(&b);
528#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000529 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000530 buffer_clear(&b);
531 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000532 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000533 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100534 buffer_put_cstring(&b, authctxt->server_user);
535 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100536 buffer_put_cstring(&b, authctxt->method->name);
537 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000538 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000539 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000540 buffer_put_string(&b, blob, bloblen);
541 }
542 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000543
Damien Millereba71ba2000-04-29 23:57:08 +1000544 /* append signature */
545 buffer_put_string(&b, signature, slen);
546 xfree(signature);
547
548 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000549 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100550 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000551 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000552
553 /* put remaining data from buffer into packet */
554 packet_start(SSH2_MSG_USERAUTH_REQUEST);
555 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
556 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000557 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000558
559 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000560}
561
Ben Lindstrombba81212001-06-25 05:01:22 +0000562static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000563send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
564 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000565{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000566 u_char *blob;
567 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000568
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000569 debug3("send_pubkey_test");
570
571 if (key_to_blob(k, &blob, &bloblen) == 0) {
572 /* we cannot handle this key */
573 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000574 return 0;
575 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000576 /* register callback for USERAUTH_PK_OK message */
577 authctxt->last_key_sign = sign_callback;
578 authctxt->last_key_hint = hint;
579 authctxt->last_key = k;
580 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000581
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000582 packet_start(SSH2_MSG_USERAUTH_REQUEST);
583 packet_put_cstring(authctxt->server_user);
584 packet_put_cstring(authctxt->service);
585 packet_put_cstring(authctxt->method->name);
586 packet_put_char(have_sig);
587 if (!(datafellows & SSH_BUG_PKAUTH))
588 packet_put_cstring(key_ssh_name(k));
589 packet_put_string(blob, bloblen);
590 xfree(blob);
591 packet_send();
592 return 1;
593}
594
Ben Lindstrombba81212001-06-25 05:01:22 +0000595static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596load_identity_file(char *filename)
597{
598 Key *private;
599 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000600 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000601 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000602
Ben Lindstrom329782e2001-03-10 17:08:59 +0000603 if (stat(filename, &st) < 0) {
604 debug3("no such identity: %s", filename);
605 return NULL;
606 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000607 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
608 if (private == NULL) {
609 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000611 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100612 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100613 for (i = 0; i < options.number_of_password_prompts; i++) {
614 passphrase = read_passphrase(prompt, 0);
615 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000616 private = key_load_private_type(KEY_UNSPEC, filename,
617 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000618 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100619 } else {
620 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000621 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100622 }
623 memset(passphrase, 0, strlen(passphrase));
624 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000625 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100626 break;
627 debug2("bad passphrase given, try again...");
628 }
Damien Miller994cf142000-07-21 10:19:44 +1000629 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000630 return private;
631}
632
Ben Lindstrombba81212001-06-25 05:01:22 +0000633static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000634identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
635 u_char *data, int datalen)
636{
637 Key *private;
638 int idx, ret;
639
640 idx = authctxt->last_key_hint;
641 if (idx < 0)
642 return -1;
643 private = load_identity_file(options.identity_files[idx]);
644 if (private == NULL)
645 return -1;
646 ret = key_sign(private, sigp, lenp, data, datalen);
647 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000648 return ret;
649}
650
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static int
652agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000653 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000654{
Damien Miller62cee002000-09-23 17:15:56 +1100655 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000656}
657
Ben Lindstrombba81212001-06-25 05:01:22 +0000658static int
659key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000660 u_char *data, int datalen)
661{
Ben Lindstroma3700052001-04-05 23:26:32 +0000662 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000663}
664
Ben Lindstrombba81212001-06-25 05:01:22 +0000665static int
Damien Miller62cee002000-09-23 17:15:56 +1100666userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000667{
668 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100669 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000670 char *comment;
671 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000672
673 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100674 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
675 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100676 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000677 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100678 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100679 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100680 debug2("userauth_pubkey_agent: no more keys");
681 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000682 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100683 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000684 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
685 if (ret == 0)
686 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100687 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100688 if (ret == 0)
689 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000690 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000691}
692
Damien Miller62cee002000-09-23 17:15:56 +1100693int
694userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000695{
Damien Miller62cee002000-09-23 17:15:56 +1100696 static int idx = 0;
697 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000698 Key *key;
699 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000700
Damien Miller0bc1bd82000-11-13 22:57:25 +1100701 if (authctxt->agent != NULL) {
702 do {
703 sent = userauth_pubkey_agent(authctxt);
704 } while(!sent && authctxt->agent->howmany > 0);
705 }
706 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000707 key = options.identity_keys[idx];
708 filename = options.identity_files[idx];
709 if (key == NULL) {
710 debug("try privkey: %s", filename);
711 key = load_identity_file(filename);
712 if (key != NULL) {
713 sent = sign_and_send_pubkey(authctxt, key,
714 key_sign_cb);
715 key_free(key);
716 }
717 } else if (key->type != KEY_RSA1) {
718 debug("try pubkey: %s", filename);
719 sent = send_pubkey_test(authctxt, key,
720 identity_sign_cb, idx);
721 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722 idx++;
723 }
Damien Miller62cee002000-09-23 17:15:56 +1100724 return sent;
725}
Damien Millereba71ba2000-04-29 23:57:08 +1000726
Damien Miller874d77b2000-10-14 16:23:11 +1100727/*
728 * Send userauth request message specifying keyboard-interactive method.
729 */
730int
731userauth_kbdint(Authctxt *authctxt)
732{
733 static int attempt = 0;
734
735 if (attempt++ >= options.number_of_password_prompts)
736 return 0;
737
738 debug2("userauth_kbdint");
739 packet_start(SSH2_MSG_USERAUTH_REQUEST);
740 packet_put_cstring(authctxt->server_user);
741 packet_put_cstring(authctxt->service);
742 packet_put_cstring(authctxt->method->name);
743 packet_put_cstring(""); /* lang */
744 packet_put_cstring(options.kbd_interactive_devices ?
745 options.kbd_interactive_devices : "");
746 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100747
748 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
749 return 1;
750}
751
752/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000753 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100754 */
755void
756input_userauth_info_req(int type, int plen, void *ctxt)
757{
758 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000759 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000760 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100761 int echo = 0;
762
763 debug2("input_userauth_info_req");
764
765 if (authctxt == NULL)
766 fatal("input_userauth_info_req: no authentication context");
767
768 name = packet_get_string(NULL);
769 inst = packet_get_string(NULL);
770 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100771 if (strlen(name) > 0)
Ben Lindstrom949974b2001-06-25 05:20:31 +0000772 log(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100773 if (strlen(inst) > 0)
Ben Lindstrom949974b2001-06-25 05:20:31 +0000774 log(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000775 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100776 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000777 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100778
779 num_prompts = packet_get_int();
780 /*
781 * Begin to build info response packet based on prompts requested.
782 * We commit to providing the correct number of responses, so if
783 * further on we run into a problem that prevents this, we have to
784 * be sure and clean this up and send a correct error response.
785 */
786 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
787 packet_put_int(num_prompts);
788
Ben Lindstrom551ea372001-06-05 18:56:16 +0000789 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100790 for (i = 0; i < num_prompts; i++) {
791 prompt = packet_get_string(NULL);
792 echo = packet_get_char();
793
Ben Lindstrom949974b2001-06-25 05:20:31 +0000794 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100795
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000796 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100797 memset(response, 0, strlen(response));
798 xfree(response);
799 xfree(prompt);
800 }
801 packet_done(); /* done with parsing incoming message. */
802
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000803 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100804 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100805}
Damien Miller62cee002000-09-23 17:15:56 +1100806
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000807/*
808 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
809 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
810 */
811int
812userauth_hostbased(Authctxt *authctxt)
813{
814 Key *private = NULL;
815 Buffer b;
816 u_char *signature, *blob;
817 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000818 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000819 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000820 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000821
822 p = get_local_name(packet_get_connection_in());
823 if (p == NULL) {
824 error("userauth_hostbased: cannot get local ipaddr/name");
825 return 0;
826 }
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000827 len = strlen(p) + 2;
828 chost = xmalloc(len);
829 strlcpy(chost, p, len);
830 strlcat(chost, ".", len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000831 debug2("userauth_hostbased: chost %s", chost);
832 /* check for a useful key */
833 for (i = 0; i < authctxt->nkeys; i++) {
834 private = authctxt->keys[i];
835 if (private && private->type != KEY_RSA1) {
836 found = 1;
837 /* we take and free the key */
838 authctxt->keys[i] = NULL;
839 break;
840 }
841 }
842 if (!found) {
843 xfree(chost);
844 return 0;
845 }
846 if (key_to_blob(private, &blob, &blen) == 0) {
847 key_free(private);
848 xfree(chost);
849 return 0;
850 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000851 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
852 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000853 pkalg = xstrdup(key_ssh_name(private));
854 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000855 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +0000856 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000857 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
858 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000859 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000860 buffer_put_cstring(&b, authctxt->method->name);
861 buffer_put_cstring(&b, pkalg);
862 buffer_put_string(&b, blob, blen);
863 buffer_put_cstring(&b, chost);
864 buffer_put_cstring(&b, authctxt->local_user);
865#ifdef DEBUG_PK
866 buffer_dump(&b);
867#endif
868 debug2("xxx: chost %s", chost);
869 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
870 key_free(private);
871 buffer_free(&b);
872 if (ok != 0) {
873 error("key_sign failed");
874 xfree(chost);
875 xfree(pkalg);
876 return 0;
877 }
878 packet_start(SSH2_MSG_USERAUTH_REQUEST);
879 packet_put_cstring(authctxt->server_user);
880 packet_put_cstring(authctxt->service);
881 packet_put_cstring(authctxt->method->name);
882 packet_put_cstring(pkalg);
883 packet_put_string(blob, blen);
884 packet_put_cstring(chost);
885 packet_put_cstring(authctxt->local_user);
886 packet_put_string(signature, slen);
887 memset(signature, 's', slen);
888 xfree(signature);
889 xfree(chost);
890 xfree(pkalg);
891
892 packet_send();
893 return 1;
894}
895
Damien Miller62cee002000-09-23 17:15:56 +1100896/* find auth method */
897
Damien Miller62cee002000-09-23 17:15:56 +1100898/*
899 * given auth method name, if configurable options permit this method fill
900 * in auth_ident field and return true, otherwise return false.
901 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static int
Damien Miller62cee002000-09-23 17:15:56 +1100903authmethod_is_enabled(Authmethod *method)
904{
905 if (method == NULL)
906 return 0;
907 /* return false if options indicate this method is disabled */
908 if (method->enabled == NULL || *method->enabled == 0)
909 return 0;
910 /* return false if batch mode is enabled but method needs interactive mode */
911 if (method->batch_flag != NULL && *method->batch_flag != 0)
912 return 0;
913 return 1;
914}
915
Ben Lindstrombba81212001-06-25 05:01:22 +0000916static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +1100917authmethod_lookup(const char *name)
918{
919 Authmethod *method = NULL;
920 if (name != NULL)
921 for (method = authmethods; method->name != NULL; method++)
922 if (strcmp(name, method->name) == 0)
923 return method;
924 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
925 return NULL;
926}
927
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000928/* XXX internal state */
929static Authmethod *current = NULL;
930static char *supported = NULL;
931static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100932/*
933 * Given the authentication method list sent by the server, return the
934 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +0000935 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +0000936 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000937static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +1100938authmethod_get(char *authlist)
939{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000940
941 char *name = NULL;
942 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000943
Damien Miller62cee002000-09-23 17:15:56 +1100944 /* Use a suitable default if we're passed a nil list. */
945 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000946 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +1100947
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000948 if (supported == NULL || strcmp(authlist, supported) != 0) {
949 debug3("start over, passed a different list %s", authlist);
950 if (supported != NULL)
951 xfree(supported);
952 supported = xstrdup(authlist);
953 preferred = options.preferred_authentications;
954 debug3("preferred %s", preferred);
955 current = NULL;
956 } else if (current != NULL && authmethod_is_enabled(current))
957 return current;
Damien Millereba71ba2000-04-29 23:57:08 +1000958
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000959 for (;;) {
960 if ((name = match_list(preferred, supported, &next)) == NULL) {
961 debug("no more auth methods to try");
962 current = NULL;
963 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100964 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000965 preferred += next;
966 debug3("authmethod_lookup %s", name);
967 debug3("remaining preferred: %s", preferred);
968 if ((current = authmethod_lookup(name)) != NULL &&
969 authmethod_is_enabled(current)) {
970 debug3("authmethod_is_enabled %s", name);
971 debug("next auth method to try is %s", name);
972 return current;
973 }
Damien Millereba71ba2000-04-29 23:57:08 +1000974 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000975}
Damien Miller62cee002000-09-23 17:15:56 +1100976
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000977
978#define DELIM ","
979char *
980authmethods_get(void)
981{
982 Authmethod *method = NULL;
983 char buf[1024];
984
985 buf[0] = '\0';
986 for (method = authmethods; method->name != NULL; method++) {
987 if (authmethod_is_enabled(method)) {
988 if (buf[0] != '\0')
989 strlcat(buf, DELIM, sizeof buf);
990 strlcat(buf, method->name, sizeof buf);
991 }
Damien Miller62cee002000-09-23 17:15:56 +1100992 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000993 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000994}