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