s4-python: Add more prototypes.
[Samba/gebeck_regimport.git] / source4 / lib / ldb-samba / pyldb.c
blobcce653e20ac72ab36b5c445748ee5399b472de11
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)));
50 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
52 PyObject *py_lp_ctx;
53 struct loadparm_context *lp_ctx;
54 struct ldb_context *ldb;
56 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
57 return NULL;
59 ldb = PyLdb_AsLdbContext(self);
61 lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
62 if (lp_ctx == NULL) {
63 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
64 return NULL;
67 ldb_set_opaque(ldb, "loadparm", lp_ctx);
69 Py_RETURN_NONE;
72 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
74 PyObject *py_creds;
75 struct cli_credentials *creds;
76 struct ldb_context *ldb;
78 if (!PyArg_ParseTuple(args, "O", &py_creds))
79 return NULL;
81 creds = cli_credentials_from_py_object(py_creds);
82 if (creds == NULL) {
83 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
84 return NULL;
87 ldb = PyLdb_AsLdbContext(self);
89 ldb_set_opaque(ldb, "credentials", creds);
91 Py_RETURN_NONE;
94 /* XXX: This function really should be in libldb's pyldb.c */
95 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
97 int value;
98 int *old_val, *new_val;
99 char *py_opaque_name, *opaque_name_talloc;
100 struct ldb_context *ldb;
101 int ret;
102 TALLOC_CTX *tmp_ctx;
104 if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
105 return NULL;
107 ldb = PyLdb_AsLdbContext(self);
109 /* see if we have a cached copy */
110 old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
111 /* XXX: We shouldn't just blindly assume that the value that is
112 * already present has the size of an int and is not shared
113 * with other code that may rely on it not changing.
114 * JRV 20100403 */
116 if (old_val) {
117 *old_val = value;
118 Py_RETURN_NONE;
121 tmp_ctx = talloc_new(ldb);
122 if (tmp_ctx == NULL) {
123 PyErr_NoMemory();
124 return NULL;
127 new_val = talloc(tmp_ctx, int);
128 if (new_val == NULL) {
129 talloc_free(tmp_ctx);
130 PyErr_NoMemory();
131 return NULL;
134 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
135 if (opaque_name_talloc == NULL) {
136 talloc_free(tmp_ctx);
137 PyErr_NoMemory();
138 return NULL;
141 *new_val = value;
143 /* cache the domain_sid in the ldb */
144 ret = ldb_set_opaque(ldb, opaque_name_talloc, new_val);
146 if (ret != LDB_SUCCESS) {
147 talloc_free(tmp_ctx);
148 PyErr_SetLdbError(py_ldb_error, ret, ldb);
149 return NULL;
152 talloc_steal(ldb, new_val);
153 talloc_steal(ldb, opaque_name_talloc);
154 talloc_free(tmp_ctx);
156 Py_RETURN_NONE;
159 static PyObject *py_ldb_set_utf8_casefold(PyObject *self)
161 struct ldb_context *ldb;
163 ldb = PyLdb_AsLdbContext(self);
165 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
167 Py_RETURN_NONE;
170 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
172 PyObject *py_session_info;
173 struct auth_session_info *info;
174 struct ldb_context *ldb;
175 PyObject *mod_samba_auth;
176 PyObject *PyAuthSession_Type;
177 bool ret;
179 mod_samba_auth = PyImport_ImportModule("samba.auth");
180 if (mod_samba_auth == NULL)
181 return NULL;
183 PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "AuthSession");
184 if (PyAuthSession_Type == NULL)
185 return NULL;
187 ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
189 Py_DECREF(PyAuthSession_Type);
190 Py_DECREF(mod_samba_auth);
192 if (!ret)
193 return NULL;
195 ldb = PyLdb_AsLdbContext(self);
197 info = PyAuthSession_AsSession(py_session_info);
199 ldb_set_opaque(ldb, "sessionInfo", info);
201 Py_RETURN_NONE;
204 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
206 struct ldb_context *ldb;
207 int ret;
209 /* XXX: Perhaps call this from PySambaLdb's init function ? */
211 ldb = PyLdb_AsLdbContext(self);
212 ret = ldb_register_samba_handlers(ldb);
214 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
216 Py_RETURN_NONE;
219 static PyMethodDef py_samba_ldb_methods[] = {
220 { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
221 "ldb_set_loadparm(session_info)\n"
222 "Set loadparm context to use when connecting." },
223 { "set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
224 "ldb_set_credentials(credentials)\n"
225 "Set credentials to use when connecting." },
226 { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer,
227 METH_VARARGS, NULL },
228 { "set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold,
229 METH_NOARGS,
230 "ldb_set_utf8_casefold()\n"
231 "Set the right Samba casefolding function for UTF8 charset." },
232 { "register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers,
233 METH_NOARGS,
234 "register_samba_handlers()\n"
235 "Register Samba-specific LDB modules and schemas." },
236 { "set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
237 "set_session_info(session_info)\n"
238 "Set session info to use when connecting." },
239 { NULL },
242 static PyTypeObject PySambaLdb = {
243 .tp_name = "samba.Ldb",
244 .tp_doc = "Connection to a LDB database.",
245 .tp_methods = py_samba_ldb_methods,
246 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
250 void init_ldb(void)
252 PyObject *m;
254 pyldb_module = PyImport_ImportModule("ldb");
255 if (pyldb_module == NULL)
256 return;
258 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
259 if (PySambaLdb.tp_base == NULL)
260 return;
262 py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
264 if (PyType_Ready(&PySambaLdb) < 0)
265 return;
267 m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
268 if (m == NULL)
269 return;
271 Py_INCREF(&PySambaLdb);
272 PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);