1 /* Tastes like -*- C -*- */
4 Unix SMB/CIFS implementation.
6 Swig interface to librpc functions.
8 Copyright (C) Tim Potter 2004
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.
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.
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.
29 /* This symbol is used in both includes.h and Python.h which causes an
30 annoying compiler warning. */
40 PyObject *ntstatus_exception, *werror_exception;
42 /* Set up return of a dcerpc.NTSTATUS exception */
44 void set_ntstatus_exception(int status)
46 PyObject *obj = Py_BuildValue("(i,s)", status,
47 nt_errstr(NT_STATUS(status)));
49 PyErr_SetObject(ntstatus_exception, obj);
52 void set_werror_exception(int status)
54 PyObject *obj = Py_BuildValue("(i,s)", status,
55 win_errstr(W_ERROR(status)));
57 PyErr_SetObject(werror_exception, obj);
60 /* Conversion functions for scalar types */
62 uint8 uint8_from_python(PyObject *obj, char *name)
65 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
69 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
70 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
74 if (PyLong_Check(obj))
75 return (uint8)PyLong_AsLong(obj);
77 return (uint8)PyInt_AsLong(obj);
80 PyObject *uint8_to_python(uint8 obj)
82 return PyInt_FromLong(obj);
85 uint16 uint16_from_python(PyObject *obj, char *name)
88 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
92 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
93 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
97 if (PyLong_Check(obj))
98 return (uint16)PyLong_AsLong(obj);
100 return (uint16)PyInt_AsLong(obj);
103 PyObject *uint16_to_python(uint16 obj)
105 return PyInt_FromLong(obj);
108 uint32 uint32_from_python(PyObject *obj, char *name)
111 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
115 if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
116 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
120 if (PyLong_Check(obj))
121 return (uint32)PyLong_AsLong(obj);
123 return (uint32)PyInt_AsLong(obj);
126 PyObject *uint32_to_python(uint32 obj)
128 return PyLong_FromLong(obj);
131 int64 int64_from_python(PyObject *obj, char *name)
134 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
138 if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
139 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
143 if (PyLong_Check(obj))
144 return (int64)PyLong_AsLongLong(obj);
146 return (int64)PyInt_AsLong(obj);
149 PyObject *int64_to_python(int64 obj)
151 return PyLong_FromLongLong(obj);
154 uint64 uint64_from_python(PyObject *obj, char *name)
157 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
161 if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
162 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
166 if (PyLong_Check(obj))
167 return (uint64)PyLong_AsUnsignedLongLong(obj);
169 return (uint64)PyInt_AsLong(obj);
172 PyObject *uint64_to_python(uint64 obj)
174 return PyLong_FromUnsignedLongLong(obj);
177 NTTIME NTTIME_from_python(PyObject *obj, char *name)
180 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
184 if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
185 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
189 if (PyLong_Check(obj))
190 return (NTTIME)PyLong_AsUnsignedLongLong(obj);
192 return (NTTIME)PyInt_AsUnsignedLongMask(obj);
195 PyObject *NTTIME_to_python(NTTIME obj)
197 return PyLong_FromUnsignedLongLong(obj);
200 HYPER_T HYPER_T_from_python(PyObject *obj, char *name)
203 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
207 if (!PyLong_Check(obj) && !PyInt_Check(obj)) {
208 PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name);
212 if (PyLong_Check(obj))
213 return (HYPER_T)PyLong_AsUnsignedLongLong(obj);
215 return (HYPER_T)PyInt_AsUnsignedLongMask(obj);
218 PyObject *HYPER_T_to_python(HYPER_T obj)
220 return PyLong_FromUnsignedLongLong(obj);
223 /* Conversion functions for types that we don't want generated automatically.
224 This is mostly security realted stuff in misc.idl */
226 char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)
229 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
236 if (!PyString_Check(obj)) {
237 PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name);
241 return PyString_AsString(obj);
244 PyObject *string_ptr_to_python(TALLOC_CTX *mem_ctx, char *obj)
251 return PyString_FromString(obj);
254 #define dom_sid2_ptr_to_python dom_sid_ptr_to_python
255 #define dom_sid2_ptr_from_python dom_sid_ptr_from_python
257 void DATA_BLOB_ptr_from_python(TALLOC_CTX *mem_ctx, DATA_BLOB **s,
258 PyObject *obj, char *name)
261 PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
265 if (obj == Py_None) {
270 if (!PyString_Check(obj)) {
271 PyErr_Format(PyExc_TypeError, "Expecting string value for key '%s'", name);
275 *s = talloc(mem_ctx, sizeof(DATA_BLOB));
277 (*s)->length = PyString_Size(obj);
278 (*s)->data = PyString_AsString(obj);
281 PyObject *DATA_BLOB_to_python(DATA_BLOB obj)
283 return PyString_FromStringAndSize(obj.data, obj.length);
291 NTSTATUS = _dcerpc.NTSTATUS
292 WERROR = _dcerpc.WERROR
296 setup_logging("python", DEBUG_STDOUT);
297 lp_load(dyn_CONFIGFILE, True, False, False);
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);
305 %typemap(in, numinputs=0) struct dcerpc_pipe **OUT (struct dcerpc_pipe *temp_dcerpc_pipe) {
306 $1 = &temp_dcerpc_pipe;
309 %typemap(in, numinputs=0) TALLOC_CTX * {
310 $1 = talloc_init("$symname");
313 %typemap(freearg) TALLOC_CTX * {
317 %typemap(argout) struct dcerpc_pipe ** {
318 long status = PyLong_AsLong(resultobj);
320 /* Throw exception if result was not OK */
323 set_ntstatus_exception(status);
327 /* Set REF_ALLOC flag so we don't have to do too much extra
328 mucking around with ref variables in ndr unmarshalling. */
330 (*$1)->flags |= DCERPC_NDR_REF_ALLOC;
332 /* Return swig handle on dcerpc_pipe */
334 resultobj = SWIG_NewPointerObj(*$1, SWIGTYPE_p_dcerpc_pipe, 0);
337 %types(struct dcerpc_pipe *);
339 %rename(pipe_connect) dcerpc_pipe_connect;
341 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **OUT,
343 const char *pipe_uuid,
346 const char *username,
347 const char *password);
349 %include "librpc/gen_ndr/misc.i"
351 /* Wrapped functions returning NTSTATUS */
356 if (NT_STATUS_IS_ERR(result)) {
357 set_ntstatus_exception(NT_STATUS_V(result));
362 %include "librpc/gen_ndr/lsa.i"
363 %include "librpc/gen_ndr/samr.i"
365 /* Wrapped functions returning WERROR */
370 if (NT_STATUS_IS_ERR(result)) {
371 set_ntstatus_exception(NT_STATUS_V(result));
375 /* Emulate NT_STATUS_IS_ERR() */
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));
384 %include "librpc/gen_ndr/winreg.i"
385 %include "librpc/gen_ndr/spoolss.i"
387 /* The status codes must be included last otherwise the automatically
388 generated .i files get confused. This is kind of yucky. */
390 %include "status_codes.i"