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 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
32 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
37 if (!PyArg_ParseTuple(args
, "i", &len
))
40 retstr
= generate_random_str(NULL
, len
);
41 ret
= PyString_FromString(retstr
);
46 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
51 if (!PyArg_ParseTuple(args
, "ii", &min
, &max
))
54 retstr
= generate_random_password(NULL
, min
, max
);
58 ret
= PyString_FromString(retstr
);
63 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
67 if (!PyArg_ParseTuple(args
, "I", &t
))
70 unix_to_nt_time(&nt
, t
);
72 return PyLong_FromLongLong((uint64_t)nt
);
75 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
79 if (!PyArg_ParseTuple(args
, "K", &nt
))
82 t
= nt_time_to_unix(nt
);
84 return PyInt_FromLong((uint64_t)t
);
87 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
93 if (!PyArg_ParseTuple(args
, "K", &nt
))
96 tmp_ctx
= talloc_new(NULL
);
97 if (tmp_ctx
== NULL
) {
102 string
= nt_time_string(tmp_ctx
, nt
);
103 ret
= PyString_FromString(string
);
105 talloc_free(tmp_ctx
);
110 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
113 if (!PyArg_ParseTuple(args
, "I", &level
))
115 (DEBUGLEVEL
) = level
;
119 static PyObject
*py_get_debug_level(PyObject
*self
)
121 return PyInt_FromLong(DEBUGLEVEL
);
125 return the list of interface IPs we have configured
126 takes an loadparm context, returns a list of IPs in string form
128 Does not return addresses on 127.0.0.0/8
130 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
136 struct loadparm_context
*lp_ctx
;
137 struct interface
*ifaces
;
141 if (!PyArg_ParseTuple(args
, "Oi", &py_lp_ctx
, &all_interfaces
))
144 tmp_ctx
= talloc_new(NULL
);
145 if (tmp_ctx
== NULL
) {
150 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
151 if (lp_ctx
== NULL
) {
152 talloc_free(tmp_ctx
);
156 load_interface_list(tmp_ctx
, lpcfg_interfaces(lp_ctx
), &ifaces
);
158 count
= iface_list_count(ifaces
);
160 /* first count how many are not loopback addresses */
161 for (ifcount
= i
= 0; i
<count
; i
++) {
162 const char *ip
= iface_list_n_ip(ifaces
, i
);
163 if (!(!all_interfaces
&& iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
168 pylist
= PyList_New(ifcount
);
169 for (ifcount
= i
= 0; i
<count
; i
++) {
170 const char *ip
= iface_list_n_ip(ifaces
, i
);
171 if (!(!all_interfaces
&& iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0"))) {
172 PyList_SetItem(pylist
, ifcount
, PyString_FromString(ip
));
176 talloc_free(tmp_ctx
);
180 static PyObject
*py_strcasecmp_m(PyObject
*self
, PyObject
*args
)
184 if (!PyArg_ParseTuple(args
, "ss", &s1
, &s2
))
187 return PyInt_FromLong(strcasecmp_m(s1
, s2
));
190 static PyObject
*py_strstr_m(PyObject
*self
, PyObject
*args
)
194 if (!PyArg_ParseTuple(args
, "ss", &s1
, &s2
))
197 ret
= strstr_m(s1
, s2
);
201 return PyString_FromString(ret
);
204 static PyMethodDef py_misc_methods
[] = {
205 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
206 "generate_random_str(len) -> string\n"
207 "Generate random string with specified length." },
208 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
209 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
210 "Generate random password with a length >= min and <= max." },
211 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
212 "unix2nttime(timestamp) -> nttime" },
213 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
214 "nttime2unix(nttime) -> timestamp" },
215 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
216 "nttime2string(nttime) -> string" },
217 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
219 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
221 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
222 "get interface IP address list"},
223 { "strcasecmp_m", (PyCFunction
)py_strcasecmp_m
, METH_VARARGS
,
224 "(for testing) compare two strings using Samba's strcasecmp_m()"},
225 { "strstr_m", (PyCFunction
)py_strstr_m
, METH_VARARGS
,
226 "(for testing) find one string in another with Samba's strstr_m()"},
234 debug_setup_talloc_log();
236 m
= Py_InitModule3("_glue", py_misc_methods
,
237 "Python bindings for miscellaneous Samba functions.");
241 PyModule_AddObject(m
, "version",
242 PyString_FromString(SAMBA_VERSION_STRING
));
244 /* one of the most annoying things about python scripts is
245 that they don't die when you hit control-C. This fixes that
246 sillyness. As we do all database operations using
247 transactions, this is also safe.
249 signal(SIGINT
, SIG_DFL
);