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"
30 #define CHECK_STATUS(status, correct) do { \
31 if (!NT_STATUS_EQUAL(status, correct)) { \
32 printf("(%s) Incorrect status %s - should be %s\n", \
33 __location__, nt_errstr(status), nt_errstr(correct)); \
40 test netlogon operations
42 static bool test_cldap_netlogon(TALLOC_CTX *mem_ctx, const char *dest)
44 struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
46 struct cldap_netlogon search, empty_search;
47 union nbt_cldap_netlogon n1;
53 search.in.dest_address = dest;
54 search.in.acct_control = -1;
55 search.in.version = 6;
57 empty_search = search;
59 printf("Trying without any attributes\n");
60 search = empty_search;
61 status = cldap_netlogon(cldap, mem_ctx, &search);
62 CHECK_STATUS(status, NT_STATUS_OK);
64 n1 = search.out.netlogon;
66 search.in.user = "Administrator";
67 search.in.realm = n1.logon5.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, mem_ctx, &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, mem_ctx, &search);
83 CHECK_STATUS(status, NT_STATUS_OK);
86 search.in.version = 6;
87 status = cldap_netlogon(cldap, mem_ctx, &search);
88 CHECK_STATUS(status, NT_STATUS_OK);
90 printf("Trying with User=NULL\n");
92 search.in.user = NULL;
93 status = cldap_netlogon(cldap, mem_ctx, &search);
94 CHECK_STATUS(status, NT_STATUS_OK);
96 printf("Trying with User=Administrator\n");
98 search.in.user = "Administrator";
99 status = cldap_netlogon(cldap, mem_ctx, &search);
100 CHECK_STATUS(status, NT_STATUS_OK);
102 printf("Trying with a GUID\n");
103 search.in.realm = NULL;
104 search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
105 status = cldap_netlogon(cldap, mem_ctx, &search);
106 CHECK_STATUS(status, NT_STATUS_OK);
108 printf("Trying with a incorrect GUID\n");
109 guid = GUID_random();
110 search.in.user = NULL;
111 search.in.domain_guid = GUID_string(mem_ctx, &guid);
112 status = cldap_netlogon(cldap, mem_ctx, &search);
113 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
115 printf("Trying with a AAC\n");
116 search.in.acct_control = 0x180;
117 search.in.realm = n1.logon5.dns_domain;
118 status = cldap_netlogon(cldap, mem_ctx, &search);
119 CHECK_STATUS(status, NT_STATUS_OK);
121 printf("Trying with a bad AAC\n");
122 search.in.acct_control = 0xFF00FF00;
123 search.in.realm = n1.logon5.dns_domain;
124 status = cldap_netlogon(cldap, mem_ctx, &search);
125 CHECK_STATUS(status, NT_STATUS_OK);
127 printf("Trying with a user only\n");
128 search = empty_search;
129 search.in.user = "Administrator";
130 status = cldap_netlogon(cldap, mem_ctx, &search);
131 CHECK_STATUS(status, NT_STATUS_OK);
133 printf("Trying with just a bad username\n");
134 search.in.user = "___no_such_user___";
135 status = cldap_netlogon(cldap, mem_ctx, &search);
136 CHECK_STATUS(status, NT_STATUS_OK);
138 printf("Trying with just a bad domain\n");
139 search = empty_search;
140 search.in.realm = "___no_such_domain___";
141 status = cldap_netlogon(cldap, mem_ctx, &search);
142 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
144 printf("Trying with a incorrect domain and correct guid\n");
145 search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
146 status = cldap_netlogon(cldap, mem_ctx, &search);
147 CHECK_STATUS(status, NT_STATUS_OK);
149 printf("Trying with a incorrect domain and incorrect guid\n");
150 search.in.domain_guid = GUID_string(mem_ctx, &guid);
151 status = cldap_netlogon(cldap, mem_ctx, &search);
152 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
154 printf("Trying with a incorrect GUID and correct domain\n");
155 search.in.domain_guid = GUID_string(mem_ctx, &guid);
156 search.in.realm = n1.logon5.dns_domain;
157 status = cldap_netlogon(cldap, mem_ctx, &search);
158 CHECK_STATUS(status, NT_STATUS_OK);
165 convert a ldap result message to a ldb message. This allows us to
166 use the convenient ldif dump routines in ldb to print out cldap
169 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
171 struct ldb_message *msg;
173 msg = ldb_msg_new(mem_ctx);
174 msg->dn = ldb_dn_new(msg, ldb, res->dn);
175 msg->num_elements = res->num_attributes;
176 msg->elements = talloc_steal(msg, res->attributes);
181 dump a set of cldap results
183 static void cldap_dump_results(struct cldap_search *search)
185 struct ldb_ldif ldif;
186 struct ldb_context *ldb;
188 if (!search || !(search->out.response)) {
192 /* we need a ldb context to use ldb_ldif_write_file() */
193 ldb = ldb_init(NULL);
196 ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
198 ldb_ldif_write_file(ldb, stdout, &ldif);
204 test generic cldap operations
206 static bool test_cldap_generic(TALLOC_CTX *mem_ctx, const char *dest)
208 struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
210 struct cldap_search search;
212 const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
213 const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
214 const char *attrs3[] = { "netlogon", NULL };
217 search.in.dest_address = dest;
218 search.in.timeout = 10;
219 search.in.retries = 3;
221 status = cldap_search(cldap, mem_ctx, &search);
222 CHECK_STATUS(status, NT_STATUS_OK);
224 printf("fetching whole rootDSE\n");
225 search.in.filter = "(objectclass=*)";
226 search.in.attributes = NULL;
228 status = cldap_search(cldap, mem_ctx, &search);
229 CHECK_STATUS(status, NT_STATUS_OK);
231 if (DEBUGLVL(3)) cldap_dump_results(&search);
233 printf("fetching currentTime and USN\n");
234 search.in.filter = "(objectclass=*)";
235 search.in.attributes = attrs1;
237 status = cldap_search(cldap, mem_ctx, &search);
238 CHECK_STATUS(status, NT_STATUS_OK);
240 if (DEBUGLVL(3)) cldap_dump_results(&search);
242 printf("Testing currentTime, USN and netlogon\n");
243 search.in.filter = "(objectclass=*)";
244 search.in.attributes = attrs2;
246 status = cldap_search(cldap, mem_ctx, &search);
247 CHECK_STATUS(status, NT_STATUS_OK);
249 if (DEBUGLVL(3)) cldap_dump_results(&search);
251 printf("Testing objectClass=* and netlogon\n");
252 search.in.filter = "(objectclass2=*)";
253 search.in.attributes = attrs3;
255 status = cldap_search(cldap, mem_ctx, &search);
256 CHECK_STATUS(status, NT_STATUS_OK);
258 if (DEBUGLVL(3)) cldap_dump_results(&search);
260 printf("Testing a false expression\n");
261 search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
262 search.in.attributes = attrs1;
264 status = cldap_search(cldap, mem_ctx, &search);
265 CHECK_STATUS(status, NT_STATUS_OK);
267 if (DEBUGLVL(3)) cldap_dump_results(&search);
273 bool torture_cldap(struct torture_context *torture)
277 const char *host = torture_setting_string(torture, "host", NULL);
279 mem_ctx = talloc_init("torture_cldap");
281 ret &= test_cldap_netlogon(mem_ctx, host);
282 ret &= test_cldap_generic(mem_ctx, host);
284 talloc_free(mem_ctx);