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 "lib/ldb/pyldb.h"
24 #include "param/pyparam.h"
25 #include "auth/credentials/pycredentials.h"
27 #include "lib/ldb-samba/ldif_handlers.h"
28 #include "auth/pyauth.h"
30 static PyObject *pyldb_module;
31 static PyObject *py_ldb_error;
32 staticforward PyTypeObject PySambaLdb;
34 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
36 if (ret == LDB_ERR_PYTHON_EXCEPTION)
37 return; /* Python exception should already be set, just keep that */
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)));
46 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
49 struct loadparm_context *lp_ctx;
50 struct ldb_context *ldb;
52 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
55 ldb = PyLdb_AsLdbContext(self);
57 lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
59 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
63 ldb_set_opaque(ldb, "loadparm", lp_ctx);
68 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
71 struct cli_credentials *creds;
72 struct ldb_context *ldb;
74 if (!PyArg_ParseTuple(args, "O", &py_creds))
77 creds = cli_credentials_from_py_object(py_creds);
79 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
83 ldb = PyLdb_AsLdbContext(self);
85 ldb_set_opaque(ldb, "credentials", creds);
90 /* XXX: This function really should be in libldb's pyldb.c */
91 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
94 int *old_val, *new_val;
95 char *py_opaque_name, *opaque_name_talloc;
96 struct ldb_context *ldb;
100 if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
103 ldb = PyLdb_AsLdbContext(self);
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.
117 tmp_ctx = talloc_new(ldb);
118 if (tmp_ctx == NULL) {
123 new_val = talloc(tmp_ctx, int);
124 if (new_val == NULL) {
125 talloc_free(tmp_ctx);
130 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
131 if (opaque_name_talloc == NULL) {
132 talloc_free(tmp_ctx);
139 /* cache the domain_sid in the ldb */
140 ret = ldb_set_opaque(ldb, opaque_name_talloc, new_val);
142 if (ret != LDB_SUCCESS) {
143 talloc_free(tmp_ctx);
144 PyErr_SetLdbError(py_ldb_error, ret, ldb);
148 talloc_steal(ldb, new_val);
149 talloc_steal(ldb, opaque_name_talloc);
150 talloc_free(tmp_ctx);
155 static PyObject *py_ldb_set_utf8_casefold(PyObject *self)
157 struct ldb_context *ldb;
159 ldb = PyLdb_AsLdbContext(self);
161 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
166 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
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;
175 mod_samba_auth = PyImport_ImportModule("samba.auth");
176 if (mod_samba_auth == NULL)
179 PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "AuthSession");
180 if (PyAuthSession_Type == NULL)
183 ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
185 Py_DECREF(PyAuthSession_Type);
186 Py_DECREF(mod_samba_auth);
191 ldb = PyLdb_AsLdbContext(self);
193 info = PyAuthSession_AsSession(py_session_info);
195 ldb_set_opaque(ldb, "sessionInfo", info);
200 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
202 struct ldb_context *ldb;
205 /* XXX: Perhaps call this from PySambaLdb's init function ? */
207 ldb = PyLdb_AsLdbContext(self);
208 ret = ldb_register_samba_handlers(ldb);
210 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
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,
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,
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." },
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,
250 pyldb_module = PyImport_ImportModule("ldb");
251 if (pyldb_module == NULL)
254 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
255 if (PySambaLdb.tp_base == NULL)
258 py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
260 if (PyType_Ready(&PySambaLdb) < 0)
263 m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
267 Py_INCREF(&PySambaLdb);
268 PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);