Example #1
0
        protected bool _execCmdWaitFlag;  // wait response flag for sending exec command to server

        protected SSHConnection(SSHConnectionParameter param, AbstractGranadosSocket strm, ISSHConnectionEventReceiver receiver) {
            _param = (SSHConnectionParameter)param.Clone();
            _stream = strm;
            _eventReceiver = receiver;
            _channel_collection = new ChannelCollection();
            _autoDisconnect = true;
            _execCmd = null;
            _execCmdWaitFlag = true;
        }
Example #2
0
        private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, VersionExchangeHandler pnh, AbstractGranadosSocket s) {
            DataFragment data = pnh.WaitResponse();
            string sv = pnh.ServerVersion;

            SSHConnection con = null;
            if (param.Protocol == SSHProtocol.SSH1)
                throw new SSHException("SSH1 is not supported");
            else
                con = new SSH2Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));

            con.TraceReceptionEvent("server version-string", sv.Trim());
            pnh.Close();
            s.SetHandler(con.PacketBuilder);
            con.SendMyVersion(param);

            if (con.Connect() != AuthenticationResult.Failure) {
                return con;
            }
            else {
                s.Close();
                return null;
            }
        }
Example #3
0
 private void SendMyVersion(SSHConnectionParameter param) {
     string cv = SSHUtil.ClientVersionString(param.Protocol);
     string cv2 = cv + param.VersionEOL;
     byte[] data = Encoding.ASCII.GetBytes(cv2);
     _stream.Write(data, 0, data.Length);
     TraceTransmissionEvent("client version-string", cv);
 }
Example #4
0
        /**
         * open a new SSH connection via the .NET socket
         */
        public static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, Socket underlying_socket) {
            if (param.UserName == null)
                throw new InvalidOperationException("UserName property is not set");
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null)
                throw new InvalidOperationException("Password property is not set");

            PlainSocket s = new PlainSocket(underlying_socket, null);
            VersionExchangeHandler pnh = new VersionExchangeHandler(param, s);
            s.SetHandler(pnh);
            s.RepeatAsyncRead();
            return ConnectMain(param, receiver, pnh, s);
        }
Example #5
0
        /**
         * opens another SSH connection via port-forwarded connection
         */
        public SSHConnection OpenPortForwardedAnotherConnection(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, string host, int port) {
            ChannelSocket s = new ChannelSocket(null);
            SSHChannel ch = ForwardPort(s, host, port, "localhost", 0);
            s.SSHChennal = ch;
            VersionExchangeHandler pnh = new VersionExchangeHandler(param, s);
            s.SetHandler(pnh);

            return ConnectMain(param, receiver, pnh, s);
        }
Example #6
0
    /// <summary>
    /// Connect to remote host:port over SSH tunnel
    /// </summary>
    public void Connect(string host, int port, int timeout)
    {
      try
      {
        //remember remote target
        m_RemoteTarget = new IPEndPoint(ResolveHost(host), port);

        //connect to SSH server
        if (!connectAsync(m_RemoteTarget.Address, SSHServerPort).Wait(timeout))
          throw new ErlException(ERL_CONN_CANT_CONNECT_TO_HOST_ERROR.Args("SSH", host, port));

        //get password from user
        var pass                 = ErlTransportPasswordSource.GetPassword(this, NodeName, SSHUserName);

        //set params
        var param                = new SSHConnectionParameter();
        param.EventTracer        = this; //to receive detailed events
        param.UserName           = SSHUserName;
        param.Password           = pass;
        param.Protocol           = SSHProtocol.SSH2;
        param.AuthenticationType = (AuthenticationType)
                                   Enum.Parse(typeof (SSH.AuthenticationType), SSHAuthenticationType);

        if (param.AuthenticationType == AuthenticationType.PublicKey)
          param.IdentityFile = SSHPrivateKeyFilePath;

        //former algorithm is given priority in the algorithm negotiation
        param.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[]
                                            {PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA};
        param.PreferableCipherAlgorithms  = new CipherAlgorithm[]
        {
          CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES, CipherAlgorithm.AES192CTR,
          CipherAlgorithm.AES256CTR, CipherAlgorithm.AES128CTR
        };

        param.WindowSize            = 0x1000; //this option is ignored with SSH1

        //Creating a new SSH connection over the underlying socket
        m_Connection                = SSHConnection.Connect(param, this, m_Client);
        m_Connection.AutoDisconnect = true;
        m_IsChannelReady            = 0;

        //Local->Remote port forwarding (we use localhost:0 as local port, because local port is not required for us, we will use this tunnel directly)
        m_Channel = m_Connection.ForwardPort(this, host, port, "localhost", 0);
        var deadLine = DateTime.Now.AddMilliseconds(timeout);
        while (m_IsChannelReady == 0 && deadLine > DateTime.Now)
          System.Threading.Thread.Sleep(50); //wait response

        //if timeouted - throw exception
        if (m_IsChannelReady <= 0)
          throw new ErlException(ERL_CREATE_SSH_TUNNEL_ERROR);

        //create network stream
        m_Stream = new SshTunnelStream(m_Channel);

        //Remote->Local
        // if you want to listen to a port on the SSH server, follow this line:
        //_conn.ListenForwardedPort("0.0.0.0", 10000);

        //NOTE: if you use SSH2, dynamic key exchange feature is supported.
        //((SSH2Connection)_conn).ReexchangeKeys();
      }
      catch (Exception ex)
      {
        OnTrace(ErlTraceLevel.Ctrl, Direction.Inbound, ex.Message);
        throw;
      }
    }
Example #7
0
 public object Clone()
 {
   var n = new SSHConnectionParameter
   {
     AuthenticationType          = AuthenticationType,
     PreferableCipherAlgorithms  = PreferableCipherAlgorithms,
     TerminalHeight              = TerminalHeight,
     PreferableHostKeyAlgorithms = PreferableHostKeyAlgorithms,
     IdentityFile                = IdentityFile,
     KeyCheck                    = KeyCheck,
     MaxPacketSize               = MaxPacketSize,
     Password                    = Password,
     _protocol                   = Protocol,
     TerminalName                = TerminalName,
     UserName                    = UserName,
     TerminalWidth               = TerminalWidth,
     WindowSize                  = WindowSize,
     CheckMACError               = CheckMACError,
     EventTracer                 = EventTracer,
     AgentForward                = AgentForward
   };
   return n;
 }
 public object Clone() {
     SSHConnectionParameter n = new SSHConnectionParameter();
     n._authtype = _authtype;
     n._cipherAlgorithms = _cipherAlgorithms;
     n._height = _height;
     n._hostkeyAlgorithms = _hostkeyAlgorithms;
     n._identityFile = _identityFile;
     n._keycheck = _keycheck;
     n._maxpacketsize = _maxpacketsize;
     n._password = _password;
     n._protocol = _protocol;
     n._terminalname = _terminalname;
     n._username = _username;
     n._width = _width;
     n._windowsize = _windowsize;
     n._checkMACError = _checkMACError;
     n._tracer = _tracer;
     n._agentForward = _agentForward;
     return n;
 }