Remove use of the "staticforward" macro
[metze/samba/wip.git] / lib / ldb-samba / pyldb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to ldb, Samba-specific functions
5
6    Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 3 of the License, or (at your option) any later version.
12
13    This library 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 GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <Python.h>
23 #include "includes.h"
24 #include <ldb.h>
25 #include <pyldb.h>
26 #include "param/pyparam.h"
27 #include "auth/credentials/pycredentials.h"
28 #include "ldb_wrap.h"
29 #include "lib/ldb-samba/ldif_handlers.h"
30 #include "auth/pyauth.h"
31
32 void init_ldb(void);
33
34 static PyObject *pyldb_module;
35 static PyObject *py_ldb_error;
36 static PyTypeObject PySambaLdb;
37
38 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
39 {
40         if (ret == LDB_ERR_PYTHON_EXCEPTION)
41                 return; /* Python exception should already be set, just keep that */
42
43         PyErr_SetObject(error, 
44                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
45                         ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
46 }
47
48 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
49 {
50         PyObject *py_lp_ctx;
51         struct loadparm_context *lp_ctx;
52         struct ldb_context *ldb;
53
54         if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
55                 return NULL;
56
57         ldb = pyldb_Ldb_AsLdbContext(self);
58
59         lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
60         if (lp_ctx == NULL) {
61                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
62                 return NULL;
63         }
64
65         ldb_set_opaque(ldb, "loadparm", lp_ctx);
66
67         Py_RETURN_NONE;
68 }
69
70 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
71 {
72         PyObject *py_creds;
73         struct cli_credentials *creds;
74         struct ldb_context *ldb;
75
76         if (!PyArg_ParseTuple(args, "O", &py_creds))
77                 return NULL;
78
79         creds = cli_credentials_from_py_object(py_creds);
80         if (creds == NULL) {
81                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
82                 return NULL;
83         }
84
85         ldb = pyldb_Ldb_AsLdbContext(self);
86
87         ldb_set_opaque(ldb, "credentials", creds);
88
89         Py_RETURN_NONE;
90 }
91
92 /* XXX: This function really should be in libldb's pyldb.c */
93 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
94 {
95         int value;
96         int *old_val, *new_val;
97         char *py_opaque_name, *opaque_name_talloc;
98         struct ldb_context *ldb;
99         int ret;
100         TALLOC_CTX *tmp_ctx;
101
102         if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
103                 return NULL;
104
105         ldb = pyldb_Ldb_AsLdbContext(self);
106
107         /* see if we have a cached copy */
108         old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
109         /* XXX: We shouldn't just blindly assume that the value that is 
110          * already present has the size of an int and is not shared 
111          * with other code that may rely on it not changing. 
112          * JRV 20100403 */
113
114         if (old_val) {
115                 *old_val = value;
116                 Py_RETURN_NONE;
117         }
118
119         tmp_ctx = talloc_new(ldb);
120         if (tmp_ctx == NULL) {
121                 PyErr_NoMemory();
122                 return NULL;
123         }
124
125         new_val = talloc(tmp_ctx, int);
126         if (new_val == NULL) {
127                 talloc_free(tmp_ctx);
128                 PyErr_NoMemory();
129                 return NULL;
130         }
131
132         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
133         if (opaque_name_talloc == NULL) {
134                 talloc_free(tmp_ctx);
135                 PyErr_NoMemory();
136                 return NULL;
137         }
138
139         *new_val = value;
140
141         /* cache the domain_sid in the ldb */
142         ret = ldb_set_opaque(ldb, opaque_name_talloc, new_val);
143
144         if (ret != LDB_SUCCESS) {
145                 talloc_free(tmp_ctx);
146                 PyErr_SetLdbError(py_ldb_error, ret, ldb);
147                 return NULL;
148         }
149
150         talloc_steal(ldb, new_val);
151         talloc_steal(ldb, opaque_name_talloc);
152         talloc_free(tmp_ctx);
153
154         Py_RETURN_NONE;
155 }
156
157 static PyObject *py_ldb_set_utf8_casefold(PyObject *self)
158 {
159         struct ldb_context *ldb;
160
161         ldb = pyldb_Ldb_AsLdbContext(self);
162
163         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
164
165         Py_RETURN_NONE;
166 }
167
168 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
169 {
170         PyObject *py_session_info;
171         struct auth_session_info *info;
172         struct ldb_context *ldb;
173         PyObject *mod_samba_auth;
174         PyObject *PyAuthSession_Type;
175         bool ret;
176
177         mod_samba_auth = PyImport_ImportModule("samba.dcerpc.auth");
178         if (mod_samba_auth == NULL)
179                 return NULL;
180
181         PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
182         if (PyAuthSession_Type == NULL)
183                 return NULL;
184
185         ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
186
187         Py_DECREF(PyAuthSession_Type);
188         Py_DECREF(mod_samba_auth);
189
190         if (!ret)
191                 return NULL;
192
193         ldb = pyldb_Ldb_AsLdbContext(self);
194
195         info = PyAuthSession_AsSession(py_session_info);
196
197         ldb_set_opaque(ldb, "sessionInfo", info);
198
199         Py_RETURN_NONE;
200 }
201
202 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
203 {
204         struct ldb_context *ldb;
205         int ret;
206
207         /* XXX: Perhaps call this from PySambaLdb's init function ? */
208
209         ldb = pyldb_Ldb_AsLdbContext(self);
210         ret = ldb_register_samba_handlers(ldb);
211
212         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
213
214         Py_RETURN_NONE;
215 }
216
217 static PyMethodDef py_samba_ldb_methods[] = {
218         { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, 
219                 "ldb_set_loadparm(session_info)\n"
220                 "Set loadparm context to use when connecting." },
221         { "set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
222                 "ldb_set_credentials(credentials)\n"
223                 "Set credentials to use when connecting." },
224         { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer,
225                 METH_VARARGS, NULL },
226         { "set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold, 
227                 METH_NOARGS,
228                 "ldb_set_utf8_casefold()\n"
229                 "Set the right Samba casefolding function for UTF8 charset." },
230         { "register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers,
231                 METH_NOARGS,
232                 "register_samba_handlers()\n"
233                 "Register Samba-specific LDB modules and schemas." },
234         { "set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
235                 "set_session_info(session_info)\n"
236                 "Set session info to use when connecting." },
237         { NULL },
238 };
239
240 static PyTypeObject PySambaLdb = {
241         .tp_name = "samba._ldb.Ldb",
242         .tp_doc = "Connection to a LDB database.",
243         .tp_methods = py_samba_ldb_methods,
244         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
245 };
246
247 void init_ldb(void)
248 {
249         PyObject *m;
250
251         pyldb_module = PyImport_ImportModule("ldb");
252         if (pyldb_module == NULL)
253                 return;
254
255         PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
256         if (PySambaLdb.tp_base == NULL)
257                 return;
258
259         py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
260
261         if (PyType_Ready(&PySambaLdb) < 0)
262                 return;
263
264         m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
265         if (m == NULL)
266                 return;
267
268         Py_INCREF(&PySambaLdb);
269         PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);
270 }