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/>.
25 #include "lib/ldb/pyldb.h"
26 #include "param/pyparam.h"
27 #include "auth/credentials/pycredentials.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
)
50 struct loadparm_context
*lp_ctx
;
51 struct ldb_context
*ldb
;
53 if (!PyArg_ParseTuple(args
, "O", &py_lp_ctx
))
56 lp_ctx
= lp_from_py_object(py_lp_ctx
);
58 PyErr_SetString(PyExc_TypeError
, "Expected loadparm object");
62 ldb
= PyLdb_AsLdbContext(self
);
64 ldb_set_opaque(ldb
, "loadparm", lp_ctx
);
69 static PyObject
*py_ldb_set_credentials(PyObject
*self
, PyObject
*args
)
72 struct cli_credentials
*creds
;
73 struct ldb_context
*ldb
;
75 if (!PyArg_ParseTuple(args
, "O", &py_creds
))
78 creds
= cli_credentials_from_py_object(py_creds
);
80 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
84 ldb
= PyLdb_AsLdbContext(self
);
86 ldb_set_opaque(ldb
, "credentials", creds
);
91 /* XXX: This function really should be in libldb's pyldb.c */
92 static PyObject
*py_ldb_set_opaque_integer(PyObject
*self
, PyObject
*args
)
95 int *old_val
, *new_val
;
96 char *py_opaque_name
, *opaque_name_talloc
;
97 struct ldb_context
*ldb
;
101 if (!PyArg_ParseTuple(args
, "si", &py_opaque_name
, &value
))
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.
118 tmp_ctx
= talloc_new(ldb
);
119 if (tmp_ctx
== NULL
) {
124 new_val
= talloc(tmp_ctx
, int);
125 if (new_val
== NULL
) {
126 talloc_free(tmp_ctx
);
131 opaque_name_talloc
= talloc_strdup(tmp_ctx
, py_opaque_name
);
132 if (opaque_name_talloc
== NULL
) {
133 talloc_free(tmp_ctx
);
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
);
149 talloc_steal(ldb
, new_val
);
150 talloc_steal(ldb
, opaque_name_talloc
);
151 talloc_free(tmp_ctx
);
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
);
167 static PyObject
*py_ldb_register_samba_handlers(PyObject
*self
)
169 struct ldb_context
*ldb
;
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
);
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
,
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
,
197 "register_samba_handlers()\n"
198 "Register Samba-specific LDB modules and schemas." },
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
,
214 pyldb_module
= PyImport_ImportModule("ldb");
215 if (pyldb_module
== NULL
)
218 PySambaLdb
.tp_base
= (PyTypeObject
*)PyObject_GetAttrString(pyldb_module
, "Ldb");
219 if (PySambaLdb
.tp_base
== NULL
)
222 py_ldb_error
= PyObject_GetAttrString(pyldb_module
, "LdbError");
224 if (PyType_Ready(&PySambaLdb
) < 0)
227 m
= Py_InitModule3("_ldb", NULL
, "Samba-specific LDB python bindings");
231 Py_INCREF(&PySambaLdb
);
232 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PySambaLdb
);