cryptonas-branches/pythonrewrite/bin2/CryptoBox.py
2006-08-16 11:07:57 +00:00

118 lines
3.7 KiB
Python

#!/usr/bin/env python
'''
This is a secure fileserver with encrypted filesystem and webinterface.
It was originally written in bash/perl. Now a complete rewrite is in
progress. So things might be confusing here. Hopefully not for long.
:)
'''
import re
import CryptoBoxLogger
import CryptoBoxContainer
import CryptoBoxPreferences
CONFIG_FILE="cbx.conf"
class CryptoBoxProps:
'''Get and set the properties of a CryptoBox
This class returns the available _and_ allowed devices, which will
be used further.
'''
def __init__(self):
'''read config and fill class variables'''
print CONFIG_FILE
self.cbxPrefs = CryptoBoxPreferences.Preferences(CONFIG_FILE)
self.debug = CryptoBoxLogger.CryptoBoxLogger(
self.cbxPrefs["debuglevel"],
self.cbxPrefs["debugfacility"],
self.cbxPrefs["logfile"] )
self.alloweddevices = self.__csv2list(self.cbxPrefs["allowed_devices"])
def __csv2list(self, csvstring):
'''transform a csv preferences string into a list'''
commalist = csvstring.split(",") # split the csv by ","
list = []
for element in commalist:
list.append(element.strip()) # remove whitespaces
return list
def __deviceIsReallyAllowed(self, device):
'''return "true" if the given device is white-listed for being used as cryptobox container'''
for a in self.alloweddevices:
if re.search('^' + a, device):
return True
def __allAvailablePartitions(self):
'''retrieve a list of all available containers
TODO: if the code is not like a poem, write prosadocumentation ;)
'''
ret_list = []
try:
fpart = open("/proc/partitions", "r")
try:
line = fpart.readline()
while line:
p_details = line.split()
if (len(p_details) == 4):
(p_major, p_minor, p_size, p_device) = p_details
if re.search('^[0-9]*$', p_major) and re.search('^[0-9]*$', p_minor):
p_parent = re.sub('[1-9]?[0-9]$', '', p_device)
if p_parent == p_device:
if [e for e in ret_list if re.search('^' + p_parent + '[1-9]?[0-9]$', e)]:
"major partition - its children are already in the list"
pass
else:
"major partition - but there are no children for now"
ret_list.append(p_device)
else:
"minor partition - remove parent if necessary"
if p_parent in ret_list: ret_list.remove(p_parent)
ret_list.append(p_device)
line = fpart.readline()
finally:
fpart.close()
return ["/dev/" + e for e in ret_list]
except IOError:
self.debugMessage(
"Could not read /proc/partitions",
CryptoBoxLogger.DebugLevels["warn"])
return []
def debugMessage(self, level, text):
'''print a debug message to the previously choosen debugging facility'''
self.debug.printMessage(level,text)
def getContainerList(self):
'''return a list of all actual available containers of this cryptobox'''
self.containers = []
for device in self.__allAvailablePartitions():
if self.__deviceIsReallyAllowed(device):
self.containers.append(CryptoBoxContainer.CryptoBoxContainer(device))
try:
return self.containers[:]
except AttributeError:
return None
def getConfigValue(self, key):
'''return a tuple of key+value from the configfile'''
return (key, self.cbxPrefs[key])
def setConfigValue(self, key, value):
'''save the strings key+value in configfile'''
self.cbxPrefs[key]=value
if __name__ == "__main__":
'''
Start the Cryptobox with: `python CryptoBox.py`
'''
cbprops = CryptoBoxProps()
#print "Allowed_Devices: %s" % (cb.getConfigValue("allowed_devices"), )
#print "non-existing: %s" % (cb.getConfigValue("alowed_devices"), )
print [e.device for e in cbprops.getContainerList()]
print "Config %s" % str(cbprops.getConfigValue("logfile"))
#cbprops.setConfigValue("foo","bar")