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 "torture/torture.h"
28 #include "lib/ldb/include/ldb.h"
29 #include "param/param.h"
31 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
33 #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
35 #define CHECK_STRING(v, correct) torture_assert_str_equal(tctx, v, correct, "incorrect value");
37 test netlogon operations
39 static bool test_cldap_netlogon(struct torture_context *tctx, const char *dest)
41 struct cldap_socket *cldap;
43 struct cldap_netlogon search, empty_search;
44 struct netlogon_samlogon_response n1;
48 cldap = cldap_socket_init(tctx, tctx->ev, lp_iconv_convenience(tctx->lp_ctx));
51 search.in.dest_address = dest;
52 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
53 search.in.acct_control = -1;
54 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
55 search.in.map_response = true;
57 empty_search = search;
59 printf("Trying without any attributes\n");
60 search = empty_search;
61 status = cldap_netlogon(cldap, tctx, &search);
62 CHECK_STATUS(status, NT_STATUS_OK);
64 n1 = search.out.netlogon;
66 search.in.user = "Administrator";
67 search.in.realm = n1.nt5_ex.dns_domain;
68 search.in.host = "__cldap_torture__";
70 printf("Scanning for netlogon levels\n");
72 search.in.version = i;
73 printf("Trying netlogon level %d\n", i);
74 status = cldap_netlogon(cldap, tctx, &search);
75 CHECK_STATUS(status, NT_STATUS_OK);
78 printf("Scanning for netlogon level bits\n");
80 search.in.version = (1<<i);
81 printf("Trying netlogon level 0x%x\n", i);
82 status = cldap_netlogon(cldap, tctx, &search);
83 CHECK_STATUS(status, NT_STATUS_OK);
86 search.in.version = NETLOGON_NT_VERSION_5|NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_IP;
88 status = cldap_netlogon(cldap, tctx, &search);
89 CHECK_STATUS(status, NT_STATUS_OK);
91 printf("Trying with User=NULL\n");
93 search.in.user = NULL;
94 status = cldap_netlogon(cldap, tctx, &search);
95 CHECK_STATUS(status, NT_STATUS_OK);
96 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
97 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
99 printf("Trying with User=Administrator\n");
101 search.in.user = "Administrator";
102 status = cldap_netlogon(cldap, tctx, &search);
103 CHECK_STATUS(status, NT_STATUS_OK);
105 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, search.in.user);
106 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
108 search.in.version = NETLOGON_NT_VERSION_5;
109 status = cldap_netlogon(cldap, tctx, &search);
110 CHECK_STATUS(status, NT_STATUS_OK);
112 printf("Trying with User=NULL\n");
114 search.in.user = NULL;
115 status = cldap_netlogon(cldap, tctx, &search);
116 CHECK_STATUS(status, NT_STATUS_OK);
117 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
118 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE);
120 printf("Trying with User=Administrator\n");
122 search.in.user = "Administrator";
123 status = cldap_netlogon(cldap, tctx, &search);
124 CHECK_STATUS(status, NT_STATUS_OK);
126 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, search.in.user);
127 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN);
129 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
131 printf("Trying with a GUID\n");
132 search.in.realm = NULL;
133 search.in.domain_guid = GUID_string(tctx, &n1.nt5_ex.domain_uuid);
134 status = cldap_netlogon(cldap, tctx, &search);
135 CHECK_STATUS(status, NT_STATUS_OK);
136 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
137 CHECK_STRING(GUID_string(tctx, &search.out.netlogon.nt5_ex.domain_uuid), search.in.domain_guid);
139 printf("Trying with a incorrect GUID\n");
140 guid = GUID_random();
141 search.in.user = NULL;
142 search.in.domain_guid = GUID_string(tctx, &guid);
143 status = cldap_netlogon(cldap, tctx, &search);
144 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
146 printf("Trying with a AAC\n");
147 search.in.acct_control = 0x180;
148 search.in.realm = n1.nt5_ex.dns_domain;
149 status = cldap_netlogon(cldap, tctx, &search);
150 CHECK_STATUS(status, NT_STATUS_OK);
151 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
152 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
154 printf("Trying with a bad AAC\n");
155 search.in.acct_control = 0xFF00FF00;
156 search.in.realm = n1.nt5_ex.dns_domain;
157 status = cldap_netlogon(cldap, tctx, &search);
158 CHECK_STATUS(status, NT_STATUS_OK);
160 printf("Trying with a user only\n");
161 search = empty_search;
162 search.in.user = "Administrator";
163 status = cldap_netlogon(cldap, tctx, &search);
164 CHECK_STATUS(status, NT_STATUS_OK);
165 CHECK_STRING(search.out.netlogon.nt5_ex.dns_domain, n1.nt5_ex.dns_domain);
166 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, search.in.user);
168 printf("Trying with just a bad username\n");
169 search.in.user = "___no_such_user___";
170 status = cldap_netlogon(cldap, tctx, &search);
171 CHECK_STATUS(status, NT_STATUS_OK);
172 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, search.in.user);
173 CHECK_STRING(search.out.netlogon.nt5_ex.dns_domain, n1.nt5_ex.dns_domain);
174 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN_EX);
176 printf("Trying with just a bad domain\n");
177 search = empty_search;
178 search.in.realm = "___no_such_domain___";
179 status = cldap_netlogon(cldap, tctx, &search);
180 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
182 printf("Trying with a incorrect domain and correct guid\n");
183 search.in.domain_guid = GUID_string(tctx, &n1.nt5_ex.domain_uuid);
184 status = cldap_netlogon(cldap, tctx, &search);
185 CHECK_STATUS(status, NT_STATUS_OK);
186 CHECK_STRING(search.out.netlogon.nt5_ex.dns_domain, n1.nt5_ex.dns_domain);
187 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
188 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
190 printf("Trying with a incorrect domain and incorrect guid\n");
191 search.in.domain_guid = GUID_string(tctx, &guid);
192 status = cldap_netlogon(cldap, tctx, &search);
193 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
194 CHECK_STRING(search.out.netlogon.nt5_ex.dns_domain, n1.nt5_ex.dns_domain);
195 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
196 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
198 printf("Trying with a incorrect GUID and correct domain\n");
199 search.in.domain_guid = GUID_string(tctx, &guid);
200 search.in.realm = n1.nt5_ex.dns_domain;
201 status = cldap_netlogon(cldap, tctx, &search);
202 CHECK_STATUS(status, NT_STATUS_OK);
203 CHECK_STRING(search.out.netlogon.nt5_ex.dns_domain, n1.nt5_ex.dns_domain);
204 CHECK_STRING(search.out.netlogon.nt5_ex.user_name, "");
205 CHECK_VAL(search.out.netlogon.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX);
211 convert a ldap result message to a ldb message. This allows us to
212 use the convenient ldif dump routines in ldb to print out cldap
215 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
217 struct ldb_message *msg;
219 msg = ldb_msg_new(mem_ctx);
220 msg->dn = ldb_dn_new(msg, ldb, res->dn);
221 msg->num_elements = res->num_attributes;
222 msg->elements = talloc_steal(msg, res->attributes);
227 dump a set of cldap results
229 static void cldap_dump_results(struct cldap_search *search)
231 struct ldb_ldif ldif;
232 struct ldb_context *ldb;
234 if (!search || !(search->out.response)) {
238 /* we need a ldb context to use ldb_ldif_write_file() */
239 ldb = ldb_init(NULL);
242 ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
244 ldb_ldif_write_file(ldb, stdout, &ldif);
250 test generic cldap operations
252 static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
254 struct cldap_socket *cldap;
256 struct cldap_search search;
257 const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
258 const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
259 const char *attrs3[] = { "netlogon", NULL };
261 cldap = cldap_socket_init(tctx, tctx->ev, lp_iconv_convenience(tctx->lp_ctx));
264 search.in.dest_address = dest;
265 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
266 search.in.timeout = 10;
267 search.in.retries = 3;
269 status = cldap_search(cldap, tctx, &search);
270 CHECK_STATUS(status, NT_STATUS_OK);
272 printf("fetching whole rootDSE\n");
273 search.in.filter = "(objectclass=*)";
274 search.in.attributes = NULL;
276 status = cldap_search(cldap, tctx, &search);
277 CHECK_STATUS(status, NT_STATUS_OK);
279 if (DEBUGLVL(3)) cldap_dump_results(&search);
281 printf("fetching currentTime and USN\n");
282 search.in.filter = "(objectclass=*)";
283 search.in.attributes = attrs1;
285 status = cldap_search(cldap, tctx, &search);
286 CHECK_STATUS(status, NT_STATUS_OK);
288 if (DEBUGLVL(3)) cldap_dump_results(&search);
290 printf("Testing currentTime, USN and netlogon\n");
291 search.in.filter = "(objectclass=*)";
292 search.in.attributes = attrs2;
294 status = cldap_search(cldap, tctx, &search);
295 CHECK_STATUS(status, NT_STATUS_OK);
297 if (DEBUGLVL(3)) cldap_dump_results(&search);
299 printf("Testing objectClass=* and netlogon\n");
300 search.in.filter = "(objectclass2=*)";
301 search.in.attributes = attrs3;
303 status = cldap_search(cldap, tctx, &search);
304 CHECK_STATUS(status, NT_STATUS_OK);
306 if (DEBUGLVL(3)) cldap_dump_results(&search);
308 printf("Testing a false expression\n");
309 search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
310 search.in.attributes = attrs1;
312 status = cldap_search(cldap, tctx, &search);
313 CHECK_STATUS(status, NT_STATUS_OK);
315 if (DEBUGLVL(3)) cldap_dump_results(&search);
320 bool torture_cldap(struct torture_context *torture)
323 const char *host = torture_setting_string(torture, "host", NULL);
325 ret &= test_cldap_netlogon(torture, host);
326 ret &= test_cldap_generic(torture, host);