r2415: Throw a TypeError exception if a scalar value doesn't have the correct
[samba.git] / source4 / scripting / swig / dcerpc.i
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Swig interface to librpc functions.
5
6    Copyright (C) Tim Potter 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 %module dcerpc
24
25 %{
26
27 /* This symbol is used in both includes.h and Python.h which causes an
28    annoying compiler warning. */
29
30 #ifdef HAVE_FSTAT
31 #undef HAVE_FSTAT
32 #endif
33
34 #include "includes.h"
35
36 #undef strcpy
37
38 PyObject *ntstatus_exception;
39
40 /* Set up return of a dcerpc.NTSTATUS exception */
41
42 void set_ntstatus_exception(int status)
43 {
44         PyObject *obj = Py_BuildValue("(i,s)", status, 
45                                 nt_errstr(NT_STATUS(status)));
46
47         PyErr_SetObject(ntstatus_exception, obj);
48 }
49
50 /* Conversion functions for scalar types */
51
52 uint8 uint8_from_python(PyObject *obj, char *name)
53 {
54         if (!PyInt_Check(obj)) {
55                 PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
56                 return 0;
57         }
58
59         return (uint8)PyInt_AsLong(obj);
60 }
61
62 PyObject *uint8_to_python(uint8 obj)
63 {
64         return PyInt_FromLong(obj);
65 }
66
67 uint16 uint16_from_python(PyObject *obj, char *name)
68 {
69         if (!PyInt_Check(obj)) {
70                 PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
71                 return 0;
72         }
73
74         return (uint16)PyInt_AsLong(obj);
75 }
76
77 PyObject *uint16_to_python(uint16 obj)
78 {
79         return PyInt_FromLong(obj);
80 }
81
82 uint32 uint32_from_python(PyObject *obj, char *name)
83 {
84         if (!PyInt_Check(obj)) {
85                 PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
86                 return 0;
87         }
88
89         return (uint32)PyInt_AsLong(obj);
90 }
91
92 PyObject *uint32_to_python(uint32 obj)
93 {
94         return PyInt_FromLong(obj);
95 }
96
97 int64 int64_from_python(PyObject *obj, char *name)
98 {
99         if (!PyInt_Check(obj)) {
100                 PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
101                 return 0;
102         }
103
104         return (int64)PyLong_AsLong(obj);
105 }
106
107 PyObject *int64_to_python(int64 obj)
108 {
109         return PyLong_FromLong(obj);
110 }
111
112 uint64 uint64_from_python(PyObject *obj, char *name)
113 {
114         if (!PyInt_Check(obj)) {
115                 PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
116                 return 0;
117         }
118
119         return (uint64)PyLong_AsLong(obj);
120 }
121
122 PyObject *uint64_to_python(uint64 obj)
123 {
124         return PyLong_FromLong(obj);
125 }
126
127 NTTIME NTTIME_from_python(PyObject *obj, char *name)
128 {
129         if (!PyInt_Check(obj)) {
130                 PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name);
131                 return 0;
132         }
133
134         return (NTTIME)PyLong_AsLong(obj);
135 }
136
137 PyObject *NTTIME_to_python(NTTIME obj)
138 {
139         return PyLong_FromLong(obj);
140 }
141
142 HYPER_T HYPER_T_from_python(PyObject *obj, char *name)
143 {
144         if (!PyInt_Check(obj)) {
145                 PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name);
146                 return 0;
147         }
148
149         return (HYPER_T)PyLong_AsLong(obj);
150 }
151
152 PyObject *HYPER_T_to_python(HYPER_T obj)
153 {
154         return PyLong_FromLong(obj);
155 }
156
157 /* Conversion functions for types that we don't want generated automatically.
158    This is mostly security realted stuff in misc.idl */
159
160 char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)
161 {
162         if (obj == Py_None)
163                 return NULL;
164
165         if (!PyString_Check(obj)) {
166                 PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name);
167                 return 0;
168         }
169
170         return PyString_AsString(obj);
171 }
172
173 PyObject *string_ptr_to_python(TALLOC_CTX *mem_ctx, char *obj)
174 {
175         if (obj == NULL) {
176                 Py_INCREF(Py_None);
177                 return Py_None;
178         }
179
180         return PyString_FromString(obj);
181 }
182
183 #define dom_sid2_ptr_to_python dom_sid_ptr_to_python
184 #define dom_sid2_ptr_from_python dom_sid_ptr_from_python
185
186 %}
187
188 %include "samba.i"
189
190 %init  %{
191         setup_logging("python", DEBUG_STDOUT);  
192         lp_load(dyn_CONFIGFILE, True, False, False);
193         load_interfaces();
194         ntstatus_exception = PyErr_NewException("dcerpc.NTSTATUS", NULL, NULL);
195 %}
196
197 %typemap(in, numinputs=0) struct dcerpc_pipe **OUT (struct dcerpc_pipe *temp_dcerpc_pipe) {
198         $1 = &temp_dcerpc_pipe;
199 }
200
201 %typemap(in, numinputs=0) TALLOC_CTX * {
202         $1 = talloc_init("$symname");
203 }
204
205 %typemap(freearg) TALLOC_CTX * {
206         talloc_free($1);
207 }
208
209 %typemap(argout) struct dcerpc_pipe ** {
210         long status = PyLong_AsLong(resultobj);
211
212         /* Throw exception if result was not OK */
213
214         if (status != 0) {
215                 set_ntstatus_exception(status);
216                 return NULL;
217         }
218
219         /* Set REF_ALLOC flag so we don't have to do too much extra
220            mucking around with ref variables in ndr unmarshalling. */
221
222         (*$1)->flags |= DCERPC_NDR_REF_ALLOC;
223
224         /* Return swig handle on dcerpc_pipe */
225
226         resultobj = SWIG_NewPointerObj(*$1, SWIGTYPE_p_dcerpc_pipe, 0);
227 }
228
229 %types(struct dcerpc_pipe *);
230
231 %rename(pipe_connect) dcerpc_pipe_connect;
232
233 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **OUT,
234                              const char *binding,
235                              const char *pipe_uuid,
236                              uint32 pipe_version,
237                              const char *domain,
238                              const char *username,
239                              const char *password);
240
241 %include "librpc/gen_ndr/misc.i"
242 %include "librpc/gen_ndr/lsa.i"
243 %include "librpc/gen_ndr/samr.i"