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 "version.h"
|
---|
23 | #include "param/pyparam.h"
|
---|
24 | #include "lib/socket/netif.h"
|
---|
25 |
|
---|
26 | void init_glue(void);
|
---|
27 |
|
---|
28 | static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
|
---|
29 | {
|
---|
30 | int len;
|
---|
31 | PyObject *ret;
|
---|
32 | char *retstr;
|
---|
33 | if (!PyArg_ParseTuple(args, "i", &len))
|
---|
34 | return NULL;
|
---|
35 |
|
---|
36 | retstr = generate_random_str(NULL, len);
|
---|
37 | ret = PyString_FromString(retstr);
|
---|
38 | talloc_free(retstr);
|
---|
39 | return ret;
|
---|
40 | }
|
---|
41 |
|
---|
42 | static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
|
---|
43 | {
|
---|
44 | int min, max;
|
---|
45 | PyObject *ret;
|
---|
46 | char *retstr;
|
---|
47 | if (!PyArg_ParseTuple(args, "ii", &min, &max))
|
---|
48 | return NULL;
|
---|
49 |
|
---|
50 | retstr = generate_random_password(NULL, min, max);
|
---|
51 | if (retstr == NULL) {
|
---|
52 | return NULL;
|
---|
53 | }
|
---|
54 | ret = PyString_FromString(retstr);
|
---|
55 | talloc_free(retstr);
|
---|
56 | return ret;
|
---|
57 | }
|
---|
58 |
|
---|
59 | static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
|
---|
60 | {
|
---|
61 | time_t t;
|
---|
62 | unsigned int _t;
|
---|
63 | NTTIME nt;
|
---|
64 |
|
---|
65 | if (!PyArg_ParseTuple(args, "I", &_t)) {
|
---|
66 | return NULL;
|
---|
67 | }
|
---|
68 | t = _t;
|
---|
69 |
|
---|
70 | unix_to_nt_time(&nt, t);
|
---|
71 |
|
---|
72 | return PyLong_FromLongLong((uint64_t)nt);
|
---|
73 | }
|
---|
74 |
|
---|
75 | static PyObject *py_nttime2unix(PyObject *self, PyObject *args)
|
---|
76 | {
|
---|
77 | time_t t;
|
---|
78 | NTTIME nt;
|
---|
79 | if (!PyArg_ParseTuple(args, "K", &nt))
|
---|
80 | return NULL;
|
---|
81 |
|
---|
82 | t = nt_time_to_unix(nt);
|
---|
83 |
|
---|
84 | return PyInt_FromLong((uint64_t)t);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static PyObject *py_nttime2string(PyObject *self, PyObject *args)
|
---|
88 | {
|
---|
89 | PyObject *ret;
|
---|
90 | NTTIME nt;
|
---|
91 | TALLOC_CTX *tmp_ctx;
|
---|
92 | const char *string;
|
---|
93 | if (!PyArg_ParseTuple(args, "K", &nt))
|
---|
94 | return NULL;
|
---|
95 |
|
---|
96 | tmp_ctx = talloc_new(NULL);
|
---|
97 | if (tmp_ctx == NULL) {
|
---|
98 | PyErr_NoMemory();
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|
102 | string = nt_time_string(tmp_ctx, nt);
|
---|
103 | ret = PyString_FromString(string);
|
---|
104 |
|
---|
105 | talloc_free(tmp_ctx);
|
---|
106 |
|
---|
107 | return ret;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
|
---|
111 | {
|
---|
112 | unsigned level;
|
---|
113 | if (!PyArg_ParseTuple(args, "I", &level))
|
---|
114 | return NULL;
|
---|
115 | (DEBUGLEVEL) = level;
|
---|
116 | Py_RETURN_NONE;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static PyObject *py_get_debug_level(PyObject *self)
|
---|
120 | {
|
---|
121 | return PyInt_FromLong(DEBUGLEVEL);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
|
---|
125 | {
|
---|
126 | #ifdef WITH_NTVFS_FILESERVER
|
---|
127 | Py_RETURN_TRUE;
|
---|
128 | #else
|
---|
129 | Py_RETURN_FALSE;
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | return the list of interface IPs we have configured
|
---|
135 | takes an loadparm context, returns a list of IPs in string form
|
---|
136 |
|
---|
137 | Does not return addresses on 127.0.0.0/8
|
---|
138 | */
|
---|
139 | static PyObject *py_interface_ips(PyObject *self, PyObject *args)
|
---|
140 | {
|
---|
141 | PyObject *pylist;
|
---|
142 | int count;
|
---|
143 | TALLOC_CTX *tmp_ctx;
|
---|
144 | PyObject *py_lp_ctx;
|
---|
145 | struct loadparm_context *lp_ctx;
|
---|
146 | struct interface *ifaces;
|
---|
147 | int i, ifcount;
|
---|
148 | int all_interfaces = 1;
|
---|
149 |
|
---|
150 | if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces))
|
---|
151 | return NULL;
|
---|
152 |
|
---|
153 | tmp_ctx = talloc_new(NULL);
|
---|
154 | if (tmp_ctx == NULL) {
|
---|
155 | PyErr_NoMemory();
|
---|
156 | return NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx);
|
---|
160 | if (lp_ctx == NULL) {
|
---|
161 | talloc_free(tmp_ctx);
|
---|
162 | return NULL;
|
---|
163 | }
|
---|
164 |
|
---|
165 | load_interface_list(tmp_ctx, lp_ctx, &ifaces);
|
---|
166 |
|
---|
167 | count = iface_list_count(ifaces);
|
---|
168 |
|
---|
169 | /* first count how many are not loopback addresses */
|
---|
170 | for (ifcount = i = 0; i<count; i++) {
|
---|
171 | const char *ip = iface_list_n_ip(ifaces, i);
|
---|
172 |
|
---|
173 | if (all_interfaces) {
|
---|
174 | ifcount++;
|
---|
175 | continue;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
|
---|
179 | continue;
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
|
---|
187 | continue;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
|
---|
191 | continue;
|
---|
192 | }
|
---|
193 |
|
---|
194 | ifcount++;
|
---|
195 | }
|
---|
196 |
|
---|
197 | pylist = PyList_New(ifcount);
|
---|
198 | for (ifcount = i = 0; i<count; i++) {
|
---|
199 | const char *ip = iface_list_n_ip(ifaces, i);
|
---|
200 |
|
---|
201 | if (all_interfaces) {
|
---|
202 | PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
|
---|
203 | ifcount++;
|
---|
204 | continue;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) {
|
---|
208 | continue;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) {
|
---|
212 | continue;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) {
|
---|
216 | continue;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) {
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 |
|
---|
223 | PyList_SetItem(pylist, ifcount, PyString_FromString(ip));
|
---|
224 | ifcount++;
|
---|
225 | }
|
---|
226 | talloc_free(tmp_ctx);
|
---|
227 | return pylist;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
|
---|
231 | {
|
---|
232 | char *s1, *s2;
|
---|
233 |
|
---|
234 | if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
|
---|
235 | return NULL;
|
---|
236 |
|
---|
237 | return PyInt_FromLong(strcasecmp_m(s1, s2));
|
---|
238 | }
|
---|
239 |
|
---|
240 | static PyObject *py_strstr_m(PyObject *self, PyObject *args)
|
---|
241 | {
|
---|
242 | char *s1, *s2, *ret;
|
---|
243 |
|
---|
244 | if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
|
---|
245 | return NULL;
|
---|
246 |
|
---|
247 | ret = strstr_m(s1, s2);
|
---|
248 | if (!ret) {
|
---|
249 | Py_RETURN_NONE;
|
---|
250 | }
|
---|
251 | return PyString_FromString(ret);
|
---|
252 | }
|
---|
253 |
|
---|
254 | static PyMethodDef py_misc_methods[] = {
|
---|
255 | { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
|
---|
256 | "generate_random_str(len) -> string\n"
|
---|
257 | "Generate random string with specified length." },
|
---|
258 | { "generate_random_password", (PyCFunction)py_generate_random_password,
|
---|
259 | METH_VARARGS, "generate_random_password(min, max) -> string\n"
|
---|
260 | "Generate random password with a length >= min and <= max." },
|
---|
261 | { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
|
---|
262 | "unix2nttime(timestamp) -> nttime" },
|
---|
263 | { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
|
---|
264 | "nttime2unix(nttime) -> timestamp" },
|
---|
265 | { "nttime2string", (PyCFunction)py_nttime2string, METH_VARARGS,
|
---|
266 | "nttime2string(nttime) -> string" },
|
---|
267 | { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
|
---|
268 | "set debug level" },
|
---|
269 | { "get_debug_level", (PyCFunction)py_get_debug_level, METH_NOARGS,
|
---|
270 | "get debug level" },
|
---|
271 | { "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
|
---|
272 | "interface_ips(lp_ctx[, all_interfaces) -> list_of_ifaces\n"
|
---|
273 | "\n"
|
---|
274 | "get interface IP address list"},
|
---|
275 | { "strcasecmp_m", (PyCFunction)py_strcasecmp_m, METH_VARARGS,
|
---|
276 | "(for testing) compare two strings using Samba's strcasecmp_m()"},
|
---|
277 | { "strstr_m", (PyCFunction)py_strstr_m, METH_VARARGS,
|
---|
278 | "(for testing) find one string in another with Samba's strstr_m()"},
|
---|
279 | { "is_ntvfs_fileserver_built", (PyCFunction)py_is_ntvfs_fileserver_built, METH_NOARGS,
|
---|
280 | "is the NTVFS file server built in this installation?" },
|
---|
281 | { NULL }
|
---|
282 | };
|
---|
283 |
|
---|
284 | void init_glue(void)
|
---|
285 | {
|
---|
286 | PyObject *m;
|
---|
287 |
|
---|
288 | debug_setup_talloc_log();
|
---|
289 |
|
---|
290 | m = Py_InitModule3("_glue", py_misc_methods,
|
---|
291 | "Python bindings for miscellaneous Samba functions.");
|
---|
292 | if (m == NULL)
|
---|
293 | return;
|
---|
294 |
|
---|
295 | PyModule_AddObject(m, "version",
|
---|
296 | PyString_FromString(SAMBA_VERSION_STRING));
|
---|
297 | }
|
---|
298 |
|
---|