import CryptoBox import WebInterfaceDataset import re import Plugins from CryptoBoxExceptions import * import cherrypy class WebInterfacePlugins: def __init__(self, log, plugins, handler_func): for plugin in plugins.getPlugins(): if not plugin: continue plname = plugin.getName() log.info("Plugin '%s' loaded" % plname) ## this should be the "easiest" way to expose all plugins as URLs setattr(self, plname, handler_func(plugin)) setattr(getattr(self, plname), "exposed", True) # TODO: check, if this really works - for now the "stream_response" feature seems to be broken #setattr(getattr(self, plname), "stream_respones", True) class WebInterfaceSites: ''' url2func = {'index':'show_status','doc':'show_doc','logs':'show_log'} ''' def __init__(self): import logging self.cbox = CryptoBox.CryptoBoxProps() self.log = logging.getLogger("CryptoBox") self.prefs = self.cbox.prefs self.__resetDataset() def __resetDataset(self): """this method has to be called at the beginning of every "site" action important: only at the beginning of an action (to not loose information) important: for _every_ "site" action (cherrypy is stateful) also take care for the plugins, as they also contain datasets """ self.pluginList = Plugins.PluginManager(self.cbox, self.prefs["Locations"]["PluginDir"]) self.plugins = WebInterfacePlugins(self.log, self.pluginList, self.return_plugin_action) ## publish the url "/system" as an alias for "/plugins" self.plugins.index = self.system self.dataset = WebInterfaceDataset.WebInterfaceDataset(self.cbox, self.prefs, self.pluginList.getPlugins()) def __check_config(self): #TODO: from now on a cryptobox is always configured return True def __check_init_running(self): #TODO: implement this check (is mkfs still running?) return False ## this is a function decorator to check authentication ## it has to be defined before any page definition requiring authentification def __requestAuth(self=None): def check_credentials(site): def _inner_wrapper(self, *args, **kargs): import base64 ## define a "non-allowed" function user, password = None, None try: resp = cherrypy.request.headers["Authorization"][6:] # ignore "Basic " (user, password) = base64.b64decode(resp).split(":",1) except KeyError: ## no "authorization" header was sent pass except TypeError: ## invalid base64 string pass except AttributeError: ## no cherrypy response header defined pass authDict = self.cbox.prefs.userDB["admins"] if user in authDict.keys(): if self.cbox.prefs.userDB.getDigest(password) == authDict[user]: ## ok: return the choosen page self.cbox.log.info("access granted for: %s" % user) return site(self, *args, **kargs) else: self.cbox.log.info("wrong password supplied for: %s" % user) else: self.cbox.log.info("unknown user: %s" % str(user)) ## wrong credentials: return "access denied" cherrypy.response.headers["WWW-Authenticate"] = '''Basic realm="Test-Arena"''' cherrypy.response.status = 401 return self.__render("access_denied") return _inner_wrapper return check_credentials ###################################################################### ## put real sites down here and don't forget to expose them at the end @cherrypy.expose def status(self, weblang=""): '''shows the current status of the box ''' self.__resetDataset() self.__setWebLang(weblang) if not self.__check_config(): self.dataset["Data.Warning"] = "NotInitialized" return self.__render("form_init") elif self.__check_init_running(): self.dataset["Data.Warning"] = "InitNotFinished" self.dataset["Data.Redirect.Action"] = "form_config" self.dataset["Data.Redirect.Delay"] = "30" return self.__render("empty") else: self.dataset["Data.Redirect.Delay"] = "60" return self.__render("show_status") @cherrypy.expose def doc(self,page="",weblang=""): '''prints the offline wikipage ''' import re self.__resetDataset() self.__setWebLang(weblang) ## check for invalid characters if page and not re.search(u'\W', page): self.dataset["Data.Doc.Page"] = page else: ## display this page as default help page self.dataset["Data.Doc.Page"] ="CryptoBoxUser" return self.__render("show_doc") @cherrypy.expose def system(self, weblang=""): self.__resetDataset() self.__setWebLang(weblang) return self.__render("form_system") @cherrypy.expose def index(self, weblang=""): self.__resetDataset() self.__setWebLang(weblang) return self.__render("show_status") @cherrypy.expose def show_volume(self, device="", weblang=""): self.__resetDataset() self.__setWebLang(weblang) if self.__setDevice(device): return self.__render("show_volume") else: if self.cbox.getContainerList(): return self.__render("show_volumes") else: return self.__render("show_status") @cherrypy.expose def show_volumes(self, weblang=""): self.__resetDataset() self.__setWebLang(weblang) return self.__render("show_volumes") def return_plugin_action(self, plugin): def handler(self, **args): self.__resetDataset() try: self.__setWebLang(args["weblang"]) del args["weblang"] except KeyError: pass ## check the device argument of volume plugins if "volume" in plugin.pluginCapabilities: try: ## initialize the dataset of the selected device if necessary if self.__setDevice(args["device"]): plugin.device = args["device"] self.dataset.setCurrentDiskState(plugin.device) else: return self.__render("show_status") except KeyError: return self.__render("show_status") else: ## the parameter 'device' exists - we have to remove it del args["device"] ## call the plugin handler nextTemplate = plugin.doAction(**args) ## for 'volume' plugins: reread the dataset of the current disk ## additionally: set the default template for plugins if "volume" in plugin.pluginCapabilities: self.dataset.setCurrentDiskState(plugin.device) if not nextTemplate: nextTemplate = "show_volume" else: self.dataset.setPluginData() if not nextTemplate: nextTemplate = "form_system" ## save the currently active plugin name self.dataset["Data.ActivePlugin"] = plugin.getName() return self.__render(nextTemplate, plugin) ## apply authentication? if plugin.isAuthRequired(): return lambda **args: self.__requestAuth()(handler)(self, **args) else: return lambda **args: handler(self, **args) ## test authentication @cherrypy.expose @__requestAuth def test(self, weblang=""): self.__resetDataset() self.__setWebLang(weblang) return "test passed" @cherrypy.expose def test_stream(self): """just for testing purposes - to check if the "stream_response" feature actually works - for now (September 02006) it does not seem to be ok""" import time yield "