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)
43 n = PyTuple_GET_SIZE(arg);
44 if (n < 2) TYERR("__init__() takes at least two arguments");
45 me = PyTuple_GET_ITEM(arg, 0);
46 err = PyInt_AsLong(PyTuple_GET_ITEM(arg, 1));
47 if (err == -1 && PyErr_Occurred()) goto end;
48 if (INT_MIN > err || err > INT_MAX) OVFERR("error code out of range");
50 x = PyInt_FromLong(err); if (!x) goto end;
51 if (PyObject_SetAttrString(me, "err", x)) goto end;
54 x = TEXT_FROMSTR(key_strerror(err)); if (!x) goto end;
55 if (PyObject_SetAttrString(me, "errstring", x)) goto end;
58 x = PyTuple_GetSlice(arg, 1, n); if (!x) goto end;
59 if (PyObject_SetAttrString(me, "args", x)) goto end;
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 = TEXT_STR(x)) == 0)
92 rc = TEXT_FORMAT("%s (%ld): %s", errtag, -err, errstr);
99 static const 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 keyfile_pyobj {
145 static PyTypeObject *keyfile_pytype;
146 #define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype)
147 #define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf)
149 typedef struct key_pyobj {
155 static PyTypeObject *key_pytype;
156 #define KEY_PYCHECK(o) PyObject_TypeCheck(o, key_pytype)
157 #define KEY_K(o) (((key_pyobj *)(o))->k)
158 #define KEY_KFOBJ(o) (((key_pyobj *)(o))->kfobj)
159 #define KEY_KF(o) KEYFILE_KF(KEY_KFOBJ(o))
161 typedef struct keyattrs_pyobj {
166 static PyTypeObject *keyattrs_pytype;
167 #define KEYATTRS_PYCHECK(o) PyObject_TypeCheck(o, keyattrs_pytype)
168 #define KEYATTRS_KOBJ(o) (((keyattrs_pyobj *)(o))->kobj)
169 #define KEYATTRS_KF(o) KEY_KF(KEYATTRS_KOBJ(o))
170 #define KEYATTRS_K(o) KEY_K(KEYATTRS_KOBJ(o))
172 /*----- Filters -----------------------------------------------------------*/
174 static int convfilter(PyObject *x, void *p)
180 PyObject *a = 0, *b = 0;
184 if ((fs = TEXT_STR(x)) != 0) {
185 if ((err = key_readflags(fs, &end, &f->f, &f->m)) != 0)
188 KEYERR(KERR_BADFLAGS);
191 if (!PySequence_Check(x))
193 else if ((n = PySequence_Size(x)) < 0)
197 else if ((a = PySequence_GetItem(x, 0)) == 0 || !convuint(a, &f->f) ||
198 (b = PySequence_GetItem(x, 1)) == 0 || !convuint(b, &f->m))
204 TYERR("expected flag string or flag/mask pair");
211 static int convflags(PyObject *x, void *p)
223 if ((fs = TEXT_STR(x)) != 0) {
224 if ((err = key_readflags(fs, &end, f, 0)) != 0)
227 KEYERR(KERR_BADFLAGS);
236 TYERR("expected flag string or integer bitfield");
241 static PyObject *kdmeth_readflags(PyObject *me, PyObject *arg)
249 if (!PyArg_ParseTuple(arg, "s:readflags", &p)) goto end;
250 if ((err = key_readflags(p, &end, &f, &m)) != 0) KEYERR(err);
251 rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end);
256 static PyObject *kdmeth_writeflags(PyObject *me, PyObject *arg)
262 if (!PyArg_ParseTuple(arg, "O&:key_writeflags", convuint, &f)) return (0);
263 key_writeflags(f, &d);
264 rc = TEXT_FROMSTRLEN(d.buf, d.len);
269 /*----- Key data ----------------------------------------------------------*/
271 static const gmap_ops keydatastruct_gmops;
273 static PyObject *keydata_pywrap(key_data *kd)
276 keydata_pyobj *kdobj;
277 const gmap_ops *gmops = 0;
279 switch (kd->e & KF_ENCMASK) {
280 case KENC_BINARY: ty = keydatabin_pytype; break;
281 case KENC_ENCRYPT: ty = keydataenc_pytype; break;
282 case KENC_MP: ty = keydatamp_pytype; break;
284 ty = keydatastruct_pytype;
285 gmops = &keydatastruct_gmops;
287 case KENC_STRING: ty = keydatastr_pytype; break;
288 case KENC_EC: ty = keydataec_pytype; break;
291 kdobj = PyObject_NEW(keydata_pyobj, ty);
292 kdobj->gmops = gmops;
294 return ((PyObject *)kdobj);
297 static void keydata_pydealloc(PyObject *me)
299 key_drop(KEYDATA_KD(me));
303 static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg)
307 if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f))
309 return (getbool(KEY_MATCH(KEYDATA_KD(me), &f)));
312 static PyObject *kdmeth_split(PyObject *me)
313 { key_split(&KEYDATA_KD(me)); RETURN_ME; }
315 static PyObject *kdmeth_copy(PyObject *me, PyObject *arg, PyObject *kw)
317 key_filter f = { 0, 0 };
318 static const char *const kwlist[] = { "filter", 0 };
321 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", KWLIST,
324 if ((kd = key_copydata(KEYDATA_KD(me), &f)) == 0)
327 return (keydata_pywrap(kd));
330 static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw)
332 key_filter f = { 0, 0 };
335 static const char *const kwlist[] = { "filter", 0 };
337 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", KWLIST,
340 key_write(KEYDATA_KD(me), &d, &f);
341 rc = TEXT_FROMSTRLEN(d.buf, d.len);
346 static PyObject *kdmeth_encode(PyObject *me, PyObject *arg, PyObject *kw)
348 key_filter f = { 0, 0 };
351 static const char *const kwlist[] = { "filter", 0 };
353 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", KWLIST,
356 key_encode(KEYDATA_KD(me), &d, &f);
357 rc = bytestring_pywrap(d.buf, d.len);
362 static PyObject *kdmeth_plock(PyObject *me, PyObject *arg)
369 if (!PyArg_ParseTuple(arg, "s:plock", &tag))
371 if ((err = key_plock(&kd, KEYDATA_KD(me), tag)) != 0)
373 rc = keydata_pywrap(kd);
378 static PyObject *kdmeth_lock(PyObject *me, PyObject *arg)
384 if (!PyArg_ParseTuple(arg, "O&:lock", convbin, &pp))
386 key_lock(&kd, KEYDATA_KD(me), pp.p, pp.sz);
387 rc = keydata_pywrap(kd);
392 static PyObject *kdmeth_read(PyObject *me, PyObject *arg)
399 if (!PyArg_ParseTuple(arg, "s:read", &p)) goto end;
400 if ((kd = key_read(p, &end)) == 0) KEYERR(KERR_MALFORMED);
401 rc = Py_BuildValue("(Ns)", keydata_pywrap(kd), end);
406 static PyObject *kdmeth_decode(PyObject *me, PyObject *arg)
412 if (!PyArg_ParseTuple(arg, "O&:decode", convbin, &in)) goto end;
413 if ((kd = key_decode(in.p, in.sz)) == 0) KEYERR(KERR_MALFORMED);
414 rc = keydata_pywrap(kd);
419 static PyObject *kdget_flags(PyObject *me, void *hunoz)
420 { return (getulong(KEYDATA_KD(me)->e)); }
422 static const PyMethodDef keydata_pymethods[] = {
423 #define METHNAME(func) kdmeth_##func
424 METH (matchp, "KD.matchp(FILTER) -> BOOL")
425 NAMETH(split, "KD.split()")
426 KWMETH(write, "KD.write([filter = <any>]) -> STRING")
427 KWMETH(encode, "KD.encode([filter = <any>]) -> BYTES")
428 KWMETH(copy, "KD.copy([filter = <any>]) -> KD")
429 METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD")
430 METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD")
431 SMTH (readflags, "readflags(STRING) -> (FLAGS, MASK, REST)")
432 SMTH (writeflags, "writeflags(FLAGS) -> STRING")
433 SMTH (read, "read(STRING) -> (KD, REST)")
434 SMTH (decode, "decode(BYTES) -> KD")
439 static const PyGetSetDef keydata_pygetset[] = {
440 #define GETSETNAME(op, name) kd##op##_##name
441 GET (flags, "KD.flags -> FLAGS")
446 static const PyTypeObject keydata_pytype_skel = {
447 PyVarObject_HEAD_INIT(0, 0) /* Header */
448 "KeyData", /* @tp_name@ */
449 sizeof(keydata_pyobj), /* @tp_basicsize@ */
450 0, /* @tp_itemsize@ */
452 keydata_pydealloc, /* @tp_dealloc@ */
454 0, /* @tp_getattr@ */
455 0, /* @tp_setattr@ */
456 0, /* @tp_compare@ */
458 0, /* @tp_as_number@ */
459 0, /* @tp_as_sequence@ */
460 0, /* @tp_as_mapping@ */
464 0, /* @tp_getattro@ */
465 0, /* @tp_setattro@ */
466 0, /* @tp_as_buffer@ */
467 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
471 "Key data base class.",
473 0, /* @tp_traverse@ */
475 0, /* @tp_richcompare@ */
476 0, /* @tp_weaklistoffset@ */
478 0, /* @tp_iternext@ */
479 PYMETHODS(keydata), /* @tp_methods@ */
480 0, /* @tp_members@ */
481 PYGETSET(keydata), /* @tp_getset@ */
484 0, /* @tp_descr_get@ */
485 0, /* @tp_descr_set@ */
486 0, /* @tp_dictoffset@ */
488 PyType_GenericAlloc, /* @tp_alloc@ */
489 abstract_pynew, /* @tp_new@ */
494 static PyObject *keydatabin_pynew(PyTypeObject *ty,
495 PyObject *arg, PyObject *kw)
499 keydata_pyobj *me = 0;
500 static const char *const kwlist[] = { "key", "flags", 0 };
502 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
503 convbin, &in, convflags, &f))
505 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
506 me->kd = key_newbinary(f & ~KF_ENCMASK, in.p, in.sz);
508 return ((PyObject *)me);
511 static PyObject *kdbget_bin(PyObject *me, void *hunoz)
512 { return (bytestring_pywrap(KEYDATA_KD(me)->u.k.k,
513 KEYDATA_KD(me)->u.k.sz)); }
515 static const PyGetSetDef keydatabin_pygetset[] = {
516 #define GETSETNAME(op, name) kdb##op##_##name
517 GET (bin, "KD.bin -> BYTES")
522 static const PyTypeObject keydatabin_pytype_skel = {
523 PyVarObject_HEAD_INIT(0, 0) /* Header */
524 "KeyDataBinary", /* @tp_name@ */
525 sizeof(keydata_pyobj), /* @tp_basicsize@ */
526 0, /* @tp_itemsize@ */
528 0, /* @tp_dealloc@ */
530 0, /* @tp_getattr@ */
531 0, /* @tp_setattr@ */
532 0, /* @tp_compare@ */
534 0, /* @tp_as_number@ */
535 0, /* @tp_as_sequence@ */
536 0, /* @tp_as_mapping@ */
540 0, /* @tp_getattro@ */
541 0, /* @tp_setattro@ */
542 0, /* @tp_as_buffer@ */
543 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
547 "KeyDataBinary(KEY, [flags = 0]): key data for binary keys.",
549 0, /* @tp_traverse@ */
551 0, /* @tp_richcompare@ */
552 0, /* @tp_weaklistoffset@ */
554 0, /* @tp_iternext@ */
555 0, /* @tp_methods@ */
556 0, /* @tp_members@ */
557 PYGETSET(keydatabin), /* @tp_getset@ */
560 0, /* @tp_descr_get@ */
561 0, /* @tp_descr_set@ */
562 0, /* @tp_dictoffset@ */
564 PyType_GenericAlloc, /* @tp_alloc@ */
565 keydatabin_pynew, /* @tp_new@ */
570 static PyObject *keydataenc_pynew(PyTypeObject *ty,
571 PyObject *arg, PyObject *kw)
575 keydata_pyobj *me = 0;
576 static const char *const kwlist[] = { "key", "flags", 0 };
578 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
579 convbin, &in, convflags, &f))
581 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
582 me->kd = key_newencrypted(f & ~KF_ENCMASK, in.p, in.sz);
584 return ((PyObject *)me);
587 static PyObject *kdemeth_plock(PyObject *me, PyObject *arg)
590 if (!PyArg_ParseTuple(arg, "s:plock", &hunoz)) goto end;
591 KEYERR(KERR_WRONGTYPE);
596 static PyObject *kdemeth_lock(PyObject *me, PyObject *arg)
599 if (!PyArg_ParseTuple(arg, "O&:lock", convbin, &hunoz)) goto end;
600 KEYERR(KERR_WRONGTYPE);
605 static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg)
612 if (!PyArg_ParseTuple(arg, "s:punlock", &tag))
614 if ((err = key_punlock(&kd, KEYDATA_KD(me), tag)) != 0)
616 rc = keydata_pywrap(kd);
621 static PyObject *kdemeth_unlock(PyObject *me, PyObject *arg)
628 if (!PyArg_ParseTuple(arg, "O&:unlock", convbin, &pp))
630 if ((err = key_unlock(&kd, KEYDATA_KD(me), pp.p, pp.sz)) != 0)
632 rc = keydata_pywrap(kd);
637 #define kdeget_ct kdbget_bin
639 static const PyMethodDef keydataenc_pymethods[] = {
640 #define METHNAME(func) kdemeth_##func
641 METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD")
642 METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD")
643 METH (punlock, "KD.punlock(TAG) -> KD")
644 METH (unlock, "KD.unlock(KEY) -> KD")
649 static const PyGetSetDef keydataenc_pygetset[] = {
650 #define GETSETNAME(op, name) kde##op##_##name
651 GET (ct, "KD.ct -> BYTES")
656 static const PyTypeObject keydataenc_pytype_skel = {
657 PyVarObject_HEAD_INIT(0, 0) /* Header */
658 "KeyDataEncrypted", /* @tp_name@ */
659 sizeof(keydata_pyobj), /* @tp_basicsize@ */
660 0, /* @tp_itemsize@ */
662 0, /* @tp_dealloc@ */
664 0, /* @tp_getattr@ */
665 0, /* @tp_setattr@ */
666 0, /* @tp_compare@ */
668 0, /* @tp_as_number@ */
669 0, /* @tp_as_sequence@ */
670 0, /* @tp_as_mapping@ */
674 0, /* @tp_getattro@ */
675 0, /* @tp_setattro@ */
676 0, /* @tp_as_buffer@ */
677 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
681 "KeyDataEncrypted(KEY, [flags = 0]): key data for encrypted keys.",
683 0, /* @tp_traverse@ */
685 0, /* @tp_richcompare@ */
686 0, /* @tp_weaklistoffset@ */
688 0, /* @tp_iternext@ */
689 PYMETHODS(keydataenc), /* @tp_methods@ */
690 0, /* @tp_members@ */
691 PYGETSET(keydataenc), /* @tp_getset@ */
694 0, /* @tp_descr_get@ */
695 0, /* @tp_descr_set@ */
696 0, /* @tp_dictoffset@ */
698 PyType_GenericAlloc, /* @tp_alloc@ */
699 keydataenc_pynew, /* @tp_new@ */
704 static PyObject *keydatamp_pynew(PyTypeObject *ty,
705 PyObject *arg, PyObject *kw)
709 keydata_pyobj *me = 0;
710 static const char *const kwlist[] = { "key", "flags", 0 };
712 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
713 convmp, &x, convflags, &f))
715 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
716 me->kd = key_newmp(f & ~KF_ENCMASK, x);
719 return ((PyObject *)me);
722 static PyObject *kdmget_mp(PyObject *me, void *hunoz)
723 { return (mp_pywrap(MP_COPY(KEYDATA_KD(me)->u.m))); }
725 static const PyGetSetDef keydatamp_pygetset[] = {
726 #define GETSETNAME(op, name) kdm##op##_##name
727 GET (mp, "KD.mp -> X")
732 static const PyTypeObject keydatamp_pytype_skel = {
733 PyVarObject_HEAD_INIT(0, 0) /* Header */
734 "KeyDataMP", /* @tp_name@ */
735 sizeof(keydata_pyobj), /* @tp_basicsize@ */
736 0, /* @tp_itemsize@ */
738 0, /* @tp_dealloc@ */
740 0, /* @tp_getattr@ */
741 0, /* @tp_setattr@ */
742 0, /* @tp_compare@ */
744 0, /* @tp_as_number@ */
745 0, /* @tp_as_sequence@ */
746 0, /* @tp_as_mapping@ */
750 0, /* @tp_getattro@ */
751 0, /* @tp_setattro@ */
752 0, /* @tp_as_buffer@ */
753 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
757 "KeyDataMP(KEY, [flags = 0]): key data for large-integer keys.",
759 0, /* @tp_traverse@ */
761 0, /* @tp_richcompare@ */
762 0, /* @tp_weaklistoffset@ */
764 0, /* @tp_iternext@ */
765 0, /* @tp_methods@ */
766 0, /* @tp_members@ */
767 PYGETSET(keydatamp), /* @tp_getset@ */
770 0, /* @tp_descr_get@ */
771 0, /* @tp_descr_set@ */
772 0, /* @tp_dictoffset@ */
774 PyType_GenericAlloc, /* @tp_alloc@ */
775 keydatamp_pynew, /* @tp_new@ */
780 static PyObject *keydatastr_pynew(PyTypeObject *ty,
781 PyObject *arg, PyObject *kw)
785 keydata_pyobj *me = 0;
786 static const char *const kwlist[] = { "key", "flags", 0 };
788 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", KWLIST,
791 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
792 me->kd = key_newstring(f & ~KF_ENCMASK, p);
794 return ((PyObject *)me);
797 static PyObject *kdsget_str(PyObject *me, void *hunoz)
798 { return (TEXT_FROMSTR(KEYDATA_KD(me)->u.p)); }
800 static const PyGetSetDef keydatastr_pygetset[] = {
801 #define GETSETNAME(op, name) kds##op##_##name
802 GET (str, "KD.str -> STRING")
807 static const PyTypeObject keydatastr_pytype_skel = {
808 PyVarObject_HEAD_INIT(0, 0) /* Header */
809 "KeyDataString", /* @tp_name@ */
810 sizeof(keydata_pyobj), /* @tp_basicsize@ */
811 0, /* @tp_itemsize@ */
813 0, /* @tp_dealloc@ */
815 0, /* @tp_getattr@ */
816 0, /* @tp_setattr@ */
817 0, /* @tp_compare@ */
819 0, /* @tp_as_number@ */
820 0, /* @tp_as_sequence@ */
821 0, /* @tp_as_mapping@ */
825 0, /* @tp_getattro@ */
826 0, /* @tp_setattro@ */
827 0, /* @tp_as_buffer@ */
828 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
832 "KeyDataString(KEY, [flags = 0]): key data for string keys.",
834 0, /* @tp_traverse@ */
836 0, /* @tp_richcompare@ */
837 0, /* @tp_weaklistoffset@ */
839 0, /* @tp_iternext@ */
840 0, /* @tp_methods@ */
841 0, /* @tp_members@ */
842 PYGETSET(keydatastr), /* @tp_getset@ */
845 0, /* @tp_descr_get@ */
846 0, /* @tp_descr_set@ */
847 0, /* @tp_dictoffset@ */
849 PyType_GenericAlloc, /* @tp_alloc@ */
850 keydatastr_pynew, /* @tp_new@ */
855 static PyObject *keydataec_pynew(PyTypeObject *ty,
856 PyObject *arg, PyObject *kw)
860 keydata_pyobj *me = 0;
861 static const char *const kwlist[] = { "key", "flags", 0 };
863 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
864 convecpt, &x, convflags, &f))
866 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
867 me->kd = key_newec(f & ~KF_ENCMASK, &x);
870 return ((PyObject *)me);
873 static PyObject *kdeget_ecpt(PyObject *me, void *hunoz)
876 EC_COPY(&pt, &KEYDATA_KD(me)->u.e);
877 return (ecpt_pywrapout(ecpt_pytype, &pt));
880 static const PyGetSetDef keydataec_pygetset[] = {
881 #define GETSETNAME(op, name) kde##op##_##name
882 GET (ecpt, "KD.ecpt -> ECPT")
887 static const PyTypeObject keydataec_pytype_skel = {
888 PyVarObject_HEAD_INIT(0, 0) /* Header */
889 "KeyDataECPt", /* @tp_name@ */
890 sizeof(keydata_pyobj), /* @tp_basicsize@ */
891 0, /* @tp_itemsize@ */
893 0, /* @tp_dealloc@ */
895 0, /* @tp_getattr@ */
896 0, /* @tp_setattr@ */
897 0, /* @tp_compare@ */
899 0, /* @tp_as_number@ */
900 0, /* @tp_as_sequence@ */
901 0, /* @tp_as_mapping@ */
905 0, /* @tp_getattro@ */
906 0, /* @tp_setattro@ */
907 0, /* @tp_as_buffer@ */
908 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
912 "KeyDataECPt(KEY, [flags = 0]): key data for elliptic-curve keys.",
914 0, /* @tp_traverse@ */
916 0, /* @tp_richcompare@ */
917 0, /* @tp_weaklistoffset@ */
919 0, /* @tp_iternext@ */
920 0, /* @tp_methods@ */
921 0, /* @tp_members@ */
922 PYGETSET(keydataec), /* @tp_getset@ */
925 0, /* @tp_descr_get@ */
926 0, /* @tp_descr_set@ */
927 0, /* @tp_dictoffset@ */
929 PyType_GenericAlloc, /* @tp_alloc@ */
930 keydataec_pynew, /* @tp_new@ */
935 static void *keydatastruct_gmlookup(PyObject *me, PyObject *k, unsigned *f)
937 key_data *kd = KEYDATA_KD(me);
941 assert((kd->e&KF_ENCMASK) == KENC_STRUCT);
942 if ((tag = TEXT_STR(k)) == 0) return (0);
943 if (f) { key_split(&kd); KEYDATA_KD(me) = kd; }
944 ks = sym_find(&kd->u.s, tag, -1, f ? sizeof(key_struct) : 0, f);
945 if (ks && f && !*f) ks->k = 0;
949 static void keydatastruct_gmiterinit(PyObject *me, void *i)
951 key_data *kd = KEYDATA_KD(me);
953 assert((kd->e&KF_ENCMASK) == KENC_STRUCT);
954 sym_mkiter(i, &kd->u.s);
957 static void *keydatastruct_gmiternext(PyObject *me, void *i)
958 { return (sym_next(i)); }
960 static PyObject *keydatastruct_gmentrykey(PyObject *me, void *e)
961 { key_struct *ks = e; return (TEXT_FROMSTR(SYM_NAME(ks))); }
963 static PyObject *keydatastruct_gmentryvalue(PyObject *me, void *e)
966 key_data *kd = ks->k;
969 return (keydata_pywrap(kd));
972 static key_struct *maybe_split_and_reprobe(PyObject *me, key_struct *ks)
974 key_data *kd = KEYDATA_KD(me);
976 if (kd->ref == 1) return (ks);
977 key_split(&kd); KEYDATA_KD(me) = kd;
978 ks = sym_find(&kd->u.s, SYM_NAME(ks), SYM_LEN(ks), 0, 0); assert(ks);
982 static int keydatastruct_gmsetentry(PyObject *me, void *e, PyObject *val)
988 if (!KEYDATA_PYCHECK(val)) TYERR("expected KeyData value");
989 ks = maybe_split_and_reprobe(me, e);
990 if (ks->k) key_drop(ks->k);
991 ks->k = kd = KEYDATA_KD(val); key_incref(kd);
997 static int keydatastruct_gmdelentry(PyObject *me, void *e)
1001 ks = maybe_split_and_reprobe(me, e);
1002 if (ks->k) key_drop(ks->k);
1003 sym_remove(&KEYDATA_KD(me)->u.s, ks);
1007 static const gmap_ops keydatastruct_gmops = {
1009 keydatastruct_gmlookup,
1010 keydatastruct_gmiterinit,
1011 keydatastruct_gmiternext,
1012 keydatastruct_gmentrykey,
1013 keydatastruct_gmentryvalue,
1014 keydatastruct_gmsetentry,
1015 keydatastruct_gmdelentry
1018 static int populate_struct(key_data *kd, PyObject *map)
1020 PyObject *it = 0, *name = 0, *val = 0;
1024 if (!PyMapping_Check(map)) TYERR("subkeys must be an iterable mapping");
1025 if ((it = PyObject_GetIter(map)) == 0) goto end;
1026 while ((name = PyIter_Next(it)) != 0) {
1027 if ((p = TEXT_STR(name)) == 0 ||
1028 (val = PyObject_GetItem(map, name)) == 0)
1030 if (!KEYDATA_PYCHECK(val))
1031 TYERR("subkey objects must be instances of KeyData");
1032 if (key_structfind(kd, p)) VALERR("duplicate tag");
1033 key_structset(kd, p, KEYDATA_KD(val));
1034 Py_DECREF(name); name = 0;
1035 Py_DECREF(val); val = 0;
1037 if (PyErr_Occurred()) goto end;
1040 Py_XDECREF(it); Py_XDECREF(name); Py_XDECREF(val);
1044 static PyObject *keydatastruct_pynew(PyTypeObject *ty,
1045 PyObject *arg, PyObject *kw)
1048 keydata_pyobj *me = 0;
1051 if (!PyArg_ParseTuple(arg, "|O:new", &sub)) goto end;
1052 kd = key_newstruct();
1053 if (sub && populate_struct(kd, sub)) goto end;
1054 if (kw && populate_struct(kd, kw)) goto end;
1055 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
1056 me->gmops = &keydatastruct_gmops;
1057 me->kd = kd; kd = 0;
1059 if (kd) key_drop(kd);
1060 return ((PyObject *)me);
1063 static Py_ssize_t keydatastruct_pysize(PyObject *me)
1064 { return gmap_pysize_from_sym(&KEYDATA_KD(me)->u.s); }
1066 static const PyMappingMethods keydatastruct_pymapping = {
1067 keydatastruct_pysize,
1072 static const PyTypeObject keydatastruct_pytype_skel = {
1073 PyVarObject_HEAD_INIT(0, 0) /* Header */
1074 "KeyDataStructured", /* @tp_name@ */
1075 sizeof(keydata_pyobj), /* @tp_basicsize@ */
1076 0, /* @tp_itemsize@ */
1078 0, /* @tp_dealloc@ */
1080 0, /* @tp_getattr@ */
1081 0, /* @tp_setattr@ */
1082 0, /* @tp_compare@ */
1084 0, /* @tp_as_number@ */
1085 PYSEQUENCE(gmap), /* @tp_as_sequence@ */
1086 PYMAPPING(keydatastruct), /* @tp_as_mapping@ */
1090 0, /* @tp_getattro@ */
1091 0, /* @tp_setattro@ */
1092 0, /* @tp_as_buffer@ */
1093 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1094 Py_TPFLAGS_BASETYPE,
1097 "KeyDataStructured([subkeys = []]): key data for structured keys.",
1099 0, /* @tp_traverse@ */
1101 0, /* @tp_richcompare@ */
1102 0, /* @tp_weaklistoffset@ */
1103 gmap_pyiter, /* @tp_iter@ */
1104 0, /* @tp_iternext@ */
1105 PYMETHODS(gmap), /* @tp_methods@ */
1106 0, /* @tp_members@ */
1107 0, /* @tp_getset@ */
1110 0, /* @tp_descr_get@ */
1111 0, /* @tp_descr_set@ */
1112 0, /* @tp_dictoffset@ */
1114 PyType_GenericAlloc, /* @tp_alloc@ */
1115 keydatastruct_pynew, /* @tp_new@ */
1120 /*----- Key attributes ----------------------------------------------------*/
1122 static void *keyattrs_gmlookup(PyObject *me, PyObject *k, unsigned *f)
1124 char *name = TEXT_STR(k);
1127 if (!name) goto end;
1128 if (f && !(KEYATTRS_KF(me)->f&KF_WRITE)) KEYERR(KERR_READONLY);
1129 a = sym_find(&KEYATTRS_K(me)->a, name, -1, f ? sizeof(key_attr) : 0, f);
1130 if (f && !*f) a->p = 0;
1135 static void keyattrs_gmiterinit(PyObject *me, void *i)
1136 { sym_mkiter(i, &KEYATTRS_K(me)->a); }
1138 static void *keyattrs_gmiternext(PyObject *me, void *i)
1139 { return (sym_next(i)); }
1141 static PyObject *keyattrs_gmentrykey(PyObject *me, void *e)
1142 { return (TEXT_FROMSTR(SYM_NAME(e))); }
1144 static PyObject *keyattrs_gmentryvalue(PyObject *me, void *e)
1145 { return (TEXT_FROMSTR(((key_attr *)e)->p)); }
1147 static int keyattrs_gmsetentry(PyObject *me, void *e, PyObject *val)
1154 if (!TEXT_CHECK(val)) TYERR("expected string");
1155 TEXT_PTRLEN(val, p, n);
1156 if (n > 255) VALERR("attribute too long");
1157 if (memchr(p, 0, n)) VALERR("attribute must not contain nul");
1158 if (a->p) xfree(a->p);
1159 a->p = xmalloc(n + 1); memcpy(a->p, p, n + 1);
1160 KEYATTRS_KF(me)->f |= KF_MODIFIED;
1166 static int keyattrs_gmdelentry(PyObject *me, void *e)
1171 if (!(KEYATTRS_KF(me)->f&KF_WRITE)) KEYERR(KERR_READONLY);
1172 if (a->p) { KEYATTRS_KF(me)->f |= KF_MODIFIED; xfree(a->p); }
1173 sym_remove(&KEYATTRS_K(me)->a, a);
1179 static const gmap_ops keyattrs_gmops = {
1182 keyattrs_gmiterinit,
1183 keyattrs_gmiternext,
1184 keyattrs_gmentrykey,
1185 keyattrs_gmentryvalue,
1186 keyattrs_gmsetentry,
1190 static PyObject *keyattrs_make(PyObject *kobj)
1192 keyattrs_pyobj *me = PyObject_NEW(keyattrs_pyobj, keyattrs_pytype);
1193 me->gmops = &keyattrs_gmops;
1196 return ((PyObject *)me);
1199 static void keyattrs_pydealloc(PyObject *me)
1201 Py_DECREF(KEYATTRS_KOBJ(me));
1205 static Py_ssize_t keyattrs_pysize(PyObject *me)
1206 { return gmap_pysize_from_sym(&KEYATTRS_K(me)->a); }
1208 static const PyMappingMethods keyattrs_pymapping = {
1214 static const PyTypeObject keyattrs_pytype_skel = {
1215 PyVarObject_HEAD_INIT(0, 0) /* Header */
1216 "KeyAttributes", /* @tp_name@ */
1217 sizeof(keyattrs_pyobj), /* @tp_basicsize@ */
1218 0, /* @tp_itemsize@ */
1220 keyattrs_pydealloc, /* @tp_dealloc@ */
1222 0, /* @tp_getattr@ */
1223 0, /* @tp_setattr@ */
1224 0, /* @tp_compare@ */
1226 0, /* @tp_as_number@ */
1227 PYSEQUENCE(gmap), /* @tp_as_sequence@ */
1228 PYMAPPING(keyattrs), /* @tp_as_mapping@ */
1232 0, /* @tp_getattro@ */
1233 0, /* @tp_setattro@ */
1234 0, /* @tp_as_buffer@ */
1235 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1236 Py_TPFLAGS_BASETYPE,
1239 "Proxy thing for talking about key attributes.",
1241 0, /* @tp_traverse@ */
1243 0, /* @tp_richcompare@ */
1244 0, /* @tp_weaklistoffset@ */
1245 gmap_pyiter, /* @tp_iter@ */
1246 0, /* @tp_iternext@ */
1247 PYMETHODS(gmap), /* @tp_methods@ */
1248 0, /* @tp_members@ */
1249 0, /* @tp_getset@ */
1252 0, /* @tp_descr_get@ */
1253 0, /* @tp_descr_set@ */
1254 0, /* @tp_dictoffset@ */
1256 PyType_GenericAlloc, /* @tp_alloc@ */
1257 abstract_pynew, /* @tp_new@ */
1262 /*----- Key objects -------------------------------------------------------*/
1264 static PyObject *key_dowrap(PyTypeObject *ty, PyObject *kfobj, key *k)
1266 key_pyobj *kobj = (key_pyobj *)ty->tp_alloc(ty, 0);
1267 kobj->kfobj = kfobj;
1270 return ((PyObject *)kobj);
1273 static PyObject *key_pywrap(PyObject *kfobj, key *k)
1274 { return (key_dowrap(key_pytype, kfobj, k)); }
1276 static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1281 unsigned long exptime = KEXP_FOREVER;
1282 static const char *const kwlist[] =
1283 { "keyfile", "id", "type", "exptime", 0 };
1287 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&s|O&:new", KWLIST,
1288 keyfile_pytype, &kfobj, convu32, &id,
1289 &type, convulong, &exptime))
1291 if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) != 0)
1293 return (key_dowrap(ty, kfobj, k));
1298 static void key_pydealloc(PyObject *me)
1300 Py_DECREF(KEY_KFOBJ(me));
1304 static Py_hash_t key_pyhash(PyObject *me)
1305 { return ((Py_hash_t)KEY_K(me)); }
1307 static PyObject *key_pyrichcompare(PyObject *me, PyObject *you, int op)
1309 if (!KEY_PYCHECK(you)) RETURN_NOTIMPL;
1311 case Py_EQ: return (getbool(KEY_K(me) == KEY_K(you)));
1312 case Py_NE: return (getbool(KEY_K(me) == KEY_K(you)));
1313 default: TYERR("ordering makes no sense");
1319 static PyObject *kmeth_delete(PyObject *me)
1323 if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
1329 static PyObject *kmeth_expire(PyObject *me)
1333 if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
1339 static PyObject *kmeth_used(PyObject *me, PyObject *arg)
1344 if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end;
1345 if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err);
1351 static PyObject *kmeth_extractline(PyObject *me, PyObject *arg, PyObject *kw)
1353 key_filter f = { 0, 0 };
1356 static const char *const kwlist[] = { "filter", 0 };
1358 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:extract", KWLIST,
1361 key_extractline(KEY_KF(me), KEY_K(me), &d, &f);
1362 rc = TEXT_FROMSTRLEN(d.buf, d.len);
1368 static PyObject *kmeth_fingerprint(PyObject *me,
1369 PyObject *arg, PyObject *kw)
1372 key_filter f = { KF_NONSECRET, KF_NONSECRET };
1373 static const char *const kwlist[] = { "hash", "filter", 0 };
1375 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", KWLIST,
1376 convghash, &h, convfilter, &f))
1378 return (getbool(key_fingerprint(KEY_K(me), h, &f)));
1381 static PyObject *kget_id(PyObject *me, void *hunoz)
1382 { return (getulong(KEY_K(me)->id)); }
1383 static PyObject *kget_file(PyObject *me, void *hunoz)
1384 { RETURN_OBJ(KEY_KFOBJ(me)); }
1385 static PyObject *kget_type(PyObject *me, void *hunoz)
1386 { return (TEXT_FROMSTR(KEY_K(me)->type)); }
1387 static PyObject *kget_exptime(PyObject *me, void *hunoz)
1388 { return (getulong(KEY_K(me)->exp)); }
1389 static PyObject *kget_deltime(PyObject *me, void *hunoz)
1390 { return (getulong(KEY_K(me)->del)); }
1391 static PyObject *kget_expiredp(PyObject *me, void *hunoz)
1392 { return (getbool(key_expired(KEY_K(me)))); }
1393 static PyObject *kget_attr(PyObject *me, void *hunoz)
1394 { return (keyattrs_make(me)); }
1396 static int kset_exptime(PyObject *me, PyObject *x, void *hunoz)
1401 if (!x) NIERR("__del__");
1402 if (!convulong(x, &et))
1404 if (!(KEY_KF(me)->f & KF_WRITE))
1405 KEYERR(KERR_READONLY);
1407 KEY_KF(me)->f |= KF_MODIFIED;
1413 static int kset_deltime(PyObject *me, PyObject *x, void *hunoz)
1418 if (!x) NIERR("__del__");
1419 if (!convulong(x, &dt))
1421 if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER)
1422 VALERR("key will eventually expire");
1423 if (!(KEY_KF(me)->f & KF_WRITE))
1424 KEYERR(KERR_READONLY);
1426 KEY_KF(me)->f |= KF_MODIFIED;
1432 static PyObject *kget_data(PyObject *me, void *hunoz)
1434 key_data *kd = KEY_K(me)->k;
1436 return (keydata_pywrap(kd));
1438 static int kset_data(PyObject *me, PyObject *x, void *hunoz)
1442 if (!x) NIERR("__del__");
1443 if (!KEYDATA_PYCHECK(x)) TYERR("expected KeyData object");
1444 if ((err = key_setkeydata(KEY_KF(me), KEY_K(me), KEYDATA_KD(x))) != 0)
1451 static PyObject *kget_fulltag(PyObject *me, void *hunoz)
1456 key_fulltag(KEY_K(me), &d);
1457 rc = TEXT_FROMSTRLEN(d.buf, d.len);
1462 static PyObject *kget_tag(PyObject *me, void *hunoz)
1464 if (!KEY_K(me)->tag) RETURN_NONE;
1465 return (TEXT_FROMSTR(KEY_K(me)->tag));
1467 static int kset_tag(PyObject *me, PyObject *x, void *hunoz)
1472 if (!x || x == Py_None) tag = 0;
1473 else if ((tag = TEXT_STR(x)) == 0) goto end;
1474 if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err);
1480 static PyObject *kget_comment(PyObject *me, void *hunoz)
1482 if (!KEY_K(me)->c) RETURN_NONE;
1483 return (TEXT_FROMSTR(KEY_K(me)->c));
1485 static int kset_comment(PyObject *me, PyObject *x, void *hunoz)
1490 if (!x || x == Py_None) c = 0;
1491 else if ((c = TEXT_STR(x)) == 0) goto end;
1492 if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err);
1498 static const PyMethodDef key_pymethods[] = {
1499 #define METHNAME(func) kmeth_##func
1500 NAMETH(delete, "KEY.delete()")
1501 NAMETH(expire, "KEY.expire()")
1502 METH (used, "KEY.used(TIME)")
1503 KWMETH(extractline, "KEY.extractline([filter = <any>])")
1504 KWMETH(fingerprint, "KEY.fingerprint(HASH, [filter = '-secret'])")
1509 static const PyGetSetDef key_pygetset[] = {
1510 #define GETSETNAME(op, name) k##op##_##name
1511 GET (file, "KEY.file -> KF")
1512 GET (id, "KEY.id -> ID")
1513 GETSET(tag, "KEY.tag -> TAG")
1514 GET (type, "KEY.type -> TYPE")
1515 GETSET(exptime, "KEY.exptime -> TIME")
1516 GETSET(deltime, "KEY.deltime -> TIME")
1517 GET (expiredp, "KEY.expiredp -> BOOL")
1518 GET (attr, "KEY.attr -> ATTRIBUTES")
1519 GETSET(data, "KEY.data -> KD")
1520 GETSET(comment, "KEY.comment -> COMMENT")
1521 GET (fulltag, "KEY.fulltag -> FULLTAG")
1526 static const PyTypeObject key_pytype_skel = {
1527 PyVarObject_HEAD_INIT(0, 0) /* Header */
1528 "Key", /* @tp_name@ */
1529 sizeof(key_pyobj), /* @tp_basicsize@ */
1530 0, /* @tp_itemsize@ */
1532 key_pydealloc, /* @tp_dealloc@ */
1534 0, /* @tp_getattr@ */
1535 0, /* @tp_setattr@ */
1536 0, /* @tp_compare@ */
1538 0, /* @tp_as_number@ */
1539 0, /* @tp_as_sequence@ */
1540 0, /* @tp_as_mapping@ */
1541 key_pyhash, /* @tp_hash@ */
1544 0, /* @tp_getattro@ */
1545 0, /* @tp_setattro@ */
1546 0, /* @tp_as_buffer@ */
1547 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1548 Py_TPFLAGS_BASETYPE,
1551 "Key(KF, ID, TYPE, [exptime = KEXP_FOREVER]): key object.",
1553 0, /* @tp_traverse@ */
1555 key_pyrichcompare, /* @tp_richcompare@ */
1556 0, /* @tp_weaklistoffset@ */
1558 0, /* @tp_iternext@ */
1559 PYMETHODS(key), /* @tp_methods@ */
1560 0, /* @tp_members@ */
1561 PYGETSET(key), /* @tp_getset@ */
1564 0, /* @tp_descr_get@ */
1565 0, /* @tp_descr_set@ */
1566 0, /* @tp_dictoffset@ */
1568 PyType_GenericAlloc, /* @tp_alloc@ */
1569 key_pynew, /* @tp_new@ */
1574 /*----- Key files ---------------------------------------------------------*/
1576 static key *bytag(PyObject *me, PyObject *tagobj)
1582 if (convu32(tagobj, &id))
1583 k = key_byid(KEYFILE_KF(me), id);
1586 if ((tag = TEXT_STR(tagobj)) == 0)
1588 k = key_bytag(KEYFILE_KF(me), tag);
1594 static void *keyfile_gmlookup(PyObject *me, PyObject *k, unsigned *f)
1595 { key *kk = bytag(me, k); if (f) *f = !!kk; return (kk); }
1597 static void keyfile_gmiterinit(PyObject *me, void *i)
1598 { key_mkiter(i, KEYFILE_KF(me)); }
1600 static void *keyfile_gmiternext(PyObject *me, void *i)
1601 { return (key_next(i)); }
1603 static PyObject *keyfile_gmentrykey(PyObject *me, void *e)
1604 { key *k = e; return (getulong(k->id)); }
1606 static PyObject *keyfile_gmentryvalue(PyObject *me, void *e)
1607 { return (key_pywrap(me, e)); }
1609 static const gmap_ops keyfile_gmops = {
1615 keyfile_gmentryvalue,
1625 static void pythonreporter(const char *file, int line,
1626 const char *msg, void *p)
1628 struct reportinfo *ri = p;
1633 if (ri->func == Py_None)
1634 key_moan(file, line, msg, 0);
1635 else if ((res = PyObject_CallFunction(ri->func, "sis",
1636 file, line, msg)) == 0)
1642 static PyObject *keyfile_pynew(PyTypeObject *ty,
1643 PyObject *arg, PyObject *kw)
1645 struct reportinfo ri = { Py_None, 0 };
1647 unsigned how = KOPEN_READ;
1648 keyfile_pyobj *rc = 0;
1649 static const char *const kwlist[] = { "file", "how", "report", 0 };
1651 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", KWLIST,
1652 &file, &how, &ri.func))
1654 if (ri.func != Py_None && !PyCallable_Check(ri.func))
1655 TYERR("reporter function not callable");
1656 if ((rc = (keyfile_pyobj *)ty->tp_alloc(ty, 0)) == 0)
1658 rc->gmops = &keyfile_gmops;
1659 if (key_open(&rc->kf, file, how, pythonreporter, &ri))
1662 key_discard(&rc->kf);
1673 return ((PyObject *)rc);
1676 static void keyfile_pydealloc(PyObject *me)
1678 key_discard(KEYFILE_KF(me));
1682 static PyObject *kfmeth_save(PyObject *me)
1684 switch (key_save(KEYFILE_KF(me))) {
1688 KEYIOERR(KEYFILE_KF(me)->name);
1690 KEYFILEBROKEN(KEYFILE_KF(me)->name);
1698 static PyObject *kfmeth_mergeline(PyObject *me, PyObject *arg, PyObject *kw)
1700 struct reportinfo ri = { Py_None, 0 };
1701 const char *file, *line;
1703 static const char *const kwlist[] = { "name", "lno", "line", "report", 0 };
1705 if (!PyArg_ParseTupleAndKeywords(arg, kw, "sis|O:merge", KWLIST,
1706 &file, &lno, &line, &ri.func))
1708 if (ri.func != Py_None && !PyCallable_Check(ri.func))
1709 TYERR("reporter function not callable");
1710 rc = key_mergeline(KEYFILE_KF(me), file, lno, line, pythonreporter, &ri);
1721 static PyObject *kfmeth_byid(PyObject *me, PyObject *arg, PyObject *kw)
1725 PyObject *failp = Py_True;
1727 static const char *const kwlist[] = { "id", "fail", 0 };
1729 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:byid", KWLIST,
1730 convu32, &id, &failp))
1732 if ((k = key_byid(KEYFILE_KF(me), id)) != 0) rc = key_pywrap(me, k);
1733 else if (PyObject_IsTrue(failp)) KEYERR(KERR_NOTFOUND);
1739 static PyObject *kfmeth_bytype(PyObject *me, PyObject *arg, PyObject *kw)
1743 PyObject *failp = Py_True;
1745 static const char *const kwlist[] = { "type", "fail", 0 };
1747 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O:bytype", KWLIST,
1750 if ((k = key_bytype(KEYFILE_KF(me), type)) != 0) rc = key_pywrap(me, k);
1751 else if (PyObject_IsTrue(failp)) KEYERR(KERR_NOTFOUND);
1757 static PyObject *kfmeth_bytag(PyObject *me, PyObject *arg, PyObject *kw)
1761 PyObject *failp = Py_True;
1763 static const char *const kwlist[] = { "type", "fail", 0 };
1765 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:bytag", KWLIST,
1768 if ((k = bytag(me, tagobj)) != 0) rc = key_pywrap(me, k);
1769 else if (PyObject_IsTrue(failp)) KEYERR(KERR_NOTFOUND);
1775 static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw)
1779 long exptime = KEXP_FOREVER;
1780 static const char *const kwlist[] = { "id", "type", "exptime", 0 };
1784 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", KWLIST,
1785 convu32, &id, &type, &exptime))
1787 if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) != 0)
1789 return (key_pywrap(me, k));
1794 static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw)
1797 key_data **kd, *okd;
1798 PyObject *newkdobj = 0;
1802 static const char *const kwlist[] = { "tag", "new", 0 };
1804 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", KWLIST,
1805 &tag, keydata_pytype, &newkdobj))
1807 if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd))
1808 KEYERR(KERR_NOTFOUND);
1811 if (!(KEYFILE_KF(me)->f & KF_WRITE))
1812 KEYERR(KERR_READONLY);
1813 KEYFILE_KF(me)->f |= KF_MODIFIED;
1814 *kd = KEYDATA_KD(newkdobj);
1817 rc = Py_BuildValue("(s#NN)",
1818 d.buf, (Py_ssize_t)d.len,
1820 keydata_pywrap(okd));
1825 static PyObject *kfget_name(PyObject *me, void *hunoz)
1826 { return (TEXT_FROMSTR(KEYFILE_KF(me)->name)); }
1827 static PyObject *kfget_modifiedp(PyObject *me, void *hunoz)
1828 { return (getbool(KEYFILE_KF(me)->f & KF_MODIFIED)); }
1829 static PyObject *kfget_writep(PyObject *me, void *hunoz)
1830 { return (getbool(KEYFILE_KF(me)->f & KF_WRITE)); }
1831 static PyObject *kfget_filep(PyObject *me, void *hunoz)
1832 { return (getbool(!!KEYFILE_KF(me)->fp)); }
1834 static const PyMethodDef keyfile_pymethods[] = {
1835 #define METHNAME(func) kfmeth_##func
1836 NAMETH(save, "KF.save()")
1837 KWMETH(mergeline, "KF.mergeline(NAME, LNO, LINE, "
1838 "[report = <built-in-reporter>])")
1839 KWMETH(newkey, "KF.newkey(ID, TYPE, [exptime = KEXP_FOREVER]) "
1841 KWMETH(byid, "KF.byid(KEYID, [fail = True]) -> KEY|None")
1842 KWMETH(bytype, "KF.bytype(TYPE, [fail = True]) -> KEY|None")
1843 KWMETH(bytag, "KF.bytag(TAG, [fail = True]) -> KEY|None")
1844 KWMETH(qtag, "KF.qtag(TAG, [new = KD]) -> FULLTAG, KEY, OLDKD")
1850 static const PyGetSetDef keyfile_pygetset[] = {
1851 #define GETSETNAME(op, name) kf##op##_##name
1852 GET (name, "KF.name -> file name")
1853 GET (modifiedp, "KF.modifiedp -> has keyring been modified?")
1854 GET (writep, "KF.writep -> is keyring modifiable?")
1855 GET (filep, "KF.filep -> does keyring have a backing file?")
1860 static const PyMappingMethods keyfile_pymapping = {
1866 static const PyTypeObject keyfile_pytype_skel = {
1867 PyVarObject_HEAD_INIT(0, 0) /* Header */
1868 "KeyFile", /* @tp_name@ */
1869 sizeof(keyfile_pyobj), /* @tp_basicsize@ */
1870 0, /* @tp_itemsize@ */
1872 keyfile_pydealloc, /* @tp_dealloc@ */
1874 0, /* @tp_getattr@ */
1875 0, /* @tp_setattr@ */
1876 0, /* @tp_compare@ */
1878 0, /* @tp_as_number@ */
1879 PYSEQUENCE(gmap), /* @tp_as_sequence@ */
1880 PYMAPPING(keyfile), /* @tp_as_mapping@ */
1884 0, /* @tp_getattro@ */
1885 0, /* @tp_setattro@ */
1886 0, /* @tp_as_buffer@ */
1887 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1888 Py_TPFLAGS_BASETYPE,
1891 "KeyFile(FILE, [how = KOPEN_READ], [report = ?]): Keyring file.\n"
1892 " calls REPORT(FILE, LINE, MSG) on problems",
1894 0, /* @tp_traverse@ */
1896 0, /* @tp_richcompare@ */
1897 0, /* @tp_weaklistoffset@ */
1898 gmap_pyiter, /* @tp_iter@ */
1899 0, /* @tp_iternext@ */
1900 PYMETHODS(keyfile), /* @tp_methods@ */
1901 0, /* @tp_members@ */
1902 PYGETSET(keyfile), /* @tp_getset@ */
1905 0, /* @tp_descr_get@ */
1906 0, /* @tp_descr_set@ */
1907 0, /* @tp_dictoffset@ */
1909 PyType_GenericAlloc, /* @tp_alloc@ */
1910 keyfile_pynew, /* @tp_new@ */
1915 /*----- Initialization ----------------------------------------------------*/
1917 static const struct nameval consts[] = {
1918 CONST(KOPEN_READ), CONST(KOPEN_WRITE), CONST(KOPEN_NOFILE),
1919 CONSTFLAG(0, KEXP_FOREVER), CONSTFLAG(0, KEXP_EXPIRE),
1920 CONST(KF_ENCMASK), CONST(KENC_BINARY), CONST(KENC_MP), CONST(KENC_STRUCT),
1921 CONST(KENC_ENCRYPT), CONST(KENC_STRING), CONST(KENC_EC),
1922 CONST(KF_CATMASK), CONST(KCAT_SYMM), CONST(KCAT_PRIV), CONST(KCAT_PUB),
1924 CONST(KF_NONSECRET),
1925 CONST(KF_BURN), CONST(KF_OPT),
1926 #define ENTRY(tag, val, str) CONST(KERR_##tag),
1932 void key_pyinit(void)
1934 INITTYPE(keyfile, root);
1935 INITTYPE(key, root);
1936 INITTYPE(keydata, root);
1937 INITTYPE(keydatabin, keydata);
1938 INITTYPE(keydataenc, keydata);
1939 INITTYPE(keydatastr, keydata);
1940 INITTYPE(keydatamp, keydata);
1941 INITTYPE(keydataec, keydata);
1942 INITTYPE(keydatastruct, keydata);
1943 INITTYPE(keyattrs, root);
1946 void key_pyinsert(PyObject *mod)
1948 INSEXC("KeyError", keyexc, PyExc_Exception, keyexc_pymethods);
1949 INSEXC("KeyFileIOError", keyioexc, PyExc_OSError, 0);
1950 INSEXC("KeyFileBroken", keyfilebrokenexc, keyioexc, 0);
1951 INSERT("KeyFile", keyfile_pytype);
1952 INSERT("Key", key_pytype);
1953 INSERT("KeyAttributes", keyattrs_pytype);
1954 INSERT("KeyData", keydata_pytype);
1955 INSERT("KeyDataBinary", keydatabin_pytype);
1956 INSERT("KeyDataEncrypted", keydataenc_pytype);
1957 INSERT("KeyDataMP", keydatamp_pytype);
1958 INSERT("KeyDataECPt", keydataec_pytype);
1959 INSERT("KeyDataString", keydatastr_pytype);
1960 INSERT("KeyDataStructured", keydatastruct_pytype);
1961 setconstants(mod, consts);
1964 /*----- That's all, folks -------------------------------------------------*/