import sys

from twisted.conch import recvline, avatar
from twisted.conch.interfaces import IConchUser
from twisted.conch.ssh import keys, session
from twisted.cred import portal, checkers
from twisted.internet import reactor
from twisted.conch.ssh.factory import SSHFactory
from zope.interface import implements
from twisted.python import log


log.startLogging(sys.stdout)

with open('./demo.pem') as pipe:
    private_blob = pipe.read()
    private_key = keys.Key.fromString(data=private_blob)

with open('./demo.pub') as pipe:
    public_blob = pipe.read()
    public_key = keys.Key.fromString(data=public_blob)

class HoneyRealm(object):
    implements(portal.IRealm)
    def requestAvatar(self, avatarId, mind, *interfaces):
        print avatarId, mind
        pass

class HoneyProtocol(recvline.HistoricRecvLine):
    def __init__(self, user):
        self.user = user

    def connectionMade(self):
        self.showPrompt()

    def showPrompt(self):
        self.terminal.write("$ ")


class HoneyAvatar(avatar.ConchUser):
    implements(IConchUser)

    def __init__(self, username):
        log.msg(username)
        avatar.ConchUser.__init__(self)
        self.username = username

class HoneyRealm(object):
    implements(portal.IRealm)

    def requestAvatar(self, avatarId, mind, *interfaces):
        if IConchUser in interfaces:
            return interfaces[0], HoneyAvatar(avatarId), lambda: None
        else:
            raise NotImplementedError("No supported interfaces found")


factory = SSHFactory()
factory.privateKeys = {'ssh-rsa': private_key}
factory.publicKeys = {'ssh-rsa': public_key}
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
checker.addUser('root', 'password')
portal = portal.Portal(HoneyRealm())
portal.registerChecker(checker)

factory.portal = portal



reactor.listenTCP(2000, factory)

log.msg('Starting up')
reactor.run()

@@ -304,17 +304,18 @@
 mplement
-s
+er
 %0Afrom tw
@@ -615,163 +615,8 @@
 b)%0A%0A
-class HoneyRealm(object):%0A    implements(portal.IRealm)%0A    def requestAvatar(self, avatarId, mind, *interfaces):%0A        print avatarId, mind%0A        pass
 %0A%0Acl
@@ -739,32 +739,67 @@
 tionMade(self):%0A
+        log.msg('connection made')%0A
         self.sho
@@ -867,16 +867,42 @@
 ite(
-%22$ %22
+'$ '
 )%0A%0A%0A
+%0A@implementer(IConchUser)%0A
 clas
@@ -942,30 +942,63 @@
     
-implements(IConchUser)
+'''%0A    An avatar represents one user logged in%0A    '''
 %0A%0A  
@@ -1045,16 +1045,29 @@
 log.msg(
+'username:', 
 username
@@ -1145,39 +1145,446 @@
 ame%0A
-%0Aclass HoneyRealm(object):%0A    
+        self.channelLookup.update(%7B'session': session.SSHSession%7D)%0A%0A    def openShell(transport):%0A        log.msg('Opening shell')%0A        protocol = HoneyProtocol()%0A        protocol.makeConnection(transport)%0A        transport.makeConnection(session.wrapProtocol(protocol))%0A        log.msg('Shell created')%0A%0A    def getPty(self, **args):%0A        return None%0A%0A    def eofReceived(self):%0A        pass%0A%0A    def closed(self):%0A        pass%0A%0A%0A@
 impl
@@ -1588,17 +1588,18 @@
 mplement
-s
+er
 (portal.
@@ -1605,16 +1605,133 @@
 .IRealm)
+%0Aclass HoneyRealm(object):%0A    '''%0A    A realm is a factory that returns avatars after authentication is made%0A    '''
 %0A%0A    de
@@ -1819,16 +1819,66 @@
 rfaces:%0A
+            log.msg('Interface IConchUser found')%0A
         
@@ -1991,17 +1991,17 @@
 edError(
-%22
+'
 No suppo
@@ -2025,9 +2025,9 @@
 ound
-%22
+'
 )%0A%0A%0A

Remove old realm object