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/>.
23 #include "python/py3compat.h"
27 #include "param/pyparam.h"
28 #include "auth/credentials/pycredentials.h"
30 #include "lib/ldb-samba/ldif_handlers.h"
31 #include "auth/pyauth.h"
32 #include "source4/dsdb/common/util.h"
33 #include "lib/ldb/include/ldb_private.h"
36 static PyObject
*pyldb_module
;
37 static PyObject
*py_ldb_error
;
38 static PyTypeObject PySambaLdb
;
40 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
42 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
43 return; /* Python exception should already be set, just keep that */
45 PyErr_SetObject(error
,
46 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
47 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
50 static PyObject
*py_ldb_set_loadparm(PyObject
*self
, PyObject
*args
)
53 struct loadparm_context
*lp_ctx
;
54 struct ldb_context
*ldb
;
56 if (!PyArg_ParseTuple(args
, "O", &py_lp_ctx
))
59 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
61 lp_ctx
= lpcfg_from_py_object(ldb
, py_lp_ctx
);
63 PyErr_SetString(PyExc_TypeError
, "Expected loadparm object");
67 ldb_set_opaque(ldb
, "loadparm", lp_ctx
);
72 static PyObject
*py_ldb_set_credentials(PyObject
*self
, PyObject
*args
)
75 struct cli_credentials
*creds
;
76 struct ldb_context
*ldb
;
78 if (!PyArg_ParseTuple(args
, "O", &py_creds
))
81 creds
= cli_credentials_from_py_object(py_creds
);
83 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
87 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
89 ldb_set_opaque(ldb
, "credentials", creds
);
94 /* XXX: This function really should be in libldb's pyldb.c */
95 static PyObject
*py_ldb_set_opaque_integer(PyObject
*self
, PyObject
*args
)
98 int *old_val
, *new_val
;
99 char *py_opaque_name
, *opaque_name_talloc
;
100 struct ldb_context
*ldb
;
104 if (!PyArg_ParseTuple(args
, "si", &py_opaque_name
, &value
))
107 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
109 /* see if we have a cached copy */
110 old_val
= (int *)ldb_get_opaque(ldb
, py_opaque_name
);
111 /* XXX: We shouldn't just blindly assume that the value that is
112 * already present has the size of an int and is not shared
113 * with other code that may rely on it not changing.
121 tmp_ctx
= talloc_new(ldb
);
122 if (tmp_ctx
== NULL
) {
127 new_val
= talloc(tmp_ctx
, int);
128 if (new_val
== NULL
) {
129 talloc_free(tmp_ctx
);
134 opaque_name_talloc
= talloc_strdup(tmp_ctx
, py_opaque_name
);
135 if (opaque_name_talloc
== NULL
) {
136 talloc_free(tmp_ctx
);
143 /* cache the domain_sid in the ldb */
144 ret
= ldb_set_opaque(ldb
, opaque_name_talloc
, new_val
);
146 if (ret
!= LDB_SUCCESS
) {
147 talloc_free(tmp_ctx
);
148 PyErr_SetLdbError(py_ldb_error
, ret
, ldb
);
152 talloc_steal(ldb
, new_val
);
153 talloc_steal(ldb
, opaque_name_talloc
);
154 talloc_free(tmp_ctx
);
159 static PyObject
*py_ldb_set_utf8_casefold(PyObject
*self
,
160 PyObject
*Py_UNUSED(ignored
))
162 struct ldb_context
*ldb
;
164 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
166 ldb_set_utf8_fns(ldb
, NULL
, wrap_casefold
);
171 static PyObject
*py_ldb_set_session_info(PyObject
*self
, PyObject
*args
)
173 PyObject
*py_session_info
;
174 struct auth_session_info
*info
;
175 struct ldb_context
*ldb
;
176 PyObject
*mod_samba_auth
;
177 PyObject
*PyAuthSession_Type
;
180 mod_samba_auth
= PyImport_ImportModule("samba.dcerpc.auth");
181 if (mod_samba_auth
== NULL
)
184 PyAuthSession_Type
= PyObject_GetAttrString(mod_samba_auth
, "session_info");
185 if (PyAuthSession_Type
== NULL
) {
186 Py_CLEAR(mod_samba_auth
);
190 ret
= PyArg_ParseTuple(args
, "O!", PyAuthSession_Type
, &py_session_info
);
192 Py_DECREF(PyAuthSession_Type
);
193 Py_DECREF(mod_samba_auth
);
198 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
200 info
= PyAuthSession_AsSession(py_session_info
);
202 ldb_set_opaque(ldb
, DSDB_SESSION_INFO
, info
);
207 static PyObject
*py_ldb_samba_schema_attribute_add(PyLdbObject
*self
,
210 char *attribute
, *syntax
;
211 const struct ldb_schema_syntax
*s
;
214 struct ldb_context
*ldb_ctx
;
216 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
219 ldb_ctx
= pyldb_Ldb_AsLdbContext((PyObject
*)self
);
221 s
= ldb_samba_syntax_by_name(ldb_ctx
, syntax
);
222 ret
= ldb_schema_attribute_add_with_syntax(ldb_ctx
, attribute
,
225 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error
, ret
, ldb_ctx
);
230 static PyObject
*py_ldb_register_samba_handlers(PyObject
*self
,
231 PyObject
*Py_UNUSED(ignored
))
233 struct ldb_context
*ldb
;
236 /* XXX: Perhaps call this from PySambaLdb's init function ? */
238 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
239 ret
= ldb_register_samba_handlers(ldb
);
241 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error
, ret
, ldb
);
246 static PyMethodDef py_samba_ldb_methods
[] = {
247 { "set_loadparm", (PyCFunction
)py_ldb_set_loadparm
, METH_VARARGS
,
248 "set_loadparm(session_info)\n"
249 "Set loadparm context to use when connecting." },
250 { "set_credentials", (PyCFunction
)py_ldb_set_credentials
, METH_VARARGS
,
251 "set_credentials(credentials)\n"
252 "Set credentials to use when connecting." },
253 { "set_opaque_integer", (PyCFunction
)py_ldb_set_opaque_integer
,
254 METH_VARARGS
, NULL
},
255 { "set_utf8_casefold", (PyCFunction
)py_ldb_set_utf8_casefold
,
257 "set_utf8_casefold()\n"
258 "Set the right Samba casefolding function for UTF8 charset." },
259 { "register_samba_handlers", (PyCFunction
)py_ldb_register_samba_handlers
,
261 "register_samba_handlers()\n"
262 "Register Samba-specific LDB modules and schemas." },
263 { "set_session_info", (PyCFunction
)py_ldb_set_session_info
, METH_VARARGS
,
264 "set_session_info(session_info)\n"
265 "Set session info to use when connecting." },
266 { "samba_schema_attribute_add",
267 (PyCFunction
)py_ldb_samba_schema_attribute_add
,
268 METH_VARARGS
, NULL
},
272 static struct PyModuleDef moduledef
= {
273 PyModuleDef_HEAD_INIT
,
275 .m_doc
= "Samba-specific LDB python bindings",
277 .m_methods
= py_samba_ldb_methods
,
280 static PyTypeObject PySambaLdb
= {
281 .tp_name
= "samba._ldb.Ldb",
282 .tp_doc
= "Connection to a LDB database.",
283 .tp_methods
= py_samba_ldb_methods
,
284 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
288 MODULE_INIT_FUNC(_ldb
)
292 pyldb_module
= PyImport_ImportModule("ldb");
293 if (pyldb_module
== NULL
)
296 PySambaLdb
.tp_base
= (PyTypeObject
*)PyObject_GetAttrString(pyldb_module
, "Ldb");
297 if (PySambaLdb
.tp_base
== NULL
) {
298 Py_CLEAR(pyldb_module
);
302 py_ldb_error
= PyObject_GetAttrString(pyldb_module
, "LdbError");
304 Py_CLEAR(pyldb_module
);
306 if (PyType_Ready(&PySambaLdb
) < 0)
309 m
= PyModule_Create(&moduledef
);
313 Py_INCREF(&PySambaLdb
);
314 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PySambaLdb
);
316 #define ADD_LDB_STRING(val) PyModule_AddStringConstant(m, #val, LDB_## val)
317 ADD_LDB_STRING(SYNTAX_SAMBA_INT32
);