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)
44 static int convpixie(PyObject
*o
, void *p
)
46 if (!PIXIE_PYCHECK(o
))
48 *(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
)
78 static PyObject
*pixmeth_read(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
80 unsigned mode
= PMODE_READ
;
82 char *kwlist
[] = { "tag", "mode", 0 };
87 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O&:read", kwlist
,
88 &tag
, convuint
, &mode
))
90 r
= pixie_read(PIXIE_FD(me
), tag
, mode
, buf
, sizeof(buf
));
96 rc
= PyString_FromString(buf
);
101 static PyObject
*pixmeth_set(PyObject
*me
, PyObject
*arg
)
106 if (!PyArg_ParseTuple(arg
, "ss:set", &tag
, &phrase
))
108 pixie_set(PIXIE_FD(me
), tag
, phrase
);
112 static PyObject
*pixmeth_cancel(PyObject
*me
, PyObject
*arg
)
116 if (!PyArg_ParseTuple(arg
, "s:cancel", &tag
))
118 pixie_cancel(PIXIE_FD(me
), tag
);
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)")
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@ */
137 pixie_pydealloc
, /* @tp_dealloc@ */
139 0, /* @tp_getattr@ */
140 0, /* @tp_setattr@ */
141 0, /* @tp_compare@ */
143 0, /* @tp_as_number@ */
144 0, /* @tp_as_sequence@ */
145 0, /* @tp_as_mapping@ */
149 0, /* @tp_getattro@ */
150 0, /* @tp_setattro@ */
151 0, /* @tp_as_buffer@ */
152 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
156 "Passphrase pixie connection.",
158 0, /* @tp_traverse@ */
160 0, /* @tp_richcompare@ */
161 0, /* @tp_weaklistoffset@ */
163 0, /* @tp_iternexr@ */
164 pixie_pymethods
, /* @tp_methods@ */
165 0, /* @tp_members@ */
169 0, /* @tp_descr_get@ */
170 0, /* @tp_descr_set@ */
171 0, /* @tp_dictoffset@ */
173 PyType_GenericAlloc
, /* @tp_alloc@ */
174 pixie_pynew
, /* @tp_new@ */
175 _PyObject_Del
, /* @tp_free@ */
179 /*----- Main code ---------------------------------------------------------*/
181 static PyObject
*meth_ppread(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
184 unsigned f
= PMODE_READ
;
186 char *kwlist
[] = { "tag", "mode", 0 };
189 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s|O&:ppread", kwlist
,
192 if (passphrase_read(tag
, f
, buf
, sizeof(buf
)))
193 SYSERR("passphrase read failed");
194 rc
= PyString_FromString(buf
);
199 static PyObject
*meth_ppcancel(PyObject
*me
, PyObject
*arg
)
203 if (!PyArg_ParseTuple(arg
, "s:ppcancel", &tag
))
205 passphrase_cancel(tag
);
209 static PyObject
*meth_getpass(PyObject
*me
, PyObject
*arg
)
215 if (!PyArg_ParseTuple(arg
, "s:getpass", &prompt
))
217 if (pixie_getpass(prompt
, buf
, sizeof(buf
)))
219 rc
= PyString_FromString(buf
);
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")
233 void passphrase_pyinit(void)
235 INITTYPE(pixie
, root
);
239 void passphrase_pyinsert(PyObject
*mod
)
241 INSERT("Pixie", pixie_pytype
);
244 /*----- That's all, folks -------------------------------------------------*/