2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
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.
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.
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/>.
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"
29 #include "librpc/rpc/pyrpc_util.h"
31 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
35 struct gensec_security *security;
37 if (!PyArg_ParseTuple(args, "i", &type))
40 security = pytalloc_get_type(self, struct gensec_security);
42 name = gensec_get_name_by_authtype(security, type);
46 return PyString_FromString(name);
49 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
51 struct gensec_settings *s;
52 PyObject *py_hostname, *py_lp_ctx;
54 if (!PyDict_Check(object)) {
55 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
59 s = talloc_zero(mem_ctx, struct gensec_settings);
62 py_hostname = PyDict_GetItemString(object, "target_hostname");
64 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
68 py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
70 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
74 s->target_hostname = PyString_AsString(py_hostname);
75 s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx);
79 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
83 struct gensec_settings *settings;
84 const char *kwnames[] = { "settings", NULL };
85 PyObject *py_settings = Py_None;
86 struct gensec_security *gensec;
89 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &py_settings))
92 frame = talloc_stackframe();
94 if (py_settings != Py_None) {
95 settings = settings_from_object(frame, py_settings);
96 if (settings == NULL) {
102 settings = talloc_zero(frame, struct gensec_settings);
103 if (settings == NULL) {
109 settings->lp_ctx = loadparm_init_global(true);
110 if (settings->lp_ctx == NULL) {
117 status = gensec_init();
118 if (!NT_STATUS_IS_OK(status)) {
119 PyErr_SetNTSTATUS(status);
124 status = gensec_client_start(frame, &gensec, settings);
125 if (!NT_STATUS_IS_OK(status)) {
126 PyErr_SetNTSTATUS(status);
131 self = pytalloc_steal(type, gensec);
134 return (PyObject *)self;
137 static PyObject *py_gensec_start_server(PyTypeObject *type, PyObject *args, PyObject *kwargs)
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;
149 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", discard_const_p(char *, kwnames), &py_settings, &py_auth_context))
152 frame = talloc_stackframe();
154 if (py_settings != Py_None) {
155 settings = settings_from_object(frame, py_settings);
156 if (settings == NULL) {
162 settings = talloc_zero(frame, struct gensec_settings);
163 if (settings == NULL) {
169 settings->lp_ctx = loadparm_init_global(true);
170 if (settings->lp_ctx == NULL) {
177 if (py_auth_context != Py_None) {
178 auth_context = pytalloc_get_type(py_auth_context, struct auth4_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)));
187 status = gensec_init();
188 if (!NT_STATUS_IS_OK(status)) {
189 PyErr_SetNTSTATUS(status);
194 status = gensec_server_start(frame, settings, auth_context, &gensec);
195 if (!NT_STATUS_IS_OK(status)) {
196 PyErr_SetNTSTATUS(status);
201 self = pytalloc_steal(type, gensec);
207 static PyObject *py_gensec_set_target_hostname(PyObject *self, PyObject *args)
209 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
210 char *target_hostname;
213 if (!PyArg_ParseTuple(args, "s", &target_hostname))
216 status = gensec_set_target_hostname(security, target_hostname);
217 if (!NT_STATUS_IS_OK(status)) {
218 PyErr_SetNTSTATUS(status);
225 static PyObject *py_gensec_set_target_service(PyObject *self, PyObject *args)
227 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
228 char *target_service;
231 if (!PyArg_ParseTuple(args, "s", &target_service))
234 status = gensec_set_target_service(security, target_service);
235 if (!NT_STATUS_IS_OK(status)) {
236 PyErr_SetNTSTATUS(status);
243 static PyObject *py_gensec_set_credentials(PyObject *self, PyObject *args)
245 PyObject *py_creds = Py_None;
246 struct cli_credentials *creds;
247 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
250 if (!PyArg_ParseTuple(args, "O", &py_creds))
253 creds = PyCredentials_AsCliCredentials(py_creds);
255 PyErr_Format(PyExc_TypeError,
256 "Expected samba.credentaials for credentials argument got %s",
257 talloc_get_name(pytalloc_get_ptr(py_creds)));
261 status = gensec_set_credentials(security, creds);
262 if (!NT_STATUS_IS_OK(status)) {
263 PyErr_SetNTSTATUS(status);
270 static PyObject *py_gensec_session_info(PyObject *self)
274 PyObject *py_session_info;
275 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
276 struct auth_session_info *info;
277 if (security->ops == NULL) {
278 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
281 mem_ctx = talloc_new(NULL);
283 status = gensec_session_info(security, mem_ctx, &info);
284 if (NT_STATUS_IS_ERR(status)) {
285 PyErr_SetNTSTATUS(status);
289 py_session_info = py_return_ndr_struct("samba.dcerpc.auth", "session_info",
291 talloc_free(mem_ctx);
292 return py_session_info;
295 static PyObject *py_gensec_session_key(PyObject *self)
299 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
300 DATA_BLOB session_key = data_blob_null;
301 static PyObject *session_key_obj = NULL;
303 if (security->ops == NULL) {
304 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
307 mem_ctx = talloc_new(NULL);
309 status = gensec_session_key(security, mem_ctx, &session_key);
310 if (!NT_STATUS_IS_OK(status)) {
311 talloc_free(mem_ctx);
312 PyErr_SetNTSTATUS(status);
316 session_key_obj = PyString_FromStringAndSize((const char *)session_key.data,
318 talloc_free(mem_ctx);
319 return session_key_obj;
322 static PyObject *py_gensec_start_mech_by_name(PyObject *self, PyObject *args)
325 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
328 if (!PyArg_ParseTuple(args, "s", &name))
331 status = gensec_start_mech_by_name(security, name);
332 if (!NT_STATUS_IS_OK(status)) {
333 PyErr_SetNTSTATUS(status);
340 static PyObject *py_gensec_start_mech_by_sasl_name(PyObject *self, PyObject *args)
343 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
346 if (!PyArg_ParseTuple(args, "s", &sasl_name))
349 status = gensec_start_mech_by_sasl_name(security, sasl_name);
350 if (!NT_STATUS_IS_OK(status)) {
351 PyErr_SetNTSTATUS(status);
358 static PyObject *py_gensec_start_mech_by_authtype(PyObject *self, PyObject *args)
361 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
363 if (!PyArg_ParseTuple(args, "ii", &authtype, &level))
366 status = gensec_start_mech_by_authtype(security, authtype, level);
367 if (!NT_STATUS_IS_OK(status)) {
368 PyErr_SetNTSTATUS(status);
375 static PyObject *py_gensec_want_feature(PyObject *self, PyObject *args)
378 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
379 /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
380 if (!PyArg_ParseTuple(args, "i", &feature))
383 gensec_want_feature(security, feature);
388 static PyObject *py_gensec_have_feature(PyObject *self, PyObject *args)
391 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
392 /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
393 if (!PyArg_ParseTuple(args, "i", &feature))
396 if (gensec_have_feature(security, feature)) {
402 static PyObject *py_gensec_set_max_update_size(PyObject *self, PyObject *args)
404 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
405 unsigned int max_update_size = 0;
407 if (!PyArg_ParseTuple(args, "I", &max_update_size))
410 gensec_set_max_update_size(security, max_update_size);
415 static PyObject *py_gensec_max_update_size(PyObject *self)
417 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
418 unsigned int max_update_size = gensec_max_update_size(security);
420 return PyInt_FromLong(max_update_size);
423 static PyObject *py_gensec_update(PyObject *self, PyObject *args)
428 PyObject *ret, *py_in;
429 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
430 PyObject *finished_processing;
432 if (!PyArg_ParseTuple(args, "O", &py_in))
435 mem_ctx = talloc_new(NULL);
437 if (!PyString_Check(py_in)) {
438 PyErr_Format(PyExc_TypeError, "expected a string");
442 in.data = (uint8_t *)PyString_AsString(py_in);
443 in.length = PyString_Size(py_in);
445 status = gensec_update(security, mem_ctx, in, &out);
447 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)
448 && !NT_STATUS_IS_OK(status)) {
449 PyErr_SetNTSTATUS(status);
450 talloc_free(mem_ctx);
453 ret = PyString_FromStringAndSize((const char *)out.data, out.length);
454 talloc_free(mem_ctx);
456 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
457 finished_processing = Py_False;
459 finished_processing = Py_True;
462 return PyTuple_Pack(2, finished_processing, ret);
465 static PyObject *py_gensec_wrap(PyObject *self, PyObject *args)
471 PyObject *ret, *py_in;
472 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
474 if (!PyArg_ParseTuple(args, "O", &py_in))
477 mem_ctx = talloc_new(NULL);
479 if (!PyString_Check(py_in)) {
480 PyErr_Format(PyExc_TypeError, "expected a string");
483 in.data = (uint8_t *)PyString_AsString(py_in);
484 in.length = PyString_Size(py_in);
486 status = gensec_wrap(security, mem_ctx, &in, &out);
488 if (!NT_STATUS_IS_OK(status)) {
489 PyErr_SetNTSTATUS(status);
490 talloc_free(mem_ctx);
494 ret = PyString_FromStringAndSize((const char *)out.data, out.length);
495 talloc_free(mem_ctx);
499 static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args)
505 PyObject *ret, *py_in;
506 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
508 if (!PyArg_ParseTuple(args, "O", &py_in))
511 mem_ctx = talloc_new(NULL);
513 if (!PyString_Check(py_in)) {
514 PyErr_Format(PyExc_TypeError, "expected a string");
518 in.data = (uint8_t *)PyString_AsString(py_in);
519 in.length = PyString_Size(py_in);
521 status = gensec_unwrap(security, mem_ctx, &in, &out);
523 if (!NT_STATUS_IS_OK(status)) {
524 PyErr_SetNTSTATUS(status);
525 talloc_free(mem_ctx);
529 ret = PyString_FromStringAndSize((const char *)out.data, out.length);
530 talloc_free(mem_ctx);
534 static PyObject *py_gensec_sig_size(PyObject *self, PyObject *args)
536 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
537 Py_ssize_t data_size = 0;
540 if (!PyArg_ParseTuple(args, "n", &data_size)) {
544 sig_size = gensec_sig_size(security, data_size);
546 return PyLong_FromSize_t(sig_size);
549 static PyObject *py_gensec_sign_packet(PyObject *self, PyObject *args)
552 TALLOC_CTX *mem_ctx = NULL;
553 Py_ssize_t data_length = 0;
554 Py_ssize_t pdu_length = 0;
555 DATA_BLOB data, pdu, sig;
557 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
559 if (!PyArg_ParseTuple(args, "z#z#", &data.data, &data_length, &pdu.data, &pdu_length)) {
562 data.length = data_length;
563 pdu.length = pdu_length;
565 mem_ctx = talloc_new(NULL);
567 status = gensec_sign_packet(security, mem_ctx,
568 data.data, data.length,
569 pdu.data, pdu.length, &sig);
570 if (!NT_STATUS_IS_OK(status)) {
571 PyErr_SetNTSTATUS(status);
572 talloc_free(mem_ctx);
576 py_sig = PyBytes_FromStringAndSize((const char *)sig.data, sig.length);
577 talloc_free(mem_ctx);
581 static PyObject *py_gensec_check_packet(PyObject *self, PyObject *args)
584 Py_ssize_t data_length = 0;
585 Py_ssize_t pdu_length = 0;
586 Py_ssize_t sig_length = 0;
587 DATA_BLOB data, pdu, sig;
588 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
590 if (!PyArg_ParseTuple(args, "z#z#z#",
591 &data.data, &data_length,
592 &pdu.data, &pdu_length,
593 &sig.data, &sig_length)) {
596 data.length = data_length;
597 pdu.length = pdu_length;
598 sig.length = sig_length;
600 status = gensec_check_packet(security,
601 data.data, data.length,
602 pdu.data, pdu.length, &sig);
603 if (!NT_STATUS_IS_OK(status)) {
604 PyErr_SetNTSTATUS(status);
611 static PyMethodDef py_gensec_security_methods[] = {
612 { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
613 "S.start_client(settings) -> gensec" },
614 { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
615 "S.start_server(auth_ctx, settings) -> gensec" },
616 { "set_credentials", (PyCFunction)py_gensec_set_credentials, METH_VARARGS,
617 "S.start_client(credentials)" },
618 { "set_target_hostname", (PyCFunction)py_gensec_set_target_hostname, METH_VARARGS,
619 "S.start_target_hostname(target_hostname)" },
620 { "set_target_service", (PyCFunction)py_gensec_set_target_service, METH_VARARGS,
621 "S.start_target_service(target_service)" },
622 { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
623 "S.session_info() -> info" },
624 { "session_key", (PyCFunction)py_gensec_session_key, METH_NOARGS,
625 "S.session_key() -> key" },
626 { "start_mech_by_name", (PyCFunction)py_gensec_start_mech_by_name, METH_VARARGS,
627 "S.start_mech_by_name(name)" },
628 { "start_mech_by_sasl_name", (PyCFunction)py_gensec_start_mech_by_sasl_name, METH_VARARGS,
629 "S.start_mech_by_sasl_name(name)" },
630 { "start_mech_by_authtype", (PyCFunction)py_gensec_start_mech_by_authtype, METH_VARARGS,
631 "S.start_mech_by_authtype(authtype, level)" },
632 { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
633 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
634 { "want_feature", (PyCFunction)py_gensec_want_feature, METH_VARARGS,
635 "S.want_feature(feature)\n Request that GENSEC negotiate a particular feature." },
636 { "have_feature", (PyCFunction)py_gensec_have_feature, METH_VARARGS,
637 "S.have_feature()\n Return True if GENSEC negotiated a particular feature." },
638 { "set_max_update_size", (PyCFunction)py_gensec_set_max_update_size, METH_VARARGS,
639 "S.set_max_update_size(max_size) \n Some mechs can fragment update packets, needs to be use before the mech is started." },
640 { "max_update_size", (PyCFunction)py_gensec_max_update_size, 0,
641 "S.max_update_size() \n Return the current max_update_size." },
642 { "update", (PyCFunction)py_gensec_update, METH_VARARGS,
643 "S.update(blob_in) -> (finished, blob_out)\nPerform one step in a GENSEC dance. Repeat with new packets until finished is true or exception." },
644 { "wrap", (PyCFunction)py_gensec_wrap, METH_VARARGS,
645 "S.wrap(blob_in) -> blob_out\nPackage one clear packet into a wrapped GENSEC packet." },
646 { "unwrap", (PyCFunction)py_gensec_unwrap, METH_VARARGS,
647 "S.unwrap(blob_in) -> blob_out\nPerform one wrapped GENSEC packet into a clear packet." },
648 { "sig_size", (PyCFunction)py_gensec_sig_size, METH_VARARGS,
649 "S.sig_size(data_size) -> sig_size\nSize of the DCERPC packet signature" },
650 { "sign_packet", (PyCFunction)py_gensec_sign_packet, METH_VARARGS,
651 "S.sign_packet(data, whole_pdu) -> sig\nSign a DCERPC packet." },
652 { "check_packet", (PyCFunction)py_gensec_check_packet, METH_VARARGS,
653 "S.check_packet(data, whole_pdu, sig)\nCheck a DCERPC packet." },
657 static PyTypeObject Py_Security = {
658 .tp_name = "gensec.Security",
659 .tp_flags = Py_TPFLAGS_DEFAULT,
660 .tp_methods = py_gensec_security_methods,
663 void initgensec(void);
664 void initgensec(void)
668 if (pytalloc_BaseObject_PyType_Ready(&Py_Security) < 0)
671 m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
675 PyModule_AddObject(m, "FEATURE_SESSION_KEY", PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY));
676 PyModule_AddObject(m, "FEATURE_SIGN", PyInt_FromLong(GENSEC_FEATURE_SIGN));
677 PyModule_AddObject(m, "FEATURE_SEAL", PyInt_FromLong(GENSEC_FEATURE_SEAL));
678 PyModule_AddObject(m, "FEATURE_DCE_STYLE", PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE));
679 PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES", PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
680 PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE", PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
681 PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
682 PyModule_AddObject(m, "FEATURE_NEW_SPNEGO", PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
684 Py_INCREF(&Py_Security);
685 PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);