Merge commit 'release-4-0-0alpha1' into v4-0-test
[samba.git] / source4 / torture / ldap / cldap.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    test CLDAP operations
5    
6    Copyright (C) Andrew Tridgell 2005
7     
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.
12    
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.
17    
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/>.
20    
21 */
22
23 #include "includes.h"
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
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)); \
34                 ret = false; \
35                 goto done; \
36         } \
37 } while (0)
38
39 /*
40   test netlogon operations
41 */
42 static bool test_cldap_netlogon(TALLOC_CTX *mem_ctx, const char *dest)
43 {
44         struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
45         NTSTATUS status;
46         struct cldap_netlogon search, empty_search;
47         union nbt_cldap_netlogon n1;
48         struct GUID guid;
49         int i;
50         bool ret = true;
51
52         ZERO_STRUCT(search);
53         search.in.dest_address = dest;
54         search.in.acct_control = -1;
55         search.in.version = 6;
56
57         empty_search = search;
58
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);
63
64         n1 = search.out.netlogon;
65
66         search.in.user         = "Administrator";
67         search.in.realm        = n1.logon5.dns_domain;
68         search.in.host         = "__cldap_torture__";
69
70         printf("Scanning for netlogon levels\n");
71         for (i=0;i<256;i++) {
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);
76         }
77
78         printf("Scanning for netlogon level bits\n");
79         for (i=0;i<31;i++) {
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);
84         }
85
86         search.in.version = 6;
87         status = cldap_netlogon(cldap, mem_ctx, &search);
88         CHECK_STATUS(status, NT_STATUS_OK);
89
90         printf("Trying with User=NULL\n");
91
92         search.in.user = NULL;
93         status = cldap_netlogon(cldap, mem_ctx, &search);
94         CHECK_STATUS(status, NT_STATUS_OK);
95
96         printf("Trying with User=Administrator\n");
97
98         search.in.user = "Administrator";
99         status = cldap_netlogon(cldap, mem_ctx, &search);
100         CHECK_STATUS(status, NT_STATUS_OK);
101
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);
107
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);
114
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);
120
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);
126
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);
132
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);
137
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);
143
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);
148
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);
153
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);
159
160 done:
161         return ret;     
162 }
163
164 /*
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
167   search results
168 */
169 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
170 {
171         struct ldb_message *msg;
172
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);
177         return msg;
178 }
179
180 /*
181   dump a set of cldap results
182 */
183 static void cldap_dump_results(struct cldap_search *search)
184 {
185         struct ldb_ldif ldif;
186         struct ldb_context *ldb;
187
188         if (!search || !(search->out.response)) {
189                 return;
190         }
191
192         /* we need a ldb context to use ldb_ldif_write_file() */
193         ldb = ldb_init(NULL);
194
195         ZERO_STRUCT(ldif);
196         ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
197
198         ldb_ldif_write_file(ldb, stdout, &ldif);
199
200         talloc_free(ldb);
201 }
202
203 /*
204   test generic cldap operations
205 */
206 static bool test_cldap_generic(TALLOC_CTX *mem_ctx, const char *dest)
207 {
208         struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
209         NTSTATUS status;
210         struct cldap_search search;
211         bool ret = true;
212         const char *attrs1[] = { "currentTime", "highestCommittedUSN", NULL };
213         const char *attrs2[] = { "currentTime", "highestCommittedUSN", "netlogon", NULL };
214         const char *attrs3[] = { "netlogon", NULL };
215
216         ZERO_STRUCT(search);
217         search.in.dest_address = dest;
218         search.in.timeout = 10;
219         search.in.retries = 3;
220
221         status = cldap_search(cldap, mem_ctx, &search);
222         CHECK_STATUS(status, NT_STATUS_OK);
223
224         printf("fetching whole rootDSE\n");
225         search.in.filter = "(objectclass=*)";
226         search.in.attributes = NULL;
227
228         status = cldap_search(cldap, mem_ctx, &search);
229         CHECK_STATUS(status, NT_STATUS_OK);
230
231         if (DEBUGLVL(3)) cldap_dump_results(&search);
232
233         printf("fetching currentTime and USN\n");
234         search.in.filter = "(objectclass=*)";
235         search.in.attributes = attrs1;
236
237         status = cldap_search(cldap, mem_ctx, &search);
238         CHECK_STATUS(status, NT_STATUS_OK);
239         
240         if (DEBUGLVL(3)) cldap_dump_results(&search);
241
242         printf("Testing currentTime, USN and netlogon\n");
243         search.in.filter = "(objectclass=*)";
244         search.in.attributes = attrs2;
245
246         status = cldap_search(cldap, mem_ctx, &search);
247         CHECK_STATUS(status, NT_STATUS_OK);
248
249         if (DEBUGLVL(3)) cldap_dump_results(&search);
250
251         printf("Testing objectClass=* and netlogon\n");
252         search.in.filter = "(objectclass2=*)";
253         search.in.attributes = attrs3;
254
255         status = cldap_search(cldap, mem_ctx, &search);
256         CHECK_STATUS(status, NT_STATUS_OK);
257
258         if (DEBUGLVL(3)) cldap_dump_results(&search);
259
260         printf("Testing a false expression\n");
261         search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
262         search.in.attributes = attrs1;
263
264         status = cldap_search(cldap, mem_ctx, &search);
265         CHECK_STATUS(status, NT_STATUS_OK);
266
267         if (DEBUGLVL(3)) cldap_dump_results(&search);   
268
269 done:
270         return ret;     
271 }
272
273 bool torture_cldap(struct torture_context *torture)
274 {
275         TALLOC_CTX *mem_ctx;
276         bool ret = true;
277         const char *host = torture_setting_string(torture, "host", NULL);
278
279         mem_ctx = talloc_init("torture_cldap");
280
281         ret &= test_cldap_netlogon(mem_ctx, host);
282         ret &= test_cldap_generic(mem_ctx, host);
283
284         talloc_free(mem_ctx);
285
286         return ret;
287 }
288