python: py_strcasecmp_m & py_strstr_m don't handle unicode properly
[amitay/samba.git] / python / pyglue.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    Copyright (C) Matthias Dieter Wallnöfer          2009
5    
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.
10    
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.
15    
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/>.
18 */
19
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
27 void init_glue(void);
28 static PyObject *PyExc_NTSTATUSError;
29 static PyObject *PyExc_WERRORError;
30 static PyObject *PyExc_HRESULTError;
31 static PyObject *PyExc_DsExtendedError;
32
33 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
34 {
35         int len;
36         PyObject *ret;
37         char *retstr;
38         if (!PyArg_ParseTuple(args, "i", &len))
39                 return NULL;
40
41         retstr = generate_random_str(NULL, len);
42         ret = PyStr_FromString(retstr);
43         talloc_free(retstr);
44         return ret;
45 }
46
47 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
48 {
49         int min, max;
50         PyObject *ret;
51         char *retstr;
52         if (!PyArg_ParseTuple(args, "ii", &min, &max))
53                 return NULL;
54
55         retstr = generate_random_password(NULL, min, max);
56         if (retstr == NULL) {
57                 return NULL;
58         }
59         ret = PyStr_FromString(retstr);
60         talloc_free(retstr);
61         return ret;
62 }
63
64 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
65 {
66         int min, max;
67         PyObject *ret;
68         char *retstr;
69         if (!PyArg_ParseTuple(args, "ii", &min, &max))
70                 return NULL;
71
72         retstr = generate_random_machine_password(NULL, min, max);
73         if (retstr == NULL) {
74                 return NULL;
75         }
76         ret = PyUnicode_FromString(retstr);
77         talloc_free(retstr);
78         return ret;
79 }
80
81 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
82 {
83         char *pass;
84
85         if (!PyArg_ParseTuple(args, "s", &pass)) {
86                 return NULL;
87         }
88
89         return PyBool_FromLong(check_password_quality(pass));
90 }
91
92 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
93 {
94         int len;
95         PyObject *ret;
96         uint8_t *bytes = NULL;
97
98         if (!PyArg_ParseTuple(args, "i", &len))
99                 return NULL;
100
101         bytes = talloc_zero_size(NULL, len);
102         generate_random_buffer(bytes, len);
103         ret = PyBytes_FromStringAndSize((const char *)bytes, len);
104         talloc_free(bytes);
105         return ret;
106 }
107
108 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
109 {
110         time_t t;
111         unsigned int _t;
112         NTTIME nt;
113
114         if (!PyArg_ParseTuple(args, "I", &_t)) {
115                 return NULL;
116         }
117         t = _t;
118
119         unix_to_nt_time(&nt, t);
120
121         return PyLong_FromLongLong((uint64_t)nt);
122 }
123
124 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
125 {
126         time_t t;
127         NTTIME nt;
128         if (!PyArg_ParseTuple(args, "K", &nt))
129                 return NULL;
130
131         t = nt_time_to_unix(nt);
132
133         return PyInt_FromLong((uint64_t)t);
134 }
135
136 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
137 {
138         PyObject *ret;
139         NTTIME nt;
140         TALLOC_CTX *tmp_ctx;
141         const char *string;
142         if (!PyArg_ParseTuple(args, "K", &nt))
143                 return NULL;
144
145         tmp_ctx = talloc_new(NULL);
146         if (tmp_ctx == NULL) {
147                 PyErr_NoMemory();
148                 return NULL;
149         }
150
151         string = nt_time_string(tmp_ctx, nt);
152         ret =  PyStr_FromString(string);
153
154         talloc_free(tmp_ctx);
155
156         return ret;
157 }
158
159 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
160 {
161         unsigned level;
162         if (!PyArg_ParseTuple(args, "I", &level))
163                 return NULL;
164         (DEBUGLEVEL) = level;
165         Py_RETURN_NONE;
166 }
167
168 static PyObject *py_get_debug_level(PyObject *self)
169 {
170         return PyInt_FromLong(DEBUGLEVEL);
171 }
172
173 static PyObject *py_fault_setup(PyObject *self)
174 {
175         static bool done;
176         if (!done) {
177                 fault_setup();
178                 done = true;
179         }
180         Py_RETURN_NONE;
181 }
182
183 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
184 {
185 #ifdef WITH_NTVFS_FILESERVER
186         Py_RETURN_TRUE;
187 #else
188         Py_RETURN_FALSE;
189 #endif
190 }
191
192 static PyObject *py_is_heimdal_built(PyObject *self)
193 {
194 #ifdef SAMBA4_USES_HEIMDAL
195         Py_RETURN_TRUE;
196 #else
197         Py_RETURN_FALSE;
198 #endif
199 }
200
201 /*
202   return the list of interface IPs we have configured
203   takes an loadparm context, returns a list of IPs in string form
204
205   Does not return addresses on 127.0.0.0/8
206  */
207 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
208 {
209         PyObject *pylist;
210         int count;
211         TALLOC_CTX *tmp_ctx;
212         PyObject *py_lp_ctx;
213         struct loadparm_context *lp_ctx;
214         struct interface *ifaces;
215         int i, ifcount;
216         int all_interfaces = 1;
217
218         if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
219                 return NULL;
220
221         tmp_ctx = talloc_new(NULL);
222         if (tmp_ctx == NULL) {
223                 PyErr_NoMemory();
224                 return NULL;
225         }
226
227         lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
228         if (lp_ctx == NULL) {
229                 talloc_free(tmp_ctx);
230                 return NULL;
231         }
232
233         load_interface_list(tmp_ctx, lp_ctx, &ifaces);
234
235         count = iface_list_count(ifaces);
236
237         /* first count how many are not loopback addresses */
238         for (ifcount = i = 0; i<count; i++) {
239                 const char *ip = iface_list_n_ip(ifaces, i);
240
241                 if (all_interfaces) {
242                         ifcount++;
243                         continue;
244                 }
245
246                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
247                         continue;
248                 }
249
250                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
251                         continue;
252                 }
253
254                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
255                         continue;
256                 }
257
258                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
259                         continue;
260                 }
261
262                 ifcount++;
263         }
264
265         pylist = PyList_New(ifcount);
266         for (ifcount = i = 0; i<count; i++) {
267                 const char *ip = iface_list_n_ip(ifaces, i);
268
269                 if (all_interfaces) {
270                         PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
271                         ifcount++;
272                         continue;
273                 }
274
275                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
276                         continue;
277                 }
278
279                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
280                         continue;
281                 }
282
283                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
284                         continue;
285                 }
286
287                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
288                         continue;
289                 }
290
291                 PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
292                 ifcount++;
293         }
294         talloc_free(tmp_ctx);
295         return pylist;
296 }
297
298 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
299 {
300         const char *s1 = NULL;
301         const char *s2 = NULL;
302
303         if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2)) {
304                 return NULL;
305         }
306
307         return PyInt_FromLong(strcasecmp_m(s1, s2));
308 }
309
310 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
311 {
312         const char *s1 = NULL;
313         const char *s2 = NULL;
314         char *ret = NULL;
315
316         if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2))
317                 return NULL;
318
319         ret = strstr_m(s1, s2);
320         if (!ret) {
321                 Py_RETURN_NONE;
322         }
323         return PyUnicode_FromString(ret);
324 }
325
326 static PyMethodDef py_misc_methods[] = {
327         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
328                 "generate_random_str(len) -> string\n"
329                 "Generate random string with specified length." },
330         { "generate_random_password", (PyCFunction)py_generate_random_password,
331                 METH_VARARGS, "generate_random_password(min, max) -> string\n"
332                 "Generate random password (based on printable ascii characters) "
333                 "with a length >= min and <= max." },
334         { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
335                 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
336                 "Generate random password "
337                 "(based on random utf16 characters converted to utf8 or "
338                 "random ascii characters if 'unix charset' is not 'utf8')"
339                 "with a length >= min (at least 14) and <= max (at most 255)." },
340         { "check_password_quality", (PyCFunction)py_check_password_quality,
341                 METH_VARARGS, "check_password_quality(pass) -> bool\n"
342                 "Check password quality against Samba's check_password_quality,"
343                 "the implementation of Microsoft's rules:"
344                 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
345         },
346         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
347                 "unix2nttime(timestamp) -> nttime" },
348         { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
349                 "nttime2unix(nttime) -> timestamp" },
350         { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
351                 "nttime2string(nttime) -> string" },
352         { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
353                 "set debug level" },
354         { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
355                 "get debug level" },
356         { "fault_setup", (PyCFunction)py_fault_setup, METH_NOARGS,
357                 "setup the default samba panic handler" },
358         { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
359                 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
360                 "\n"
361                 "get interface IP address list"},
362         { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
363                 "(for testing) compare two strings using Samba's strcasecmp_m()"},
364         { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
365                 "(for testing) find one string in another with Samba's strstr_m()"},
366         { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
367                 "is the NTVFS file server built in this installation?" },
368         { "is_heimdal_built", (PyCFunction)py_is_heimdal_built, METH_NOARGS,
369                 "is Samba built with Heimdal Kerberbos?" },
370         { "generate_random_bytes",
371                 (PyCFunction)py_generate_random_bytes,
372                 METH_VARARGS,
373                 "generate_random_bytes(len) -> bytes\n"
374                 "Generate random bytes with specified length." },
375         { NULL }
376 };
377
378 static struct PyModuleDef moduledef = {
379     PyModuleDef_HEAD_INIT,
380     .m_name = "_glue",
381     .m_doc = "Python bindings for miscellaneous Samba functions.",
382     .m_size = -1,
383     .m_methods = py_misc_methods,
384 };
385
386 MODULE_INIT_FUNC(_glue)
387 {
388         PyObject *m;
389
390         debug_setup_talloc_log();
391
392         m = PyModule_Create(&moduledef);
393         if (m == NULL)
394                 return NULL;
395
396         PyModule_AddObject(m, "version",
397                                            PyStr_FromString(SAMBA_VERSION_STRING));
398         PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
399         if (PyExc_NTSTATUSError != NULL) {
400                 Py_INCREF(PyExc_NTSTATUSError);
401                 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
402         }
403
404         PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
405         if (PyExc_WERRORError != NULL) {
406                 Py_INCREF(PyExc_WERRORError);
407                 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
408         }
409
410         PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
411         if (PyExc_HRESULTError != NULL) {
412                 Py_INCREF(PyExc_HRESULTError);
413                 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
414         }
415
416         PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
417         if (PyExc_DsExtendedError != NULL) {
418                 Py_INCREF(PyExc_DsExtendedError);
419                 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);
420         }
421
422         return m;
423 }
424