7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Exceptions --------------------------------------------------------*/
35 static PyObject
*keyexc
;
36 static PyObject
*keyioexc
;
37 static PyObject
*keyfilebrokenexc
;
39 static PyObject
*kxmeth___init__(PyObject
*me
, PyObject
*arg
)
44 if (!PyArg_ParseTuple(arg
, "Oi:__init__", &me
, &err
) ||
45 (x
= PyInt_FromLong(err
)) == 0 ||
46 PyObject_SetAttrString(me
, "err", x
))
49 if ((x
= PyString_FromString(key_strerror(err
))) == 0 ||
50 PyObject_SetAttrString(me
, "errstring", x
))
53 if ((x
= PyString_FromString(key_strerror(err
))) == 0 ||
54 PyObject_SetAttrString(me
, "errstring", x
))
57 if ((x
= PySequence_GetSlice(arg
, 1, PySequence_Size(arg
))) == 0 ||
58 PyObject_SetAttrString(me
, "args", x
))
68 static PyObject
*kxmeth___str__(PyObject
*me
, PyObject
*arg
)
71 const char *errstr
, *errtag
;
75 static const char *const tab
[] = {
76 #define ENTRY(tag, num, str) "KERR_" #tag,
81 if (!PyArg_ParseTuple(arg
, "O:__str__", &me
) ||
82 (x
= PyObject_GetAttrString(me
, "err")) == 0 ||
83 (err
= PyInt_AsLong(x
), PyErr_Occurred()))
87 if (err
>= 0 && err
< N(tab
)) errtag
= tab
[err
];
88 else errtag
= "<unknown>";
89 if ((x
= PyObject_GetAttrString(me
, "errstring")) == 0 ||
90 (errstr
= PyString_AsString(x
)) == 0)
92 rc
= PyString_FromFormat("%s (%ld): %s", errtag
, -err
, errstr
);
99 static PyMethodDef keyexc_pymethods
[] = {
100 #define METHNAME(func) kxmeth_##func
101 METH (__init__
, "KeyError(CODE)")
102 METH (__str__
, "E.__str__() -> STRING")
107 static void keyexc_raise(int err
)
109 PyObject
*arg
= Py_BuildValue("(i)", err
);
110 if (arg
) PyErr_SetObject(keyexc
, arg
);
113 #define KEYERR(err) do { keyexc_raise(err); goto end; } while (0)
114 #define KEYIOERR(name) do { \
115 PyErr_SetFromErrnoWithFilename(keyioexc, name); \
118 #define KEYFILEBROKEN(name) do { \
119 PyErr_SetFromErrnoWithFilename(keyfilebrokenexc, name); \
123 /*----- Data structures ---------------------------------------------------*/
125 typedef struct keydata_pyobj
{
130 static PyTypeObject
*keydata_pytype
;
131 static PyTypeObject
*keydatabin_pytype
;
132 static PyTypeObject
*keydataenc_pytype
;
133 static PyTypeObject
*keydatamp_pytype
;
134 static PyTypeObject
*keydatastruct_pytype
;
135 static PyTypeObject
*keydatastr_pytype
;
136 static PyTypeObject
*keydataec_pytype
;
137 #define KEYDATA_PYCHECK(o) PyObject_TypeCheck(o, keydata_pytype)
138 #define KEYDATA_KD(o) (((keydata_pyobj *)(o))->kd)
140 typedef struct subkeyiter_pyobj
{
146 static PyTypeObject
*subkeyiter_pytype
;
147 #define SUBKEYITER_PYCHECK(o) PyObject_TypeCheck(o, subkeyiter_pytype);
148 #define SUBKEYITER_I(o) (&((subkeyiter_pyobj *)(o))->i)
149 #define SUBKEYITER_KDOBJ(o) (((subkeyiter_pyobj *)(o))->kdobj)
151 typedef struct keyfile_pyobj
{
156 static PyTypeObject
*keyfile_pytype
;
157 #define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype)
158 #define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf)
160 typedef struct key_pyobj
{
166 static PyTypeObject
*key_pytype
;
167 #define KEY_PYCHECK(o) PyObject_TypeCheck(o, key_pytype)
168 #define KEY_K(o) (((key_pyobj *)(o))->k)
169 #define KEY_KFOBJ(o) (((key_pyobj *)(o))->kfobj)
170 #define KEY_KF(o) KEYFILE_KF(KEY_KFOBJ(o))
172 typedef struct keyiter_pyobj
{
178 static PyTypeObject
*keyiter_pytype
;
179 #define KEYITER_PYCHECK(o) PyObject_TypeCheck(o, keyiter_pytype)
180 #define KEYITER_I(o) (&((keyiter_pyobj *)(o))->i)
181 #define KEYITER_KFOBJ(o) (((keyiter_pyobj *)(o))->kfobj)
182 #define KEYITER_KF(o) KEYFILE_KF(KEYITER_KFOBJ(o))
184 typedef struct keyattrs_pyobj
{
189 static PyTypeObject
*keyattrs_pytype
;
190 #define KEYATTRS_PYCHECK(o) PyObject_TypeCheck(o, keyattrs_pytype)
191 #define KEYATTRS_KOBJ(o) (((keyattrs_pyobj *)(o))->kobj)
192 #define KEYATTRS_KF(o) KEY_KF(KEYATTRS_KOBJ(o))
193 #define KEYATTRS_K(o) KEY_K(KEYATTRS_KOBJ(o))
195 typedef struct keyattriter_pyobj
{
201 static PyTypeObject
*keyattriter_pytype
;
202 #define KEYATTRITER_PYCHECK(o) PyObject_TypeCheck(o, keyattriter_pytype)
203 #define KEYATTRITER_I(o) (&((keyattriter_pyobj *)(o))->i)
204 #define KEYATTRITER_KOBJ(o) (((keyattriter_pyobj *)(o))->kobj)
205 #define KEYATTRITER_K(o) KEY_K(KEYATTRITER_KOBJ(o))
206 #define KEYATTRITER_KFOBJ(o) KEY_KFOBJ(KEYATTRITER_KOBJ(o))
207 #define KEYATTRITER_KF(o) KEY_KF(KEYATTRITER_KOBJ(o))
209 /*----- Filters -----------------------------------------------------------*/
211 static int convfilter(PyObject
*x
, void *p
)
217 PyObject
*a
= 0, *b
= 0;
221 if ((fs
= PyString_AsString(x
)) != 0) {
222 if ((err
= key_readflags(fs
, &end
, &f
->f
, &f
->m
)) != 0)
225 KEYERR(KERR_BADFLAGS
);
228 if (!PySequence_Check(x
))
230 else if ((n
= PySequence_Size(x
)) < 0)
234 else if ((a
= PySequence_GetItem(x
, 0)) == 0 || convuint(a
, &f
->f
) ||
235 (b
= PySequence_GetItem(x
, 1)) == 0 || convuint(b
, &f
->m
))
241 TYERR("expected flag string or flag/mask pair");
248 static int convflags(PyObject
*x
, void *p
)
260 if ((fs
= PyString_AsString(x
)) != 0) {
261 if ((err
= key_readflags(fs
, &end
, f
, 0)) != 0)
264 KEYERR(KERR_BADFLAGS
);
273 TYERR("expected flag string or flag/mask pair");
278 static PyObject
*meth__KeyData_readflags(PyObject
*me
, PyObject
*arg
)
286 if (!PyArg_ParseTuple(arg
, "Os:key_readflags", &me
, &p
))
288 if ((err
= key_readflags(p
, &end
, &f
, &m
)) != 0)
290 rc
= Py_BuildValue("(NNs)", getulong(f
), getulong(m
), end
);
295 static PyObject
*meth__KeyData_writeflags(PyObject
*me
, PyObject
*arg
)
301 if (!PyArg_ParseTuple(arg
, "OO&:key_writeflags", &me
, convuint
, &f
))
303 key_writeflags(f
, &d
);
304 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
309 /*----- Key data ----------------------------------------------------------*/
311 static PyObject
*keydata_pywrap(key_data
*kd
)
314 keydata_pyobj
*kdobj
;
316 switch (kd
->e
& KF_ENCMASK
) {
317 case KENC_BINARY
: ty
= keydatabin_pytype
; break;
318 case KENC_ENCRYPT
: ty
= keydataenc_pytype
; break;
319 case KENC_MP
: ty
= keydatamp_pytype
; break;
320 case KENC_STRUCT
: ty
= keydatastruct_pytype
; break;
321 case KENC_STRING
: ty
= keydatastr_pytype
; break;
322 case KENC_EC
: ty
= keydataec_pytype
; break;
325 kdobj
= PyObject_NEW(keydata_pyobj
, ty
);
327 return ((PyObject
*)kdobj
);
330 static void keydata_pydealloc(PyObject
*me
)
332 key_drop(KEYDATA_KD(me
));
336 static PyObject
*kdmeth_matchp(PyObject
*me
, PyObject
*arg
)
340 if (!PyArg_ParseTuple(arg
, "O&:matchp", convfilter
, &f
))
342 return (getbool(KEY_MATCH(KEYDATA_KD(me
), &f
)));
345 static PyObject
*kdmeth_split(PyObject
*me
, PyObject
*arg
)
347 if (!PyArg_ParseTuple(arg
, ":split"))
349 key_split(&KEYDATA_KD(me
));
353 static PyObject
*kdmeth_write(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
355 key_filter f
= { 0, 0 };
358 static char *kwlist
[] = { "filter", 0 };
360 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:write", kwlist
,
363 key_write(KEYDATA_KD(me
), &d
, &f
);
364 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
369 static PyObject
*kdmeth_encode(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
371 key_filter f
= { 0, 0 };
374 static char *kwlist
[] = { "filter", 0 };
376 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:encode", kwlist
,
379 key_encode(KEYDATA_KD(me
), &d
, &f
);
380 rc
= bytestring_pywrap(d
.buf
, d
.len
);
385 static PyObject
*kdmeth_plock(PyObject
*me
, PyObject
*arg
)
392 if (!PyArg_ParseTuple(arg
, "s:plock", &tag
))
394 if ((err
= key_plock(&kd
, KEYDATA_KD(me
), tag
)) != 0)
396 rc
= keydata_pywrap(kd
);
401 static PyObject
*kdmeth_lock(PyObject
*me
, PyObject
*arg
)
408 if (!PyArg_ParseTuple(arg
, "s#:lock", &p
, &n
))
410 key_lock(&kd
, KEYDATA_KD(me
), p
, n
);
411 rc
= keydata_pywrap(kd
);
416 static PyObject
*meth__KeyData_read(PyObject
*me
, PyObject
*arg
)
423 if (!PyArg_ParseTuple(arg
, "Os:read", &me
, &p
))
425 if ((kd
= key_read(p
, &end
)) == 0)
426 KEYERR(KERR_MALFORMED
);
427 rc
= Py_BuildValue("(Ns)", keydata_pywrap(kd
), end
);
432 static PyObject
*meth__KeyData_decode(PyObject
*me
, PyObject
*arg
)
439 if (!PyArg_ParseTuple(arg
, "Os#:decode", &me
, &p
, &n
))
441 if ((kd
= key_decode(p
, n
)) == 0)
442 KEYERR(KERR_MALFORMED
);
443 rc
= keydata_pywrap(kd
);
448 static PyObject
*kdget_flags(PyObject
*me
, void *hunoz
)
449 { return (getulong(KEYDATA_KD(me
)->e
)); }
451 static PyMethodDef keydata_pymethods
[] = {
452 #define METHNAME(func) kdmeth_##func
453 METH (matchp
, "KD.matchp(FILTER) -> BOOL")
454 METH (split
, "KD.split()")
455 KWMETH(write
, "KD.write(filter = <any>) -> STRING")
456 KWMETH(encode
, "KD.encode(filter = <any>) -> BYTES")
457 METH (plock
, "KD.plock(TAG) -> ENCRYPTED-KD")
458 METH (lock
, "KD.lock(KEY) -> ENCRYPTED-KD")
463 static PyGetSetDef keydata_pygetset
[] = {
464 #define GETSETNAME(op, name) kd##op##_##name
465 GET (flags
, "KD.flags -> FLAGS")
470 static PyTypeObject keydata_pytype_skel
= {
471 PyObject_HEAD_INIT(0) 0, /* Header */
472 "catacomb.KeyData", /* @tp_name@ */
473 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
474 0, /* @tp_itemsize@ */
476 keydata_pydealloc
, /* @tp_dealloc@ */
478 0, /* @tp_getattr@ */
479 0, /* @tp_setattr@ */
480 0, /* @tp_compare@ */
482 0, /* @tp_as_number@ */
483 0, /* @tp_as_sequence@ */
484 0, /* @tp_as_mapping@ */
488 0, /* @tp_getattro@ */
489 0, /* @tp_setattro@ */
490 0, /* @tp_as_buffer@ */
491 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
495 "Key data base class.",
497 0, /* @tp_traverse@ */
499 0, /* @tp_richcompare@ */
500 0, /* @tp_weaklistoffset@ */
502 0, /* @tp_iternext@ */
503 keydata_pymethods
, /* @tp_methods@ */
504 0, /* @tp_members@ */
505 keydata_pygetset
, /* @tp_getset@ */
508 0, /* @tp_descr_get@ */
509 0, /* @tp_descr_set@ */
510 0, /* @tp_dictoffset@ */
512 PyType_GenericAlloc
, /* @tp_alloc@ */
513 abstract_pynew
, /* @tp_new@ */
518 static PyObject
*keydatabin_pynew(PyTypeObject
*ty
,
519 PyObject
*arg
, PyObject
*kw
)
524 keydata_pyobj
*me
= 0;
525 static char *kwlist
[] = { "key", "flags", 0 };
527 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|O&:new", kwlist
,
528 &p
, &n
, convflags
, &f
))
530 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
531 me
->kd
= key_newbinary(f
& ~KF_ENCMASK
, p
, n
);
533 return ((PyObject
*)me
);
536 static PyObject
*kdbget_bin(PyObject
*me
, void *hunoz
)
537 { return (bytestring_pywrap(KEYDATA_KD(me
)->u
.k
.k
,
538 KEYDATA_KD(me
)->u
.k
.sz
)); }
540 static PyGetSetDef keydatabin_pygetset
[] = {
541 #define GETSETNAME(op, name) kdb##op##_##name
542 GET (bin
, "KD.bin -> BYTES")
547 static PyTypeObject keydatabin_pytype_skel
= {
548 PyObject_HEAD_INIT(0) 0, /* Header */
549 "catacomb.KeyDataBinary", /* @tp_name@ */
550 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
551 0, /* @tp_itemsize@ */
553 0, /* @tp_dealloc@ */
555 0, /* @tp_getattr@ */
556 0, /* @tp_setattr@ */
557 0, /* @tp_compare@ */
559 0, /* @tp_as_number@ */
560 0, /* @tp_as_sequence@ */
561 0, /* @tp_as_mapping@ */
565 0, /* @tp_getattro@ */
566 0, /* @tp_setattro@ */
567 0, /* @tp_as_buffer@ */
568 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
572 "Key data for binary keys.",
574 0, /* @tp_traverse@ */
576 0, /* @tp_richcompare@ */
577 0, /* @tp_weaklistoffset@ */
579 0, /* @tp_iternext@ */
580 0, /* @tp_methods@ */
581 0, /* @tp_members@ */
582 keydatabin_pygetset
, /* @tp_getset@ */
585 0, /* @tp_descr_get@ */
586 0, /* @tp_descr_set@ */
587 0, /* @tp_dictoffset@ */
589 PyType_GenericAlloc
, /* @tp_alloc@ */
590 keydatabin_pynew
, /* @tp_new@ */
595 static PyObject
*keydataenc_pynew(PyTypeObject
*ty
,
596 PyObject
*arg
, PyObject
*kw
)
601 keydata_pyobj
*me
= 0;
602 static char *kwlist
[] = { "key", "flags", 0 };
604 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|O&:new", kwlist
,
605 &p
, &n
, convflags
, &f
))
607 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
608 me
->kd
= key_newencrypted(f
& ~KF_ENCMASK
, p
, n
);
610 return ((PyObject
*)me
);
613 static PyObject
*kdemeth_plock(PyObject
*me
, PyObject
*arg
)
616 if (!PyArg_ParseTuple(arg
, "s:plock", &hunoz
)) goto end
;
617 KEYERR(KERR_WRONGTYPE
);
622 static PyObject
*kdemeth_lock(PyObject
*me
, PyObject
*arg
)
626 if (!PyArg_ParseTuple(arg
, "s#:lock", &hunoz
, &hukairz
)) goto end
;
627 KEYERR(KERR_WRONGTYPE
);
632 static PyObject
*kdemeth_punlock(PyObject
*me
, PyObject
*arg
)
639 if (!PyArg_ParseTuple(arg
, "s:punlock", &tag
))
641 if ((err
= key_punlock(&kd
, KEYDATA_KD(me
), tag
)) != 0)
643 rc
= keydata_pywrap(kd
);
648 static PyObject
*kdemeth_unlock(PyObject
*me
, PyObject
*arg
)
656 if (!PyArg_ParseTuple(arg
, "s#:unlock", &p
, &n
))
658 if ((err
= key_unlock(&kd
, KEYDATA_KD(me
), p
, n
)) != 0)
660 rc
= keydata_pywrap(kd
);
665 #define kdeget_ct kdbget_bin
667 static PyMethodDef keydataenc_pymethods
[] = {
668 #define METHNAME(func) kdemeth_##func
669 METH (plock
, "KD.plock(TAG) -> ENCRYPTED-KD")
670 METH (lock
, "KD.lock(KEY) -> ENCRYPTED-KD")
671 METH (punlock
, "KD.punlock(TAG) -> KD")
672 METH (unlock
, "KD.unlock(KEY) -> KD")
677 static PyGetSetDef keydataenc_pygetset
[] = {
678 #define GETSETNAME(op, name) kde##op##_##name
679 GET (ct
, "KD.ct -> BYTES")
684 static PyTypeObject keydataenc_pytype_skel
= {
685 PyObject_HEAD_INIT(0) 0, /* Header */
686 "catacomb.KeyDataEncrypted", /* @tp_name@ */
687 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
688 0, /* @tp_itemsize@ */
690 0, /* @tp_dealloc@ */
692 0, /* @tp_getattr@ */
693 0, /* @tp_setattr@ */
694 0, /* @tp_compare@ */
696 0, /* @tp_as_number@ */
697 0, /* @tp_as_sequence@ */
698 0, /* @tp_as_mapping@ */
702 0, /* @tp_getattro@ */
703 0, /* @tp_setattro@ */
704 0, /* @tp_as_buffer@ */
705 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
709 "Key data for encrypted keys.",
711 0, /* @tp_traverse@ */
713 0, /* @tp_richcompare@ */
714 0, /* @tp_weaklistoffset@ */
716 0, /* @tp_iternext@ */
717 keydataenc_pymethods
, /* @tp_methods@ */
718 0, /* @tp_members@ */
719 keydataenc_pygetset
, /* @tp_getset@ */
722 0, /* @tp_descr_get@ */
723 0, /* @tp_descr_set@ */
724 0, /* @tp_dictoffset@ */
726 PyType_GenericAlloc
, /* @tp_alloc@ */
727 keydataenc_pynew
, /* @tp_new@ */
732 static PyObject
*keydatamp_pynew(PyTypeObject
*ty
,
733 PyObject
*arg
, PyObject
*kw
)
737 keydata_pyobj
*me
= 0;
738 static char *kwlist
[] = { "key", "flags", 0 };
740 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", kwlist
,
741 convmp
, &x
, convflags
, &f
))
743 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
744 me
->kd
= key_newmp(f
& ~KF_ENCMASK
, x
);
747 return ((PyObject
*)me
);
750 static PyObject
*kdmget_mp(PyObject
*me
, void *hunoz
)
751 { return (mp_pywrap(MP_COPY(KEYDATA_KD(me
)->u
.m
))); }
753 static PyGetSetDef keydatamp_pygetset
[] = {
754 #define GETSETNAME(op, name) kdm##op##_##name
755 GET (mp
, "KD.mp -> X")
760 static PyTypeObject keydatamp_pytype_skel
= {
761 PyObject_HEAD_INIT(0) 0, /* Header */
762 "catacomb.KeyDataMP", /* @tp_name@ */
763 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
764 0, /* @tp_itemsize@ */
766 0, /* @tp_dealloc@ */
768 0, /* @tp_getattr@ */
769 0, /* @tp_setattr@ */
770 0, /* @tp_compare@ */
772 0, /* @tp_as_number@ */
773 0, /* @tp_as_sequence@ */
774 0, /* @tp_as_mapping@ */
778 0, /* @tp_getattro@ */
779 0, /* @tp_setattro@ */
780 0, /* @tp_as_buffer@ */
781 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
785 "Key data for large-integer keys.",
787 0, /* @tp_traverse@ */
789 0, /* @tp_richcompare@ */
790 0, /* @tp_weaklistoffset@ */
792 0, /* @tp_iternext@ */
793 0, /* @tp_methods@ */
794 0, /* @tp_members@ */
795 keydatamp_pygetset
, /* @tp_getset@ */
798 0, /* @tp_descr_get@ */
799 0, /* @tp_descr_set@ */
800 0, /* @tp_dictoffset@ */
802 PyType_GenericAlloc
, /* @tp_alloc@ */
803 keydatamp_pynew
, /* @tp_new@ */
808 static PyObject
*keydatastr_pynew(PyTypeObject
*ty
,
809 PyObject
*arg
, PyObject
*kw
)
813 keydata_pyobj
*me
= 0;
814 static char *kwlist
[] = { "key", "flags", 0 };
816 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O&:new", kwlist
,
819 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
820 me
->kd
= key_newstring(f
& ~KF_ENCMASK
, p
);
822 return ((PyObject
*)me
);
825 static PyObject
*kdsget_str(PyObject
*me
, void *hunoz
)
826 { return (PyString_FromString(KEYDATA_KD(me
)->u
.p
)); }
828 static PyGetSetDef keydatastr_pygetset
[] = {
829 #define GETSETNAME(op, name) kds##op##_##name
830 GET (str
, "KD.str -> STRING")
835 static PyTypeObject keydatastr_pytype_skel
= {
836 PyObject_HEAD_INIT(0) 0, /* Header */
837 "catacomb.KeyDataString", /* @tp_name@ */
838 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
839 0, /* @tp_itemsize@ */
841 0, /* @tp_dealloc@ */
843 0, /* @tp_getattr@ */
844 0, /* @tp_setattr@ */
845 0, /* @tp_compare@ */
847 0, /* @tp_as_number@ */
848 0, /* @tp_as_sequence@ */
849 0, /* @tp_as_mapping@ */
853 0, /* @tp_getattro@ */
854 0, /* @tp_setattro@ */
855 0, /* @tp_as_buffer@ */
856 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
860 "Key data for string keys.",
862 0, /* @tp_traverse@ */
864 0, /* @tp_richcompare@ */
865 0, /* @tp_weaklistoffset@ */
867 0, /* @tp_iternext@ */
868 0, /* @tp_methods@ */
869 0, /* @tp_members@ */
870 keydatastr_pygetset
, /* @tp_getset@ */
873 0, /* @tp_descr_get@ */
874 0, /* @tp_descr_set@ */
875 0, /* @tp_dictoffset@ */
877 PyType_GenericAlloc
, /* @tp_alloc@ */
878 keydatastr_pynew
, /* @tp_new@ */
883 static PyObject
*keydataec_pynew(PyTypeObject
*ty
,
884 PyObject
*arg
, PyObject
*kw
)
888 keydata_pyobj
*me
= 0;
889 static char *kwlist
[] = { "key", "flags", 0 };
891 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", kwlist
,
892 convecpt
, &x
, convflags
, &f
))
894 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
895 me
->kd
= key_newec(f
& ~KF_ENCMASK
, &x
);
898 return ((PyObject
*)me
);
901 static PyObject
*kdeget_ecpt(PyObject
*me
, void *hunoz
)
904 EC_COPY(&pt
, &KEYDATA_KD(me
)->u
.e
);
905 return (ecpt_pywrapout(ecpt_pytype
, &pt
));
908 static PyGetSetDef keydataec_pygetset
[] = {
909 #define GETSETNAME(op, name) kde##op##_##name
910 GET (ecpt
, "KD.ecpt -> ECPT")
915 static PyTypeObject keydataec_pytype_skel
= {
916 PyObject_HEAD_INIT(0) 0, /* Header */
917 "catacomb.KeyDataECPt", /* @tp_name@ */
918 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
919 0, /* @tp_itemsize@ */
921 0, /* @tp_dealloc@ */
923 0, /* @tp_getattr@ */
924 0, /* @tp_setattr@ */
925 0, /* @tp_compare@ */
927 0, /* @tp_as_number@ */
928 0, /* @tp_as_sequence@ */
929 0, /* @tp_as_mapping@ */
933 0, /* @tp_getattro@ */
934 0, /* @tp_setattro@ */
935 0, /* @tp_as_buffer@ */
936 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
940 "Key data for elliptic-curve keys.",
942 0, /* @tp_traverse@ */
944 0, /* @tp_richcompare@ */
945 0, /* @tp_weaklistoffset@ */
947 0, /* @tp_iternext@ */
948 0, /* @tp_methods@ */
949 0, /* @tp_members@ */
950 keydataec_pygetset
, /* @tp_getset@ */
953 0, /* @tp_descr_get@ */
954 0, /* @tp_descr_set@ */
955 0, /* @tp_dictoffset@ */
957 PyType_GenericAlloc
, /* @tp_alloc@ */
958 keydataec_pynew
, /* @tp_new@ */
963 static PyObject
*subkeyiter_make(PyObject
*kdobj
)
965 subkeyiter_pyobj
*me
= PyObject_NEW(subkeyiter_pyobj
, subkeyiter_pytype
);
968 key_mksubkeyiter(&me
->i
, KEYDATA_KD(kdobj
));
969 return ((PyObject
*)me
);
972 static PyObject
*subkeyiter_pynext(PyObject
*me
)
975 if (!key_nextsubkey(SUBKEYITER_I(me
), &tag
, 0))
977 return (PyString_FromString(tag
));
980 static void subkeyiter_pydealloc(PyObject
*me
)
982 Py_DECREF(SUBKEYITER_KDOBJ(me
));
986 static PyTypeObject subkeyiter_pytype_skel
= {
987 PyObject_HEAD_INIT(0) 0, /* Header */
988 "catacomb.SubKeyIter", /* @tp_name@ */
989 sizeof(subkeyiter_pyobj
), /* @tp_basicsize@ */
990 0, /* @tp_itemsize@ */
992 subkeyiter_pydealloc
, /* @tp_dealloc@ */
994 0, /* @tp_getattr@ */
995 0, /* @tp_setattr@ */
996 0, /* @tp_compare@ */
998 0, /* @tp_as_number@ */
999 0, /* @tp_as_sequence@ */
1000 0, /* @tp_as_mapping@ */
1004 0, /* @tp_getattro@ */
1005 0, /* @tp_setattro@ */
1006 0, /* @tp_as_buffer@ */
1007 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1008 Py_TPFLAGS_BASETYPE
,
1011 "Iterator for structured keys.",
1013 0, /* @tp_traverse@ */
1015 0, /* @tp_richcompare@ */
1016 0, /* @tp_weaklistoffset@ */
1017 PyObject_SelfIter
, /* @tp_iter@ */
1018 subkeyiter_pynext
, /* @tp_iternext@ */
1019 0, /* @tp_methods@ */
1020 0, /* @tp_members@ */
1021 0, /* @tp_getset@ */
1024 0, /* @tp_descr_get@ */
1025 0, /* @tp_descr_set@ */
1026 0, /* @tp_dictoffset@ */
1028 PyType_GenericAlloc
, /* @tp_alloc@ */
1029 abstract_pynew
, /* @tp_new@ */
1034 static PyObject
*keydatastruct_pynew(PyTypeObject
*ty
,
1035 PyObject
*arg
, PyObject
*kw
)
1038 PyObject
*it
= 0, *name
= 0, *val
= 0;
1040 keydata_pyobj
*me
= 0;
1042 static char *kwlist
[] = { "subkeys", 0 };
1044 Py_XINCREF(arg
); Py_XINCREF(kw
);
1045 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:new", kwlist
, &sub
))
1047 kd
= key_newstruct();
1049 if (!PyMapping_Check(sub
))
1050 TYERR("subkeys must be an iterable mapping");
1051 if ((it
= PyObject_GetIter(sub
)) == 0)
1053 while ((name
= PyIter_Next(it
)) != 0) {
1054 if ((p
= PyString_AsString(name
)) == 0 ||
1055 (val
= PyObject_GetItem(sub
, name
)) == 0)
1057 if (!KEYDATA_PYCHECK(val
))
1058 TYERR("subkey objects must be subclasses of KeyData");
1059 key_structset(kd
, p
, KEYDATA_KD(val
));
1060 Py_DECREF(name
); name
= 0;
1061 Py_DECREF(val
); val
= 0;
1063 if (PyErr_Occurred())
1065 Py_DECREF(it
); it
= 0;
1067 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
1070 if (kd
&& !me
) key_drop(kd
);
1071 Py_XDECREF(name
); Py_XDECREF(val
); Py_XDECREF(it
);
1072 Py_XDECREF(arg
); Py_XDECREF(kw
);
1073 return ((PyObject
*)me
);
1076 static PyObject
*keydatastruct_pylookup(PyObject
*me
, PyObject
*key
)
1082 if ((tag
= PyString_AsString(key
)) == 0)
1084 if ((kd
= key_structfind(KEYDATA_KD(me
), tag
)) == 0)
1087 rc
= keydata_pywrap(kd
);
1092 static int keydatastruct_pystore(PyObject
*me
,
1093 PyObject
*key
, PyObject
*value
)
1098 if ((tag
= PyString_AsString(key
)) == 0)
1101 if (!KEYDATA_PYCHECK(value
))
1102 TYERR("expected KeyData value");
1103 key_structset(KEYDATA_KD(me
), tag
, KEYDATA_KD(value
));
1105 if (!key_structfind(KEYDATA_KD(me
), tag
))
1107 key_structset(KEYDATA_KD(me
), tag
, 0);
1114 static PyMappingMethods keydatastruct_pymapping
= {
1115 gmap_pysize
, /* @mp_length@ */
1116 keydatastruct_pylookup
, /* @mp_subscript@ */
1117 keydatastruct_pystore
/* @mp_ass_subscript@ */
1120 static PyTypeObject keydatastruct_pytype_skel
= {
1121 PyObject_HEAD_INIT(0) 0, /* Header */
1122 "catacomb.KeyDataStructured", /* @tp_name@ */
1123 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
1124 0, /* @tp_itemsize@ */
1126 0, /* @tp_dealloc@ */
1128 0, /* @tp_getattr@ */
1129 0, /* @tp_setattr@ */
1130 0, /* @tp_compare@ */
1132 0, /* @tp_as_number@ */
1133 0, /* @tp_as_sequence@ */
1134 &keydatastruct_pymapping
, /* @tp_as_mapping@ */
1138 0, /* @tp_getattro@ */
1139 0, /* @tp_setattro@ */
1140 0, /* @tp_as_buffer@ */
1141 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1142 Py_TPFLAGS_BASETYPE
,
1145 "Key data for structured keys.",
1147 0, /* @tp_traverse@ */
1149 0, /* @tp_richcompare@ */
1150 0, /* @tp_weaklistoffset@ */
1151 subkeyiter_make
, /* @tp_iter@ */
1152 0, /* @tp_iternext@ */
1153 gmap_pymethods
, /* @tp_methods@ */
1154 0, /* @tp_members@ */
1155 0, /* @tp_getset@ */
1158 0, /* @tp_descr_get@ */
1159 0, /* @tp_descr_set@ */
1160 0, /* @tp_dictoffset@ */
1162 PyType_GenericAlloc
, /* @tp_alloc@ */
1163 keydatastruct_pynew
, /* @tp_new@ */
1168 /*----- Key attributes ----------------------------------------------------*/
1170 static PyObject
*keyattriter_make(PyObject
*kaobj
)
1172 PyObject
*kobj
= KEYATTRS_KOBJ(kaobj
);
1173 keyattriter_pyobj
*me
= PyObject_NEW(keyattriter_pyobj
,
1174 keyattriter_pytype
);
1177 key_mkattriter(&me
->i
, KEY_K(kobj
));
1178 return ((PyObject
*)me
);
1181 static PyObject
*keyattriter_pynext(PyObject
*me
)
1185 if (!key_nextattr(KEYATTRITER_I(me
), &name
, 0))
1187 return (PyString_FromString(name
));
1190 static void keyattriter_pydealloc(PyObject
*me
)
1192 Py_DECREF(KEYATTRITER_KOBJ(me
));
1196 static PyTypeObject keyattriter_pytype_skel
= {
1197 PyObject_HEAD_INIT(0) 0, /* Header */
1198 "catacomb.KeyAttributeIter", /* @tp_name@ */
1199 sizeof(keyattriter_pyobj
), /* @tp_basicsize@ */
1200 0, /* @tp_itemsize@ */
1202 keyattriter_pydealloc
, /* @tp_dealloc@ */
1204 0, /* @tp_getattr@ */
1205 0, /* @tp_setattr@ */
1206 0, /* @tp_compare@ */
1208 0, /* @tp_as_number@ */
1209 0, /* @tp_as_sequence@ */
1210 0, /* @tp_as_mapping@ */
1214 0, /* @tp_getattro@ */
1215 0, /* @tp_setattro@ */
1216 0, /* @tp_as_buffer@ */
1217 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1218 Py_TPFLAGS_BASETYPE
,
1221 "Iterator for key attributes.",
1223 0, /* @tp_traverse@ */
1225 0, /* @tp_richcompare@ */
1226 0, /* @tp_weaklistoffset@ */
1227 PyObject_SelfIter
, /* @tp_iter@ */
1228 keyattriter_pynext
, /* @tp_iternext@ */
1229 0, /* @tp_methods@ */
1230 0, /* @tp_members@ */
1231 0, /* @tp_getset@ */
1234 0, /* @tp_descr_get@ */
1235 0, /* @tp_descr_set@ */
1236 0, /* @tp_dictoffset@ */
1238 PyType_GenericAlloc
, /* @tp_alloc@ */
1239 abstract_pynew
, /* @tp_new@ */
1244 static PyObject
*keyattrs_pylookup(PyObject
*me
, PyObject
*key
)
1250 if ((attr
= PyString_AsString(key
)) == 0)
1252 if ((value
= key_getattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
)) == 0)
1254 rc
= PyString_FromString(value
);
1259 static int keyattrs_pystore(PyObject
*me
,
1260 PyObject
*key
, PyObject
*value
)
1267 if ((attr
= PyString_AsString(key
)) == 0)
1270 if ((val
= PyString_AsString(value
)) == 0)
1272 if ((err
= key_putattr(KEYATTRS_KF(me
), KEYATTRS_K(me
),
1276 if (!key_getattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
))
1278 if ((err
= key_putattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
, 0)) != 0)
1286 static PyObject
*keyattrs_make(PyObject
*kobj
)
1288 keyattrs_pyobj
*me
= PyObject_NEW(keyattrs_pyobj
, keyattrs_pytype
);
1291 return ((PyObject
*)me
);
1294 static void keyattrs_pydealloc(PyObject
*me
)
1296 Py_DECREF(KEYATTRS_KOBJ(me
));
1300 static PyMappingMethods keyattrs_pymapping
= {
1301 gmap_pysize
, /* @mp_length@ */
1302 keyattrs_pylookup
, /* @mp_subscript@ */
1303 keyattrs_pystore
/* @mp_ass_subscript@ */
1306 static PyTypeObject keyattrs_pytype_skel
= {
1307 PyObject_HEAD_INIT(0) 0, /* Header */
1308 "catacomb.KeyAttributes", /* @tp_name@ */
1309 sizeof(keyattrs_pyobj
), /* @tp_basicsize@ */
1310 0, /* @tp_itemsize@ */
1312 keyattrs_pydealloc
, /* @tp_dealloc@ */
1314 0, /* @tp_getattr@ */
1315 0, /* @tp_setattr@ */
1316 0, /* @tp_compare@ */
1318 0, /* @tp_as_number@ */
1319 &gmap_pysequence
, /* @tp_as_sequence@ */
1320 &keyattrs_pymapping
, /* @tp_as_mapping@ */
1324 0, /* @tp_getattro@ */
1325 0, /* @tp_setattro@ */
1326 0, /* @tp_as_buffer@ */
1327 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1328 Py_TPFLAGS_BASETYPE
,
1331 "Proxy thing for talking about key attributes.",
1333 0, /* @tp_traverse@ */
1335 0, /* @tp_richcompare@ */
1336 0, /* @tp_weaklistoffset@ */
1337 keyattriter_make
, /* @tp_iter@ */
1338 0, /* @tp_iternext@ */
1339 gmap_pymethods
, /* @tp_methods@ */
1340 0, /* @tp_members@ */
1341 0, /* @tp_getset@ */
1344 0, /* @tp_descr_get@ */
1345 0, /* @tp_descr_set@ */
1346 0, /* @tp_dictoffset@ */
1348 PyType_GenericAlloc
, /* @tp_alloc@ */
1349 abstract_pynew
, /* @tp_new@ */
1354 /*----- Key objects -------------------------------------------------------*/
1356 static PyObject
*key_dowrap(PyTypeObject
*ty
, PyObject
*kfobj
, key
*k
)
1358 key_pyobj
*kobj
= (key_pyobj
*)ty
->tp_alloc(ty
, 0);
1359 kobj
->kfobj
= kfobj
;
1362 return ((PyObject
*)kobj
);
1365 static PyObject
*key_pywrap(PyObject
*kfobj
, key
*k
)
1366 { return (key_dowrap(key_pytype
, kfobj
, k
)); }
1368 static PyObject
*key_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1373 long exptime
= KEXP_FOREVER
;
1374 static char *kwlist
[] = { "keyfile", "id", "type", "exptime", 0 };
1378 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O&sl:new", kwlist
,
1379 keyfile_pytype
, &kfobj
, convu32
, &id
,
1382 if ((err
= key_new(KEYFILE_KF(kfobj
), id
, type
, exptime
, &k
)) == 0)
1384 return (key_dowrap(ty
, kfobj
, k
));
1389 static void key_pydealloc(PyObject
*me
)
1391 Py_DECREF(KEY_KFOBJ(me
));
1395 static PyObject
*kmeth_delete(PyObject
*me
, PyObject
*arg
)
1399 if (!PyArg_ParseTuple(arg
, ":delete")) goto end
;
1400 if ((err
= key_delete(KEY_KF(me
), KEY_K(me
))) != 0) KEYERR(err
);
1406 static PyObject
*kmeth_expire(PyObject
*me
, PyObject
*arg
)
1410 if (!PyArg_ParseTuple(arg
, ":expire")) goto end
;
1411 if ((err
= key_expire(KEY_KF(me
), KEY_K(me
))) != 0) KEYERR(err
);
1417 static PyObject
*kmeth_used(PyObject
*me
, PyObject
*arg
)
1422 if (!PyArg_ParseTuple(arg
, "l:used", &t
)) goto end
;
1423 if ((err
= key_used(KEY_KF(me
), KEY_K(me
), t
)) != 0) KEYERR(err
);
1429 static PyObject
*kmeth_extract(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1431 key_filter f
= { 0, 0 };
1436 static char *kwlist
[] = { "file", "filter", 0 };
1438 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!|O&:extract", kwlist
,
1439 &PyFile_Type
, &file
,
1441 (fp
= PyFile_AsFile(file
)) == 0 ||
1442 (nameobj
= PyFile_Name(file
)) == 0 ||
1443 (name
= PyString_AsString(nameobj
)) == 0)
1445 if (key_extract(KEY_KF(me
), KEY_K(me
), fp
, &f
))
1452 static PyObject
*kmeth_fingerprint(PyObject
*me
,
1453 PyObject
*arg
, PyObject
*kw
)
1456 key_filter f
= { KF_NONSECRET
, KF_NONSECRET
};
1457 static char *kwlist
[] = { "hash", "filter", 0 };
1459 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:fingerprint", kwlist
,
1460 convghash
, &h
, convfilter
, &f
))
1462 return (getbool(key_fingerprint(KEY_K(me
), h
, &f
)));
1465 static PyObject
*kget_id(PyObject
*me
, void *hunoz
)
1466 { return (getulong(KEY_K(me
)->id
)); }
1467 static PyObject
*kget_file(PyObject
*me
, void *hunoz
)
1468 { RETURN_OBJ(KEY_KFOBJ(me
)); }
1469 static PyObject
*kget_type(PyObject
*me
, void *hunoz
)
1470 { return (PyString_FromString(KEY_K(me
)->type
)); }
1471 static PyObject
*kget_exptime(PyObject
*me
, void *hunoz
)
1472 { return (getulong(KEY_K(me
)->exp
)); }
1473 static PyObject
*kget_deltime(PyObject
*me
, void *hunoz
)
1474 { return (getulong(KEY_K(me
)->del
)); }
1475 static PyObject
*kget_expiredp(PyObject
*me
, void *hunoz
)
1476 { return (getbool(key_expired(KEY_K(me
)))); }
1477 static PyObject
*kget_attr(PyObject
*me
, void *hunoz
)
1478 { return (keyattrs_make(me
)); }
1480 static int kset_exptime(PyObject
*me
, PyObject
*x
, void *hunoz
)
1485 if (!convulong(x
, &et
))
1487 if (!(KEY_KF(me
)->f
& KF_WRITE
))
1488 KEYERR(KERR_READONLY
);
1490 KEY_KF(me
)->f
|= KF_MODIFIED
;
1496 static int kset_deltime(PyObject
*me
, PyObject
*x
, void *hunoz
)
1501 if (!convulong(x
, &dt
))
1503 if (dt
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
)
1504 VALERR("key will eventually expire");
1505 if (!(KEY_KF(me
)->f
& KF_WRITE
))
1506 KEYERR(KERR_READONLY
);
1508 KEY_KF(me
)->f
|= KF_MODIFIED
;
1514 static PyObject
*kget_data(PyObject
*me
, void *hunoz
)
1516 key_data
*kd
= KEY_K(me
)->k
;
1518 return (keydata_pywrap(kd
));
1520 static int kset_data(PyObject
*me
, PyObject
*x
, void *hunoz
)
1524 if (!x
) NIERR("__del__");
1525 if (!KEYDATA_PYCHECK(x
)) TYERR("expected KeyData object");
1526 if ((err
= key_setkeydata(KEY_KF(me
), KEY_K(me
), KEYDATA_KD(x
))) != 0)
1533 static PyObject
*kget_fulltag(PyObject
*me
, void *hunoz
)
1538 key_fulltag(KEY_K(me
), &d
);
1539 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
1544 static PyObject
*kget_tag(PyObject
*me
, void *hunoz
)
1546 if (!KEY_K(me
)->tag
) RETURN_NONE
;
1547 return (PyString_FromString(KEY_K(me
)->tag
));
1549 static int kset_tag(PyObject
*me
, PyObject
*x
, void *hunoz
)
1554 if (!x
|| x
== Py_None
) tag
= 0;
1555 else if ((tag
= PyString_AsString(x
)) == 0) goto end
;
1556 if ((err
= key_settag(KEY_KF(me
), KEY_K(me
), tag
)) != 0) KEYERR(err
);
1562 static PyObject
*kget_comment(PyObject
*me
, void *hunoz
)
1564 if (!KEY_K(me
)->c
) RETURN_NONE
;
1565 return (PyString_FromString(KEY_K(me
)->c
));
1567 static int kset_comment(PyObject
*me
, PyObject
*x
, void *hunoz
)
1572 if (!x
|| x
== Py_None
) c
= 0;
1573 else if ((c
= PyString_AsString(x
)) == 0) goto end
;
1574 if ((err
= key_setcomment(KEY_KF(me
), KEY_K(me
), c
)) != 0) KEYERR(err
);
1580 static PyMethodDef key_pymethods
[] = {
1581 #define METHNAME(func) kmeth_##func
1582 METH (delete, "KEY.delete()")
1583 METH (expire
, "KEY.expire()")
1584 METH (used
, "KEY.used(TIME)")
1585 KWMETH(extract
, "KEY.extract(FILE, filter = '')")
1586 KWMETH(fingerprint
, "KEY.fingerprint(HASH, filtr = '-secret')")
1591 static PyGetSetDef key_pygetset
[] = {
1592 #define GETSETNAME(op, name) k##op##_##name
1593 GET (file
, "KEY.file -> KF")
1594 GET (id
, "KEY.id -> ID")
1595 GETSET(tag
, "KEY.tag -> TAG")
1596 GET (type
, "KEY.type -> TYPE")
1597 GETSET(exptime
, "KEY.exptime -> TIME")
1598 GETSET(deltime
, "KEY.deltime -> TIME")
1599 GET (expiredp
, "KEY.expiredp -> BOOL")
1600 GET (attr
, "KEY.attr -> ATTRIBUTES")
1601 GETSET(data
, "KEY.data -> KD")
1602 GETSET(comment
, "KEY.comment -> COMMENT")
1603 GET (fulltag
, "KEY.fulltag -> FULLTAG")
1608 static PyTypeObject key_pytype_skel
= {
1609 PyObject_HEAD_INIT(0) 0, /* Header */
1610 "catacomb.Key", /* @tp_name@ */
1611 sizeof(key_pyobj
), /* @tp_basicsize@ */
1612 0, /* @tp_itemsize@ */
1614 key_pydealloc
, /* @tp_dealloc@ */
1616 0, /* @tp_getattr@ */
1617 0, /* @tp_setattr@ */
1618 0, /* @tp_compare@ */
1620 0, /* @tp_as_number@ */
1621 0, /* @tp_as_sequence@ */
1622 0, /* @tp_as_mapping@ */
1626 0, /* @tp_getattro@ */
1627 0, /* @tp_setattro@ */
1628 0, /* @tp_as_buffer@ */
1629 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1630 Py_TPFLAGS_BASETYPE
,
1635 0, /* @tp_traverse@ */
1637 0, /* @tp_richcompare@ */
1638 0, /* @tp_weaklistoffset@ */
1640 0, /* @tp_iternext@ */
1641 key_pymethods
, /* @tp_methods@ */
1642 0, /* @tp_members@ */
1643 key_pygetset
, /* @tp_getset@ */
1646 0, /* @tp_descr_get@ */
1647 0, /* @tp_descr_set@ */
1648 0, /* @tp_dictoffset@ */
1650 PyType_GenericAlloc
, /* @tp_alloc@ */
1651 key_pynew
, /* @tp_new@ */
1656 /*----- Key iteration -----------------------------------------------------*/
1658 static PyObject
*keyiter_new(PyObject
*kfobj
)
1660 keyiter_pyobj
*me
= PyObject_NEW(keyiter_pyobj
, keyiter_pytype
);
1661 key_mkiter(&me
->i
, KEYFILE_KF(kfobj
));
1664 return ((PyObject
*)me
);
1667 static PyObject
*keyiter_pynext(PyObject
*me
)
1671 if ((k
= key_next(KEYITER_I(me
))) == 0)
1673 return (getulong(k
->id
));
1676 static void keyiter_pydealloc(PyObject
*me
)
1678 Py_DECREF(KEYITER_KFOBJ(me
));
1682 static PyTypeObject keyiter_pytype_skel
= {
1683 PyObject_HEAD_INIT(0) 0, /* Header */
1684 "catacomb.KeyFileIter", /* @tp_name@ */
1685 sizeof(keyiter_pyobj
), /* @tp_basicsize@ */
1686 0, /* @tp_itemsize@ */
1688 keyiter_pydealloc
, /* @tp_dealloc@ */
1690 0, /* @tp_getattr@ */
1691 0, /* @tp_setattr@ */
1692 0, /* @tp_compare@ */
1694 0, /* @tp_as_number@ */
1695 0, /* @tp_as_sequence@ */
1696 0, /* @tp_as_mapping@ */
1700 0, /* @tp_getattro@ */
1701 0, /* @tp_setattro@ */
1702 0, /* @tp_as_buffer@ */
1703 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1704 Py_TPFLAGS_BASETYPE
,
1707 "Keyring iterator.",
1709 0, /* @tp_traverse@ */
1711 0, /* @tp_richcompare@ */
1712 0, /* @tp_weaklistoffset@ */
1713 PyObject_SelfIter
, /* @tp_iter@ */
1714 keyiter_pynext
, /* @tp_iternext@ */
1715 0, /* @tp_methods@ */
1716 0, /* @tp_members@ */
1717 0, /* @tp_getset@ */
1720 0, /* @tp_descr_get@ */
1721 0, /* @tp_descr_set@ */
1722 0, /* @tp_dictoffset@ */
1724 PyType_GenericAlloc
, /* @tp_alloc@ */
1725 abstract_pynew
, /* @tp_new@ */
1730 /*----- Key files ---------------------------------------------------------*/
1737 static void pythonreporter(const char *file
, int line
,
1738 const char *msg
, void *p
)
1740 struct reportinfo
*ri
= p
;
1746 key_moan(file
, line
, msg
, 0);
1747 else if ((res
= PyObject_CallFunction(ri
->func
, "sis",
1748 file
, line
, msg
)) == 0)
1754 static PyObject
*keyfile_pynew(PyTypeObject
*ty
,
1755 PyObject
*arg
, PyObject
*kw
)
1757 struct reportinfo ri
= { 0, 0 };
1759 unsigned how
= KOPEN_READ
;
1760 keyfile_pyobj
*rc
= 0;
1761 static char *kwlist
[] = { "file", "how", "report", 0 };
1763 Py_XINCREF(arg
); Py_XINCREF(kw
);
1764 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|iO:new", kwlist
,
1765 &file
, &how
, &ri
.func
))
1767 if (ri
.func
&& !PyCallable_Check(ri
.func
))
1768 TYERR("reporter function not callable");
1769 if ((rc
= (keyfile_pyobj
*)ty
->tp_alloc(ty
, 0)) == 0)
1771 if (key_open(&rc
->kf
, file
, how
, pythonreporter
, &ri
))
1774 key_discard(&rc
->kf
);
1785 Py_XDECREF(arg
); Py_XDECREF(kw
);
1786 return ((PyObject
*)rc
);
1789 static void keyfile_pydealloc(PyObject
*me
)
1791 key_discard(KEYFILE_KF(me
));
1795 static PyObject
*kfmeth_save(PyObject
*me
, PyObject
*arg
)
1797 if (!PyArg_ParseTuple(arg
, ":save"))
1799 switch (key_save(KEYFILE_KF(me
))) {
1803 KEYIOERR(KEYFILE_KF(me
)->name
);
1805 KEYFILEBROKEN(KEYFILE_KF(me
)->name
);
1813 static PyObject
*kfmeth_merge(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1815 struct reportinfo ri
= { 0, 0 };
1820 static char *kwlist
[] = { "file", "report", 0 };
1822 Py_XINCREF(arg
); Py_XINCREF(kw
);
1823 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!|O:merge", kwlist
,
1824 &PyFile_Type
, &x
, &ri
.func
))
1826 if (ri
.func
&& !PyCallable_Check(ri
.func
))
1827 TYERR("reporter function not callable");
1828 if ((fp
= PyFile_AsFile(x
)) == 0)
1831 if ((name
= PyString_AsString(x
)) == 0)
1833 rc
= key_merge(KEYFILE_KF(me
), name
, fp
, pythonreporter
, &ri
);
1838 Py_XDECREF(arg
); Py_XDECREF(kw
);
1842 Py_XDECREF(arg
); Py_XDECREF(kw
);
1846 static PyObject
*kfmeth_byid(PyObject
*me
, PyObject
*arg
)
1852 if (!PyArg_ParseTuple(arg
, "O&:byid", convu32
, &id
)) goto end
;
1853 if ((k
= key_byid(KEYFILE_KF(me
), id
)) == 0) KEYERR(KERR_NOTFOUND
);
1854 rc
= key_pywrap(me
, k
);
1859 static PyObject
*kfmeth_bytype(PyObject
*me
, PyObject
*arg
)
1865 if (!PyArg_ParseTuple(arg
, "s:bytype", &type
)) goto end
;
1866 if ((k
= key_bytype(KEYFILE_KF(me
), type
)) == 0) RETURN_NONE
;
1867 rc
= key_pywrap(me
, k
);
1872 static PyObject
*bytag(PyObject
*me
, PyObject
*tagobj
)
1879 if (convu32(tagobj
, &id
))
1880 k
= key_byid(KEYFILE_KF(me
), id
);
1883 if ((tag
= PyString_AsString(tagobj
)) == 0)
1885 k
= key_bytag(KEYFILE_KF(me
), tag
);
1887 if (!k
) RETURN_NONE
;
1888 rc
= key_pywrap(me
, k
);
1893 static PyObject
*kfmeth_bytag(PyObject
*me
, PyObject
*arg
)
1897 if (!PyArg_ParseTuple(arg
, "O:bytag", &tagobj
)) return (0);
1898 return (bytag(me
, tagobj
));
1901 static PyObject
*keyfile_pylookup(PyObject
*me
, PyObject
*key
)
1903 PyObject
*rc
= bytag(me
, key
);
1905 if (rc
== Py_None
) {
1914 static PyObject
*kfmeth_newkey(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1918 long exptime
= KEXP_FOREVER
;
1919 static char *kwlist
[] = { "id", "type", "exptime", 0 };
1923 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&s|l:newkey", kwlist
,
1924 convu32
, &id
, &type
, &exptime
))
1926 if ((err
= key_new(KEYFILE_KF(me
), id
, type
, exptime
, &k
)) != 0)
1928 return (key_pywrap(me
, k
));
1933 static PyObject
*kfmeth_qtag(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1936 key_data
**kd
, *okd
;
1937 PyObject
*newkdobj
= 0;
1941 static char *kwlist
[] = { "tag", "new", 0 };
1943 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O!:qtag", kwlist
,
1944 &tag
, keydata_pytype
, &newkdobj
))
1946 if (key_qtag(KEYFILE_KF(me
), tag
, &d
, &k
, &kd
))
1947 KEYERR(KERR_NOTFOUND
);
1950 if (!(KEYFILE_KF(me
)->f
& KF_WRITE
))
1951 KEYERR(KERR_READONLY
);
1952 KEYFILE_KF(me
)->f
|= KF_MODIFIED
;
1953 *kd
= KEYDATA_KD(newkdobj
);
1956 rc
= Py_BuildValue("(s#NN)",
1959 keydata_pywrap(okd
));
1964 static PyObject
*kfget_name(PyObject
*me
, void *hunoz
)
1965 { return (PyString_FromString(KEYFILE_KF(me
)->name
)); }
1966 static PyObject
*kfget_modifiedp(PyObject
*me
, void *hunoz
)
1967 { return (getbool(KEYFILE_KF(me
)->f
& KF_MODIFIED
)); }
1968 static PyObject
*kfget_writep(PyObject
*me
, void *hunoz
)
1969 { return (getbool(KEYFILE_KF(me
)->f
& KF_WRITE
)); }
1970 static PyObject
*kfget_filep(PyObject
*me
, void *hunoz
)
1971 { return (getbool(!!KEYFILE_KF(me
)->fp
)); }
1973 static PyMethodDef keyfile_pymethods
[] = {
1974 #define METHNAME(func) kfmeth_##func
1975 METH (save
, "KF.save()")
1976 KWMETH(merge
, "KF.merge(FILE, report = <built-in-reporter>)")
1977 KWMETH(newkey
, "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY")
1978 METH (byid
, "KF.byid(KEYID) -> KEY|None")
1979 METH (bytype
, "KF.bytype(TYPE) -> KEY|None")
1980 METH (bytag
, "KF.bytag(TAG) -> KEY|None")
1981 KWMETH(qtag
, "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD")
1987 static PyGetSetDef keyfile_pygetset
[] = {
1988 #define GETSETNAME(op, name) kf##op##_##name
1989 GET (name
, "KF.name -> file name")
1990 GET (modifiedp
, "KF.modifiedp -> has keyring been modified?")
1991 GET (writep
, "KF.writep -> is keyring modifiable?")
1992 GET (filep
, "KF.filep -> does keyring have a backing file?")
1997 static PyMappingMethods keyfile_pymapping
= {
2003 static PyTypeObject keyfile_pytype_skel
= {
2004 PyObject_HEAD_INIT(0) 0, /* Header */
2005 "catacomb.KeyFile", /* @tp_name@ */
2006 sizeof(keyfile_pyobj
), /* @tp_basicsize@ */
2007 0, /* @tp_itemsize@ */
2009 keyfile_pydealloc
, /* @tp_dealloc@ */
2011 0, /* @tp_getattr@ */
2012 0, /* @tp_setattr@ */
2013 0, /* @tp_compare@ */
2015 0, /* @tp_as_number@ */
2016 &gmap_pysequence
, /* @tp_as_sequence@ */
2017 &keyfile_pymapping
, /* @tp_as_mapping@ */
2021 0, /* @tp_getattro@ */
2022 0, /* @tp_setattro@ */
2023 0, /* @tp_as_buffer@ */
2024 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2025 Py_TPFLAGS_BASETYPE
,
2030 0, /* @tp_traverse@ */
2032 0, /* @tp_richcompare@ */
2033 0, /* @tp_weaklistoffset@ */
2034 keyiter_new
, /* @tp_iter@ */
2035 0, /* @tp_iternext@ */
2036 keyfile_pymethods
, /* @tp_methods@ */
2037 0, /* @tp_members@ */
2038 keyfile_pygetset
, /* @tp_getset@ */
2041 0, /* @tp_descr_get@ */
2042 0, /* @tp_descr_set@ */
2043 0, /* @tp_dictoffset@ */
2045 PyType_GenericAlloc
, /* @tp_alloc@ */
2046 keyfile_pynew
, /* @tp_new@ */
2051 /*----- Global stuff ------------------------------------------------------*/
2053 static PyObject
*meth_barf(PyObject
*me
, PyObject
*arg
)
2057 if (PyArg_ParseTuple(arg
, "i:barf", &err
))
2063 static PyMethodDef methods
[] = {
2064 #define METHNAME(func) meth_##func
2065 METH (_KeyData_readflags
,
2066 "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)")
2067 METH (_KeyData_writeflags
, "KeyData.writeflags(FLAGS) -> STRING")
2068 METH (_KeyData_read
, "KeyData.read(STRING) -> (KD, REST)")
2069 METH (_KeyData_decode
, "KeyData.read(BYTES) -> KD")
2070 METH (barf
, "barf(ERR)")
2075 /*----- Initialization ----------------------------------------------------*/
2077 void key_pyinit(void)
2079 INITTYPE(keyfile
, root
);
2080 INITTYPE(key
, root
);
2081 INITTYPE(keyiter
, root
);
2082 INITTYPE(keydata
, root
);
2083 INITTYPE(keydatabin
, keydata
);
2084 INITTYPE(keydataenc
, keydata
);
2085 INITTYPE(keydatastr
, keydata
);
2086 INITTYPE(keydatamp
, keydata
);
2087 INITTYPE(keydataec
, keydata
);
2088 INITTYPE(keydatastruct
, keydata
);
2089 INITTYPE(subkeyiter
, root
);
2090 INITTYPE(keyattrs
, root
);
2091 INITTYPE(keyattriter
, root
);
2092 addmethods(methods
);
2095 void key_pyinsert(PyObject
*mod
)
2097 INSEXC("KeyError", keyexc
, PyExc_Exception
, keyexc_pymethods
);
2098 INSEXC("KeyFileIOError", keyioexc
, PyExc_OSError
, 0);
2099 INSEXC("KeyFileBroken", keyfilebrokenexc
, keyioexc
, 0);
2100 INSERT("KeyFile", keyfile_pytype
);
2101 INSERT("KeyFileIter", keyiter_pytype
);
2102 INSERT("Key", key_pytype
);
2103 INSERT("KeyAttributes", keyattrs_pytype
);
2104 INSERT("KeyAttributeIter", keyattriter_pytype
);
2105 INSERT("KeyData", keydata_pytype
);
2106 INSERT("KeyDataBinary", keydatabin_pytype
);
2107 INSERT("KeyDataEncrypted", keydataenc_pytype
);
2108 INSERT("KeyDataMP", keydatamp_pytype
);
2109 INSERT("KeyDataECPt", keydataec_pytype
);
2110 INSERT("KeyDataString", keydatastr_pytype
);
2111 INSERT("KeyDataStructured", keydatastruct_pytype
);
2112 INSERT("SubKeyIter", subkeyiter_pytype
);
2115 /*----- That's all, folks -------------------------------------------------*/