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_global_schema(PyObject *self, PyObject *args)
210 struct ldb_context *ldb;
212 if (!PyArg_ParseTuple(args, "O", &py_ldb))
215 PyErr_LDB_OR_RAISE(py_ldb, ldb);
217 ret = dsdb_set_global_schema(ldb);
218 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
223 static PyObject *py_dsdb_attach_schema_from_ldif(PyObject *self, PyObject *args)
228 struct ldb_context *ldb;
230 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
233 PyErr_LDB_OR_RAISE(py_ldb, ldb);
235 result = dsdb_attach_schema_from_ldif(ldb, pf, df);
236 PyErr_WERROR_IS_ERR_RAISE(result);
241 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
243 char *target_str, *mapping;
245 struct ldb_context *ldb;
249 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
252 PyErr_LDB_OR_RAISE(py_ldb, ldb);
254 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
256 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
259 ret = PyString_FromString(retstr);
264 static PyMethodDef py_misc_methods[] = {
265 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
266 "random_password(len) -> string\n"
267 "Generate random password with specified length." },
268 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
269 "unix2nttime(timestamp) -> nttime" },
270 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
271 "ldb_set_credentials(ldb, credentials) -> None\n"
272 "Set credentials to use when connecting." },
273 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
274 "ldb_set_session_info(ldb, session_info)\n"
275 "Set session info to use when connecting." },
276 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
277 "ldb_set_loadparm(ldb, session_info)\n"
278 "Set loadparm context to use when connecting." },
279 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
280 "samdb_set_domain_sid(samdb, sid)\n"
281 "Set SID of domain to use." },
282 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
283 "ldb_register_samba_handlers(ldb)\n"
284 "Register Samba-specific LDB modules and schemas." },
285 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
287 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
289 { "dsdb_attach_schema_from_ldif", (PyCFunction)py_dsdb_attach_schema_from_ldif, METH_VARARGS,
291 { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
300 m = Py_InitModule3("glue", py_misc_methods,
301 "Python bindings for miscellaneous Samba functions.");
305 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));