Skip to content

Commit

Permalink
Prompt for known_hosts file if sandboxing denies access and store sec…
Browse files Browse the repository at this point in the history
…urity scoped bookmark in Preferences.

Former-commit-id: cdc7fd0378130f4ceec6ff08b723ebd8fe91e5f5
  • Loading branch information
dkocher committed Nov 2, 2013
1 parent 93e9227 commit 8faa526
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions source/ch/cyberduck/core/sftp/MemoryHostKeyVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public MemoryHostKeyVerifier() {
}

public MemoryHostKeyVerifier(final Local file) {
this.setDatabase(file);
}

protected void setDatabase(Local file) {
if(!file.exists()) {
file.touch();
}
Expand Down
56 changes: 54 additions & 2 deletions source/ch/cyberduck/ui/cocoa/AlertHostKeyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
import ch.cyberduck.ui.HostKeyControllerFactory;
import ch.cyberduck.ui.cocoa.application.NSAlert;
import ch.cyberduck.ui.cocoa.application.NSCell;
import ch.cyberduck.ui.cocoa.application.NSOpenPanel;
import ch.cyberduck.ui.cocoa.application.NSWindow;
import ch.cyberduck.ui.cocoa.foundation.NSArray;
import ch.cyberduck.ui.cocoa.foundation.NSEnumerator;
import ch.cyberduck.ui.cocoa.foundation.NSObject;

import org.apache.log4j.Logger;
import org.rococoa.Foundation;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -72,8 +78,12 @@ public HostKeyController create(final Controller c) {
*/
private final Local file;

private NSOpenPanel panel;

public AlertHostKeyController(final WindowController c) {
this(c, LocalFactory.createLocal(Preferences.instance().getProperty("ssh.knownhosts")));
this(c, LocalFactory.createLocal(Preferences.instance().getProperty("ssh.knownhosts")).withBookmark(
Preferences.instance().getProperty("ssh.knownhosts.bookmark")
));
}

public AlertHostKeyController(final WindowController parent, final Local file) {
Expand All @@ -84,7 +94,49 @@ public AlertHostKeyController(final WindowController parent, final Local file) {

@Override
protected boolean isHostKeyDatabaseWritable() {
return file.attributes().getPermission().isWritable();
if(!file.attributes().getPermission().isWritable()) {
// Ask for known_hosts file to select. Possibly not allowed by sandbox
final SheetController sheet = new SheetController(parent) {
@Override
public void callback(int returncode) {
//
}

@Override
protected void beginSheetImpl() {
panel = NSOpenPanel.openPanel();
panel.setCanChooseDirectories(false);
panel.setCanChooseFiles(true);
panel.setAllowsMultipleSelection(false);
panel.setMessage(LocaleFactory.localizedString("Select the SSH known_hosts file to save host keys.", "Credentials"));
panel.setPrompt(LocaleFactory.localizedString("Choose"));
panel.beginSheetForDirectory(LocalFactory.createLocal("~/.ssh").getAbsolute(),
null, parent.window(), this.id(), Foundation.selector("sheetDidClose:returnCode:contextInfo:"), null);
}

@Override
public NSWindow window() {
return panel;
}
};
sheet.beginSheet();
if(sheet.returnCode() == SheetCallback.DEFAULT_OPTION) {
NSArray selected = panel.filenames();
final NSEnumerator enumerator = selected.objectEnumerator();
NSObject next;
while((next = enumerator.nextObject()) != null) {
final Local f = LocalFactory.createLocal(next.toString());
Preferences.instance().setProperty("ssh.knownhosts", f.getAbbreviatedPath());
Preferences.instance().setProperty("ssh.knownhosts.bookmark", f.getBookmark());
setDatabase(f);
return f.attributes().getPermission().isWritable();
}
}
if(sheet.returnCode() == SheetCallback.CANCEL_OPTION) {
return false;
}
}
return true;
}

@Override
Expand Down

0 comments on commit 8faa526

Please sign in to comment.