libds:common Remove DS_DC_* domain functionality flags
[Samba/gebeck_regimport.git] / source4 / dsdb / pydsdb.c
blob4060b327af3b8503927fc8bc74b200fd1774a007
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
4 Copyright (C) Matthias Dieter Wallnöfer 2009
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.
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.
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/>.
20 #include <Python.h>
21 #include "includes.h"
22 #include "libcli/util/pyerrors.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/pyldb.h"
25 #include "libcli/security/security.h"
26 #include "librpc/ndr/libndr.h"
28 /* FIXME: These should be in a header file somewhere, once we finish moving
29 * away from SWIG .. */
30 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
31 /* if (!PyLdb_Check(py_ldb)) { \
32 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
33 return NULL; \
34 } */\
35 ldb = PyLdb_AsLdbContext(py_ldb);
37 static PyObject *py_ldb_get_exception(void)
39 PyObject *mod = PyImport_ImportModule("ldb");
40 if (mod == NULL)
41 return NULL;
43 return PyObject_GetAttrString(mod, "LdbError");
46 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
48 if (ret == LDB_ERR_PYTHON_EXCEPTION)
49 return; /* Python exception should already be set, just keep that */
51 PyErr_SetObject(error,
52 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
53 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
56 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
58 PyObject *py_ldb, *result;
59 struct ldb_context *ldb;
60 const char *site;
61 TALLOC_CTX *mem_ctx;
63 if (!PyArg_ParseTuple(args, "O", &py_ldb))
64 return NULL;
66 PyErr_LDB_OR_RAISE(py_ldb, ldb);
68 mem_ctx = talloc_new(NULL);
70 site = samdb_server_site_name(ldb, mem_ctx);
71 if (site == NULL) {
72 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
73 talloc_free(mem_ctx);
74 return NULL;
77 result = PyString_FromString(site);
78 talloc_free(mem_ctx);
79 return result;
82 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
83 PyObject *args)
85 char *target_str, *mapping;
86 PyObject *py_ldb;
87 struct ldb_context *ldb;
88 PyObject *ret;
89 char *retstr;
91 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
92 return NULL;
94 PyErr_LDB_OR_RAISE(py_ldb, ldb);
96 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
97 if (retstr == NULL) {
98 PyErr_SetString(PyExc_RuntimeError,
99 "dsdb_convert_schema_to_openldap failed");
100 return NULL;
103 ret = PyString_FromString(retstr);
104 talloc_free(retstr);
105 return ret;
108 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
110 PyObject *py_ldb, *py_sid;
111 struct ldb_context *ldb;
112 struct dom_sid *sid;
113 bool ret;
115 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
116 return NULL;
118 PyErr_LDB_OR_RAISE(py_ldb, ldb);
120 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
122 ret = samdb_set_domain_sid(ldb, sid);
123 if (!ret) {
124 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
125 return NULL;
127 Py_RETURN_NONE;
130 static PyObject *py_samdb_set_ntds_settings_dn(PyLdbObject *self, PyObject *args)
132 PyObject *py_ldb, *py_ntds_settings_dn;
133 struct ldb_context *ldb;
134 struct ldb_dn *ntds_settings_dn;
135 TALLOC_CTX *tmp_ctx;
136 bool ret;
138 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ntds_settings_dn))
139 return NULL;
141 PyErr_LDB_OR_RAISE(py_ldb, ldb);
143 tmp_ctx = talloc_new(NULL);
144 if (tmp_ctx == NULL) {
145 PyErr_NoMemory();
146 return NULL;
149 if (!PyObject_AsDn(tmp_ctx, py_ntds_settings_dn, ldb, &ntds_settings_dn)) {
150 return NULL;
153 ret = samdb_set_ntds_settings_dn(ldb, ntds_settings_dn);
154 talloc_free(tmp_ctx);
155 if (!ret) {
156 PyErr_SetString(PyExc_RuntimeError, "set_ntds_settings_dn failed");
157 return NULL;
159 Py_RETURN_NONE;
162 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
164 PyObject *py_ldb;
165 struct ldb_context *ldb;
166 const struct dom_sid *sid;
167 PyObject *ret;
168 char *retstr;
170 if (!PyArg_ParseTuple(args, "O", &py_ldb))
171 return NULL;
173 PyErr_LDB_OR_RAISE(py_ldb, ldb);
175 sid = samdb_domain_sid(ldb);
176 if (!sid) {
177 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
178 return NULL;
180 retstr = dom_sid_string(NULL, sid);
181 ret = PyString_FromString(retstr);
182 talloc_free(retstr);
183 return ret;
186 static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
188 PyObject *py_ldb, *result;
189 struct ldb_context *ldb;
190 TALLOC_CTX *mem_ctx;
191 const struct GUID *guid;
193 mem_ctx = talloc_new(NULL);
194 if (mem_ctx == NULL) {
195 PyErr_NoMemory();
196 return NULL;
199 if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
200 talloc_free(mem_ctx);
201 return NULL;
204 PyErr_LDB_OR_RAISE(py_ldb, ldb);
206 guid = samdb_ntds_invocation_id(ldb);
207 if (guid == NULL) {
208 PyErr_SetString(PyExc_RuntimeError,
209 "Failed to find NTDS invocation ID");
210 talloc_free(mem_ctx);
211 return NULL;
214 result = PyString_FromString(GUID_string(mem_ctx, guid));
215 talloc_free(mem_ctx);
216 return result;
219 static PyObject *py_dsdb_get_oid_from_attid(PyObject *self, PyObject *args)
221 PyObject *py_ldb;
222 struct ldb_context *ldb;
223 uint32_t attid;
224 struct dsdb_schema *schema;
225 const char *oid;
226 PyObject *ret;
227 TALLOC_CTX *mem_ctx;
228 WERROR status;
230 if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &attid))
231 return NULL;
233 mem_ctx = talloc_new(NULL);
234 if (mem_ctx == NULL) {
235 PyErr_NoMemory();
236 return NULL;
239 PyErr_LDB_OR_RAISE(py_ldb, ldb);
241 schema = dsdb_get_schema(ldb, NULL);
243 if (!schema) {
244 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb \n");
245 talloc_free(mem_ctx);
246 return NULL;
249 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attid,
250 mem_ctx, &oid);
251 PyErr_WERROR_IS_ERR_RAISE(status);
253 ret = PyString_FromString(oid);
255 talloc_free(mem_ctx);
257 return ret;
260 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
262 PyObject *py_ldb, *py_guid;
263 bool ret;
264 struct GUID guid;
265 struct ldb_context *ldb;
266 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
267 return NULL;
269 PyErr_LDB_OR_RAISE(py_ldb, ldb);
270 GUID_from_string(PyString_AsString(py_guid), &guid);
272 ret = samdb_set_ntds_invocation_id(ldb, &guid);
273 if (!ret) {
274 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
275 return NULL;
277 Py_RETURN_NONE;
280 static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
282 PyObject *py_ldb, *result;
283 struct ldb_context *ldb;
284 TALLOC_CTX *mem_ctx;
285 const struct GUID *guid;
287 mem_ctx = talloc_new(NULL);
288 if (mem_ctx == NULL) {
289 PyErr_NoMemory();
290 return NULL;
293 if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
294 talloc_free(mem_ctx);
295 return NULL;
298 PyErr_LDB_OR_RAISE(py_ldb, ldb);
300 guid = samdb_ntds_objectGUID(ldb);
301 if (guid == NULL) {
302 PyErr_SetString(PyExc_RuntimeError, "Failed to find NTDS GUID");
303 talloc_free(mem_ctx);
304 return NULL;
307 result = PyString_FromString(GUID_string(mem_ctx, guid));
308 talloc_free(mem_ctx);
309 return result;
312 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
314 PyObject *py_ldb;
315 struct ldb_context *ldb;
316 int ret;
317 if (!PyArg_ParseTuple(args, "O", &py_ldb))
318 return NULL;
320 PyErr_LDB_OR_RAISE(py_ldb, ldb);
322 ret = dsdb_set_global_schema(ldb);
323 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
325 Py_RETURN_NONE;
328 static PyObject *py_dsdb_load_partition_usn(PyObject *self, PyObject *args)
330 PyObject *py_dn, *py_ldb, *result;
331 struct ldb_dn *dn;
332 uint64_t highest_uSN, urgent_uSN;
333 struct ldb_context *ldb;
334 TALLOC_CTX *mem_ctx;
335 int ret;
337 mem_ctx = talloc_new(NULL);
338 if (mem_ctx == NULL) {
339 PyErr_NoMemory();
340 return NULL;
343 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dn)) {
344 talloc_free(mem_ctx);
345 return NULL;
348 PyErr_LDB_OR_RAISE(py_ldb, ldb);
350 if (!PyObject_AsDn(mem_ctx, py_dn, ldb, &dn)) {
351 talloc_free(mem_ctx);
352 return NULL;
355 ret = dsdb_load_partition_usn(ldb, dn, &highest_uSN, &urgent_uSN);
356 if (ret != LDB_SUCCESS) {
357 char *errstr = talloc_asprintf(mem_ctx, "Failed to load partition uSN - %s", ldb_errstring(ldb));
358 PyErr_SetString(PyExc_RuntimeError, errstr);
359 talloc_free(mem_ctx);
360 return NULL;
363 talloc_free(mem_ctx);
365 result = PyDict_New();
367 PyDict_SetItemString(result, "uSNHighest", PyInt_FromLong((uint64_t)highest_uSN));
368 PyDict_SetItemString(result, "uSNUrgent", PyInt_FromLong((uint64_t)urgent_uSN));
371 return result;
374 static PyObject *py_dsdb_set_am_rodc(PyObject *self, PyObject *args)
376 PyObject *py_ldb;
377 bool ret;
378 struct ldb_context *ldb;
379 int py_val;
381 if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &py_val))
382 return NULL;
384 PyErr_LDB_OR_RAISE(py_ldb, ldb);
385 ret = samdb_set_am_rodc(ldb, (bool)py_val);
386 if (!ret) {
387 PyErr_SetString(PyExc_RuntimeError, "set_am_rodc failed");
388 return NULL;
390 Py_RETURN_NONE;
393 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
395 WERROR result;
396 char *pf, *df;
397 PyObject *py_ldb;
398 struct ldb_context *ldb;
400 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
401 return NULL;
403 PyErr_LDB_OR_RAISE(py_ldb, ldb);
405 result = dsdb_set_schema_from_ldif(ldb, pf, df);
406 PyErr_WERROR_IS_ERR_RAISE(result);
408 Py_RETURN_NONE;
411 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
413 PyObject *py_ldb;
414 struct ldb_context *ldb;
415 PyObject *py_from_ldb;
416 struct ldb_context *from_ldb;
417 struct dsdb_schema *schema;
418 int ret;
419 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_from_ldb))
420 return NULL;
422 PyErr_LDB_OR_RAISE(py_ldb, ldb);
424 PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
426 schema = dsdb_get_schema(from_ldb, NULL);
427 if (!schema) {
428 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
429 return NULL;
432 ret = dsdb_reference_schema(ldb, schema, true);
433 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
435 Py_RETURN_NONE;
438 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
440 PyObject *py_ldb;
441 struct ldb_context *ldb;
442 WERROR result;
443 struct dsdb_schema *schema;
445 if (!PyArg_ParseTuple(args, "O", &py_ldb))
446 return NULL;
448 PyErr_LDB_OR_RAISE(py_ldb, ldb);
450 schema = dsdb_get_schema(ldb, NULL);
451 if (!schema) {
452 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
453 return NULL;
456 result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
457 PyErr_WERROR_IS_ERR_RAISE(result);
459 Py_RETURN_NONE;
464 static PyMethodDef py_dsdb_methods[] = {
465 { "_samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
466 METH_VARARGS, "Get the server site name as a string"},
467 { "_dsdb_convert_schema_to_openldap",
468 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
469 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
470 "Create an OpenLDAP schema from a schema." },
471 { "_samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid,
472 METH_VARARGS,
473 "samdb_set_domain_sid(samdb, sid)\n"
474 "Set SID of domain to use." },
475 { "_samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid,
476 METH_VARARGS,
477 "samdb_get_domain_sid(samdb)\n"
478 "Get SID of domain in use." },
479 { "_samdb_ntds_invocation_id", (PyCFunction)py_samdb_ntds_invocation_id,
480 METH_VARARGS, "get the NTDS invocation ID GUID as a string"},
481 { "_samdb_set_ntds_settings_dn", (PyCFunction)py_samdb_set_ntds_settings_dn,
482 METH_VARARGS,
483 "samdb_set_ntds_settings_dn(samdb, ntds_settings_dn)\n"
484 "Set NTDS Settings DN for this LDB (allows it to be set before the DB fully exists)." },
485 { "_dsdb_get_oid_from_attid", (PyCFunction)py_dsdb_get_oid_from_attid,
486 METH_VARARGS, NULL },
487 { "_dsdb_set_ntds_invocation_id",
488 (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
489 NULL },
490 { "_samdb_ntds_objectGUID", (PyCFunction)py_samdb_ntds_objectGUID,
491 METH_VARARGS, "get the NTDS objectGUID as a string"},
492 { "_dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema,
493 METH_VARARGS, NULL },
494 { "_dsdb_load_partition_usn", (PyCFunction)py_dsdb_load_partition_usn,
495 METH_VARARGS,
496 "get uSNHighest and uSNUrgent from the partition @REPLCHANGED"},
497 { "_dsdb_set_am_rodc",
498 (PyCFunction)py_dsdb_set_am_rodc, METH_VARARGS,
499 NULL },
500 { "_dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
501 NULL },
502 { "_dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
503 NULL },
504 { "_dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
505 NULL },
506 { NULL }
509 void initdsdb(void)
511 PyObject *m;
513 m = Py_InitModule3("dsdb", py_dsdb_methods,
514 "Python bindings for the directory service databases.");
515 if (m == NULL)
516 return;
518 /* "userAccountControl" flags */
519 PyModule_AddObject(m, "UF_NORMAL_ACCOUNT",
520 PyInt_FromLong(UF_NORMAL_ACCOUNT));
521 PyModule_AddObject(m, "UF_TEMP_DUPLICATE_ACCOUNT",
522 PyInt_FromLong(UF_TEMP_DUPLICATE_ACCOUNT));
523 PyModule_AddObject(m, "UF_SERVER_TRUST_ACCOUNT",
524 PyInt_FromLong(UF_SERVER_TRUST_ACCOUNT));
525 PyModule_AddObject(m, "UF_WORKSTATION_TRUST_ACCOUNT",
526 PyInt_FromLong(UF_WORKSTATION_TRUST_ACCOUNT));
527 PyModule_AddObject(m, "UF_INTERDOMAIN_TRUST_ACCOUNT",
528 PyInt_FromLong(UF_INTERDOMAIN_TRUST_ACCOUNT));
529 PyModule_AddObject(m, "UF_PASSWD_NOTREQD",
530 PyInt_FromLong(UF_PASSWD_NOTREQD));
531 PyModule_AddObject(m, "UF_ACCOUNTDISABLE",
532 PyInt_FromLong(UF_ACCOUNTDISABLE));
534 /* "groupType" flags */
535 PyModule_AddObject(m, "GTYPE_SECURITY_BUILTIN_LOCAL_GROUP",
536 PyInt_FromLong(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP));
537 PyModule_AddObject(m, "GTYPE_SECURITY_GLOBAL_GROUP",
538 PyInt_FromLong(GTYPE_SECURITY_GLOBAL_GROUP));
539 PyModule_AddObject(m, "GTYPE_SECURITY_DOMAIN_LOCAL_GROUP",
540 PyInt_FromLong(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP));
541 PyModule_AddObject(m, "GTYPE_SECURITY_UNIVERSAL_GROUP",
542 PyInt_FromLong(GTYPE_SECURITY_UNIVERSAL_GROUP));
543 PyModule_AddObject(m, "GTYPE_DISTRIBUTION_GLOBAL_GROUP",
544 PyInt_FromLong(GTYPE_DISTRIBUTION_GLOBAL_GROUP));
545 PyModule_AddObject(m, "GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP",
546 PyInt_FromLong(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP));
547 PyModule_AddObject(m, "GTYPE_DISTRIBUTION_UNIVERSAL_GROUP",
548 PyInt_FromLong(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP));
550 /* "sAMAccountType" flags */
551 PyModule_AddObject(m, "ATYPE_NORMAL_ACCOUNT",
552 PyInt_FromLong(ATYPE_NORMAL_ACCOUNT));
553 PyModule_AddObject(m, "ATYPE_WORKSTATION_TRUST",
554 PyInt_FromLong(ATYPE_WORKSTATION_TRUST));
555 PyModule_AddObject(m, "ATYPE_INTERDOMAIN_TRUST",
556 PyInt_FromLong(ATYPE_INTERDOMAIN_TRUST));
557 PyModule_AddObject(m, "ATYPE_SECURITY_GLOBAL_GROUP",
558 PyInt_FromLong(ATYPE_SECURITY_GLOBAL_GROUP));
559 PyModule_AddObject(m, "ATYPE_SECURITY_LOCAL_GROUP",
560 PyInt_FromLong(ATYPE_SECURITY_LOCAL_GROUP));
561 PyModule_AddObject(m, "ATYPE_SECURITY_UNIVERSAL_GROUP",
562 PyInt_FromLong(ATYPE_SECURITY_UNIVERSAL_GROUP));
563 PyModule_AddObject(m, "ATYPE_DISTRIBUTION_GLOBAL_GROUP",
564 PyInt_FromLong(ATYPE_DISTRIBUTION_GLOBAL_GROUP));
565 PyModule_AddObject(m, "ATYPE_DISTRIBUTION_LOCAL_GROUP",
566 PyInt_FromLong(ATYPE_DISTRIBUTION_LOCAL_GROUP));
567 PyModule_AddObject(m, "ATYPE_DISTRIBUTION_UNIVERSAL_GROUP",
568 PyInt_FromLong(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP));
570 /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
571 PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2000",
572 PyInt_FromLong(DS_DOMAIN_FUNCTION_2000));
573 PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003_MIXED",
574 PyInt_FromLong(DS_DOMAIN_FUNCTION_2003_MIXED));
575 PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003",
576 PyInt_FromLong(DS_DOMAIN_FUNCTION_2003));
577 PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008",
578 PyInt_FromLong(DS_DOMAIN_FUNCTION_2008));
579 PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008_R2",
580 PyInt_FromLong(DS_DOMAIN_FUNCTION_2008_R2));