smbd: Clarify smb2 lock checks
[Samba.git] / source4 / torture / rpc / epmapper.c
blob9033d58083b49ec9f14fdc8c623d01fd4f825946
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for epmapper rpc operations
5 Copyright (C) Andrew Tridgell 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "librpc/gen_ndr/ndr_epmapper_c.h"
23 #include "librpc/ndr/ndr_table.h"
24 #include "librpc/rpc/dcerpc_proto.h"
25 #include "torture/rpc/torture_rpc.h"
26 #include "lib/util/util_net.h"
27 #include "librpc/rpc/rpc_common.h"
30 display any protocol tower
32 static void display_tower(struct torture_context *tctx, struct epm_tower *twr)
34 int i;
36 for (i = 0; i < twr->num_floors; i++) {
37 torture_comment(tctx,
38 " %s",
39 epm_floor_string(tctx, &twr->floors[i]));
41 torture_comment(tctx, "\n");
44 static bool test_Insert(struct torture_context *tctx,
45 struct dcerpc_binding_handle *h,
46 struct ndr_syntax_id object,
47 const char *annotation,
48 const struct dcerpc_binding *b)
50 struct epm_Insert r;
51 NTSTATUS status;
53 r.in.num_ents = 1;
54 r.in.entries = talloc_array(tctx, struct epm_entry_t, 1);
56 if (torture_setting_bool(tctx, "samba4", false)) {
57 torture_skip(tctx, "Skip Insert test against Samba4");
60 /* FIXME zero */
61 ZERO_STRUCT(r.in.entries[0].object);
62 r.in.entries[0].annotation = annotation;
64 r.in.entries[0].tower = talloc(tctx, struct epm_twr_t);
66 status = dcerpc_binding_build_tower(tctx,
68 &r.in.entries[0].tower->tower);
70 torture_assert_ntstatus_ok(tctx,
71 status,
72 "Unable to build tower from binding struct");
73 r.in.replace = 0;
75 /* shoot! */
76 status = dcerpc_epm_Insert_r(h, tctx, &r);
78 if (NT_STATUS_IS_ERR(status)) {
79 torture_comment(tctx,
80 "epm_Insert failed - %s\n",
81 nt_errstr(status));
82 return false;
85 if (r.out.result != EPMAPPER_STATUS_OK) {
86 torture_comment(tctx,
87 "epm_Insert failed - internal error: 0x%.4x\n",
88 r.out.result);
89 return false;
92 return true;
95 static bool test_Delete(struct torture_context *tctx,
96 struct dcerpc_binding_handle *h,
97 const char *annotation,
98 const struct dcerpc_binding *b)
100 NTSTATUS status;
101 struct epm_Delete r;
103 r.in.num_ents = 1;
104 r.in.entries = talloc_array(tctx, struct epm_entry_t, 1);
106 ZERO_STRUCT(r.in.entries[0].object);
107 r.in.entries[0].annotation = annotation;
109 r.in.entries[0].tower = talloc(tctx, struct epm_twr_t);
111 status = dcerpc_binding_build_tower(tctx,
113 &r.in.entries[0].tower->tower);
115 torture_assert_ntstatus_ok(tctx,
116 status,
117 "Unable to build tower from binding struct");
118 r.in.num_ents = 1;
120 status = dcerpc_epm_Delete_r(h, tctx, &r);
121 if (NT_STATUS_IS_ERR(status)) {
122 torture_comment(tctx,
123 "epm_Delete failed - %s\n",
124 nt_errstr(status));
125 return false;
128 if (r.out.result != EPMAPPER_STATUS_OK) {
129 torture_comment(tctx,
130 "epm_Delete failed - internal error: 0x%.4x\n",
131 r.out.result);
132 return false;
135 return true;
138 static bool test_Map_tcpip(struct torture_context *tctx,
139 struct dcerpc_binding_handle *h,
140 struct ndr_syntax_id map_syntax)
142 struct epm_Map r;
143 struct GUID uuid;
144 struct policy_handle entry_handle;
145 struct ndr_syntax_id syntax;
146 struct dcerpc_binding *map_binding;
147 struct epm_twr_t map_tower;
148 struct epm_twr_p_t towers[20];
149 struct epm_tower t;
150 uint32_t num_towers;
151 uint32_t port;
152 uint32_t i;
153 long int p;
154 const char *tmp;
155 const char *ip;
156 char *ptr;
157 NTSTATUS status;
159 torture_comment(tctx, "Testing epm_Map\n");
161 ZERO_STRUCT(uuid);
162 ZERO_STRUCT(entry_handle);
164 r.in.object = &uuid;
165 r.in.map_tower = &map_tower;
166 r.in.entry_handle = &entry_handle;
167 r.out.entry_handle = &entry_handle;
168 r.in.max_towers = 10;
169 r.out.towers = towers;
170 r.out.num_towers = &num_towers;
172 /* Create map tower */
173 status = dcerpc_parse_binding(tctx, "ncacn_ip_tcp:[135]", &map_binding);
174 torture_assert_ntstatus_ok(tctx, status,
175 "epm_Map_tcpip failed: can't create map_binding");
177 status = dcerpc_binding_set_abstract_syntax(map_binding, &map_syntax);
178 torture_assert_ntstatus_ok(tctx, status,
179 "epm_Map_tcpip failed: set map_syntax");
181 status = dcerpc_binding_build_tower(tctx, map_binding,
182 &map_tower.tower);
183 torture_assert_ntstatus_ok(tctx, status,
184 "epm_Map_tcpip failed: can't create map_tower");
186 torture_comment(tctx,
187 "epm_Map request for '%s':\n",
188 ndr_interface_name(&map_syntax.uuid, map_syntax.if_version));
189 display_tower(tctx, &r.in.map_tower->tower);
191 status = dcerpc_epm_Map_r(h, tctx, &r);
193 torture_assert_ntstatus_ok(tctx, status, "epm_Map_simple failed");
194 torture_assert(tctx, r.out.result == EPMAPPER_STATUS_OK,
195 "epm_Map_tcpip failed: result is not EPMAPPER_STATUS_OK");
197 /* Check the result */
198 t = r.out.towers[0].twr->tower;
200 /* Check if we got the correct RPC interface identifier */
201 dcerpc_floor_get_lhs_data(&t.floors[0], &syntax);
202 torture_assert(tctx, ndr_syntax_id_equal(&syntax, &map_syntax),
203 "epm_Map_tcpip failed: Interface identifier mismatch");
205 torture_comment(tctx,
206 "epm_Map_tcpip response for '%s':\n",
207 ndr_interface_name(&syntax.uuid, syntax.if_version));
209 dcerpc_floor_get_lhs_data(&t.floors[1], &syntax);
210 torture_assert(tctx, ndr_syntax_id_equal(&syntax, &ndr_transfer_syntax_ndr),
211 "epm_Map_tcpip failed: floor 2 is not NDR encoded");
213 torture_assert(tctx, t.floors[2].lhs.protocol == EPM_PROTOCOL_NCACN,
214 "epm_Map_tcpip failed: floor 3 is not NCACN_IP_TCP");
216 tmp = dcerpc_floor_get_rhs_data(tctx, &t.floors[3]);
217 p = strtol(tmp, &ptr, 10);
218 port = p & 0xffff;
219 torture_assert(tctx, port > 1024 && port < 65535, "epm_Map_tcpip failed");
221 ip = dcerpc_floor_get_rhs_data(tctx, &t.floors[4]);
222 torture_assert(tctx, is_ipaddress(ip), "epm_Map_tcpip failed");
224 for (i = 0; i < *r.out.num_towers; i++) {
225 if (r.out.towers[i].twr) {
226 display_tower(tctx, &t);
230 return true;
233 static bool test_Map_full(struct torture_context *tctx,
234 struct dcerpc_pipe *p)
236 const struct ndr_syntax_id obj = {
237 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
240 struct dcerpc_binding_handle *h = p->binding_handle;
241 const char *annotation = "SMBTORTURE";
242 struct dcerpc_binding *b;
243 NTSTATUS status;
244 bool ok;
246 status = dcerpc_parse_binding(tctx, "ncacn_ip_tcp:216.83.154.106[41768]", &b);
247 torture_assert_ntstatus_ok(tctx,
248 status,
249 "Unable to generate dcerpc_binding struct");
250 status = dcerpc_binding_set_abstract_syntax(b, &obj);
251 torture_assert_ntstatus_ok(tctx, status, "dcerpc_binding_set_abstract_syntax");
253 ok = test_Insert(tctx, h, obj, annotation, b);
254 if (!ok) {
255 return false;
258 ok = test_Map_tcpip(tctx, h, obj);
259 if (!ok) {
260 return false;
263 ok = test_Delete(tctx, h, annotation, b);
264 if (!ok) {
265 return false;
268 return true;
271 static bool test_Map_display(struct dcerpc_binding_handle *b,
272 struct torture_context *tctx,
273 struct epm_entry_t *entry)
276 NTSTATUS status;
277 struct epm_twr_t *twr = entry->tower;
278 struct epm_Map r;
279 struct GUID uuid = entry->object;
280 struct policy_handle handle;
281 struct ndr_syntax_id syntax;
282 uint32_t num_towers;
283 uint32_t i;
285 ZERO_STRUCT(handle);
287 r.in.object = &uuid;
288 r.in.map_tower = twr;
289 r.in.entry_handle = &handle;
290 r.out.entry_handle = &handle;
291 r.in.max_towers = 10;
292 r.out.num_towers = &num_towers;
294 dcerpc_floor_get_lhs_data(&twr->tower.floors[0], &syntax);
296 torture_comment(tctx,
297 "epm_Map results for '%s':\n",
298 ndr_interface_name(&syntax.uuid, syntax.if_version));
300 status = dcerpc_epm_Map_r(b, tctx, &r);
301 if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
302 for (i=0;i<*r.out.num_towers;i++) {
303 if (r.out.towers[i].twr) {
304 display_tower(tctx, &r.out.towers[i].twr->tower);
309 /* RPC protocol identifier */
310 twr->tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
311 twr->tower.floors[2].lhs.lhs_data = data_blob(NULL, 0);
312 twr->tower.floors[2].rhs.ncacn.minor_version = 0;
314 /* Port address */
315 twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
316 twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
317 twr->tower.floors[3].rhs.tcp.port = 0;
319 /* Transport */
320 twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
321 twr->tower.floors[4].lhs.lhs_data = data_blob(NULL, 0);
322 twr->tower.floors[4].rhs.ip.ipaddr = "0.0.0.0";
324 status = dcerpc_epm_Map_r(b, tctx, &r);
325 if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
326 for (i=0;i<*r.out.num_towers;i++) {
327 if (r.out.towers[i].twr) {
328 display_tower(tctx, &r.out.towers[i].twr->tower);
333 twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_HTTP;
334 twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
335 twr->tower.floors[3].rhs.http.port = 0;
337 status = dcerpc_epm_Map_r(b, tctx, &r);
338 if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
339 for (i=0;i<*r.out.num_towers;i++) {
340 if (r.out.towers[i].twr) {
341 display_tower(tctx, &r.out.towers[i].twr->tower);
346 twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_UDP;
347 twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
348 twr->tower.floors[3].rhs.udp.port = 0;
350 status = dcerpc_epm_Map_r(b, tctx, &r);
351 if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
352 for (i=0;i<*r.out.num_towers;i++) {
353 if (r.out.towers[i].twr) {
354 display_tower(tctx, &r.out.towers[i].twr->tower);
359 twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_SMB;
360 twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
361 twr->tower.floors[3].rhs.smb.unc = "";
363 twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_NETBIOS;
364 twr->tower.floors[4].lhs.lhs_data = data_blob(NULL, 0);
365 twr->tower.floors[4].rhs.netbios.name = "";
367 status = dcerpc_epm_Map_r(b, tctx, &r);
368 if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
369 for (i = 0; i < *r.out.num_towers; i++) {
370 if (r.out.towers[i].twr) {
371 display_tower(tctx, &r.out.towers[i].twr->tower);
376 /* FIXME: Extend to do other protocols as well (ncacn_unix_stream, ncalrpc) */
378 return true;
381 static bool test_Map_simple(struct torture_context *tctx,
382 struct dcerpc_pipe *p)
384 NTSTATUS status;
385 struct epm_Lookup r;
386 struct policy_handle entry_handle;
387 uint32_t num_ents = 0;
388 struct dcerpc_binding_handle *h = p->binding_handle;
390 ZERO_STRUCT(entry_handle);
392 torture_comment(tctx, "Testing epm_Map\n");
394 /* get all elements */
395 r.in.inquiry_type = RPC_C_EP_ALL_ELTS;
396 r.in.object = NULL;
397 r.in.interface_id = NULL;
398 r.in.vers_option = RPC_C_VERS_ALL;
400 r.in.entry_handle = &entry_handle;
401 r.in.max_ents = 10;
403 r.out.entry_handle = &entry_handle;
404 r.out.num_ents = &num_ents;
406 do {
407 int i;
409 status = dcerpc_epm_Lookup_r(h, tctx, &r);
410 if (!NT_STATUS_IS_OK(status) ||
411 r.out.result != EPMAPPER_STATUS_OK) {
412 break;
415 for (i = 0; i < *r.out.num_ents; i++) {
416 if (r.out.entries[i].tower->tower.num_floors == 5) {
417 test_Map_display(h, tctx, &r.out.entries[i]);
420 } while (NT_STATUS_IS_OK(status) &&
421 r.out.result == EPMAPPER_STATUS_OK &&
422 *r.out.num_ents == r.in.max_ents &&
423 !ndr_policy_handle_empty(&entry_handle));
425 torture_assert_ntstatus_ok(tctx, status, "epm_Map_simple failed");
427 torture_assert(tctx,
428 ndr_policy_handle_empty(&entry_handle),
429 "epm_Map_simple failed - The policy handle should be emtpy.");
431 return true;
434 static bool test_LookupHandleFree(struct torture_context *tctx,
435 struct dcerpc_binding_handle *h,
436 struct policy_handle *entry_handle) {
437 NTSTATUS status;
438 struct epm_LookupHandleFree r;
440 if (torture_setting_bool(tctx, "samba4", false)) {
441 torture_skip(tctx, "Skip Insert test against Samba4");
444 if (ndr_policy_handle_empty(entry_handle)) {
445 torture_comment(tctx,
446 "epm_LookupHandleFree failed - empty policy_handle\n");
447 return false;
450 r.in.entry_handle = entry_handle;
451 r.out.entry_handle = entry_handle;
453 status = dcerpc_epm_LookupHandleFree_r(h, tctx, &r);
454 if (NT_STATUS_IS_ERR(status)) {
455 torture_comment(tctx,
456 "epm_LookupHandleFree failed - %s\n",
457 nt_errstr(status));
458 return false;
461 if (r.out.result != EPMAPPER_STATUS_OK) {
462 torture_comment(tctx,
463 "epm_LookupHandleFree failed - internal error: "
464 "0x%.4x\n",
465 r.out.result);
466 return false;
469 return true;
472 static bool test_Lookup_simple(struct torture_context *tctx,
473 struct dcerpc_pipe *p)
475 NTSTATUS status;
476 struct epm_Lookup r;
477 struct policy_handle entry_handle;
478 uint32_t num_ents = 0;
479 struct dcerpc_binding_handle *h = p->binding_handle;
481 ZERO_STRUCT(entry_handle);
483 torture_comment(tctx, "Testing epm_Lookup\n");
485 /* get all elements */
486 r.in.inquiry_type = RPC_C_EP_ALL_ELTS;
487 r.in.object = NULL;
488 r.in.interface_id = NULL;
489 r.in.vers_option = RPC_C_VERS_ALL;
491 r.in.entry_handle = &entry_handle;
492 r.in.max_ents = 10;
494 r.out.entry_handle = &entry_handle;
495 r.out.num_ents = &num_ents;
497 do {
498 int i;
500 status = dcerpc_epm_Lookup_r(h, tctx, &r);
501 if (!NT_STATUS_IS_OK(status) ||
502 r.out.result != EPMAPPER_STATUS_OK) {
503 break;
506 torture_comment(tctx,
507 "epm_Lookup returned %d events, entry_handle: %s\n",
508 *r.out.num_ents,
509 GUID_string(tctx, &entry_handle.uuid));
511 for (i = 0; i < *r.out.num_ents; i++) {
512 torture_comment(tctx,
513 "\n Found '%s' Object[%s]\n",
514 r.out.entries[i].annotation,
515 GUID_string(tctx, &r.out.entries[i].object));
517 display_tower(tctx, &r.out.entries[i].tower->tower);
519 } while (NT_STATUS_IS_OK(status) &&
520 r.out.result == EPMAPPER_STATUS_OK &&
521 *r.out.num_ents == r.in.max_ents &&
522 !ndr_policy_handle_empty(&entry_handle));
524 torture_assert_ntstatus_ok(tctx, status, "epm_Lookup failed");
525 torture_assert(tctx, r.out.result == EPMAPPER_STATUS_NO_MORE_ENTRIES, "epm_Lookup failed");
527 torture_assert(tctx,
528 ndr_policy_handle_empty(&entry_handle),
529 "epm_Lookup failed - The policy handle should be emtpy.");
531 return true;
535 * This test starts a epm_Lookup request, but doesn't finish the
536 * call terminates the search. So it will call epm_LookupHandleFree.
538 static bool test_Lookup_terminate_search(struct torture_context *tctx,
539 struct dcerpc_pipe *p)
541 bool ok;
542 NTSTATUS status;
543 struct epm_Lookup r;
544 struct policy_handle entry_handle;
545 uint32_t i, num_ents = 0;
546 struct dcerpc_binding_handle *h = p->binding_handle;
548 ZERO_STRUCT(entry_handle);
550 torture_comment(tctx, "Testing epm_Lookup and epm_LookupHandleFree\n");
552 /* get all elements */
553 r.in.inquiry_type = RPC_C_EP_ALL_ELTS;
554 r.in.object = NULL;
555 r.in.interface_id = NULL;
556 r.in.vers_option = RPC_C_VERS_ALL;
558 r.in.entry_handle = &entry_handle;
559 r.in.max_ents = 2;
561 r.out.entry_handle = &entry_handle;
562 r.out.num_ents = &num_ents;
564 status = dcerpc_epm_Lookup_r(h, tctx, &r);
566 torture_assert_ntstatus_ok(tctx, status, "epm_Lookup failed");
567 torture_assert(tctx, r.out.result == EPMAPPER_STATUS_OK, "epm_Lookup failed");
569 torture_comment(tctx,
570 "epm_Lookup returned %d events, entry_handle: %s\n",
571 *r.out.num_ents,
572 GUID_string(tctx, &entry_handle.uuid));
574 for (i = 0; i < *r.out.num_ents; i++) {
575 torture_comment(tctx,
576 "\n Found '%s'\n",
577 r.out.entries[i].annotation);
580 ok = test_LookupHandleFree(tctx,
582 &entry_handle);
583 if (!ok) {
584 return false;
587 return true;
590 static bool test_Insert_noreplace(struct torture_context *tctx,
591 struct dcerpc_pipe *p)
593 bool ok;
594 NTSTATUS status;
595 struct epm_Insert r;
596 struct dcerpc_binding *b;
597 struct dcerpc_binding_handle *h = p->binding_handle;
599 torture_comment(tctx, "Testing epm_Insert(noreplace) and epm_Delete\n");
601 if (torture_setting_bool(tctx, "samba4", false)) {
602 torture_skip(tctx, "Skip Insert test against Samba4");
605 r.in.num_ents = 1;
606 r.in.entries = talloc_array(tctx, struct epm_entry_t, 1);
608 ZERO_STRUCT(r.in.entries[0].object);
609 r.in.entries[0].annotation = "smbtorture endpoint";
611 status = dcerpc_parse_binding(tctx, "ncalrpc:[SMBTORTURE]", &b);
612 torture_assert_ntstatus_ok(tctx,
613 status,
614 "Unable to generate dcerpc_binding struct");
616 r.in.entries[0].tower = talloc(tctx, struct epm_twr_t);
618 status = dcerpc_binding_build_tower(tctx,
620 &r.in.entries[0].tower->tower);
621 torture_assert_ntstatus_ok(tctx,
622 status,
623 "Unable to build tower from binding struct");
624 r.in.replace = 0;
626 status = dcerpc_epm_Insert_r(h, tctx, &r);
627 torture_assert_ntstatus_ok(tctx, status, "epm_Insert failed");
629 torture_assert(tctx, r.out.result == 0, "epm_Insert failed");
631 ok = test_Delete(tctx, h, "smbtorture", b);
632 if (!ok) {
633 return false;
636 return true;
639 #if 0
641 * The MS-RPCE documentation states that this function isn't implemented and
642 * SHOULD NOT be called by a client.
644 static bool test_InqObject(struct torture_context *tctx, struct dcerpc_pipe *p)
646 NTSTATUS status;
647 struct epm_InqObject r;
648 struct dcerpc_binding_handle *b = p->binding_handle;
650 r.in.epm_object = talloc(tctx, struct GUID);
651 *r.in.epm_object = ndr_table_epmapper.syntax_id.uuid;
653 status = dcerpc_epm_InqObject_r(b, tctx, &r);
654 torture_assert_ntstatus_ok(tctx, status, "InqObject failed");
656 return true;
658 #endif
660 struct torture_suite *torture_rpc_epmapper(TALLOC_CTX *mem_ctx)
662 struct torture_suite *suite = torture_suite_create(mem_ctx, "epmapper");
663 struct torture_rpc_tcase *tcase;
665 tcase = torture_suite_add_rpc_iface_tcase(suite,
666 "epmapper",
667 &ndr_table_epmapper);
669 /* This is a stack */
670 torture_rpc_tcase_add_test(tcase,
671 "Map_simple",
672 test_Map_simple);
673 torture_rpc_tcase_add_test(tcase,
674 "Map_full",
675 test_Map_full);
676 torture_rpc_tcase_add_test(tcase,
677 "Lookup_simple",
678 test_Lookup_simple);
679 torture_rpc_tcase_add_test(tcase,
680 "Lookup_terminate_search",
681 test_Lookup_terminate_search);
682 torture_rpc_tcase_add_test(tcase,
683 "Insert_noreplace",
684 test_Insert_noreplace);
686 return suite;
689 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */