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 "param/pyparam.h"
24 #include "lib/socket/netif.h"
28 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
33 if (!PyArg_ParseTuple(args
, "i", &len
))
36 retstr
= generate_random_str(NULL
, len
);
37 ret
= PyString_FromString(retstr
);
42 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
47 if (!PyArg_ParseTuple(args
, "ii", &min
, &max
))
50 retstr
= generate_random_password(NULL
, min
, max
);
54 ret
= PyString_FromString(retstr
);
59 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
63 if (!PyArg_ParseTuple(args
, "I", &t
))
66 unix_to_nt_time(&nt
, t
);
68 return PyLong_FromLongLong((uint64_t)nt
);
71 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
75 if (!PyArg_ParseTuple(args
, "K", &nt
))
78 t
= nt_time_to_unix(nt
);
80 return PyInt_FromLong((uint64_t)t
);
83 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
89 if (!PyArg_ParseTuple(args
, "K", &nt
))
92 tmp_ctx
= talloc_new(NULL
);
93 if (tmp_ctx
== NULL
) {
98 string
= nt_time_string(tmp_ctx
, nt
);
99 ret
= PyString_FromString(string
);
101 talloc_free(tmp_ctx
);
106 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
109 if (!PyArg_ParseTuple(args
, "I", &level
))
111 (DEBUGLEVEL
) = level
;
115 static PyObject
*py_get_debug_level(PyObject
*self
)
117 return PyInt_FromLong(DEBUGLEVEL
);
121 return the list of interface IPs we have configured
122 takes an loadparm context, returns a list of IPs in string form
124 Does not return addresses on 127.0.0.0/8
126 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
132 struct loadparm_context
*lp_ctx
;
133 struct interface
*ifaces
;
137 if (!PyArg_ParseTuple(args
, "Oi", &py_lp_ctx
, &all_interfaces
))
140 tmp_ctx
= talloc_new(NULL
);
141 if (tmp_ctx
== NULL
) {
146 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
147 if (lp_ctx
== NULL
) {
148 talloc_free(tmp_ctx
);
152 load_interfaces(tmp_ctx
, lpcfg_interfaces(lp_ctx
), &ifaces
);
154 count
= iface_count(ifaces
);
156 /* first count how many are not loopback addresses */
157 for (ifcount
= i
= 0; i
<count
; i
++) {
158 const char *ip
= iface_n_ip(ifaces
, i
);
159 if (!(!all_interfaces
&& iface_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
164 pylist
= PyList_New(ifcount
);
165 for (ifcount
= i
= 0; i
<count
; i
++) {
166 const char *ip
= iface_n_ip(ifaces
, i
);
167 if (!(!all_interfaces
&& iface_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
168 PyList_SetItem(pylist
, ifcount
, PyString_FromString(ip
));
172 talloc_free(tmp_ctx
);
176 static PyMethodDef py_misc_methods
[] = {
177 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
178 "generate_random_str(len) -> string\n"
179 "Generate random string with specified length." },
180 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
181 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
182 "Generate random password with a length >= min and <= max." },
183 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
184 "unix2nttime(timestamp) -> nttime" },
185 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
186 "nttime2unix(nttime) -> timestamp" },
187 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
188 "nttime2string(nttime) -> string" },
189 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
191 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
193 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
194 "get interface IP address list"},
202 debug_setup_talloc_log();
204 m
= Py_InitModule3("_glue", py_misc_methods
,
205 "Python bindings for miscellaneous Samba functions.");
209 PyModule_AddObject(m
, "version",
210 PyString_FromString(SAMBA_VERSION_STRING
));
212 /* one of the most annoying things about python scripts is
213 that they don't die when you hit control-C. This fixes that
214 sillyness. As we do all database operations using
215 transactions, this is also safe.
217 signal(SIGINT
, SIG_DFL
);