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