ctdb-tests: nfs_iterate_test() marks RPC service down
[samba4-gss.git] / lib / ldb-samba / pyldb.c
blobb2a485aaefad6cdfddc6aa232b54f87123e925eb
1 /*
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"
24 #include "includes.h"
25 #include <ldb.h>
26 #include <pyldb.h>
27 #include "param/pyparam.h"
28 #include "auth/credentials/pycredentials.h"
29 #include "ldb_wrap.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)
42 PyObject *py_lp_ctx;
43 struct loadparm_context *lp_ctx;
44 struct ldb_context *ldb;
46 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
47 return NULL;
49 ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
51 lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
52 if (lp_ctx == NULL) {
53 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
54 return NULL;
57 ldb_set_opaque(ldb, "loadparm", lp_ctx);
59 Py_RETURN_NONE;
62 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
64 PyObject *py_creds;
65 struct cli_credentials *creds;
66 struct ldb_context *ldb;
68 if (!PyArg_ParseTuple(args, "O", &py_creds))
69 return NULL;
71 creds = cli_credentials_from_py_object(py_creds);
72 if (creds == NULL) {
73 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
74 return NULL;
77 ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
79 ldb_set_opaque(ldb, "credentials", creds);
81 Py_RETURN_NONE;
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);
93 Py_RETURN_NONE;
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;
103 bool ret;
105 mod_samba_auth = PyImport_ImportModule("samba.dcerpc.auth");
106 if (mod_samba_auth == NULL)
107 return NULL;
109 PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
110 if (PyAuthSession_Type == NULL) {
111 Py_CLEAR(mod_samba_auth);
112 return NULL;
115 ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
117 Py_DECREF(PyAuthSession_Type);
118 Py_DECREF(mod_samba_auth);
120 if (!ret)
121 return NULL;
123 ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
125 info = PyAuthSession_AsSession(py_session_info);
127 ldb_set_opaque(ldb, DSDB_SESSION_INFO, info);
129 Py_RETURN_NONE;
132 static PyObject *py_ldb_samba_schema_attribute_add(PyLdbObject *self,
133 PyObject *args)
135 char *attribute, *syntax;
136 const struct ldb_schema_syntax *s;
137 unsigned int flags;
138 int ret;
139 struct ldb_context *ldb_ctx;
141 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
142 return NULL;
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,
148 flags, s);
150 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb_ctx);
152 Py_RETURN_NONE;
155 static PyObject *py_ldb_register_samba_handlers(PyObject *self,
156 PyObject *Py_UNUSED(ignored))
158 struct ldb_context *ldb;
159 int ret;
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);
168 Py_RETURN_NONE;
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,
179 METH_NOARGS,
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,
183 METH_NOARGS,
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 },
192 {0},
195 static struct PyModuleDef moduledef = {
196 PyModuleDef_HEAD_INIT,
197 .m_name = "_ldb",
198 .m_doc = "Samba-specific LDB python bindings",
199 .m_size = -1,
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)
213 PyObject *m;
215 pyldb_module = PyImport_ImportModule("ldb");
216 if (pyldb_module == NULL)
217 return NULL;
219 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
220 if (PySambaLdb.tp_base == NULL) {
221 Py_CLEAR(pyldb_module);
222 return NULL;
225 py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
227 Py_CLEAR(pyldb_module);
229 if (PyType_Ready(&PySambaLdb) < 0)
230 return NULL;
232 m = PyModule_Create(&moduledef);
233 if (m == NULL)
234 return NULL;
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);
242 return m;