Allow connecting to a DCE/RPC interface with Python for which we don't have IDL.
[kai/samba-autobuild/.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 = (uint8_t *)talloc_strndup(mem_ctx, in_data, in_length);
62         data_in.length = in_length;
63
64         ZERO_STRUCT(data_out);
65
66         status = dcerpc_request(iface->pipe, NULL /* FIXME: object GUID */, 
67                                 opnum, false, mem_ctx, &data_in, &data_out);
68
69         if (NT_STATUS_IS_ERR(status)) {
70                 PyErr_SetNTSTATUS(status);
71                 talloc_free(mem_ctx);
72                 return NULL;
73         }
74
75         ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
76
77         talloc_free(mem_ctx);
78         return ret;
79 }
80
81 static PyMethodDef dcerpc_interface_methods[] = {
82         { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data) -> data\nMake a raw request" },
83         { NULL, NULL, 0, NULL },
84 };
85
86
87 static void dcerpc_interface_dealloc(PyObject* self)
88 {
89         dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
90         talloc_free(interface->pipe);
91         PyObject_Del(self);
92 }
93
94 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
95 {
96         NTSTATUS status;
97         status = GUID_from_string(PyString_AsString(object), uuid);
98         if (NT_STATUS_IS_ERR(status)) {
99                 PyErr_SetNTSTATUS(status);
100                 return false;
101         }
102         return true;
103 }
104
105 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
106 {
107         ZERO_STRUCTP(syntax_id);
108
109         if (PyString_Check(object)) {
110                 return PyString_AsGUID(object, &syntax_id->uuid);
111         } else if (PyTuple_Check(object)) {
112                 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
113                         PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
114                         return false;
115                 }
116
117                 if (!PyString_Check(PyTuple_GetItem(object, 0))) {
118                         PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
119                         return false;
120                 }
121
122                 if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) 
123                         return false;
124
125                 if (!PyInt_Check(PyTuple_GetItem(object, 1))) {
126                         PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
127                         return false;
128                 }
129
130                 syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1));
131                 return true;
132         }
133
134         PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
135         return false;
136 }       
137
138 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
139 {
140         dcerpc_InterfaceObject *ret;
141         const char *binding_string;
142         struct cli_credentials *credentials;
143         struct loadparm_context *lp_ctx = NULL;
144         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
145         TALLOC_CTX *mem_ctx = NULL;
146         struct event_context *event_ctx;
147         NTSTATUS status;
148
149         PyObject *syntax;
150         const char *kwnames[] = {
151                 "binding", "syntax", "lp_ctx", "credentials", NULL
152         };
153         extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
154         extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
155         struct ndr_interface_table *table;
156
157         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials)) {
158                 return NULL;
159         }
160
161         lp_ctx = lp_from_py_object(py_lp_ctx);
162         if (lp_ctx == NULL) {
163                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
164                 return NULL;
165         }
166
167         credentials = cli_credentials_from_py_object(py_credentials);
168         if (credentials == NULL) {
169                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
170                 return NULL;
171         }
172         ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
173
174         event_ctx = event_context_init(mem_ctx);
175
176         /* Create a dummy interface table struct. TODO: In the future, we should rather just allow 
177          * connecting without requiring an interface table.
178          */
179
180         table = talloc_zero(mem_ctx, struct ndr_interface_table);
181
182         if (table == NULL) {
183                 PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
184                 return NULL;
185         }
186
187         if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
188                 return NULL;
189         }
190
191         status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, 
192                      table, credentials, event_ctx, lp_ctx);
193         if (NT_STATUS_IS_ERR(status)) {
194                 PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
195                 talloc_free(mem_ctx);
196                 return NULL;
197         }
198
199         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
200         return (PyObject *)ret;
201 }
202
203 PyTypeObject dcerpc_InterfaceType = {
204         PyObject_HEAD_INIT(NULL) 0,
205         .tp_name = "dcerpc.ClientConnection",
206         .tp_basicsize = sizeof(dcerpc_InterfaceObject),
207         .tp_dealloc = dcerpc_interface_dealloc,
208         .tp_getset = dcerpc_interface_getsetters,
209         .tp_methods = dcerpc_interface_methods,
210         .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
211 "\n"
212 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
213 "syntax should be a tuple with a GUID and version number of an interface\n"
214 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
215 "credentials should be a credentials.Credentials object.\n\n",
216         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
217         .tp_new = dcerpc_interface_new,
218 };
219
220 void initbase(void)
221 {
222         PyObject *m;
223
224         if (PyType_Ready(&dcerpc_InterfaceType) < 0)
225                 return;
226
227         m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
228         if (m == NULL)
229                 return;
230
231         Py_INCREF((PyObject *)&dcerpc_InterfaceType);
232         PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
233 }