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