r2986: Add correct value to dict when generating wrapper for functions that
[samba.git] / source4 / scripting / swig / dcerpc.i
1 /* Tastes like -*- C -*- */
2
3 /* 
4    Unix SMB/CIFS implementation.
5
6    Swig interface to librpc functions.
7
8    Copyright (C) Tim Potter 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 %module dcerpc
26
27 %{
28
29 /* This symbol is used in both includes.h and Python.h which causes an
30    annoying compiler warning. */
31
32 #ifdef HAVE_FSTAT
33 #undef HAVE_FSTAT
34 #endif
35
36 #include "includes.h"
37
38 #undef strcpy
39
40 PyObject *ntstatus_exception, *werror_exception;
41
42 /* Set up return of a dcerpc.NTSTATUS exception */
43
44 void set_ntstatus_exception(int status)
45 {
46         PyObject *obj = Py_BuildValue("(i,s)", status, 
47                                 nt_errstr(NT_STATUS(status)));
48
49         PyErr_SetObject(ntstatus_exception, obj);
50 }
51
52 void set_werror_exception(int status)
53 {
54         PyObject *obj = Py_BuildValue("(i,s)", status, 
55                                 win_errstr(W_ERROR(status)));
56
57         PyErr_SetObject(werror_exception, obj);
58 }
59
60 /* Conversion functions for scalar types */
61
62 uint8 uint8_from_python(PyObject *obj, char *name)
63 {
64         if (obj == NULL) {
65                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
66                 return 0;
67         }
68
69         if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
70                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
71                 return 0;
72         }
73
74         if (PyLong_Check(obj))
75                 return (uint8)PyLong_AsLong(obj);
76         else
77                 return (uint8)PyInt_AsLong(obj);
78 }
79
80 PyObject *uint8_to_python(uint8 obj)
81 {
82         return PyInt_FromLong(obj);
83 }
84
85 uint16 uint16_from_python(PyObject *obj, char *name)
86 {
87         if (obj == NULL) {
88                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
89                 return 0;
90         }
91
92         if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
93                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
94                 return 0;
95         }
96
97         if (PyLong_Check(obj))
98                 return (uint16)PyLong_AsLong(obj);
99         else
100                 return (uint16)PyInt_AsLong(obj);
101 }
102
103 PyObject *uint16_to_python(uint16 obj)
104 {
105         return PyInt_FromLong(obj);
106 }
107
108 uint32 uint32_from_python(PyObject *obj, char *name)
109 {
110         if (obj == NULL) {
111                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
112                 return 0;
113         }
114
115         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
116                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
117                 return 0;
118         }
119
120         if (PyLong_Check(obj))
121                 return (uint32)PyLong_AsLong(obj);
122         else
123                 return (uint32)PyInt_AsLong(obj);
124 }
125
126 PyObject *uint32_to_python(uint32 obj)
127 {
128         return PyLong_FromLong(obj);
129 }
130
131 int64 int64_from_python(PyObject *obj, char *name)
132 {
133         if (obj == NULL) {
134                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
135                 return 0;
136         }
137
138         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
139                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
140                 return 0;
141         }
142
143         if (PyLong_Check(obj))
144                 return (int64)PyLong_AsLongLong(obj);
145         else
146                 return (int64)PyInt_AsLong(obj);
147 }
148
149 PyObject *int64_to_python(int64 obj)
150 {
151         return PyLong_FromLongLong(obj);
152 }
153
154 uint64 uint64_from_python(PyObject *obj, char *name)
155 {
156         if (obj == NULL) {
157                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
158                 return 0;
159         }
160
161         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
162                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
163                 return 0;
164         }
165
166         if (PyLong_Check(obj))
167                 return (uint64)PyLong_AsUnsignedLongLong(obj);
168         else
169                 return (uint64)PyInt_AsLong(obj);
170 }
171
172 PyObject *uint64_to_python(uint64 obj)
173 {
174         return PyLong_FromUnsignedLongLong(obj);
175 }
176
177 NTTIME NTTIME_from_python(PyObject *obj, char *name)
178 {
179         if (obj == NULL) {
180                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
181                 return 0;
182         }
183
184         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
185                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
186                 return 0;
187         }
188
189         if (PyLong_Check(obj))
190                 return (NTTIME)PyLong_AsUnsignedLongLong(obj);
191         else
192                 return (NTTIME)PyInt_AsUnsignedLongMask(obj);
193 }
194
195 PyObject *NTTIME_to_python(NTTIME obj)
196 {
197         return PyLong_FromUnsignedLongLong(obj);
198 }
199
200 HYPER_T HYPER_T_from_python(PyObject *obj, char *name)
201 {
202         if (obj == NULL) {
203                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
204                 return 0;
205         }
206
207         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
208                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
209                 return 0;
210         }
211
212         if (PyLong_Check(obj))
213                 return (HYPER_T)PyLong_AsUnsignedLongLong(obj);
214         else
215                 return (HYPER_T)PyInt_AsUnsignedLongMask(obj);
216 }
217
218 PyObject *HYPER_T_to_python(HYPER_T obj)
219 {
220         return PyLong_FromUnsignedLongLong(obj);
221 }
222
223 /* Conversion functions for types that we don't want generated automatically.
224    This is mostly security realted stuff in misc.idl */
225
226 char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)
227 {
228         if (obj == NULL) {
229                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
230                 return NULL;
231         }
232
233         if (obj == Py_None)
234                 return NULL;
235
236         if (!PyString_Check(obj)) {
237                 PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name);
238                 return NULL;
239         }
240
241         return PyString_AsString(obj);
242 }
243
244 PyObject *string_ptr_to_python(TALLOC_CTX *mem_ctx, char *obj)
245 {
246         if (obj == NULL) {
247                 Py_INCREF(Py_None);
248                 return Py_None;
249         }
250
251         return PyString_FromString(obj);
252 }
253
254 #define dom_sid2_ptr_to_python dom_sid_ptr_to_python
255 #define dom_sid2_ptr_from_python dom_sid_ptr_from_python
256
257 void DATA_BLOB_ptr_from_python(TALLOC_CTX *mem_ctx, DATA_BLOB **s, 
258                                PyObject *obj, char *name)
259 {
260         if (obj == NULL) {
261                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
262                 return;
263         }
264
265         if (obj == Py_None) {
266                 *s = NULL;
267                 return;
268         }
269
270         if (!PyString_Check(obj)) {
271                 PyErr_Format(PyExc_TypeError, "Expecting string value for key '%s'", name);
272                 return;
273         }
274
275         *s = talloc(mem_ctx, sizeof(DATA_BLOB));
276
277         (*s)->length = PyString_Size(obj);
278         (*s)->data = PyString_AsString(obj);
279 }
280
281 PyObject *DATA_BLOB_to_python(DATA_BLOB obj)
282 {
283         return PyString_FromStringAndSize(obj.data, obj.length);
284 }
285
286 %}
287
288 %include "samba.i"
289
290 %pythoncode %{
291         NTSTATUS = _dcerpc.NTSTATUS
292         WERROR = _dcerpc.WERROR
293 %}
294
295 %init  %{
296         setup_logging("python", DEBUG_STDOUT);  
297         lp_load(dyn_CONFIGFILE, True, False, False);
298         load_interfaces();
299         ntstatus_exception = PyErr_NewException("_dcerpc.NTSTATUS", NULL, NULL);
300         werror_exception = PyErr_NewException("_dcerpc.WERROR", NULL, NULL);
301         PyDict_SetItemString(d, "NTSTATUS", ntstatus_exception);
302         PyDict_SetItemString(d, "WERROR", werror_exception);
303 %}
304
305 %typemap(in, numinputs=0) struct dcerpc_pipe **OUT (struct dcerpc_pipe *temp_dcerpc_pipe) {
306         $1 = &temp_dcerpc_pipe;
307 }
308
309 %typemap(in, numinputs=0) TALLOC_CTX * {
310         $1 = talloc_init("$symname");
311 }
312
313 %typemap(freearg) TALLOC_CTX * {
314         talloc_free($1);
315 }
316
317 %typemap(argout) struct dcerpc_pipe ** {
318         long status = PyLong_AsLong(resultobj);
319
320         /* Throw exception if result was not OK */
321
322         if (status != 0) {
323                 set_ntstatus_exception(status);
324                 return NULL;
325         }
326
327         /* Set REF_ALLOC flag so we don't have to do too much extra
328            mucking around with ref variables in ndr unmarshalling. */
329
330         (*$1)->flags |= DCERPC_NDR_REF_ALLOC;
331
332         /* Return swig handle on dcerpc_pipe */
333
334         resultobj = SWIG_NewPointerObj(*$1, SWIGTYPE_p_dcerpc_pipe, 0);
335 }
336
337 %types(struct dcerpc_pipe *);
338
339 %rename(pipe_connect) dcerpc_pipe_connect;
340
341 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **OUT,
342                              const char *binding,
343                              const char *pipe_uuid,
344                              uint32 pipe_version,
345                              const char *domain,
346                              const char *username,
347                              const char *password);
348
349 %include "librpc/gen_ndr/misc.i"
350
351 /* Wrapped functions returning NTSTATUS */
352
353 %exception {
354         $action
355
356         if (NT_STATUS_IS_ERR(result)) {
357                 set_ntstatus_exception(NT_STATUS_V(result));
358                 return NULL;
359         }
360 }
361
362 %include "librpc/gen_ndr/lsa.i"
363 %include "librpc/gen_ndr/samr.i"
364
365 /* Wrapped functions returning WERROR */
366
367 %exception {
368         $action
369
370         if (NT_STATUS_IS_ERR(result)) {
371                 set_ntstatus_exception(NT_STATUS_V(result));
372                 return NULL;
373         }
374
375         /* Emulate NT_STATUS_IS_ERR() */
376
377         if (!W_ERROR_IS_OK(arg3->out.result) && 
378             !(W_ERROR_EQUAL(arg3->out.result, WERR_INSUFFICIENT_BUFFER))) {
379                 set_werror_exception(W_ERROR_V(arg3->out.result));
380                 return NULL;
381         }
382 }
383
384 %include "librpc/gen_ndr/winreg.i"
385 %include "librpc/gen_ndr/spoolss.i"
386
387 /* The status codes must be included last otherwise the automatically
388    generated .i files get confused.  This is kind of yucky. */
389
390 %include "status_codes.i"