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/>.
20 #include "python/py3compat.h"
22 #include "python/modules.h"
23 #include "param/pyparam.h"
24 #include "auth/gensec/gensec.h"
25 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
26 #include "auth/credentials/pycredentials.h"
27 #include "libcli/util/pyerrors.h"
28 #include "python/modules.h"
31 #include "librpc/rpc/pyrpc_util.h"
33 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
37 struct gensec_security *security;
39 if (!PyArg_ParseTuple(args, "i", &type))
42 security = pytalloc_get_type(self, struct gensec_security);
44 name = gensec_get_name_by_authtype(security, type);
48 return PyUnicode_FromString(name);
51 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
53 struct gensec_settings *s;
54 PyObject *py_hostname, *py_lp_ctx;
56 if (!PyDict_Check(object)) {
57 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
61 s = talloc_zero(mem_ctx, struct gensec_settings);
64 py_hostname = PyDict_GetItemString(object, "target_hostname");
66 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
70 py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
72 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
76 s->target_hostname = PyUnicode_AsUTF8(py_hostname);
77 s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx);
81 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
85 struct gensec_settings *settings;
86 const char *kwnames[] = { "settings", NULL };
87 PyObject *py_settings = Py_None;
88 struct gensec_security *gensec;
91 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &py_settings))
94 frame = talloc_stackframe();
96 if (py_settings != Py_None) {
97 settings = settings_from_object(frame, py_settings);
98 if (settings == NULL) {
104 settings = talloc_zero(frame, struct gensec_settings);
105 if (settings == NULL) {
111 settings->lp_ctx = loadparm_init_global(true);
112 if (settings->lp_ctx == NULL) {
119 status = gensec_init();
120 if (!NT_STATUS_IS_OK(status)) {
121 PyErr_SetNTSTATUS(status);
126 status = gensec_client_start(frame, &gensec, settings);
127 if (!NT_STATUS_IS_OK(status)) {
128 PyErr_SetNTSTATUS(status);
133 self = pytalloc_steal(type, gensec);
136 return (PyObject *)self;
139 static PyObject *py_gensec_start_server(PyTypeObject *type, PyObject *args, PyObject *kwargs)
143 struct gensec_settings *settings = NULL;
144 const char *kwnames[] = { "settings", "auth_context", NULL };
145 PyObject *py_settings = Py_None;
146 PyObject *py_auth_context = Py_None;
147 struct gensec_security *gensec;
148 struct auth4_context *auth_context = NULL;
151 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", discard_const_p(char *, kwnames), &py_settings, &py_auth_context))
154 frame = talloc_stackframe();
156 if (py_settings != Py_None) {
157 settings = settings_from_object(frame, py_settings);
158 if (settings == NULL) {
164 settings = talloc_zero(frame, struct gensec_settings);
165 if (settings == NULL) {
171 settings->lp_ctx = loadparm_init_global(true);
172 if (settings->lp_ctx == NULL) {
179 if (py_auth_context != Py_None) {
180 bool ok = py_check_dcerpc_type(py_auth_context,
187 auth_context = pytalloc_get_type(py_auth_context,
188 struct auth4_context);
190 PyErr_Format(PyExc_TypeError,
191 "Expected auth.AuthContext for auth_context argument, got %s",
192 pytalloc_get_name(py_auth_context));
197 status = gensec_init();
198 if (!NT_STATUS_IS_OK(status)) {
199 PyErr_SetNTSTATUS(status);
204 status = gensec_server_start(frame, settings, auth_context, &gensec);
205 if (!NT_STATUS_IS_OK(status)) {
206 PyErr_SetNTSTATUS(status);
211 self = pytalloc_steal(type, gensec);
217 static PyObject *py_gensec_set_target_hostname(PyObject *self, PyObject *args)
219 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
220 char *target_hostname;
223 if (!PyArg_ParseTuple(args, "s", &target_hostname))
226 status = gensec_set_target_hostname(security, target_hostname);
227 if (!NT_STATUS_IS_OK(status)) {
228 PyErr_SetNTSTATUS(status);
235 static PyObject *py_gensec_set_target_service(PyObject *self, PyObject *args)
237 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
238 char *target_service;
241 if (!PyArg_ParseTuple(args, "s", &target_service))
244 status = gensec_set_target_service(security, target_service);
245 if (!NT_STATUS_IS_OK(status)) {
246 PyErr_SetNTSTATUS(status);
253 static PyObject *py_gensec_set_target_service_description(PyObject *self, PyObject *args)
255 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
256 char *target_service_description;
259 if (!PyArg_ParseTuple(args, "s", &target_service_description))
262 status = gensec_set_target_service_description(security,
263 target_service_description);
264 if (!NT_STATUS_IS_OK(status)) {
265 PyErr_SetNTSTATUS(status);
272 static PyObject *py_gensec_set_credentials(PyObject *self, PyObject *args)
274 PyObject *py_creds = Py_None;
275 struct cli_credentials *creds;
276 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
279 if (!PyArg_ParseTuple(args, "O", &py_creds))
282 creds = PyCredentials_AsCliCredentials(py_creds);
286 "Expected samba.credentials for credentials argument, "
287 "got %s", pytalloc_get_name(py_creds));
291 status = gensec_set_credentials(security, creds);
292 if (!NT_STATUS_IS_OK(status)) {
293 PyErr_SetNTSTATUS(status);
300 static PyObject *py_gensec_session_info(PyObject *self,
301 PyObject *Py_UNUSED(ignored))
305 PyObject *py_session_info;
306 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
307 struct auth_session_info *info;
308 if (security->ops == NULL) {
309 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
312 mem_ctx = talloc_new(NULL);
314 status = gensec_session_info(security, mem_ctx, &info);
315 if (NT_STATUS_IS_ERR(status)) {
316 PyErr_SetNTSTATUS(status);
320 py_session_info = py_return_ndr_struct("samba.dcerpc.auth", "session_info",
322 talloc_free(mem_ctx);
323 return py_session_info;
326 static PyObject *py_gensec_session_key(PyObject *self,
327 PyObject *Py_UNUSED(ignored))
331 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
332 DATA_BLOB session_key = data_blob_null;
333 static PyObject *session_key_obj = NULL;
335 if (security->ops == NULL) {
336 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
339 mem_ctx = talloc_new(NULL);
341 status = gensec_session_key(security, mem_ctx, &session_key);
342 if (!NT_STATUS_IS_OK(status)) {
343 talloc_free(mem_ctx);
344 PyErr_SetNTSTATUS(status);
348 session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data,
350 talloc_free(mem_ctx);
351 return session_key_obj;
354 static PyObject *py_gensec_start_mech_by_name(PyObject *self, PyObject *args)
357 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
360 if (!PyArg_ParseTuple(args, "s", &name))
363 status = gensec_start_mech_by_name(security, name);
364 if (!NT_STATUS_IS_OK(status)) {
365 PyErr_SetNTSTATUS(status);
372 static PyObject *py_gensec_start_mech_by_sasl_name(PyObject *self, PyObject *args)
375 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
378 if (!PyArg_ParseTuple(args, "s", &sasl_name))
381 status = gensec_start_mech_by_sasl_name(security, sasl_name);
382 if (!NT_STATUS_IS_OK(status)) {
383 PyErr_SetNTSTATUS(status);
390 static PyObject *py_gensec_start_mech_by_authtype(PyObject *self, PyObject *args)
393 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
395 if (!PyArg_ParseTuple(args, "ii", &authtype, &level))
398 status = gensec_start_mech_by_authtype(security, authtype, level);
399 if (!NT_STATUS_IS_OK(status)) {
400 PyErr_SetNTSTATUS(status);
407 static PyObject *py_gensec_want_feature(PyObject *self, PyObject *args)
410 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
411 /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
412 if (!PyArg_ParseTuple(args, "i", &feature))
415 gensec_want_feature(security, feature);
420 static PyObject *py_gensec_have_feature(PyObject *self, PyObject *args)
423 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
424 /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
425 if (!PyArg_ParseTuple(args, "i", &feature))
428 if (gensec_have_feature(security, feature)) {
434 static PyObject *py_gensec_set_max_update_size(PyObject *self, PyObject *args)
436 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
437 unsigned int max_update_size = 0;
439 if (!PyArg_ParseTuple(args, "I", &max_update_size))
442 gensec_set_max_update_size(security, max_update_size);
447 static PyObject *py_gensec_max_update_size(PyObject *self,
448 PyObject *Py_UNUSED(ignored))
450 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
451 unsigned int max_update_size = gensec_max_update_size(security);
453 return PyLong_FromLong(max_update_size);
456 static PyObject *py_gensec_update(PyObject *self, PyObject *args)
461 PyObject *py_bytes, *result, *py_in;
462 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
463 PyObject *finished_processing;
465 if (!PyArg_ParseTuple(args, "O", &py_in))
468 mem_ctx = talloc_new(NULL);
469 if (!PyBytes_Check(py_in)) {
470 PyErr_Format(PyExc_TypeError, "bytes expected");
474 in.data = (uint8_t *)PyBytes_AsString(py_in);
475 in.length = PyBytes_Size(py_in);
477 status = gensec_update(security, mem_ctx, in, &out);
479 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)
480 && !NT_STATUS_IS_OK(status)) {
481 PyErr_SetNTSTATUS(status);
482 talloc_free(mem_ctx);
485 py_bytes = PyBytes_FromStringAndSize((const char *)out.data,
487 talloc_free(mem_ctx);
489 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
490 finished_processing = Py_False;
492 finished_processing = Py_True;
495 result = PyTuple_Pack(2, finished_processing, py_bytes);
496 Py_XDECREF(py_bytes);
500 static PyObject *py_gensec_wrap(PyObject *self, PyObject *args)
506 PyObject *ret, *py_in;
507 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
509 if (!PyArg_ParseTuple(args, "O", &py_in))
512 mem_ctx = talloc_new(NULL);
514 if (!PyBytes_Check(py_in)) {
515 PyErr_Format(PyExc_TypeError, "bytes expected");
518 in.data = (uint8_t *)PyBytes_AsString(py_in);
519 in.length = PyBytes_Size(py_in);
521 status = gensec_wrap(security, mem_ctx, &in, &out);
523 if (!NT_STATUS_IS_OK(status)) {
524 PyErr_SetNTSTATUS(status);
525 talloc_free(mem_ctx);
529 ret = PyBytes_FromStringAndSize((const char *)out.data, out.length);
530 talloc_free(mem_ctx);
535 static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args)
541 PyObject *ret, *py_in;
542 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
544 if (!PyArg_ParseTuple(args, "O", &py_in))
547 mem_ctx = talloc_new(NULL);
549 if (!PyBytes_Check(py_in)) {
550 PyErr_Format(PyExc_TypeError, "bytes expected");
554 in.data = (uint8_t *)PyBytes_AsString(py_in);
555 in.length = PyBytes_Size(py_in);
557 status = gensec_unwrap(security, mem_ctx, &in, &out);
559 if (!NT_STATUS_IS_OK(status)) {
560 PyErr_SetNTSTATUS(status);
561 talloc_free(mem_ctx);
565 ret = PyBytes_FromStringAndSize((const char *)out.data, out.length);
566 talloc_free(mem_ctx);
570 static PyObject *py_gensec_sig_size(PyObject *self, PyObject *args)
572 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
573 Py_ssize_t data_size = 0;
576 if (!PyArg_ParseTuple(args, "n", &data_size)) {
580 sig_size = gensec_sig_size(security, data_size);
582 return PyLong_FromSize_t(sig_size);
585 static PyObject *py_gensec_sign_packet(PyObject *self, PyObject *args)
588 TALLOC_CTX *mem_ctx = NULL;
589 Py_ssize_t data_length = 0;
590 Py_ssize_t pdu_length = 0;
591 DATA_BLOB data, pdu, sig;
593 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
595 if (!PyArg_ParseTuple(args, "z#z#", &data.data, &data_length, &pdu.data, &pdu_length)) {
598 data.length = data_length;
599 pdu.length = pdu_length;
601 mem_ctx = talloc_new(NULL);
603 status = gensec_sign_packet(security, mem_ctx,
604 data.data, data.length,
605 pdu.data, pdu.length, &sig);
606 if (!NT_STATUS_IS_OK(status)) {
607 PyErr_SetNTSTATUS(status);
608 talloc_free(mem_ctx);
612 py_sig = PyBytes_FromStringAndSize((const char *)sig.data, sig.length);
613 talloc_free(mem_ctx);
617 static PyObject *py_gensec_check_packet(PyObject *self, PyObject *args)
620 Py_ssize_t data_length = 0;
621 Py_ssize_t pdu_length = 0;
622 Py_ssize_t sig_length = 0;
623 DATA_BLOB data, pdu, sig;
624 struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
626 if (!PyArg_ParseTuple(args, "z#z#z#",
627 &data.data, &data_length,
628 &pdu.data, &pdu_length,
629 &sig.data, &sig_length)) {
632 data.length = data_length;
633 pdu.length = pdu_length;
634 sig.length = sig_length;
636 status = gensec_check_packet(security,
637 data.data, data.length,
638 pdu.data, pdu.length, &sig);
639 if (!NT_STATUS_IS_OK(status)) {
640 PyErr_SetNTSTATUS(status);
647 static PyMethodDef py_gensec_security_methods[] = {
648 { "start_client", PY_DISCARD_FUNC_SIG(PyCFunction,
649 py_gensec_start_client),
650 METH_VARARGS|METH_KEYWORDS|METH_CLASS,
651 "S.start_client(settings) -> gensec" },
652 { "start_server", PY_DISCARD_FUNC_SIG(PyCFunction,
653 py_gensec_start_server),
654 METH_VARARGS|METH_KEYWORDS|METH_CLASS,
655 "S.start_server(auth_ctx, settings) -> gensec" },
656 { "set_credentials", (PyCFunction)py_gensec_set_credentials, METH_VARARGS,
657 "S.start_client(credentials)" },
658 { "set_target_hostname", (PyCFunction)py_gensec_set_target_hostname, METH_VARARGS,
659 "S.start_target_hostname(target_hostname) \n This sets the Kerberos target hostname to obtain a ticket for." },
660 { "set_target_service", (PyCFunction)py_gensec_set_target_service, METH_VARARGS,
661 "S.start_target_service(target_service) \n This sets the Kerberos target service to obtain a ticket for. The default value is 'host'" },
662 { "set_target_service_description", (PyCFunction)py_gensec_set_target_service_description, METH_VARARGS,
663 "S.start_target_service_description(target_service_description) \n This description is set server-side and used in authentication and authorization logs. The default value is that provided to set_target_service() or None."},
664 { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
665 "S.session_info() -> info" },
666 { "session_key", (PyCFunction)py_gensec_session_key, METH_NOARGS,
667 "S.session_key() -> key" },
668 { "start_mech_by_name", (PyCFunction)py_gensec_start_mech_by_name, METH_VARARGS,
669 "S.start_mech_by_name(name)" },
670 { "start_mech_by_sasl_name", (PyCFunction)py_gensec_start_mech_by_sasl_name, METH_VARARGS,
671 "S.start_mech_by_sasl_name(name)" },
672 { "start_mech_by_authtype", (PyCFunction)py_gensec_start_mech_by_authtype, METH_VARARGS,
673 "S.start_mech_by_authtype(authtype, level)" },
674 { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
675 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
676 { "want_feature", (PyCFunction)py_gensec_want_feature, METH_VARARGS,
677 "S.want_feature(feature)\n Request that GENSEC negotiate a particular feature." },
678 { "have_feature", (PyCFunction)py_gensec_have_feature, METH_VARARGS,
679 "S.have_feature()\n Return True if GENSEC negotiated a particular feature." },
680 { "set_max_update_size", (PyCFunction)py_gensec_set_max_update_size, METH_VARARGS,
681 "S.set_max_update_size(max_size) \n Some mechs can fragment update packets, needs to be use before the mech is started." },
682 { "max_update_size", (PyCFunction)py_gensec_max_update_size, METH_NOARGS,
683 "S.max_update_size() \n Return the current max_update_size." },
684 { "update", (PyCFunction)py_gensec_update, METH_VARARGS,
685 "S.update(blob_in) -> (finished, blob_out)\nPerform one step in a GENSEC dance. Repeat with new packets until finished is true or exception." },
686 { "wrap", (PyCFunction)py_gensec_wrap, METH_VARARGS,
687 "S.wrap(blob_in) -> blob_out\nPackage one clear packet into a wrapped GENSEC packet." },
688 { "unwrap", (PyCFunction)py_gensec_unwrap, METH_VARARGS,
689 "S.unwrap(blob_in) -> blob_out\nPerform one wrapped GENSEC packet into a clear packet." },
690 { "sig_size", (PyCFunction)py_gensec_sig_size, METH_VARARGS,
691 "S.sig_size(data_size) -> sig_size\nSize of the DCERPC packet signature" },
692 { "sign_packet", (PyCFunction)py_gensec_sign_packet, METH_VARARGS,
693 "S.sign_packet(data, whole_pdu) -> sig\nSign a DCERPC packet." },
694 { "check_packet", (PyCFunction)py_gensec_check_packet, METH_VARARGS,
695 "S.check_packet(data, whole_pdu, sig)\nCheck a DCERPC packet." },
699 static struct PyModuleDef moduledef = {
700 PyModuleDef_HEAD_INIT,
702 .m_doc = "Generic Security Interface.",
706 static PyTypeObject Py_Security = {
707 .tp_name = "gensec.Security",
708 .tp_flags = Py_TPFLAGS_DEFAULT,
709 .tp_methods = py_gensec_security_methods,
712 MODULE_INIT_FUNC(gensec)
716 if (pytalloc_BaseObject_PyType_Ready(&Py_Security) < 0)
719 m = PyModule_Create(&moduledef);
723 PyModule_AddObject(m, "FEATURE_SESSION_KEY", PyLong_FromLong(GENSEC_FEATURE_SESSION_KEY));
724 PyModule_AddObject(m, "FEATURE_SIGN", PyLong_FromLong(GENSEC_FEATURE_SIGN));
725 PyModule_AddObject(m, "FEATURE_SEAL", PyLong_FromLong(GENSEC_FEATURE_SEAL));
726 PyModule_AddObject(m, "FEATURE_DCE_STYLE", PyLong_FromLong(GENSEC_FEATURE_DCE_STYLE));
727 PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES", PyLong_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
728 PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE", PyLong_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
729 PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyLong_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
730 PyModule_AddObject(m, "FEATURE_NEW_SPNEGO", PyLong_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
732 Py_INCREF(&Py_Security);
733 PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);