r3505: #include dynconfig.h
[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 #include "dynconfig.h"
38
39 #undef strcpy
40
41 PyObject *ntstatus_exception, *werror_exception;
42
43 /* Set up return of a dcerpc.NTSTATUS exception */
44
45 void set_ntstatus_exception(int status)
46 {
47         PyObject *obj = Py_BuildValue("(i,s)", status, 
48                                 nt_errstr(NT_STATUS(status)));
49
50         PyErr_SetObject(ntstatus_exception, obj);
51 }
52
53 void set_werror_exception(int status)
54 {
55         PyObject *obj = Py_BuildValue("(i,s)", status, 
56                                 win_errstr(W_ERROR(status)));
57
58         PyErr_SetObject(werror_exception, obj);
59 }
60
61 /* Conversion functions for scalar types */
62
63 uint8 uint8_from_python(PyObject *obj, char *name)
64 {
65         if (obj == NULL) {
66                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
67                 return 0;
68         }
69
70         if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
71                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
72                 return 0;
73         }
74
75         if (PyLong_Check(obj))
76                 return (uint8)PyLong_AsLong(obj);
77         else
78                 return (uint8)PyInt_AsLong(obj);
79 }
80
81 PyObject *uint8_to_python(uint8 obj)
82 {
83         return PyInt_FromLong(obj);
84 }
85
86 uint16 uint16_from_python(PyObject *obj, char *name)
87 {
88         if (obj == NULL) {
89                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
90                 return 0;
91         }
92
93         if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
94                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
95                 return 0;
96         }
97
98         if (PyLong_Check(obj))
99                 return (uint16)PyLong_AsLong(obj);
100         else
101                 return (uint16)PyInt_AsLong(obj);
102 }
103
104 PyObject *uint16_to_python(uint16 obj)
105 {
106         return PyInt_FromLong(obj);
107 }
108
109 uint32 uint32_from_python(PyObject *obj, char *name)
110 {
111         if (obj == NULL) {
112                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
113                 return 0;
114         }
115
116         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
117                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
118                 return 0;
119         }
120
121         if (PyLong_Check(obj))
122                 return (uint32)PyLong_AsLong(obj);
123         else
124                 return (uint32)PyInt_AsLong(obj);
125 }
126
127 PyObject *uint32_to_python(uint32 obj)
128 {
129         return PyLong_FromLong(obj);
130 }
131
132 int64 int64_from_python(PyObject *obj, char *name)
133 {
134         if (obj == NULL) {
135                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
136                 return 0;
137         }
138
139         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
140                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
141                 return 0;
142         }
143
144         if (PyLong_Check(obj))
145                 return (int64)PyLong_AsLongLong(obj);
146         else
147                 return (int64)PyInt_AsLong(obj);
148 }
149
150 PyObject *int64_to_python(int64 obj)
151 {
152         return PyLong_FromLongLong(obj);
153 }
154
155 uint64 uint64_from_python(PyObject *obj, char *name)
156 {
157         if (obj == NULL) {
158                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
159                 return 0;
160         }
161
162         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
163                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
164                 return 0;
165         }
166
167         if (PyLong_Check(obj))
168                 return (uint64)PyLong_AsUnsignedLongLong(obj);
169         else
170                 return (uint64)PyInt_AsLong(obj);
171 }
172
173 PyObject *uint64_to_python(uint64 obj)
174 {
175         return PyLong_FromUnsignedLongLong(obj);
176 }
177
178 NTTIME NTTIME_from_python(PyObject *obj, char *name)
179 {
180         if (obj == NULL) {
181                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
182                 return 0;
183         }
184
185         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
186                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
187                 return 0;
188         }
189
190         if (PyLong_Check(obj))
191                 return (NTTIME)PyLong_AsUnsignedLongLong(obj);
192         else
193                 return (NTTIME)PyInt_AsUnsignedLongMask(obj);
194 }
195
196 PyObject *NTTIME_to_python(NTTIME obj)
197 {
198         return PyLong_FromUnsignedLongLong(obj);
199 }
200
201 time_t time_t_from_python(PyObject *obj, char *name)
202 {
203         if (obj == NULL) {
204                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
205                 return 0;
206         }
207
208         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
209                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
210                 return 0;
211         }
212
213         if (PyLong_Check(obj))
214                 return (time_t)PyLong_AsUnsignedLongLong(obj);
215         else
216                 return (time_t)PyInt_AsUnsignedLongMask(obj);
217 }
218
219 PyObject *time_t_to_python(time_t obj)
220 {
221         return PyLong_FromUnsignedLongLong(obj);
222 }
223
224 HYPER_T HYPER_T_from_python(PyObject *obj, char *name)
225 {
226         if (obj == NULL) {
227                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
228                 return 0;
229         }
230
231         if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
232                 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
233                 return 0;
234         }
235
236         if (PyLong_Check(obj))
237                 return (HYPER_T)PyLong_AsUnsignedLongLong(obj);
238         else
239                 return (HYPER_T)PyInt_AsUnsignedLongMask(obj);
240 }
241
242 PyObject *HYPER_T_to_python(HYPER_T obj)
243 {
244         return PyLong_FromUnsignedLongLong(obj);
245 }
246
247 /* Conversion functions for types that we don't want generated automatically.
248    This is mostly security realted stuff in misc.idl */
249
250 char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)
251 {
252         if (obj == NULL) {
253                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
254                 return NULL;
255         }
256
257         if (obj == Py_None)
258                 return NULL;
259
260         if (!PyString_Check(obj)) {
261                 PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name);
262                 return NULL;
263         }
264
265         return PyString_AsString(obj);
266 }
267
268 PyObject *string_ptr_to_python(TALLOC_CTX *mem_ctx, char *obj)
269 {
270         if (obj == NULL) {
271                 Py_INCREF(Py_None);
272                 return Py_None;
273         }
274
275         return PyString_FromString(obj);
276 }
277
278 #define dom_sid2_ptr_to_python dom_sid_ptr_to_python
279 #define dom_sid2_ptr_from_python dom_sid_ptr_from_python
280
281 void DATA_BLOB_from_python(TALLOC_CTX *mem_ctx, DATA_BLOB *s,
282                            PyObject *obj, char name)
283 {
284         if (obj == NULL) {
285                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
286                 return;
287         }
288
289         if (!PyString_Check(obj)) {
290                 PyErr_Format(PyExc_TypeError, "Expecting string value for key '%s'", name);
291                 return;
292         }
293
294         s->length = PyString_Size(obj);
295         s->data = PyString_AsString(obj);
296 }
297
298 void DATA_BLOB_ptr_from_python(TALLOC_CTX *mem_ctx, DATA_BLOB **s, 
299                                PyObject *obj, char *name)
300 {
301         if (obj == NULL) {
302                 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
303                 return;
304         }
305
306         if (obj == Py_None) {
307                 *s = NULL;
308                 return;
309         }
310
311         if (!PyString_Check(obj)) {
312                 PyErr_Format(PyExc_TypeError, "Expecting string value for key '%s'", name);
313                 return;
314         }
315
316         *s = talloc(mem_ctx, sizeof(DATA_BLOB));
317
318         (*s)->length = PyString_Size(obj);
319         (*s)->data = PyString_AsString(obj);
320 }
321
322 PyObject *DATA_BLOB_to_python(DATA_BLOB obj)
323 {
324         return PyString_FromStringAndSize(obj.data, obj.length);
325 }
326
327 %}
328
329 %include "samba.i"
330
331 %pythoncode %{
332         NTSTATUS = _dcerpc.NTSTATUS
333         WERROR = _dcerpc.WERROR
334 %}
335
336 %init  %{
337         setup_logging("python", DEBUG_STDOUT);  
338         lp_load(dyn_CONFIGFILE, True, False, False);
339         load_interfaces();
340         ntstatus_exception = PyErr_NewException("_dcerpc.NTSTATUS", NULL, NULL);
341         werror_exception = PyErr_NewException("_dcerpc.WERROR", NULL, NULL);
342         PyDict_SetItemString(d, "NTSTATUS", ntstatus_exception);
343         PyDict_SetItemString(d, "WERROR", werror_exception);
344 %}
345
346 %typemap(in, numinputs=0) struct dcerpc_pipe **OUT (struct dcerpc_pipe *temp_dcerpc_pipe) {
347         $1 = &temp_dcerpc_pipe;
348 }
349
350 %typemap(in, numinputs=0) TALLOC_CTX * {
351         $1 = talloc_init("$symname");
352 }
353
354 %typemap(freearg) TALLOC_CTX * {
355         talloc_free($1);
356 }
357
358 %typemap(argout) struct dcerpc_pipe ** {
359         long status = PyLong_AsLong(resultobj);
360
361         /* Throw exception if result was not OK */
362
363         if (status != 0) {
364                 set_ntstatus_exception(status);
365                 return NULL;
366         }
367
368         /* Set REF_ALLOC flag so we don't have to do too much extra
369            mucking around with ref variables in ndr unmarshalling. */
370
371         (*$1)->flags |= DCERPC_NDR_REF_ALLOC;
372
373         /* Return swig handle on dcerpc_pipe */
374
375         resultobj = SWIG_NewPointerObj(*$1, SWIGTYPE_p_dcerpc_pipe, 0);
376 }
377
378 %types(struct dcerpc_pipe *);
379
380 %rename(pipe_connect) dcerpc_pipe_connect;
381
382 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **OUT,
383                              const char *binding,
384                              const char *pipe_uuid,
385                              uint32 pipe_version,
386                              const char *domain,
387                              const char *username,
388                              const char *password);
389
390 %typemap(in) DATA_BLOB * (DATA_BLOB temp_data_blob) {
391         temp_data_blob.data = PyString_AsString($input);
392         temp_data_blob.length = PyString_Size($input);
393         $1 = &temp_data_blob;
394 }
395
396 const char *dcerpc_server_name(struct dcerpc_pipe *p);
397
398 %{
399 #include "librpc/gen_ndr/ndr_misc.h"
400 #include "librpc/gen_ndr/ndr_lsa.h"
401 #include "librpc/gen_ndr/ndr_samr.h"
402 #include "librpc/gen_ndr/ndr_winreg.h"
403 #include "librpc/gen_ndr/ndr_spoolss.h"
404 %}
405
406 %include "librpc/gen_ndr/misc.i"
407 %include "librpc/gen_ndr/lsa.i"
408 %include "librpc/gen_ndr/samr.i"
409 %include "librpc/gen_ndr/winreg.i"
410 %include "librpc/gen_ndr/spoolss.i"
411
412 /* The status codes must be included last otherwise the automatically
413    generated .i files get confused.  This is kind of yucky. */
414
415 %include "status_codes.i"