VERSION: Release Samba 4.6.6 for CVE-2017-11103
[Samba.git] / python / pyglue.c
blob0e80ba6260959b1d3970f299722a1215293e8dc1
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 "includes.h"
22 #include "version.h"
23 #include "param/pyparam.h"
24 #include "lib/socket/netif.h"
26 void init_glue(void);
27 static PyObject *PyExc_NTSTATUSError;
28 static PyObject *PyExc_WERRORError;
29 static PyObject *PyExc_HRESULTError;
30 static PyObject *PyExc_DsExtendedError;
32 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
34 int len;
35 PyObject *ret;
36 char *retstr;
37 if (!PyArg_ParseTuple(args, "i", &len))
38 return NULL;
40 retstr = generate_random_str(NULL, len);
41 ret = PyString_FromString(retstr);
42 talloc_free(retstr);
43 return ret;
46 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
48 int min, max;
49 PyObject *ret;
50 char *retstr;
51 if (!PyArg_ParseTuple(args, "ii", &min, &max))
52 return NULL;
54 retstr = generate_random_password(NULL, min, max);
55 if (retstr == NULL) {
56 return NULL;
58 ret = PyString_FromString(retstr);
59 talloc_free(retstr);
60 return ret;
63 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
65 int min, max;
66 PyObject *ret;
67 char *retstr;
68 if (!PyArg_ParseTuple(args, "ii", &min, &max))
69 return NULL;
71 retstr = generate_random_machine_password(NULL, min, max);
72 if (retstr == NULL) {
73 return NULL;
75 ret = PyUnicode_FromString(retstr);
76 talloc_free(retstr);
77 return ret;
80 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
82 time_t t;
83 unsigned int _t;
84 NTTIME nt;
86 if (!PyArg_ParseTuple(args, "I", &_t)) {
87 return NULL;
89 t = _t;
91 unix_to_nt_time(&nt, t);
93 return PyLong_FromLongLong((uint64_t)nt);
96 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
98 time_t t;
99 NTTIME nt;
100 if (!PyArg_ParseTuple(args, "K", &nt))
101 return NULL;
103 t = nt_time_to_unix(nt);
105 return PyInt_FromLong((uint64_t)t);
108 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
110 PyObject *ret;
111 NTTIME nt;
112 TALLOC_CTX *tmp_ctx;
113 const char *string;
114 if (!PyArg_ParseTuple(args, "K", &nt))
115 return NULL;
117 tmp_ctx = talloc_new(NULL);
118 if (tmp_ctx == NULL) {
119 PyErr_NoMemory();
120 return NULL;
123 string = nt_time_string(tmp_ctx, nt);
124 ret = PyString_FromString(string);
126 talloc_free(tmp_ctx);
128 return ret;
131 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
133 unsigned level;
134 if (!PyArg_ParseTuple(args, "I", &level))
135 return NULL;
136 (DEBUGLEVEL) = level;
137 Py_RETURN_NONE;
140 static PyObject *py_get_debug_level(PyObject *self)
142 return PyInt_FromLong(DEBUGLEVEL);
145 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
147 #ifdef WITH_NTVFS_FILESERVER
148 Py_RETURN_TRUE;
149 #else
150 Py_RETURN_FALSE;
151 #endif
155 return the list of interface IPs we have configured
156 takes an loadparm context, returns a list of IPs in string form
158 Does not return addresses on 127.0.0.0/8
160 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
162 PyObject *pylist;
163 int count;
164 TALLOC_CTX *tmp_ctx;
165 PyObject *py_lp_ctx;
166 struct loadparm_context *lp_ctx;
167 struct interface *ifaces;
168 int i, ifcount;
169 int all_interfaces = 1;
171 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
172 return NULL;
174 tmp_ctx = talloc_new(NULL);
175 if (tmp_ctx == NULL) {
176 PyErr_NoMemory();
177 return NULL;
180 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
181 if (lp_ctx == NULL) {
182 talloc_free(tmp_ctx);
183 return NULL;
186 load_interface_list(tmp_ctx, lp_ctx, &ifaces);
188 count = iface_list_count(ifaces);
190 /* first count how many are not loopback addresses */
191 for (ifcount = i = 0; i<count; i++) {
192 const char *ip = iface_list_n_ip(ifaces, i);
194 if (all_interfaces) {
195 ifcount++;
196 continue;
199 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
200 continue;
203 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
204 continue;
207 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
208 continue;
211 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
212 continue;
215 ifcount++;
218 pylist = PyList_New(ifcount);
219 for (ifcount = i = 0; i<count; i++) {
220 const char *ip = iface_list_n_ip(ifaces, i);
222 if (all_interfaces) {
223 PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
224 ifcount++;
225 continue;
228 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
229 continue;
232 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
233 continue;
236 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
237 continue;
240 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
241 continue;
244 PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
245 ifcount++;
247 talloc_free(tmp_ctx);
248 return pylist;
251 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
253 char *s1, *s2;
255 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
256 return NULL;
258 return PyInt_FromLong(strcasecmp_m(s1, s2));
261 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
263 char *s1, *s2, *ret;
265 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
266 return NULL;
268 ret = strstr_m(s1, s2);
269 if (!ret) {
270 Py_RETURN_NONE;
272 return PyString_FromString(ret);
275 static PyMethodDef py_misc_methods[] = {
276 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
277 "generate_random_str(len) -> string\n"
278 "Generate random string with specified length." },
279 { "generate_random_password", (PyCFunction)py_generate_random_password,
280 METH_VARARGS, "generate_random_password(min, max) -> string\n"
281 "Generate random password (based on printable ascii characters) "
282 "with a length >= min and <= max." },
283 { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
284 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
285 "Generate random password "
286 "(based on random utf16 characters converted to utf8 or "
287 "random ascii characters if 'unix charset' is not 'utf8')"
288 "with a length >= min (at least 14) and <= max (at most 255)." },
289 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
290 "unix2nttime(timestamp) -> nttime" },
291 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
292 "nttime2unix(nttime) -> timestamp" },
293 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
294 "nttime2string(nttime) -> string" },
295 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
296 "set debug level" },
297 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
298 "get debug level" },
299 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
300 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
301 "\n"
302 "get interface IP address list"},
303 { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
304 "(for testing) compare two strings using Samba's strcasecmp_m()"},
305 { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
306 "(for testing) find one string in another with Samba's strstr_m()"},
307 { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
308 "is the NTVFS file server built in this installation?" },
309 { NULL }
312 void init_glue(void)
314 PyObject *m;
316 debug_setup_talloc_log();
318 m = Py_InitModule3("_glue", py_misc_methods,
319 "Python bindings for miscellaneous Samba functions.");
320 if (m == NULL)
321 return;
323 PyModule_AddObject(m, "version",
324 PyString_FromString(SAMBA_VERSION_STRING));
325 PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
326 if (PyExc_NTSTATUSError != NULL) {
327 Py_INCREF(PyExc_NTSTATUSError);
328 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
331 PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
332 if (PyExc_WERRORError != NULL) {
333 Py_INCREF(PyExc_WERRORError);
334 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
337 PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
338 if (PyExc_HRESULTError != NULL) {
339 Py_INCREF(PyExc_HRESULTError);
340 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
343 PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
344 if (PyExc_DsExtendedError != NULL) {
345 Py_INCREF(PyExc_DsExtendedError);
346 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);