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/>.
20 #include "lib/replace/system/python.h"
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"
30 #include "lib/crypto/gkdi.h"
33 static PyObject
*PyExc_NTSTATUSError
;
34 static PyObject
*PyExc_WERRORError
;
35 static PyObject
*PyExc_HRESULTError
;
36 static PyObject
*PyExc_DsExtendedError
;
38 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
44 if (!PyArg_ParseTuple(args
, "n", &len
)) {
48 PyErr_Format(PyExc_ValueError
,
49 "random string length should be positive, not %zd",
53 retstr
= generate_random_str(NULL
, len
);
55 return PyErr_NoMemory();
57 ret
= PyUnicode_FromStringAndSize(retstr
, len
);
62 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
68 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
71 if (max
< 0 || min
< 0) {
73 * The real range checks happens in generate_random_password().
74 * Here just filter out any negative numbers.
76 PyErr_Format(PyExc_ValueError
,
77 "invalid range: %zd - %zd",
82 retstr
= generate_random_password(NULL
, min
, max
);
84 if (errno
== EINVAL
) {
85 return PyErr_Format(PyExc_ValueError
,
86 "invalid range: %zd - %zd",
89 return PyErr_NoMemory();
91 ret
= PyUnicode_FromString(retstr
);
96 static PyObject
*py_generate_random_machine_password(PyObject
*self
, PyObject
*args
)
102 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
105 if (max
< 0 || min
< 0) {
107 * The real range checks happens in
108 * generate_random_machine_password().
109 * Here we just filter out any negative numbers.
111 PyErr_Format(PyExc_ValueError
,
112 "invalid range: %zd - %zd",
117 retstr
= generate_random_machine_password(NULL
, min
, max
);
118 if (retstr
== NULL
) {
119 if (errno
== EINVAL
) {
120 return PyErr_Format(PyExc_ValueError
,
121 "invalid range: %zd - %zd",
124 return PyErr_NoMemory();
126 ret
= PyUnicode_FromString(retstr
);
131 static PyObject
*py_check_password_quality(PyObject
*self
, PyObject
*args
)
135 if (!PyArg_ParseTuple(args
, "s", &pass
)) {
139 return PyBool_FromLong(check_password_quality(pass
));
142 static PyObject
*py_generate_random_bytes(PyObject
*self
, PyObject
*args
)
146 uint8_t *bytes
= NULL
;
148 if (!PyArg_ParseTuple(args
, "n", &len
)) {
152 PyErr_Format(PyExc_ValueError
,
153 "random bytes length should be positive, not %zd",
157 bytes
= talloc_zero_size(NULL
, len
);
162 generate_random_buffer(bytes
, len
);
163 ret
= PyBytes_FromStringAndSize((const char *)bytes
, len
);
168 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
174 if (!PyArg_ParseTuple(args
, "I", &_t
)) {
179 unix_to_nt_time(&nt
, t
);
181 return PyLong_FromLongLong((uint64_t)nt
);
184 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
188 if (!PyArg_ParseTuple(args
, "K", &nt
))
191 t
= nt_time_to_unix(nt
);
193 return PyLong_FromLong((uint64_t)t
);
196 static PyObject
*py_float2nttime(PyObject
*self
, PyObject
*args
)
204 if (!PyArg_ParseTuple(args
, "d", &ft
)) {
208 ft_sec
= (double)(int)ft
;
209 ft_nsec
= (ft
- ft_sec
) * 1.0e+9;
211 ts
.tv_sec
= (int)ft_sec
;
212 ts
.tv_nsec
= (int)ft_nsec
;
214 nt
= full_timespec_to_nt_time(&ts
);
216 return PyLong_FromLongLong((uint64_t)nt
);
219 static PyObject
*py_nttime2float(PyObject
*self
, PyObject
*args
)
223 const struct timespec ts_zero
= { .tv_sec
= 0, };
226 if (!PyArg_ParseTuple(args
, "K", &nt
)) {
230 ts
= nt_time_to_full_timespec(nt
);
231 if (is_omit_timespec(&ts
)) {
232 return PyFloat_FromDouble(1.0);
234 ft
= timespec_elapsed2(&ts_zero
, &ts
);
236 return PyFloat_FromDouble(ft
);
239 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
245 if (!PyArg_ParseTuple(args
, "K", &nt
))
248 tmp_ctx
= talloc_new(NULL
);
249 if (tmp_ctx
== NULL
) {
254 string
= nt_time_string(tmp_ctx
, nt
);
255 ret
= PyUnicode_FromString(string
);
257 talloc_free(tmp_ctx
);
262 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
265 if (!PyArg_ParseTuple(args
, "I", &level
))
267 debuglevel_set(level
);
271 static PyObject
*py_get_debug_level(PyObject
*self
,
272 PyObject
*Py_UNUSED(ignored
))
274 return PyLong_FromLong(debuglevel_get());
277 static PyObject
*py_fault_setup(PyObject
*self
,
278 PyObject
*Py_UNUSED(ignored
))
288 static PyObject
*py_is_ntvfs_fileserver_built(PyObject
*self
,
289 PyObject
*Py_UNUSED(ignored
))
291 #ifdef WITH_NTVFS_FILESERVER
298 static PyObject
*py_is_heimdal_built(PyObject
*self
,
299 PyObject
*Py_UNUSED(ignored
))
301 #ifdef SAMBA4_USES_HEIMDAL
308 static PyObject
*py_is_ad_dc_built(PyObject
*self
,
309 PyObject
*Py_UNUSED(ignored
))
311 #ifdef AD_DC_BUILD_IS_ENABLED
318 static PyObject
*py_is_selftest_enabled(PyObject
*self
,
319 PyObject
*Py_UNUSED(ignored
))
321 #ifdef ENABLE_SELFTEST
328 static PyObject
*py_ndr_token_max_list_size(PyObject
*self
,
329 PyObject
*Py_UNUSED(ignored
))
331 return PyLong_FromLong(ndr_token_max_list_size());
335 return the list of interface IPs we have configured
336 takes an loadparm context, returns a list of IPs in string form
338 Does not return addresses on 127.0.0.0/8
340 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
346 struct loadparm_context
*lp_ctx
;
347 struct interface
*ifaces
;
349 int all_interfaces
= 1;
351 if (!PyArg_ParseTuple(args
, "O|i", &py_lp_ctx
, &all_interfaces
))
354 tmp_ctx
= talloc_new(NULL
);
355 if (tmp_ctx
== NULL
) {
360 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
361 if (lp_ctx
== NULL
) {
362 talloc_free(tmp_ctx
);
363 return PyErr_NoMemory();
366 load_interface_list(tmp_ctx
, lp_ctx
, &ifaces
);
368 count
= iface_list_count(ifaces
);
370 /* first count how many are not loopback addresses */
371 for (ifcount
= i
= 0; i
<count
; i
++) {
372 const char *ip
= iface_list_n_ip(ifaces
, i
);
374 if (all_interfaces
) {
379 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
383 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
387 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
391 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
398 pylist
= PyList_New(ifcount
);
399 for (ifcount
= i
= 0; i
<count
; i
++) {
400 const char *ip
= iface_list_n_ip(ifaces
, i
);
402 if (all_interfaces
) {
403 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
408 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
412 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
416 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
420 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
424 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
427 talloc_free(tmp_ctx
);
431 static PyObject
*py_strcasecmp_m(PyObject
*self
, PyObject
*args
)
436 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
438 "utf8", &s1
, "utf8", &s2
)) {
442 cmp_result
= strcasecmp_m(s1
, s2
);
445 return PyLong_FromLong(cmp_result
);
448 static PyObject
*py_strstr_m(PyObject
*self
, PyObject
*args
)
452 char *strstr_ret
= NULL
;
453 PyObject
*result
= NULL
;
454 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
456 "utf8", &s1
, "utf8", &s2
))
459 strstr_ret
= strstr_m(s1
, s2
);
465 result
= PyUnicode_FromString(strstr_ret
);
471 static PyObject
*py_get_burnt_commandline(PyObject
*self
, PyObject
*args
)
473 PyObject
*cmdline_as_list
, *ret
;
474 char *burnt_cmdline
= NULL
;
477 TALLOC_CTX
*frame
= talloc_stackframe();
480 if (!PyArg_ParseTuple(args
, "O!", &PyList_Type
, &cmdline_as_list
))
486 argc
= PyList_GET_SIZE(cmdline_as_list
);
493 argv
= PyList_AsStringList(frame
, cmdline_as_list
, "sys.argv");
499 burnt
= samba_cmdline_burn(argc
, argv
);
505 for (i
= 0; i
< argc
; i
++) {
507 burnt_cmdline
= talloc_strdup(frame
,
511 = talloc_asprintf_append(burnt_cmdline
,
515 if (burnt_cmdline
== NULL
) {
522 ret
= PyUnicode_FromString(burnt_cmdline
);
528 static PyMethodDef py_misc_methods
[] = {
529 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
530 "generate_random_str(len) -> string\n"
531 "Generate random string with specified length." },
532 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
533 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
534 "Generate random password (based on printable ascii characters) "
535 "with a length >= min and <= max." },
536 { "generate_random_machine_password", (PyCFunction
)py_generate_random_machine_password
,
537 METH_VARARGS
, "generate_random_machine_password(min, max) -> string\n"
538 "Generate random password "
539 "(based on random utf16 characters converted to utf8 or "
540 "random ascii characters if 'unix charset' is not 'utf8') "
541 "with a length >= min (at least 14) and <= max (at most 255)." },
542 { "check_password_quality", (PyCFunction
)py_check_password_quality
,
543 METH_VARARGS
, "check_password_quality(pass) -> bool\n"
544 "Check password quality against Samba's check_password_quality, "
545 "the implementation of Microsoft's rules: "
546 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
548 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
549 "unix2nttime(timestamp) -> nttime" },
550 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
551 "nttime2unix(nttime) -> timestamp" },
552 { "float2nttime", (PyCFunction
)py_float2nttime
, METH_VARARGS
,
553 "pytime2nttime(floattimestamp) -> nttime" },
554 { "nttime2float", (PyCFunction
)py_nttime2float
, METH_VARARGS
,
555 "nttime2pytime(nttime) -> floattimestamp" },
556 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
557 "nttime2string(nttime) -> string" },
558 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
560 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
562 { "fault_setup", (PyCFunction
)py_fault_setup
, METH_NOARGS
,
563 "setup the default samba panic handler" },
564 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
565 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
567 "get interface IP address list"},
568 { "strcasecmp_m", (PyCFunction
)py_strcasecmp_m
, METH_VARARGS
,
569 "(for testing) compare two strings using Samba's strcasecmp_m()"},
570 { "strstr_m", (PyCFunction
)py_strstr_m
, METH_VARARGS
,
571 "(for testing) find one string in another with Samba's strstr_m()"},
572 { "is_ntvfs_fileserver_built", (PyCFunction
)py_is_ntvfs_fileserver_built
, METH_NOARGS
,
573 "is the NTVFS file server built in this installation?" },
574 { "is_heimdal_built", (PyCFunction
)py_is_heimdal_built
, METH_NOARGS
,
575 "is Samba built with Heimdal Kerberos?" },
576 { "generate_random_bytes",
577 (PyCFunction
)py_generate_random_bytes
,
579 "generate_random_bytes(len) -> bytes\n"
580 "Generate random bytes with specified length." },
581 { "is_ad_dc_built", (PyCFunction
)py_is_ad_dc_built
, METH_NOARGS
,
582 "is Samba built with AD DC?" },
583 { "is_selftest_enabled", (PyCFunction
)py_is_selftest_enabled
,
584 METH_NOARGS
, "is Samba built with selftest enabled?" },
585 { "ndr_token_max_list_size", (PyCFunction
)py_ndr_token_max_list_size
,
586 METH_NOARGS
, "How many NDR internal tokens is too many for this build?" },
587 { "get_burnt_commandline", (PyCFunction
)py_get_burnt_commandline
,
588 METH_VARARGS
, "Return a redacted commandline to feed to setproctitle (None if no redaction required)" },
592 static struct PyModuleDef moduledef
= {
593 PyModuleDef_HEAD_INIT
,
595 .m_doc
= "Python bindings for miscellaneous Samba functions.",
597 .m_methods
= py_misc_methods
,
600 MODULE_INIT_FUNC(_glue
)
603 PyObject
*py_obj
= NULL
;
606 debug_setup_talloc_log();
608 m
= PyModule_Create(&moduledef
);
612 PyModule_AddObject(m
, "version",
613 PyUnicode_FromString(SAMBA_VERSION_STRING
));
614 PyExc_NTSTATUSError
= PyErr_NewException("samba.NTSTATUSError", PyExc_RuntimeError
, NULL
);
615 if (PyExc_NTSTATUSError
!= NULL
) {
616 Py_INCREF(PyExc_NTSTATUSError
);
617 PyModule_AddObject(m
, "NTSTATUSError", PyExc_NTSTATUSError
);
620 PyExc_WERRORError
= PyErr_NewException("samba.WERRORError", PyExc_RuntimeError
, NULL
);
621 if (PyExc_WERRORError
!= NULL
) {
622 Py_INCREF(PyExc_WERRORError
);
623 PyModule_AddObject(m
, "WERRORError", PyExc_WERRORError
);
626 PyExc_HRESULTError
= PyErr_NewException("samba.HRESULTError", PyExc_RuntimeError
, NULL
);
627 if (PyExc_HRESULTError
!= NULL
) {
628 Py_INCREF(PyExc_HRESULTError
);
629 PyModule_AddObject(m
, "HRESULTError", PyExc_HRESULTError
);
632 PyExc_DsExtendedError
= PyErr_NewException("samba.DsExtendedError", PyExc_RuntimeError
, NULL
);
633 if (PyExc_DsExtendedError
!= NULL
) {
634 Py_INCREF(PyExc_DsExtendedError
);
635 PyModule_AddObject(m
, "DsExtendedError", PyExc_DsExtendedError
);
638 ret
= PyModule_AddIntConstant(m
, "GKDI_L1_KEY_ITERATION", gkdi_l1_key_iteration
);
643 ret
= PyModule_AddIntConstant(m
, "GKDI_L2_KEY_ITERATION", gkdi_l2_key_iteration
);
648 py_obj
= PyLong_FromLongLong(gkdi_key_cycle_duration
);
649 if (py_obj
== NULL
) {
653 ret
= PyModule_AddObject(m
, "GKDI_KEY_CYCLE_DURATION", py_obj
);
659 py_obj
= PyLong_FromLongLong(gkdi_max_clock_skew
);
660 if (py_obj
== NULL
) {
664 ret
= PyModule_AddObject(m
, "GKDI_MAX_CLOCK_SKEW", py_obj
);