s4:kdc: restore the behavior before the last heimdal import
[Samba/gebeck_regimport.git] / source4 / dsdb / pydsdb.c
blobe74f2bb584bab27efd2a8f3be596e296c0623acf
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 <ldb.h>
23 #include <pyldb.h>
24 #include "dsdb/samdb/samdb.h"
25 #include "libcli/security/security.h"
26 #include "librpc/ndr/libndr.h"
27 #include "system/kerberos.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "librpc/rpc/pyrpc_util.h"
30 #include "lib/policy/policy.h"
32 void initdsdb(void);
34 /* There's no Py_ssize_t in 2.4, apparently */
35 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
36 typedef int Py_ssize_t;
37 typedef inquiry lenfunc;
38 typedef intargfunc ssizeargfunc;
39 #endif
41 /* FIXME: These should be in a header file somewhere, once we finish moving
42 * away from SWIG .. */
43 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
44 /* if (!PyLdb_Check(py_ldb)) { \
45 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
46 return NULL; \
47 } */\
48 ldb = PyLdb_AsLdbContext(py_ldb);
50 static PyObject *py_ldb_get_exception(void)
52 PyObject *mod = PyImport_ImportModule("ldb");
53 if (mod == NULL)
54 return NULL;
56 return PyObject_GetAttrString(mod, "LdbError");
59 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
61 if (ret == LDB_ERR_PYTHON_EXCEPTION)
62 return; /* Python exception should already be set, just keep that */
64 PyErr_SetObject(error,
65 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
66 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
69 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
71 PyObject *py_ldb, *result;
72 struct ldb_context *ldb;
73 const char *site;
74 TALLOC_CTX *mem_ctx;
76 if (!PyArg_ParseTuple(args, "O", &py_ldb))
77 return NULL;
79 PyErr_LDB_OR_RAISE(py_ldb, ldb);
81 mem_ctx = talloc_new(NULL);
82 if (mem_ctx == NULL) {
83 PyErr_NoMemory();
84 return NULL;
87 site = samdb_server_site_name(ldb, mem_ctx);
88 if (site == NULL) {
89 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
90 talloc_free(mem_ctx);
91 return NULL;
94 result = PyString_FromString(site);
95 talloc_free(mem_ctx);
96 return result;
99 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
100 PyObject *args)
102 char *target_str, *mapping;
103 PyObject *py_ldb;
104 struct ldb_context *ldb;
105 PyObject *ret;
106 char *retstr;
108 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
109 return NULL;
111 PyErr_LDB_OR_RAISE(py_ldb, ldb);
113 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
114 if (retstr == NULL) {
115 PyErr_SetString(PyExc_RuntimeError,
116 "dsdb_convert_schema_to_openldap failed");
117 return NULL;
120 ret = PyString_FromString(retstr);
121 talloc_free(retstr);
122 return ret;
125 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
127 PyObject *py_ldb, *py_sid;
128 struct ldb_context *ldb;
129 struct dom_sid *sid;
130 bool ret;
132 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
133 return NULL;
135 PyErr_LDB_OR_RAISE(py_ldb, ldb);
137 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
138 if (sid == NULL) {
139 PyErr_NoMemory();
140 return NULL;
143 ret = samdb_set_domain_sid(ldb, sid);
144 talloc_free(sid);
145 if (!ret) {
146 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
147 return NULL;
149 Py_RETURN_NONE;
152 static PyObject *py_samdb_set_ntds_settings_dn(PyLdbObject *self, PyObject *args)
154 PyObject *py_ldb, *py_ntds_settings_dn;
155 struct ldb_context *ldb;
156 struct ldb_dn *ntds_settings_dn;
157 TALLOC_CTX *tmp_ctx;
158 bool ret;
160 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ntds_settings_dn))
161 return NULL;
163 PyErr_LDB_OR_RAISE(py_ldb, ldb);
165 tmp_ctx = talloc_new(NULL);
166 if (tmp_ctx == NULL) {
167 PyErr_NoMemory();
168 return NULL;
171 if (!PyObject_AsDn(tmp_ctx, py_ntds_settings_dn, ldb, &ntds_settings_dn)) {
172 /* exception thrown by "PyObject_AsDn" */
173 talloc_free(tmp_ctx);
174 return NULL;
177 ret = samdb_set_ntds_settings_dn(ldb, ntds_settings_dn);
178 talloc_free(tmp_ctx);
179 if (!ret) {
180 PyErr_SetString(PyExc_RuntimeError, "set_ntds_settings_dn failed");
181 return NULL;
183 Py_RETURN_NONE;
186 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
188 PyObject *py_ldb;
189 struct ldb_context *ldb;
190 const struct dom_sid *sid;
191 PyObject *ret;
192 char *retstr;
194 if (!PyArg_ParseTuple(args, "O", &py_ldb))
195 return NULL;
197 PyErr_LDB_OR_RAISE(py_ldb, ldb);
199 sid = samdb_domain_sid(ldb);
200 if (!sid) {
201 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
202 return NULL;
205 retstr = dom_sid_string(NULL, sid);
206 if (retstr == NULL) {
207 PyErr_NoMemory();
208 return NULL;
210 ret = PyString_FromString(retstr);
211 talloc_free(retstr);
212 return ret;
215 static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
217 PyObject *py_ldb, *result;
218 struct ldb_context *ldb;
219 const struct GUID *guid;
220 char *retstr;
222 if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
223 return NULL;
226 PyErr_LDB_OR_RAISE(py_ldb, ldb);
228 guid = samdb_ntds_invocation_id(ldb);
229 if (guid == NULL) {
230 PyErr_SetString(PyExc_RuntimeError,
231 "Failed to find NTDS invocation ID");
232 return NULL;
235 retstr = GUID_string(NULL, guid);
236 if (retstr == NULL) {
237 PyErr_NoMemory();
238 return NULL;
240 result = PyString_FromString(retstr);
241 talloc_free(retstr);
242 return result;
245 static PyObject *py_dsdb_get_oid_from_attid(PyObject *self, PyObject *args)
247 PyObject *py_ldb;
248 struct ldb_context *ldb;
249 uint32_t attid;
250 struct dsdb_schema *schema;
251 const char *oid;
252 PyObject *ret;
253 WERROR status;
254 TALLOC_CTX *mem_ctx;
256 if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &attid))
257 return NULL;
259 PyErr_LDB_OR_RAISE(py_ldb, ldb);
261 mem_ctx = talloc_new(NULL);
262 if (!mem_ctx) {
263 PyErr_NoMemory();
264 return NULL;
267 schema = dsdb_get_schema(ldb, mem_ctx);
268 if (!schema) {
269 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb \n");
270 talloc_free(mem_ctx);
271 return NULL;
274 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attid,
275 mem_ctx, &oid);
276 if (!W_ERROR_IS_OK(status)) {
277 PyErr_SetWERROR(status);
278 talloc_free(mem_ctx);
279 return NULL;
282 ret = PyString_FromString(oid);
284 talloc_free(mem_ctx);
286 return ret;
290 static PyObject *py_dsdb_get_attid_from_lDAPDisplayName(PyObject *self, PyObject *args)
292 PyObject *py_ldb, *is_schema_nc;
293 struct ldb_context *ldb;
294 struct dsdb_schema *schema;
295 const char *ldap_display_name;
296 bool schema_nc = false;
297 const struct dsdb_attribute *a;
298 uint32_t attid;
300 if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &is_schema_nc))
301 return NULL;
303 PyErr_LDB_OR_RAISE(py_ldb, ldb);
305 if (is_schema_nc) {
306 if (!PyBool_Check(is_schema_nc)) {
307 PyErr_SetString(PyExc_TypeError, "Expected boolean is_schema_nc");
308 return NULL;
310 if (is_schema_nc == Py_True) {
311 schema_nc = true;
315 schema = dsdb_get_schema(ldb, NULL);
317 if (!schema) {
318 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
319 return NULL;
322 a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
323 if (a == NULL) {
324 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
325 return NULL;
328 attid = dsdb_attribute_get_attid(a, schema_nc);
330 return PyLong_FromUnsignedLong(attid);
334 return the systemFlags as int from the attribute name
336 static PyObject *py_dsdb_get_systemFlags_from_lDAPDisplayName(PyObject *self, PyObject *args)
338 PyObject *py_ldb;
339 struct ldb_context *ldb;
340 struct dsdb_schema *schema;
341 const char *ldap_display_name;
342 const struct dsdb_attribute *attribute;
344 if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
345 return NULL;
347 PyErr_LDB_OR_RAISE(py_ldb, ldb);
349 schema = dsdb_get_schema(ldb, NULL);
351 if (!schema) {
352 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
353 return NULL;
356 attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
357 if (attribute == NULL) {
358 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
359 return NULL;
362 return PyInt_FromLong(attribute->systemFlags);
366 return the linkID from the attribute name
368 static PyObject *py_dsdb_get_linkId_from_lDAPDisplayName(PyObject *self, PyObject *args)
370 PyObject *py_ldb;
371 struct ldb_context *ldb;
372 struct dsdb_schema *schema;
373 const char *ldap_display_name;
374 const struct dsdb_attribute *attribute;
376 if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
377 return NULL;
379 PyErr_LDB_OR_RAISE(py_ldb, ldb);
381 schema = dsdb_get_schema(ldb, NULL);
383 if (!schema) {
384 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
385 return NULL;
388 attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
389 if (attribute == NULL) {
390 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
391 return NULL;
394 return PyInt_FromLong(attribute->linkID);
398 return the backlink attribute name (if any) for an attribute
400 static PyObject *py_dsdb_get_backlink_from_lDAPDisplayName(PyObject *self, PyObject *args)
402 PyObject *py_ldb;
403 struct ldb_context *ldb;
404 struct dsdb_schema *schema;
405 const char *ldap_display_name;
406 const struct dsdb_attribute *attribute, *target_attr;
408 if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
409 return NULL;
411 PyErr_LDB_OR_RAISE(py_ldb, ldb);
413 schema = dsdb_get_schema(ldb, NULL);
415 if (!schema) {
416 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
417 return NULL;
420 attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
421 if (attribute == NULL) {
422 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
423 return NULL;
426 if (attribute->linkID == 0) {
427 Py_RETURN_NONE;
430 target_attr = dsdb_attribute_by_linkID(schema, attribute->linkID ^ 1);
431 if (target_attr == NULL) {
432 /* when we add pseudo-backlinks we'll need to handle
433 them here */
434 Py_RETURN_NONE;
437 return PyString_FromString(target_attr->lDAPDisplayName);
441 static PyObject *py_dsdb_get_lDAPDisplayName_by_attid(PyObject *self, PyObject *args)
443 PyObject *py_ldb;
444 struct ldb_context *ldb;
445 struct dsdb_schema *schema;
446 const struct dsdb_attribute *a;
447 uint32_t attid;
449 if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &attid))
450 return NULL;
452 PyErr_LDB_OR_RAISE(py_ldb, ldb);
454 schema = dsdb_get_schema(ldb, NULL);
456 if (!schema) {
457 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
458 return NULL;
461 a = dsdb_attribute_by_attributeID_id(schema, attid);
462 if (a == NULL) {
463 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '0x%08x'", attid);
464 return NULL;
467 return PyString_FromString(a->lDAPDisplayName);
472 return the attribute syntax oid as a string from the attribute name
474 static PyObject *py_dsdb_get_syntax_oid_from_lDAPDisplayName(PyObject *self, PyObject *args)
476 PyObject *py_ldb;
477 struct ldb_context *ldb;
478 struct dsdb_schema *schema;
479 const char *ldap_display_name;
480 const struct dsdb_attribute *attribute;
482 if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
483 return NULL;
485 PyErr_LDB_OR_RAISE(py_ldb, ldb);
487 schema = dsdb_get_schema(ldb, NULL);
489 if (!schema) {
490 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
491 return NULL;
494 attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
495 if (attribute == NULL) {
496 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
497 return NULL;
500 return PyString_FromString(attribute->syntax->ldap_oid);
504 convert a python string to a DRSUAPI drsuapi_DsReplicaAttribute attribute
506 static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
508 PyObject *py_ldb, *el_list, *ret;
509 struct ldb_context *ldb;
510 char *ldap_display_name;
511 const struct dsdb_attribute *a;
512 struct dsdb_schema *schema;
513 struct dsdb_syntax_ctx syntax_ctx;
514 struct ldb_message_element *el;
515 struct drsuapi_DsReplicaAttribute *attr;
516 TALLOC_CTX *tmp_ctx;
517 WERROR werr;
518 Py_ssize_t i;
520 if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
521 return NULL;
524 PyErr_LDB_OR_RAISE(py_ldb, ldb);
526 if (!PyList_Check(el_list)) {
527 PyErr_Format(PyExc_TypeError, "ldif_elements must be a list");
528 return NULL;
531 schema = dsdb_get_schema(ldb, NULL);
532 if (!schema) {
533 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
534 return NULL;
537 a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
538 if (a == NULL) {
539 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
540 return NULL;
543 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
544 syntax_ctx.is_schema_nc = false;
546 tmp_ctx = talloc_new(ldb);
547 if (tmp_ctx == NULL) {
548 PyErr_NoMemory();
549 return NULL;
552 el = talloc_zero(tmp_ctx, struct ldb_message_element);
553 if (el == NULL) {
554 PyErr_NoMemory();
555 talloc_free(tmp_ctx);
556 return NULL;
559 el->name = ldap_display_name;
560 el->num_values = PyList_Size(el_list);
562 el->values = talloc_array(el, struct ldb_val, el->num_values);
563 if (el->values == NULL) {
564 PyErr_NoMemory();
565 talloc_free(tmp_ctx);
566 return NULL;
569 for (i = 0; i < el->num_values; i++) {
570 PyObject *item = PyList_GetItem(el_list, i);
571 if (!PyString_Check(item)) {
572 PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
573 return NULL;
575 el->values[i].data = (uint8_t *)PyString_AsString(item);
576 el->values[i].length = PyString_Size(item);
579 attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
580 if (attr == NULL) {
581 PyErr_NoMemory();
582 talloc_free(tmp_ctx);
583 return NULL;
586 werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
587 PyErr_WERROR_IS_ERR_RAISE(werr);
589 ret = py_return_ndr_struct("samba.dcerpc.drsuapi", "DsReplicaAttribute", attr, attr);
591 talloc_free(tmp_ctx);
593 return ret;
598 normalise a ldb attribute list
600 static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
602 PyObject *py_ldb, *el_list, *ret;
603 struct ldb_context *ldb;
604 char *ldap_display_name;
605 const struct dsdb_attribute *a;
606 struct dsdb_schema *schema;
607 struct dsdb_syntax_ctx syntax_ctx;
608 struct ldb_message_element *el;
609 struct drsuapi_DsReplicaAttribute *attr;
610 TALLOC_CTX *tmp_ctx;
611 WERROR werr;
612 Py_ssize_t i;
614 if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
615 return NULL;
618 PyErr_LDB_OR_RAISE(py_ldb, ldb);
620 if (!PyList_Check(el_list)) {
621 PyErr_Format(PyExc_TypeError, "ldif_elements must be a list");
622 return NULL;
625 schema = dsdb_get_schema(ldb, NULL);
626 if (!schema) {
627 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
628 return NULL;
631 a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
632 if (a == NULL) {
633 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
634 return NULL;
637 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
638 syntax_ctx.is_schema_nc = false;
640 tmp_ctx = talloc_new(ldb);
641 if (tmp_ctx == NULL) {
642 PyErr_NoMemory();
643 return NULL;
646 el = talloc_zero(tmp_ctx, struct ldb_message_element);
647 if (el == NULL) {
648 PyErr_NoMemory();
649 talloc_free(tmp_ctx);
650 return NULL;
653 el->name = ldap_display_name;
654 el->num_values = PyList_Size(el_list);
656 el->values = talloc_array(el, struct ldb_val, el->num_values);
657 if (el->values == NULL) {
658 PyErr_NoMemory();
659 talloc_free(tmp_ctx);
660 return NULL;
663 for (i = 0; i < el->num_values; i++) {
664 PyObject *item = PyList_GetItem(el_list, i);
665 if (!PyString_Check(item)) {
666 PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
667 return NULL;
669 el->values[i].data = (uint8_t *)PyString_AsString(item);
670 el->values[i].length = PyString_Size(item);
673 /* first run ldb_to_drsuapi, then convert back again. This has
674 * the effect of normalising the attributes
677 attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
678 if (attr == NULL) {
679 PyErr_NoMemory();
680 talloc_free(tmp_ctx);
681 return NULL;
684 werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
685 PyErr_WERROR_IS_ERR_RAISE(werr);
687 /* now convert back again */
688 werr = a->syntax->drsuapi_to_ldb(&syntax_ctx, a, attr, el, el);
689 PyErr_WERROR_IS_ERR_RAISE(werr);
691 ret = py_return_ndr_struct("ldb", "MessageElement", el, el);
693 talloc_free(tmp_ctx);
695 return ret;
699 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
701 PyObject *py_ldb, *py_guid;
702 bool ret;
703 struct GUID guid;
704 struct ldb_context *ldb;
705 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
706 return NULL;
708 PyErr_LDB_OR_RAISE(py_ldb, ldb);
709 GUID_from_string(PyString_AsString(py_guid), &guid);
711 ret = samdb_set_ntds_invocation_id(ldb, &guid);
712 if (!ret) {
713 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
714 return NULL;
716 Py_RETURN_NONE;
719 static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
721 PyObject *py_ldb, *result;
722 struct ldb_context *ldb;
723 const struct GUID *guid;
724 char *retstr;
726 if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
727 return NULL;
730 PyErr_LDB_OR_RAISE(py_ldb, ldb);
732 guid = samdb_ntds_objectGUID(ldb);
733 if (guid == NULL) {
734 PyErr_SetString(PyExc_RuntimeError, "Failed to find NTDS GUID");
735 return NULL;
738 retstr = GUID_string(NULL, guid);
739 if (retstr == NULL) {
740 PyErr_NoMemory();
741 return NULL;
743 result = PyString_FromString(retstr);
744 talloc_free(retstr);
745 return result;
748 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
750 PyObject *py_ldb;
751 struct ldb_context *ldb;
752 int ret;
753 if (!PyArg_ParseTuple(args, "O", &py_ldb))
754 return NULL;
756 PyErr_LDB_OR_RAISE(py_ldb, ldb);
758 ret = dsdb_set_global_schema(ldb);
759 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
761 Py_RETURN_NONE;
764 static PyObject *py_dsdb_load_partition_usn(PyObject *self, PyObject *args)
766 PyObject *py_dn, *py_ldb, *result;
767 struct ldb_dn *dn;
768 uint64_t highest_uSN, urgent_uSN;
769 struct ldb_context *ldb;
770 TALLOC_CTX *mem_ctx;
771 int ret;
773 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dn)) {
774 return NULL;
777 PyErr_LDB_OR_RAISE(py_ldb, ldb);
779 mem_ctx = talloc_new(NULL);
780 if (mem_ctx == NULL) {
781 PyErr_NoMemory();
782 return NULL;
785 if (!PyObject_AsDn(mem_ctx, py_dn, ldb, &dn)) {
786 talloc_free(mem_ctx);
787 return NULL;
790 ret = dsdb_load_partition_usn(ldb, dn, &highest_uSN, &urgent_uSN);
791 if (ret != LDB_SUCCESS) {
792 PyErr_Format(PyExc_RuntimeError,
793 "Failed to load partition [%s] uSN - %s",
794 ldb_dn_get_linearized(dn),
795 ldb_errstring(ldb));
796 talloc_free(mem_ctx);
797 return NULL;
800 talloc_free(mem_ctx);
802 result = PyDict_New();
804 PyDict_SetItemString(result, "uSNHighest", PyInt_FromLong((uint64_t)highest_uSN));
805 PyDict_SetItemString(result, "uSNUrgent", PyInt_FromLong((uint64_t)urgent_uSN));
808 return result;
811 static PyObject *py_dsdb_set_am_rodc(PyObject *self, PyObject *args)
813 PyObject *py_ldb;
814 bool ret;
815 struct ldb_context *ldb;
816 int py_val;
818 if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &py_val))
819 return NULL;
821 PyErr_LDB_OR_RAISE(py_ldb, ldb);
822 ret = samdb_set_am_rodc(ldb, (bool)py_val);
823 if (!ret) {
824 PyErr_SetString(PyExc_RuntimeError, "set_am_rodc failed");
825 return NULL;
827 Py_RETURN_NONE;
830 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
832 WERROR result;
833 char *pf, *df;
834 PyObject *py_ldb;
835 struct ldb_context *ldb;
837 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
838 return NULL;
840 PyErr_LDB_OR_RAISE(py_ldb, ldb);
842 result = dsdb_set_schema_from_ldif(ldb, pf, df);
843 PyErr_WERROR_IS_ERR_RAISE(result);
845 Py_RETURN_NONE;
848 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
850 PyObject *py_ldb;
851 struct ldb_context *ldb;
852 PyObject *py_from_ldb;
853 struct ldb_context *from_ldb;
854 struct dsdb_schema *schema;
855 int ret;
856 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_from_ldb))
857 return NULL;
859 PyErr_LDB_OR_RAISE(py_ldb, ldb);
861 PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
863 schema = dsdb_get_schema(from_ldb, NULL);
864 if (!schema) {
865 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
866 return NULL;
869 ret = dsdb_reference_schema(ldb, schema, true);
870 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
872 Py_RETURN_NONE;
875 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
877 PyObject *py_ldb;
878 struct ldb_context *ldb;
879 WERROR result;
880 struct dsdb_schema *schema;
882 if (!PyArg_ParseTuple(args, "O", &py_ldb))
883 return NULL;
885 PyErr_LDB_OR_RAISE(py_ldb, ldb);
887 schema = dsdb_get_schema(ldb, NULL);
888 if (!schema) {
889 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
890 return NULL;
893 result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
894 PyErr_WERROR_IS_ERR_RAISE(result);
896 Py_RETURN_NONE;
900 static PyObject *py_dsdb_get_partitions_dn(PyObject *self, PyObject *args)
902 struct ldb_context *ldb;
903 struct ldb_dn *dn;
904 PyObject *py_ldb, *ret;
905 PyObject *mod;
907 mod = PyImport_ImportModule("ldb");
909 if (!PyArg_ParseTuple(args, "O", &py_ldb))
910 return NULL;
912 PyErr_LDB_OR_RAISE(py_ldb, ldb);
914 dn = samdb_partitions_dn(ldb, NULL);
915 if (dn == NULL) {
916 PyErr_NoMemory();
917 return NULL;
919 ret = PyLdbDn_FromDn(dn);
920 talloc_free(dn);
921 return ret;
926 call into samdb_rodc()
928 static PyObject *py_dsdb_am_rodc(PyObject *self, PyObject *args)
930 PyObject *py_ldb;
931 struct ldb_context *ldb;
932 int ret;
933 bool am_rodc;
935 if (!PyArg_ParseTuple(args, "O", &py_ldb))
936 return NULL;
938 PyErr_LDB_OR_RAISE(py_ldb, ldb);
940 ret = samdb_rodc(ldb, &am_rodc);
941 if (ret != LDB_SUCCESS) {
942 PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
943 return NULL;
946 return PyBool_FromLong(am_rodc);
950 static PyMethodDef py_dsdb_methods[] = {
951 { "_samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
952 METH_VARARGS, "Get the server site name as a string"},
953 { "_dsdb_convert_schema_to_openldap",
954 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
955 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
956 "Create an OpenLDAP schema from a schema." },
957 { "_samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid,
958 METH_VARARGS,
959 "samdb_set_domain_sid(samdb, sid)\n"
960 "Set SID of domain to use." },
961 { "_samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid,
962 METH_VARARGS,
963 "samdb_get_domain_sid(samdb)\n"
964 "Get SID of domain in use." },
965 { "_samdb_ntds_invocation_id", (PyCFunction)py_samdb_ntds_invocation_id,
966 METH_VARARGS, "get the NTDS invocation ID GUID as a string"},
967 { "_samdb_set_ntds_settings_dn", (PyCFunction)py_samdb_set_ntds_settings_dn,
968 METH_VARARGS,
969 "samdb_set_ntds_settings_dn(samdb, ntds_settings_dn)\n"
970 "Set NTDS Settings DN for this LDB (allows it to be set before the DB fully exists)." },
971 { "_dsdb_get_oid_from_attid", (PyCFunction)py_dsdb_get_oid_from_attid,
972 METH_VARARGS, NULL },
973 { "_dsdb_get_attid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_attid_from_lDAPDisplayName,
974 METH_VARARGS, NULL },
975 { "_dsdb_get_syntax_oid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_syntax_oid_from_lDAPDisplayName,
976 METH_VARARGS, NULL },
977 { "_dsdb_get_systemFlags_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_systemFlags_from_lDAPDisplayName,
978 METH_VARARGS, NULL },
979 { "_dsdb_get_linkId_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_linkId_from_lDAPDisplayName,
980 METH_VARARGS, NULL },
981 { "_dsdb_get_lDAPDisplayName_by_attid", (PyCFunction)py_dsdb_get_lDAPDisplayName_by_attid,
982 METH_VARARGS, NULL },
983 { "_dsdb_get_backlink_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_backlink_from_lDAPDisplayName,
984 METH_VARARGS, NULL },
985 { "_dsdb_set_ntds_invocation_id",
986 (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
987 NULL },
988 { "_samdb_ntds_objectGUID", (PyCFunction)py_samdb_ntds_objectGUID,
989 METH_VARARGS, "get the NTDS objectGUID as a string"},
990 { "_dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema,
991 METH_VARARGS, NULL },
992 { "_dsdb_load_partition_usn", (PyCFunction)py_dsdb_load_partition_usn,
993 METH_VARARGS,
994 "get uSNHighest and uSNUrgent from the partition @REPLCHANGED"},
995 { "_dsdb_set_am_rodc",
996 (PyCFunction)py_dsdb_set_am_rodc, METH_VARARGS,
997 NULL },
998 { "_am_rodc",
999 (PyCFunction)py_dsdb_am_rodc, METH_VARARGS,
1000 NULL },
1001 { "_dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
1002 NULL },
1003 { "_dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
1004 NULL },
1005 { "_dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
1006 NULL },
1007 { "_dsdb_get_partitions_dn", (PyCFunction)py_dsdb_get_partitions_dn, METH_VARARGS, NULL },
1008 { "_dsdb_DsReplicaAttribute", (PyCFunction)py_dsdb_DsReplicaAttribute, METH_VARARGS, NULL },
1009 { "_dsdb_normalise_attributes", (PyCFunction)py_dsdb_normalise_attributes, METH_VARARGS, NULL },
1010 { NULL }
1013 void initdsdb(void)
1015 PyObject *m;
1017 m = Py_InitModule3("dsdb", py_dsdb_methods,
1018 "Python bindings for the directory service databases.");
1019 if (m == NULL)
1020 return;
1022 #define ADD_DSDB_FLAG(val) PyModule_AddObject(m, #val, PyInt_FromLong(val))
1024 /* "userAccountControl" flags */
1025 ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1026 ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1027 ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1028 ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1029 ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1030 ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1031 ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1033 ADD_DSDB_FLAG(UF_SCRIPT);
1034 ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1035 ADD_DSDB_FLAG(UF_00000004);
1036 ADD_DSDB_FLAG(UF_HOMEDIR_REQUIRED);
1037 ADD_DSDB_FLAG(UF_LOCKOUT);
1038 ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1039 ADD_DSDB_FLAG(UF_PASSWD_CANT_CHANGE);
1040 ADD_DSDB_FLAG(UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED);
1041 ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1042 ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1043 ADD_DSDB_FLAG(UF_00000400);
1044 ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1045 ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1046 ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1047 ADD_DSDB_FLAG(UF_00004000);
1048 ADD_DSDB_FLAG(UF_00008000);
1049 ADD_DSDB_FLAG(UF_DONT_EXPIRE_PASSWD);
1050 ADD_DSDB_FLAG(UF_MNS_LOGON_ACCOUNT);
1051 ADD_DSDB_FLAG(UF_SMARTCARD_REQUIRED);
1052 ADD_DSDB_FLAG(UF_TRUSTED_FOR_DELEGATION);
1053 ADD_DSDB_FLAG(UF_NOT_DELEGATED);
1054 ADD_DSDB_FLAG(UF_USE_DES_KEY_ONLY);
1055 ADD_DSDB_FLAG(UF_DONT_REQUIRE_PREAUTH);
1056 ADD_DSDB_FLAG(UF_PASSWORD_EXPIRED);
1057 ADD_DSDB_FLAG(UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION);
1058 ADD_DSDB_FLAG(UF_NO_AUTH_DATA_REQUIRED);
1059 ADD_DSDB_FLAG(UF_PARTIAL_SECRETS_ACCOUNT);
1061 /* groupType flags */
1062 ADD_DSDB_FLAG(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP);
1063 ADD_DSDB_FLAG(GTYPE_SECURITY_GLOBAL_GROUP);
1064 ADD_DSDB_FLAG(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
1065 ADD_DSDB_FLAG(GTYPE_SECURITY_UNIVERSAL_GROUP);
1066 ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_GLOBAL_GROUP);
1067 ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP);
1068 ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1070 /* "sAMAccountType" flags */
1071 ADD_DSDB_FLAG(ATYPE_NORMAL_ACCOUNT);
1072 ADD_DSDB_FLAG(ATYPE_WORKSTATION_TRUST);
1073 ADD_DSDB_FLAG(ATYPE_INTERDOMAIN_TRUST);
1074 ADD_DSDB_FLAG(ATYPE_SECURITY_GLOBAL_GROUP);
1075 ADD_DSDB_FLAG(ATYPE_SECURITY_LOCAL_GROUP);
1076 ADD_DSDB_FLAG(ATYPE_SECURITY_UNIVERSAL_GROUP);
1077 ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_GLOBAL_GROUP);
1078 ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_LOCAL_GROUP);
1079 ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1081 /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
1082 ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2000);
1083 ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003_MIXED);
1084 ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003);
1085 ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008);
1086 ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008_R2);
1088 /* "systemFlags" */
1089 ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NC);
1090 ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_DOMAIN);
1091 ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NOT_GC_REPLICATED);
1092 ADD_DSDB_FLAG(SYSTEM_FLAG_SCHEMA_BASE_OBJECT);
1093 ADD_DSDB_FLAG(SYSTEM_FLAG_ATTR_IS_RDN);
1094 ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
1095 ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE);
1096 ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME);
1097 ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
1098 ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_MOVE);
1099 ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
1100 ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_DELETE);
1102 /* Kerberos encryption type constants */
1103 ADD_DSDB_FLAG(ENC_ALL_TYPES);
1104 ADD_DSDB_FLAG(ENC_CRC32);
1105 ADD_DSDB_FLAG(ENC_RSA_MD5);
1106 ADD_DSDB_FLAG(ENC_RC4_HMAC_MD5);
1107 ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES128);
1108 ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES256);
1110 ADD_DSDB_FLAG(SEARCH_FLAG_ATTINDEX);
1111 ADD_DSDB_FLAG(SEARCH_FLAG_PDNTATTINDEX);
1112 ADD_DSDB_FLAG(SEARCH_FLAG_ANR);
1113 ADD_DSDB_FLAG(SEARCH_FLAG_PRESERVEONDELETE);
1114 ADD_DSDB_FLAG(SEARCH_FLAG_COPY);
1115 ADD_DSDB_FLAG(SEARCH_FLAG_TUPLEINDEX);
1116 ADD_DSDB_FLAG(SEARCH_FLAG_SUBTREEATTRINDEX);
1117 ADD_DSDB_FLAG(SEARCH_FLAG_CONFIDENTIAL);
1118 ADD_DSDB_FLAG(SEARCH_FLAG_NEVERVALUEAUDIT);
1119 ADD_DSDB_FLAG(SEARCH_FLAG_RODC_ATTRIBUTE);
1121 ADD_DSDB_FLAG(DS_FLAG_ATTR_NOT_REPLICATED);
1122 ADD_DSDB_FLAG(DS_FLAG_ATTR_REQ_PARTIAL_SET_MEMBER);
1123 ADD_DSDB_FLAG(DS_FLAG_ATTR_IS_CONSTRUCTED);
1125 ADD_DSDB_FLAG(DS_NTDSDSA_OPT_IS_GC);
1126 ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL);
1127 ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
1128 ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_NTDSCONN_XLATE);
1129 ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_SPN_REGISTRATION);
1131 ADD_DSDB_FLAG(NTDSCONN_KCC_GC_TOPOLOGY);
1132 ADD_DSDB_FLAG(NTDSCONN_KCC_RING_TOPOLOGY);
1133 ADD_DSDB_FLAG(NTDSCONN_KCC_MINIMIZE_HOPS_TOPOLOGY);
1134 ADD_DSDB_FLAG(NTDSCONN_KCC_STALE_SERVERS_TOPOLOGY);
1135 ADD_DSDB_FLAG(NTDSCONN_KCC_OSCILLATING_CONNECTION_TOPOLOGY);
1136 ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_GC_TOPOLOGY);
1137 ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_TOPOLOGY);
1138 ADD_DSDB_FLAG(NTDSCONN_KCC_SERVER_FAILOVER_TOPOLOGY);
1139 ADD_DSDB_FLAG(NTDSCONN_KCC_SITE_FAILOVER_TOPOLOGY);
1140 ADD_DSDB_FLAG(NTDSCONN_KCC_REDUNDANT_SERVER_TOPOLOGY);
1142 /* GPO policy flags */
1143 ADD_DSDB_FLAG(GPLINK_OPT_DISABLE);
1144 ADD_DSDB_FLAG(GPLINK_OPT_ENFORCE);
1145 ADD_DSDB_FLAG(GPO_FLAG_USER_DISABLE);
1146 ADD_DSDB_FLAG(GPO_FLAG_MACHINE_DISABLE);
1147 ADD_DSDB_FLAG(GPO_INHERIT);
1148 ADD_DSDB_FLAG(GPO_BLOCK_INHERITANCE);
1150 #define ADD_DSDB_STRING(val) PyModule_AddObject(m, #val, PyString_FromString(val))
1152 ADD_DSDB_STRING(DSDB_SYNTAX_BINARY_DN);
1153 ADD_DSDB_STRING(DSDB_SYNTAX_STRING_DN);
1154 ADD_DSDB_STRING(DSDB_SYNTAX_OR_NAME);