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