d7ab1bab |
1 | /* -*-c-*- |
2 | * |
3 | * $Id$ |
4 | * |
5 | * Key files and data |
6 | * |
7 | * (c) 2005 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
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. |
18 | * |
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. |
23 | * |
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 | |
35 | static PyObject *keyexc; |
36 | static PyObject *keyioexc; |
37 | static PyObject *keyfilebrokenexc; |
38 | |
39 | static 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 | |
63 | fail: |
64 | Py_XDECREF(x); |
65 | return (0); |
66 | } |
67 | |
68 | static 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 | |
94 | done: |
95 | Py_XDECREF(x); |
96 | return (rc); |
97 | } |
98 | |
99 | static 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 | |
107 | static 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 | |
125 | typedef struct keydata_pyobj { |
126 | PyObject_HEAD |
127 | key_data *kd; |
128 | } keydata_pyobj; |
129 | |
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) |
139 | |
140 | typedef struct subkeyiter_pyobj { |
141 | PyObject_HEAD |
142 | key_subkeyiter i; |
143 | PyObject *kdobj; |
144 | } subkeyiter_pyobj; |
145 | |
146 | static PyTypeObject *subkeyiter_pytype; |
147 | #define SUBKEYITER_PYCHECK(o) PyObject_TypeCheck(o, subkeyiter_pytype); |
148 | #define SUBKEYITER_I(o) (&((subkeyiter_pyobj *)(o))->i) |
149 | #define SUBKEYITER_KDOBJ(o) (((subkeyiter_pyobj *)(o))->kdobj) |
150 | |
151 | typedef struct keyfile_pyobj { |
152 | PyObject_HEAD |
153 | key_file kf; |
154 | } keyfile_pyobj; |
155 | |
156 | static PyTypeObject *keyfile_pytype; |
157 | #define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype) |
158 | #define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf) |
159 | |
160 | typedef struct key_pyobj { |
161 | PyObject_HEAD |
162 | key *k; |
163 | PyObject *kfobj; |
164 | } key_pyobj; |
165 | |
166 | static PyTypeObject *key_pytype; |
167 | #define KEY_PYCHECK(o) PyObject_TypeCheck(o, key_pytype) |
168 | #define KEY_K(o) (((key_pyobj *)(o))->k) |
169 | #define KEY_KFOBJ(o) (((key_pyobj *)(o))->kfobj) |
170 | #define KEY_KF(o) KEYFILE_KF(KEY_KFOBJ(o)) |
171 | |
172 | typedef struct keyiter_pyobj { |
173 | PyObject_HEAD |
174 | key_iter i; |
175 | PyObject *kfobj; |
176 | } keyiter_pyobj; |
177 | |
178 | static PyTypeObject *keyiter_pytype; |
179 | #define KEYITER_PYCHECK(o) PyObject_TypeCheck(o, keyiter_pytype) |
180 | #define KEYITER_I(o) (&((keyiter_pyobj *)(o))->i) |
181 | #define KEYITER_KFOBJ(o) (((keyiter_pyobj *)(o))->kfobj) |
182 | #define KEYITER_KF(o) KEYFILE_KF(KEYITER_KFOBJ(o)) |
183 | |
184 | typedef struct keyattrs_pyobj { |
185 | PyObject_HEAD |
186 | PyObject *kobj; |
187 | } keyattrs_pyobj; |
188 | |
189 | static PyTypeObject *keyattrs_pytype; |
190 | #define KEYATTRS_PYCHECK(o) PyObject_TypeCheck(o, keyattrs_pytype) |
191 | #define KEYATTRS_KOBJ(o) (((keyattrs_pyobj *)(o))->kobj) |
192 | #define KEYATTRS_KF(o) KEY_KF(KEYATTRS_KOBJ(o)) |
193 | #define KEYATTRS_K(o) KEY_K(KEYATTRS_KOBJ(o)) |
194 | |
195 | typedef struct keyattriter_pyobj { |
196 | PyObject_HEAD |
197 | key_attriter i; |
198 | PyObject *kobj; |
199 | } keyattriter_pyobj; |
200 | |
201 | static PyTypeObject *keyattriter_pytype; |
202 | #define KEYATTRITER_PYCHECK(o) PyObject_TypeCheck(o, keyattriter_pytype) |
203 | #define KEYATTRITER_I(o) (&((keyattriter_pyobj *)(o))->i) |
204 | #define KEYATTRITER_KOBJ(o) (((keyattriter_pyobj *)(o))->kobj) |
205 | #define KEYATTRITER_K(o) KEY_K(KEYATTRITER_KOBJ(o)) |
206 | #define KEYATTRITER_KFOBJ(o) KEY_KFOBJ(KEYATTRITER_KOBJ(o)) |
207 | #define KEYATTRITER_KF(o) KEY_KF(KEYATTRITER_KOBJ(o)) |
208 | |
209 | /*----- Filters -----------------------------------------------------------*/ |
210 | |
211 | static 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; |
240 | tyerr: |
241 | TYERR("expected flag string or flag/mask pair"); |
242 | end: |
243 | Py_XDECREF(a); |
244 | Py_XDECREF(b); |
245 | return (rc); |
246 | } |
247 | |
248 | static 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; |
272 | tyerr: |
273 | TYERR("expected flag string or flag/mask pair"); |
274 | end: |
275 | return (rc); |
276 | } |
277 | |
278 | static 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); |
290 | rc = Py_BuildValue("(NNs)", getu32(f), getu32(m), end); |
291 | end: |
292 | return (rc); |
293 | } |
294 | |
295 | static 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 | |
311 | static 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 | |
330 | static void keydata_pydealloc(PyObject *me) |
331 | { |
332 | key_drop(KEYDATA_KD(me)); |
333 | FREEOBJ(me); |
334 | } |
335 | |
336 | static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg) |
337 | { |
338 | key_filter f; |
339 | |
340 | if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f)) |
341 | return (0); |
342 | return (getbool(KEY_MATCH(KEYDATA_KD(me), &f))); |
343 | } |
344 | |
345 | static 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 | |
353 | static 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 | |
369 | static 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 | |
385 | static PyObject *kdmeth_plock(PyObject *me, PyObject *arg) |
386 | { |
387 | char *tag; |
388 | int err; |
389 | PyObject *rc = 0; |
390 | key_data *kd; |
391 | |
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); |
397 | end: |
398 | return (rc); |
399 | } |
400 | |
401 | static PyObject *kdmeth_lock(PyObject *me, PyObject *arg) |
402 | { |
403 | char *p; |
404 | int n; |
405 | PyObject *rc = 0; |
406 | key_data *kd; |
407 | |
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); |
412 | end: |
413 | return (rc); |
414 | } |
415 | |
416 | static 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); |
428 | end: |
429 | return (rc); |
430 | } |
431 | |
432 | static 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); |
443 | rc = keydata_pywrap(kd); |
444 | end: |
445 | return (rc); |
446 | } |
447 | |
448 | static PyObject *kdget_flags(PyObject *me, void *hunoz) |
449 | { return (getu32(KEYDATA_KD(me)->e)); } |
450 | |
451 | static PyMethodDef keydata_pymethods[] = { |
452 | #define METHNAME(func) kdmeth_##func |
453 | METH (matchp, "KD.matchp(FILTER) -> BOOL") |
454 | METH (split, "KD.split()") |
455 | KWMETH(write, "KD.write(filter = <any>) -> STRING") |
456 | KWMETH(encode, "KD.encode(filter = <any>) -> BYTES") |
457 | METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD") |
458 | METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD") |
459 | #undef METHNAME |
460 | { 0 } |
461 | }; |
462 | |
463 | static PyGetSetDef keydata_pygetset[] = { |
464 | #define GETSETNAME(op, name) kd##op##_##name |
465 | GET (flags, "KD.flags -> FLAGS") |
466 | #undef GETSETNAME |
467 | { 0 } |
468 | }; |
469 | |
470 | static PyTypeObject keydata_pytype_skel = { |
471 | PyObject_HEAD_INIT(0) 0, /* Header */ |
472 | "catacomb.KeyData", /* @tp_name@ */ |
473 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
474 | 0, /* @tp_itemsize@ */ |
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 | |
518 | static 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); |
532 | end: |
533 | return ((PyObject *)me); |
534 | } |
535 | |
536 | static PyObject *kdbget_bin(PyObject *me, void *hunoz) |
537 | { return (bytestring_pywrap(KEYDATA_KD(me)->u.k.k, |
538 | KEYDATA_KD(me)->u.k.sz)); } |
539 | |
540 | static PyGetSetDef keydatabin_pygetset[] = { |
541 | #define GETSETNAME(op, name) kdb##op##_##name |
542 | GET (bin, "KD.bin -> BYTES") |
543 | #undef GETSETNAME |
544 | { 0 } |
545 | }; |
546 | |
547 | static PyTypeObject keydatabin_pytype_skel = { |
548 | PyObject_HEAD_INIT(0) 0, /* Header */ |
549 | "catacomb.KeyDataBinary", /* @tp_name@ */ |
550 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
551 | 0, /* @tp_itemsize@ */ |
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 | |
595 | static 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); |
609 | end: |
610 | return ((PyObject *)me); |
611 | } |
612 | |
613 | static 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); |
618 | end: |
619 | return (0); |
620 | } |
621 | |
622 | static 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); |
628 | end: |
629 | return (0); |
630 | } |
631 | |
632 | static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg) |
633 | { |
634 | char *tag; |
635 | int err; |
636 | PyObject *rc = 0; |
637 | key_data *kd; |
638 | |
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); |
644 | end: |
645 | return (rc); |
646 | } |
647 | |
648 | static 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; |
655 | |
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); |
661 | end: |
662 | return (rc); |
663 | } |
664 | |
665 | #define kdeget_ct kdbget_bin |
666 | |
667 | static PyMethodDef keydataenc_pymethods[] = { |
668 | #define METHNAME(func) kdemeth_##func |
669 | METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD") |
670 | METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD") |
671 | METH (punlock, "KD.punlock(TAG) -> KD") |
672 | METH (unlock, "KD.unlock(KEY) -> KD") |
673 | #undef METHNAME |
674 | { 0 } |
675 | }; |
676 | |
677 | static PyGetSetDef keydataenc_pygetset[] = { |
678 | #define GETSETNAME(op, name) kde##op##_##name |
679 | GET (ct, "KD.ct -> BYTES") |
680 | #undef GETSETNAME |
681 | { 0 } |
682 | }; |
683 | |
684 | static PyTypeObject keydataenc_pytype_skel = { |
685 | PyObject_HEAD_INIT(0) 0, /* Header */ |
686 | "catacomb.KeyDataEncrypted", /* @tp_name@ */ |
687 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
688 | 0, /* @tp_itemsize@ */ |
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 | |
732 | static 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); |
745 | end: |
746 | mp_drop(x); |
747 | return ((PyObject *)me); |
748 | } |
749 | |
750 | static PyObject *kdmget_mp(PyObject *me, void *hunoz) |
751 | { return (mp_pywrap(MP_COPY(KEYDATA_KD(me)->u.m))); } |
752 | |
753 | static PyGetSetDef keydatamp_pygetset[] = { |
754 | #define GETSETNAME(op, name) kdm##op##_##name |
755 | GET (mp, "KD.mp -> X") |
756 | #undef GETSETNAME |
757 | { 0 } |
758 | }; |
759 | |
760 | static PyTypeObject keydatamp_pytype_skel = { |
761 | PyObject_HEAD_INIT(0) 0, /* Header */ |
762 | "catacomb.KeyDataMP", /* @tp_name@ */ |
763 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
764 | 0, /* @tp_itemsize@ */ |
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 | |
808 | static 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); |
821 | end: |
822 | return ((PyObject *)me); |
823 | } |
824 | |
825 | static PyObject *kdsget_str(PyObject *me, void *hunoz) |
826 | { return (PyString_FromString(KEYDATA_KD(me)->u.p)); } |
827 | |
828 | static PyGetSetDef keydatastr_pygetset[] = { |
829 | #define GETSETNAME(op, name) kds##op##_##name |
830 | GET (str, "KD.str -> STRING") |
831 | #undef GETSETNAME |
832 | { 0 } |
833 | }; |
834 | |
835 | static PyTypeObject keydatastr_pytype_skel = { |
836 | PyObject_HEAD_INIT(0) 0, /* Header */ |
837 | "catacomb.KeyDataString", /* @tp_name@ */ |
838 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
839 | 0, /* @tp_itemsize@ */ |
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 | |
883 | static 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); |
896 | end: |
897 | EC_DESTROY(&x); |
898 | return ((PyObject *)me); |
899 | } |
900 | |
901 | static 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 | |
908 | static PyGetSetDef keydataec_pygetset[] = { |
909 | #define GETSETNAME(op, name) kde##op##_##name |
910 | GET (ecpt, "KD.ecpt -> ECPT") |
911 | #undef GETSETNAME |
912 | { 0 } |
913 | }; |
914 | |
915 | static PyTypeObject keydataec_pytype_skel = { |
916 | PyObject_HEAD_INIT(0) 0, /* Header */ |
917 | "catacomb.KeyDataECPt", /* @tp_name@ */ |
918 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
919 | 0, /* @tp_itemsize@ */ |
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 | |
963 | static 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 | |
972 | static PyObject *subkeyiter_next(PyObject *me) |
973 | { |
974 | const char *tag; |
975 | if (!key_nextsubkey(SUBKEYITER_I(me), &tag, 0)) |
976 | return (0); |
977 | return (PyString_FromString(tag)); |
978 | } |
979 | |
980 | static void subkeyiter_pydealloc(PyObject *me) |
981 | { |
982 | Py_DECREF(SUBKEYITER_KDOBJ(me)); |
983 | FREEOBJ(me); |
984 | } |
985 | |
986 | static PyTypeObject subkeyiter_pytype_skel = { |
987 | PyObject_HEAD_INIT(0) 0, /* Header */ |
988 | "catacomb.SubKeyIter", /* @tp_name@ */ |
989 | sizeof(subkeyiter_pyobj), /* @tp_basicsize@ */ |
990 | 0, /* @tp_itemsize@ */ |
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@ */ |
963a6148 |
1018 | subkeyiter_next, /* @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 | |
1034 | static 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 | |
1044 | Py_INCREF(arg); Py_INCREF(kw); |
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; |
1069 | end: |
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 | |
1076 | static 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); |
1088 | end: |
1089 | return (rc); |
1090 | } |
1091 | |
1092 | static 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; |
1100 | if (value) { |
1101 | if (!KEYDATA_PYCHECK(value)) |
1102 | TYERR("expected KeyData value"); |
1103 | key_structset(KEYDATA_KD(me), tag, KEYDATA_KD(value)); |
1104 | } else { |
1105 | if (!key_structfind(KEYDATA_KD(me), tag)) |
1106 | INDEXERR(key); |
1107 | key_structset(KEYDATA_KD(me), tag, 0); |
1108 | } |
1109 | rc = 0; |
1110 | end: |
1111 | return (rc); |
1112 | } |
1113 | |
1114 | static PyMappingMethods keydatastruct_pymapping = { |
1115 | 0, /* @mp_length@ */ |
1116 | keydatastruct_pylookup, /* @mp_subscript@ */ |
1117 | keydatastruct_pystore, /* @mp_ass_subscript@ */ |
1118 | }; |
1119 | |
1120 | static PyTypeObject keydatastruct_pytype_skel = { |
1121 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1122 | "catacomb.KeyDataStructured", /* @tp_name@ */ |
1123 | sizeof(keydata_pyobj), /* @tp_basicsize@ */ |
1124 | 0, /* @tp_itemsize@ */ |
1125 | |
1126 | 0, /* @tp_dealloc@ */ |
1127 | 0, /* @tp_print@ */ |
1128 | 0, /* @tp_getattr@ */ |
1129 | 0, /* @tp_setattr@ */ |
1130 | 0, /* @tp_compare@ */ |
1131 | 0, /* @tp_repr@ */ |
1132 | 0, /* @tp_as_number@ */ |
1133 | 0, /* @tp_as_sequence@ */ |
1134 | &keydatastruct_pymapping, /* @tp_as_mapping@ */ |
1135 | 0, /* @tp_hash@ */ |
1136 | 0, /* @tp_call@ */ |
1137 | 0, /* @tp_str@ */ |
1138 | 0, /* @tp_getattro@ */ |
1139 | 0, /* @tp_setattro@ */ |
1140 | 0, /* @tp_as_buffer@ */ |
1141 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1142 | Py_TPFLAGS_BASETYPE, |
1143 | |
1144 | /* @tp_doc@ */ |
1145 | "Key data for structured keys.", |
1146 | |
1147 | 0, /* @tp_traverse@ */ |
1148 | 0, /* @tp_clear@ */ |
1149 | 0, /* @tp_richcompare@ */ |
1150 | 0, /* @tp_weaklistoffset@ */ |
1151 | subkeyiter_make, /* @tp_iter@ */ |
963a6148 |
1152 | 0, /* @tp_iternext@ */ |
46e6ad89 |
1153 | 0, /* @tp_methods@ */ |
1154 | 0, /* @tp_members@ */ |
1155 | 0, /* @tp_getset@ */ |
1156 | 0, /* @tp_base@ */ |
1157 | 0, /* @tp_dict@ */ |
1158 | 0, /* @tp_descr_get@ */ |
1159 | 0, /* @tp_descr_set@ */ |
1160 | 0, /* @tp_dictoffset@ */ |
1161 | 0, /* @tp_init@ */ |
1162 | PyType_GenericAlloc, /* @tp_alloc@ */ |
1163 | keydatastruct_pynew, /* @tp_new@ */ |
1164 | 0, /* @tp_free@ */ |
1165 | 0 /* @tp_is_gc@ */ |
1166 | }; |
1167 | |
1168 | /*----- Key attributes ----------------------------------------------------*/ |
1169 | |
1170 | static PyObject *keyattriter_make(PyObject *kaobj) |
1171 | { |
1172 | PyObject *kobj = KEYATTRS_KOBJ(kaobj); |
1173 | keyattriter_pyobj *me = PyObject_NEW(keyattriter_pyobj, |
1174 | keyattriter_pytype); |
1175 | me->kobj = kobj; |
1176 | Py_INCREF(kobj); |
1177 | key_mkattriter(&me->i, KEY_K(kobj)); |
1178 | return ((PyObject *)me); |
1179 | } |
1180 | |
1181 | static PyObject *keyattriter_next(PyObject *me) |
1182 | { |
1183 | const char *name; |
1184 | |
1185 | if (!key_nextattr(KEYATTRITER_I(me), &name, 0)) |
1186 | return (0); |
1187 | return (PyString_FromString(name)); |
1188 | } |
1189 | |
1190 | static void keyattriter_pydealloc(PyObject *me) |
1191 | { |
1192 | Py_DECREF(KEYATTRITER_KOBJ(me)); |
1193 | FREEOBJ(me); |
1194 | } |
1195 | |
1196 | static PyTypeObject keyattriter_pytype_skel = { |
1197 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1198 | "catacomb.KeyAttributeIter", /* @tp_name@ */ |
1199 | sizeof(keyattriter_pyobj), /* @tp_basicsize@ */ |
1200 | 0, /* @tp_itemsize@ */ |
1201 | |
1202 | keyattriter_pydealloc, /* @tp_dealloc@ */ |
1203 | 0, /* @tp_print@ */ |
1204 | 0, /* @tp_getattr@ */ |
1205 | 0, /* @tp_setattr@ */ |
1206 | 0, /* @tp_compare@ */ |
1207 | 0, /* @tp_repr@ */ |
1208 | 0, /* @tp_as_number@ */ |
1209 | 0, /* @tp_as_sequence@ */ |
1210 | 0, /* @tp_as_mapping@ */ |
1211 | 0, /* @tp_hash@ */ |
1212 | 0, /* @tp_call@ */ |
1213 | 0, /* @tp_str@ */ |
1214 | 0, /* @tp_getattro@ */ |
1215 | 0, /* @tp_setattro@ */ |
1216 | 0, /* @tp_as_buffer@ */ |
1217 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1218 | Py_TPFLAGS_BASETYPE, |
1219 | |
1220 | /* @tp_doc@ */ |
1221 | "Iterator for key attributes.", |
1222 | |
1223 | 0, /* @tp_traverse@ */ |
1224 | 0, /* @tp_clear@ */ |
1225 | 0, /* @tp_richcompare@ */ |
1226 | 0, /* @tp_weaklistoffset@ */ |
1227 | PyObject_SelfIter, /* @tp_iter@ */ |
963a6148 |
1228 | keyattriter_next, /* @tp_iternext@ */ |
46e6ad89 |
1229 | 0, /* @tp_methods@ */ |
1230 | 0, /* @tp_members@ */ |
1231 | 0, /* @tp_getset@ */ |
1232 | 0, /* @tp_base@ */ |
1233 | 0, /* @tp_dict@ */ |
1234 | 0, /* @tp_descr_get@ */ |
1235 | 0, /* @tp_descr_set@ */ |
1236 | 0, /* @tp_dictoffset@ */ |
1237 | 0, /* @tp_init@ */ |
1238 | PyType_GenericAlloc, /* @tp_alloc@ */ |
1239 | abstract_pynew, /* @tp_new@ */ |
1240 | 0, /* @tp_free@ */ |
1241 | 0 /* @tp_is_gc@ */ |
1242 | }; |
1243 | |
1244 | static PyObject *keyattrs_pylookup(PyObject *me, PyObject *key) |
1245 | { |
1246 | const char *attr; |
1247 | const char *value; |
1248 | PyObject *rc = 0; |
1249 | |
1250 | if ((attr = PyString_AsString(key)) == 0) |
1251 | goto end; |
1252 | if ((value = key_getattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr)) == 0) |
1253 | INDEXERR(key); |
1254 | rc = PyString_FromString(value); |
1255 | end: |
1256 | return (rc); |
1257 | } |
1258 | |
1259 | static int keyattrs_pystore(PyObject *me, |
1260 | PyObject *key, PyObject *value) |
1261 | { |
1262 | const char *attr; |
1263 | const char *val; |
1264 | int err; |
1265 | int rc = -1; |
1266 | |
1267 | if ((attr = PyString_AsString(key)) == 0) |
1268 | goto end; |
1269 | if (value) { |
1270 | if ((val = PyString_AsString(value)) == 0) |
1271 | goto end; |
1272 | if ((err = key_putattr(KEYATTRS_KF(me), KEYATTRS_K(me), |
1273 | attr, val)) != 0) |
1274 | KEYERR(err); |
1275 | } else { |
1276 | if (!key_getattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr)) |
1277 | INDEXERR(key); |
1278 | if ((err = key_putattr(KEYATTRS_KF(me), KEYATTRS_K(me), attr, 0)) != 0) |
1279 | KEYERR(err); |
1280 | } |
1281 | rc = 0; |
1282 | end: |
1283 | return (rc); |
1284 | } |
1285 | |
1286 | static PyObject *keyattrs_make(PyObject *kobj) |
1287 | { |
1288 | keyattrs_pyobj *me = PyObject_NEW(keyattrs_pyobj, keyattrs_pytype); |
1289 | me->kobj = kobj; |
1290 | Py_INCREF(kobj); |
1291 | return ((PyObject *)me); |
1292 | } |
1293 | |
1294 | static void keyattrs_pydealloc(PyObject *me) |
1295 | { |
1296 | Py_DECREF(KEYATTRS_KOBJ(me)); |
1297 | FREEOBJ(me); |
1298 | } |
1299 | |
1300 | static PyMappingMethods keyattrs_pymapping = { |
1301 | 0, |
1302 | keyattrs_pylookup, |
1303 | keyattrs_pystore |
1304 | }; |
1305 | |
1306 | static PyTypeObject keyattrs_pytype_skel = { |
1307 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1308 | "catacomb.KeyAttributes", /* @tp_name@ */ |
1309 | sizeof(keyattrs_pyobj), /* @tp_basicsize@ */ |
1310 | 0, /* @tp_itemsize@ */ |
1311 | |
1312 | keyattrs_pydealloc, /* @tp_dealloc@ */ |
1313 | 0, /* @tp_print@ */ |
1314 | 0, /* @tp_getattr@ */ |
1315 | 0, /* @tp_setattr@ */ |
1316 | 0, /* @tp_compare@ */ |
1317 | 0, /* @tp_repr@ */ |
1318 | 0, /* @tp_as_number@ */ |
1319 | 0, /* @tp_as_sequence@ */ |
1320 | &keyattrs_pymapping, /* @tp_as_mapping@ */ |
1321 | 0, /* @tp_hash@ */ |
1322 | 0, /* @tp_call@ */ |
1323 | 0, /* @tp_str@ */ |
1324 | 0, /* @tp_getattro@ */ |
1325 | 0, /* @tp_setattro@ */ |
1326 | 0, /* @tp_as_buffer@ */ |
1327 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1328 | Py_TPFLAGS_BASETYPE, |
1329 | |
1330 | /* @tp_doc@ */ |
1331 | "Proxy thing for talking about key attributes.", |
1332 | |
1333 | 0, /* @tp_traverse@ */ |
1334 | 0, /* @tp_clear@ */ |
1335 | 0, /* @tp_richcompare@ */ |
1336 | 0, /* @tp_weaklistoffset@ */ |
1337 | keyattriter_make, /* @tp_iter@ */ |
963a6148 |
1338 | 0, /* @tp_iternext@ */ |
46e6ad89 |
1339 | 0, /* @tp_methods@ */ |
1340 | 0, /* @tp_members@ */ |
1341 | 0, /* @tp_getset@ */ |
1342 | 0, /* @tp_base@ */ |
1343 | 0, /* @tp_dict@ */ |
1344 | 0, /* @tp_descr_get@ */ |
1345 | 0, /* @tp_descr_set@ */ |
1346 | 0, /* @tp_dictoffset@ */ |
1347 | 0, /* @tp_init@ */ |
1348 | PyType_GenericAlloc, /* @tp_alloc@ */ |
1349 | abstract_pynew, /* @tp_new@ */ |
1350 | 0, /* @tp_free@ */ |
1351 | 0 /* @tp_is_gc@ */ |
1352 | }; |
1353 | |
1354 | /*----- Key objects -------------------------------------------------------*/ |
1355 | |
1356 | static PyObject *key_dowrap(PyTypeObject *ty, PyObject *kfobj, key *k) |
1357 | { |
1358 | key_pyobj *kobj = (key_pyobj *)ty->tp_alloc(ty, 0); |
1359 | kobj->kfobj = kfobj; |
1360 | Py_INCREF(kfobj); |
1361 | kobj->k = k; |
1362 | return ((PyObject *)kobj); |
1363 | } |
1364 | |
1365 | static PyObject *key_pywrap(PyObject *kfobj, key *k) |
1366 | { return (key_dowrap(key_pytype, kfobj, k)); } |
1367 | |
1368 | static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
1369 | { |
1370 | PyObject *kfobj; |
1371 | uint32 id; |
1372 | char *type; |
1373 | long exptime = KEXP_FOREVER; |
1374 | static char *kwlist[] = { "keyfile", "id", "type", "exptime", 0 }; |
1375 | key *k; |
1376 | int err; |
1377 | |
1378 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&sl:new", kwlist, |
1379 | keyfile_pytype, &kfobj, convu32, &id, |
1380 | &type, &exptime)) |
1381 | goto end; |
1382 | if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) == 0) |
1383 | KEYERR(err); |
1384 | return (key_dowrap(ty, kfobj, k)); |
1385 | end: |
1386 | return (0); |
1387 | } |
1388 | |
1389 | static void key_pydealloc(PyObject *me) |
1390 | { |
1391 | Py_DECREF(KEY_KFOBJ(me)); |
1392 | FREEOBJ(me); |
1393 | } |
1394 | |
1395 | static PyObject *kmeth_delete(PyObject *me, PyObject *arg) |
1396 | { |
1397 | int err; |
1398 | |
1399 | if (!PyArg_ParseTuple(arg, ":delete")) goto end; |
1400 | if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err); |
1401 | RETURN_ME; |
1402 | end: |
1403 | return (0); |
1404 | } |
1405 | |
1406 | static PyObject *kmeth_expire(PyObject *me, PyObject *arg) |
1407 | { |
1408 | int err; |
1409 | |
1410 | if (!PyArg_ParseTuple(arg, ":expire")) goto end; |
1411 | if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err); |
1412 | RETURN_ME; |
1413 | end: |
1414 | return (0); |
1415 | } |
1416 | |
1417 | static PyObject *kmeth_used(PyObject *me, PyObject *arg) |
1418 | { |
1419 | long t; |
1420 | int err; |
1421 | |
1422 | if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end; |
1423 | if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err); |
1424 | RETURN_ME; |
1425 | end: |
1426 | return (0); |
1427 | } |
1428 | |
1429 | static PyObject *kmeth_extract(PyObject *me, PyObject *arg, PyObject *kw) |
1430 | { |
1431 | key_filter f = { 0, 0 }; |
1432 | PyObject *file; |
1433 | PyObject *nameobj; |
1434 | char *name; |
1435 | FILE *fp; |
1436 | static char *kwlist[] = { "file", "filter", 0 }; |
1437 | |
1438 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", kwlist, |
1439 | &PyFile_Type, &file, |
1440 | convfilter, &f) || |
1441 | (fp = PyFile_AsFile(file)) == 0 || |
1442 | (nameobj = PyFile_Name(file)) == 0 || |
1443 | (name = PyString_AsString(nameobj)) == 0) |
1444 | goto end; |
1445 | if (key_extract(KEY_KF(me), KEY_K(me), fp, &f)) |
1446 | OSERR(name); |
1447 | RETURN_ME; |
1448 | end: |
1449 | return (0); |
1450 | } |
1451 | |
1452 | static PyObject *kmeth_fingerprint(PyObject *me, |
1453 | PyObject *arg, PyObject *kw) |
1454 | { |
1455 | ghash *h; |
1456 | key_filter f = { KF_NONSECRET, KF_NONSECRET }; |
1457 | static char *kwlist[] = { "hash", "filter", 0 }; |
1458 | |
1459 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", kwlist, |
1460 | convghash, &h, convfilter, &f)) |
1461 | return (0); |
1462 | return (getbool(key_fingerprint(KEY_K(me), h, &f))); |
1463 | } |
1464 | |
1465 | static PyObject *kget_id(PyObject *me, void *hunoz) |
1466 | { return (getu32(KEY_K(me)->id)); } |
1467 | static PyObject *kget_type(PyObject *me, void *hunoz) |
1468 | { return (PyString_FromString(KEY_K(me)->type)); } |
1469 | static PyObject *kget_exptime(PyObject *me, void *hunoz) |
1470 | { return (PyInt_FromLong(KEY_K(me)->exp)); } |
1471 | static PyObject *kget_deltime(PyObject *me, void *hunoz) |
1472 | { return (PyInt_FromLong(KEY_K(me)->del)); } |
1473 | static PyObject *kget_expiredp(PyObject *me, void *hunoz) |
1474 | { return (getbool(key_expired(KEY_K(me)))); } |
1475 | static PyObject *kget_attr(PyObject *me, void *hunoz) |
1476 | { return (keyattrs_make(me)); } |
1477 | |
1478 | static PyObject *kget_data(PyObject *me, void *hunoz) |
1479 | { |
1480 | key_data *kd = KEY_K(me)->k; |
1481 | key_incref(kd); |
1482 | return (keydata_pywrap(kd)); |
1483 | } |
1484 | static int kset_data(PyObject *me, PyObject *x, void *hunoz) |
1485 | { |
1486 | int err; |
1487 | if (!KEYDATA_PYCHECK(x)) TYERR("expected KeyData object"); |
1488 | if ((err = key_setkeydata(KEY_KF(me), KEY_K(me), KEYDATA_KD(x))) != 0) |
1489 | KEYERR(err); |
1490 | return (0); |
1491 | end: |
1492 | return (-1); |
1493 | } |
1494 | |
1495 | static PyObject *kget_fulltag(PyObject *me, void *hunoz) |
1496 | { |
1497 | dstr d = DSTR_INIT; |
1498 | PyObject *rc; |
1499 | |
1500 | key_fulltag(KEY_K(me), &d); |
1501 | rc = PyString_FromStringAndSize(d.buf, d.len); |
1502 | dstr_destroy(&d); |
1503 | return (rc); |
1504 | } |
1505 | |
1506 | static PyObject *kget_tag(PyObject *me, void *hunoz) |
1507 | { |
1508 | if (!KEY_K(me)->tag) RETURN_NONE; |
1509 | return (PyString_FromString(KEY_K(me)->tag)); |
1510 | } |
1511 | static int kset_tag(PyObject *me, PyObject *x, void *hunoz) |
1512 | { |
1513 | int err; |
1514 | char *tag; |
1515 | |
1516 | if (x == Py_None) tag = 0; |
1517 | else if ((tag = PyString_AsString(x)) == 0) goto end; |
1518 | if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err); |
1519 | return (0); |
1520 | end: |
1521 | return (-1); |
1522 | } |
1523 | |
1524 | static PyObject *kget_comment(PyObject *me, void *hunoz) |
1525 | { |
1526 | if (!KEY_K(me)->c) RETURN_NONE; |
1527 | return (PyString_FromString(KEY_K(me)->c)); |
1528 | } |
1529 | static int kset_comment(PyObject *me, PyObject *x, void *hunoz) |
1530 | { |
1531 | int err; |
1532 | char *c; |
1533 | |
1534 | if (x == Py_None) c = 0; |
1535 | else if ((c = PyString_AsString(x)) == 0) goto end; |
1536 | if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err); |
1537 | return (0); |
1538 | end: |
1539 | return (-1); |
1540 | } |
1541 | |
1542 | static PyMethodDef key_pymethods[] = { |
1543 | #define METHNAME(func) kmeth_##func |
1544 | METH (delete, "KEY.delete()") |
1545 | METH (expire, "KEY.expire()") |
1546 | METH (used, "KEY.used(TIME)") |
1547 | KWMETH(extract, "KEY.extract(FILE, filter = '')") |
1548 | KWMETH(fingerprint, "KEY.fingerprint(HASH, filtr = '-secret')") |
1549 | #undef METHNAME |
1550 | { 0 } |
1551 | }; |
1552 | |
1553 | static PyGetSetDef key_pygetset[] = { |
1554 | #define GETSETNAME(op, name) k##op##_##name |
1555 | GET (id, "KEY.id -> ID") |
1556 | GETSET(tag, "KEY.tag -> TAG") |
1557 | GET (type, "KEY.type -> TYPE") |
1558 | GET (exptime, "KEY.exptime -> TIME") |
1559 | GET (deltime, "KEY.deltime -> TIME") |
1560 | GET (expiredp, "KEY.expiredp -> BOOL") |
1561 | GET (attr, "KEY.attr -> ATTRIBUTES") |
1562 | GETSET(data, "KEY.data -> KD") |
1563 | GETSET(comment, "KEY.comment -> COMMENT") |
1564 | GET (fulltag, "KEY.fulltag -> FULLTAG") |
1565 | #undef GETSETNAME |
1566 | { 0 } |
1567 | }; |
1568 | |
1569 | static PyTypeObject key_pytype_skel = { |
1570 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1571 | "catacomb.Key", /* @tp_name@ */ |
1572 | sizeof(key_pyobj), /* @tp_basicsize@ */ |
1573 | 0, /* @tp_itemsize@ */ |
1574 | |
1575 | key_pydealloc, /* @tp_dealloc@ */ |
1576 | 0, /* @tp_print@ */ |
1577 | 0, /* @tp_getattr@ */ |
1578 | 0, /* @tp_setattr@ */ |
1579 | 0, /* @tp_compare@ */ |
1580 | 0, /* @tp_repr@ */ |
1581 | 0, /* @tp_as_number@ */ |
1582 | 0, /* @tp_as_sequence@ */ |
1583 | 0, /* @tp_as_mapping@ */ |
1584 | 0, /* @tp_hash@ */ |
1585 | 0, /* @tp_call@ */ |
1586 | 0, /* @tp_str@ */ |
1587 | 0, /* @tp_getattro@ */ |
1588 | 0, /* @tp_setattro@ */ |
1589 | 0, /* @tp_as_buffer@ */ |
1590 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1591 | Py_TPFLAGS_BASETYPE, |
1592 | |
1593 | /* @tp_doc@ */ |
1594 | "Key object.", |
1595 | |
1596 | 0, /* @tp_traverse@ */ |
1597 | 0, /* @tp_clear@ */ |
1598 | 0, /* @tp_richcompare@ */ |
1599 | 0, /* @tp_weaklistoffset@ */ |
1600 | 0, /* @tp_iter@ */ |
963a6148 |
1601 | 0, /* @tp_iternext@ */ |
46e6ad89 |
1602 | key_pymethods, /* @tp_methods@ */ |
1603 | 0, /* @tp_members@ */ |
1604 | key_pygetset, /* @tp_getset@ */ |
1605 | 0, /* @tp_base@ */ |
1606 | 0, /* @tp_dict@ */ |
1607 | 0, /* @tp_descr_get@ */ |
1608 | 0, /* @tp_descr_set@ */ |
1609 | 0, /* @tp_dictoffset@ */ |
1610 | 0, /* @tp_init@ */ |
1611 | PyType_GenericAlloc, /* @tp_alloc@ */ |
1612 | key_pynew, /* @tp_new@ */ |
1613 | 0, /* @tp_free@ */ |
1614 | 0 /* @tp_is_gc@ */ |
1615 | }; |
1616 | |
1617 | /*----- Key iteration -----------------------------------------------------*/ |
1618 | |
1619 | static PyObject *keyiter_new(PyObject *kfobj) |
1620 | { |
1621 | keyiter_pyobj *me = PyObject_NEW(keyiter_pyobj, keyiter_pytype); |
1622 | key_mkiter(&me->i, KEYFILE_KF(kfobj)); |
1623 | me->kfobj = kfobj; |
1624 | Py_INCREF(kfobj); |
1625 | return ((PyObject *)me); |
1626 | } |
1627 | |
1628 | static PyObject *keyiter_next(PyObject *me) |
1629 | { |
1630 | key *k; |
1631 | |
1632 | if ((k = key_next(KEYITER_I(me))) == 0) |
1633 | return (0); |
1634 | return (key_pywrap(KEYITER_KFOBJ(me), k)); |
1635 | } |
1636 | |
1637 | static void keyiter_pydealloc(PyObject *me) |
1638 | { |
1639 | Py_DECREF(KEYITER_KFOBJ(me)); |
1640 | FREEOBJ(me); |
1641 | } |
1642 | |
1643 | static PyTypeObject keyiter_pytype_skel = { |
1644 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1645 | "catacomb.KeyFileIter", /* @tp_name@ */ |
1646 | sizeof(keyiter_pyobj), /* @tp_basicsize@ */ |
1647 | 0, /* @tp_itemsize@ */ |
1648 | |
1649 | keyiter_pydealloc, /* @tp_dealloc@ */ |
1650 | 0, /* @tp_print@ */ |
1651 | 0, /* @tp_getattr@ */ |
1652 | 0, /* @tp_setattr@ */ |
1653 | 0, /* @tp_compare@ */ |
1654 | 0, /* @tp_repr@ */ |
1655 | 0, /* @tp_as_number@ */ |
1656 | 0, /* @tp_as_sequence@ */ |
1657 | 0, /* @tp_as_mapping@ */ |
1658 | 0, /* @tp_hash@ */ |
1659 | 0, /* @tp_call@ */ |
1660 | 0, /* @tp_str@ */ |
1661 | 0, /* @tp_getattro@ */ |
1662 | 0, /* @tp_setattro@ */ |
1663 | 0, /* @tp_as_buffer@ */ |
1664 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1665 | Py_TPFLAGS_BASETYPE, |
1666 | |
1667 | /* @tp_doc@ */ |
1668 | "Keyring iterator.", |
1669 | |
1670 | 0, /* @tp_traverse@ */ |
1671 | 0, /* @tp_clear@ */ |
1672 | 0, /* @tp_richcompare@ */ |
1673 | 0, /* @tp_weaklistoffset@ */ |
1674 | PyObject_SelfIter, /* @tp_iter@ */ |
963a6148 |
1675 | keyiter_next, /* @tp_iternext@ */ |
46e6ad89 |
1676 | 0, /* @tp_methods@ */ |
1677 | 0, /* @tp_members@ */ |
1678 | 0, /* @tp_getset@ */ |
1679 | 0, /* @tp_base@ */ |
1680 | 0, /* @tp_dict@ */ |
1681 | 0, /* @tp_descr_get@ */ |
1682 | 0, /* @tp_descr_set@ */ |
1683 | 0, /* @tp_dictoffset@ */ |
1684 | 0, /* @tp_init@ */ |
1685 | PyType_GenericAlloc, /* @tp_alloc@ */ |
1686 | abstract_pynew, /* @tp_new@ */ |
1687 | 0, /* @tp_free@ */ |
1688 | 0 /* @tp_is_gc@ */ |
1689 | }; |
1690 | |
d7ab1bab |
1691 | /*----- Key files ---------------------------------------------------------*/ |
1692 | |
46e6ad89 |
1693 | struct reportinfo { |
1694 | PyObject *func; |
1695 | int stop; |
1696 | }; |
1697 | |
1698 | static void pythonreporter(const char *file, int line, |
1699 | const char *msg, void *p) |
1700 | { |
1701 | struct reportinfo *ri = p; |
1702 | PyObject *res = 0; |
1703 | |
1704 | if (ri->stop) |
1705 | return; |
1706 | if (!ri->func) |
1707 | key_moan(file, line, msg, 0); |
1708 | else if ((res = PyObject_CallFunction(ri->func, "sis", |
1709 | file, line, msg)) == 0) |
1710 | ri->stop = 1; |
1711 | else |
1712 | Py_DECREF(res); |
1713 | } |
1714 | |
d7ab1bab |
1715 | static PyObject *keyfile_pynew(PyTypeObject *ty, |
1716 | PyObject *arg, PyObject *kw) |
1717 | { |
46e6ad89 |
1718 | struct reportinfo ri = { 0, 0 }; |
1719 | char *file = 0; |
1720 | unsigned how = KOPEN_READ; |
1721 | keyfile_pyobj *rc = 0; |
1722 | static char *kwlist[] = { "file", "how", "report", 0 }; |
1723 | |
1724 | Py_XINCREF(arg); Py_XINCREF(kw); |
1725 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", kwlist, |
1726 | &file, &how, &ri.func)) |
1727 | goto end; |
1728 | if (ri.func && !PyCallable_Check(ri.func)) |
1729 | TYERR("reporter function not callable"); |
1730 | if ((rc = (keyfile_pyobj *)ty->tp_alloc(ty, 0)) == 0) |
1731 | goto end; |
1732 | if (key_open(&rc->kf, file, how, pythonreporter, &ri)) |
1733 | OSERR(file); |
1734 | if (ri.stop) { |
1735 | key_discard(&rc->kf); |
1736 | goto end; |
1737 | } |
1738 | goto done; |
1739 | |
1740 | end: |
1741 | if (rc) { |
1742 | FREEOBJ(rc); |
1743 | rc = 0; |
1744 | } |
1745 | done: |
1746 | Py_XDECREF(arg); Py_XDECREF(kw); |
1747 | return ((PyObject *)rc); |
1748 | } |
1749 | |
1750 | static void keyfile_pydealloc(PyObject *me) |
1751 | { |
1752 | key_discard(KEYFILE_KF(me)); |
1753 | FREEOBJ(me); |
1754 | } |
1755 | |
1756 | static PyObject *kfmeth_save(PyObject *me, PyObject *arg) |
1757 | { |
1758 | if (!PyArg_ParseTuple(arg, ":save")) |
1759 | goto end; |
1760 | switch (key_save(KEYFILE_KF(me))) { |
1761 | case KWRITE_OK: |
1762 | RETURN_ME; |
1763 | case KWRITE_FAIL: |
1764 | KEYIOERR(KEYFILE_KF(me)->name); |
1765 | case KWRITE_BROKEN: |
1766 | KEYFILEBROKEN(KEYFILE_KF(me)->name); |
1767 | default: |
1768 | abort(); |
1769 | } |
1770 | end: |
1771 | return (0); |
1772 | } |
1773 | |
1774 | static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw) |
1775 | { |
1776 | struct reportinfo ri = { 0, 0 }; |
1777 | char *name; |
1778 | PyObject *x = 0; |
1779 | FILE *fp = 0; |
1780 | int rc; |
1781 | static char *kwlist[] = { "file", "report", 0 }; |
1782 | |
1783 | Py_XINCREF(arg); Py_XINCREF(kw); |
1784 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", kwlist, |
1785 | &PyFile_Type, &x, &ri.func)) |
1786 | goto end; |
1787 | if (ri.func && !PyCallable_Check(ri.func)) |
1788 | TYERR("reporter function not callable"); |
1789 | if ((fp = PyFile_AsFile(x)) == 0) |
1790 | goto end; |
1791 | x = PyFile_Name(x); |
1792 | if ((name = PyString_AsString(x)) == 0) |
1793 | goto end; |
1794 | rc = key_merge(KEYFILE_KF(me), name, fp, pythonreporter, &ri); |
1795 | if (ri.stop) |
1796 | goto end; |
1797 | if (rc != 0) |
1798 | KEYERR(rc); |
1799 | Py_XDECREF(arg); Py_XDECREF(kw); |
1800 | RETURN_ME; |
1801 | |
1802 | end: |
1803 | Py_XDECREF(arg); Py_XDECREF(kw); |
1804 | return (0); |
1805 | } |
1806 | |
1807 | static PyObject *kfmeth_byid(PyObject *me, PyObject *arg) |
1808 | { |
1809 | uint32 id; |
1810 | key *k; |
1811 | PyObject *rc = 0; |
1812 | |
1813 | if (!PyArg_ParseTuple(arg, "O&:byid", convu32, &id)) goto end; |
1814 | if ((k = key_byid(KEYFILE_KF(me), id)) == 0) KEYERR(KERR_NOTFOUND); |
1815 | rc = key_pywrap(me, k); |
1816 | end: |
1817 | return (rc); |
1818 | } |
1819 | |
1820 | static PyObject *kfmeth_bytype(PyObject *me, PyObject *arg) |
1821 | { |
1822 | char *type; |
1823 | key *k; |
1824 | PyObject *rc = 0; |
1825 | |
1826 | if (!PyArg_ParseTuple(arg, "s:bytype", &type)) goto end; |
1827 | if ((k = key_bytype(KEYFILE_KF(me), type)) == 0) RETURN_NONE; |
1828 | rc = key_pywrap(me, k); |
1829 | end: |
1830 | return (rc); |
1831 | } |
1832 | |
1833 | static PyObject *bytag(PyObject *me, PyObject *tagobj) |
1834 | { |
1835 | uint32 id; |
1836 | char *tag; |
1837 | key *k; |
1838 | PyObject *rc = 0; |
1839 | |
1840 | if (convu32(tagobj, &id)) |
1841 | k = key_byid(KEYFILE_KF(me), id); |
1842 | else { |
1843 | PyErr_Clear(); |
1844 | if ((tag = PyString_AsString(tagobj)) == 0) |
1845 | goto end; |
1846 | k = key_bytag(KEYFILE_KF(me), tag); |
1847 | } |
1848 | if (!k) RETURN_NONE; |
1849 | rc = key_pywrap(me, k); |
1850 | end: |
1851 | return (rc); |
1852 | } |
1853 | |
1854 | static PyObject *kfmeth_bytag(PyObject *me, PyObject *arg) |
1855 | { |
1856 | PyObject *tagobj; |
1857 | |
1858 | if (!PyArg_ParseTuple(arg, "O:bytag", &tagobj)) return (0); |
1859 | return (bytag(me, tagobj)); |
1860 | } |
1861 | |
1862 | static PyObject *keyfile_pylookup(PyObject *me, PyObject *key) |
1863 | { |
1864 | PyObject *rc = bytag(me, key); |
1865 | if (!rc) goto end; |
1866 | if (rc == Py_None) { |
1867 | Py_DECREF(rc); |
1868 | rc = 0; |
1869 | INDEXERR(key); |
1870 | } |
1871 | end: |
1872 | return (rc); |
1873 | } |
1874 | |
1875 | static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw) |
1876 | { |
1877 | uint32 id; |
1878 | char *type; |
1879 | long exptime = KEXP_FOREVER; |
1880 | static char *kwlist[] = { "id", "type", "exptime", 0 }; |
1881 | key *k; |
1882 | int err; |
1883 | |
1884 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&sl:new", kwlist, |
1885 | convu32, &id, &type, &exptime)) |
1886 | goto end; |
1887 | if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) == 0) |
1888 | KEYERR(err); |
1889 | return (key_pywrap(me, k)); |
1890 | end: |
1891 | return (0); |
1892 | } |
1893 | |
1894 | static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw) |
1895 | { |
1896 | key *k; |
ae5cafdb |
1897 | key_data **kd, *okd; |
46e6ad89 |
1898 | PyObject *newkdobj = 0; |
1899 | char *tag; |
46e6ad89 |
1900 | dstr d = DSTR_INIT; |
1901 | PyObject *rc = 0; |
1902 | static char *kwlist[] = { "tag", "new", 0 }; |
1903 | |
1904 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", kwlist, |
1905 | &tag, keydata_pytype, &newkdobj)) |
1906 | goto end; |
1907 | if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd)) |
1908 | KEYERR(KERR_NOTFOUND); |
ae5cafdb |
1909 | okd = *kd; |
46e6ad89 |
1910 | if (newkdobj) { |
1911 | if (!(KEYFILE_KF(me)->f & KF_WRITE)) |
1912 | KEYERR(KERR_READONLY); |
ae5cafdb |
1913 | KEYFILE_KF(me)->f |= KF_MODIFIED; |
46e6ad89 |
1914 | *kd = KEYDATA_KD(newkdobj); |
46e6ad89 |
1915 | } |
ae5cafdb |
1916 | key_incref(*kd); |
1917 | rc = Py_BuildValue("(s#NN)", |
1918 | d.buf, d.len, |
1919 | key_pywrap(me, k), |
1920 | keydata_pywrap(okd)); |
46e6ad89 |
1921 | end: |
1922 | return (rc); |
1923 | } |
1924 | |
1925 | static PyObject *kfget_name(PyObject *me, void *hunoz) |
1926 | { return (PyString_FromString(KEYFILE_KF(me)->name)); } |
1927 | static PyObject *kfget_modifiedp(PyObject *me, void *hunoz) |
1928 | { return (getbool(KEYFILE_KF(me)->f & KF_MODIFIED)); } |
1929 | static PyObject *kfget_writep(PyObject *me, void *hunoz) |
1930 | { return (getbool(KEYFILE_KF(me)->f & KF_WRITE)); } |
1931 | static PyObject *kfget_filep(PyObject *me, void *hunoz) |
1932 | { return (getbool(!!KEYFILE_KF(me)->fp)); } |
1933 | |
1934 | static PyMethodDef keyfile_pymethods[] = { |
1935 | #define METHNAME(func) kfmeth_##func |
1936 | METH (save, "KF.save()") |
1937 | KWMETH(merge, "KF.merge(FILE, report = <built-in-reporter>)") |
1938 | KWMETH(newkey, "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY") |
1939 | METH (byid, "KF.byid(KEYID) -> KEY|None") |
1940 | METH (bytype, "KF.bytype(TYPE) -> KEY|None") |
1941 | METH (bytag, "KF.bytag(TAG) -> KEY|None") |
1942 | KWMETH(qtag, "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD") |
1943 | #undef METHNAME |
1944 | { 0 } |
1945 | }; |
1946 | |
1947 | static PyGetSetDef keyfile_pygetset[] = { |
1948 | #define GETSETNAME(op, name) kf##op##_##name |
1949 | GET (name, "KF.name -> file name") |
1950 | GET (modifiedp, "KF.modifiedp -> has keyring been modified?") |
1951 | GET (writep, "KF.writep -> is keyring modifiable?") |
1952 | GET (filep, "KF.filep -> does keyring have a backing file?") |
1953 | #undef GETSETNAME |
1954 | { 0 } |
1955 | }; |
1956 | |
1957 | static PyMappingMethods keyfile_pymapping = { |
1958 | 0, |
1959 | keyfile_pylookup, |
1960 | 0 |
1961 | }; |
1962 | |
1963 | static PyTypeObject keyfile_pytype_skel = { |
1964 | PyObject_HEAD_INIT(0) 0, /* Header */ |
1965 | "catacomb.KeyFile", /* @tp_name@ */ |
1966 | sizeof(keyfile_pyobj), /* @tp_basicsize@ */ |
1967 | 0, /* @tp_itemsize@ */ |
1968 | |
1969 | keyfile_pydealloc, /* @tp_dealloc@ */ |
1970 | 0, /* @tp_print@ */ |
1971 | 0, /* @tp_getattr@ */ |
1972 | 0, /* @tp_setattr@ */ |
1973 | 0, /* @tp_compare@ */ |
1974 | 0, /* @tp_repr@ */ |
1975 | 0, /* @tp_as_number@ */ |
1976 | 0, /* @tp_as_sequence@ */ |
1977 | &keyfile_pymapping, /* @tp_as_mapping@ */ |
1978 | 0, /* @tp_hash@ */ |
1979 | 0, /* @tp_call@ */ |
1980 | 0, /* @tp_str@ */ |
1981 | 0, /* @tp_getattro@ */ |
1982 | 0, /* @tp_setattro@ */ |
1983 | 0, /* @tp_as_buffer@ */ |
1984 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
1985 | Py_TPFLAGS_BASETYPE, |
1986 | |
1987 | /* @tp_doc@ */ |
1988 | "Keyring file.", |
1989 | |
1990 | 0, /* @tp_traverse@ */ |
1991 | 0, /* @tp_clear@ */ |
1992 | 0, /* @tp_richcompare@ */ |
1993 | 0, /* @tp_weaklistoffset@ */ |
1994 | keyiter_new, /* @tp_iter@ */ |
963a6148 |
1995 | 0, /* @tp_iternext@ */ |
46e6ad89 |
1996 | keyfile_pymethods, /* @tp_methods@ */ |
1997 | 0, /* @tp_members@ */ |
1998 | keyfile_pygetset, /* @tp_getset@ */ |
1999 | 0, /* @tp_base@ */ |
2000 | 0, /* @tp_dict@ */ |
2001 | 0, /* @tp_descr_get@ */ |
2002 | 0, /* @tp_descr_set@ */ |
2003 | 0, /* @tp_dictoffset@ */ |
2004 | 0, /* @tp_init@ */ |
2005 | PyType_GenericAlloc, /* @tp_alloc@ */ |
2006 | keyfile_pynew, /* @tp_new@ */ |
2007 | 0, /* @tp_free@ */ |
2008 | 0 /* @tp_is_gc@ */ |
2009 | }; |
2010 | |
2011 | /*----- Global stuff ------------------------------------------------------*/ |
2012 | |
2013 | static PyObject *meth_barf(PyObject *me, PyObject *arg) |
2014 | { |
2015 | int err; |
2016 | |
2017 | if (PyArg_ParseTuple(arg, "i:barf", &err)) |
2018 | KEYERR(err); |
2019 | end: |
2020 | return (0); |
2021 | } |
2022 | |
2023 | static PyMethodDef methods[] = { |
2024 | #define METHNAME(func) meth_##func |
2025 | METH (_KeyData_readflags, |
2026 | "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)") |
2027 | METH (_KeyData_writeflags, "KeyData.writeflags(FLAGS) -> STRING") |
2028 | METH (_KeyData_read, "KeyData.read(STRING) -> (KD, REST)") |
2029 | METH (_KeyData_decode, "KeyData.read(BYTES) -> KD") |
2030 | METH (barf, "barf(ERR)") |
2031 | #undef METHNAME |
2032 | { 0 } |
2033 | }; |
2034 | |
2035 | /*----- Initialization ----------------------------------------------------*/ |
2036 | |
2037 | void key_pyinit(void) |
2038 | { |
2039 | INITTYPE(keyfile, root); |
2040 | INITTYPE(key, root); |
2041 | INITTYPE(keyiter, root); |
2042 | INITTYPE(keydata, root); |
2043 | INITTYPE(keydatabin, keydata); |
2044 | INITTYPE(keydataenc, keydata); |
2045 | INITTYPE(keydatastr, keydata); |
2046 | INITTYPE(keydatamp, keydata); |
2047 | INITTYPE(keydataec, keydata); |
2048 | INITTYPE(keydatastruct, keydata); |
2049 | INITTYPE(subkeyiter, root); |
2050 | INITTYPE(keyattrs, root); |
2051 | INITTYPE(keyattriter, root); |
2052 | addmethods(methods); |
2053 | } |
2054 | |
2055 | void key_pyinsert(PyObject *mod) |
2056 | { |
2057 | INSEXC("KeyError", keyexc, PyExc_Exception, keyexc_pymethods); |
2058 | INSEXC("KeyFileIOError", keyioexc, PyExc_OSError, 0); |
2059 | INSEXC("KeyFileBroken", keyfilebrokenexc, keyioexc, 0); |
2060 | INSERT("KeyFile", keyfile_pytype); |
2061 | INSERT("KeyFileIter", keyiter_pytype); |
2062 | INSERT("Key", key_pytype); |
2063 | INSERT("KeyAttributes", keyattrs_pytype); |
2064 | INSERT("KeyAttributeIter", keyattriter_pytype); |
2065 | INSERT("KeyData", keydata_pytype); |
2066 | INSERT("KeyDataBinary", keydatabin_pytype); |
2067 | INSERT("KeyDataEncrypted", keydataenc_pytype); |
2068 | INSERT("KeyDataMP", keydatamp_pytype); |
2069 | INSERT("KeyDataECPt", keydataec_pytype); |
2070 | INSERT("KeyDataString", keydatastr_pytype); |
2071 | INSERT("KeyDataStructured", keydatastruct_pytype); |
2072 | INSERT("SubKeyIter", subkeyiter_pytype); |
d7ab1bab |
2073 | } |
2074 | |
2075 | /*----- That's all, folks -------------------------------------------------*/ |