source: trunk/server/libcli/nbt/pynbt.c@ 855

Last change on this file since 855 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

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