lib/param: move enum dns_update_settings to lib/param
[Samba/bb.git] / lib / ldb-samba / pyldb.c
blobaae25a01e6806d6f2921d5a3b5dd56f04e55a53f
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 <Python.h>
23 #include "includes.h"
24 #include <ldb.h>
25 #include <pyldb.h>
26 #include "param/pyparam.h"
27 #include "auth/credentials/pycredentials.h"
28 #include "ldb_wrap.h"
29 #include "lib/ldb-samba/ldif_handlers.h"
30 #include "auth/pyauth.h"
32 void init_ldb(void);
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)
50 PyObject *py_lp_ctx;
51 struct loadparm_context *lp_ctx;
52 struct ldb_context *ldb;
54 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
55 return NULL;
57 ldb = pyldb_Ldb_AsLdbContext(self);
59 lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
60 if (lp_ctx == NULL) {
61 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
62 return NULL;
65 ldb_set_opaque(ldb, "loadparm", lp_ctx);
67 Py_RETURN_NONE;
70 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
72 PyObject *py_creds;
73 struct cli_credentials *creds;
74 struct ldb_context *ldb;
76 if (!PyArg_ParseTuple(args, "O", &py_creds))
77 return NULL;
79 creds = cli_credentials_from_py_object(py_creds);
80 if (creds == NULL) {
81 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
82 return NULL;
85 ldb = pyldb_Ldb_AsLdbContext(self);
87 ldb_set_opaque(ldb, "credentials", creds);
89 Py_RETURN_NONE;
92 /* XXX: This function really should be in libldb's pyldb.c */
93 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
95 int value;
96 int *old_val, *new_val;
97 char *py_opaque_name, *opaque_name_talloc;
98 struct ldb_context *ldb;
99 int ret;
100 TALLOC_CTX *tmp_ctx;
102 if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
103 return NULL;
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.
112 * JRV 20100403 */
114 if (old_val) {
115 *old_val = value;
116 Py_RETURN_NONE;
119 tmp_ctx = talloc_new(ldb);
120 if (tmp_ctx == NULL) {
121 PyErr_NoMemory();
122 return NULL;
125 new_val = talloc(tmp_ctx, int);
126 if (new_val == NULL) {
127 talloc_free(tmp_ctx);
128 PyErr_NoMemory();
129 return NULL;
132 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
133 if (opaque_name_talloc == NULL) {
134 talloc_free(tmp_ctx);
135 PyErr_NoMemory();
136 return NULL;
139 *new_val = value;
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);
147 return NULL;
150 talloc_steal(ldb, new_val);
151 talloc_steal(ldb, opaque_name_talloc);
152 talloc_free(tmp_ctx);
154 Py_RETURN_NONE;
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);
165 Py_RETURN_NONE;
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;
175 bool ret;
177 mod_samba_auth = PyImport_ImportModule("samba.dcerpc.auth");
178 if (mod_samba_auth == NULL)
179 return NULL;
181 PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
182 if (PyAuthSession_Type == NULL)
183 return NULL;
185 ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
187 Py_DECREF(PyAuthSession_Type);
188 Py_DECREF(mod_samba_auth);
190 if (!ret)
191 return NULL;
193 ldb = pyldb_Ldb_AsLdbContext(self);
195 info = PyAuthSession_AsSession(py_session_info);
197 ldb_set_opaque(ldb, "sessionInfo", info);
199 Py_RETURN_NONE;
202 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
204 struct ldb_context *ldb;
205 int ret;
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);
214 Py_RETURN_NONE;
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,
227 METH_NOARGS,
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,
231 METH_NOARGS,
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." },
237 { NULL },
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,
247 void init_ldb(void)
249 PyObject *m;
251 pyldb_module = PyImport_ImportModule("ldb");
252 if (pyldb_module == NULL)
253 return;
255 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
256 if (PySambaLdb.tp_base == NULL)
257 return;
259 py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
261 if (PyType_Ready(&PySambaLdb) < 0)
262 return;
264 m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
265 if (m == NULL)
266 return;
268 Py_INCREF(&PySambaLdb);
269 PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);