d7ab1bab |
1 | /* -*-c-*- |
d7ab1bab |
2 | * |
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 |
95 | rc = PyString_FromString(buf); |
96 | end: |
97 | return (rc); |
98 | } |
99 | |
100 | static PyObject *pixmeth_set(PyObject *me, PyObject *arg) |
101 | { |
102 | char *tag; |
103 | char *phrase; |
104 | |
105 | if (!PyArg_ParseTuple(arg, "ss:set", &tag, &phrase)) |
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 | |
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)") |
126 | #undef METHNAME |
127 | { 0 } |
128 | }; |
129 | |
130 | static PyTypeObject pixie_pytype_skel = { |
6d4db0bf |
131 | PyObject_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@ */ |
06cd26e8 |
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@ */ |
d7ab1bab |
163 | pixie_pymethods, /* @tp_methods@ */ |
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 | |
180 | static PyObject *meth_ppread(PyObject *me, PyObject *arg, PyObject *kw) |
181 | { |
182 | char *tag; |
183 | unsigned f = PMODE_READ; |
184 | PyObject *rc = 0; |
827f89d7 |
185 | static const char *const kwlist[] = { "tag", "mode", 0 }; |
d7ab1bab |
186 | char buf[1024]; |
187 | |
827f89d7 |
188 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:ppread", KWLIST, |
d7ab1bab |
189 | &tag, convuint, &f)) |
190 | goto end; |
191 | if (passphrase_read(tag, f, buf, sizeof(buf))) |
192 | SYSERR("passphrase read failed"); |
193 | rc = PyString_FromString(buf); |
194 | end: |
195 | return (rc); |
196 | } |
197 | |
198 | static PyObject *meth_ppcancel(PyObject *me, PyObject *arg) |
199 | { |
200 | char *tag; |
201 | |
202 | if (!PyArg_ParseTuple(arg, "s:ppcancel", &tag)) |
203 | return (0); |
204 | passphrase_cancel(tag); |
205 | RETURN_NONE; |
206 | } |
207 | |
208 | static PyObject *meth_getpass(PyObject *me, PyObject *arg) |
209 | { |
210 | char *prompt; |
211 | char buf[1024]; |
212 | PyObject *rc = 0; |
213 | |
214 | if (!PyArg_ParseTuple(arg, "s:getpass", &prompt)) |
215 | goto end; |
216 | if (pixie_getpass(prompt, buf, sizeof(buf))) |
217 | OSERR(0); |
218 | rc = PyString_FromString(buf); |
219 | end: |
220 | return (rc); |
221 | } |
222 | |
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") |
228 | #undef METHNAME |
229 | { 0 } |
230 | }; |
231 | |
232 | void passphrase_pyinit(void) |
233 | { |
234 | INITTYPE(pixie, root); |
235 | addmethods(methods); |
236 | } |
237 | |
238 | void passphrase_pyinsert(PyObject *mod) |
239 | { |
240 | INSERT("Pixie", pixie_pytype); |
241 | } |
242 | |
243 | /*----- That's all, folks -------------------------------------------------*/ |