Fix compile.
[cyberduck.git] / source / ch / cyberduck / ui / controller / HostKeyController.cs
blob7c4e348da55d08919bccb7f6456f2353a30cb7b3
1 //
2 // Copyright (c) 2010-2013 Yves Langisch. All rights reserved.
3 // http://cyberduck.ch/
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // Bug fixes, suggestions and comments should be sent to:
16 // yves@cyberduck.ch
17 //
19 using System;
20 using Ch.Cyberduck.Ui.Winforms.Taskdialog;
21 using ch.cyberduck.core;
22 using ch.cyberduck.core.exception;
23 using ch.cyberduck.core.sftp;
24 using ch.cyberduck.ui;
25 using ch.ethz.ssh2;
26 using org.apache.log4j;
28 namespace Ch.Cyberduck.Ui.Controller
30 public class HostKeyController : PreferencesHostKeyVerifier
32 private static readonly Logger Log = Logger.getLogger(typeof (HostKeyController).FullName);
34 /// <summary>
35 /// Parent browser
36 /// </summary>
37 private readonly WindowController _parent;
39 private HostKeyController(WindowController c)
41 _parent = c;
44 protected override bool isUnknownKeyAccepted(string hostname, int port, string serverHostKeyAlgorithm,
45 byte[] serverHostKey)
47 if (base.isUnknownKeyAccepted(hostname, port, serverHostKeyAlgorithm, serverHostKey))
49 return true;
51 AsyncController.AsyncDelegate d = delegate
53 _parent.CommandBox(
54 String.Format(LocaleFactory.localizedString("Unknown host key for {0}."), hostname),
55 String.Format(LocaleFactory.localizedString("Unknown host key for {0}."), hostname),
56 String.Format(
57 LocaleFactory.localizedString(
58 "The host is currently unknown to the system. The host key fingerprint is {0}."),
59 KnownHosts.createHexFingerprint(serverHostKeyAlgorithm, serverHostKey)),
60 String.Format("{0}|{1}", LocaleFactory.localizedString("Allow"),
61 LocaleFactory.localizedString("Deny")), false,
62 LocaleFactory.localizedString("Always"), SysIcons.Question,
63 Preferences.instance().getProperty("website.help") + "/" + Scheme.sftp.name(),
64 delegate(int option, bool verificationChecked)
66 switch (option)
68 case 0:
69 allow(hostname, serverHostKeyAlgorithm, serverHostKey, verificationChecked);
70 break;
71 case 1:
72 Log.warn("Cannot continue without a valid host key");
73 throw new ConnectionCanceledException();
75 });
77 _parent.Invoke(d, true);
78 return true;
81 protected override bool isChangedKeyAccepted(string hostname, int port, string serverHostKeyAlgorithm,
82 byte[] serverHostKey)
84 AsyncController.AsyncDelegate d = delegate
86 _parent.CommandBox(
87 String.Format(LocaleFactory.localizedString("Host key mismatch for {0}"), hostname),
88 String.Format(LocaleFactory.localizedString("Host key mismatch for {0}"), hostname),
89 String.Format(LocaleFactory.localizedString("The host key supplied is {0}."),
90 KnownHosts.createHexFingerprint(serverHostKeyAlgorithm, serverHostKey)),
91 String.Format("{0}|{1}", LocaleFactory.localizedString("Allow"),
92 LocaleFactory.localizedString("Deny")), false,
93 LocaleFactory.localizedString("Always"), SysIcons.Warning,
94 Preferences.instance().getProperty("website.help") + "/" + Scheme.sftp.name(),
95 delegate(int option, bool verificationChecked)
97 switch (option)
99 case 0:
100 allow(hostname, serverHostKeyAlgorithm, serverHostKey, verificationChecked);
101 break;
102 case 1:
103 Log.warn("Cannot continue without a valid host key");
104 throw new ConnectionCanceledException();
108 _parent.Invoke(d, true);
109 return true;
112 public static void Register()
114 HostKeyControllerFactory.addFactory(ch.cyberduck.core.Factory.NATIVE_PLATFORM, new Factory());
117 private class Factory : HostKeyControllerFactory
119 protected override object create()
121 return null;
124 public override HostKeyCallback create(ch.cyberduck.ui.Controller c, Protocol protocol)
126 if (Scheme.sftp.equals(protocol.getScheme()))
128 return new HostKeyController(c as WindowController);
130 return new DisabledHostKeyCallback();