s4:torture "cldap" test - rework and test more results
[Samba/ekacnet.git] / source4 / torture / ldap / cldap.c
bloba20f0a3624d1e63587b6f2ba30851ba663b384d9
1 /*
2 Unix SMB/CIFS mplementation.
4 test CLDAP operations
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Matthias Dieter Wallnöfer 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "libcli/cldap/cldap.h"
26 #include "libcli/ldap/ldap.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "torture/torture.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "param/param.h"
32 #include "../lib/tsocket/tsocket.h"
34 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
36 #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
38 #define CHECK_STRING(v, correct) torture_assert_str_equal(tctx, v, correct, "incorrect value");
40 test netlogon operations
42 static bool test_cldap_netlogon(struct torture_context *tctx, const char *dest)
44 struct cldap_socket *cldap;
45 NTSTATUS status;
46 struct cldap_netlogon search, empty_search;
47 struct netlogon_samlogon_response n1;
48 struct GUID guid;
49 int i;
50 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
51 struct tsocket_address *dest_addr;
52 int ret;
54 ret = tsocket_address_inet_from_strings(tctx, "ip",
55 dest,
56 lp_cldap_port(tctx->lp_ctx),
57 &dest_addr);
58 CHECK_VAL(ret, 0);
60 status = cldap_socket_init(tctx, NULL, NULL, dest_addr, &cldap);
61 CHECK_STATUS(status, NT_STATUS_OK);
63 ZERO_STRUCT(search);
64 search.in.dest_address = NULL;
65 search.in.dest_port = 0;
66 search.in.acct_control = -1;
67 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
68 search.in.map_response = true;
70 empty_search = search;
72 printf("Trying without any attributes\n");
73 search = empty_search;
74 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
75 CHECK_STATUS(status, NT_STATUS_OK);
77 n1 = search.out.netlogon;
79 search.in.user = "Administrator";
80 search.in.realm = n1.data.nt5_ex.dns_domain;
81 search.in.host = "__cldap_torture__";
83 printf("Scanning for netlogon levels\n");
84 for (i=0;i<256;i++) {
85 search.in.version = i;
86 printf("Trying netlogon level %d\n", i);
87 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
88 CHECK_STATUS(status, NT_STATUS_OK);
91 printf("Scanning for netlogon level bits\n");
92 for (i=0;i<31;i++) {
93 search.in.version = (1<<i);
94 printf("Trying netlogon level 0x%x\n", i);
95 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
96 CHECK_STATUS(status, NT_STATUS_OK);
99 search.in.version = NETLOGON_NT_VERSION_5|NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_IP;
100 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
101 CHECK_STATUS(status, NT_STATUS_OK);
103 printf("Trying with User=NULL\n");
104 search.in.user = NULL;
105 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
106 CHECK_STATUS(status, NT_STATUS_OK);
107 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
108 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
110 printf("Trying with User=Administrator\n");
111 search.in.user = "Administrator";
112 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
113 CHECK_STATUS(status, NT_STATUS_OK);
114 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
115 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
117 search.in.version = NETLOGON_NT_VERSION_5;
118 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
119 CHECK_STATUS(status, NT_STATUS_OK);
121 printf("Trying with User=NULL\n");
122 search.in.user = NULL;
123 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
124 CHECK_STATUS(status, NT_STATUS_OK);
125 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE);
126 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
128 printf("Trying with User=Administrator\n");
129 search.in.user = "Administrator";
130 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
131 CHECK_STATUS(status, NT_STATUS_OK);
132 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN);
133 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
135 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
137 printf("Trying with a GUID\n");
138 search.in.realm = NULL;
139 search.in.domain_guid = GUID_string(tctx, &n1.data.nt5_ex.domain_uuid);
140 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
141 CHECK_STATUS(status, NT_STATUS_OK);
142 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
143 CHECK_STRING(GUID_string(tctx, &search.out.netlogon.data.nt5_ex.domain_uuid), search.in.domain_guid);
145 printf("Trying with a incorrect GUID\n");
146 guid = GUID_random();
147 search.in.user = NULL;
148 search.in.domain_guid = GUID_string(tctx, &guid);
149 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
150 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
152 printf("Trying with a AAC\n");
153 search.in.acct_control = ACB_WSTRUST|ACB_SVRTRUST;
154 search.in.realm = n1.data.nt5_ex.dns_domain;
155 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
156 CHECK_STATUS(status, NT_STATUS_OK);
157 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
158 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
160 printf("Trying with a zero AAC\n");
161 search.in.acct_control = 0x0;
162 search.in.realm = n1.data.nt5_ex.dns_domain;
163 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
164 CHECK_STATUS(status, NT_STATUS_OK);
165 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
166 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
168 printf("Trying with a zero AAC and user=Administrator\n");
169 search.in.acct_control = 0x0;
170 search.in.user = "Administrator";
171 search.in.realm = n1.data.nt5_ex.dns_domain;
172 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
173 CHECK_STATUS(status, NT_STATUS_OK);
174 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
175 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "Administrator");
177 printf("Trying with a bad AAC\n");
178 search.in.user = NULL;
179 search.in.acct_control = 0xFF00FF00;
180 search.in.realm = n1.data.nt5_ex.dns_domain;
181 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
182 CHECK_STATUS(status, NT_STATUS_OK);
183 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
184 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
186 printf("Trying with a user only\n");
187 search = empty_search;
188 search.in.user = "Administrator";
189 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
190 CHECK_STATUS(status, NT_STATUS_OK);
191 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
193 printf("Trying with just a bad username\n");
194 search.in.user = "___no_such_user___";
195 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
196 CHECK_STATUS(status, NT_STATUS_OK);
197 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
198 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
200 printf("Trying with just a bad domain\n");
201 search = empty_search;
202 search.in.realm = "___no_such_domain___";
203 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
204 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
206 printf("Trying with a incorrect domain and correct guid\n");
207 search.in.domain_guid = GUID_string(tctx, &n1.data.nt5_ex.domain_uuid);
208 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
209 CHECK_STATUS(status, NT_STATUS_OK);
210 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
211 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
213 printf("Trying with a incorrect domain and incorrect guid\n");
214 search.in.domain_guid = GUID_string(tctx, &guid);
215 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
216 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
217 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
218 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
220 printf("Trying with a incorrect GUID and correct domain\n");
221 search.in.domain_guid = GUID_string(tctx, &guid);
222 search.in.realm = n1.data.nt5_ex.dns_domain;
223 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
224 CHECK_STATUS(status, NT_STATUS_OK);
225 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
226 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
228 printf("Proof other results\n");
229 search.in.user = "Administrator";
230 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
231 CHECK_STATUS(status, NT_STATUS_OK);
232 CHECK_STRING(search.out.netlogon.data.nt5_ex.forest, n1.data.nt5_ex.dns_domain);
233 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
234 CHECK_STRING(search.out.netlogon.data.nt5_ex.domain, n1.data.nt5_ex.domain);
235 CHECK_STRING(search.out.netlogon.data.nt5_ex.pdc_name, n1.data.nt5_ex.pdc_name);
236 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
237 CHECK_STRING(search.out.netlogon.data.nt5_ex.server_site, n1.data.nt5_ex.server_site);
238 CHECK_STRING(search.out.netlogon.data.nt5_ex.client_site, n1.data.nt5_ex.client_site);
240 return true;
244 test cldap netlogon server type flags
246 static bool test_cldap_netlogon_flags(struct torture_context *tctx,
247 const char *dest)
249 struct cldap_socket *cldap;
250 NTSTATUS status;
251 struct cldap_netlogon search;
252 struct netlogon_samlogon_response n1;
253 uint32_t server_type;
254 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
256 /* cldap_socket_init should now know about the dest. address */
257 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
258 CHECK_STATUS(status, NT_STATUS_OK);
260 printf("Printing out netlogon server type flags: %s\n", dest);
262 ZERO_STRUCT(search);
263 search.in.dest_address = dest;
264 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
265 search.in.acct_control = -1;
266 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
267 search.in.map_response = true;
269 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
270 CHECK_STATUS(status, NT_STATUS_OK);
272 n1 = search.out.netlogon;
273 if (n1.ntver == NETLOGON_NT_VERSION_5)
274 server_type = n1.data.nt5.server_type;
275 else if (n1.ntver == NETLOGON_NT_VERSION_5EX)
276 server_type = n1.data.nt5_ex.server_type;
278 printf("The word is: %i\n", server_type);
279 if (server_type & NBT_SERVER_PDC)
280 printf("NBT_SERVER_PDC ");
281 if (server_type & NBT_SERVER_GC)
282 printf("NBT_SERVER_GC ");
283 if (server_type & NBT_SERVER_LDAP)
284 printf("NBT_SERVER_LDAP ");
285 if (server_type & NBT_SERVER_DS)
286 printf("NBT_SERVER_DS ");
287 if (server_type & NBT_SERVER_KDC)
288 printf("NBT_SERVER_KDC ");
289 if (server_type & NBT_SERVER_TIMESERV)
290 printf("NBT_SERVER_TIMESERV ");
291 if (server_type & NBT_SERVER_CLOSEST)
292 printf("NBT_SERVER_CLOSEST ");
293 if (server_type & NBT_SERVER_WRITABLE)
294 printf("NBT_SERVER_WRITABLE ");
295 if (server_type & NBT_SERVER_GOOD_TIMESERV)
296 printf("NBT_SERVER_GOOD_TIMESERV ");
297 if (server_type & NBT_SERVER_NDNC)
298 printf("NBT_SERVER_NDNC ");
299 if (server_type & NBT_SERVER_SELECT_SECRET_DOMAIN_6)
300 printf("NBT_SERVER_SELECT_SECRET_DOMAIN_6");
301 if (server_type & NBT_SERVER_FULL_SECRET_DOMAIN_6)
302 printf("NBT_SERVER_FULL_SECRET_DOMAIN_6");
303 if (server_type & DS_DNS_CONTROLLER)
304 printf("DS_DNS_CONTROLLER ");
305 if (server_type & DS_DNS_DOMAIN)
306 printf("DS_DNS_DOMAIN ");
307 if (server_type & DS_DNS_FOREST_ROOT)
308 printf("DS_DNS_FOREST_ROOT ");
310 printf("\n");
312 return true;
316 convert a ldap result message to a ldb message. This allows us to
317 use the convenient ldif dump routines in ldb to print out cldap
318 search results
320 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
322 struct ldb_message *msg;
324 msg = ldb_msg_new(mem_ctx);
325 msg->dn = ldb_dn_new(msg, ldb, res->dn);
326 msg->num_elements = res->num_attributes;
327 msg->elements = talloc_steal(msg, res->attributes);
328 return msg;
332 dump a set of cldap results
334 static void cldap_dump_results(struct cldap_search *search)
336 struct ldb_ldif ldif;
337 struct ldb_context *ldb;
339 if (!search || !(search->out.response)) {
340 return;
343 /* we need a ldb context to use ldb_ldif_write_file() */
344 ldb = ldb_init(NULL, NULL);
346 ZERO_STRUCT(ldif);
347 ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
349 ldb_ldif_write_file(ldb, stdout, &ldif);
351 talloc_free(ldb);
356 test cldap netlogon server type flag "NBT_SERVER_FOREST_ROOT"
358 static bool test_cldap_netlogon_flag_ds_dns_forest(struct torture_context *tctx,
359 const char *dest)
361 struct cldap_socket *cldap;
362 NTSTATUS status;
363 struct cldap_netlogon search;
364 uint32_t server_type;
365 struct netlogon_samlogon_response n1;
366 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
367 bool result = true;
369 /* cldap_socket_init should now know about the dest. address */
370 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
371 CHECK_STATUS(status, NT_STATUS_OK);
373 printf("Testing netlogon server type flag NBT_SERVER_FOREST_ROOT: ");
375 ZERO_STRUCT(search);
376 search.in.dest_address = dest;
377 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
378 search.in.acct_control = -1;
379 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
380 search.in.map_response = true;
382 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
383 CHECK_STATUS(status, NT_STATUS_OK);
385 n1 = search.out.netlogon;
386 if (n1.ntver == NETLOGON_NT_VERSION_5)
387 server_type = n1.data.nt5.server_type;
388 else if (n1.ntver == NETLOGON_NT_VERSION_5EX)
389 server_type = n1.data.nt5_ex.server_type;
391 if (server_type & DS_DNS_FOREST_ROOT) {
392 struct cldap_search search2;
393 const char *attrs[] = { "defaultNamingContext", "rootDomainNamingContext",
394 NULL };
395 struct ldb_context *ldb;
396 struct ldb_message *msg;
398 /* Trying to fetch the attributes "defaultNamingContext" and
399 "rootDomainNamingContext" */
400 ZERO_STRUCT(search2);
401 search2.in.dest_address = dest;
402 search2.in.dest_port = lp_cldap_port(tctx->lp_ctx);
403 search2.in.timeout = 10;
404 search2.in.retries = 3;
405 search2.in.filter = "(objectclass=*)";
406 search2.in.attributes = attrs;
408 status = cldap_search(cldap, tctx, &search2);
409 CHECK_STATUS(status, NT_STATUS_OK);
411 ldb = ldb_init(NULL, NULL);
413 msg = ldap_msg_to_ldb(ldb, ldb, search2.out.response);
415 /* Try to compare the two attributes */
416 if (ldb_msg_element_compare(ldb_msg_find_element(msg, attrs[0]),
417 ldb_msg_find_element(msg, attrs[1])))
418 result = false;
420 talloc_free(ldb);
423 if (result)
424 printf("passed\n");
425 else
426 printf("failed\n");
428 return result;
432 test generic cldap operations
434 static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
436 struct cldap_socket *cldap;
437 NTSTATUS status;
438 struct cldap_search search;
439 const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
440 const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
441 const char *attrs3[] = { "netlogon", NULL };
443 /* cldap_socket_init should now know about the dest. address */
444 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
445 CHECK_STATUS(status, NT_STATUS_OK);
447 ZERO_STRUCT(search);
448 search.in.dest_address = dest;
449 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
450 search.in.timeout = 10;
451 search.in.retries = 3;
453 status = cldap_search(cldap, tctx, &search);
454 CHECK_STATUS(status, NT_STATUS_OK);
456 printf("fetching whole rootDSE\n");
457 search.in.filter = "(objectclass=*)";
458 search.in.attributes = NULL;
460 status = cldap_search(cldap, tctx, &search);
461 CHECK_STATUS(status, NT_STATUS_OK);
463 if (DEBUGLVL(3)) cldap_dump_results(&search);
465 printf("fetching currentTime and USN\n");
466 search.in.filter = "(objectclass=*)";
467 search.in.attributes = attrs1;
469 status = cldap_search(cldap, tctx, &search);
470 CHECK_STATUS(status, NT_STATUS_OK);
472 if (DEBUGLVL(3)) cldap_dump_results(&search);
474 printf("Testing currentTime, USN and netlogon\n");
475 search.in.filter = "(objectclass=*)";
476 search.in.attributes = attrs2;
478 status = cldap_search(cldap, tctx, &search);
479 CHECK_STATUS(status, NT_STATUS_OK);
481 if (DEBUGLVL(3)) cldap_dump_results(&search);
483 printf("Testing objectClass=* and netlogon\n");
484 search.in.filter = "(objectclass2=*)";
485 search.in.attributes = attrs3;
487 status = cldap_search(cldap, tctx, &search);
488 CHECK_STATUS(status, NT_STATUS_OK);
490 if (DEBUGLVL(3)) cldap_dump_results(&search);
492 printf("Testing a false expression\n");
493 search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
494 search.in.attributes = attrs1;
496 status = cldap_search(cldap, tctx, &search);
497 CHECK_STATUS(status, NT_STATUS_OK);
499 if (DEBUGLVL(3)) cldap_dump_results(&search);
501 return true;
504 bool torture_cldap(struct torture_context *torture)
506 bool ret = true;
507 const char *host = torture_setting_string(torture, "host", NULL);
509 ret &= test_cldap_netlogon(torture, host);
510 ret &= test_cldap_netlogon_flags(torture, host);
511 ret &= test_cldap_netlogon_flag_ds_dns_forest(torture, host);
512 ret &= test_cldap_generic(torture, host);
514 return ret;