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 "librpc/rpc/pyrpc.h"
23 #include "librpc/rpc/dcerpc.h"
24 #include "lib/events/events.h"
26 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
28 const char *server_name;
29 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
31 server_name = dcerpc_server_name(iface->pipe);
32 if (server_name == NULL)
35 return PyString_FromString(server_name);
38 static PyGetSetDef dcerpc_interface_getsetters[] = {
39 { discard_const_p(char, "server_name"), py_iface_server_name, NULL },
43 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
45 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
47 DATA_BLOB data_in, data_out;
53 TALLOC_CTX *mem_ctx = talloc_new(NULL);
54 const char *kwnames[] = { "opnum", "data", "object", NULL };
56 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|s:request",
57 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
61 data_in.data = talloc_strndup(mem_ctx, in_data, in_length);
62 data_in.length = in_length;
64 status = dcerpc_request(iface->pipe, NULL /* FIXME: object GUID */,
65 opnum, false, mem_ctx, &data_in, &data_out);
67 if (NT_STATUS_IS_ERR(status)) {
68 /* FIXME: Set more appropriate error */
69 PyErr_SetString(PyExc_RuntimeError, "Unable to connect");
74 ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
80 static PyMethodDef dcerpc_interface_methods[] = {
81 { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data) -> data\nMake a raw request" },
82 { NULL, NULL, 0, NULL },
86 static void dcerpc_interface_dealloc(PyObject* self)
88 dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
89 talloc_free(interface->pipe);
93 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
95 dcerpc_InterfaceObject *ret;
96 const char *binding_string;
97 struct cli_credentials *credentials;
98 struct loadparm_context *lp_ctx = NULL;
99 PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
100 TALLOC_CTX *mem_ctx = NULL;
101 struct event_context *event_ctx;
105 const char *kwnames[] = {
106 "binding", "syntax", "lp_ctx", "credentials", NULL
108 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
109 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
111 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials)) {
115 lp_ctx = lp_from_py_object(py_lp_ctx);
116 if (lp_ctx == NULL) {
117 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
121 credentials = cli_credentials_from_py_object(py_credentials);
122 if (credentials == NULL) {
123 PyErr_SetString(PyExc_TypeError, "Expected credentials");
126 ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
128 event_ctx = event_context_init(mem_ctx);
130 status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string,
131 NULL, credentials, event_ctx, lp_ctx);
132 if (NT_STATUS_IS_ERR(status)) {
133 PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
134 talloc_free(mem_ctx);
138 ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
139 return (PyObject *)ret;
142 PyTypeObject dcerpc_InterfaceType = {
143 PyObject_HEAD_INIT(NULL) 0,
144 .tp_name = "dcerpc.ClientConnection",
145 .tp_basicsize = sizeof(dcerpc_InterfaceObject),
146 .tp_dealloc = dcerpc_interface_dealloc,
147 .tp_getset = dcerpc_interface_getsetters,
148 .tp_methods = dcerpc_interface_methods,
149 .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
151 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
152 "syntax should be a tuple with a GUID and version number of an interface\n"
153 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
154 "credentials should be a credentials.Credentials object.\n\n",
155 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
156 .tp_new = dcerpc_interface_new,
163 if (PyType_Ready(&dcerpc_InterfaceType) < 0)
166 m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
170 Py_INCREF((PyObject *)&dcerpc_InterfaceType);
171 PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);