Пример #1
0
        public SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
        {
            _channel_entry = new TelnetChannel(this, socket, receiver);
            TelnetPacketBuilder pb = new TelnetPacketBuilder(receiver);
            PlainSocket         ps = new PlainSocket(socket, pb);

            ps.RepeatAsyncRead();
            return(_channel_entry);
        }
Пример #2
0
 public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
 {
     if (_shellID != -1)
     {
         throw new SSHException("A shell is opened already");
     }
     _shellID = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID;
     SendRequestPTY();
     _executingShell = true;
     return(new SSH1Channel(this, ChannelType.Shell, _shellID));
 }
Пример #3
0
 public override void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel)
 {
     try {
         Channel ch = (Channel)receiver;
         ch.FixChannel(channel);
         ch.OnChannelReady();
         ch.StartAsyncReceive();
     }
     catch (Exception ex) {
         Debug.WriteLine(ex.StackTrace);
         Util.InterThreadWarning(ex.Message);
     }
 }
Пример #4
0
        public Entry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r) {
            lock (this) {
                Entry e = new Entry(ch, r, _channel_sequence++);

                if (_first == null)
                    _first = e;
                else {
                    e.Next = _first;
                    _first = e;
                }

                _count++;
                return e;
            }
        }
        public Entry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r)
        {
            lock (this) {
                Entry e = new Entry(ch, r, _channel_sequence++);

                if (_first == null)
                {
                    _first = e;
                }
                else
                {
                    e.Next = _first;
                    _first = e;
                }

                _count++;
                return(e);
            }
        }
Пример #6
0
        protected ChannelEntry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r)
        {
            lock (this) {
                ChannelEntry e = new ChannelEntry();
                e._channel  = ch;
                e._receiver = r;
                e._localID  = _channel_sequence++;

                for (int i = 0; i < _channel_entries.Count; i++)
                {
                    if (_channel_entries[i] == null)
                    {
                        _channel_entries[i] = e;
                        return(e);
                    }
                }
                _channel_entries.Add(e);
                return(e);
            }
        }
        public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            if (_shellID == -1)
            {
                ExecShell();
                _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID;
            }

            int local_id = this.RegisterChannelEventReceiver(null, receiver)._localID;

            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(local_id);             //channel id is fixed to 0
            writer.Write(remote_host);
            writer.Write(remote_port);
            //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified
            //writer.Write(originator_host);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_PORT_OPEN, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);

            return(new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id));
        }
Пример #8
0
        internal void ProcessPacket(ISSHChannelEventReceiver receiver, PacketType pt, int data_length, SSH2DataReader re)
        {
            //NOTE: the offset of 're' is next to 'receipiant channel' field
            _leftWindowSize -= data_length;
            while(_leftWindowSize <= _windowSize) {
                SSH2DataWriter adj = new SSH2DataWriter();
                adj.WritePacketType(PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST);
                adj.Write(_remoteID);
                adj.Write(_windowSize);
                TransmitPacket(adj.ToByteArray());
                _leftWindowSize += _windowSize;
                //Debug.WriteLine("Window size is adjusted to " + _leftWindowSize);
            }

            if(pt==PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST) {
                int w = re.ReadInt32();
                //Debug.WriteLine(String.Format("Window Adjust +={0}",w));
            }
            else if(_negotiationStatus!=0) { //when the negotiation is not completed
                if(_type==ChannelType.Shell)
                    OpenShell(receiver, pt, re);
                else if(_type==ChannelType.ForwardedLocalToRemote)
                    ReceivePortForwardingResponse(receiver, pt, re);
                else if(_type==ChannelType.Session)
                    EstablishSession(receiver, pt, re);
            }
            else {
                switch(pt) {
                    case PacketType.SSH_MSG_CHANNEL_DATA: {
                        int len = re.ReadInt32();
                        receiver.OnData(re.Image, re.Offset, len);
                    }
                        break;
                    case PacketType.SSH_MSG_CHANNEL_EXTENDED_DATA: {
                        int t = re.ReadInt32();
                        byte[] data = re.ReadString();
                        receiver.OnExtendedData(t, data);
                    }
                        break;
                    case PacketType.SSH_MSG_CHANNEL_REQUEST: {
                        string request = Encoding.ASCII.GetString(re.ReadString());
                        bool reply = re.ReadBool();
                        if(request=="exit-status") {
                            int status = re.ReadInt32();
                        }
                        else if(reply) { //we reject unknown requests including keep-alive check
                            SSH2DataWriter wr = new SSH2DataWriter();
                            wr.Write((byte)PacketType.SSH_MSG_CHANNEL_FAILURE);
                            wr.Write(_remoteID);
                            TransmitPacket(wr.ToByteArray());
                        }
                    }
                        break;
                    case PacketType.SSH_MSG_CHANNEL_EOF:
                        receiver.OnChannelEOF();
                        break;
                    case PacketType.SSH_MSG_CHANNEL_CLOSE:
                        _connection.UnregisterChannelEventReceiver(_localID);
                        receiver.OnChannelClosed();
                        break;
                    case PacketType.SSH_MSG_CHANNEL_FAILURE:
                    case PacketType.SSH_MSG_CHANNEL_SUCCESS:
                        receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest);
                        break;
                    default:
                        receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest);
                        Debug.WriteLine("Unknown Packet "+pt);
                        break;
                }

            }
        }
Пример #9
0
 public Entry(SSHChannel ch, ISSHChannelEventReceiver r, int seq) {
     _channel = ch;
     _receiver = r;
     _localID = seq;
 }
Пример #10
0
        protected ChannelEntry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r)
        {
            lock(this) {
                ChannelEntry e = new ChannelEntry();
                e._channel = ch;
                e._receiver = r;
                e._localID = _channel_sequence++;

                for(int i=0; i<_channel_entries.Count; i++) {
                    if(_channel_entries[i]==null) {
                        _channel_entries[i] = e;
                        return e;
                    }
                }
                _channel_entries.Add(e);
                return e;
            }
        }
Пример #11
0
 /**
  * forwards the remote end to another host
  */
 public abstract SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port);
Пример #12
0
 public virtual void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel)
 {
 }
Пример #13
0
 public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
 {
     if (_shellID != -1)
         throw new SSHException("A shell is opened already");
     _shellID = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID;
     SendRequestPTY();
     _executingShell = true;
     return new SSH1Channel(this, ChannelType.Shell, _shellID);
 }
 public Entry(SSHChannel ch, ISSHChannelEventReceiver r, int seq)
 {
     _channel  = ch;
     _receiver = r;
     _localID  = seq;
 }
Пример #15
0
 public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) {
     return DoExecCommandInternal(receiver, ChannelType.Shell, null, "opening shell");
 }
Пример #16
0
 public abstract SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command);
        internal void AsyncReceivePacket(SSH1Packet p)
        {
            try {
                int len = 0, channel = 0;
                switch (p.Type)
                {
                case PacketType.SSH_SMSG_STDOUT_DATA:
                    len = SSHUtil.ReadInt32(p.Data, 0);
                    FindChannelEntry(_shellID)._receiver.OnData(p.Data, 4, len);
                    break;

                case PacketType.SSH_SMSG_STDERR_DATA: {
                    SSH1DataReader re = new SSH1DataReader(p.Data);
                    FindChannelEntry(_shellID)._receiver.OnExtendedData((int)PacketType.SSH_SMSG_STDERR_DATA, re.ReadString());
                }
                break;

                case PacketType.SSH_MSG_CHANNEL_DATA:
                    channel = SSHUtil.ReadInt32(p.Data, 0);
                    len     = SSHUtil.ReadInt32(p.Data, 4);
                    FindChannelEntry(channel)._receiver.OnData(p.Data, 8, len);
                    break;

                case PacketType.SSH_MSG_PORT_OPEN:
                    this.ProcessPortforwardingRequest(_eventReceiver, p);
                    break;

                case PacketType.SSH_MSG_CHANNEL_CLOSE: {
                    channel = SSHUtil.ReadInt32(p.Data, 0);
                    ISSHChannelEventReceiver r = FindChannelEntry(channel)._receiver;
                    UnregisterChannelEventReceiver(channel);
                    r.OnChannelClosed();
                }
                break;

                case PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION:
                    channel = SSHUtil.ReadInt32(p.Data, 0);
                    break;

                case PacketType.SSH_MSG_DISCONNECT:
                    _eventReceiver.OnConnectionClosed();
                    break;

                case PacketType.SSH_SMSG_EXITSTATUS:
                    FindChannelEntry(_shellID)._receiver.OnChannelClosed();
                    break;

                case PacketType.SSH_MSG_DEBUG: {
                    SSH1DataReader re = new SSH1DataReader(p.Data);
                    _eventReceiver.OnDebugMessage(false, re.ReadString());
                }
                break;

                case PacketType.SSH_MSG_IGNORE: {
                    SSH1DataReader re = new SSH1DataReader(p.Data);
                    _eventReceiver.OnIgnoreMessage(re.ReadString());
                }
                break;

                case PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION: {
                    int local  = SSHUtil.ReadInt32(p.Data, 0);
                    int remote = SSHUtil.ReadInt32(p.Data, 4);
                    FindChannelEntry(local)._receiver.OnChannelReady();
                }
                break;

                case PacketType.SSH_SMSG_SUCCESS:
                    if (_executingShell)
                    {
                        ExecShell();
                        this.FindChannelEntry(_shellID)._receiver.OnChannelReady();
                        _executingShell = false;
                    }
                    break;

                default:
                    _eventReceiver.OnUnknownMessage((byte)p.Type, p.Data);
                    break;
                }
            }
            catch (Exception ex) {
                if (!_closed)
                {
                    _eventReceiver.OnError(ex, ex.Message);
                }
            }
        }
Пример #18
0
 public void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel)
 {
     Channel = channel;
 }
Пример #19
0
 /// <summary>
 /// Sending exec command for SCP.
 /// </summary>
 /// <param name="receiver"></param>
 /// <param name="command"></param>
 /// <returns></returns>
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command)
 {
     SendExecCommand();
     return(null);
 }
Пример #20
0
 public TelnetChannel(TelnetConnection parent, StreamSocket socket, ISSHChannelEventReceiver r)
 {
     _parent   = parent;
     _socket   = socket;
     _receiver = r;
 }
Пример #21
0
 private void ReceivePortForwardingResponse(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader)
 {
     if(_negotiationStatus==1) {
         if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
             if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE)
                 receiver.OnChannelError(null, "opening channel failed; packet type="+pt);
             else {
                 int errcode = reader.ReadInt32();
                 string msg = Encoding.ASCII.GetString(reader.ReadString());
                 receiver.OnChannelError(null, msg);
             }
             Close();
         }
         else {
             _remoteID = reader.ReadInt32();
             _serverMaxPacketSize = reader.ReadInt32();
             _negotiationStatus = 0;
             receiver.OnChannelReady();
         }
     }
     else
         Debug.Assert(false);
 }
Пример #22
0
 // open new channel for SCP
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) {
     return DoExecCommandInternal(receiver, ChannelType.ExecCommand, command, "executing " + command);
 }
Пример #23
0
        public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
        {
            //open channel
            SSH2DataWriter wr = new SSH2DataWriter();
            wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN);
            wr.Write("session");
            int local_channel = this.RegisterChannelEventReceiver(null, receiver)._localID;

            wr.Write(local_channel);
            wr.Write(_param.WindowSize); //initial window size
            int windowsize = _param.WindowSize;
            wr.Write(_param.MaxPacketSize); //max packet size
            SSH2Channel channel = new SSH2Channel(this, ChannelType.Shell, local_channel);
            TransmitPacket(wr.ToByteArray());

            return channel;
        }
Пример #24
0
 // open subsystem such as NETCONF
 public SSHChannel OpenSubsystem(ISSHChannelEventReceiver receiver, string subsystem) {
     return DoExecCommandInternal(receiver, ChannelType.Subsystem, subsystem, "subsystem " + subsystem);
 }
Пример #25
0
 // sending exec command for SCP
 // TODO: まだ実装中です
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command)
 {
     //_executingExecCmd = true;
     SendExecCommand();
     return null;
 }
Пример #26
0
        //open channel
        private SSHChannel DoExecCommandInternal(ISSHChannelEventReceiver receiver, ChannelType channel_type, string command, string message) {
            SSH2DataWriter wr = OpenTransmissionPacket();
            wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN);
            wr.Write("session");
            int local_channel = this.ChannelCollection.RegisterChannelEventReceiver(null, receiver).LocalID;

            wr.Write(local_channel);
            wr.Write(_param.WindowSize); //initial window size
            int windowsize = _param.WindowSize;
            wr.Write(_param.MaxPacketSize); //max packet size
            SSH2Channel channel = new SSH2Channel(this, channel_type, local_channel, command);
            TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_OPEN, "executing command");
            TransmitPacket(wr);
            
            return channel;
        }
Пример #27
0
 /**
  * forwards the remote end to another host
  */
 public abstract SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port);
Пример #28
0
        public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) {
            SSH2DataWriter wr = OpenTransmissionPacket();
            wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN);
            wr.Write("direct-tcpip");
            int local_id = this.ChannelCollection.RegisterChannelEventReceiver(null, receiver).LocalID;
            wr.Write(local_id);
            wr.Write(_param.WindowSize); //initial window size
            int windowsize = _param.WindowSize;
            wr.Write(_param.MaxPacketSize); //max packet size
            wr.Write(remote_host);
            wr.Write(remote_port);
            wr.Write(originator_host);
            wr.Write(originator_port);

            SSH2Channel channel = new SSH2Channel(this, ChannelType.ForwardedLocalToRemote, local_id, null);
            
            TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_OPEN, "opening a forwarded port : host={0} port={1}", remote_host, remote_port);
            TransmitPacket(wr);
            
            return channel;
        }
Пример #29
0
 public abstract SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command);
Пример #30
0
        internal void ProcessPacket(ISSHChannelEventReceiver receiver, PacketType pt, int data_length, SSH2DataReader re) {
            //NOTE: the offset of 're' is next to 'receipiant channel' field

            AdjustWindowSize(pt, data_length);

            //SSH_MSG_CHANNEL_WINDOW_ADJUST comes before the complete of channel establishment
            if(pt==PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST) {
                int w = re.ReadInt32();
                //some servers may not send SSH_MSG_CHANNEL_WINDOW_ADJUST. 
                //it is dangerous to wait this message in send procedure
                _allowedDataSize += w;
                if(_connection.IsEventTracerAvailable)
                    _connection.TraceReceptionEvent("SSH_MSG_CHANNEL_WINDOW_ADJUST", "adjusted to {0} by increasing {1}", _allowedDataSize, w);
                return;
            }

            if(_negotiationStatus!=NegotiationStatus.Ready) //when the negotiation is not completed
                ProgressChannelNegotiation(receiver, pt, re);
            else 
                ProcessChannelLocalData(receiver, pt, re);
        }
Пример #31
0
 /**
 * opens a pseudo terminal
 */
 public abstract SSHChannel OpenShell(ISSHChannelEventReceiver receiver);
Пример #32
0
 //Progress the state of this channel establishment negotiation
 private void ProgressChannelNegotiation(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader re) {
     if(_type==ChannelType.Shell)
         OpenShellOrSubsystem(receiver, pt, re, "shell");
     else if(_type==ChannelType.ForwardedLocalToRemote)
         ReceivePortForwardingResponse(receiver, pt, re);
     else if(_type==ChannelType.Session)
         EstablishSession(receiver, pt, re);
     else if(_type==ChannelType.ExecCommand)  // for SCP
         ExecCommand(receiver, pt, re);
     else if(_type==ChannelType.Subsystem)
         OpenShellOrSubsystem(receiver, pt, re, "subsystem");
 }
Пример #33
0
 public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
 {
     this._pf = channel;
 }
Пример #34
0
 private void ProcessChannelLocalData(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader re) {
     switch(pt) {
         case PacketType.SSH_MSG_CHANNEL_DATA: {
             int len = re.ReadInt32();
             receiver.OnData(re.Image, re.Offset, len);
         }
             break;
         case PacketType.SSH_MSG_CHANNEL_EXTENDED_DATA: {
             int t = re.ReadInt32();
             byte[] data = re.ReadString();
             receiver.OnExtendedData(t, data);
         }
             break;
         case PacketType.SSH_MSG_CHANNEL_REQUEST: {
             string request = Encoding.ASCII.GetString(re.ReadString());
             bool reply = re.ReadBool();
             if(request=="exit-status") {
                 int status = re.ReadInt32();
             }
             else if(reply) { //we reject unknown requests including keep-alive check
                 SSH2DataWriter wr = new SSH2DataWriter();
                 wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_FAILURE);
                 wr.Write(_remoteID);
                 TransmitPayload(wr.ToByteArray());
             }
         }
             break;
         case PacketType.SSH_MSG_CHANNEL_EOF:
             receiver.OnChannelEOF();
             break;
         case PacketType.SSH_MSG_CHANNEL_CLOSE:
             _connection.ChannelCollection.UnregisterChannelEventReceiver(_localID);
             receiver.OnChannelClosed();
             break;
         case PacketType.SSH_MSG_CHANNEL_FAILURE:
             receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest);
             break;
         default:
             receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest);
             Debug.WriteLine("Unknown Packet "+pt);
             break;
     }			
 }
Пример #35
0
 public SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
 {
     throw new NotImplementedException();
 }
Пример #36
0
        private void OpenShellOrSubsystem(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader, string scheme) {
            if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
                    if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE)
                        receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt));
                    else {
                        int errcode = reader.ReadInt32();
                        string msg = Encoding.ASCII.GetString(reader.ReadString());
                        receiver.OnChannelError(new SSHException(msg));
                    }
                    Close();
                }
                else {
                    _remoteID = reader.ReadInt32();
                    _allowedDataSize = reader.ReadInt32();
                    _serverMaxPacketSize = reader.ReadInt32();

                    //open pty
                    SSH2DataWriter wr = new SSH2DataWriter();
                    SSHConnectionParameter param = _connection.Param;
                    wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST);
                    wr.Write(_remoteID);
                    wr.Write("pty-req");
                    wr.Write(true);
                    wr.Write(param.TerminalName);
                    wr.Write(param.TerminalWidth);
                    wr.Write(param.TerminalHeight);
                    wr.Write(param.TerminalPixelWidth);
                    wr.Write(param.TerminalPixelHeight);
                    wr.WriteAsString(new byte[0]);
                    if(_connection.IsEventTracerAvailable)
                        _connection.TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_REQUEST, "pty-req", "terminal={0} width={1} height={2}", param.TerminalName, param.TerminalWidth, param.TerminalHeight);
                    TransmitPayload(wr.ToByteArray());

                    _negotiationStatus = NegotiationStatus.WaitingPtyReqConfirmation;
                }
            }
            else if(_negotiationStatus==NegotiationStatus.WaitingPtyReqConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) {
                    receiver.OnChannelError(new SSHException("opening pty failed"));
                    Close();
                }
                else {
                    //agent request (optional)
                    if(_connection.Param.AgentForward!=null) {
                        SSH2DataWriter wr = new SSH2DataWriter();
                        wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST);
                        wr.Write(_remoteID);
                        wr.Write("*****@*****.**");
                        wr.Write(true);
                        _connection.TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_REQUEST, "auth-agent-req", "");
                        TransmitPayload(wr.ToByteArray());
                        _negotiationStatus = NegotiationStatus.WaitingAuthAgentReqConfirmation;
                    }
                    else {
                        OpenScheme(scheme);
                        _negotiationStatus = NegotiationStatus.WaitingShellConfirmation;
                    }
                }
            }
            else if(_negotiationStatus==NegotiationStatus.WaitingAuthAgentReqConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS && pt!=PacketType.SSH_MSG_CHANNEL_FAILURE) {
                    receiver.OnChannelError(new SSHException("auth-agent-req error"));
                    Close();
                }
                else { //auth-agent-req is optional
                    _connection.SetAgentForwardConfirmed(pt==PacketType.SSH_MSG_CHANNEL_SUCCESS);
                    _connection.TraceReceptionEvent(pt, "auth-agent-req");

                    OpenScheme(scheme);
                    _negotiationStatus = NegotiationStatus.WaitingShellConfirmation;
                }
            }
            else if(_negotiationStatus==NegotiationStatus.WaitingShellConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) {
                    receiver.OnChannelError(new SSHException("Opening shell failed: packet type="+pt.ToString()));
                    Close();
                }
                else {
                    receiver.OnChannelReady();
                    _negotiationStatus = NegotiationStatus.Ready; //goal!
                }
            }
        }
Пример #37
0
        private void OpenShell(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader)
        {
            if(_negotiationStatus==3) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
                    if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE)
                        receiver.OnChannelError(null, "opening channel failed; packet type="+pt);
                    else {
                        int errcode = reader.ReadInt32();
                        string msg = Encoding.ASCII.GetString(reader.ReadString());
                        receiver.OnChannelError(null, msg);
                    }
                    Close();
                }
                else {
                    _remoteID = reader.ReadInt32();
                    _serverMaxPacketSize = reader.ReadInt32();

                    //open pty
                    SSH2DataWriter wr = new SSH2DataWriter();
                    wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST);
                    wr.Write(_remoteID);
                    wr.Write("pty-req");
                    wr.Write(true);
                    wr.Write(_connection.Param.TerminalName);
                    wr.Write(_connection.Param.TerminalWidth);
                    wr.Write(_connection.Param.TerminalHeight);
                    wr.Write(_connection.Param.TerminalPixelWidth);
                    wr.Write(_connection.Param.TerminalPixelHeight);
                    wr.WriteAsString(new byte[0]);
                    TransmitPacket(wr.ToByteArray());

                    _negotiationStatus = 2;
                }
            }
            else if(_negotiationStatus==2) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) {
                    receiver.OnChannelError(null, "opening pty failed");
                    Close();
                }
                else {
                    //open shell
                    SSH2DataWriter wr = new SSH2DataWriter();
                    wr.Write((byte)PacketType.SSH_MSG_CHANNEL_REQUEST);
                    wr.Write(_remoteID);
                    wr.Write("shell");
                    wr.Write(true);
                    TransmitPacket(wr.ToByteArray());

                    _negotiationStatus = 1;
                }
            }
            else if(_negotiationStatus==1) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) {
                    receiver.OnChannelError(null, "Opening shell failed: packet type="+pt.ToString());
                    Close();
                }
                else {
                    receiver.OnChannelReady();
                    _negotiationStatus = 0; //goal!
                }
            }
            else
                Debug.Assert(false);
        }
Пример #38
0
        // sending "exec" service for SCP protocol.
        private void ExecCommand(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) {
            if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
                    if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE)
                        receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt));
                    else {
                        int errcode = reader.ReadInt32();
                        string msg = Encoding.ASCII.GetString(reader.ReadString());
                        receiver.OnChannelError(new SSHException(msg));
                    }
                    Close();
                }
                else {
                    _remoteID = reader.ReadInt32();
                    _allowedDataSize = reader.ReadInt32();
                    _serverMaxPacketSize = reader.ReadInt32();

                    // exec command
                    SSH2DataWriter wr = new SSH2DataWriter();
                    SSHConnectionParameter param = _connection.Param;
                    wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST);
                    wr.Write(_remoteID);
                    wr.Write("exec");  // "exec"
                    wr.Write(false);   // want confirm is disabled. (*)
                    wr.Write(_command);

                    if (_connection.IsEventTracerAvailable)
                        _connection.TraceTransmissionEvent("exec command","cmd={0}", _command);
                    TransmitPayload(wr.ToByteArray());

                    //confirmation is omitted
                    receiver.OnChannelReady();
                    _negotiationStatus = NegotiationStatus.Ready; //goal!
                }
            }
            else if(_negotiationStatus==NegotiationStatus.WaitingExecCmdConfirmation) {
                if(pt!=PacketType.SSH_MSG_CHANNEL_DATA) {
                    receiver.OnChannelError(new SSHException("exec command failed"));
                    Close();
                }
                else {
                    receiver.OnChannelReady();
                    _negotiationStatus = NegotiationStatus.Ready; //goal!
                }
            }
            else
                throw new SSHException("internal state error");
        }
Пример #39
0
        public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            SSH2DataWriter wr = new SSH2DataWriter();
            wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN);
            wr.Write("direct-tcpip");
            int local_id = RegisterChannelEventReceiver(null, receiver)._localID;
            wr.Write(local_id);
            wr.Write(_param.WindowSize); //initial window size
            int windowsize = _param.WindowSize;
            wr.Write(_param.MaxPacketSize); //max packet size
            wr.Write(remote_host);
            wr.Write(remote_port);
            wr.Write(originator_host);
            wr.Write(originator_port);

            SSH2Channel channel = new SSH2Channel(this, ChannelType.ForwardedLocalToRemote, local_id);

            TransmitPacket(wr.ToByteArray());

            return channel;
        }
Пример #40
0
 private void EstablishSession(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) {
     if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) {
         if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
             if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE)
                 receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt));
             else {
                 int remote_id = reader.ReadInt32();
                 int errcode = reader.ReadInt32();
                 string msg = Encoding.ASCII.GetString(reader.ReadString());
                 receiver.OnChannelError(new SSHException(msg));
             }
             Close();
         }
         else {
             _remoteID = reader.ReadInt32();
             _serverMaxPacketSize = reader.ReadInt32();
             _negotiationStatus = NegotiationStatus.Ready;
             receiver.OnChannelReady();
         }
     }
     else
         throw new SSHException("internal state error");
 }
 public virtual void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel)
 {
 }
Пример #42
0
 // sending exec command for SCP
 // TODO: まだ実装中です
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command)
 {
     //_executingExecCmd = true;
     SendExecCommand();
     return(null);
 }
Пример #43
0
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Пример #44
0
        internal void AsyncReceivePacket(DataFragment data)
        {
            try {
                int            len = 0, channel = 0;
                SSH1DataReader re = new SSH1DataReader(data);
                PacketType     pt = re.ReadPacketType();
                switch (pt)
                {
                case PacketType.SSH_SMSG_STDOUT_DATA:
                    len = re.ReadInt32();
                    _channel_collection.FindChannelEntry(_shellID).Receiver.OnData(re.Image, re.Offset, len);
                    break;

                case PacketType.SSH_SMSG_STDERR_DATA: {
                    _channel_collection.FindChannelEntry(_shellID).Receiver.OnExtendedData((int)PacketType.SSH_SMSG_STDERR_DATA, re.ReadString());
                }
                break;

                case PacketType.SSH_MSG_CHANNEL_DATA:
                    channel = re.ReadInt32();
                    len     = re.ReadInt32();
                    _channel_collection.FindChannelEntry(channel).Receiver.OnData(re.Image, re.Offset, len);
                    break;

                case PacketType.SSH_MSG_PORT_OPEN:
                    ProcessPortforwardingRequest(_eventReceiver, re);
                    break;

                case PacketType.SSH_MSG_CHANNEL_CLOSE: {
                    channel = re.ReadInt32();
                    ISSHChannelEventReceiver r = _channel_collection.FindChannelEntry(channel).Receiver;
                    _channel_collection.UnregisterChannelEventReceiver(channel);
                    r.OnChannelClosed();
                }
                break;

                case PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION:
                    channel = re.ReadInt32();
                    break;

                case PacketType.SSH_MSG_DISCONNECT:
                    _eventReceiver.OnConnectionClosed();
                    break;

                case PacketType.SSH_SMSG_EXITSTATUS:
                    _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelClosed();
                    break;

                case PacketType.SSH_MSG_DEBUG:
                    _eventReceiver.OnDebugMessage(false, re.ReadString());
                    break;

                case PacketType.SSH_MSG_IGNORE:
                    _eventReceiver.OnIgnoreMessage(re.ReadString());
                    break;

                case PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION: {
                    int local  = re.ReadInt32();
                    int remote = re.ReadInt32();
                    _channel_collection.FindChannelEntry(local).Receiver.OnChannelReady();
                }
                break;

                case PacketType.SSH_SMSG_SUCCESS:
                    if (_executingShell)
                    {
                        ExecShell();
                        _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelReady();
                        _executingShell = false;
                    }
                    break;

                default:
                    _eventReceiver.OnUnknownMessage((byte)pt, re.ReadAll());
                    break;
                }
            }
            catch (Exception ex) {
                _eventReceiver.OnError(ex);
            }
        }
Пример #45
0
        public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            if (_shellID == -1) {
                ExecShell();
                _shellID = _channel_collection.RegisterChannelEventReceiver(null, new SSH1DummyReceiver()).LocalID;
            }

            int local_id = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID;

            Transmit(
                new SSH1Packet(SSH1PacketType.SSH_MSG_PORT_OPEN)
                    .WriteInt32(local_id) //channel id is fixed to 0
                    .WriteString(remote_host)
                    .WriteInt32(remote_port)
                //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified
                //writer.Write(originator_host);
            );
            TraceTransmissionEvent(SSH1PacketType.SSH_MSG_PORT_OPEN, "open forwarded port: host={0} port={1}", remote_host, remote_port);

            return new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id);
        }
Пример #46
0
 public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Пример #47
0
 /**
  * opens a pseudo terminal
  */
 public abstract SSHChannel OpenShell(ISSHChannelEventReceiver receiver);
Пример #48
0
 public SSHChannel StartShell(ISSHChannelEventReceiver receiver)
 {
     return(m_conn.OpenShell(receiver));
 }
Пример #49
0
 public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Пример #50
0
 public TelnetPacketBuilder(ISSHChannelEventReceiver handler)
 {
     this._handler = handler;
 }
Пример #51
0
 public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Пример #52
0
 public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Пример #53
0
 public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
 {
     _pf = channel;
 }
Пример #54
0
 public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver)
 {
     throw new Exception("The method or operation is not implemented.");
 }