]> andersk Git - openssh.git/blame - sshconnect2.c
- djm@cvs.openbsd.org 2009/10/22 22:26:13
[openssh.git] / sshconnect2.c
CommitLineData
5b01421b 1/* $OpenBSD: sshconnect2.c,v 1.171 2009/03/05 07:18:19 djm Exp $ */
a306f2dd 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5adf6b9a 4 * Copyright (c) 2008 Damien Miller. All rights reserved.
a306f2dd 5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
a306f2dd 14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
c3ff311a 28
aa2eae64 29#include <sys/types.h>
31652869 30#include <sys/socket.h>
aa2eae64 31#include <sys/wait.h>
4095f623 32#include <sys/stat.h>
a306f2dd 33
028094f4 34#include <errno.h>
b2349766 35#include <netdb.h>
31652869 36#include <pwd.h>
37#include <signal.h>
24436b92 38#include <stdarg.h>
cf851879 39#include <stdio.h>
00146caa 40#include <string.h>
67514848 41#include <unistd.h>
3a69fb58 42#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
e5df5ff2 43#include <vis.h>
3a69fb58 44#endif
028094f4 45
751092f9 46#include "openbsd-compat/sys-queue.h"
47
31652869 48#include "xmalloc.h"
a306f2dd 49#include "ssh.h"
42f11eb2 50#include "ssh2.h"
a306f2dd 51#include "buffer.h"
52#include "packet.h"
a306f2dd 53#include "compat.h"
42f11eb2 54#include "cipher.h"
31652869 55#include "key.h"
a306f2dd 56#include "kex.h"
57#include "myproposal.h"
a306f2dd 58#include "sshconnect.h"
59#include "authfile.h"
db1cd2f3 60#include "dh.h"
2e73a022 61#include "authfd.h"
42f11eb2 62#include "log.h"
63#include "readconf.h"
58ae9cb8 64#include "misc.h"
cab80f75 65#include "match.h"
a5c9ffdb 66#include "dispatch.h"
8002af61 67#include "canohost.h"
39c00dc2 68#include "msg.h"
69#include "pathnames.h"
82aeee5d 70#include "uidswap.h"
5b01421b 71#include "schnorr.h"
5adf6b9a 72#include "jpake.h"
94ec8c6b 73
7364bd04 74#ifdef GSSAPI
75#include "ssh-gss.h"
76#endif
77
a306f2dd 78/* import */
79extern char *client_version_string;
80extern char *server_version_string;
81extern Options options;
82
83/*
84 * SSH2 key exchange
85 */
86
1e3b8b07 87u_char *session_id2 = NULL;
a27002e5 88u_int session_id2_len = 0;
a306f2dd 89
a5c9ffdb 90char *xxx_host;
91struct sockaddr *xxx_hostaddr;
92
e092ce67 93Kex *xxx_kex = NULL;
94
396c147e 95static int
f49bc4f7 96verify_host_key_callback(Key *hostkey)
a5c9ffdb 97{
f49bc4f7 98 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
01e9ef57 99 fatal("Host key verification failed.");
a5c9ffdb 100 return 0;
101}
102
a306f2dd 103void
94ec8c6b 104ssh_kex2(char *host, struct sockaddr *hostaddr)
105{
94ec8c6b 106 Kex *kex;
a5c9ffdb 107
108 xxx_host = host;
109 xxx_hostaddr = hostaddr;
94ec8c6b 110
c523303b 111 if (options.ciphers == (char *)-1) {
bbe88b6d 112 logit("No valid ciphers for protocol version 2 given, using defaults.");
c523303b 113 options.ciphers = NULL;
94ec8c6b 114 }
115 if (options.ciphers != NULL) {
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
118 }
1ad64a93 119 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
121 myproposal[PROPOSAL_ENC_ALGS_STOC] =
122 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
94ec8c6b 123 if (options.compression) {
b2552997 124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
07200973 125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
94ec8c6b 126 } else {
b2552997 127 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
07200973 128 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
94ec8c6b 129 }
b2552997 130 if (options.macs != NULL) {
131 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
132 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
133 }
e961a8f9 134 if (options.hostkeyalgorithms != NULL)
184eed6a 135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
e961a8f9 136 options.hostkeyalgorithms;
94ec8c6b 137
ffd7b36b 138 if (options.rekey_limit)
fe771396 139 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
ffd7b36b 140
7a37c112 141 /* start key exchange */
d8ee838b 142 kex = kex_setup(myproposal);
98a58eda 143 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
41e5bd9a 144 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
98a58eda 145 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
2ff8003a 146 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
a5c9ffdb 147 kex->client_version_string=client_version_string;
148 kex->server_version_string=server_version_string;
f49bc4f7 149 kex->verify_host_key=&verify_host_key_callback;
94ec8c6b 150
e092ce67 151 xxx_kex = kex;
152
c422989b 153 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
94ec8c6b 154
d1ac6175 155 session_id2 = kex->session_id;
156 session_id2_len = kex->session_id_len;
157
94ec8c6b 158#ifdef DEBUG_KEXDH
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE);
161 packet_put_cstring("markus");
162 packet_send();
163 packet_write_wait();
164#endif
a306f2dd 165}
71276795 166
a306f2dd 167/*
168 * Authenticate user
169 */
188adeb2 170
171typedef struct Authctxt Authctxt;
172typedef struct Authmethod Authmethod;
43348518 173typedef struct identity Identity;
174typedef struct idlist Idlist;
175
176struct identity {
177 TAILQ_ENTRY(identity) next;
178 AuthenticationConnection *ac; /* set if agent supports key */
179 Key *key; /* public/private key */
180 char *filename; /* comment for agent-only keys */
181 int tried;
182 int isprivate; /* key points to the private key */
183};
184TAILQ_HEAD(idlist, identity);
188adeb2 185
186struct Authctxt {
187 const char *server_user;
8002af61 188 const char *local_user;
188adeb2 189 const char *host;
190 const char *service;
188adeb2 191 Authmethod *method;
94ec8c6b 192 int success;
fee56204 193 char *authlist;
8002af61 194 /* pubkey */
43348518 195 Idlist keys;
8002af61 196 AuthenticationConnection *agent;
197 /* hostbased */
39c00dc2 198 Sensitive *sensitive;
6b759005 199 /* kbd-interactive */
200 int info_req_seen;
7364bd04 201 /* generic */
202 void *methoddata;
188adeb2 203};
204struct Authmethod {
205 char *name; /* string to compare against server's list */
206 int (*userauth)(Authctxt *authctxt);
5adf6b9a 207 void (*cleanup)(Authctxt *authctxt);
188adeb2 208 int *enabled; /* flag in option struct that enables method */
209 int *batch_flag; /* flag in option struct that disables method */
210};
211
7819b5c3 212void input_userauth_success(int, u_int32_t, void *);
213void input_userauth_failure(int, u_int32_t, void *);
214void input_userauth_banner(int, u_int32_t, void *);
215void input_userauth_error(int, u_int32_t, void *);
216void input_userauth_info_req(int, u_int32_t, void *);
217void input_userauth_pk_ok(int, u_int32_t, void *);
67ec167d 218void input_userauth_passwd_changereq(int, u_int32_t, void *);
5adf6b9a 219void input_userauth_jpake_server_step1(int, u_int32_t, void *);
220void input_userauth_jpake_server_step2(int, u_int32_t, void *);
221void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
94ec8c6b 222
d5bb9418 223int userauth_none(Authctxt *);
224int userauth_pubkey(Authctxt *);
225int userauth_passwd(Authctxt *);
226int userauth_kbdint(Authctxt *);
227int userauth_hostbased(Authctxt *);
5adf6b9a 228int userauth_jpake(Authctxt *);
229
230void userauth_jpake_cleanup(Authctxt *);
188adeb2 231
7364bd04 232#ifdef GSSAPI
233int userauth_gssapi(Authctxt *authctxt);
234void input_gssapi_response(int type, u_int32_t, void *);
235void input_gssapi_token(int type, u_int32_t, void *);
236void input_gssapi_hash(int type, u_int32_t, void *);
237void input_gssapi_error(int, u_int32_t, void *);
238void input_gssapi_errtok(int, u_int32_t, void *);
239#endif
240
d5bb9418 241void userauth(Authctxt *, char *);
fee56204 242
43348518 243static int sign_and_send_pubkey(Authctxt *, Identity *);
43348518 244static void pubkey_prepare(Authctxt *);
245static void pubkey_cleanup(Authctxt *);
246static Key *load_identity_file(char *);
fee56204 247
396c147e 248static Authmethod *authmethod_get(char *authlist);
249static Authmethod *authmethod_lookup(const char *name);
250static char *authmethods_get(void);
188adeb2 251
252Authmethod authmethods[] = {
7364bd04 253#ifdef GSSAPI
0789992b 254 {"gssapi-with-mic",
7364bd04 255 userauth_gssapi,
5adf6b9a 256 NULL,
7364bd04 257 &options.gss_authentication,
258 NULL},
259#endif
3398dda9 260 {"hostbased",
261 userauth_hostbased,
5adf6b9a 262 NULL,
3398dda9 263 &options.hostbased_authentication,
264 NULL},
9f37c0af 265 {"publickey",
266 userauth_pubkey,
5adf6b9a 267 NULL,
9f37c0af 268 &options.pubkey_authentication,
269 NULL},
5adf6b9a 270#ifdef JPAKE
271 {"jpake-01@openssh.com",
272 userauth_jpake,
273 userauth_jpake_cleanup,
274 &options.zero_knowledge_password_authentication,
275 &options.batch_mode},
276#endif
94ec8c6b 277 {"keyboard-interactive",
278 userauth_kbdint,
5adf6b9a 279 NULL,
94ec8c6b 280 &options.kbd_interactive_authentication,
281 &options.batch_mode},
9f37c0af 282 {"password",
283 userauth_passwd,
5adf6b9a 284 NULL,
9f37c0af 285 &options.password_authentication,
286 &options.batch_mode},
94ec8c6b 287 {"none",
288 userauth_none,
289 NULL,
5adf6b9a 290 NULL,
94ec8c6b 291 NULL},
5adf6b9a 292 {NULL, NULL, NULL, NULL, NULL}
188adeb2 293};
294
295void
8002af61 296ssh_userauth2(const char *local_user, const char *server_user, char *host,
39c00dc2 297 Sensitive *sensitive)
188adeb2 298{
299 Authctxt authctxt;
300 int type;
188adeb2 301
5ba55ada 302 if (options.challenge_response_authentication)
d464095c 303 options.kbd_interactive_authentication = 1;
304
188adeb2 305 packet_start(SSH2_MSG_SERVICE_REQUEST);
306 packet_put_cstring("ssh-userauth");
307 packet_send();
a77673cc 308 debug("SSH2_MSG_SERVICE_REQUEST sent");
188adeb2 309 packet_write_wait();
54a5250f 310 type = packet_read();
a77673cc 311 if (type != SSH2_MSG_SERVICE_ACCEPT)
312 fatal("Server denied authentication request: %d", type);
188adeb2 313 if (packet_remaining() > 0) {
54a5250f 314 char *reply = packet_get_string(NULL);
a77673cc 315 debug2("service_accept: %s", reply);
188adeb2 316 xfree(reply);
188adeb2 317 } else {
6226a8f8 318 debug2("buggy server: service_accept w/o service");
188adeb2 319 }
95500969 320 packet_check_eom();
a77673cc 321 debug("SSH2_MSG_SERVICE_ACCEPT received");
188adeb2 322
cab80f75 323 if (options.preferred_authentications == NULL)
324 options.preferred_authentications = authmethods_get();
325
188adeb2 326 /* setup authentication context */
6b759005 327 memset(&authctxt, 0, sizeof(authctxt));
43348518 328 pubkey_prepare(&authctxt);
188adeb2 329 authctxt.server_user = server_user;
8002af61 330 authctxt.local_user = local_user;
188adeb2 331 authctxt.host = host;
332 authctxt.service = "ssh-connection"; /* service name */
333 authctxt.success = 0;
94ec8c6b 334 authctxt.method = authmethod_lookup("none");
fee56204 335 authctxt.authlist = NULL;
7364bd04 336 authctxt.methoddata = NULL;
39c00dc2 337 authctxt.sensitive = sensitive;
6b759005 338 authctxt.info_req_seen = 0;
94ec8c6b 339 if (authctxt.method == NULL)
340 fatal("ssh_userauth2: internal error: cannot send userauth none request");
188adeb2 341
342 /* initial userauth request */
94ec8c6b 343 userauth_none(&authctxt);
188adeb2 344
7a37c112 345 dispatch_init(&input_userauth_error);
188adeb2 346 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
347 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
9616313f 348 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
188adeb2 349 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
350
43348518 351 pubkey_cleanup(&authctxt);
b8c2031b 352 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
353
6226a8f8 354 debug("Authentication succeeded (%s).", authctxt.method->name);
188adeb2 355}
b8c2031b 356
188adeb2 357void
fee56204 358userauth(Authctxt *authctxt, char *authlist)
359{
5adf6b9a 360 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
361 authctxt->method->cleanup(authctxt);
362
7364bd04 363 if (authctxt->methoddata) {
364 xfree(authctxt->methoddata);
365 authctxt->methoddata = NULL;
366 }
fee56204 367 if (authlist == NULL) {
368 authlist = authctxt->authlist;
369 } else {
370 if (authctxt->authlist)
371 xfree(authctxt->authlist);
372 authctxt->authlist = authlist;
373 }
374 for (;;) {
375 Authmethod *method = authmethod_get(authlist);
376 if (method == NULL)
377 fatal("Permission denied (%s).", authlist);
378 authctxt->method = method;
b8c2031b 379
380 /* reset the per method handler */
381 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
382 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
383
384 /* and try new method */
fee56204 385 if (method->userauth(authctxt) != 0) {
386 debug2("we sent a %s packet, wait for reply", method->name);
387 break;
388 } else {
389 debug2("we did not send a packet, disable method");
390 method->enabled = NULL;
391 }
392 }
393}
d6133f43 394
e64399cc 395/* ARGSUSED */
fee56204 396void
7819b5c3 397input_userauth_error(int type, u_int32_t seq, void *ctxt)
188adeb2 398{
9616313f 399 fatal("input_userauth_error: bad message during authentication: "
4e2e5cfd 400 "type %d", type);
9616313f 401}
d6133f43 402
e64399cc 403/* ARGSUSED */
9616313f 404void
7819b5c3 405input_userauth_banner(int type, u_int32_t seq, void *ctxt)
9616313f 406{
e5df5ff2 407 char *msg, *raw, *lang;
408 u_int len;
c8f0cf13 409
9616313f 410 debug3("input_userauth_banner");
e5df5ff2 411 raw = packet_get_string(&len);
9616313f 412 lang = packet_get_string(NULL);
341cb46b 413 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
e5df5ff2 414 if (len > 65536)
415 len = 65536;
90d5350e 416 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
417 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
19e633e7 418 fprintf(stderr, "%s", msg);
e5df5ff2 419 xfree(msg);
420 }
421 xfree(raw);
9616313f 422 xfree(lang);
188adeb2 423}
d6133f43 424
e64399cc 425/* ARGSUSED */
188adeb2 426void
7819b5c3 427input_userauth_success(int type, u_int32_t seq, void *ctxt)
188adeb2 428{
429 Authctxt *authctxt = ctxt;
430 if (authctxt == NULL)
431 fatal("input_userauth_success: no authentication context");
c8f0cf13 432 if (authctxt->authlist) {
fee56204 433 xfree(authctxt->authlist);
c8f0cf13 434 authctxt->authlist = NULL;
435 }
436 if (authctxt->methoddata) {
7364bd04 437 xfree(authctxt->methoddata);
c8f0cf13 438 authctxt->methoddata = NULL;
439 }
188adeb2 440 authctxt->success = 1; /* break out */
441}
d6133f43 442
e64399cc 443/* ARGSUSED */
188adeb2 444void
7819b5c3 445input_userauth_failure(int type, u_int32_t seq, void *ctxt)
188adeb2 446{
188adeb2 447 Authctxt *authctxt = ctxt;
448 char *authlist = NULL;
449 int partial;
188adeb2 450
451 if (authctxt == NULL)
452 fatal("input_userauth_failure: no authentication context");
453
94ec8c6b 454 authlist = packet_get_string(NULL);
188adeb2 455 partial = packet_get_char();
95500969 456 packet_check_eom();
188adeb2 457
458 if (partial != 0)
bbe88b6d 459 logit("Authenticated with partial success.");
6226a8f8 460 debug("Authentications that can continue: %s", authlist);
188adeb2 461
fee56204 462 userauth(authctxt, authlist);
463}
e64399cc 464
465/* ARGSUSED */
fee56204 466void
7819b5c3 467input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
fee56204 468{
469 Authctxt *authctxt = ctxt;
470 Key *key = NULL;
43348518 471 Identity *id = NULL;
fee56204 472 Buffer b;
c66f9d0e 473 int pktype, sent = 0;
474 u_int alen, blen;
475 char *pkalg, *fp;
476 u_char *pkblob;
fee56204 477
478 if (authctxt == NULL)
479 fatal("input_userauth_pk_ok: no authentication context");
480 if (datafellows & SSH_BUG_PKOK) {
481 /* this is similar to SSH_BUG_PKAUTH */
482 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
483 pkblob = packet_get_string(&blen);
484 buffer_init(&b);
485 buffer_append(&b, pkblob, blen);
486 pkalg = buffer_get_string(&b, &alen);
487 buffer_free(&b);
488 } else {
489 pkalg = packet_get_string(&alen);
490 pkblob = packet_get_string(&blen);
491 }
95500969 492 packet_check_eom();
fee56204 493
43348518 494 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
fee56204 495
43348518 496 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
497 debug("unknown pkalg %s", pkalg);
498 goto done;
499 }
500 if ((key = key_from_blob(pkblob, blen)) == NULL) {
501 debug("no key from blob. pkalg %s", pkalg);
502 goto done;
503 }
504 if (key->type != pktype) {
505 error("input_userauth_pk_ok: type mismatch "
506 "for decoded key (received %d, expected %d)",
507 key->type, pktype);
508 goto done;
509 }
510 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
511 debug2("input_userauth_pk_ok: fp %s", fp);
512 xfree(fp);
513
b56e99e2 514 /*
515 * search keys in the reverse order, because last candidate has been
516 * moved to the end of the queue. this also avoids confusion by
517 * duplicate keys
518 */
484b2208 519 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
43348518 520 if (key_equal(key, id->key)) {
521 sent = sign_and_send_pubkey(authctxt, id);
fee56204 522 break;
523 }
43348518 524 }
525done:
fee56204 526 if (key != NULL)
527 key_free(key);
528 xfree(pkalg);
529 xfree(pkblob);
530
343288b8 531 /* try another method if we did not send a packet */
fee56204 532 if (sent == 0)
533 userauth(authctxt, NULL);
188adeb2 534}
535
7364bd04 536#ifdef GSSAPI
aff51935 537int
7364bd04 538userauth_gssapi(Authctxt *authctxt)
539{
540 Gssctxt *gssctxt = NULL;
b0b30ca6 541 static gss_OID_set gss_supported = NULL;
2ceb8101 542 static u_int mech = 0;
7364bd04 543 OM_uint32 min;
544 int ok = 0;
545
546 /* Try one GSSAPI method at a time, rather than sending them all at
547 * once. */
548
b0b30ca6 549 if (gss_supported == NULL)
550 gss_indicate_mechs(&min, &gss_supported);
7364bd04 551
552 /* Check to see if the mechanism is usable before we offer it */
b0b30ca6 553 while (mech < gss_supported->count && !ok) {
7364bd04 554 /* My DER encoding requires length<128 */
b0b30ca6 555 if (gss_supported->elements[mech].length < 128 &&
592de384 556 ssh_gssapi_check_mechanism(&gssctxt,
557 &gss_supported->elements[mech], authctxt->host)) {
7364bd04 558 ok = 1; /* Mechanism works */
559 } else {
560 mech++;
561 }
562 }
563
592de384 564 if (!ok)
2ceb8101 565 return 0;
7364bd04 566
567 authctxt->methoddata=(void *)gssctxt;
568
569 packet_start(SSH2_MSG_USERAUTH_REQUEST);
570 packet_put_cstring(authctxt->server_user);
571 packet_put_cstring(authctxt->service);
572 packet_put_cstring(authctxt->method->name);
573
574 packet_put_int(1);
575
d8d9afd0 576 packet_put_int((gss_supported->elements[mech].length) + 2);
577 packet_put_char(SSH_GSS_OIDTYPE);
578 packet_put_char(gss_supported->elements[mech].length);
579 packet_put_raw(gss_supported->elements[mech].elements,
580 gss_supported->elements[mech].length);
7364bd04 581
582 packet_send();
583
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
587 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
588
589 mech++; /* Move along to next candidate */
590
591 return 1;
592}
593
b930668c 594static OM_uint32
595process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
596{
597 Authctxt *authctxt = ctxt;
598 Gssctxt *gssctxt = authctxt->methoddata;
599 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
fe206de9 600 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
601 gss_buffer_desc gssbuf;
0789992b 602 OM_uint32 status, ms, flags;
603 Buffer b;
b6453d99 604
b930668c 605 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
0789992b 606 recv_tok, &send_tok, &flags);
b930668c 607
608 if (send_tok.length > 0) {
609 if (GSS_ERROR(status))
610 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
611 else
612 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
b6453d99 613
b930668c 614 packet_put_string(send_tok.value, send_tok.length);
615 packet_send();
616 gss_release_buffer(&ms, &send_tok);
617 }
b6453d99 618
b930668c 619 if (status == GSS_S_COMPLETE) {
0789992b 620 /* send either complete or MIC, depending on mechanism */
621 if (!(flags & GSS_C_INTEG_FLAG)) {
622 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
623 packet_send();
624 } else {
625 ssh_gssapi_buildmic(&b, authctxt->server_user,
626 authctxt->service, "gssapi-with-mic");
627
628 gssbuf.value = buffer_ptr(&b);
629 gssbuf.length = buffer_len(&b);
b6453d99 630
0789992b 631 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
b6453d99 632
0789992b 633 if (!GSS_ERROR(status)) {
634 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
635 packet_put_string(mic.value, mic.length);
b6453d99 636
0789992b 637 packet_send();
638 }
b6453d99 639
0789992b 640 buffer_free(&b);
641 gss_release_buffer(&ms, &mic);
b6453d99 642 }
b930668c 643 }
b6453d99 644
b930668c 645 return status;
646}
647
e64399cc 648/* ARGSUSED */
7364bd04 649void
650input_gssapi_response(int type, u_int32_t plen, void *ctxt)
651{
652 Authctxt *authctxt = ctxt;
653 Gssctxt *gssctxt;
7364bd04 654 int oidlen;
655 char *oidv;
7364bd04 656
657 if (authctxt == NULL)
658 fatal("input_gssapi_response: no authentication context");
659 gssctxt = authctxt->methoddata;
660
661 /* Setup our OID */
662 oidv = packet_get_string(&oidlen);
663
d8d9afd0 664 if (oidlen <= 2 ||
665 oidv[0] != SSH_GSS_OIDTYPE ||
666 oidv[1] != oidlen - 2) {
b930668c 667 xfree(oidv);
d8d9afd0 668 debug("Badly encoded mechanism OID received");
669 userauth(authctxt, NULL);
d8d9afd0 670 return;
7364bd04 671 }
672
d8d9afd0 673 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
674 fatal("Server returned different OID than expected");
675
7364bd04 676 packet_check_eom();
677
678 xfree(oidv);
679
b930668c 680 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
7364bd04 681 /* Start again with next method on list */
682 debug("Trying to start again");
683 userauth(authctxt, NULL);
684 return;
685 }
7364bd04 686}
687
e64399cc 688/* ARGSUSED */
7364bd04 689void
690input_gssapi_token(int type, u_int32_t plen, void *ctxt)
691{
692 Authctxt *authctxt = ctxt;
7364bd04 693 gss_buffer_desc recv_tok;
b930668c 694 OM_uint32 status;
7364bd04 695 u_int slen;
696
697 if (authctxt == NULL)
698 fatal("input_gssapi_response: no authentication context");
7364bd04 699
700 recv_tok.value = packet_get_string(&slen);
701 recv_tok.length = slen; /* safe typecast */
702
703 packet_check_eom();
704
b930668c 705 status = process_gssapi_token(ctxt, &recv_tok);
7364bd04 706
707 xfree(recv_tok.value);
708
709 if (GSS_ERROR(status)) {
7364bd04 710 /* Start again with the next method in the list */
711 userauth(authctxt, NULL);
712 return;
713 }
7364bd04 714}
715
e64399cc 716/* ARGSUSED */
7364bd04 717void
718input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
719{
720 Authctxt *authctxt = ctxt;
721 Gssctxt *gssctxt;
722 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
723 gss_buffer_desc recv_tok;
724 OM_uint32 status, ms;
f99e1ca4 725 u_int len;
7364bd04 726
727 if (authctxt == NULL)
728 fatal("input_gssapi_response: no authentication context");
729 gssctxt = authctxt->methoddata;
730
f99e1ca4 731 recv_tok.value = packet_get_string(&len);
732 recv_tok.length = len;
7364bd04 733
734 packet_check_eom();
735
736 /* Stick it into GSSAPI and see what it says */
737 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
4e2e5cfd 738 &recv_tok, &send_tok, NULL);
7364bd04 739
740 xfree(recv_tok.value);
741 gss_release_buffer(&ms, &send_tok);
742
743 /* Server will be returning a failed packet after this one */
744}
745
e64399cc 746/* ARGSUSED */
7364bd04 747void
748input_gssapi_error(int type, u_int32_t plen, void *ctxt)
749{
750 OM_uint32 maj, min;
751 char *msg;
752 char *lang;
753
754 maj=packet_get_int();
755 min=packet_get_int();
756 msg=packet_get_string(NULL);
757 lang=packet_get_string(NULL);
758
759 packet_check_eom();
760
17318dd6 761 debug("Server GSSAPI Error:\n%s", msg);
7364bd04 762 xfree(msg);
763 xfree(lang);
764}
765#endif /* GSSAPI */
766
94ec8c6b 767int
768userauth_none(Authctxt *authctxt)
769{
770 /* initial userauth request */
771 packet_start(SSH2_MSG_USERAUTH_REQUEST);
772 packet_put_cstring(authctxt->server_user);
773 packet_put_cstring(authctxt->service);
774 packet_put_cstring(authctxt->method->name);
775 packet_send();
94ec8c6b 776 return 1;
777}
778
a306f2dd 779int
188adeb2 780userauth_passwd(Authctxt *authctxt)
a306f2dd 781{
1d1ffb87 782 static int attempt = 0;
67ec167d 783 char prompt[150];
a306f2dd 784 char *password;
785
fa649821 786 if (attempt++ >= options.number_of_password_prompts)
1d1ffb87 787 return 0;
788
6aacefa7 789 if (attempt != 1)
fa649821 790 error("Permission denied, please try again.");
791
f72e01a5 792 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
188adeb2 793 authctxt->server_user, authctxt->host);
a306f2dd 794 password = read_passphrase(prompt, 0);
795 packet_start(SSH2_MSG_USERAUTH_REQUEST);
188adeb2 796 packet_put_cstring(authctxt->server_user);
797 packet_put_cstring(authctxt->service);
94ec8c6b 798 packet_put_cstring(authctxt->method->name);
a306f2dd 799 packet_put_char(0);
a6215e53 800 packet_put_cstring(password);
a306f2dd 801 memset(password, 0, strlen(password));
802 xfree(password);
b4b701be 803 packet_add_padding(64);
a306f2dd 804 packet_send();
67ec167d 805
7203d6bb 806 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
67ec167d 807 &input_userauth_passwd_changereq);
808
a306f2dd 809 return 1;
810}
e64399cc 811
67ec167d 812/*
813 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
814 */
e64399cc 815/* ARGSUSED */
67ec167d 816void
ef077e37 817input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
67ec167d 818{
819 Authctxt *authctxt = ctxt;
820 char *info, *lang, *password = NULL, *retype = NULL;
821 char prompt[150];
822
823 debug2("input_userauth_passwd_changereq");
824
825 if (authctxt == NULL)
826 fatal("input_userauth_passwd_changereq: "
827 "no authentication context");
828
829 info = packet_get_string(NULL);
830 lang = packet_get_string(NULL);
831 if (strlen(info) > 0)
bbe88b6d 832 logit("%s", info);
67ec167d 833 xfree(info);
834 xfree(lang);
835 packet_start(SSH2_MSG_USERAUTH_REQUEST);
836 packet_put_cstring(authctxt->server_user);
837 packet_put_cstring(authctxt->service);
838 packet_put_cstring(authctxt->method->name);
839 packet_put_char(1); /* additional info */
7203d6bb 840 snprintf(prompt, sizeof(prompt),
67ec167d 841 "Enter %.30s@%.128s's old password: ",
842 authctxt->server_user, authctxt->host);
843 password = read_passphrase(prompt, 0);
844 packet_put_cstring(password);
845 memset(password, 0, strlen(password));
846 xfree(password);
847 password = NULL;
848 while (password == NULL) {
7203d6bb 849 snprintf(prompt, sizeof(prompt),
67ec167d 850 "Enter %.30s@%.128s's new password: ",
851 authctxt->server_user, authctxt->host);
852 password = read_passphrase(prompt, RP_ALLOW_EOF);
853 if (password == NULL) {
854 /* bail out */
855 return;
856 }
7203d6bb 857 snprintf(prompt, sizeof(prompt),
67ec167d 858 "Retype %.30s@%.128s's new password: ",
859 authctxt->server_user, authctxt->host);
860 retype = read_passphrase(prompt, 0);
861 if (strcmp(password, retype) != 0) {
862 memset(password, 0, strlen(password));
863 xfree(password);
bbe88b6d 864 logit("Mismatch; try again, EOF to quit.");
67ec167d 865 password = NULL;
866 }
867 memset(retype, 0, strlen(retype));
868 xfree(retype);
869 }
870 packet_put_cstring(password);
871 memset(password, 0, strlen(password));
872 xfree(password);
873 packet_add_padding(64);
874 packet_send();
7203d6bb 875
876 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
67ec167d 877 &input_userauth_passwd_changereq);
878}
a306f2dd 879
5adf6b9a 880#ifdef JPAKE
881static char *
882pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
883{
884 /* OpenBSD crypt(3) handles all of these */
885 if (strcmp(crypt_scheme, "crypt") == 0 ||
886 strcmp(crypt_scheme, "bcrypt") == 0 ||
887 strcmp(crypt_scheme, "md5crypt") == 0 ||
888 strcmp(crypt_scheme, "crypt-extended") == 0)
889 return xstrdup(crypt(password, salt));
890 error("%s: unsupported password encryption scheme \"%.100s\"",
891 __func__, crypt_scheme);
892 return NULL;
893}
894
895static BIGNUM *
896jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
897 const char *salt)
898{
899 char prompt[256], *password, *crypted;
900 u_char *secret;
901 u_int secret_len;
902 BIGNUM *ret;
903
904 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
905 authctxt->server_user, authctxt->host);
906 password = read_passphrase(prompt, 0);
907
908 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
909 logit("Disabling %s authentication", authctxt->method->name);
910 authctxt->method->enabled = NULL;
911 /* Continue with an empty password to fail gracefully */
912 crypted = xstrdup("");
913 }
914
915#ifdef JPAKE_DEBUG
916 debug3("%s: salt = %s", __func__, salt);
917 debug3("%s: scheme = %s", __func__, crypt_scheme);
918 debug3("%s: crypted = %s", __func__, crypted);
919#endif
920
921 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
922 &secret, &secret_len) != 0)
923 fatal("%s: hash_buffer", __func__);
924
925 bzero(password, strlen(password));
926 bzero(crypted, strlen(crypted));
927 xfree(password);
928 xfree(crypted);
929
930 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
931 fatal("%s: BN_bin2bn (secret)", __func__);
932 bzero(secret, secret_len);
933 xfree(secret);
934
935 return ret;
936}
937
938/* ARGSUSED */
939void
940input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
941{
942 Authctxt *authctxt = ctxt;
943 struct jpake_ctx *pctx = authctxt->methoddata;
944 u_char *x3_proof, *x4_proof, *x2_s_proof;
945 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
946 char *crypt_scheme, *salt;
947
948 /* Disable this message */
949 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
950
951 if ((pctx->g_x3 = BN_new()) == NULL ||
952 (pctx->g_x4 = BN_new()) == NULL)
953 fatal("%s: BN_new", __func__);
954
955 /* Fetch step 1 values */
956 crypt_scheme = packet_get_string(NULL);
957 salt = packet_get_string(NULL);
958 pctx->server_id = packet_get_string(&pctx->server_id_len);
959 packet_get_bignum2(pctx->g_x3);
960 packet_get_bignum2(pctx->g_x4);
961 x3_proof = packet_get_string(&x3_proof_len);
962 x4_proof = packet_get_string(&x4_proof_len);
963 packet_check_eom();
964
965 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
966
967 /* Obtain password and derive secret */
968 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
969 bzero(crypt_scheme, strlen(crypt_scheme));
970 bzero(salt, strlen(salt));
971 xfree(crypt_scheme);
972 xfree(salt);
973 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
974
975 /* Calculate step 2 values */
976 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
977 pctx->g_x3, pctx->g_x4, pctx->x2,
978 pctx->server_id, pctx->server_id_len,
979 pctx->client_id, pctx->client_id_len,
980 x3_proof, x3_proof_len,
981 x4_proof, x4_proof_len,
982 &pctx->a,
983 &x2_s_proof, &x2_s_proof_len);
984
985 bzero(x3_proof, x3_proof_len);
986 bzero(x4_proof, x4_proof_len);
987 xfree(x3_proof);
988 xfree(x4_proof);
989
990 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
991
992 /* Send values for step 2 */
993 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
994 packet_put_bignum2(pctx->a);
995 packet_put_string(x2_s_proof, x2_s_proof_len);
996 packet_send();
997
998 bzero(x2_s_proof, x2_s_proof_len);
999 xfree(x2_s_proof);
1000
1001 /* Expect step 2 packet from peer */
1002 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1003 input_userauth_jpake_server_step2);
1004}
1005
1006/* ARGSUSED */
1007void
1008input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1009{
1010 Authctxt *authctxt = ctxt;
1011 struct jpake_ctx *pctx = authctxt->methoddata;
1012 u_char *x4_s_proof;
1013 u_int x4_s_proof_len;
1014
1015 /* Disable this message */
1016 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1017
1018 if ((pctx->b = BN_new()) == NULL)
1019 fatal("%s: BN_new", __func__);
1020
1021 /* Fetch step 2 values */
1022 packet_get_bignum2(pctx->b);
1023 x4_s_proof = packet_get_string(&x4_s_proof_len);
1024 packet_check_eom();
1025
1026 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1027
1028 /* Derive shared key and calculate confirmation hash */
1029 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1030 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1031 pctx->client_id, pctx->client_id_len,
1032 pctx->server_id, pctx->server_id_len,
1033 session_id2, session_id2_len,
1034 x4_s_proof, x4_s_proof_len,
1035 &pctx->k,
1036 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1037
1038 bzero(x4_s_proof, x4_s_proof_len);
1039 xfree(x4_s_proof);
1040
1041 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1042
1043 /* Send key confirmation proof */
1044 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1045 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1046 packet_send();
1047
1048 /* Expect confirmation from peer */
1049 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1050 input_userauth_jpake_server_confirm);
1051}
1052
1053/* ARGSUSED */
1054void
1055input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1056{
1057 Authctxt *authctxt = ctxt;
1058 struct jpake_ctx *pctx = authctxt->methoddata;
1059
1060 /* Disable this message */
1061 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1062
1063 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1064 packet_check_eom();
1065
1066 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1067
1068 /* Verify expected confirmation hash */
1069 if (jpake_check_confirm(pctx->k,
1070 pctx->server_id, pctx->server_id_len,
1071 session_id2, session_id2_len,
1072 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1073 debug("%s: %s success", __func__, authctxt->method->name);
1074 else {
1075 debug("%s: confirmation mismatch", __func__);
1076 /* XXX stash this so if auth succeeds then we can warn/kill */
1077 }
1078
1079 userauth_jpake_cleanup(authctxt);
1080}
1081#endif /* JPAKE */
1082
43348518 1083static int
1084identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1085 u_char *data, u_int datalen)
1086{
1087 Key *prv;
1088 int ret;
1089
1090 /* the agent supports this key */
1091 if (id->ac)
1092 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1093 data, datalen));
1094 /*
1095 * we have already loaded the private key or
1096 * the private key is stored in external hardware
1097 */
1098 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1099 return (key_sign(id->key, sigp, lenp, data, datalen));
1100 /* load the private key from the file */
1101 if ((prv = load_identity_file(id->filename)) == NULL)
1102 return (-1);
1103 ret = key_sign(prv, sigp, lenp, data, datalen);
1104 key_free(prv);
1105 return (ret);
fee56204 1106}
1107
396c147e 1108static int
43348518 1109sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
a306f2dd 1110{
1111 Buffer b;
1e3b8b07 1112 u_char *blob, *signature;
c66f9d0e 1113 u_int bloblen, slen;
a27002e5 1114 u_int skip = 0;
2e73a022 1115 int ret = -1;
94ec8c6b 1116 int have_sig = 1;
a306f2dd 1117
cbc5abf9 1118 debug3("sign_and_send_pubkey");
fee56204 1119
43348518 1120 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
fa08c86b 1121 /* we cannot handle this key */
cbc5abf9 1122 debug3("sign_and_send_pubkey: cannot handle key");
fa08c86b 1123 return 0;
1124 }
a306f2dd 1125 /* data to be signed */
1126 buffer_init(&b);
33de75a3 1127 if (datafellows & SSH_OLD_SESSIONID) {
74fc9186 1128 buffer_append(&b, session_id2, session_id2_len);
2b87da3b 1129 skip = session_id2_len;
33de75a3 1130 } else {
1131 buffer_put_string(&b, session_id2, session_id2_len);
1132 skip = buffer_len(&b);
74fc9186 1133 }
a306f2dd 1134 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 1135 buffer_put_cstring(&b, authctxt->server_user);
d0c832f3 1136 buffer_put_cstring(&b,
cbc5abf9 1137 datafellows & SSH_BUG_PKSERVICE ?
d0c832f3 1138 "ssh-userauth" :
188adeb2 1139 authctxt->service);
cbc5abf9 1140 if (datafellows & SSH_BUG_PKAUTH) {
1141 buffer_put_char(&b, have_sig);
1142 } else {
1143 buffer_put_cstring(&b, authctxt->method->name);
1144 buffer_put_char(&b, have_sig);
43348518 1145 buffer_put_cstring(&b, key_ssh_name(id->key));
cbc5abf9 1146 }
a306f2dd 1147 buffer_put_string(&b, blob, bloblen);
a306f2dd 1148
1149 /* generate signature */
43348518 1150 ret = identity_sign(id, &signature, &slen,
fee56204 1151 buffer_ptr(&b), buffer_len(&b));
2e73a022 1152 if (ret == -1) {
1153 xfree(blob);
1154 buffer_free(&b);
1155 return 0;
1156 }
fa08c86b 1157#ifdef DEBUG_PK
a306f2dd 1158 buffer_dump(&b);
1159#endif
cbc5abf9 1160 if (datafellows & SSH_BUG_PKSERVICE) {
d0c832f3 1161 buffer_clear(&b);
1162 buffer_append(&b, session_id2, session_id2_len);
fee56204 1163 skip = session_id2_len;
d0c832f3 1164 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 1165 buffer_put_cstring(&b, authctxt->server_user);
1166 buffer_put_cstring(&b, authctxt->service);
94ec8c6b 1167 buffer_put_cstring(&b, authctxt->method->name);
1168 buffer_put_char(&b, have_sig);
cbc5abf9 1169 if (!(datafellows & SSH_BUG_PKAUTH))
43348518 1170 buffer_put_cstring(&b, key_ssh_name(id->key));
d0c832f3 1171 buffer_put_string(&b, blob, bloblen);
1172 }
1173 xfree(blob);
fee56204 1174
a306f2dd 1175 /* append signature */
1176 buffer_put_string(&b, signature, slen);
1177 xfree(signature);
1178
1179 /* skip session id and packet type */
74fc9186 1180 if (buffer_len(&b) < skip + 1)
188adeb2 1181 fatal("userauth_pubkey: internal error");
74fc9186 1182 buffer_consume(&b, skip + 1);
a306f2dd 1183
1184 /* put remaining data from buffer into packet */
1185 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1186 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1187 buffer_free(&b);
a306f2dd 1188 packet_send();
2e73a022 1189
1190 return 1;
4c8722d9 1191}
1192
396c147e 1193static int
43348518 1194send_pubkey_test(Authctxt *authctxt, Identity *id)
4c8722d9 1195{
fee56204 1196 u_char *blob;
21b30f38 1197 u_int bloblen, have_sig = 0;
4c8722d9 1198
fee56204 1199 debug3("send_pubkey_test");
1200
43348518 1201 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
fee56204 1202 /* we cannot handle this key */
1203 debug3("send_pubkey_test: cannot handle key");
4c8722d9 1204 return 0;
1205 }
fee56204 1206 /* register callback for USERAUTH_PK_OK message */
fee56204 1207 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
4c8722d9 1208
fee56204 1209 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1210 packet_put_cstring(authctxt->server_user);
1211 packet_put_cstring(authctxt->service);
1212 packet_put_cstring(authctxt->method->name);
1213 packet_put_char(have_sig);
1214 if (!(datafellows & SSH_BUG_PKAUTH))
43348518 1215 packet_put_cstring(key_ssh_name(id->key));
fee56204 1216 packet_put_string(blob, bloblen);
1217 xfree(blob);
1218 packet_send();
1219 return 1;
1220}
1221
396c147e 1222static Key *
fee56204 1223load_identity_file(char *filename)
1224{
1225 Key *private;
1226 char prompt[300], *passphrase;
fc231518 1227 int perm_ok, quit, i;
d156519a 1228 struct stat st;
fee56204 1229
d156519a 1230 if (stat(filename, &st) < 0) {
1231 debug3("no such identity: %s", filename);
1232 return NULL;
1233 }
fc231518 1234 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1235 if (!perm_ok)
1236 return NULL;
aeaa3d9e 1237 if (private == NULL) {
1238 if (options.batch_mode)
fee56204 1239 return NULL;
4c8722d9 1240 snprintf(prompt, sizeof prompt,
184eed6a 1241 "Enter passphrase for key '%.100s': ", filename);
188adeb2 1242 for (i = 0; i < options.number_of_password_prompts; i++) {
1243 passphrase = read_passphrase(prompt, 0);
1244 if (strcmp(passphrase, "") != 0) {
fc231518 1245 private = key_load_private_type(KEY_UNSPEC,
1246 filename, passphrase, NULL, NULL);
fee56204 1247 quit = 0;
188adeb2 1248 } else {
1249 debug2("no passphrase given, try next key");
fee56204 1250 quit = 1;
188adeb2 1251 }
1252 memset(passphrase, 0, strlen(passphrase));
1253 xfree(passphrase);
aeaa3d9e 1254 if (private != NULL || quit)
188adeb2 1255 break;
1256 debug2("bad passphrase given, try again...");
1257 }
4c8722d9 1258 }
fee56204 1259 return private;
1260}
1261
43348518 1262/*
1263 * try keys in the following order:
1264 * 1. agent keys that are found in the config file
1265 * 2. other agent keys
1266 * 3. keys that are only listed in the config file
1267 */
1268static void
1269pubkey_prepare(Authctxt *authctxt)
fee56204 1270{
43348518 1271 Identity *id;
1272 Idlist agent, files, *preferred;
1273 Key *key;
1274 AuthenticationConnection *ac;
1275 char *comment;
1276 int i, found;
1277
1278 TAILQ_INIT(&agent); /* keys from the agent */
1279 TAILQ_INIT(&files); /* keys from the config file */
1280 preferred = &authctxt->keys;
1281 TAILQ_INIT(preferred); /* preferred order of keys */
1282
1283 /* list of keys stored in the filesystem */
1284 for (i = 0; i < options.num_identity_files; i++) {
1285 key = options.identity_keys[i];
1286 if (key && key->type == KEY_RSA1)
1287 continue;
1288 options.identity_keys[i] = NULL;
52e3daed 1289 id = xcalloc(1, sizeof(*id));
43348518 1290 id->key = key;
1291 id->filename = xstrdup(options.identity_files[i]);
1292 TAILQ_INSERT_TAIL(&files, id, next);
1293 }
1294 /* list of keys supported by the agent */
1295 if ((ac = ssh_get_authentication_connection())) {
1296 for (key = ssh_get_first_identity(ac, &comment, 2);
1297 key != NULL;
1298 key = ssh_get_next_identity(ac, &comment, 2)) {
1299 found = 0;
1300 TAILQ_FOREACH(id, &files, next) {
aff51935 1301 /* agent keys from the config file are preferred */
43348518 1302 if (key_equal(key, id->key)) {
1303 key_free(key);
1304 xfree(comment);
1305 TAILQ_REMOVE(&files, id, next);
1306 TAILQ_INSERT_TAIL(preferred, id, next);
1307 id->ac = ac;
1308 found = 1;
1309 break;
1310 }
1311 }
3a065ed0 1312 if (!found && !options.identities_only) {
52e3daed 1313 id = xcalloc(1, sizeof(*id));
43348518 1314 id->key = key;
1315 id->filename = comment;
1316 id->ac = ac;
1317 TAILQ_INSERT_TAIL(&agent, id, next);
1318 }
1319 }
1320 /* append remaining agent keys */
1321 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1322 TAILQ_REMOVE(&agent, id, next);
1323 TAILQ_INSERT_TAIL(preferred, id, next);
1324 }
1325 authctxt->agent = ac;
1326 }
1327 /* append remaining keys from the config file */
1328 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1329 TAILQ_REMOVE(&files, id, next);
1330 TAILQ_INSERT_TAIL(preferred, id, next);
1331 }
1332 TAILQ_FOREACH(id, preferred, next) {
1333 debug2("key: %s (%p)", id->filename, id->key);
1334 }
fee56204 1335}
1336
43348518 1337static void
1338pubkey_cleanup(Authctxt *authctxt)
2e73a022 1339{
43348518 1340 Identity *id;
1341
1342 if (authctxt->agent != NULL)
1343 ssh_close_authentication_connection(authctxt->agent);
1344 for (id = TAILQ_FIRST(&authctxt->keys); id;
1345 id = TAILQ_FIRST(&authctxt->keys)) {
1346 TAILQ_REMOVE(&authctxt->keys, id, next);
1347 if (id->key)
1348 key_free(id->key);
1349 if (id->filename)
1350 xfree(id->filename);
1351 xfree(id);
2e73a022 1352 }
a306f2dd 1353}
1354
188adeb2 1355int
1356userauth_pubkey(Authctxt *authctxt)
a306f2dd 1357{
43348518 1358 Identity *id;
188adeb2 1359 int sent = 0;
1360
43348518 1361 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1362 if (id->tried++)
1363 return (0);
b56e99e2 1364 /* move key to the end of the queue */
43348518 1365 TAILQ_REMOVE(&authctxt->keys, id, next);
1366 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1367 /*
1368 * send a test message if we have the public key. for
1369 * encrypted keys we cannot do this and have to load the
1370 * private key instead
1371 */
1372 if (id->key && id->key->type != KEY_RSA1) {
1373 debug("Offering public key: %s", id->filename);
1374 sent = send_pubkey_test(authctxt, id);
1375 } else if (id->key == NULL) {
1376 debug("Trying private key: %s", id->filename);
1377 id->key = load_identity_file(id->filename);
1378 if (id->key != NULL) {
1379 id->isprivate = 1;
1380 sent = sign_and_send_pubkey(authctxt, id);
1381 key_free(id->key);
1382 id->key = NULL;
fee56204 1383 }
fee56204 1384 }
43348518 1385 if (sent)
1386 return (sent);
fa08c86b 1387 }
43348518 1388 return (0);
188adeb2 1389}
a306f2dd 1390
94ec8c6b 1391/*
1392 * Send userauth request message specifying keyboard-interactive method.
1393 */
1394int
1395userauth_kbdint(Authctxt *authctxt)
1396{
1397 static int attempt = 0;
1398
1399 if (attempt++ >= options.number_of_password_prompts)
1400 return 0;
6b759005 1401 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1402 if (attempt > 1 && !authctxt->info_req_seen) {
1403 debug3("userauth_kbdint: disable: no info_req_seen");
1404 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1405 return 0;
1406 }
94ec8c6b 1407
1408 debug2("userauth_kbdint");
1409 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1410 packet_put_cstring(authctxt->server_user);
1411 packet_put_cstring(authctxt->service);
1412 packet_put_cstring(authctxt->method->name);
1413 packet_put_cstring(""); /* lang */
1414 packet_put_cstring(options.kbd_interactive_devices ?
1415 options.kbd_interactive_devices : "");
1416 packet_send();
94ec8c6b 1417
1418 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1419 return 1;
1420}
1421
1422/*
f72e01a5 1423 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
94ec8c6b 1424 */
1425void
7819b5c3 1426input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
94ec8c6b 1427{
1428 Authctxt *authctxt = ctxt;
f72e01a5 1429 char *name, *inst, *lang, *prompt, *response;
1e3b8b07 1430 u_int num_prompts, i;
94ec8c6b 1431 int echo = 0;
1432
1433 debug2("input_userauth_info_req");
1434
1435 if (authctxt == NULL)
1436 fatal("input_userauth_info_req: no authentication context");
1437
6b759005 1438 authctxt->info_req_seen = 1;
1439
94ec8c6b 1440 name = packet_get_string(NULL);
1441 inst = packet_get_string(NULL);
1442 lang = packet_get_string(NULL);
94ec8c6b 1443 if (strlen(name) > 0)
bbe88b6d 1444 logit("%s", name);
94ec8c6b 1445 if (strlen(inst) > 0)
bbe88b6d 1446 logit("%s", inst);
f72e01a5 1447 xfree(name);
94ec8c6b 1448 xfree(inst);
f72e01a5 1449 xfree(lang);
94ec8c6b 1450
1451 num_prompts = packet_get_int();
1452 /*
1453 * Begin to build info response packet based on prompts requested.
1454 * We commit to providing the correct number of responses, so if
1455 * further on we run into a problem that prevents this, we have to
1456 * be sure and clean this up and send a correct error response.
1457 */
1458 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1459 packet_put_int(num_prompts);
1460
5ba55ada 1461 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
94ec8c6b 1462 for (i = 0; i < num_prompts; i++) {
1463 prompt = packet_get_string(NULL);
1464 echo = packet_get_char();
1465
1843a425 1466 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
94ec8c6b 1467
a6215e53 1468 packet_put_cstring(response);
94ec8c6b 1469 memset(response, 0, strlen(response));
1470 xfree(response);
1471 xfree(prompt);
1472 }
95500969 1473 packet_check_eom(); /* done with parsing incoming message. */
94ec8c6b 1474
b4b701be 1475 packet_add_padding(64);
94ec8c6b 1476 packet_send();
94ec8c6b 1477}
a306f2dd 1478
39c00dc2 1479static int
d6133f43 1480ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
39c00dc2 1481 u_char *data, u_int datalen)
1482{
1483 Buffer b;
7091a26b 1484 struct stat st;
39c00dc2 1485 pid_t pid;
d4826734 1486 int to[2], from[2], status, version = 2;
39c00dc2 1487
6226a8f8 1488 debug2("ssh_keysign called");
39c00dc2 1489
7091a26b 1490 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1491 error("ssh_keysign: no installed: %s", strerror(errno));
1492 return -1;
1493 }
39c00dc2 1494 if (fflush(stdout) != 0)
1495 error("ssh_keysign: fflush: %s", strerror(errno));
1496 if (pipe(to) < 0) {
1497 error("ssh_keysign: pipe: %s", strerror(errno));
1498 return -1;
1499 }
1500 if (pipe(from) < 0) {
1501 error("ssh_keysign: pipe: %s", strerror(errno));
1502 return -1;
1503 }
1504 if ((pid = fork()) < 0) {
1505 error("ssh_keysign: fork: %s", strerror(errno));
1506 return -1;
1507 }
1508 if (pid == 0) {
3c33c1b6 1509 permanently_drop_suid(getuid());
39c00dc2 1510 close(from[0]);
1511 if (dup2(from[1], STDOUT_FILENO) < 0)
1512 fatal("ssh_keysign: dup2: %s", strerror(errno));
1513 close(to[1]);
1514 if (dup2(to[0], STDIN_FILENO) < 0)
1515 fatal("ssh_keysign: dup2: %s", strerror(errno));
d4826734 1516 close(from[1]);
1517 close(to[0]);
a3f69458 1518 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
39c00dc2 1519 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1520 strerror(errno));
1521 }
1522 close(from[1]);
1523 close(to[0]);
1524
1525 buffer_init(&b);
d4826734 1526 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
39c00dc2 1527 buffer_put_string(&b, data, datalen);
d3cbe6f8 1528 if (ssh_msg_send(to[1], version, &b) == -1)
1529 fatal("ssh_keysign: couldn't send request");
39c00dc2 1530
e987fdbe 1531 if (ssh_msg_recv(from[0], &b) < 0) {
7091a26b 1532 error("ssh_keysign: no reply");
43f7a4b8 1533 buffer_free(&b);
39c00dc2 1534 return -1;
1535 }
39c00dc2 1536 close(from[0]);
1537 close(to[1]);
1538
d4826734 1539 while (waitpid(pid, &status, 0) < 0)
1540 if (errno != EINTR)
1541 break;
39c00dc2 1542
7091a26b 1543 if (buffer_get_char(&b) != version) {
1544 error("ssh_keysign: bad version");
43f7a4b8 1545 buffer_free(&b);
7091a26b 1546 return -1;
1547 }
1548 *sigp = buffer_get_string(&b, lenp);
43f7a4b8 1549 buffer_free(&b);
7091a26b 1550
39c00dc2 1551 return 0;
1552}
1553
8002af61 1554int
1555userauth_hostbased(Authctxt *authctxt)
1556{
1557 Key *private = NULL;
39c00dc2 1558 Sensitive *sensitive = authctxt->sensitive;
8002af61 1559 Buffer b;
1560 u_char *signature, *blob;
7fd1deb2 1561 char *chost, *pkalg, *p, myname[NI_MAXHOST];
8dddf799 1562 const char *service;
8002af61 1563 u_int blen, slen;
f98c3421 1564 int ok, i, len, found = 0;
8002af61 1565
8002af61 1566 /* check for a useful key */
39c00dc2 1567 for (i = 0; i < sensitive->nkeys; i++) {
1568 private = sensitive->keys[i];
8002af61 1569 if (private && private->type != KEY_RSA1) {
1570 found = 1;
1571 /* we take and free the key */
39c00dc2 1572 sensitive->keys[i] = NULL;
8002af61 1573 break;
1574 }
1575 }
1576 if (!found) {
6226a8f8 1577 debug("No more client hostkeys for hostbased authentication.");
8002af61 1578 return 0;
1579 }
1580 if (key_to_blob(private, &blob, &blen) == 0) {
1581 key_free(private);
8002af61 1582 return 0;
1583 }
e8d59b4d 1584 /* figure out a name for the client host */
7fd1deb2 1585 p = NULL;
1586 if (packet_connection_is_on_socket())
1587 p = get_local_name(packet_get_connection_in());
1588 if (p == NULL) {
1589 if (gethostname(myname, sizeof(myname)) == -1) {
1590 verbose("userauth_hostbased: gethostname: %s",
1591 strerror(errno));
1592 } else
1593 p = xstrdup(myname);
1594 }
e8d59b4d 1595 if (p == NULL) {
1596 error("userauth_hostbased: cannot get local ipaddr/name");
1597 key_free(private);
18fc231c 1598 xfree(blob);
e8d59b4d 1599 return 0;
1600 }
1601 len = strlen(p) + 2;
52e3daed 1602 xasprintf(&chost, "%s.", p);
e8d59b4d 1603 debug2("userauth_hostbased: chost %s", chost);
3e2f2431 1604 xfree(p);
e8d59b4d 1605
8dddf799 1606 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1607 authctxt->service;
8002af61 1608 pkalg = xstrdup(key_ssh_name(private));
1609 buffer_init(&b);
8002af61 1610 /* construct data */
8dddf799 1611 buffer_put_string(&b, session_id2, session_id2_len);
8002af61 1612 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1613 buffer_put_cstring(&b, authctxt->server_user);
8dddf799 1614 buffer_put_cstring(&b, service);
8002af61 1615 buffer_put_cstring(&b, authctxt->method->name);
1616 buffer_put_cstring(&b, pkalg);
1617 buffer_put_string(&b, blob, blen);
1618 buffer_put_cstring(&b, chost);
1619 buffer_put_cstring(&b, authctxt->local_user);
1620#ifdef DEBUG_PK
1621 buffer_dump(&b);
1622#endif
39c00dc2 1623 if (sensitive->external_keysign)
1624 ok = ssh_keysign(private, &signature, &slen,
1625 buffer_ptr(&b), buffer_len(&b));
1626 else
1627 ok = key_sign(private, &signature, &slen,
1628 buffer_ptr(&b), buffer_len(&b));
8002af61 1629 key_free(private);
1630 buffer_free(&b);
1631 if (ok != 0) {
1632 error("key_sign failed");
1633 xfree(chost);
1634 xfree(pkalg);
18fc231c 1635 xfree(blob);
8002af61 1636 return 0;
1637 }
1638 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1639 packet_put_cstring(authctxt->server_user);
1640 packet_put_cstring(authctxt->service);
1641 packet_put_cstring(authctxt->method->name);
1642 packet_put_cstring(pkalg);
1643 packet_put_string(blob, blen);
1644 packet_put_cstring(chost);
1645 packet_put_cstring(authctxt->local_user);
1646 packet_put_string(signature, slen);
1647 memset(signature, 's', slen);
1648 xfree(signature);
1649 xfree(chost);
1650 xfree(pkalg);
18fc231c 1651 xfree(blob);
8002af61 1652
1653 packet_send();
1654 return 1;
1655}
1656
5adf6b9a 1657#ifdef JPAKE
1658int
1659userauth_jpake(Authctxt *authctxt)
1660{
1661 struct jpake_ctx *pctx;
1662 u_char *x1_proof, *x2_proof;
1663 u_int x1_proof_len, x2_proof_len;
1664 static int attempt = 0; /* XXX share with userauth_password's? */
1665
1666 if (attempt++ >= options.number_of_password_prompts)
1667 return 0;
1668 if (attempt != 1)
1669 error("Permission denied, please try again.");
1670
1671 if (authctxt->methoddata != NULL)
1672 fatal("%s: authctxt->methoddata already set (%p)",
1673 __func__, authctxt->methoddata);
1674
1675 authctxt->methoddata = pctx = jpake_new();
1676
1677 /*
1678 * Send request immediately, to get the protocol going while
1679 * we do the initial computations.
1680 */
1681 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1682 packet_put_cstring(authctxt->server_user);
1683 packet_put_cstring(authctxt->service);
1684 packet_put_cstring(authctxt->method->name);
1685 packet_send();
1686 packet_write_wait();
1687
1688 jpake_step1(pctx->grp,
1689 &pctx->client_id, &pctx->client_id_len,
1690 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1691 &x1_proof, &x1_proof_len,
1692 &x2_proof, &x2_proof_len);
1693
1694 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1695
1696 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1697 packet_put_string(pctx->client_id, pctx->client_id_len);
1698 packet_put_bignum2(pctx->g_x1);
1699 packet_put_bignum2(pctx->g_x2);
1700 packet_put_string(x1_proof, x1_proof_len);
1701 packet_put_string(x2_proof, x2_proof_len);
1702 packet_send();
1703
1704 bzero(x1_proof, x1_proof_len);
1705 bzero(x2_proof, x2_proof_len);
1706 xfree(x1_proof);
1707 xfree(x2_proof);
1708
1709 /* Expect step 1 packet from peer */
1710 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1711 input_userauth_jpake_server_step1);
1712
1713 return 1;
1714}
1715
1716void
1717userauth_jpake_cleanup(Authctxt *authctxt)
1718{
1719 debug3("%s: clean up", __func__);
1720 if (authctxt->methoddata != NULL) {
1721 jpake_free(authctxt->methoddata);
1722 authctxt->methoddata = NULL;
1723 }
1724}
1725#endif /* JPAKE */
1726
188adeb2 1727/* find auth method */
1728
188adeb2 1729/*
1730 * given auth method name, if configurable options permit this method fill
1731 * in auth_ident field and return true, otherwise return false.
1732 */
396c147e 1733static int
188adeb2 1734authmethod_is_enabled(Authmethod *method)
1735{
1736 if (method == NULL)
1737 return 0;
1738 /* return false if options indicate this method is disabled */
1739 if (method->enabled == NULL || *method->enabled == 0)
1740 return 0;
1741 /* return false if batch mode is enabled but method needs interactive mode */
1742 if (method->batch_flag != NULL && *method->batch_flag != 0)
1743 return 0;
1744 return 1;
1745}
a306f2dd 1746
396c147e 1747static Authmethod *
188adeb2 1748authmethod_lookup(const char *name)
1749{
1750 Authmethod *method = NULL;
1751 if (name != NULL)
1752 for (method = authmethods; method->name != NULL; method++)
1753 if (strcmp(name, method->name) == 0)
1754 return method;
1755 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1756 return NULL;
1757}
1758
cab80f75 1759/* XXX internal state */
1760static Authmethod *current = NULL;
1761static char *supported = NULL;
1762static char *preferred = NULL;
d6133f43 1763
188adeb2 1764/*
1765 * Given the authentication method list sent by the server, return the
1766 * next method we should try. If the server initially sends a nil list,
cd332296 1767 * use a built-in default list.
2b87da3b 1768 */
396c147e 1769static Authmethod *
188adeb2 1770authmethod_get(char *authlist)
1771{
cab80f75 1772 char *name = NULL;
21b30f38 1773 u_int next;
2b87da3b 1774
188adeb2 1775 /* Use a suitable default if we're passed a nil list. */
1776 if (authlist == NULL || strlen(authlist) == 0)
cab80f75 1777 authlist = options.preferred_authentications;
1778
1779 if (supported == NULL || strcmp(authlist, supported) != 0) {
1780 debug3("start over, passed a different list %s", authlist);
1781 if (supported != NULL)
1782 xfree(supported);
1783 supported = xstrdup(authlist);
1784 preferred = options.preferred_authentications;
1785 debug3("preferred %s", preferred);
1786 current = NULL;
1787 } else if (current != NULL && authmethod_is_enabled(current))
1788 return current;
188adeb2 1789
cab80f75 1790 for (;;) {
1791 if ((name = match_list(preferred, supported, &next)) == NULL) {
6226a8f8 1792 debug("No more authentication methods to try.");
cab80f75 1793 current = NULL;
1794 return NULL;
1795 }
1796 preferred += next;
94ec8c6b 1797 debug3("authmethod_lookup %s", name);
cab80f75 1798 debug3("remaining preferred: %s", preferred);
1799 if ((current = authmethod_lookup(name)) != NULL &&
1800 authmethod_is_enabled(current)) {
94ec8c6b 1801 debug3("authmethod_is_enabled %s", name);
6226a8f8 1802 debug("Next authentication method: %s", name);
cab80f75 1803 return current;
94ec8c6b 1804 }
a306f2dd 1805 }
cab80f75 1806}
a22aff1f 1807
ffb215be 1808static char *
cab80f75 1809authmethods_get(void)
1810{
1811 Authmethod *method = NULL;
3469eac4 1812 Buffer b;
1813 char *list;
cab80f75 1814
3469eac4 1815 buffer_init(&b);
cab80f75 1816 for (method = authmethods; method->name != NULL; method++) {
1817 if (authmethod_is_enabled(method)) {
3469eac4 1818 if (buffer_len(&b) > 0)
1819 buffer_append(&b, ",", 1);
1820 buffer_append(&b, method->name, strlen(method->name));
cab80f75 1821 }
1822 }
3469eac4 1823 buffer_append(&b, "\0", 1);
1824 list = xstrdup(buffer_ptr(&b));
1825 buffer_free(&b);
1826 return list;
a306f2dd 1827}
5adf6b9a 1828
This page took 0.667594 seconds and 5 git commands to generate.