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