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