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
)
41 if (!PyArg_ParseTuple(args
, "n", &len
)) {
45 PyErr_Format(PyExc_ValueError
,
46 "random string length should be positive, not %zd",
50 retstr
= generate_random_str(NULL
, len
);
52 return PyErr_NoMemory();
54 ret
= PyUnicode_FromStringAndSize(retstr
, len
);
59 static PyObject
*py_generate_random_password(PyObject
*self
, PyObject
*args
)
65 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
68 if (max
< 0 || min
< 0) {
70 * The real range checks happens in generate_random_password().
71 * Here just filter out any negative numbers.
73 PyErr_Format(PyExc_ValueError
,
74 "invalid range: %zd - %zd",
79 retstr
= generate_random_password(NULL
, min
, max
);
81 if (errno
== EINVAL
) {
82 return PyErr_Format(PyExc_ValueError
,
83 "invalid range: %zd - %zd",
86 return PyErr_NoMemory();
88 ret
= PyUnicode_FromString(retstr
);
93 static PyObject
*py_generate_random_machine_password(PyObject
*self
, PyObject
*args
)
99 if (!PyArg_ParseTuple(args
, "nn", &min
, &max
)) {
102 if (max
< 0 || min
< 0) {
104 * The real range checks happens in
105 * generate_random_machine_password().
106 * Here we just filter out any negative numbers.
108 PyErr_Format(PyExc_ValueError
,
109 "invalid range: %zd - %zd",
114 retstr
= generate_random_machine_password(NULL
, min
, max
);
115 if (retstr
== NULL
) {
116 if (errno
== EINVAL
) {
117 return PyErr_Format(PyExc_ValueError
,
118 "invalid range: %zd - %zd",
121 return PyErr_NoMemory();
123 ret
= PyUnicode_FromString(retstr
);
128 static PyObject
*py_check_password_quality(PyObject
*self
, PyObject
*args
)
132 if (!PyArg_ParseTuple(args
, "s", &pass
)) {
136 return PyBool_FromLong(check_password_quality(pass
));
139 static PyObject
*py_generate_random_bytes(PyObject
*self
, PyObject
*args
)
143 uint8_t *bytes
= NULL
;
145 if (!PyArg_ParseTuple(args
, "n", &len
)) {
149 PyErr_Format(PyExc_ValueError
,
150 "random bytes length should be positive, not %zd",
154 bytes
= talloc_zero_size(NULL
, len
);
159 generate_random_buffer(bytes
, len
);
160 ret
= PyBytes_FromStringAndSize((const char *)bytes
, len
);
165 static PyObject
*py_unix2nttime(PyObject
*self
, PyObject
*args
)
171 if (!PyArg_ParseTuple(args
, "I", &_t
)) {
176 unix_to_nt_time(&nt
, t
);
178 return PyLong_FromLongLong((uint64_t)nt
);
181 static PyObject
*py_nttime2unix(PyObject
*self
, PyObject
*args
)
185 if (!PyArg_ParseTuple(args
, "K", &nt
))
188 t
= nt_time_to_unix(nt
);
190 return PyLong_FromLong((uint64_t)t
);
193 static PyObject
*py_float2nttime(PyObject
*self
, PyObject
*args
)
201 if (!PyArg_ParseTuple(args
, "d", &ft
)) {
205 ft_sec
= (double)(int)ft
;
206 ft_nsec
= (ft
- ft_sec
) * 1.0e+9;
208 ts
.tv_sec
= (int)ft_sec
;
209 ts
.tv_nsec
= (int)ft_nsec
;
211 nt
= full_timespec_to_nt_time(&ts
);
213 return PyLong_FromLongLong((uint64_t)nt
);
216 static PyObject
*py_nttime2float(PyObject
*self
, PyObject
*args
)
220 const struct timespec ts_zero
= { .tv_sec
= 0, };
223 if (!PyArg_ParseTuple(args
, "K", &nt
)) {
227 ts
= nt_time_to_full_timespec(nt
);
228 if (is_omit_timespec(&ts
)) {
229 return PyFloat_FromDouble(1.0);
231 ft
= timespec_elapsed2(&ts_zero
, &ts
);
233 return PyFloat_FromDouble(ft
);
236 static PyObject
*py_nttime2string(PyObject
*self
, PyObject
*args
)
242 if (!PyArg_ParseTuple(args
, "K", &nt
))
245 tmp_ctx
= talloc_new(NULL
);
246 if (tmp_ctx
== NULL
) {
251 string
= nt_time_string(tmp_ctx
, nt
);
252 ret
= PyUnicode_FromString(string
);
254 talloc_free(tmp_ctx
);
259 static PyObject
*py_set_debug_level(PyObject
*self
, PyObject
*args
)
262 if (!PyArg_ParseTuple(args
, "I", &level
))
264 debuglevel_set(level
);
268 static PyObject
*py_get_debug_level(PyObject
*self
,
269 PyObject
*Py_UNUSED(ignored
))
271 return PyLong_FromLong(debuglevel_get());
274 static PyObject
*py_fault_setup(PyObject
*self
,
275 PyObject
*Py_UNUSED(ignored
))
285 static PyObject
*py_is_ntvfs_fileserver_built(PyObject
*self
,
286 PyObject
*Py_UNUSED(ignored
))
288 #ifdef WITH_NTVFS_FILESERVER
295 static PyObject
*py_is_heimdal_built(PyObject
*self
,
296 PyObject
*Py_UNUSED(ignored
))
298 #ifdef SAMBA4_USES_HEIMDAL
305 static PyObject
*py_is_ad_dc_built(PyObject
*self
,
306 PyObject
*Py_UNUSED(ignored
))
308 #ifdef AD_DC_BUILD_IS_ENABLED
315 static PyObject
*py_is_selftest_enabled(PyObject
*self
,
316 PyObject
*Py_UNUSED(ignored
))
318 #ifdef ENABLE_SELFTEST
325 static PyObject
*py_ndr_token_max_list_size(PyObject
*self
,
326 PyObject
*Py_UNUSED(ignored
))
328 return PyLong_FromLong(ndr_token_max_list_size());
332 return the list of interface IPs we have configured
333 takes an loadparm context, returns a list of IPs in string form
335 Does not return addresses on 127.0.0.0/8
337 static PyObject
*py_interface_ips(PyObject
*self
, PyObject
*args
)
343 struct loadparm_context
*lp_ctx
;
344 struct interface
*ifaces
;
346 int all_interfaces
= 1;
348 if (!PyArg_ParseTuple(args
, "O|i", &py_lp_ctx
, &all_interfaces
))
351 tmp_ctx
= talloc_new(NULL
);
352 if (tmp_ctx
== NULL
) {
357 lp_ctx
= lpcfg_from_py_object(tmp_ctx
, py_lp_ctx
);
358 if (lp_ctx
== NULL
) {
359 talloc_free(tmp_ctx
);
360 return PyErr_NoMemory();
363 load_interface_list(tmp_ctx
, lp_ctx
, &ifaces
);
365 count
= iface_list_count(ifaces
);
367 /* first count how many are not loopback addresses */
368 for (ifcount
= i
= 0; i
<count
; i
++) {
369 const char *ip
= iface_list_n_ip(ifaces
, i
);
371 if (all_interfaces
) {
376 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
380 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
384 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
388 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
395 pylist
= PyList_New(ifcount
);
396 for (ifcount
= i
= 0; i
<count
; i
++) {
397 const char *ip
= iface_list_n_ip(ifaces
, i
);
399 if (all_interfaces
) {
400 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
405 if (iface_list_same_net(ip
, "127.0.0.1", "255.0.0.0")) {
409 if (iface_list_same_net(ip
, "169.254.0.0", "255.255.0.0")) {
413 if (iface_list_same_net(ip
, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
417 if (iface_list_same_net(ip
, "fe80::", "ffff:ffff:ffff:ffff::")) {
421 PyList_SetItem(pylist
, ifcount
, PyUnicode_FromString(ip
));
424 talloc_free(tmp_ctx
);
428 static PyObject
*py_strcasecmp_m(PyObject
*self
, PyObject
*args
)
430 const char *s1
= NULL
;
431 const char *s2
= NULL
;
433 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
435 "utf8", &s1
, "utf8", &s2
)) {
439 cmp_result
= strcasecmp_m(s1
, s2
);
440 PyMem_Free(discard_const_p(char, s1
));
441 PyMem_Free(discard_const_p(char, s2
));
442 return PyLong_FromLong(cmp_result
);
445 static PyObject
*py_strstr_m(PyObject
*self
, PyObject
*args
)
447 const char *s1
= NULL
;
448 const char *s2
= NULL
;
449 char *strstr_ret
= NULL
;
450 PyObject
*result
= NULL
;
451 if (!PyArg_ParseTuple(args
, PYARG_STR_UNI
453 "utf8", &s1
, "utf8", &s2
))
456 strstr_ret
= strstr_m(s1
, s2
);
458 PyMem_Free(discard_const_p(char, s1
));
459 PyMem_Free(discard_const_p(char, s2
));
462 result
= PyUnicode_FromString(strstr_ret
);
463 PyMem_Free(discard_const_p(char, s1
));
464 PyMem_Free(discard_const_p(char, s2
));
468 static PyMethodDef py_misc_methods
[] = {
469 { "generate_random_str", (PyCFunction
)py_generate_random_str
, METH_VARARGS
,
470 "generate_random_str(len) -> string\n"
471 "Generate random string with specified length." },
472 { "generate_random_password", (PyCFunction
)py_generate_random_password
,
473 METH_VARARGS
, "generate_random_password(min, max) -> string\n"
474 "Generate random password (based on printable ascii characters) "
475 "with a length >= min and <= max." },
476 { "generate_random_machine_password", (PyCFunction
)py_generate_random_machine_password
,
477 METH_VARARGS
, "generate_random_machine_password(min, max) -> string\n"
478 "Generate random password "
479 "(based on random utf16 characters converted to utf8 or "
480 "random ascii characters if 'unix charset' is not 'utf8')"
481 "with a length >= min (at least 14) and <= max (at most 255)." },
482 { "check_password_quality", (PyCFunction
)py_check_password_quality
,
483 METH_VARARGS
, "check_password_quality(pass) -> bool\n"
484 "Check password quality against Samba's check_password_quality,"
485 "the implementation of Microsoft's rules:"
486 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
488 { "unix2nttime", (PyCFunction
)py_unix2nttime
, METH_VARARGS
,
489 "unix2nttime(timestamp) -> nttime" },
490 { "nttime2unix", (PyCFunction
)py_nttime2unix
, METH_VARARGS
,
491 "nttime2unix(nttime) -> timestamp" },
492 { "float2nttime", (PyCFunction
)py_float2nttime
, METH_VARARGS
,
493 "pytime2nttime(floattimestamp) -> nttime" },
494 { "nttime2float", (PyCFunction
)py_nttime2float
, METH_VARARGS
,
495 "nttime2pytime(nttime) -> floattimestamp" },
496 { "nttime2string", (PyCFunction
)py_nttime2string
, METH_VARARGS
,
497 "nttime2string(nttime) -> string" },
498 { "set_debug_level", (PyCFunction
)py_set_debug_level
, METH_VARARGS
,
500 { "get_debug_level", (PyCFunction
)py_get_debug_level
, METH_NOARGS
,
502 { "fault_setup", (PyCFunction
)py_fault_setup
, METH_NOARGS
,
503 "setup the default samba panic handler" },
504 { "interface_ips", (PyCFunction
)py_interface_ips
, METH_VARARGS
,
505 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
507 "get interface IP address list"},
508 { "strcasecmp_m", (PyCFunction
)py_strcasecmp_m
, METH_VARARGS
,
509 "(for testing) compare two strings using Samba's strcasecmp_m()"},
510 { "strstr_m", (PyCFunction
)py_strstr_m
, METH_VARARGS
,
511 "(for testing) find one string in another with Samba's strstr_m()"},
512 { "is_ntvfs_fileserver_built", (PyCFunction
)py_is_ntvfs_fileserver_built
, METH_NOARGS
,
513 "is the NTVFS file server built in this installation?" },
514 { "is_heimdal_built", (PyCFunction
)py_is_heimdal_built
, METH_NOARGS
,
515 "is Samba built with Heimdal Kerberbos?" },
516 { "generate_random_bytes",
517 (PyCFunction
)py_generate_random_bytes
,
519 "generate_random_bytes(len) -> bytes\n"
520 "Generate random bytes with specified length." },
521 { "is_ad_dc_built", (PyCFunction
)py_is_ad_dc_built
, METH_NOARGS
,
522 "is Samba built with AD DC?" },
523 { "is_selftest_enabled", (PyCFunction
)py_is_selftest_enabled
,
524 METH_NOARGS
, "is Samba built with selftest enabled?" },
525 { "ndr_token_max_list_size", (PyCFunction
)py_ndr_token_max_list_size
,
526 METH_NOARGS
, "How many NDR internal tokens is too many for this build?" },
530 static struct PyModuleDef moduledef
= {
531 PyModuleDef_HEAD_INIT
,
533 .m_doc
= "Python bindings for miscellaneous Samba functions.",
535 .m_methods
= py_misc_methods
,
538 MODULE_INIT_FUNC(_glue
)
542 debug_setup_talloc_log();
544 m
= PyModule_Create(&moduledef
);
548 PyModule_AddObject(m
, "version",
549 PyUnicode_FromString(SAMBA_VERSION_STRING
));
550 PyExc_NTSTATUSError
= PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError
, NULL
);
551 if (PyExc_NTSTATUSError
!= NULL
) {
552 Py_INCREF(PyExc_NTSTATUSError
);
553 PyModule_AddObject(m
, "NTSTATUSError", PyExc_NTSTATUSError
);
556 PyExc_WERRORError
= PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError
, NULL
);
557 if (PyExc_WERRORError
!= NULL
) {
558 Py_INCREF(PyExc_WERRORError
);
559 PyModule_AddObject(m
, "WERRORError", PyExc_WERRORError
);
562 PyExc_HRESULTError
= PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError
, NULL
);
563 if (PyExc_HRESULTError
!= NULL
) {
564 Py_INCREF(PyExc_HRESULTError
);
565 PyModule_AddObject(m
, "HRESULTError", PyExc_HRESULTError
);
568 PyExc_DsExtendedError
= PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError
, NULL
);
569 if (PyExc_DsExtendedError
!= NULL
) {
570 Py_INCREF(PyExc_DsExtendedError
);
571 PyModule_AddObject(m
, "DsExtendedError", PyExc_DsExtendedError
);