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/>.
26 #include "param/pyparam.h"
27 #include "auth/credentials/pycredentials.h"
29 #include "lib/ldb-samba/ldif_handlers.h"
30 #include "auth/pyauth.h"
34 static PyObject
*pyldb_module
;
35 static PyObject
*py_ldb_error
;
36 staticforward PyTypeObject PySambaLdb
;
38 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
40 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
41 return; /* Python exception should already be set, just keep that */
43 PyErr_SetObject(error
,
44 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
45 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
48 static PyObject
*py_ldb_set_loadparm(PyObject
*self
, PyObject
*args
)
51 struct loadparm_context
*lp_ctx
;
52 struct ldb_context
*ldb
;
54 if (!PyArg_ParseTuple(args
, "O", &py_lp_ctx
))
57 ldb
= pyldb_Ldb_AsLdbContext(self
);
59 lp_ctx
= lpcfg_from_py_object(ldb
, py_lp_ctx
);
61 PyErr_SetString(PyExc_TypeError
, "Expected loadparm object");
65 ldb_set_opaque(ldb
, "loadparm", lp_ctx
);
70 static PyObject
*py_ldb_set_credentials(PyObject
*self
, PyObject
*args
)
73 struct cli_credentials
*creds
;
74 struct ldb_context
*ldb
;
76 if (!PyArg_ParseTuple(args
, "O", &py_creds
))
79 creds
= cli_credentials_from_py_object(py_creds
);
81 PyErr_SetString(PyExc_TypeError
, "Expected credentials object");
85 ldb
= pyldb_Ldb_AsLdbContext(self
);
87 ldb_set_opaque(ldb
, "credentials", creds
);
92 /* XXX: This function really should be in libldb's pyldb.c */
93 static PyObject
*py_ldb_set_opaque_integer(PyObject
*self
, PyObject
*args
)
96 int *old_val
, *new_val
;
97 char *py_opaque_name
, *opaque_name_talloc
;
98 struct ldb_context
*ldb
;
102 if (!PyArg_ParseTuple(args
, "si", &py_opaque_name
, &value
))
105 ldb
= pyldb_Ldb_AsLdbContext(self
);
107 /* see if we have a cached copy */
108 old_val
= (int *)ldb_get_opaque(ldb
, py_opaque_name
);
109 /* XXX: We shouldn't just blindly assume that the value that is
110 * already present has the size of an int and is not shared
111 * with other code that may rely on it not changing.
119 tmp_ctx
= talloc_new(ldb
);
120 if (tmp_ctx
== NULL
) {
125 new_val
= talloc(tmp_ctx
, int);
126 if (new_val
== NULL
) {
127 talloc_free(tmp_ctx
);
132 opaque_name_talloc
= talloc_strdup(tmp_ctx
, py_opaque_name
);
133 if (opaque_name_talloc
== NULL
) {
134 talloc_free(tmp_ctx
);
141 /* cache the domain_sid in the ldb */
142 ret
= ldb_set_opaque(ldb
, opaque_name_talloc
, new_val
);
144 if (ret
!= LDB_SUCCESS
) {
145 talloc_free(tmp_ctx
);
146 PyErr_SetLdbError(py_ldb_error
, ret
, ldb
);
150 talloc_steal(ldb
, new_val
);
151 talloc_steal(ldb
, opaque_name_talloc
);
152 talloc_free(tmp_ctx
);
157 static PyObject
*py_ldb_set_utf8_casefold(PyObject
*self
)
159 struct ldb_context
*ldb
;
161 ldb
= pyldb_Ldb_AsLdbContext(self
);
163 ldb_set_utf8_fns(ldb
, NULL
, wrap_casefold
);
168 static PyObject
*py_ldb_set_session_info(PyObject
*self
, PyObject
*args
)
170 PyObject
*py_session_info
;
171 struct auth_session_info
*info
;
172 struct ldb_context
*ldb
;
173 PyObject
*mod_samba_auth
;
174 PyObject
*PyAuthSession_Type
;
177 mod_samba_auth
= PyImport_ImportModule("samba.dcerpc.auth");
178 if (mod_samba_auth
== NULL
)
181 PyAuthSession_Type
= PyObject_GetAttrString(mod_samba_auth
, "session_info");
182 if (PyAuthSession_Type
== NULL
)
185 ret
= PyArg_ParseTuple(args
, "O!", PyAuthSession_Type
, &py_session_info
);
187 Py_DECREF(PyAuthSession_Type
);
188 Py_DECREF(mod_samba_auth
);
193 ldb
= pyldb_Ldb_AsLdbContext(self
);
195 info
= PyAuthSession_AsSession(py_session_info
);
197 ldb_set_opaque(ldb
, "sessionInfo", info
);
202 static PyObject
*py_ldb_register_samba_handlers(PyObject
*self
)
204 struct ldb_context
*ldb
;
207 /* XXX: Perhaps call this from PySambaLdb's init function ? */
209 ldb
= pyldb_Ldb_AsLdbContext(self
);
210 ret
= ldb_register_samba_handlers(ldb
);
212 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error
, ret
, ldb
);
217 static PyMethodDef py_samba_ldb_methods
[] = {
218 { "set_loadparm", (PyCFunction
)py_ldb_set_loadparm
, METH_VARARGS
,
219 "ldb_set_loadparm(session_info)\n"
220 "Set loadparm context to use when connecting." },
221 { "set_credentials", (PyCFunction
)py_ldb_set_credentials
, METH_VARARGS
,
222 "ldb_set_credentials(credentials)\n"
223 "Set credentials to use when connecting." },
224 { "set_opaque_integer", (PyCFunction
)py_ldb_set_opaque_integer
,
225 METH_VARARGS
, NULL
},
226 { "set_utf8_casefold", (PyCFunction
)py_ldb_set_utf8_casefold
,
228 "ldb_set_utf8_casefold()\n"
229 "Set the right Samba casefolding function for UTF8 charset." },
230 { "register_samba_handlers", (PyCFunction
)py_ldb_register_samba_handlers
,
232 "register_samba_handlers()\n"
233 "Register Samba-specific LDB modules and schemas." },
234 { "set_session_info", (PyCFunction
)py_ldb_set_session_info
, METH_VARARGS
,
235 "set_session_info(session_info)\n"
236 "Set session info to use when connecting." },
240 static PyTypeObject PySambaLdb
= {
241 .tp_name
= "samba._ldb.Ldb",
242 .tp_doc
= "Connection to a LDB database.",
243 .tp_methods
= py_samba_ldb_methods
,
244 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
251 pyldb_module
= PyImport_ImportModule("ldb");
252 if (pyldb_module
== NULL
)
255 PySambaLdb
.tp_base
= (PyTypeObject
*)PyObject_GetAttrString(pyldb_module
, "Ldb");
256 if (PySambaLdb
.tp_base
== NULL
)
259 py_ldb_error
= PyObject_GetAttrString(pyldb_module
, "LdbError");
261 if (PyType_Ready(&PySambaLdb
) < 0)
264 m
= Py_InitModule3("_ldb", NULL
, "Samba-specific LDB python bindings");
268 Py_INCREF(&PySambaLdb
);
269 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PySambaLdb
);