3 * Reading and writing passphrases
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Pixie stuff -------------------------------------------------------*/
33 typedef struct pixie_pyobj {
38 static PyTypeObject *pixie_pytype;
39 #define PIXIE_PYCHECK(o) PyObject_TypeCheck((o), pixie_pytype)
40 #define PIXIE_FD(o) (((pixie_pyobj *)(o))->fd)
43 static int convpixie(PyObject *o, void *p)
45 if (!PIXIE_PYCHECK(o))
47 *(int *)p = PIXIE_FD(o);
54 static PyObject *pixie_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
57 char *kwlist[] = { "socket", 0 };
61 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s:new", kwlist, &sock))
63 if ((fd = pixie_open(sock)) < 0)
65 rc = (pixie_pyobj *)ty->tp_alloc(ty, 0);
68 return ((PyObject *)rc);
71 static void pixie_pydealloc(PyObject *me)
77 static PyObject *pixmeth_read(PyObject *me, PyObject *arg, PyObject *kw)
79 unsigned mode = PMODE_READ;
81 char *kwlist[] = { "tag", "mode", 0 };
86 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:read", kwlist,
87 &tag, convuint, &mode))
89 r = pixie_read(PIXIE_FD(me), tag, mode, buf, sizeof(buf));
95 rc = PyString_FromString(buf);
100 static PyObject *pixmeth_set(PyObject *me, PyObject *arg)
105 if (!PyArg_ParseTuple(arg, "ss:set", &tag, &phrase))
107 pixie_set(PIXIE_FD(me), tag, phrase);
111 static PyObject *pixmeth_cancel(PyObject *me, PyObject *arg)
115 if (!PyArg_ParseTuple(arg, "s:cancel", &tag))
117 pixie_cancel(PIXIE_FD(me), tag);
121 static PyMethodDef pixie_pymethods[] = {
122 #define METHNAME(name) pixmeth_##name
123 KWMETH(read, "P.read(TAG, [mode = PMODE_READ]) -> STRING")
124 METH (set, "P.set(TAG, PHRASE)")
125 METH (cancel, "P.cancel(TAG)")
130 static PyTypeObject pixie_pytype_skel = {
131 PyObject_HEAD_INIT(0) 0, /* Header */
132 "Pixie", /* @tp_name@ */
133 sizeof(pixie_pyobj), /* @tp_basicsize@ */
134 0, /* @tp_itemsize@ */
136 pixie_pydealloc, /* @tp_dealloc@ */
138 0, /* @tp_getattr@ */
139 0, /* @tp_setattr@ */
140 0, /* @tp_compare@ */
142 0, /* @tp_as_number@ */
143 0, /* @tp_as_sequence@ */
144 0, /* @tp_as_mapping@ */
148 0, /* @tp_getattro@ */
149 0, /* @tp_setattro@ */
150 0, /* @tp_as_buffer@ */
151 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
155 "Passphrase pixie connection.",
157 0, /* @tp_traverse@ */
159 0, /* @tp_richcompare@ */
160 0, /* @tp_weaklistoffset@ */
162 0, /* @tp_iternext@ */
163 pixie_pymethods, /* @tp_methods@ */
164 0, /* @tp_members@ */
168 0, /* @tp_descr_get@ */
169 0, /* @tp_descr_set@ */
170 0, /* @tp_dictoffset@ */
172 PyType_GenericAlloc, /* @tp_alloc@ */
173 pixie_pynew, /* @tp_new@ */
178 /*----- Main code ---------------------------------------------------------*/
180 static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw)
183 unsigned f = PMODE_READ;
185 char *kwlist[] = { "tag", "mode", 0 };
188 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", kwlist,
191 if (passphrase_read(tag, f, buf, sizeof(buf)))
192 SYSERR("passphrase read failed");
193 rc = PyString_FromString(buf);
198 static PyObject *meth_ppcancel(PyObject *me, PyObject *arg)
202 if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag))
204 passphrase_cancel(tag);
208 static PyObject *meth_getpass(PyObject *me, PyObject *arg)
214 if (!PyArg_ParseTuple(arg, "s:getpass", &prompt))
216 if (pixie_getpass(prompt, buf, sizeof(buf)))
218 rc = PyString_FromString(buf);
223 static PyMethodDef methods[] = {
224 #define METHNAME(name) meth_##name
225 KWMETH(ppread, "ppread(TAG, [mode = PMODE_READ]) -> STRING")
226 METH (ppcancel, "ppcancel(TAG)")
227 METH (getpass, "getpass(PROMPT) -> STRING")
232 void passphrase_pyinit(void)
234 INITTYPE(pixie, root);
238 void passphrase_pyinsert(PyObject *mod)
240 INSERT("Pixie", pixie_pytype);
243 /*----- That's all, folks -------------------------------------------------*/