s4-python: Move register_samba_handlers to PySambaLdb.
[Samba/gebeck_regimport.git] / source4 / lib / ldb-samba / pyldb.c
blobb5ce7f31916e01dbb26c41c51d20355876b427c1
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to ldb, Samba-specific functions
6 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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/>.
22 #include <Python.h>
23 #include "includes.h"
24 #include <ldb.h>
25 #include "lib/ldb/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"
31 static PyObject *pyldb_module;
32 static PyObject *py_ldb_error;
33 staticforward PyTypeObject PySambaLdb;
35 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
37 if (ret == LDB_ERR_PYTHON_EXCEPTION)
38 return; /* Python exception should already be set, just keep that */
40 PyErr_SetObject(error,
41 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
42 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
47 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
49 PyObject *py_lp_ctx;
50 struct loadparm_context *lp_ctx;
51 struct ldb_context *ldb;
53 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
54 return NULL;
56 lp_ctx = lp_from_py_object(py_lp_ctx);
57 if (lp_ctx == NULL) {
58 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
59 return NULL;
62 ldb = PyLdb_AsLdbContext(self);
64 ldb_set_opaque(ldb, "loadparm", lp_ctx);
66 Py_RETURN_NONE;
69 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
71 PyObject *py_creds;
72 struct cli_credentials *creds;
73 struct ldb_context *ldb;
75 if (!PyArg_ParseTuple(args, "O", &py_creds))
76 return NULL;
78 creds = cli_credentials_from_py_object(py_creds);
79 if (creds == NULL) {
80 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
81 return NULL;
84 ldb = PyLdb_AsLdbContext(self);
86 ldb_set_opaque(ldb, "credentials", creds);
88 Py_RETURN_NONE;
91 /* XXX: This function really should be in libldb's pyldb.c */
92 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
94 int value;
95 int *old_val, *new_val;
96 char *py_opaque_name, *opaque_name_talloc;
97 struct ldb_context *ldb;
98 int ret;
99 TALLOC_CTX *tmp_ctx;
101 if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
102 return NULL;
104 ldb = PyLdb_AsLdbContext(self);
106 /* see if we have a cached copy */
107 old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
108 /* XXX: We shouldn't just blindly assume that the value that is
109 * already present has the size of an int and is not shared
110 * with other code that may rely on it not changing.
111 * JRV 20100403 */
113 if (old_val) {
114 *old_val = value;
115 Py_RETURN_NONE;
118 tmp_ctx = talloc_new(ldb);
119 if (tmp_ctx == NULL) {
120 PyErr_NoMemory();
121 return NULL;
124 new_val = talloc(tmp_ctx, int);
125 if (new_val == NULL) {
126 talloc_free(tmp_ctx);
127 PyErr_NoMemory();
128 return NULL;
131 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
132 if (opaque_name_talloc == NULL) {
133 talloc_free(tmp_ctx);
134 PyErr_NoMemory();
135 return NULL;
138 *new_val = value;
140 /* cache the domain_sid in the ldb */
141 ret = ldb_set_opaque(ldb, opaque_name_talloc, new_val);
143 if (ret != LDB_SUCCESS) {
144 talloc_free(tmp_ctx);
145 PyErr_SetLdbError(py_ldb_error, ret, ldb);
146 return NULL;
149 talloc_steal(ldb, new_val);
150 talloc_steal(ldb, opaque_name_talloc);
151 talloc_free(tmp_ctx);
153 Py_RETURN_NONE;
156 static PyObject *py_ldb_set_utf8_casefold(PyObject *self)
158 struct ldb_context *ldb;
160 ldb = PyLdb_AsLdbContext(self);
162 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
164 Py_RETURN_NONE;
167 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
169 struct ldb_context *ldb;
170 int ret;
172 /* XXX: Perhaps call this from PySambaLdb's init function ? */
174 ldb = PyLdb_AsLdbContext(self);
175 ret = ldb_register_samba_handlers(ldb);
177 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
179 Py_RETURN_NONE;
182 static PyMethodDef py_samba_ldb_methods[] = {
183 { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
184 "ldb_set_loadparm(session_info)\n"
185 "Set loadparm context to use when connecting." },
186 { "set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
187 "ldb_set_credentials(credentials)\n"
188 "Set credentials to use when connecting." },
189 { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer,
190 METH_VARARGS, NULL },
191 { "set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold,
192 METH_NOARGS,
193 "ldb_set_utf8_casefold()\n"
194 "Set the right Samba casefolding function for UTF8 charset." },
195 { "register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers,
196 METH_NOARGS,
197 "register_samba_handlers()\n"
198 "Register Samba-specific LDB modules and schemas." },
199 { NULL },
202 static PyTypeObject PySambaLdb = {
203 .tp_name = "samba.Ldb",
204 .tp_doc = "Connection to a LDB database.",
205 .tp_methods = py_samba_ldb_methods,
206 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
210 void init_ldb(void)
212 PyObject *m;
214 pyldb_module = PyImport_ImportModule("ldb");
215 if (pyldb_module == NULL)
216 return;
218 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
219 if (PySambaLdb.tp_base == NULL)
220 return;
222 py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
224 if (PyType_Ready(&PySambaLdb) < 0)
225 return;
227 m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
228 if (m == NULL)
229 return;
231 Py_INCREF(&PySambaLdb);
232 PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);