/
xmlrpcserver.py
76 lines (58 loc) · 2.61 KB
/
xmlrpcserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Modular XMLRPC Server with SSL, Authentication using Twistd
# Etopian Inc.
# (c) Sami Khan 2009
# Licensed under GPL v 3
from zope.interface import Interface, implements
from twisted.application import service, internet, strports
from twisted.cred import portal as portal1
from twisted.cred import checkers
from twisted.internet import protocol, reactor, ssl
from twisted.protocols import basic
from twisted.web2 import channel, resource, http, responsecode, xmlrpc, server
from twisted.web2.auth.interfaces import IAuthenticatedRequest, IHTTPUser
from twisted.web2.auth import basic, digest, wrapper
#dynamically load the protocol
from ConfigParser import *
config = ConfigParser()
config.read('xmlrpc.conf')
try:
service = config.get('XMLRPCServer', 'service')
print service
exec "import " + service + " as xmlrpcprotocol"
except ImportError:
print 'Protocol file was not found. Check to see if it compiles.'
class IHTTPUser(Interface):
pass
class HTTPUser(object):
implements(IHTTPUser)
class HTTPAuthRealm(object):
implements(portal1.IRealm)
def requestAvatar(self, avatarId, mind, *interfaces):
if IHTTPUser in interfaces:
return IHTTPUser, HTTPUser()
raise NotImplementedError("Only IHTTPUser interface is supported")
class XMLRPCService(internet.SSLServer):
#check a password generated by htpasswd, keeps you from storing plaintext on server
def checkHashedPassword(self, username, password, hashedpassword):
"""For files created with htpasswd"""
from crypt import crypt
return crypt(password, hashedpassword[:2])
def start(self):
#rsrc = NameServerProtocol( )
rsrc = apply(xmlrpcprotocol.XMLRPCProtocol)
#handle auth stuff
portal = portal1.Portal(HTTPAuthRealm())
checker = checkers.FilePasswordDB('httpspass.conf', ':', 0, 1, True, hash=self.checkHashedPassword)
portal.registerChecker(checker)
httpauthrealm = port = config.get('XMLRPCServer', 'httpauthrealm')
credFactories = (basic.BasicCredentialFactory(httpauthrealm), digest.DigestCredentialFactory('md5', httpauthrealm))
ifaces = (IHTTPUser,)
root = wrapper.HTTPAuthResource(rsrc, credFactories, portal, ifaces)
site = server.Site(root)
# Comment out the HTTPAuth wrapper
#site = server.Site(rsrc)
port = config.get('XMLRPCServer', 'transport_port')
privateKey = config.get('XMLRPCServer', 'sslprivateKey')
certKey = config.get('XMLRPCServer', 'sslcertKey')
s = strports.service('ssl:'+port+':privateKey='+privateKey+':certKey='+certKey, channel.HTTPFactory(site))
return s