aa4759edaf98f9185815413fb5f831dba6a6ecca
[kai/samba.git] / source4 / torture / nbt / dgram.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT dgram testing
5
6    Copyright (C) Andrew Tridgell 2005
7    
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/dgram/libdgram.h"
24 #include "lib/socket/socket.h"
25 #include "lib/events/events.h"
26 #include "torture/rpc/torture_rpc.h"
27 #include "libcli/resolve/resolve.h"
28 #include "system/network.h"
29 #include "lib/socket/netif.h"
30 #include "param/param.h"
31
32 #define TEST_NAME "TORTURE_TEST"
33
34 /*
35   reply handler for netlogon request
36 */
37 static void netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
38                              struct nbt_dgram_packet *packet, 
39                              struct socket_address *src)
40 {
41         NTSTATUS status;
42         struct nbt_netlogon_response *netlogon = dgmslot->private_data;
43
44         dgmslot->private_data = netlogon = talloc(dgmslot, struct nbt_netlogon_response);
45
46         if (!dgmslot->private_data) {
47                 return;
48         }
49        
50         printf("netlogon reply from %s:%d\n", src->addr, src->port);
51
52         /* Fills in the netlogon pointer */
53         status = dgram_mailslot_netlogon_parse_response(dgmslot, netlogon, packet, netlogon);
54         if (!NT_STATUS_IS_OK(status)) {
55                 printf("Failed to parse netlogon packet from %s:%d\n",
56                        src->addr, src->port);
57                 return;
58         }
59
60 }
61
62
63 /* test UDP/138 netlogon requests */
64 static bool nbt_test_netlogon(struct torture_context *tctx)
65 {
66         struct dgram_mailslot_handler *dgmslot;
67         struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(tctx, tctx->ev);
68         struct socket_address *dest;
69         const char *myaddress;
70         struct nbt_netlogon_packet logon;
71         struct nbt_netlogon_response *response;
72         struct nbt_name myname;
73         NTSTATUS status;
74         struct timeval tv = timeval_current();
75
76         struct socket_address *socket_address;
77
78         const char *address;
79         struct nbt_name name;
80
81         struct interface *ifaces;
82
83         name.name = lpcfg_workgroup(tctx->lp_ctx);
84         name.type = NBT_NAME_LOGON;
85         name.scope = NULL;
86
87         /* do an initial name resolution to find its IP */
88         torture_assert_ntstatus_ok(tctx, 
89                                    resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
90                                                    0, 0,
91                                                    &name, tctx, &address, tctx->ev),
92                                    talloc_asprintf(tctx, "Failed to resolve %s", name.name));
93
94         load_interface_list(tctx, tctx->lp_ctx, &ifaces);
95         myaddress = talloc_strdup(dgmsock, iface_list_best_ip(ifaces, address));
96
97
98         socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
99                                                      myaddress, lpcfg_dgram_port(tctx->lp_ctx));
100         torture_assert(tctx, socket_address != NULL, "Error getting address");
101
102         /* try receiving replies on port 138 first, which will only
103            work if we are root and smbd/nmbd are not running - fall
104            back to listening on any port, which means replies from
105            most windows versions won't be seen */
106         status = socket_listen(dgmsock->sock, socket_address, 0, 0);
107         if (!NT_STATUS_IS_OK(status)) {
108                 talloc_free(socket_address);
109                 socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
110                                                              myaddress, 0);
111                 torture_assert(tctx, socket_address != NULL, "Error getting address");
112
113                 socket_listen(dgmsock->sock, socket_address, 0, 0);
114         }
115
116         /* setup a temporary mailslot listener for replies */
117         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
118                                       netlogon_handler, NULL);
119         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
120
121         ZERO_STRUCT(logon);
122         logon.command = LOGON_PRIMARY_QUERY;
123         logon.req.pdc.computer_name = TEST_NAME;
124         logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
125         logon.req.pdc.unicode_name  = TEST_NAME;
126         logon.req.pdc.nt_version    = 1;
127         logon.req.pdc.lmnt_token    = 0xFFFF;
128         logon.req.pdc.lm20_token    = 0xFFFF;
129
130         make_nbt_name_client(&myname, TEST_NAME);
131
132         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
133                                            address, lpcfg_dgram_port(tctx->lp_ctx));
134         torture_assert(tctx, dest != NULL, "Error getting address");
135
136         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
137                                               NBT_MAILSLOT_NETLOGON, 
138                                               &myname, &logon);
139         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
140
141         while (timeval_elapsed(&tv) < 5 && !dgmslot->private_data) {
142                 event_loop_once(dgmsock->event_ctx);
143         }
144
145         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
146
147         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
148
149         torture_assert(tctx, response->response_type == NETLOGON_GET_PDC, "Got incorrect type of netlogon response");
150         torture_assert(tctx, response->data.get_pdc.command == NETLOGON_RESPONSE_FROM_PDC, "Got incorrect netlogon response command");
151
152         return true;
153 }
154
155
156 /* test UDP/138 netlogon requests */
157 static bool nbt_test_netlogon2(struct torture_context *tctx)
158 {
159         struct dgram_mailslot_handler *dgmslot;
160         struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(tctx, tctx->ev);
161         struct socket_address *dest;
162         const char *myaddress;
163         struct nbt_netlogon_packet logon;
164         struct nbt_netlogon_response *response;
165         struct nbt_name myname;
166         NTSTATUS status;
167         struct timeval tv = timeval_current();
168
169         struct socket_address *socket_address;
170
171         const char *address;
172         struct nbt_name name;
173
174         struct interface *ifaces;
175         struct test_join *join_ctx;
176         struct cli_credentials *machine_credentials;
177         const struct dom_sid *dom_sid;
178         
179         name.name = lpcfg_workgroup(tctx->lp_ctx);
180         name.type = NBT_NAME_LOGON;
181         name.scope = NULL;
182
183         /* do an initial name resolution to find its IP */
184         torture_assert_ntstatus_ok(tctx, 
185                                    resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
186                                                    0, 0,
187                                                    &name, tctx, &address, tctx->ev),
188                                    talloc_asprintf(tctx, "Failed to resolve %s", name.name));
189
190         load_interface_list(tctx, tctx->lp_ctx, &ifaces);
191         myaddress = talloc_strdup(dgmsock, iface_list_best_ip(ifaces, address));
192
193         socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
194                                                      myaddress, lpcfg_dgram_port(tctx->lp_ctx));
195         torture_assert(tctx, socket_address != NULL, "Error getting address");
196
197         /* try receiving replies on port 138 first, which will only
198            work if we are root and smbd/nmbd are not running - fall
199            back to listening on any port, which means replies from
200            some windows versions won't be seen */
201         status = socket_listen(dgmsock->sock, socket_address, 0, 0);
202         if (!NT_STATUS_IS_OK(status)) {
203                 talloc_free(socket_address);
204                 socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
205                                                              myaddress, 0);
206                 torture_assert(tctx, socket_address != NULL, "Error getting address");
207
208                 socket_listen(dgmsock->sock, socket_address, 0, 0);
209         }
210
211         /* setup a temporary mailslot listener for replies */
212         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
213                                       netlogon_handler, NULL);
214         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
215
216         ZERO_STRUCT(logon);
217         logon.command = LOGON_SAM_LOGON_REQUEST;
218         logon.req.logon.request_count = 0;
219         logon.req.logon.computer_name = TEST_NAME;
220         logon.req.logon.user_name     = "";
221         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
222         logon.req.logon.nt_version    = NETLOGON_NT_VERSION_5EX_WITH_IP|NETLOGON_NT_VERSION_5|NETLOGON_NT_VERSION_1;
223         logon.req.logon.lmnt_token    = 0xFFFF;
224         logon.req.logon.lm20_token    = 0xFFFF;
225
226         make_nbt_name_client(&myname, TEST_NAME);
227
228         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
229                                            address, lpcfg_dgram_port(tctx->lp_ctx));
230
231         torture_assert(tctx, dest != NULL, "Error getting address");
232         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
233                                               NBT_MAILSLOT_NETLOGON, 
234                                               &myname, &logon);
235         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
236
237         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
238                 event_loop_once(dgmsock->event_ctx);
239         }
240
241         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
242
243         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
244
245         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
246         map_netlogon_samlogon_response(&response->data.samlogon);
247
248         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX, "Got incorrect netlogon response command");
249         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.nt_version, NETLOGON_NT_VERSION_5EX_WITH_IP|NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_1, "Got incorrect netlogon response command");
250
251         /* setup (another) temporary mailslot listener for replies */
252         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
253                                       netlogon_handler, NULL);
254         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
255         
256         ZERO_STRUCT(logon);
257         logon.command = LOGON_SAM_LOGON_REQUEST;
258         logon.req.logon.request_count = 0;
259         logon.req.logon.computer_name = TEST_NAME;
260         logon.req.logon.user_name     = TEST_NAME"$";
261         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
262         logon.req.logon.nt_version    = 1;
263         logon.req.logon.lmnt_token    = 0xFFFF;
264         logon.req.logon.lm20_token    = 0xFFFF;
265
266         make_nbt_name_client(&myname, TEST_NAME);
267
268         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
269                                            address, lpcfg_dgram_port(tctx->lp_ctx));
270
271         torture_assert(tctx, dest != NULL, "Error getting address");
272         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
273                                               NBT_MAILSLOT_NETLOGON, 
274                                               &myname, &logon);
275         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
276
277         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
278                 event_loop_once(dgmsock->event_ctx);
279         }
280
281         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
282
283         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
284
285         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
286         map_netlogon_samlogon_response(&response->data.samlogon);
287
288         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");
289
290         torture_assert_str_equal(tctx, response->data.samlogon.data.nt5_ex.user_name, TEST_NAME"$", "Got incorrect user in netlogon response");
291
292         join_ctx = torture_join_domain(tctx, TEST_NAME, 
293                                        ACB_WSTRUST, &machine_credentials);
294
295         torture_assert(tctx, join_ctx != NULL,
296                        talloc_asprintf(tctx, "Failed to join domain %s as %s\n",
297                                        lpcfg_workgroup(tctx->lp_ctx), TEST_NAME));
298
299         dom_sid = torture_join_sid(join_ctx);
300
301         /* setup (another) temporary mailslot listener for replies */
302         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
303                                       netlogon_handler, NULL);
304         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
305         
306         ZERO_STRUCT(logon);
307         logon.command = LOGON_SAM_LOGON_REQUEST;
308         logon.req.logon.request_count = 0;
309         logon.req.logon.computer_name = TEST_NAME;
310         logon.req.logon.user_name     = TEST_NAME"$";
311         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
312         logon.req.logon.sid           = *dom_sid;
313         logon.req.logon.nt_version    = 1;
314         logon.req.logon.lmnt_token    = 0xFFFF;
315         logon.req.logon.lm20_token    = 0xFFFF;
316
317         make_nbt_name_client(&myname, TEST_NAME);
318
319         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
320                                            address, lpcfg_dgram_port(tctx->lp_ctx));
321
322         torture_assert(tctx, dest != NULL, "Error getting address");
323         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
324                                               NBT_MAILSLOT_NETLOGON, 
325                                               &myname, &logon);
326         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
327
328
329         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
330                 event_loop_once(dgmsock->event_ctx);
331         }
332
333         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
334
335         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
336
337         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
338         map_netlogon_samlogon_response(&response->data.samlogon);
339
340         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");
341
342         /* setup (another) temporary mailslot listener for replies */
343         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
344                                       netlogon_handler, NULL);
345         torture_assert(tctx, dgmslot != NULL, "Error getting a Mailslot for GetDC reply");
346
347         ZERO_STRUCT(logon);
348         logon.command = LOGON_SAM_LOGON_REQUEST;
349         logon.req.logon.request_count = 0;
350         logon.req.logon.computer_name = TEST_NAME;
351         logon.req.logon.user_name     = TEST_NAME"$";
352         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
353         logon.req.logon.sid           = *dom_sid;
354         logon.req.logon.acct_control  = ACB_WSTRUST;
355         logon.req.logon.nt_version    = 1;
356         logon.req.logon.lmnt_token    = 0xFFFF;
357         logon.req.logon.lm20_token    = 0xFFFF;
358
359         make_nbt_name_client(&myname, TEST_NAME);
360
361         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
362                                            address, lpcfg_dgram_port(tctx->lp_ctx));
363
364         torture_assert(tctx, dest != NULL, "Error getting address");
365         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
366                                               NBT_MAILSLOT_NETLOGON, 
367                                               &myname, &logon);
368         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
369
370
371         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
372                 event_loop_once(dgmsock->event_ctx);
373         }
374
375         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
376
377         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
378
379         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
380         map_netlogon_samlogon_response(&response->data.samlogon);
381
382         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE, "Got incorrect netlogon response command");
383
384         dgmslot->private_data = NULL;
385
386         ZERO_STRUCT(logon);
387         logon.command = LOGON_SAM_LOGON_REQUEST;
388         logon.req.logon.request_count = 0;
389         logon.req.logon.computer_name = TEST_NAME;
390         logon.req.logon.user_name     = TEST_NAME"$";
391         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
392         logon.req.logon.sid           = *dom_sid;
393         logon.req.logon.acct_control  = ACB_NORMAL;
394         logon.req.logon.nt_version    = 1;
395         logon.req.logon.lmnt_token    = 0xFFFF;
396         logon.req.logon.lm20_token    = 0xFFFF;
397
398         make_nbt_name_client(&myname, TEST_NAME);
399
400         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
401                                            address, lpcfg_dgram_port(tctx->lp_ctx));
402
403         torture_assert(tctx, dest != NULL, "Error getting address");
404         status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
405                                               NBT_MAILSLOT_NETLOGON, 
406                                               &myname, &logon);
407         torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");
408
409
410         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
411                 event_loop_once(dgmsock->event_ctx);
412         }
413
414         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
415
416         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
417
418         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
419         map_netlogon_samlogon_response(&response->data.samlogon);
420
421         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");
422
423         torture_leave_domain(tctx, join_ctx);
424         return true;
425 }
426
427
428 /* test UDP/138 ntlogon requests */
429 static bool nbt_test_ntlogon(struct torture_context *tctx)
430 {
431         struct dgram_mailslot_handler *dgmslot;
432         struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(tctx, tctx->ev);
433         struct socket_address *dest;
434         struct test_join *join_ctx;
435         const struct dom_sid *dom_sid;
436         struct cli_credentials *machine_credentials;
437
438         const char *myaddress;
439         struct nbt_netlogon_packet logon;
440         struct nbt_netlogon_response *response;
441         struct nbt_name myname;
442         NTSTATUS status;
443         struct timeval tv = timeval_current();
444
445         struct socket_address *socket_address;
446         const char *address;
447         struct nbt_name name;
448
449         struct interface *ifaces;
450         
451         name.name = lpcfg_workgroup(tctx->lp_ctx);
452         name.type = NBT_NAME_LOGON;
453         name.scope = NULL;
454
455         /* do an initial name resolution to find its IP */
456         torture_assert_ntstatus_ok(tctx, 
457                                    resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
458                                                    0, 0, &name, tctx, &address, tctx->ev),
459                                    talloc_asprintf(tctx, "Failed to resolve %s", name.name));
460
461         load_interface_list(tctx, tctx->lp_ctx, &ifaces);
462         myaddress = talloc_strdup(dgmsock, iface_list_best_ip(ifaces, address));
463
464         socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
465                                                      myaddress, lpcfg_dgram_port(tctx->lp_ctx));
466         torture_assert(tctx, socket_address != NULL, "Error getting address");
467
468         /* try receiving replies on port 138 first, which will only
469            work if we are root and smbd/nmbd are not running - fall
470            back to listening on any port, which means replies from
471            most windows versions won't be seen */
472         status = socket_listen(dgmsock->sock, socket_address, 0, 0);
473         if (!NT_STATUS_IS_OK(status)) {
474                 talloc_free(socket_address);
475                 socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
476                                                              myaddress, 0);
477                 torture_assert(tctx, socket_address != NULL, "Error getting address");
478
479                 socket_listen(dgmsock->sock, socket_address, 0, 0);
480         }
481
482         join_ctx = torture_join_domain(tctx, TEST_NAME, 
483                                        ACB_WSTRUST, &machine_credentials);
484         dom_sid = torture_join_sid(join_ctx);
485
486         torture_assert(tctx, join_ctx != NULL,
487                        talloc_asprintf(tctx, "Failed to join domain %s as %s\n",
488                                        lpcfg_workgroup(tctx->lp_ctx), TEST_NAME));
489
490         /* setup a temporary mailslot listener for replies */
491         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
492                                       netlogon_handler, NULL);
493         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
494
495         ZERO_STRUCT(logon);
496         logon.command = LOGON_SAM_LOGON_REQUEST;
497         logon.req.logon.request_count = 0;
498         logon.req.logon.computer_name = TEST_NAME;
499         logon.req.logon.user_name     = TEST_NAME"$";
500         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
501         logon.req.logon.acct_control  = ACB_WSTRUST;
502         /* Try with a SID this time */
503         logon.req.logon.sid           = *dom_sid;
504         logon.req.logon.nt_version    = 1;
505         logon.req.logon.lmnt_token    = 0xFFFF;
506         logon.req.logon.lm20_token    = 0xFFFF;
507
508         make_nbt_name_client(&myname, TEST_NAME);
509
510         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
511                                            address, lpcfg_dgram_port(tctx->lp_ctx));
512         torture_assert(tctx, dest != NULL, "Error getting address");
513         status = dgram_mailslot_netlogon_send(dgmsock, 
514                                               &name, dest, 
515                                               NBT_MAILSLOT_NTLOGON, 
516                                               &myname, &logon);
517         torture_assert_ntstatus_ok(tctx, status, "Failed to send ntlogon request");
518
519         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
520                 event_loop_once(dgmsock->event_ctx);
521         }
522
523         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
524
525         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
526
527         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
528         map_netlogon_samlogon_response(&response->data.samlogon);
529
530         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE, "Got incorrect netlogon response command");
531
532         torture_assert_str_equal(tctx, response->data.samlogon.data.nt5_ex.user_name, TEST_NAME"$", "Got incorrect user in netlogon response");
533
534
535         /* setup a temporary mailslot listener for replies */
536         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
537                                       netlogon_handler, NULL);
538         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
539
540         ZERO_STRUCT(logon);
541         logon.command = LOGON_SAM_LOGON_REQUEST;
542         logon.req.logon.request_count = 0;
543         logon.req.logon.computer_name = TEST_NAME;
544         logon.req.logon.user_name     = TEST_NAME"$";
545         logon.req.logon.mailslot_name = dgmslot->mailslot_name;
546         logon.req.logon.acct_control  = ACB_WSTRUST;
547         /* Leave sid as all zero */
548         logon.req.logon.nt_version    = 1;
549         logon.req.logon.lmnt_token    = 0xFFFF;
550         logon.req.logon.lm20_token    = 0xFFFF;
551
552         make_nbt_name_client(&myname, TEST_NAME);
553
554         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
555                                            address, lpcfg_dgram_port(tctx->lp_ctx));
556         torture_assert(tctx, dest != NULL, "Error getting address");
557         status = dgram_mailslot_netlogon_send(dgmsock, 
558                                               &name, dest, 
559                                               NBT_MAILSLOT_NTLOGON, 
560                                               &myname, &logon);
561         torture_assert_ntstatus_ok(tctx, status, "Failed to send ntlogon request");
562
563         while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
564                 event_loop_once(dgmsock->event_ctx);
565         }
566
567         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
568
569         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
570
571         torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
572         map_netlogon_samlogon_response(&response->data.samlogon);
573
574         torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE, "Got incorrect netlogon response command");
575
576         torture_assert_str_equal(tctx, response->data.samlogon.data.nt5_ex.user_name, TEST_NAME"$", "Got incorrect user in netlogon response");
577
578
579         /* setup (another) temporary mailslot listener for replies */
580         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
581                                       netlogon_handler, NULL);
582         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
583         
584         ZERO_STRUCT(logon);
585         logon.command = LOGON_PRIMARY_QUERY;
586         logon.req.pdc.computer_name = TEST_NAME;
587         logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
588         logon.req.pdc.unicode_name  = TEST_NAME;
589         logon.req.pdc.nt_version    = 1;
590         logon.req.pdc.lmnt_token    = 0xFFFF;
591         logon.req.pdc.lm20_token    = 0xFFFF;
592
593         make_nbt_name_client(&myname, TEST_NAME);
594
595         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
596                                            address, lpcfg_dgram_port(tctx->lp_ctx));
597         torture_assert(tctx, dest != NULL, "Error getting address");
598         status = dgram_mailslot_netlogon_send(dgmsock, 
599                                               &name, dest, 
600                                               NBT_MAILSLOT_NTLOGON, 
601                                               &myname, &logon);
602         torture_assert_ntstatus_ok(tctx, status, "Failed to send ntlogon request");
603
604         while (timeval_elapsed(&tv) < 5 && !dgmslot->private_data) {
605                 event_loop_once(dgmsock->event_ctx);
606         }
607
608         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
609
610         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
611
612         torture_assert_int_equal(tctx, response->response_type, NETLOGON_GET_PDC, "Got incorrect type of ntlogon response");
613         torture_assert_int_equal(tctx, response->data.get_pdc.command, NETLOGON_RESPONSE_FROM_PDC, "Got incorrect ntlogon response command");
614
615         torture_leave_domain(tctx, join_ctx);
616
617         /* setup (another) temporary mailslot listener for replies */
618         dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
619                                       netlogon_handler, NULL);
620         torture_assert(tctx, dgmslot != NULL, "Error temporary mailslot for GetDC");
621         
622         ZERO_STRUCT(logon);
623         logon.command = LOGON_PRIMARY_QUERY;
624         logon.req.pdc.computer_name = TEST_NAME;
625         logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
626         logon.req.pdc.unicode_name  = TEST_NAME;
627         logon.req.pdc.nt_version    = 1;
628         logon.req.pdc.lmnt_token    = 0xFFFF;
629         logon.req.pdc.lm20_token    = 0xFFFF;
630
631         make_nbt_name_client(&myname, TEST_NAME);
632
633         dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
634                                            address, lpcfg_dgram_port(tctx->lp_ctx));
635         torture_assert(tctx, dest != NULL, "Error getting address");
636         status = dgram_mailslot_netlogon_send(dgmsock, 
637                                               &name, dest, 
638                                               NBT_MAILSLOT_NTLOGON, 
639                                               &myname, &logon);
640         torture_assert_ntstatus_ok(tctx, status, "Failed to send ntlogon request");
641
642         while (timeval_elapsed(&tv) < 5 && !dgmslot->private_data) {
643                 event_loop_once(dgmsock->event_ctx);
644         }
645
646         response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);
647
648         torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");
649
650         torture_assert_int_equal(tctx, response->response_type, NETLOGON_GET_PDC, "Got incorrect type of ntlogon response");
651         torture_assert_int_equal(tctx, response->data.get_pdc.command, NETLOGON_RESPONSE_FROM_PDC, "Got incorrect ntlogon response command");
652
653
654         return true;
655 }
656
657
658 /*
659   test nbt dgram operations
660 */
661 struct torture_suite *torture_nbt_dgram(TALLOC_CTX *mem_ctx)
662 {
663         struct torture_suite *suite = torture_suite_create(mem_ctx, "dgram");
664
665         torture_suite_add_simple_test(suite, "netlogon", nbt_test_netlogon);
666         torture_suite_add_simple_test(suite, "netlogon2", nbt_test_netlogon2);
667         torture_suite_add_simple_test(suite, "ntlogon", nbt_test_ntlogon);
668
669         return suite;
670 }