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/>.
22 #include "param/param.h"
24 #include "libcli/util/pyerrors.h"
25 #include "param/pyparam.h"
26 #include "lib/socket/netif.h"
27 #include "lib/socket/netif_proto.h"
28 #include "lib/talloc/pytalloc.h"
30 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
35 if (!PyArg_ParseTuple(args, "i", &len))
38 retstr = generate_random_str(NULL, len);
39 ret = PyString_FromString(retstr);
44 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
49 if (!PyArg_ParseTuple(args, "ii", &min, &max))
52 retstr = generate_random_password(NULL, min, max);
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 PyLong_FromLongLong((uint64_t)nt);
73 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
77 if (!PyArg_ParseTuple(args, "K", &nt))
80 t = nt_time_to_unix(nt);
82 return PyInt_FromLong((uint64_t)t);
85 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
91 if (!PyArg_ParseTuple(args, "K", &nt))
94 tmp_ctx = talloc_new(NULL);
96 string = nt_time_string(tmp_ctx, nt);
97 ret = PyString_FromString(string);
104 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
107 if (!PyArg_ParseTuple(args, "I", &level))
109 (DEBUGLEVEL) = level;
113 static PyObject *py_get_debug_level(PyObject *self)
115 return PyInt_FromLong(DEBUGLEVEL);
119 return the list of interface IPs we have configured
120 takes an loadparm context, returns a list of IPs in string form
122 Does not return addresses on 127.0.0.0/8
124 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
130 struct loadparm_context *lp_ctx;
131 struct interface *ifaces;
135 if (!PyArg_ParseTuple(args, "Oi", &py_lp_ctx, &all_interfaces))
138 tmp_ctx = talloc_new(NULL);
139 if (tmp_ctx == NULL) {
144 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
145 if (lp_ctx == NULL) {
146 talloc_free(tmp_ctx);
150 load_interfaces(tmp_ctx, lpcfg_interfaces(lp_ctx), &ifaces);
152 count = iface_count(ifaces);
154 /* first count how many are not loopback addresses */
155 for (ifcount = i = 0; i<count; i++) {
156 const char *ip = iface_n_ip(ifaces, i);
157 if (!(!all_interfaces && iface_same_net(ip, "127.0.0.1", "255.0.0.0"))) {
162 pylist = PyList_New(ifcount);
163 for (ifcount = i = 0; i<count; i++) {
164 const char *ip = iface_n_ip(ifaces, i);
165 if (!(!all_interfaces && iface_same_net(ip, "127.0.0.1", "255.0.0.0"))) {
166 PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
170 talloc_free(tmp_ctx);
174 static PyMethodDef py_misc_methods[] = {
175 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
176 "generate_random_str(len) -> string\n"
177 "Generate random string with specified length." },
178 { "generate_random_password", (PyCFunction)py_generate_random_password,
179 METH_VARARGS, "generate_random_password(min, max) -> string\n"
180 "Generate random password with a length >= min and <= max." },
181 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
182 "unix2nttime(timestamp) -> nttime" },
183 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
184 "nttime2unix(nttime) -> timestamp" },
185 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
186 "nttime2string(nttime) -> string" },
187 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
189 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
191 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
192 "get interface IP address list"},
200 debug_setup_talloc_log();
202 m = Py_InitModule3("_glue", py_misc_methods,
203 "Python bindings for miscellaneous Samba functions.");
207 PyModule_AddObject(m, "version",
208 PyString_FromString(SAMBA_VERSION_STRING));
210 /* one of the most annoying things about python scripts is
211 that they don't die when you hit control-C. This fixes that
212 sillyness. As we do all database operations using
213 transactions, this is also safe.
215 signal(SIGINT, SIG_DFL);