chiark / gitweb /
key.c (convflags): Fix error message.
[catacomb-python] / key.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
3 * $Id$
4 *
5 * Key files and data
6 *
7 * (c) 2005 Straylight/Edgeware
8 */
9
b2687a0a 10/*----- Licensing notice --------------------------------------------------*
d7ab1bab 11 *
12 * This file is part of the Python interface to Catacomb.
13 *
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.
b2687a0a 18 *
d7ab1bab 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.
b2687a0a 23 *
d7ab1bab 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.
27 */
28
29/*----- Header files ------------------------------------------------------*/
30
31#include "catacomb-python.h"
32
46e6ad89 33/*----- Exceptions --------------------------------------------------------*/
34
35static PyObject *keyexc;
36static PyObject *keyioexc;
37static PyObject *keyfilebrokenexc;
38
39static PyObject *kxmeth___init__(PyObject *me, PyObject *arg)
40{
41 int err;
42 PyObject *x = 0;
43
44 if (!PyArg_ParseTuple(arg, "Oi:__init__", &me, &err) ||
45 (x = PyInt_FromLong(err)) == 0 ||
46 PyObject_SetAttrString(me, "err", x))
47 goto fail;
48 Py_DECREF(x); x = 0;
49 if ((x = PyString_FromString(key_strerror(err))) == 0 ||
50 PyObject_SetAttrString(me, "errstring", x))
51 goto fail;
52 Py_DECREF(x); x = 0;
53 if ((x = PyString_FromString(key_strerror(err))) == 0 ||
54 PyObject_SetAttrString(me, "errstring", x))
55 goto fail;
56 Py_DECREF(x); x = 0;
57 if ((x = PySequence_GetSlice(arg, 1, PySequence_Size(arg))) == 0 ||
58 PyObject_SetAttrString(me, "args", x))
59 goto fail;
60 Py_DECREF(x); x = 0;
61 RETURN_NONE;
62
63fail:
64 Py_XDECREF(x);
65 return (0);
66}
67
68static PyObject *kxmeth___str__(PyObject *me, PyObject *arg)
69{
70 long err;
71 const char *errstr, *errtag;
72 PyObject *x = 0;
73 PyObject *rc = 0;
74
75 static const char *const tab[] = {
76#define ENTRY(tag, num, str) "KERR_" #tag,
77 KEY_ERRORS(ENTRY)
78#undef ENTRY
79 };
80
81 if (!PyArg_ParseTuple(arg, "O:__str__", &me) ||
82 (x = PyObject_GetAttrString(me, "err")) == 0 ||
83 (err = PyInt_AsLong(x), PyErr_Occurred()))
84 goto done;
85 Py_DECREF(x); x = 0;
86 err = -err;
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)
91 goto done;
92 rc = PyString_FromFormat("%s (%ld): %s", errtag, -err, errstr);
93
94done:
95 Py_XDECREF(x);
96 return (rc);
97}
98
99static PyMethodDef keyexc_pymethods[] = {
100#define METHNAME(func) kxmeth_##func
101 METH (__init__, "KeyError(CODE)")
102 METH (__str__, "E.__str__() -> STRING")
103#undef METHNAME
104 { 0 }
105};
106
107static void keyexc_raise(int err)
108{
109 PyObject *arg = Py_BuildValue("(i)", err);
110 if (arg) PyErr_SetObject(keyexc, arg);
111 Py_XDECREF(arg);
112}
113#define KEYERR(err) do { keyexc_raise(err); goto end; } while (0)
114#define KEYIOERR(name) do { \
115 PyErr_SetFromErrnoWithFilename(keyioexc, name); \
116 goto end; \
117} while (0)
118#define KEYFILEBROKEN(name) do { \
119 PyErr_SetFromErrnoWithFilename(keyfilebrokenexc, name); \
120 goto end; \
121} while (0)
122
123/*----- Data structures ---------------------------------------------------*/
124
125typedef struct keydata_pyobj {
126 PyObject_HEAD
127 key_data *kd;
128} keydata_pyobj;
129
130static PyTypeObject *keydata_pytype;
131static PyTypeObject *keydatabin_pytype;
132static PyTypeObject *keydataenc_pytype;
133static PyTypeObject *keydatamp_pytype;
134static PyTypeObject *keydatastruct_pytype;
135static PyTypeObject *keydatastr_pytype;
136static PyTypeObject *keydataec_pytype;
137#define KEYDATA_PYCHECK(o) PyObject_TypeCheck(o, keydata_pytype)
138#define KEYDATA_KD(o) (((keydata_pyobj *)(o))->kd)
139
140typedef struct subkeyiter_pyobj {
141 PyObject_HEAD
142 key_subkeyiter i;
143 PyObject *kdobj;
144} subkeyiter_pyobj;
145
146static 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)
150
151typedef struct keyfile_pyobj {
152 PyObject_HEAD
153 key_file kf;
154} keyfile_pyobj;
155
156static PyTypeObject *keyfile_pytype;
157#define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype)
158#define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf)
159
160typedef struct key_pyobj {
161 PyObject_HEAD
162 key *k;
163 PyObject *kfobj;
164} key_pyobj;
165
166static 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))
171
172typedef struct keyiter_pyobj {
173 PyObject_HEAD
174 key_iter i;
175 PyObject *kfobj;
176} keyiter_pyobj;
177
178static 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))
183
184typedef struct keyattrs_pyobj {
185 PyObject_HEAD
186 PyObject *kobj;
187} keyattrs_pyobj;
188
189static 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))
194
195typedef struct keyattriter_pyobj {
196 PyObject_HEAD
197 key_attriter i;
198 PyObject *kobj;
199} keyattriter_pyobj;
200
201static 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))
208
209/*----- Filters -----------------------------------------------------------*/
210
211static int convfilter(PyObject *x, void *p)
212{
213 key_filter *f = p;
214 const char *fs;
215 char *end;
216 int n;
217 PyObject *a = 0, *b = 0;
218 int err;
219 int rc = 0;
220
221 if ((fs = PyString_AsString(x)) != 0) {
222 if ((err = key_readflags(fs, &end, &f->f, &f->m)) != 0)
223 KEYERR(err);
224 if (*end)
225 KEYERR(KERR_BADFLAGS);
226 } else {
227 PyErr_Clear();
228 if (!PySequence_Check(x))
229 goto tyerr;
230 else if ((n = PySequence_Size(x)) < 0)
231 goto end;
232 else if (n != 2)
233 goto tyerr;
234 else if ((a = PySequence_GetItem(x, 0)) == 0 || convuint(a, &f->f) ||
235 (b = PySequence_GetItem(x, 1)) == 0 || convuint(b, &f->m))
236 goto end;
237 }
238 rc = 1;
239 goto end;
240tyerr:
241 TYERR("expected flag string or flag/mask pair");
242end:
243 Py_XDECREF(a);
244 Py_XDECREF(b);
245 return (rc);
246}
247
248static int convflags(PyObject *x, void *p)
249{
250 unsigned *f = p;
251 const char *fs;
252 char *end;
253 int err;
254 int rc = 0;
255
256 if (convuint(x, p))
257 return (1);
258 else {
259 PyErr_Clear();
260 if ((fs = PyString_AsString(x)) != 0) {
261 if ((err = key_readflags(fs, &end, f, 0)) != 0)
262 KEYERR(err);
263 if (*end)
264 KEYERR(KERR_BADFLAGS);
265 } else {
266 PyErr_Clear();
267 goto tyerr;
268 }
269 }
270 rc = 1;
271 goto end;
272tyerr:
6140fbf7 273 TYERR("expected flag string or integer bitfield");
46e6ad89 274end:
275 return (rc);
276}
277
278static PyObject *meth__KeyData_readflags(PyObject *me, PyObject *arg)
279{
280 const char *p;
281 char *end;
282 unsigned f, m;
283 PyObject *rc = 0;
284 int err;
285
286 if (!PyArg_ParseTuple(arg, "Os:key_readflags", &me, &p))
287 goto end;
288 if ((err = key_readflags(p, &end, &f, &m)) != 0)
289 KEYERR(err);
fbca05a1 290 rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end);
46e6ad89 291end:
292 return (rc);
293}
294
295static PyObject *meth__KeyData_writeflags(PyObject *me, PyObject *arg)
296{
297 dstr d = DSTR_INIT;
298 PyObject *rc;
299 unsigned f;
300
301 if (!PyArg_ParseTuple(arg, "OO&:key_writeflags", &me, convuint, &f))
302 return (0);
303 key_writeflags(f, &d);
304 rc = PyString_FromStringAndSize(d.buf, d.len);
305 dstr_destroy(&d);
306 return (rc);
307}
308
309/*----- Key data ----------------------------------------------------------*/
310
311static PyObject *keydata_pywrap(key_data *kd)
312{
313 PyTypeObject *ty;
314 keydata_pyobj *kdobj;
315
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;
323 default: abort();
324 }
325 kdobj = PyObject_NEW(keydata_pyobj, ty);
326 kdobj->kd = kd;
327 return ((PyObject *)kdobj);
328}
329
330static void keydata_pydealloc(PyObject *me)
331{
332 key_drop(KEYDATA_KD(me));
333 FREEOBJ(me);
334}
335
336static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg)
337{
338 key_filter f;
b2687a0a 339
46e6ad89 340 if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f))
341 return (0);
342 return (getbool(KEY_MATCH(KEYDATA_KD(me), &f)));
343}
344
345static PyObject *kdmeth_split(PyObject *me, PyObject *arg)
346{
347 if (!PyArg_ParseTuple(arg, ":split"))
348 return (0);
349 key_split(&KEYDATA_KD(me));
350 RETURN_ME;
351}
352
353static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw)
354{
355 key_filter f = { 0, 0 };
356 dstr d = DSTR_INIT;
357 PyObject *rc = 0;
358 static char *kwlist[] = { "filter", 0 };
359
360 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", kwlist,
361 convfilter, &f))
362 return (0);
363 key_write(KEYDATA_KD(me), &d, &f);
364 rc = PyString_FromStringAndSize(d.buf, d.len);
365 dstr_destroy(&d);
366 return (rc);
367}
368
369static PyObject *kdmeth_encode(PyObject *me, PyObject *arg, PyObject *kw)
370{
371 key_filter f = { 0, 0 };
372 dstr d = DSTR_INIT;
373 PyObject *rc = 0;
374 static char *kwlist[] = { "filter", 0 };
375
376 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", kwlist,
377 convfilter, &f))
378 return (0);
379 key_encode(KEYDATA_KD(me), &d, &f);
380 rc = bytestring_pywrap(d.buf, d.len);
381 dstr_destroy(&d);
382 return (rc);
383}
384
385static PyObject *kdmeth_plock(PyObject *me, PyObject *arg)
386{
387 char *tag;
388 int err;
389 PyObject *rc = 0;
390 key_data *kd;
b2687a0a 391
46e6ad89 392 if (!PyArg_ParseTuple(arg, "s:plock", &tag))
393 goto end;
394 if ((err = key_plock(&kd, KEYDATA_KD(me), tag)) != 0)
395 KEYERR(err);
396 rc = keydata_pywrap(kd);
397end:
398 return (rc);
399}
400
401static PyObject *kdmeth_lock(PyObject *me, PyObject *arg)
402{
403 char *p;
404 int n;
405 PyObject *rc = 0;
406 key_data *kd;
b2687a0a 407
46e6ad89 408 if (!PyArg_ParseTuple(arg, "s#:lock", &p, &n))
409 goto end;
410 key_lock(&kd, KEYDATA_KD(me), p, n);
411 rc = keydata_pywrap(kd);
412end:
413 return (rc);
414}
415
416static PyObject *meth__KeyData_read(PyObject *me, PyObject *arg)
417{
418 const char *p;
419 char *end;
420 key_data *kd;
421 PyObject *rc = 0;
422
423 if (!PyArg_ParseTuple(arg, "Os:read", &me, &p))
424 goto end;
425 if ((kd = key_read(p, &end)) == 0)
426 KEYERR(KERR_MALFORMED);
427 rc = Py_BuildValue("(Ns)", keydata_pywrap(kd), end);
428end:
429 return (rc);
430}
431
432static PyObject *meth__KeyData_decode(PyObject *me, PyObject *arg)
433{
434 const char *p;
435 int n;
436 key_data *kd;
437 PyObject *rc = 0;
438
439 if (!PyArg_ParseTuple(arg, "Os#:decode", &me, &p, &n))
440 goto end;
441 if ((kd = key_decode(p, n)) == 0)
442 KEYERR(KERR_MALFORMED);
b2687a0a 443 rc = keydata_pywrap(kd);
46e6ad89 444end:
445 return (rc);
446}
447
448static PyObject *kdget_flags(PyObject *me, void *hunoz)
fbca05a1 449 { return (getulong(KEYDATA_KD(me)->e)); }
46e6ad89 450
451static 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")
459#undef METHNAME
460 { 0 }
461};
462
463static PyGetSetDef keydata_pygetset[] = {
464#define GETSETNAME(op, name) kd##op##_##name
465 GET (flags, "KD.flags -> FLAGS")
466#undef GETSETNAME
467 { 0 }
468};
469
470static 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@ */
475
476 keydata_pydealloc, /* @tp_dealloc@ */
477 0, /* @tp_print@ */
478 0, /* @tp_getattr@ */
479 0, /* @tp_setattr@ */
480 0, /* @tp_compare@ */
481 0, /* @tp_repr@ */
482 0, /* @tp_as_number@ */
483 0, /* @tp_as_sequence@ */
484 0, /* @tp_as_mapping@ */
485 0, /* @tp_hash@ */
486 0, /* @tp_call@ */
487 0, /* @tp_str@ */
488 0, /* @tp_getattro@ */
489 0, /* @tp_setattro@ */
490 0, /* @tp_as_buffer@ */
491 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
492 Py_TPFLAGS_BASETYPE,
493
494 /* @tp_doc@ */
495"Key data base class.",
496
497 0, /* @tp_traverse@ */
498 0, /* @tp_clear@ */
499 0, /* @tp_richcompare@ */
500 0, /* @tp_weaklistoffset@ */
501 0, /* @tp_iter@ */
963a6148 502 0, /* @tp_iternext@ */
46e6ad89 503 keydata_pymethods, /* @tp_methods@ */
504 0, /* @tp_members@ */
505 keydata_pygetset, /* @tp_getset@ */
506 0, /* @tp_base@ */
507 0, /* @tp_dict@ */
508 0, /* @tp_descr_get@ */
509 0, /* @tp_descr_set@ */
510 0, /* @tp_dictoffset@ */
511 0, /* @tp_init@ */
512 PyType_GenericAlloc, /* @tp_alloc@ */
513 abstract_pynew, /* @tp_new@ */
514 0, /* @tp_free@ */
515 0 /* @tp_is_gc@ */
516};
517
518static PyObject *keydatabin_pynew(PyTypeObject *ty,
519 PyObject *arg, PyObject *kw)
520{
521 char *p;
522 int n;
523 unsigned f = 0;
524 keydata_pyobj *me = 0;
525 static char *kwlist[] = { "key", "flags", 0 };
526
527 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist,
528 &p, &n, convflags, &f))
529 goto end;
530 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
531 me->kd = key_newbinary(f & ~KF_ENCMASK, p, n);
532end:
533 return ((PyObject *)me);
534}
535
536static PyObject *kdbget_bin(PyObject *me, void *hunoz)
537 { return (bytestring_pywrap(KEYDATA_KD(me)->u.k.k,
538 KEYDATA_KD(me)->u.k.sz)); }
539
540static PyGetSetDef keydatabin_pygetset[] = {
541#define GETSETNAME(op, name) kdb##op##_##name
542 GET (bin, "KD.bin -> BYTES")
543#undef GETSETNAME
544 { 0 }
545};
546
547static 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@ */
552
553 0, /* @tp_dealloc@ */
554 0, /* @tp_print@ */
555 0, /* @tp_getattr@ */
556 0, /* @tp_setattr@ */
557 0, /* @tp_compare@ */
558 0, /* @tp_repr@ */
559 0, /* @tp_as_number@ */
560 0, /* @tp_as_sequence@ */
561 0, /* @tp_as_mapping@ */
562 0, /* @tp_hash@ */
563 0, /* @tp_call@ */
564 0, /* @tp_str@ */
565 0, /* @tp_getattro@ */
566 0, /* @tp_setattro@ */
567 0, /* @tp_as_buffer@ */
568 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
569 Py_TPFLAGS_BASETYPE,
570
571 /* @tp_doc@ */
572"Key data for binary keys.",
573
574 0, /* @tp_traverse@ */
575 0, /* @tp_clear@ */
576 0, /* @tp_richcompare@ */
577 0, /* @tp_weaklistoffset@ */
578 0, /* @tp_iter@ */
963a6148 579 0, /* @tp_iternext@ */
46e6ad89 580 0, /* @tp_methods@ */
581 0, /* @tp_members@ */
582 keydatabin_pygetset, /* @tp_getset@ */
583 0, /* @tp_base@ */
584 0, /* @tp_dict@ */
585 0, /* @tp_descr_get@ */
586 0, /* @tp_descr_set@ */
587 0, /* @tp_dictoffset@ */
588 0, /* @tp_init@ */
589 PyType_GenericAlloc, /* @tp_alloc@ */
590 keydatabin_pynew, /* @tp_new@ */
591 0, /* @tp_free@ */
592 0 /* @tp_is_gc@ */
593};
594
595static PyObject *keydataenc_pynew(PyTypeObject *ty,
596 PyObject *arg, PyObject *kw)
597{
598 char *p;
599 int n;
600 unsigned f = 0;
601 keydata_pyobj *me = 0;
602 static char *kwlist[] = { "key", "flags", 0 };
603
604 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist,
605 &p, &n, convflags, &f))
606 goto end;
607 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
608 me->kd = key_newencrypted(f & ~KF_ENCMASK, p, n);
609end:
610 return ((PyObject *)me);
611}
612
613static PyObject *kdemeth_plock(PyObject *me, PyObject *arg)
614{
615 char *hunoz;
616 if (!PyArg_ParseTuple(arg, "s:plock", &hunoz)) goto end;
617 KEYERR(KERR_WRONGTYPE);
618end:
619 return (0);
620}
621
622static PyObject *kdemeth_lock(PyObject *me, PyObject *arg)
623{
624 char *hunoz;
625 int hukairz;
626 if (!PyArg_ParseTuple(arg, "s#:lock", &hunoz, &hukairz)) goto end;
627 KEYERR(KERR_WRONGTYPE);
628end:
629 return (0);
630}
631
632static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg)
633{
634 char *tag;
635 int err;
636 PyObject *rc = 0;
637 key_data *kd;
b2687a0a 638
46e6ad89 639 if (!PyArg_ParseTuple(arg, "s:punlock", &tag))
640 goto end;
641 if ((err = key_punlock(&kd, KEYDATA_KD(me), tag)) != 0)
642 KEYERR(err);
643 rc = keydata_pywrap(kd);
644end:
645 return (rc);
646}
647
648static PyObject *kdemeth_unlock(PyObject *me, PyObject *arg)
649{
650 char *p;
651 int n;
652 int err;
653 PyObject *rc = 0;
654 key_data *kd;
b2687a0a 655
46e6ad89 656 if (!PyArg_ParseTuple(arg, "s#:unlock", &p, &n))
657 goto end;
658 if ((err = key_unlock(&kd, KEYDATA_KD(me), p, n)) != 0)
659 KEYERR(err);
660 rc = keydata_pywrap(kd);
661end:
662 return (rc);
663}
664
665#define kdeget_ct kdbget_bin
666
667static 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")
673#undef METHNAME
674 { 0 }
675};
676
677static PyGetSetDef keydataenc_pygetset[] = {
678#define GETSETNAME(op, name) kde##op##_##name
679 GET (ct, "KD.ct -> BYTES")
680#undef GETSETNAME
681 { 0 }
682};
683
684static 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@ */
689
690 0, /* @tp_dealloc@ */
691 0, /* @tp_print@ */
692 0, /* @tp_getattr@ */
693 0, /* @tp_setattr@ */
694 0, /* @tp_compare@ */
695 0, /* @tp_repr@ */
696 0, /* @tp_as_number@ */
697 0, /* @tp_as_sequence@ */
698 0, /* @tp_as_mapping@ */
699 0, /* @tp_hash@ */
700 0, /* @tp_call@ */
701 0, /* @tp_str@ */
702 0, /* @tp_getattro@ */
703 0, /* @tp_setattro@ */
704 0, /* @tp_as_buffer@ */
705 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
706 Py_TPFLAGS_BASETYPE,
707
708 /* @tp_doc@ */
709"Key data for encrypted keys.",
710
711 0, /* @tp_traverse@ */
712 0, /* @tp_clear@ */
713 0, /* @tp_richcompare@ */
714 0, /* @tp_weaklistoffset@ */
715 0, /* @tp_iter@ */
963a6148 716 0, /* @tp_iternext@ */
46e6ad89 717 keydataenc_pymethods, /* @tp_methods@ */
718 0, /* @tp_members@ */
719 keydataenc_pygetset, /* @tp_getset@ */
720 0, /* @tp_base@ */
721 0, /* @tp_dict@ */
722 0, /* @tp_descr_get@ */
723 0, /* @tp_descr_set@ */
724 0, /* @tp_dictoffset@ */
725 0, /* @tp_init@ */
726 PyType_GenericAlloc, /* @tp_alloc@ */
727 keydataenc_pynew, /* @tp_new@ */
728 0, /* @tp_free@ */
729 0 /* @tp_is_gc@ */
730};
731
732static PyObject *keydatamp_pynew(PyTypeObject *ty,
733 PyObject *arg, PyObject *kw)
734{
735 mp *x = 0;
736 unsigned f = 0;
737 keydata_pyobj *me = 0;
738 static char *kwlist[] = { "key", "flags", 0 };
739
740 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
741 convmp, &x, convflags, &f))
742 goto end;
743 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
744 me->kd = key_newmp(f & ~KF_ENCMASK, x);
745end:
746 mp_drop(x);
747 return ((PyObject *)me);
748}
749
750static PyObject *kdmget_mp(PyObject *me, void *hunoz)
751 { return (mp_pywrap(MP_COPY(KEYDATA_KD(me)->u.m))); }
752
753static PyGetSetDef keydatamp_pygetset[] = {
754#define GETSETNAME(op, name) kdm##op##_##name
755 GET (mp, "KD.mp -> X")
756#undef GETSETNAME
757 { 0 }
758};
759
760static 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@ */
765
766 0, /* @tp_dealloc@ */
767 0, /* @tp_print@ */
768 0, /* @tp_getattr@ */
769 0, /* @tp_setattr@ */
770 0, /* @tp_compare@ */
771 0, /* @tp_repr@ */
772 0, /* @tp_as_number@ */
773 0, /* @tp_as_sequence@ */
774 0, /* @tp_as_mapping@ */
775 0, /* @tp_hash@ */
776 0, /* @tp_call@ */
777 0, /* @tp_str@ */
778 0, /* @tp_getattro@ */
779 0, /* @tp_setattro@ */
780 0, /* @tp_as_buffer@ */
781 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
782 Py_TPFLAGS_BASETYPE,
783
784 /* @tp_doc@ */
785"Key data for large-integer keys.",
786
787 0, /* @tp_traverse@ */
788 0, /* @tp_clear@ */
789 0, /* @tp_richcompare@ */
790 0, /* @tp_weaklistoffset@ */
791 0, /* @tp_iter@ */
963a6148 792 0, /* @tp_iternext@ */
46e6ad89 793 0, /* @tp_methods@ */
794 0, /* @tp_members@ */
795 keydatamp_pygetset, /* @tp_getset@ */
796 0, /* @tp_base@ */
797 0, /* @tp_dict@ */
798 0, /* @tp_descr_get@ */
799 0, /* @tp_descr_set@ */
800 0, /* @tp_dictoffset@ */
801 0, /* @tp_init@ */
802 PyType_GenericAlloc, /* @tp_alloc@ */
803 keydatamp_pynew, /* @tp_new@ */
804 0, /* @tp_free@ */
805 0 /* @tp_is_gc@ */
806};
807
808static PyObject *keydatastr_pynew(PyTypeObject *ty,
809 PyObject *arg, PyObject *kw)
810{
811 char *p;
812 unsigned f = 0;
813 keydata_pyobj *me = 0;
814 static char *kwlist[] = { "key", "flags", 0 };
815
816 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", kwlist,
817 &p, convflags, &f))
818 goto end;
819 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
820 me->kd = key_newstring(f & ~KF_ENCMASK, p);
821end:
822 return ((PyObject *)me);
823}
824
825static PyObject *kdsget_str(PyObject *me, void *hunoz)
826 { return (PyString_FromString(KEYDATA_KD(me)->u.p)); }
827
828static PyGetSetDef keydatastr_pygetset[] = {
829#define GETSETNAME(op, name) kds##op##_##name
830 GET (str, "KD.str -> STRING")
831#undef GETSETNAME
832 { 0 }
833};
834
835static 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@ */
840
841 0, /* @tp_dealloc@ */
842 0, /* @tp_print@ */
843 0, /* @tp_getattr@ */
844 0, /* @tp_setattr@ */
845 0, /* @tp_compare@ */
846 0, /* @tp_repr@ */
847 0, /* @tp_as_number@ */
848 0, /* @tp_as_sequence@ */
849 0, /* @tp_as_mapping@ */
850 0, /* @tp_hash@ */
851 0, /* @tp_call@ */
852 0, /* @tp_str@ */
853 0, /* @tp_getattro@ */
854 0, /* @tp_setattro@ */
855 0, /* @tp_as_buffer@ */
856 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
857 Py_TPFLAGS_BASETYPE,
858
859 /* @tp_doc@ */
860"Key data for string keys.",
861
862 0, /* @tp_traverse@ */
863 0, /* @tp_clear@ */
864 0, /* @tp_richcompare@ */
865 0, /* @tp_weaklistoffset@ */
866 0, /* @tp_iter@ */
963a6148 867 0, /* @tp_iternext@ */
46e6ad89 868 0, /* @tp_methods@ */
869 0, /* @tp_members@ */
870 keydatastr_pygetset, /* @tp_getset@ */
871 0, /* @tp_base@ */
872 0, /* @tp_dict@ */
873 0, /* @tp_descr_get@ */
874 0, /* @tp_descr_set@ */
875 0, /* @tp_dictoffset@ */
876 0, /* @tp_init@ */
877 PyType_GenericAlloc, /* @tp_alloc@ */
878 keydatastr_pynew, /* @tp_new@ */
879 0, /* @tp_free@ */
880 0 /* @tp_is_gc@ */
881};
882
883static PyObject *keydataec_pynew(PyTypeObject *ty,
884 PyObject *arg, PyObject *kw)
885{
886 ec x = EC_INIT;
887 unsigned f = 0;
888 keydata_pyobj *me = 0;
889 static char *kwlist[] = { "key", "flags", 0 };
890
891 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
892 convecpt, &x, convflags, &f))
893 goto end;
894 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
895 me->kd = key_newec(f & ~KF_ENCMASK, &x);
896end:
897 EC_DESTROY(&x);
898 return ((PyObject *)me);
899}
900
901static PyObject *kdeget_ecpt(PyObject *me, void *hunoz)
902{
903 ec pt = EC_INIT;
904 EC_COPY(&pt, &KEYDATA_KD(me)->u.e);
905 return (ecpt_pywrapout(ecpt_pytype, &pt));
906}
907
908static PyGetSetDef keydataec_pygetset[] = {
909#define GETSETNAME(op, name) kde##op##_##name
910 GET (ecpt, "KD.ecpt -> ECPT")
911#undef GETSETNAME
912 { 0 }
913};
914
915static 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@ */
920
921 0, /* @tp_dealloc@ */
922 0, /* @tp_print@ */
923 0, /* @tp_getattr@ */
924 0, /* @tp_setattr@ */
925 0, /* @tp_compare@ */
926 0, /* @tp_repr@ */
927 0, /* @tp_as_number@ */
928 0, /* @tp_as_sequence@ */
929 0, /* @tp_as_mapping@ */
930 0, /* @tp_hash@ */
931 0, /* @tp_call@ */
932 0, /* @tp_str@ */
933 0, /* @tp_getattro@ */
934 0, /* @tp_setattro@ */
935 0, /* @tp_as_buffer@ */
936 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
937 Py_TPFLAGS_BASETYPE,
938
939 /* @tp_doc@ */
940"Key data for elliptic-curve keys.",
941
942 0, /* @tp_traverse@ */
943 0, /* @tp_clear@ */
944 0, /* @tp_richcompare@ */
945 0, /* @tp_weaklistoffset@ */
946 0, /* @tp_iter@ */
963a6148 947 0, /* @tp_iternext@ */
46e6ad89 948 0, /* @tp_methods@ */
949 0, /* @tp_members@ */
950 keydataec_pygetset, /* @tp_getset@ */
951 0, /* @tp_base@ */
952 0, /* @tp_dict@ */
953 0, /* @tp_descr_get@ */
954 0, /* @tp_descr_set@ */
955 0, /* @tp_dictoffset@ */
956 0, /* @tp_init@ */
957 PyType_GenericAlloc, /* @tp_alloc@ */
958 keydataec_pynew, /* @tp_new@ */
959 0, /* @tp_free@ */
960 0 /* @tp_is_gc@ */
961};
962
963static PyObject *subkeyiter_make(PyObject *kdobj)
964{
965 subkeyiter_pyobj *me = PyObject_NEW(subkeyiter_pyobj, subkeyiter_pytype);
966 me->kdobj = kdobj;
967 Py_INCREF(kdobj);
968 key_mksubkeyiter(&me->i, KEYDATA_KD(kdobj));
969 return ((PyObject *)me);
970}
971
11cb3d97 972static PyObject *subkeyiter_pynext(PyObject *me)
46e6ad89 973{
974 const char *tag;
975 if (!key_nextsubkey(SUBKEYITER_I(me), &tag, 0))
976 return (0);
977 return (PyString_FromString(tag));
978}
979
980static void subkeyiter_pydealloc(PyObject *me)
981{
982 Py_DECREF(SUBKEYITER_KDOBJ(me));
983 FREEOBJ(me);
984}
985
986static 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@ */
991
992 subkeyiter_pydealloc, /* @tp_dealloc@ */
993 0, /* @tp_print@ */
994 0, /* @tp_getattr@ */
995 0, /* @tp_setattr@ */
996 0, /* @tp_compare@ */
997 0, /* @tp_repr@ */
998 0, /* @tp_as_number@ */
999 0, /* @tp_as_sequence@ */
1000 0, /* @tp_as_mapping@ */
1001 0, /* @tp_hash@ */
1002 0, /* @tp_call@ */
1003 0, /* @tp_str@ */
1004 0, /* @tp_getattro@ */
1005 0, /* @tp_setattro@ */
1006 0, /* @tp_as_buffer@ */
1007 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1008 Py_TPFLAGS_BASETYPE,
1009
1010 /* @tp_doc@ */
1011"Iterator for structured keys.",
1012
1013 0, /* @tp_traverse@ */
1014 0, /* @tp_clear@ */
1015 0, /* @tp_richcompare@ */
1016 0, /* @tp_weaklistoffset@ */
1017 PyObject_SelfIter, /* @tp_iter@ */
11cb3d97 1018 subkeyiter_pynext, /* @tp_iternext@ */
46e6ad89 1019 0, /* @tp_methods@ */
1020 0, /* @tp_members@ */
1021 0, /* @tp_getset@ */
1022 0, /* @tp_base@ */
1023 0, /* @tp_dict@ */
1024 0, /* @tp_descr_get@ */
1025 0, /* @tp_descr_set@ */
1026 0, /* @tp_dictoffset@ */
1027 0, /* @tp_init@ */
1028 PyType_GenericAlloc, /* @tp_alloc@ */
1029 abstract_pynew, /* @tp_new@ */
1030 0, /* @tp_free@ */
1031 0 /* @tp_is_gc@ */
1032};
1033
1034static PyObject *keydatastruct_pynew(PyTypeObject *ty,
1035 PyObject *arg, PyObject *kw)
1036{
1037 PyObject *sub = 0;
1038 PyObject *it = 0, *name = 0, *val = 0;
1039 char *p;
1040 keydata_pyobj *me = 0;
1041 key_data *kd = 0;
1042 static char *kwlist[] = { "subkeys", 0 };
1043
11cb3d97 1044 Py_XINCREF(arg); Py_XINCREF(kw);
46e6ad89 1045 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", kwlist, &sub))
1046 goto end;
1047 kd = key_newstruct();
1048 if (sub) {
1049 if (!PyMapping_Check(sub))
1050 TYERR("subkeys must be an iterable mapping");
1051 if ((it = PyObject_GetIter(sub)) == 0)
1052 goto end;
1053 while ((name = PyIter_Next(it)) != 0) {
1054 if ((p = PyString_AsString(name)) == 0 ||
1055 (val = PyObject_GetItem(sub, name)) == 0)
1056 goto end;
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;
1062 }
1063 if (PyErr_Occurred())
1064 goto end;
1065 Py_DECREF(it); it = 0;
1066 }
1067 me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
1068 me->kd = kd;
1069end:
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);
1074}
1075
1076static PyObject *keydatastruct_pylookup(PyObject *me, PyObject *key)
1077{
1078 const char *tag;
1079 key_data *kd;
1080 PyObject *rc = 0;
1081
1082 if ((tag = PyString_AsString(key)) == 0)
1083 goto end;
1084 if ((kd = key_structfind(KEYDATA_KD(me), tag)) == 0)
1085 INDEXERR(key);
1086 key_incref(kd);
1087 rc = keydata_pywrap(kd);
1088end:
1089 return (rc);
1090}
1091
1092static int keydatastruct_pystore(PyObject *me,
1093 PyObject *key, PyObject *value)
1094{
1095 const char *tag;
1096 int rc = -1;
1097
1098 if ((tag = PyString_AsString(key)) == 0)
1099 goto end;
5fbb60b8 1100 key_split(&KEYDATA_KD(me));
46e6ad89 1101 if (value) {
1102 if (!KEYDATA_PYCHECK(value))
1103 TYERR("expected KeyData value");
1104 key_structset(KEYDATA_KD(me), tag, KEYDATA_KD(value));
1105 } else {
1106 if (!key_structfind(KEYDATA_KD(me), tag))
1107 INDEXERR(key);
1108 key_structset(KEYDATA_KD(me), tag, 0);
1109 }
1110 rc = 0;
1111end:
1112 return (rc);
1113}
1114
1115static PyMappingMethods keydatastruct_pymapping = {
11cb3d97 1116 gmap_pysize, /* @mp_length@ */
46e6ad89 1117 keydatastruct_pylookup, /* @mp_subscript@ */
11cb3d97 1118 keydatastruct_pystore /* @mp_ass_subscript@ */
46e6ad89 1119};
1120
1121static PyTypeObject keydatastruct_pytype_skel = {
1122 PyObject_HEAD_INIT(0) 0, /* Header */
1123 "catacomb.KeyDataStructured", /* @tp_name@ */
1124 sizeof(keydata_pyobj), /* @tp_basicsize@ */
1125 0, /* @tp_itemsize@ */
1126
1127 0, /* @tp_dealloc@ */
1128 0, /* @tp_print@ */
1129 0, /* @tp_getattr@ */
1130 0, /* @tp_setattr@ */
1131 0, /* @tp_compare@ */
1132 0, /* @tp_repr@ */
1133 0, /* @tp_as_number@ */
1134 0, /* @tp_as_sequence@ */
1135 &keydatastruct_pymapping, /* @tp_as_mapping@ */
1136 0, /* @tp_hash@ */
1137 0, /* @tp_call@ */
1138 0, /* @tp_str@ */
1139 0, /* @tp_getattro@ */
1140 0, /* @tp_setattro@ */
1141 0, /* @tp_as_buffer@ */
1142 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1143 Py_TPFLAGS_BASETYPE,
1144
1145 /* @tp_doc@ */
1146"Key data for structured keys.",
1147
1148 0, /* @tp_traverse@ */
1149 0, /* @tp_clear@ */
1150 0, /* @tp_richcompare@ */
1151 0, /* @tp_weaklistoffset@ */
1152 subkeyiter_make, /* @tp_iter@ */
963a6148 1153 0, /* @tp_iternext@ */
11cb3d97 1154 gmap_pymethods, /* @tp_methods@ */
46e6ad89 1155 0, /* @tp_members@ */
1156 0, /* @tp_getset@ */
1157 0, /* @tp_base@ */
1158 0, /* @tp_dict@ */
1159 0, /* @tp_descr_get@ */
1160 0, /* @tp_descr_set@ */
1161 0, /* @tp_dictoffset@ */
1162 0, /* @tp_init@ */
1163 PyType_GenericAlloc, /* @tp_alloc@ */
1164 keydatastruct_pynew, /* @tp_new@ */
1165 0, /* @tp_free@ */
1166 0 /* @tp_is_gc@ */
1167};
1168
1169/*----- Key attributes ----------------------------------------------------*/
1170
1171static PyObject *keyattriter_make(PyObject *kaobj)
1172{
1173 PyObject *kobj = KEYATTRS_KOBJ(kaobj);
1174 keyattriter_pyobj *me = PyObject_NEW(keyattriter_pyobj,
1175 keyattriter_pytype);
1176 me->kobj = kobj;
1177 Py_INCREF(kobj);
1178 key_mkattriter(&me->i, KEY_K(kobj));
1179 return ((PyObject *)me);
1180}
1181
11cb3d97 1182static PyObject *keyattriter_pynext(PyObject *me)
46e6ad89 1183{
1184 const char *name;
1185
1186 if (!key_nextattr(KEYATTRITER_I(me), &name, 0))
1187 return (0);
1188 return (PyString_FromString(name));
1189}
1190
1191static void keyattriter_pydealloc(PyObject *me)
1192{
1193 Py_DECREF(KEYATTRITER_KOBJ(me));
1194 FREEOBJ(me);
1195}
1196
1197static PyTypeObject keyattriter_pytype_skel = {
1198 PyObject_HEAD_INIT(0) 0, /* Header */
1199 "catacomb.KeyAttributeIter", /* @tp_name@ */
1200 sizeof(keyattriter_pyobj), /* @tp_basicsize@ */
1201 0, /* @tp_itemsize@ */
1202
1203 keyattriter_pydealloc, /* @tp_dealloc@ */
1204 0, /* @tp_print@ */
1205 0, /* @tp_getattr@ */
1206 0, /* @tp_setattr@ */
1207 0, /* @tp_compare@ */
1208 0, /* @tp_repr@ */
1209 0, /* @tp_as_number@ */
1210 0, /* @tp_as_sequence@ */
1211 0, /* @tp_as_mapping@ */
1212 0, /* @tp_hash@ */
1213 0, /* @tp_call@ */
1214 0, /* @tp_str@ */
1215 0, /* @tp_getattro@ */
1216 0, /* @tp_setattro@ */
1217 0, /* @tp_as_buffer@ */
1218 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1219 Py_TPFLAGS_BASETYPE,
1220
1221 /* @tp_doc@ */
1222"Iterator for key attributes.",
1223
1224 0, /* @tp_traverse@ */
1225 0, /* @tp_clear@ */
1226 0, /* @tp_richcompare@ */
1227 0, /* @tp_weaklistoffset@ */
1228 PyObject_SelfIter, /* @tp_iter@ */
11cb3d97 1229 keyattriter_pynext, /* @tp_iternext@ */
46e6ad89 1230 0, /* @tp_methods@ */
1231 0, /* @tp_members@ */
1232 0, /* @tp_getset@ */
1233 0, /* @tp_base@ */
1234 0, /* @tp_dict@ */
1235 0, /* @tp_descr_get@ */
1236 0, /* @tp_descr_set@ */
1237 0, /* @tp_dictoffset@ */
1238 0, /* @tp_init@ */
1239 PyType_GenericAlloc, /* @tp_alloc@ */
1240 abstract_pynew, /* @tp_new@ */
1241 0, /* @tp_free@ */
1242 0 /* @tp_is_gc@ */
1243};
1244
1245static PyObject *keyattrs_pylookup(PyObject *me, PyObject *key)
1246{
1247 const char *attr;
1248 const char *value;
1249 PyObject *rc = 0;
1250
1251 if ((attr = PyString_AsString(key)) == 0)
1252 goto end;
1253 if ((value = key_getattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr)) == 0)
1254 INDEXERR(key);
1255 rc = PyString_FromString(value);
1256end:
1257 return (rc);
1258}
1259
1260static int keyattrs_pystore(PyObject *me,
1261 PyObject *key, PyObject *value)
1262{
1263 const char *attr;
1264 const char *val;
1265 int err;
1266 int rc = -1;
1267
1268 if ((attr = PyString_AsString(key)) == 0)
1269 goto end;
1270 if (value) {
1271 if ((val = PyString_AsString(value)) == 0)
1272 goto end;
1273 if ((err = key_putattr(KEYATTRS_KF(me), KEYATTRS_K(me),
1274 attr, val)) != 0)
1275 KEYERR(err);
1276 } else {
1277 if (!key_getattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr))
1278 INDEXERR(key);
1279 if ((err = key_putattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr, 0)) != 0)
1280 KEYERR(err);
1281 }
1282 rc = 0;
1283end:
1284 return (rc);
1285}
1286
1287static PyObject *keyattrs_make(PyObject *kobj)
1288{
1289 keyattrs_pyobj *me = PyObject_NEW(keyattrs_pyobj, keyattrs_pytype);
1290 me->kobj = kobj;
1291 Py_INCREF(kobj);
1292 return ((PyObject *)me);
1293}
1294
1295static void keyattrs_pydealloc(PyObject *me)
1296{
1297 Py_DECREF(KEYATTRS_KOBJ(me));
1298 FREEOBJ(me);
1299}
1300
1301static PyMappingMethods keyattrs_pymapping = {
11cb3d97
MW
1302 gmap_pysize, /* @mp_length@ */
1303 keyattrs_pylookup, /* @mp_subscript@ */
1304 keyattrs_pystore /* @mp_ass_subscript@ */
46e6ad89 1305};
1306
1307static PyTypeObject keyattrs_pytype_skel = {
1308 PyObject_HEAD_INIT(0) 0, /* Header */
1309 "catacomb.KeyAttributes", /* @tp_name@ */
1310 sizeof(keyattrs_pyobj), /* @tp_basicsize@ */
1311 0, /* @tp_itemsize@ */
1312
1313 keyattrs_pydealloc, /* @tp_dealloc@ */
1314 0, /* @tp_print@ */
1315 0, /* @tp_getattr@ */
1316 0, /* @tp_setattr@ */
1317 0, /* @tp_compare@ */
1318 0, /* @tp_repr@ */
1319 0, /* @tp_as_number@ */
11cb3d97 1320 &gmap_pysequence, /* @tp_as_sequence@ */
46e6ad89 1321 &keyattrs_pymapping, /* @tp_as_mapping@ */
1322 0, /* @tp_hash@ */
1323 0, /* @tp_call@ */
1324 0, /* @tp_str@ */
1325 0, /* @tp_getattro@ */
1326 0, /* @tp_setattro@ */
1327 0, /* @tp_as_buffer@ */
1328 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1329 Py_TPFLAGS_BASETYPE,
1330
1331 /* @tp_doc@ */
1332"Proxy thing for talking about key attributes.",
1333
1334 0, /* @tp_traverse@ */
1335 0, /* @tp_clear@ */
1336 0, /* @tp_richcompare@ */
1337 0, /* @tp_weaklistoffset@ */
1338 keyattriter_make, /* @tp_iter@ */
963a6148 1339 0, /* @tp_iternext@ */
11cb3d97 1340 gmap_pymethods, /* @tp_methods@ */
46e6ad89 1341 0, /* @tp_members@ */
1342 0, /* @tp_getset@ */
1343 0, /* @tp_base@ */
1344 0, /* @tp_dict@ */
1345 0, /* @tp_descr_get@ */
1346 0, /* @tp_descr_set@ */
1347 0, /* @tp_dictoffset@ */
1348 0, /* @tp_init@ */
1349 PyType_GenericAlloc, /* @tp_alloc@ */
1350 abstract_pynew, /* @tp_new@ */
1351 0, /* @tp_free@ */
1352 0 /* @tp_is_gc@ */
1353};
1354
1355/*----- Key objects -------------------------------------------------------*/
1356
1357static PyObject *key_dowrap(PyTypeObject *ty, PyObject *kfobj, key *k)
1358{
1359 key_pyobj *kobj = (key_pyobj *)ty->tp_alloc(ty, 0);
1360 kobj->kfobj = kfobj;
1361 Py_INCREF(kfobj);
1362 kobj->k = k;
1363 return ((PyObject *)kobj);
1364}
1365
1366static PyObject *key_pywrap(PyObject *kfobj, key *k)
1367 { return (key_dowrap(key_pytype, kfobj, k)); }
1368
1369static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1370{
1371 PyObject *kfobj;
1372 uint32 id;
1373 char *type;
1374 long exptime = KEXP_FOREVER;
1375 static char *kwlist[] = { "keyfile", "id", "type", "exptime", 0 };
1376 key *k;
1377 int err;
1378
1379 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&sl:new", kwlist,
1380 keyfile_pytype, &kfobj, convu32, &id,
1381 &type, &exptime))
1382 goto end;
1383 if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) == 0)
1384 KEYERR(err);
1385 return (key_dowrap(ty, kfobj, k));
1386end:
1387 return (0);
1388}
1389
1390static void key_pydealloc(PyObject *me)
1391{
1392 Py_DECREF(KEY_KFOBJ(me));
1393 FREEOBJ(me);
1394}
1395
1396static PyObject *kmeth_delete(PyObject *me, PyObject *arg)
1397{
1398 int err;
b2687a0a 1399
46e6ad89 1400 if (!PyArg_ParseTuple(arg, ":delete")) goto end;
1401 if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
1402 RETURN_ME;
1403end:
1404 return (0);
1405}
1406
1407static PyObject *kmeth_expire(PyObject *me, PyObject *arg)
1408{
1409 int err;
b2687a0a 1410
46e6ad89 1411 if (!PyArg_ParseTuple(arg, ":expire")) goto end;
1412 if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
1413 RETURN_ME;
1414end:
1415 return (0);
1416}
1417
1418static PyObject *kmeth_used(PyObject *me, PyObject *arg)
1419{
1420 long t;
1421 int err;
b2687a0a 1422
46e6ad89 1423 if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end;
1424 if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err);
1425 RETURN_ME;
1426end:
1427 return (0);
1428}
1429
1430static PyObject *kmeth_extract(PyObject *me, PyObject *arg, PyObject *kw)
1431{
1432 key_filter f = { 0, 0 };
1433 PyObject *file;
1434 PyObject *nameobj;
1435 char *name;
1436 FILE *fp;
1437 static char *kwlist[] = { "file", "filter", 0 };
1438
1439 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", kwlist,
1440 &PyFile_Type, &file,
1441 convfilter, &f) ||
1442 (fp = PyFile_AsFile(file)) == 0 ||
1443 (nameobj = PyFile_Name(file)) == 0 ||
1444 (name = PyString_AsString(nameobj)) == 0)
1445 goto end;
1446 if (key_extract(KEY_KF(me), KEY_K(me), fp, &f))
1447 OSERR(name);
1448 RETURN_ME;
1449end:
1450 return (0);
1451}
1452
1453static PyObject *kmeth_fingerprint(PyObject *me,
1454 PyObject *arg, PyObject *kw)
1455{
1456 ghash *h;
1457 key_filter f = { KF_NONSECRET, KF_NONSECRET };
1458 static char *kwlist[] = { "hash", "filter", 0 };
1459
1460 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", kwlist,
1461 convghash, &h, convfilter, &f))
1462 return (0);
1463 return (getbool(key_fingerprint(KEY_K(me), h, &f)));
1464}
1465
1466static PyObject *kget_id(PyObject *me, void *hunoz)
fbca05a1 1467 { return (getulong(KEY_K(me)->id)); }
11cb3d97
MW
1468static PyObject *kget_file(PyObject *me, void *hunoz)
1469 { RETURN_OBJ(KEY_KFOBJ(me)); }
46e6ad89 1470static PyObject *kget_type(PyObject *me, void *hunoz)
1471 { return (PyString_FromString(KEY_K(me)->type)); }
1472static PyObject *kget_exptime(PyObject *me, void *hunoz)
69ba6b57 1473 { return (getulong(KEY_K(me)->exp)); }
46e6ad89 1474static PyObject *kget_deltime(PyObject *me, void *hunoz)
69ba6b57 1475 { return (getulong(KEY_K(me)->del)); }
46e6ad89 1476static PyObject *kget_expiredp(PyObject *me, void *hunoz)
1477 { return (getbool(key_expired(KEY_K(me)))); }
1478static PyObject *kget_attr(PyObject *me, void *hunoz)
1479 { return (keyattrs_make(me)); }
1480
69ba6b57
MW
1481static int kset_exptime(PyObject *me, PyObject *x, void *hunoz)
1482{
1483 key *k = KEY_K(me);
1484 unsigned long et;
1485
61e45e9b 1486 if (!convulong(x, &et))
69ba6b57
MW
1487 goto end;
1488 if (!(KEY_KF(me)->f & KF_WRITE))
1489 KEYERR(KERR_READONLY);
1490 k->exp = et;
1491 KEY_KF(me)->f |= KF_MODIFIED;
1492 return (0);
1493end:
1494 return (-1);
1495}
1496
1497static int kset_deltime(PyObject *me, PyObject *x, void *hunoz)
1498{
1499 key *k = KEY_K(me);
1500 unsigned long dt;
1501
61e45e9b 1502 if (!convulong(x, &dt))
69ba6b57
MW
1503 goto end;
1504 if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER)
1505 VALERR("key will eventually expire");
1506 if (!(KEY_KF(me)->f & KF_WRITE))
1507 KEYERR(KERR_READONLY);
1508 k->del = dt;
1509 KEY_KF(me)->f |= KF_MODIFIED;
1510 return (0);
1511end:
1512 return (-1);
1513}
1514
46e6ad89 1515static PyObject *kget_data(PyObject *me, void *hunoz)
1516{
1517 key_data *kd = KEY_K(me)->k;
1518 key_incref(kd);
1519 return (keydata_pywrap(kd));
1520}
1521static int kset_data(PyObject *me, PyObject *x, void *hunoz)
1522{
1523 int err;
11cb3d97
MW
1524
1525 if (!x) NIERR("__del__");
46e6ad89 1526 if (!KEYDATA_PYCHECK(x)) TYERR("expected KeyData object");
1527 if ((err = key_setkeydata(KEY_KF(me), KEY_K(me), KEYDATA_KD(x))) != 0)
1528 KEYERR(err);
1529 return (0);
1530end:
1531 return (-1);
1532}
1533
1534static PyObject *kget_fulltag(PyObject *me, void *hunoz)
1535{
1536 dstr d = DSTR_INIT;
1537 PyObject *rc;
1538
1539 key_fulltag(KEY_K(me), &d);
1540 rc = PyString_FromStringAndSize(d.buf, d.len);
1541 dstr_destroy(&d);
1542 return (rc);
1543}
1544
1545static PyObject *kget_tag(PyObject *me, void *hunoz)
1546{
1547 if (!KEY_K(me)->tag) RETURN_NONE;
1548 return (PyString_FromString(KEY_K(me)->tag));
1549}
1550static int kset_tag(PyObject *me, PyObject *x, void *hunoz)
1551{
1552 int err;
1553 char *tag;
1554
11cb3d97 1555 if (!x || x == Py_None) tag = 0;
46e6ad89 1556 else if ((tag = PyString_AsString(x)) == 0) goto end;
1557 if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err);
1558 return (0);
1559end:
1560 return (-1);
1561}
1562
1563static PyObject *kget_comment(PyObject *me, void *hunoz)
1564{
1565 if (!KEY_K(me)->c) RETURN_NONE;
1566 return (PyString_FromString(KEY_K(me)->c));
1567}
1568static int kset_comment(PyObject *me, PyObject *x, void *hunoz)
1569{
1570 int err;
1571 char *c;
1572
11cb3d97 1573 if (!x || x == Py_None) c = 0;
46e6ad89 1574 else if ((c = PyString_AsString(x)) == 0) goto end;
1575 if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err);
1576 return (0);
1577end:
1578 return (-1);
1579}
1580
1581static PyMethodDef key_pymethods[] = {
1582#define METHNAME(func) kmeth_##func
1583 METH (delete, "KEY.delete()")
1584 METH (expire, "KEY.expire()")
1585 METH (used, "KEY.used(TIME)")
1586 KWMETH(extract, "KEY.extract(FILE, filter = '')")
1587 KWMETH(fingerprint, "KEY.fingerprint(HASH, filtr = '-secret')")
1588#undef METHNAME
1589 { 0 }
1590};
1591
1592static PyGetSetDef key_pygetset[] = {
1593#define GETSETNAME(op, name) k##op##_##name
11cb3d97 1594 GET (file, "KEY.file -> KF")
46e6ad89 1595 GET (id, "KEY.id -> ID")
1596 GETSET(tag, "KEY.tag -> TAG")
1597 GET (type, "KEY.type -> TYPE")
69ba6b57
MW
1598 GETSET(exptime, "KEY.exptime -> TIME")
1599 GETSET(deltime, "KEY.deltime -> TIME")
46e6ad89 1600 GET (expiredp, "KEY.expiredp -> BOOL")
1601 GET (attr, "KEY.attr -> ATTRIBUTES")
1602 GETSET(data, "KEY.data -> KD")
1603 GETSET(comment, "KEY.comment -> COMMENT")
1604 GET (fulltag, "KEY.fulltag -> FULLTAG")
1605#undef GETSETNAME
1606 { 0 }
1607};
1608
1609static PyTypeObject key_pytype_skel = {
1610 PyObject_HEAD_INIT(0) 0, /* Header */
1611 "catacomb.Key", /* @tp_name@ */
1612 sizeof(key_pyobj), /* @tp_basicsize@ */
1613 0, /* @tp_itemsize@ */
1614
1615 key_pydealloc, /* @tp_dealloc@ */
1616 0, /* @tp_print@ */
1617 0, /* @tp_getattr@ */
1618 0, /* @tp_setattr@ */
1619 0, /* @tp_compare@ */
1620 0, /* @tp_repr@ */
1621 0, /* @tp_as_number@ */
1622 0, /* @tp_as_sequence@ */
1623 0, /* @tp_as_mapping@ */
1624 0, /* @tp_hash@ */
1625 0, /* @tp_call@ */
1626 0, /* @tp_str@ */
1627 0, /* @tp_getattro@ */
1628 0, /* @tp_setattro@ */
1629 0, /* @tp_as_buffer@ */
1630 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1631 Py_TPFLAGS_BASETYPE,
1632
1633 /* @tp_doc@ */
1634"Key object.",
1635
1636 0, /* @tp_traverse@ */
1637 0, /* @tp_clear@ */
1638 0, /* @tp_richcompare@ */
1639 0, /* @tp_weaklistoffset@ */
1640 0, /* @tp_iter@ */
963a6148 1641 0, /* @tp_iternext@ */
46e6ad89 1642 key_pymethods, /* @tp_methods@ */
1643 0, /* @tp_members@ */
1644 key_pygetset, /* @tp_getset@ */
1645 0, /* @tp_base@ */
1646 0, /* @tp_dict@ */
1647 0, /* @tp_descr_get@ */
1648 0, /* @tp_descr_set@ */
1649 0, /* @tp_dictoffset@ */
1650 0, /* @tp_init@ */
1651 PyType_GenericAlloc, /* @tp_alloc@ */
1652 key_pynew, /* @tp_new@ */
1653 0, /* @tp_free@ */
1654 0 /* @tp_is_gc@ */
1655};
1656
1657/*----- Key iteration -----------------------------------------------------*/
1658
1659static PyObject *keyiter_new(PyObject *kfobj)
1660{
1661 keyiter_pyobj *me = PyObject_NEW(keyiter_pyobj, keyiter_pytype);
1662 key_mkiter(&me->i, KEYFILE_KF(kfobj));
1663 me->kfobj = kfobj;
1664 Py_INCREF(kfobj);
1665 return ((PyObject *)me);
1666}
1667
11cb3d97 1668static PyObject *keyiter_pynext(PyObject *me)
46e6ad89 1669{
1670 key *k;
1671
1672 if ((k = key_next(KEYITER_I(me))) == 0)
1673 return (0);
426e898f 1674 return (getulong(k->id));
46e6ad89 1675}
1676
1677static void keyiter_pydealloc(PyObject *me)
1678{
1679 Py_DECREF(KEYITER_KFOBJ(me));
1680 FREEOBJ(me);
1681}
1682
1683static PyTypeObject keyiter_pytype_skel = {
1684 PyObject_HEAD_INIT(0) 0, /* Header */
1685 "catacomb.KeyFileIter", /* @tp_name@ */
1686 sizeof(keyiter_pyobj), /* @tp_basicsize@ */
1687 0, /* @tp_itemsize@ */
1688
1689 keyiter_pydealloc, /* @tp_dealloc@ */
1690 0, /* @tp_print@ */
1691 0, /* @tp_getattr@ */
1692 0, /* @tp_setattr@ */
1693 0, /* @tp_compare@ */
1694 0, /* @tp_repr@ */
1695 0, /* @tp_as_number@ */
1696 0, /* @tp_as_sequence@ */
1697 0, /* @tp_as_mapping@ */
1698 0, /* @tp_hash@ */
1699 0, /* @tp_call@ */
1700 0, /* @tp_str@ */
1701 0, /* @tp_getattro@ */
1702 0, /* @tp_setattro@ */
1703 0, /* @tp_as_buffer@ */
1704 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1705 Py_TPFLAGS_BASETYPE,
1706
1707 /* @tp_doc@ */
1708"Keyring iterator.",
1709
1710 0, /* @tp_traverse@ */
1711 0, /* @tp_clear@ */
1712 0, /* @tp_richcompare@ */
1713 0, /* @tp_weaklistoffset@ */
1714 PyObject_SelfIter, /* @tp_iter@ */
11cb3d97 1715 keyiter_pynext, /* @tp_iternext@ */
46e6ad89 1716 0, /* @tp_methods@ */
1717 0, /* @tp_members@ */
1718 0, /* @tp_getset@ */
1719 0, /* @tp_base@ */
1720 0, /* @tp_dict@ */
1721 0, /* @tp_descr_get@ */
1722 0, /* @tp_descr_set@ */
1723 0, /* @tp_dictoffset@ */
1724 0, /* @tp_init@ */
1725 PyType_GenericAlloc, /* @tp_alloc@ */
1726 abstract_pynew, /* @tp_new@ */
1727 0, /* @tp_free@ */
1728 0 /* @tp_is_gc@ */
1729};
1730
d7ab1bab 1731/*----- Key files ---------------------------------------------------------*/
1732
46e6ad89 1733struct reportinfo {
1734 PyObject *func;
1735 int stop;
1736};
1737
1738static void pythonreporter(const char *file, int line,
1739 const char *msg, void *p)
1740{
1741 struct reportinfo *ri = p;
1742 PyObject *res = 0;
1743
1744 if (ri->stop)
1745 return;
1746 if (!ri->func)
1747 key_moan(file, line, msg, 0);
1748 else if ((res = PyObject_CallFunction(ri->func, "sis",
1749 file, line, msg)) == 0)
1750 ri->stop = 1;
1751 else
1752 Py_DECREF(res);
1753}
1754
d7ab1bab 1755static PyObject *keyfile_pynew(PyTypeObject *ty,
1756 PyObject *arg, PyObject *kw)
1757{
46e6ad89 1758 struct reportinfo ri = { 0, 0 };
1759 char *file = 0;
1760 unsigned how = KOPEN_READ;
1761 keyfile_pyobj *rc = 0;
1762 static char *kwlist[] = { "file", "how", "report", 0 };
1763
1764 Py_XINCREF(arg); Py_XINCREF(kw);
1765 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", kwlist,
1766 &file, &how, &ri.func))
1767 goto end;
1768 if (ri.func && !PyCallable_Check(ri.func))
1769 TYERR("reporter function not callable");
1770 if ((rc = (keyfile_pyobj *)ty->tp_alloc(ty, 0)) == 0)
1771 goto end;
1772 if (key_open(&rc->kf, file, how, pythonreporter, &ri))
1773 OSERR(file);
1774 if (ri.stop) {
1775 key_discard(&rc->kf);
1776 goto end;
1777 }
1778 goto done;
1779
1780end:
1781 if (rc) {
1782 FREEOBJ(rc);
1783 rc = 0;
1784 }
1785done:
1786 Py_XDECREF(arg); Py_XDECREF(kw);
1787 return ((PyObject *)rc);
1788}
1789
1790static void keyfile_pydealloc(PyObject *me)
1791{
1792 key_discard(KEYFILE_KF(me));
1793 FREEOBJ(me);
1794}
1795
1796static PyObject *kfmeth_save(PyObject *me, PyObject *arg)
1797{
1798 if (!PyArg_ParseTuple(arg, ":save"))
1799 goto end;
1800 switch (key_save(KEYFILE_KF(me))) {
1801 case KWRITE_OK:
1802 RETURN_ME;
1803 case KWRITE_FAIL:
1804 KEYIOERR(KEYFILE_KF(me)->name);
1805 case KWRITE_BROKEN:
1806 KEYFILEBROKEN(KEYFILE_KF(me)->name);
1807 default:
1808 abort();
1809 }
1810end:
1811 return (0);
1812}
1813
1814static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw)
1815{
1816 struct reportinfo ri = { 0, 0 };
1817 char *name;
1818 PyObject *x = 0;
1819 FILE *fp = 0;
1820 int rc;
1821 static char *kwlist[] = { "file", "report", 0 };
1822
1823 Py_XINCREF(arg); Py_XINCREF(kw);
1824 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", kwlist,
1825 &PyFile_Type, &x, &ri.func))
1826 goto end;
1827 if (ri.func && !PyCallable_Check(ri.func))
1828 TYERR("reporter function not callable");
1829 if ((fp = PyFile_AsFile(x)) == 0)
1830 goto end;
1831 x = PyFile_Name(x);
1832 if ((name = PyString_AsString(x)) == 0)
1833 goto end;
1834 rc = key_merge(KEYFILE_KF(me), name, fp, pythonreporter, &ri);
1835 if (ri.stop)
1836 goto end;
1837 if (rc != 0)
1838 KEYERR(rc);
1839 Py_XDECREF(arg); Py_XDECREF(kw);
1840 RETURN_ME;
1841
1842end:
1843 Py_XDECREF(arg); Py_XDECREF(kw);
1844 return (0);
1845}
1846
1847static PyObject *kfmeth_byid(PyObject *me, PyObject *arg)
1848{
1849 uint32 id;
1850 key *k;
1851 PyObject *rc = 0;
1852
1853 if (!PyArg_ParseTuple(arg, "O&:byid", convu32, &id)) goto end;
1854 if ((k = key_byid(KEYFILE_KF(me), id)) == 0) KEYERR(KERR_NOTFOUND);
1855 rc = key_pywrap(me, k);
1856end:
1857 return (rc);
1858}
1859
1860static PyObject *kfmeth_bytype(PyObject *me, PyObject *arg)
1861{
1862 char *type;
1863 key *k;
1864 PyObject *rc = 0;
1865
1866 if (!PyArg_ParseTuple(arg, "s:bytype", &type)) goto end;
1867 if ((k = key_bytype(KEYFILE_KF(me), type)) == 0) RETURN_NONE;
1868 rc = key_pywrap(me, k);
1869end:
1870 return (rc);
1871}
1872
1873static PyObject *bytag(PyObject *me, PyObject *tagobj)
1874{
1875 uint32 id;
1876 char *tag;
1877 key *k;
1878 PyObject *rc = 0;
1879
1880 if (convu32(tagobj, &id))
1881 k = key_byid(KEYFILE_KF(me), id);
1882 else {
1883 PyErr_Clear();
1884 if ((tag = PyString_AsString(tagobj)) == 0)
1885 goto end;
1886 k = key_bytag(KEYFILE_KF(me), tag);
1887 }
1888 if (!k) RETURN_NONE;
1889 rc = key_pywrap(me, k);
1890end:
1891 return (rc);
1892}
1893
1894static PyObject *kfmeth_bytag(PyObject *me, PyObject *arg)
1895{
1896 PyObject *tagobj;
1897
1898 if (!PyArg_ParseTuple(arg, "O:bytag", &tagobj)) return (0);
1899 return (bytag(me, tagobj));
1900}
1901
1902static PyObject *keyfile_pylookup(PyObject *me, PyObject *key)
1903{
1904 PyObject *rc = bytag(me, key);
1905 if (!rc) goto end;
1906 if (rc == Py_None) {
1907 Py_DECREF(rc);
1908 rc = 0;
1909 INDEXERR(key);
1910 }
1911end:
1912 return (rc);
b2687a0a 1913}
46e6ad89 1914
1915static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw)
1916{
1917 uint32 id;
1918 char *type;
1919 long exptime = KEXP_FOREVER;
1920 static char *kwlist[] = { "id", "type", "exptime", 0 };
1921 key *k;
1922 int err;
1923
24b3d57b 1924 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", kwlist,
46e6ad89 1925 convu32, &id, &type, &exptime))
1926 goto end;
24b3d57b 1927 if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) != 0)
46e6ad89 1928 KEYERR(err);
1929 return (key_pywrap(me, k));
1930end:
1931 return (0);
1932}
1933
1934static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw)
1935{
1936 key *k;
ae5cafdb 1937 key_data **kd, *okd;
46e6ad89 1938 PyObject *newkdobj = 0;
1939 char *tag;
46e6ad89 1940 dstr d = DSTR_INIT;
1941 PyObject *rc = 0;
1942 static char *kwlist[] = { "tag", "new", 0 };
1943
1944 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", kwlist,
1945 &tag, keydata_pytype, &newkdobj))
1946 goto end;
1947 if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd))
1948 KEYERR(KERR_NOTFOUND);
ae5cafdb 1949 okd = *kd;
46e6ad89 1950 if (newkdobj) {
1951 if (!(KEYFILE_KF(me)->f & KF_WRITE))
1952 KEYERR(KERR_READONLY);
ae5cafdb 1953 KEYFILE_KF(me)->f |= KF_MODIFIED;
46e6ad89 1954 *kd = KEYDATA_KD(newkdobj);
46e6ad89 1955 }
ae5cafdb 1956 key_incref(*kd);
1957 rc = Py_BuildValue("(s#NN)",
1958 d.buf, d.len,
1959 key_pywrap(me, k),
1960 keydata_pywrap(okd));
46e6ad89 1961end:
1962 return (rc);
1963}
1964
1965static PyObject *kfget_name(PyObject *me, void *hunoz)
1966 { return (PyString_FromString(KEYFILE_KF(me)->name)); }
1967static PyObject *kfget_modifiedp(PyObject *me, void *hunoz)
1968 { return (getbool(KEYFILE_KF(me)->f & KF_MODIFIED)); }
1969static PyObject *kfget_writep(PyObject *me, void *hunoz)
1970 { return (getbool(KEYFILE_KF(me)->f & KF_WRITE)); }
1971static PyObject *kfget_filep(PyObject *me, void *hunoz)
1972 { return (getbool(!!KEYFILE_KF(me)->fp)); }
1973
1974static PyMethodDef keyfile_pymethods[] = {
1975#define METHNAME(func) kfmeth_##func
1976 METH (save, "KF.save()")
1977 KWMETH(merge, "KF.merge(FILE, report = <built-in-reporter>)")
1978 KWMETH(newkey, "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY")
1979 METH (byid, "KF.byid(KEYID) -> KEY|None")
1980 METH (bytype, "KF.bytype(TYPE) -> KEY|None")
1981 METH (bytag, "KF.bytag(TAG) -> KEY|None")
1982 KWMETH(qtag, "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD")
810c8f3c 1983 GMAP_ROMETHODS
46e6ad89 1984#undef METHNAME
1985 { 0 }
1986};
1987
1988static PyGetSetDef keyfile_pygetset[] = {
1989#define GETSETNAME(op, name) kf##op##_##name
1990 GET (name, "KF.name -> file name")
1991 GET (modifiedp, "KF.modifiedp -> has keyring been modified?")
1992 GET (writep, "KF.writep -> is keyring modifiable?")
1993 GET (filep, "KF.filep -> does keyring have a backing file?")
1994#undef GETSETNAME
1995 { 0 }
1996};
1997
1998static PyMappingMethods keyfile_pymapping = {
810c8f3c 1999 gmap_pysize,
46e6ad89 2000 keyfile_pylookup,
2001 0
2002};
2003
2004static PyTypeObject keyfile_pytype_skel = {
2005 PyObject_HEAD_INIT(0) 0, /* Header */
2006 "catacomb.KeyFile", /* @tp_name@ */
2007 sizeof(keyfile_pyobj), /* @tp_basicsize@ */
2008 0, /* @tp_itemsize@ */
2009
2010 keyfile_pydealloc, /* @tp_dealloc@ */
2011 0, /* @tp_print@ */
2012 0, /* @tp_getattr@ */
2013 0, /* @tp_setattr@ */
2014 0, /* @tp_compare@ */
2015 0, /* @tp_repr@ */
2016 0, /* @tp_as_number@ */
11cb3d97 2017 &gmap_pysequence, /* @tp_as_sequence@ */
46e6ad89 2018 &keyfile_pymapping, /* @tp_as_mapping@ */
2019 0, /* @tp_hash@ */
2020 0, /* @tp_call@ */
2021 0, /* @tp_str@ */
2022 0, /* @tp_getattro@ */
2023 0, /* @tp_setattro@ */
2024 0, /* @tp_as_buffer@ */
2025 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2026 Py_TPFLAGS_BASETYPE,
2027
2028 /* @tp_doc@ */
2029"Keyring file.",
2030
2031 0, /* @tp_traverse@ */
2032 0, /* @tp_clear@ */
2033 0, /* @tp_richcompare@ */
2034 0, /* @tp_weaklistoffset@ */
2035 keyiter_new, /* @tp_iter@ */
963a6148 2036 0, /* @tp_iternext@ */
46e6ad89 2037 keyfile_pymethods, /* @tp_methods@ */
2038 0, /* @tp_members@ */
2039 keyfile_pygetset, /* @tp_getset@ */
2040 0, /* @tp_base@ */
2041 0, /* @tp_dict@ */
2042 0, /* @tp_descr_get@ */
2043 0, /* @tp_descr_set@ */
2044 0, /* @tp_dictoffset@ */
2045 0, /* @tp_init@ */
2046 PyType_GenericAlloc, /* @tp_alloc@ */
2047 keyfile_pynew, /* @tp_new@ */
2048 0, /* @tp_free@ */
2049 0 /* @tp_is_gc@ */
2050};
2051
2052/*----- Global stuff ------------------------------------------------------*/
2053
2054static PyObject *meth_barf(PyObject *me, PyObject *arg)
2055{
2056 int err;
2057
2058 if (PyArg_ParseTuple(arg, "i:barf", &err))
2059 KEYERR(err);
2060end:
2061 return (0);
2062}
2063
2064static PyMethodDef methods[] = {
2065#define METHNAME(func) meth_##func
2066 METH (_KeyData_readflags,
2067 "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)")
2068 METH (_KeyData_writeflags, "KeyData.writeflags(FLAGS) -> STRING")
2069 METH (_KeyData_read, "KeyData.read(STRING) -> (KD, REST)")
2070 METH (_KeyData_decode, "KeyData.read(BYTES) -> KD")
2071 METH (barf, "barf(ERR)")
2072#undef METHNAME
2073 { 0 }
2074};
2075
2076/*----- Initialization ----------------------------------------------------*/
2077
2078void key_pyinit(void)
2079{
2080 INITTYPE(keyfile, root);
2081 INITTYPE(key, root);
2082 INITTYPE(keyiter, root);
2083 INITTYPE(keydata, root);
2084 INITTYPE(keydatabin, keydata);
2085 INITTYPE(keydataenc, keydata);
2086 INITTYPE(keydatastr, keydata);
2087 INITTYPE(keydatamp, keydata);
2088 INITTYPE(keydataec, keydata);
2089 INITTYPE(keydatastruct, keydata);
2090 INITTYPE(subkeyiter, root);
2091 INITTYPE(keyattrs, root);
2092 INITTYPE(keyattriter, root);
2093 addmethods(methods);
2094}
2095
2096void key_pyinsert(PyObject *mod)
2097{
2098 INSEXC("KeyError", keyexc, PyExc_Exception, keyexc_pymethods);
2099 INSEXC("KeyFileIOError", keyioexc, PyExc_OSError, 0);
2100 INSEXC("KeyFileBroken", keyfilebrokenexc, keyioexc, 0);
2101 INSERT("KeyFile", keyfile_pytype);
2102 INSERT("KeyFileIter", keyiter_pytype);
2103 INSERT("Key", key_pytype);
2104 INSERT("KeyAttributes", keyattrs_pytype);
2105 INSERT("KeyAttributeIter", keyattriter_pytype);
2106 INSERT("KeyData", keydata_pytype);
2107 INSERT("KeyDataBinary", keydatabin_pytype);
2108 INSERT("KeyDataEncrypted", keydataenc_pytype);
2109 INSERT("KeyDataMP", keydatamp_pytype);
2110 INSERT("KeyDataECPt", keydataec_pytype);
2111 INSERT("KeyDataString", keydatastr_pytype);
2112 INSERT("KeyDataStructured", keydatastruct_pytype);
2113 INSERT("SubKeyIter", subkeyiter_pytype);
d7ab1bab 2114}
2115
2116/*----- That's all, folks -------------------------------------------------*/