dbchecker: Fixing up incorrect DNs wasn't working
[samba.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 "python/py3compat.h"
22 #include "includes.h"
23 #include <ldb.h>
24 #include <pyldb.h>
25 #include "dsdb/samdb/samdb.h"
26 #include "libcli/security/security.h"
27 #include "librpc/ndr/libndr.h"
28 #include "system/kerberos.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "librpc/rpc/pyrpc_util.h"
31 #include "lib/policy/policy.h"
32 #include "param/pyparam.h"
33 #include "lib/util/dlinklist.h"
34 #include "dsdb/kcc/garbage_collect_tombstones.h"
35
36
37 /* FIXME: These should be in a header file somewhere */
38 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
39         if (!py_check_dcerpc_type(py_ldb, "ldb", "Ldb")) { \
40                 PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
41                 return NULL; \
42         } \
43         ldb = pyldb_Ldb_AsLdbContext(py_ldb);
44
45 #define PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn) \
46         if (!py_check_dcerpc_type(py_ldb_dn, "ldb", "Dn")) { \
47                 PyErr_SetString(PyExc_TypeError, "ldb Dn object required"); \
48                 return NULL; \
49         } \
50         dn = pyldb_Dn_AsDn(py_ldb_dn);
51
52 static PyObject *py_ldb_get_exception(void)
53 {
54         PyObject *mod = PyImport_ImportModule("ldb");
55         if (mod == NULL)
56                 return NULL;
57
58         return PyObject_GetAttrString(mod, "LdbError");
59 }
60
61 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
62 {
63         if (ret == LDB_ERR_PYTHON_EXCEPTION)
64                 return; /* Python exception should already be set, just keep that */
65
66         PyErr_SetObject(error, 
67                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
68                         ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
69 }
70
71 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
72 {
73         PyObject *py_ldb, *result;
74         struct ldb_context *ldb;
75         const char *site;
76         TALLOC_CTX *mem_ctx;
77
78         if (!PyArg_ParseTuple(args, "O", &py_ldb))
79                 return NULL;
80
81         PyErr_LDB_OR_RAISE(py_ldb, ldb);
82
83         mem_ctx = talloc_new(NULL);
84         if (mem_ctx == NULL) {
85                 PyErr_NoMemory();
86                 return NULL;
87         }
88
89         site = samdb_server_site_name(ldb, mem_ctx);
90         if (site == NULL) {
91                 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
92                 talloc_free(mem_ctx);
93                 return NULL;
94         }
95
96         result = PyStr_FromString(site);
97         talloc_free(mem_ctx);
98         return result;
99 }
100
101 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
102                                                     PyObject *args)
103 {
104         char *target_str, *mapping;
105         PyObject *py_ldb;
106         struct ldb_context *ldb;
107         PyObject *ret;
108         char *retstr;
109
110         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
111                 return NULL;
112
113         PyErr_LDB_OR_RAISE(py_ldb, ldb);
114
115         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
116         if (retstr == NULL) {
117                 PyErr_SetString(PyExc_RuntimeError,
118                                                 "dsdb_convert_schema_to_openldap failed");
119                 return NULL;
120         } 
121
122         ret = PyStr_FromString(retstr);
123         talloc_free(retstr);
124         return ret;
125 }
126
127 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
128
129         PyObject *py_ldb, *py_sid;
130         struct ldb_context *ldb;
131         struct dom_sid *sid;
132         bool ret;
133
134         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
135                 return NULL;
136         
137         PyErr_LDB_OR_RAISE(py_ldb, ldb);
138
139         sid = dom_sid_parse_talloc(NULL, PyStr_AsString(py_sid));
140         if (sid == NULL) {
141                 PyErr_NoMemory();
142                 return NULL;
143         }
144
145         ret = samdb_set_domain_sid(ldb, sid);
146         talloc_free(sid);
147         if (!ret) {
148                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
149                 return NULL;
150         } 
151         Py_RETURN_NONE;
152 }
153
154 static PyObject *py_samdb_set_ntds_settings_dn(PyLdbObject *self, PyObject *args)
155
156         PyObject *py_ldb, *py_ntds_settings_dn;
157         struct ldb_context *ldb;
158         struct ldb_dn *ntds_settings_dn;
159         TALLOC_CTX *tmp_ctx;
160         bool ret;
161
162         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ntds_settings_dn))
163                 return NULL;
164
165         PyErr_LDB_OR_RAISE(py_ldb, ldb);
166
167         tmp_ctx = talloc_new(NULL);
168         if (tmp_ctx == NULL) {
169                 PyErr_NoMemory();
170                 return NULL;
171         }
172
173         if (!pyldb_Object_AsDn(tmp_ctx, py_ntds_settings_dn, ldb, &ntds_settings_dn)) {
174                 /* exception thrown by "pyldb_Object_AsDn" */
175                 talloc_free(tmp_ctx);
176                 return NULL;
177         }
178
179         ret = samdb_set_ntds_settings_dn(ldb, ntds_settings_dn);
180         talloc_free(tmp_ctx);
181         if (!ret) {
182                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_settings_dn failed");
183                 return NULL;
184         } 
185         Py_RETURN_NONE;
186 }
187
188 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
189
190         PyObject *py_ldb;
191         struct ldb_context *ldb;
192         const struct dom_sid *sid;
193         PyObject *ret;
194         char *retstr;
195
196         if (!PyArg_ParseTuple(args, "O", &py_ldb))
197                 return NULL;
198
199         PyErr_LDB_OR_RAISE(py_ldb, ldb);
200
201         sid = samdb_domain_sid(ldb);
202         if (!sid) {
203                 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
204                 return NULL;
205         }
206
207         retstr = dom_sid_string(NULL, sid);
208         if (retstr == NULL) {
209                 PyErr_NoMemory();
210                 return NULL;
211         }
212         ret = PyStr_FromString(retstr);
213         talloc_free(retstr);
214         return ret;
215 }
216
217 static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
218 {
219         PyObject *py_ldb, *result;
220         struct ldb_context *ldb;
221         const struct GUID *guid;
222         char *retstr;
223
224         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
225                 return NULL;
226         }
227
228         PyErr_LDB_OR_RAISE(py_ldb, ldb);
229
230         guid = samdb_ntds_invocation_id(ldb);
231         if (guid == NULL) {
232                 PyErr_SetString(PyExc_RuntimeError,
233                                                 "Failed to find NTDS invocation ID");
234                 return NULL;
235         }
236
237         retstr = GUID_string(NULL, guid);
238         if (retstr == NULL) {
239                 PyErr_NoMemory();
240                 return NULL;
241         }
242         result = PyStr_FromString(retstr);
243         talloc_free(retstr);
244         return result;
245 }
246
247 static PyObject *py_dsdb_get_oid_from_attid(PyObject *self, PyObject *args)
248 {
249         PyObject *py_ldb;
250         struct ldb_context *ldb;
251         uint32_t attid;
252         struct dsdb_schema *schema;
253         const char *oid;
254         PyObject *ret;
255         WERROR status;
256         TALLOC_CTX *mem_ctx;
257
258         if (!PyArg_ParseTuple(args, "OI", &py_ldb, &attid))
259                 return NULL;
260
261         PyErr_LDB_OR_RAISE(py_ldb, ldb);
262
263         mem_ctx = talloc_new(NULL);
264         if (!mem_ctx) {
265                 PyErr_NoMemory();
266                 return NULL;
267         }
268
269         schema = dsdb_get_schema(ldb, mem_ctx);
270         if (!schema) {
271                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb \n");
272                 talloc_free(mem_ctx);
273                 return NULL;
274         }
275         
276         status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attid,
277                                                 mem_ctx, &oid);
278         if (!W_ERROR_IS_OK(status)) {
279                 PyErr_SetWERROR(status);
280                 talloc_free(mem_ctx);
281                 return NULL;
282         }
283
284         ret = PyStr_FromString(oid);
285
286         talloc_free(mem_ctx);
287
288         return ret;
289 }
290
291
292 static PyObject *py_dsdb_get_attid_from_lDAPDisplayName(PyObject *self, PyObject *args)
293 {
294         PyObject *py_ldb, *is_schema_nc;
295         struct ldb_context *ldb;
296         struct dsdb_schema *schema;
297         const char *ldap_display_name;
298         bool schema_nc = false;
299         const struct dsdb_attribute *a;
300         uint32_t attid;
301
302         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &is_schema_nc))
303                 return NULL;
304
305         PyErr_LDB_OR_RAISE(py_ldb, ldb);
306
307         if (is_schema_nc) {
308                 if (!PyBool_Check(is_schema_nc)) {
309                         PyErr_SetString(PyExc_TypeError, "Expected boolean is_schema_nc");
310                         return NULL;
311                 }
312                 if (is_schema_nc == Py_True) {
313                         schema_nc = true;
314                 }
315         }
316
317         schema = dsdb_get_schema(ldb, NULL);
318
319         if (!schema) {
320                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
321                 return NULL;
322         }
323
324         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
325         if (a == NULL) {
326                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
327                 return NULL;
328         }
329
330         attid = dsdb_attribute_get_attid(a, schema_nc);
331
332         return PyLong_FromUnsignedLong(attid);
333 }
334
335 /*
336   return the systemFlags as int from the attribute name
337  */
338 static PyObject *py_dsdb_get_systemFlags_from_lDAPDisplayName(PyObject *self, PyObject *args)
339 {
340         PyObject *py_ldb;
341         struct ldb_context *ldb;
342         struct dsdb_schema *schema;
343         const char *ldap_display_name;
344         const struct dsdb_attribute *attribute;
345
346         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
347                 return NULL;
348
349         PyErr_LDB_OR_RAISE(py_ldb, ldb);
350
351         schema = dsdb_get_schema(ldb, NULL);
352
353         if (!schema) {
354                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
355                 return NULL;
356         }
357
358         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
359         if (attribute == NULL) {
360                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
361                 return NULL;
362         }
363
364         return PyInt_FromLong(attribute->systemFlags);
365 }
366
367 /*
368   return the linkID from the attribute name
369  */
370 static PyObject *py_dsdb_get_linkId_from_lDAPDisplayName(PyObject *self, PyObject *args)
371 {
372         PyObject *py_ldb;
373         struct ldb_context *ldb;
374         struct dsdb_schema *schema;
375         const char *ldap_display_name;
376         const struct dsdb_attribute *attribute;
377
378         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
379                 return NULL;
380
381         PyErr_LDB_OR_RAISE(py_ldb, ldb);
382
383         schema = dsdb_get_schema(ldb, NULL);
384
385         if (!schema) {
386                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
387                 return NULL;
388         }
389
390         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
391         if (attribute == NULL) {
392                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
393                 return NULL;
394         }
395
396         return PyInt_FromLong(attribute->linkID);
397 }
398
399 /*
400   return the backlink attribute name (if any) for an attribute
401  */
402 static PyObject *py_dsdb_get_backlink_from_lDAPDisplayName(PyObject *self, PyObject *args)
403 {
404         PyObject *py_ldb;
405         struct ldb_context *ldb;
406         struct dsdb_schema *schema;
407         const char *ldap_display_name;
408         const struct dsdb_attribute *attribute, *target_attr;
409
410         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
411                 return NULL;
412
413         PyErr_LDB_OR_RAISE(py_ldb, ldb);
414
415         schema = dsdb_get_schema(ldb, NULL);
416
417         if (!schema) {
418                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
419                 return NULL;
420         }
421
422         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
423         if (attribute == NULL) {
424                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
425                 return NULL;
426         }
427
428         if (attribute->linkID == 0) {
429                 Py_RETURN_NONE;
430         }
431
432         target_attr = dsdb_attribute_by_linkID(schema, attribute->linkID ^ 1);
433         if (target_attr == NULL) {
434                 /* when we add pseudo-backlinks we'll need to handle
435                    them here */
436                 Py_RETURN_NONE;
437         }
438
439         return PyStr_FromString(target_attr->lDAPDisplayName);
440 }
441
442
443 static PyObject *py_dsdb_get_lDAPDisplayName_by_attid(PyObject *self, PyObject *args)
444 {
445         PyObject *py_ldb;
446         struct ldb_context *ldb;
447         struct dsdb_schema *schema;
448         const struct dsdb_attribute *a;
449         uint32_t attid;
450
451         if (!PyArg_ParseTuple(args, "OI", &py_ldb, &attid))
452                 return NULL;
453
454         PyErr_LDB_OR_RAISE(py_ldb, ldb);
455
456         schema = dsdb_get_schema(ldb, NULL);
457
458         if (!schema) {
459                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
460                 return NULL;
461         }
462
463         a = dsdb_attribute_by_attributeID_id(schema, attid);
464         if (a == NULL) {
465                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '0x%08x'", attid);
466                 return NULL;
467         }
468
469         return PyStr_FromString(a->lDAPDisplayName);
470 }
471
472
473 /*
474   return the attribute syntax oid as a string from the attribute name
475  */
476 static PyObject *py_dsdb_get_syntax_oid_from_lDAPDisplayName(PyObject *self, PyObject *args)
477 {
478         PyObject *py_ldb;
479         struct ldb_context *ldb;
480         struct dsdb_schema *schema;
481         const char *ldap_display_name;
482         const struct dsdb_attribute *attribute;
483
484         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
485                 return NULL;
486
487         PyErr_LDB_OR_RAISE(py_ldb, ldb);
488
489         schema = dsdb_get_schema(ldb, NULL);
490
491         if (!schema) {
492                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
493                 return NULL;
494         }
495
496         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
497         if (attribute == NULL) {
498                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
499                 return NULL;
500         }
501
502         return PyStr_FromString(attribute->syntax->ldap_oid);
503 }
504
505 /*
506   convert a python string to a DRSUAPI drsuapi_DsReplicaAttribute attribute
507  */
508 static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
509 {
510         PyObject *py_ldb, *el_list, *ret;
511         struct ldb_context *ldb;
512         char *ldap_display_name;
513         const struct dsdb_attribute *a;
514         struct dsdb_schema *schema;
515         struct dsdb_syntax_ctx syntax_ctx;
516         struct ldb_message_element *el;
517         struct drsuapi_DsReplicaAttribute *attr;
518         TALLOC_CTX *tmp_ctx;
519         WERROR werr;
520         Py_ssize_t i;
521         Py_ssize_t _size;
522
523         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
524                 return NULL;
525         }
526
527         PyErr_LDB_OR_RAISE(py_ldb, ldb);
528
529         schema = dsdb_get_schema(ldb, NULL);
530         if (!schema) {
531                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
532                 return NULL;
533         }
534
535         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
536         if (a == NULL) {
537                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
538                 return NULL;
539         }
540
541         dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
542         syntax_ctx.is_schema_nc = false;
543
544         tmp_ctx = talloc_new(ldb);
545         if (tmp_ctx == NULL) {
546                 PyErr_NoMemory();
547                 return NULL;
548         }
549
550         /* If we were not given an LdbMessageElement */
551         if (!PyList_Check(el_list)) {
552                 if (!py_check_dcerpc_type(el_list, "ldb", "MessageElement")) {
553                         PyErr_SetString(py_ldb_get_exception(),
554                                         "list of strings or ldb MessageElement object required");
555                         return NULL;
556                 }
557                 /*
558                  * NOTE:
559                  * el may not be a valid talloc context, it
560                  * could be part of an array
561                  */
562                 el = pyldb_MessageElement_AsMessageElement(el_list);
563         } else {
564                 el = talloc_zero(tmp_ctx, struct ldb_message_element);
565                 if (el == NULL) {
566                         PyErr_NoMemory();
567                         talloc_free(tmp_ctx);
568                         return NULL;
569                 }
570
571                 el->name = ldap_display_name;
572                 el->num_values = PyList_Size(el_list);
573
574                 el->values = talloc_array(el, struct ldb_val, el->num_values);
575                 if (el->values == NULL) {
576                         PyErr_NoMemory();
577                         talloc_free(tmp_ctx);
578                         return NULL;
579                 }
580
581                 for (i = 0; i < el->num_values; i++) {
582                         PyObject *item = PyList_GetItem(el_list, i);
583                         if (!(PyStr_Check(item) || PyUnicode_Check(item))) {
584                                 PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
585                                 talloc_free(tmp_ctx);
586                                 return NULL;
587                         }
588                         el->values[i].data = (uint8_t *)PyStr_AsUTF8AndSize(item, &_size);
589                         el->values[i].length = _size;
590                 }
591         }
592
593         attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
594         if (attr == NULL) {
595                 PyErr_NoMemory();
596                 talloc_free(tmp_ctx);
597                 return NULL;
598         }
599
600         werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
601         PyErr_WERROR_NOT_OK_RAISE(werr);
602
603         ret = py_return_ndr_struct("samba.dcerpc.drsuapi", "DsReplicaAttribute", attr, attr);
604
605         talloc_free(tmp_ctx);
606
607         return ret;
608 }
609
610
611 /*
612   normalise a ldb attribute list
613  */
614 static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
615 {
616         PyObject *py_ldb, *el_list, *py_ret;
617         struct ldb_context *ldb;
618         char *ldap_display_name;
619         const struct dsdb_attribute *a;
620         struct dsdb_schema *schema;
621         struct dsdb_syntax_ctx syntax_ctx;
622         struct ldb_message_element *el, *new_el;
623         struct drsuapi_DsReplicaAttribute *attr;
624         PyLdbMessageElementObject *ret;
625         TALLOC_CTX *tmp_ctx;
626         WERROR werr;
627         Py_ssize_t i;
628         PyTypeObject *py_type = NULL;
629         PyObject *module = NULL;
630
631         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
632                 return NULL;
633         }
634
635         PyErr_LDB_OR_RAISE(py_ldb, ldb);
636
637         schema = dsdb_get_schema(ldb, NULL);
638         if (!schema) {
639                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
640                 return NULL;
641         }
642
643         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
644         if (a == NULL) {
645                 PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
646                 return NULL;
647         }
648
649         dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
650         syntax_ctx.is_schema_nc = false;
651
652         tmp_ctx = talloc_new(ldb);
653         if (tmp_ctx == NULL) {
654                 PyErr_NoMemory();
655                 return NULL;
656         }
657
658         if (!PyList_Check(el_list)) {
659                 if (!py_check_dcerpc_type(el_list, "ldb", "MessageElement")) {
660                         PyErr_SetString(py_ldb_get_exception(),
661                                         "list of strings or ldb MessageElement object required");
662                         return NULL;
663                 }
664                 /*
665                  * NOTE:
666                  * el may not be a valid talloc context, it
667                  * could be part of an array
668                  */
669                 el = pyldb_MessageElement_AsMessageElement(el_list);
670         } else {
671                 el = talloc_zero(tmp_ctx, struct ldb_message_element);
672                 if (el == NULL) {
673                         PyErr_NoMemory();
674                         talloc_free(tmp_ctx);
675                         return NULL;
676                 }
677
678                 el->name = ldap_display_name;
679                 el->num_values = PyList_Size(el_list);
680
681                 el->values = talloc_array(el, struct ldb_val, el->num_values);
682                 if (el->values == NULL) {
683                         PyErr_NoMemory();
684                         talloc_free(tmp_ctx);
685                         return NULL;
686                 }
687
688                 for (i = 0; i < el->num_values; i++) {
689                         PyObject *item = PyList_GetItem(el_list, i);
690                         if (!PyBytes_Check(item)) {
691                                 PyErr_Format(PyExc_TypeError,
692                                              "ldif_element type should be "
693                                              PY_DESC_PY3_BYTES
694                                              );
695                                 talloc_free(tmp_ctx);
696                                 return NULL;
697                         }
698                         el->values[i].data = (uint8_t *)PyBytes_AsString(item);
699                         el->values[i].length = PyBytes_Size(item);
700                 }
701         }
702
703         new_el = talloc_zero(tmp_ctx, struct ldb_message_element);
704         if (new_el == NULL) {
705                 PyErr_NoMemory();
706                 talloc_free(tmp_ctx);
707                 return NULL;
708         }
709
710         /* Normalise "objectClass" attribute if needed */
711         if (ldb_attr_cmp(a->lDAPDisplayName, "objectClass") == 0) {
712                 int iret;
713                 iret = dsdb_sort_objectClass_attr(ldb, schema, el, new_el, new_el);
714                 if (iret != LDB_SUCCESS) {
715                         PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
716                         talloc_free(tmp_ctx);
717                         return NULL;
718                 }
719         }
720
721         /* first run ldb_to_drsuapi, then convert back again. This has
722          * the effect of normalising the attributes
723          */
724
725         attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
726         if (attr == NULL) {
727                 PyErr_NoMemory();
728                 talloc_free(tmp_ctx);
729                 return NULL;
730         }
731
732         werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
733         PyErr_WERROR_NOT_OK_RAISE(werr);
734
735         /* now convert back again */
736         werr = a->syntax->drsuapi_to_ldb(&syntax_ctx, a, attr, new_el, new_el);
737         PyErr_WERROR_NOT_OK_RAISE(werr);
738
739         module = PyImport_ImportModule("ldb");
740         if (module == NULL) {
741                 return NULL;
742         }
743
744         py_type = (PyTypeObject *)PyObject_GetAttrString(module, "MessageElement");
745         if (py_type == NULL) {
746                 return NULL;
747         }
748         py_ret = py_type->tp_alloc(py_type, 0);
749         ret = (PyLdbMessageElementObject *)py_ret;
750
751         ret->mem_ctx = talloc_new(NULL);
752         if (talloc_reference(ret->mem_ctx, new_el) == NULL) {
753                 PyErr_NoMemory();
754                 return NULL;
755         }
756         ret->el = new_el;
757
758         talloc_free(tmp_ctx);
759
760         return py_ret;
761 }
762
763
764 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
765 {
766         PyObject *py_ldb, *py_guid;
767         bool ret;
768         struct GUID guid;
769         struct ldb_context *ldb;
770         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
771                 return NULL;
772
773         PyErr_LDB_OR_RAISE(py_ldb, ldb);
774         GUID_from_string(PyStr_AsString(py_guid), &guid);
775
776         if (GUID_all_zero(&guid)) {
777                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id rejected due to all-zero invocation ID");
778                 return NULL;
779         }
780
781         ret = samdb_set_ntds_invocation_id(ldb, &guid);
782         if (!ret) {
783                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
784                 return NULL;
785         }
786         Py_RETURN_NONE;
787 }
788
789 static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
790 {
791         PyObject *py_ldb, *result;
792         struct ldb_context *ldb;
793         const struct GUID *guid;
794         char *retstr;
795
796         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
797                 return NULL;
798         }
799
800         PyErr_LDB_OR_RAISE(py_ldb, ldb);
801
802         guid = samdb_ntds_objectGUID(ldb);
803         if (guid == NULL) {
804                 PyErr_SetString(PyExc_RuntimeError, "Failed to find NTDS GUID");
805                 return NULL;
806         }
807
808         retstr = GUID_string(NULL, guid);
809         if (retstr == NULL) {
810                 PyErr_NoMemory();
811                 return NULL;
812         }
813         result = PyStr_FromString(retstr);
814         talloc_free(retstr);
815         return result;
816 }
817
818 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
819 {
820         PyObject *py_ldb;
821         struct ldb_context *ldb;
822         int ret;
823         if (!PyArg_ParseTuple(args, "O", &py_ldb))
824                 return NULL;
825
826         PyErr_LDB_OR_RAISE(py_ldb, ldb);
827
828         ret = dsdb_set_global_schema(ldb);
829         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
830
831         Py_RETURN_NONE;
832 }
833
834 static PyObject *py_dsdb_load_partition_usn(PyObject *self, PyObject *args)
835 {
836         PyObject *py_dn, *py_ldb, *result;
837         struct ldb_dn *dn;
838         uint64_t highest_uSN, urgent_uSN;
839         struct ldb_context *ldb;
840         TALLOC_CTX *mem_ctx;
841         int ret;
842
843         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dn)) {
844                 return NULL;
845         }
846
847         PyErr_LDB_OR_RAISE(py_ldb, ldb);
848
849         mem_ctx = talloc_new(NULL);
850         if (mem_ctx == NULL) {
851                 PyErr_NoMemory();
852                 return NULL;
853         }
854
855         if (!pyldb_Object_AsDn(mem_ctx, py_dn, ldb, &dn)) {
856                 talloc_free(mem_ctx);
857                 return NULL;
858         }
859
860         ret = dsdb_load_partition_usn(ldb, dn, &highest_uSN, &urgent_uSN);
861         if (ret != LDB_SUCCESS) {
862            PyErr_Format(PyExc_RuntimeError,
863                         "Failed to load partition [%s] uSN - %s",
864                         ldb_dn_get_linearized(dn),
865                         ldb_errstring(ldb));
866            talloc_free(mem_ctx);
867            return NULL;
868         }
869
870         talloc_free(mem_ctx);
871
872         result = PyDict_New();
873
874         PyDict_SetItemString(result, "uSNHighest", PyInt_FromLong((uint64_t)highest_uSN));
875         PyDict_SetItemString(result, "uSNUrgent", PyInt_FromLong((uint64_t)urgent_uSN));
876
877
878         return result;
879 }
880
881 static PyObject *py_dsdb_set_am_rodc(PyObject *self, PyObject *args)
882 {
883         PyObject *py_ldb;
884         bool ret;
885         struct ldb_context *ldb;
886         int py_val;
887
888         if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &py_val))
889                 return NULL;
890
891         PyErr_LDB_OR_RAISE(py_ldb, ldb);
892         ret = samdb_set_am_rodc(ldb, (bool)py_val);
893         if (!ret) {
894                 PyErr_SetString(PyExc_RuntimeError, "set_am_rodc failed");
895                 return NULL;
896         }
897         Py_RETURN_NONE;
898 }
899
900 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
901 {
902         WERROR result;
903         char *pf, *df, *dn;
904         PyObject *py_ldb;
905         struct ldb_context *ldb;
906
907         if (!PyArg_ParseTuple(args, "Osss", &py_ldb, &pf, &df, &dn))
908                 return NULL;
909
910         PyErr_LDB_OR_RAISE(py_ldb, ldb);
911
912         result = dsdb_set_schema_from_ldif(ldb, pf, df, dn);
913         PyErr_WERROR_NOT_OK_RAISE(result);
914
915         Py_RETURN_NONE;
916 }
917
918 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
919 {
920         PyObject *py_ldb;
921         struct ldb_context *ldb;
922         PyObject *py_from_ldb;
923         struct ldb_context *from_ldb;
924         struct dsdb_schema *schema;
925         int ret;
926         char write_indices_and_attributes = SCHEMA_WRITE;
927         if (!PyArg_ParseTuple(args, "OO|b",
928                               &py_ldb, &py_from_ldb, &write_indices_and_attributes))
929                 return NULL;
930
931         PyErr_LDB_OR_RAISE(py_ldb, ldb);
932
933         PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
934
935         schema = dsdb_get_schema(from_ldb, NULL);
936         if (!schema) {
937                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
938                 return NULL;
939         }
940
941         ret = dsdb_reference_schema(ldb, schema, write_indices_and_attributes);
942         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
943
944         Py_RETURN_NONE;
945 }
946
947 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
948 {
949         PyObject *py_ldb;
950         struct ldb_context *ldb;
951         WERROR result;
952         struct dsdb_schema *schema;
953
954         if (!PyArg_ParseTuple(args, "O", &py_ldb))
955                 return NULL;
956
957         PyErr_LDB_OR_RAISE(py_ldb, ldb);
958
959         schema = dsdb_get_schema(ldb, NULL);
960         if (!schema) {
961                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
962                 return NULL;
963         }
964
965         result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
966         PyErr_WERROR_NOT_OK_RAISE(result);
967
968         Py_RETURN_NONE;
969 }
970
971
972 static PyObject *py_dsdb_get_partitions_dn(PyObject *self, PyObject *args)
973 {
974         struct ldb_context *ldb;
975         struct ldb_dn *dn;
976         PyObject *py_ldb, *ret;
977
978         if (!PyArg_ParseTuple(args, "O", &py_ldb))
979                 return NULL;
980
981         PyErr_LDB_OR_RAISE(py_ldb, ldb);
982
983         dn = samdb_partitions_dn(ldb, NULL);
984         if (dn == NULL) {
985                 PyErr_NoMemory();
986                 return NULL;
987         }
988         ret = pyldb_Dn_FromDn(dn);
989         talloc_free(dn);
990         return ret;
991 }
992
993
994 static PyObject *py_dsdb_get_nc_root(PyObject *self, PyObject *args)
995 {
996         struct ldb_context *ldb;
997         struct ldb_dn *dn, *nc_root;
998         PyObject *py_ldb, *py_ldb_dn, *py_nc_root;
999         int ret;
1000
1001         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ldb_dn))
1002                 return NULL;
1003
1004         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1005         PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn);
1006
1007         ret = dsdb_find_nc_root(ldb, ldb, dn, &nc_root);
1008         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
1009
1010         py_nc_root = pyldb_Dn_FromDn(nc_root);
1011         talloc_unlink(ldb, nc_root);
1012         return py_nc_root;
1013 }
1014
1015 static PyObject *py_dsdb_get_wellknown_dn(PyObject *self, PyObject *args)
1016 {
1017         struct ldb_context *ldb;
1018         struct ldb_dn *nc_dn, *wk_dn;
1019         char *wkguid;
1020         PyObject *py_ldb, *py_nc_dn, *py_wk_dn;
1021         int ret;
1022
1023         if (!PyArg_ParseTuple(args, "OOs", &py_ldb, &py_nc_dn, &wkguid))
1024                 return NULL;
1025
1026         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1027         PyErr_LDB_DN_OR_RAISE(py_nc_dn, nc_dn);
1028
1029         ret = dsdb_wellknown_dn(ldb, ldb, nc_dn, wkguid, &wk_dn);
1030         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1031                 PyErr_Format(PyExc_KeyError, "Failed to find well known DN for GUID %s", wkguid);
1032                 return NULL;
1033         }
1034
1035         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
1036
1037         py_wk_dn = pyldb_Dn_FromDn(wk_dn);
1038         talloc_unlink(ldb, wk_dn);
1039         return py_wk_dn;
1040 }
1041
1042
1043 /*
1044   call into samdb_rodc()
1045  */
1046 static PyObject *py_dsdb_am_rodc(PyObject *self, PyObject *args)
1047 {
1048         PyObject *py_ldb;
1049         struct ldb_context *ldb;
1050         int ret;
1051         bool am_rodc;
1052
1053         if (!PyArg_ParseTuple(args, "O", &py_ldb))
1054                 return NULL;
1055
1056         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1057
1058         ret = samdb_rodc(ldb, &am_rodc);
1059         if (ret != LDB_SUCCESS) {
1060                 PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
1061                 return NULL;
1062         }
1063
1064         return PyBool_FromLong(am_rodc);
1065 }
1066
1067 /*
1068   call into samdb_is_pdc()
1069  */
1070 static PyObject *py_dsdb_am_pdc(PyObject *self, PyObject *args)
1071 {
1072         PyObject *py_ldb;
1073         struct ldb_context *ldb;
1074         bool am_pdc;
1075
1076         if (!PyArg_ParseTuple(args, "O", &py_ldb))
1077                 return NULL;
1078
1079         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1080
1081         am_pdc = samdb_is_pdc(ldb);
1082         return PyBool_FromLong(am_pdc);
1083 }
1084
1085 /*
1086   call DSDB_EXTENDED_CREATE_OWN_RID_SET to get a new RID set for this server
1087  */
1088 static PyObject *py_dsdb_create_own_rid_set(PyObject *self, PyObject *args)
1089 {
1090         PyObject *py_ldb;
1091         struct ldb_context *ldb;
1092         int ret;
1093         struct ldb_result *ext_res;
1094
1095         if (!PyArg_ParseTuple(args, "O", &py_ldb))
1096                 return NULL;
1097
1098         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1099
1100         /*
1101          * Run DSDB_EXTENDED_CREATE_OWN_RID_SET to get a RID set
1102          */
1103
1104         ret = ldb_extended(ldb, DSDB_EXTENDED_CREATE_OWN_RID_SET, NULL, &ext_res);
1105
1106         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
1107
1108         TALLOC_FREE(ext_res);
1109
1110         Py_RETURN_NONE;
1111 }
1112
1113 /*
1114   call DSDB_EXTENDED_ALLOCATE_RID to get a new RID set for this server
1115  */
1116 static PyObject *py_dsdb_allocate_rid(PyObject *self, PyObject *args)
1117 {
1118         PyObject *py_ldb;
1119         struct ldb_context *ldb;
1120         int ret;
1121         uint32_t rid;
1122         struct ldb_result *ext_res = NULL;
1123         struct dsdb_extended_allocate_rid *rid_return = NULL;
1124         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
1125                 return NULL;
1126         }
1127
1128         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1129
1130         rid_return = talloc_zero(ldb, struct dsdb_extended_allocate_rid);
1131         if (rid_return == NULL) {
1132                 return PyErr_NoMemory();
1133         }
1134
1135         /*
1136          * Run DSDB_EXTENDED_ALLOCATE_RID to get a new RID
1137          */
1138
1139         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID, rid_return, &ext_res);
1140         if (ret != LDB_SUCCESS) {
1141                 TALLOC_FREE(rid_return);
1142                 TALLOC_FREE(ext_res);
1143                 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
1144         }
1145
1146         rid = rid_return->rid;
1147         TALLOC_FREE(rid_return);
1148         TALLOC_FREE(ext_res);
1149
1150         return PyInt_FromLong(rid);
1151 }
1152
1153 static PyObject *py_dsdb_garbage_collect_tombstones(PyObject *self, PyObject *args)
1154 {
1155         PyObject *py_ldb, *py_list_dn;
1156         struct ldb_context *ldb = NULL;
1157         Py_ssize_t i;
1158         Py_ssize_t length;
1159         long long _current_time, _tombstone_lifetime = LLONG_MAX;
1160         uint32_t tombstone_lifetime32;
1161         struct dsdb_ldb_dn_list_node *part = NULL;
1162         time_t current_time, tombstone_lifetime;
1163         TALLOC_CTX *mem_ctx = NULL;
1164         NTSTATUS status;
1165         unsigned int num_objects_removed = 0;
1166         unsigned int num_links_removed = 0;
1167         char *error_string = NULL;
1168
1169         if (!PyArg_ParseTuple(args, "OOL|L", &py_ldb,
1170                               &py_list_dn, &_current_time, &_tombstone_lifetime)) {
1171                 return NULL;
1172         }
1173
1174
1175         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1176
1177         mem_ctx = talloc_new(ldb);
1178         if (mem_ctx == NULL) {
1179                 return PyErr_NoMemory();
1180         }
1181
1182         current_time = _current_time;
1183
1184         if (_tombstone_lifetime == LLONG_MAX) {
1185                 int ret = dsdb_tombstone_lifetime(ldb, &tombstone_lifetime32);
1186                 if (ret != LDB_SUCCESS) {
1187                         PyErr_Format(PyExc_RuntimeError,
1188                                      "Failed to get tombstone lifetime: %s",
1189                                      ldb_errstring(ldb));
1190                         TALLOC_FREE(mem_ctx);
1191                         return NULL;
1192                 }
1193                 tombstone_lifetime = tombstone_lifetime32;
1194         } else {
1195                 tombstone_lifetime = _tombstone_lifetime;
1196         }
1197
1198         if (!PyList_Check(py_list_dn)) {
1199                 PyErr_SetString(PyExc_TypeError, "A list of DNs were expected");
1200                 TALLOC_FREE(mem_ctx);
1201                 return NULL;
1202         }
1203
1204         length = PyList_GET_SIZE(py_list_dn);
1205
1206         for (i = 0; i < length; i++) {
1207                 char *part_str = PyStr_AsString(PyList_GetItem(py_list_dn, i));
1208                 struct ldb_dn *p;
1209                 struct dsdb_ldb_dn_list_node *node;
1210
1211                 if (part_str == NULL) {
1212                         TALLOC_FREE(mem_ctx);
1213                         return PyErr_NoMemory();
1214                 }
1215
1216                 p = ldb_dn_new(mem_ctx, ldb, part_str);
1217                 if (p == NULL) {
1218                         PyErr_Format(PyExc_RuntimeError, "Failed to parse DN %s", part_str);
1219                         TALLOC_FREE(mem_ctx);
1220                         return NULL;
1221                 }
1222                 node = talloc_zero(mem_ctx, struct dsdb_ldb_dn_list_node);
1223                 node->dn = p;
1224
1225                 DLIST_ADD_END(part, node);
1226         }
1227
1228         status = dsdb_garbage_collect_tombstones(mem_ctx, ldb,
1229                                                  part, current_time,
1230                                                  tombstone_lifetime,
1231                                                  &num_objects_removed,
1232                                                  &num_links_removed,
1233                                                  &error_string);
1234
1235         if (!NT_STATUS_IS_OK(status)) {
1236                 if (error_string) {
1237                         PyErr_Format(PyExc_RuntimeError, "%s", error_string);
1238                 } else {
1239                         PyErr_SetNTSTATUS(status);
1240                 }
1241                 TALLOC_FREE(mem_ctx);
1242                 return NULL;
1243         }
1244
1245         TALLOC_FREE(mem_ctx);
1246
1247         return Py_BuildValue("(II)", num_objects_removed,
1248                             num_links_removed);
1249 }
1250
1251 static PyObject *py_dsdb_load_udv_v2(PyObject *self, PyObject *args)
1252 {
1253         uint32_t count;
1254         int ret, i;
1255         bool ok;
1256         PyObject *py_ldb = NULL, *py_dn = NULL, *pylist = NULL;
1257         struct ldb_context *samdb = NULL;
1258         struct ldb_dn *dn = NULL;
1259         struct drsuapi_DsReplicaCursor2 *cursors = NULL;
1260         TALLOC_CTX *tmp_ctx = NULL;
1261
1262         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dn)) {
1263                 return NULL;
1264         }
1265
1266         PyErr_LDB_OR_RAISE(py_ldb, samdb);
1267
1268         tmp_ctx = talloc_new(samdb);
1269         if (tmp_ctx == NULL) {
1270                 return PyErr_NoMemory();
1271         }
1272
1273         ok = pyldb_Object_AsDn(tmp_ctx, py_dn, samdb, &dn);
1274         if (!ok) {
1275                 TALLOC_FREE(tmp_ctx);
1276                 return NULL;
1277         }
1278
1279         ret = dsdb_load_udv_v2(samdb, dn, tmp_ctx, &cursors, &count);
1280         if (ret != LDB_SUCCESS) {
1281                 TALLOC_FREE(tmp_ctx);
1282                 PyErr_SetString(PyExc_RuntimeError,
1283                                 "Failed to load udv from ldb");
1284                 return NULL;
1285         }
1286
1287         pylist = PyList_New(count);
1288         if (pylist == NULL) {
1289                 TALLOC_FREE(tmp_ctx);
1290                 return PyErr_NoMemory();
1291         }
1292
1293         for (i = 0; i < count; i++) {
1294                 PyObject *py_cursor;
1295                 struct drsuapi_DsReplicaCursor2 *cursor;
1296                 cursor = talloc(tmp_ctx, struct drsuapi_DsReplicaCursor2);
1297                 if (cursor == NULL) {
1298                         TALLOC_FREE(tmp_ctx);
1299                         return PyErr_NoMemory();
1300                 }
1301                 *cursor = cursors[i];
1302
1303                 py_cursor = py_return_ndr_struct("samba.dcerpc.drsuapi",
1304                                                  "DsReplicaCursor2",
1305                                                  cursor, cursor);
1306                 if (py_cursor == NULL) {
1307                         TALLOC_FREE(tmp_ctx);
1308                         return PyErr_NoMemory();
1309                 }
1310
1311                 PyList_SetItem(pylist, i, py_cursor);
1312         }
1313
1314         TALLOC_FREE(tmp_ctx);
1315         return pylist;
1316 }
1317
1318 static PyMethodDef py_dsdb_methods[] = {
1319         { "_samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
1320                 METH_VARARGS, "Get the server site name as a string"},
1321         { "_dsdb_convert_schema_to_openldap",
1322                 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS, 
1323                 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
1324                 "Create an OpenLDAP schema from a schema." },
1325         { "_samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid,
1326                 METH_VARARGS,
1327                 "samdb_set_domain_sid(samdb, sid)\n"
1328                 "Set SID of domain to use." },
1329         { "_samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid,
1330                 METH_VARARGS,
1331                 "samdb_get_domain_sid(samdb)\n"
1332                 "Get SID of domain in use." },
1333         { "_samdb_ntds_invocation_id", (PyCFunction)py_samdb_ntds_invocation_id,
1334                 METH_VARARGS, "get the NTDS invocation ID GUID as a string"},
1335         { "_samdb_set_ntds_settings_dn", (PyCFunction)py_samdb_set_ntds_settings_dn,
1336                 METH_VARARGS,
1337                 "samdb_set_ntds_settings_dn(samdb, ntds_settings_dn)\n"
1338                 "Set NTDS Settings DN for this LDB (allows it to be set before the DB fully exists)." },
1339         { "_dsdb_get_oid_from_attid", (PyCFunction)py_dsdb_get_oid_from_attid,
1340                 METH_VARARGS, NULL },
1341         { "_dsdb_get_attid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_attid_from_lDAPDisplayName,
1342                 METH_VARARGS, NULL },
1343         { "_dsdb_get_syntax_oid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_syntax_oid_from_lDAPDisplayName,
1344                 METH_VARARGS, NULL },
1345         { "_dsdb_get_systemFlags_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_systemFlags_from_lDAPDisplayName,
1346                 METH_VARARGS, NULL },
1347         { "_dsdb_get_linkId_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_linkId_from_lDAPDisplayName,
1348                 METH_VARARGS, NULL },
1349         { "_dsdb_get_lDAPDisplayName_by_attid", (PyCFunction)py_dsdb_get_lDAPDisplayName_by_attid,
1350                 METH_VARARGS, NULL },
1351         { "_dsdb_get_backlink_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_backlink_from_lDAPDisplayName,
1352                 METH_VARARGS, NULL },
1353         { "_dsdb_set_ntds_invocation_id",
1354                 (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
1355                 NULL },
1356         { "_samdb_ntds_objectGUID", (PyCFunction)py_samdb_ntds_objectGUID,
1357                 METH_VARARGS, "get the NTDS objectGUID as a string"},
1358         { "_dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema,
1359                 METH_VARARGS, NULL },
1360         { "_dsdb_load_partition_usn", (PyCFunction)py_dsdb_load_partition_usn,
1361                 METH_VARARGS,
1362                 "get uSNHighest and uSNUrgent from the partition @REPLCHANGED"},
1363         { "_dsdb_set_am_rodc",
1364                 (PyCFunction)py_dsdb_set_am_rodc, METH_VARARGS,
1365                 NULL },
1366         { "_am_rodc",
1367                 (PyCFunction)py_dsdb_am_rodc, METH_VARARGS,
1368                 NULL },
1369         { "_am_pdc",
1370                 (PyCFunction)py_dsdb_am_pdc, METH_VARARGS,
1371                 NULL },
1372         { "_dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
1373                 NULL },
1374         { "_dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
1375                 NULL },
1376         { "_dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
1377                 NULL },
1378         { "_dsdb_get_partitions_dn", (PyCFunction)py_dsdb_get_partitions_dn, METH_VARARGS, NULL },
1379         { "_dsdb_get_nc_root", (PyCFunction)py_dsdb_get_nc_root, METH_VARARGS, NULL },
1380         { "_dsdb_get_wellknown_dn", (PyCFunction)py_dsdb_get_wellknown_dn, METH_VARARGS, NULL },
1381         { "_dsdb_DsReplicaAttribute", (PyCFunction)py_dsdb_DsReplicaAttribute, METH_VARARGS, NULL },
1382         { "_dsdb_normalise_attributes", (PyCFunction)py_dsdb_normalise_attributes, METH_VARARGS, NULL },
1383         { "_dsdb_garbage_collect_tombstones", (PyCFunction)py_dsdb_garbage_collect_tombstones, METH_VARARGS,
1384                 "_dsdb_kcc_check_deleted(samdb, [dn], current_time, tombstone_lifetime)"
1385                 " -> (num_objects_expunged, num_links_expunged)" },
1386         { "_dsdb_create_own_rid_set", (PyCFunction)py_dsdb_create_own_rid_set, METH_VARARGS,
1387                 "_dsdb_create_own_rid_set(samdb)"
1388                 " -> None" },
1389         { "_dsdb_allocate_rid", (PyCFunction)py_dsdb_allocate_rid, METH_VARARGS,
1390                 "_dsdb_allocate_rid(samdb)"
1391                 " -> RID" },
1392         { "_dsdb_load_udv_v2", (PyCFunction)py_dsdb_load_udv_v2, METH_VARARGS, NULL },
1393         { NULL }
1394 };
1395
1396 static struct PyModuleDef moduledef = {
1397     PyModuleDef_HEAD_INIT,
1398     .m_name = "dsdb",
1399     .m_doc = "Python bindings for the directory service databases.",
1400     .m_size = -1,
1401     .m_methods = py_dsdb_methods,
1402 };
1403
1404 MODULE_INIT_FUNC(dsdb)
1405 {
1406         PyObject *m;
1407
1408         m = PyModule_Create(&moduledef);
1409
1410         if (m == NULL)
1411                 return NULL;
1412
1413 #define ADD_DSDB_FLAG(val)  PyModule_AddObject(m, #val, PyInt_FromLong(val))
1414
1415         /* "userAccountControl" flags */
1416         ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1417         ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1418         ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1419         ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1420         ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1421         ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1422         ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1423
1424         ADD_DSDB_FLAG(UF_SCRIPT);
1425         ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1426         ADD_DSDB_FLAG(UF_00000004);
1427         ADD_DSDB_FLAG(UF_HOMEDIR_REQUIRED);
1428         ADD_DSDB_FLAG(UF_LOCKOUT);
1429         ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1430         ADD_DSDB_FLAG(UF_PASSWD_CANT_CHANGE);
1431         ADD_DSDB_FLAG(UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED);
1432         ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1433         ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1434         ADD_DSDB_FLAG(UF_00000400);
1435         ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1436         ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1437         ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1438         ADD_DSDB_FLAG(UF_00004000);
1439         ADD_DSDB_FLAG(UF_00008000);
1440         ADD_DSDB_FLAG(UF_DONT_EXPIRE_PASSWD);
1441         ADD_DSDB_FLAG(UF_MNS_LOGON_ACCOUNT);
1442         ADD_DSDB_FLAG(UF_SMARTCARD_REQUIRED);
1443         ADD_DSDB_FLAG(UF_TRUSTED_FOR_DELEGATION);
1444         ADD_DSDB_FLAG(UF_NOT_DELEGATED);
1445         ADD_DSDB_FLAG(UF_USE_DES_KEY_ONLY);
1446         ADD_DSDB_FLAG(UF_DONT_REQUIRE_PREAUTH);
1447         ADD_DSDB_FLAG(UF_PASSWORD_EXPIRED);
1448         ADD_DSDB_FLAG(UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION);
1449         ADD_DSDB_FLAG(UF_NO_AUTH_DATA_REQUIRED);
1450         ADD_DSDB_FLAG(UF_PARTIAL_SECRETS_ACCOUNT);
1451         ADD_DSDB_FLAG(UF_USE_AES_KEYS);
1452
1453         /* groupType flags */
1454         ADD_DSDB_FLAG(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP);
1455         ADD_DSDB_FLAG(GTYPE_SECURITY_GLOBAL_GROUP);
1456         ADD_DSDB_FLAG(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
1457         ADD_DSDB_FLAG(GTYPE_SECURITY_UNIVERSAL_GROUP);
1458         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_GLOBAL_GROUP);
1459         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP);
1460         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1461
1462         /* "sAMAccountType" flags */
1463         ADD_DSDB_FLAG(ATYPE_NORMAL_ACCOUNT);
1464         ADD_DSDB_FLAG(ATYPE_WORKSTATION_TRUST);
1465         ADD_DSDB_FLAG(ATYPE_INTERDOMAIN_TRUST);
1466         ADD_DSDB_FLAG(ATYPE_SECURITY_GLOBAL_GROUP);
1467         ADD_DSDB_FLAG(ATYPE_SECURITY_LOCAL_GROUP);
1468         ADD_DSDB_FLAG(ATYPE_SECURITY_UNIVERSAL_GROUP);
1469         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_GLOBAL_GROUP);
1470         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_LOCAL_GROUP);
1471         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1472
1473         /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
1474         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2000);
1475         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003_MIXED);
1476         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003);
1477         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008);
1478         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008_R2);
1479         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2012);
1480         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2012_R2);
1481         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2016);
1482
1483         /* nc replica flags */
1484         ADD_DSDB_FLAG(INSTANCE_TYPE_IS_NC_HEAD);
1485         ADD_DSDB_FLAG(INSTANCE_TYPE_UNINSTANT);
1486         ADD_DSDB_FLAG(INSTANCE_TYPE_WRITE);
1487         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_ABOVE);
1488         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_COMING);
1489         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_GOING);
1490
1491         /* "systemFlags" */
1492         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NC);
1493         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_DOMAIN);
1494         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NOT_GC_REPLICATED);
1495         ADD_DSDB_FLAG(SYSTEM_FLAG_SCHEMA_BASE_OBJECT);
1496         ADD_DSDB_FLAG(SYSTEM_FLAG_ATTR_IS_RDN);
1497         ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
1498         ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE);
1499         ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME);
1500         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
1501         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_MOVE);
1502         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
1503         ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_DELETE);
1504
1505         /* Kerberos encryption type constants */
1506         ADD_DSDB_FLAG(ENC_ALL_TYPES);
1507         ADD_DSDB_FLAG(ENC_CRC32);
1508         ADD_DSDB_FLAG(ENC_RSA_MD5);
1509         ADD_DSDB_FLAG(ENC_RC4_HMAC_MD5);
1510         ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES128);
1511         ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES256);
1512
1513         ADD_DSDB_FLAG(SEARCH_FLAG_ATTINDEX);
1514         ADD_DSDB_FLAG(SEARCH_FLAG_PDNTATTINDEX);
1515         ADD_DSDB_FLAG(SEARCH_FLAG_ANR);
1516         ADD_DSDB_FLAG(SEARCH_FLAG_PRESERVEONDELETE);
1517         ADD_DSDB_FLAG(SEARCH_FLAG_COPY);
1518         ADD_DSDB_FLAG(SEARCH_FLAG_TUPLEINDEX);
1519         ADD_DSDB_FLAG(SEARCH_FLAG_SUBTREEATTRINDEX);
1520         ADD_DSDB_FLAG(SEARCH_FLAG_CONFIDENTIAL);
1521         ADD_DSDB_FLAG(SEARCH_FLAG_NEVERVALUEAUDIT);
1522         ADD_DSDB_FLAG(SEARCH_FLAG_RODC_ATTRIBUTE);
1523
1524         ADD_DSDB_FLAG(DS_FLAG_ATTR_NOT_REPLICATED);
1525         ADD_DSDB_FLAG(DS_FLAG_ATTR_REQ_PARTIAL_SET_MEMBER);
1526         ADD_DSDB_FLAG(DS_FLAG_ATTR_IS_CONSTRUCTED);
1527
1528         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_AUTO_TOPOLOGY_DISABLED);
1529         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_CLEANUP_DISABLED);
1530         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_MIN_HOPS_DISABLED);
1531         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_DETECT_STALE_DISABLED);
1532         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_INTER_SITE_AUTO_TOPOLOGY_DISABLED);
1533         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_GROUP_CACHING_ENABLED);
1534         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_FORCE_KCC_WHISTLER_BEHAVIOR);
1535         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_RAND_BH_SELECTION_DISABLED);
1536         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_SCHEDULE_HASHING_ENABLED);
1537         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_REDUNDANT_SERVER_TOPOLOGY_ENABLED);
1538
1539         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_IS_GC);
1540         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL);
1541         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
1542         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_NTDSCONN_XLATE);
1543         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_SPN_REGISTRATION);
1544
1545         ADD_DSDB_FLAG(NTDSCONN_KCC_GC_TOPOLOGY);
1546         ADD_DSDB_FLAG(NTDSCONN_KCC_RING_TOPOLOGY);
1547         ADD_DSDB_FLAG(NTDSCONN_KCC_MINIMIZE_HOPS_TOPOLOGY);
1548         ADD_DSDB_FLAG(NTDSCONN_KCC_STALE_SERVERS_TOPOLOGY);
1549         ADD_DSDB_FLAG(NTDSCONN_KCC_OSCILLATING_CONNECTION_TOPOLOGY);
1550         ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_GC_TOPOLOGY);
1551         ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_TOPOLOGY);
1552         ADD_DSDB_FLAG(NTDSCONN_KCC_SERVER_FAILOVER_TOPOLOGY);
1553         ADD_DSDB_FLAG(NTDSCONN_KCC_SITE_FAILOVER_TOPOLOGY);
1554         ADD_DSDB_FLAG(NTDSCONN_KCC_REDUNDANT_SERVER_TOPOLOGY);
1555
1556         ADD_DSDB_FLAG(NTDSCONN_OPT_IS_GENERATED);
1557         ADD_DSDB_FLAG(NTDSCONN_OPT_TWOWAY_SYNC);
1558         ADD_DSDB_FLAG(NTDSCONN_OPT_OVERRIDE_NOTIFY_DEFAULT);
1559         ADD_DSDB_FLAG(NTDSCONN_OPT_USE_NOTIFY);
1560         ADD_DSDB_FLAG(NTDSCONN_OPT_DISABLE_INTERSITE_COMPRESSION);
1561         ADD_DSDB_FLAG(NTDSCONN_OPT_USER_OWNED_SCHEDULE);
1562         ADD_DSDB_FLAG(NTDSCONN_OPT_RODC_TOPOLOGY);
1563
1564         /* Site Link Object options */
1565         ADD_DSDB_FLAG(NTDSSITELINK_OPT_USE_NOTIFY);
1566         ADD_DSDB_FLAG(NTDSSITELINK_OPT_TWOWAY_SYNC);
1567         ADD_DSDB_FLAG(NTDSSITELINK_OPT_DISABLE_COMPRESSION);
1568
1569         /* GPO policy flags */
1570         ADD_DSDB_FLAG(GPLINK_OPT_DISABLE);
1571         ADD_DSDB_FLAG(GPLINK_OPT_ENFORCE);
1572         ADD_DSDB_FLAG(GPO_FLAG_USER_DISABLE);
1573         ADD_DSDB_FLAG(GPO_FLAG_MACHINE_DISABLE);
1574         ADD_DSDB_FLAG(GPO_INHERIT);
1575         ADD_DSDB_FLAG(GPO_BLOCK_INHERITANCE);
1576
1577 #define ADD_DSDB_STRING(val)  PyModule_AddObject(m, #val, PyStr_FromString(val))
1578
1579         ADD_DSDB_STRING(DSDB_SYNTAX_BINARY_DN);
1580         ADD_DSDB_STRING(DSDB_SYNTAX_STRING_DN);
1581         ADD_DSDB_STRING(DSDB_SYNTAX_OR_NAME);
1582         ADD_DSDB_STRING(DSDB_CONTROL_DBCHECK);
1583         ADD_DSDB_STRING(DSDB_CONTROL_DBCHECK_MODIFY_RO_REPLICA);
1584         ADD_DSDB_STRING(DSDB_CONTROL_DBCHECK_FIX_DUPLICATE_LINKS);
1585         ADD_DSDB_STRING(DSDB_CONTROL_DBCHECK_FIX_LINK_DN_NAME);
1586         ADD_DSDB_STRING(DSDB_CONTROL_REPLMD_VANISH_LINKS);
1587         ADD_DSDB_STRING(DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID);
1588         ADD_DSDB_STRING(DSDB_CONTROL_SKIP_DUPLICATES_CHECK_OID);
1589         ADD_DSDB_STRING(DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID);
1590         ADD_DSDB_STRING(DSDB_CONTROL_INVALID_NOT_IMPLEMENTED);
1591
1592         ADD_DSDB_STRING(DS_GUID_COMPUTERS_CONTAINER);
1593         ADD_DSDB_STRING(DS_GUID_DELETED_OBJECTS_CONTAINER);
1594         ADD_DSDB_STRING(DS_GUID_DOMAIN_CONTROLLERS_CONTAINER);
1595         ADD_DSDB_STRING(DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER);
1596         ADD_DSDB_STRING(DS_GUID_INFRASTRUCTURE_CONTAINER);
1597         ADD_DSDB_STRING(DS_GUID_LOSTANDFOUND_CONTAINER);
1598         ADD_DSDB_STRING(DS_GUID_MICROSOFT_PROGRAM_DATA_CONTAINER);
1599         ADD_DSDB_STRING(DS_GUID_NTDS_QUOTAS_CONTAINER);
1600         ADD_DSDB_STRING(DS_GUID_PROGRAM_DATA_CONTAINER);
1601         ADD_DSDB_STRING(DS_GUID_SYSTEMS_CONTAINER);
1602         ADD_DSDB_STRING(DS_GUID_USERS_CONTAINER);
1603
1604         return m;
1605 }