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/>.
21 #include "python/py3compat.h"
24 #include "param/pyparam.h"
25 #include "lib/socket/netif.h"
28 static PyObject *PyExc_NTSTATUSError;
29 static PyObject *PyExc_WERRORError;
30 static PyObject *PyExc_HRESULTError;
31 static PyObject *PyExc_DsExtendedError;
33 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
38 if (!PyArg_ParseTuple(args, "i", &len))
41 retstr = generate_random_str(NULL, len);
42 ret = PyStr_FromString(retstr);
47 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
52 if (!PyArg_ParseTuple(args, "ii", &min, &max))
55 retstr = generate_random_password(NULL, min, max);
59 ret = PyStr_FromString(retstr);
64 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
69 if (!PyArg_ParseTuple(args, "ii", &min, &max))
72 retstr = generate_random_machine_password(NULL, min, max);
76 ret = PyUnicode_FromString(retstr);
81 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
87 if (!PyArg_ParseTuple(args, "I", &_t)) {
92 unix_to_nt_time(&nt, t);
94 return PyLong_FromLongLong((uint64_t)nt);
97 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
101 if (!PyArg_ParseTuple(args, "K", &nt))
104 t = nt_time_to_unix(nt);
106 return PyInt_FromLong((uint64_t)t);
109 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
115 if (!PyArg_ParseTuple(args, "K", &nt))
118 tmp_ctx = talloc_new(NULL);
119 if (tmp_ctx == NULL) {
124 string = nt_time_string(tmp_ctx, nt);
125 ret = PyStr_FromString(string);
127 talloc_free(tmp_ctx);
132 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
135 if (!PyArg_ParseTuple(args, "I", &level))
137 (DEBUGLEVEL) = level;
141 static PyObject *py_get_debug_level(PyObject *self)
143 return PyInt_FromLong(DEBUGLEVEL);
146 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
148 #ifdef WITH_NTVFS_FILESERVER
156 return the list of interface IPs we have configured
157 takes an loadparm context, returns a list of IPs in string form
159 Does not return addresses on 127.0.0.0/8
161 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
167 struct loadparm_context *lp_ctx;
168 struct interface *ifaces;
170 int all_interfaces = 1;
172 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
175 tmp_ctx = talloc_new(NULL);
176 if (tmp_ctx == NULL) {
181 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
182 if (lp_ctx == NULL) {
183 talloc_free(tmp_ctx);
187 load_interface_list(tmp_ctx, lp_ctx, &ifaces);
189 count = iface_list_count(ifaces);
191 /* first count how many are not loopback addresses */
192 for (ifcount = i = 0; i<count; i++) {
193 const char *ip = iface_list_n_ip(ifaces, i);
195 if (all_interfaces) {
200 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
204 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
208 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
212 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
219 pylist = PyList_New(ifcount);
220 for (ifcount = i = 0; i<count; i++) {
221 const char *ip = iface_list_n_ip(ifaces, i);
223 if (all_interfaces) {
224 PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
229 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
233 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
237 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
241 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
245 PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
248 talloc_free(tmp_ctx);
252 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
256 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
259 return PyInt_FromLong(strcasecmp_m(s1, s2));
262 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
266 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
269 ret = strstr_m(s1, s2);
273 return PyStr_FromString(ret);
276 static PyMethodDef py_misc_methods[] = {
277 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
278 "generate_random_str(len) -> string\n"
279 "Generate random string with specified length." },
280 { "generate_random_password", (PyCFunction)py_generate_random_password,
281 METH_VARARGS, "generate_random_password(min, max) -> string\n"
282 "Generate random password (based on printable ascii characters) "
283 "with a length >= min and <= max." },
284 { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
285 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
286 "Generate random password "
287 "(based on random utf16 characters converted to utf8 or "
288 "random ascii characters if 'unix charset' is not 'utf8')"
289 "with a length >= min (at least 14) and <= max (at most 255)." },
290 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
291 "unix2nttime(timestamp) -> nttime" },
292 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
293 "nttime2unix(nttime) -> timestamp" },
294 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
295 "nttime2string(nttime) -> string" },
296 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
298 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
300 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
301 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
303 "get interface IP address list"},
304 { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
305 "(for testing) compare two strings using Samba's strcasecmp_m()"},
306 { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
307 "(for testing) find one string in another with Samba's strstr_m()"},
308 { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
309 "is the NTVFS file server built in this installation?" },
313 static struct PyModuleDef moduledef = {
314 PyModuleDef_HEAD_INIT,
316 .m_doc = "Python bindings for miscellaneous Samba functions.",
318 .m_methods = py_misc_methods,
321 MODULE_INIT_FUNC(_glue)
325 debug_setup_talloc_log();
327 m = PyModule_Create(&moduledef);
331 PyModule_AddObject(m, "version",
332 PyStr_FromString(SAMBA_VERSION_STRING));
333 PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
334 if (PyExc_NTSTATUSError != NULL) {
335 Py_INCREF(PyExc_NTSTATUSError);
336 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
339 PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
340 if (PyExc_WERRORError != NULL) {
341 Py_INCREF(PyExc_WERRORError);
342 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
345 PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
346 if (PyExc_HRESULTError != NULL) {
347 Py_INCREF(PyExc_HRESULTError);
348 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
351 PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
352 if (PyExc_DsExtendedError != NULL) {
353 Py_INCREF(PyExc_DsExtendedError);
354 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);