Imported Upstream version 1.6.7
[platform/upstream/cryptsetup.git] / python / pycryptsetup.c
1 /*
2  * Python bindings to libcryptsetup
3  *
4  * Copyright (C) 2009-2014, Red Hat, Inc. All rights reserved.
5  * Written by Martin Sivak
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <Python.h>
23 #include <structmember.h>
24 #include <errno.h>
25
26 #include "libcryptsetup.h"
27
28 /* Python API use char* where const char* should be used... */
29 #define CONST_CAST(x) (x)(uintptr_t)
30
31 #if PY_MAJOR_VERSION < 3
32   #define MOD_ERROR_VAL
33   #define MOD_SUCCESS_VAL(val)
34   #define MOD_INIT(name) void init##name(void)
35   #define MOD_DEF(ob, name, doc, methods) \
36           ob = Py_InitModule3(name, methods, doc);
37 #else
38   #define PyInt_AsLong PyLong_AsLong
39   #define PyInt_Check PyLong_Check
40   #define MOD_ERROR_VAL NULL
41   #define MOD_SUCCESS_VAL(val) val
42   #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
43   #define MOD_DEF(ob, name, doc, methods) \
44           static struct PyModuleDef moduledef = { \
45             PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
46           ob = PyModule_Create(&moduledef);
47 #endif
48
49 MOD_INIT(pycryptsetup);
50
51 typedef struct {
52         PyObject_HEAD
53
54         /* Type-specific fields go here. */
55         struct crypt_device *device;
56         char *activated_as;
57
58         /* Callbacks */
59         PyObject *yesDialogCB;
60         PyObject *cmdLineLogCB;
61         PyObject *passwordDialogCB;
62 } CryptSetupObject;
63
64 static int yesDialog(const char *msg, void *this)
65 {
66         CryptSetupObject *self = this;
67         PyObject *result, *arglist;
68         int r;
69
70         if (self->yesDialogCB){
71                 arglist = Py_BuildValue("(s)", msg);
72                 if (!arglist)
73                         return -ENOMEM;
74
75                 result = PyEval_CallObject(self->yesDialogCB, arglist);
76                 Py_DECREF(arglist);
77
78                 if (!result)
79                         return -EINVAL;
80
81                 if (!PyArg_Parse(result, "i", &r))
82                         r = -EINVAL;
83
84                 Py_DECREF(result);
85                 return r;
86         }
87
88         return 1;
89 }
90
91 static int passwordDialog(const char *msg, char *buf, size_t length, void *this)
92 {
93         CryptSetupObject *self = this;
94         PyObject *result, *arglist;
95         size_t len;
96         char *res = NULL;
97
98         if(self->passwordDialogCB){
99                 arglist = Py_BuildValue("(s)", msg);
100                 if (!arglist)
101                         return -ENOMEM;
102
103                 result = PyEval_CallObject(self->passwordDialogCB, arglist);
104                 Py_DECREF(arglist);
105
106                 if (!result)
107                         return -EINVAL;
108
109                 if (!PyArg_Parse(result, "z", &res)) {
110                         Py_DECREF(result);
111                         return -EINVAL;
112                 }
113
114                 strncpy(buf, res, length - 1);
115                 len = strlen(res);
116
117                 memset(res, 0, len);
118                 Py_DECREF(result);
119
120                 return (int)len;
121         }
122
123         return -EINVAL;
124 }
125
126 static void cmdLineLog(int cls, const char *msg, void *this)
127 {
128         CryptSetupObject *self = this;
129         PyObject *result, *arglist;
130
131         if(self->cmdLineLogCB) {
132                 arglist = Py_BuildValue("(is)", cls, msg);
133                 if(!arglist)
134                         return;
135
136                 result = PyEval_CallObject(self->cmdLineLogCB, arglist);
137                 Py_DECREF(arglist);
138                 Py_XDECREF(result);
139         }
140 }
141
142 static void CryptSetup_dealloc(CryptSetupObject* self)
143 {
144         /* free the callbacks */
145         Py_XDECREF(self->yesDialogCB);
146         Py_XDECREF(self->cmdLineLogCB);
147         Py_XDECREF(self->passwordDialogCB);
148
149         free(self->activated_as);
150
151         crypt_free(self->device);
152
153         /* free self */
154         Py_TYPE(self)->tp_free((PyObject*)self);
155 }
156
157 static PyObject *CryptSetup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
158 {
159         CryptSetupObject *self = (CryptSetupObject *)type->tp_alloc(type, 0);
160
161         if (self) {
162                 self->yesDialogCB = NULL;
163                 self->passwordDialogCB = NULL;
164                 self->cmdLineLogCB = NULL;
165                 self->activated_as = NULL;
166         }
167
168         return (PyObject *)self;
169 }
170
171 static PyObject *PyObjectResult(int is)
172 {
173         PyObject *result = Py_BuildValue("i", is);
174
175         if (!result)
176                 PyErr_SetString(PyExc_RuntimeError, "Error during constructing values for return value");
177
178         return result;
179 }
180
181 static char
182 CryptSetup_HELP[] =
183 "CryptSetup object\n\n\
184 constructor takes one to five arguments:\n\
185   __init__(device, name, yesDialog, passwordDialog, logFunc)\n\n\
186   yesDialog - python function with func(text) signature, \n\
187               which asks the user question text and returns 1\n\
188               of the answer was positive or 0 if not\n\
189   logFunc   - python function with func(level, text) signature to log stuff somewhere";
190
191 static int CryptSetup_init(CryptSetupObject* self, PyObject *args, PyObject *kwds)
192 {
193         static const char *kwlist[] = {"device", "name", "yesDialog", "passwordDialog", "logFunc", NULL};
194         PyObject *yesDialogCB = NULL,
195                  *passwordDialogCB = NULL,
196                  *cmdLineLogCB = NULL,
197                  *tmp = NULL;
198         char *device = NULL, *deviceName = NULL;
199         int r;
200
201         if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOOO", CONST_CAST(char**)kwlist, &device, &deviceName,
202                                          &yesDialogCB, &passwordDialogCB, &cmdLineLogCB))
203                 return -1;
204
205         if (device) {
206                 if (crypt_init(&(self->device), device)) {
207                         PyErr_SetString(PyExc_IOError, "Device cannot be opened");
208                         return -1;
209                 }
210                 /* Try to load header form device */
211                 r = crypt_load(self->device, NULL, NULL);
212                 if (r && r != -EINVAL) {
213                         PyErr_SetString(PyExc_RuntimeError, "Cannot initialize device context");
214                         return -1;
215                 }
216         } else if (deviceName) {
217                 if (crypt_init_by_name(&(self->device), deviceName)) {
218                         PyErr_SetString(PyExc_IOError, "Device cannot be opened");
219                         return -1;
220                 }
221                 /* Context is initialized automatically from active device */
222         } else {
223                 PyErr_SetString(PyExc_RuntimeError, "Either device file or luks name has to be specified");
224                 return -1;
225         }
226
227         if(deviceName)
228                 self->activated_as = strdup(deviceName);
229
230         if (yesDialogCB) {
231                 tmp = self->yesDialogCB;
232                 Py_INCREF(yesDialogCB);
233                 self->yesDialogCB = yesDialogCB;
234                 Py_XDECREF(tmp);
235                 crypt_set_confirm_callback(self->device, yesDialog, self);
236         }
237
238         if (passwordDialogCB) {
239                 tmp = self->passwordDialogCB;
240                 Py_INCREF(passwordDialogCB);
241                 self->passwordDialogCB = passwordDialogCB;
242                 Py_XDECREF(tmp);
243                 crypt_set_password_callback(self->device, passwordDialog, self);
244         }
245
246         if (cmdLineLogCB) {
247                 tmp = self->cmdLineLogCB;
248                 Py_INCREF(cmdLineLogCB);
249                 self->cmdLineLogCB = cmdLineLogCB;
250                 Py_XDECREF(tmp);
251                 crypt_set_log_callback(self->device, cmdLineLog, self);
252         }
253
254         return 0;
255 }
256
257 static char
258 CryptSetup_activate_HELP[] =
259 "Activate LUKS device\n\n\
260   activate(name)";
261
262 static PyObject *CryptSetup_activate(CryptSetupObject* self, PyObject *args, PyObject *kwds)
263 {
264         static const char *kwlist[] = {"name", "passphrase", NULL};
265         char *name = NULL, *passphrase = NULL;
266         int is;
267
268         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", CONST_CAST(char**)kwlist, &name, &passphrase))
269                 return NULL;
270
271         // FIXME: allow keyfile and \0 in passphrase
272         is = crypt_activate_by_passphrase(self->device, name, CRYPT_ANY_SLOT,
273                                           passphrase, passphrase ? strlen(passphrase) : 0, 0);
274
275         if (is >= 0) {
276                 free(self->activated_as);
277                 self->activated_as = strdup(name);
278         }
279
280         return PyObjectResult(is);
281 }
282
283 static char
284 CryptSetup_deactivate_HELP[] =
285 "Dectivate LUKS device\n\n\
286   deactivate()";
287
288 static PyObject *CryptSetup_deactivate(CryptSetupObject* self, PyObject *args, PyObject *kwds)
289 {
290         int is = crypt_deactivate(self->device, self->activated_as);
291
292         if (!is) {
293                 free(self->activated_as);
294                 self->activated_as = NULL;
295         }
296
297         return PyObjectResult(is);
298 }
299
300 static char
301 CryptSetup_askyes_HELP[] =
302 "Asks a question using the configured dialog CB\n\n\
303   int askyes(message)";
304
305 static PyObject *CryptSetup_askyes(CryptSetupObject* self, PyObject *args, PyObject *kwds)
306 {
307         static const char *kwlist[] = {"message", NULL};
308         PyObject *message = NULL, *result, *arglist;
309
310         if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", CONST_CAST(char**)kwlist, &message))
311                 return NULL;
312
313         Py_INCREF(message);
314
315         arglist = Py_BuildValue("(O)", message);
316         if (!arglist){
317                 PyErr_SetString(PyExc_RuntimeError, "Error during constructing values for internal call");
318                 return NULL;
319         }
320
321         result = PyEval_CallObject(self->yesDialogCB, arglist);
322         Py_DECREF(arglist);
323         Py_DECREF(message);
324
325         return result;
326 }
327
328 static char
329 CryptSetup_log_HELP[] =
330 "Logs a string using the configured log CB\n\n\
331   log(int level, message)";
332
333 static PyObject *CryptSetup_log(CryptSetupObject* self, PyObject *args, PyObject *kwds)
334 {
335         static const char *kwlist[] = {"priority", "message", NULL};
336         PyObject *message = NULL, *priority = NULL, *result, *arglist;
337
338         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", CONST_CAST(char**)kwlist, &message, &priority))
339                 return NULL;
340
341         Py_INCREF(message);
342         Py_INCREF(priority);
343
344         arglist = Py_BuildValue("(OO)", message, priority);
345         if (!arglist){
346                 PyErr_SetString(PyExc_RuntimeError, "Error during constructing values for internal call");
347                 return NULL;
348         }
349
350         result = PyEval_CallObject(self->cmdLineLogCB, arglist);
351         Py_DECREF(arglist);
352         Py_DECREF(priority);
353         Py_DECREF(message);
354
355         return result;
356 }
357
358 static char
359 CryptSetup_luksUUID_HELP[] =
360 "Get UUID of the LUKS device\n\n\
361   luksUUID()";
362
363 static PyObject *CryptSetup_luksUUID(CryptSetupObject* self, PyObject *args, PyObject *kwds)
364 {
365         PyObject *result;
366
367         result = Py_BuildValue("s", crypt_get_uuid(self->device));
368         if (!result)
369                 PyErr_SetString(PyExc_RuntimeError, "Error during constructing values for return value");
370
371         return result;
372 }
373
374 static char
375 CryptSetup_isLuks_HELP[] =
376 "Is the device LUKS?\n\n\
377   isLuks()";
378
379 static PyObject *CryptSetup_isLuks(CryptSetupObject* self, PyObject *args, PyObject *kwds)
380 {
381         return PyObjectResult(crypt_load(self->device, CRYPT_LUKS1, NULL));
382 }
383
384 static char
385 CryptSetup_Info_HELP[] =
386 "Returns dictionary with info about opened device\nKeys:\n\
387   dir\n  name\n  uuid\n  cipher\n  cipher_mode\n  keysize\n  device\n\
388   offset\n  size\n  skip\n  mode\n";
389
390 static PyObject *CryptSetup_Info(CryptSetupObject* self, PyObject *args, PyObject *kwds)
391 {
392         PyObject *result;
393
394         result = Py_BuildValue("{s:s,s:s,s:z,s:s,s:s,s:s,s:i,s:K}",
395                                 "dir",          crypt_get_dir(),
396                                 "device",       crypt_get_device_name(self->device),
397                                 "name",         self->activated_as,
398                                 "uuid",         crypt_get_uuid(self->device),
399                                 "cipher",       crypt_get_cipher(self->device),
400                                 "cipher_mode",  crypt_get_cipher_mode(self->device),
401                                 "keysize",      crypt_get_volume_key_size(self->device) * 8,
402                                 //"size",       co.size,
403                                 //"mode",       (co.flags & CRYPT_FLAG_READONLY) ? "readonly" : "read/write",
404                                 "offset",       crypt_get_data_offset(self->device)
405                                 );
406
407         if (!result)
408                 PyErr_SetString(PyExc_RuntimeError, "Error during constructing values for return value");
409
410         return result;
411 }
412
413 static char
414 CryptSetup_luksFormat_HELP[] =
415 "Format device to enable LUKS\n\n\
416   luksFormat(cipher = 'aes', cipherMode = 'cbc-essiv:sha256', keysize = 256)\n\n\
417   cipher - cipher specification, e.g. aes, serpent\n\
418   cipherMode - cipher mode specification, e.g. cbc-essiv:sha256, xts-plain64\n\
419   keysize - key size in bits";
420
421 static PyObject *CryptSetup_luksFormat(CryptSetupObject* self, PyObject *args, PyObject *kwds)
422 {
423         static const char *kwlist[] = {"cipher", "cipherMode", "keysize", NULL};
424         char *cipher_mode = NULL, *cipher = NULL;
425         int keysize = 256;
426         PyObject *keysize_object = NULL;
427
428         if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzO", CONST_CAST(char**)kwlist,
429                                         &cipher, &cipher_mode, &keysize_object))
430                 return NULL;
431
432         if (!keysize_object || keysize_object == Py_None) {
433                 /* use default value */
434         } else if (!PyInt_Check(keysize_object)) {
435                 PyErr_SetString(PyExc_TypeError, "keysize must be an integer");
436                 return NULL;
437         } else if (PyInt_AsLong(keysize_object) % 8) {
438                 PyErr_SetString(PyExc_TypeError, "keysize must have integer value dividable by 8");
439                 return NULL;
440         } else if (PyInt_AsLong(keysize_object) <= 0) {
441                 PyErr_SetString(PyExc_TypeError, "keysize must be positive number bigger than 0");
442                 return NULL;
443         } else
444                 keysize = PyInt_AsLong(keysize_object);
445
446         // FIXME use #defined defaults
447         return PyObjectResult(crypt_format(self->device, CRYPT_LUKS1,
448                                 cipher ?: "aes", cipher_mode ?: "cbc-essiv:sha256",
449                                 NULL, NULL, keysize / 8, NULL));
450 }
451
452 static char
453 CryptSetup_addKeyByPassphrase_HELP[] =
454 "Initialize keyslot using passphrase\n\n\
455   addKeyByPassphrase(passphrase, newPassphrase, slot)\n\n\
456   passphrase - string or none to ask the user\n\
457   newPassphrase - passphrase to add\n\
458   slot - which slot to use (optional)";
459
460 static PyObject *CryptSetup_addKeyByPassphrase(CryptSetupObject* self, PyObject *args, PyObject *kwds)
461 {
462         static const char *kwlist[] = {"passphrase", "newPassphrase", "slot", NULL};
463         char *passphrase = NULL, *newpassphrase = NULL;
464         size_t passphrase_len = 0, newpassphrase_len = 0;
465         int slot = CRYPT_ANY_SLOT;
466
467         if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|i", CONST_CAST(char**)kwlist, &passphrase, &newpassphrase, &slot))
468                 return NULL;
469
470         if(passphrase)
471                 passphrase_len = strlen(passphrase);
472
473         if(newpassphrase)
474                 newpassphrase_len = strlen(newpassphrase);
475
476         return PyObjectResult(crypt_keyslot_add_by_passphrase(self->device, slot,
477                                         passphrase, passphrase_len,
478                                         newpassphrase, newpassphrase_len));
479 }
480
481 static char
482 CryptSetup_addKeyByVolumeKey_HELP[] =
483 "Initialize keyslot using cached volume key\n\n\
484   addKeyByVolumeKey(passphrase, newPassphrase, slot)\n\n\
485   newPassphrase - passphrase to add\n\
486   slot - which slot to use (optional)";
487
488 static PyObject *CryptSetup_addKeyByVolumeKey(CryptSetupObject* self, PyObject *args, PyObject *kwds)
489 {
490         static const char *kwlist[] = {"newPassphrase", "slot", NULL};
491         char *newpassphrase = NULL;
492         size_t newpassphrase_len = 0;
493         int slot = CRYPT_ANY_SLOT;
494
495         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", CONST_CAST(char**)kwlist, &newpassphrase, &slot))
496                 return NULL;
497
498         if (newpassphrase)
499                 newpassphrase_len = strlen(newpassphrase);
500
501         return PyObjectResult(crypt_keyslot_add_by_volume_key(self->device, slot,
502                                         NULL, 0, newpassphrase, newpassphrase_len));
503 }
504
505 static char
506 CryptSetup_removePassphrase_HELP[] =
507 "Destroy keyslot using passphrase\n\n\
508   removePassphrase(passphrase)\n\n\
509   passphrase - string or none to ask the user";
510
511 static PyObject *CryptSetup_removePassphrase(CryptSetupObject* self, PyObject *args, PyObject *kwds)
512 {
513         static const char *kwlist[] = {"passphrase", NULL};
514         char *passphrase = NULL;
515         size_t passphrase_len = 0;
516         int is;
517
518         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", CONST_CAST(char**)kwlist, &passphrase))
519                 return NULL;
520
521         if (passphrase)
522                 passphrase_len = strlen(passphrase);
523
524         is = crypt_activate_by_passphrase(self->device, NULL, CRYPT_ANY_SLOT,
525                                           passphrase, passphrase_len, 0);
526         if (is < 0)
527                 return PyObjectResult(is);
528
529         return PyObjectResult(crypt_keyslot_destroy(self->device, is));
530 }
531
532 static char
533 CryptSetup_killSlot_HELP[] =
534 "Destroy keyslot\n\n\
535   killSlot(slot)\n\n\
536   slot - the slot to remove";
537
538 static PyObject *CryptSetup_killSlot(CryptSetupObject* self, PyObject *args, PyObject *kwds)
539 {
540         static const char *kwlist[] = {"slot", NULL};
541         int slot = CRYPT_ANY_SLOT;
542
543         if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", CONST_CAST(char**)kwlist, &slot))
544                 return NULL;
545
546         switch (crypt_keyslot_status(self->device, slot)) {
547         case CRYPT_SLOT_ACTIVE:
548                 return PyObjectResult(crypt_keyslot_destroy(self->device, slot));
549         case CRYPT_SLOT_ACTIVE_LAST:
550                 PyErr_SetString(PyExc_ValueError, "Last slot, removing it would render the device unusable");
551                 break;
552         case CRYPT_SLOT_INACTIVE:
553                 PyErr_SetString(PyExc_ValueError, "Inactive slot");
554                 break;
555         case CRYPT_SLOT_INVALID:
556                 PyErr_SetString(PyExc_ValueError, "Invalid slot");
557                 break;
558         }
559
560         return NULL;
561 }
562
563 static char
564 CryptSetup_Status_HELP[] =
565 "Status of LUKS device\n\n\
566   luksStatus()";
567
568 static PyObject *CryptSetup_Status(CryptSetupObject* self, PyObject *args, PyObject *kwds)
569 {
570         if (!self->activated_as){
571                 PyErr_SetString(PyExc_IOError, "Device has not been activated yet.");
572                 return NULL;
573         }
574
575         return PyObjectResult(crypt_status(self->device, self->activated_as));
576 }
577
578 static char
579 CryptSetup_Resume_HELP[] =
580 "Resume LUKS device\n\n\
581   luksOpen(passphrase)\n\n\
582   passphrase - string or none to ask the user";
583
584 static PyObject *CryptSetup_Resume(CryptSetupObject* self, PyObject *args, PyObject *kwds)
585 {
586         static const char *kwlist[] = {"passphrase", NULL};
587         char* passphrase = NULL;
588         size_t passphrase_len = 0;
589
590         if (!self->activated_as){
591                 PyErr_SetString(PyExc_IOError, "Device has not been activated yet.");
592                 return NULL;
593         }
594
595         if (! PyArg_ParseTupleAndKeywords(args, kwds, "|s", CONST_CAST(char**)kwlist, &passphrase))
596                 return NULL;
597
598         if (passphrase)
599                 passphrase_len = strlen(passphrase);
600
601         return PyObjectResult(crypt_resume_by_passphrase(self->device, self->activated_as,
602                                         CRYPT_ANY_SLOT, passphrase, passphrase_len));
603 }
604
605 static char
606 CryptSetup_Suspend_HELP[] =
607 "Suspend LUKS device\n\n\
608   luksSupsend()";
609
610 static PyObject *CryptSetup_Suspend(CryptSetupObject* self, PyObject *args, PyObject *kwds)
611 {
612         if (!self->activated_as){
613                 PyErr_SetString(PyExc_IOError, "Device has not been activated yet.");
614                 return NULL;
615         }
616
617         return PyObjectResult(crypt_suspend(self->device, self->activated_as));
618 }
619
620 static char
621 CryptSetup_debugLevel_HELP[] =
622 "Set debug level\n\n\
623   debugLevel(level)\n\n\
624   level - debug level";
625
626 static PyObject *CryptSetup_debugLevel(CryptSetupObject* self, PyObject *args, PyObject *kwds)
627 {
628         static const char *kwlist[] = {"level", NULL};
629         int level = 0;
630
631         if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", CONST_CAST(char**)kwlist, &level))
632                 return NULL;
633
634         crypt_set_debug_level(level);
635
636         Py_RETURN_NONE;
637 }
638
639 static char
640 CryptSetup_iterationTime_HELP[] =
641 "Set iteration time\n\n\
642   iterationTime(time_ms)\n\n\
643   time_ms - time in miliseconds";
644
645 static PyObject *CryptSetup_iterationTime(CryptSetupObject* self, PyObject *args, PyObject *kwds)
646 {
647         static const char *kwlist[] = {"time_ms", NULL};
648         uint64_t time_ms = 0;
649
650         if (!PyArg_ParseTupleAndKeywords(args, kwds, "K", CONST_CAST(char**)kwlist, &time_ms))
651                 return NULL;
652
653         crypt_set_iteration_time(self->device, time_ms);
654
655         Py_RETURN_NONE;
656 }
657
658 static PyMemberDef CryptSetup_members[] = {
659         {CONST_CAST(char*)"yesDialogCB", T_OBJECT_EX, offsetof(CryptSetupObject, yesDialogCB), 0, CONST_CAST(char*)"confirmation dialog callback"},
660         {CONST_CAST(char*)"cmdLineLogCB", T_OBJECT_EX, offsetof(CryptSetupObject, cmdLineLogCB), 0, CONST_CAST(char*)"logging callback"},
661         {CONST_CAST(char*)"passwordDialogCB", T_OBJECT_EX, offsetof(CryptSetupObject, passwordDialogCB), 0, CONST_CAST(char*)"password dialog callback"},
662         {NULL}
663 };
664
665 static PyMethodDef CryptSetup_methods[] = {
666         /* self-test methods */
667         {"log", (PyCFunction)CryptSetup_log, METH_VARARGS|METH_KEYWORDS, CryptSetup_askyes_HELP},
668         {"askyes", (PyCFunction)CryptSetup_askyes, METH_VARARGS|METH_KEYWORDS, CryptSetup_log_HELP},
669
670         /* activation and deactivation */
671         {"deactivate", (PyCFunction)CryptSetup_deactivate, METH_NOARGS, CryptSetup_deactivate_HELP},
672         {"activate", (PyCFunction)CryptSetup_activate, METH_VARARGS|METH_KEYWORDS, CryptSetup_activate_HELP},
673
674         /* cryptsetup info entrypoints */
675         {"luksUUID", (PyCFunction)CryptSetup_luksUUID, METH_NOARGS, CryptSetup_luksUUID_HELP},
676         {"isLuks", (PyCFunction)CryptSetup_isLuks, METH_NOARGS, CryptSetup_isLuks_HELP},
677         {"info", (PyCFunction)CryptSetup_Info, METH_NOARGS, CryptSetup_Info_HELP},
678         {"status", (PyCFunction)CryptSetup_Status, METH_NOARGS, CryptSetup_Status_HELP},
679
680         /* cryptsetup mgmt entrypoints */
681         {"luksFormat", (PyCFunction)CryptSetup_luksFormat, METH_VARARGS|METH_KEYWORDS, CryptSetup_luksFormat_HELP},
682         {"addKeyByPassphrase", (PyCFunction)CryptSetup_addKeyByPassphrase, METH_VARARGS|METH_KEYWORDS, CryptSetup_addKeyByPassphrase_HELP},
683         {"addKeyByVolumeKey", (PyCFunction)CryptSetup_addKeyByVolumeKey, METH_VARARGS|METH_KEYWORDS, CryptSetup_addKeyByVolumeKey_HELP},
684         {"removePassphrase", (PyCFunction)CryptSetup_removePassphrase, METH_VARARGS|METH_KEYWORDS, CryptSetup_removePassphrase_HELP},
685         {"killSlot", (PyCFunction)CryptSetup_killSlot, METH_VARARGS|METH_KEYWORDS, CryptSetup_killSlot_HELP},
686
687         /* suspend resume */
688         {"resume", (PyCFunction)CryptSetup_Resume, METH_VARARGS|METH_KEYWORDS, CryptSetup_Resume_HELP},
689         {"suspend", (PyCFunction)CryptSetup_Suspend, METH_NOARGS, CryptSetup_Suspend_HELP},
690
691         /* misc */
692         {"debugLevel", (PyCFunction)CryptSetup_debugLevel, METH_VARARGS|METH_KEYWORDS, CryptSetup_debugLevel_HELP},
693         {"iterationTime", (PyCFunction)CryptSetup_iterationTime, METH_VARARGS|METH_KEYWORDS, CryptSetup_iterationTime_HELP},
694
695         {NULL} /* Sentinel */
696 };
697
698 static PyTypeObject CryptSetupType = {
699         PyVarObject_HEAD_INIT(NULL, 0)
700         "pycryptsetup.CryptSetup", /*tp_name*/
701         sizeof(CryptSetupObject), /*tp_basicsize*/
702         0, /*tp_itemsize*/
703         (destructor)CryptSetup_dealloc, /*tp_dealloc*/
704         0, /*tp_print*/
705         0, /*tp_getattr*/
706         0, /*tp_setattr*/
707         0, /*tp_compare*/
708         0, /*tp_repr*/
709         0, /*tp_as_number*/
710         0, /*tp_as_sequence*/
711         0, /*tp_as_mapping*/
712         0, /*tp_hash */
713         0, /*tp_call*/
714         0, /*tp_str*/
715         0, /*tp_getattro*/
716         0, /*tp_setattro*/
717         0, /*tp_as_buffer*/
718         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
719         CryptSetup_HELP, /* tp_doc */
720         0, /* tp_traverse */
721         0, /* tp_clear */
722         0, /* tp_richcompare */
723         0, /* tp_weaklistoffset */
724         0, /* tp_iter */
725         0, /* tp_iternext */
726         CryptSetup_methods, /* tp_methods */
727         CryptSetup_members, /* tp_members */
728         0, /* tp_getset */
729         0, /* tp_base */
730         0, /* tp_dict */
731         0, /* tp_descr_get */
732         0, /* tp_descr_set */
733         0, /* tp_dictoffset */
734         (initproc)CryptSetup_init, /* tp_init */
735         0, /* tp_alloc */
736         CryptSetup_new, /* tp_new */
737 };
738
739 static PyMethodDef pycryptsetup_methods[] = {
740         {NULL} /* Sentinel */
741 };
742
743 MOD_INIT(pycryptsetup)
744 {
745         PyObject *m;
746
747         if (PyType_Ready(&CryptSetupType) < 0)
748                 return MOD_ERROR_VAL;
749
750         MOD_DEF(m, "pycryptsetup", "CryptSetup pythonized API.", pycryptsetup_methods);
751         Py_INCREF(&CryptSetupType);
752
753         PyModule_AddObject(m, "CryptSetup", (PyObject *)&CryptSetupType);
754
755         /* debug constants */
756         PyModule_AddIntConstant(m, "CRYPT_DEBUG_ALL", CRYPT_DEBUG_ALL);
757         PyModule_AddIntConstant(m, "CRYPT_DEBUG_NONE", CRYPT_DEBUG_NONE);
758
759         /* log constants */
760         PyModule_AddIntConstant(m, "CRYPT_LOG_NORMAL", CRYPT_LOG_NORMAL);
761         PyModule_AddIntConstant(m, "CRYPT_LOG_ERROR", CRYPT_LOG_ERROR);
762         PyModule_AddIntConstant(m, "CRYPT_LOG_VERBOSE", CRYPT_LOG_VERBOSE);
763         PyModule_AddIntConstant(m, "CRYPT_LOG_DEBUG", CRYPT_LOG_DEBUG);
764
765         /* status constants */
766         PyModule_AddIntConstant(m, "CRYPT_INVALID", CRYPT_INVALID);
767         PyModule_AddIntConstant(m, "CRYPT_INACTIVE", CRYPT_INACTIVE);
768         PyModule_AddIntConstant(m, "CRYPT_ACTIVE", CRYPT_ACTIVE);
769         PyModule_AddIntConstant(m, "CRYPT_BUSY", CRYPT_BUSY);
770
771         return MOD_SUCCESS_VAL(m);
772 }