Exemplo n.º 1
0
 //attach to an existing channel
 public SSH2Channel(SSH2Connection con, ChannelType type, int local_id, int remote_id, int maxpacketsize)
     : base(con, type, local_id)
 {
     _connection = con;
     _windowSize = _leftWindowSize = con.Param.WindowSize;
     Debug.Assert(type == ChannelType.ForwardedRemoteToLocal || type == ChannelType.AgentForward);
     _remoteID = remote_id;
     _serverMaxPacketSize = maxpacketsize;
     _negotiationStatus = NegotiationStatus.Ready;
 }
Exemplo n.º 2
0
 public SSH2Channel(SSH2Connection con, ChannelType type, int local_id, string command)
     : base(con, type, local_id)
 {
     _command = command;
     if (type == ChannelType.ExecCommand || type == ChannelType.Subsystem)
         Debug.Assert(command != null); //'command' is required for ChannelType.ExecCommand
     _connection = con;
     _windowSize = _leftWindowSize = con.Param.WindowSize;
     _negotiationStatus = NegotiationStatus.WaitingChannelConfirmation;
 }
Exemplo n.º 3
0
 internal CallbackSSH2PacketHandler(SSH2Connection con)
 {
     _connection = con;
 }
Exemplo n.º 4
0
 public KeyExchanger(SSH2Connection con, byte[] sessionID) {
     _connection = con;
     _param = con.Param;
     _cInfo = (SSH2ConnectionInfo)con.ConnectionInfo;
     _sessionID = sessionID;
     _status = Status.INITIAL;
 }
Exemplo n.º 5
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)
                con = new SSH1Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol));
            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;
            }
        }
Exemplo n.º 6
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);
            try {
                SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver();
                protoVerReceiver.Receive(s, 5000);
                protoVerReceiver.Verify(param.Protocol);

                SSHConnection con;
                if (param.Protocol == SSHProtocol.SSH1)
                    con = new SSH1Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol));
                else
                    con = new SSH2Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol));

                s.SetHandler(con.Packetizer);
                s.RepeatAsyncRead();
                con.SendMyVersion(param);

                if (con.Connect() == AuthenticationResult.Failure) {
                    s.Close();
                    return null;
                }

                return con;
            }
            catch (Exception) {
                s.Close();
                throw;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Establish a SSH connection
        /// </summary>
        /// <param name="socket">TCP socket which is already connected to the server.</param>
        /// <param name="param">SSH connection parameter</param>
        /// <param name="connectionEventHandlerCreator">a factory function to create a connection event handler (can be null)</param>
        /// <param name="protocolEventLoggerCreator">a factory function to create a protocol log event handler (can be null)</param>
        /// <returns>new connection object</returns>
        public static ISSHConnection Connect(
                    Socket socket,
                    SSHConnectionParameter param,
                    Func<ISSHConnection, ISSHConnectionEventHandler> connectionEventHandlerCreator = null,
                    Func<ISSHConnection, ISSHProtocolEventLogger> protocolEventLoggerCreator = null)
        {
            if (socket == null) {
                throw new ArgumentNullException("socket");
            }
            if (param == null) {
                throw new ArgumentNullException("param");
            }
            if (!socket.Connected) {
                throw new ArgumentException("socket is not connected to the remote host", "socket");
            }
            if (param.UserName == null) {
                throw new ArgumentException("UserName property is not set", "param");
            }
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) {
                throw new ArgumentException("Password property is not set", "param");
            }

            string clientVersion = SSHUtil.ClientVersionString(param.Protocol);

            PlainSocket psocket = new PlainSocket(socket, null);
            try {
                // receive protocol version string
                SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver();
                protoVerReceiver.Receive(psocket, 5000);
                // verify the version string
                protoVerReceiver.Verify(param.Protocol);

                ISSHConnection sshConnection;

                if (param.Protocol == SSHProtocol.SSH1) {
                    // create a connection object
                    var con = new SSH1Connection(
                                psocket,
                                param,
                                protoVerReceiver.ServerVersion,
                                clientVersion,
                                connectionEventHandlerCreator,
                                protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }
                else {
                    // create a connection object
                    var con = new SSH2Connection(
                                psocket,
                                param,
                                protoVerReceiver.ServerVersion,
                                clientVersion,
                                connectionEventHandlerCreator,
                                protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }

                return sshConnection;
            }
            catch (Exception) {
                psocket.Close();
                throw;
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Opens SFTP channel and creates a new instance.
 /// </summary>
 /// <param name="connection">SSH2 connection object</param>
 /// <returns>New instance.</returns>
 public static SFTPClient OpenSFTPChannel(SSH2Connection connection)
 {
     SFTPClientChannelEventReceiver channelReceiver = new SFTPClientChannelEventReceiver();
     SSHChannel channel = connection.OpenSubsystem(channelReceiver, "sftp");
     return new SFTPClient(channel, channelReceiver);
 }