public void Deserialize(SSHLoginParameter tp, StructuredText node)
        {
            base.Deserialize(tp, node);
            tp.Method             = "SSH1".Equals(node.Get("method")) ? SSHProtocol.SSH1 : SSHProtocol.SSH2;
            tp.AuthenticationType = ParseUtil.ParseEnum <AuthenticationType>(node.Get("authentication", ""), AuthenticationType.Password);
            tp.Account            = node.Get("account", "");
            tp.IdentityFileName   = node.Get("identityFileName", "");
            if (ProtocolsPlugin.Instance.ProtocolOptions.ReadSerializedPassword)
            {
                string pw = node.Get("passphrase", null);
                if (pw != null)
                {
                    tp.PasswordOrPassphrase = pw;
                    tp.LetUserInputPassword = false;
                }
                else
                {
                    pw = node.Get("password", null);
                    if (pw != null)
                    {
                        pw = new SimpleStringEncrypt().DecryptString(pw);
                        if (pw != null)
                        {
                            tp.PasswordOrPassphrase = pw;
                            tp.LetUserInputPassword = false;
                        }
                    }
                }
            }

            tp.EnableAgentForwarding = GetBoolValue(node, "enableAgentForwarding", false);

            tp.EnableX11Forwarding = GetBoolValue(node, "enableX11Forwarding", false);

            StructuredText x11Node = node.FindChild("x11Forwarding");

            if (x11Node != null)
            {
                int display = GetIntValue(x11Node, "display", 0);
                X11ForwardingParams x11params = new X11ForwardingParams(display);
                x11params.Screen                    = GetIntValue(x11Node, "screen", 0);
                x11params.NeedAuth                  = GetBoolValue(x11Node, "needAuth", false);
                x11params.XauthorityFile            = x11Node.Get("xauthorityFile", null);
                x11params.UseCygwinUnixDomainSocket = GetBoolValue(x11Node, "useCygwinUnixDomainSocket", false);
                x11params.X11UnixFolder             = x11Node.Get("x11UnixFolder", null);
                tp.X11Forwarding                    = x11params;
            }
        }
Exemplo n.º 2
0
        private string FindXauthorityFile(X11ForwardingParams param)
        {
            string xauthPath;

            if (!String.IsNullOrEmpty(param.XauthorityFile))
            {
                xauthPath = param.XauthorityFile;
            }
            else
            {
                var home = Environment.GetEnvironmentVariable("HOME");
                if (home == null)
                {
                    return(null);
                }
                xauthPath = Path.Combine(home, ".Xauthority");
            }

            return(File.Exists(xauthPath) ? xauthPath : null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates input values and constructs parameter objects.
        /// </summary>
        /// <param name="loginParam">SSH parameter object is set when this method returns true.</param>
        /// <param name="terminalSettings">terminal settings object is set when this method returns true.</param>
        /// <param name="errorMessage">validation error message is set when this method returns false. this can be null when displaying error message is not needed.</param>
        /// <returns>true if all validations passed and parameter objects were created.</returns>
        private bool Validate(out ISSHLoginParameter loginParam, out ITerminalSettings terminalSettings, out string errorMessage)
        {
            loginParam       = null;
            terminalSettings = null;
            errorMessage     = null;

            var ssh = TerminalSessionsPlugin.Instance.ProtocolService.CreateDefaultSSHParameter();
            var tcp = (ITCPParameter)ssh.GetAdapter(typeof(ITCPParameter));

            //--- SSH connection settings

            if (_ssh1RadioButton.Checked)
            {
                ssh.Method = SSHProtocol.SSH1;
            }
            else if (_ssh2RadioButton.Checked)
            {
                ssh.Method = SSHProtocol.SSH2;
            }
            else
            {
                errorMessage = TEnv.Strings.GetString("Message.LoginDialog.ProtocolVersionIsNotSpecified");
                return(false);
            }

            tcp.Destination = _hostBox.Text;
            if (String.IsNullOrEmpty(tcp.Destination))
            {
                errorMessage = TEnv.Strings.GetString("Message.LoginDialog.HostIsEmpty");
                return(false);
            }

            int port;

            if (Int32.TryParse(_portBox.Text, out port) && port >= 0 && port <= 65535)
            {
                tcp.Port = port;
            }
            else
            {
                errorMessage = String.Format(
                    TEnv.Strings.GetString("Message.LoginDialog.InvalidPort"),
                    _portBox.Text);
                return(false);
            }

            ssh.Account = _userNameBox.Text;

            AuthType authType = ((EnumListItem <AuthType>)_authOptions.SelectedItem).Value;

            ssh.AuthenticationType = authType.ToAuthenticationType();

            if (ssh.AuthenticationType == AuthenticationType.PublicKey)
            {
                ssh.IdentityFileName = _privateKeyFile.Text;

                if (String.IsNullOrEmpty(ssh.IdentityFileName))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.PrivateKeyFileIsNotSpecified");
                    return(false);
                }

                if (!File.Exists(ssh.IdentityFileName))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.KeyFileNotExist");
                    return(false);
                }
            }

            if (ssh.AuthenticationType == AuthenticationType.Password || ssh.AuthenticationType == AuthenticationType.PublicKey)
            {
                ssh.PasswordOrPassphrase = _passphraseBox.Text;
            }

            //--- Log settings

            ISimpleLogSettings logSettings = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultSimpleLogSettings();

            logSettings.LogType = ((EnumListItem <LogType>)_logTypeBox.SelectedItem).Value;
            if (logSettings.LogType != LogType.None)
            {
                logSettings.LogPath = _logFileBox.Text;
                LogFileCheckResult r = LogUtil.CheckLogFileName(logSettings.LogPath, this.ParentForm);
                if (r == LogFileCheckResult.Cancel || r == LogFileCheckResult.Error)
                {
                    errorMessage = null;
                    return(false);
                }
                logSettings.LogAppend = (r == LogFileCheckResult.Append);
            }

            //--- Terminal settings

            ITerminalParameter termParam    = (ITerminalParameter)tcp.GetAdapter(typeof(ITerminalParameter));
            TerminalType       terminalType = ((EnumListItem <TerminalType>)_terminalTypeBox.SelectedItem).Value;

            termParam.SetTerminalName(terminalType.ToTermValue());

            string            terminalCaption = tcp.Destination;
            Image             terminalIcon    = Poderosa.TerminalSession.Properties.Resources.NewConnection16x16;
            ITerminalSettings termSettings    = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultTerminalSettings(terminalCaption, terminalIcon);

            termSettings.BeginUpdate();
            termSettings.Encoding     = ((EnumListItem <EncodingType>)_encodingBox.SelectedItem).Value;
            termSettings.LocalEcho    = ((ListItem <bool>)_localEchoBox.SelectedItem).Value;
            termSettings.TransmitNL   = ((EnumListItem <NewLine>)_newLineBox.SelectedItem).Value;
            termSettings.TerminalType = terminalType;
            termSettings.LogSettings.Reset(logSettings);
            termSettings.EndUpdate();

            //--- X11 forwarding settings

            if (_useX11ForwardingCheckBox.Checked)
            {
                if (String.IsNullOrEmpty(_x11DisplayText.Text))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11DisplayIsNotEntered");
                    return(false);
                }
                int display;
                if (!Int32.TryParse(_x11DisplayText.Text, out display) || display < 0 || display > (65535 - 6000))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.InvalidX11Display");
                    return(false);
                }

                X11ForwardingParams x11Param = new X11ForwardingParams(display);

                if (String.IsNullOrEmpty(_x11ScreenText.Text))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11ScreenIsNotEntered");
                    return(false);
                }
                int screen;
                if (!Int32.TryParse(_x11ScreenText.Text, out screen) || screen < 0)
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.InvalidX11Screen");
                    return(false);
                }
                x11Param.Screen = screen;

                if (_x11NeedAuthCheckBox.Checked)
                {
                    x11Param.NeedAuth       = true;
                    x11Param.XauthorityFile = _x11XauthorityText.Text;
                    if (String.IsNullOrEmpty(x11Param.XauthorityFile))
                    {
                        errorMessage = TEnv.Strings.GetString("Message.LoginDialog.XauthorityFileIsNotSpecified");
                        return(false);
                    }
                }
                else
                {
                    x11Param.NeedAuth = false;
                }

                if (_x11UseCygwinDomainSocketCheckBox.Checked)
                {
                    x11Param.UseCygwinUnixDomainSocket = true;
                    x11Param.X11UnixFolder             = _x11CygwinX11UnixFolderText.Text;
                    if (String.IsNullOrEmpty(x11Param.X11UnixFolder))
                    {
                        errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11UnixFolderIsNotSpecified");
                        return(false);
                    }
                }

                ssh.EnableX11Forwarding = true;
                ssh.X11Forwarding       = x11Param;
            }
            else
            {
                ssh.EnableX11Forwarding = false;
                ssh.X11Forwarding       = null;
            }

            //--- Agent forwarding settings

            if (_useAgentForwardingCheckBox.Checked)
            {
                ssh.EnableAgentForwarding          = true;
                ssh.AgentForwardingAuthKeyProvider = null;  // set later
            }
            else
            {
                ssh.EnableAgentForwarding          = false;
                ssh.AgentForwardingAuthKeyProvider = null;
            }

            //--- Macro

            IAutoExecMacroParameter autoExecParams = tcp.GetAdapter(typeof(IAutoExecMacroParameter)) as IAutoExecMacroParameter;

            if (autoExecParams != null)     // macro plugin is enabled
            {
                if (!String.IsNullOrEmpty(_autoExecMacroPathBox.Text))
                {
                    autoExecParams.AutoExecMacroPath = _autoExecMacroPathBox.Text;
                }
                else
                {
                    autoExecParams.AutoExecMacroPath = null;
                }
            }

            loginParam       = ssh;
            terminalSettings = termSettings;
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Setup the connection manager according to the parameters.
        /// </summary>
        /// <param name="param">parameters</param>
        /// <exception cref="X11UtilException"></exception>
        /// <exception cref="X11SocketException"></exception>
        public void Setup(X11ForwardingParams param)
        {
            if (_setupDone)
            {
                return;
            }

            _param = param.Clone();

            _spoofedAuthProtocolName = DEFAULT_AUTH_NAME;
            _spoofedAuthCookie       = GenerateCookie();

            if (param.UseCygwinUnixDomainSocket)
            {
                _protocolEventManager.Trace("[X11] Use Cygwin's domain socket");
                _socketFactory = () => new X11CygwinDomainSocket(param.X11UnixFolder);
            }
            else
            {
                _protocolEventManager.Trace("[X11] Use TCP socket");
                _socketFactory = () => new X11TcpSocket();
            }

            XauthorityEntry xauthEntry;

            if (param.NeedAuth)
            {
                string xauthFile = FindXauthorityFile(param);
                if (xauthFile == null)
                {
                    throw new X11UtilException(Strings.GetString("XauthorityFileNotFound"));
                }
                var parser = new XauthorityParser();
                xauthEntry = parser.FindBest(xauthFile, param.Display);
                if (xauthEntry == null)
                {
                    throw new X11UtilException(Strings.GetString("SuitableAuthorizationInformationNotFound"));
                }
            }
            else
            {
                xauthEntry = new XauthorityEntry(0, "", param.Display, "", new byte[0]);
            }

            var cookieInfo = GetUntrustedAccessCookie(_socketFactory, param.Display, xauthEntry);

            if (cookieInfo == null)
            {
                // no SECURITY extension
                _protocolEventManager.Trace("[X11] \"Trusted\" access will be used.");
                _xAuthProtocolName = xauthEntry.Name;
                _xAuthCookie       = xauthEntry.Data;
                _authEntry         = xauthEntry;
                _authId            = null;
                _setupDone         = true;
                return;
            }

            _protocolEventManager.Trace("[X11] \"Untrusted\" access will be used.");
            _xAuthProtocolName = DEFAULT_AUTH_NAME;
            _xAuthCookie       = cookieInfo.Item2;
            _authEntry         = xauthEntry;
            _authId            = cookieInfo.Item1;
            _setupDone         = true;
            // TODO:
            // the authorization cookie should be deleted from the X server when
            // the forwarding channel is closed.
        }