@ -8,12 +8,17 @@ except:
raise CryptoBoxExceptions . CBEnvironmentError ( " couldn ' t import ' configobj ' ! Try ' apt-get install python-configobj ' . " )
class CryptoBoxSettings :
CONF_LOCATIONS = [
" ./cryptobox.conf " ,
" ~/.cryptobox.conf " ,
" /etc/cryptobox/cryptobox.conf " ]
NAMEDB_FILE = " cryptobox_names.db "
PLUGINCONF_FILE = " cryptobox_plugins.conf "
USERDB_FILE = " cryptobox_users.db "
def __init__ ( self , config_file = None ) :
@ -25,8 +30,38 @@ class CryptoBoxSettings:
self . __configureLogHandler ( )
self . __checkUnknownPreferences ( )
self . nameDB = self . __getNameDatabase ( )
self . pluginConf = self . __getPluginConfig ( )
self . userDB = self . __getUserDB ( )
self . misc_files = self . __getMiscFiles ( )
def write ( self ) :
"""
write all local setting files including the content of the " misc " subdirectory
"""
ok = True
try :
self . nameDB . write ( )
except IOError :
self . log . warn ( " could not save the name database " )
ok = False
try :
self . pluginConf . write ( )
except IOError :
self . log . warn ( " could not save the plugin configuration " )
ok = False
try :
self . userDB . write ( )
except IOError :
self . log . warn ( " could not save the user database " )
ok = False
for misc_file in self . misc_files :
if not misc_file . save ( ) :
self . log . warn ( " could not save a misc setting file ( %s ) " % misc_file . filename )
ok = False
return ok
def __getitem__ ( self , key ) :
""" redirect all requests to the ' prefs ' attribute """
return self . prefs [ key ]
@ -92,12 +127,12 @@ class CryptoBoxSettings:
def __getNameDatabase ( self ) :
try :
try :
nameDB_file = self . prefs [ " Locations " ] [ " NameDatabase" ]
nameDB_file = os . path . join ( self . prefs [ " Locations " ] [ " SettingsDir" ] , self . NAMEDB_FILE )
except KeyError :
raise CryptoBoxExceptions . CBConfigUndefinedError ( " Locations " , " NameDatabase " )
raise CryptoBoxExceptions . CBConfigUndefinedError ( " Locations " , " SettingsDir " )
except SyntaxError :
raise CryptoBoxExceptions . CBConfigInvalidValueError ( " Locations " , " NameDatabase " , nameDB_file , " failed to interprete the filename of the name database correctly " )
## create nameDB i s necessary
raise CryptoBoxExceptions . CBConfigInvalidValueError ( " Locations " , " SettingsDir " , nameDB_file , " failed to interprete the filename of the name database correctly (%s ) " % nameDB_file )
## create nameDB i f necessary
if os . path . exists ( nameDB_file ) :
nameDB = configobj . ConfigObj ( nameDB_file )
else :
@ -108,6 +143,63 @@ class CryptoBoxSettings:
return nameDB
def __getPluginConfig ( self ) :
import StringIO
plugin_rules = StringIO . StringIO ( self . pluginValidationSpec )
try :
try :
pluginConf_file = os . path . join ( self . prefs [ " Locations " ] [ " SettingsDir " ] , self . PLUGINCONF_FILE )
except KeyError :
raise CryptoBoxExceptions . CBConfigUndefinedError ( " Locations " , " SettingsDir " )
except SyntaxError :
raise CryptoBoxExceptions . CBConfigInvalidValueError ( " Locations " , " SettingsDir " , pluginConf_file , " failed to interprete the filename of the plugin config file correctly ( %s ) " % pluginConf_file )
## create pluginConf_file if necessary
if os . path . exists ( pluginConf_file ) :
pluginConf = configobj . ConfigObj ( pluginConf_file , configspec = plugin_rules )
else :
pluginConf = configobj . ConfigObj ( pluginConf_file , configspec = plugin_rules , create_empty = True )
## validate and convert values according to the spec
pluginConf . validate ( validate . Validator ( ) )
## check if pluginConf_file file was created successfully?
if not os . path . exists ( pluginConf_file ) :
raise CryptoBoxExceptions . CBEnvironmentError ( " failed to create plugin configuration file ( %s ) " % pluginConf_file )
return pluginConf
def __getUserDB ( self ) :
import StringIO , sha
userDB_rules = StringIO . StringIO ( self . userDatabaseSpec )
try :
try :
userDB_file = os . path . join ( self . prefs [ " Locations " ] [ " SettingsDir " ] , self . USERDB_FILE )
except KeyError :
raise CryptoBoxExceptions . CBConfigUndefinedError ( " Locations " , " SettingsDir " )
except SyntaxError :
raise CryptoBoxExceptions . CBConfigInvalidValueError ( " Locations " , " SettingsDir " , userDB_file , " failed to interprete the filename of the users database file correctly ( %s ) " % userDB_file )
## create userDB_file if necessary
if os . path . exists ( userDB_file ) :
userDB = configobj . ConfigObj ( userDB_file , configspec = userDB_rules )
else :
userDB = configobj . ConfigObj ( userDB_file , configspec = userDB_rules , create_empty = True )
## validate and set default value for "admin" user
userDB . validate ( validate . Validator ( ) )
## check if userDB file was created successfully?
if not os . path . exists ( userDB_file ) :
raise CryptoBoxExceptions . CBEnvironmentError ( " failed to create user database file ( %s ) " % userDB_file )
## define password hash function - never use "sha" directly - SPOT
userDB . getDigest = lambda password : sha . new ( password ) . hexdigest ( )
return userDB
def __getMiscFiles ( self ) :
misc_dir = os . path . join ( self . prefs [ " Locations " ] [ " SettingsDir " ] , " misc " )
if ( not os . path . isdir ( misc_dir ) ) or ( not os . access ( misc_dir , os . X_OK ) ) :
return [ ]
return [ MiscConfigFile ( os . path . join ( misc_dir , f ) , self . log )
for f in os . listdir ( misc_dir )
if os . path . isfile ( os . path . join ( misc_dir , f ) ) ]
def __getConfigFileName ( self , config_file ) :
# search for the configuration file
import types
@ -168,7 +260,7 @@ ConfigVolumeLabel = string(min=1,default="cbox_config")
[ Locations ]
MountParentDir = directoryExists ( default = " /var/cache/cryptobox/mnt " )
NameDatabase = fileWriteable ( default = " /var/cache/cryptobox/volumen_names.db " )
SettingsDir = directoryExists ( default = " /var/cache/cryptobox/settings " )
TemplateDir = directoryExists ( default = " /usr/share/cryptobox/template " )
LangDir = directoryExists ( default = " /usr/share/cryptobox/lang " )
DocDir = directoryExists ( default = " /usr/share/doc/cryptobox/html " )
@ -194,6 +286,17 @@ super = fileExecutable(default="/usr/bin/super")
CryptoBoxRootActions = string ( min = 1 )
"""
pluginValidationSpec = """
[ __many__ ]
enabled = boolean ( default = None )
requestAuth = boolean ( default = None )
rank = integer ( default = None )
"""
userDatabaseSpec = """
[ admins ]
admin = string ( default = d033e22ae348aeb5660fc2140aec35850c4da997 )
"""
class CryptoBoxSettingsValidator ( validate . Validator ) :
@ -234,4 +337,46 @@ class CryptoBoxSettingsValidator(validate.Validator):
return file_path
raise validate . VdtValueError ( " %s (directory does not exist) " % value )
return file_path
class MiscConfigFile :
maxSize = 20480
def __init__ ( self , filename , logger ) :
self . filename = filename
self . log = logger
self . load ( )
def load ( self ) :
fd = open ( self . filename , " rb " )
## limit the maximum size
self . content = fd . read ( self . maxSize )
if fd . tell ( ) == self . maxSize :
self . log . warn ( " file in misc settings directory ( %s ) is bigger than allowed ( %s ) " % ( self . filename , self . maxSize ) )
fd . close ( )
def save ( self ) :
save_dir = os . path . dirname ( self . filename )
## create the directory, if necessary
if not os . path . isdir ( save_dir ) :
try :
os . mkdir ( save_dir )
except IOError :
return False
## save the content of the file
try :
fd = open ( self . filename , " wb " )
except IOError :
return False
try :
fd . write ( self . content )
fd . close ( )
return True
except IOError :
fd . close ( )
return False