s4-python: Move set_global_schema to pydsdb.
[metze/samba/wip.git] / source4 / dsdb / pydsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
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 <Python.h>
21 #include "includes.h"
22 #include "dsdb/samdb/samdb.h"
23 #include "lib/ldb/pyldb.h"
24 #include "libcli/security/security.h"
25 #include "librpc/ndr/libndr.h"
26
27 /* FIXME: These should be in a header file somewhere, once we finish moving
28  * away from SWIG .. */
29 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
30 /*      if (!PyLdb_Check(py_ldb)) { \
31                 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
32                 return NULL; \
33         } */\
34         ldb = PyLdb_AsLdbContext(py_ldb);
35
36 static PyObject *py_ldb_get_exception(void)
37 {
38         PyObject *mod = PyImport_ImportModule("ldb");
39         if (mod == NULL)
40                 return NULL;
41
42         return PyObject_GetAttrString(mod, "LdbError");
43 }
44
45 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
46 {
47         if (ret == LDB_ERR_PYTHON_EXCEPTION)
48                 return; /* Python exception should already be set, just keep that */
49
50         PyErr_SetObject(error, 
51                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
52                         ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
53 }
54
55 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
56 {
57         PyObject *py_ldb, *result;
58         struct ldb_context *ldb;
59         const char *site;
60         TALLOC_CTX *mem_ctx;
61
62         if (!PyArg_ParseTuple(args, "O", &py_ldb))
63                 return NULL;
64
65         PyErr_LDB_OR_RAISE(py_ldb, ldb);
66
67         mem_ctx = talloc_new(NULL);
68
69         site = samdb_server_site_name(ldb, mem_ctx);
70         if (site == NULL) {
71                 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
72                 talloc_free(mem_ctx);
73                 return NULL;
74         }
75
76         result = PyString_FromString(site);
77         talloc_free(mem_ctx);
78         return result;
79 }
80
81 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
82                                                                                                         PyObject *args)
83 {
84         char *target_str, *mapping;
85         PyObject *py_ldb;
86         struct ldb_context *ldb;
87         PyObject *ret;
88         char *retstr;
89
90         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
91                 return NULL;
92
93         PyErr_LDB_OR_RAISE(py_ldb, ldb);
94
95         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
96         if (retstr == NULL) {
97                 PyErr_SetString(PyExc_RuntimeError,
98                                                 "dsdb_convert_schema_to_openldap failed");
99                 return NULL;
100         } 
101
102         ret = PyString_FromString(retstr);
103         talloc_free(retstr);
104         return ret;
105 }
106
107 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
108
109         PyObject *py_ldb, *py_sid;
110         struct ldb_context *ldb;
111         struct dom_sid *sid;
112         bool ret;
113
114         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
115                 return NULL;
116         
117         PyErr_LDB_OR_RAISE(py_ldb, ldb);
118
119         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
120
121         ret = samdb_set_domain_sid(ldb, sid);
122         if (!ret) {
123                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
124                 return NULL;
125         } 
126         Py_RETURN_NONE;
127 }
128
129 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
130
131         PyObject *py_ldb;
132         struct ldb_context *ldb;
133         const struct dom_sid *sid;
134         PyObject *ret;
135         char *retstr;
136
137         if (!PyArg_ParseTuple(args, "O", &py_ldb))
138                 return NULL;
139         
140         PyErr_LDB_OR_RAISE(py_ldb, ldb);
141
142         sid = samdb_domain_sid(ldb);
143         if (!sid) {
144                 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
145                 return NULL;
146         } 
147         retstr = dom_sid_string(NULL, sid);
148         ret = PyString_FromString(retstr);
149         talloc_free(retstr);
150         return ret;
151 }
152
153 static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
154 {
155         PyObject *py_ldb, *result;
156         struct ldb_context *ldb;
157         TALLOC_CTX *mem_ctx;
158         const struct GUID *guid;
159
160         mem_ctx = talloc_new(NULL);
161         if (mem_ctx == NULL) {
162                 PyErr_NoMemory();
163                 return NULL;
164         }
165
166         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
167                 talloc_free(mem_ctx);
168                 return NULL;
169         }
170
171         PyErr_LDB_OR_RAISE(py_ldb, ldb);
172
173         guid = samdb_ntds_invocation_id(ldb);
174         if (guid == NULL) {
175                 PyErr_SetString(PyExc_RuntimeError,
176                                                 "Failed to find NTDS invocation ID");
177                 talloc_free(mem_ctx);
178                 return NULL;
179         }
180
181         result = PyString_FromString(GUID_string(mem_ctx, guid));
182         talloc_free(mem_ctx);
183         return result;
184 }
185
186 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
187 {
188         PyObject *py_ldb, *py_guid;
189         bool ret;
190         struct GUID guid;
191         struct ldb_context *ldb;
192         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
193                 return NULL;
194
195         PyErr_LDB_OR_RAISE(py_ldb, ldb);
196         GUID_from_string(PyString_AsString(py_guid), &guid);
197
198         ret = samdb_set_ntds_invocation_id(ldb, &guid);
199         if (!ret) {
200                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
201                 return NULL;
202         }
203         Py_RETURN_NONE;
204 }
205
206 static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
207 {
208         PyObject *py_ldb, *result;
209         struct ldb_context *ldb;
210         TALLOC_CTX *mem_ctx;
211         const struct GUID *guid;
212
213         mem_ctx = talloc_new(NULL);
214         if (mem_ctx == NULL) {
215                 PyErr_NoMemory();
216                 return NULL;
217         }
218
219         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
220                 talloc_free(mem_ctx);
221                 return NULL;
222         }
223
224         PyErr_LDB_OR_RAISE(py_ldb, ldb);
225
226         guid = samdb_ntds_objectGUID(ldb);
227         if (guid == NULL) {
228                 PyErr_SetString(PyExc_RuntimeError, "Failed to find NTDS GUID");
229                 talloc_free(mem_ctx);
230                 return NULL;
231         }
232
233         result = PyString_FromString(GUID_string(mem_ctx, guid));
234         talloc_free(mem_ctx);
235         return result;
236 }
237
238 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
239 {
240         PyObject *py_ldb;
241         struct ldb_context *ldb;
242         int ret;
243         if (!PyArg_ParseTuple(args, "O", &py_ldb))
244                 return NULL;
245
246         PyErr_LDB_OR_RAISE(py_ldb, ldb);
247
248         ret = dsdb_set_global_schema(ldb);
249         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
250
251         Py_RETURN_NONE;
252 }
253
254 static PyMethodDef py_dsdb_methods[] = {
255         { "samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
256                 METH_VARARGS, "Get the server site name as a string"},
257         { "dsdb_convert_schema_to_openldap",
258                 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS, 
259                 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
260                 "Create an OpenLDAP schema from a schema." },
261         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid,
262                 METH_VARARGS,
263                 "samdb_set_domain_sid(samdb, sid)\n"
264                 "Set SID of domain to use." },
265         { "samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid,
266                 METH_VARARGS,
267                 "samdb_get_domain_sid(samdb)\n"
268                 "Get SID of domain in use." },
269         { "samdb_ntds_invocation_id", (PyCFunction)py_samdb_ntds_invocation_id,
270                 METH_VARARGS, "get the NTDS invocation ID GUID as a string"},
271         { "dsdb_set_ntds_invocation_id",
272                 (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
273                 NULL },
274         { "samdb_ntds_objectGUID", (PyCFunction)py_samdb_ntds_objectGUID,
275                 METH_VARARGS, "get the NTDS objectGUID as a string"},
276         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema,
277                 METH_VARARGS, NULL },
278         { NULL }
279 };
280
281 void initdsdb(void)
282 {
283         PyObject *m;
284
285         m = Py_InitModule3("dsdb", py_dsdb_methods, 
286                            "Python bindings for the directory service databases.");
287         if (m == NULL)
288                 return;
289
290         /* "userAccountControl" flags */
291         PyModule_AddObject(m, "UF_NORMAL_ACCOUNT",
292                                            PyInt_FromLong(UF_NORMAL_ACCOUNT));
293         PyModule_AddObject(m, "UF_TEMP_DUPLICATE_ACCOUNT",
294                                            PyInt_FromLong(UF_TEMP_DUPLICATE_ACCOUNT));
295         PyModule_AddObject(m, "UF_SERVER_TRUST_ACCOUNT",
296                                            PyInt_FromLong(UF_SERVER_TRUST_ACCOUNT));
297         PyModule_AddObject(m, "UF_WORKSTATION_TRUST_ACCOUNT",
298                                            PyInt_FromLong(UF_WORKSTATION_TRUST_ACCOUNT));
299         PyModule_AddObject(m, "UF_INTERDOMAIN_TRUST_ACCOUNT",
300                                            PyInt_FromLong(UF_INTERDOMAIN_TRUST_ACCOUNT));
301         PyModule_AddObject(m, "UF_PASSWD_NOTREQD",
302                                            PyInt_FromLong(UF_PASSWD_NOTREQD));
303         PyModule_AddObject(m, "UF_ACCOUNTDISABLE",
304                                            PyInt_FromLong(UF_ACCOUNTDISABLE));
305
306         /* "groupType" flags */
307         PyModule_AddObject(m, "GTYPE_SECURITY_BUILTIN_LOCAL_GROUP",
308                                            PyInt_FromLong(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP));
309         PyModule_AddObject(m, "GTYPE_SECURITY_GLOBAL_GROUP",
310                                            PyInt_FromLong(GTYPE_SECURITY_GLOBAL_GROUP));
311         PyModule_AddObject(m, "GTYPE_SECURITY_DOMAIN_LOCAL_GROUP",
312                                            PyInt_FromLong(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP));
313         PyModule_AddObject(m, "GTYPE_SECURITY_UNIVERSAL_GROUP",
314                                            PyInt_FromLong(GTYPE_SECURITY_UNIVERSAL_GROUP));
315         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_GLOBAL_GROUP",
316                                            PyInt_FromLong(GTYPE_DISTRIBUTION_GLOBAL_GROUP));
317         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP",
318                                            PyInt_FromLong(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP));
319         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_UNIVERSAL_GROUP",
320                                            PyInt_FromLong(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP));
321
322         /* "sAMAccountType" flags */
323         PyModule_AddObject(m, "ATYPE_NORMAL_ACCOUNT",
324                                            PyInt_FromLong(ATYPE_NORMAL_ACCOUNT));
325         PyModule_AddObject(m, "ATYPE_WORKSTATION_TRUST",
326                                            PyInt_FromLong(ATYPE_WORKSTATION_TRUST));
327         PyModule_AddObject(m, "ATYPE_INTERDOMAIN_TRUST",
328                                            PyInt_FromLong(ATYPE_INTERDOMAIN_TRUST));
329         PyModule_AddObject(m, "ATYPE_SECURITY_GLOBAL_GROUP",
330                                            PyInt_FromLong(ATYPE_SECURITY_GLOBAL_GROUP));
331         PyModule_AddObject(m, "ATYPE_SECURITY_LOCAL_GROUP",
332                                            PyInt_FromLong(ATYPE_SECURITY_LOCAL_GROUP));
333         PyModule_AddObject(m, "ATYPE_SECURITY_UNIVERSAL_GROUP",
334                                            PyInt_FromLong(ATYPE_SECURITY_UNIVERSAL_GROUP));
335         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_GLOBAL_GROUP",
336                                            PyInt_FromLong(ATYPE_DISTRIBUTION_GLOBAL_GROUP));
337         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_LOCAL_GROUP",
338                                            PyInt_FromLong(ATYPE_DISTRIBUTION_LOCAL_GROUP));
339         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_UNIVERSAL_GROUP",
340                                            PyInt_FromLong(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP));
341
342         /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
343         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2000",
344                                            PyInt_FromLong(DS_DOMAIN_FUNCTION_2000));
345         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003_MIXED",
346                                            PyInt_FromLong(DS_DOMAIN_FUNCTION_2003_MIXED));
347         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003",
348                                            PyInt_FromLong(DS_DOMAIN_FUNCTION_2003));
349         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008",
350                                            PyInt_FromLong(DS_DOMAIN_FUNCTION_2008));
351         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008_R2",
352                                            PyInt_FromLong(DS_DOMAIN_FUNCTION_2008_R2));
353
354         /* "domainControllerFunctionality" flags in the rootDSE */
355         PyModule_AddObject(m, "DS_DC_FUNCTION_2000",
356                                            PyInt_FromLong(DS_DC_FUNCTION_2000));
357         PyModule_AddObject(m, "DS_DC_FUNCTION_2003",
358                                            PyInt_FromLong(DS_DC_FUNCTION_2003));
359         PyModule_AddObject(m, "DS_DC_FUNCTION_2008",
360                                            PyInt_FromLong(DS_DC_FUNCTION_2008));
361         PyModule_AddObject(m, "DS_DC_FUNCTION_2008_R2",
362                                            PyInt_FromLong(DS_DC_FUNCTION_2008_R2));
363 }