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"
27 #include "librpc/ndr/ndr_private.h"
30 static PyObject
*PyExc_NTSTATUSError
;
31 static PyObject
*PyExc_WERRORError
;
32 static PyObject
*PyExc_HRESULTError
;
33 static PyObject
*PyExc_DsExtendedError
;
35 static PyObject
*py_generate_random_str(PyObject
*self
, PyObject
*args
)
40 if (!PyArg_ParseTuple(args
, "i", &len
)) {
44 PyErr_Format(PyExc_ValueError
,
45 "random string length should be positive, not %d",
49 retstr
= generate_random_str(NULL
, len
);
50 ret
= PyUnicode_FromString(retstr
);
55 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
60 if (!PyArg_ParseTuple(args
, "ii", &min
, &max
)) {
63 if (max
< 0 || min
< 0) {
65 * The real range checks happen in generate_random_password().
66 * Here we are just checking the values won't overflow into
67 * numbers when cast to size_t.
69 PyErr_Format(PyExc_ValueError
,
70 "invalid range: %d - %d",
75 retstr
= generate_random_password(NULL
, min
, max
);
77 if (errno
== EINVAL
) {
78 PyErr_Format(PyExc_ValueError
,
79 "invalid range: %d - %d",
84 ret
= PyUnicode_FromString(retstr
);
89 static PyObject
*py_generate_random_machine_password(PyObject
*self
, PyObject
*args
)
94 if (!PyArg_ParseTuple(args
, "ii", &min
, &max
)) {
97 if (max
< 0 || min
< 0) {
99 * The real range checks happen in
100 * generate_random_machine_password().
101 * Here we are just checking the values won't overflow into
102 * numbers when cast to size_t.
104 PyErr_Format(PyExc_ValueError
,
105 "invalid range: %d - %d",
110 retstr
= generate_random_machine_password(NULL
, min
, max
);
111 if (retstr
== NULL
) {
112 if (errno
== EINVAL
) {
113 PyErr_Format(PyExc_ValueError
,
114 "invalid range: %d - %d",
119 ret
= PyUnicode_FromString(retstr
);
124 static PyObject
*py_check_password_quality(PyObject
*self
, PyObject
*args
)
128 if (!PyArg_ParseTuple(args
, "s", &pass
)) {
132 return PyBool_FromLong(check_password_quality(pass
));
135 static PyObject
*py_generate_random_bytes(PyObject
*self
, PyObject
*args
)
139 uint8_t *bytes
= NULL
;
141 if (!PyArg_ParseTuple(args
, "i", &len
)) {
145 PyErr_Format(PyExc_ValueError
,
146 "random bytes length should be positive, not %d",
150 bytes
= talloc_zero_size(NULL
, len
);
155 generate_random_buffer(bytes
, len
);
156 ret
= PyBytes_FromStringAndSize((const char *)bytes
, len
);
161 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
167 if (!PyArg_ParseTuple(args
, "I", &_t
)) {
172 unix_to_nt_time(&nt
, t
);
174 return PyLong_FromLongLong((uint64_t)nt
);
177 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
181 if (!PyArg_ParseTuple(args
, "K", &nt
))
184 t
= nt_time_to_unix(nt
);
186 return PyLong_FromLong((uint64_t)t
);
189 static PyObject
*py_float2nttime(PyObject
*self
, PyObject
*args
)
197 if (!PyArg_ParseTuple(args
, "d", &ft
)) {
201 ft_sec
= (double)(int)ft
;
202 ft_nsec
= (ft
- ft_sec
) * 1.0e+9;
204 ts
.tv_sec
= (int)ft_sec
;
205 ts
.tv_nsec
= (int)ft_nsec
;
207 nt
= full_timespec_to_nt_time(&ts
);
209 return PyLong_FromLongLong((uint64_t)nt
);
212 static PyObject
*py_nttime2float(PyObject
*self
, PyObject
*args
)
216 const struct timespec ts_zero
= { .tv_sec
= 0, };
219 if (!PyArg_ParseTuple(args
, "K", &nt
)) {
223 ts
= nt_time_to_full_timespec(nt
);
224 if (is_omit_timespec(&ts
)) {
225 return PyFloat_FromDouble(1.0);
227 ft
= timespec_elapsed2(&ts_zero
, &ts
);
229 return PyFloat_FromDouble(ft
);
232 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
238 if (!PyArg_ParseTuple(args
, "K", &nt
))
241 tmp_ctx
= talloc_new(NULL
);
242 if (tmp_ctx
== NULL
) {
247 string
= nt_time_string(tmp_ctx
, nt
);
248 ret
= PyUnicode_FromString(string
);
250 talloc_free(tmp_ctx
);
255 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
258 if (!PyArg_ParseTuple(args
, "I", &level
))
260 debuglevel_set(level
);
264 static PyObject
*py_get_debug_level(PyObject
*self
,
265 PyObject
*Py_UNUSED(ignored
))
267 return PyLong_FromLong(debuglevel_get());
270 static PyObject
*py_fault_setup(PyObject
*self
,
271 PyObject
*Py_UNUSED(ignored
))
281 static PyObject
*py_is_ntvfs_fileserver_built(PyObject
*self
,
282 PyObject
*Py_UNUSED(ignored
))
284 #ifdef WITH_NTVFS_FILESERVER
291 static PyObject
*py_is_heimdal_built(PyObject
*self
,
292 PyObject
*Py_UNUSED(ignored
))
294 #ifdef SAMBA4_USES_HEIMDAL
301 static PyObject
*py_is_ad_dc_built(PyObject
*self
,
302 PyObject
*Py_UNUSED(ignored
))
304 #ifdef AD_DC_BUILD_IS_ENABLED
311 static PyObject
*py_is_selftest_enabled(PyObject
*self
,
312 PyObject
*Py_UNUSED(ignored
))
314 #ifdef ENABLE_SELFTEST
321 static PyObject
*py_ndr_token_max_list_size(PyObject
*self
,
322 PyObject
*Py_UNUSED(ignored
))
324 return PyLong_FromLong(ndr_token_max_list_size());
328 return the list of interface IPs we have configured
329 takes an loadparm context, returns a list of IPs in string form
331 Does not return addresses on 127.0.0.0/8
333 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
339 struct loadparm_context
*lp_ctx
;
340 struct interface
*ifaces
;
342 int all_interfaces
= 1;
344 if (!PyArg_ParseTuple(args
, "O|i", &py_lp_ctx
, &all_interfaces
))
347 tmp_ctx
= talloc_new(NULL
);
348 if (tmp_ctx
== NULL
) {
353 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
354 if (lp_ctx
== NULL
) {
355 talloc_free(tmp_ctx
);
359 load_interface_list(tmp_ctx
, lp_ctx
, &ifaces
);
361 count
= iface_list_count(ifaces
);
363 /* first count how many are not loopback addresses */
364 for (ifcount
= i
= 0; i
<count
; i
++) {
365 const char *ip
= iface_list_n_ip(ifaces
, i
);
367 if (all_interfaces
) {
372 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
376 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
380 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
384 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
391 pylist
= PyList_New(ifcount
);
392 for (ifcount
= i
= 0; i
<count
; i
++) {
393 const char *ip
= iface_list_n_ip(ifaces
, i
);
395 if (all_interfaces
) {
396 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
401 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
405 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
409 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
413 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
417 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
420 talloc_free(tmp_ctx
);
424 static PyObject
*py_strcasecmp_m(PyObject
*self
, PyObject
*args
)
426 const char *s1
= NULL
;
427 const char *s2
= NULL
;
429 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
431 "utf8", &s1
, "utf8", &s2
)) {
435 cmp_result
= strcasecmp_m(s1
, s2
);
436 PyMem_Free(discard_const_p(char, s1
));
437 PyMem_Free(discard_const_p(char, s2
));
438 return PyLong_FromLong(cmp_result
);
441 static PyObject
*py_strstr_m(PyObject
*self
, PyObject
*args
)
443 const char *s1
= NULL
;
444 const char *s2
= NULL
;
445 char *strstr_ret
= NULL
;
446 PyObject
*result
= NULL
;
447 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
449 "utf8", &s1
, "utf8", &s2
))
452 strstr_ret
= strstr_m(s1
, s2
);
454 PyMem_Free(discard_const_p(char, s1
));
455 PyMem_Free(discard_const_p(char, s2
));
458 result
= PyUnicode_FromString(strstr_ret
);
459 PyMem_Free(discard_const_p(char, s1
));
460 PyMem_Free(discard_const_p(char, s2
));
464 static PyMethodDef py_misc_methods
[] = {
465 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
466 "generate_random_str(len) -> string\n"
467 "Generate random string with specified length." },
468 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
469 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
470 "Generate random password (based on printable ascii characters) "
471 "with a length >= min and <= max." },
472 { "generate_random_machine_password", (PyCFunction
)py_generate_random_machine_password
,
473 METH_VARARGS
, "generate_random_machine_password(min, max) -> string\n"
474 "Generate random password "
475 "(based on random utf16 characters converted to utf8 or "
476 "random ascii characters if 'unix charset' is not 'utf8')"
477 "with a length >= min (at least 14) and <= max (at most 255)." },
478 { "check_password_quality", (PyCFunction
)py_check_password_quality
,
479 METH_VARARGS
, "check_password_quality(pass) -> bool\n"
480 "Check password quality against Samba's check_password_quality,"
481 "the implementation of Microsoft's rules:"
482 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
484 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
485 "unix2nttime(timestamp) -> nttime" },
486 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
487 "nttime2unix(nttime) -> timestamp" },
488 { "float2nttime", (PyCFunction
)py_float2nttime
, METH_VARARGS
,
489 "pytime2nttime(floattimestamp) -> nttime" },
490 { "nttime2float", (PyCFunction
)py_nttime2float
, METH_VARARGS
,
491 "nttime2pytime(nttime) -> floattimestamp" },
492 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
493 "nttime2string(nttime) -> string" },
494 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
496 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
498 { "fault_setup", (PyCFunction
)py_fault_setup
, METH_NOARGS
,
499 "setup the default samba panic handler" },
500 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
501 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
503 "get interface IP address list"},
504 { "strcasecmp_m", (PyCFunction
)py_strcasecmp_m
, METH_VARARGS
,
505 "(for testing) compare two strings using Samba's strcasecmp_m()"},
506 { "strstr_m", (PyCFunction
)py_strstr_m
, METH_VARARGS
,
507 "(for testing) find one string in another with Samba's strstr_m()"},
508 { "is_ntvfs_fileserver_built", (PyCFunction
)py_is_ntvfs_fileserver_built
, METH_NOARGS
,
509 "is the NTVFS file server built in this installation?" },
510 { "is_heimdal_built", (PyCFunction
)py_is_heimdal_built
, METH_NOARGS
,
511 "is Samba built with Heimdal Kerberbos?" },
512 { "generate_random_bytes",
513 (PyCFunction
)py_generate_random_bytes
,
515 "generate_random_bytes(len) -> bytes\n"
516 "Generate random bytes with specified length." },
517 { "is_ad_dc_built", (PyCFunction
)py_is_ad_dc_built
, METH_NOARGS
,
518 "is Samba built with AD DC?" },
519 { "is_selftest_enabled", (PyCFunction
)py_is_selftest_enabled
,
520 METH_NOARGS
, "is Samba built with selftest enabled?" },
521 { "ndr_token_max_list_size", (PyCFunction
)py_ndr_token_max_list_size
,
522 METH_NOARGS
, "How many NDR internal tokens is too many for this build?" },
526 static struct PyModuleDef moduledef
= {
527 PyModuleDef_HEAD_INIT
,
529 .m_doc
= "Python bindings for miscellaneous Samba functions.",
531 .m_methods
= py_misc_methods
,
534 MODULE_INIT_FUNC(_glue
)
538 debug_setup_talloc_log();
540 m
= PyModule_Create(&moduledef
);
544 PyModule_AddObject(m
, "version",
545 PyUnicode_FromString(SAMBA_VERSION_STRING
));
546 PyExc_NTSTATUSError
= PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError
, NULL
);
547 if (PyExc_NTSTATUSError
!= NULL
) {
548 Py_INCREF(PyExc_NTSTATUSError
);
549 PyModule_AddObject(m
, "NTSTATUSError", PyExc_NTSTATUSError
);
552 PyExc_WERRORError
= PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError
, NULL
);
553 if (PyExc_WERRORError
!= NULL
) {
554 Py_INCREF(PyExc_WERRORError
);
555 PyModule_AddObject(m
, "WERRORError", PyExc_WERRORError
);
558 PyExc_HRESULTError
= PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError
, NULL
);
559 if (PyExc_HRESULTError
!= NULL
) {
560 Py_INCREF(PyExc_HRESULTError
);
561 PyModule_AddObject(m
, "HRESULTError", PyExc_HRESULTError
);
564 PyExc_DsExtendedError
= PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError
, NULL
);
565 if (PyExc_DsExtendedError
!= NULL
) {
566 Py_INCREF(PyExc_DsExtendedError
);
567 PyModule_AddObject(m
, "DsExtendedError", PyExc_DsExtendedError
);