Allow request() call to do custom calls on DCE/RPC interfaces.
[gd/samba/.git] / source4 / librpc / rpc / pyrpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include <Python.h>
22 #include "librpc/rpc/pyrpc.h"
23 #include "librpc/rpc/dcerpc.h"
24 #include "lib/events/events.h"
25
26 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
27 {
28         const char *server_name;
29         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
30         
31         server_name = dcerpc_server_name(iface->pipe);
32         if (server_name == NULL)
33                 return Py_None;
34
35         return PyString_FromString(server_name);
36 }
37
38 static PyGetSetDef dcerpc_interface_getsetters[] = {
39         { discard_const_p(char, "server_name"), py_iface_server_name, NULL },
40         { NULL }
41 };
42
43 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
44 {
45         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
46         int opnum;
47         DATA_BLOB data_in, data_out;
48         NTSTATUS status;
49         char *in_data;
50         int in_length;
51         PyObject *ret;
52         char *object;
53         TALLOC_CTX *mem_ctx = talloc_new(NULL);
54         const char *kwnames[] = { "opnum", "data", "object", NULL };
55
56         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|s:request", 
57                 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
58                 return NULL;
59         }
60
61         data_in.data = talloc_strndup(mem_ctx, in_data, in_length);
62         data_in.length = in_length;
63
64         status = dcerpc_request(iface->pipe, NULL /* FIXME: object GUID */, 
65                                 opnum, false, mem_ctx, &data_in, &data_out);
66
67         if (NT_STATUS_IS_ERR(status)) {
68                 /* FIXME: Set more appropriate error */
69                 PyErr_SetString(PyExc_RuntimeError, "Unable to connect");
70                 talloc_free(mem_ctx);
71                 return NULL;
72         }
73
74         ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
75
76         talloc_free(mem_ctx);
77         return ret;
78 }
79
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 },
83 };
84
85
86 static void dcerpc_interface_dealloc(PyObject* self)
87 {
88         dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
89         talloc_free(interface->pipe);
90         PyObject_Del(self);
91 }
92
93 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
94 {
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;
102         NTSTATUS status;
103
104         PyObject *syntax;
105         const char *kwnames[] = {
106                 "binding", "syntax", "lp_ctx", "credentials", NULL
107         };
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);
110
111         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials)) {
112                 return NULL;
113         }
114
115         lp_ctx = lp_from_py_object(py_lp_ctx);
116         if (lp_ctx == NULL) {
117                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
118                 return NULL;
119         }
120
121         credentials = cli_credentials_from_py_object(py_credentials);
122         if (credentials == NULL) {
123                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
124                 return NULL;
125         }
126         ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
127
128         event_ctx = event_context_init(mem_ctx);
129
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);
135                 return NULL;
136         }
137
138         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
139         return (PyObject *)ret;
140 }
141
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"
150 "\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,
157 };
158
159 void initbase(void)
160 {
161         PyObject *m;
162
163         if (PyType_Ready(&dcerpc_InterfaceType) < 0)
164                 return;
165
166         m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
167         if (m == NULL)
168                 return;
169
170         Py_INCREF((PyObject *)&dcerpc_InterfaceType);
171         PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
172 }