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