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 = cldap_socket_init(tctx, NULL);
43 struct cldap_netlogon search, empty_search;
44 union nbt_cldap_netlogon n1;
49 search.in.dest_address = dest;
50 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
51 search.in.acct_control = -1;
52 search.in.version = 6;
54 empty_search = search;
56 printf("Trying without any attributes\n");
57 search = empty_search;
58 status = cldap_netlogon(cldap, tctx, &search);
59 CHECK_STATUS(status, NT_STATUS_OK);
61 n1 = search.out.netlogon;
63 search.in.user = "Administrator";
64 search.in.realm = n1.logon5.dns_domain;
65 search.in.host = "__cldap_torture__";
67 printf("Scanning for netlogon levels\n");
69 search.in.version = i;
70 printf("Trying netlogon level %d\n", i);
71 status = cldap_netlogon(cldap, tctx, &search);
72 CHECK_STATUS(status, NT_STATUS_OK);
75 printf("Scanning for netlogon level bits\n");
77 search.in.version = (1<<i);
78 printf("Trying netlogon level 0x%x\n", i);
79 status = cldap_netlogon(cldap, tctx, &search);
80 CHECK_STATUS(status, NT_STATUS_OK);
83 search.in.version = 6;
84 status = cldap_netlogon(cldap, tctx, &search);
85 CHECK_STATUS(status, NT_STATUS_OK);
87 printf("Trying with User=NULL\n");
89 search.in.user = NULL;
90 status = cldap_netlogon(cldap, tctx, &search);
91 CHECK_STATUS(status, NT_STATUS_OK);
92 CHECK_STRING(search.out.netlogon.logon5.user_name, "");
93 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC2);
95 printf("Trying with User=Administrator\n");
97 search.in.user = "Administrator";
98 status = cldap_netlogon(cldap, tctx, &search);
99 CHECK_STATUS(status, NT_STATUS_OK);
101 CHECK_STRING(search.out.netlogon.logon5.user_name, search.in.user);
102 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC_USER);
104 printf("Trying with a GUID\n");
105 search.in.realm = NULL;
106 search.in.domain_guid = GUID_string(tctx, &n1.logon5.domain_uuid);
107 status = cldap_netlogon(cldap, tctx, &search);
108 CHECK_STATUS(status, NT_STATUS_OK);
109 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC_USER);
110 CHECK_STRING(GUID_string(tctx, &search.out.netlogon.logon5.domain_uuid), search.in.domain_guid);
112 printf("Trying with a incorrect GUID\n");
113 guid = GUID_random();
114 search.in.user = NULL;
115 search.in.domain_guid = GUID_string(tctx, &guid);
116 status = cldap_netlogon(cldap, tctx, &search);
117 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
119 printf("Trying with a AAC\n");
120 search.in.acct_control = 0x180;
121 search.in.realm = n1.logon5.dns_domain;
122 status = cldap_netlogon(cldap, tctx, &search);
123 CHECK_STATUS(status, NT_STATUS_OK);
124 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC2);
125 CHECK_STRING(search.out.netlogon.logon5.user_name, "");
127 printf("Trying with a bad AAC\n");
128 search.in.acct_control = 0xFF00FF00;
129 search.in.realm = n1.logon5.dns_domain;
130 status = cldap_netlogon(cldap, tctx, &search);
131 CHECK_STATUS(status, NT_STATUS_OK);
133 printf("Trying with a user only\n");
134 search = empty_search;
135 search.in.user = "Administrator";
136 status = cldap_netlogon(cldap, tctx, &search);
137 CHECK_STATUS(status, NT_STATUS_OK);
138 CHECK_STRING(search.out.netlogon.logon5.dns_domain, n1.logon5.dns_domain);
139 CHECK_STRING(search.out.netlogon.logon5.user_name, search.in.user);
141 printf("Trying with just a bad username\n");
142 search.in.user = "___no_such_user___";
143 status = cldap_netlogon(cldap, tctx, &search);
144 CHECK_STATUS(status, NT_STATUS_OK);
145 CHECK_STRING(search.out.netlogon.logon5.user_name, search.in.user);
146 CHECK_STRING(search.out.netlogon.logon5.dns_domain, n1.logon5.dns_domain);
148 printf("Trying with just a bad domain\n");
149 search = empty_search;
150 search.in.realm = "___no_such_domain___";
151 status = cldap_netlogon(cldap, tctx, &search);
152 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
154 printf("Trying with a incorrect domain and correct guid\n");
155 search.in.domain_guid = GUID_string(tctx, &n1.logon5.domain_uuid);
156 status = cldap_netlogon(cldap, tctx, &search);
157 CHECK_STATUS(status, NT_STATUS_OK);
158 CHECK_STRING(search.out.netlogon.logon5.dns_domain, n1.logon5.dns_domain);
159 CHECK_STRING(search.out.netlogon.logon5.user_name, "");
160 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC2);
162 printf("Trying with a incorrect domain and incorrect guid\n");
163 search.in.domain_guid = GUID_string(tctx, &guid);
164 status = cldap_netlogon(cldap, tctx, &search);
165 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
166 CHECK_STRING(search.out.netlogon.logon5.dns_domain, n1.logon5.dns_domain);
167 CHECK_STRING(search.out.netlogon.logon5.user_name, "");
168 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC2);
170 printf("Trying with a incorrect GUID and correct domain\n");
171 search.in.domain_guid = GUID_string(tctx, &guid);
172 search.in.realm = n1.logon5.dns_domain;
173 status = cldap_netlogon(cldap, tctx, &search);
174 CHECK_STATUS(status, NT_STATUS_OK);
175 CHECK_STRING(search.out.netlogon.logon5.dns_domain, n1.logon5.dns_domain);
176 CHECK_STRING(search.out.netlogon.logon5.user_name, "");
177 CHECK_VAL(search.out.netlogon.logon5.type, NETLOGON_RESPONSE_FROM_PDC2);
183 convert a ldap result message to a ldb message. This allows us to
184 use the convenient ldif dump routines in ldb to print out cldap
187 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
189 struct ldb_message *msg;
191 msg = ldb_msg_new(mem_ctx);
192 msg->dn = ldb_dn_new(msg, ldb, res->dn);
193 msg->num_elements = res->num_attributes;
194 msg->elements = talloc_steal(msg, res->attributes);
199 dump a set of cldap results
201 static void cldap_dump_results(struct cldap_search *search)
203 struct ldb_ldif ldif;
204 struct ldb_context *ldb;
206 if (!search || !(search->out.response)) {
210 /* we need a ldb context to use ldb_ldif_write_file() */
211 ldb = ldb_init(NULL);
214 ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
216 ldb_ldif_write_file(ldb, stdout, &ldif);
222 test generic cldap operations
224 static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
226 struct cldap_socket *cldap = cldap_socket_init(tctx, NULL);
228 struct cldap_search search;
229 const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
230 const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
231 const char *attrs3[] = { "netlogon", NULL };
234 search.in.dest_address = dest;
235 search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
236 search.in.timeout = 10;
237 search.in.retries = 3;
239 status = cldap_search(cldap, tctx, &search);
240 CHECK_STATUS(status, NT_STATUS_OK);
242 printf("fetching whole rootDSE\n");
243 search.in.filter = "(objectclass=*)";
244 search.in.attributes = NULL;
246 status = cldap_search(cldap, tctx, &search);
247 CHECK_STATUS(status, NT_STATUS_OK);
249 if (DEBUGLVL(3)) cldap_dump_results(&search);
251 printf("fetching currentTime and USN\n");
252 search.in.filter = "(objectclass=*)";
253 search.in.attributes = attrs1;
255 status = cldap_search(cldap, tctx, &search);
256 CHECK_STATUS(status, NT_STATUS_OK);
258 if (DEBUGLVL(3)) cldap_dump_results(&search);
260 printf("Testing currentTime, USN and netlogon\n");
261 search.in.filter = "(objectclass=*)";
262 search.in.attributes = attrs2;
264 status = cldap_search(cldap, tctx, &search);
265 CHECK_STATUS(status, NT_STATUS_OK);
267 if (DEBUGLVL(3)) cldap_dump_results(&search);
269 printf("Testing objectClass=* and netlogon\n");
270 search.in.filter = "(objectclass2=*)";
271 search.in.attributes = attrs3;
273 status = cldap_search(cldap, tctx, &search);
274 CHECK_STATUS(status, NT_STATUS_OK);
276 if (DEBUGLVL(3)) cldap_dump_results(&search);
278 printf("Testing a false expression\n");
279 search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
280 search.in.attributes = attrs1;
282 status = cldap_search(cldap, tctx, &search);
283 CHECK_STATUS(status, NT_STATUS_OK);
285 if (DEBUGLVL(3)) cldap_dump_results(&search);
290 bool torture_cldap(struct torture_context *torture)
293 const char *host = torture_setting_string(torture, "host", NULL);
295 ret &= test_cldap_netlogon(torture, host);
296 ret &= test_cldap_generic(torture, host);