s4-python: Move set_opaque_integer to pyldb.
[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
29 static PyObject *pyldb_module;
30 staticforward PyTypeObject PySambaLdb;
31
32 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
33 {
34         PyObject *py_lp_ctx;
35         struct loadparm_context *lp_ctx;
36         struct ldb_context *ldb;
37
38         if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
39                 return NULL;
40
41         lp_ctx = lp_from_py_object(py_lp_ctx);
42         if (lp_ctx == NULL) {
43                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
44                 return NULL;
45         }
46
47         ldb = PyLdb_AsLdbContext(self);
48
49         ldb_set_opaque(ldb, "loadparm", lp_ctx);
50
51         Py_RETURN_NONE;
52 }
53
54 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
55 {
56         PyObject *py_creds;
57         struct cli_credentials *creds;
58         struct ldb_context *ldb;
59
60         if (!PyArg_ParseTuple(args, "O", &py_creds))
61                 return NULL;
62
63         creds = cli_credentials_from_py_object(py_creds);
64         if (creds == NULL) {
65                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
66                 return NULL;
67         }
68
69         ldb = PyLdb_AsLdbContext(self);
70
71         ldb_set_opaque(ldb, "credentials", creds);
72
73         Py_RETURN_NONE;
74 }
75
76 /* XXX: This function really should be in libldb's pyldb.c */
77 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
78 {
79         int value;
80         int *old_val, *new_val;
81         char *py_opaque_name, *opaque_name_talloc;
82         struct ldb_context *ldb;
83         TALLOC_CTX *tmp_ctx;
84
85         if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
86                 return NULL;
87
88         ldb = PyLdb_AsLdbContext(self);
89
90         /* see if we have a cached copy */
91         old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
92         /* XXX: We shouldn't just blindly assume that the value that is 
93          * already present has the size of an int and is not shared 
94          * with other code that may rely on it not changing. 
95          * JRV 20100403 */
96
97         if (old_val) {
98                 *old_val = value;
99                 Py_RETURN_NONE;
100         } 
101
102         tmp_ctx = talloc_new(ldb);
103         if (tmp_ctx == NULL) {
104                 PyErr_NoMemory();
105                 return NULL;
106         }
107         
108         new_val = talloc(tmp_ctx, int);
109         if (new_val == NULL) {
110                 talloc_free(tmp_ctx);
111                 PyErr_NoMemory();
112                 return NULL;
113         }
114         
115         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
116         if (opaque_name_talloc == NULL) {
117                 talloc_free(tmp_ctx);
118                 PyErr_NoMemory();
119                 return NULL;
120         }
121         
122         *new_val = value;
123
124         /* cache the domain_sid in the ldb */
125         if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
126                 talloc_free(tmp_ctx);
127                 PyErr_SetString(PyExc_RuntimeError,
128                                         "Failed to set opaque integer into the ldb");
129                 return NULL;
130         }
131
132         talloc_steal(ldb, new_val);
133         talloc_steal(ldb, opaque_name_talloc);
134         talloc_free(tmp_ctx);
135
136         Py_RETURN_NONE;
137 }
138
139 static PyMethodDef py_samba_ldb_methods[] = {
140         { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, 
141                 "ldb_set_loadparm(ldb, session_info)\n"
142                 "Set loadparm context to use when connecting." },
143         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
144                 "ldb_set_credentials(ldb, credentials)\n"
145                 "Set credentials to use when connecting." },
146         { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer,
147                 METH_VARARGS, NULL },
148         { NULL },
149 };
150
151 static PyTypeObject PySambaLdb = {
152         .tp_name = "samba.Ldb",
153         .tp_doc = "Connection to a LDB database.",
154         .tp_methods = py_samba_ldb_methods,
155         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
156 };
157
158
159 void init_ldb(void)
160 {
161         PyObject *m;
162
163         pyldb_module = PyImport_ImportModule("ldb");
164         if (pyldb_module == NULL)
165                 return;
166
167         PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
168         if (PySambaLdb.tp_base == NULL)
169                 return;
170
171         if (PyType_Ready(&PySambaLdb) < 0)
172                 return;
173
174         m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
175         if (m == NULL)
176                 return;
177
178         Py_INCREF(&PySambaLdb);
179         PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);
180 }