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