5 * Reading and writing passphrases
7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Pixie stuff -------------------------------------------------------*/
35 typedef struct pixie_pyobj {
40 static PyTypeObject *pixie_pytype;
41 #define PIXIE_PYCHECK(o) PyObject_TypeCheck((o), pixie_pytype)
42 #define PIXIE_FD(o) (((pixie_pyobj *)(o))->fd)
45 static int convpixie(PyObject *o, void *p)
47 if (!PIXIE_PYCHECK(o))
49 *(int *)p = PIXIE_FD(o);
56 static PyObject *pixie_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
59 char *kwlist[] = { "socket", 0 };
63 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s:new", kwlist, &sock))
65 if ((fd = pixie_open(sock)) < 0)
67 rc = (pixie_pyobj *)ty->tp_alloc(ty, 0);
70 return ((PyObject *)rc);
73 static void pixie_pydealloc(PyObject *me)
79 static PyObject *pixmeth_read(PyObject *me, PyObject *arg, PyObject *kw)
81 unsigned mode = PMODE_READ;
83 char *kwlist[] = { "tag", "mode", 0 };
88 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:read", kwlist,
89 &tag, convuint, &mode))
91 r = pixie_read(PIXIE_FD(me), tag, mode, buf, sizeof(buf));
97 rc = PyString_FromString(buf);
102 static PyObject *pixmeth_set(PyObject *me, PyObject *arg)
107 if (!PyArg_ParseTuple(arg, "ss:set", &tag, &phrase))
109 pixie_set(PIXIE_FD(me), tag, phrase);
113 static PyObject *pixmeth_cancel(PyObject *me, PyObject *arg)
117 if (!PyArg_ParseTuple(arg, "s:cancel", &tag))
119 pixie_cancel(PIXIE_FD(me), tag);
123 static PyMethodDef pixie_pymethods[] = {
124 #define METHNAME(name) pixmeth_##name
125 KWMETH(read, "P.read(TAG, [mode = PMODE_READ]) -> STRING")
126 METH (set, "P.set(TAG, PHRASE)")
127 METH (cancel, "P.cancel(TAG)")
132 static PyTypeObject pixie_pytype_skel = {
133 PyObject_HEAD_INIT(0) 0, /* Header */
134 "catacomb.Pixie", /* @tp_name@ */
135 sizeof(pixie_pyobj), /* @tp_basicsize@ */
136 0, /* @tp_itemsize@ */
138 pixie_pydealloc, /* @tp_dealloc@ */
140 0, /* @tp_getattr@ */
141 0, /* @tp_setattr@ */
142 0, /* @tp_compare@ */
144 0, /* @tp_as_number@ */
145 0, /* @tp_as_sequence@ */
146 0, /* @tp_as_mapping@ */
150 0, /* @tp_getattro@ */
151 0, /* @tp_setattro@ */
152 0, /* @tp_as_buffer@ */
153 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
157 "Passphrase pixie connection.",
159 0, /* @tp_traverse@ */
161 0, /* @tp_richcompare@ */
162 0, /* @tp_weaklistoffset@ */
164 0, /* @tp_iternexr@ */
165 pixie_pymethods, /* @tp_methods@ */
166 0, /* @tp_members@ */
170 0, /* @tp_descr_get@ */
171 0, /* @tp_descr_set@ */
172 0, /* @tp_dictoffset@ */
174 PyType_GenericAlloc, /* @tp_alloc@ */
175 pixie_pynew, /* @tp_new@ */
180 /*----- Main code ---------------------------------------------------------*/
182 static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw)
185 unsigned f = PMODE_READ;
187 char *kwlist[] = { "tag", "mode", 0 };
190 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", kwlist,
193 if (passphrase_read(tag, f, buf, sizeof(buf)))
194 SYSERR("passphrase read failed");
195 rc = PyString_FromString(buf);
200 static PyObject *meth_ppcancel(PyObject *me, PyObject *arg)
204 if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag))
206 passphrase_cancel(tag);
210 static PyObject *meth_getpass(PyObject *me, PyObject *arg)
216 if (!PyArg_ParseTuple(arg, "s:getpass", &prompt))
218 if (pixie_getpass(prompt, buf, sizeof(buf)))
220 rc = PyString_FromString(buf);
225 static PyMethodDef methods[] = {
226 #define METHNAME(name) meth_##name
227 KWMETH(ppread, "ppread(TAG, [mode = PMODE_READ]) -> STRING")
228 METH (ppcancel, "ppcancel(TAG)")
229 METH (getpass, "getpass(PROMPT) -> STRING")
234 void passphrase_pyinit(void)
236 INITTYPE(pixie, root);
240 void passphrase_pyinsert(PyObject *mod)
242 INSERT("Pixie", pixie_pytype);
245 /*----- That's all, folks -------------------------------------------------*/