Beispiel #1
0
 def request_shell(self, data):
     protocol = Protocol()
     transport = SSHSessionProcessProtocol(self)
     protocol.makeConnection(transport)
     transport.makeConnection(wrapProtocol(protocol))
     self.client = transport
     return True
 def request_shell(self, data):
     protocol  = EchoProtocol()
     transport = SSHSessionProcessProtocol(self)
     protocol.makeConnection(transport)
     transport.makeConnection(wrapProtocol(protocol))
     self.client = transport
     return True
 def request_exec(self, data):
     print 'request_exec', data
     protocol = SCPProtocol()
     transport = SSHSessionProcessProtocol(self)
     protocol.makeConnection(transport)
     transport.makeConnection(wrapProtocol(protocol))
     self.client = transport
     return True
Beispiel #4
0
def attach_protocol_to_channel(protocol, channel):
    # These are from
    # http://as.ynchrono.us/2011/08/twisted-conch-in-60-seconds-protocols.html
    transport = SSHSessionProcessProtocol(channel)
    protocol.makeConnection(transport)
    transport.makeConnection(wrapProtocol(protocol))
    channel.client = transport

    # And this one's from me :3
    channel.dataReceived = protocol.dataReceived
Beispiel #5
0
def attach_protocol_to_channel(protocol, channel):
    # These are from
    # http://as.ynchrono.us/2011/08/twisted-conch-in-60-seconds-protocols.html
    transport = SSHSessionProcessProtocol(channel)
    protocol.makeConnection(transport)
    transport.makeConnection(wrapProtocol(protocol))
    channel.client = transport

    # And this one's from me :3
    channel.dataReceived = protocol.dataReceived
Beispiel #6
0
    def request_subsystem(self, data):
        subsystem, junk = common.getNS(data)
        if subsystem == 'amp':
            #import pdb; pdb.set_trace()
            protocol = EchoDateProtocol(self.avatar)
            transport = SSHSessionProcessProtocol(self)
            protocol.makeConnection(transport)
            transport.makeConnection(wrapProtocol(protocol))
            self.client = transport
            return True  # subsystem request OK

        else:
            log.msg('Unknown subsystem requested: %r' % (subsystem, ))
            return False  # Fail subsystem request.
    def request_subsystem(self, data):
        subsystem, junk = common.getNS(data)
        if subsystem == 'amp':
            #import pdb; pdb.set_trace()
            protocol = EchoDateProtocol(self.avatar)
            transport = SSHSessionProcessProtocol(self)
            protocol.makeConnection(transport)
            transport.makeConnection(wrapProtocol(protocol))
            self.client = transport
            return True # subsystem request OK

        else:
            log.msg('Unknown subsystem requested: %r' % (subsystem,))
            return False # Fail subsystem request.
Beispiel #8
0
 def setUp(self):
     self.session = SSHSession(conn=StubConnection(),
                               remoteWindow=500,
                               remoteMaxPacket=100)
     self.pp = SSHSessionProcessProtocol(self.session)
Beispiel #9
0
class SSHSessionProcessProtocolTestCase(TestCase):
    """
    Tests for L{SSHSessionProcessProtocol}.
    """
    def setUp(self):
        self.session = SSHSession(conn=StubConnection(),
                                  remoteWindow=500,
                                  remoteMaxPacket=100)
        self.pp = SSHSessionProcessProtocol(self.session)

    def assertSessionClosed(self):
        """
        Assert that C{self.session} is closed.
        """
        self.assertTrue(self.session.conn.closes[self.session])

    def assertRequestsEqual(self, expectedRequests):
        """
        Assert that C{self.session} has sent the C{expectedRequests}.
        """
        self.assertEqual(self.session.conn.requests[self.session],
                         expectedRequests)

    def test_getSignalName(self):
        """
        _getSignalName should return the name of a signal when given the
        signal number.
        """
        for signalName in SUPPORTED_SIGNALS:
            signalName = 'SIG' + signalName
            signalValue = getattr(signal, signalName)
            sshName = self.pp._getSignalName(signalValue)
            self.assertEquals(
                sshName, signalName,
                "%i: %s != %s" % (signalValue, sshName, signalName))

    def test_getSignalNameWithLocalSignal(self):
        """
        If there are signals in the signal module which aren't in the SSH RFC,
        we map their name to [signal name]@[platform].
        """
        signal.SIGTwistedTest = signal.NSIG + 1  # value can't exist normally
        # Force reinitialization of signals
        self.pp._signalValuesToNames = None
        self.assertEquals(self.pp._getSignalName(signal.SIGTwistedTest),
                          'SIGTwistedTest@' + sys.platform)

    if getattr(signal, 'SIGALRM', None) is None:
        test_getSignalName.skip = test_getSignalNameWithLocalSignal.skip = \
            "Not all signals available"

    def test_processEndedWithExitCode(self):
        """
        When processEnded is called, if there is an exit code in the reason
        it should be sent in an exit-status method.  The connection should be
        closed.
        """
        self.pp.processEnded(Failure(ProcessDone(None)))
        self.assertRequestsEqual([('exit-status', struct.pack('>I',
                                                              0), False)])
        self.assertSessionClosed()

    def test_processEndedWithExitSignalCoreDump(self):
        """
        When processEnded is called, if there is an exit signal in the reason
        it should be sent in an exit-signal message.  The connection should be
        closed.
        """
        self.pp.processEnded(
            Failure(ProcessTerminated(1, signal.SIGTERM,
                                      1 << 7)))  # 7th bit means core dumped
        self.assertRequestsEqual([(
            'exit-signal',
            NS('TERM')  # signal name
            + '\x01'  # core dumped is true
            + NS('')  # error message
            + NS(''),  # language tag
            False)])
        self.assertSessionClosed()

    def test_processEndedWithExitSignalNoCoreDump(self):
        """
        When processEnded is called, if there is an exit signal in the
        reason it should be sent in an exit-signal message.  If no
        core was dumped, don't set the core-dump bit.
        """
        self.pp.processEnded(Failure(ProcessTerminated(1, signal.SIGTERM, 0)))
        # see comments in test_processEndedWithExitSignalCoreDump for the
        # meaning of the parts in the request
        self.assertRequestsEqual([
            ('exit-signal', NS('TERM') + '\x00' + NS('') + NS(''), False)
        ])
        self.assertSessionClosed()

    if getattr(os, 'WCOREDUMP', None) is None:
        skipMsg = "can't run this w/o os.WCOREDUMP"
        test_processEndedWithExitSignalCoreDump.skip = skipMsg
        test_processEndedWithExitSignalNoCoreDump.skip = skipMsg
Beispiel #10
0
 def inConnectionLost(self):
     # log.msg('TPP.inConnectionLost()')
     SSHSessionProcessProtocol.inConnectionLost(self)
Beispiel #11
0
 def outReceived(self, data):
     # log.msg(len(data))
     # if len(data) < 8192:
     #     log.msg(data)
     SSHSessionProcessProtocol.outReceived(self, data)
Beispiel #12
0
 def __init__(self, session):
     SSHSessionProcessProtocol.__init__(self, session)
     self.session = session
Beispiel #13
0
 def setUp(self):
     self.session = SSHSession(
         conn=StubConnection(), remoteWindow=500, remoteMaxPacket=100)
     self.pp = SSHSessionProcessProtocol(self.session)
Beispiel #14
0
class SSHSessionProcessProtocolTestCase(TestCase):
    """
    Tests for L{SSHSessionProcessProtocol}.
    """

    def setUp(self):
        self.session = SSHSession(
            conn=StubConnection(), remoteWindow=500, remoteMaxPacket=100)
        self.pp = SSHSessionProcessProtocol(self.session)


    def assertSessionClosed(self):
        """
        Assert that C{self.session} is closed.
        """
        self.assertTrue(self.session.conn.closes[self.session])


    def assertRequestsEqual(self, expectedRequests):
        """
        Assert that C{self.session} has sent the C{expectedRequests}.
        """
        self.assertEqual(
            self.session.conn.requests[self.session],
            expectedRequests)


    def test_getSignalName(self):
        """
        _getSignalName should return the name of a signal when given the
        signal number.
        """
        for signalName in SUPPORTED_SIGNALS:
            signalName = 'SIG' + signalName
            signalValue = getattr(signal, signalName)
            sshName = self.pp._getSignalName(signalValue)
            self.assertEquals(sshName, signalName,
                              "%i: %s != %s" % (signalValue, sshName,
                                                signalName))


    def test_getSignalNameWithLocalSignal(self):
        """
        If there are signals in the signal module which aren't in the SSH RFC,
        we map their name to [signal name]@[platform].
        """
        signal.SIGTwistedTest = signal.NSIG + 1 # value can't exist normally
        # Force reinitialization of signals
        self.pp._signalValuesToNames = None
        self.assertEquals(self.pp._getSignalName(signal.SIGTwistedTest),
                          'SIGTwistedTest@' + sys.platform)


    if getattr(signal, 'SIGALRM', None) is None:
        test_getSignalName.skip = test_getSignalNameWithLocalSignal.skip = \
            "Not all signals available"


    def test_processEndedWithExitCode(self):
        """
        When processEnded is called, if there is an exit code in the reason
        it should be sent in an exit-status method.  The connection should be
        closed.
        """
        self.pp.processEnded(Failure(ProcessDone(None)))
        self.assertRequestsEqual(
            [('exit-status', struct.pack('>I', 0) , False)])
        self.assertSessionClosed()


    def test_processEndedWithExitSignalCoreDump(self):
        """
        When processEnded is called, if there is an exit signal in the reason
        it should be sent in an exit-signal message.  The connection should be
        closed.
        """
        self.pp.processEnded(
            Failure(ProcessTerminated(1,
                signal.SIGTERM, 1 << 7))) # 7th bit means core dumped
        self.assertRequestsEqual(
            [('exit-signal',
              NS('TERM') # signal name
              + '\x01' # core dumped is true
              + NS('') # error message
              + NS(''), # language tag
              False)])
        self.assertSessionClosed()


    def test_processEndedWithExitSignalNoCoreDump(self):
        """
        When processEnded is called, if there is an exit signal in the
        reason it should be sent in an exit-signal message.  If no
        core was dumped, don't set the core-dump bit.
        """
        self.pp.processEnded(
            Failure(ProcessTerminated(1, signal.SIGTERM, 0)))
        # see comments in test_processEndedWithExitSignalCoreDump for the
        # meaning of the parts in the request
        self.assertRequestsEqual(
             [('exit-signal', NS('TERM') + '\x00' + NS('') + NS(''), False)])
        self.assertSessionClosed()


    if getattr(os, 'WCOREDUMP', None) is None:
        skipMsg = "can't run this w/o os.WCOREDUMP"
        test_processEndedWithExitSignalCoreDump.skip = skipMsg
        test_processEndedWithExitSignalNoCoreDump.skip = skipMsg