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