Commit | Line | Data |
---|---|---|
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 | ||
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); | |
fbca05a1 | 290 | rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end); |
46e6ad89 | 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; | |
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 | ||
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; | |
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); | |
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; | |
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); | |
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); | |
b2687a0a | 443 | rc = keydata_pywrap(kd); |
46e6ad89 | 444 | end: |
445 | return (rc); | |
446 | } | |
447 | ||
448 | static PyObject *kdget_flags(PyObject *me, void *hunoz) | |
fbca05a1 | 449 | { return (getulong(KEYDATA_KD(me)->e)); } |
46e6ad89 | 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; | |
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); | |
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; | |
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); | |
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 | ||
11cb3d97 | 972 | static 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 | ||
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@ */ | |
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 | ||
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 | ||
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; | |
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 = { | |
11cb3d97 | 1115 | gmap_pysize, /* @mp_length@ */ |
46e6ad89 | 1116 | keydatastruct_pylookup, /* @mp_subscript@ */ |
11cb3d97 | 1117 | keydatastruct_pystore /* @mp_ass_subscript@ */ |
46e6ad89 | 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@ */ |
11cb3d97 | 1153 | gmap_pymethods, /* @tp_methods@ */ |
46e6ad89 | 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 | ||
11cb3d97 | 1181 | static PyObject *keyattriter_pynext(PyObject *me) |
46e6ad89 | 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@ */ | |
11cb3d97 | 1228 | keyattriter_pynext, /* @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 = { | |
11cb3d97 MW |
1301 | gmap_pysize, /* @mp_length@ */ |
1302 | keyattrs_pylookup, /* @mp_subscript@ */ | |
1303 | keyattrs_pystore /* @mp_ass_subscript@ */ | |
46e6ad89 | 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@ */ | |
11cb3d97 | 1319 | &gmap_pysequence, /* @tp_as_sequence@ */ |
46e6ad89 | 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@ */ |
11cb3d97 | 1339 | gmap_pymethods, /* @tp_methods@ */ |
46e6ad89 | 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; | |
b2687a0a | 1398 | |
46e6ad89 | 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; | |
b2687a0a | 1409 | |
46e6ad89 | 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; | |
b2687a0a | 1421 | |
46e6ad89 | 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) | |
fbca05a1 | 1466 | { return (getulong(KEY_K(me)->id)); } |
11cb3d97 MW |
1467 | static PyObject *kget_file(PyObject *me, void *hunoz) |
1468 | { RETURN_OBJ(KEY_KFOBJ(me)); } | |
46e6ad89 | 1469 | static PyObject *kget_type(PyObject *me, void *hunoz) |
1470 | { return (PyString_FromString(KEY_K(me)->type)); } | |
1471 | static PyObject *kget_exptime(PyObject *me, void *hunoz) | |
69ba6b57 | 1472 | { return (getulong(KEY_K(me)->exp)); } |
46e6ad89 | 1473 | static PyObject *kget_deltime(PyObject *me, void *hunoz) |
69ba6b57 | 1474 | { return (getulong(KEY_K(me)->del)); } |
46e6ad89 | 1475 | static PyObject *kget_expiredp(PyObject *me, void *hunoz) |
1476 | { return (getbool(key_expired(KEY_K(me)))); } | |
1477 | static PyObject *kget_attr(PyObject *me, void *hunoz) | |
1478 | { return (keyattrs_make(me)); } | |
1479 | ||
69ba6b57 MW |
1480 | static int kset_exptime(PyObject *me, PyObject *x, void *hunoz) |
1481 | { | |
1482 | key *k = KEY_K(me); | |
1483 | unsigned long et; | |
1484 | ||
61e45e9b | 1485 | if (!convulong(x, &et)) |
69ba6b57 MW |
1486 | goto end; |
1487 | if (!(KEY_KF(me)->f & KF_WRITE)) | |
1488 | KEYERR(KERR_READONLY); | |
1489 | k->exp = et; | |
1490 | KEY_KF(me)->f |= KF_MODIFIED; | |
1491 | return (0); | |
1492 | end: | |
1493 | return (-1); | |
1494 | } | |
1495 | ||
1496 | static int kset_deltime(PyObject *me, PyObject *x, void *hunoz) | |
1497 | { | |
1498 | key *k = KEY_K(me); | |
1499 | unsigned long dt; | |
1500 | ||
61e45e9b | 1501 | if (!convulong(x, &dt)) |
69ba6b57 MW |
1502 | goto end; |
1503 | if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER) | |
1504 | VALERR("key will eventually expire"); | |
1505 | if (!(KEY_KF(me)->f & KF_WRITE)) | |
1506 | KEYERR(KERR_READONLY); | |
1507 | k->del = dt; | |
1508 | KEY_KF(me)->f |= KF_MODIFIED; | |
1509 | return (0); | |
1510 | end: | |
1511 | return (-1); | |
1512 | } | |
1513 | ||
46e6ad89 | 1514 | static PyObject *kget_data(PyObject *me, void *hunoz) |
1515 | { | |
1516 | key_data *kd = KEY_K(me)->k; | |
1517 | key_incref(kd); | |
1518 | return (keydata_pywrap(kd)); | |
1519 | } | |
1520 | static int kset_data(PyObject *me, PyObject *x, void *hunoz) | |
1521 | { | |
1522 | int err; | |
11cb3d97 MW |
1523 | |
1524 | if (!x) NIERR("__del__"); | |
46e6ad89 | 1525 | if (!KEYDATA_PYCHECK(x)) TYERR("expected KeyData object"); |
1526 | if ((err = key_setkeydata(KEY_KF(me), KEY_K(me), KEYDATA_KD(x))) != 0) | |
1527 | KEYERR(err); | |
1528 | return (0); | |
1529 | end: | |
1530 | return (-1); | |
1531 | } | |
1532 | ||
1533 | static PyObject *kget_fulltag(PyObject *me, void *hunoz) | |
1534 | { | |
1535 | dstr d = DSTR_INIT; | |
1536 | PyObject *rc; | |
1537 | ||
1538 | key_fulltag(KEY_K(me), &d); | |
1539 | rc = PyString_FromStringAndSize(d.buf, d.len); | |
1540 | dstr_destroy(&d); | |
1541 | return (rc); | |
1542 | } | |
1543 | ||
1544 | static PyObject *kget_tag(PyObject *me, void *hunoz) | |
1545 | { | |
1546 | if (!KEY_K(me)->tag) RETURN_NONE; | |
1547 | return (PyString_FromString(KEY_K(me)->tag)); | |
1548 | } | |
1549 | static int kset_tag(PyObject *me, PyObject *x, void *hunoz) | |
1550 | { | |
1551 | int err; | |
1552 | char *tag; | |
1553 | ||
11cb3d97 | 1554 | if (!x || x == Py_None) tag = 0; |
46e6ad89 | 1555 | else if ((tag = PyString_AsString(x)) == 0) goto end; |
1556 | if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err); | |
1557 | return (0); | |
1558 | end: | |
1559 | return (-1); | |
1560 | } | |
1561 | ||
1562 | static PyObject *kget_comment(PyObject *me, void *hunoz) | |
1563 | { | |
1564 | if (!KEY_K(me)->c) RETURN_NONE; | |
1565 | return (PyString_FromString(KEY_K(me)->c)); | |
1566 | } | |
1567 | static int kset_comment(PyObject *me, PyObject *x, void *hunoz) | |
1568 | { | |
1569 | int err; | |
1570 | char *c; | |
1571 | ||
11cb3d97 | 1572 | if (!x || x == Py_None) c = 0; |
46e6ad89 | 1573 | else if ((c = PyString_AsString(x)) == 0) goto end; |
1574 | if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err); | |
1575 | return (0); | |
1576 | end: | |
1577 | return (-1); | |
1578 | } | |
1579 | ||
1580 | static PyMethodDef key_pymethods[] = { | |
1581 | #define METHNAME(func) kmeth_##func | |
1582 | METH (delete, "KEY.delete()") | |
1583 | METH (expire, "KEY.expire()") | |
1584 | METH (used, "KEY.used(TIME)") | |
1585 | KWMETH(extract, "KEY.extract(FILE, filter = '')") | |
1586 | KWMETH(fingerprint, "KEY.fingerprint(HASH, filtr = '-secret')") | |
1587 | #undef METHNAME | |
1588 | { 0 } | |
1589 | }; | |
1590 | ||
1591 | static PyGetSetDef key_pygetset[] = { | |
1592 | #define GETSETNAME(op, name) k##op##_##name | |
11cb3d97 | 1593 | GET (file, "KEY.file -> KF") |
46e6ad89 | 1594 | GET (id, "KEY.id -> ID") |
1595 | GETSET(tag, "KEY.tag -> TAG") | |
1596 | GET (type, "KEY.type -> TYPE") | |
69ba6b57 MW |
1597 | GETSET(exptime, "KEY.exptime -> TIME") |
1598 | GETSET(deltime, "KEY.deltime -> TIME") | |
46e6ad89 | 1599 | GET (expiredp, "KEY.expiredp -> BOOL") |
1600 | GET (attr, "KEY.attr -> ATTRIBUTES") | |
1601 | GETSET(data, "KEY.data -> KD") | |
1602 | GETSET(comment, "KEY.comment -> COMMENT") | |
1603 | GET (fulltag, "KEY.fulltag -> FULLTAG") | |
1604 | #undef GETSETNAME | |
1605 | { 0 } | |
1606 | }; | |
1607 | ||
1608 | static PyTypeObject key_pytype_skel = { | |
1609 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1610 | "catacomb.Key", /* @tp_name@ */ | |
1611 | sizeof(key_pyobj), /* @tp_basicsize@ */ | |
1612 | 0, /* @tp_itemsize@ */ | |
1613 | ||
1614 | key_pydealloc, /* @tp_dealloc@ */ | |
1615 | 0, /* @tp_print@ */ | |
1616 | 0, /* @tp_getattr@ */ | |
1617 | 0, /* @tp_setattr@ */ | |
1618 | 0, /* @tp_compare@ */ | |
1619 | 0, /* @tp_repr@ */ | |
1620 | 0, /* @tp_as_number@ */ | |
1621 | 0, /* @tp_as_sequence@ */ | |
1622 | 0, /* @tp_as_mapping@ */ | |
1623 | 0, /* @tp_hash@ */ | |
1624 | 0, /* @tp_call@ */ | |
1625 | 0, /* @tp_str@ */ | |
1626 | 0, /* @tp_getattro@ */ | |
1627 | 0, /* @tp_setattro@ */ | |
1628 | 0, /* @tp_as_buffer@ */ | |
1629 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1630 | Py_TPFLAGS_BASETYPE, | |
1631 | ||
1632 | /* @tp_doc@ */ | |
1633 | "Key object.", | |
1634 | ||
1635 | 0, /* @tp_traverse@ */ | |
1636 | 0, /* @tp_clear@ */ | |
1637 | 0, /* @tp_richcompare@ */ | |
1638 | 0, /* @tp_weaklistoffset@ */ | |
1639 | 0, /* @tp_iter@ */ | |
963a6148 | 1640 | 0, /* @tp_iternext@ */ |
46e6ad89 | 1641 | key_pymethods, /* @tp_methods@ */ |
1642 | 0, /* @tp_members@ */ | |
1643 | key_pygetset, /* @tp_getset@ */ | |
1644 | 0, /* @tp_base@ */ | |
1645 | 0, /* @tp_dict@ */ | |
1646 | 0, /* @tp_descr_get@ */ | |
1647 | 0, /* @tp_descr_set@ */ | |
1648 | 0, /* @tp_dictoffset@ */ | |
1649 | 0, /* @tp_init@ */ | |
1650 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1651 | key_pynew, /* @tp_new@ */ | |
1652 | 0, /* @tp_free@ */ | |
1653 | 0 /* @tp_is_gc@ */ | |
1654 | }; | |
1655 | ||
1656 | /*----- Key iteration -----------------------------------------------------*/ | |
1657 | ||
1658 | static PyObject *keyiter_new(PyObject *kfobj) | |
1659 | { | |
1660 | keyiter_pyobj *me = PyObject_NEW(keyiter_pyobj, keyiter_pytype); | |
1661 | key_mkiter(&me->i, KEYFILE_KF(kfobj)); | |
1662 | me->kfobj = kfobj; | |
1663 | Py_INCREF(kfobj); | |
1664 | return ((PyObject *)me); | |
1665 | } | |
1666 | ||
11cb3d97 | 1667 | static PyObject *keyiter_pynext(PyObject *me) |
46e6ad89 | 1668 | { |
1669 | key *k; | |
1670 | ||
1671 | if ((k = key_next(KEYITER_I(me))) == 0) | |
1672 | return (0); | |
426e898f | 1673 | return (getulong(k->id)); |
46e6ad89 | 1674 | } |
1675 | ||
1676 | static void keyiter_pydealloc(PyObject *me) | |
1677 | { | |
1678 | Py_DECREF(KEYITER_KFOBJ(me)); | |
1679 | FREEOBJ(me); | |
1680 | } | |
1681 | ||
1682 | static PyTypeObject keyiter_pytype_skel = { | |
1683 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1684 | "catacomb.KeyFileIter", /* @tp_name@ */ | |
1685 | sizeof(keyiter_pyobj), /* @tp_basicsize@ */ | |
1686 | 0, /* @tp_itemsize@ */ | |
1687 | ||
1688 | keyiter_pydealloc, /* @tp_dealloc@ */ | |
1689 | 0, /* @tp_print@ */ | |
1690 | 0, /* @tp_getattr@ */ | |
1691 | 0, /* @tp_setattr@ */ | |
1692 | 0, /* @tp_compare@ */ | |
1693 | 0, /* @tp_repr@ */ | |
1694 | 0, /* @tp_as_number@ */ | |
1695 | 0, /* @tp_as_sequence@ */ | |
1696 | 0, /* @tp_as_mapping@ */ | |
1697 | 0, /* @tp_hash@ */ | |
1698 | 0, /* @tp_call@ */ | |
1699 | 0, /* @tp_str@ */ | |
1700 | 0, /* @tp_getattro@ */ | |
1701 | 0, /* @tp_setattro@ */ | |
1702 | 0, /* @tp_as_buffer@ */ | |
1703 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1704 | Py_TPFLAGS_BASETYPE, | |
1705 | ||
1706 | /* @tp_doc@ */ | |
1707 | "Keyring iterator.", | |
1708 | ||
1709 | 0, /* @tp_traverse@ */ | |
1710 | 0, /* @tp_clear@ */ | |
1711 | 0, /* @tp_richcompare@ */ | |
1712 | 0, /* @tp_weaklistoffset@ */ | |
1713 | PyObject_SelfIter, /* @tp_iter@ */ | |
11cb3d97 | 1714 | keyiter_pynext, /* @tp_iternext@ */ |
46e6ad89 | 1715 | 0, /* @tp_methods@ */ |
1716 | 0, /* @tp_members@ */ | |
1717 | 0, /* @tp_getset@ */ | |
1718 | 0, /* @tp_base@ */ | |
1719 | 0, /* @tp_dict@ */ | |
1720 | 0, /* @tp_descr_get@ */ | |
1721 | 0, /* @tp_descr_set@ */ | |
1722 | 0, /* @tp_dictoffset@ */ | |
1723 | 0, /* @tp_init@ */ | |
1724 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1725 | abstract_pynew, /* @tp_new@ */ | |
1726 | 0, /* @tp_free@ */ | |
1727 | 0 /* @tp_is_gc@ */ | |
1728 | }; | |
1729 | ||
d7ab1bab | 1730 | /*----- Key files ---------------------------------------------------------*/ |
1731 | ||
46e6ad89 | 1732 | struct reportinfo { |
1733 | PyObject *func; | |
1734 | int stop; | |
1735 | }; | |
1736 | ||
1737 | static void pythonreporter(const char *file, int line, | |
1738 | const char *msg, void *p) | |
1739 | { | |
1740 | struct reportinfo *ri = p; | |
1741 | PyObject *res = 0; | |
1742 | ||
1743 | if (ri->stop) | |
1744 | return; | |
1745 | if (!ri->func) | |
1746 | key_moan(file, line, msg, 0); | |
1747 | else if ((res = PyObject_CallFunction(ri->func, "sis", | |
1748 | file, line, msg)) == 0) | |
1749 | ri->stop = 1; | |
1750 | else | |
1751 | Py_DECREF(res); | |
1752 | } | |
1753 | ||
d7ab1bab | 1754 | static PyObject *keyfile_pynew(PyTypeObject *ty, |
1755 | PyObject *arg, PyObject *kw) | |
1756 | { | |
46e6ad89 | 1757 | struct reportinfo ri = { 0, 0 }; |
1758 | char *file = 0; | |
1759 | unsigned how = KOPEN_READ; | |
1760 | keyfile_pyobj *rc = 0; | |
1761 | static char *kwlist[] = { "file", "how", "report", 0 }; | |
1762 | ||
1763 | Py_XINCREF(arg); Py_XINCREF(kw); | |
1764 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", kwlist, | |
1765 | &file, &how, &ri.func)) | |
1766 | goto end; | |
1767 | if (ri.func && !PyCallable_Check(ri.func)) | |
1768 | TYERR("reporter function not callable"); | |
1769 | if ((rc = (keyfile_pyobj *)ty->tp_alloc(ty, 0)) == 0) | |
1770 | goto end; | |
1771 | if (key_open(&rc->kf, file, how, pythonreporter, &ri)) | |
1772 | OSERR(file); | |
1773 | if (ri.stop) { | |
1774 | key_discard(&rc->kf); | |
1775 | goto end; | |
1776 | } | |
1777 | goto done; | |
1778 | ||
1779 | end: | |
1780 | if (rc) { | |
1781 | FREEOBJ(rc); | |
1782 | rc = 0; | |
1783 | } | |
1784 | done: | |
1785 | Py_XDECREF(arg); Py_XDECREF(kw); | |
1786 | return ((PyObject *)rc); | |
1787 | } | |
1788 | ||
1789 | static void keyfile_pydealloc(PyObject *me) | |
1790 | { | |
1791 | key_discard(KEYFILE_KF(me)); | |
1792 | FREEOBJ(me); | |
1793 | } | |
1794 | ||
1795 | static PyObject *kfmeth_save(PyObject *me, PyObject *arg) | |
1796 | { | |
1797 | if (!PyArg_ParseTuple(arg, ":save")) | |
1798 | goto end; | |
1799 | switch (key_save(KEYFILE_KF(me))) { | |
1800 | case KWRITE_OK: | |
1801 | RETURN_ME; | |
1802 | case KWRITE_FAIL: | |
1803 | KEYIOERR(KEYFILE_KF(me)->name); | |
1804 | case KWRITE_BROKEN: | |
1805 | KEYFILEBROKEN(KEYFILE_KF(me)->name); | |
1806 | default: | |
1807 | abort(); | |
1808 | } | |
1809 | end: | |
1810 | return (0); | |
1811 | } | |
1812 | ||
1813 | static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw) | |
1814 | { | |
1815 | struct reportinfo ri = { 0, 0 }; | |
1816 | char *name; | |
1817 | PyObject *x = 0; | |
1818 | FILE *fp = 0; | |
1819 | int rc; | |
1820 | static char *kwlist[] = { "file", "report", 0 }; | |
1821 | ||
1822 | Py_XINCREF(arg); Py_XINCREF(kw); | |
1823 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", kwlist, | |
1824 | &PyFile_Type, &x, &ri.func)) | |
1825 | goto end; | |
1826 | if (ri.func && !PyCallable_Check(ri.func)) | |
1827 | TYERR("reporter function not callable"); | |
1828 | if ((fp = PyFile_AsFile(x)) == 0) | |
1829 | goto end; | |
1830 | x = PyFile_Name(x); | |
1831 | if ((name = PyString_AsString(x)) == 0) | |
1832 | goto end; | |
1833 | rc = key_merge(KEYFILE_KF(me), name, fp, pythonreporter, &ri); | |
1834 | if (ri.stop) | |
1835 | goto end; | |
1836 | if (rc != 0) | |
1837 | KEYERR(rc); | |
1838 | Py_XDECREF(arg); Py_XDECREF(kw); | |
1839 | RETURN_ME; | |
1840 | ||
1841 | end: | |
1842 | Py_XDECREF(arg); Py_XDECREF(kw); | |
1843 | return (0); | |
1844 | } | |
1845 | ||
1846 | static PyObject *kfmeth_byid(PyObject *me, PyObject *arg) | |
1847 | { | |
1848 | uint32 id; | |
1849 | key *k; | |
1850 | PyObject *rc = 0; | |
1851 | ||
1852 | if (!PyArg_ParseTuple(arg, "O&:byid", convu32, &id)) goto end; | |
1853 | if ((k = key_byid(KEYFILE_KF(me), id)) == 0) KEYERR(KERR_NOTFOUND); | |
1854 | rc = key_pywrap(me, k); | |
1855 | end: | |
1856 | return (rc); | |
1857 | } | |
1858 | ||
1859 | static PyObject *kfmeth_bytype(PyObject *me, PyObject *arg) | |
1860 | { | |
1861 | char *type; | |
1862 | key *k; | |
1863 | PyObject *rc = 0; | |
1864 | ||
1865 | if (!PyArg_ParseTuple(arg, "s:bytype", &type)) goto end; | |
1866 | if ((k = key_bytype(KEYFILE_KF(me), type)) == 0) RETURN_NONE; | |
1867 | rc = key_pywrap(me, k); | |
1868 | end: | |
1869 | return (rc); | |
1870 | } | |
1871 | ||
1872 | static PyObject *bytag(PyObject *me, PyObject *tagobj) | |
1873 | { | |
1874 | uint32 id; | |
1875 | char *tag; | |
1876 | key *k; | |
1877 | PyObject *rc = 0; | |
1878 | ||
1879 | if (convu32(tagobj, &id)) | |
1880 | k = key_byid(KEYFILE_KF(me), id); | |
1881 | else { | |
1882 | PyErr_Clear(); | |
1883 | if ((tag = PyString_AsString(tagobj)) == 0) | |
1884 | goto end; | |
1885 | k = key_bytag(KEYFILE_KF(me), tag); | |
1886 | } | |
1887 | if (!k) RETURN_NONE; | |
1888 | rc = key_pywrap(me, k); | |
1889 | end: | |
1890 | return (rc); | |
1891 | } | |
1892 | ||
1893 | static PyObject *kfmeth_bytag(PyObject *me, PyObject *arg) | |
1894 | { | |
1895 | PyObject *tagobj; | |
1896 | ||
1897 | if (!PyArg_ParseTuple(arg, "O:bytag", &tagobj)) return (0); | |
1898 | return (bytag(me, tagobj)); | |
1899 | } | |
1900 | ||
1901 | static PyObject *keyfile_pylookup(PyObject *me, PyObject *key) | |
1902 | { | |
1903 | PyObject *rc = bytag(me, key); | |
1904 | if (!rc) goto end; | |
1905 | if (rc == Py_None) { | |
1906 | Py_DECREF(rc); | |
1907 | rc = 0; | |
1908 | INDEXERR(key); | |
1909 | } | |
1910 | end: | |
1911 | return (rc); | |
b2687a0a | 1912 | } |
46e6ad89 | 1913 | |
1914 | static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw) | |
1915 | { | |
1916 | uint32 id; | |
1917 | char *type; | |
1918 | long exptime = KEXP_FOREVER; | |
1919 | static char *kwlist[] = { "id", "type", "exptime", 0 }; | |
1920 | key *k; | |
1921 | int err; | |
1922 | ||
24b3d57b | 1923 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", kwlist, |
46e6ad89 | 1924 | convu32, &id, &type, &exptime)) |
1925 | goto end; | |
24b3d57b | 1926 | if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) != 0) |
46e6ad89 | 1927 | KEYERR(err); |
1928 | return (key_pywrap(me, k)); | |
1929 | end: | |
1930 | return (0); | |
1931 | } | |
1932 | ||
1933 | static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw) | |
1934 | { | |
1935 | key *k; | |
ae5cafdb | 1936 | key_data **kd, *okd; |
46e6ad89 | 1937 | PyObject *newkdobj = 0; |
1938 | char *tag; | |
46e6ad89 | 1939 | dstr d = DSTR_INIT; |
1940 | PyObject *rc = 0; | |
1941 | static char *kwlist[] = { "tag", "new", 0 }; | |
1942 | ||
1943 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", kwlist, | |
1944 | &tag, keydata_pytype, &newkdobj)) | |
1945 | goto end; | |
1946 | if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd)) | |
1947 | KEYERR(KERR_NOTFOUND); | |
ae5cafdb | 1948 | okd = *kd; |
46e6ad89 | 1949 | if (newkdobj) { |
1950 | if (!(KEYFILE_KF(me)->f & KF_WRITE)) | |
1951 | KEYERR(KERR_READONLY); | |
ae5cafdb | 1952 | KEYFILE_KF(me)->f |= KF_MODIFIED; |
46e6ad89 | 1953 | *kd = KEYDATA_KD(newkdobj); |
46e6ad89 | 1954 | } |
ae5cafdb | 1955 | key_incref(*kd); |
1956 | rc = Py_BuildValue("(s#NN)", | |
1957 | d.buf, d.len, | |
1958 | key_pywrap(me, k), | |
1959 | keydata_pywrap(okd)); | |
46e6ad89 | 1960 | end: |
1961 | return (rc); | |
1962 | } | |
1963 | ||
1964 | static PyObject *kfget_name(PyObject *me, void *hunoz) | |
1965 | { return (PyString_FromString(KEYFILE_KF(me)->name)); } | |
1966 | static PyObject *kfget_modifiedp(PyObject *me, void *hunoz) | |
1967 | { return (getbool(KEYFILE_KF(me)->f & KF_MODIFIED)); } | |
1968 | static PyObject *kfget_writep(PyObject *me, void *hunoz) | |
1969 | { return (getbool(KEYFILE_KF(me)->f & KF_WRITE)); } | |
1970 | static PyObject *kfget_filep(PyObject *me, void *hunoz) | |
1971 | { return (getbool(!!KEYFILE_KF(me)->fp)); } | |
1972 | ||
1973 | static PyMethodDef keyfile_pymethods[] = { | |
1974 | #define METHNAME(func) kfmeth_##func | |
1975 | METH (save, "KF.save()") | |
1976 | KWMETH(merge, "KF.merge(FILE, report = <built-in-reporter>)") | |
1977 | KWMETH(newkey, "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY") | |
1978 | METH (byid, "KF.byid(KEYID) -> KEY|None") | |
1979 | METH (bytype, "KF.bytype(TYPE) -> KEY|None") | |
1980 | METH (bytag, "KF.bytag(TAG) -> KEY|None") | |
1981 | KWMETH(qtag, "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD") | |
810c8f3c | 1982 | GMAP_ROMETHODS |
46e6ad89 | 1983 | #undef METHNAME |
1984 | { 0 } | |
1985 | }; | |
1986 | ||
1987 | static PyGetSetDef keyfile_pygetset[] = { | |
1988 | #define GETSETNAME(op, name) kf##op##_##name | |
1989 | GET (name, "KF.name -> file name") | |
1990 | GET (modifiedp, "KF.modifiedp -> has keyring been modified?") | |
1991 | GET (writep, "KF.writep -> is keyring modifiable?") | |
1992 | GET (filep, "KF.filep -> does keyring have a backing file?") | |
1993 | #undef GETSETNAME | |
1994 | { 0 } | |
1995 | }; | |
1996 | ||
1997 | static PyMappingMethods keyfile_pymapping = { | |
810c8f3c | 1998 | gmap_pysize, |
46e6ad89 | 1999 | keyfile_pylookup, |
2000 | 0 | |
2001 | }; | |
2002 | ||
2003 | static PyTypeObject keyfile_pytype_skel = { | |
2004 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
2005 | "catacomb.KeyFile", /* @tp_name@ */ | |
2006 | sizeof(keyfile_pyobj), /* @tp_basicsize@ */ | |
2007 | 0, /* @tp_itemsize@ */ | |
2008 | ||
2009 | keyfile_pydealloc, /* @tp_dealloc@ */ | |
2010 | 0, /* @tp_print@ */ | |
2011 | 0, /* @tp_getattr@ */ | |
2012 | 0, /* @tp_setattr@ */ | |
2013 | 0, /* @tp_compare@ */ | |
2014 | 0, /* @tp_repr@ */ | |
2015 | 0, /* @tp_as_number@ */ | |
11cb3d97 | 2016 | &gmap_pysequence, /* @tp_as_sequence@ */ |
46e6ad89 | 2017 | &keyfile_pymapping, /* @tp_as_mapping@ */ |
2018 | 0, /* @tp_hash@ */ | |
2019 | 0, /* @tp_call@ */ | |
2020 | 0, /* @tp_str@ */ | |
2021 | 0, /* @tp_getattro@ */ | |
2022 | 0, /* @tp_setattro@ */ | |
2023 | 0, /* @tp_as_buffer@ */ | |
2024 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2025 | Py_TPFLAGS_BASETYPE, | |
2026 | ||
2027 | /* @tp_doc@ */ | |
2028 | "Keyring file.", | |
2029 | ||
2030 | 0, /* @tp_traverse@ */ | |
2031 | 0, /* @tp_clear@ */ | |
2032 | 0, /* @tp_richcompare@ */ | |
2033 | 0, /* @tp_weaklistoffset@ */ | |
2034 | keyiter_new, /* @tp_iter@ */ | |
963a6148 | 2035 | 0, /* @tp_iternext@ */ |
46e6ad89 | 2036 | keyfile_pymethods, /* @tp_methods@ */ |
2037 | 0, /* @tp_members@ */ | |
2038 | keyfile_pygetset, /* @tp_getset@ */ | |
2039 | 0, /* @tp_base@ */ | |
2040 | 0, /* @tp_dict@ */ | |
2041 | 0, /* @tp_descr_get@ */ | |
2042 | 0, /* @tp_descr_set@ */ | |
2043 | 0, /* @tp_dictoffset@ */ | |
2044 | 0, /* @tp_init@ */ | |
2045 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2046 | keyfile_pynew, /* @tp_new@ */ | |
2047 | 0, /* @tp_free@ */ | |
2048 | 0 /* @tp_is_gc@ */ | |
2049 | }; | |
2050 | ||
2051 | /*----- Global stuff ------------------------------------------------------*/ | |
2052 | ||
2053 | static PyObject *meth_barf(PyObject *me, PyObject *arg) | |
2054 | { | |
2055 | int err; | |
2056 | ||
2057 | if (PyArg_ParseTuple(arg, "i:barf", &err)) | |
2058 | KEYERR(err); | |
2059 | end: | |
2060 | return (0); | |
2061 | } | |
2062 | ||
2063 | static PyMethodDef methods[] = { | |
2064 | #define METHNAME(func) meth_##func | |
2065 | METH (_KeyData_readflags, | |
2066 | "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)") | |
2067 | METH (_KeyData_writeflags, "KeyData.writeflags(FLAGS) -> STRING") | |
2068 | METH (_KeyData_read, "KeyData.read(STRING) -> (KD, REST)") | |
2069 | METH (_KeyData_decode, "KeyData.read(BYTES) -> KD") | |
2070 | METH (barf, "barf(ERR)") | |
2071 | #undef METHNAME | |
2072 | { 0 } | |
2073 | }; | |
2074 | ||
2075 | /*----- Initialization ----------------------------------------------------*/ | |
2076 | ||
2077 | void key_pyinit(void) | |
2078 | { | |
2079 | INITTYPE(keyfile, root); | |
2080 | INITTYPE(key, root); | |
2081 | INITTYPE(keyiter, root); | |
2082 | INITTYPE(keydata, root); | |
2083 | INITTYPE(keydatabin, keydata); | |
2084 | INITTYPE(keydataenc, keydata); | |
2085 | INITTYPE(keydatastr, keydata); | |
2086 | INITTYPE(keydatamp, keydata); | |
2087 | INITTYPE(keydataec, keydata); | |
2088 | INITTYPE(keydatastruct, keydata); | |
2089 | INITTYPE(subkeyiter, root); | |
2090 | INITTYPE(keyattrs, root); | |
2091 | INITTYPE(keyattriter, root); | |
2092 | addmethods(methods); | |
2093 | } | |
2094 | ||
2095 | void key_pyinsert(PyObject *mod) | |
2096 | { | |
2097 | INSEXC("KeyError", keyexc, PyExc_Exception, keyexc_pymethods); | |
2098 | INSEXC("KeyFileIOError", keyioexc, PyExc_OSError, 0); | |
2099 | INSEXC("KeyFileBroken", keyfilebrokenexc, keyioexc, 0); | |
2100 | INSERT("KeyFile", keyfile_pytype); | |
2101 | INSERT("KeyFileIter", keyiter_pytype); | |
2102 | INSERT("Key", key_pytype); | |
2103 | INSERT("KeyAttributes", keyattrs_pytype); | |
2104 | INSERT("KeyAttributeIter", keyattriter_pytype); | |
2105 | INSERT("KeyData", keydata_pytype); | |
2106 | INSERT("KeyDataBinary", keydatabin_pytype); | |
2107 | INSERT("KeyDataEncrypted", keydataenc_pytype); | |
2108 | INSERT("KeyDataMP", keydatamp_pytype); | |
2109 | INSERT("KeyDataECPt", keydataec_pytype); | |
2110 | INSERT("KeyDataString", keydatastr_pytype); | |
2111 | INSERT("KeyDataStructured", keydatastruct_pytype); | |
2112 | INSERT("SubKeyIter", subkeyiter_pytype); | |
d7ab1bab | 2113 | } |
2114 | ||
2115 | /*----- That's all, folks -------------------------------------------------*/ |