samba-tool: Add a gpo command for listing Sudoers Group Policies
[Samba.git] / python / pyglue.c
blob156eaf731505c70426538f3dfededdf0871639ad
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"
28 void init_glue(void);
29 static PyObject *PyExc_NTSTATUSError;
30 static PyObject *PyExc_WERRORError;
31 static PyObject *PyExc_HRESULTError;
32 static PyObject *PyExc_DsExtendedError;
34 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
36 int len;
37 PyObject *ret;
38 char *retstr;
39 if (!PyArg_ParseTuple(args, "i", &len))
40 return NULL;
42 retstr = generate_random_str(NULL, len);
43 ret = PyUnicode_FromString(retstr);
44 talloc_free(retstr);
45 return ret;
48 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
50 int min, max;
51 PyObject *ret;
52 char *retstr;
53 if (!PyArg_ParseTuple(args, "ii", &min, &max))
54 return NULL;
56 retstr = generate_random_password(NULL, min, max);
57 if (retstr == NULL) {
58 return NULL;
60 ret = PyUnicode_FromString(retstr);
61 talloc_free(retstr);
62 return ret;
65 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
67 int min, max;
68 PyObject *ret;
69 char *retstr;
70 if (!PyArg_ParseTuple(args, "ii", &min, &max))
71 return NULL;
73 retstr = generate_random_machine_password(NULL, min, max);
74 if (retstr == NULL) {
75 return NULL;
77 ret = PyUnicode_FromString(retstr);
78 talloc_free(retstr);
79 return ret;
82 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
84 char *pass;
86 if (!PyArg_ParseTuple(args, "s", &pass)) {
87 return NULL;
90 return PyBool_FromLong(check_password_quality(pass));
93 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
95 int len;
96 PyObject *ret;
97 uint8_t *bytes = NULL;
99 if (!PyArg_ParseTuple(args, "i", &len))
100 return NULL;
102 bytes = talloc_zero_size(NULL, len);
103 generate_random_buffer(bytes, len);
104 ret = PyBytes_FromStringAndSize((const char *)bytes, len);
105 talloc_free(bytes);
106 return ret;
109 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
111 time_t t;
112 unsigned int _t;
113 NTTIME nt;
115 if (!PyArg_ParseTuple(args, "I", &_t)) {
116 return NULL;
118 t = _t;
120 unix_to_nt_time(&nt, t);
122 return PyLong_FromLongLong((uint64_t)nt);
125 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
127 time_t t;
128 NTTIME nt;
129 if (!PyArg_ParseTuple(args, "K", &nt))
130 return NULL;
132 t = nt_time_to_unix(nt);
134 return PyLong_FromLong((uint64_t)t);
137 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
139 PyObject *ret;
140 NTTIME nt;
141 TALLOC_CTX *tmp_ctx;
142 const char *string;
143 if (!PyArg_ParseTuple(args, "K", &nt))
144 return NULL;
146 tmp_ctx = talloc_new(NULL);
147 if (tmp_ctx == NULL) {
148 PyErr_NoMemory();
149 return NULL;
152 string = nt_time_string(tmp_ctx, nt);
153 ret = PyUnicode_FromString(string);
155 talloc_free(tmp_ctx);
157 return ret;
160 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
162 unsigned level;
163 if (!PyArg_ParseTuple(args, "I", &level))
164 return NULL;
165 debuglevel_set(level);
166 Py_RETURN_NONE;
169 static PyObject *py_get_debug_level(PyObject *self,
170 PyObject *Py_UNUSED(ignored))
172 return PyLong_FromLong(debuglevel_get());
175 static PyObject *py_fault_setup(PyObject *self,
176 PyObject *Py_UNUSED(ignored))
178 static bool done;
179 if (!done) {
180 fault_setup();
181 done = true;
183 Py_RETURN_NONE;
186 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
187 PyObject *Py_UNUSED(ignored))
189 #ifdef WITH_NTVFS_FILESERVER
190 Py_RETURN_TRUE;
191 #else
192 Py_RETURN_FALSE;
193 #endif
196 static PyObject *py_is_heimdal_built(PyObject *self,
197 PyObject *Py_UNUSED(ignored))
199 #ifdef SAMBA4_USES_HEIMDAL
200 Py_RETURN_TRUE;
201 #else
202 Py_RETURN_FALSE;
203 #endif
206 static PyObject *py_is_ad_dc_built(PyObject *self,
207 PyObject *Py_UNUSED(ignored))
209 #ifdef AD_DC_BUILD_IS_ENABLED
210 Py_RETURN_TRUE;
211 #else
212 Py_RETURN_FALSE;
213 #endif
217 return the list of interface IPs we have configured
218 takes an loadparm context, returns a list of IPs in string form
220 Does not return addresses on 127.0.0.0/8
222 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
224 PyObject *pylist;
225 int count;
226 TALLOC_CTX *tmp_ctx;
227 PyObject *py_lp_ctx;
228 struct loadparm_context *lp_ctx;
229 struct interface *ifaces;
230 int i, ifcount;
231 int all_interfaces = 1;
233 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
234 return NULL;
236 tmp_ctx = talloc_new(NULL);
237 if (tmp_ctx == NULL) {
238 PyErr_NoMemory();
239 return NULL;
242 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
243 if (lp_ctx == NULL) {
244 talloc_free(tmp_ctx);
245 return NULL;
248 load_interface_list(tmp_ctx, lp_ctx, &ifaces);
250 count = iface_list_count(ifaces);
252 /* first count how many are not loopback addresses */
253 for (ifcount = i = 0; i<count; i++) {
254 const char *ip = iface_list_n_ip(ifaces, i);
256 if (all_interfaces) {
257 ifcount++;
258 continue;
261 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
262 continue;
265 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
266 continue;
269 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
270 continue;
273 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
274 continue;
277 ifcount++;
280 pylist = PyList_New(ifcount);
281 for (ifcount = i = 0; i<count; i++) {
282 const char *ip = iface_list_n_ip(ifaces, i);
284 if (all_interfaces) {
285 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
286 ifcount++;
287 continue;
290 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
291 continue;
294 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
295 continue;
298 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
299 continue;
302 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
303 continue;
306 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
307 ifcount++;
309 talloc_free(tmp_ctx);
310 return pylist;
313 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
315 const char *s1 = NULL;
316 const char *s2 = NULL;
317 long cmp_result = 0;
318 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
319 PYARG_STR_UNI,
320 "utf8", &s1, "utf8", &s2)) {
321 return NULL;
324 cmp_result = strcasecmp_m(s1, s2);
325 PyMem_Free(discard_const_p(char, s1));
326 PyMem_Free(discard_const_p(char, s2));
327 return PyLong_FromLong(cmp_result);
330 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
332 const char *s1 = NULL;
333 const char *s2 = NULL;
334 char *strstr_ret = NULL;
335 PyObject *result = NULL;
336 if (!PyArg_ParseTuple(args, PYARG_STR_UNI
337 PYARG_STR_UNI,
338 "utf8", &s1, "utf8", &s2))
339 return NULL;
341 strstr_ret = strstr_m(s1, s2);
342 if (!strstr_ret) {
343 PyMem_Free(discard_const_p(char, s1));
344 PyMem_Free(discard_const_p(char, s2));
345 Py_RETURN_NONE;
347 result = PyUnicode_FromString(strstr_ret);
348 PyMem_Free(discard_const_p(char, s1));
349 PyMem_Free(discard_const_p(char, s2));
350 return result;
353 static PyMethodDef py_misc_methods[] = {
354 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
355 "generate_random_str(len) -> string\n"
356 "Generate random string with specified length." },
357 { "generate_random_password", (PyCFunction)py_generate_random_password,
358 METH_VARARGS, "generate_random_password(min, max) -> string\n"
359 "Generate random password (based on printable ascii characters) "
360 "with a length >= min and <= max." },
361 { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
362 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
363 "Generate random password "
364 "(based on random utf16 characters converted to utf8 or "
365 "random ascii characters if 'unix charset' is not 'utf8')"
366 "with a length >= min (at least 14) and <= max (at most 255)." },
367 { "check_password_quality", (PyCFunction)py_check_password_quality,
368 METH_VARARGS, "check_password_quality(pass) -> bool\n"
369 "Check password quality against Samba's check_password_quality,"
370 "the implementation of Microsoft's rules:"
371 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
373 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
374 "unix2nttime(timestamp) -> nttime" },
375 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
376 "nttime2unix(nttime) -> timestamp" },
377 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
378 "nttime2string(nttime) -> string" },
379 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
380 "set debug level" },
381 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
382 "get debug level" },
383 { "fault_setup", (PyCFunction)py_fault_setup, METH_NOARGS,
384 "setup the default samba panic handler" },
385 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
386 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
387 "\n"
388 "get interface IP address list"},
389 { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
390 "(for testing) compare two strings using Samba's strcasecmp_m()"},
391 { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
392 "(for testing) find one string in another with Samba's strstr_m()"},
393 { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
394 "is the NTVFS file server built in this installation?" },
395 { "is_heimdal_built", (PyCFunction)py_is_heimdal_built, METH_NOARGS,
396 "is Samba built with Heimdal Kerberbos?" },
397 { "generate_random_bytes",
398 (PyCFunction)py_generate_random_bytes,
399 METH_VARARGS,
400 "generate_random_bytes(len) -> bytes\n"
401 "Generate random bytes with specified length." },
402 { "is_ad_dc_built", (PyCFunction)py_is_ad_dc_built, METH_NOARGS,
403 "is Samba built with AD DC?" },
407 static struct PyModuleDef moduledef = {
408 PyModuleDef_HEAD_INIT,
409 .m_name = "_glue",
410 .m_doc = "Python bindings for miscellaneous Samba functions.",
411 .m_size = -1,
412 .m_methods = py_misc_methods,
415 MODULE_INIT_FUNC(_glue)
417 PyObject *m;
419 debug_setup_talloc_log();
421 m = PyModule_Create(&moduledef);
422 if (m == NULL)
423 return NULL;
425 PyModule_AddObject(m, "version",
426 PyUnicode_FromString(SAMBA_VERSION_STRING));
427 PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
428 if (PyExc_NTSTATUSError != NULL) {
429 Py_INCREF(PyExc_NTSTATUSError);
430 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
433 PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
434 if (PyExc_WERRORError != NULL) {
435 Py_INCREF(PyExc_WERRORError);
436 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
439 PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
440 if (PyExc_HRESULTError != NULL) {
441 Py_INCREF(PyExc_HRESULTError);
442 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
445 PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
446 if (PyExc_DsExtendedError != NULL) {
447 Py_INCREF(PyExc_DsExtendedError);
448 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);
451 return m;