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"
26 #include "lib/util/debug.h"
29 static PyObject *PyExc_NTSTATUSError;
30 static PyObject *PyExc_WERRORError;
31 static PyObject *PyExc_HRESULTError;
32 static PyObject *PyExc_DsExtendedError;
34 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
39 if (!PyArg_ParseTuple(args, "i", &len))
42 retstr = generate_random_str(NULL, len);
43 ret = PyUnicode_FromString(retstr);
48 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
53 if (!PyArg_ParseTuple(args, "ii", &min, &max))
56 retstr = generate_random_password(NULL, min, max);
60 ret = PyUnicode_FromString(retstr);
65 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
70 if (!PyArg_ParseTuple(args, "ii", &min, &max))
73 retstr = generate_random_machine_password(NULL, min, max);
77 ret = PyUnicode_FromString(retstr);
82 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
86 if (!PyArg_ParseTuple(args, "s", &pass)) {
90 return PyBool_FromLong(check_password_quality(pass));
93 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
97 uint8_t *bytes = NULL;
99 if (!PyArg_ParseTuple(args, "i", &len))
102 bytes = talloc_zero_size(NULL, len);
103 generate_random_buffer(bytes, len);
104 ret = PyBytes_FromStringAndSize((const char *)bytes, len);
109 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
115 if (!PyArg_ParseTuple(args, "I", &_t)) {
120 unix_to_nt_time(&nt, t);
122 return PyLong_FromLongLong((uint64_t)nt);
125 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
129 if (!PyArg_ParseTuple(args, "K", &nt))
132 t = nt_time_to_unix(nt);
134 return PyInt_FromLong((uint64_t)t);
137 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
143 if (!PyArg_ParseTuple(args, "K", &nt))
146 tmp_ctx = talloc_new(NULL);
147 if (tmp_ctx == NULL) {
152 string = nt_time_string(tmp_ctx, nt);
153 ret = PyUnicode_FromString(string);
155 talloc_free(tmp_ctx);
160 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
163 if (!PyArg_ParseTuple(args, "I", &level))
165 debuglevel_set(level);
169 static PyObject *py_get_debug_level(PyObject *self,
170 PyObject *Py_UNUSED(ignored))
172 return PyInt_FromLong(debuglevel_get());
175 static PyObject *py_fault_setup(PyObject *self,
176 PyObject *Py_UNUSED(ignored))
186 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
187 PyObject *Py_UNUSED(ignored))
189 #ifdef WITH_NTVFS_FILESERVER
196 static PyObject *py_is_heimdal_built(PyObject *self,
197 PyObject *Py_UNUSED(ignored))
199 #ifdef SAMBA4_USES_HEIMDAL
207 return the list of interface IPs we have configured
208 takes an loadparm context, returns a list of IPs in string form
210 Does not return addresses on 127.0.0.0/8
212 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
218 struct loadparm_context *lp_ctx;
219 struct interface *ifaces;
221 int all_interfaces = 1;
223 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
226 tmp_ctx = talloc_new(NULL);
227 if (tmp_ctx == NULL) {
232 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
233 if (lp_ctx == NULL) {
234 talloc_free(tmp_ctx);
238 load_interface_list(tmp_ctx, lp_ctx, &ifaces);
240 count = iface_list_count(ifaces);
242 /* first count how many are not loopback addresses */
243 for (ifcount = i = 0; i<count; i++) {
244 const char *ip = iface_list_n_ip(ifaces, i);
246 if (all_interfaces) {
251 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
255 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
259 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
263 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
270 pylist = PyList_New(ifcount);
271 for (ifcount = i = 0; i<count; i++) {
272 const char *ip = iface_list_n_ip(ifaces, i);
274 if (all_interfaces) {
275 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
280 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
284 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
288 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
292 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
296 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
299 talloc_free(tmp_ctx);
303 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
305 const char *s1 = NULL;
306 const char *s2 = NULL;
308 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
310 "utf8", &s1, "utf8", &s2)) {
314 cmp_result = strcasecmp_m(s1, s2);
315 PyMem_Free(discard_const_p(char, s1));
316 PyMem_Free(discard_const_p(char, s2));
317 return PyInt_FromLong(cmp_result);
320 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
322 const char *s1 = NULL;
323 const char *s2 = NULL;
324 char *strstr_ret = NULL;
325 PyObject *result = NULL;
326 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
328 "utf8", &s1, "utf8", &s2))
331 strstr_ret = strstr_m(s1, s2);
333 PyMem_Free(discard_const_p(char, s1));
334 PyMem_Free(discard_const_p(char, s2));
337 result = PyUnicode_FromString(strstr_ret);
338 PyMem_Free(discard_const_p(char, s1));
339 PyMem_Free(discard_const_p(char, s2));
343 static PyMethodDef py_misc_methods[] = {
344 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
345 "generate_random_str(len) -> string\n"
346 "Generate random string with specified length." },
347 { "generate_random_password", (PyCFunction)py_generate_random_password,
348 METH_VARARGS, "generate_random_password(min, max) -> string\n"
349 "Generate random password (based on printable ascii characters) "
350 "with a length >= min and <= max." },
351 { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
352 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
353 "Generate random password "
354 "(based on random utf16 characters converted to utf8 or "
355 "random ascii characters if 'unix charset' is not 'utf8')"
356 "with a length >= min (at least 14) and <= max (at most 255)." },
357 { "check_password_quality", (PyCFunction)py_check_password_quality,
358 METH_VARARGS, "check_password_quality(pass) -> bool\n"
359 "Check password quality against Samba's check_password_quality,"
360 "the implementation of Microsoft's rules:"
361 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
363 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
364 "unix2nttime(timestamp) -> nttime" },
365 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
366 "nttime2unix(nttime) -> timestamp" },
367 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
368 "nttime2string(nttime) -> string" },
369 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
371 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
373 { "fault_setup", (PyCFunction)py_fault_setup, METH_NOARGS,
374 "setup the default samba panic handler" },
375 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
376 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
378 "get interface IP address list"},
379 { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
380 "(for testing) compare two strings using Samba's strcasecmp_m()"},
381 { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
382 "(for testing) find one string in another with Samba's strstr_m()"},
383 { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
384 "is the NTVFS file server built in this installation?" },
385 { "is_heimdal_built", (PyCFunction)py_is_heimdal_built, METH_NOARGS,
386 "is Samba built with Heimdal Kerberbos?" },
387 { "generate_random_bytes",
388 (PyCFunction)py_generate_random_bytes,
390 "generate_random_bytes(len) -> bytes\n"
391 "Generate random bytes with specified length." },
395 static struct PyModuleDef moduledef = {
396 PyModuleDef_HEAD_INIT,
398 .m_doc = "Python bindings for miscellaneous Samba functions.",
400 .m_methods = py_misc_methods,
403 MODULE_INIT_FUNC(_glue)
407 debug_setup_talloc_log();
409 m = PyModule_Create(&moduledef);
413 PyModule_AddObject(m, "version",
414 PyUnicode_FromString(SAMBA_VERSION_STRING));
415 PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
416 if (PyExc_NTSTATUSError != NULL) {
417 Py_INCREF(PyExc_NTSTATUSError);
418 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
421 PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
422 if (PyExc_WERRORError != NULL) {
423 Py_INCREF(PyExc_WERRORError);
424 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
427 PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
428 if (PyExc_HRESULTError != NULL) {
429 Py_INCREF(PyExc_HRESULTError);
430 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
433 PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
434 if (PyExc_DsExtendedError != NULL) {
435 Py_INCREF(PyExc_DsExtendedError);
436 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);