Remove another use of global_loadparm.
[ira/wip.git] / libcli / nbt / pynbt.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
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 "includes.h"
21 #include <Python.h>
22 #include "libcli/util/pyerrors.h"
23 #include "scripting/python/modules.h"
24 #include "../libcli/nbt/libnbt.h"
25 #include "lib/events/events.h"
26
27 PyAPI_DATA(PyTypeObject) nbt_node_Type;
28
29 typedef struct {
30         PyObject_HEAD
31         TALLOC_CTX *mem_ctx;
32         struct nbt_name_socket *socket;
33 } nbt_node_Object;
34
35 static void py_nbt_node_dealloc(PyObject *obj)
36 {
37         talloc_free(((nbt_node_Object *)obj)->mem_ctx);
38         PyObject_Del(obj);
39 }
40
41 static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject *kwargs)
42 {
43         struct event_context *ev;
44         nbt_node_Object *ret = PyObject_New(nbt_node_Object, &nbt_node_Type);
45
46         ret->mem_ctx = talloc_new(NULL);
47         if (ret->mem_ctx == NULL)
48                 return NULL;
49
50         ev = s4_event_context_init(ret->mem_ctx);
51         ret->socket = nbt_name_socket_init(ret->mem_ctx, ev, 
52                                                                            py_iconv_convenience(ret->mem_ctx));
53         return (PyObject *)ret;
54 }
55
56 static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port)
57 {
58         if (PyString_Check(obj)) {
59                 *dest_addr = PyString_AsString(obj);
60                 *dest_port = NBT_NAME_SERVICE_PORT;
61                 return true;
62         }
63
64         if (PyTuple_Check(obj)) {
65                 if (PyTuple_Size(obj) < 1) {
66                         PyErr_SetString(PyExc_TypeError, "Destination tuple size invalid");
67                         return false;
68                 }
69
70                 if (!PyString_Check(PyTuple_GetItem(obj, 0))) {
71                         PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string");
72                         return false;
73                 }
74
75                 *dest_addr = PyString_AsString(obj);
76
77                 if (PyTuple_Size(obj) == 1) {
78                         *dest_port = NBT_NAME_SERVICE_PORT;
79                         return true;
80                 } else if (PyInt_Check(PyTuple_GetItem(obj, 1))) {
81                         *dest_port = PyInt_AsLong(PyTuple_GetItem(obj, 1));
82                         return true;
83                 } else {
84                         PyErr_SetString(PyExc_TypeError, "Destination tuple second element not a port");
85                         return false;
86                 }
87         }
88
89         PyErr_SetString(PyExc_TypeError, "Destination tuple second element not a port");
90         return false;
91 }
92
93 static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *socket, struct nbt_name *name)
94 {
95         if (PyTuple_Check(obj)) {
96                 if (PyTuple_Size(obj) == 2) {
97                         name->name = PyString_AsString(PyTuple_GetItem(obj, 0));
98                         name->type = PyInt_AsLong(PyTuple_GetItem(obj, 1));
99                         name->scope = NULL;
100                         return true;
101                 } else if (PyTuple_Size(obj) == 3) {
102                         name->name = PyString_AsString(PyTuple_GetItem(obj, 0));
103                         name->scope = PyString_AsString(PyTuple_GetItem(obj, 1));
104                         name->type = PyInt_AsLong(PyTuple_GetItem(obj, 2));
105                         return true;
106                 } else {
107                         PyErr_SetString(PyExc_TypeError, "Invalid tuple size");
108                         return false;
109                 }
110         }
111
112         if (PyString_Check(obj)) {
113                 /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */
114                 name->name = PyString_AsString(obj);
115                 name->scope = NULL;
116                 name->type = 0;
117                 return true;
118         }
119
120         PyErr_SetString(PyExc_TypeError, "Invalid type for object");
121         return false;
122 }
123
124 static PyObject *PyObject_FromNBTName(struct nbt_name_socket *socket, struct smb_iconv_convenience *ic,
125                                       struct nbt_name *name)
126 {
127         if (name->scope) {
128                 return Py_BuildValue("(ssi)", name->name, name->scope, name->type);
129         } else {
130                 return Py_BuildValue("(si)", name->name, name->type);
131         }
132 }
133
134 static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwargs)
135 {
136         nbt_node_Object *node = (nbt_node_Object *)self;
137         PyObject *ret, *reply_addrs, *py_dest, *py_name;
138         struct nbt_name_query io;
139         NTSTATUS status;
140         int i;
141
142         const char *kwnames[] = { "name", "dest", "broadcast", "wins", "timeout",
143                                   "retries", NULL };
144         io.in.broadcast = true;
145         io.in.wins_lookup = false;
146         io.in.timeout = 0;
147         io.in.retries = 3;
148
149         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|bbii:query_name",
150                                          discard_const_p(char *, kwnames),
151                                          &py_name, &py_dest,
152                                          &io.in.broadcast, &io.in.wins_lookup,
153                                          &io.in.timeout, &io.in.retries)) {
154                 return NULL;
155         }
156
157         if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
158                 return NULL;
159
160         if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
161                 return NULL;
162
163         status = nbt_name_query(node->socket, NULL, &io);
164
165         if (NT_STATUS_IS_ERR(status)) {
166                 PyErr_SetNTSTATUS(status);
167                 return NULL;
168         }
169
170         ret = PyTuple_New(3);
171         if (ret == NULL)
172                 return NULL;
173         PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
174
175         py_name = PyObject_FromNBTName(node->socket, py_iconv_convenience(node->socket), &io.out.name);
176         if (py_name == NULL)
177                 return NULL;
178
179         PyTuple_SetItem(ret, 1, py_name);
180
181         reply_addrs = PyList_New(io.out.num_addrs);
182         if (reply_addrs == NULL) {
183                 Py_DECREF(ret);
184                 return NULL;
185         }
186
187         for (i = 0; i < io.out.num_addrs; i++) {
188                 PyList_SetItem(reply_addrs, i, PyString_FromString(io.out.reply_addrs[i]));
189         }
190
191         PyTuple_SetItem(ret, 2, reply_addrs);
192         return ret;
193 }
194
195 static PyObject *py_nbt_name_status(PyObject *self, PyObject *args, PyObject *kwargs)
196 {
197         nbt_node_Object *node = (nbt_node_Object *)self;
198         PyObject *ret, *py_dest, *py_name, *py_names;
199         struct nbt_name_status io;
200         int i;
201         NTSTATUS status;
202
203         const char *kwnames[] = { "name", "dest", "timeout", "retries", NULL };
204
205         io.in.timeout = 0;
206         io.in.retries = 0;
207
208         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|ii:name_status",
209                                          discard_const_p(char *, kwnames),
210                                          &py_name, &py_dest,
211                                          &io.in.timeout, &io.in.retries)) {
212                 return NULL;
213         }
214
215         if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
216                 return NULL;
217
218         if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
219                 return NULL;
220
221         status = nbt_name_status(node->socket, NULL, &io);
222
223         if (NT_STATUS_IS_ERR(status)) {
224                 PyErr_SetNTSTATUS(status);
225                 return NULL;
226         }
227
228         ret = PyTuple_New(3);
229         if (ret == NULL)
230                 return NULL;
231         PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
232
233         py_name = PyObject_FromNBTName(node->socket, py_iconv_convenience(NULL), &io.out.name);
234         if (py_name == NULL)
235                 return NULL;
236
237         PyTuple_SetItem(ret, 1, py_name);
238
239         py_names = PyList_New(io.out.status.num_names);
240
241         for (i = 0; i < io.out.status.num_names; i++) {
242                 PyList_SetItem(py_names, i, Py_BuildValue("(sii)",
243                                 io.out.status.names[i].name,
244                                 io.out.status.names[i].nb_flags,
245                                 io.out.status.names[i].type));
246         }
247
248         PyTuple_SetItem(ret, 2, py_names);
249
250         return ret;
251 }
252
253 static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject *kwargs)
254 {
255         nbt_node_Object *node = (nbt_node_Object *)self;
256         PyObject *ret, *py_dest, *py_name;
257         struct nbt_name_register io;
258         NTSTATUS status;
259
260         const char *kwnames[] = { "name", "address", "dest", "register_demand", "broadcast",
261                                   "multi_homed", "ttl", "timeout", "retries", NULL };
262
263         io.in.broadcast = true;
264         io.in.multi_homed = true;
265         io.in.register_demand = true;
266         io.in.timeout = 0;
267         io.in.retries = 0;
268
269         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsO|bbbiii:query_name",
270                                          discard_const_p(char *, kwnames),
271                                          &py_name, &io.in.address, &py_dest,
272                                          &io.in.register_demand,
273                                          &io.in.broadcast, &io.in.multi_homed,
274                                          &io.in.ttl, &io.in.timeout, &io.in.retries)) {
275                 return NULL;
276         }
277
278         if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
279                 return NULL;
280
281         if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
282                 return NULL;
283
284         status = nbt_name_register(node->socket, NULL, &io);
285
286         if (NT_STATUS_IS_ERR(status)) {
287                 PyErr_SetNTSTATUS(status);
288                 return NULL;
289         }
290
291         ret = PyTuple_New(3);
292         if (ret == NULL)
293                 return NULL;
294         PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
295
296         py_name = PyObject_FromNBTName(node->socket, py_iconv_convenience(NULL), &io.out.name);
297         if (py_name == NULL)
298                 return NULL;
299
300         PyTuple_SetItem(ret, 1, py_name);
301
302         PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr));
303
304         PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
305
306         return ret;
307 }
308
309 static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *kwargs)
310 {
311         nbt_node_Object *node = (nbt_node_Object *)self;
312         PyObject *ret, *py_dest, *py_name;
313         struct nbt_name_refresh io;
314         NTSTATUS status;
315
316         const char *kwnames[] = { "name", "address", "dest", "nb_flags", "broadcast",
317                                   "ttl", "timeout", "retries", NULL };
318
319         io.in.broadcast = true;
320         io.in.nb_flags = 0;
321         io.in.timeout = 0;
322         io.in.retries = 0;
323
324         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsO|ibiii:query_name",
325                                          discard_const_p(char *, kwnames),
326                                          &py_name, &io.in.address, &py_dest,
327                                          &io.in.nb_flags,
328                                          &io.in.broadcast,
329                                          &io.in.ttl, &io.in.timeout, &io.in.retries)) {
330                 return NULL;
331         }
332
333         if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
334                 return NULL;
335
336         if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
337                 return NULL;
338
339         status = nbt_name_refresh(node->socket, NULL, &io);
340
341         if (NT_STATUS_IS_ERR(status)) {
342                 PyErr_SetNTSTATUS(status);
343                 return NULL;
344         }
345
346         ret = PyTuple_New(3);
347         if (ret == NULL)
348                 return NULL;
349         PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
350
351         py_name = PyObject_FromNBTName(node->socket, py_iconv_convenience(NULL), &io.out.name);
352         if (py_name == NULL)
353                 return NULL;
354
355         PyTuple_SetItem(ret, 1, py_name);
356
357         PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr));
358
359         PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
360
361         return ret;
362 }
363
364 static PyObject *py_nbt_name_release(PyObject *self, PyObject *args, PyObject *kwargs)
365 {
366         return Py_None; /* FIXME */
367 }
368
369 static PyMethodDef py_nbt_methods[] = {
370         { "query_name", (PyCFunction)py_nbt_name_query, METH_VARARGS|METH_KEYWORDS,
371                 "S.query_name(name, dest, broadcast=True, wins=False, timeout=0, retries=3) -> (reply_from, name, reply_addr)\n"
372                 "Query for a NetBIOS name" },
373         { "register_name", (PyCFunction)py_nbt_name_register, METH_VARARGS|METH_KEYWORDS,
374                 "S.register_name(name, address, dest, register_demand=True, broadcast=True, multi_homed=True, ttl=0, timeout=0, retries=0) -> (reply_from, name, reply_addr, rcode)\n"
375                 "Register a new name" },
376         { "release_name", (PyCFunction)py_nbt_name_release, METH_VARARGS|METH_KEYWORDS, "S.release_name(name, address, dest, nb_flags=0, broadcast=true, timeout=0, retries=3) -> (reply_from, name, reply_addr, rcode)\n"
377                 "release a previously registered name" },
378         { "refresh_name", (PyCFunction)py_nbt_name_refresh, METH_VARARGS|METH_KEYWORDS, "S.refresh_name(name, address, dest, nb_flags=0, broadcast=True, ttl=0, timeout=0, retries=0) -> (reply_from, name, reply_addr, rcode)\n"
379                 "release a previously registered name" },
380         { "name_status", (PyCFunction)py_nbt_name_status, METH_VARARGS|METH_KEYWORDS,
381                 "S.name_status(name, dest, timeout=0, retries=0) -> (reply_from, name, status)\n"
382                 "Find the status of a name" },
383
384         { NULL }
385 };
386
387 PyTypeObject nbt_node_Type = {
388         PyObject_HEAD_INIT(NULL) 0,
389         .tp_name = "netbios.Node",
390         .tp_basicsize = sizeof(nbt_node_Object),
391         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
392         .tp_new = py_nbt_node_init,
393         .tp_dealloc = py_nbt_node_dealloc,
394         .tp_methods = py_nbt_methods,
395         .tp_doc = "Node()\n"
396                   "Create a new NetBIOS node\n"
397 };
398
399 void initnetbios(void)
400 {
401         PyObject *mod;
402         if (PyType_Ready(&nbt_node_Type) < 0)
403                 return;
404
405         mod = Py_InitModule3("netbios", NULL, "NetBIOS over TCP/IP support");
406
407         Py_INCREF((PyObject *)&nbt_node_Type);
408         PyModule_AddObject(mod, "Node", (PyObject *)&nbt_node_Type);
409 }