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"
23 #include "python/modules.h"
25 #include "param/pyparam.h"
26 #include "lib/socket/netif.h"
27 #include "lib/util/debug.h"
28 #include "librpc/ndr/ndr_private.h"
29 #include "lib/cmdline/cmdline.h"
32 static PyObject
*PyExc_NTSTATUSError
;
33 static PyObject
*PyExc_WERRORError
;
34 static PyObject
*PyExc_HRESULTError
;
35 static PyObject
*PyExc_DsExtendedError
;
37 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
43 if (!PyArg_ParseTuple(args
, "n", &len
)) {
47 PyErr_Format(PyExc_ValueError
,
48 "random string length should be positive, not %zd",
52 retstr
= generate_random_str(NULL
, len
);
54 return PyErr_NoMemory();
56 ret
= PyUnicode_FromStringAndSize(retstr
, len
);
61 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
67 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
70 if (max
< 0 || min
< 0) {
72 * The real range checks happens in generate_random_password().
73 * Here just filter out any negative numbers.
75 PyErr_Format(PyExc_ValueError
,
76 "invalid range: %zd - %zd",
81 retstr
= generate_random_password(NULL
, min
, max
);
83 if (errno
== EINVAL
) {
84 return PyErr_Format(PyExc_ValueError
,
85 "invalid range: %zd - %zd",
88 return PyErr_NoMemory();
90 ret
= PyUnicode_FromString(retstr
);
95 static PyObject
*py_generate_random_machine_password(PyObject
*self
, PyObject
*args
)
101 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
104 if (max
< 0 || min
< 0) {
106 * The real range checks happens in
107 * generate_random_machine_password().
108 * Here we just filter out any negative numbers.
110 PyErr_Format(PyExc_ValueError
,
111 "invalid range: %zd - %zd",
116 retstr
= generate_random_machine_password(NULL
, min
, max
);
117 if (retstr
== NULL
) {
118 if (errno
== EINVAL
) {
119 return PyErr_Format(PyExc_ValueError
,
120 "invalid range: %zd - %zd",
123 return PyErr_NoMemory();
125 ret
= PyUnicode_FromString(retstr
);
130 static PyObject
*py_check_password_quality(PyObject
*self
, PyObject
*args
)
134 if (!PyArg_ParseTuple(args
, "s", &pass
)) {
138 return PyBool_FromLong(check_password_quality(pass
));
141 static PyObject
*py_generate_random_bytes(PyObject
*self
, PyObject
*args
)
145 uint8_t *bytes
= NULL
;
147 if (!PyArg_ParseTuple(args
, "n", &len
)) {
151 PyErr_Format(PyExc_ValueError
,
152 "random bytes length should be positive, not %zd",
156 bytes
= talloc_zero_size(NULL
, len
);
161 generate_random_buffer(bytes
, len
);
162 ret
= PyBytes_FromStringAndSize((const char *)bytes
, len
);
167 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
173 if (!PyArg_ParseTuple(args
, "I", &_t
)) {
178 unix_to_nt_time(&nt
, t
);
180 return PyLong_FromLongLong((uint64_t)nt
);
183 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
187 if (!PyArg_ParseTuple(args
, "K", &nt
))
190 t
= nt_time_to_unix(nt
);
192 return PyLong_FromLong((uint64_t)t
);
195 static PyObject
*py_float2nttime(PyObject
*self
, PyObject
*args
)
203 if (!PyArg_ParseTuple(args
, "d", &ft
)) {
207 ft_sec
= (double)(int)ft
;
208 ft_nsec
= (ft
- ft_sec
) * 1.0e+9;
210 ts
.tv_sec
= (int)ft_sec
;
211 ts
.tv_nsec
= (int)ft_nsec
;
213 nt
= full_timespec_to_nt_time(&ts
);
215 return PyLong_FromLongLong((uint64_t)nt
);
218 static PyObject
*py_nttime2float(PyObject
*self
, PyObject
*args
)
222 const struct timespec ts_zero
= { .tv_sec
= 0, };
225 if (!PyArg_ParseTuple(args
, "K", &nt
)) {
229 ts
= nt_time_to_full_timespec(nt
);
230 if (is_omit_timespec(&ts
)) {
231 return PyFloat_FromDouble(1.0);
233 ft
= timespec_elapsed2(&ts_zero
, &ts
);
235 return PyFloat_FromDouble(ft
);
238 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
244 if (!PyArg_ParseTuple(args
, "K", &nt
))
247 tmp_ctx
= talloc_new(NULL
);
248 if (tmp_ctx
== NULL
) {
253 string
= nt_time_string(tmp_ctx
, nt
);
254 ret
= PyUnicode_FromString(string
);
256 talloc_free(tmp_ctx
);
261 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
264 if (!PyArg_ParseTuple(args
, "I", &level
))
266 debuglevel_set(level
);
270 static PyObject
*py_get_debug_level(PyObject
*self
,
271 PyObject
*Py_UNUSED(ignored
))
273 return PyLong_FromLong(debuglevel_get());
276 static PyObject
*py_fault_setup(PyObject
*self
,
277 PyObject
*Py_UNUSED(ignored
))
287 static PyObject
*py_is_ntvfs_fileserver_built(PyObject
*self
,
288 PyObject
*Py_UNUSED(ignored
))
290 #ifdef WITH_NTVFS_FILESERVER
297 static PyObject
*py_is_heimdal_built(PyObject
*self
,
298 PyObject
*Py_UNUSED(ignored
))
300 #ifdef SAMBA4_USES_HEIMDAL
307 static PyObject
*py_is_ad_dc_built(PyObject
*self
,
308 PyObject
*Py_UNUSED(ignored
))
310 #ifdef AD_DC_BUILD_IS_ENABLED
317 static PyObject
*py_is_selftest_enabled(PyObject
*self
,
318 PyObject
*Py_UNUSED(ignored
))
320 #ifdef ENABLE_SELFTEST
327 static PyObject
*py_ndr_token_max_list_size(PyObject
*self
,
328 PyObject
*Py_UNUSED(ignored
))
330 return PyLong_FromLong(ndr_token_max_list_size());
334 return the list of interface IPs we have configured
335 takes an loadparm context, returns a list of IPs in string form
337 Does not return addresses on 127.0.0.0/8
339 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
345 struct loadparm_context
*lp_ctx
;
346 struct interface
*ifaces
;
348 int all_interfaces
= 1;
350 if (!PyArg_ParseTuple(args
, "O|i", &py_lp_ctx
, &all_interfaces
))
353 tmp_ctx
= talloc_new(NULL
);
354 if (tmp_ctx
== NULL
) {
359 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
360 if (lp_ctx
== NULL
) {
361 talloc_free(tmp_ctx
);
362 return PyErr_NoMemory();
365 load_interface_list(tmp_ctx
, lp_ctx
, &ifaces
);
367 count
= iface_list_count(ifaces
);
369 /* first count how many are not loopback addresses */
370 for (ifcount
= i
= 0; i
<count
; i
++) {
371 const char *ip
= iface_list_n_ip(ifaces
, i
);
373 if (all_interfaces
) {
378 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
382 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
386 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
390 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
397 pylist
= PyList_New(ifcount
);
398 for (ifcount
= i
= 0; i
<count
; i
++) {
399 const char *ip
= iface_list_n_ip(ifaces
, i
);
401 if (all_interfaces
) {
402 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
407 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
411 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
415 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
419 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
423 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
426 talloc_free(tmp_ctx
);
430 static PyObject
*py_strcasecmp_m(PyObject
*self
, PyObject
*args
)
432 const char *s1
= NULL
;
433 const char *s2
= NULL
;
435 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
437 "utf8", &s1
, "utf8", &s2
)) {
441 cmp_result
= strcasecmp_m(s1
, s2
);
442 PyMem_Free(discard_const_p(char, s1
));
443 PyMem_Free(discard_const_p(char, s2
));
444 return PyLong_FromLong(cmp_result
);
447 static PyObject
*py_strstr_m(PyObject
*self
, PyObject
*args
)
449 const char *s1
= NULL
;
450 const char *s2
= NULL
;
451 char *strstr_ret
= NULL
;
452 PyObject
*result
= NULL
;
453 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
455 "utf8", &s1
, "utf8", &s2
))
458 strstr_ret
= strstr_m(s1
, s2
);
460 PyMem_Free(discard_const_p(char, s1
));
461 PyMem_Free(discard_const_p(char, s2
));
464 result
= PyUnicode_FromString(strstr_ret
);
465 PyMem_Free(discard_const_p(char, s1
));
466 PyMem_Free(discard_const_p(char, s2
));
470 static PyObject
*py_get_burnt_commandline(PyObject
*self
, PyObject
*args
)
472 PyObject
*cmdline_as_list
, *ret
;
473 char *burnt_cmdline
= NULL
;
476 TALLOC_CTX
*frame
= talloc_stackframe();
479 if (!PyArg_ParseTuple(args
, "O!", &PyList_Type
, &cmdline_as_list
))
485 argc
= PyList_GET_SIZE(cmdline_as_list
);
492 argv
= PyList_AsStringList(frame
, cmdline_as_list
, "sys.argv");
498 burnt
= samba_cmdline_burn(argc
, argv
);
504 for (i
= 0; i
< argc
; i
++) {
506 burnt_cmdline
= talloc_strdup(frame
,
510 = talloc_asprintf_append(burnt_cmdline
,
514 if (burnt_cmdline
== NULL
) {
521 ret
= PyUnicode_FromString(burnt_cmdline
);
527 static PyMethodDef py_misc_methods
[] = {
528 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
529 "generate_random_str(len) -> string\n"
530 "Generate random string with specified length." },
531 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
532 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
533 "Generate random password (based on printable ascii characters) "
534 "with a length >= min and <= max." },
535 { "generate_random_machine_password", (PyCFunction
)py_generate_random_machine_password
,
536 METH_VARARGS
, "generate_random_machine_password(min, max) -> string\n"
537 "Generate random password "
538 "(based on random utf16 characters converted to utf8 or "
539 "random ascii characters if 'unix charset' is not 'utf8') "
540 "with a length >= min (at least 14) and <= max (at most 255)." },
541 { "check_password_quality", (PyCFunction
)py_check_password_quality
,
542 METH_VARARGS
, "check_password_quality(pass) -> bool\n"
543 "Check password quality against Samba's check_password_quality, "
544 "the implementation of Microsoft's rules: "
545 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
547 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
548 "unix2nttime(timestamp) -> nttime" },
549 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
550 "nttime2unix(nttime) -> timestamp" },
551 { "float2nttime", (PyCFunction
)py_float2nttime
, METH_VARARGS
,
552 "pytime2nttime(floattimestamp) -> nttime" },
553 { "nttime2float", (PyCFunction
)py_nttime2float
, METH_VARARGS
,
554 "nttime2pytime(nttime) -> floattimestamp" },
555 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
556 "nttime2string(nttime) -> string" },
557 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
559 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
561 { "fault_setup", (PyCFunction
)py_fault_setup
, METH_NOARGS
,
562 "setup the default samba panic handler" },
563 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
564 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
566 "get interface IP address list"},
567 { "strcasecmp_m", (PyCFunction
)py_strcasecmp_m
, METH_VARARGS
,
568 "(for testing) compare two strings using Samba's strcasecmp_m()"},
569 { "strstr_m", (PyCFunction
)py_strstr_m
, METH_VARARGS
,
570 "(for testing) find one string in another with Samba's strstr_m()"},
571 { "is_ntvfs_fileserver_built", (PyCFunction
)py_is_ntvfs_fileserver_built
, METH_NOARGS
,
572 "is the NTVFS file server built in this installation?" },
573 { "is_heimdal_built", (PyCFunction
)py_is_heimdal_built
, METH_NOARGS
,
574 "is Samba built with Heimdal Kerberbos?" },
575 { "generate_random_bytes",
576 (PyCFunction
)py_generate_random_bytes
,
578 "generate_random_bytes(len) -> bytes\n"
579 "Generate random bytes with specified length." },
580 { "is_ad_dc_built", (PyCFunction
)py_is_ad_dc_built
, METH_NOARGS
,
581 "is Samba built with AD DC?" },
582 { "is_selftest_enabled", (PyCFunction
)py_is_selftest_enabled
,
583 METH_NOARGS
, "is Samba built with selftest enabled?" },
584 { "ndr_token_max_list_size", (PyCFunction
)py_ndr_token_max_list_size
,
585 METH_NOARGS
, "How many NDR internal tokens is too many for this build?" },
586 { "get_burnt_commandline", (PyCFunction
)py_get_burnt_commandline
,
587 METH_VARARGS
, "Return a redacted commandline to feed to setproctitle (None if no redaction required)" },
591 static struct PyModuleDef moduledef
= {
592 PyModuleDef_HEAD_INIT
,
594 .m_doc
= "Python bindings for miscellaneous Samba functions.",
596 .m_methods
= py_misc_methods
,
599 MODULE_INIT_FUNC(_glue
)
603 debug_setup_talloc_log();
605 m
= PyModule_Create(&moduledef
);
609 PyModule_AddObject(m
, "version",
610 PyUnicode_FromString(SAMBA_VERSION_STRING
));
611 PyExc_NTSTATUSError
= PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError
, NULL
);
612 if (PyExc_NTSTATUSError
!= NULL
) {
613 Py_INCREF(PyExc_NTSTATUSError
);
614 PyModule_AddObject(m
, "NTSTATUSError", PyExc_NTSTATUSError
);
617 PyExc_WERRORError
= PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError
, NULL
);
618 if (PyExc_WERRORError
!= NULL
) {
619 Py_INCREF(PyExc_WERRORError
);
620 PyModule_AddObject(m
, "WERRORError", PyExc_WERRORError
);
623 PyExc_HRESULTError
= PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError
, NULL
);
624 if (PyExc_HRESULTError
!= NULL
) {
625 Py_INCREF(PyExc_HRESULTError
);
626 PyModule_AddObject(m
, "HRESULTError", PyExc_HRESULTError
);
629 PyExc_DsExtendedError
= PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError
, NULL
);
630 if (PyExc_DsExtendedError
!= NULL
) {
631 Py_INCREF(PyExc_DsExtendedError
);
632 PyModule_AddObject(m
, "DsExtendedError", PyExc_DsExtendedError
);