s4-pyglue: added get_debug_level() method
[metze/samba/wip.git] / source4 / scripting / 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 "includes.h"
22 #include "param/param.h"
23 #include "version.h"
24 #include "libcli/util/pyerrors.h"
25 #include "param/pyparam.h"
26 #include "lib/socket/netif.h"
27 #include "lib/socket/netif_proto.h"
28 #include "lib/talloc/pytalloc.h"
29
30 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
31 {
32         int len;
33         PyObject *ret;
34         char *retstr;
35         if (!PyArg_ParseTuple(args, "i", &len))
36                 return NULL;
37
38         retstr = generate_random_str(NULL, len);
39         ret = PyString_FromString(retstr);
40         talloc_free(retstr);
41         return ret;
42 }
43
44 static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
45 {
46         int min, max;
47         PyObject *ret;
48         char *retstr;
49         if (!PyArg_ParseTuple(args, "ii", &min, &max))
50                 return NULL;
51
52         retstr = generate_random_password(NULL, min, max);
53         if (retstr == NULL) {
54                 return NULL;
55         }
56         ret = PyString_FromString(retstr);
57         talloc_free(retstr);
58         return ret;
59 }
60
61 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
62 {
63         time_t t;
64         NTTIME nt;
65         if (!PyArg_ParseTuple(args, "I", &t))
66                 return NULL;
67
68         unix_to_nt_time(&nt, t);
69
70         return PyLong_FromLongLong((uint64_t)nt);
71 }
72
73 static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
74 {
75         time_t t;
76         NTTIME nt;
77         if (!PyArg_ParseTuple(args, "K", &nt))
78                 return NULL;
79
80         t = nt_time_to_unix(nt);
81
82         return PyInt_FromLong((uint64_t)t);
83 }
84
85 static PyObject *py_nttime2string(PyObject *self, PyObject *args)
86 {
87         PyObject *ret;
88         NTTIME nt;
89         TALLOC_CTX *tmp_ctx;
90         const char *string;
91         if (!PyArg_ParseTuple(args, "K", &nt))
92                 return NULL;
93
94         tmp_ctx = talloc_new(NULL);
95
96         string = nt_time_string(tmp_ctx, nt);
97         ret =  PyString_FromString(string);
98
99         talloc_free(tmp_ctx);
100
101         return ret;
102 }
103
104 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
105 {
106         unsigned level;
107         if (!PyArg_ParseTuple(args, "I", &level))
108                 return NULL;
109         (DEBUGLEVEL) = level;
110         Py_RETURN_NONE;
111 }
112
113 static PyObject *py_get_debug_level(PyObject *self)
114 {
115         return PyInt_FromLong(DEBUGLEVEL);
116 }
117
118 /*
119   return the list of interface IPs we have configured
120   takes an loadparm context, returns a list of IPs in string form
121
122   Does not return addresses on 127.0.0.0/8
123  */
124 static PyObject *py_interface_ips(PyObject *self, PyObject *args)
125 {
126         PyObject *pylist;
127         int count;
128         TALLOC_CTX *tmp_ctx;
129         PyObject *py_lp_ctx;
130         struct loadparm_context *lp_ctx;
131         struct interface *ifaces;
132         int i, ifcount;
133         int all_interfaces;
134
135         if (!PyArg_ParseTuple(args, "Oi", &py_lp_ctx, &all_interfaces))
136                 return NULL;
137
138         tmp_ctx = talloc_new(NULL);
139         if (tmp_ctx == NULL) {
140                 PyErr_NoMemory();
141                 return NULL;
142         }
143
144         lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
145         if (lp_ctx == NULL) {
146                 talloc_free(tmp_ctx);
147                 return NULL;
148         }
149
150         load_interfaces(tmp_ctx, lpcfg_interfaces(lp_ctx), &ifaces);
151
152         count = iface_count(ifaces);
153
154         /* first count how many are not loopback addresses */
155         for (ifcount = i = 0; i<count; i++) {
156                 const char *ip = iface_n_ip(ifaces, i);
157                 if (!(!all_interfaces && iface_same_net(ip, "127.0.0.1", "255.0.0.0"))) {
158                         ifcount++;
159                 }
160         }
161
162         pylist = PyList_New(ifcount);
163         for (ifcount = i = 0; i<count; i++) {
164                 const char *ip = iface_n_ip(ifaces, i);
165                 if (!(!all_interfaces && iface_same_net(ip, "127.0.0.1", "255.0.0.0"))) {
166                         PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
167                         ifcount++;
168                 }
169         }
170         talloc_free(tmp_ctx);
171         return pylist;
172 }
173
174 static PyMethodDef py_misc_methods[] = {
175         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
176                 "generate_random_str(len) -> string\n"
177                 "Generate random string with specified length." },
178         { "generate_random_password", (PyCFunction)py_generate_random_password,
179                 METH_VARARGS, "generate_random_password(min, max) -> string\n"
180                 "Generate random password with a length >= min and <= max." },
181         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
182                 "unix2nttime(timestamp) -> nttime" },
183         { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
184                 "nttime2unix(nttime) -> timestamp" },
185         { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
186                 "nttime2string(nttime) -> string" },
187         { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
188                 "set debug level" },
189         { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
190                 "get debug level" },
191         { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
192                 "get interface IP address list"},
193         { NULL }
194 };
195
196 void init_glue(void)
197 {
198         PyObject *m;
199
200         debug_setup_talloc_log();
201
202         m = Py_InitModule3("_glue", py_misc_methods, 
203                            "Python bindings for miscellaneous Samba functions.");
204         if (m == NULL)
205                 return;
206
207         PyModule_AddObject(m, "version",
208                                            PyString_FromString(SAMBA_VERSION_STRING));
209
210         /* one of the most annoying things about python scripts is
211            that they don't die when you hit control-C. This fixes that
212            sillyness. As we do all database operations using
213            transactions, this is also safe. 
214         */
215         signal(SIGINT, SIG_DFL);
216 }
217