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