2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4 Copyright (C) Matthias Dieter Wallnöfer 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "ldb_errors.h"
25 #include "param/param.h"
26 #include "auth/credentials/credentials.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "lib/ldb-samba/ldif_handlers.h"
29 #include "librpc/ndr/libndr.h"
31 #include "lib/ldb/pyldb.h"
32 #include "libcli/util/pyerrors.h"
33 #include "libcli/security/security.h"
34 #include "auth/pyauth.h"
35 #include "param/pyparam.h"
36 #include "auth/credentials/pycredentials.h"
37 #include "lib/socket/netif.h"
38 #include "lib/socket/netif_proto.h"
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);
49 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
51 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
52 return; /* Python exception should already be set, just keep that */
54 PyErr_SetObject(error
,
55 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
56 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
59 static PyObject
*py_ldb_get_exception(void)
61 PyObject
*mod
= PyImport_ImportModule("ldb");
65 return PyObject_GetAttrString(mod
, "LdbError");
68 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
73 if (!PyArg_ParseTuple(args
, "i", &len
))
76 retstr
= generate_random_str(NULL
, len
);
77 ret
= PyString_FromString(retstr
);
82 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
87 if (!PyArg_ParseTuple(args
, "ii", &min
, &max
))
90 retstr
= generate_random_password(NULL
, min
, max
);
94 ret
= PyString_FromString(retstr
);
99 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
103 if (!PyArg_ParseTuple(args
, "I", &t
))
106 unix_to_nt_time(&nt
, t
);
108 return PyLong_FromLongLong((uint64_t)nt
);
111 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
115 if (!PyArg_ParseTuple(args
, "K", &nt
))
118 t
= nt_time_to_unix(nt
);
120 return PyInt_FromLong((uint64_t)t
);
123 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
129 if (!PyArg_ParseTuple(args
, "K", &nt
))
132 tmp_ctx
= talloc_new(NULL
);
134 string
= nt_time_string(tmp_ctx
, nt
);
135 ret
= PyString_FromString(string
);
137 talloc_free(tmp_ctx
);
142 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
145 if (!PyArg_ParseTuple(args
, "I", &level
))
147 (DEBUGLEVEL
) = level
;
151 static PyObject
*py_dsdb_set_schema_from_ldif(PyObject
*self
, PyObject
*args
)
156 struct ldb_context
*ldb
;
158 if (!PyArg_ParseTuple(args
, "Oss", &py_ldb
, &pf
, &df
))
161 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
163 result
= dsdb_set_schema_from_ldif(ldb
, pf
, df
);
164 PyErr_WERROR_IS_ERR_RAISE(result
);
169 static PyObject
*py_dsdb_write_prefixes_from_schema_to_ldb(PyObject
*self
, PyObject
*args
)
172 struct ldb_context
*ldb
;
174 struct dsdb_schema
*schema
;
176 if (!PyArg_ParseTuple(args
, "O", &py_ldb
))
179 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
181 schema
= dsdb_get_schema(ldb
, NULL
);
183 PyErr_SetString(PyExc_RuntimeError
, "Failed to set find a schema on ldb!\n");
187 result
= dsdb_write_prefixes_from_schema_to_ldb(NULL
, ldb
, schema
);
188 PyErr_WERROR_IS_ERR_RAISE(result
);
193 static PyObject
*py_dsdb_set_schema_from_ldb(PyObject
*self
, PyObject
*args
)
196 struct ldb_context
*ldb
;
197 PyObject
*py_from_ldb
;
198 struct ldb_context
*from_ldb
;
199 struct dsdb_schema
*schema
;
201 if (!PyArg_ParseTuple(args
, "OO", &py_ldb
, &py_from_ldb
))
204 PyErr_LDB_OR_RAISE(py_ldb
, ldb
);
206 PyErr_LDB_OR_RAISE(py_from_ldb
, from_ldb
);
208 schema
= dsdb_get_schema(from_ldb
, NULL
);
210 PyErr_SetString(PyExc_RuntimeError
, "Failed to set find a schema on 'from' ldb!\n");
214 ret
= dsdb_reference_schema(ldb
, schema
, true);
215 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret
, ldb
);
221 return the list of interface IPs we have configured
222 takes an loadparm context, returns a list of IPs in string form
224 Does not return addresses on 127.0.0.0/8
226 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
232 struct loadparm_context
*lp_ctx
;
233 struct interface
*ifaces
;
237 if (!PyArg_ParseTuple(args
, "Oi", &py_lp_ctx
, &all_interfaces
))
240 tmp_ctx
= talloc_new(NULL
);
242 lp_ctx
= lp_from_py_object(NULL
, py_lp_ctx
); /* FIXME: leaky */
243 if (lp_ctx
== NULL
) {
244 PyErr_SetString(PyExc_TypeError
, "Expected loadparm object");
245 talloc_free(tmp_ctx
);
249 load_interfaces(tmp_ctx
, lp_interfaces(lp_ctx
), &ifaces
);
251 count
= iface_count(ifaces
);
253 /* first count how many are not loopback addresses */
254 for (ifcount
= i
= 0; i
<count
; i
++) {
255 const char *ip
= iface_n_ip(ifaces
, i
);
256 if (!(!all_interfaces
&& iface_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
261 pylist
= PyList_New(ifcount
);
262 for (ifcount
= i
= 0; i
<count
; i
++) {
263 const char *ip
= iface_n_ip(ifaces
, i
);
264 if (!(!all_interfaces
&& iface_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
265 PyList_SetItem(pylist
, ifcount
, PyString_FromString(ip
));
269 talloc_free(tmp_ctx
);
274 static PyMethodDef py_misc_methods
[] = {
275 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
276 "generate_random_str(len) -> string\n"
277 "Generate random string with specified length." },
278 { "generate_random_password", (PyCFunction
)py_generate_random_password
, METH_VARARGS
,
279 "generate_random_password(min, max) -> string\n"
280 "Generate random password with a length >= min and <= max." },
281 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
282 "unix2nttime(timestamp) -> nttime" },
283 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
284 "nttime2unix(nttime) -> timestamp" },
285 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
286 "nttime2string(nttime) -> string" },
287 { "dsdb_set_schema_from_ldif", (PyCFunction
)py_dsdb_set_schema_from_ldif
, METH_VARARGS
,
289 { "dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction
)py_dsdb_write_prefixes_from_schema_to_ldb
, METH_VARARGS
,
291 { "dsdb_set_schema_from_ldb", (PyCFunction
)py_dsdb_set_schema_from_ldb
, METH_VARARGS
,
293 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
295 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
296 "get interface IP address list"},
304 debug_setup_talloc_log();
306 m
= Py_InitModule3("_glue", py_misc_methods
,
307 "Python bindings for miscellaneous Samba functions.");
311 PyModule_AddObject(m
, "version", PyString_FromString(SAMBA_VERSION_STRING
));
313 /* one of the most annoying things about python scripts is
314 that they don't die when you hit control-C. This fixes that
315 sillyness. As we do all database operations using
316 transactions, this is also safe. In fact, not dying
317 immediately is unsafe as we could end up treating the
318 control-C exception as a different error and try to modify
319 as database incorrectly
321 signal(SIGINT
, SIG_DFL
);