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/replace/system/python.h"
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 PyObject
*py_ldb_set_loadparm(PyObject
*self
, PyObject
*args
)
43 struct loadparm_context
*lp_ctx
;
44 struct ldb_context
*ldb
;
46 if (!PyArg_ParseTuple(args
, "O", &py_lp_ctx
))
49 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
51 lp_ctx
= lpcfg_from_py_object(ldb
, py_lp_ctx
);
53 PyErr_SetString(PyExc_TypeError
, "Expected loadparm object");
57 ldb_set_opaque(ldb
, "loadparm", lp_ctx
);
62 static PyObject
*py_ldb_set_credentials(PyObject
*self
, PyObject
*args
)
65 struct cli_credentials
*creds
;
66 struct ldb_context
*ldb
;
68 if (!PyArg_ParseTuple(args
, "O", &py_creds
))
71 creds
= cli_credentials_from_py_object(py_creds
);
73 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
77 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
79 ldb_set_opaque(ldb
, "credentials", creds
);
84 static PyObject
*py_ldb_set_utf8_casefold(PyObject
*self
,
85 PyObject
*Py_UNUSED(ignored
))
87 struct ldb_context
*ldb
;
89 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
91 ldb_set_utf8_functions(ldb
, NULL
, wrap_casefold
, ldb_comparison_fold_utf8
);
96 static PyObject
*py_ldb_set_session_info(PyObject
*self
, PyObject
*args
)
98 PyObject
*py_session_info
;
99 struct auth_session_info
*info
;
100 struct ldb_context
*ldb
;
101 PyObject
*mod_samba_auth
;
102 PyObject
*PyAuthSession_Type
;
105 mod_samba_auth
= PyImport_ImportModule("samba.dcerpc.auth");
106 if (mod_samba_auth
== NULL
)
109 PyAuthSession_Type
= PyObject_GetAttrString(mod_samba_auth
, "session_info");
110 if (PyAuthSession_Type
== NULL
) {
111 Py_CLEAR(mod_samba_auth
);
115 ret
= PyArg_ParseTuple(args
, "O!", PyAuthSession_Type
, &py_session_info
);
117 Py_DECREF(PyAuthSession_Type
);
118 Py_DECREF(mod_samba_auth
);
123 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
125 info
= PyAuthSession_AsSession(py_session_info
);
127 ldb_set_opaque(ldb
, DSDB_SESSION_INFO
, info
);
132 static PyObject
*py_ldb_samba_schema_attribute_add(PyLdbObject
*self
,
135 char *attribute
, *syntax
;
136 const struct ldb_schema_syntax
*s
;
139 struct ldb_context
*ldb_ctx
;
141 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
144 ldb_ctx
= pyldb_Ldb_AsLdbContext((PyObject
*)self
);
146 s
= ldb_samba_syntax_by_name(ldb_ctx
, syntax
);
147 ret
= ldb_schema_attribute_add_with_syntax(ldb_ctx
, attribute
,
150 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error
, ret
, ldb_ctx
);
155 static PyObject
*py_ldb_register_samba_handlers(PyObject
*self
,
156 PyObject
*Py_UNUSED(ignored
))
158 struct ldb_context
*ldb
;
161 /* XXX: Perhaps call this from PySambaLdb's init function ? */
163 ldb
= pyldb_Ldb_AS_LDBCONTEXT(self
);
164 ret
= ldb_register_samba_handlers(ldb
);
166 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error
, ret
, ldb
);
171 static PyMethodDef py_samba_ldb_methods
[] = {
172 { "set_loadparm", (PyCFunction
)py_ldb_set_loadparm
, METH_VARARGS
,
173 "set_loadparm(session_info)\n"
174 "Set loadparm context to use when connecting." },
175 { "set_credentials", (PyCFunction
)py_ldb_set_credentials
, METH_VARARGS
,
176 "set_credentials(credentials)\n"
177 "Set credentials to use when connecting." },
178 { "set_utf8_casefold", (PyCFunction
)py_ldb_set_utf8_casefold
,
180 "set_utf8_casefold()\n"
181 "Set the right Samba casefolding function for UTF8 charset." },
182 { "register_samba_handlers", (PyCFunction
)py_ldb_register_samba_handlers
,
184 "register_samba_handlers()\n"
185 "Register Samba-specific LDB modules and schemas." },
186 { "set_session_info", (PyCFunction
)py_ldb_set_session_info
, METH_VARARGS
,
187 "set_session_info(session_info)\n"
188 "Set session info to use when connecting." },
189 { "samba_schema_attribute_add",
190 (PyCFunction
)py_ldb_samba_schema_attribute_add
,
191 METH_VARARGS
, NULL
},
195 static struct PyModuleDef moduledef
= {
196 PyModuleDef_HEAD_INIT
,
198 .m_doc
= "Samba-specific LDB python bindings",
200 .m_methods
= py_samba_ldb_methods
,
203 static PyTypeObject PySambaLdb
= {
204 .tp_name
= "samba._ldb.Ldb",
205 .tp_doc
= "Connection to a LDB database.",
206 .tp_methods
= py_samba_ldb_methods
,
207 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
211 MODULE_INIT_FUNC(_ldb
)
215 pyldb_module
= PyImport_ImportModule("ldb");
216 if (pyldb_module
== NULL
)
219 PySambaLdb
.tp_base
= (PyTypeObject
*)PyObject_GetAttrString(pyldb_module
, "Ldb");
220 if (PySambaLdb
.tp_base
== NULL
) {
221 Py_CLEAR(pyldb_module
);
225 py_ldb_error
= PyObject_GetAttrString(pyldb_module
, "LdbError");
227 Py_CLEAR(pyldb_module
);
229 if (PyType_Ready(&PySambaLdb
) < 0)
232 m
= PyModule_Create(&moduledef
);
236 Py_INCREF(&PySambaLdb
);
237 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PySambaLdb
);
239 #define ADD_LDB_STRING(val) PyModule_AddStringConstant(m, #val, LDB_## val)
240 ADD_LDB_STRING(SYNTAX_SAMBA_INT32
);