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