chiark / gitweb /
pubkey: Various stupid DSA bugs fixed.
[catacomb-python] / passphrase.c
CommitLineData
d7ab1bab 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
35typedef struct pixie_pyobj {
36 PyObject_HEAD
37 int fd;
38} pixie_pyobj;
39
40static PyTypeObject *pixie_pytype;
41#define PIXIE_PYCHECK(o) PyObject_TypeCheck((o), pixie_pytype)
42#define PIXIE_FD(o) (((pixie_pyobj *)(o))->fd)
43
3aa33042 44#ifdef WANT_CONVPIXIE
d7ab1bab 45static int convpixie(PyObject *o, void *p)
46{
47 if (!PIXIE_PYCHECK(o))
48 TYERR("want pixie");
49 *(int *)p = PIXIE_FD(o);
50 return (1);
51end:
52 return (0);
53}
3aa33042 54#endif
d7ab1bab 55
56static PyObject *pixie_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
57{
58 pixie_pyobj *rc = 0;
59 char *kwlist[] = { "socket", 0 };
60 char *sock = 0;
61 int fd;
62
63 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s:new", kwlist, &sock))
64 goto end;
65 if ((fd = pixie_open(sock)) < 0)
66 OSERR(sock);
67 rc = (pixie_pyobj *)ty->tp_alloc(ty, 0);
68 rc->fd = fd;
69end:
70 return ((PyObject *)rc);
71}
72
73static void pixie_pydealloc(PyObject *me)
74{
75 close(PIXIE_FD(me));
3aa33042 76 FREEOBJ(me);
d7ab1bab 77}
78
79static PyObject *pixmeth_read(PyObject *me, PyObject *arg, PyObject *kw)
80{
81 unsigned mode = PMODE_READ;
82 char *tag;
83 char *kwlist[] = { "tag", "mode", 0 };
84 PyObject *rc = 0;
85 int r;
86 char buf[1024];
87
88 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:read", kwlist,
89 &tag, convuint, &mode))
90 goto end;
91 r = pixie_read(PIXIE_FD(me), tag, mode, buf, sizeof(buf));
92 if (r < 0)
93 OSERR(0);
94 else if (r > 0)
95 RETURN_NONE;
96 else
97 rc = PyString_FromString(buf);
98end:
99 return (rc);
100}
101
102static PyObject *pixmeth_set(PyObject *me, PyObject *arg)
103{
104 char *tag;
105 char *phrase;
106
107 if (!PyArg_ParseTuple(arg, "ss:set", &tag, &phrase))
108 return (0);
109 pixie_set(PIXIE_FD(me), tag, phrase);
110 RETURN_ME;
111}
112
113static PyObject *pixmeth_cancel(PyObject *me, PyObject *arg)
114{
115 char *tag;
116
117 if (!PyArg_ParseTuple(arg, "s:cancel", &tag))
118 return (0);
119 pixie_cancel(PIXIE_FD(me), tag);
120 RETURN_ME;
121}
122
123static 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)")
128#undef METHNAME
129 { 0 }
130};
131
132static PyTypeObject pixie_pytype_skel = {
6d4db0bf 133 PyObject_HEAD_INIT(0) 0, /* Header */
d7ab1bab 134 "catacomb.Pixie", /* @tp_name@ */
135 sizeof(pixie_pyobj), /* @tp_basicsize@ */
136 0, /* @tp_itemsize@ */
137
138 pixie_pydealloc, /* @tp_dealloc@ */
139 0, /* @tp_print@ */
140 0, /* @tp_getattr@ */
141 0, /* @tp_setattr@ */
142 0, /* @tp_compare@ */
143 0, /* @tp_repr@ */
144 0, /* @tp_as_number@ */
145 0, /* @tp_as_sequence@ */
146 0, /* @tp_as_mapping@ */
147 0, /* @tp_hash@ */
148 0, /* @tp_call@ */
149 0, /* @tp_str@ */
150 0, /* @tp_getattro@ */
151 0, /* @tp_setattro@ */
152 0, /* @tp_as_buffer@ */
153 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
154 Py_TPFLAGS_BASETYPE,
155
156 /* @tp_doc@ */
157"Passphrase pixie connection.",
158
159 0, /* @tp_traverse@ */
160 0, /* @tp_clear@ */
161 0, /* @tp_richcompare@ */
162 0, /* @tp_weaklistoffset@ */
163 0, /* @tp_iter@ */
963a6148 164 0, /* @tp_iternext@ */
d7ab1bab 165 pixie_pymethods, /* @tp_methods@ */
166 0, /* @tp_members@ */
167 0, /* @tp_getset@ */
168 0, /* @tp_base@ */
169 0, /* @tp_dict@ */
170 0, /* @tp_descr_get@ */
171 0, /* @tp_descr_set@ */
172 0, /* @tp_dictoffset@ */
173 0, /* @tp_init@ */
174 PyType_GenericAlloc, /* @tp_alloc@ */
175 pixie_pynew, /* @tp_new@ */
3aa33042 176 0, /* @tp_free@ */
d7ab1bab 177 0 /* @tp_is_gc@ */
178};
179
180/*----- Main code ---------------------------------------------------------*/
181
182static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw)
183{
184 char *tag;
185 unsigned f = PMODE_READ;
186 PyObject *rc = 0;
187 char *kwlist[] = { "tag", "mode", 0 };
188 char buf[1024];
189
190 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", kwlist,
191 &tag, convuint, &f))
192 goto end;
193 if (passphrase_read(tag, f, buf, sizeof(buf)))
194 SYSERR("passphrase read failed");
195 rc = PyString_FromString(buf);
196end:
197 return (rc);
198}
199
200static PyObject *meth_ppcancel(PyObject *me, PyObject *arg)
201{
202 char *tag;
203
204 if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag))
205 return (0);
206 passphrase_cancel(tag);
207 RETURN_NONE;
208}
209
210static PyObject *meth_getpass(PyObject *me, PyObject *arg)
211{
212 char *prompt;
213 char buf[1024];
214 PyObject *rc = 0;
215
216 if (!PyArg_ParseTuple(arg, "s:getpass", &prompt))
217 goto end;
218 if (pixie_getpass(prompt, buf, sizeof(buf)))
219 OSERR(0);
220 rc = PyString_FromString(buf);
221end:
222 return (rc);
223}
224
225static 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")
230#undef METHNAME
231 { 0 }
232};
233
234void passphrase_pyinit(void)
235{
236 INITTYPE(pixie, root);
237 addmethods(methods);
238}
239
240void passphrase_pyinsert(PyObject *mod)
241{
242 INSERT("Pixie", pixie_pytype);
243}
244
245/*----- That's all, folks -------------------------------------------------*/