s4-pydsdb: Add bindings for dsdb_find_nc_root()
[mat/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 "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"
31
32 void initdsdb(void);
33
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
40
41 /* FIXME: These should be in a header file somewhere */
42 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
43         if (!py_check_dcerpc_type(py_ldb, "ldb", "Ldb")) { \
44                 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
45                 return NULL; \
46         } \
47         ldb = pyldb_Ldb_AsLdbContext(py_ldb);
48
49 #define PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn) \
50         if (!py_check_dcerpc_type(py_ldb_dn, "ldb", "Dn")) { \
51                 PyErr_SetString(py_ldb_get_exception(), "ldb Dn object required"); \
52                 return NULL; \
53         } \
54         dn = pyldb_Dn_AsDn(py_ldb_dn);
55
56 static PyObject *py_ldb_get_exception(void)
57 {
58         PyObject *mod = PyImport_ImportModule("ldb");
59         if (mod == NULL)
60                 return NULL;
61
62         return PyObject_GetAttrString(mod, "LdbError");
63 }
64
65 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
66 {
67         if (ret == LDB_ERR_PYTHON_EXCEPTION)
68                 return; /* Python exception should already be set, just keep that */
69
70         PyErr_SetObject(error, 
71                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
72                         ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
73 }
74
75 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
76 {
77         PyObject *py_ldb, *result;
78         struct ldb_context *ldb;
79         const char *site;
80         TALLOC_CTX *mem_ctx;
81
82         if (!PyArg_ParseTuple(args, "O", &py_ldb))
83                 return NULL;
84
85         PyErr_LDB_OR_RAISE(py_ldb, ldb);
86
87         mem_ctx = talloc_new(NULL);
88         if (mem_ctx == NULL) {
89                 PyErr_NoMemory();
90                 return NULL;
91         }
92
93         site = samdb_server_site_name(ldb, mem_ctx);
94         if (site == NULL) {
95                 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
96                 talloc_free(mem_ctx);
97                 return NULL;
98         }
99
100         result = PyString_FromString(site);
101         talloc_free(mem_ctx);
102         return result;
103 }
104
105 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
106                                                     PyObject *args)
107 {
108         char *target_str, *mapping;
109         PyObject *py_ldb;
110         struct ldb_context *ldb;
111         PyObject *ret;
112         char *retstr;
113
114         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
115                 return NULL;
116
117         PyErr_LDB_OR_RAISE(py_ldb, ldb);
118
119         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
120         if (retstr == NULL) {
121                 PyErr_SetString(PyExc_RuntimeError,
122                                                 "dsdb_convert_schema_to_openldap failed");
123                 return NULL;
124         } 
125
126         ret = PyString_FromString(retstr);
127         talloc_free(retstr);
128         return ret;
129 }
130
131 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
132
133         PyObject *py_ldb, *py_sid;
134         struct ldb_context *ldb;
135         struct dom_sid *sid;
136         bool ret;
137
138         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
139                 return NULL;
140         
141         PyErr_LDB_OR_RAISE(py_ldb, ldb);
142
143         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
144         if (sid == NULL) {
145                 PyErr_NoMemory();
146                 return NULL;
147         }
148
149         ret = samdb_set_domain_sid(ldb, sid);
150         talloc_free(sid);
151         if (!ret) {
152                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
153                 return NULL;
154         } 
155         Py_RETURN_NONE;
156 }
157
158 static PyObject *py_samdb_set_ntds_settings_dn(PyLdbObject *self, PyObject *args)
159
160         PyObject *py_ldb, *py_ntds_settings_dn;
161         struct ldb_context *ldb;
162         struct ldb_dn *ntds_settings_dn;
163         TALLOC_CTX *tmp_ctx;
164         bool ret;
165
166         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ntds_settings_dn))
167                 return NULL;
168
169         PyErr_LDB_OR_RAISE(py_ldb, ldb);
170
171         tmp_ctx = talloc_new(NULL);
172         if (tmp_ctx == NULL) {
173                 PyErr_NoMemory();
174                 return NULL;
175         }
176
177         if (!pyldb_Object_AsDn(tmp_ctx, py_ntds_settings_dn, ldb, &ntds_settings_dn)) {
178                 /* exception thrown by "pyldb_Object_AsDn" */
179                 talloc_free(tmp_ctx);
180                 return NULL;
181         }
182
183         ret = samdb_set_ntds_settings_dn(ldb, ntds_settings_dn);
184         talloc_free(tmp_ctx);
185         if (!ret) {
186                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_settings_dn failed");
187                 return NULL;
188         } 
189         Py_RETURN_NONE;
190 }
191
192 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
193
194         PyObject *py_ldb;
195         struct ldb_context *ldb;
196         const struct dom_sid *sid;
197         PyObject *ret;
198         char *retstr;
199
200         if (!PyArg_ParseTuple(args, "O", &py_ldb))
201                 return NULL;
202
203         PyErr_LDB_OR_RAISE(py_ldb, ldb);
204
205         sid = samdb_domain_sid(ldb);
206         if (!sid) {
207                 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
208                 return NULL;
209         }
210
211         retstr = dom_sid_string(NULL, sid);
212         if (retstr == NULL) {
213                 PyErr_NoMemory();
214                 return NULL;
215         }
216         ret = PyString_FromString(retstr);
217         talloc_free(retstr);
218         return ret;
219 }
220
221 static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
222 {
223         PyObject *py_ldb, *result;
224         struct ldb_context *ldb;
225         const struct GUID *guid;
226         char *retstr;
227
228         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
229                 return NULL;
230         }
231
232         PyErr_LDB_OR_RAISE(py_ldb, ldb);
233
234         guid = samdb_ntds_invocation_id(ldb);
235         if (guid == NULL) {
236                 PyErr_SetString(PyExc_RuntimeError,
237                                                 "Failed to find NTDS invocation ID");
238                 return NULL;
239         }
240
241         retstr = GUID_string(NULL, guid);
242         if (retstr == NULL) {
243                 PyErr_NoMemory();
244                 return NULL;
245         }
246         result = PyString_FromString(retstr);
247         talloc_free(retstr);
248         return result;
249 }
250
251 static PyObject *py_dsdb_get_oid_from_attid(PyObject *self, PyObject *args)
252 {
253         PyObject *py_ldb;
254         struct ldb_context *ldb;
255         uint32_t attid;
256         struct dsdb_schema *schema;
257         const char *oid;
258         PyObject *ret;
259         WERROR status;
260         TALLOC_CTX *mem_ctx;
261
262         if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &attid))
263                 return NULL;
264
265         PyErr_LDB_OR_RAISE(py_ldb, ldb);
266
267         mem_ctx = talloc_new(NULL);
268         if (!mem_ctx) {
269                 PyErr_NoMemory();
270                 return NULL;
271         }
272
273         schema = dsdb_get_schema(ldb, mem_ctx);
274         if (!schema) {
275                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb \n");
276                 talloc_free(mem_ctx);
277                 return NULL;
278         }
279         
280         status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attid,
281                                                 mem_ctx, &oid);
282         if (!W_ERROR_IS_OK(status)) {
283                 PyErr_SetWERROR(status);
284                 talloc_free(mem_ctx);
285                 return NULL;
286         }
287
288         ret = PyString_FromString(oid);
289
290         talloc_free(mem_ctx);
291
292         return ret;
293 }
294
295
296 static PyObject *py_dsdb_get_attid_from_lDAPDisplayName(PyObject *self, PyObject *args)
297 {
298         PyObject *py_ldb, *is_schema_nc;
299         struct ldb_context *ldb;
300         struct dsdb_schema *schema;
301         const char *ldap_display_name;
302         bool schema_nc = false;
303         const struct dsdb_attribute *a;
304         uint32_t attid;
305
306         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &is_schema_nc))
307                 return NULL;
308
309         PyErr_LDB_OR_RAISE(py_ldb, ldb);
310
311         if (is_schema_nc) {
312                 if (!PyBool_Check(is_schema_nc)) {
313                         PyErr_SetString(PyExc_TypeError, "Expected boolean is_schema_nc");
314                         return NULL;
315                 }
316                 if (is_schema_nc == Py_True) {
317                         schema_nc = true;
318                 }
319         }
320
321         schema = dsdb_get_schema(ldb, NULL);
322
323         if (!schema) {
324                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
325                 return NULL;
326         }
327
328         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
329         if (a == NULL) {
330                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
331                 return NULL;
332         }
333
334         attid = dsdb_attribute_get_attid(a, schema_nc);
335
336         return PyLong_FromUnsignedLong(attid);
337 }
338
339 /*
340   return the systemFlags as int from the attribute name
341  */
342 static PyObject *py_dsdb_get_systemFlags_from_lDAPDisplayName(PyObject *self, PyObject *args)
343 {
344         PyObject *py_ldb;
345         struct ldb_context *ldb;
346         struct dsdb_schema *schema;
347         const char *ldap_display_name;
348         const struct dsdb_attribute *attribute;
349
350         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
351                 return NULL;
352
353         PyErr_LDB_OR_RAISE(py_ldb, ldb);
354
355         schema = dsdb_get_schema(ldb, NULL);
356
357         if (!schema) {
358                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
359                 return NULL;
360         }
361
362         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
363         if (attribute == NULL) {
364                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
365                 return NULL;
366         }
367
368         return PyInt_FromLong(attribute->systemFlags);
369 }
370
371 /*
372   return the linkID from the attribute name
373  */
374 static PyObject *py_dsdb_get_linkId_from_lDAPDisplayName(PyObject *self, PyObject *args)
375 {
376         PyObject *py_ldb;
377         struct ldb_context *ldb;
378         struct dsdb_schema *schema;
379         const char *ldap_display_name;
380         const struct dsdb_attribute *attribute;
381
382         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
383                 return NULL;
384
385         PyErr_LDB_OR_RAISE(py_ldb, ldb);
386
387         schema = dsdb_get_schema(ldb, NULL);
388
389         if (!schema) {
390                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
391                 return NULL;
392         }
393
394         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
395         if (attribute == NULL) {
396                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
397                 return NULL;
398         }
399
400         return PyInt_FromLong(attribute->linkID);
401 }
402
403 /*
404   return the backlink attribute name (if any) for an attribute
405  */
406 static PyObject *py_dsdb_get_backlink_from_lDAPDisplayName(PyObject *self, PyObject *args)
407 {
408         PyObject *py_ldb;
409         struct ldb_context *ldb;
410         struct dsdb_schema *schema;
411         const char *ldap_display_name;
412         const struct dsdb_attribute *attribute, *target_attr;
413
414         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
415                 return NULL;
416
417         PyErr_LDB_OR_RAISE(py_ldb, ldb);
418
419         schema = dsdb_get_schema(ldb, NULL);
420
421         if (!schema) {
422                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
423                 return NULL;
424         }
425
426         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
427         if (attribute == NULL) {
428                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
429                 return NULL;
430         }
431
432         if (attribute->linkID == 0) {
433                 Py_RETURN_NONE;
434         }
435
436         target_attr = dsdb_attribute_by_linkID(schema, attribute->linkID ^ 1);
437         if (target_attr == NULL) {
438                 /* when we add pseudo-backlinks we'll need to handle
439                    them here */
440                 Py_RETURN_NONE;
441         }
442
443         return PyString_FromString(target_attr->lDAPDisplayName);
444 }
445
446
447 static PyObject *py_dsdb_get_lDAPDisplayName_by_attid(PyObject *self, PyObject *args)
448 {
449         PyObject *py_ldb;
450         struct ldb_context *ldb;
451         struct dsdb_schema *schema;
452         const struct dsdb_attribute *a;
453         uint32_t attid;
454
455         if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &attid))
456                 return NULL;
457
458         PyErr_LDB_OR_RAISE(py_ldb, ldb);
459
460         schema = dsdb_get_schema(ldb, NULL);
461
462         if (!schema) {
463                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
464                 return NULL;
465         }
466
467         a = dsdb_attribute_by_attributeID_id(schema, attid);
468         if (a == NULL) {
469                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '0x%08x'", attid);
470                 return NULL;
471         }
472
473         return PyString_FromString(a->lDAPDisplayName);
474 }
475
476
477 /*
478   return the attribute syntax oid as a string from the attribute name
479  */
480 static PyObject *py_dsdb_get_syntax_oid_from_lDAPDisplayName(PyObject *self, PyObject *args)
481 {
482         PyObject *py_ldb;
483         struct ldb_context *ldb;
484         struct dsdb_schema *schema;
485         const char *ldap_display_name;
486         const struct dsdb_attribute *attribute;
487
488         if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
489                 return NULL;
490
491         PyErr_LDB_OR_RAISE(py_ldb, ldb);
492
493         schema = dsdb_get_schema(ldb, NULL);
494
495         if (!schema) {
496                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
497                 return NULL;
498         }
499
500         attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
501         if (attribute == NULL) {
502                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
503                 return NULL;
504         }
505
506         return PyString_FromString(attribute->syntax->ldap_oid);
507 }
508
509 /*
510   convert a python string to a DRSUAPI drsuapi_DsReplicaAttribute attribute
511  */
512 static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
513 {
514         PyObject *py_ldb, *el_list, *ret;
515         struct ldb_context *ldb;
516         char *ldap_display_name;
517         const struct dsdb_attribute *a;
518         struct dsdb_schema *schema;
519         struct dsdb_syntax_ctx syntax_ctx;
520         struct ldb_message_element *el;
521         struct drsuapi_DsReplicaAttribute *attr;
522         TALLOC_CTX *tmp_ctx;
523         WERROR werr;
524         Py_ssize_t i;
525
526         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
527                 return NULL;
528         }
529
530         PyErr_LDB_OR_RAISE(py_ldb, ldb);
531
532         if (!PyList_Check(el_list)) {
533                 PyErr_Format(PyExc_TypeError, "ldif_elements must be a list");
534                 return NULL;
535         }
536
537         schema = dsdb_get_schema(ldb, NULL);
538         if (!schema) {
539                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
540                 return NULL;
541         }
542
543         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
544         if (a == NULL) {
545                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
546                 return NULL;
547         }
548
549         dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
550         syntax_ctx.is_schema_nc = false;
551
552         tmp_ctx = talloc_new(ldb);
553         if (tmp_ctx == NULL) {
554                 PyErr_NoMemory();
555                 return NULL;
556         }
557
558         el = talloc_zero(tmp_ctx, struct ldb_message_element);
559         if (el == NULL) {
560                 PyErr_NoMemory();
561                 talloc_free(tmp_ctx);
562                 return NULL;
563         }
564
565         el->name = ldap_display_name;
566         el->num_values = PyList_Size(el_list);
567
568         el->values = talloc_array(el, struct ldb_val, el->num_values);
569         if (el->values == NULL) {
570                 PyErr_NoMemory();
571                 talloc_free(tmp_ctx);
572                 return NULL;
573         }
574
575         for (i = 0; i < el->num_values; i++) {
576                 PyObject *item = PyList_GetItem(el_list, i);
577                 if (!PyString_Check(item)) {
578                         PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
579                         talloc_free(tmp_ctx);
580                         return NULL;
581                 }
582                 el->values[i].data = (uint8_t *)PyString_AsString(item);
583                 el->values[i].length = PyString_Size(item);
584         }
585
586         attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
587         if (attr == NULL) {
588                 PyErr_NoMemory();
589                 talloc_free(tmp_ctx);
590                 return NULL;
591         }
592
593         werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
594         PyErr_WERROR_IS_ERR_RAISE(werr);
595
596         ret = py_return_ndr_struct("samba.dcerpc.drsuapi", "DsReplicaAttribute", attr, attr);
597
598         talloc_free(tmp_ctx);
599
600         return ret;
601 }
602
603
604 /*
605   normalise a ldb attribute list
606  */
607 static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
608 {
609         PyObject *py_ldb, *el_list, *ret;
610         struct ldb_context *ldb;
611         char *ldap_display_name;
612         const struct dsdb_attribute *a;
613         struct dsdb_schema *schema;
614         struct dsdb_syntax_ctx syntax_ctx;
615         struct ldb_message_element *el;
616         struct drsuapi_DsReplicaAttribute *attr;
617         TALLOC_CTX *tmp_ctx;
618         WERROR werr;
619         Py_ssize_t i;
620
621         if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
622                 return NULL;
623         }
624
625         PyErr_LDB_OR_RAISE(py_ldb, ldb);
626
627         if (!PyList_Check(el_list)) {
628                 PyErr_Format(PyExc_TypeError, "ldif_elements must be a list");
629                 return NULL;
630         }
631
632         schema = dsdb_get_schema(ldb, NULL);
633         if (!schema) {
634                 PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
635                 return NULL;
636         }
637
638         a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
639         if (a == NULL) {
640                 PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
641                 return NULL;
642         }
643
644         dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
645         syntax_ctx.is_schema_nc = false;
646
647         tmp_ctx = talloc_new(ldb);
648         if (tmp_ctx == NULL) {
649                 PyErr_NoMemory();
650                 return NULL;
651         }
652
653         el = talloc_zero(tmp_ctx, struct ldb_message_element);
654         if (el == NULL) {
655                 PyErr_NoMemory();
656                 talloc_free(tmp_ctx);
657                 return NULL;
658         }
659
660         el->name = ldap_display_name;
661         el->num_values = PyList_Size(el_list);
662
663         el->values = talloc_array(el, struct ldb_val, el->num_values);
664         if (el->values == NULL) {
665                 PyErr_NoMemory();
666                 talloc_free(tmp_ctx);
667                 return NULL;
668         }
669
670         for (i = 0; i < el->num_values; i++) {
671                 PyObject *item = PyList_GetItem(el_list, i);
672                 if (!PyString_Check(item)) {
673                         PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
674                         talloc_free(tmp_ctx);
675                         return NULL;
676                 }
677                 el->values[i].data = (uint8_t *)PyString_AsString(item);
678                 el->values[i].length = PyString_Size(item);
679         }
680
681         /* Normalise "objectClass" attribute if needed */
682         if (ldb_attr_cmp(a->lDAPDisplayName, "objectClass") == 0) {
683                 int iret;
684                 iret = dsdb_sort_objectClass_attr(ldb, schema, tmp_ctx, el,
685                                                  tmp_ctx, el);
686                 if (iret != LDB_SUCCESS) {
687                         PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
688                         talloc_free(tmp_ctx);
689                         return NULL;
690                 }
691         }
692
693         /* first run ldb_to_drsuapi, then convert back again. This has
694          * the effect of normalising the attributes
695          */
696
697         attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
698         if (attr == NULL) {
699                 PyErr_NoMemory();
700                 talloc_free(tmp_ctx);
701                 return NULL;
702         }
703
704         werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
705         PyErr_WERROR_IS_ERR_RAISE(werr);
706
707         /* now convert back again */
708         werr = a->syntax->drsuapi_to_ldb(&syntax_ctx, a, attr, el, el);
709         PyErr_WERROR_IS_ERR_RAISE(werr);
710
711         ret = py_return_ndr_struct("ldb", "MessageElement", el, el);
712
713         talloc_free(tmp_ctx);
714
715         return ret;
716 }
717
718
719 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
720 {
721         PyObject *py_ldb, *py_guid;
722         bool ret;
723         struct GUID guid;
724         struct ldb_context *ldb;
725         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
726                 return NULL;
727
728         PyErr_LDB_OR_RAISE(py_ldb, ldb);
729         GUID_from_string(PyString_AsString(py_guid), &guid);
730
731         ret = samdb_set_ntds_invocation_id(ldb, &guid);
732         if (!ret) {
733                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
734                 return NULL;
735         }
736         Py_RETURN_NONE;
737 }
738
739 static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
740 {
741         PyObject *py_ldb, *result;
742         struct ldb_context *ldb;
743         const struct GUID *guid;
744         char *retstr;
745
746         if (!PyArg_ParseTuple(args, "O", &py_ldb)) {
747                 return NULL;
748         }
749
750         PyErr_LDB_OR_RAISE(py_ldb, ldb);
751
752         guid = samdb_ntds_objectGUID(ldb);
753         if (guid == NULL) {
754                 PyErr_SetString(PyExc_RuntimeError, "Failed to find NTDS GUID");
755                 return NULL;
756         }
757
758         retstr = GUID_string(NULL, guid);
759         if (retstr == NULL) {
760                 PyErr_NoMemory();
761                 return NULL;
762         }
763         result = PyString_FromString(retstr);
764         talloc_free(retstr);
765         return result;
766 }
767
768 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
769 {
770         PyObject *py_ldb;
771         struct ldb_context *ldb;
772         int ret;
773         if (!PyArg_ParseTuple(args, "O", &py_ldb))
774                 return NULL;
775
776         PyErr_LDB_OR_RAISE(py_ldb, ldb);
777
778         ret = dsdb_set_global_schema(ldb);
779         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
780
781         Py_RETURN_NONE;
782 }
783
784 static PyObject *py_dsdb_load_partition_usn(PyObject *self, PyObject *args)
785 {
786         PyObject *py_dn, *py_ldb, *result;
787         struct ldb_dn *dn;
788         uint64_t highest_uSN, urgent_uSN;
789         struct ldb_context *ldb;
790         TALLOC_CTX *mem_ctx;
791         int ret;
792
793         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dn)) {
794                 return NULL;
795         }
796
797         PyErr_LDB_OR_RAISE(py_ldb, ldb);
798
799         mem_ctx = talloc_new(NULL);
800         if (mem_ctx == NULL) {
801                 PyErr_NoMemory();
802                 return NULL;
803         }
804
805         if (!pyldb_Object_AsDn(mem_ctx, py_dn, ldb, &dn)) {
806                 talloc_free(mem_ctx);
807                 return NULL;
808         }
809
810         ret = dsdb_load_partition_usn(ldb, dn, &highest_uSN, &urgent_uSN);
811         if (ret != LDB_SUCCESS) {
812            PyErr_Format(PyExc_RuntimeError,
813                         "Failed to load partition [%s] uSN - %s",
814                         ldb_dn_get_linearized(dn),
815                         ldb_errstring(ldb));
816            talloc_free(mem_ctx);
817            return NULL;
818         }
819
820         talloc_free(mem_ctx);
821
822         result = PyDict_New();
823
824         PyDict_SetItemString(result, "uSNHighest", PyInt_FromLong((uint64_t)highest_uSN));
825         PyDict_SetItemString(result, "uSNUrgent", PyInt_FromLong((uint64_t)urgent_uSN));
826
827
828         return result;
829 }
830
831 static PyObject *py_dsdb_set_am_rodc(PyObject *self, PyObject *args)
832 {
833         PyObject *py_ldb;
834         bool ret;
835         struct ldb_context *ldb;
836         int py_val;
837
838         if (!PyArg_ParseTuple(args, "Oi", &py_ldb, &py_val))
839                 return NULL;
840
841         PyErr_LDB_OR_RAISE(py_ldb, ldb);
842         ret = samdb_set_am_rodc(ldb, (bool)py_val);
843         if (!ret) {
844                 PyErr_SetString(PyExc_RuntimeError, "set_am_rodc failed");
845                 return NULL;
846         }
847         Py_RETURN_NONE;
848 }
849
850 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
851 {
852         WERROR result;
853         char *pf, *df, *dn;
854         PyObject *py_ldb;
855         struct ldb_context *ldb;
856
857         if (!PyArg_ParseTuple(args, "Osss", &py_ldb, &pf, &df, &dn))
858                 return NULL;
859
860         PyErr_LDB_OR_RAISE(py_ldb, ldb);
861
862         result = dsdb_set_schema_from_ldif(ldb, pf, df, dn);
863         PyErr_WERROR_IS_ERR_RAISE(result);
864
865         Py_RETURN_NONE;
866 }
867
868 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
869 {
870         PyObject *py_ldb;
871         struct ldb_context *ldb;
872         PyObject *py_from_ldb;
873         struct ldb_context *from_ldb;
874         struct dsdb_schema *schema;
875         int ret;
876         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_from_ldb))
877                 return NULL;
878
879         PyErr_LDB_OR_RAISE(py_ldb, ldb);
880
881         PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
882
883         schema = dsdb_get_schema(from_ldb, NULL);
884         if (!schema) {
885                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
886                 return NULL;
887         }
888
889         ret = dsdb_reference_schema(ldb, schema, true);
890         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
891
892         Py_RETURN_NONE;
893 }
894
895 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
896 {
897         PyObject *py_ldb;
898         struct ldb_context *ldb;
899         WERROR result;
900         struct dsdb_schema *schema;
901
902         if (!PyArg_ParseTuple(args, "O", &py_ldb))
903                 return NULL;
904
905         PyErr_LDB_OR_RAISE(py_ldb, ldb);
906
907         schema = dsdb_get_schema(ldb, NULL);
908         if (!schema) {
909                 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
910                 return NULL;
911         }
912
913         result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
914         PyErr_WERROR_IS_ERR_RAISE(result);
915
916         Py_RETURN_NONE;
917 }
918
919
920 static PyObject *py_dsdb_get_partitions_dn(PyObject *self, PyObject *args)
921 {
922         struct ldb_context *ldb;
923         struct ldb_dn *dn;
924         PyObject *py_ldb, *ret;
925         PyObject *mod;
926
927         mod = PyImport_ImportModule("ldb");
928
929         if (!PyArg_ParseTuple(args, "O", &py_ldb))
930                 return NULL;
931
932         PyErr_LDB_OR_RAISE(py_ldb, ldb);
933
934         dn = samdb_partitions_dn(ldb, NULL);
935         if (dn == NULL) {
936                 PyErr_NoMemory();
937                 return NULL;
938         }
939         ret = pyldb_Dn_FromDn(dn);
940         talloc_free(dn);
941         return ret;
942 }
943
944
945 static PyObject *py_dsdb_get_nc_root(PyObject *self, PyObject *args)
946 {
947         struct ldb_context *ldb;
948         struct ldb_dn *dn, *nc_root;
949         PyObject *py_ldb, *py_ldb_dn, *py_nc_root;
950         int ret;
951
952         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_ldb_dn))
953                 return NULL;
954
955         PyErr_LDB_OR_RAISE(py_ldb, ldb);
956         PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn);
957
958         ret = dsdb_find_nc_root(ldb, ldb, dn, &nc_root);
959         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
960
961         py_nc_root = pyldb_Dn_FromDn(nc_root);
962         talloc_unlink(ldb, nc_root);
963         return py_nc_root;
964 }
965
966
967 /*
968   call into samdb_rodc()
969  */
970 static PyObject *py_dsdb_am_rodc(PyObject *self, PyObject *args)
971 {
972         PyObject *py_ldb;
973         struct ldb_context *ldb;
974         int ret;
975         bool am_rodc;
976
977         if (!PyArg_ParseTuple(args, "O", &py_ldb))
978                 return NULL;
979
980         PyErr_LDB_OR_RAISE(py_ldb, ldb);
981
982         ret = samdb_rodc(ldb, &am_rodc);
983         if (ret != LDB_SUCCESS) {
984                 PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
985                 return NULL;
986         }
987
988         return PyBool_FromLong(am_rodc);
989 }
990
991 /*
992   call into samdb_is_pdc()
993  */
994 static PyObject *py_dsdb_am_pdc(PyObject *self, PyObject *args)
995 {
996         PyObject *py_ldb;
997         struct ldb_context *ldb;
998         bool am_pdc;
999
1000         if (!PyArg_ParseTuple(args, "O", &py_ldb))
1001                 return NULL;
1002
1003         PyErr_LDB_OR_RAISE(py_ldb, ldb);
1004
1005         am_pdc = samdb_is_pdc(ldb);
1006         return PyBool_FromLong(am_pdc);
1007 }
1008
1009
1010 static PyMethodDef py_dsdb_methods[] = {
1011         { "_samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
1012                 METH_VARARGS, "Get the server site name as a string"},
1013         { "_dsdb_convert_schema_to_openldap",
1014                 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS, 
1015                 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
1016                 "Create an OpenLDAP schema from a schema." },
1017         { "_samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid,
1018                 METH_VARARGS,
1019                 "samdb_set_domain_sid(samdb, sid)\n"
1020                 "Set SID of domain to use." },
1021         { "_samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid,
1022                 METH_VARARGS,
1023                 "samdb_get_domain_sid(samdb)\n"
1024                 "Get SID of domain in use." },
1025         { "_samdb_ntds_invocation_id", (PyCFunction)py_samdb_ntds_invocation_id,
1026                 METH_VARARGS, "get the NTDS invocation ID GUID as a string"},
1027         { "_samdb_set_ntds_settings_dn", (PyCFunction)py_samdb_set_ntds_settings_dn,
1028                 METH_VARARGS,
1029                 "samdb_set_ntds_settings_dn(samdb, ntds_settings_dn)\n"
1030                 "Set NTDS Settings DN for this LDB (allows it to be set before the DB fully exists)." },
1031         { "_dsdb_get_oid_from_attid", (PyCFunction)py_dsdb_get_oid_from_attid,
1032                 METH_VARARGS, NULL },
1033         { "_dsdb_get_attid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_attid_from_lDAPDisplayName,
1034                 METH_VARARGS, NULL },
1035         { "_dsdb_get_syntax_oid_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_syntax_oid_from_lDAPDisplayName,
1036                 METH_VARARGS, NULL },
1037         { "_dsdb_get_systemFlags_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_systemFlags_from_lDAPDisplayName,
1038                 METH_VARARGS, NULL },
1039         { "_dsdb_get_linkId_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_linkId_from_lDAPDisplayName,
1040                 METH_VARARGS, NULL },
1041         { "_dsdb_get_lDAPDisplayName_by_attid", (PyCFunction)py_dsdb_get_lDAPDisplayName_by_attid,
1042                 METH_VARARGS, NULL },
1043         { "_dsdb_get_backlink_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_backlink_from_lDAPDisplayName,
1044                 METH_VARARGS, NULL },
1045         { "_dsdb_set_ntds_invocation_id",
1046                 (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
1047                 NULL },
1048         { "_samdb_ntds_objectGUID", (PyCFunction)py_samdb_ntds_objectGUID,
1049                 METH_VARARGS, "get the NTDS objectGUID as a string"},
1050         { "_dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema,
1051                 METH_VARARGS, NULL },
1052         { "_dsdb_load_partition_usn", (PyCFunction)py_dsdb_load_partition_usn,
1053                 METH_VARARGS,
1054                 "get uSNHighest and uSNUrgent from the partition @REPLCHANGED"},
1055         { "_dsdb_set_am_rodc",
1056                 (PyCFunction)py_dsdb_set_am_rodc, METH_VARARGS,
1057                 NULL },
1058         { "_am_rodc",
1059                 (PyCFunction)py_dsdb_am_rodc, METH_VARARGS,
1060                 NULL },
1061         { "_am_pdc",
1062                 (PyCFunction)py_dsdb_am_pdc, METH_VARARGS,
1063                 NULL },
1064         { "_dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
1065                 NULL },
1066         { "_dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
1067                 NULL },
1068         { "_dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
1069                 NULL },
1070         { "_dsdb_get_partitions_dn", (PyCFunction)py_dsdb_get_partitions_dn, METH_VARARGS, NULL },
1071         { "_dsdb_get_nc_root", (PyCFunction)py_dsdb_get_nc_root, METH_VARARGS, NULL },
1072         { "_dsdb_DsReplicaAttribute", (PyCFunction)py_dsdb_DsReplicaAttribute, METH_VARARGS, NULL },
1073         { "_dsdb_normalise_attributes", (PyCFunction)py_dsdb_normalise_attributes, METH_VARARGS, NULL },
1074         { NULL }
1075 };
1076
1077 void initdsdb(void)
1078 {
1079         PyObject *m;
1080
1081         m = Py_InitModule3("dsdb", py_dsdb_methods, 
1082                            "Python bindings for the directory service databases.");
1083         if (m == NULL)
1084                 return;
1085
1086 #define ADD_DSDB_FLAG(val)  PyModule_AddObject(m, #val, PyInt_FromLong(val))
1087
1088         /* "userAccountControl" flags */
1089         ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1090         ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1091         ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1092         ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1093         ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1094         ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1095         ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1096
1097         ADD_DSDB_FLAG(UF_SCRIPT);
1098         ADD_DSDB_FLAG(UF_ACCOUNTDISABLE);
1099         ADD_DSDB_FLAG(UF_00000004);
1100         ADD_DSDB_FLAG(UF_HOMEDIR_REQUIRED);
1101         ADD_DSDB_FLAG(UF_LOCKOUT);
1102         ADD_DSDB_FLAG(UF_PASSWD_NOTREQD);
1103         ADD_DSDB_FLAG(UF_PASSWD_CANT_CHANGE);
1104         ADD_DSDB_FLAG(UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED);
1105         ADD_DSDB_FLAG(UF_TEMP_DUPLICATE_ACCOUNT);
1106         ADD_DSDB_FLAG(UF_NORMAL_ACCOUNT);
1107         ADD_DSDB_FLAG(UF_00000400);
1108         ADD_DSDB_FLAG(UF_INTERDOMAIN_TRUST_ACCOUNT);
1109         ADD_DSDB_FLAG(UF_WORKSTATION_TRUST_ACCOUNT);
1110         ADD_DSDB_FLAG(UF_SERVER_TRUST_ACCOUNT);
1111         ADD_DSDB_FLAG(UF_00004000);
1112         ADD_DSDB_FLAG(UF_00008000);
1113         ADD_DSDB_FLAG(UF_DONT_EXPIRE_PASSWD);
1114         ADD_DSDB_FLAG(UF_MNS_LOGON_ACCOUNT);
1115         ADD_DSDB_FLAG(UF_SMARTCARD_REQUIRED);
1116         ADD_DSDB_FLAG(UF_TRUSTED_FOR_DELEGATION);
1117         ADD_DSDB_FLAG(UF_NOT_DELEGATED);
1118         ADD_DSDB_FLAG(UF_USE_DES_KEY_ONLY);
1119         ADD_DSDB_FLAG(UF_DONT_REQUIRE_PREAUTH);
1120         ADD_DSDB_FLAG(UF_PASSWORD_EXPIRED);
1121         ADD_DSDB_FLAG(UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION);
1122         ADD_DSDB_FLAG(UF_NO_AUTH_DATA_REQUIRED);
1123         ADD_DSDB_FLAG(UF_PARTIAL_SECRETS_ACCOUNT);
1124
1125         /* groupType flags */
1126         ADD_DSDB_FLAG(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP);
1127         ADD_DSDB_FLAG(GTYPE_SECURITY_GLOBAL_GROUP);
1128         ADD_DSDB_FLAG(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
1129         ADD_DSDB_FLAG(GTYPE_SECURITY_UNIVERSAL_GROUP);
1130         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_GLOBAL_GROUP);
1131         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP);
1132         ADD_DSDB_FLAG(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1133
1134         /* "sAMAccountType" flags */
1135         ADD_DSDB_FLAG(ATYPE_NORMAL_ACCOUNT);
1136         ADD_DSDB_FLAG(ATYPE_WORKSTATION_TRUST);
1137         ADD_DSDB_FLAG(ATYPE_INTERDOMAIN_TRUST);
1138         ADD_DSDB_FLAG(ATYPE_SECURITY_GLOBAL_GROUP);
1139         ADD_DSDB_FLAG(ATYPE_SECURITY_LOCAL_GROUP);
1140         ADD_DSDB_FLAG(ATYPE_SECURITY_UNIVERSAL_GROUP);
1141         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_GLOBAL_GROUP);
1142         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_LOCAL_GROUP);
1143         ADD_DSDB_FLAG(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP);
1144
1145         /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
1146         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2000);
1147         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003_MIXED);
1148         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2003);
1149         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008);
1150         ADD_DSDB_FLAG(DS_DOMAIN_FUNCTION_2008_R2);
1151
1152         /* nc replica flags */
1153         ADD_DSDB_FLAG(INSTANCE_TYPE_IS_NC_HEAD);
1154         ADD_DSDB_FLAG(INSTANCE_TYPE_UNINSTANT);
1155         ADD_DSDB_FLAG(INSTANCE_TYPE_WRITE);
1156         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_ABOVE);
1157         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_COMING);
1158         ADD_DSDB_FLAG(INSTANCE_TYPE_NC_GOING);
1159
1160         /* "systemFlags" */
1161         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NC);
1162         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_DOMAIN);
1163         ADD_DSDB_FLAG(SYSTEM_FLAG_CR_NTDS_NOT_GC_REPLICATED);
1164         ADD_DSDB_FLAG(SYSTEM_FLAG_SCHEMA_BASE_OBJECT);
1165         ADD_DSDB_FLAG(SYSTEM_FLAG_ATTR_IS_RDN);
1166         ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
1167         ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE);
1168         ADD_DSDB_FLAG(SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME);
1169         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
1170         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_MOVE);
1171         ADD_DSDB_FLAG(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
1172         ADD_DSDB_FLAG(SYSTEM_FLAG_DISALLOW_DELETE);
1173
1174         /* Kerberos encryption type constants */
1175         ADD_DSDB_FLAG(ENC_ALL_TYPES);
1176         ADD_DSDB_FLAG(ENC_CRC32);
1177         ADD_DSDB_FLAG(ENC_RSA_MD5);
1178         ADD_DSDB_FLAG(ENC_RC4_HMAC_MD5);
1179         ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES128);
1180         ADD_DSDB_FLAG(ENC_HMAC_SHA1_96_AES256);
1181
1182         ADD_DSDB_FLAG(SEARCH_FLAG_ATTINDEX);
1183         ADD_DSDB_FLAG(SEARCH_FLAG_PDNTATTINDEX);
1184         ADD_DSDB_FLAG(SEARCH_FLAG_ANR);
1185         ADD_DSDB_FLAG(SEARCH_FLAG_PRESERVEONDELETE);
1186         ADD_DSDB_FLAG(SEARCH_FLAG_COPY);
1187         ADD_DSDB_FLAG(SEARCH_FLAG_TUPLEINDEX);
1188         ADD_DSDB_FLAG(SEARCH_FLAG_SUBTREEATTRINDEX);
1189         ADD_DSDB_FLAG(SEARCH_FLAG_CONFIDENTIAL);
1190         ADD_DSDB_FLAG(SEARCH_FLAG_NEVERVALUEAUDIT);
1191         ADD_DSDB_FLAG(SEARCH_FLAG_RODC_ATTRIBUTE);
1192
1193         ADD_DSDB_FLAG(DS_FLAG_ATTR_NOT_REPLICATED);
1194         ADD_DSDB_FLAG(DS_FLAG_ATTR_REQ_PARTIAL_SET_MEMBER);
1195         ADD_DSDB_FLAG(DS_FLAG_ATTR_IS_CONSTRUCTED);
1196
1197         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_AUTO_TOPOLOGY_DISABLED);
1198         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_CLEANUP_DISABLED);
1199         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_MIN_HOPS_DISABLED);
1200         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_TOPL_DETECT_STALE_DISABLED);
1201         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_INTER_SITE_AUTO_TOPOLOGY_DISABLED);
1202         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_GROUP_CACHING_ENABLED);
1203         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_FORCE_KCC_WHISTLER_BEHAVIOR);
1204         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_RAND_BH_SELECTION_DISABLED);
1205         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_SCHEDULE_HASHING_ENABLED);
1206         ADD_DSDB_FLAG(DS_NTDSSETTINGS_OPT_IS_REDUNDANT_SERVER_TOPOLOGY_ENABLED);
1207
1208         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_IS_GC);
1209         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL);
1210         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL);
1211         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_NTDSCONN_XLATE);
1212         ADD_DSDB_FLAG(DS_NTDSDSA_OPT_DISABLE_SPN_REGISTRATION);
1213
1214         ADD_DSDB_FLAG(NTDSCONN_KCC_GC_TOPOLOGY);
1215         ADD_DSDB_FLAG(NTDSCONN_KCC_RING_TOPOLOGY);
1216         ADD_DSDB_FLAG(NTDSCONN_KCC_MINIMIZE_HOPS_TOPOLOGY);
1217         ADD_DSDB_FLAG(NTDSCONN_KCC_STALE_SERVERS_TOPOLOGY);
1218         ADD_DSDB_FLAG(NTDSCONN_KCC_OSCILLATING_CONNECTION_TOPOLOGY);
1219         ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_GC_TOPOLOGY);
1220         ADD_DSDB_FLAG(NTDSCONN_KCC_INTERSITE_TOPOLOGY);
1221         ADD_DSDB_FLAG(NTDSCONN_KCC_SERVER_FAILOVER_TOPOLOGY);
1222         ADD_DSDB_FLAG(NTDSCONN_KCC_SITE_FAILOVER_TOPOLOGY);
1223         ADD_DSDB_FLAG(NTDSCONN_KCC_REDUNDANT_SERVER_TOPOLOGY);
1224
1225         ADD_DSDB_FLAG(NTDSCONN_OPT_IS_GENERATED);
1226         ADD_DSDB_FLAG(NTDSCONN_OPT_TWOWAY_SYNC);
1227         ADD_DSDB_FLAG(NTDSCONN_OPT_OVERRIDE_NOTIFY_DEFAULT);
1228         ADD_DSDB_FLAG(NTDSCONN_OPT_USE_NOTIFY);
1229         ADD_DSDB_FLAG(NTDSCONN_OPT_DISABLE_INTERSITE_COMPRESSION);
1230         ADD_DSDB_FLAG(NTDSCONN_OPT_USER_OWNED_SCHEDULE);
1231         ADD_DSDB_FLAG(NTDSCONN_OPT_RODC_TOPOLOGY);
1232
1233         /* Site Link Object options */
1234         ADD_DSDB_FLAG(NTDSSITELINK_OPT_USE_NOTIFY);
1235         ADD_DSDB_FLAG(NTDSSITELINK_OPT_TWOWAY_SYNC);
1236         ADD_DSDB_FLAG(NTDSSITELINK_OPT_DISABLE_COMPRESSION);
1237
1238         /* GPO policy flags */
1239         ADD_DSDB_FLAG(GPLINK_OPT_DISABLE);
1240         ADD_DSDB_FLAG(GPLINK_OPT_ENFORCE);
1241         ADD_DSDB_FLAG(GPO_FLAG_USER_DISABLE);
1242         ADD_DSDB_FLAG(GPO_FLAG_MACHINE_DISABLE);
1243         ADD_DSDB_FLAG(GPO_INHERIT);
1244         ADD_DSDB_FLAG(GPO_BLOCK_INHERITANCE);
1245
1246 #define ADD_DSDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(val))
1247
1248         ADD_DSDB_STRING(DSDB_SYNTAX_BINARY_DN);
1249         ADD_DSDB_STRING(DSDB_SYNTAX_STRING_DN);
1250         ADD_DSDB_STRING(DSDB_SYNTAX_OR_NAME);
1251         ADD_DSDB_STRING(DSDB_CONTROL_DBCHECK);
1252 }