| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Reading and writing passphrases |
| 6 | * |
| 7 | * (c) 2005 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the Python interface to Catacomb. |
| 13 | * |
| 14 | * Catacomb/Python is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb/Python is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with Catacomb/Python; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "catacomb-python.h" |
| 32 | |
| 33 | /*----- Pixie stuff -------------------------------------------------------*/ |
| 34 | |
| 35 | typedef struct pixie_pyobj { |
| 36 | PyObject_HEAD |
| 37 | int fd; |
| 38 | } pixie_pyobj; |
| 39 | |
| 40 | static PyTypeObject *pixie_pytype; |
| 41 | #define PIXIE_PYCHECK(o) PyObject_TypeCheck((o), pixie_pytype) |
| 42 | #define PIXIE_FD(o) (((pixie_pyobj *)(o))->fd) |
| 43 | |
| 44 | static int convpixie(PyObject *o, void *p) |
| 45 | { |
| 46 | if (!PIXIE_PYCHECK(o)) |
| 47 | TYERR("want pixie"); |
| 48 | *(int *)p = PIXIE_FD(o); |
| 49 | return (1); |
| 50 | end: |
| 51 | return (0); |
| 52 | } |
| 53 | |
| 54 | static PyObject *pixie_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 55 | { |
| 56 | pixie_pyobj *rc = 0; |
| 57 | char *kwlist[] = { "socket", 0 }; |
| 58 | char *sock = 0; |
| 59 | int fd; |
| 60 | |
| 61 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s:new", kwlist, &sock)) |
| 62 | goto end; |
| 63 | if ((fd = pixie_open(sock)) < 0) |
| 64 | OSERR(sock); |
| 65 | rc = (pixie_pyobj *)ty->tp_alloc(ty, 0); |
| 66 | rc->fd = fd; |
| 67 | end: |
| 68 | return ((PyObject *)rc); |
| 69 | } |
| 70 | |
| 71 | static void pixie_pydealloc(PyObject *me) |
| 72 | { |
| 73 | close(PIXIE_FD(me)); |
| 74 | PyObject_DEL(me); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | static PyObject *pixmeth_read(PyObject *me, PyObject *arg, PyObject *kw) |
| 79 | { |
| 80 | unsigned mode = PMODE_READ; |
| 81 | char *tag; |
| 82 | char *kwlist[] = { "tag", "mode", 0 }; |
| 83 | PyObject *rc = 0; |
| 84 | int r; |
| 85 | char buf[1024]; |
| 86 | |
| 87 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:read", kwlist, |
| 88 | &tag, convuint, &mode)) |
| 89 | goto end; |
| 90 | r = pixie_read(PIXIE_FD(me), tag, mode, buf, sizeof(buf)); |
| 91 | if (r < 0) |
| 92 | OSERR(0); |
| 93 | else if (r > 0) |
| 94 | RETURN_NONE; |
| 95 | else |
| 96 | rc = PyString_FromString(buf); |
| 97 | end: |
| 98 | return (rc); |
| 99 | } |
| 100 | |
| 101 | static PyObject *pixmeth_set(PyObject *me, PyObject *arg) |
| 102 | { |
| 103 | char *tag; |
| 104 | char *phrase; |
| 105 | |
| 106 | if (!PyArg_ParseTuple(arg, "ss:set", &tag, &phrase)) |
| 107 | return (0); |
| 108 | pixie_set(PIXIE_FD(me), tag, phrase); |
| 109 | RETURN_ME; |
| 110 | } |
| 111 | |
| 112 | static PyObject *pixmeth_cancel(PyObject *me, PyObject *arg) |
| 113 | { |
| 114 | char *tag; |
| 115 | |
| 116 | if (!PyArg_ParseTuple(arg, "s:cancel", &tag)) |
| 117 | return (0); |
| 118 | pixie_cancel(PIXIE_FD(me), tag); |
| 119 | RETURN_ME; |
| 120 | } |
| 121 | |
| 122 | static PyMethodDef pixie_pymethods[] = { |
| 123 | #define METHNAME(name) pixmeth_##name |
| 124 | KWMETH(read, "P.read(TAG, [mode = PMODE_READ]) -> STRING") |
| 125 | METH (set, "P.set(TAG, PHRASE)") |
| 126 | METH (cancel, "P.cancel(TAG)") |
| 127 | #undef METHNAME |
| 128 | { 0 } |
| 129 | }; |
| 130 | |
| 131 | static PyTypeObject pixie_pytype_skel = { |
| 132 | PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ |
| 133 | "catacomb.Pixie", /* @tp_name@ */ |
| 134 | sizeof(pixie_pyobj), /* @tp_basicsize@ */ |
| 135 | 0, /* @tp_itemsize@ */ |
| 136 | |
| 137 | pixie_pydealloc, /* @tp_dealloc@ */ |
| 138 | 0, /* @tp_print@ */ |
| 139 | 0, /* @tp_getattr@ */ |
| 140 | 0, /* @tp_setattr@ */ |
| 141 | 0, /* @tp_compare@ */ |
| 142 | 0, /* @tp_repr@ */ |
| 143 | 0, /* @tp_as_number@ */ |
| 144 | 0, /* @tp_as_sequence@ */ |
| 145 | 0, /* @tp_as_mapping@ */ |
| 146 | 0, /* @tp_hash@ */ |
| 147 | 0, /* @tp_call@ */ |
| 148 | 0, /* @tp_str@ */ |
| 149 | 0, /* @tp_getattro@ */ |
| 150 | 0, /* @tp_setattro@ */ |
| 151 | 0, /* @tp_as_buffer@ */ |
| 152 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 153 | Py_TPFLAGS_BASETYPE, |
| 154 | |
| 155 | /* @tp_doc@ */ |
| 156 | "Passphrase pixie connection.", |
| 157 | |
| 158 | 0, /* @tp_traverse@ */ |
| 159 | 0, /* @tp_clear@ */ |
| 160 | 0, /* @tp_richcompare@ */ |
| 161 | 0, /* @tp_weaklistoffset@ */ |
| 162 | 0, /* @tp_iter@ */ |
| 163 | 0, /* @tp_iternexr@ */ |
| 164 | pixie_pymethods, /* @tp_methods@ */ |
| 165 | 0, /* @tp_members@ */ |
| 166 | 0, /* @tp_getset@ */ |
| 167 | 0, /* @tp_base@ */ |
| 168 | 0, /* @tp_dict@ */ |
| 169 | 0, /* @tp_descr_get@ */ |
| 170 | 0, /* @tp_descr_set@ */ |
| 171 | 0, /* @tp_dictoffset@ */ |
| 172 | 0, /* @tp_init@ */ |
| 173 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 174 | pixie_pynew, /* @tp_new@ */ |
| 175 | _PyObject_Del, /* @tp_free@ */ |
| 176 | 0 /* @tp_is_gc@ */ |
| 177 | }; |
| 178 | |
| 179 | /*----- Main code ---------------------------------------------------------*/ |
| 180 | |
| 181 | static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw) |
| 182 | { |
| 183 | char *tag; |
| 184 | unsigned f = PMODE_READ; |
| 185 | PyObject *rc = 0; |
| 186 | char *kwlist[] = { "tag", "mode", 0 }; |
| 187 | char buf[1024]; |
| 188 | |
| 189 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", kwlist, |
| 190 | &tag, convuint, &f)) |
| 191 | goto end; |
| 192 | if (passphrase_read(tag, f, buf, sizeof(buf))) |
| 193 | SYSERR("passphrase read failed"); |
| 194 | rc = PyString_FromString(buf); |
| 195 | end: |
| 196 | return (rc); |
| 197 | } |
| 198 | |
| 199 | static PyObject *meth_ppcancel(PyObject *me, PyObject *arg) |
| 200 | { |
| 201 | char *tag; |
| 202 | |
| 203 | if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag)) |
| 204 | return (0); |
| 205 | passphrase_cancel(tag); |
| 206 | RETURN_NONE; |
| 207 | } |
| 208 | |
| 209 | static PyObject *meth_getpass(PyObject *me, PyObject *arg) |
| 210 | { |
| 211 | char *prompt; |
| 212 | char buf[1024]; |
| 213 | PyObject *rc = 0; |
| 214 | |
| 215 | if (!PyArg_ParseTuple(arg, "s:getpass", &prompt)) |
| 216 | goto end; |
| 217 | if (pixie_getpass(prompt, buf, sizeof(buf))) |
| 218 | OSERR(0); |
| 219 | rc = PyString_FromString(buf); |
| 220 | end: |
| 221 | return (rc); |
| 222 | } |
| 223 | |
| 224 | static PyMethodDef methods[] = { |
| 225 | #define METHNAME(name) meth_##name |
| 226 | KWMETH(ppread, "ppread(TAG, [mode = PMODE_READ]) -> STRING") |
| 227 | METH (ppcancel, "ppcancel(TAG)") |
| 228 | METH (getpass, "getpass(PROMPT) -> STRING") |
| 229 | #undef METHNAME |
| 230 | { 0 } |
| 231 | }; |
| 232 | |
| 233 | void passphrase_pyinit(void) |
| 234 | { |
| 235 | INITTYPE(pixie, root); |
| 236 | addmethods(methods); |
| 237 | } |
| 238 | |
| 239 | void passphrase_pyinsert(PyObject *mod) |
| 240 | { |
| 241 | INSERT("Pixie", pixie_pytype); |
| 242 | } |
| 243 | |
| 244 | /*----- That's all, folks -------------------------------------------------*/ |