librpc ndr/py_security: Export security_ace_equal as richcmp to python
[Samba.git] / python / pyglue.c
blob60592874ae6467d8bdd7de853fa4b0abe27026bc
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 int len;
38 PyObject *ret;
39 char *retstr;
40 if (!PyArg_ParseTuple(args, "i", &len)) {
41 return NULL;
43 if (len < 0) {
44 PyErr_Format(PyExc_ValueError,
45 "random string length should be positive, not %d",
46 len);
47 return NULL;
49 retstr = generate_random_str(NULL, len);
50 ret = PyUnicode_FromString(retstr);
51 talloc_free(retstr);
52 return ret;
55 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
57 int min, max;
58 PyObject *ret;
59 char *retstr;
60 if (!PyArg_ParseTuple(args, "ii", &min, &max)) {
61 return NULL;
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",
71 min, max);
72 return NULL;
75 retstr = generate_random_password(NULL, min, max);
76 if (retstr == NULL) {
77 if (errno == EINVAL) {
78 PyErr_Format(PyExc_ValueError,
79 "invalid range: %d - %d",
80 min, max);
82 return NULL;
84 ret = PyUnicode_FromString(retstr);
85 talloc_free(retstr);
86 return ret;
89 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
91 int min, max;
92 PyObject *ret;
93 char *retstr;
94 if (!PyArg_ParseTuple(args, "ii", &min, &max)) {
95 return NULL;
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",
106 min, max);
107 return NULL;
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",
115 min, max);
117 return NULL;
119 ret = PyUnicode_FromString(retstr);
120 talloc_free(retstr);
121 return ret;
124 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
126 char *pass;
128 if (!PyArg_ParseTuple(args, "s", &pass)) {
129 return NULL;
132 return PyBool_FromLong(check_password_quality(pass));
135 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
137 int len;
138 PyObject *ret;
139 uint8_t *bytes = NULL;
141 if (!PyArg_ParseTuple(args, "i", &len)) {
142 return NULL;
144 if (len < 0) {
145 PyErr_Format(PyExc_ValueError,
146 "random bytes length should be positive, not %d",
147 len);
148 return NULL;
150 bytes = talloc_zero_size(NULL, len);
151 if (bytes == NULL) {
152 PyErr_NoMemory();
153 return NULL;
155 generate_random_buffer(bytes, len);
156 ret = PyBytes_FromStringAndSize((const char *)bytes, len);
157 talloc_free(bytes);
158 return ret;
161 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
163 time_t t;
164 unsigned int _t;
165 NTTIME nt;
167 if (!PyArg_ParseTuple(args, "I", &_t)) {
168 return NULL;
170 t = _t;
172 unix_to_nt_time(&nt, t);
174 return PyLong_FromLongLong((uint64_t)nt);
177 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
179 time_t t;
180 NTTIME nt;
181 if (!PyArg_ParseTuple(args, "K", &nt))
182 return NULL;
184 t = nt_time_to_unix(nt);
186 return PyLong_FromLong((uint64_t)t);
189 static PyObject *py_float2nttime(PyObject *self, PyObject *args)
191 double ft = 0;
192 double ft_sec = 0;
193 double ft_nsec = 0;
194 struct timespec ts;
195 NTTIME nt = 0;
197 if (!PyArg_ParseTuple(args, "d", &ft)) {
198 return NULL;
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)
214 double ft = 0;
215 struct timespec ts;
216 const struct timespec ts_zero = { .tv_sec = 0, };
217 NTTIME nt = 0;
219 if (!PyArg_ParseTuple(args, "K", &nt)) {
220 return NULL;
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)
234 PyObject *ret;
235 NTTIME nt;
236 TALLOC_CTX *tmp_ctx;
237 const char *string;
238 if (!PyArg_ParseTuple(args, "K", &nt))
239 return NULL;
241 tmp_ctx = talloc_new(NULL);
242 if (tmp_ctx == NULL) {
243 PyErr_NoMemory();
244 return NULL;
247 string = nt_time_string(tmp_ctx, nt);
248 ret = PyUnicode_FromString(string);
250 talloc_free(tmp_ctx);
252 return ret;
255 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
257 unsigned level;
258 if (!PyArg_ParseTuple(args, "I", &level))
259 return NULL;
260 debuglevel_set(level);
261 Py_RETURN_NONE;
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))
273 static bool done;
274 if (!done) {
275 fault_setup();
276 done = true;
278 Py_RETURN_NONE;
281 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
282 PyObject *Py_UNUSED(ignored))
284 #ifdef WITH_NTVFS_FILESERVER
285 Py_RETURN_TRUE;
286 #else
287 Py_RETURN_FALSE;
288 #endif
291 static PyObject *py_is_heimdal_built(PyObject *self,
292 PyObject *Py_UNUSED(ignored))
294 #ifdef SAMBA4_USES_HEIMDAL
295 Py_RETURN_TRUE;
296 #else
297 Py_RETURN_FALSE;
298 #endif
301 static PyObject *py_is_ad_dc_built(PyObject *self,
302 PyObject *Py_UNUSED(ignored))
304 #ifdef AD_DC_BUILD_IS_ENABLED
305 Py_RETURN_TRUE;
306 #else
307 Py_RETURN_FALSE;
308 #endif
311 static PyObject *py_is_selftest_enabled(PyObject *self,
312 PyObject *Py_UNUSED(ignored))
314 #ifdef ENABLE_SELFTEST
315 Py_RETURN_TRUE;
316 #else
317 Py_RETURN_FALSE;
318 #endif
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)
335 PyObject *pylist;
336 int count;
337 TALLOC_CTX *tmp_ctx;
338 PyObject *py_lp_ctx;
339 struct loadparm_context *lp_ctx;
340 struct interface *ifaces;
341 int i, ifcount;
342 int all_interfaces = 1;
344 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
345 return NULL;
347 tmp_ctx = talloc_new(NULL);
348 if (tmp_ctx == NULL) {
349 PyErr_NoMemory();
350 return NULL;
353 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
354 if (lp_ctx == NULL) {
355 talloc_free(tmp_ctx);
356 return NULL;
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) {
368 ifcount++;
369 continue;
372 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
373 continue;
376 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
377 continue;
380 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
381 continue;
384 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
385 continue;
388 ifcount++;
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));
397 ifcount++;
398 continue;
401 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
402 continue;
405 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
406 continue;
409 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
410 continue;
413 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
414 continue;
417 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
418 ifcount++;
420 talloc_free(tmp_ctx);
421 return pylist;
424 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
426 const char *s1 = NULL;
427 const char *s2 = NULL;
428 long cmp_result = 0;
429 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
430 PYARG_STR_UNI,
431 "utf8", &s1, "utf8", &s2)) {
432 return NULL;
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
448 PYARG_STR_UNI,
449 "utf8", &s1, "utf8", &s2))
450 return NULL;
452 strstr_ret = strstr_m(s1, s2);
453 if (!strstr_ret) {
454 PyMem_Free(discard_const_p(char, s1));
455 PyMem_Free(discard_const_p(char, s2));
456 Py_RETURN_NONE;
458 result = PyUnicode_FromString(strstr_ret);
459 PyMem_Free(discard_const_p(char, s1));
460 PyMem_Free(discard_const_p(char, s2));
461 return result;
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,
495 "set debug level" },
496 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
497 "get debug level" },
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"
502 "\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,
514 METH_VARARGS,
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,
528 .m_name = "_glue",
529 .m_doc = "Python bindings for miscellaneous Samba functions.",
530 .m_size = -1,
531 .m_methods = py_misc_methods,
534 MODULE_INIT_FUNC(_glue)
536 PyObject *m;
538 debug_setup_talloc_log();
540 m = PyModule_Create(&moduledef);
541 if (m == NULL)
542 return NULL;
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);
570 return m;