samba-tool: validate password early in `domain provision`
[bbaumbach/samba-autobuild/.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_unix2nttime(PyObject *self, PyObject *args)
93 {
94         time_t t;
95         unsigned int _t;
96         NTTIME nt;
97
98         if (!PyArg_ParseTuple(args, "I", &_t)) {
99                 return NULL;
100         }
101         t = _t;
102
103         unix_to_nt_time(&nt, t);
104
105         return PyLong_FromLongLong((uint64_t)nt);
106 }
107
108 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
109 {
110         time_t t;
111         NTTIME nt;
112         if (!PyArg_ParseTuple(args, "K", &nt))
113                 return NULL;
114
115         t = nt_time_to_unix(nt);
116
117         return PyInt_FromLong((uint64_t)t);
118 }
119
120 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
121 {
122         PyObject *ret;
123         NTTIME nt;
124         TALLOC_CTX *tmp_ctx;
125         const char *string;
126         if (!PyArg_ParseTuple(args, "K", &nt))
127                 return NULL;
128
129         tmp_ctx = talloc_new(NULL);
130         if (tmp_ctx == NULL) {
131                 PyErr_NoMemory();
132                 return NULL;
133         }
134
135         string = nt_time_string(tmp_ctx, nt);
136         ret =  PyStr_FromString(string);
137
138         talloc_free(tmp_ctx);
139
140         return ret;
141 }
142
143 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
144 {
145         unsigned level;
146         if (!PyArg_ParseTuple(args, "I", &level))
147                 return NULL;
148         (DEBUGLEVEL) = level;
149         Py_RETURN_NONE;
150 }
151
152 static PyObject *py_get_debug_level(PyObject *self)
153 {
154         return PyInt_FromLong(DEBUGLEVEL);
155 }
156
157 static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
158 {
159 #ifdef WITH_NTVFS_FILESERVER
160         Py_RETURN_TRUE;
161 #else
162         Py_RETURN_FALSE;
163 #endif
164 }
165
166 static PyObject *py_is_heimdal_built(PyObject *self)
167 {
168 #ifdef SAMBA4_USES_HEIMDAL
169         Py_RETURN_TRUE;
170 #else
171         Py_RETURN_FALSE;
172 #endif
173 }
174
175 /*
176   return the list of interface IPs we have configured
177   takes an loadparm context, returns a list of IPs in string form
178
179   Does not return addresses on 127.0.0.0/8
180  */
181 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
182 {
183         PyObject *pylist;
184         int count;
185         TALLOC_CTX *tmp_ctx;
186         PyObject *py_lp_ctx;
187         struct loadparm_context *lp_ctx;
188         struct interface *ifaces;
189         int i, ifcount;
190         int all_interfaces = 1;
191
192         if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
193                 return NULL;
194
195         tmp_ctx = talloc_new(NULL);
196         if (tmp_ctx == NULL) {
197                 PyErr_NoMemory();
198                 return NULL;
199         }
200
201         lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
202         if (lp_ctx == NULL) {
203                 talloc_free(tmp_ctx);
204                 return NULL;
205         }
206
207         load_interface_list(tmp_ctx, lp_ctx, &ifaces);
208
209         count = iface_list_count(ifaces);
210
211         /* first count how many are not loopback addresses */
212         for (ifcount = i = 0; i<count; i++) {
213                 const char *ip = iface_list_n_ip(ifaces, i);
214
215                 if (all_interfaces) {
216                         ifcount++;
217                         continue;
218                 }
219
220                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
221                         continue;
222                 }
223
224                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
225                         continue;
226                 }
227
228                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
229                         continue;
230                 }
231
232                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
233                         continue;
234                 }
235
236                 ifcount++;
237         }
238
239         pylist = PyList_New(ifcount);
240         for (ifcount = i = 0; i<count; i++) {
241                 const char *ip = iface_list_n_ip(ifaces, i);
242
243                 if (all_interfaces) {
244                         PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
245                         ifcount++;
246                         continue;
247                 }
248
249                 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
250                         continue;
251                 }
252
253                 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
254                         continue;
255                 }
256
257                 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
258                         continue;
259                 }
260
261                 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
262                         continue;
263                 }
264
265                 PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
266                 ifcount++;
267         }
268         talloc_free(tmp_ctx);
269         return pylist;
270 }
271
272 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
273 {
274         char *s1, *s2;
275
276         if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
277                 return NULL;
278
279         return PyInt_FromLong(strcasecmp_m(s1, s2));
280 }
281
282 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
283 {
284         char *s1, *s2, *ret;
285
286         if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
287                 return NULL;
288
289         ret = strstr_m(s1, s2);
290         if (!ret) {
291                 Py_RETURN_NONE;
292         }
293         return PyStr_FromString(ret);
294 }
295
296 static PyMethodDef py_misc_methods[] = {
297         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
298                 "generate_random_str(len) -> string\n"
299                 "Generate random string with specified length." },
300         { "generate_random_password", (PyCFunction)py_generate_random_password,
301                 METH_VARARGS, "generate_random_password(min, max) -> string\n"
302                 "Generate random password (based on printable ascii characters) "
303                 "with a length >= min and <= max." },
304         { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
305                 METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
306                 "Generate random password "
307                 "(based on random utf16 characters converted to utf8 or "
308                 "random ascii characters if 'unix charset' is not 'utf8')"
309                 "with a length >= min (at least 14) and <= max (at most 255)." },
310         { "check_password_quality", (PyCFunction)py_check_password_quality,
311                 METH_VARARGS, "check_password_quality(pass) -> bool\n"
312                 "Check password quality against Samba's check_password_quality,"
313                 "the implementation of Microsoft's rules:"
314                 "http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx"
315         },
316         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
317                 "unix2nttime(timestamp) -> nttime" },
318         { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
319                 "nttime2unix(nttime) -> timestamp" },
320         { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
321                 "nttime2string(nttime) -> string" },
322         { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
323                 "set debug level" },
324         { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
325                 "get debug level" },
326         { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
327                 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
328                 "\n"
329                 "get interface IP address list"},
330         { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
331                 "(for testing) compare two strings using Samba's strcasecmp_m()"},
332         { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
333                 "(for testing) find one string in another with Samba's strstr_m()"},
334         { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
335                 "is the NTVFS file server built in this installation?" },
336         { "is_heimdal_built", (PyCFunction)py_is_heimdal_built, METH_NOARGS,
337                 "is Samba built with Heimdal Kerberbos?" },
338         { NULL }
339 };
340
341 static struct PyModuleDef moduledef = {
342     PyModuleDef_HEAD_INIT,
343     .m_name = "_glue",
344     .m_doc = "Python bindings for miscellaneous Samba functions.",
345     .m_size = -1,
346     .m_methods = py_misc_methods,
347 };
348
349 MODULE_INIT_FUNC(_glue)
350 {
351         PyObject *m;
352
353         debug_setup_talloc_log();
354
355         m = PyModule_Create(&moduledef);
356         if (m == NULL)
357                 return NULL;
358
359         PyModule_AddObject(m, "version",
360                                            PyStr_FromString(SAMBA_VERSION_STRING));
361         PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
362         if (PyExc_NTSTATUSError != NULL) {
363                 Py_INCREF(PyExc_NTSTATUSError);
364                 PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
365         }
366
367         PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
368         if (PyExc_WERRORError != NULL) {
369                 Py_INCREF(PyExc_WERRORError);
370                 PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
371         }
372
373         PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
374         if (PyExc_HRESULTError != NULL) {
375                 Py_INCREF(PyExc_HRESULTError);
376                 PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
377         }
378
379         PyExc_DsExtendedError = PyErr_NewException(discard_const_p(char, "samba.DsExtendedError"), PyExc_RuntimeError, NULL);
380         if (PyExc_DsExtendedError != NULL) {
381                 Py_INCREF(PyExc_DsExtendedError);
382                 PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);
383         }
384
385         return m;
386 }
387