2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/nbt/libnbt.h"
25 #include "libcli/dgram/libdgram.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "lib/socket/socket.h"
28 #include "lib/events/events.h"
30 #define TEST_NAME "TORTURE_TEST"
33 reply handler for netlogon request
35 static void netlogon_handler(struct dgram_mailslot_handler *dgmslot,
36 struct nbt_dgram_packet *packet,
37 const char *src_address, int src_port)
40 struct nbt_netlogon_packet netlogon;
42 printf("netlogon reply from %s:%d\n", src_address, src_port);
44 status = dgram_mailslot_netlogon_parse(dgmslot, dgmslot, packet, &netlogon);
45 if (!NT_STATUS_IS_OK(status)) {
46 printf("Failed to parse netlogon packet from %s:%d\n",
47 src_address, src_port);
51 NDR_PRINT_DEBUG(nbt_netlogon_packet, &netlogon);
55 /* test UDP/138 netlogon requests */
56 static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx,
57 struct nbt_name name, const char *address)
59 struct dgram_mailslot_handler *dgmslot;
60 struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL);
61 const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
62 struct nbt_netlogon_packet logon;
63 struct nbt_name myname;
65 int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
66 struct timeval tv = timeval_current();
68 /* try receiving replies on port 138 first, which will only
69 work if we are root and smbd/nmbd are not running - fall
70 back to listening on any port, which means replies from
71 some windows versions won't be seen */
72 status = socket_listen(dgmsock->sock, myaddress, lp_dgram_port(), 0, 0);
73 if (!NT_STATUS_IS_OK(status)) {
74 socket_listen(dgmsock->sock, myaddress, 0, 0, 0);
77 /* setup a temporary mailslot listener for replies */
78 dgmslot = dgram_mailslot_temp(dgmsock, "\\MAILSLOT\\NET\\GETDC",
79 netlogon_handler, NULL);
83 logon.command = NETLOGON_QUERY_FOR_PDC;
84 logon.req.pdc.computer_name = TEST_NAME;
85 logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
86 logon.req.pdc.unicode_name = TEST_NAME;
87 logon.req.pdc.nt_version = 1;
88 logon.req.pdc.lmnt_token = 0xFFFF;
89 logon.req.pdc.lm20_token = 0xFFFF;
91 myname.name = TEST_NAME;
92 myname.type = NBT_NAME_CLIENT;
95 status = dgram_mailslot_netlogon_send(dgmsock, &name, address, &myname, &logon);
96 if (!NT_STATUS_IS_OK(status)) {
97 printf("Failed to send netlogon request - %s\n", nt_errstr(status));
102 while (timeval_elapsed(&tv) < timelimit) {
103 event_loop_once(dgmsock->event_ctx);
106 talloc_free(dgmsock);
110 talloc_free(dgmsock);
116 test nbt dgram operations
118 BOOL torture_nbt_dgram(void)
121 struct nbt_name name;
122 TALLOC_CTX *mem_ctx = talloc_new(NULL);
126 name.name = lp_parm_string(-1, "torture", "host");
127 name.type = NBT_NAME_PDC;
130 /* do an initial name resolution to find its IP */
131 status = resolve_name(&name, mem_ctx, &address);
132 if (!NT_STATUS_IS_OK(status)) {
133 printf("Failed to resolve %s - %s\n",
134 name.name, nt_errstr(status));
135 talloc_free(mem_ctx);
139 ret &= nbt_test_netlogon(mem_ctx, name, address);
141 talloc_free(mem_ctx);