1 package ch
.cyberduck
.ui
.cocoa
;
4 * Copyright (c) 2007 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch
.cyberduck
.binding
.application
.NSAlert
;
22 import ch
.cyberduck
.binding
.application
.NSCell
;
23 import ch
.cyberduck
.binding
.application
.SheetCallback
;
24 import ch
.cyberduck
.core
.DefaultProviderHelpService
;
25 import ch
.cyberduck
.core
.Local
;
26 import ch
.cyberduck
.core
.LocalFactory
;
27 import ch
.cyberduck
.core
.LocaleFactory
;
28 import ch
.cyberduck
.core
.Scheme
;
29 import ch
.cyberduck
.core
.exception
.ChecksumException
;
30 import ch
.cyberduck
.core
.exception
.ConnectionCanceledException
;
31 import ch
.cyberduck
.core
.io
.MD5ChecksumCompute
;
32 import ch
.cyberduck
.core
.preferences
.PreferencesFactory
;
33 import ch
.cyberduck
.core
.sftp
.openssh
.OpenSSHHostKeyVerifier
;
35 import org
.apache
.log4j
.Logger
;
37 import java
.security
.PublicKey
;
38 import java
.text
.MessageFormat
;
40 import net
.schmizz
.sshj
.common
.KeyType
;
43 * Using known_hosts from OpenSSH to store accepted host keys.
47 public class AlertHostKeyController
extends OpenSSHHostKeyVerifier
{
48 private static final Logger log
= Logger
.getLogger(AlertHostKeyController
.class);
50 private WindowController parent
;
52 public AlertHostKeyController(final WindowController c
) {
53 this(c
, LocalFactory
.get(PreferencesFactory
.get().getProperty("ssh.knownhosts")).withBookmark(
54 PreferencesFactory
.get().getProperty("ssh.knownhosts.bookmark")
58 public AlertHostKeyController(final WindowController parent
, final Local file
) {
64 protected boolean isUnknownKeyAccepted(final String hostname
, final PublicKey key
)
65 throws ConnectionCanceledException
, ChecksumException
{
66 final NSAlert alert
= NSAlert
.alert(MessageFormat
.format(LocaleFactory
.localizedString("Unknown fingerprint", "Sftp"), hostname
), //title
67 MessageFormat
.format(LocaleFactory
.localizedString("The fingerprint for the {1} key sent by the server is {0}.", "Sftp"),
68 new MD5ChecksumCompute().fingerprint(key
),
69 KeyType
.fromKey(key
).name()),
70 LocaleFactory
.localizedString("Allow"), // default button
71 LocaleFactory
.localizedString("Deny"), // alternate button
74 alert
.setShowsSuppressionButton(true);
75 alert
.suppressionButton().setTitle(LocaleFactory
.localizedString("Always"));
76 alert
.setShowsHelp(true);
77 SheetController c
= new AlertController(parent
, alert
) {
79 public void callback(final int returncode
) {
80 if(returncode
== DEFAULT_OPTION
) {// allow host (once)
82 alert
.suppressionButton().state() == NSCell
.NSOnState
);
85 log
.warn("Cannot continue without a valid host key");
90 protected void help() {
91 new DefaultProviderHelpService().help(Scheme
.sftp
);
95 if(c
.returnCode() == SheetCallback
.ALTERNATE_OPTION
) {
96 throw new ConnectionCanceledException();
98 return c
.returnCode() == SheetCallback
.DEFAULT_OPTION
;
103 protected boolean isChangedKeyAccepted(final String hostname
, final PublicKey key
)
104 throws ConnectionCanceledException
, ChecksumException
{
105 NSAlert alert
= NSAlert
.alert(MessageFormat
.format(LocaleFactory
.localizedString("Changed fingerprint", "Sftp"), hostname
), //title
106 MessageFormat
.format(LocaleFactory
.localizedString("The fingerprint for the {1} key sent by the server is {0}.", "Sftp"),
107 new MD5ChecksumCompute().fingerprint(key
),
108 KeyType
.fromKey(key
).name()),
109 LocaleFactory
.localizedString("Allow"), // defaultbutton
110 LocaleFactory
.localizedString("Deny"), //alternative button
113 alert
.setShowsSuppressionButton(true);
114 alert
.suppressionButton().setTitle(LocaleFactory
.localizedString("Always"));
115 alert
.setShowsHelp(true);
116 SheetController c
= new AlertController(parent
, alert
) {
118 public void callback(final int returncode
) {
119 if(returncode
== DEFAULT_OPTION
) {
121 alert
.suppressionButton().state() == NSCell
.NSOnState
);
124 log
.warn("Cannot continue without a valid host key");
129 protected void help() {
130 new DefaultProviderHelpService().help(Scheme
.sftp
);
134 if(c
.returnCode() == SheetCallback
.ALTERNATE_OPTION
) {
135 throw new ConnectionCanceledException();
137 return c
.returnCode() == SheetCallback
.DEFAULT_OPTION
;