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 "libcli/security/security.h"
31 #include "auth/pyauth.h"
32 #include "param/pyparam.h"
34 /* FIXME: These should be in a header file somewhere, once we finish moving
35 * away from SWIG .. */
36 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
38 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
39 if (!PyLdb_Check(py_ldb)) { \
40 /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
43 ldb = PyLdb_AsLdbContext(py_ldb);
46 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
51 if (!PyArg_ParseTuple(args, "i", &len))
54 retstr = generate_random_str(NULL, len);
55 ret = PyString_FromString(retstr);
60 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
64 if (!PyArg_ParseTuple(args, "I", &t))
67 unix_to_nt_time(&nt, t);
69 return PyInt_FromLong((uint64_t)nt);
72 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
74 PyObject *py_creds, *py_ldb;
75 struct cli_credentials *creds;
76 struct ldb_context *ldb;
77 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
80 PyErr_LDB_OR_RAISE(py_ldb, ldb);
82 creds = cli_credentials_from_py_object(py_creds);
84 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
88 ldb_set_opaque(ldb, "credentials", creds);
93 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
95 PyObject *py_lp_ctx, *py_ldb;
96 struct loadparm_context *lp_ctx;
97 struct ldb_context *ldb;
98 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
101 PyErr_LDB_OR_RAISE(py_ldb, ldb);
103 lp_ctx = lp_from_py_object(py_lp_ctx);
104 if (lp_ctx == NULL) {
105 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
109 ldb_set_opaque(ldb, "loadparm", lp_ctx);
115 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
117 PyObject *py_session_info, *py_ldb;
118 struct auth_session_info *info;
119 struct ldb_context *ldb;
120 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
123 PyErr_LDB_OR_RAISE(py_ldb, ldb);
124 /*if (!PyAuthSession_Check(py_session_info)) {
125 PyErr_SetString(PyExc_TypeError, "Expected session info object");
129 info = PyAuthSession_AsSession(py_session_info);
131 ldb_set_opaque(ldb, "sessionInfo", info);
136 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
138 PyObject *py_ldb, *py_sid;
139 struct ldb_context *ldb;
143 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
146 PyErr_LDB_OR_RAISE(py_ldb, ldb);
148 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
150 ret = samdb_set_domain_sid(ldb, sid);
152 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
158 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
161 struct ldb_context *ldb;
164 if (!PyArg_ParseTuple(args, "O", &py_ldb))
167 PyErr_LDB_OR_RAISE(py_ldb, ldb);
168 ret = ldb_register_samba_handlers(ldb);
170 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
174 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
176 PyObject *py_ldb, *py_guid;
179 struct ldb_context *ldb;
180 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
183 PyErr_LDB_OR_RAISE(py_ldb, ldb);
184 GUID_from_string(PyString_AsString(py_guid), &guid);
186 ret = samdb_set_ntds_invocation_id(ldb, &guid);
188 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
194 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
197 struct ldb_context *ldb;
199 if (!PyArg_ParseTuple(args, "O", &py_ldb))
202 PyErr_LDB_OR_RAISE(py_ldb, ldb);
204 ret = dsdb_set_global_schema(ldb);
205 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
210 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
215 struct ldb_context *ldb;
217 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
220 PyErr_LDB_OR_RAISE(py_ldb, ldb);
222 result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
223 PyErr_WERROR_IS_ERR_RAISE(result);
228 static PyMethodDef py_misc_methods[] = {
229 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
230 "random_password(len) -> string\n"
231 "Generate random password with specified length." },
232 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
233 "unix2nttime(timestamp) -> nttime" },
234 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
235 "ldb_set_credentials(ldb, credentials) -> None\n"
236 "Set credentials to use when connecting." },
237 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
238 "ldb_set_session_info(ldb, session_info)\n"
239 "Set session info to use when connecting." },
240 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
241 "ldb_set_loadparm(ldb, session_info)\n"
242 "Set loadparm context to use when connecting." },
243 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
244 "samdb_set_domain_sid(samdb, sid)\n"
245 "Set SID of domain to use." },
246 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
247 "ldb_register_samba_handlers(ldb)\n"
248 "Register Samba-specific LDB modules and schemas." },
249 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
251 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
253 { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
262 m = Py_InitModule3("glue", py_misc_methods,
263 "Python bindings for miscellaneous Samba functions.");
267 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));