2 Unix SMB/CIFS mplementation.
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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "libcli/cldap/cldap.h"
25 #include "libcli/ldap/ldap.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "librpc/gen_ndr/netlogon.h"
28 #include "torture/torture.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "param/param.h"
31 #include "../lib/tsocket/tsocket.h"
33 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
35 #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
37 #define CHECK_STRING(v, correct) torture_assert_str_equal(tctx, v, correct, "incorrect value");
39 test netlogon operations
41 static bool test_cldap_netlogon(struct torture_context *tctx, const char *dest)
43 struct cldap_socket *cldap;
45 struct cldap_netlogon search, empty_search;
46 struct netlogon_samlogon_response n1;
49 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
50 struct tsocket_address *dest_addr;
53 ret = tsocket_address_inet_from_strings(tctx, "ip",
55 lp_cldap_port(tctx->lp_ctx),
59 status = cldap_socket_init(tctx, NULL, NULL, dest_addr, &cldap);
60 CHECK_STATUS(status, NT_STATUS_OK);
63 search.in.dest_address = NULL;
64 search.in.dest_port = 0;
65 search.in.acct_control = -1;
66 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
67 search.in.map_response = true;
69 empty_search = search;
71 printf("Trying without any attributes\n");
72 search = empty_search;
73 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
74 CHECK_STATUS(status, NT_STATUS_OK);
76 n1 = search.out.netlogon;
78 search.in.user = "Administrator";
79 search.in.realm = n1.data.nt5_ex.dns_domain;
80 search.in.host = "__cldap_torture__";
82 printf("Scanning for netlogon levels\n");
84 search.in.version = i;
85 printf("Trying netlogon level %d\n", i);
86 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
87 CHECK_STATUS(status, NT_STATUS_OK);
90 printf("Scanning for netlogon level bits\n");
92 search.in.version = (1<<i);
93 printf("Trying netlogon level 0x%x\n", i);
94 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
95 CHECK_STATUS(status, NT_STATUS_OK);
98 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");
105 search.in.user = NULL;
106 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
107 CHECK_STATUS(status, NT_STATUS_OK);
108 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
109 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
111 printf("Trying with User=Administrator\n");
113 search.in.user = "Administrator";
114 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
115 CHECK_STATUS(status, NT_STATUS_OK);
117 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
118 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
120 search.in.version = NETLOGON_NT_VERSION_5;
121 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
122 CHECK_STATUS(status, NT_STATUS_OK);
124 printf("Trying with User=NULL\n");
126 search.in.user = NULL;
127 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
128 CHECK_STATUS(status, NT_STATUS_OK);
129 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
130 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE);
132 printf("Trying with User=Administrator\n");
134 search.in.user = "Administrator";
135 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
136 CHECK_STATUS(status, NT_STATUS_OK);
138 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
139 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN);
141 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
143 printf("Trying with a GUID\n");
144 search.in.realm = NULL;
145 search.in.domain_guid = GUID_string(tctx, &n1.data.nt5_ex.domain_uuid);
146 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
147 CHECK_STATUS(status, NT_STATUS_OK);
148 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
149 CHECK_STRING(GUID_string(tctx, &search.out.netlogon.data.nt5_ex.domain_uuid), search.in.domain_guid);
151 printf("Trying with a incorrect GUID\n");
152 guid = GUID_random();
153 search.in.user = NULL;
154 search.in.domain_guid = GUID_string(tctx, &guid);
155 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
156 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
158 printf("Trying with a AAC\n");
159 search.in.acct_control = ACB_WSTRUST|ACB_SVRTRUST;
160 search.in.realm = n1.data.nt5_ex.dns_domain;
161 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
162 CHECK_STATUS(status, NT_STATUS_OK);
163 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
164 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
166 printf("Trying with a zero AAC\n");
167 search.in.acct_control = 0x0;
168 search.in.realm = n1.data.nt5_ex.dns_domain;
169 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
170 CHECK_STATUS(status, NT_STATUS_OK);
171 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
172 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
174 printf("Trying with a zero AAC and user=Administrator\n");
175 search.in.acct_control = 0x0;
176 search.in.user = "Administrator";
177 search.in.realm = n1.data.nt5_ex.dns_domain;
178 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
179 CHECK_STATUS(status, NT_STATUS_OK);
180 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
181 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "Administrator");
183 printf("Trying with a bad AAC\n");
184 search.in.user = NULL;
185 search.in.acct_control = 0xFF00FF00;
186 search.in.realm = n1.data.nt5_ex.dns_domain;
187 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
188 CHECK_STATUS(status, NT_STATUS_OK);
189 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
190 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
192 printf("Trying with a user only\n");
193 search = empty_search;
194 search.in.user = "Administrator";
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.forest, n1.data.nt5_ex.dns_domain);
198 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
199 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
201 printf("Trying with just a bad username\n");
202 search.in.user = "___no_such_user___";
203 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
204 CHECK_STATUS(status, NT_STATUS_OK);
205 CHECK_STRING(search.out.netlogon.data.nt5_ex.forest, n1.data.nt5_ex.dns_domain);
206 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
207 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, search.in.user);
208 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
210 printf("Trying with just a bad domain\n");
211 search = empty_search;
212 search.in.realm = "___no_such_domain___";
213 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
214 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
216 printf("Trying with a incorrect domain and correct guid\n");
217 search.in.domain_guid = GUID_string(tctx, &n1.data.nt5_ex.domain_uuid);
218 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
219 CHECK_STATUS(status, NT_STATUS_OK);
220 CHECK_STRING(search.out.netlogon.data.nt5_ex.forest, n1.data.nt5_ex.dns_domain);
221 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
222 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
223 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
225 printf("Trying with a incorrect domain and incorrect guid\n");
226 search.in.domain_guid = GUID_string(tctx, &guid);
227 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
228 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
229 CHECK_STRING(search.out.netlogon.data.nt5_ex.forest, n1.data.nt5_ex.dns_domain);
230 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
231 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
232 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
234 printf("Trying with a incorrect GUID and correct domain\n");
235 search.in.domain_guid = GUID_string(tctx, &guid);
236 search.in.realm = n1.data.nt5_ex.dns_domain;
237 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
238 CHECK_STATUS(status, NT_STATUS_OK);
239 CHECK_STRING(search.out.netlogon.data.nt5_ex.forest, n1.data.nt5_ex.dns_domain);
240 CHECK_STRING(search.out.netlogon.data.nt5_ex.dns_domain, n1.data.nt5_ex.dns_domain);
241 CHECK_STRING(search.out.netlogon.data.nt5_ex.user_name, "");
242 CHECK_VAL(search.out.netlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
248 test cldap netlogon server type flags
250 static bool test_cldap_netlogon_flags(struct torture_context *tctx,
253 struct cldap_socket *cldap;
255 struct cldap_netlogon search;
256 struct netlogon_samlogon_response n1;
257 uint32_t server_type;
258 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
260 /* cldap_socket_init should now know about the dest. address */
261 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
262 CHECK_STATUS(status, NT_STATUS_OK);
264 printf("Printing out netlogon server type flags: %s\n", dest);
267 search.in.dest_address = dest;
268 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
269 search.in.acct_control = -1;
270 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
271 search.in.map_response = true;
273 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
274 CHECK_STATUS(status, NT_STATUS_OK);
276 n1 = search.out.netlogon;
277 if (n1.ntver == NETLOGON_NT_VERSION_5)
278 server_type = n1.data.nt5.server_type;
279 else if (n1.ntver == NETLOGON_NT_VERSION_5EX)
280 server_type = n1.data.nt5_ex.server_type;
282 printf("The word is: %i\n", server_type);
283 if (server_type & NBT_SERVER_PDC)
284 printf("NBT_SERVER_PDC ");
285 if (server_type & NBT_SERVER_GC)
286 printf("NBT_SERVER_GC ");
287 if (server_type & NBT_SERVER_LDAP)
288 printf("NBT_SERVER_LDAP ");
289 if (server_type & NBT_SERVER_DS)
290 printf("NBT_SERVER_DS ");
291 if (server_type & NBT_SERVER_KDC)
292 printf("NBT_SERVER_KDC ");
293 if (server_type & NBT_SERVER_TIMESERV)
294 printf("NBT_SERVER_TIMESERV ");
295 if (server_type & NBT_SERVER_CLOSEST)
296 printf("NBT_SERVER_CLOSEST ");
297 if (server_type & NBT_SERVER_WRITABLE)
298 printf("NBT_SERVER_WRITABLE ");
299 if (server_type & NBT_SERVER_GOOD_TIMESERV)
300 printf("NBT_SERVER_GOOD_TIMESERV ");
301 if (server_type & NBT_SERVER_NDNC)
302 printf("NBT_SERVER_NDNC ");
303 if (server_type & NBT_SERVER_SELECT_SECRET_DOMAIN_6)
304 printf("NBT_SERVER_SELECT_SECRET_DOMAIN_6");
305 if (server_type & NBT_SERVER_FULL_SECRET_DOMAIN_6)
306 printf("NBT_SERVER_FULL_SECRET_DOMAIN_6");
307 if (server_type & DS_DNS_CONTROLLER)
308 printf("DS_DNS_CONTROLLER ");
309 if (server_type & DS_DNS_DOMAIN)
310 printf("DS_DNS_DOMAIN ");
311 if (server_type & DS_DNS_FOREST_ROOT)
312 printf("DS_DNS_FOREST_ROOT ");
320 convert a ldap result message to a ldb message. This allows us to
321 use the convenient ldif dump routines in ldb to print out cldap
324 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
326 struct ldb_message *msg;
328 msg = ldb_msg_new(mem_ctx);
329 msg->dn = ldb_dn_new(msg, ldb, res->dn);
330 msg->num_elements = res->num_attributes;
331 msg->elements = talloc_steal(msg, res->attributes);
336 dump a set of cldap results
338 static void cldap_dump_results(struct cldap_search *search)
340 struct ldb_ldif ldif;
341 struct ldb_context *ldb;
343 if (!search || !(search->out.response)) {
347 /* we need a ldb context to use ldb_ldif_write_file() */
348 ldb = ldb_init(NULL, NULL);
351 ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
353 ldb_ldif_write_file(ldb, stdout, &ldif);
360 test cldap netlogon server type flag "NBT_SERVER_FOREST_ROOT"
362 static bool test_cldap_netlogon_flag_ds_dns_forest(struct torture_context *tctx,
365 struct cldap_socket *cldap;
367 struct cldap_netlogon search;
368 uint32_t server_type;
369 struct netlogon_samlogon_response n1;
370 struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
373 /* cldap_socket_init should now know about the dest. address */
374 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
375 CHECK_STATUS(status, NT_STATUS_OK);
377 printf("Testing netlogon server type flag NBT_SERVER_FOREST_ROOT: ");
380 search.in.dest_address = dest;
381 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
382 search.in.acct_control = -1;
383 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
384 search.in.map_response = true;
386 status = cldap_netlogon(cldap, iconv_convenience, tctx, &search);
387 CHECK_STATUS(status, NT_STATUS_OK);
389 n1 = search.out.netlogon;
390 if (n1.ntver == NETLOGON_NT_VERSION_5)
391 server_type = n1.data.nt5.server_type;
392 else if (n1.ntver == NETLOGON_NT_VERSION_5EX)
393 server_type = n1.data.nt5_ex.server_type;
395 if (server_type & DS_DNS_FOREST_ROOT) {
396 struct cldap_search search2;
397 const char *attrs[] = { "defaultNamingContext", "rootDomainNamingContext",
399 struct ldb_context *ldb;
400 struct ldb_message *msg;
402 /* Trying to fetch the attributes "defaultNamingContext" and
403 "rootDomainNamingContext" */
404 ZERO_STRUCT(search2);
405 search2.in.dest_address = dest;
406 search2.in.dest_port = lp_cldap_port(tctx->lp_ctx);
407 search2.in.timeout = 10;
408 search2.in.retries = 3;
409 search2.in.filter = "(objectclass=*)";
410 search2.in.attributes = attrs;
412 status = cldap_search(cldap, tctx, &search2);
413 CHECK_STATUS(status, NT_STATUS_OK);
415 ldb = ldb_init(NULL, NULL);
417 msg = ldap_msg_to_ldb(ldb, ldb, search2.out.response);
419 /* Try to compare the two attributes */
420 if (ldb_msg_element_compare(ldb_msg_find_element(msg, attrs[0]),
421 ldb_msg_find_element(msg, attrs[1])))
436 test generic cldap operations
438 static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
440 struct cldap_socket *cldap;
442 struct cldap_search search;
443 const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
444 const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
445 const char *attrs3[] = { "netlogon", NULL };
447 /* cldap_socket_init should now know about the dest. address */
448 status = cldap_socket_init(tctx, NULL, NULL, NULL, &cldap);
449 CHECK_STATUS(status, NT_STATUS_OK);
452 search.in.dest_address = dest;
453 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
454 search.in.timeout = 10;
455 search.in.retries = 3;
457 status = cldap_search(cldap, tctx, &search);
458 CHECK_STATUS(status, NT_STATUS_OK);
460 printf("fetching whole rootDSE\n");
461 search.in.filter = "(objectclass=*)";
462 search.in.attributes = NULL;
464 status = cldap_search(cldap, tctx, &search);
465 CHECK_STATUS(status, NT_STATUS_OK);
467 if (DEBUGLVL(3)) cldap_dump_results(&search);
469 printf("fetching currentTime and USN\n");
470 search.in.filter = "(objectclass=*)";
471 search.in.attributes = attrs1;
473 status = cldap_search(cldap, tctx, &search);
474 CHECK_STATUS(status, NT_STATUS_OK);
476 if (DEBUGLVL(3)) cldap_dump_results(&search);
478 printf("Testing currentTime, USN and netlogon\n");
479 search.in.filter = "(objectclass=*)";
480 search.in.attributes = attrs2;
482 status = cldap_search(cldap, tctx, &search);
483 CHECK_STATUS(status, NT_STATUS_OK);
485 if (DEBUGLVL(3)) cldap_dump_results(&search);
487 printf("Testing objectClass=* and netlogon\n");
488 search.in.filter = "(objectclass2=*)";
489 search.in.attributes = attrs3;
491 status = cldap_search(cldap, tctx, &search);
492 CHECK_STATUS(status, NT_STATUS_OK);
494 if (DEBUGLVL(3)) cldap_dump_results(&search);
496 printf("Testing a false expression\n");
497 search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
498 search.in.attributes = attrs1;
500 status = cldap_search(cldap, tctx, &search);
501 CHECK_STATUS(status, NT_STATUS_OK);
503 if (DEBUGLVL(3)) cldap_dump_results(&search);
508 bool torture_cldap(struct torture_context *torture)
511 const char *host = torture_setting_string(torture, "host", NULL);
513 ret &= test_cldap_netlogon(torture, host);
514 ret &= test_cldap_netlogon_flags(torture, host);
515 ret &= test_cldap_netlogon_flag_ds_dns_forest(torture, host);
516 ret &= test_cldap_generic(torture, host);