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);
50 static PyObject *py_ldb_get_exception(void)
52 PyObject *mod = PyImport_ImportModule("ldb");
56 return PyObject_GetAttrString(mod, "LdbError");
59 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
64 if (!PyArg_ParseTuple(args, "i", &len))
67 retstr = generate_random_str(NULL, len);
68 ret = PyString_FromString(retstr);
73 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
77 if (!PyArg_ParseTuple(args, "I", &t))
80 unix_to_nt_time(&nt, t);
82 return PyInt_FromLong((uint64_t)nt);
85 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
87 PyObject *py_creds, *py_ldb;
88 struct cli_credentials *creds;
89 struct ldb_context *ldb;
90 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
93 PyErr_LDB_OR_RAISE(py_ldb, ldb);
95 creds = cli_credentials_from_py_object(py_creds);
97 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
101 ldb_set_opaque(ldb, "credentials", creds);
106 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
108 PyObject *py_lp_ctx, *py_ldb;
109 struct loadparm_context *lp_ctx;
110 struct ldb_context *ldb;
111 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
114 PyErr_LDB_OR_RAISE(py_ldb, ldb);
116 lp_ctx = lp_from_py_object(py_lp_ctx);
117 if (lp_ctx == NULL) {
118 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
122 ldb_set_opaque(ldb, "loadparm", lp_ctx);
128 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
130 PyObject *py_session_info, *py_ldb;
131 struct auth_session_info *info;
132 struct ldb_context *ldb;
133 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
136 PyErr_LDB_OR_RAISE(py_ldb, ldb);
137 /*if (!PyAuthSession_Check(py_session_info)) {
138 PyErr_SetString(PyExc_TypeError, "Expected session info object");
142 info = PyAuthSession_AsSession(py_session_info);
144 ldb_set_opaque(ldb, "sessionInfo", info);
149 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
151 PyObject *py_ldb, *py_sid;
152 struct ldb_context *ldb;
156 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
159 PyErr_LDB_OR_RAISE(py_ldb, ldb);
161 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
163 ret = samdb_set_domain_sid(ldb, sid);
165 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
171 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
174 struct ldb_context *ldb;
177 if (!PyArg_ParseTuple(args, "O", &py_ldb))
180 PyErr_LDB_OR_RAISE(py_ldb, ldb);
181 ret = ldb_register_samba_handlers(ldb);
183 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
187 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
189 PyObject *py_ldb, *py_guid;
192 struct ldb_context *ldb;
193 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
196 PyErr_LDB_OR_RAISE(py_ldb, ldb);
197 GUID_from_string(PyString_AsString(py_guid), &guid);
199 ret = samdb_set_ntds_invocation_id(ldb, &guid);
201 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
207 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
211 int *old_val, *new_val;
212 char *py_opaque_name, *opaque_name_talloc;
213 struct ldb_context *ldb;
216 if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
219 PyErr_LDB_OR_RAISE(py_ldb, ldb);
221 /* see if we have a cached copy */
222 old_val = (int *)ldb_get_opaque(ldb,
230 tmp_ctx = talloc_new(ldb);
231 if (tmp_ctx == NULL) {
235 new_val = talloc(tmp_ctx, int);
240 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
241 if (!opaque_name_talloc) {
247 /* cache the domain_sid in the ldb */
248 if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
252 talloc_steal(ldb, new_val);
253 talloc_steal(ldb, opaque_name_talloc);
254 talloc_free(tmp_ctx);
259 talloc_free(tmp_ctx);
260 PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb!\n");
264 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
267 struct ldb_context *ldb;
269 if (!PyArg_ParseTuple(args, "O", &py_ldb))
272 PyErr_LDB_OR_RAISE(py_ldb, ldb);
274 ret = dsdb_set_global_schema(ldb);
275 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
280 static PyObject *py_dsdb_attach_schema_from_ldif(PyObject *self, PyObject *args)
285 struct ldb_context *ldb;
287 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
290 PyErr_LDB_OR_RAISE(py_ldb, ldb);
292 result = dsdb_attach_schema_from_ldif(ldb, pf, df);
293 PyErr_WERROR_IS_ERR_RAISE(result);
298 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
300 char *target_str, *mapping;
302 struct ldb_context *ldb;
306 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
309 PyErr_LDB_OR_RAISE(py_ldb, ldb);
311 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
313 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
316 ret = PyString_FromString(retstr);
321 static PyMethodDef py_misc_methods[] = {
322 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
323 "random_password(len) -> string\n"
324 "Generate random password with specified length." },
325 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
326 "unix2nttime(timestamp) -> nttime" },
327 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
328 "ldb_set_credentials(ldb, credentials) -> None\n"
329 "Set credentials to use when connecting." },
330 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
331 "ldb_set_session_info(ldb, session_info)\n"
332 "Set session info to use when connecting." },
333 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
334 "ldb_set_loadparm(ldb, session_info)\n"
335 "Set loadparm context to use when connecting." },
336 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
337 "samdb_set_domain_sid(samdb, sid)\n"
338 "Set SID of domain to use." },
339 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
340 "ldb_register_samba_handlers(ldb)\n"
341 "Register Samba-specific LDB modules and schemas." },
342 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
344 { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer, METH_VARARGS,
346 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
348 { "dsdb_attach_schema_from_ldif", (PyCFunction)py_dsdb_attach_schema_from_ldif, METH_VARARGS,
350 { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
359 m = Py_InitModule3("glue", py_misc_methods,
360 "Python bindings for miscellaneous Samba functions.");
364 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));