ctdb:tests/integration: Be more careful when killing ctdbd
[Samba/wip.git] / python / pyglue.c
blob02fb005071a01ee0f593b07db6c0cf33cc871823
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4 Copyright (C) Matthias Dieter Wallnöfer 2009
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.
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.
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/>.
20 #include <Python.h>
21 #include "includes.h"
22 #include "version.h"
23 #include "param/pyparam.h"
24 #include "lib/socket/netif.h"
26 void init_glue(void);
28 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
30 #endif
32 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
34 int len;
35 PyObject *ret;
36 char *retstr;
37 if (!PyArg_ParseTuple(args, "i", &len))
38 return NULL;
40 retstr = generate_random_str(NULL, len);
41 ret = PyString_FromString(retstr);
42 talloc_free(retstr);
43 return ret;
46 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
48 int min, max;
49 PyObject *ret;
50 char *retstr;
51 if (!PyArg_ParseTuple(args, "ii", &min, &max))
52 return NULL;
54 retstr = generate_random_password(NULL, min, max);
55 if (retstr == NULL) {
56 return NULL;
58 ret = PyString_FromString(retstr);
59 talloc_free(retstr);
60 return ret;
63 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
65 time_t t;
66 unsigned int _t;
67 NTTIME nt;
69 if (!PyArg_ParseTuple(args, "I", &_t)) {
70 return NULL;
72 t = _t;
74 unix_to_nt_time(&nt, t);
76 return PyLong_FromLongLong((uint64_t)nt);
79 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
81 time_t t;
82 NTTIME nt;
83 if (!PyArg_ParseTuple(args, "K", &nt))
84 return NULL;
86 t = nt_time_to_unix(nt);
88 return PyInt_FromLong((uint64_t)t);
91 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
93 PyObject *ret;
94 NTTIME nt;
95 TALLOC_CTX *tmp_ctx;
96 const char *string;
97 if (!PyArg_ParseTuple(args, "K", &nt))
98 return NULL;
100 tmp_ctx = talloc_new(NULL);
101 if (tmp_ctx == NULL) {
102 PyErr_NoMemory();
103 return NULL;
106 string = nt_time_string(tmp_ctx, nt);
107 ret = PyString_FromString(string);
109 talloc_free(tmp_ctx);
111 return ret;
114 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
116 unsigned level;
117 if (!PyArg_ParseTuple(args, "I", &level))
118 return NULL;
119 (DEBUGLEVEL) = level;
120 Py_RETURN_NONE;
123 static PyObject *py_get_debug_level(PyObject *self)
125 return PyInt_FromLong(DEBUGLEVEL);
129 return the list of interface IPs we have configured
130 takes an loadparm context, returns a list of IPs in string form
132 Does not return addresses on 127.0.0.0/8
134 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
136 PyObject *pylist;
137 int count;
138 TALLOC_CTX *tmp_ctx;
139 PyObject *py_lp_ctx;
140 struct loadparm_context *lp_ctx;
141 struct interface *ifaces;
142 int i, ifcount;
143 int all_interfaces = 1;
145 if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
146 return NULL;
148 tmp_ctx = talloc_new(NULL);
149 if (tmp_ctx == NULL) {
150 PyErr_NoMemory();
151 return NULL;
154 lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
155 if (lp_ctx == NULL) {
156 talloc_free(tmp_ctx);
157 return NULL;
160 load_interface_list(tmp_ctx, lp_ctx, &ifaces);
162 count = iface_list_count(ifaces);
164 /* first count how many are not loopback addresses */
165 for (ifcount = i = 0; i<count; i++) {
166 const char *ip = iface_list_n_ip(ifaces, i);
168 if (all_interfaces) {
169 ifcount++;
170 continue;
173 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
174 continue;
177 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
178 continue;
181 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
182 continue;
185 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
186 continue;
189 ifcount++;
192 pylist = PyList_New(ifcount);
193 for (ifcount = i = 0; i<count; i++) {
194 const char *ip = iface_list_n_ip(ifaces, i);
196 if (all_interfaces) {
197 PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
198 ifcount++;
199 continue;
202 if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
203 continue;
206 if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
207 continue;
210 if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
211 continue;
214 if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
215 continue;
218 PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
219 ifcount++;
221 talloc_free(tmp_ctx);
222 return pylist;
225 static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
227 char *s1, *s2;
229 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
230 return NULL;
232 return PyInt_FromLong(strcasecmp_m(s1, s2));
235 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
237 char *s1, *s2, *ret;
239 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
240 return NULL;
242 ret = strstr_m(s1, s2);
243 if (!ret) {
244 Py_RETURN_NONE;
246 return PyString_FromString(ret);
249 static PyMethodDef py_misc_methods[] = {
250 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
251 "generate_random_str(len) -> string\n"
252 "Generate random string with specified length." },
253 { "generate_random_password", (PyCFunction)py_generate_random_password,
254 METH_VARARGS, "generate_random_password(min, max) -> string\n"
255 "Generate random password with a length >= min and <= max." },
256 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
257 "unix2nttime(timestamp) -> nttime" },
258 { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
259 "nttime2unix(nttime) -> timestamp" },
260 { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
261 "nttime2string(nttime) -> string" },
262 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
263 "set debug level" },
264 { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
265 "get debug level" },
266 { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
267 "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
268 "\n"
269 "get interface IP address list"},
270 { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
271 "(for testing) compare two strings using Samba's strcasecmp_m()"},
272 { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
273 "(for testing) find one string in another with Samba's strstr_m()"},
274 { NULL }
277 void init_glue(void)
279 PyObject *m;
281 debug_setup_talloc_log();
283 m = Py_InitModule3("_glue", py_misc_methods,
284 "Python bindings for miscellaneous Samba functions.");
285 if (m == NULL)
286 return;
288 PyModule_AddObject(m, "version",
289 PyString_FromString(SAMBA_VERSION_STRING));