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