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