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