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