2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
4 Copyright (C) Matthias Dieter Wallnöfer 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libcli/util/pyerrors.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/pyldb.h"
25 #include "libcli/security/security.h"
26 #include "librpc/ndr/libndr.h"
28 /* FIXME: These should be in a header file somewhere, once we finish moving
29 * away from SWIG .. */
30 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
31 /* if (!PyLdb_Check(py_ldb)) { \
32 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
35 ldb = PyLdb_AsLdbContext(py_ldb);
37 static PyObject
*py_ldb_get_exception(void)
39 PyObject
*mod
= PyImport_ImportModule("ldb");
43 return PyObject_GetAttrString(mod
, "LdbError");
46 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
48 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
49 return; /* Python exception should already be set, just keep that */
51 PyErr_SetObject(error
,
52 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
53 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
56 static PyObject
*py_samdb_server_site_name(PyObject
*self
, PyObject
*args
)
58 PyObject
*py_ldb
, *result
;
59 struct ldb_context
*ldb
;
63 if (!PyArg_ParseTuple(args
, "O", &py_ldb
))
66 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
68 mem_ctx
= talloc_new(NULL
);
70 site
= samdb_server_site_name(ldb
, mem_ctx
);
72 PyErr_SetString(PyExc_RuntimeError
, "Failed to find server site");
77 result
= PyString_FromString(site
);
82 static PyObject
*py_dsdb_convert_schema_to_openldap(PyObject
*self
,
85 char *target_str
, *mapping
;
87 struct ldb_context
*ldb
;
91 if (!PyArg_ParseTuple(args
, "Oss", &py_ldb
, &target_str
, &mapping
))
94 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
96 retstr
= dsdb_convert_schema_to_openldap(ldb
, target_str
, mapping
);
98 PyErr_SetString(PyExc_RuntimeError
,
99 "dsdb_convert_schema_to_openldap failed");
103 ret
= PyString_FromString(retstr
);
108 static PyObject
*py_samdb_set_domain_sid(PyLdbObject
*self
, PyObject
*args
)
110 PyObject
*py_ldb
, *py_sid
;
111 struct ldb_context
*ldb
;
115 if (!PyArg_ParseTuple(args
, "OO", &py_ldb
, &py_sid
))
118 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
120 sid
= dom_sid_parse_talloc(NULL
, PyString_AsString(py_sid
));
122 ret
= samdb_set_domain_sid(ldb
, sid
);
124 PyErr_SetString(PyExc_RuntimeError
, "set_domain_sid failed");
130 static PyObject
*py_samdb_set_ntds_settings_dn(PyLdbObject
*self
, PyObject
*args
)
132 PyObject
*py_ldb
, *py_ntds_settings_dn
;
133 struct ldb_context
*ldb
;
134 struct ldb_dn
*ntds_settings_dn
;
138 if (!PyArg_ParseTuple(args
, "OO", &py_ldb
, &py_ntds_settings_dn
))
141 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
143 tmp_ctx
= talloc_new(NULL
);
144 if (tmp_ctx
== NULL
) {
149 if (!PyObject_AsDn(tmp_ctx
, py_ntds_settings_dn
, ldb
, &ntds_settings_dn
)) {
153 ret
= samdb_set_ntds_settings_dn(ldb
, ntds_settings_dn
);
154 talloc_free(tmp_ctx
);
156 PyErr_SetString(PyExc_RuntimeError
, "set_ntds_settings_dn failed");
162 static PyObject
*py_samdb_get_domain_sid(PyLdbObject
*self
, PyObject
*args
)
165 struct ldb_context
*ldb
;
166 const struct dom_sid
*sid
;
170 if (!PyArg_ParseTuple(args
, "O", &py_ldb
))
173 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
175 sid
= samdb_domain_sid(ldb
);
177 PyErr_SetString(PyExc_RuntimeError
, "samdb_domain_sid failed");
180 retstr
= dom_sid_string(NULL
, sid
);
181 ret
= PyString_FromString(retstr
);
186 static PyObject
*py_samdb_ntds_invocation_id(PyObject
*self
, PyObject
*args
)
188 PyObject
*py_ldb
, *result
;
189 struct ldb_context
*ldb
;
191 const struct GUID
*guid
;
193 mem_ctx
= talloc_new(NULL
);
194 if (mem_ctx
== NULL
) {
199 if (!PyArg_ParseTuple(args
, "O", &py_ldb
)) {
200 talloc_free(mem_ctx
);
204 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
206 guid
= samdb_ntds_invocation_id(ldb
);
208 PyErr_SetString(PyExc_RuntimeError
,
209 "Failed to find NTDS invocation ID");
210 talloc_free(mem_ctx
);
214 result
= PyString_FromString(GUID_string(mem_ctx
, guid
));
215 talloc_free(mem_ctx
);
219 static PyObject
*py_dsdb_get_oid_from_attid(PyObject
*self
, PyObject
*args
)
222 struct ldb_context
*ldb
;
224 struct dsdb_schema
*schema
;
230 if (!PyArg_ParseTuple(args
, "Oi", &py_ldb
, &attid
))
233 mem_ctx
= talloc_new(NULL
);
234 if (mem_ctx
== NULL
) {
239 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
241 schema
= dsdb_get_schema(ldb
, NULL
);
244 PyErr_SetString(PyExc_RuntimeError
, "Failed to find a schema from ldb \n");
245 talloc_free(mem_ctx
);
249 status
= dsdb_schema_pfm_oid_from_attid(schema
->prefixmap
, attid
,
251 PyErr_WERROR_IS_ERR_RAISE(status
);
253 ret
= PyString_FromString(oid
);
255 talloc_free(mem_ctx
);
260 static PyObject
*py_dsdb_set_ntds_invocation_id(PyObject
*self
, PyObject
*args
)
262 PyObject
*py_ldb
, *py_guid
;
265 struct ldb_context
*ldb
;
266 if (!PyArg_ParseTuple(args
, "OO", &py_ldb
, &py_guid
))
269 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
270 GUID_from_string(PyString_AsString(py_guid
), &guid
);
272 ret
= samdb_set_ntds_invocation_id(ldb
, &guid
);
274 PyErr_SetString(PyExc_RuntimeError
, "set_ntds_invocation_id failed");
280 static PyObject
*py_samdb_ntds_objectGUID(PyObject
*self
, PyObject
*args
)
282 PyObject
*py_ldb
, *result
;
283 struct ldb_context
*ldb
;
285 const struct GUID
*guid
;
287 mem_ctx
= talloc_new(NULL
);
288 if (mem_ctx
== NULL
) {
293 if (!PyArg_ParseTuple(args
, "O", &py_ldb
)) {
294 talloc_free(mem_ctx
);
298 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
300 guid
= samdb_ntds_objectGUID(ldb
);
302 PyErr_SetString(PyExc_RuntimeError
, "Failed to find NTDS GUID");
303 talloc_free(mem_ctx
);
307 result
= PyString_FromString(GUID_string(mem_ctx
, guid
));
308 talloc_free(mem_ctx
);
312 static PyObject
*py_dsdb_set_global_schema(PyObject
*self
, PyObject
*args
)
315 struct ldb_context
*ldb
;
317 if (!PyArg_ParseTuple(args
, "O", &py_ldb
))
320 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
322 ret
= dsdb_set_global_schema(ldb
);
323 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret
, ldb
);
328 static PyObject
*py_dsdb_load_partition_usn(PyObject
*self
, PyObject
*args
)
330 PyObject
*py_dn
, *py_ldb
, *result
;
332 uint64_t highest_uSN
, urgent_uSN
;
333 struct ldb_context
*ldb
;
337 mem_ctx
= talloc_new(NULL
);
338 if (mem_ctx
== NULL
) {
343 if (!PyArg_ParseTuple(args
, "OO", &py_ldb
, &py_dn
)) {
344 talloc_free(mem_ctx
);
348 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
350 if (!PyObject_AsDn(mem_ctx
, py_dn
, ldb
, &dn
)) {
351 talloc_free(mem_ctx
);
355 ret
= dsdb_load_partition_usn(ldb
, dn
, &highest_uSN
, &urgent_uSN
);
356 if (ret
!= LDB_SUCCESS
) {
357 char *errstr
= talloc_asprintf(mem_ctx
, "Failed to load partition uSN - %s", ldb_errstring(ldb
));
358 PyErr_SetString(PyExc_RuntimeError
, errstr
);
359 talloc_free(mem_ctx
);
363 talloc_free(mem_ctx
);
365 result
= PyDict_New();
367 PyDict_SetItemString(result
, "uSNHighest", PyInt_FromLong((uint64_t)highest_uSN
));
368 PyDict_SetItemString(result
, "uSNUrgent", PyInt_FromLong((uint64_t)urgent_uSN
));
374 static PyObject
*py_dsdb_set_am_rodc(PyObject
*self
, PyObject
*args
)
378 struct ldb_context
*ldb
;
381 if (!PyArg_ParseTuple(args
, "Oi", &py_ldb
, &py_val
))
384 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
385 ret
= samdb_set_am_rodc(ldb
, (bool)py_val
);
387 PyErr_SetString(PyExc_RuntimeError
, "set_am_rodc failed");
393 static PyMethodDef py_dsdb_methods
[] = {
394 { "samdb_server_site_name", (PyCFunction
)py_samdb_server_site_name
,
395 METH_VARARGS
, "Get the server site name as a string"},
396 { "dsdb_convert_schema_to_openldap",
397 (PyCFunction
)py_dsdb_convert_schema_to_openldap
, METH_VARARGS
,
398 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
399 "Create an OpenLDAP schema from a schema." },
400 { "samdb_set_domain_sid", (PyCFunction
)py_samdb_set_domain_sid
,
402 "samdb_set_domain_sid(samdb, sid)\n"
403 "Set SID of domain to use." },
404 { "samdb_get_domain_sid", (PyCFunction
)py_samdb_get_domain_sid
,
406 "samdb_get_domain_sid(samdb)\n"
407 "Get SID of domain in use." },
408 { "samdb_ntds_invocation_id", (PyCFunction
)py_samdb_ntds_invocation_id
,
409 METH_VARARGS
, "get the NTDS invocation ID GUID as a string"},
410 { "samdb_set_ntds_settings_dn", (PyCFunction
)py_samdb_set_ntds_settings_dn
,
412 "samdb_set_ntds_settings_dn(samdb, ntds_settings_dn)\n"
413 "Set NTDS Settings DN for this LDB (allows it to be set before the DB fully exists)." },
414 { "dsdb_get_oid_from_attid", (PyCFunction
)py_dsdb_get_oid_from_attid
,
415 METH_VARARGS
, NULL
},
416 { "dsdb_set_ntds_invocation_id",
417 (PyCFunction
)py_dsdb_set_ntds_invocation_id
, METH_VARARGS
,
419 { "samdb_ntds_objectGUID", (PyCFunction
)py_samdb_ntds_objectGUID
,
420 METH_VARARGS
, "get the NTDS objectGUID as a string"},
421 { "dsdb_set_global_schema", (PyCFunction
)py_dsdb_set_global_schema
,
422 METH_VARARGS
, NULL
},
423 { "dsdb_load_partition_usn", (PyCFunction
)py_dsdb_load_partition_usn
,
425 "get uSNHighest and uSNUrgent from the partition @REPLCHANGED"},
426 { "dsdb_set_am_rodc",
427 (PyCFunction
)py_dsdb_set_am_rodc
, METH_VARARGS
,
436 m
= Py_InitModule3("dsdb", py_dsdb_methods
,
437 "Python bindings for the directory service databases.");
441 /* "userAccountControl" flags */
442 PyModule_AddObject(m
, "UF_NORMAL_ACCOUNT",
443 PyInt_FromLong(UF_NORMAL_ACCOUNT
));
444 PyModule_AddObject(m
, "UF_TEMP_DUPLICATE_ACCOUNT",
445 PyInt_FromLong(UF_TEMP_DUPLICATE_ACCOUNT
));
446 PyModule_AddObject(m
, "UF_SERVER_TRUST_ACCOUNT",
447 PyInt_FromLong(UF_SERVER_TRUST_ACCOUNT
));
448 PyModule_AddObject(m
, "UF_WORKSTATION_TRUST_ACCOUNT",
449 PyInt_FromLong(UF_WORKSTATION_TRUST_ACCOUNT
));
450 PyModule_AddObject(m
, "UF_INTERDOMAIN_TRUST_ACCOUNT",
451 PyInt_FromLong(UF_INTERDOMAIN_TRUST_ACCOUNT
));
452 PyModule_AddObject(m
, "UF_PASSWD_NOTREQD",
453 PyInt_FromLong(UF_PASSWD_NOTREQD
));
454 PyModule_AddObject(m
, "UF_ACCOUNTDISABLE",
455 PyInt_FromLong(UF_ACCOUNTDISABLE
));
457 /* "groupType" flags */
458 PyModule_AddObject(m
, "GTYPE_SECURITY_BUILTIN_LOCAL_GROUP",
459 PyInt_FromLong(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
));
460 PyModule_AddObject(m
, "GTYPE_SECURITY_GLOBAL_GROUP",
461 PyInt_FromLong(GTYPE_SECURITY_GLOBAL_GROUP
));
462 PyModule_AddObject(m
, "GTYPE_SECURITY_DOMAIN_LOCAL_GROUP",
463 PyInt_FromLong(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
));
464 PyModule_AddObject(m
, "GTYPE_SECURITY_UNIVERSAL_GROUP",
465 PyInt_FromLong(GTYPE_SECURITY_UNIVERSAL_GROUP
));
466 PyModule_AddObject(m
, "GTYPE_DISTRIBUTION_GLOBAL_GROUP",
467 PyInt_FromLong(GTYPE_DISTRIBUTION_GLOBAL_GROUP
));
468 PyModule_AddObject(m
, "GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP",
469 PyInt_FromLong(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
));
470 PyModule_AddObject(m
, "GTYPE_DISTRIBUTION_UNIVERSAL_GROUP",
471 PyInt_FromLong(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
));
473 /* "sAMAccountType" flags */
474 PyModule_AddObject(m
, "ATYPE_NORMAL_ACCOUNT",
475 PyInt_FromLong(ATYPE_NORMAL_ACCOUNT
));
476 PyModule_AddObject(m
, "ATYPE_WORKSTATION_TRUST",
477 PyInt_FromLong(ATYPE_WORKSTATION_TRUST
));
478 PyModule_AddObject(m
, "ATYPE_INTERDOMAIN_TRUST",
479 PyInt_FromLong(ATYPE_INTERDOMAIN_TRUST
));
480 PyModule_AddObject(m
, "ATYPE_SECURITY_GLOBAL_GROUP",
481 PyInt_FromLong(ATYPE_SECURITY_GLOBAL_GROUP
));
482 PyModule_AddObject(m
, "ATYPE_SECURITY_LOCAL_GROUP",
483 PyInt_FromLong(ATYPE_SECURITY_LOCAL_GROUP
));
484 PyModule_AddObject(m
, "ATYPE_SECURITY_UNIVERSAL_GROUP",
485 PyInt_FromLong(ATYPE_SECURITY_UNIVERSAL_GROUP
));
486 PyModule_AddObject(m
, "ATYPE_DISTRIBUTION_GLOBAL_GROUP",
487 PyInt_FromLong(ATYPE_DISTRIBUTION_GLOBAL_GROUP
));
488 PyModule_AddObject(m
, "ATYPE_DISTRIBUTION_LOCAL_GROUP",
489 PyInt_FromLong(ATYPE_DISTRIBUTION_LOCAL_GROUP
));
490 PyModule_AddObject(m
, "ATYPE_DISTRIBUTION_UNIVERSAL_GROUP",
491 PyInt_FromLong(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP
));
493 /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
494 PyModule_AddObject(m
, "DS_DOMAIN_FUNCTION_2000",
495 PyInt_FromLong(DS_DOMAIN_FUNCTION_2000
));
496 PyModule_AddObject(m
, "DS_DOMAIN_FUNCTION_2003_MIXED",
497 PyInt_FromLong(DS_DOMAIN_FUNCTION_2003_MIXED
));
498 PyModule_AddObject(m
, "DS_DOMAIN_FUNCTION_2003",
499 PyInt_FromLong(DS_DOMAIN_FUNCTION_2003
));
500 PyModule_AddObject(m
, "DS_DOMAIN_FUNCTION_2008",
501 PyInt_FromLong(DS_DOMAIN_FUNCTION_2008
));
502 PyModule_AddObject(m
, "DS_DOMAIN_FUNCTION_2008_R2",
503 PyInt_FromLong(DS_DOMAIN_FUNCTION_2008_R2
));
505 /* "domainControllerFunctionality" flags in the rootDSE */
506 PyModule_AddObject(m
, "DS_DC_FUNCTION_2000",
507 PyInt_FromLong(DS_DC_FUNCTION_2000
));
508 PyModule_AddObject(m
, "DS_DC_FUNCTION_2003",
509 PyInt_FromLong(DS_DC_FUNCTION_2003
));
510 PyModule_AddObject(m
, "DS_DC_FUNCTION_2008",
511 PyInt_FromLong(DS_DC_FUNCTION_2008
));
512 PyModule_AddObject(m
, "DS_DC_FUNCTION_2008_R2",
513 PyInt_FromLong(DS_DC_FUNCTION_2008_R2
));