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