s3: libsmb: In cli_chkpath_send() (SMBcheckpath) check for DFS pathname.
[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 #include "lib/util/debug.h"
27 #include "librpc/ndr/ndr_private.h"
28
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;
34
35 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
36 {
37         int len;
38         PyObject *ret;
39         char *retstr;
40         if (!PyArg_ParseTuple(args, "i", &len)) {
41                 return NULL;
42         }
43         if (len < 0) {
44                 PyErr_Format(PyExc_ValueError,
45                              "random string length should be positive, not %d",
46                              len);
47                 return NULL;
48         }
49         retstr = generate_random_str(NULL, len);
50         ret = PyUnicode_FromString(retstr);
51         talloc_free(retstr);
52         return ret;
53 }
54
55 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
56 {
57         int min, max;
58         PyObject *ret;
59         char *retstr;
60         if (!PyArg_ParseTuple(args, "ii", &min, &max)) {
61                 return NULL;
62         }
63         if (max < 0 || min < 0) {
64                 /*
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.
68                  */
69                 PyErr_Format(PyExc_ValueError,
70                              "invalid range: %d - %d",
71                              min, max);
72                 return NULL;
73         }
74
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);
81                 }
82                 return NULL;
83         }
84         ret = PyUnicode_FromString(retstr);
85         talloc_free(retstr);
86         return ret;
87 }
88
89 static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
90 {
91         int min, max;
92         PyObject *ret;
93         char *retstr;
94         if (!PyArg_ParseTuple(args, "ii", &min, &max)) {
95                 return NULL;
96         }
97         if (max < 0 || min < 0) {
98                 /*
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.
103                  */
104                 PyErr_Format(PyExc_ValueError,
105                              "invalid range: %d - %d",
106                              min, max);
107                 return NULL;
108         }
109
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);
116                 }
117                 return NULL;
118         }
119         ret = PyUnicode_FromString(retstr);
120         talloc_free(retstr);
121         return ret;
122 }
123
124 static PyObject *py_check_password_quality(PyObject *self, PyObject *args)
125 {
126         char *pass;
127
128         if (!PyArg_ParseTuple(args, "s", &pass)) {
129                 return NULL;
130         }
131
132         return PyBool_FromLong(check_password_quality(pass));
133 }
134
135 static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
136 {
137         int len;
138         PyObject *ret;
139         uint8_t *bytes = NULL;
140
141         if (!PyArg_ParseTuple(args, "i", &len)) {
142                 return NULL;
143         }
144         if (len < 0) {
145                 PyErr_Format(PyExc_ValueError,
146                              "random bytes length should be positive, not %d",
147                              len);
148                 return NULL;
149         }
150         bytes = talloc_zero_size(NULL, len);
151         if (bytes == NULL) {
152                 PyErr_NoMemory();
153                 return NULL;
154         }
155         generate_random_buffer(bytes, len);
156         ret = PyBytes_FromStringAndSize((const char *)bytes, len);
157         talloc_free(bytes);
158         return ret;
159 }
160
161 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
162 {
163         time_t t;
164         unsigned int _t;
165         NTTIME nt;
166
167         if (!PyArg_ParseTuple(args, "I", &_t)) {
168                 return NULL;
169         }
170         t = _t;
171
172         unix_to_nt_time(&nt, t);
173
174         return PyLong_FromLongLong((uint64_t)nt);
175 }
176
177 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
178 {
179         time_t t;
180         NTTIME nt;
181         if (!PyArg_ParseTuple(args, "K", &nt))
182                 return NULL;
183
184         t = nt_time_to_unix(nt);
185
186         return PyLong_FromLong((uint64_t)t);
187 }
188
189 static PyObject *py_float2nttime(PyObject *self, PyObject *args)
190 {
191         double ft = 0;
192         double ft_sec = 0;
193         double ft_nsec = 0;
194         struct timespec ts;
195         NTTIME nt = 0;
196
197         if (!PyArg_ParseTuple(args, "d", &ft)) {
198                 return NULL;
199         }
200
201         ft_sec = (double)(int)ft;
202         ft_nsec = (ft - ft_sec) * 1.0e+9;
203
204         ts.tv_sec = (int)ft_sec;
205         ts.tv_nsec = (int)ft_nsec;
206
207         nt = full_timespec_to_nt_time(&ts);
208
209         return PyLong_FromLongLong((uint64_t)nt);
210 }
211
212 static PyObject *py_nttime2float(PyObject *self, PyObject *args)
213 {
214         double ft = 0;
215         struct timespec ts;
216         const struct timespec ts_zero = { .tv_sec = 0, };
217         NTTIME nt = 0;
218
219         if (!PyArg_ParseTuple(args, "K", &nt)) {
220                 return NULL;
221         }
222
223         ts = nt_time_to_full_timespec(nt);
224         if (is_omit_timespec(&ts)) {
225                 return PyFloat_FromDouble(1.0);
226         }
227         ft = timespec_elapsed2(&ts_zero, &ts);
228
229         return PyFloat_FromDouble(ft);
230 }
231
232 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
233 {
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;
240
241         tmp_ctx = talloc_new(NULL);
242         if (tmp_ctx == NULL) {
243                 PyErr_NoMemory();
244                 return NULL;
245         }
246
247         string = nt_time_string(tmp_ctx, nt);
248         ret =  PyUnicode_FromString(string);
249
250         talloc_free(tmp_ctx);
251
252         return ret;
253 }
254
255 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
256 {
257         unsigned level;
258         if (!PyArg_ParseTuple(args, "I", &level))
259                 return NULL;
260         debuglevel_set(level);
261         Py_RETURN_NONE;
262 }
263
264 static PyObject *py_get_debug_level(PyObject *self,
265                 PyObject *Py_UNUSED(ignored))
266 {
267         return PyLong_FromLong(debuglevel_get());
268 }
269
270 static PyObject *py_fault_setup(PyObject *self,
271                 PyObject *Py_UNUSED(ignored))
272 {
273         static bool done;
274         if (!done) {
275                 fault_setup();
276                 done = true;
277         }
278         Py_RETURN_NONE;
279 }
280
281 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
282                 PyObject *Py_UNUSED(ignored))
283 {
284 #ifdef WITH_NTVFS_FILESERVER
285         Py_RETURN_TRUE;
286 #else
287         Py_RETURN_FALSE;
288 #endif
289 }
290
291 static PyObject *py_is_heimdal_built(PyObject *self,
292                 PyObject *Py_UNUSED(ignored))
293 {
294 #ifdef SAMBA4_USES_HEIMDAL
295         Py_RETURN_TRUE;
296 #else
297         Py_RETURN_FALSE;
298 #endif
299 }
300
301 static PyObject *py_is_ad_dc_built(PyObject *self,
302                 PyObject *Py_UNUSED(ignored))
303 {
304 #ifdef AD_DC_BUILD_IS_ENABLED
305         Py_RETURN_TRUE;
306 #else
307         Py_RETURN_FALSE;
308 #endif
309 }
310
311 static PyObject *py_is_selftest_enabled(PyObject *self,
312                 PyObject *Py_UNUSED(ignored))
313 {
314 #ifdef ENABLE_SELFTEST
315         Py_RETURN_TRUE;
316 #else
317         Py_RETURN_FALSE;
318 #endif
319 }
320
321 static PyObject *py_ndr_token_max_list_size(PyObject *self,
322                 PyObject *Py_UNUSED(ignored))
323 {
324         return PyLong_FromLong(ndr_token_max_list_size());
325 }
326
327 /*
328   return the list of interface IPs we have configured
329   takes an loadparm context, returns a list of IPs in string form
330
331   Does not return addresses on 127.0.0.0/8
332  */
333 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
334 {
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;
343
344         if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
345                 return NULL;
346
347         tmp_ctx = talloc_new(NULL);
348         if (tmp_ctx == NULL) {
349                 PyErr_NoMemory();
350                 return NULL;
351         }
352
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;
357         }
358
359         load_interface_list(tmp_ctx, lp_ctx, &ifaces);
360
361         count = iface_list_count(ifaces);
362
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);
366
367                 if (all_interfaces) {
368                         ifcount++;
369                         continue;
370                 }
371
372                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
373                         continue;
374                 }
375
376                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
377                         continue;
378                 }
379
380                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
381                         continue;
382                 }
383
384                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
385                         continue;
386                 }
387
388                 ifcount++;
389         }
390
391         pylist = PyList_New(ifcount);
392         for (ifcount = i = 0; i<count; i++) {
393                 const char *ip = iface_list_n_ip(ifaces, i);
394
395                 if (all_interfaces) {
396                         PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
397                         ifcount++;
398                         continue;
399                 }
400
401                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
402                         continue;
403                 }
404
405                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
406                         continue;
407                 }
408
409                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
410                         continue;
411                 }
412
413                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
414                         continue;
415                 }
416
417                 PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
418                 ifcount++;
419         }
420         talloc_free(tmp_ctx);
421         return pylist;
422 }
423
424 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
425 {
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;
433         }
434
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);
439 }
440
441 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
442 {
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;
451
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;
457         }
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;
462 }
463
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"
483         },
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?" },
523         {0}
524 };
525
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,
532 };
533
534 MODULE_INIT_FUNC(_glue)
535 {
536         PyObject *m;
537
538         debug_setup_talloc_log();
539
540         m = PyModule_Create(&moduledef);
541         if (m == NULL)
542                 return NULL;
543
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);
550         }
551
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);
556         }
557
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);
562         }
563
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);
568         }
569
570         return m;
571 }
572