Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
d7ab1bab | 3 | * Reading and writing passphrases |
4 | * | |
5 | * (c) 2005 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
d7ab1bab | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
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. | |
b2687a0a | 16 | * |
d7ab1bab | 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. | |
b2687a0a | 21 | * |
d7ab1bab | 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. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
31 | /*----- Pixie stuff -------------------------------------------------------*/ | |
32 | ||
33 | typedef struct pixie_pyobj { | |
34 | PyObject_HEAD | |
35 | int fd; | |
36 | } pixie_pyobj; | |
37 | ||
38 | static PyTypeObject *pixie_pytype; | |
39 | #define PIXIE_PYCHECK(o) PyObject_TypeCheck((o), pixie_pytype) | |
40 | #define PIXIE_FD(o) (((pixie_pyobj *)(o))->fd) | |
41 | ||
3aa33042 | 42 | #ifdef WANT_CONVPIXIE |
d7ab1bab | 43 | static int convpixie(PyObject *o, void *p) |
44 | { | |
45 | if (!PIXIE_PYCHECK(o)) | |
46 | TYERR("want pixie"); | |
47 | *(int *)p = PIXIE_FD(o); | |
48 | return (1); | |
49 | end: | |
50 | return (0); | |
51 | } | |
3aa33042 | 52 | #endif |
d7ab1bab | 53 | |
54 | static PyObject *pixie_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
55 | { | |
56 | pixie_pyobj *rc = 0; | |
827f89d7 | 57 | static const char *const kwlist[] = { "socket", 0 }; |
d7ab1bab | 58 | char *sock = 0; |
59 | int fd; | |
60 | ||
827f89d7 | 61 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s:new", KWLIST, &sock)) |
d7ab1bab | 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)); | |
b2687a0a | 74 | FREEOBJ(me); |
d7ab1bab | 75 | } |
76 | ||
77 | static PyObject *pixmeth_read(PyObject *me, PyObject *arg, PyObject *kw) | |
78 | { | |
79 | unsigned mode = PMODE_READ; | |
80 | char *tag; | |
827f89d7 | 81 | static const char *const kwlist[] = { "tag", "mode", 0 }; |
d7ab1bab | 82 | PyObject *rc = 0; |
83 | int r; | |
84 | char buf[1024]; | |
85 | ||
827f89d7 | 86 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:read", KWLIST, |
d7ab1bab | 87 | &tag, convuint, &mode)) |
88 | goto end; | |
89 | r = pixie_read(PIXIE_FD(me), tag, mode, buf, sizeof(buf)); | |
90 | if (r < 0) | |
91 | OSERR(0); | |
92 | else if (r > 0) | |
93 | RETURN_NONE; | |
94 | else | |
2135a6d3 | 95 | rc = BIN_FROMSTR(buf); |
d7ab1bab | 96 | end: |
97 | return (rc); | |
98 | } | |
99 | ||
100 | static PyObject *pixmeth_set(PyObject *me, PyObject *arg) | |
101 | { | |
102 | char *tag; | |
103 | char *phrase; | |
104 | ||
2135a6d3 | 105 | if (!PyArg_ParseTuple(arg, "s"Y":set", &tag, &phrase)) |
d7ab1bab | 106 | return (0); |
107 | pixie_set(PIXIE_FD(me), tag, phrase); | |
108 | RETURN_ME; | |
109 | } | |
110 | ||
111 | static PyObject *pixmeth_cancel(PyObject *me, PyObject *arg) | |
112 | { | |
113 | char *tag; | |
114 | ||
115 | if (!PyArg_ParseTuple(arg, "s:cancel", &tag)) | |
116 | return (0); | |
117 | pixie_cancel(PIXIE_FD(me), tag); | |
118 | RETURN_ME; | |
119 | } | |
120 | ||
c90f712e | 121 | static const PyMethodDef pixie_pymethods[] = { |
d7ab1bab | 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)") | |
126 | #undef METHNAME | |
127 | { 0 } | |
128 | }; | |
129 | ||
c263b05c | 130 | static const PyTypeObject pixie_pytype_skel = { |
591bf41b | 131 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 132 | "Pixie", /* @tp_name@ */ |
d7ab1bab | 133 | sizeof(pixie_pyobj), /* @tp_basicsize@ */ |
134 | 0, /* @tp_itemsize@ */ | |
135 | ||
136 | pixie_pydealloc, /* @tp_dealloc@ */ | |
137 | 0, /* @tp_print@ */ | |
138 | 0, /* @tp_getattr@ */ | |
139 | 0, /* @tp_setattr@ */ | |
140 | 0, /* @tp_compare@ */ | |
141 | 0, /* @tp_repr@ */ | |
142 | 0, /* @tp_as_number@ */ | |
143 | 0, /* @tp_as_sequence@ */ | |
144 | 0, /* @tp_as_mapping@ */ | |
145 | 0, /* @tp_hash@ */ | |
146 | 0, /* @tp_call@ */ | |
147 | 0, /* @tp_str@ */ | |
148 | 0, /* @tp_getattro@ */ | |
149 | 0, /* @tp_setattro@ */ | |
150 | 0, /* @tp_as_buffer@ */ | |
151 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
152 | Py_TPFLAGS_BASETYPE, | |
153 | ||
154 | /* @tp_doc@ */ | |
d96c882e | 155 | "Pixie([socket = ?]): passphrase pixie connection.", |
d7ab1bab | 156 | |
157 | 0, /* @tp_traverse@ */ | |
158 | 0, /* @tp_clear@ */ | |
159 | 0, /* @tp_richcompare@ */ | |
160 | 0, /* @tp_weaklistoffset@ */ | |
161 | 0, /* @tp_iter@ */ | |
963a6148 | 162 | 0, /* @tp_iternext@ */ |
c90f712e | 163 | PYMETHODS(pixie), /* @tp_methods@ */ |
d7ab1bab | 164 | 0, /* @tp_members@ */ |
165 | 0, /* @tp_getset@ */ | |
166 | 0, /* @tp_base@ */ | |
167 | 0, /* @tp_dict@ */ | |
168 | 0, /* @tp_descr_get@ */ | |
169 | 0, /* @tp_descr_set@ */ | |
170 | 0, /* @tp_dictoffset@ */ | |
171 | 0, /* @tp_init@ */ | |
172 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
173 | pixie_pynew, /* @tp_new@ */ | |
3aa33042 | 174 | 0, /* @tp_free@ */ |
d7ab1bab | 175 | 0 /* @tp_is_gc@ */ |
176 | }; | |
177 | ||
178 | /*----- Main code ---------------------------------------------------------*/ | |
179 | ||
810542b0 MW |
180 | static const struct nameval consts[] = { |
181 | CONST(PMODE_READ), CONST(PMODE_VERIFY), | |
182 | { 0 } | |
183 | }; | |
184 | ||
d7ab1bab | 185 | static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw) |
186 | { | |
187 | char *tag; | |
188 | unsigned f = PMODE_READ; | |
189 | PyObject *rc = 0; | |
827f89d7 | 190 | static const char *const kwlist[] = { "tag", "mode", 0 }; |
d7ab1bab | 191 | char buf[1024]; |
192 | ||
827f89d7 | 193 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", KWLIST, |
d7ab1bab | 194 | &tag, convuint, &f)) |
195 | goto end; | |
196 | if (passphrase_read(tag, f, buf, sizeof(buf))) | |
197 | SYSERR("passphrase read failed"); | |
2135a6d3 | 198 | rc = BIN_FROMSTR(buf); |
d7ab1bab | 199 | end: |
200 | return (rc); | |
201 | } | |
202 | ||
203 | static PyObject *meth_ppcancel(PyObject *me, PyObject *arg) | |
204 | { | |
205 | char *tag; | |
206 | ||
207 | if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag)) | |
208 | return (0); | |
209 | passphrase_cancel(tag); | |
210 | RETURN_NONE; | |
211 | } | |
212 | ||
213 | static PyObject *meth_getpass(PyObject *me, PyObject *arg) | |
214 | { | |
215 | char *prompt; | |
216 | char buf[1024]; | |
217 | PyObject *rc = 0; | |
218 | ||
219 | if (!PyArg_ParseTuple(arg, "s:getpass", &prompt)) | |
220 | goto end; | |
221 | if (pixie_getpass(prompt, buf, sizeof(buf))) | |
222 | OSERR(0); | |
2135a6d3 | 223 | rc = BIN_FROMSTR(buf); |
d7ab1bab | 224 | end: |
225 | return (rc); | |
226 | } | |
227 | ||
c90f712e | 228 | static const PyMethodDef methods[] = { |
d7ab1bab | 229 | #define METHNAME(name) meth_##name |
d96c882e MW |
230 | KWMETH(ppread, "ppread(TAG, [mode = PMODE_READ]) -> STRING") |
231 | METH (ppcancel, "ppcancel(TAG)") | |
232 | METH (getpass, "getpass(PROMPT) -> STRING") | |
d7ab1bab | 233 | #undef METHNAME |
234 | { 0 } | |
235 | }; | |
236 | ||
237 | void passphrase_pyinit(void) | |
238 | { | |
239 | INITTYPE(pixie, root); | |
240 | addmethods(methods); | |
241 | } | |
242 | ||
243 | void passphrase_pyinsert(PyObject *mod) | |
244 | { | |
245 | INSERT("Pixie", pixie_pytype); | |
810542b0 | 246 | setconstants(mod, consts); |
d7ab1bab | 247 | } |
248 | ||
249 | /*----- That's all, folks -------------------------------------------------*/ |