2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "ldb_errors.h"
22 #include "param/param.h"
23 #include "auth/credentials/credentials.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb-samba/ldif_handlers.h"
26 #include "librpc/ndr/libndr.h"
29 #include "lib/ldb/pyldb.h"
30 #include "libcli/util/pyerrors.h"
31 #include "libcli/security/security.h"
32 #include "auth/pyauth.h"
33 #include "param/pyparam.h"
34 #include "auth/credentials/pycredentials.h"
36 #ifndef Py_RETURN_NONE
37 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
40 /* FIXME: These should be in a header file somewhere, once we finish moving
41 * away from SWIG .. */
42 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
43 /* if (!PyLdb_Check(py_ldb)) { \
44 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
47 ldb = PyLdb_AsLdbContext(py_ldb);
49 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
51 if (ret == LDB_ERR_PYTHON_EXCEPTION)
52 return; /* Python exception should already be set, just keep that */
54 PyErr_SetObject(error,
55 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
56 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
59 static PyObject *py_ldb_get_exception(void)
61 PyObject *mod = PyImport_ImportModule("ldb");
65 return PyObject_GetAttrString(mod, "LdbError");
68 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
73 if (!PyArg_ParseTuple(args, "i", &len))
76 retstr = generate_random_str(NULL, len);
77 ret = PyString_FromString(retstr);
82 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
86 if (!PyArg_ParseTuple(args, "I", &t))
89 unix_to_nt_time(&nt, t);
91 return PyInt_FromLong((uint64_t)nt);
94 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
96 PyObject *py_creds, *py_ldb;
97 struct cli_credentials *creds;
98 struct ldb_context *ldb;
99 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
102 PyErr_LDB_OR_RAISE(py_ldb, ldb);
104 creds = cli_credentials_from_py_object(py_creds);
106 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
110 ldb_set_opaque(ldb, "credentials", creds);
115 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
117 PyObject *py_lp_ctx, *py_ldb;
118 struct loadparm_context *lp_ctx;
119 struct ldb_context *ldb;
120 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
123 PyErr_LDB_OR_RAISE(py_ldb, ldb);
125 lp_ctx = lp_from_py_object(py_lp_ctx);
126 if (lp_ctx == NULL) {
127 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
131 ldb_set_opaque(ldb, "loadparm", lp_ctx);
137 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
139 PyObject *py_session_info, *py_ldb;
140 struct auth_session_info *info;
141 struct ldb_context *ldb;
142 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
145 PyErr_LDB_OR_RAISE(py_ldb, ldb);
146 /*if (!PyAuthSession_Check(py_session_info)) {
147 PyErr_SetString(PyExc_TypeError, "Expected session info object");
151 info = PyAuthSession_AsSession(py_session_info);
153 ldb_set_opaque(ldb, "sessionInfo", info);
158 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
160 PyObject *py_ldb, *py_sid;
161 struct ldb_context *ldb;
165 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
168 PyErr_LDB_OR_RAISE(py_ldb, ldb);
170 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
172 ret = samdb_set_domain_sid(ldb, sid);
174 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
180 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
183 struct ldb_context *ldb;
186 if (!PyArg_ParseTuple(args, "O", &py_ldb))
189 PyErr_LDB_OR_RAISE(py_ldb, ldb);
190 ret = ldb_register_samba_handlers(ldb);
192 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
196 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
198 PyObject *py_ldb, *py_guid;
201 struct ldb_context *ldb;
202 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
205 PyErr_LDB_OR_RAISE(py_ldb, ldb);
206 GUID_from_string(PyString_AsString(py_guid), &guid);
208 ret = samdb_set_ntds_invocation_id(ldb, &guid);
210 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
216 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
220 int *old_val, *new_val;
221 char *py_opaque_name, *opaque_name_talloc;
222 struct ldb_context *ldb;
225 if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
228 PyErr_LDB_OR_RAISE(py_ldb, ldb);
230 /* see if we have a cached copy */
231 old_val = (int *)ldb_get_opaque(ldb,
239 tmp_ctx = talloc_new(ldb);
240 if (tmp_ctx == NULL) {
244 new_val = talloc(tmp_ctx, int);
249 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
250 if (!opaque_name_talloc) {
256 /* cache the domain_sid in the ldb */
257 if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
261 talloc_steal(ldb, new_val);
262 talloc_steal(ldb, opaque_name_talloc);
263 talloc_free(tmp_ctx);
268 talloc_free(tmp_ctx);
269 PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb!\n");
273 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
276 struct ldb_context *ldb;
278 if (!PyArg_ParseTuple(args, "O", &py_ldb))
281 PyErr_LDB_OR_RAISE(py_ldb, ldb);
283 ret = dsdb_set_global_schema(ldb);
284 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
289 static PyObject *py_dsdb_attach_schema_from_ldif(PyObject *self, PyObject *args)
294 struct ldb_context *ldb;
296 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
299 PyErr_LDB_OR_RAISE(py_ldb, ldb);
301 result = dsdb_attach_schema_from_ldif(ldb, pf, df);
302 PyErr_WERROR_IS_ERR_RAISE(result);
307 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
309 char *target_str, *mapping;
311 struct ldb_context *ldb;
315 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
318 PyErr_LDB_OR_RAISE(py_ldb, ldb);
320 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
322 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
325 ret = PyString_FromString(retstr);
330 static PyObject *py_dom_sid_to_rid(PyLdbObject *self, PyObject *args)
337 if(!PyArg_ParseTuple(args, "O", &py_sid))
340 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
342 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
343 if (!NT_STATUS_IS_OK(status)) {
344 PyErr_SetString(PyExc_RuntimeError, "dom_sid_split_rid failed");
348 return PyInt_FromLong(rid);
351 static PyMethodDef py_misc_methods[] = {
352 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
353 "random_password(len) -> string\n"
354 "Generate random password with specified length." },
355 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
356 "unix2nttime(timestamp) -> nttime" },
357 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
358 "ldb_set_credentials(ldb, credentials) -> None\n"
359 "Set credentials to use when connecting." },
360 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
361 "ldb_set_session_info(ldb, session_info)\n"
362 "Set session info to use when connecting." },
363 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
364 "ldb_set_loadparm(ldb, session_info)\n"
365 "Set loadparm context to use when connecting." },
366 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
367 "samdb_set_domain_sid(samdb, sid)\n"
368 "Set SID of domain to use." },
369 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
370 "ldb_register_samba_handlers(ldb)\n"
371 "Register Samba-specific LDB modules and schemas." },
372 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
374 { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer, METH_VARARGS,
376 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
378 { "dsdb_attach_schema_from_ldif", (PyCFunction)py_dsdb_attach_schema_from_ldif, METH_VARARGS,
380 { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
382 { "dom_sid_to_rid", (PyCFunction)py_dom_sid_to_rid, METH_VARARGS,
391 m = Py_InitModule3("glue", py_misc_methods,
392 "Python bindings for miscellaneous Samba functions.");
396 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
398 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2000", PyInt_FromLong(DS_BEHAVIOR_WIN2000));
399 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003_INTERIM", PyInt_FromLong(DS_BEHAVIOR_WIN2003_INTERIM));
400 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003", PyInt_FromLong(DS_BEHAVIOR_WIN2003));
401 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2008", PyInt_FromLong(DS_BEHAVIOR_WIN2008));