1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <Python.h>
|
---|
20 | #include "includes.h"
|
---|
21 | #include "param/pyparam.h"
|
---|
22 | #include "auth/gensec/gensec.h"
|
---|
23 | #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
|
---|
24 | #include "auth/credentials/pycredentials.h"
|
---|
25 | #include "libcli/util/pyerrors.h"
|
---|
26 | #include "python/modules.h"
|
---|
27 | #include <pytalloc.h>
|
---|
28 | #include <tevent.h>
|
---|
29 | #include "librpc/rpc/pyrpc_util.h"
|
---|
30 |
|
---|
31 | static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
|
---|
32 | {
|
---|
33 | int type;
|
---|
34 | const char *name;
|
---|
35 | struct gensec_security *security;
|
---|
36 |
|
---|
37 | if (!PyArg_ParseTuple(args, "i", &type))
|
---|
38 | return NULL;
|
---|
39 |
|
---|
40 | security = pytalloc_get_type(self, struct gensec_security);
|
---|
41 |
|
---|
42 | name = gensec_get_name_by_authtype(security, type);
|
---|
43 | if (name == NULL)
|
---|
44 | Py_RETURN_NONE;
|
---|
45 |
|
---|
46 | return PyString_FromString(name);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
|
---|
50 | {
|
---|
51 | struct gensec_settings *s;
|
---|
52 | PyObject *py_hostname, *py_lp_ctx;
|
---|
53 |
|
---|
54 | if (!PyDict_Check(object)) {
|
---|
55 | PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | s = talloc_zero(mem_ctx, struct gensec_settings);
|
---|
60 | if (!s) return NULL;
|
---|
61 |
|
---|
62 | py_hostname = PyDict_GetItemString(object, "target_hostname");
|
---|
63 | if (!py_hostname) {
|
---|
64 | PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
|
---|
65 | return NULL;
|
---|
66 | }
|
---|
67 |
|
---|
68 | py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
|
---|
69 | if (!py_lp_ctx) {
|
---|
70 | PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
|
---|
71 | return NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | s->target_hostname = PyString_AsString(py_hostname);
|
---|
75 | s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx);
|
---|
76 | return s;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
---|
80 | {
|
---|
81 | NTSTATUS status;
|
---|
82 | PyObject *self;
|
---|
83 | struct gensec_settings *settings;
|
---|
84 | const char *kwnames[] = { "settings", NULL };
|
---|
85 | PyObject *py_settings = Py_None;
|
---|
86 | struct gensec_security *gensec;
|
---|
87 | TALLOC_CTX *frame;
|
---|
88 |
|
---|
89 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &py_settings))
|
---|
90 | return NULL;
|
---|
91 |
|
---|
92 | frame = talloc_stackframe();
|
---|
93 |
|
---|
94 | if (py_settings != Py_None) {
|
---|
95 | settings = settings_from_object(frame, py_settings);
|
---|
96 | if (settings == NULL) {
|
---|
97 | PyErr_NoMemory();
|
---|
98 | TALLOC_FREE(frame);
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 | } else {
|
---|
102 | settings = talloc_zero(frame, struct gensec_settings);
|
---|
103 | if (settings == NULL) {
|
---|
104 | PyErr_NoMemory();
|
---|
105 | TALLOC_FREE(frame);
|
---|
106 | return NULL;
|
---|
107 | }
|
---|
108 |
|
---|
109 | settings->lp_ctx = loadparm_init_global(true);
|
---|
110 | if (settings->lp_ctx == NULL) {
|
---|
111 | PyErr_NoMemory();
|
---|
112 | TALLOC_FREE(frame);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | status = gensec_init();
|
---|
118 | if (!NT_STATUS_IS_OK(status)) {
|
---|
119 | PyErr_SetNTSTATUS(status);
|
---|
120 | TALLOC_FREE(frame);
|
---|
121 | return NULL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | status = gensec_client_start(frame, &gensec, settings);
|
---|
125 | if (!NT_STATUS_IS_OK(status)) {
|
---|
126 | PyErr_SetNTSTATUS(status);
|
---|
127 | TALLOC_FREE(frame);
|
---|
128 | return NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | self = pytalloc_steal(type, gensec);
|
---|
132 | TALLOC_FREE(frame);
|
---|
133 |
|
---|
134 | return (PyObject *)self;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static PyObject *py_gensec_start_server(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
---|
138 | {
|
---|
139 | NTSTATUS status;
|
---|
140 | PyObject *self;
|
---|
141 | struct gensec_settings *settings = NULL;
|
---|
142 | const char *kwnames[] = { "settings", "auth_context", NULL };
|
---|
143 | PyObject *py_settings = Py_None;
|
---|
144 | PyObject *py_auth_context = Py_None;
|
---|
145 | struct gensec_security *gensec;
|
---|
146 | struct auth4_context *auth_context = NULL;
|
---|
147 | TALLOC_CTX *frame;
|
---|
148 |
|
---|
149 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", discard_const_p(char *, kwnames), &py_settings, &py_auth_context))
|
---|
150 | return NULL;
|
---|
151 |
|
---|
152 | frame = talloc_stackframe();
|
---|
153 |
|
---|
154 | if (py_settings != Py_None) {
|
---|
155 | settings = settings_from_object(frame, py_settings);
|
---|
156 | if (settings == NULL) {
|
---|
157 | PyErr_NoMemory();
|
---|
158 | TALLOC_FREE(frame);
|
---|
159 | return NULL;
|
---|
160 | }
|
---|
161 | } else {
|
---|
162 | settings = talloc_zero(frame, struct gensec_settings);
|
---|
163 | if (settings == NULL) {
|
---|
164 | PyErr_NoMemory();
|
---|
165 | TALLOC_FREE(frame);
|
---|
166 | return NULL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | settings->lp_ctx = loadparm_init_global(true);
|
---|
170 | if (settings->lp_ctx == NULL) {
|
---|
171 | PyErr_NoMemory();
|
---|
172 | TALLOC_FREE(frame);
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (py_auth_context != Py_None) {
|
---|
178 | auth_context = pytalloc_get_type(py_auth_context, struct auth4_context);
|
---|
179 | if (!auth_context) {
|
---|
180 | PyErr_Format(PyExc_TypeError,
|
---|
181 | "Expected auth.AuthContext for auth_context argument, got %s",
|
---|
182 | talloc_get_name(pytalloc_get_ptr(py_auth_context)));
|
---|
183 | return NULL;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | status = gensec_init();
|
---|
188 | if (!NT_STATUS_IS_OK(status)) {
|
---|
189 | PyErr_SetNTSTATUS(status);
|
---|
190 | TALLOC_FREE(frame);
|
---|
191 | return NULL;
|
---|
192 | }
|
---|
193 |
|
---|
194 | status = gensec_server_start(frame, settings, auth_context, &gensec);
|
---|
195 | if (!NT_STATUS_IS_OK(status)) {
|
---|
196 | PyErr_SetNTSTATUS(status);
|
---|
197 | TALLOC_FREE(frame);
|
---|
198 | return NULL;
|
---|
199 | }
|
---|
200 |
|
---|
201 | self = pytalloc_steal(type, gensec);
|
---|
202 | TALLOC_FREE(frame);
|
---|
203 |
|
---|
204 | return self;
|
---|
205 | }
|
---|
206 |
|
---|
207 | static PyObject *py_gensec_set_target_hostname(PyObject *self, PyObject *args)
|
---|
208 | {
|
---|
209 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
210 | char *target_hostname;
|
---|
211 | NTSTATUS status;
|
---|
212 |
|
---|
213 | if (!PyArg_ParseTuple(args, "s", &target_hostname))
|
---|
214 | return NULL;
|
---|
215 |
|
---|
216 | status = gensec_set_target_hostname(security, target_hostname);
|
---|
217 | if (!NT_STATUS_IS_OK(status)) {
|
---|
218 | PyErr_SetNTSTATUS(status);
|
---|
219 | return NULL;
|
---|
220 | }
|
---|
221 |
|
---|
222 | Py_RETURN_NONE;
|
---|
223 | }
|
---|
224 |
|
---|
225 | static PyObject *py_gensec_set_target_service(PyObject *self, PyObject *args)
|
---|
226 | {
|
---|
227 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
228 | char *target_service;
|
---|
229 | NTSTATUS status;
|
---|
230 |
|
---|
231 | if (!PyArg_ParseTuple(args, "s", &target_service))
|
---|
232 | return NULL;
|
---|
233 |
|
---|
234 | status = gensec_set_target_service(security, target_service);
|
---|
235 | if (!NT_STATUS_IS_OK(status)) {
|
---|
236 | PyErr_SetNTSTATUS(status);
|
---|
237 | return NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | Py_RETURN_NONE;
|
---|
241 | }
|
---|
242 |
|
---|
243 | static PyObject *py_gensec_set_credentials(PyObject *self, PyObject *args)
|
---|
244 | {
|
---|
245 | PyObject *py_creds = Py_None;
|
---|
246 | struct cli_credentials *creds;
|
---|
247 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
248 | NTSTATUS status;
|
---|
249 |
|
---|
250 | if (!PyArg_ParseTuple(args, "O", &py_creds))
|
---|
251 | return NULL;
|
---|
252 |
|
---|
253 | creds = PyCredentials_AsCliCredentials(py_creds);
|
---|
254 | if (!creds) {
|
---|
255 | PyErr_Format(PyExc_TypeError,
|
---|
256 | "Expected samba.credentaials for credentials argument got %s",
|
---|
257 | talloc_get_name(pytalloc_get_ptr(py_creds)));
|
---|
258 | }
|
---|
259 |
|
---|
260 | status = gensec_set_credentials(security, creds);
|
---|
261 | if (!NT_STATUS_IS_OK(status)) {
|
---|
262 | PyErr_SetNTSTATUS(status);
|
---|
263 | return NULL;
|
---|
264 | }
|
---|
265 |
|
---|
266 | Py_RETURN_NONE;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static PyObject *py_gensec_session_info(PyObject *self)
|
---|
270 | {
|
---|
271 | TALLOC_CTX *mem_ctx;
|
---|
272 | NTSTATUS status;
|
---|
273 | PyObject *py_session_info;
|
---|
274 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
275 | struct auth_session_info *info;
|
---|
276 | if (security->ops == NULL) {
|
---|
277 | PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
|
---|
278 | return NULL;
|
---|
279 | }
|
---|
280 | mem_ctx = talloc_new(NULL);
|
---|
281 |
|
---|
282 | status = gensec_session_info(security, mem_ctx, &info);
|
---|
283 | if (NT_STATUS_IS_ERR(status)) {
|
---|
284 | PyErr_SetNTSTATUS(status);
|
---|
285 | return NULL;
|
---|
286 | }
|
---|
287 |
|
---|
288 | py_session_info = py_return_ndr_struct("samba.dcerpc.auth", "session_info",
|
---|
289 | info, info);
|
---|
290 | talloc_free(mem_ctx);
|
---|
291 | return py_session_info;
|
---|
292 | }
|
---|
293 |
|
---|
294 | static PyObject *py_gensec_session_key(PyObject *self)
|
---|
295 | {
|
---|
296 | TALLOC_CTX *mem_ctx;
|
---|
297 | NTSTATUS status;
|
---|
298 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
299 | DATA_BLOB session_key = data_blob_null;
|
---|
300 | static PyObject *session_key_obj = NULL;
|
---|
301 |
|
---|
302 | if (security->ops == NULL) {
|
---|
303 | PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
|
---|
304 | return NULL;
|
---|
305 | }
|
---|
306 | mem_ctx = talloc_new(NULL);
|
---|
307 |
|
---|
308 | status = gensec_session_key(security, mem_ctx, &session_key);
|
---|
309 | if (!NT_STATUS_IS_OK(status)) {
|
---|
310 | talloc_free(mem_ctx);
|
---|
311 | PyErr_SetNTSTATUS(status);
|
---|
312 | return NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | session_key_obj = PyString_FromStringAndSize((const char *)session_key.data,
|
---|
316 | session_key.length);
|
---|
317 | talloc_free(mem_ctx);
|
---|
318 | return session_key_obj;
|
---|
319 | }
|
---|
320 |
|
---|
321 | static PyObject *py_gensec_start_mech_by_name(PyObject *self, PyObject *args)
|
---|
322 | {
|
---|
323 | char *name;
|
---|
324 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
325 | NTSTATUS status;
|
---|
326 |
|
---|
327 | if (!PyArg_ParseTuple(args, "s", &name))
|
---|
328 | return NULL;
|
---|
329 |
|
---|
330 | status = gensec_start_mech_by_name(security, name);
|
---|
331 | if (!NT_STATUS_IS_OK(status)) {
|
---|
332 | PyErr_SetNTSTATUS(status);
|
---|
333 | return NULL;
|
---|
334 | }
|
---|
335 |
|
---|
336 | Py_RETURN_NONE;
|
---|
337 | }
|
---|
338 |
|
---|
339 | static PyObject *py_gensec_start_mech_by_sasl_name(PyObject *self, PyObject *args)
|
---|
340 | {
|
---|
341 | char *sasl_name;
|
---|
342 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
343 | NTSTATUS status;
|
---|
344 |
|
---|
345 | if (!PyArg_ParseTuple(args, "s", &sasl_name))
|
---|
346 | return NULL;
|
---|
347 |
|
---|
348 | status = gensec_start_mech_by_sasl_name(security, sasl_name);
|
---|
349 | if (!NT_STATUS_IS_OK(status)) {
|
---|
350 | PyErr_SetNTSTATUS(status);
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 |
|
---|
354 | Py_RETURN_NONE;
|
---|
355 | }
|
---|
356 |
|
---|
357 | static PyObject *py_gensec_start_mech_by_authtype(PyObject *self, PyObject *args)
|
---|
358 | {
|
---|
359 | int authtype, level;
|
---|
360 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
361 | NTSTATUS status;
|
---|
362 | if (!PyArg_ParseTuple(args, "ii", &authtype, &level))
|
---|
363 | return NULL;
|
---|
364 |
|
---|
365 | status = gensec_start_mech_by_authtype(security, authtype, level);
|
---|
366 | if (!NT_STATUS_IS_OK(status)) {
|
---|
367 | PyErr_SetNTSTATUS(status);
|
---|
368 | return NULL;
|
---|
369 | }
|
---|
370 |
|
---|
371 | Py_RETURN_NONE;
|
---|
372 | }
|
---|
373 |
|
---|
374 | static PyObject *py_gensec_want_feature(PyObject *self, PyObject *args)
|
---|
375 | {
|
---|
376 | int feature;
|
---|
377 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
378 | /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
|
---|
379 | if (!PyArg_ParseTuple(args, "i", &feature))
|
---|
380 | return NULL;
|
---|
381 |
|
---|
382 | gensec_want_feature(security, feature);
|
---|
383 |
|
---|
384 | Py_RETURN_NONE;
|
---|
385 | }
|
---|
386 |
|
---|
387 | static PyObject *py_gensec_have_feature(PyObject *self, PyObject *args)
|
---|
388 | {
|
---|
389 | int feature;
|
---|
390 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
391 | /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
|
---|
392 | if (!PyArg_ParseTuple(args, "i", &feature))
|
---|
393 | return NULL;
|
---|
394 |
|
---|
395 | if (gensec_have_feature(security, feature)) {
|
---|
396 | return Py_True;
|
---|
397 | }
|
---|
398 | return Py_False;
|
---|
399 | }
|
---|
400 |
|
---|
401 | static PyObject *py_gensec_set_max_update_size(PyObject *self, PyObject *args)
|
---|
402 | {
|
---|
403 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
404 | unsigned int max_update_size = 0;
|
---|
405 |
|
---|
406 | if (!PyArg_ParseTuple(args, "I", &max_update_size))
|
---|
407 | return NULL;
|
---|
408 |
|
---|
409 | gensec_set_max_update_size(security, max_update_size);
|
---|
410 |
|
---|
411 | Py_RETURN_NONE;
|
---|
412 | }
|
---|
413 |
|
---|
414 | static PyObject *py_gensec_max_update_size(PyObject *self)
|
---|
415 | {
|
---|
416 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
417 | unsigned int max_update_size = gensec_max_update_size(security);
|
---|
418 |
|
---|
419 | return PyInt_FromLong(max_update_size);
|
---|
420 | }
|
---|
421 |
|
---|
422 | static PyObject *py_gensec_update(PyObject *self, PyObject *args)
|
---|
423 | {
|
---|
424 | NTSTATUS status;
|
---|
425 | TALLOC_CTX *mem_ctx;
|
---|
426 | DATA_BLOB in, out;
|
---|
427 | PyObject *ret, *py_in;
|
---|
428 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
429 | PyObject *finished_processing;
|
---|
430 |
|
---|
431 | if (!PyArg_ParseTuple(args, "O", &py_in))
|
---|
432 | return NULL;
|
---|
433 |
|
---|
434 | mem_ctx = talloc_new(NULL);
|
---|
435 |
|
---|
436 | if (!PyString_Check(py_in)) {
|
---|
437 | PyErr_Format(PyExc_TypeError, "expected a string");
|
---|
438 | return NULL;
|
---|
439 | }
|
---|
440 |
|
---|
441 | in.data = (uint8_t *)PyString_AsString(py_in);
|
---|
442 | in.length = PyString_Size(py_in);
|
---|
443 |
|
---|
444 | status = gensec_update(security, mem_ctx, in, &out);
|
---|
445 |
|
---|
446 | if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)
|
---|
447 | && !NT_STATUS_IS_OK(status)) {
|
---|
448 | PyErr_SetNTSTATUS(status);
|
---|
449 | talloc_free(mem_ctx);
|
---|
450 | return NULL;
|
---|
451 | }
|
---|
452 | ret = PyString_FromStringAndSize((const char *)out.data, out.length);
|
---|
453 | talloc_free(mem_ctx);
|
---|
454 |
|
---|
455 | if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
|
---|
456 | finished_processing = Py_False;
|
---|
457 | } else {
|
---|
458 | finished_processing = Py_True;
|
---|
459 | }
|
---|
460 |
|
---|
461 | return PyTuple_Pack(2, finished_processing, ret);
|
---|
462 | }
|
---|
463 |
|
---|
464 | static PyObject *py_gensec_wrap(PyObject *self, PyObject *args)
|
---|
465 | {
|
---|
466 | NTSTATUS status;
|
---|
467 |
|
---|
468 | TALLOC_CTX *mem_ctx;
|
---|
469 | DATA_BLOB in, out;
|
---|
470 | PyObject *ret, *py_in;
|
---|
471 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
472 |
|
---|
473 | if (!PyArg_ParseTuple(args, "O", &py_in))
|
---|
474 | return NULL;
|
---|
475 |
|
---|
476 | mem_ctx = talloc_new(NULL);
|
---|
477 |
|
---|
478 | if (!PyString_Check(py_in)) {
|
---|
479 | PyErr_Format(PyExc_TypeError, "expected a string");
|
---|
480 | return NULL;
|
---|
481 | }
|
---|
482 | in.data = (uint8_t *)PyString_AsString(py_in);
|
---|
483 | in.length = PyString_Size(py_in);
|
---|
484 |
|
---|
485 | status = gensec_wrap(security, mem_ctx, &in, &out);
|
---|
486 |
|
---|
487 | if (!NT_STATUS_IS_OK(status)) {
|
---|
488 | PyErr_SetNTSTATUS(status);
|
---|
489 | talloc_free(mem_ctx);
|
---|
490 | return NULL;
|
---|
491 | }
|
---|
492 |
|
---|
493 | ret = PyString_FromStringAndSize((const char *)out.data, out.length);
|
---|
494 | talloc_free(mem_ctx);
|
---|
495 | return ret;
|
---|
496 | }
|
---|
497 |
|
---|
498 | static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args)
|
---|
499 | {
|
---|
500 | NTSTATUS status;
|
---|
501 |
|
---|
502 | TALLOC_CTX *mem_ctx;
|
---|
503 | DATA_BLOB in, out;
|
---|
504 | PyObject *ret, *py_in;
|
---|
505 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
506 |
|
---|
507 | if (!PyArg_ParseTuple(args, "O", &py_in))
|
---|
508 | return NULL;
|
---|
509 |
|
---|
510 | mem_ctx = talloc_new(NULL);
|
---|
511 |
|
---|
512 | if (!PyString_Check(py_in)) {
|
---|
513 | PyErr_Format(PyExc_TypeError, "expected a string");
|
---|
514 | return NULL;
|
---|
515 | }
|
---|
516 |
|
---|
517 | in.data = (uint8_t *)PyString_AsString(py_in);
|
---|
518 | in.length = PyString_Size(py_in);
|
---|
519 |
|
---|
520 | status = gensec_unwrap(security, mem_ctx, &in, &out);
|
---|
521 |
|
---|
522 | if (!NT_STATUS_IS_OK(status)) {
|
---|
523 | PyErr_SetNTSTATUS(status);
|
---|
524 | talloc_free(mem_ctx);
|
---|
525 | return NULL;
|
---|
526 | }
|
---|
527 |
|
---|
528 | ret = PyString_FromStringAndSize((const char *)out.data, out.length);
|
---|
529 | talloc_free(mem_ctx);
|
---|
530 | return ret;
|
---|
531 | }
|
---|
532 |
|
---|
533 | static PyObject *py_gensec_sig_size(PyObject *self, PyObject *args)
|
---|
534 | {
|
---|
535 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
536 | Py_ssize_t data_size = 0;
|
---|
537 | size_t sig_size = 0;
|
---|
538 |
|
---|
539 | if (!PyArg_ParseTuple(args, "n", &data_size)) {
|
---|
540 | return NULL;
|
---|
541 | }
|
---|
542 |
|
---|
543 | sig_size = gensec_sig_size(security, data_size);
|
---|
544 |
|
---|
545 | return PyLong_FromSize_t(sig_size);
|
---|
546 | }
|
---|
547 |
|
---|
548 | static PyObject *py_gensec_sign_packet(PyObject *self, PyObject *args)
|
---|
549 | {
|
---|
550 | NTSTATUS status;
|
---|
551 | TALLOC_CTX *mem_ctx = NULL;
|
---|
552 | Py_ssize_t data_length = 0;
|
---|
553 | Py_ssize_t pdu_length = 0;
|
---|
554 | DATA_BLOB data, pdu, sig;
|
---|
555 | PyObject *py_sig;
|
---|
556 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
557 |
|
---|
558 | if (!PyArg_ParseTuple(args, "z#z#", &data.data, &data_length, &pdu.data, &pdu_length)) {
|
---|
559 | return NULL;
|
---|
560 | }
|
---|
561 | data.length = data_length;
|
---|
562 | pdu.length = pdu_length;
|
---|
563 |
|
---|
564 | mem_ctx = talloc_new(NULL);
|
---|
565 |
|
---|
566 | status = gensec_sign_packet(security, mem_ctx,
|
---|
567 | data.data, data.length,
|
---|
568 | pdu.data, pdu.length, &sig);
|
---|
569 | if (!NT_STATUS_IS_OK(status)) {
|
---|
570 | PyErr_SetNTSTATUS(status);
|
---|
571 | talloc_free(mem_ctx);
|
---|
572 | return NULL;
|
---|
573 | }
|
---|
574 |
|
---|
575 | py_sig = PyBytes_FromStringAndSize((const char *)sig.data, sig.length);
|
---|
576 | talloc_free(mem_ctx);
|
---|
577 | return py_sig;
|
---|
578 | }
|
---|
579 |
|
---|
580 | static PyObject *py_gensec_check_packet(PyObject *self, PyObject *args)
|
---|
581 | {
|
---|
582 | NTSTATUS status;
|
---|
583 | Py_ssize_t data_length = 0;
|
---|
584 | Py_ssize_t pdu_length = 0;
|
---|
585 | Py_ssize_t sig_length = 0;
|
---|
586 | DATA_BLOB data, pdu, sig;
|
---|
587 | struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
|
---|
588 |
|
---|
589 | if (!PyArg_ParseTuple(args, "z#z#z#",
|
---|
590 | &data.data, &data_length,
|
---|
591 | &pdu.data, &pdu_length,
|
---|
592 | &sig.data, &sig_length)) {
|
---|
593 | return NULL;
|
---|
594 | }
|
---|
595 | data.length = data_length;
|
---|
596 | pdu.length = pdu_length;
|
---|
597 | sig.length = sig_length;
|
---|
598 |
|
---|
599 | status = gensec_check_packet(security,
|
---|
600 | data.data, data.length,
|
---|
601 | pdu.data, pdu.length, &sig);
|
---|
602 | if (!NT_STATUS_IS_OK(status)) {
|
---|
603 | PyErr_SetNTSTATUS(status);
|
---|
604 | return NULL;
|
---|
605 | }
|
---|
606 |
|
---|
607 | Py_RETURN_NONE;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static PyMethodDef py_gensec_security_methods[] = {
|
---|
611 | { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
|
---|
612 | "S.start_client(settings) -> gensec" },
|
---|
613 | { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
|
---|
614 | "S.start_server(auth_ctx, settings) -> gensec" },
|
---|
615 | { "set_credentials", (PyCFunction)py_gensec_set_credentials, METH_VARARGS,
|
---|
616 | "S.start_client(credentials)" },
|
---|
617 | { "set_target_hostname", (PyCFunction)py_gensec_set_target_hostname, METH_VARARGS,
|
---|
618 | "S.start_target_hostname(target_hostname)" },
|
---|
619 | { "set_target_service", (PyCFunction)py_gensec_set_target_service, METH_VARARGS,
|
---|
620 | "S.start_target_service(target_service)" },
|
---|
621 | { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
|
---|
622 | "S.session_info() -> info" },
|
---|
623 | { "session_key", (PyCFunction)py_gensec_session_key, METH_NOARGS,
|
---|
624 | "S.session_key() -> key" },
|
---|
625 | { "start_mech_by_name", (PyCFunction)py_gensec_start_mech_by_name, METH_VARARGS,
|
---|
626 | "S.start_mech_by_name(name)" },
|
---|
627 | { "start_mech_by_sasl_name", (PyCFunction)py_gensec_start_mech_by_sasl_name, METH_VARARGS,
|
---|
628 | "S.start_mech_by_sasl_name(name)" },
|
---|
629 | { "start_mech_by_authtype", (PyCFunction)py_gensec_start_mech_by_authtype, METH_VARARGS,
|
---|
630 | "S.start_mech_by_authtype(authtype, level)" },
|
---|
631 | { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
|
---|
632 | "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
|
---|
633 | { "want_feature", (PyCFunction)py_gensec_want_feature, METH_VARARGS,
|
---|
634 | "S.want_feature(feature)\n Request that GENSEC negotiate a particular feature." },
|
---|
635 | { "have_feature", (PyCFunction)py_gensec_have_feature, METH_VARARGS,
|
---|
636 | "S.have_feature()\n Return True if GENSEC negotiated a particular feature." },
|
---|
637 | { "set_max_update_size", (PyCFunction)py_gensec_set_max_update_size, METH_VARARGS,
|
---|
638 | "S.set_max_update_size(max_size) \n Some mechs can fragment update packets, needs to be use before the mech is started." },
|
---|
639 | { "max_update_size", (PyCFunction)py_gensec_max_update_size, 0,
|
---|
640 | "S.max_update_size() \n Return the current max_update_size." },
|
---|
641 | { "update", (PyCFunction)py_gensec_update, METH_VARARGS,
|
---|
642 | "S.update(blob_in) -> (finished, blob_out)\nPerform one step in a GENSEC dance. Repeat with new packets until finished is true or exception." },
|
---|
643 | { "wrap", (PyCFunction)py_gensec_wrap, METH_VARARGS,
|
---|
644 | "S.wrap(blob_in) -> blob_out\nPackage one clear packet into a wrapped GENSEC packet." },
|
---|
645 | { "unwrap", (PyCFunction)py_gensec_unwrap, METH_VARARGS,
|
---|
646 | "S.unwrap(blob_in) -> blob_out\nPerform one wrapped GENSEC packet into a clear packet." },
|
---|
647 | { "sig_size", (PyCFunction)py_gensec_sig_size, METH_VARARGS,
|
---|
648 | "S.sig_size(data_size) -> sig_size\nSize of the DCERPC packet signature" },
|
---|
649 | { "sign_packet", (PyCFunction)py_gensec_sign_packet, METH_VARARGS,
|
---|
650 | "S.sign_packet(data, whole_pdu) -> sig\nSign a DCERPC packet." },
|
---|
651 | { "check_packet", (PyCFunction)py_gensec_check_packet, METH_VARARGS,
|
---|
652 | "S.check_packet(data, whole_pdu, sig)\nCheck a DCERPC packet." },
|
---|
653 | { NULL }
|
---|
654 | };
|
---|
655 |
|
---|
656 | static PyTypeObject Py_Security = {
|
---|
657 | .tp_name = "gensec.Security",
|
---|
658 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
659 | .tp_methods = py_gensec_security_methods,
|
---|
660 | };
|
---|
661 |
|
---|
662 | void initgensec(void);
|
---|
663 | void initgensec(void)
|
---|
664 | {
|
---|
665 | PyObject *m;
|
---|
666 |
|
---|
667 | if (pytalloc_BaseObject_PyType_Ready(&Py_Security) < 0)
|
---|
668 | return;
|
---|
669 |
|
---|
670 | m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
|
---|
671 | if (m == NULL)
|
---|
672 | return;
|
---|
673 |
|
---|
674 | PyModule_AddObject(m, "FEATURE_SESSION_KEY", PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY));
|
---|
675 | PyModule_AddObject(m, "FEATURE_SIGN", PyInt_FromLong(GENSEC_FEATURE_SIGN));
|
---|
676 | PyModule_AddObject(m, "FEATURE_SEAL", PyInt_FromLong(GENSEC_FEATURE_SEAL));
|
---|
677 | PyModule_AddObject(m, "FEATURE_DCE_STYLE", PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE));
|
---|
678 | PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES", PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
|
---|
679 | PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE", PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
|
---|
680 | PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
|
---|
681 | PyModule_AddObject(m, "FEATURE_NEW_SPNEGO", PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
|
---|
682 |
|
---|
683 | Py_INCREF(&Py_Security);
|
---|
684 | PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);
|
---|
685 | }
|
---|