import CryptoBox import CryptoBoxWebserverSettings import CryptoBoxWebserverRender # RFC: is this global variable necessary? [l] website = CryptoBoxWebserverRender.CryptoBoxWebserverRender() # RFC: where should we put the information gathering? (available harddisks, current volume info, ...) - selectively for every "site" or always (as before)? [l] # RFC: is it necessary to inherit these both classes? # for clarity they should be just instanciated - or not? [l] class CryptoBoxWebserverSites(CryptoBox.CryptoBoxProps, CryptoBoxWebserverSettings.CryptoBoxWebserverSettings): ''' url2func = {'index':'show_status','doc':'show_doc','logs':'show_log'} ''' def __prepare(self, action="show_status"): '''handles stuff that all sites need as preparation The default site to render is 'show_status'. Self.settings is filled by the following methodcall thus every rendered site will get actual values from the configfile. After that the corresponding site-method (e.g. doc) may set individual values. ''' # RFC: the following line somehow implicitly calls 'setSettings(self, self)' # should it be that way? [l] self.setSettings(self) #self.settings self.settings["Data.Action"] = action #TODO: check ssl status def __sanitize_input(self, evilparams): '''mistrusts every given parameter and wipes crap out Every single site method has to call this before even looking at url-given parameters. This has to be called manually, since I don't see any other way of sanitizing input automatically for all sites. To take full effect it is good style not to use any given parameter for any website. Instead use "settings.["..." if it isn't already defined. Then define it in here, after carefully checking. # RFC: why shouldn't it be called in __init__? [l] there is no such thing like __init__ in cherrypy sites [a] what about the unnamed place, where 'exposed' attributes are set? [l] ''' # RFC: this dictionary is not sufficient for arbitrary text inputs (e.g.: names) or numbers [l] niceparams = { 'weblang': ["Settings.Language", self.settings["Settings.AvailableLanguages"]], 'loglevel': ["Log.Level", ('','info', 'warn', 'debug', 'error')], 'type': ["Data.Type", ('reboot', 'poweroff')] } ## check all given evil parameters against the nice ones ## set them to self.settings if accepted, otherwise do nothing for evilkey in evilparams.keys(): # TODO: should be easier without the following for-loop for nicekey in niceparams.keys(): if evilkey == nicekey: #self.log.warn(niceparams[nicekey][0]) #self.log.warn(niceparams[nicekey][1]) if evilparams[nicekey] and evilparams[nicekey] in niceparams[nicekey][1]: # RFC: isn't "self.settings" a non-obvious name for user input? [l] self.settings[niceparams[nicekey][0]] = evilparams[nicekey] #self.log.warn(niceparams[nicekey][0]) #self.log.warn(evilparams[nicekey]) ''' ## e.g. do this manually if evilkey == "weblang": if evilparams["weblang"] and evilparams["weblang"] in niceparams["weblang"]: self.settings["Settings.Language"] = evilparams["weblang"] if evilkey == "loglevel": if evilparams["loglevel"] and evilparams["loglevel"] in niceparams["loglevel"]: self.settings["Log.Level"] = evilparams["loglevel"] if evilkey == "type": if evilparams["type"] and evilparams["type"] in niceparams["type"]: self.settings["Data.Type"] = evilparams["type"] ''' return def __isHDAvailable(self): #TODO: implement this return True 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 ###################################################################### ## put real sites down here and don't forget to expose them at the end def logs(self, loglevel=""): '''displays a HTML version of the logfile The loglevel has to be set and nothing else, as we just log in english. RFC: what does this mean? We still have to save the current language - or not? [l] Be aware not to name this method just "log" as it seems to be a reserved word. # RFC: maybe it conflicts with CryptoBoxProps.log - which we inherited? [l] ''' self.__prepare("show_log") self.__sanitize_input({"loglevel":loglevel}) self.settings["Data.Log"] = "
".join(self.getLogData(lines=30, maxSize=2000)) return website.render(self) def status(self, weblang=""): '''shows the current status of the box ''' self.__prepare("show_status") self.__sanitize_input({"weblang":weblang}) if not self.__check_config(): self.settings["Data.Warning"] = "NotInitialized" self.settings["Data.Action"] = "form_init" elif self.__check_init_running(): self.settings["Data.Warning"] = "InitNotFinished" self.settings["Data.Action"] = "empty" self.settings["Data.Redirect.Action"] = "form_config" self.settings["Data.Redirect.Delay"] = "30" else: self.settings["Data.Action"] = "show_status" self.settings["Data.Redirect.Delay"] = "60" return website.render(self) def config(self,weblang=""): pass # TODO: add "doclang" - it is selected by each link in the doc pages def doc(self,page="",weblang=""): '''prints the offline wikipage TODO: "action" is unnessessary, remove it from all html files in doc/html/[de|en]/* ''' self.__prepare("show_doc") # TODO: single pagenames should be sanitized self.__sanitize_input({"weblang":weblang}) # TODO: check the supplied page name for validity (is it text? pattern match?) if page: self.settings["Data.Doc.Page"] = page else: ## display this site as default helpsite self.settings["Data.Doc.Page"] ="CryptoBoxUser" if len(self.settings["Settings.AvailableDocLanguages"]) < 1: self.settings["Data.Error"] = "NoDocumentation" # TODO: what should be done, if there is an error? ## set doclang to weblang, otherwise the default weblang from the config will be used for doclang # TODO: hard coded languages? elif self.settings["Settings.Language"] in ("en","de"): self.settings["Settings.DocLang"] = self.settings["Settings.Language"] # TODO: missing 'else'? return website.render(self) def system(self, type="", weblang=""): self.__prepare("form_system") self.__sanitize_input({"type":type,"weblang":weblang}) if type == "reboot": self.settings["Data.Success"] = "ReBoot" self.settings["Data.Redirect.Action"] = "show_status" self.settings["Data.Redirect.Delay"] = "180" self.log.info("TODO: call function for system reboot") elif type == "poweroff": self.settings["Data.Success"] = "PowerOff" self.log.info("TODO: call function for system shutdown") else: self.log.warn("This shutdown-mode (%s) is not supplied." % type) return website.render(self) def index(self): self.__prepare("show_status") return website.render(self) def test(self): return "test passed" def umount_do(self): if not __isHDAvailable(): pass else: pass ''' ## DONE: these functions are pythonized #################### show_log ####################### ##################### doc ############################ ##################### poweroff ###################### ##################### reboot ######################## ## but there are even more TODO #-------------------------------------------------------# # here you may define all cases that require a harddisk # #-------------------------------------------------------# ################ umount_do ####################### elif action == "unmount_do": if not device: self.log.debug("invalid device chosen: %s" device settings["Data.Warning"] = "InvalidDevice" settings["Data.Action"] = "empty" elif not True: #TODO: replace True with check_config() settings["Data.Warning"] = "NotInitialized" settings["Data.Action"] = "form_init" elif True: #TODO: replace True with check_init_running() settings["Data.Warning"] = "InitNotFinished" settings["Data.Action"] = "empty" settings["Data.Redirect.Action"] = "form_config" settings["Data.Redirect.Delay"] = "30" elif not True: #TODO: replace True with check_mounted(device) settings["Data.Warning"] = "NotMounted" settings["Data.Action"] = "show_volume" else: #unmount #TODO: replace this line with umount_vol(device) if True: #TODO: replace True with check_mounted(device) settings["Data.Warning"] = "UmountFailed" settings["Data.Action"] = "show_volume" else: settings["Data.Action"] = "show_volume" ################ mount_do ######################## elif action == "mount_do": if device: pass #TODO: is_encrypted = check_device_encryption(device) else: self.log.debug("invalid device chosen: %s" device settings["Data.Warning"] = "InvalidDevice" settings["Data.Action"] = "empty" elif not True: #TODO: replace True with check_config() settings["Data.Warning"] = "NotInitialized" settings["Data.Action"] = "form_init" #at cryptobox.pl line 568 ''' ############################################################################ ## to make the sites visible through the webserver they must be exposed here index.exposed = True doc.exposed = True logs.exposed = True system.exposed = True status.exposed = True test.exposed = True