Commit 89797286 authored by Brad Kennedy's avatar Brad Kennedy
Browse files

Fixed ssh from matlab, using a better dialog box for password and only prompt for password once

parent bf422eae
......@@ -58,8 +58,8 @@ end
exec_properties = {
'session_init', ...
'job_name', ...
'mfile_name', ...
'job_init', ...
'mfile_name', ...
'm_init', ...
'submit_options', ...
'memory', ...
......
......@@ -4,8 +4,9 @@
% >> out = read_jobs(driver, result_str)
%
% Inputs:
% driver - the driver we are using, should be subtype of ef_base_driver
% job_spec - results of the output of running the submission script
% driver - the driver we are using, should be subtype of ef_base_driver
% result_str - results of the output of running the submission script,
% must be of type char
%
% Outputs:
% out - scheduler independent format for the concrete scheduler
......
......@@ -71,12 +71,11 @@ if ~issshfm
tjob.remote_log);
system_cmd_or_error(cmdstr, 'Failed to copy to remote');
else
% TODO(brad) this is broken
sftpfrommatlab(cc.remote_user_name, ...
cc.remote_exec_host, ...
sshfm_opts.password, ...
ziplocal, ...
tjob.remote_log)
ssh2_conn = ssh2_config(cc.remote_exec_host, cc.remote_user_name, ...
sshfm_opts.password);
[localpath, localname, localext ] = fileparts(ziplocal);
ssh2_conn = scp_put(ssh2_conn, [localname localext], ...
tjob.remote_log, localpath);
end
......@@ -98,12 +97,13 @@ if ~issshfm
sys_str = sprintf('ssh %s ''%s''', sshhost, cmd_str);
result_str = system_cmd_or_error(sys_str, 'Failed to submit script');
else
conn = sshfrommatlab(cc.remote_user_name, ...
cc.remote_exec_host, ...
sshfm_opts.password);
[conn, result_str] = sshfrommatlabissue(conn, unzipcom);
sshfrommatlabclose(conn);
% TODO(brad) this is broken
ssh2_conn = ssh2_command(ssh2_conn, cmd_str);
result_str = ssh2_conn.command_result;
% result_str is cell array, we must change it to one string
result_str = sprintf('%s\n', result_str{:});
% remove last newline
result_str = result_str(1:end-1);
ssh2_close(ssh2_conn);
end
%% COLLECT THE NEW JOBIDS FROM THE SCHEDULER...
......
......@@ -61,7 +61,7 @@ catch %if nonexistent in workspace
context_config = init_context_config;
end
rsub_meth_cell = {'system', 'sshfrommatlab', 'none'};
rsub_meth_cell = {'system', 'ssh2', 'none'};
%% HANDLE batch_config...
PropGridStr_batchconfig = ...
......@@ -317,14 +317,13 @@ for hi=1:length(htb_fname)
switch rsub_meth
case 'system'
job_struct = rsub_sys(job_struct, driver);
case 'sshfrommatlab'
case 'ssh2'
disp('submitting jobs using sshfrommatlab...')
if isempty(password_sshfm)
password_sshfm = logindlg( ...
'Title', sprintf('Password for %s on %s', ...
password_sshfm = passwordEntryDialog(...
'WindowName', sprintf('Password for %s on %s', ...
context_config.remote_user_name, ...
context_config.remote_exec_host), ...
'Password', 'only');
context_config.remote_exec_host));
end
sshfm_opts.password = password_sshfm;
job_struct = rsub_sys(job_struct, driver, sshfm_opts);
......
Copyright (c) 2010, Jeremy Smith
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
% LOGINDLG Dialog for visually secure login.
% Examples:
% [login password] = logindlg('Title','Login Title'); % Returns the login and
% password with the dialog title 'Login Title'.
%
% password = logindlg; % Returns only the password with a default
% dialog title.
%
% password = logindlg('Password','only'); % Displays only a password
% edit box and returns the password.
%
% password = logindlg('Title','Login Title','Password','only');
% % Creates a password only dialog with 'Login Title' as the
% dialog title.
%
%
%
%
% Author: Jeremy Smith
% Date: September 24, 2005
% Last Edit: January 28, 2010
% Version: 1.3
% Tested on: Matlab 7.0.4.365 (R14) Service Pack 2, Matlab 7.1 SP 3, and
% Matlab 7.4.0.287 (R2007a)
% Description: custom login dialog because Matlab doesn't have an option
% for characters in an edit field to be replaced by asterisks
% (password security).
%
% Note: On very slow computers the first few password characters may
% have a delay before they are converted to asterisks.
% Changelist:
% 1.4: - Brad Kennedy fixed this garbage
% 1.3: -Pressing the Escape key with focus anywhere on the dialog will
% now cancel the dialog box
% -Typo correction
% -Escape no longer creates an empty character in the password field
% 1.2: -Tab no longer triggers the OK button in the password box
% -Improved the script help
% -Removed horizontal alignment from buttons
% -Added the option to display only the password box
% 1.1: -Added positioning code so it'll display in the center of the screen
% -If only one output is specified the password will be returned
% instead of the login as in Version 1.0
% -Escape will not only close the dialog if neither edit box is active
% -When the dialog appears the first edit box will be active
% -Added a few more comments
% -Removed the clc, it was left in by mistake in Version 1.0
function [varargout]=logindlg(varargin)
% Number of inputs check
if nargin == 0 || nargin == 2 || nargin == 4
else
error('Incorrect number of input arguments.')
end
% Input Type Check
for i=1:1:length(varargin)
if ~ischar(varargin{i})
error('Inputs must be strings.')
end
end
% Title Option
if nargin == 0
Title = 'Login';
elseif nargin == 2 && ~isempty(strmatch('title',lower(varargin)))
Title = varargin{2};
elseif nargin == 2 && isempty(strmatch('title',lower(varargin)))
Title = 'Login';
elseif nargin == 4 && ~isempty(strmatch('title',lower(varargin)))
S = strmatch('title',lower(varargin));
if S == 1
Title = varargin{2};
elseif S == 3
Title = varargin{4};
else
error('Invalid title.')
end
else
error('Invalid title.')
end
% Password Option
if nargin == 0
Pass = 0;
elseif nargin == 2 && ~isempty(strmatch('password',lower(varargin{1}))) && ~isempty(strmatch('only',lower(varargin{2})))
Pass = 1;
elseif nargin == 4 && ~isempty(strmatch('password',lower(varargin))) && ~isempty(strmatch('only',lower(varargin)))
P = strcmpi('password',varargin);
O = strcmpi('only',varargin);
if find(P, 1) == 1 && find(O, 1) == 2
Pass = 1;
elseif find(P, 1) == 3 && find(O, 1) == 4
Pass = 1;
end
elseif nargin == 2 && isempty(strmatch('password',lower(varargin))) == 1
Pass = 0;
else
error('Invalid password option.')
end
% Output Error Check
if nargout > 1 && Pass == 1 || nargout > 2
error('Too many output arguments.')
end
% Get Properties
Color = get(0,'DefaultUicontrolBackgroundcolor');
% Determine the size and position of the login interface
if Pass == 0
Height = 9.5;
else
Height = 5.5;
end
set(0,'Units','characters')
Screen = get(0,'screensize');
Position = [Screen(3)/2-17.5 Screen(4)/2-4.75 35 Height];
set(0,'Units','pixels')
% Create the GUI
gui.main = dialog('HandleVisibility','on',...
'IntegerHandle','off',...
'Menubar','none',...
'NumberTitle','off',...
'Name','Login',...
'Tag','logindlg',...
'Color',Color,...
'Units','characters',...
'Userdata','logindlg',...
'Position',Position);
% Set the title
if ischar(Title) == 1
set(gui.main,'Name',Title,'Closerequestfcn',{@Cancel,gui.main},'Keypressfcn',{@Escape})
end
% Texts
if Pass == 0
gui.login_text = uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left','Units','characters','String','Login','Position',[1 7.65 20 1]);
end
gui.password_text = uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left','Units','characters','String','Password','Position',[1 4.15 20 1]);
% Edits
if Pass == 0
gui.edit1 = uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 6.02 33 1.7],'KeyPressfcn',{@Escape});
end
gui.edit2 = uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left','BackgroundColor','white','Units','characters','String','','Position',[1 2.52 33 1.7],'KeyPressfcn',{@KeyPress_Function,gui.main},'Userdata','');
% Buttons
gui.OK = uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','String','OK','Position',[12 .2 10 1.7],'Callback',{@OK,gui.main},'KeyPressfcn',{@Escape});
gui.Cancel = uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','String','Cancel','Position',[23 .2 10 1.7],'Callback',{@Cancel,gui.main},'KeyPressfcn',{@Escape});
setappdata(0,'logindlg',gui) % Save handle data
setappdata(gui.main,'Check',0) % Error check setup. If Check remains 0 an empty cell array will be returned
if Pass == 0
uicontrol(gui.edit1) % Make the first edit box active
else
uicontrol(gui.edit2) % Make the second edit box active if the first isn't present
end
% Pause the GUI and wait for a button to be pressed
uiwait(gui.main)
Check = getappdata(gui.main,'Check'); % Check to see if a button was pressed
% Format output
if Check == 1
if Pass == 0
Login = get(gui.edit1,'String');
end
Password = get(gui.edit2,'Userdata');
if nargout == 1 % If only one output specified output Password
varargout(1) = {Password};
elseif nargout == 2 % If two outputs specified output both Login and Password
varargout(1) = {Login};
varargout(2) = {Password};
end
else % If OK wasn't pressed output nothing
if nargout == 1
varargout(1) = {[]};
elseif nargout == 2
varargout(1) = {[]};
varargout(2) = {[]};
end
end
delete(gui.main) % Close the GUI
setappdata(0,'logindlg',[]) % Erase handles from memory
%% Hide Password
function KeyPress_Function(h,eventdata,fig)
% Function to replace all characters in the password edit box with
% asterisks
password = get(h,'Userdata');
key = get(fig,'currentkey');
switch key
case 'backspace'
password = password(1:end-1); % Delete the last character in the password
case 'return' % This cannot be done through callback without making tab to the same thing
gui = getappdata(0,'logindlg');
OK([],[],gui.main);
case 'tab' % Avoid tab triggering the OK button
gui = getappdata(0,'logindlg');
uicontrol(gui.OK);
case 'escape'
% Close the login dialog
Escape(fig,[])
otherwise
password = [password get(fig,'currentcharacter')]; % Add the typed character to the password
end
SizePass = size(password); % Find the number of asterisks
if SizePass(2) > 0
asterisk(1,1:SizePass(2)) = '*'; % Create a string of asterisks the same size as the password
set(h,'String',asterisk) % Set the text in the password edit box to the asterisk string
else
set(h,'String','')
end
set(h,'Userdata',password) % Store the password in its current state
%% Cancel
function Cancel(h,eventdata,fig)
uiresume(fig)
%% OK
function OK(h,eventdata,fig)
% Set the check and resume
setappdata(fig,'Check',1)
uiresume(fig)
%% Escape
function Escape(h,eventdata)
% Close the login if the escape button is pushed and neither edit box is
% active
fig = h;
while ~strcmp(get(fig,'Type'),'figure')
fig = get(fig,'Parent');
end
key = get(fig,'currentkey');
if isempty(strfind(key,'escape')) == 0
Cancel([],[],fig)
end
\ No newline at end of file
function [Password, UserName] = passwordEntryDialog(varargin)
% PASSWORDENTRYDIALOG
% [Password, UserName] = passwordEntryDialog(varargin)
%
% Create a password entry dialog for entering a password that is visibly
% hidden. Java must be enabled for this function to work properly.
%
% It has only been tested on the Windows platform in R2008a. It should
% work in R2007a or later.
%
% The password box is created using the Java Swing component
% JPasswordField.
%
% Optional Input Arguments
% ------------------------
%
% 'enterUserName' DEFAULT: false
% Display the user name entry box. The user name entered must be at least
% one character or an error dialog is displayed.
%
% 'DefaultUserName' DEFAULT: ''
% String value of user name to populate in User Name box upon creation.
%
% 'ValidatePassword' DEFAULT: false
% Display dialog box to reenter password for validation purposes.
%
% 'CheckPasswordLength' DEFAULT: true
% Check the password length to ensure it meets the specified criteria.
%
% 'PasswordLengthMin' DEFAULT: 2
% Minimum password length allowed.
%
% 'PasswordLengthMax' DEFAULT: 8
% Maximum password length allowed.
%
% 'WindowName' DEFAULT: 'Login'
% Title of the password entry window.
%
% Examples
% --------
%
% Create a password dialog box with the default options.
% -----------------------------------------------------------------------
% [Password] = passwordEntryDialog;
%
% Create a user name and password entry dialog box without password
% verification.
% -----------------------------------------------------------------------
% [Password, UserName] = passwordEntryDialog('enterUserName', true)
%
% Create a user name and password entry dialog box without password
% verification. Set the user name to 'jdoe' upon startup.
% -----------------------------------------------------------------------
% [Password, UserName] = passwordEntryDialog('enterUserName', true,...
% 'DefaultUserName', 'jdoe')
%
% Create a password dialog box with password validation
% -----------------------------------------------------------------------
% [Password] = passwordEntryDialog('ValidatePassword', true);
%
% Create a user name and password entry dialog box with password
% verification.
% -----------------------------------------------------------------------
% [Password, UserName] = passwordEntryDialog('enterUserName', true,...
% 'ValidatePassword', true)
%
% Check the length of the password to be between 5 and 8 characters
% -----------------------------------------------------------------------
% [Password, UserName] = passwordEntryDialog('CheckPasswordLength', true,...
% 'PasswordLengthMin', 5,...
% 'PasswordLengthMax', 8)
%
% -----------------------------------------------------------------------
% Copyright (C) 2007-2008, Jesse B. Lai
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published
% by the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>
%% History
% The history of this program is outlined in this section.
%
% 20080616 - JBL - 0.0.2
% Started 20080612
%
% Updated to remove requirement for uicomponent function written by Yair
% Altman. Now, the Java components are created manually and the
% undocumented feature javacomponent is used.
%
% A focus issue was worked out by using the drawnow command in a couple of
% places to allow the objects to be focused properly upon startup.
%
% 20080427 - JBL - 0.0.1
% Started 20080425.
%
% Initial version. Uses Java to create the password entry box. An edit
% box was initially attemped with the Java frame, but was occasionally
% getting Java exceptions when trying to query the 'SelectionStart' and
% 'SelectionEnd' properties.
%
% Basic options of entering user name and password with options for
% password validation.
%
% ToDo: Maybe add valid string input argument to only allow certain
% characters.
%% Program Information
% ProgramName = 'passwordEntryDialog';
% ProgramVersion = '0.0.2';
% svnRevision = '$Revision: 184 $';
% svnRevision = getSVNRevision(svnRevision);
% ProgramVersion = [ProgramVersion, '.' ,svnRevision];
%
% LastChangedDate = '$LastChangedDate: 2008-06-16 09:08:17 -0600 (Mon, 16 Jun 2008) $';
% ProgramDate = getSVNDate(LastChangedDate);
%% Check for Existance of Java
if ~usejava('swing')
error('passwordEntryDialog: Java is required for this program to run.');
end
%% Parse Input Arguments
% Input arguments are parsed with the MATLAB inputParser class.
% Create input parser object
ProgramOptionsParser = inputParser;
ProgramOptionsParser.KeepUnmatched = true;
ProgramOptionsParser.addParamValue('enterUserName', false, @(x) islogical(x) || isnumeric(x));
ProgramOptionsParser.addParamValue('DefaultUserName', '', @ischar);
ProgramOptionsParser.addParamValue('ValidatePassword', false, @(x) islogical(x) || isnumeric(x));
ProgramOptionsParser.addParamValue('CheckPasswordLength', true, @(x) islogical(x) || isnumeric(x));
ProgramOptionsParser.addParamValue('PasswordLengthMin', 2, @isnumeric);
ProgramOptionsParser.addParamValue('PasswordLengthMax', 8, @isnumeric);
ProgramOptionsParser.addParamValue('WindowName', 'Login', @ischar);
% Parse Input Arguments
try
ProgramOptionsParser.parse(varargin{:});
catch Error
ProgramOptionsParser.parse;
if strcmpi(Error.identifier, 'MATLAB:InputParser:ArgumentFailedValidation')
error(Error.identifier, Error.message);
end;
end;
ProgramOptions = ProgramOptionsParser.Results;
% Validate password length options
if ProgramOptions.CheckPasswordLength
if ProgramOptions.PasswordLengthMax < ProgramOptions.PasswordLengthMin
error('MATLAB:InputParser:ArgumentFailedValidation', 'PasswordLengthMax must be greater than PasswordLengthMin');
end;
end;
%% Determine GUI Size and Position
% Center the GUI on the screen.
set(0,'Units','pixels')
Screen = get(0,'screensize');
PositionGUI = [0 0 300 50];
if ProgramOptions.enterUserName
PositionGUI = PositionGUI + [0 0 0 50];
end;
if ProgramOptions.ValidatePassword
PositionGUI = PositionGUI + [0 0 0 50];
OffsetBottom = 43;
else
OffsetBottom = 0;
end;
PositionGUI = [Screen(3:4)/2-PositionGUI(3:4)/2 PositionGUI(3:4)];
PositionLeft = 10;
BoxWidth = 200;
%% Create the GUI
BackgroundColor = get(0,'DefaultUicontrolBackgroundcolor');
handles.figure1 = figure('Menubar','none', ...
'Units','Pixels', ...
'Resize','off', ...
'NumberTitle','off', ...
'Name',ProgramOptions.WindowName, ...
'Position',PositionGUI, ...
'Color', BackgroundColor, ...
'WindowStyle','modal');
% Create Password Validation Entry Box
if ProgramOptions.ValidatePassword
handles.java_PasswordValidate = javax.swing.JPasswordField();
handles.java_PasswordValidate.setFocusable(true);
[handles.java_PasswordValidate, handles.edit_PasswordValidate] = javacomponent(handles.java_PasswordValidate, [], handles.figure1);
set(handles.edit_PasswordValidate, ...
'Parent', handles.figure1, ...
'Tag', 'edit_PasswordValidate', ...
'Units', 'Pixels', ...
'Position',[PositionLeft 10 BoxWidth 23]);
handles.text_LabelPasswordValidate = uicontrol('Parent',handles.figure1, ...
'Tag', 'text_LabelPassword', ...
'Style','Text', ...
'Units','Pixels',...
'Position',[PositionLeft 33 BoxWidth 16], ...
'FontSize',10, ...
'String','Reenter password:',...
'HorizontalAlignment', 'Left');
end;
% Create Password Entry Box
handles.java_Password = javax.swing.JPasswordField();
[handles.java_Password, handles.edit_Password] = javacomponent(handles.java_Password, [PositionLeft 10+OffsetBottom BoxWidth 23], handles.figure1);
handles.java_Password.setFocusable(true);
set(handles.edit_Password, ...
'Parent', handles.figure1, ...
'Tag', 'edit_Password', ...
'Units', 'Pixels', ...
'Position',[PositionLeft 10+OffsetBottom BoxWidth 23]);
drawnow; % This drawnow is required to allow the focus to work
handles.text_LabelPassword = uicontrol('Parent',handles.figure1, ...
'Tag', 'text_LabelPassword', ...
'Style','Text', ...
'Units','Pixels',...
'Position',[PositionLeft 33+OffsetBottom BoxWidth 16], ...
'FontSize',10, ...
'String','Password:',...
'HorizontalAlignment', 'Left');
% Create OK Pushbutton