5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Exceptions --------------------------------------------------------*/
33 static PyObject
*keyexc
;
34 static PyObject
*keyioexc
;
35 static PyObject
*keyfilebrokenexc
;
37 static PyObject
*kxmeth___init__(PyObject
*me
, PyObject
*arg
)
42 if (!PyArg_ParseTuple(arg
, "Oi:__init__", &me
, &err
) ||
43 (x
= PyInt_FromLong(err
)) == 0 ||
44 PyObject_SetAttrString(me
, "err", x
))
47 if ((x
= PyString_FromString(key_strerror(err
))) == 0 ||
48 PyObject_SetAttrString(me
, "errstring", x
))
51 if ((x
= PyString_FromString(key_strerror(err
))) == 0 ||
52 PyObject_SetAttrString(me
, "errstring", x
))
55 if ((x
= PySequence_GetSlice(arg
, 1, PySequence_Size(arg
))) == 0 ||
56 PyObject_SetAttrString(me
, "args", x
))
66 static PyObject
*kxmeth___str__(PyObject
*me
, PyObject
*arg
)
69 const char *errstr
, *errtag
;
73 static const char *const tab
[] = {
74 #define ENTRY(tag, num, str) "KERR_" #tag,
79 if (!PyArg_ParseTuple(arg
, "O:__str__", &me
) ||
80 (x
= PyObject_GetAttrString(me
, "err")) == 0 ||
81 (err
= PyInt_AsLong(x
), PyErr_Occurred()))
85 if (err
>= 0 && err
< N(tab
)) errtag
= tab
[err
];
86 else errtag
= "<unknown>";
87 if ((x
= PyObject_GetAttrString(me
, "errstring")) == 0 ||
88 (errstr
= PyString_AsString(x
)) == 0)
90 rc
= PyString_FromFormat("%s (%ld): %s", errtag
, -err
, errstr
);
97 static PyMethodDef keyexc_pymethods
[] = {
98 #define METHNAME(func) kxmeth_##func
99 METH (__init__
, "KeyError(CODE)")
100 METH (__str__
, "E.__str__() -> STRING")
105 static void keyexc_raise(int err
)
107 PyObject
*arg
= Py_BuildValue("(is)", err
, key_strerror(err
));
108 if (arg
) PyErr_SetObject(keyexc
, arg
);
111 #define KEYERR(err) do { keyexc_raise(err); goto end; } while (0)
112 #define KEYIOERR(name) do { \
113 PyErr_SetFromErrnoWithFilename(keyioexc, name); \
116 #define KEYFILEBROKEN(name) do { \
117 PyErr_SetFromErrnoWithFilename(keyfilebrokenexc, name); \
121 /*----- Data structures ---------------------------------------------------*/
123 typedef struct keydata_pyobj
{
128 static PyTypeObject
*keydata_pytype
;
129 static PyTypeObject
*keydatabin_pytype
;
130 static PyTypeObject
*keydataenc_pytype
;
131 static PyTypeObject
*keydatamp_pytype
;
132 static PyTypeObject
*keydatastruct_pytype
;
133 static PyTypeObject
*keydatastr_pytype
;
134 static PyTypeObject
*keydataec_pytype
;
135 #define KEYDATA_PYCHECK(o) PyObject_TypeCheck(o, keydata_pytype)
136 #define KEYDATA_KD(o) (((keydata_pyobj *)(o))->kd)
138 typedef struct subkeyiter_pyobj
{
144 static PyTypeObject
*subkeyiter_pytype
;
145 #define SUBKEYITER_PYCHECK(o) PyObject_TypeCheck(o, subkeyiter_pytype);
146 #define SUBKEYITER_I(o) (&((subkeyiter_pyobj *)(o))->i)
147 #define SUBKEYITER_KDOBJ(o) (((subkeyiter_pyobj *)(o))->kdobj)
149 typedef struct keyfile_pyobj
{
154 static PyTypeObject
*keyfile_pytype
;
155 #define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype)
156 #define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf)
158 typedef struct key_pyobj
{
164 static PyTypeObject
*key_pytype
;
165 #define KEY_PYCHECK(o) PyObject_TypeCheck(o, key_pytype)
166 #define KEY_K(o) (((key_pyobj *)(o))->k)
167 #define KEY_KFOBJ(o) (((key_pyobj *)(o))->kfobj)
168 #define KEY_KF(o) KEYFILE_KF(KEY_KFOBJ(o))
170 typedef struct keyiter_pyobj
{
176 static PyTypeObject
*keyiter_pytype
;
177 #define KEYITER_PYCHECK(o) PyObject_TypeCheck(o, keyiter_pytype)
178 #define KEYITER_I(o) (&((keyiter_pyobj *)(o))->i)
179 #define KEYITER_KFOBJ(o) (((keyiter_pyobj *)(o))->kfobj)
180 #define KEYITER_KF(o) KEYFILE_KF(KEYITER_KFOBJ(o))
182 typedef struct keyattrs_pyobj
{
187 static PyTypeObject
*keyattrs_pytype
;
188 #define KEYATTRS_PYCHECK(o) PyObject_TypeCheck(o, keyattrs_pytype)
189 #define KEYATTRS_KOBJ(o) (((keyattrs_pyobj *)(o))->kobj)
190 #define KEYATTRS_KF(o) KEY_KF(KEYATTRS_KOBJ(o))
191 #define KEYATTRS_K(o) KEY_K(KEYATTRS_KOBJ(o))
193 typedef struct keyattriter_pyobj
{
199 static PyTypeObject
*keyattriter_pytype
;
200 #define KEYATTRITER_PYCHECK(o) PyObject_TypeCheck(o, keyattriter_pytype)
201 #define KEYATTRITER_I(o) (&((keyattriter_pyobj *)(o))->i)
202 #define KEYATTRITER_KOBJ(o) (((keyattriter_pyobj *)(o))->kobj)
203 #define KEYATTRITER_K(o) KEY_K(KEYATTRITER_KOBJ(o))
204 #define KEYATTRITER_KFOBJ(o) KEY_KFOBJ(KEYATTRITER_KOBJ(o))
205 #define KEYATTRITER_KF(o) KEY_KF(KEYATTRITER_KOBJ(o))
207 /*----- Filters -----------------------------------------------------------*/
209 static int convfilter(PyObject
*x
, void *p
)
215 PyObject
*a
= 0, *b
= 0;
219 if ((fs
= PyString_AsString(x
)) != 0) {
220 if ((err
= key_readflags(fs
, &end
, &f
->f
, &f
->m
)) != 0)
223 KEYERR(KERR_BADFLAGS
);
226 if (!PySequence_Check(x
))
228 else if ((n
= PySequence_Size(x
)) < 0)
232 else if ((a
= PySequence_GetItem(x
, 0)) == 0 || convuint(a
, &f
->f
) ||
233 (b
= PySequence_GetItem(x
, 1)) == 0 || convuint(b
, &f
->m
))
239 TYERR("expected flag string or flag/mask pair");
246 static int convflags(PyObject
*x
, void *p
)
258 if ((fs
= PyString_AsString(x
)) != 0) {
259 if ((err
= key_readflags(fs
, &end
, f
, 0)) != 0)
262 KEYERR(KERR_BADFLAGS
);
271 TYERR("expected flag string or integer bitfield");
276 static PyObject
*meth__KeyData_readflags(PyObject
*me
, PyObject
*arg
)
284 if (!PyArg_ParseTuple(arg
, "Os:key_readflags", &me
, &p
))
286 if ((err
= key_readflags(p
, &end
, &f
, &m
)) != 0)
288 rc
= Py_BuildValue("(NNs)", getulong(f
), getulong(m
), end
);
293 static PyObject
*meth__KeyData_writeflags(PyObject
*me
, PyObject
*arg
)
299 if (!PyArg_ParseTuple(arg
, "OO&:key_writeflags", &me
, convuint
, &f
))
301 key_writeflags(f
, &d
);
302 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
307 /*----- Key data ----------------------------------------------------------*/
309 static PyObject
*keydata_pywrap(key_data
*kd
)
312 keydata_pyobj
*kdobj
;
314 switch (kd
->e
& KF_ENCMASK
) {
315 case KENC_BINARY
: ty
= keydatabin_pytype
; break;
316 case KENC_ENCRYPT
: ty
= keydataenc_pytype
; break;
317 case KENC_MP
: ty
= keydatamp_pytype
; break;
318 case KENC_STRUCT
: ty
= keydatastruct_pytype
; break;
319 case KENC_STRING
: ty
= keydatastr_pytype
; break;
320 case KENC_EC
: ty
= keydataec_pytype
; break;
323 kdobj
= PyObject_NEW(keydata_pyobj
, ty
);
325 return ((PyObject
*)kdobj
);
328 static void keydata_pydealloc(PyObject
*me
)
330 key_drop(KEYDATA_KD(me
));
334 static PyObject
*kdmeth_matchp(PyObject
*me
, PyObject
*arg
)
338 if (!PyArg_ParseTuple(arg
, "O&:matchp", convfilter
, &f
))
340 return (getbool(KEY_MATCH(KEYDATA_KD(me
), &f
)));
343 static PyObject
*kdmeth_split(PyObject
*me
, PyObject
*arg
)
345 if (!PyArg_ParseTuple(arg
, ":split"))
347 key_split(&KEYDATA_KD(me
));
351 static PyObject
*kdmeth_copy(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
353 key_filter f
= { 0, 0 };
354 static char *kwlist
[] = { "filter", 0 };
357 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:copy", kwlist
,
360 if ((kd
= key_copydata(KEYDATA_KD(me
), &f
)) == 0)
363 return (keydata_pywrap(kd
));
366 static PyObject
*kdmeth_write(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
368 key_filter f
= { 0, 0 };
371 static char *kwlist
[] = { "filter", 0 };
373 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:write", kwlist
,
376 key_write(KEYDATA_KD(me
), &d
, &f
);
377 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
382 static PyObject
*kdmeth_encode(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
384 key_filter f
= { 0, 0 };
387 static char *kwlist
[] = { "filter", 0 };
389 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:encode", kwlist
,
392 key_encode(KEYDATA_KD(me
), &d
, &f
);
393 rc
= bytestring_pywrap(d
.buf
, d
.len
);
398 static PyObject
*kdmeth_plock(PyObject
*me
, PyObject
*arg
)
405 if (!PyArg_ParseTuple(arg
, "s:plock", &tag
))
407 if ((err
= key_plock(&kd
, KEYDATA_KD(me
), tag
)) != 0)
409 rc
= keydata_pywrap(kd
);
414 static PyObject
*kdmeth_lock(PyObject
*me
, PyObject
*arg
)
421 if (!PyArg_ParseTuple(arg
, "s#:lock", &p
, &n
))
423 key_lock(&kd
, KEYDATA_KD(me
), p
, n
);
424 rc
= keydata_pywrap(kd
);
429 static PyObject
*meth__KeyData_read(PyObject
*me
, PyObject
*arg
)
436 if (!PyArg_ParseTuple(arg
, "Os:read", &me
, &p
))
438 if ((kd
= key_read(p
, &end
)) == 0)
439 KEYERR(KERR_MALFORMED
);
440 rc
= Py_BuildValue("(Ns)", keydata_pywrap(kd
), end
);
445 static PyObject
*meth__KeyData_decode(PyObject
*me
, PyObject
*arg
)
452 if (!PyArg_ParseTuple(arg
, "Os#:decode", &me
, &p
, &n
))
454 if ((kd
= key_decode(p
, n
)) == 0)
455 KEYERR(KERR_MALFORMED
);
456 rc
= keydata_pywrap(kd
);
461 static PyObject
*kdget_flags(PyObject
*me
, void *hunoz
)
462 { return (getulong(KEYDATA_KD(me
)->e
)); }
464 static PyMethodDef keydata_pymethods
[] = {
465 #define METHNAME(func) kdmeth_##func
466 METH (matchp
, "KD.matchp(FILTER) -> BOOL")
467 METH (split
, "KD.split()")
468 KWMETH(write
, "KD.write(filter = <any>) -> STRING")
469 KWMETH(encode
, "KD.encode(filter = <any>) -> BYTES")
470 KWMETH(copy
, "KD.copy(filter = <any>) -> KD")
471 METH (plock
, "KD.plock(TAG) -> ENCRYPTED-KD")
472 METH (lock
, "KD.lock(KEY) -> ENCRYPTED-KD")
477 static PyGetSetDef keydata_pygetset
[] = {
478 #define GETSETNAME(op, name) kd##op##_##name
479 GET (flags
, "KD.flags -> FLAGS")
484 static PyTypeObject keydata_pytype_skel
= {
485 PyObject_HEAD_INIT(0) 0, /* Header */
486 "KeyData", /* @tp_name@ */
487 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
488 0, /* @tp_itemsize@ */
490 keydata_pydealloc
, /* @tp_dealloc@ */
492 0, /* @tp_getattr@ */
493 0, /* @tp_setattr@ */
494 0, /* @tp_compare@ */
496 0, /* @tp_as_number@ */
497 0, /* @tp_as_sequence@ */
498 0, /* @tp_as_mapping@ */
502 0, /* @tp_getattro@ */
503 0, /* @tp_setattro@ */
504 0, /* @tp_as_buffer@ */
505 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
509 "Key data base class.",
511 0, /* @tp_traverse@ */
513 0, /* @tp_richcompare@ */
514 0, /* @tp_weaklistoffset@ */
516 0, /* @tp_iternext@ */
517 keydata_pymethods
, /* @tp_methods@ */
518 0, /* @tp_members@ */
519 keydata_pygetset
, /* @tp_getset@ */
522 0, /* @tp_descr_get@ */
523 0, /* @tp_descr_set@ */
524 0, /* @tp_dictoffset@ */
526 PyType_GenericAlloc
, /* @tp_alloc@ */
527 abstract_pynew
, /* @tp_new@ */
532 static PyObject
*keydatabin_pynew(PyTypeObject
*ty
,
533 PyObject
*arg
, PyObject
*kw
)
538 keydata_pyobj
*me
= 0;
539 static char *kwlist
[] = { "key", "flags", 0 };
541 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|O&:new", kwlist
,
542 &p
, &n
, convflags
, &f
))
544 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
545 me
->kd
= key_newbinary(f
& ~KF_ENCMASK
, p
, n
);
547 return ((PyObject
*)me
);
550 static PyObject
*kdbget_bin(PyObject
*me
, void *hunoz
)
551 { return (bytestring_pywrap(KEYDATA_KD(me
)->u
.k
.k
,
552 KEYDATA_KD(me
)->u
.k
.sz
)); }
554 static PyGetSetDef keydatabin_pygetset
[] = {
555 #define GETSETNAME(op, name) kdb##op##_##name
556 GET (bin
, "KD.bin -> BYTES")
561 static PyTypeObject keydatabin_pytype_skel
= {
562 PyObject_HEAD_INIT(0) 0, /* Header */
563 "KeyDataBinary", /* @tp_name@ */
564 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
565 0, /* @tp_itemsize@ */
567 0, /* @tp_dealloc@ */
569 0, /* @tp_getattr@ */
570 0, /* @tp_setattr@ */
571 0, /* @tp_compare@ */
573 0, /* @tp_as_number@ */
574 0, /* @tp_as_sequence@ */
575 0, /* @tp_as_mapping@ */
579 0, /* @tp_getattro@ */
580 0, /* @tp_setattro@ */
581 0, /* @tp_as_buffer@ */
582 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
586 "Key data for binary keys.",
588 0, /* @tp_traverse@ */
590 0, /* @tp_richcompare@ */
591 0, /* @tp_weaklistoffset@ */
593 0, /* @tp_iternext@ */
594 0, /* @tp_methods@ */
595 0, /* @tp_members@ */
596 keydatabin_pygetset
, /* @tp_getset@ */
599 0, /* @tp_descr_get@ */
600 0, /* @tp_descr_set@ */
601 0, /* @tp_dictoffset@ */
603 PyType_GenericAlloc
, /* @tp_alloc@ */
604 keydatabin_pynew
, /* @tp_new@ */
609 static PyObject
*keydataenc_pynew(PyTypeObject
*ty
,
610 PyObject
*arg
, PyObject
*kw
)
615 keydata_pyobj
*me
= 0;
616 static char *kwlist
[] = { "key", "flags", 0 };
618 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|O&:new", kwlist
,
619 &p
, &n
, convflags
, &f
))
621 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
622 me
->kd
= key_newencrypted(f
& ~KF_ENCMASK
, p
, n
);
624 return ((PyObject
*)me
);
627 static PyObject
*kdemeth_plock(PyObject
*me
, PyObject
*arg
)
630 if (!PyArg_ParseTuple(arg
, "s:plock", &hunoz
)) goto end
;
631 KEYERR(KERR_WRONGTYPE
);
636 static PyObject
*kdemeth_lock(PyObject
*me
, PyObject
*arg
)
640 if (!PyArg_ParseTuple(arg
, "s#:lock", &hunoz
, &hukairz
)) goto end
;
641 KEYERR(KERR_WRONGTYPE
);
646 static PyObject
*kdemeth_punlock(PyObject
*me
, PyObject
*arg
)
653 if (!PyArg_ParseTuple(arg
, "s:punlock", &tag
))
655 if ((err
= key_punlock(&kd
, KEYDATA_KD(me
), tag
)) != 0)
657 rc
= keydata_pywrap(kd
);
662 static PyObject
*kdemeth_unlock(PyObject
*me
, PyObject
*arg
)
670 if (!PyArg_ParseTuple(arg
, "s#:unlock", &p
, &n
))
672 if ((err
= key_unlock(&kd
, KEYDATA_KD(me
), p
, n
)) != 0)
674 rc
= keydata_pywrap(kd
);
679 #define kdeget_ct kdbget_bin
681 static PyMethodDef keydataenc_pymethods
[] = {
682 #define METHNAME(func) kdemeth_##func
683 METH (plock
, "KD.plock(TAG) -> ENCRYPTED-KD")
684 METH (lock
, "KD.lock(KEY) -> ENCRYPTED-KD")
685 METH (punlock
, "KD.punlock(TAG) -> KD")
686 METH (unlock
, "KD.unlock(KEY) -> KD")
691 static PyGetSetDef keydataenc_pygetset
[] = {
692 #define GETSETNAME(op, name) kde##op##_##name
693 GET (ct
, "KD.ct -> BYTES")
698 static PyTypeObject keydataenc_pytype_skel
= {
699 PyObject_HEAD_INIT(0) 0, /* Header */
700 "KeyDataEncrypted", /* @tp_name@ */
701 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
702 0, /* @tp_itemsize@ */
704 0, /* @tp_dealloc@ */
706 0, /* @tp_getattr@ */
707 0, /* @tp_setattr@ */
708 0, /* @tp_compare@ */
710 0, /* @tp_as_number@ */
711 0, /* @tp_as_sequence@ */
712 0, /* @tp_as_mapping@ */
716 0, /* @tp_getattro@ */
717 0, /* @tp_setattro@ */
718 0, /* @tp_as_buffer@ */
719 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
723 "Key data for encrypted keys.",
725 0, /* @tp_traverse@ */
727 0, /* @tp_richcompare@ */
728 0, /* @tp_weaklistoffset@ */
730 0, /* @tp_iternext@ */
731 keydataenc_pymethods
, /* @tp_methods@ */
732 0, /* @tp_members@ */
733 keydataenc_pygetset
, /* @tp_getset@ */
736 0, /* @tp_descr_get@ */
737 0, /* @tp_descr_set@ */
738 0, /* @tp_dictoffset@ */
740 PyType_GenericAlloc
, /* @tp_alloc@ */
741 keydataenc_pynew
, /* @tp_new@ */
746 static PyObject
*keydatamp_pynew(PyTypeObject
*ty
,
747 PyObject
*arg
, PyObject
*kw
)
751 keydata_pyobj
*me
= 0;
752 static char *kwlist
[] = { "key", "flags", 0 };
754 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", kwlist
,
755 convmp
, &x
, convflags
, &f
))
757 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
758 me
->kd
= key_newmp(f
& ~KF_ENCMASK
, x
);
761 return ((PyObject
*)me
);
764 static PyObject
*kdmget_mp(PyObject
*me
, void *hunoz
)
765 { return (mp_pywrap(MP_COPY(KEYDATA_KD(me
)->u
.m
))); }
767 static PyGetSetDef keydatamp_pygetset
[] = {
768 #define GETSETNAME(op, name) kdm##op##_##name
769 GET (mp
, "KD.mp -> X")
774 static PyTypeObject keydatamp_pytype_skel
= {
775 PyObject_HEAD_INIT(0) 0, /* Header */
776 "KeyDataMP", /* @tp_name@ */
777 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
778 0, /* @tp_itemsize@ */
780 0, /* @tp_dealloc@ */
782 0, /* @tp_getattr@ */
783 0, /* @tp_setattr@ */
784 0, /* @tp_compare@ */
786 0, /* @tp_as_number@ */
787 0, /* @tp_as_sequence@ */
788 0, /* @tp_as_mapping@ */
792 0, /* @tp_getattro@ */
793 0, /* @tp_setattro@ */
794 0, /* @tp_as_buffer@ */
795 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
799 "Key data for large-integer keys.",
801 0, /* @tp_traverse@ */
803 0, /* @tp_richcompare@ */
804 0, /* @tp_weaklistoffset@ */
806 0, /* @tp_iternext@ */
807 0, /* @tp_methods@ */
808 0, /* @tp_members@ */
809 keydatamp_pygetset
, /* @tp_getset@ */
812 0, /* @tp_descr_get@ */
813 0, /* @tp_descr_set@ */
814 0, /* @tp_dictoffset@ */
816 PyType_GenericAlloc
, /* @tp_alloc@ */
817 keydatamp_pynew
, /* @tp_new@ */
822 static PyObject
*keydatastr_pynew(PyTypeObject
*ty
,
823 PyObject
*arg
, PyObject
*kw
)
827 keydata_pyobj
*me
= 0;
828 static char *kwlist
[] = { "key", "flags", 0 };
830 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O&:new", kwlist
,
833 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
834 me
->kd
= key_newstring(f
& ~KF_ENCMASK
, p
);
836 return ((PyObject
*)me
);
839 static PyObject
*kdsget_str(PyObject
*me
, void *hunoz
)
840 { return (PyString_FromString(KEYDATA_KD(me
)->u
.p
)); }
842 static PyGetSetDef keydatastr_pygetset
[] = {
843 #define GETSETNAME(op, name) kds##op##_##name
844 GET (str
, "KD.str -> STRING")
849 static PyTypeObject keydatastr_pytype_skel
= {
850 PyObject_HEAD_INIT(0) 0, /* Header */
851 "KeyDataString", /* @tp_name@ */
852 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
853 0, /* @tp_itemsize@ */
855 0, /* @tp_dealloc@ */
857 0, /* @tp_getattr@ */
858 0, /* @tp_setattr@ */
859 0, /* @tp_compare@ */
861 0, /* @tp_as_number@ */
862 0, /* @tp_as_sequence@ */
863 0, /* @tp_as_mapping@ */
867 0, /* @tp_getattro@ */
868 0, /* @tp_setattro@ */
869 0, /* @tp_as_buffer@ */
870 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
874 "Key data for string keys.",
876 0, /* @tp_traverse@ */
878 0, /* @tp_richcompare@ */
879 0, /* @tp_weaklistoffset@ */
881 0, /* @tp_iternext@ */
882 0, /* @tp_methods@ */
883 0, /* @tp_members@ */
884 keydatastr_pygetset
, /* @tp_getset@ */
887 0, /* @tp_descr_get@ */
888 0, /* @tp_descr_set@ */
889 0, /* @tp_dictoffset@ */
891 PyType_GenericAlloc
, /* @tp_alloc@ */
892 keydatastr_pynew
, /* @tp_new@ */
897 static PyObject
*keydataec_pynew(PyTypeObject
*ty
,
898 PyObject
*arg
, PyObject
*kw
)
902 keydata_pyobj
*me
= 0;
903 static char *kwlist
[] = { "key", "flags", 0 };
905 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", kwlist
,
906 convecpt
, &x
, convflags
, &f
))
908 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
909 me
->kd
= key_newec(f
& ~KF_ENCMASK
, &x
);
912 return ((PyObject
*)me
);
915 static PyObject
*kdeget_ecpt(PyObject
*me
, void *hunoz
)
918 EC_COPY(&pt
, &KEYDATA_KD(me
)->u
.e
);
919 return (ecpt_pywrapout(ecpt_pytype
, &pt
));
922 static PyGetSetDef keydataec_pygetset
[] = {
923 #define GETSETNAME(op, name) kde##op##_##name
924 GET (ecpt
, "KD.ecpt -> ECPT")
929 static PyTypeObject keydataec_pytype_skel
= {
930 PyObject_HEAD_INIT(0) 0, /* Header */
931 "KeyDataECPt", /* @tp_name@ */
932 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
933 0, /* @tp_itemsize@ */
935 0, /* @tp_dealloc@ */
937 0, /* @tp_getattr@ */
938 0, /* @tp_setattr@ */
939 0, /* @tp_compare@ */
941 0, /* @tp_as_number@ */
942 0, /* @tp_as_sequence@ */
943 0, /* @tp_as_mapping@ */
947 0, /* @tp_getattro@ */
948 0, /* @tp_setattro@ */
949 0, /* @tp_as_buffer@ */
950 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
954 "Key data for elliptic-curve keys.",
956 0, /* @tp_traverse@ */
958 0, /* @tp_richcompare@ */
959 0, /* @tp_weaklistoffset@ */
961 0, /* @tp_iternext@ */
962 0, /* @tp_methods@ */
963 0, /* @tp_members@ */
964 keydataec_pygetset
, /* @tp_getset@ */
967 0, /* @tp_descr_get@ */
968 0, /* @tp_descr_set@ */
969 0, /* @tp_dictoffset@ */
971 PyType_GenericAlloc
, /* @tp_alloc@ */
972 keydataec_pynew
, /* @tp_new@ */
977 static PyObject
*subkeyiter_make(PyObject
*kdobj
)
979 subkeyiter_pyobj
*me
= PyObject_NEW(subkeyiter_pyobj
, subkeyiter_pytype
);
982 key_mksubkeyiter(&me
->i
, KEYDATA_KD(kdobj
));
983 return ((PyObject
*)me
);
986 static PyObject
*subkeyiter_pynext(PyObject
*me
)
989 if (!key_nextsubkey(SUBKEYITER_I(me
), &tag
, 0))
991 return (PyString_FromString(tag
));
994 static void subkeyiter_pydealloc(PyObject
*me
)
996 Py_DECREF(SUBKEYITER_KDOBJ(me
));
1000 static PyTypeObject subkeyiter_pytype_skel
= {
1001 PyObject_HEAD_INIT(0) 0, /* Header */
1002 "SubKeyIter", /* @tp_name@ */
1003 sizeof(subkeyiter_pyobj
), /* @tp_basicsize@ */
1004 0, /* @tp_itemsize@ */
1006 subkeyiter_pydealloc
, /* @tp_dealloc@ */
1008 0, /* @tp_getattr@ */
1009 0, /* @tp_setattr@ */
1010 0, /* @tp_compare@ */
1012 0, /* @tp_as_number@ */
1013 0, /* @tp_as_sequence@ */
1014 0, /* @tp_as_mapping@ */
1018 0, /* @tp_getattro@ */
1019 0, /* @tp_setattro@ */
1020 0, /* @tp_as_buffer@ */
1021 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1022 Py_TPFLAGS_BASETYPE
,
1025 "Iterator for structured keys.",
1027 0, /* @tp_traverse@ */
1029 0, /* @tp_richcompare@ */
1030 0, /* @tp_weaklistoffset@ */
1031 PyObject_SelfIter
, /* @tp_iter@ */
1032 subkeyiter_pynext
, /* @tp_iternext@ */
1033 0, /* @tp_methods@ */
1034 0, /* @tp_members@ */
1035 0, /* @tp_getset@ */
1038 0, /* @tp_descr_get@ */
1039 0, /* @tp_descr_set@ */
1040 0, /* @tp_dictoffset@ */
1042 PyType_GenericAlloc
, /* @tp_alloc@ */
1043 abstract_pynew
, /* @tp_new@ */
1048 static PyObject
*keydatastruct_pynew(PyTypeObject
*ty
,
1049 PyObject
*arg
, PyObject
*kw
)
1052 PyObject
*it
= 0, *name
= 0, *val
= 0;
1054 keydata_pyobj
*me
= 0;
1056 static char *kwlist
[] = { "subkeys", 0 };
1058 Py_XINCREF(arg
); Py_XINCREF(kw
);
1059 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:new", kwlist
, &sub
))
1061 kd
= key_newstruct();
1063 if (!PyMapping_Check(sub
))
1064 TYERR("subkeys must be an iterable mapping");
1065 if ((it
= PyObject_GetIter(sub
)) == 0)
1067 while ((name
= PyIter_Next(it
)) != 0) {
1068 if ((p
= PyString_AsString(name
)) == 0 ||
1069 (val
= PyObject_GetItem(sub
, name
)) == 0)
1071 if (!KEYDATA_PYCHECK(val
))
1072 TYERR("subkey objects must be subclasses of KeyData");
1073 key_structset(kd
, p
, KEYDATA_KD(val
));
1074 Py_DECREF(name
); name
= 0;
1075 Py_DECREF(val
); val
= 0;
1077 if (PyErr_Occurred())
1079 Py_DECREF(it
); it
= 0;
1081 me
= (keydata_pyobj
*)ty
->tp_alloc(ty
, 0);
1084 if (kd
&& !me
) key_drop(kd
);
1085 Py_XDECREF(name
); Py_XDECREF(val
); Py_XDECREF(it
);
1086 Py_XDECREF(arg
); Py_XDECREF(kw
);
1087 return ((PyObject
*)me
);
1090 static PyObject
*keydatastruct_pylookup(PyObject
*me
, PyObject
*key
)
1096 if ((tag
= PyString_AsString(key
)) == 0)
1098 if ((kd
= key_structfind(KEYDATA_KD(me
), tag
)) == 0)
1101 rc
= keydata_pywrap(kd
);
1106 static int keydatastruct_pystore(PyObject
*me
,
1107 PyObject
*key
, PyObject
*value
)
1112 if ((tag
= PyString_AsString(key
)) == 0)
1114 key_split(&KEYDATA_KD(me
));
1116 if (!KEYDATA_PYCHECK(value
))
1117 TYERR("expected KeyData value");
1118 key_structset(KEYDATA_KD(me
), tag
, KEYDATA_KD(value
));
1120 if (!key_structfind(KEYDATA_KD(me
), tag
))
1122 key_structset(KEYDATA_KD(me
), tag
, 0);
1129 static PyMappingMethods keydatastruct_pymapping
= {
1130 gmap_pysize
, /* @mp_length@ */
1131 keydatastruct_pylookup
, /* @mp_subscript@ */
1132 keydatastruct_pystore
/* @mp_ass_subscript@ */
1135 static PyTypeObject keydatastruct_pytype_skel
= {
1136 PyObject_HEAD_INIT(0) 0, /* Header */
1137 "KeyDataStructured", /* @tp_name@ */
1138 sizeof(keydata_pyobj
), /* @tp_basicsize@ */
1139 0, /* @tp_itemsize@ */
1141 0, /* @tp_dealloc@ */
1143 0, /* @tp_getattr@ */
1144 0, /* @tp_setattr@ */
1145 0, /* @tp_compare@ */
1147 0, /* @tp_as_number@ */
1148 0, /* @tp_as_sequence@ */
1149 &keydatastruct_pymapping
, /* @tp_as_mapping@ */
1153 0, /* @tp_getattro@ */
1154 0, /* @tp_setattro@ */
1155 0, /* @tp_as_buffer@ */
1156 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1157 Py_TPFLAGS_BASETYPE
,
1160 "Key data for structured keys.",
1162 0, /* @tp_traverse@ */
1164 0, /* @tp_richcompare@ */
1165 0, /* @tp_weaklistoffset@ */
1166 subkeyiter_make
, /* @tp_iter@ */
1167 0, /* @tp_iternext@ */
1168 gmap_pymethods
, /* @tp_methods@ */
1169 0, /* @tp_members@ */
1170 0, /* @tp_getset@ */
1173 0, /* @tp_descr_get@ */
1174 0, /* @tp_descr_set@ */
1175 0, /* @tp_dictoffset@ */
1177 PyType_GenericAlloc
, /* @tp_alloc@ */
1178 keydatastruct_pynew
, /* @tp_new@ */
1183 /*----- Key attributes ----------------------------------------------------*/
1185 static PyObject
*keyattriter_make(PyObject
*kaobj
)
1187 PyObject
*kobj
= KEYATTRS_KOBJ(kaobj
);
1188 keyattriter_pyobj
*me
= PyObject_NEW(keyattriter_pyobj
,
1189 keyattriter_pytype
);
1192 key_mkattriter(&me
->i
, KEY_K(kobj
));
1193 return ((PyObject
*)me
);
1196 static PyObject
*keyattriter_pynext(PyObject
*me
)
1200 if (!key_nextattr(KEYATTRITER_I(me
), &name
, 0))
1202 return (PyString_FromString(name
));
1205 static void keyattriter_pydealloc(PyObject
*me
)
1207 Py_DECREF(KEYATTRITER_KOBJ(me
));
1211 static PyTypeObject keyattriter_pytype_skel
= {
1212 PyObject_HEAD_INIT(0) 0, /* Header */
1213 "KeyAttributeIter", /* @tp_name@ */
1214 sizeof(keyattriter_pyobj
), /* @tp_basicsize@ */
1215 0, /* @tp_itemsize@ */
1217 keyattriter_pydealloc
, /* @tp_dealloc@ */
1219 0, /* @tp_getattr@ */
1220 0, /* @tp_setattr@ */
1221 0, /* @tp_compare@ */
1223 0, /* @tp_as_number@ */
1224 0, /* @tp_as_sequence@ */
1225 0, /* @tp_as_mapping@ */
1229 0, /* @tp_getattro@ */
1230 0, /* @tp_setattro@ */
1231 0, /* @tp_as_buffer@ */
1232 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1233 Py_TPFLAGS_BASETYPE
,
1236 "Iterator for key attributes.",
1238 0, /* @tp_traverse@ */
1240 0, /* @tp_richcompare@ */
1241 0, /* @tp_weaklistoffset@ */
1242 PyObject_SelfIter
, /* @tp_iter@ */
1243 keyattriter_pynext
, /* @tp_iternext@ */
1244 0, /* @tp_methods@ */
1245 0, /* @tp_members@ */
1246 0, /* @tp_getset@ */
1249 0, /* @tp_descr_get@ */
1250 0, /* @tp_descr_set@ */
1251 0, /* @tp_dictoffset@ */
1253 PyType_GenericAlloc
, /* @tp_alloc@ */
1254 abstract_pynew
, /* @tp_new@ */
1259 static PyObject
*keyattrs_pylookup(PyObject
*me
, PyObject
*key
)
1265 if ((attr
= PyString_AsString(key
)) == 0)
1267 if ((value
= key_getattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
)) == 0)
1269 rc
= PyString_FromString(value
);
1274 static int keyattrs_pystore(PyObject
*me
,
1275 PyObject
*key
, PyObject
*value
)
1282 if ((attr
= PyString_AsString(key
)) == 0)
1285 if ((val
= PyString_AsString(value
)) == 0)
1287 if ((err
= key_putattr(KEYATTRS_KF(me
), KEYATTRS_K(me
),
1291 if (!key_getattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
))
1293 if ((err
= key_putattr(KEYATTRS_KF(me
), KEYATTRS_K(me
), attr
, 0)) != 0)
1301 static PyObject
*keyattrs_make(PyObject
*kobj
)
1303 keyattrs_pyobj
*me
= PyObject_NEW(keyattrs_pyobj
, keyattrs_pytype
);
1306 return ((PyObject
*)me
);
1309 static void keyattrs_pydealloc(PyObject
*me
)
1311 Py_DECREF(KEYATTRS_KOBJ(me
));
1315 static PyMappingMethods keyattrs_pymapping
= {
1316 gmap_pysize
, /* @mp_length@ */
1317 keyattrs_pylookup
, /* @mp_subscript@ */
1318 keyattrs_pystore
/* @mp_ass_subscript@ */
1321 static PyTypeObject keyattrs_pytype_skel
= {
1322 PyObject_HEAD_INIT(0) 0, /* Header */
1323 "KeyAttributes", /* @tp_name@ */
1324 sizeof(keyattrs_pyobj
), /* @tp_basicsize@ */
1325 0, /* @tp_itemsize@ */
1327 keyattrs_pydealloc
, /* @tp_dealloc@ */
1329 0, /* @tp_getattr@ */
1330 0, /* @tp_setattr@ */
1331 0, /* @tp_compare@ */
1333 0, /* @tp_as_number@ */
1334 &gmap_pysequence
, /* @tp_as_sequence@ */
1335 &keyattrs_pymapping
, /* @tp_as_mapping@ */
1339 0, /* @tp_getattro@ */
1340 0, /* @tp_setattro@ */
1341 0, /* @tp_as_buffer@ */
1342 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1343 Py_TPFLAGS_BASETYPE
,
1346 "Proxy thing for talking about key attributes.",
1348 0, /* @tp_traverse@ */
1350 0, /* @tp_richcompare@ */
1351 0, /* @tp_weaklistoffset@ */
1352 keyattriter_make
, /* @tp_iter@ */
1353 0, /* @tp_iternext@ */
1354 gmap_pymethods
, /* @tp_methods@ */
1355 0, /* @tp_members@ */
1356 0, /* @tp_getset@ */
1359 0, /* @tp_descr_get@ */
1360 0, /* @tp_descr_set@ */
1361 0, /* @tp_dictoffset@ */
1363 PyType_GenericAlloc
, /* @tp_alloc@ */
1364 abstract_pynew
, /* @tp_new@ */
1369 /*----- Key objects -------------------------------------------------------*/
1371 static PyObject
*key_dowrap(PyTypeObject
*ty
, PyObject
*kfobj
, key
*k
)
1373 key_pyobj
*kobj
= (key_pyobj
*)ty
->tp_alloc(ty
, 0);
1374 kobj
->kfobj
= kfobj
;
1377 return ((PyObject
*)kobj
);
1380 static PyObject
*key_pywrap(PyObject
*kfobj
, key
*k
)
1381 { return (key_dowrap(key_pytype
, kfobj
, k
)); }
1383 static PyObject
*key_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1388 unsigned long exptime
= KEXP_FOREVER
;
1389 static char *kwlist
[] = { "keyfile", "id", "type", "exptime", 0 };
1393 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O&s|O&:new", kwlist
,
1394 keyfile_pytype
, &kfobj
, convu32
, &id
,
1395 &type
, convulong
, &exptime
))
1397 if ((err
= key_new(KEYFILE_KF(kfobj
), id
, type
, exptime
, &k
)) != 0)
1399 return (key_dowrap(ty
, kfobj
, k
));
1404 static void key_pydealloc(PyObject
*me
)
1406 Py_DECREF(KEY_KFOBJ(me
));
1410 static PyObject
*kmeth_delete(PyObject
*me
, PyObject
*arg
)
1414 if (!PyArg_ParseTuple(arg
, ":delete")) goto end
;
1415 if ((err
= key_delete(KEY_KF(me
), KEY_K(me
))) != 0) KEYERR(err
);
1421 static PyObject
*kmeth_expire(PyObject
*me
, PyObject
*arg
)
1425 if (!PyArg_ParseTuple(arg
, ":expire")) goto end
;
1426 if ((err
= key_expire(KEY_KF(me
), KEY_K(me
))) != 0) KEYERR(err
);
1432 static PyObject
*kmeth_used(PyObject
*me
, PyObject
*arg
)
1437 if (!PyArg_ParseTuple(arg
, "l:used", &t
)) goto end
;
1438 if ((err
= key_used(KEY_KF(me
), KEY_K(me
), t
)) != 0) KEYERR(err
);
1444 static PyObject
*kmeth_extract(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1446 key_filter f
= { 0, 0 };
1451 static char *kwlist
[] = { "file", "filter", 0 };
1453 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!|O&:extract", kwlist
,
1454 &PyFile_Type
, &file
,
1456 (fp
= PyFile_AsFile(file
)) == 0 ||
1457 (nameobj
= PyFile_Name(file
)) == 0 ||
1458 (name
= PyString_AsString(nameobj
)) == 0)
1460 if (key_extract(KEY_KF(me
), KEY_K(me
), fp
, &f
))
1467 static PyObject
*kmeth_fingerprint(PyObject
*me
,
1468 PyObject
*arg
, PyObject
*kw
)
1471 key_filter f
= { KF_NONSECRET
, KF_NONSECRET
};
1472 static char *kwlist
[] = { "hash", "filter", 0 };
1474 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:fingerprint", kwlist
,
1475 convghash
, &h
, convfilter
, &f
))
1477 return (getbool(key_fingerprint(KEY_K(me
), h
, &f
)));
1480 static PyObject
*kget_id(PyObject
*me
, void *hunoz
)
1481 { return (getulong(KEY_K(me
)->id
)); }
1482 static PyObject
*kget_file(PyObject
*me
, void *hunoz
)
1483 { RETURN_OBJ(KEY_KFOBJ(me
)); }
1484 static PyObject
*kget_type(PyObject
*me
, void *hunoz
)
1485 { return (PyString_FromString(KEY_K(me
)->type
)); }
1486 static PyObject
*kget_exptime(PyObject
*me
, void *hunoz
)
1487 { return (getulong(KEY_K(me
)->exp
)); }
1488 static PyObject
*kget_deltime(PyObject
*me
, void *hunoz
)
1489 { return (getulong(KEY_K(me
)->del
)); }
1490 static PyObject
*kget_expiredp(PyObject
*me
, void *hunoz
)
1491 { return (getbool(key_expired(KEY_K(me
)))); }
1492 static PyObject
*kget_attr(PyObject
*me
, void *hunoz
)
1493 { return (keyattrs_make(me
)); }
1495 static int kset_exptime(PyObject
*me
, PyObject
*x
, void *hunoz
)
1500 if (!convulong(x
, &et
))
1502 if (!(KEY_KF(me
)->f
& KF_WRITE
))
1503 KEYERR(KERR_READONLY
);
1505 KEY_KF(me
)->f
|= KF_MODIFIED
;
1511 static int kset_deltime(PyObject
*me
, PyObject
*x
, void *hunoz
)
1516 if (!convulong(x
, &dt
))
1518 if (dt
== KEXP_FOREVER
&& k
->exp
!= KEXP_FOREVER
)
1519 VALERR("key will eventually expire");
1520 if (!(KEY_KF(me
)->f
& KF_WRITE
))
1521 KEYERR(KERR_READONLY
);
1523 KEY_KF(me
)->f
|= KF_MODIFIED
;
1529 static PyObject
*kget_data(PyObject
*me
, void *hunoz
)
1531 key_data
*kd
= KEY_K(me
)->k
;
1533 return (keydata_pywrap(kd
));
1535 static int kset_data(PyObject
*me
, PyObject
*x
, void *hunoz
)
1539 if (!x
) NIERR("__del__");
1540 if (!KEYDATA_PYCHECK(x
)) TYERR("expected KeyData object");
1541 if ((err
= key_setkeydata(KEY_KF(me
), KEY_K(me
), KEYDATA_KD(x
))) != 0)
1548 static PyObject
*kget_fulltag(PyObject
*me
, void *hunoz
)
1553 key_fulltag(KEY_K(me
), &d
);
1554 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
1559 static PyObject
*kget_tag(PyObject
*me
, void *hunoz
)
1561 if (!KEY_K(me
)->tag
) RETURN_NONE
;
1562 return (PyString_FromString(KEY_K(me
)->tag
));
1564 static int kset_tag(PyObject
*me
, PyObject
*x
, void *hunoz
)
1569 if (!x
|| x
== Py_None
) tag
= 0;
1570 else if ((tag
= PyString_AsString(x
)) == 0) goto end
;
1571 if ((err
= key_settag(KEY_KF(me
), KEY_K(me
), tag
)) != 0) KEYERR(err
);
1577 static PyObject
*kget_comment(PyObject
*me
, void *hunoz
)
1579 if (!KEY_K(me
)->c
) RETURN_NONE
;
1580 return (PyString_FromString(KEY_K(me
)->c
));
1582 static int kset_comment(PyObject
*me
, PyObject
*x
, void *hunoz
)
1587 if (!x
|| x
== Py_None
) c
= 0;
1588 else if ((c
= PyString_AsString(x
)) == 0) goto end
;
1589 if ((err
= key_setcomment(KEY_KF(me
), KEY_K(me
), c
)) != 0) KEYERR(err
);
1595 static PyMethodDef key_pymethods
[] = {
1596 #define METHNAME(func) kmeth_##func
1597 METH (delete, "KEY.delete()")
1598 METH (expire
, "KEY.expire()")
1599 METH (used
, "KEY.used(TIME)")
1600 KWMETH(extract
, "KEY.extract(FILE, filter = '')")
1601 KWMETH(fingerprint
, "KEY.fingerprint(HASH, filtr = '-secret')")
1606 static PyGetSetDef key_pygetset
[] = {
1607 #define GETSETNAME(op, name) k##op##_##name
1608 GET (file
, "KEY.file -> KF")
1609 GET (id
, "KEY.id -> ID")
1610 GETSET(tag
, "KEY.tag -> TAG")
1611 GET (type
, "KEY.type -> TYPE")
1612 GETSET(exptime
, "KEY.exptime -> TIME")
1613 GETSET(deltime
, "KEY.deltime -> TIME")
1614 GET (expiredp
, "KEY.expiredp -> BOOL")
1615 GET (attr
, "KEY.attr -> ATTRIBUTES")
1616 GETSET(data
, "KEY.data -> KD")
1617 GETSET(comment
, "KEY.comment -> COMMENT")
1618 GET (fulltag
, "KEY.fulltag -> FULLTAG")
1623 static PyTypeObject key_pytype_skel
= {
1624 PyObject_HEAD_INIT(0) 0, /* Header */
1625 "Key", /* @tp_name@ */
1626 sizeof(key_pyobj
), /* @tp_basicsize@ */
1627 0, /* @tp_itemsize@ */
1629 key_pydealloc
, /* @tp_dealloc@ */
1631 0, /* @tp_getattr@ */
1632 0, /* @tp_setattr@ */
1633 0, /* @tp_compare@ */
1635 0, /* @tp_as_number@ */
1636 0, /* @tp_as_sequence@ */
1637 0, /* @tp_as_mapping@ */
1641 0, /* @tp_getattro@ */
1642 0, /* @tp_setattro@ */
1643 0, /* @tp_as_buffer@ */
1644 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1645 Py_TPFLAGS_BASETYPE
,
1650 0, /* @tp_traverse@ */
1652 0, /* @tp_richcompare@ */
1653 0, /* @tp_weaklistoffset@ */
1655 0, /* @tp_iternext@ */
1656 key_pymethods
, /* @tp_methods@ */
1657 0, /* @tp_members@ */
1658 key_pygetset
, /* @tp_getset@ */
1661 0, /* @tp_descr_get@ */
1662 0, /* @tp_descr_set@ */
1663 0, /* @tp_dictoffset@ */
1665 PyType_GenericAlloc
, /* @tp_alloc@ */
1666 key_pynew
, /* @tp_new@ */
1671 /*----- Key iteration -----------------------------------------------------*/
1673 static PyObject
*keyiter_new(PyObject
*kfobj
)
1675 keyiter_pyobj
*me
= PyObject_NEW(keyiter_pyobj
, keyiter_pytype
);
1676 key_mkiter(&me
->i
, KEYFILE_KF(kfobj
));
1679 return ((PyObject
*)me
);
1682 static PyObject
*keyiter_pynext(PyObject
*me
)
1686 if ((k
= key_next(KEYITER_I(me
))) == 0)
1688 return (getulong(k
->id
));
1691 static void keyiter_pydealloc(PyObject
*me
)
1693 Py_DECREF(KEYITER_KFOBJ(me
));
1697 static PyTypeObject keyiter_pytype_skel
= {
1698 PyObject_HEAD_INIT(0) 0, /* Header */
1699 "KeyFileIter", /* @tp_name@ */
1700 sizeof(keyiter_pyobj
), /* @tp_basicsize@ */
1701 0, /* @tp_itemsize@ */
1703 keyiter_pydealloc
, /* @tp_dealloc@ */
1705 0, /* @tp_getattr@ */
1706 0, /* @tp_setattr@ */
1707 0, /* @tp_compare@ */
1709 0, /* @tp_as_number@ */
1710 0, /* @tp_as_sequence@ */
1711 0, /* @tp_as_mapping@ */
1715 0, /* @tp_getattro@ */
1716 0, /* @tp_setattro@ */
1717 0, /* @tp_as_buffer@ */
1718 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1719 Py_TPFLAGS_BASETYPE
,
1722 "Keyring iterator.",
1724 0, /* @tp_traverse@ */
1726 0, /* @tp_richcompare@ */
1727 0, /* @tp_weaklistoffset@ */
1728 PyObject_SelfIter
, /* @tp_iter@ */
1729 keyiter_pynext
, /* @tp_iternext@ */
1730 0, /* @tp_methods@ */
1731 0, /* @tp_members@ */
1732 0, /* @tp_getset@ */
1735 0, /* @tp_descr_get@ */
1736 0, /* @tp_descr_set@ */
1737 0, /* @tp_dictoffset@ */
1739 PyType_GenericAlloc
, /* @tp_alloc@ */
1740 abstract_pynew
, /* @tp_new@ */
1745 /*----- Key files ---------------------------------------------------------*/
1752 static void pythonreporter(const char *file
, int line
,
1753 const char *msg
, void *p
)
1755 struct reportinfo
*ri
= p
;
1761 key_moan(file
, line
, msg
, 0);
1762 else if ((res
= PyObject_CallFunction(ri
->func
, "sis",
1763 file
, line
, msg
)) == 0)
1769 static PyObject
*keyfile_pynew(PyTypeObject
*ty
,
1770 PyObject
*arg
, PyObject
*kw
)
1772 struct reportinfo ri
= { 0, 0 };
1774 unsigned how
= KOPEN_READ
;
1775 keyfile_pyobj
*rc
= 0;
1776 static char *kwlist
[] = { "file", "how", "report", 0 };
1778 Py_XINCREF(arg
); Py_XINCREF(kw
);
1779 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|iO:new", kwlist
,
1780 &file
, &how
, &ri
.func
))
1782 if (ri
.func
&& !PyCallable_Check(ri
.func
))
1783 TYERR("reporter function not callable");
1784 if ((rc
= (keyfile_pyobj
*)ty
->tp_alloc(ty
, 0)) == 0)
1786 if (key_open(&rc
->kf
, file
, how
, pythonreporter
, &ri
))
1789 key_discard(&rc
->kf
);
1800 Py_XDECREF(arg
); Py_XDECREF(kw
);
1801 return ((PyObject
*)rc
);
1804 static void keyfile_pydealloc(PyObject
*me
)
1806 key_discard(KEYFILE_KF(me
));
1810 static PyObject
*kfmeth_save(PyObject
*me
, PyObject
*arg
)
1812 if (!PyArg_ParseTuple(arg
, ":save"))
1814 switch (key_save(KEYFILE_KF(me
))) {
1818 KEYIOERR(KEYFILE_KF(me
)->name
);
1820 KEYFILEBROKEN(KEYFILE_KF(me
)->name
);
1828 static PyObject
*kfmeth_merge(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1830 struct reportinfo ri
= { 0, 0 };
1835 static char *kwlist
[] = { "file", "report", 0 };
1837 Py_XINCREF(arg
); Py_XINCREF(kw
);
1838 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!|O:merge", kwlist
,
1839 &PyFile_Type
, &x
, &ri
.func
))
1841 if (ri
.func
&& !PyCallable_Check(ri
.func
))
1842 TYERR("reporter function not callable");
1843 if ((fp
= PyFile_AsFile(x
)) == 0)
1846 if ((name
= PyString_AsString(x
)) == 0)
1848 rc
= key_merge(KEYFILE_KF(me
), name
, fp
, pythonreporter
, &ri
);
1853 Py_XDECREF(arg
); Py_XDECREF(kw
);
1857 Py_XDECREF(arg
); Py_XDECREF(kw
);
1861 static PyObject
*kfmeth_byid(PyObject
*me
, PyObject
*arg
)
1867 if (!PyArg_ParseTuple(arg
, "O&:byid", convu32
, &id
)) goto end
;
1868 if ((k
= key_byid(KEYFILE_KF(me
), id
)) == 0) KEYERR(KERR_NOTFOUND
);
1869 rc
= key_pywrap(me
, k
);
1874 static PyObject
*kfmeth_bytype(PyObject
*me
, PyObject
*arg
)
1880 if (!PyArg_ParseTuple(arg
, "s:bytype", &type
)) goto end
;
1881 if ((k
= key_bytype(KEYFILE_KF(me
), type
)) == 0) RETURN_NONE
;
1882 rc
= key_pywrap(me
, k
);
1887 static PyObject
*bytag(PyObject
*me
, PyObject
*tagobj
)
1894 if (convu32(tagobj
, &id
))
1895 k
= key_byid(KEYFILE_KF(me
), id
);
1898 if ((tag
= PyString_AsString(tagobj
)) == 0)
1900 k
= key_bytag(KEYFILE_KF(me
), tag
);
1902 if (!k
) RETURN_NONE
;
1903 rc
= key_pywrap(me
, k
);
1908 static PyObject
*kfmeth_bytag(PyObject
*me
, PyObject
*arg
)
1912 if (!PyArg_ParseTuple(arg
, "O:bytag", &tagobj
)) return (0);
1913 return (bytag(me
, tagobj
));
1916 static PyObject
*keyfile_pylookup(PyObject
*me
, PyObject
*key
)
1918 PyObject
*rc
= bytag(me
, key
);
1920 if (rc
== Py_None
) {
1929 static PyObject
*kfmeth_newkey(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1933 long exptime
= KEXP_FOREVER
;
1934 static char *kwlist
[] = { "id", "type", "exptime", 0 };
1938 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&s|l:newkey", kwlist
,
1939 convu32
, &id
, &type
, &exptime
))
1941 if ((err
= key_new(KEYFILE_KF(me
), id
, type
, exptime
, &k
)) != 0)
1943 return (key_pywrap(me
, k
));
1948 static PyObject
*kfmeth_qtag(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1951 key_data
**kd
, *okd
;
1952 PyObject
*newkdobj
= 0;
1956 static char *kwlist
[] = { "tag", "new", 0 };
1958 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O!:qtag", kwlist
,
1959 &tag
, keydata_pytype
, &newkdobj
))
1961 if (key_qtag(KEYFILE_KF(me
), tag
, &d
, &k
, &kd
))
1962 KEYERR(KERR_NOTFOUND
);
1965 if (!(KEYFILE_KF(me
)->f
& KF_WRITE
))
1966 KEYERR(KERR_READONLY
);
1967 KEYFILE_KF(me
)->f
|= KF_MODIFIED
;
1968 *kd
= KEYDATA_KD(newkdobj
);
1971 rc
= Py_BuildValue("(s#NN)",
1974 keydata_pywrap(okd
));
1979 static PyObject
*kfget_name(PyObject
*me
, void *hunoz
)
1980 { return (PyString_FromString(KEYFILE_KF(me
)->name
)); }
1981 static PyObject
*kfget_modifiedp(PyObject
*me
, void *hunoz
)
1982 { return (getbool(KEYFILE_KF(me
)->f
& KF_MODIFIED
)); }
1983 static PyObject
*kfget_writep(PyObject
*me
, void *hunoz
)
1984 { return (getbool(KEYFILE_KF(me
)->f
& KF_WRITE
)); }
1985 static PyObject
*kfget_filep(PyObject
*me
, void *hunoz
)
1986 { return (getbool(!!KEYFILE_KF(me
)->fp
)); }
1988 static PyMethodDef keyfile_pymethods
[] = {
1989 #define METHNAME(func) kfmeth_##func
1990 METH (save
, "KF.save()")
1991 KWMETH(merge
, "KF.merge(FILE, report = <built-in-reporter>)")
1992 KWMETH(newkey
, "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY")
1993 METH (byid
, "KF.byid(KEYID) -> KEY|None")
1994 METH (bytype
, "KF.bytype(TYPE) -> KEY|None")
1995 METH (bytag
, "KF.bytag(TAG) -> KEY|None")
1996 KWMETH(qtag
, "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD")
2002 static PyGetSetDef keyfile_pygetset
[] = {
2003 #define GETSETNAME(op, name) kf##op##_##name
2004 GET (name
, "KF.name -> file name")
2005 GET (modifiedp
, "KF.modifiedp -> has keyring been modified?")
2006 GET (writep
, "KF.writep -> is keyring modifiable?")
2007 GET (filep
, "KF.filep -> does keyring have a backing file?")
2012 static PyMappingMethods keyfile_pymapping
= {
2018 static PyTypeObject keyfile_pytype_skel
= {
2019 PyObject_HEAD_INIT(0) 0, /* Header */
2020 "KeyFile", /* @tp_name@ */
2021 sizeof(keyfile_pyobj
), /* @tp_basicsize@ */
2022 0, /* @tp_itemsize@ */
2024 keyfile_pydealloc
, /* @tp_dealloc@ */
2026 0, /* @tp_getattr@ */
2027 0, /* @tp_setattr@ */
2028 0, /* @tp_compare@ */
2030 0, /* @tp_as_number@ */
2031 &gmap_pysequence
, /* @tp_as_sequence@ */
2032 &keyfile_pymapping
, /* @tp_as_mapping@ */
2036 0, /* @tp_getattro@ */
2037 0, /* @tp_setattro@ */
2038 0, /* @tp_as_buffer@ */
2039 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2040 Py_TPFLAGS_BASETYPE
,
2045 0, /* @tp_traverse@ */
2047 0, /* @tp_richcompare@ */
2048 0, /* @tp_weaklistoffset@ */
2049 keyiter_new
, /* @tp_iter@ */
2050 0, /* @tp_iternext@ */
2051 keyfile_pymethods
, /* @tp_methods@ */
2052 0, /* @tp_members@ */
2053 keyfile_pygetset
, /* @tp_getset@ */
2056 0, /* @tp_descr_get@ */
2057 0, /* @tp_descr_set@ */
2058 0, /* @tp_dictoffset@ */
2060 PyType_GenericAlloc
, /* @tp_alloc@ */
2061 keyfile_pynew
, /* @tp_new@ */
2066 /*----- Global stuff ------------------------------------------------------*/
2068 static PyObject
*meth_barf(PyObject
*me
, PyObject
*arg
)
2072 if (PyArg_ParseTuple(arg
, "i:barf", &err
))
2078 static PyMethodDef methods
[] = {
2079 #define METHNAME(func) meth_##func
2080 METH (_KeyData_readflags
,
2081 "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)")
2082 METH (_KeyData_writeflags
, "KeyData.writeflags(FLAGS) -> STRING")
2083 METH (_KeyData_read
, "KeyData.read(STRING) -> (KD, REST)")
2084 METH (_KeyData_decode
, "KeyData.decode(BYTES) -> KD")
2085 METH (barf
, "barf(ERR)")
2090 /*----- Initialization ----------------------------------------------------*/
2092 void key_pyinit(void)
2094 INITTYPE(keyfile
, root
);
2095 INITTYPE(key
, root
);
2096 INITTYPE(keyiter
, root
);
2097 INITTYPE(keydata
, root
);
2098 INITTYPE(keydatabin
, keydata
);
2099 INITTYPE(keydataenc
, keydata
);
2100 INITTYPE(keydatastr
, keydata
);
2101 INITTYPE(keydatamp
, keydata
);
2102 INITTYPE(keydataec
, keydata
);
2103 INITTYPE(keydatastruct
, keydata
);
2104 INITTYPE(subkeyiter
, root
);
2105 INITTYPE(keyattrs
, root
);
2106 INITTYPE(keyattriter
, root
);
2107 addmethods(methods
);
2110 void key_pyinsert(PyObject
*mod
)
2112 INSEXC("KeyError", keyexc
, PyExc_Exception
, keyexc_pymethods
);
2113 INSEXC("KeyFileIOError", keyioexc
, PyExc_OSError
, 0);
2114 INSEXC("KeyFileBroken", keyfilebrokenexc
, keyioexc
, 0);
2115 INSERT("KeyFile", keyfile_pytype
);
2116 INSERT("KeyFileIter", keyiter_pytype
);
2117 INSERT("Key", key_pytype
);
2118 INSERT("KeyAttributes", keyattrs_pytype
);
2119 INSERT("KeyAttributeIter", keyattriter_pytype
);
2120 INSERT("KeyData", keydata_pytype
);
2121 INSERT("KeyDataBinary", keydatabin_pytype
);
2122 INSERT("KeyDataEncrypted", keydataenc_pytype
);
2123 INSERT("KeyDataMP", keydatamp_pytype
);
2124 INSERT("KeyDataECPt", keydataec_pytype
);
2125 INSERT("KeyDataString", keydatastr_pytype
);
2126 INSERT("KeyDataStructured", keydatastruct_pytype
);
2127 INSERT("SubKeyIter", subkeyiter_pytype
);
2130 /*----- That's all, folks -------------------------------------------------*/