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