s4-python: Move register_samba_handlers to PySambaLdb.
[samba.git] / source4 / lib / ldb-samba / pyldb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to ldb, Samba-specific functions
5
6    Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
7
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.
12
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.
17
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/>.
20 */
21
22 #include <Python.h>
23 #include "includes.h"
24 #include <ldb.h>
25 #include "lib/ldb/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
31 static PyObject *pyldb_module;
32 static PyObject *py_ldb_error;
33 staticforward PyTypeObject PySambaLdb;
34
35 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
36 {
37         if (ret == LDB_ERR_PYTHON_EXCEPTION)
38                 return; /* Python exception should already be set, just keep that */
39
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)));
43 }
44
45
46
47 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
48 {
49         PyObject *py_lp_ctx;
50         struct loadparm_context *lp_ctx;
51         struct ldb_context *ldb;
52
53         if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
54                 return NULL;
55
56         lp_ctx = lp_from_py_object(py_lp_ctx);
57         if (lp_ctx == NULL) {
58                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
59                 return NULL;
60         }
61
62         ldb = PyLdb_AsLdbContext(self);
63
64         ldb_set_opaque(ldb, "loadparm", lp_ctx);
65
66         Py_RETURN_NONE;
67 }
68
69 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
70 {
71         PyObject *py_creds;
72         struct cli_credentials *creds;
73         struct ldb_context *ldb;
74
75         if (!PyArg_ParseTuple(args, "O", &py_creds))
76                 return NULL;
77
78         creds = cli_credentials_from_py_object(py_creds);
79         if (creds == NULL) {
80                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
81                 return NULL;
82         }
83
84         ldb = PyLdb_AsLdbContext(self);
85
86         ldb_set_opaque(ldb, "credentials", creds);
87
88         Py_RETURN_NONE;
89 }
90
91 /* XXX: This function really should be in libldb's pyldb.c */
92 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
93 {
94         int value;
95         int *old_val, *new_val;
96         char *py_opaque_name, *opaque_name_talloc;
97         struct ldb_context *ldb;
98         int ret;
99         TALLOC_CTX *tmp_ctx;
100
101         if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
102                 return NULL;
103
104         ldb = PyLdb_AsLdbContext(self);
105
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. 
111          * JRV 20100403 */
112
113         if (old_val) {
114                 *old_val = value;
115                 Py_RETURN_NONE;
116         } 
117
118         tmp_ctx = talloc_new(ldb);
119         if (tmp_ctx == NULL) {
120                 PyErr_NoMemory();
121                 return NULL;
122         }
123         
124         new_val = talloc(tmp_ctx, int);
125         if (new_val == NULL) {
126                 talloc_free(tmp_ctx);
127                 PyErr_NoMemory();
128                 return NULL;
129         }
130         
131         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
132         if (opaque_name_talloc == NULL) {
133                 talloc_free(tmp_ctx);
134                 PyErr_NoMemory();
135                 return NULL;
136         }
137         
138         *new_val = value;
139
140         /* cache the domain_sid in the ldb */
141         ret = ldb_set_opaque(ldb, opaque_name_talloc, new_val);
142         
143         if (ret != LDB_SUCCESS) {
144                 talloc_free(tmp_ctx);
145                 PyErr_SetLdbError(py_ldb_error, ret, ldb);
146                 return NULL;
147         }
148
149         talloc_steal(ldb, new_val);
150         talloc_steal(ldb, opaque_name_talloc);
151         talloc_free(tmp_ctx);
152
153         Py_RETURN_NONE;
154 }
155
156 static PyObject *py_ldb_set_utf8_casefold(PyObject *self)
157 {
158         struct ldb_context *ldb;
159
160         ldb = PyLdb_AsLdbContext(self);
161
162         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
163
164         Py_RETURN_NONE;
165 }
166
167 static PyObject *py_ldb_register_samba_handlers(PyObject *self)
168 {
169         struct ldb_context *ldb;
170         int ret;
171
172         /* XXX: Perhaps call this from PySambaLdb's init function ? */
173
174         ldb = PyLdb_AsLdbContext(self);
175         ret = ldb_register_samba_handlers(ldb);
176
177         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
178
179         Py_RETURN_NONE;
180 }
181
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, 
192                 METH_NOARGS,
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,
196                 METH_NOARGS,
197                 "register_samba_handlers()\n"
198                 "Register Samba-specific LDB modules and schemas." },
199         { NULL },
200 };
201
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,
207 };
208
209
210 void init_ldb(void)
211 {
212         PyObject *m;
213
214         pyldb_module = PyImport_ImportModule("ldb");
215         if (pyldb_module == NULL)
216                 return;
217
218         PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
219         if (PySambaLdb.tp_base == NULL)
220                 return;
221
222         py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
223
224         if (PyType_Ready(&PySambaLdb) < 0)
225                 return;
226
227         m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
228         if (m == NULL)
229                 return;
230
231         Py_INCREF(&PySambaLdb);
232         PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);
233 }