pyrpc: Add prototype for init function.
[kamenim/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 <Python.h>
21 #include "includes.h"
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
30 void initbase(void);
31
32 staticforward PyTypeObject dcerpc_InterfaceType;
33
34 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
35 {
36         NTSTATUS status;
37         status = GUID_from_string(PyString_AsString(object), uuid);
38         if (NT_STATUS_IS_ERR(status)) {
39                 PyErr_SetNTSTATUS(status);
40                 return false;
41         }
42         return true;
43 }
44
45 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
46 {
47         ZERO_STRUCTP(syntax_id);
48
49         if (PyString_Check(object)) {
50                 return PyString_AsGUID(object, &syntax_id->uuid);
51         } else if (PyTuple_Check(object)) {
52                 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
53                         PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
54                         return false;
55                 }
56
57                 if (!PyString_Check(PyTuple_GetItem(object, 0))) {
58                         PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
59                         return false;
60                 }
61
62                 if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) 
63                         return false;
64
65                 if (!PyInt_Check(PyTuple_GetItem(object, 1))) {
66                         PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
67                         return false;
68                 }
69
70                 syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1));
71                 return true;
72         }
73
74         PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
75         return false;
76 }
77
78 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
79 {
80         const char *server_name;
81         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
82
83         server_name = dcerpc_server_name(iface->pipe);
84         if (server_name == NULL)
85                 Py_RETURN_NONE;
86
87         return PyString_FromString(server_name);
88 }
89
90 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
91 {
92         PyObject *ret;
93         char *uuid_str;
94
95         uuid_str = GUID_string(NULL, &syntax_id->uuid);
96         if (uuid_str == NULL)
97                 return NULL;
98
99         ret = Py_BuildValue("(s,i)", uuid_str, syntax_id->if_version);
100
101         talloc_free(uuid_str);
102
103         return ret;
104 }
105
106 static PyObject *py_iface_abstract_syntax(PyObject *obj, void *closure)
107 {
108         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
109
110         return py_ndr_syntax_id(&iface->pipe->syntax);
111 }
112
113 static PyObject *py_iface_transfer_syntax(PyObject *obj, void *closure)
114 {
115         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
116
117         return py_ndr_syntax_id(&iface->pipe->transfer_syntax);
118 }
119
120 static PyGetSetDef dcerpc_interface_getsetters[] = {
121         { discard_const_p(char, "server_name"), py_iface_server_name, NULL,
122           discard_const_p(char, "name of the server, if connected over SMB") },
123         { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax, NULL, 
124           discard_const_p(char, "syntax id of the abstract syntax") },
125         { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax, NULL, 
126           discard_const_p(char, "syntax id of the transfersyntax") },
127         { NULL }
128 };
129
130 static PyMemberDef dcerpc_interface_members[] = {
131         { discard_const_p(char, "request_timeout"), T_INT, 
132           offsetof(struct dcerpc_pipe, request_timeout), 0,
133           discard_const_p(char, "request timeout, in seconds") },
134         { NULL }
135 };
136
137 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
138 {
139         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
140         int opnum;
141         DATA_BLOB data_in, data_out;
142         NTSTATUS status;
143         char *in_data;
144         int in_length;
145         PyObject *ret;
146         PyObject *object = NULL;
147         struct GUID object_guid;
148         TALLOC_CTX *mem_ctx = talloc_new(NULL);
149         const char *kwnames[] = { "opnum", "data", "object", NULL };
150
151         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request", 
152                 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
153                 return NULL;
154         }
155
156         data_in.data = (uint8_t *)talloc_memdup(mem_ctx, in_data, in_length);
157         data_in.length = in_length;
158
159         ZERO_STRUCT(data_out);
160
161         if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
162                 return NULL;
163         }
164
165         status = dcerpc_request(iface->pipe, object?&object_guid:NULL,
166                                 opnum, mem_ctx, &data_in, &data_out);
167
168         if (!NT_STATUS_IS_OK(status)) {
169                 PyErr_SetDCERPCStatus(iface->pipe, status);
170                 talloc_free(mem_ctx);
171                 return NULL;
172         }
173
174         ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
175
176         talloc_free(mem_ctx);
177         return ret;
178 }
179
180 static PyObject *py_iface_alter_context(PyObject *self, PyObject *args, PyObject *kwargs)
181 {
182         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
183         NTSTATUS status;
184         const char *kwnames[] = { "abstract_syntax", "transfer_syntax", NULL };
185         PyObject *py_abstract_syntax = Py_None, *py_transfer_syntax = Py_None;
186         struct ndr_syntax_id abstract_syntax, transfer_syntax;
187
188         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:alter_context", 
189                 discard_const_p(char *, kwnames), &py_abstract_syntax,
190                 &py_transfer_syntax)) {
191                 return NULL;
192         }
193
194         if (!ndr_syntax_from_py_object(py_abstract_syntax, &abstract_syntax))
195                 return NULL;
196
197         if (py_transfer_syntax == Py_None) {
198                 transfer_syntax = ndr_transfer_syntax;
199         } else {
200                 if (!ndr_syntax_from_py_object(py_transfer_syntax, 
201                                                &transfer_syntax))
202                         return NULL;
203         }
204
205         status = dcerpc_alter_context(iface->pipe, iface->pipe, &abstract_syntax, 
206                                       &transfer_syntax);
207
208         if (!NT_STATUS_IS_OK(status)) {
209                 PyErr_SetDCERPCStatus(iface->pipe, status);
210                 return NULL;
211         }
212
213         Py_RETURN_NONE;
214 }
215
216 static PyMethodDef dcerpc_interface_methods[] = {
217         { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
218         { "alter_context", (PyCFunction)py_iface_alter_context, METH_VARARGS|METH_KEYWORDS, "S.alter_context(syntax)\nChange to a different interface" },
219         { NULL, NULL, 0, NULL },
220 };
221
222
223 static void dcerpc_interface_dealloc(PyObject* self)
224 {
225         dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
226         talloc_free(interface->mem_ctx);
227         PyObject_Del(self);
228 }
229
230 static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
231 {
232         dcerpc_InterfaceObject *ret;
233         const char *binding_string;
234         struct cli_credentials *credentials;
235         struct loadparm_context *lp_ctx = NULL;
236         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
237         TALLOC_CTX *mem_ctx;
238         struct tevent_context *event_ctx;
239         NTSTATUS status;
240
241         PyObject *syntax, *py_basis = Py_None;
242         const char *kwnames[] = {
243                 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
244         };
245         struct ndr_interface_table *table;
246
247         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) {
248                 return NULL;
249         }
250
251         mem_ctx = talloc_new(NULL);
252         if (mem_ctx == NULL) {
253                 PyErr_NoMemory();
254                 return NULL;
255         }
256
257         lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
258         if (lp_ctx == NULL) {
259                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
260                 talloc_free(mem_ctx);
261                 return NULL;
262         }
263
264         credentials = cli_credentials_from_py_object(py_credentials);
265         if (credentials == NULL) {
266                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
267                 talloc_free(mem_ctx);
268                 return NULL;
269         }
270         ret = PyObject_New(dcerpc_InterfaceObject, type);
271         ret->mem_ctx = mem_ctx;
272
273         event_ctx = s4_event_context_init(ret->mem_ctx);
274
275         /* Create a dummy interface table struct. TODO: In the future, we should
276          * rather just allow connecting without requiring an interface table.
277          */
278
279         table = talloc_zero(ret->mem_ctx, struct ndr_interface_table);
280
281         if (table == NULL) {
282                 PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
283                 talloc_free(mem_ctx);
284                 return NULL;
285         }
286
287         if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
288                 talloc_free(mem_ctx);
289                 return NULL;
290         }
291
292         ret->pipe = NULL;
293         ret->binding_handle = NULL;
294
295         if (py_basis != Py_None) {
296                 struct dcerpc_pipe *base_pipe;
297
298                 if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {
299                         PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection");
300                         talloc_free(mem_ctx);
301                         return NULL;
302                 }
303
304                 base_pipe = talloc_reference(ret->mem_ctx, 
305                                          ((dcerpc_InterfaceObject *)py_basis)->pipe);
306
307                 status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
308
309                 ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
310         } else {
311                 status = dcerpc_pipe_connect(ret->mem_ctx, &ret->pipe, binding_string, 
312                              table, credentials, event_ctx, lp_ctx);
313         }
314
315         if (!NT_STATUS_IS_OK(status)) {
316                 PyErr_SetDCERPCStatus(ret->pipe, status);
317                 talloc_free(ret->mem_ctx);
318                 return NULL;
319         }
320         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
321         ret->binding_handle = ret->pipe->binding_handle;
322         return (PyObject *)ret;
323 }
324
325 static PyTypeObject dcerpc_InterfaceType = {
326         PyObject_HEAD_INIT(NULL) 0,
327         .tp_name = "dcerpc.ClientConnection",
328         .tp_basicsize = sizeof(dcerpc_InterfaceObject),
329         .tp_dealloc = dcerpc_interface_dealloc,
330         .tp_getset = dcerpc_interface_getsetters,
331         .tp_members = dcerpc_interface_members,
332         .tp_methods = dcerpc_interface_methods,
333         .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
334 "\n"
335 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
336 "syntax should be a tuple with a GUID and version number of an interface\n"
337 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
338 "credentials should be a credentials.Credentials object.\n\n",
339         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
340         .tp_new = dcerpc_interface_new,
341 };
342
343 void initbase(void)
344 {
345         PyObject *m;
346
347         if (PyType_Ready(&dcerpc_InterfaceType) < 0)
348                 return;
349
350         m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
351         if (m == NULL)
352                 return;
353
354         Py_INCREF((PyObject *)&dcerpc_InterfaceType);
355         PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
356 }