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