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