2006-08-23 15:27:25 +02:00
|
|
|
#!/usr/bin/env python2.4
|
|
|
|
|
2006-08-25 09:37:52 +02:00
|
|
|
## check python version
|
2006-08-23 15:27:25 +02:00
|
|
|
import sys
|
|
|
|
(ver_major, ver_minor, ver_sub, ver_desc, ver_subsub) = sys.version_info
|
|
|
|
if (ver_major < 2) or ((ver_major == 2) and (ver_minor < 4)):
|
|
|
|
sys.stderr.write("You need a python version >= 2.4\nCurrent version is:\n %s\n" % sys.version)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
import subprocess
|
2006-08-16 20:17:16 +02:00
|
|
|
import os
|
|
|
|
import re
|
2006-08-22 08:55:07 +02:00
|
|
|
import logging
|
2006-09-26 10:39:43 +02:00
|
|
|
from CryptoBoxExceptions import *
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
"""exceptions:
|
|
|
|
VolumeIsActive
|
|
|
|
NameActivelyUsed
|
2006-08-17 12:38:05 +02:00
|
|
|
InvalidName
|
2006-08-16 20:17:16 +02:00
|
|
|
InvalidPassword
|
|
|
|
InvalidType
|
2006-08-17 12:38:05 +02:00
|
|
|
CreateError
|
2006-08-16 20:17:16 +02:00
|
|
|
MountError
|
|
|
|
ChangePasswordError
|
|
|
|
"""
|
|
|
|
|
2006-08-16 09:17:44 +02:00
|
|
|
class CryptoBoxContainer:
|
|
|
|
|
2006-08-16 20:17:16 +02:00
|
|
|
Types = {
|
|
|
|
"unused":0,
|
|
|
|
"plain":1,
|
|
|
|
"luks":2,
|
|
|
|
"swap":3}
|
|
|
|
|
|
|
|
|
|
|
|
__fsTypes = {
|
|
|
|
"plain":["ext3", "ext2", "vfat", "reiser"],
|
|
|
|
"swap":["swap"]}
|
2006-08-25 09:37:52 +02:00
|
|
|
# TODO: more filesystem types? / check 'reiser'
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
__dmDir = "/dev/mapper"
|
|
|
|
|
|
|
|
|
2006-08-17 12:38:05 +02:00
|
|
|
def __init__(self, device, cbox):
|
2006-08-16 09:17:44 +02:00
|
|
|
self.device = device
|
2006-08-16 20:17:16 +02:00
|
|
|
self.cbox = cbox
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log = logging.getLogger("CryptoBox")
|
2006-09-25 14:22:41 +02:00
|
|
|
self.resetObject()
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
def setName(self, new_name):
|
|
|
|
if new_name == self.name: return
|
|
|
|
if self.isMounted():
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBVolumeIsActive("the container must be inactive during renaming")
|
2006-08-17 12:38:05 +02:00
|
|
|
if not re.search(r'^[a-zA-Z0-9_\.\- ]+$', new_name):
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidName("the supplied new name contains illegal characters")
|
2006-08-16 20:17:16 +02:00
|
|
|
"check for active partitions with the same name"
|
|
|
|
prev_name_owner = self.cbox.getContainerList(filterName=new_name)
|
|
|
|
if prev_name_owner:
|
|
|
|
for a in prev_name_owner:
|
|
|
|
if a.isMounted():
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBNameActivelyUsed("the supplied new name is already in use for an active partition")
|
2006-10-09 18:44:35 +02:00
|
|
|
if not self.cbox.setNameForUUID(self.uuid, new_name):
|
|
|
|
raise CBContainerError("failed to change the volume name for unknown reasons")
|
2006-08-16 20:17:16 +02:00
|
|
|
self.name = new_name
|
|
|
|
|
|
|
|
|
|
|
|
def getDevice(self):
|
|
|
|
return self.device
|
|
|
|
|
|
|
|
|
|
|
|
def getType(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
|
|
|
|
def isMounted(self):
|
|
|
|
return os.path.ismount(self.__getMountPoint())
|
2006-08-24 09:36:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getCapacity(self):
|
|
|
|
"""return the current capacity state of the volume
|
|
|
|
|
2006-11-02 14:55:31 +01:00
|
|
|
the volume may not be mounted
|
2006-08-24 09:36:47 +02:00
|
|
|
the result is a tuple of values in megabyte:
|
|
|
|
(size, available, used)
|
|
|
|
"""
|
|
|
|
info = os.statvfs(self.__getMountPoint())
|
|
|
|
return (
|
|
|
|
int(info.f_bsize*info.f_blocks/1024/1024),
|
|
|
|
int(info.f_bsize*info.f_bavail/1024/1024),
|
|
|
|
int(info.f_bsize*(info.f_blocks-info.f_bavail)/1024/1024))
|
2006-11-02 14:55:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def getSize(self):
|
|
|
|
"""return the size of the block device (_not_ of the filesystem)
|
|
|
|
|
|
|
|
the result is a value in megabyte
|
|
|
|
an error is indicated by "-1"
|
|
|
|
"""
|
|
|
|
import CryptoBoxTools
|
|
|
|
return CryptoBoxTools.getBlockDeviceSize(self.device)
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
2006-09-25 14:22:41 +02:00
|
|
|
def resetObject(self):
|
|
|
|
""" recheck the information about this container
|
|
|
|
this is especially useful after changing the type via 'create' """
|
|
|
|
self.uuid = self.__getUUID()
|
|
|
|
self.type = self.__getTypeOfPartition()
|
|
|
|
self.name = self.__getNameOfContainer()
|
|
|
|
if self.type == self.Types["luks"]:
|
|
|
|
self.mount = self.__mountLuks
|
|
|
|
self.umount = self.__umountLuks
|
2006-10-11 17:51:28 +02:00
|
|
|
elif self.type == self.Types["plain"]:
|
2006-09-25 14:22:41 +02:00
|
|
|
self.mount = self.__mountPlain
|
|
|
|
self.umount = self.__umountPlain
|
|
|
|
|
|
|
|
|
2006-08-16 20:17:16 +02:00
|
|
|
def create(self, type, password=None):
|
|
|
|
if type == self.Types["luks"]:
|
|
|
|
self.__createLuks(password)
|
2006-09-25 14:22:41 +02:00
|
|
|
self.resetObject()
|
2006-08-16 20:17:16 +02:00
|
|
|
return
|
|
|
|
if type == self.Types["plain"]:
|
|
|
|
self.__createPlain()
|
2006-09-25 14:22:41 +02:00
|
|
|
self.resetObject()
|
2006-08-16 20:17:16 +02:00
|
|
|
return
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidType("invalid container type (%d) supplied" % (type, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def changePassword(self, oldpw, newpw):
|
|
|
|
if self.type != self.Types["luks"]:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidType("changing of password is possible only for luks containers")
|
2006-08-16 20:17:16 +02:00
|
|
|
if not oldpw:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidPassword("no old password supplied for password change")
|
2006-08-16 20:17:16 +02:00
|
|
|
if not newpw:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidPassword("no new password supplied for password change")
|
2006-08-16 20:17:16 +02:00
|
|
|
"return if new and old passwords are the same"
|
|
|
|
if oldpw == newpw: return
|
|
|
|
if self.isMounted():
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBVolumeIsActive("this container is currently active")
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
"remove any potential open luks mapping"
|
|
|
|
self.__umountLuks()
|
|
|
|
"create the luks header"
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = subprocess.PIPE,
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"cryptsetup",
|
2006-08-16 20:17:16 +02:00
|
|
|
"luksAddKey",
|
2006-08-23 15:27:25 +02:00
|
|
|
self.device,
|
|
|
|
"--batch-mode"])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.stdin.write("%s\n%s" % (oldpw, newpw))
|
|
|
|
(output, errout) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not add a new luks key: %s - %s" % (output.strip(), errout.strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBChangePasswordError(errorMsg)
|
2006-08-25 09:37:52 +02:00
|
|
|
## retrieve the key slot we used for unlocking
|
2006-08-16 20:17:16 +02:00
|
|
|
keys_found = re.search(r'key slot (\d{1,3}) unlocked', output).groups()
|
|
|
|
if keys_found:
|
|
|
|
keyslot = int(keys_found[0])
|
|
|
|
else:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBChangePasswordError("could not get the old key slot")
|
2006-08-16 20:17:16 +02:00
|
|
|
"remove the old key"
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["cryptsetup"],
|
2006-08-16 20:17:16 +02:00
|
|
|
"--batch-mode",
|
|
|
|
"luksDelKey",
|
|
|
|
self.device,
|
|
|
|
"%d" % (keyslot, )])
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not remove the old luks key: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBChangePasswordError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
" ****************** internal stuff ********************* "
|
|
|
|
|
|
|
|
def __getNameOfContainer(self):
|
|
|
|
"retrieve the name of the container by querying the database"
|
|
|
|
def_name = self.cbox.getNameForUUID(self.uuid)
|
|
|
|
if def_name: return def_name
|
|
|
|
"there is no name defined for this uuid - we will propose a good one"
|
2006-09-08 13:02:27 +02:00
|
|
|
prefix = self.cbox.prefs["Main"]["DefaultVolumePrefix"]
|
2006-08-16 20:17:16 +02:00
|
|
|
unused_found = False
|
|
|
|
counter = 1
|
|
|
|
while not unused_found:
|
|
|
|
guess = prefix + str(counter)
|
|
|
|
if self.cbox.getUUIDForName(guess):
|
|
|
|
counter += 1
|
|
|
|
else:
|
|
|
|
unused_found = True
|
|
|
|
self.cbox.setNameForUUID(self.uuid, guess)
|
|
|
|
return guess
|
|
|
|
|
|
|
|
|
|
|
|
def __getUUID(self):
|
|
|
|
"""return UUID for luks partitions, ext2/3 and vfat filesystems"""
|
2006-09-14 14:33:01 +02:00
|
|
|
emergency_default = self.device.replace(os.path.sep, "_")
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell=False,
|
|
|
|
stdin=None,
|
|
|
|
stdout=subprocess.PIPE,
|
2006-09-14 14:33:01 +02:00
|
|
|
stderr=subprocess.PIPE,
|
2006-11-03 15:27:19 +01:00
|
|
|
args=[self.cbox.prefs["Programs"]["blkid"],
|
2006-08-16 20:17:16 +02:00
|
|
|
"-s", "UUID",
|
|
|
|
"-o", "value",
|
|
|
|
"-c", os.devnull,
|
|
|
|
"-w", os.devnull,
|
|
|
|
self.device])
|
|
|
|
proc.wait()
|
2006-08-17 12:38:05 +02:00
|
|
|
result = proc.stdout.read().strip()
|
2006-08-16 20:17:16 +02:00
|
|
|
if proc.returncode != 0:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("retrieving of partition type via 'blkid' failed: %s" % (proc.stderr.read().strip(), ))
|
2006-09-14 14:33:01 +02:00
|
|
|
return emergency_default
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
if result: return result
|
2006-09-14 14:33:01 +02:00
|
|
|
return emergency_default
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def __getTypeOfPartition(self):
|
|
|
|
"retrieve the type of the given partition (see CryptoBoxContainer.Types)"
|
|
|
|
if self.__isLuksPartition(): return self.Types["luks"]
|
|
|
|
typeOfPartition = self.__getTypeIdOfPartition()
|
|
|
|
if typeOfPartition in self.__fsTypes["plain"]:
|
|
|
|
return self.Types["plain"]
|
|
|
|
if typeOfPartition in self.__fsTypes["swap"]:
|
|
|
|
return self.Types["swap"]
|
|
|
|
return self.Types["unused"]
|
|
|
|
|
|
|
|
|
|
|
|
def __getTypeIdOfPartition(self):
|
|
|
|
"returns the type of the partition (see 'man blkid')"
|
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell=False,
|
|
|
|
stdin=None,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
2006-11-03 15:27:19 +01:00
|
|
|
args=[self.cbox.prefs["Programs"]["blkid"],
|
2006-08-16 20:17:16 +02:00
|
|
|
"-s", "TYPE",
|
|
|
|
"-o", "value",
|
|
|
|
"-c", os.devnull,
|
|
|
|
"-w", os.devnull,
|
|
|
|
self.device])
|
|
|
|
proc.wait()
|
2006-08-17 12:38:05 +02:00
|
|
|
output = proc.stdout.read().strip()
|
2006-08-16 20:17:16 +02:00
|
|
|
if proc.returncode != 0:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("retrieving of partition type via 'blkid' failed: %s" % (proc.stderr.read().strip(), ))
|
2006-08-16 20:17:16 +02:00
|
|
|
return None
|
|
|
|
devnull.close()
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def __isLuksPartition(self):
|
|
|
|
"check if the given device is a luks partition"
|
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = devnull,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["cryptsetup"],
|
2006-08-16 20:17:16 +02:00
|
|
|
"--batch-mode",
|
|
|
|
"isLuks",
|
|
|
|
self.device])
|
|
|
|
proc.wait()
|
|
|
|
devnull.close()
|
|
|
|
return proc.returncode == 0
|
|
|
|
|
|
|
|
|
|
|
|
def __getMountPoint(self):
|
|
|
|
"return the name of the mountpoint of this volume"
|
2006-09-08 13:02:27 +02:00
|
|
|
return os.path.join(self.cbox.prefs["Locations"]["MountParentDir"], self.name)
|
2006-08-16 20:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def __mountLuks(self, password):
|
|
|
|
"mount a luks partition"
|
|
|
|
if not password:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidPassword("no password supplied for luksOpen")
|
|
|
|
if self.isMounted(): raise CBVolumeIsActive("this container is already active")
|
2006-08-16 20:17:16 +02:00
|
|
|
self.__umountLuks()
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-17 12:38:05 +02:00
|
|
|
self.__cleanMountDirs()
|
|
|
|
if not os.path.exists(self.__getMountPoint()):
|
|
|
|
os.mkdir(self.__getMountPoint())
|
|
|
|
if not os.path.exists(self.__getMountPoint()):
|
|
|
|
errorMsg = "Could not create mountpoint (%s)" % (self.__getMountPoint(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBMountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = subprocess.PIPE,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"cryptsetup",
|
2006-08-16 20:17:16 +02:00
|
|
|
"luksOpen",
|
|
|
|
self.device,
|
2006-08-23 15:27:25 +02:00
|
|
|
self.name,
|
|
|
|
"--batch-mode"])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.stdin.write(password)
|
|
|
|
(output, errout) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not open the luks mapping: %s" % (errout.strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBMountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
2006-08-17 12:38:05 +02:00
|
|
|
stderr = subprocess.PIPE,
|
2006-08-16 20:17:16 +02:00
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"mount",
|
2006-08-16 20:17:16 +02:00
|
|
|
os.path.join(self.__dmDir, self.name),
|
|
|
|
self.__getMountPoint()])
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not mount the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBMountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __umountLuks(self):
|
|
|
|
"umount a luks partition"
|
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
if self.isMounted():
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
2006-08-17 12:38:05 +02:00
|
|
|
stderr = subprocess.PIPE,
|
2006-08-23 15:27:25 +02:00
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"umount",
|
|
|
|
self.__getMountPoint()])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not umount the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-10-09 18:44:35 +02:00
|
|
|
raise CBUmountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
if os.path.exists(os.path.join(self.__dmDir, self.name)):
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"cryptsetup",
|
2006-08-16 20:17:16 +02:00
|
|
|
"luksClose",
|
2006-08-23 15:27:25 +02:00
|
|
|
self.name,
|
|
|
|
"--batch-mode"])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not remove the luks mapping: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-10-09 18:44:35 +02:00
|
|
|
raise CBUmountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __mountPlain(self):
|
|
|
|
"mount a plaintext partition"
|
2006-09-26 10:39:43 +02:00
|
|
|
if self.isMounted(): raise CBVolumeIsActive("this container is already active")
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
self.__cleanMountDirs()
|
|
|
|
if not os.path.exists(self.__getMountPoint()):
|
|
|
|
os.mkdir(self.__getMountPoint())
|
2006-08-17 12:38:05 +02:00
|
|
|
if not os.path.exists(self.__getMountPoint()):
|
|
|
|
errorMsg = "Could not create mountpoint (%s)" % (self.__getMountPoint(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBMountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"mount",
|
2006-08-16 20:17:16 +02:00
|
|
|
self.device,
|
|
|
|
self.__getMountPoint()])
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not mount the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBMountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __umountPlain(self):
|
|
|
|
"umount a plaintext partition"
|
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-17 12:38:05 +02:00
|
|
|
if self.isMounted():
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"umount",
|
2006-08-17 12:38:05 +02:00
|
|
|
self.__getMountPoint()])
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not umount the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn(errorMsg)
|
2006-10-09 18:44:35 +02:00
|
|
|
raise CBUmountError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __createPlain(self):
|
|
|
|
"make a plaintext partition"
|
|
|
|
if self.isMounted():
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBVolumeIsActive("deactivate the partition before filesystem initialization")
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["mkfs-data"],
|
2006-08-16 20:17:16 +02:00
|
|
|
self.device])
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not create the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBCreateError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __createLuks(self, password):
|
|
|
|
"make a luks partition"
|
|
|
|
if not password:
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBInvalidPassword("no password supplied for new luks mapping")
|
2006-08-16 20:17:16 +02:00
|
|
|
if self.isMounted():
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBVolumeIsActive("deactivate the partition before filesystem initialization")
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull = None
|
|
|
|
try:
|
|
|
|
devnull = open(os.devnull, "w")
|
|
|
|
except IOError:
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.warn("Could not open %s" % (os.devnull, ))
|
2006-08-16 20:17:16 +02:00
|
|
|
"remove any potential open luks mapping"
|
|
|
|
self.__umountLuks()
|
|
|
|
"create the luks header"
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = subprocess.PIPE,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"cryptsetup",
|
|
|
|
"luksFormat",
|
|
|
|
self.device,
|
2006-08-16 20:17:16 +02:00
|
|
|
"--batch-mode",
|
2006-09-08 13:02:27 +02:00
|
|
|
"--cipher", self.cbox.prefs["Main"]["DefaultCipher"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"--iter-time", "2000"])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.stdin.write(password)
|
|
|
|
(output, errout) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not create the luks header: %s" % (errout.strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBCreateError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
"open the luks container for mkfs"
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = subprocess.PIPE,
|
|
|
|
stdout = devnull,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["super"],
|
|
|
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
2006-08-23 15:27:25 +02:00
|
|
|
"cryptsetup",
|
2006-08-16 20:17:16 +02:00
|
|
|
"luksOpen",
|
|
|
|
self.device,
|
2006-08-23 15:27:25 +02:00
|
|
|
self.name,
|
|
|
|
"--batch-mode"])
|
2006-08-16 20:17:16 +02:00
|
|
|
proc.stdin.write(password)
|
|
|
|
(output, errout) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not open the new luks mapping: %s" % (errout.strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBCreateError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
"make the filesystem"
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdin = None,
|
|
|
|
stdout = devnull,
|
2006-08-17 12:38:05 +02:00
|
|
|
stderr = subprocess.PIPE,
|
2006-08-16 20:17:16 +02:00
|
|
|
args = [
|
2006-11-03 15:27:19 +01:00
|
|
|
self.cbox.prefs["Programs"]["mkfs-data"],
|
2006-08-16 20:17:16 +02:00
|
|
|
os.path.join(self.__dmDir, self.name)])
|
|
|
|
proc.wait()
|
|
|
|
"remove the mapping - for every exit status"
|
|
|
|
self.__umountLuks()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
errorMsg = "Could not create the filesystem: %s" % (proc.stderr.read().strip(), )
|
2006-08-22 08:55:07 +02:00
|
|
|
self.log.error(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
"remove the luks mapping"
|
2006-09-26 10:39:43 +02:00
|
|
|
raise CBCreateError(errorMsg)
|
2006-08-16 20:17:16 +02:00
|
|
|
devnull.close()
|
|
|
|
|
2006-08-16 09:17:44 +02:00
|
|
|
|
2006-08-16 20:17:16 +02:00
|
|
|
def __cleanMountDirs(self):
|
|
|
|
""" remove all unnecessary subdirs of the mount parent directory
|
|
|
|
this should be called for every (u)mount """
|
2006-09-08 13:02:27 +02:00
|
|
|
subdirs = os.listdir(self.cbox.prefs["Locations"]["MountParentDir"])
|
2006-08-16 20:17:16 +02:00
|
|
|
for dir in subdirs:
|
2006-09-08 13:02:27 +02:00
|
|
|
abs_dir = os.path.join(self.cbox.prefs["Locations"]["MountParentDir"], dir)
|
2006-08-22 08:55:07 +02:00
|
|
|
if (not os.path.islink(abs_dir)) and os.path.isdir(abs_dir) and (not os.path.ismount(abs_dir)):
|
|
|
|
os.rmdir(abs_dir)
|
2006-08-16 09:17:44 +02:00
|
|
|
|
2006-08-16 13:07:57 +02:00
|
|
|
|