2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <structmember.h>
23 #include "librpc/rpc/pyrpc.h"
24 #include "lib/events/events.h"
25 #include "param/pyparam.h"
26 #include "librpc/rpc/dcerpc.h"
27 #include "librpc/rpc/pyrpc_util.h"
28 #include "auth/credentials/pycredentials.h"
29 #include "auth/gensec/gensec.h"
33 static PyTypeObject dcerpc_InterfaceType
;
35 static PyTypeObject
*ndr_syntax_id_Type
;
37 static bool PyString_AsGUID(PyObject
*object
, struct GUID
*uuid
)
40 status
= GUID_from_string(PyString_AsString(object
), uuid
);
41 if (NT_STATUS_IS_ERR(status
)) {
42 PyErr_SetNTSTATUS(status
);
48 static bool ndr_syntax_from_py_object(PyObject
*object
, struct ndr_syntax_id
*syntax_id
)
50 ZERO_STRUCTP(syntax_id
);
52 if (PyString_Check(object
)) {
53 return PyString_AsGUID(object
, &syntax_id
->uuid
);
54 } else if (PyTuple_Check(object
)) {
55 if (PyTuple_Size(object
) < 1 || PyTuple_Size(object
) > 2) {
56 PyErr_SetString(PyExc_ValueError
, "Syntax ID tuple has invalid size");
60 if (!PyString_Check(PyTuple_GetItem(object
, 0))) {
61 PyErr_SetString(PyExc_ValueError
, "Expected GUID as first element in tuple");
65 if (!PyString_AsGUID(PyTuple_GetItem(object
, 0), &syntax_id
->uuid
))
68 if (!PyInt_Check(PyTuple_GetItem(object
, 1))) {
69 PyErr_SetString(PyExc_ValueError
, "Expected version as second element in tuple");
73 syntax_id
->if_version
= PyInt_AsLong(PyTuple_GetItem(object
, 1));
77 PyErr_SetString(PyExc_TypeError
, "Expected UUID or syntax id tuple");
81 static PyObject
*py_iface_server_name(PyObject
*obj
, void *closure
)
83 const char *server_name
;
84 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
86 server_name
= dcerpc_server_name(iface
->pipe
);
87 if (server_name
== NULL
)
90 return PyString_FromString(server_name
);
93 static PyObject
*py_ndr_syntax_id(struct ndr_syntax_id
*syntax_id
)
98 uuid_str
= GUID_string(NULL
, &syntax_id
->uuid
);
102 ret
= Py_BuildValue("(s,i)", uuid_str
, syntax_id
->if_version
);
104 talloc_free(uuid_str
);
109 static PyObject
*py_iface_abstract_syntax(PyObject
*obj
, void *closure
)
111 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
113 return py_ndr_syntax_id(&iface
->pipe
->syntax
);
116 static PyObject
*py_iface_transfer_syntax(PyObject
*obj
, void *closure
)
118 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
120 return py_ndr_syntax_id(&iface
->pipe
->transfer_syntax
);
123 static PyObject
*py_iface_session_key(PyObject
*obj
, void *closure
)
125 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
126 DATA_BLOB session_key
;
128 NTSTATUS status
= dcerpc_fetch_session_key(iface
->pipe
, &session_key
);
129 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
131 return PyString_FromStringAndSize((const char *)session_key
.data
, session_key
.length
);
134 static PyObject
*py_iface_user_session_key(PyObject
*obj
, void *closure
)
136 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
139 struct gensec_security
*security
= NULL
;
140 DATA_BLOB session_key
= data_blob_null
;
141 static PyObject
*session_key_obj
= NULL
;
143 if (iface
->pipe
== NULL
) {
144 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY
);
148 if (iface
->pipe
->conn
== NULL
) {
149 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY
);
153 if (iface
->pipe
->conn
->security_state
.generic_state
== NULL
) {
154 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY
);
158 security
= iface
->pipe
->conn
->security_state
.generic_state
;
160 mem_ctx
= talloc_new(NULL
);
162 status
= gensec_session_key(security
, mem_ctx
, &session_key
);
163 if (!NT_STATUS_IS_OK(status
)) {
164 talloc_free(mem_ctx
);
165 PyErr_SetNTSTATUS(status
);
169 session_key_obj
= PyString_FromStringAndSize((const char *)session_key
.data
,
171 talloc_free(mem_ctx
);
172 return session_key_obj
;
175 static PyObject
*py_iface_get_timeout(PyObject
*obj
, void *closure
)
177 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
180 timeout
= dcerpc_binding_handle_set_timeout(iface
->binding_handle
, 0);
181 dcerpc_binding_handle_set_timeout(iface
->binding_handle
, timeout
);
183 return PyLong_FromUnsignedLong(timeout
);
186 static int py_iface_set_timeout(PyObject
*obj
, PyObject
*value
, void *closure
)
188 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)obj
;
191 timeout
= PyLong_AsUnsignedLong(value
);
192 if (PyErr_Occurred() != NULL
) {
196 dcerpc_binding_handle_set_timeout(iface
->binding_handle
, timeout
);
200 static PyGetSetDef dcerpc_interface_getsetters
[] = {
201 { discard_const_p(char, "server_name"), py_iface_server_name
, NULL
,
202 discard_const_p(char, "name of the server, if connected over SMB") },
203 { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax
, NULL
,
204 discard_const_p(char, "syntax id of the abstract syntax") },
205 { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax
, NULL
,
206 discard_const_p(char, "syntax id of the transfersyntax") },
207 { discard_const_p(char, "session_key"), py_iface_session_key
, NULL
,
208 discard_const_p(char, "session key (as used for blob encryption on LSA and SAMR)") },
209 { discard_const_p(char, "user_session_key"), py_iface_user_session_key
, NULL
,
210 discard_const_p(char, "user_session key (as used for blob encryption on DRSUAPI)") },
211 { discard_const_p(char, "request_timeout"), py_iface_get_timeout
, py_iface_set_timeout
,
212 discard_const_p(char, "request timeout, in seconds") },
216 static PyObject
*py_iface_request(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
218 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)self
;
220 DATA_BLOB data_in
, data_out
;
223 Py_ssize_t in_length
;
225 PyObject
*object
= NULL
;
226 struct GUID object_guid
;
227 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
228 uint32_t out_flags
= 0;
229 const char *kwnames
[] = { "opnum", "data", "object", NULL
};
231 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "is#|O:request",
232 discard_const_p(char *, kwnames
), &opnum
, &in_data
, &in_length
, &object
)) {
233 talloc_free(mem_ctx
);
237 data_in
.data
= (uint8_t *)talloc_memdup(mem_ctx
, in_data
, in_length
);
238 data_in
.length
= in_length
;
240 ZERO_STRUCT(data_out
);
242 if (object
!= NULL
&& !PyString_AsGUID(object
, &object_guid
)) {
243 talloc_free(mem_ctx
);
247 status
= dcerpc_binding_handle_raw_call(iface
->binding_handle
,
248 object
?&object_guid
:NULL
,
257 if (!NT_STATUS_IS_OK(status
)) {
258 PyErr_SetDCERPCStatus(iface
->pipe
, status
);
259 talloc_free(mem_ctx
);
263 ret
= PyString_FromStringAndSize((char *)data_out
.data
, data_out
.length
);
265 talloc_free(mem_ctx
);
269 static PyMethodDef dcerpc_interface_methods
[] = {
270 { "request", (PyCFunction
)py_iface_request
, METH_VARARGS
|METH_KEYWORDS
, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
271 { NULL
, NULL
, 0, NULL
},
274 static void dcerpc_interface_dealloc(PyObject
* self
)
276 dcerpc_InterfaceObject
*interface
= (dcerpc_InterfaceObject
*)self
;
277 interface
->binding_handle
= NULL
;
278 interface
->pipe
= NULL
;
279 TALLOC_FREE(interface
->mem_ctx
);
280 self
->ob_type
->tp_free(self
);
283 static PyObject
*dcerpc_interface_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
286 const char *binding_string
= NULL
;
287 PyObject
*py_lp_ctx
= Py_None
;
288 PyObject
*py_credentials
= Py_None
;
289 PyObject
*syntax
= Py_None
;
290 PyObject
*py_basis
= Py_None
;
291 const char *kwnames
[] = {
292 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
294 static struct ndr_interface_table dummy_table
;
295 PyObject
*args2
= Py_None
;
296 PyObject
*kwargs2
= Py_None
;
298 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "sO|OOO:connect", discard_const_p(char *, kwnames
), &binding_string
, &syntax
, &py_lp_ctx
, &py_credentials
, &py_basis
)) {
302 if (strncmp(binding_string
, "irpc:", 5) == 0) {
303 PyErr_SetString(PyExc_ValueError
, "irpc: transport not supported");
308 * Fill a dummy interface table struct. TODO: In the future, we should
309 * rather just allow connecting without requiring an interface table.
311 * We just fill the syntax during the connect, but keep the memory valid
314 if (!ndr_syntax_from_py_object(syntax
, &dummy_table
.syntax_id
)) {
318 args2
= Py_BuildValue("(s)", binding_string
);
323 kwargs2
= Py_BuildValue("{s:O,s:O,s:O}",
325 "credentials", py_credentials
,
326 "basis_connection", py_basis
);
327 if (kwargs2
== NULL
) {
332 ret
= py_dcerpc_interface_init_helper(type
, args2
, kwargs2
, &dummy_table
);
333 ZERO_STRUCT(dummy_table
.syntax_id
);
339 static PyTypeObject dcerpc_InterfaceType
= {
340 PyObject_HEAD_INIT(NULL
) 0,
341 .tp_name
= "dcerpc.ClientConnection",
342 .tp_basicsize
= sizeof(dcerpc_InterfaceObject
),
343 .tp_dealloc
= dcerpc_interface_dealloc
,
344 .tp_getset
= dcerpc_interface_getsetters
,
345 .tp_methods
= dcerpc_interface_methods
,
346 .tp_doc
= "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
348 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
349 "syntax should be a tuple with a GUID and version number of an interface\n"
350 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
351 "credentials should be a credentials.Credentials object.\n\n",
352 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
353 .tp_new
= dcerpc_interface_new
,
356 static PyObject
*py_transfer_syntax_ndr_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
358 return py_dcerpc_syntax_init_helper(type
, args
, kwargs
, &ndr_transfer_syntax_ndr
);
361 static PyTypeObject py_transfer_syntax_ndr_SyntaxType
= {
362 PyObject_HEAD_INIT(NULL
) 0,
363 .tp_name
= "base.transfer_syntax_ndr",
364 .tp_doc
= "transfer_syntax_ndr()\n",
365 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
366 .tp_new
= py_transfer_syntax_ndr_new
,
369 static PyObject
*py_transfer_syntax_ndr64_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
371 return py_dcerpc_syntax_init_helper(type
, args
, kwargs
, &ndr_transfer_syntax_ndr64
);
374 static PyTypeObject py_transfer_syntax_ndr64_SyntaxType
= {
375 PyObject_HEAD_INIT(NULL
) 0,
376 .tp_name
= "base.transfer_syntax_ndr64",
377 .tp_doc
= "transfer_syntax_ndr64()\n",
378 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
379 .tp_new
= py_transfer_syntax_ndr64_new
,
382 static PyObject
*py_bind_time_features_syntax_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
384 const char *kwnames
[] = {
387 unsigned long long features
= 0;
388 struct ndr_syntax_id syntax
;
389 PyObject
*args2
= Py_None
;
390 PyObject
*kwargs2
= Py_None
;
392 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "K:features", discard_const_p(char *, kwnames
), &features
)) {
396 args2
= Py_BuildValue("()");
401 kwargs2
= Py_BuildValue("{}");
402 if (kwargs2
== NULL
) {
407 syntax
= dcerpc_construct_bind_time_features(features
);
409 return py_dcerpc_syntax_init_helper(type
, args2
, kwargs2
, &syntax
);
412 static PyTypeObject py_bind_time_features_syntax_SyntaxType
= {
413 PyObject_HEAD_INIT(NULL
) 0,
414 .tp_name
= "base.bind_time_features_syntax",
415 .tp_doc
= "bind_time_features_syntax(features)\n",
416 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
417 .tp_new
= py_bind_time_features_syntax_new
,
423 PyObject
*dep_samba_dcerpc_misc
;
425 dep_samba_dcerpc_misc
= PyImport_ImportModule("samba.dcerpc.misc");
426 if (dep_samba_dcerpc_misc
== NULL
)
429 ndr_syntax_id_Type
= (PyTypeObject
*)PyObject_GetAttrString(dep_samba_dcerpc_misc
, "ndr_syntax_id");
430 if (ndr_syntax_id_Type
== NULL
)
433 py_transfer_syntax_ndr_SyntaxType
.tp_base
= ndr_syntax_id_Type
;
434 py_transfer_syntax_ndr_SyntaxType
.tp_basicsize
= pytalloc_BaseObject_size();
435 py_transfer_syntax_ndr64_SyntaxType
.tp_base
= ndr_syntax_id_Type
;
436 py_transfer_syntax_ndr64_SyntaxType
.tp_basicsize
= pytalloc_BaseObject_size();
437 py_bind_time_features_syntax_SyntaxType
.tp_base
= ndr_syntax_id_Type
;
438 py_bind_time_features_syntax_SyntaxType
.tp_basicsize
= pytalloc_BaseObject_size();
440 if (PyType_Ready(&dcerpc_InterfaceType
) < 0)
443 if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType
) < 0)
445 if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType
) < 0)
447 if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType
) < 0)
450 m
= Py_InitModule3("base", NULL
, "DCE/RPC protocol implementation");
454 Py_INCREF((PyObject
*)&dcerpc_InterfaceType
);
455 PyModule_AddObject(m
, "ClientConnection", (PyObject
*)&dcerpc_InterfaceType
);
457 Py_INCREF((PyObject
*)(void *)&py_transfer_syntax_ndr_SyntaxType
);
458 PyModule_AddObject(m
, "transfer_syntax_ndr", (PyObject
*)(void *)&py_transfer_syntax_ndr_SyntaxType
);
459 Py_INCREF((PyObject
*)(void *)&py_transfer_syntax_ndr64_SyntaxType
);
460 PyModule_AddObject(m
, "transfer_syntax_ndr64", (PyObject
*)(void *)&py_transfer_syntax_ndr64_SyntaxType
);
461 Py_INCREF((PyObject
*)(void *)&py_bind_time_features_syntax_SyntaxType
);
462 PyModule_AddObject(m
, "bind_time_features_syntax", (PyObject
*)(void *)&py_bind_time_features_syntax_SyntaxType
);