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 "libcli/util/pyerrors.h"
24 #include "scripting/python/modules.h"
25 #include "lib/talloc/pytalloc.h"
28 static PyObject
*py_get_name_by_authtype(PyObject
*self
, PyObject
*args
)
32 struct gensec_security
*security
;
34 if (!PyArg_ParseTuple(args
, "i", &type
))
37 security
= (struct gensec_security
*)py_talloc_get_ptr(self
);
39 name
= gensec_get_name_by_authtype(security
, type
);
43 return PyString_FromString(name
);
46 static struct gensec_settings
*settings_from_object(TALLOC_CTX
*mem_ctx
, PyObject
*object
)
48 struct gensec_settings
*s
;
49 PyObject
*py_hostname
, *py_lp_ctx
;
51 if (!PyDict_Check(object
)) {
52 PyErr_SetString(PyExc_ValueError
, "settings should be a dictionary");
56 s
= talloc_zero(mem_ctx
, struct gensec_settings
);
59 py_hostname
= PyDict_GetItemString(object
, "target_hostname");
61 PyErr_SetString(PyExc_ValueError
, "settings.target_hostname not found");
65 py_lp_ctx
= PyDict_GetItemString(object
, "lp_ctx");
67 PyErr_SetString(PyExc_ValueError
, "settings.lp_ctx not found");
71 s
->target_hostname
= PyString_AsString(py_hostname
);
72 s
->lp_ctx
= lpcfg_from_py_object(s
, py_lp_ctx
);
76 static PyObject
*py_gensec_start_client(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
79 py_talloc_Object
*self
;
80 struct gensec_settings
*settings
;
81 const char *kwnames
[] = { "settings", NULL
};
82 PyObject
*py_settings
;
83 struct tevent_context
*ev
;
84 struct gensec_security
*gensec
;
86 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O", discard_const_p(char *, kwnames
), &py_settings
))
89 self
= (py_talloc_Object
*)type
->tp_alloc(type
, 0);
94 self
->talloc_ctx
= talloc_new(NULL
);
95 if (self
->talloc_ctx
== NULL
) {
100 settings
= settings_from_object(self
->talloc_ctx
, py_settings
);
101 if (settings
== NULL
) {
106 ev
= tevent_context_init(self
->talloc_ctx
);
113 status
= gensec_init(settings
->lp_ctx
);
114 if (!NT_STATUS_IS_OK(status
)) {
115 PyErr_SetNTSTATUS(status
);
120 status
= gensec_client_start(self
->talloc_ctx
, &gensec
, ev
, settings
);
121 if (!NT_STATUS_IS_OK(status
)) {
122 PyErr_SetNTSTATUS(status
);
129 return (PyObject
*)self
;
132 static PyObject
*py_gensec_session_info(PyObject
*self
)
135 struct gensec_security
*security
= (struct gensec_security
*)py_talloc_get_ptr(self
);
136 struct auth_session_info
*info
;
137 if (security
->ops
== NULL
) {
138 PyErr_SetString(PyExc_RuntimeError
, "no mechanism selected");
141 status
= gensec_session_info(security
, &info
);
142 if (NT_STATUS_IS_ERR(status
)) {
143 PyErr_SetNTSTATUS(status
);
151 static PyObject
*py_gensec_start_mech_by_name(PyObject
*self
, PyObject
*args
)
154 struct gensec_security
*security
= (struct gensec_security
*)py_talloc_get_ptr(self
);
157 if (!PyArg_ParseTuple(args
, "s", &name
))
160 status
= gensec_start_mech_by_name(security
, name
);
161 if (!NT_STATUS_IS_OK(status
)) {
162 PyErr_SetNTSTATUS(status
);
169 static PyObject
*py_gensec_start_mech_by_authtype(PyObject
*self
, PyObject
*args
)
172 struct gensec_security
*security
= (struct gensec_security
*)py_talloc_get_ptr(self
);
174 if (!PyArg_ParseTuple(args
, "ii", &authtype
, &level
))
177 status
= gensec_start_mech_by_authtype(security
, authtype
, level
);
178 if (!NT_STATUS_IS_OK(status
)) {
179 PyErr_SetNTSTATUS(status
);
186 static PyMethodDef py_gensec_security_methods
[] = {
187 { "start_client", (PyCFunction
)py_gensec_start_client
, METH_VARARGS
|METH_KEYWORDS
|METH_CLASS
,
188 "S.start_client(settings) -> gensec" },
189 /* { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
190 "S.start_server(auth_ctx, settings) -> gensec" },*/
191 { "session_info", (PyCFunction
)py_gensec_session_info
, METH_NOARGS
,
192 "S.session_info() -> info" },
193 { "start_mech_by_name", (PyCFunction
)py_gensec_start_mech_by_name
, METH_VARARGS
,
194 "S.start_mech_by_name(name)" },
195 { "start_mech_by_authtype", (PyCFunction
)py_gensec_start_mech_by_authtype
, METH_VARARGS
, "S.start_mech_by_authtype(authtype, level)" },
196 { "get_name_by_authtype", (PyCFunction
)py_get_name_by_authtype
, METH_VARARGS
,
197 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
201 static PyTypeObject Py_Security
= {
202 .tp_name
= "Security",
203 .tp_flags
= Py_TPFLAGS_DEFAULT
,
204 .tp_methods
= py_gensec_security_methods
,
205 .tp_basicsize
= sizeof(py_talloc_Object
),
208 void initgensec(void)
212 Py_Security
.tp_base
= PyTalloc_GetObjectType();
213 if (Py_Security
.tp_base
== NULL
)
216 if (PyType_Ready(&Py_Security
) < 0)
219 m
= Py_InitModule3("gensec", NULL
, "Generic Security Interface.");
223 PyModule_AddObject(m
, "FEATURE_SESSION_KEY", PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY
));
224 PyModule_AddObject(m
, "FEATURE_SIGN", PyInt_FromLong(GENSEC_FEATURE_SIGN
));
225 PyModule_AddObject(m
, "FEATURE_SEAL", PyInt_FromLong(GENSEC_FEATURE_SEAL
));
226 PyModule_AddObject(m
, "FEATURE_DCE_STYLE", PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE
));
227 PyModule_AddObject(m
, "FEATURE_ASYNC_REPLIES", PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES
));
228 PyModule_AddObject(m
, "FEATURE_DATAGRAM_MODE", PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE
));
229 PyModule_AddObject(m
, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER
));
230 PyModule_AddObject(m
, "FEATURE_NEW_SPNEGO", PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO
));
232 Py_INCREF(&Py_Security
);
233 PyModule_AddObject(m
, "Security", (PyObject
*)&Py_Security
);