/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- py_generate_random_str
- py_unix2nttime
- py_ldb_set_credentials
- py_ldb_set_loadparm
- py_ldb_set_session_info
- py_samdb_set_domain_sid
- py_ldb_register_samba_handlers
- py_dsdb_set_ntds_invocation_id
- py_dsdb_set_global_schema
- py_dsdb_attach_schema_from_ldif_file
- initglue
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4
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.
9
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.
14
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/>.
17 */
18
19 #include "includes.h"
20 #include "ldb.h"
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"
27 #include "version.h"
28 #include <Python.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
35 #ifndef Py_RETURN_NONE
36 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
37 #endif
38
39 /* FIXME: These should be in a header file somewhere, once we finish moving
40 * away from SWIG .. */
41 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
42
43 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
44 if (!PyLdb_Check(py_ldb)) { \
45 /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
46 return NULL; \ */ \
47 } \
48 ldb = PyLdb_AsLdbContext(py_ldb);
49
50
51 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
52 {
53 int len;
54 PyObject *ret;
55 char *retstr;
56 if (!PyArg_ParseTuple(args, "i", &len))
57 return NULL;
58
59 retstr = generate_random_str(NULL, len);
60 ret = PyString_FromString(retstr);
61 talloc_free(retstr);
62 return ret;
63 }
64
65 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
66 {
67 time_t t;
68 NTTIME nt;
69 if (!PyArg_ParseTuple(args, "I", &t))
70 return NULL;
71
72 unix_to_nt_time(&nt, t);
73
74 return PyInt_FromLong((uint64_t)nt);
75 }
76
77 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
78 {
79 PyObject *py_creds, *py_ldb;
80 struct cli_credentials *creds;
81 struct ldb_context *ldb;
82 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
83 return NULL;
84
85 PyErr_LDB_OR_RAISE(py_ldb, ldb);
86
87 creds = cli_credentials_from_py_object(py_creds);
88 if (creds == NULL) {
89 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
90 return NULL;
91 }
92
93 ldb_set_opaque(ldb, "credentials", creds);
94
95 Py_RETURN_NONE;
96 }
97
98 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
99 {
100 PyObject *py_lp_ctx, *py_ldb;
101 struct loadparm_context *lp_ctx;
102 struct ldb_context *ldb;
103 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
104 return NULL;
105
106 PyErr_LDB_OR_RAISE(py_ldb, ldb);
107
108 lp_ctx = lp_from_py_object(py_lp_ctx);
109 if (lp_ctx == NULL) {
110 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
111 return NULL;
112 }
113
114 ldb_set_opaque(ldb, "loadparm", lp_ctx);
115
116 Py_RETURN_NONE;
117 }
118
119
120 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
121 {
122 PyObject *py_session_info, *py_ldb;
123 struct auth_session_info *info;
124 struct ldb_context *ldb;
125 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
126 return NULL;
127
128 PyErr_LDB_OR_RAISE(py_ldb, ldb);
129 /*if (!PyAuthSession_Check(py_session_info)) {
130 PyErr_SetString(PyExc_TypeError, "Expected session info object");
131 return NULL;
132 }*/
133
134 info = PyAuthSession_AsSession(py_session_info);
135
136 ldb_set_opaque(ldb, "sessionInfo", info);
137
138 Py_RETURN_NONE;
139 }
140
141 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
142 {
143 PyObject *py_ldb, *py_sid;
144 struct ldb_context *ldb;
145 struct dom_sid *sid;
146 bool ret;
147
148 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
149 return NULL;
150
151 PyErr_LDB_OR_RAISE(py_ldb, ldb);
152
153 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
154
155 ret = samdb_set_domain_sid(ldb, sid);
156 if (!ret) {
157 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
158 return NULL;
159 }
160 Py_RETURN_NONE;
161 }
162
163 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
164 {
165 PyObject *py_ldb;
166 struct ldb_context *ldb;
167 int ret;
168
169 if (!PyArg_ParseTuple(args, "O", &py_ldb))
170 return NULL;
171
172 PyErr_LDB_OR_RAISE(py_ldb, ldb);
173 ret = ldb_register_samba_handlers(ldb);
174
175 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
176 Py_RETURN_NONE;
177 }
178
179 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
180 {
181 PyObject *py_ldb, *py_guid;
182 bool ret;
183 struct GUID guid;
184 struct ldb_context *ldb;
185 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
186 return NULL;
187
188 PyErr_LDB_OR_RAISE(py_ldb, ldb);
189 GUID_from_string(PyString_AsString(py_guid), &guid);
190
191 ret = samdb_set_ntds_invocation_id(ldb, &guid);
192 if (!ret) {
193 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
194 return NULL;
195 }
196 Py_RETURN_NONE;
197 }
198
199 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
200 {
201 PyObject *py_ldb;
202 struct ldb_context *ldb;
203 int ret;
204 if (!PyArg_ParseTuple(args, "O", &py_ldb))
205 return NULL;
206
207 PyErr_LDB_OR_RAISE(py_ldb, ldb);
208
209 ret = dsdb_set_global_schema(ldb);
210 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
211
212 Py_RETURN_NONE;
213 }
214
215 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
216 {
217 WERROR result;
218 char *pf, *df;
219 PyObject *py_ldb;
220 struct ldb_context *ldb;
221
222 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
223 return NULL;
224
225 PyErr_LDB_OR_RAISE(py_ldb, ldb);
226
227 result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
228 PyErr_WERROR_IS_ERR_RAISE(result);
229
230 Py_RETURN_NONE;
231 }
232
233 static PyMethodDef py_misc_methods[] = {
234 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
235 "random_password(len) -> string\n"
236 "Generate random password with specified length." },
237 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
238 "unix2nttime(timestamp) -> nttime" },
239 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
240 "ldb_set_credentials(ldb, credentials) -> None\n"
241 "Set credentials to use when connecting." },
242 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
243 "ldb_set_session_info(ldb, session_info)\n"
244 "Set session info to use when connecting." },
245 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
246 "ldb_set_loadparm(ldb, session_info)\n"
247 "Set loadparm context to use when connecting." },
248 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
249 "samdb_set_domain_sid(samdb, sid)\n"
250 "Set SID of domain to use." },
251 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
252 "ldb_register_samba_handlers(ldb)\n"
253 "Register Samba-specific LDB modules and schemas." },
254 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
255 NULL },
256 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
257 NULL },
258 { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
259 NULL },
260 { NULL }
261 };
262
263 void initglue(void)
/* [<][>][^][v][top][bottom][index][help] */
264 {
265 PyObject *m;
266
267 m = Py_InitModule3("glue", py_misc_methods,
268 "Python bindings for miscellaneous Samba functions.");
269 if (m == NULL)
270 return;
271
272 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
273 }
274