netcmd: domain: fix attributes created by test setUp method
[Samba.git] / python / pyglue.c
blob90c3d6e28955ef07e2acf4da05c22a388077d117
1 /*
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 <Python.h>
21 #include "python/py3compat.h"
22 #include "includes.h"
23 #include "version.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"
29 void init_glue(void);
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)
37 Py_ssize_t len;
38 PyObject *ret;
39 char *retstr;
41 if (!PyArg_ParseTuple(args, "n", &len)) {
42 return NULL;
44 if (len < 0) {
45 PyErr_Format(PyExc_ValueError,
46 "random string length should be positive, not %zd",
47 len);
48 return NULL;
50 retstr = generate_random_str(NULL, len);
51 if (retstr == NULL) {
52 return PyErr_NoMemory();
54 ret = PyUnicode_FromStringAndSize(retstr, len);
55 talloc_free(retstr);
56 return ret;
59 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
61 Py_ssize_t min, max;
62 PyObject *ret;
63 char *retstr;
65 if (!PyArg_ParseTuple(args, "nn", &min, &max)) {
66 return NULL;
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",
75 min, max);
76 return NULL;
79 retstr = generate_random_password(NULL, min, max);
80 if (retstr == NULL) {
81 if (errno == EINVAL) {
82 return PyErr_Format(PyExc_ValueError,
83 "invalid range: %zd - %zd",
84 min, max);
86 return PyErr_NoMemory();
88 ret = PyUnicode_FromString(retstr);
89 talloc_free(retstr);
90 return ret;
93 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
95 Py_ssize_t min, max;
96 PyObject *ret;
97 char *retstr;
99 if (!PyArg_ParseTuple(args, "nn", &min, &max)) {
100 return NULL;
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",
110 min, max);
111 return NULL;
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",
119 min, max);
121 return PyErr_NoMemory();
123 ret = PyUnicode_FromString(retstr);
124 talloc_free(retstr);
125 return ret;
128 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
130 char *pass;
132 if (!PyArg_ParseTuple(args, "s", &pass)) {
133 return NULL;
136 return PyBool_FromLong(check_password_quality(pass));
139 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
141 Py_ssize_t len;
142 PyObject *ret;
143 uint8_t *bytes = NULL;
145 if (!PyArg_ParseTuple(args, "n", &len)) {
146 return NULL;
148 if (len < 0) {
149 PyErr_Format(PyExc_ValueError,
150 "random bytes length should be positive, not %zd",
151 len);
152 return NULL;
154 bytes = talloc_zero_size(NULL, len);
155 if (bytes == NULL) {
156 PyErr_NoMemory();
157 return NULL;
159 generate_random_buffer(bytes, len);
160 ret = PyBytes_FromStringAndSize((const char *)bytes, len);
161 talloc_free(bytes);
162 return ret;
165 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
167 time_t t;
168 unsigned int _t;
169 NTTIME nt;
171 if (!PyArg_ParseTuple(args, "I", &_t)) {
172 return NULL;
174 t = _t;
176 unix_to_nt_time(&nt, t);
178 return PyLong_FromLongLong((uint64_t)nt);
181 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
183 time_t t;
184 NTTIME nt;
185 if (!PyArg_ParseTuple(args, "K", &nt))
186 return NULL;
188 t = nt_time_to_unix(nt);
190 return PyLong_FromLong((uint64_t)t);
193 static PyObject *py_float2nttime(PyObject *self, PyObject *args)
195 double ft = 0;
196 double ft_sec = 0;
197 double ft_nsec = 0;
198 struct timespec ts;
199 NTTIME nt = 0;
201 if (!PyArg_ParseTuple(args, "d", &ft)) {
202 return NULL;
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)
218 double ft = 0;
219 struct timespec ts;
220 const struct timespec ts_zero = { .tv_sec = 0, };
221 NTTIME nt = 0;
223 if (!PyArg_ParseTuple(args, "K", &nt)) {
224 return NULL;
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)
238 PyObject *ret;
239 NTTIME nt;
240 TALLOC_CTX *tmp_ctx;
241 const char *string;
242 if (!PyArg_ParseTuple(args, "K", &nt))
243 return NULL;
245 tmp_ctx = talloc_new(NULL);
246 if (tmp_ctx == NULL) {
247 PyErr_NoMemory();
248 return NULL;
251 string = nt_time_string(tmp_ctx, nt);
252 ret = PyUnicode_FromString(string);
254 talloc_free(tmp_ctx);
256 return ret;
259 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
261 unsigned level;
262 if (!PyArg_ParseTuple(args, "I", &level))
263 return NULL;
264 debuglevel_set(level);
265 Py_RETURN_NONE;
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))
277 static bool done;
278 if (!done) {
279 fault_setup();
280 done = true;
282 Py_RETURN_NONE;
285 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
286 PyObject *Py_UNUSED(ignored))
288 #ifdef WITH_NTVFS_FILESERVER
289 Py_RETURN_TRUE;
290 #else
291 Py_RETURN_FALSE;
292 #endif
295 static PyObject *py_is_heimdal_built(PyObject *self,
296 PyObject *Py_UNUSED(ignored))
298 #ifdef SAMBA4_USES_HEIMDAL
299 Py_RETURN_TRUE;
300 #else
301 Py_RETURN_FALSE;
302 #endif
305 static PyObject *py_is_ad_dc_built(PyObject *self,
306 PyObject *Py_UNUSED(ignored))
308 #ifdef AD_DC_BUILD_IS_ENABLED
309 Py_RETURN_TRUE;
310 #else
311 Py_RETURN_FALSE;
312 #endif
315 static PyObject *py_is_selftest_enabled(PyObject *self,
316 PyObject *Py_UNUSED(ignored))
318 #ifdef ENABLE_SELFTEST
319 Py_RETURN_TRUE;
320 #else
321 Py_RETURN_FALSE;
322 #endif
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)
339 PyObject *pylist;
340 int count;
341 TALLOC_CTX *tmp_ctx;
342 PyObject *py_lp_ctx;
343 struct loadparm_context *lp_ctx;
344 struct interface *ifaces;
345 int i, ifcount;
346 int all_interfaces = 1;
348 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
349 return NULL;
351 tmp_ctx = talloc_new(NULL);
352 if (tmp_ctx == NULL) {
353 PyErr_NoMemory();
354 return 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) {
372 ifcount++;
373 continue;
376 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
377 continue;
380 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
381 continue;
384 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
385 continue;
388 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
389 continue;
392 ifcount++;
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));
401 ifcount++;
402 continue;
405 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
406 continue;
409 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
410 continue;
413 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
414 continue;
417 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
418 continue;
421 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
422 ifcount++;
424 talloc_free(tmp_ctx);
425 return pylist;
428 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
430 const char *s1 = NULL;
431 const char *s2 = NULL;
432 long cmp_result = 0;
433 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
434 PYARG_STR_UNI,
435 "utf8", &s1, "utf8", &s2)) {
436 return NULL;
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
452 PYARG_STR_UNI,
453 "utf8", &s1, "utf8", &s2))
454 return NULL;
456 strstr_ret = strstr_m(s1, s2);
457 if (!strstr_ret) {
458 PyMem_Free(discard_const_p(char, s1));
459 PyMem_Free(discard_const_p(char, s2));
460 Py_RETURN_NONE;
462 result = PyUnicode_FromString(strstr_ret);
463 PyMem_Free(discard_const_p(char, s1));
464 PyMem_Free(discard_const_p(char, s2));
465 return result;
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,
499 "set debug level" },
500 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
501 "get debug level" },
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"
506 "\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,
518 METH_VARARGS,
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,
532 .m_name = "_glue",
533 .m_doc = "Python bindings for miscellaneous Samba functions.",
534 .m_size = -1,
535 .m_methods = py_misc_methods,
538 MODULE_INIT_FUNC(_glue)
540 PyObject *m;
542 debug_setup_talloc_log();
544 m = PyModule_Create(&moduledef);
545 if (m == NULL)
546 return NULL;
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);
574 return m;