2006-08-24 02:06:19 +02:00
import CryptoBox
import CryptoBoxWebserverSettings
import CryptoBoxWebserverRender
2006-09-05 17:03:16 +02:00
# RFC: is this global variable necessary? [l]
2006-08-24 02:06:19 +02:00
website = CryptoBoxWebserverRender . CryptoBoxWebserverRender ( )
2006-09-05 17:03:16 +02:00
# 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]
2006-08-24 02:06:19 +02:00
class CryptoBoxWebserverSites ( CryptoBox . CryptoBoxProps , CryptoBoxWebserverSettings . CryptoBoxWebserverSettings ) :
'''
url2func = { ' index ' : ' show_status ' , ' doc ' : ' show_doc ' , ' logs ' : ' show_log ' }
'''
2006-08-24 16:41:11 +02:00
def __prepare ( self , action = " show_status " ) :
''' handles stuff that all sites need as preparation
2006-08-24 02:06:19 +02:00
2006-08-24 16:41:11 +02:00
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 .
2006-08-25 09:37:52 +02:00
After that the corresponding site - method ( e . g . doc ) may set individual values .
2006-08-24 02:06:19 +02:00
'''
2006-08-24 16:41:11 +02:00
2006-08-25 09:37:52 +02:00
# RFC: the following line somehow implicitly calls 'setSettings(self, self)'
# should it be that way? [l]
2006-08-24 02:06:19 +02:00
self . setSettings ( self )
2006-08-24 16:41:11 +02:00
#self.settings
self . settings [ " Data.Action " ] = action
2006-08-24 02:06:19 +02:00
#TODO: check ssl status
2006-08-24 16:41:11 +02:00
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
2006-08-25 09:37:52 +02:00
sanitizing input automatically for all sites .
2006-08-30 21:56:37 +02:00
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 .
2006-08-25 09:37:52 +02:00
# RFC: why shouldn't it be called in __init__? [l]
2006-08-30 21:56:37 +02:00
there is no such thing like __init__ in cherrypy sites [ a ]
2006-09-05 17:03:16 +02:00
what about the unnamed place , where ' exposed ' attributes are set ? [ l ]
2006-08-25 09:37:52 +02:00
'''
2006-09-05 17:03:16 +02:00
# RFC: this dictionary is not sufficient for arbitrary text inputs (e.g.: names) or numbers [l]
2006-09-02 01:04:17 +02:00
niceparams = {
' weblang ' : [ " Settings.Language " , self . settings [ " Settings.AvailableLanguages " ] ] ,
' loglevel ' : [ " Log.Level " , ( ' ' , ' info ' , ' warn ' , ' debug ' , ' error ' ) ] ,
' type ' : [ " Data.Type " , ( ' reboot ' , ' poweroff ' ) ]
2006-08-24 16:41:11 +02:00
}
2006-09-02 01:04:17 +02:00
## check all given evil parameters against the nice ones
## set them to self.settings if accepted, otherwise do nothing
2006-08-24 16:41:11 +02:00
for evilkey in evilparams . keys ( ) :
2006-09-05 17:03:16 +02:00
# TODO: should be easier without the following for-loop
2006-09-02 01:04:17 +02:00
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 ] :
2006-09-05 17:03:16 +02:00
# RFC: isn't "self.settings" a non-obvious name for user input? [l]
2006-09-02 01:04:17 +02:00
self . settings [ niceparams [ nicekey ] [ 0 ] ] = evilparams [ nicekey ]
#self.log.warn(niceparams[nicekey][0])
#self.log.warn(evilparams[nicekey])
'''
## e.g. do this manually
2006-08-30 21:56:37 +02:00
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 " ]
2006-09-02 01:04:17 +02:00
if evilkey == " type " :
if evilparams [ " type " ] and evilparams [ " type " ] in niceparams [ " type " ] :
self . settings [ " Data.Type " ] = evilparams [ " type " ]
'''
2006-08-30 21:56:37 +02:00
return
2006-08-24 02:06:19 +02:00
2006-09-02 01:04:17 +02:00
def __isHDAvailable ( self ) :
2006-09-05 17:03:16 +02:00
#TODO: implement this
return True
2006-08-25 09:37:52 +02:00
2006-08-24 02:06:19 +02:00
def __check_config ( self ) :
2006-09-05 17:03:16 +02:00
#TODO: from now on a cryptobox is always configured
return True
2006-08-25 09:37:52 +02:00
2006-08-24 02:06:19 +02:00
def __check_init_running ( self ) :
2006-09-05 17:03:16 +02:00
#TODO: implement this check (is mkfs still running?)
return False
2006-08-24 02:06:19 +02:00
######################################################################
2006-08-25 09:37:52 +02:00
## put real sites down here and don't forget to expose them at the end
2006-08-24 02:06:19 +02:00
2006-08-24 16:41:11 +02:00
def logs ( self , loglevel = " " ) :
''' displays a HTML version of the logfile
2006-08-25 09:37:52 +02:00
The loglevel has to be set and nothing else , as we just log in english .
2006-09-05 17:03:16 +02:00
RFC : what does this mean ? We still have to save the current language - or not ? [ l ]
2006-08-25 09:37:52 +02:00
Be aware not to name this method just " log " as it seems to be a
reserved word .
2006-09-05 17:03:16 +02:00
# RFC: maybe it conflicts with CryptoBoxProps.log - which we inherited? [l]
2006-08-25 09:37:52 +02:00
'''
2006-08-24 02:06:19 +02:00
self . __prepare ( " show_log " )
2006-08-30 21:56:37 +02:00
self . __sanitize_input ( { " loglevel " : loglevel } )
2006-08-25 09:37:52 +02:00
self . settings [ " Data.Log " ] = " <br/> " . join ( self . getLogData ( lines = 30 , maxSize = 2000 ) )
2006-08-24 02:06:19 +02:00
return website . render ( self )
2006-09-05 17:03:16 +02:00
2006-08-24 16:41:11 +02:00
def status ( self , weblang = " " ) :
''' shows the current status of the box
'''
2006-08-24 02:06:19 +02:00
self . __prepare ( " show_status " )
2006-08-30 21:56:37 +02:00
self . __sanitize_input ( { " weblang " : weblang } )
2006-08-24 02:06:19 +02:00
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 )
2006-08-25 09:37:52 +02:00
2006-08-24 16:41:11 +02:00
def config ( self , weblang = " " ) :
pass
2006-08-24 02:06:19 +02:00
2006-08-25 09:37:52 +02:00
2006-09-05 17:03:16 +02:00
# TODO: add "doclang" - it is selected by each link in the doc pages
def doc ( self , page = " " , weblang = " " ) :
2006-08-24 16:41:11 +02:00
''' prints the offline wikipage
2006-09-05 17:03:16 +02:00
TODO : " action " is unnessessary , remove it from all html files in doc / html / [ de | en ] / *
2006-08-24 16:41:11 +02:00
'''
2006-08-24 02:06:19 +02:00
self . __prepare ( " show_doc " )
2006-09-02 01:04:17 +02:00
# TODO: single pagenames should be sanitized
self . __sanitize_input ( { " weblang " : weblang } )
2006-09-05 17:03:16 +02:00
# TODO: check the supplied page name for validity (is it text? pattern match?)
2006-08-24 16:41:11 +02:00
if page :
2006-08-24 02:06:19 +02:00
self . settings [ " Data.Doc.Page " ] = page
2006-08-27 10:38:48 +02:00
else :
2006-08-30 21:56:37 +02:00
## display this site as default helpsite
2006-08-24 02:06:19 +02:00
self . settings [ " Data.Doc.Page " ] = " CryptoBoxUser "
2006-08-30 21:56:37 +02:00
if len ( self . settings [ " Settings.AvailableDocLanguages " ] ) < 1 :
self . settings [ " Data.Error " ] = " NoDocumentation "
2006-09-05 17:03:16 +02:00
# TODO: what should be done, if there is an error?
2006-09-02 01:04:17 +02:00
## set doclang to weblang, otherwise the default weblang from the config will be used for doclang
2006-09-05 17:03:16 +02:00
# TODO: hard coded languages?
2006-09-02 01:04:17 +02:00
elif self . settings [ " Settings.Language " ] in ( " en " , " de " ) :
self . settings [ " Settings.DocLang " ] = self . settings [ " Settings.Language " ]
2006-09-05 17:03:16 +02:00
# TODO: missing 'else'?
2006-09-02 01:04:17 +02:00
2006-08-24 02:06:19 +02:00
return website . render ( self )
2006-08-25 09:37:52 +02:00
2006-09-02 01:04:17 +02:00
def system ( self , type = " " , weblang = " " ) :
2006-08-24 02:06:19 +02:00
self . __prepare ( " form_system " )
2006-09-02 01:04:17 +02:00
self . __sanitize_input ( { " type " : type , " weblang " : weblang } )
if type == " reboot " :
2006-08-24 02:06:19 +02:00
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 " )
2006-09-02 01:04:17 +02:00
elif type == " poweroff " :
2006-08-24 02:06:19 +02:00
self . settings [ " Data.Success " ] = " PowerOff "
self . log . info ( " TODO: call function for system shutdown " )
else :
2006-09-02 01:04:17 +02:00
self . log . warn ( " This shutdown-mode ( %s ) is not supplied. " % type )
2006-08-24 02:06:19 +02:00
return website . render ( self )
2006-08-25 09:37:52 +02:00
2006-08-24 02:06:19 +02:00
def index ( self ) :
self . __prepare ( " show_status " )
return website . render ( self )
2006-09-05 02:15:43 +02:00
def test ( self ) :
return " test passed "
2006-09-02 01:04:17 +02:00
def umount_do ( self ) :
if not __isHDAvailable ( ) :
pass
else :
pass
2006-08-24 02:06:19 +02:00
'''
2006-08-25 09:37:52 +02:00
## DONE: these functions are pythonized
2006-08-24 02:06:19 +02:00
#################### 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 " :
2006-08-27 10:38:48 +02:00
if not device :
self . log . debug ( " invalid device chosen: %s " device
2006-08-24 02:06:19 +02:00
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 " :
2006-08-27 10:38:48 +02:00
if device :
2006-08-24 02:06:19 +02:00
pass #TODO: is_encrypted = check_device_encryption(device)
2006-08-27 10:38:48 +02:00
else :
self . log . debug ( " invalid device chosen: %s " device
2006-08-24 02:06:19 +02:00
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
'''
2006-08-25 09:37:52 +02:00
2006-08-24 02:06:19 +02:00
############################################################################
## 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
2006-09-05 02:15:43 +02:00
test . exposed = True
2006-08-24 02:06:19 +02:00
2006-08-27 10:38:48 +02:00