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