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