33b3c63571da547fb3c178f1e0d7da5bd2c49e5d
[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         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                 PyErr_SetNTSTATUS(status);
69                 talloc_free(mem_ctx);
70                 return NULL;
71         }
72
73         ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
74
75         talloc_free(mem_ctx);
76         return ret;
77 }
78
79 static PyMethodDef dcerpc_interface_methods[] = {
80         { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data) -> data\nMake a raw request" },
81         { NULL, NULL, 0, NULL },
82 };
83
84
85 static void dcerpc_interface_dealloc(PyObject* self)
86 {
87         dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
88         talloc_free(interface->pipe);
89         PyObject_Del(self);
90 }
91
92 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
93 {
94         dcerpc_InterfaceObject *ret;
95         const char *binding_string;
96         struct cli_credentials *credentials;
97         struct loadparm_context *lp_ctx = NULL;
98         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
99         TALLOC_CTX *mem_ctx = NULL;
100         struct event_context *event_ctx;
101         NTSTATUS status;
102
103         PyObject *syntax;
104         const char *kwnames[] = {
105                 "binding", "syntax", "lp_ctx", "credentials", NULL
106         };
107         extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
108         extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
109
110         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials)) {
111                 return NULL;
112         }
113
114         lp_ctx = lp_from_py_object(py_lp_ctx);
115         if (lp_ctx == NULL) {
116                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
117                 return NULL;
118         }
119
120         credentials = cli_credentials_from_py_object(py_credentials);
121         if (credentials == NULL) {
122                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
123                 return NULL;
124         }
125         ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
126
127         event_ctx = event_context_init(mem_ctx);
128
129         status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, 
130                      NULL, credentials, event_ctx, lp_ctx);
131         if (NT_STATUS_IS_ERR(status)) {
132                 PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
133                 talloc_free(mem_ctx);
134                 return NULL;
135         }
136
137         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
138         return (PyObject *)ret;
139 }
140
141 PyTypeObject dcerpc_InterfaceType = {
142         PyObject_HEAD_INIT(NULL) 0,
143         .tp_name = "dcerpc.ClientConnection",
144         .tp_basicsize = sizeof(dcerpc_InterfaceObject),
145         .tp_dealloc = dcerpc_interface_dealloc,
146         .tp_getset = dcerpc_interface_getsetters,
147         .tp_methods = dcerpc_interface_methods,
148         .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
149 "\n"
150 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
151 "syntax should be a tuple with a GUID and version number of an interface\n"
152 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
153 "credentials should be a credentials.Credentials object.\n\n",
154         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
155         .tp_new = dcerpc_interface_new,
156 };
157
158 void initbase(void)
159 {
160         PyObject *m;
161
162         if (PyType_Ready(&dcerpc_InterfaceType) < 0)
163                 return;
164
165         m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
166         if (m == NULL)
167                 return;
168
169         Py_INCREF((PyObject *)&dcerpc_InterfaceType);
170         PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
171 }