Fix crypt_load usage in Python binding.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@678 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
Milan Broz
2011-11-07 13:05:16 +00:00
parent c1b01c2dc7
commit 6fe40949f9
2 changed files with 39 additions and 5 deletions

View File

@@ -70,4 +70,30 @@ print "deact. :", c.deactivate()
del c del c
c = pycryptsetup.CryptSetup(
device = IMG,
name = DEVICE,
yesDialog = askyes,
logFunc = log,
passwordDialog = askpassword)
print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
c2 = pycryptsetup.CryptSetup(
name = DEVICE,
yesDialog = askyes,
logFunc = log,
passwordDialog = askpassword)
info = c2.info()
print "cipher :", info["cipher"]
print "cmode :", info["cipher_mode"]
print "keysize :", info["keysize"]
print "deact. :", c.deactivate()
r = c2.deactivate()
print "deact. :", r
del c
del c2
os.remove(IMG) os.remove(IMG)

View File

@@ -69,6 +69,7 @@ static int passwordDialog(const char *msg, char *buf, size_t length, void *this)
{ {
CryptSetupObject *self = this; CryptSetupObject *self = this;
PyObject *result, *arglist; PyObject *result, *arglist;
size_t len;
char *res = NULL; char *res = NULL;
if(self->passwordDialogCB){ if(self->passwordDialogCB){
@@ -88,10 +89,12 @@ static int passwordDialog(const char *msg, char *buf, size_t length, void *this)
} }
strncpy(buf, res, length - 1); strncpy(buf, res, length - 1);
len = strlen(res);
// FIXME: wipe res memset(res, 0, len);
Py_DECREF(result); Py_DECREF(result);
return strlen(buf);
return (int)len;
} }
return -EINVAL; return -EINVAL;
@@ -168,6 +171,7 @@ static int CryptSetup_init(CryptSetupObject* self, PyObject *args, PyObject *kwd
*cmdLineLogCB = NULL, *cmdLineLogCB = NULL,
*tmp = NULL; *tmp = NULL;
char *device = NULL, *deviceName = NULL; char *device = NULL, *deviceName = NULL;
int r;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOOO", kwlist, &device, &deviceName, if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOOO", kwlist, &device, &deviceName,
&yesDialogCB, &passwordDialogCB, &cmdLineLogCB)) &yesDialogCB, &passwordDialogCB, &cmdLineLogCB))
@@ -178,19 +182,23 @@ static int CryptSetup_init(CryptSetupObject* self, PyObject *args, PyObject *kwd
PyErr_SetString(PyExc_IOError, "Device cannot be opened"); PyErr_SetString(PyExc_IOError, "Device cannot be opened");
return -1; return -1;
} }
/* Try to load header form device */
r = crypt_load(self->device, NULL, NULL);
if (r && r != -EINVAL) {
PyErr_SetString(PyExc_RuntimeError, "Cannot initialize device context");
return -1;
}
} else if (deviceName) { } else if (deviceName) {
if (crypt_init_by_name(&(self->device), deviceName)) { if (crypt_init_by_name(&(self->device), deviceName)) {
PyErr_SetString(PyExc_IOError, "Device cannot be opened"); PyErr_SetString(PyExc_IOError, "Device cannot be opened");
return -1; return -1;
} }
/* Context is initialized automatically from active device */
} else { } else {
PyErr_SetString(PyExc_RuntimeError, "Either device file or luks name has to be specified"); PyErr_SetString(PyExc_RuntimeError, "Either device file or luks name has to be specified");
return -1; return -1;
} }
// FIXME: check return code
crypt_load(self->device, NULL, NULL);
if(deviceName) if(deviceName)
self->activated_as = strdup(deviceName); self->activated_as = strdup(deviceName);