#!/usr/bin/env python import re import CryptoBoxPreferences, CryptoBoxLogger, CryptoBoxContainer class CryptoBox: '''Get and set the properties of a CryptoBox''' def __init__(self): '''reset all variables to default settings''' self.cbxPrefs = CryptoBoxPreferences.Preferences() self.debug = CryptoBoxLogger.CryptoBoxLogger( self.cbxPrefs["debuglevel"], self.cbxPrefs["debugfacility"], self.cbxPrefs["logfile"] ) self.alloweddevices = self.csv2list(self.cbxPrefs["allowed_devices"]) self.containers = [] for device in self.allAvailablePartitions(): if self.deviceIsReallyAllowed(device): self.containers.append(CryptoBoxContainer.CryptoBoxContainer(device)) ############################ ### internal methods 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): #print "%s allowed for cbx" % a return True def allAvailablePartitions(self): '''retrieve a list of all available containers''' 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 [] ################################## ### public methods 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 containers of this cryptobox''' 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