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 "param/param.h"
22 #include "auth/credentials/credentials.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb-samba/ldif_handlers.h"
25 #include "librpc/ndr/libndr.h"
29 #include "libcli/util/pyerrors.h"
30 #include "librpc/gen_ndr/py_misc.h"
31 #include "librpc/gen_ndr/py_security.h"
32 #include "auth/pyauth.h"
34 /* FIXME: These should be in a header file somewhere, once we finish moving
35 * away from SWIG .. */
36 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
37 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
39 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
40 if (!PyLdb_Check(py_ldb)) { \
41 /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
44 ldb = PyLdb_AsLdbContext(py_ldb);
47 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
52 if (!PyArg_ParseTuple(args, "i", &len))
55 retstr = generate_random_str(NULL, len);
56 ret = PyString_FromString(retstr);
61 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
65 if (!PyArg_ParseTuple(args, "I", &t))
68 unix_to_nt_time(&nt, t);
70 return PyInt_FromLong((uint64_t)nt);
73 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
75 PyObject *py_creds, *py_ldb;
76 struct cli_credentials *creds;
77 struct ldb_context *ldb;
78 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
81 PyErr_LDB_OR_RAISE(py_ldb, ldb);
83 creds = cli_credentials_from_py_object(py_creds);
85 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
89 ldb_set_opaque(ldb, "credentials", creds);
94 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
96 PyObject *py_lp_ctx, *py_ldb;
97 struct loadparm_context *lp_ctx;
98 struct ldb_context *ldb;
99 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
102 PyErr_LDB_OR_RAISE(py_ldb, ldb);
104 lp_ctx = lp_from_py_object(py_lp_ctx);
105 if (lp_ctx == NULL) {
106 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
110 ldb_set_opaque(ldb, "loadparm", lp_ctx);
116 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
118 PyObject *py_session_info, *py_ldb;
119 struct auth_session_info *info;
120 struct ldb_context *ldb;
121 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
124 PyErr_LDB_OR_RAISE(py_ldb, ldb);
125 /*if (!PyAuthSession_Check(py_session_info)) {
126 PyErr_SetString(PyExc_TypeError, "Expected session info object");
130 info = PyAuthSession_AsSession(py_session_info);
132 ldb_set_opaque(ldb, "sessionInfo", info);
137 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
139 PyObject *py_ldb, *py_sid;
140 struct ldb_context *ldb;
144 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
147 PyErr_LDB_OR_RAISE(py_ldb, ldb);
149 if (!dom_sid_Check(py_sid)) {
150 PyErr_SetString(PyExc_TypeError, "expected SID");
154 sid = py_talloc_get_ptr(py_sid);
156 ret = samdb_set_domain_sid(ldb, sid);
158 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
164 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
167 struct ldb_context *ldb;
170 if (!PyArg_ParseTuple(args, "O", &py_ldb))
173 PyErr_LDB_OR_RAISE(py_ldb, ldb);
174 ret = ldb_register_samba_handlers(ldb);
176 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
180 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
182 PyObject *py_ldb, *py_guid;
185 struct ldb_context *ldb;
186 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
189 PyErr_LDB_OR_RAISE(py_ldb, ldb);
190 if (!GUID_Check(py_guid)) {
191 PyErr_SetString(PyExc_TypeError, "Expected GUID");
194 guid = py_talloc_get_ptr(py_guid);
196 ret = samdb_set_ntds_invocation_id(ldb, guid);
198 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
204 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
207 struct ldb_context *ldb;
209 if (!PyArg_ParseTuple(args, "O", &py_ldb))
212 PyErr_LDB_OR_RAISE(py_ldb, ldb);
214 ret = dsdb_set_global_schema(ldb);
215 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
220 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
225 struct ldb_context *ldb;
227 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
230 PyErr_LDB_OR_RAISE(py_ldb, ldb);
232 result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
233 PyErr_WERROR_IS_ERR_RAISE(result);
238 static PyMethodDef py_misc_methods[] = {
239 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
240 "random_password(len) -> string\n"
241 "Generate random password with specified length." },
242 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
243 "unix2nttime(timestamp) -> nttime" },
244 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
245 "ldb_set_credentials(ldb, credentials) -> None\n"
246 "Set credentials to use when connecting." },
247 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
248 "ldb_set_session_info(ldb, session_info)\n"
249 "Set session info to use when connecting." },
250 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
251 "ldb_set_loadparm(ldb, session_info)\n"
252 "Set loadparm context to use when connecting." },
253 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
254 "samdb_set_domain_sid(samdb, sid)\n"
255 "Set SID of domain to use." },
256 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
257 "ldb_register_samba_handlers(ldb)\n"
258 "Register Samba-specific LDB modules and schemas." },
259 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
261 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
263 { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
272 m = Py_InitModule3("glue", py_misc_methods,
273 "Python bindings for miscellaneous Samba functions.");
277 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));