librpc/tables.pl: remove unused $opt_output option
[Samba.git] / libcli / nbt / pynbt.c
blob032561a4bd8a7b062526aa5344576d5913478f52
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
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.
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.
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/>.
20 #include <Python.h>
21 #include "includes.h"
22 #include "python/py3compat.h"
23 #include "libcli/util/pyerrors.h"
24 #include "python/modules.h"
25 #include "../libcli/nbt/libnbt.h"
26 #include "lib/events/events.h"
28 void initnetbios(void);
30 extern PyTypeObject nbt_node_Type;
32 typedef struct {
33 PyObject_HEAD
34 TALLOC_CTX *mem_ctx;
35 struct nbt_name_socket *socket;
36 } nbt_node_Object;
38 static void py_nbt_node_dealloc(nbt_node_Object *self)
40 talloc_free(self->mem_ctx);
41 Py_TYPE(self)->tp_free(self);
44 static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject *kwargs)
46 struct tevent_context *ev;
47 nbt_node_Object *ret = PyObject_New(nbt_node_Object, &nbt_node_Type);
49 ret->mem_ctx = talloc_new(NULL);
50 if (ret->mem_ctx == NULL)
51 return NULL;
53 ev = s4_event_context_init(ret->mem_ctx);
54 ret->socket = nbt_name_socket_init(ret->mem_ctx, ev);
55 return (PyObject *)ret;
58 static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port)
60 if (PyStr_Check(obj) || PyUnicode_Check(obj)) {
61 *dest_addr = PyStr_AsString(obj);
62 *dest_port = NBT_NAME_SERVICE_PORT;
63 return true;
66 if (PyTuple_Check(obj)) {
67 if (PyTuple_Size(obj) < 1) {
68 PyErr_SetString(PyExc_TypeError, "Destination tuple size invalid");
69 return false;
72 if (!(PyStr_Check(PyTuple_GetItem(obj, 0)) || PyUnicode_Check(PyTuple_GetItem(obj, 0)))) {
73 PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string");
74 return false;
77 *dest_addr = PyStr_AsString(obj);
79 if (PyTuple_Size(obj) == 1) {
80 *dest_port = NBT_NAME_SERVICE_PORT;
81 return true;
82 } else if (PyInt_Check(PyTuple_GetItem(obj, 1))) {
83 *dest_port = PyInt_AsLong(PyTuple_GetItem(obj, 1));
84 return true;
85 } else {
86 PyErr_SetString(PyExc_TypeError, "Destination tuple second element not a port");
87 return false;
91 PyErr_SetString(PyExc_TypeError, "Destination tuple second element not a port");
92 return false;
95 static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socket, struct nbt_name *name)
97 if (PyTuple_Check(obj)) {
98 if (PyTuple_Size(obj) == 2) {
99 name->name = PyStr_AsString(PyTuple_GetItem(obj, 0));
100 name->type = PyInt_AsLong(PyTuple_GetItem(obj, 1));
101 name->scope = NULL;
102 return true;
103 } else if (PyTuple_Size(obj) == 3) {
104 name->name = PyStr_AsString(PyTuple_GetItem(obj, 0));
105 name->scope = PyStr_AsString(PyTuple_GetItem(obj, 1));
106 name->type = PyInt_AsLong(PyTuple_GetItem(obj, 2));
107 return true;
108 } else {
109 PyErr_SetString(PyExc_TypeError, "Invalid tuple size");
110 return false;
114 if (PyStr_Check(obj) || PyUnicode_Check(obj)) {
115 /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */
116 name->name = PyStr_AsString(obj);
117 name->scope = NULL;
118 name->type = 0;
119 return true;
122 PyErr_SetString(PyExc_TypeError, "Invalid type for object");
123 return false;
126 static PyObject *PyObject_FromNBTName(struct nbt_name_socket *name_socket,
127 struct nbt_name *name)
129 if (name->scope) {
130 return Py_BuildValue("(ssi)", name->name, name->scope, name->type);
131 } else {
132 return Py_BuildValue("(si)", name->name, name->type);
136 static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwargs)
138 nbt_node_Object *node = (nbt_node_Object *)self;
139 PyObject *ret, *reply_addrs, *py_dest, *py_name;
140 struct nbt_name_query io;
141 NTSTATUS status;
142 int i;
144 const char *kwnames[] = { "name", "dest", "broadcast", "wins", "timeout",
145 "retries", NULL };
146 io.in.broadcast = true;
147 io.in.wins_lookup = false;
148 io.in.timeout = 0;
149 io.in.retries = 3;
151 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|bbii:query_name",
152 discard_const_p(char *, kwnames),
153 &py_name, &py_dest,
154 &io.in.broadcast, &io.in.wins_lookup,
155 &io.in.timeout, &io.in.retries)) {
156 return NULL;
159 if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
160 return NULL;
162 if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
163 return NULL;
165 status = nbt_name_query(node->socket, NULL, &io);
167 if (NT_STATUS_IS_ERR(status)) {
168 PyErr_SetNTSTATUS(status);
169 return NULL;
172 ret = PyTuple_New(3);
173 if (ret == NULL)
174 return NULL;
175 PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
177 py_name = PyObject_FromNBTName(node->socket, &io.out.name);
178 if (py_name == NULL)
179 return NULL;
181 PyTuple_SetItem(ret, 1, py_name);
183 reply_addrs = PyList_New(io.out.num_addrs);
184 if (reply_addrs == NULL) {
185 Py_DECREF(ret);
186 return NULL;
189 for (i = 0; i < io.out.num_addrs; i++) {
190 PyList_SetItem(reply_addrs, i, PyStr_FromString(io.out.reply_addrs[i]));
193 PyTuple_SetItem(ret, 2, reply_addrs);
194 return ret;
197 static PyObject *py_nbt_name_status(PyObject *self, PyObject *args, PyObject *kwargs)
199 nbt_node_Object *node = (nbt_node_Object *)self;
200 PyObject *ret, *py_dest, *py_name, *py_names;
201 struct nbt_name_status io;
202 int i;
203 NTSTATUS status;
205 const char *kwnames[] = { "name", "dest", "timeout", "retries", NULL };
207 io.in.timeout = 0;
208 io.in.retries = 0;
210 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|ii:name_status",
211 discard_const_p(char *, kwnames),
212 &py_name, &py_dest,
213 &io.in.timeout, &io.in.retries)) {
214 return NULL;
217 if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
218 return NULL;
220 if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
221 return NULL;
223 status = nbt_name_status(node->socket, NULL, &io);
225 if (NT_STATUS_IS_ERR(status)) {
226 PyErr_SetNTSTATUS(status);
227 return NULL;
230 ret = PyTuple_New(3);
231 if (ret == NULL)
232 return NULL;
233 PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
235 py_name = PyObject_FromNBTName(node->socket, &io.out.name);
236 if (py_name == NULL)
237 return NULL;
239 PyTuple_SetItem(ret, 1, py_name);
241 py_names = PyList_New(io.out.status.num_names);
243 for (i = 0; i < io.out.status.num_names; i++) {
244 PyList_SetItem(py_names, i, Py_BuildValue("(sii)",
245 io.out.status.names[i].name,
246 io.out.status.names[i].nb_flags,
247 io.out.status.names[i].type));
250 PyTuple_SetItem(ret, 2, py_names);
252 return ret;
255 static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject *kwargs)
257 nbt_node_Object *node = (nbt_node_Object *)self;
258 PyObject *ret, *py_dest, *py_name;
259 struct nbt_name_register io;
260 NTSTATUS status;
262 const char *kwnames[] = { "name", "address", "dest", "register_demand", "broadcast",
263 "multi_homed", "ttl", "timeout", "retries", NULL };
265 io.in.broadcast = true;
266 io.in.multi_homed = true;
267 io.in.register_demand = true;
268 io.in.ttl = 0;
269 io.in.timeout = 0;
270 io.in.retries = 0;
272 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsO|bbbiii:query_name",
273 discard_const_p(char *, kwnames),
274 &py_name, &io.in.address, &py_dest,
275 &io.in.register_demand,
276 &io.in.broadcast, &io.in.multi_homed,
277 &io.in.ttl, &io.in.timeout, &io.in.retries)) {
278 return NULL;
281 if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
282 return NULL;
284 if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
285 return NULL;
287 status = nbt_name_register(node->socket, NULL, &io);
289 if (NT_STATUS_IS_ERR(status)) {
290 PyErr_SetNTSTATUS(status);
291 return NULL;
294 ret = PyTuple_New(4);
295 if (ret == NULL)
296 return NULL;
297 PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
299 py_name = PyObject_FromNBTName(node->socket, &io.out.name);
300 if (py_name == NULL)
301 return NULL;
303 PyTuple_SetItem(ret, 1, py_name);
305 PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr));
307 PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
309 return ret;
312 static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *kwargs)
314 nbt_node_Object *node = (nbt_node_Object *)self;
315 PyObject *ret, *py_dest, *py_name;
316 struct nbt_name_refresh io;
317 NTSTATUS status;
319 const char *kwnames[] = { "name", "address", "dest", "nb_flags", "broadcast",
320 "ttl", "timeout", "retries", NULL };
322 io.in.broadcast = true;
323 io.in.nb_flags = 0;
324 io.in.ttl = 0;
325 io.in.timeout = 0;
326 io.in.retries = 0;
328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsO|ibiii:query_name",
329 discard_const_p(char *, kwnames),
330 &py_name, &io.in.address, &py_dest,
331 &io.in.nb_flags,
332 &io.in.broadcast,
333 &io.in.ttl, &io.in.timeout, &io.in.retries)) {
334 return NULL;
337 if (!PyObject_AsDestinationTuple(py_dest, &io.in.dest_addr, &io.in.dest_port))
338 return NULL;
340 if (!PyObject_AsNBTName(py_name, node->socket, &io.in.name))
341 return NULL;
343 status = nbt_name_refresh(node->socket, NULL, &io);
345 if (NT_STATUS_IS_ERR(status)) {
346 PyErr_SetNTSTATUS(status);
347 return NULL;
350 ret = PyTuple_New(3);
351 if (ret == NULL)
352 return NULL;
353 PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
355 py_name = PyObject_FromNBTName(node->socket, &io.out.name);
356 if (py_name == NULL)
357 return NULL;
359 PyTuple_SetItem(ret, 1, py_name);
361 PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr));
363 PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
365 return ret;
368 static PyObject *py_nbt_name_release(PyObject *self, PyObject *args, PyObject *kwargs)
370 Py_RETURN_NONE; /* FIXME */
373 static PyMethodDef py_nbt_methods[] = {
374 { "query_name", (PyCFunction)py_nbt_name_query, METH_VARARGS|METH_KEYWORDS,
375 "S.query_name(name, dest, broadcast=True, wins=False, timeout=0, retries=3) -> (reply_from, name, reply_addr)\n"
376 "Query for a NetBIOS name" },
377 { "register_name", (PyCFunction)py_nbt_name_register, METH_VARARGS|METH_KEYWORDS,
378 "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"
379 "Register a new name" },
380 { "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"
381 "release a previously registered name" },
382 { "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"
383 "release a previously registered name" },
384 { "name_status", (PyCFunction)py_nbt_name_status, METH_VARARGS|METH_KEYWORDS,
385 "S.name_status(name, dest, timeout=0, retries=0) -> (reply_from, name, status)\n"
386 "Find the status of a name" },
388 { NULL }
391 PyTypeObject nbt_node_Type = {
392 PyVarObject_HEAD_INIT(NULL, 0)
393 .tp_name = "netbios.Node",
394 .tp_basicsize = sizeof(nbt_node_Object),
395 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
396 .tp_new = py_nbt_node_init,
397 .tp_dealloc = (destructor)py_nbt_node_dealloc,
398 .tp_methods = py_nbt_methods,
399 .tp_doc = "Node()\n"
400 "Create a new NetBIOS node\n"
403 static struct PyModuleDef moduledef = {
404 PyModuleDef_HEAD_INIT,
405 .m_name = "netbios",
406 .m_doc = "NetBIOS over TCP/IP support",
407 .m_size = -1,
408 .m_methods = NULL,
411 MODULE_INIT_FUNC(netbios)
413 PyObject *mod = NULL;
414 if (PyType_Ready(&nbt_node_Type) < 0)
415 return mod;
417 mod = PyModule_Create(&moduledef);
420 Py_INCREF((PyObject *)&nbt_node_Type);
421 PyModule_AddObject(mod, "Node", (PyObject *)&nbt_node_Type);
422 return mod;