From 7c4d25da435a3e885ab1348162e05d8a3f151935 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 6 Jun 2008 16:06:05 +0000 Subject: [PATCH] Start work on XML-RPC functionality * provide an XMLRPC server start script (bin/gvaserver) (addresses #5) * encapsulate XMLRPC visible code in gnuviechadmin.xmlrpc.XMLRPCFacade * start implementation of authkit compatible Users implementation gnuviechadmin.xmlrpc.users.GVAUsers (addresses #17) git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/trunk@256 a67ec6bc-e5d5-0310-a910-815c51eb3124 --- bin/gvaserver | 58 +++++++++++++++++++ gnuviechadmin/xmlrpc/XMLRPCFacade.py | 31 ++++++++++ gnuviechadmin/xmlrpc/__init__.py | 28 +++++++++ gnuviechadmin/xmlrpc/users.py | 87 ++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100755 bin/gvaserver create mode 100644 gnuviechadmin/xmlrpc/XMLRPCFacade.py create mode 100644 gnuviechadmin/xmlrpc/__init__.py create mode 100644 gnuviechadmin/xmlrpc/users.py diff --git a/bin/gvaserver b/bin/gvaserver new file mode 100755 index 0000000..d031a82 --- /dev/null +++ b/bin/gvaserver @@ -0,0 +1,58 @@ +#!/usr/bin/python +# -*- python -*- +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +import sys, os, logging.config +from gnuviechadmin.util.passwordutils import md5_crypt_password + +logcfgs = ('gnuviechadmin/logging.cfg', '/etc/gnuviechadmin/logging.cfg', + os.path.expanduser('~/.gva-logging.cfg')) +for cfg in [x for x in logcfgs if os.path.exists(x)]: + logging.config.fileConfig(cfg) + +def usage(): + print """%s + +where host and port specify the connection parameters. Host is the +hostname or ip address to listen on. +""" % sys.argv[0] + +def main(): + if (sys.argv.__len__() < 3): + usage() + sys.exit() + from SimpleXMLRPCServer import SimpleXMLRPCServer + server = SimpleXMLRPCServer(("localhost", 9999), allow_none=True) + server.register_introspection_functions() + + from gnuviechadmin.xmlrpc import XMLRPCFacade + from gnuviechadmin.xmlrpc.users import \ + ClientUserProvider, MailuserUserProvider, SysuserUserProvider + server.register_instance(XMLRPCFacade('data', + [ClientUserProvider, + MailuserUserProvider, + SysuserUserProvider], + md5_crypt_password)) + server.serve_forever() + +if __name__ == '__main__': + main() diff --git a/gnuviechadmin/xmlrpc/XMLRPCFacade.py b/gnuviechadmin/xmlrpc/XMLRPCFacade.py new file mode 100644 index 0000000..8591fd0 --- /dev/null +++ b/gnuviechadmin/xmlrpc/XMLRPCFacade.py @@ -0,0 +1,31 @@ +# -*- python -*- +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +"""This file defines a facade for exporting gnuviechadmin +functionality via XMLRPC.""" + +from gnuviechadmin.xmlrpc.users import GVAUsers + +class XMLRPCFacade(GVAUsers): + """This class provides access to selected gnuviechadmin + functionality for use via XMLRPC.""" + pass diff --git a/gnuviechadmin/xmlrpc/__init__.py b/gnuviechadmin/xmlrpc/__init__.py new file mode 100644 index 0000000..be77719 --- /dev/null +++ b/gnuviechadmin/xmlrpc/__init__.py @@ -0,0 +1,28 @@ +# -*- python -*- +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +"""This is the gnuviechadmin.xmlrpc package. + +This package provides modules for the XMLRPC interface of the +gnuviechadmin server administration suite.""" + +from XMLRPCFacade import XMLRPCFacade diff --git a/gnuviechadmin/xmlrpc/users.py b/gnuviechadmin/xmlrpc/users.py new file mode 100644 index 0000000..076d23d --- /dev/null +++ b/gnuviechadmin/xmlrpc/users.py @@ -0,0 +1,87 @@ +# -*- python -*- +# -*- coding: utf-8 -*- +from authkit.users import Users, AuthKitNoSuchUserError +import logging + +log = logging.getLogger(__name__) + +class UserProvider(Users): + def _get_user(self, username, password, role): + return { + 'username' : username, + 'group' : None, + 'password' : password, + 'roles' : [role] + } + +class ClientUserProvider(UserProvider): + def user(self, username): + print 'checking %s' % username + if username == 'dummy': + return self._get_user(username, username, 'client') + return AuthKitNoSuchUserError() + + def list_roles(self): + return ['client'] + +class MailuserUserProvider(UserProvider): + def user(self, username): + raise AuthKitNoSuchUserError() + + def list_roles(self): + return ['mailuser'] + +class SysuserUserProvider(UserProvider): + def user(self, username): + raise AuthKitNoSuchUserError() + + def list_roles(self): + return ['sysuser'] + +class GVAUsers(Users): + def __init__(self, data, userproviders = [], encrypt = None): + """Initialize the GVAXMLRPCUsers instance.""" + Users.__init__(self, data, encrypt) + self.userproviders = [prov(self.data) for prov in userproviders] + + def list_roles(self): + """Returns a lowercase list of all role names ordered + alphabetically.""" + roles = [] + for prov in self.userproviders: + for role in prov.list_roles(): + if not role in roles: + roles.append(role) + roles.sort() + return roles + + def role_exists(self, role): + """Returns ``True`` if the role exists, ``False`` + otherwise. Roles are case insensitive.""" + for prov in self.userproviders: + if prov.role_exists(role): + return True + return False + + def user(self, username): + """Returns a dictionary in the following format: + + .. code-block :: Python + + { + 'username': username, + 'group': group, + 'password': password, + 'roles': [role1,role2,role3... etc] + } + + The role names are ordered alphabetically + Raises an exception if the user doesn't exist.""" + for prov in self.userproviders: + try: + return prov.user(username) + except Exception, e: + print e + log.debug("Backend %s didn't find user %s" % (backend, + username)) + raise AuthKitNoSuchUserError()