s4:python Add helper to get at the domain SID
[samba.git] / source4 / scripting / python / pyglue.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    Copyright (C) Matthias Dieter Wallnöfer          2009
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "ldb.h"
22 #include "ldb_errors.h"
23 #include "ldb_wrap.h"
24 #include "param/param.h"
25 #include "auth/credentials/credentials.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "lib/ldb-samba/ldif_handlers.h"
28 #include "librpc/ndr/libndr.h"
29 #include "version.h"
30 #include <Python.h>
31 #include "lib/ldb/pyldb.h"
32 #include "libcli/util/pyerrors.h"
33 #include "libcli/security/security.h"
34 #include "auth/pyauth.h"
35 #include "param/pyparam.h"
36 #include "auth/credentials/pycredentials.h"
37
38 #ifndef Py_RETURN_NONE
39 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
40 #endif
41
42 /* FIXME: These should be in a header file somewhere, once we finish moving
43  * away from SWIG .. */
44 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
45 /*      if (!PyLdb_Check(py_ldb)) { \
46                 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
47                 return NULL; \
48         } */\
49         ldb = PyLdb_AsLdbContext(py_ldb);
50
51 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
52 {
53         if (ret == LDB_ERR_PYTHON_EXCEPTION)
54                 return; /* Python exception should already be set, just keep that */
55
56         PyErr_SetObject(error, 
57                                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret, 
58                                   ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
59 }
60
61 static PyObject *py_ldb_get_exception(void)
62 {
63         PyObject *mod = PyImport_ImportModule("ldb");
64         if (mod == NULL)
65                 return NULL;
66
67         return PyObject_GetAttrString(mod, "LdbError");
68 }
69
70 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
71 {
72         int len;
73         PyObject *ret;
74         char *retstr;
75         if (!PyArg_ParseTuple(args, "i", &len))
76                 return NULL;
77
78         retstr = generate_random_str(NULL, len);
79         ret = PyString_FromString(retstr);
80         talloc_free(retstr);
81         return ret;
82 }
83
84 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
85 {
86         time_t t;
87         NTTIME nt;
88         if (!PyArg_ParseTuple(args, "I", &t))
89                 return NULL;
90
91         unix_to_nt_time(&nt, t);
92
93         return PyInt_FromLong((uint64_t)nt);
94 }
95
96 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
97 {
98         PyObject *py_creds, *py_ldb;
99         struct cli_credentials *creds;
100         struct ldb_context *ldb;
101         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
102                 return NULL;
103
104         PyErr_LDB_OR_RAISE(py_ldb, ldb);
105         
106         creds = cli_credentials_from_py_object(py_creds);
107         if (creds == NULL) {
108                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
109                 return NULL;
110         }
111
112         ldb_set_opaque(ldb, "credentials", creds);
113
114         Py_RETURN_NONE;
115 }
116
117 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
118 {
119         PyObject *py_lp_ctx, *py_ldb;
120         struct loadparm_context *lp_ctx;
121         struct ldb_context *ldb;
122         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
123                 return NULL;
124
125         PyErr_LDB_OR_RAISE(py_ldb, ldb);
126
127         lp_ctx = lp_from_py_object(py_lp_ctx);
128         if (lp_ctx == NULL) {
129                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
130                 return NULL;
131         }
132
133         ldb_set_opaque(ldb, "loadparm", lp_ctx);
134
135         Py_RETURN_NONE;
136 }
137
138
139 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
140 {
141         PyObject *py_session_info, *py_ldb;
142         struct auth_session_info *info;
143         struct ldb_context *ldb;
144         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
145                 return NULL;
146
147         PyErr_LDB_OR_RAISE(py_ldb, ldb);
148         /*if (!PyAuthSession_Check(py_session_info)) {
149                 PyErr_SetString(PyExc_TypeError, "Expected session info object");
150                 return NULL;
151         }*/
152
153         info = PyAuthSession_AsSession(py_session_info);
154
155         ldb_set_opaque(ldb, "sessionInfo", info);
156
157         Py_RETURN_NONE;
158 }
159
160 static PyObject *py_ldb_set_utf8_casefold(PyObject *self, PyObject *args)
161 {
162         PyObject *py_ldb;
163         struct ldb_context *ldb;
164
165         if (!PyArg_ParseTuple(args, "O", &py_ldb))
166                 return NULL;
167
168         PyErr_LDB_OR_RAISE(py_ldb, ldb);
169
170         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
171
172         Py_RETURN_NONE;
173 }
174
175 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
176
177         PyObject *py_ldb, *py_sid;
178         struct ldb_context *ldb;
179         struct dom_sid *sid;
180         bool ret;
181
182         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
183                 return NULL;
184         
185         PyErr_LDB_OR_RAISE(py_ldb, ldb);
186
187         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
188
189         ret = samdb_set_domain_sid(ldb, sid);
190         if (!ret) {
191                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
192                 return NULL;
193         } 
194         Py_RETURN_NONE;
195 }
196
197 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
198
199         PyObject *py_ldb;
200         struct ldb_context *ldb;
201         const struct dom_sid *sid;
202         PyObject *ret;
203         char *retstr;
204
205         if (!PyArg_ParseTuple(args, "O", &py_ldb))
206                 return NULL;
207         
208         PyErr_LDB_OR_RAISE(py_ldb, ldb);
209
210         sid = samdb_domain_sid(ldb);
211         if (!sid) {
212                 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
213                 return NULL;
214         } 
215         retstr = dom_sid_string(NULL, sid);
216         ret = PyString_FromString(retstr);
217         talloc_free(retstr);
218         return ret;
219 }
220
221 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
222 {
223         PyObject *py_ldb;
224         struct ldb_context *ldb;
225         int ret;
226
227         if (!PyArg_ParseTuple(args, "O", &py_ldb))
228                 return NULL;
229
230         PyErr_LDB_OR_RAISE(py_ldb, ldb);
231         ret = ldb_register_samba_handlers(ldb);
232
233         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
234         Py_RETURN_NONE;
235 }
236
237 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
238 {
239         PyObject *py_ldb, *py_guid;
240         bool ret;
241         struct GUID guid;
242         struct ldb_context *ldb;
243         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
244                 return NULL;
245
246         PyErr_LDB_OR_RAISE(py_ldb, ldb);
247         GUID_from_string(PyString_AsString(py_guid), &guid);
248
249         ret = samdb_set_ntds_invocation_id(ldb, &guid);
250         if (!ret) {
251                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
252                 return NULL;
253         }
254         Py_RETURN_NONE;
255 }
256
257 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
258 {
259         PyObject *py_ldb;
260         int value;
261         int *old_val, *new_val;
262         char *py_opaque_name, *opaque_name_talloc;
263         struct ldb_context *ldb;
264         TALLOC_CTX *tmp_ctx;
265
266         if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
267                 return NULL;
268
269         PyErr_LDB_OR_RAISE(py_ldb, ldb);
270
271         /* see if we have a cached copy */
272         old_val = (int *)ldb_get_opaque(ldb, 
273                                         py_opaque_name);
274
275         if (old_val) {
276                 *old_val = value;
277                 Py_RETURN_NONE;
278         } 
279
280         tmp_ctx = talloc_new(ldb);
281         if (tmp_ctx == NULL) {
282                 goto failed;
283         }
284         
285         new_val = talloc(tmp_ctx, int);
286         if (!new_val) {
287                 goto failed;
288         }
289         
290         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
291         if (!opaque_name_talloc) {
292                 goto failed;
293         }
294         
295         *new_val = value;
296
297         /* cache the domain_sid in the ldb */
298         if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
299                 goto failed;
300         }
301
302         talloc_steal(ldb, new_val);
303         talloc_steal(ldb, opaque_name_talloc);
304         talloc_free(tmp_ctx);
305
306         Py_RETURN_NONE;
307
308 failed:
309         talloc_free(tmp_ctx);
310         PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb!\n");
311         return NULL;
312 }
313
314 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
315 {
316         PyObject *py_ldb;
317         struct ldb_context *ldb;
318         int ret;
319         if (!PyArg_ParseTuple(args, "O", &py_ldb))
320                 return NULL;
321
322         PyErr_LDB_OR_RAISE(py_ldb, ldb);
323
324         ret = dsdb_set_global_schema(ldb);
325         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
326
327         Py_RETURN_NONE;
328 }
329
330 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
331 {
332         WERROR result;
333         char *pf, *df;
334         PyObject *py_ldb;
335         struct ldb_context *ldb;
336
337         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
338                 return NULL;
339
340         PyErr_LDB_OR_RAISE(py_ldb, ldb);
341
342         result = dsdb_set_schema_from_ldif(ldb, pf, df);
343         PyErr_WERROR_IS_ERR_RAISE(result);
344
345         Py_RETURN_NONE;
346 }
347
348 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
349 {
350         char *target_str, *mapping;
351         PyObject *py_ldb;
352         struct ldb_context *ldb;
353         PyObject *ret;
354         char *retstr;
355
356         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
357                 return NULL;
358
359         PyErr_LDB_OR_RAISE(py_ldb, ldb);
360
361         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
362         if (!retstr) {
363                 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
364                 return NULL;
365         } 
366         ret = PyString_FromString(retstr);
367         talloc_free(retstr);
368         return ret;
369 }
370
371 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
372 {
373         PyObject *py_ldb;
374         struct ldb_context *ldb;
375         WERROR result;
376         struct dsdb_schema *schema;
377
378         if (!PyArg_ParseTuple(args, "O", &py_ldb))
379                 return NULL;
380
381         PyErr_LDB_OR_RAISE(py_ldb, ldb);
382
383         schema = dsdb_get_schema(ldb);
384         if (!schema) {
385                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
386                 return NULL;
387         }
388
389         result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
390         PyErr_WERROR_IS_ERR_RAISE(result);
391
392         Py_RETURN_NONE;
393 }
394
395 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
396 {
397         PyObject *py_ldb;
398         struct ldb_context *ldb;
399         PyObject *py_from_ldb;
400         struct ldb_context *from_ldb;
401         struct dsdb_schema *schema;
402         int ret;
403         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_from_ldb))
404                 return NULL;
405
406         PyErr_LDB_OR_RAISE(py_ldb, ldb);
407
408         PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
409
410         schema = dsdb_get_schema(from_ldb);
411         if (!schema) {
412                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
413                 return NULL;
414         }
415
416         ret = dsdb_reference_schema(ldb, schema, true);
417         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
418
419         Py_RETURN_NONE;
420 }
421
422 static PyObject *py_dom_sid_to_rid(PyLdbObject *self, PyObject *args)
423 {
424         PyObject *py_sid;
425         struct dom_sid *sid;
426         uint32_t rid;
427         NTSTATUS status;
428         
429         if(!PyArg_ParseTuple(args, "O", &py_sid))
430                 return NULL;
431
432         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
433
434         status = dom_sid_split_rid(NULL, sid, NULL, &rid);
435         if (!NT_STATUS_IS_OK(status)) {
436                 PyErr_SetString(PyExc_RuntimeError, "dom_sid_split_rid failed");
437                 return NULL;
438         }
439
440         return PyInt_FromLong(rid);
441 }
442
443 static PyMethodDef py_misc_methods[] = {
444         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
445                 "random_password(len) -> string\n"
446                 "Generate random password with specified length." },
447         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
448                 "unix2nttime(timestamp) -> nttime" },
449         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, 
450                 "ldb_set_credentials(ldb, credentials) -> None\n"
451                 "Set credentials to use when connecting." },
452         { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
453                 "ldb_set_session_info(ldb, session_info)\n"
454                 "Set session info to use when connecting." },
455         { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
456                 "ldb_set_loadparm(ldb, session_info)\n"
457                 "Set loadparm context to use when connecting." },
458         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
459                 "samdb_set_domain_sid(samdb, sid)\n"
460                 "Set SID of domain to use." },
461         { "samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid, METH_VARARGS,
462                 "samdb_get_domain_sid(samdb)\n"
463                 "Get SID of domain in use." },
464         { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
465                 "ldb_register_samba_handlers(ldb)\n"
466                 "Register Samba-specific LDB modules and schemas." },
467         { "ldb_set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold, METH_VARARGS,
468                 "ldb_set_utf8_casefold(ldb)\n"
469                 "Set the right Samba casefolding function for UTF8 charset." },
470         { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
471                 NULL },
472         { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer, METH_VARARGS,
473                 NULL },
474         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
475                 NULL },
476         { "dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
477                 NULL },
478         { "dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
479                 NULL },
480         { "dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
481                 NULL },
482         { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
483                 NULL },
484         { "dom_sid_to_rid", (PyCFunction)py_dom_sid_to_rid, METH_VARARGS,
485                 NULL },
486         { NULL }
487 };
488
489 void initglue(void)
490 {
491         PyObject *m;
492
493         m = Py_InitModule3("glue", py_misc_methods, 
494                            "Python bindings for miscellaneous Samba functions.");
495         if (m == NULL)
496                 return;
497
498         PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
499
500         PyModule_AddObject(m, "DS_BEHAVIOR_WIN2000", PyInt_FromLong(DS_BEHAVIOR_WIN2000));
501         PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003_INTERIM", PyInt_FromLong(DS_BEHAVIOR_WIN2003_INTERIM));
502         PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003", PyInt_FromLong(DS_BEHAVIOR_WIN2003));
503         PyModule_AddObject(m, "DS_BEHAVIOR_WIN2008", PyInt_FromLong(DS_BEHAVIOR_WIN2008));
504
505 }
506