Add a way to set an opaque integer onto a samdb
[ira/wip.git] / source4 / scripting / python / pyglue.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "includes.h"
20 #include "ldb.h"
21 #include "ldb_errors.h"
22 #include "param/param.h"
23 #include "auth/credentials/credentials.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb-samba/ldif_handlers.h"
26 #include "librpc/ndr/libndr.h"
27 #include "version.h"
28 #include <Python.h>
29 #include "lib/ldb/pyldb.h"
30 #include "libcli/util/pyerrors.h"
31 #include "libcli/security/security.h"
32 #include "auth/pyauth.h"
33 #include "param/pyparam.h"
34 #include "auth/credentials/pycredentials.h"
35
36 #ifndef Py_RETURN_NONE
37 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
38 #endif
39
40 /* FIXME: These should be in a header file somewhere, once we finish moving
41  * away from SWIG .. */
42 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
43 /*      if (!PyLdb_Check(py_ldb)) { \
44                 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
45                 return NULL; \
46         } */\
47         ldb = PyLdb_AsLdbContext(py_ldb);
48
49
50 static PyObject *py_ldb_get_exception(void)
51 {
52         PyObject *mod = PyImport_ImportModule("ldb");
53         if (mod == NULL)
54                 return NULL;
55
56         return PyObject_GetAttrString(mod, "LdbError");
57 }
58
59 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
60 {
61         int len;
62         PyObject *ret;
63         char *retstr;
64         if (!PyArg_ParseTuple(args, "i", &len))
65                 return NULL;
66
67         retstr = generate_random_str(NULL, len);
68         ret = PyString_FromString(retstr);
69         talloc_free(retstr);
70         return ret;
71 }
72
73 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
74 {
75         time_t t;
76         NTTIME nt;
77         if (!PyArg_ParseTuple(args, "I", &t))
78                 return NULL;
79
80         unix_to_nt_time(&nt, t);
81
82         return PyInt_FromLong((uint64_t)nt);
83 }
84
85 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
86 {
87         PyObject *py_creds, *py_ldb;
88         struct cli_credentials *creds;
89         struct ldb_context *ldb;
90         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
91                 return NULL;
92
93         PyErr_LDB_OR_RAISE(py_ldb, ldb);
94         
95         creds = cli_credentials_from_py_object(py_creds);
96         if (creds == NULL) {
97                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
98                 return NULL;
99         }
100
101         ldb_set_opaque(ldb, "credentials", creds);
102
103         Py_RETURN_NONE;
104 }
105
106 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
107 {
108         PyObject *py_lp_ctx, *py_ldb;
109         struct loadparm_context *lp_ctx;
110         struct ldb_context *ldb;
111         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
112                 return NULL;
113
114         PyErr_LDB_OR_RAISE(py_ldb, ldb);
115
116         lp_ctx = lp_from_py_object(py_lp_ctx);
117         if (lp_ctx == NULL) {
118                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
119                 return NULL;
120         }
121
122         ldb_set_opaque(ldb, "loadparm", lp_ctx);
123
124         Py_RETURN_NONE;
125 }
126
127
128 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
129 {
130         PyObject *py_session_info, *py_ldb;
131         struct auth_session_info *info;
132         struct ldb_context *ldb;
133         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
134                 return NULL;
135
136         PyErr_LDB_OR_RAISE(py_ldb, ldb);
137         /*if (!PyAuthSession_Check(py_session_info)) {
138                 PyErr_SetString(PyExc_TypeError, "Expected session info object");
139                 return NULL;
140         }*/
141
142         info = PyAuthSession_AsSession(py_session_info);
143
144         ldb_set_opaque(ldb, "sessionInfo", info);
145
146         Py_RETURN_NONE;
147 }
148
149 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
150
151         PyObject *py_ldb, *py_sid;
152         struct ldb_context *ldb;
153         struct dom_sid *sid;
154         bool ret;
155
156         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
157                 return NULL;
158         
159         PyErr_LDB_OR_RAISE(py_ldb, ldb);
160
161         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
162
163         ret = samdb_set_domain_sid(ldb, sid);
164         if (!ret) {
165                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
166                 return NULL;
167         } 
168         Py_RETURN_NONE;
169 }
170
171 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
172 {
173         PyObject *py_ldb;
174         struct ldb_context *ldb;
175         int ret;
176
177         if (!PyArg_ParseTuple(args, "O", &py_ldb))
178                 return NULL;
179
180         PyErr_LDB_OR_RAISE(py_ldb, ldb);
181         ret = ldb_register_samba_handlers(ldb);
182
183         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
184         Py_RETURN_NONE;
185 }
186
187 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
188 {
189         PyObject *py_ldb, *py_guid;
190         bool ret;
191         struct GUID guid;
192         struct ldb_context *ldb;
193         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
194                 return NULL;
195
196         PyErr_LDB_OR_RAISE(py_ldb, ldb);
197         GUID_from_string(PyString_AsString(py_guid), &guid);
198
199         ret = samdb_set_ntds_invocation_id(ldb, &guid);
200         if (!ret) {
201                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
202                 return NULL;
203         }
204         Py_RETURN_NONE;
205 }
206
207 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
208 {
209         PyObject *py_ldb;
210         int value;
211         int *old_val, *new_val;
212         char *py_opaque_name, *opaque_name_talloc;
213         struct ldb_context *ldb;
214         TALLOC_CTX *tmp_ctx;
215
216         if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
217                 return NULL;
218
219         PyErr_LDB_OR_RAISE(py_ldb, ldb);
220
221         /* see if we have a cached copy */
222         old_val = (int *)ldb_get_opaque(ldb, 
223                                         py_opaque_name);
224
225         if (old_val) {
226                 *old_val = value;
227                 Py_RETURN_NONE;
228         } 
229
230         tmp_ctx = talloc_new(ldb);
231         if (tmp_ctx == NULL) {
232                 goto failed;
233         }
234         
235         new_val = talloc(tmp_ctx, int);
236         if (!new_val) {
237                 goto failed;
238         }
239         
240         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
241         if (!opaque_name_talloc) {
242                 goto failed;
243         }
244         
245         *new_val = value;
246
247         /* cache the domain_sid in the ldb */
248         if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
249                 goto failed;
250         }
251
252         talloc_steal(ldb, new_val);
253         talloc_steal(ldb, opaque_name_talloc);
254         talloc_free(tmp_ctx);
255
256         Py_RETURN_NONE;
257
258 failed:
259         talloc_free(tmp_ctx);
260         PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb!\n");
261         return NULL;
262 }
263
264 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
265 {
266         PyObject *py_ldb;
267         struct ldb_context *ldb;
268         int ret;
269         if (!PyArg_ParseTuple(args, "O", &py_ldb))
270                 return NULL;
271
272         PyErr_LDB_OR_RAISE(py_ldb, ldb);
273
274         ret = dsdb_set_global_schema(ldb);
275         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
276
277         Py_RETURN_NONE;
278 }
279
280 static PyObject *py_dsdb_attach_schema_from_ldif(PyObject *self, PyObject *args)
281 {
282         WERROR result;
283         char *pf, *df;
284         PyObject *py_ldb;
285         struct ldb_context *ldb;
286
287         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
288                 return NULL;
289
290         PyErr_LDB_OR_RAISE(py_ldb, ldb);
291
292         result = dsdb_attach_schema_from_ldif(ldb, pf, df);
293         PyErr_WERROR_IS_ERR_RAISE(result);
294
295         Py_RETURN_NONE;
296 }
297
298 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
299 {
300         char *target_str, *mapping;
301         PyObject *py_ldb;
302         struct ldb_context *ldb;
303         PyObject *ret;
304         char *retstr;
305
306         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
307                 return NULL;
308
309         PyErr_LDB_OR_RAISE(py_ldb, ldb);
310
311         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
312         if (!retstr) {
313                 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
314                 return NULL;
315         } 
316         ret = PyString_FromString(retstr);
317         talloc_free(retstr);
318         return ret;
319 }
320
321 static PyMethodDef py_misc_methods[] = {
322         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
323                 "random_password(len) -> string\n"
324                 "Generate random password with specified length." },
325         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
326                 "unix2nttime(timestamp) -> nttime" },
327         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, 
328                 "ldb_set_credentials(ldb, credentials) -> None\n"
329                 "Set credentials to use when connecting." },
330         { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
331                 "ldb_set_session_info(ldb, session_info)\n"
332                 "Set session info to use when connecting." },
333         { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
334                 "ldb_set_loadparm(ldb, session_info)\n"
335                 "Set loadparm context to use when connecting." },
336         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
337                 "samdb_set_domain_sid(samdb, sid)\n"
338                 "Set SID of domain to use." },
339         { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
340                 "ldb_register_samba_handlers(ldb)\n"
341                 "Register Samba-specific LDB modules and schemas." },
342         { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
343                 NULL },
344         { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer, METH_VARARGS,
345                 NULL },
346         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
347                 NULL },
348         { "dsdb_attach_schema_from_ldif", (PyCFunction)py_dsdb_attach_schema_from_ldif, METH_VARARGS,
349                 NULL },
350         { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
351                 NULL },
352         { NULL }
353 };
354
355 void initglue(void)
356 {
357         PyObject *m;
358
359         m = Py_InitModule3("glue", py_misc_methods, 
360                            "Python bindings for miscellaneous Samba functions.");
361         if (m == NULL)
362                 return;
363
364         PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
365 }
366