<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:tahoma,new york,times,serif;font-size:12pt"><div><style type="text/css"><!--DIV {margin:0px;}--></style><div style="font-family: tahoma,new york,times,serif; font-size: 12pt;"><div style="font-family: tahoma,new york,times,serif;">Hello,<br>
<br>
I have created a telnet server in python.<br>
<br>
Maybe you wonder why to create a telnet server while Windows has one?
Because the windows telnet server does not allow to interract with the
desktop. If you try to start a GUI app, it will start and run but will
not be displayed on the server desktop.<br>
<br>
My telnet server will allow to start a GUI application interracting with the windows desktop of the server.<br>
Ex. : typing "notepad" in the telnet console will pop up the notepad on the Windows desktop of the server.<br>
<br>
At the time this server works but has no authentication feature implemented.<br>
<br>
I would like to implement the authentication using AuthenticatingTelnetProtocol and credential.<br>I found the "<a rel="nofollow" target="_blank" href="http://twistedmatrix.com/projects/core/documentation/examples/cred.py">cred.py</a>" example on the twistedmatrix website (in the example section) and looked fine as starting point.<br><br>I quite well understand this expample.<br><br>I modified the code as follow but always got the same error message when a connectiion is attempted.<br>I have been trying since two weeks but I cannot get it work.<br><br>Is there a problem with "AuthenticatingTelnetProtocol"<br><br>The error message:<br>
************************************************************<br>
D:\workspace\twisted>telnet_cred.py<br>
2009-08-20 08:57:11+0200 [-] Log opened.<br>
2009-08-20 08:57:11+0200 [-] __main__.ServerFactory starting on 4738<br>
2009-08-20 08:57:11+0200 [-] Starting factory <__main__.ServerFactory instance at 0x00D79C60><br>
2009-08-20 08:57:15+0200 [__main__.ServerFactory] DEBUG: buildProtocol - addr IPv4Address(TCP, '127.0.0.1', 4978)<br>
2009-08-20 08:57:15+0200 [__main__.ServerFactory] Unhandled Error<br>
Traceback (most recent call last):<br>
File "C:\Python25\lib\site-packages\twisted\python\log.py", line 69, in callWithContext<br>
return context.call({ILogContext: newCtx}, func, *args, **kw)<br>
File "C:\Python25\lib\site-packages\twisted\python\context.py", line 59, in callWithContext<br>
return self.currentContext().callWithContext(ctx, func, *args, **kw)<br>
File "C:\Python25\lib\site-packages\twisted\python\context.py", line 37, in callWithContext<br>
return func(*args,**kw)<br>
File "C:\Python25\lib\site-packages\twisted\internet\selectreactor.py", line 146, in _doReadOrWrite<br>
why = getattr(selectable, method)()<br>
--- <exception caught here> ---<br>
File "C:\Python25\lib\site-packages\twisted\internet\tcp.py", line 932, in doRead<br>
protocol = self.factory.buildProtocol(self._buildAddr(addr))<br>
File "D:\workspace\twisted\telnet_cred.py", line 130, in buildProtocol<br>
p = protocol.ServerFactory.buildProtocol(self, addr)<br>
File "C:\Python25\lib\site-packages\twisted\internet\protocol.py", line 98, in buildProtocol<br>
p = self.protocol()<br>
exceptions.TypeError: __init__() takes exactly 2 arguments (1 given)<br>
************************************************************<br>
<br>The modified code:<br>************************************************************<br><br># Copyright (c) 2001-2004 Twisted Matrix Laboratories.<br># See LICENSE for details.<br><br><br><br>import sys<br>from zope.interface import implements, Interface<br><br>from twisted.protocols import basic<br>from twisted.internet import protocol<br>from twisted.python import log<br><br>from twisted.cred import error<br>from twisted.cred import portal<br>from twisted.cred import checkers<br>from twisted.cred import credentials<br><br>from twisted.conch.telnet import AuthenticatingTelnetProtocol, ITelnetProtocol, TelnetProtocol<br><br>class IProtocolUser(Interface):<br> def getPrivileges():<br> """Return a list of privileges this user has."""<br><br> def logout():<br> """Cleanup per-login resources allocated to this avatar"""<br><br>class
AnonymousUser:<br> implements(IProtocolUser)<br> <br> def getPrivileges(self):<br> return [1, 2, 3]<br><br> def logout(self):<br> print "Cleaning up anonymous user resources"<br><br>class RegularUser:<br> implements(IProtocolUser)<br> <br> def getPrivileges(self):<br> return [1, 2, 3, 5, 6]<br><br> def logout(self):<br> print "Cleaning up regular user resources"<br><br>class Administrator:<br> implements(IProtocolUser)<br> <br> def getPrivileges(self):<br> return range(50)<br><br> def
logout(self):<br> print "Cleaning up administrator resources"<br><br>class Protocol(basic.LineReceiver):<br> user = None<br> portal = None<br> avatar = None<br> logout = None<br><br> def connectionMade(self):<br> self.sendLine("Login with USER <name> followed by PASS <password> or ANON")<br> self.sendLine("Check privileges with PRIVS")<br><br> def connectionLost(self, reason):<br> if self.logout:<br> self.logout()<br> self.avatar = None<br> self.logout =
None<br> <br> def lineReceived(self, line):<br> f = getattr(self, 'cmd_' + line.upper().split()[0])<br> if f:<br> try:<br> f(*line.split()[1:])<br> except TypeError:<br> self.sendLine("Wrong number of arguments.")<br> except:<br> self.sendLine("Server error (probably your fault)")<br><br> def cmd_ANON(self):<br> if
self.portal:<br> self.portal.login(credentials.Anonymous(), None, IProtocolUser<br> ).addCallbacks(self._cbLogin, self._ebLogin<br> )<br> else:<br> self.sendLine("DENIED")<br> <br> def cmd_USER(self, name):<br> self.user = name<br> self.sendLine("Alright. Now PASS?")<br> <br> def cmd_PASS(self, password):<br> if not self.user:<br>
self.sendLine("USER required before PASS")<br> else:<br> if self.portal:<br> self.portal.login(<br> credentials.UsernamePassword(self.user, password),<br> None,<br> IProtocolUser<br> ).addCallbacks(self._cbLogin, self._ebLogin<br>
)<br> else:<br> self.sendLine("DENIED")<br><br> def cmd_PRIVS(self):<br> self.sendLine("You have the following privileges: ")<br> self.sendLine(" ".join(map(str, self.avatar.getPrivileges())))<br><br> def _cbLogin(self, (interface, avatar, logout)):<br> assert interface is IProtocolUser<br> self.avatar = avatar<br> self.logout = logout<br> self.sendLine("Login successful. Available commands: PRIVS")<br> <br> def _ebLogin(self,
failure):<br> failure.trap(error.UnauthorizedLogin)<br> self.sendLine("Login denied! Go away.")<br><br>class ServerFactory(protocol.ServerFactory):<br>## protocol = Protocol<br> protocol = AuthenticatingTelnetProtocol<br> <br> def __init__(self, portal):<br> self.portal = portal<br> <br> def buildProtocol(self, addr):<br> print "DEBUG: buildProtocol - addr", addr<br> p = protocol.ServerFactory.buildProtocol(self, addr)<br> print "DEBUG2: buildProtocol - addr", addr<br> p.portal = self.portal<br> return
p<br><br>class Realm:<br> implements(portal.IRealm)<br><br> def requestAvatar(self, avatarId, mind, *interfaces):<br> if IProtocolUser in interfaces:<br> if avatarId == checkers.ANONYMOUS:<br> av = AnonymousUser()<br> elif avatarId.isupper():<br> # Capitalized usernames are administrators.<br> av = Administrator()<br> else:<br>
av = RegularUser()<br> return IProtocolUser, av, av.logout<br> elif ITelnetProtocol in interfaces:<br> print "BEGUG: aaaa"<br> if avatarId.isupper():<br> av = TelnetProtocol()<br> return ITelnetProtocol, av, None<br> raise NotImplementedError("Only IProtocolUser interface is supported by this realm")<br><br>def main():<br> r = Realm()<br> p = portal.Portal(r)<br> c = checkers.InMemoryUsernamePasswordDatabaseDontUse()<br> c.addUser("auser",
"thepass")<br> c.addUser("SECONDUSER", "secret")<br> p.registerChecker(c)<br> p.registerChecker(checkers.AllowAnonymousAccess())<br> <br> f = ServerFactory(p)<br> <br> log.startLogging(sys.stdout)<br><br> from twisted.internet import reactor<br> reactor.listenTCP(4738, f)<br> reactor.run()<br><br>if __name__ == '__main__':<br> main()<br><br>************************************************************<br><br>If someone could help me.<br><br>Philippe<br></div></div><br>
</div></div><br>
</body></html>