bea4b08c8baee87c63db22a33356181bd094800f
[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 #include "param/param.h"
30
31 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
32
33 #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
34
35 #define CHECK_STRING(v, correct) torture_assert_str_equal(tctx, v, correct, "incorrect value");
36 /*
37   test netlogon operations
38 */
39 static bool test_cldap_netlogon(struct torture_context *tctx, const char *dest)
40 {
41         struct cldap_socket *cldap = cldap_socket_init(tctx, NULL);
42         NTSTATUS status;
43         struct cldap_netlogon search, empty_search;
44         union nbt_cldap_netlogon n1;
45         struct GUID guid;
46         int i;
47
48         ZERO_STRUCT(search);
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;
53
54         empty_search = search;
55
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);
60
61         n1 = search.out.netlogon;
62
63         search.in.user         = "Administrator";
64         search.in.realm        = n1.logon5.dns_domain;
65         search.in.host         = "__cldap_torture__";
66
67         printf("Scanning for netlogon levels\n");
68         for (i=0;i<256;i++) {
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);
73         }
74
75         printf("Scanning for netlogon level bits\n");
76         for (i=0;i<31;i++) {
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);
81         }
82
83         search.in.version = 6;
84         status = cldap_netlogon(cldap, tctx, &search);
85         CHECK_STATUS(status, NT_STATUS_OK);
86
87         printf("Trying with User=NULL\n");
88
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);
94
95         printf("Trying with User=Administrator\n");
96
97         search.in.user = "Administrator";
98         status = cldap_netlogon(cldap, tctx, &search);
99         CHECK_STATUS(status, NT_STATUS_OK);
100
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);
103
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);
111
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);
118
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, "");
126
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);
132
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);
140
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);
147
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);
153
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);
161
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);
169
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);
178
179         return true;
180 }
181
182 /*
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
185   search results
186 */
187 static struct ldb_message *ldap_msg_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldap_SearchResEntry *res)
188 {
189         struct ldb_message *msg;
190
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);
195         return msg;
196 }
197
198 /*
199   dump a set of cldap results
200 */
201 static void cldap_dump_results(struct cldap_search *search)
202 {
203         struct ldb_ldif ldif;
204         struct ldb_context *ldb;
205
206         if (!search || !(search->out.response)) {
207                 return;
208         }
209
210         /* we need a ldb context to use ldb_ldif_write_file() */
211         ldb = ldb_init(NULL);
212
213         ZERO_STRUCT(ldif);
214         ldif.msg = ldap_msg_to_ldb(ldb, ldb, search->out.response);
215
216         ldb_ldif_write_file(ldb, stdout, &ldif);
217
218         talloc_free(ldb);
219 }
220
221 /*
222   test generic cldap operations
223 */
224 static bool test_cldap_generic(struct torture_context *tctx, const char *dest)
225 {
226         struct cldap_socket *cldap = cldap_socket_init(tctx, NULL);
227         NTSTATUS status;
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 };
232
233         ZERO_STRUCT(search);
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;
238
239         status = cldap_search(cldap, tctx, &search);
240         CHECK_STATUS(status, NT_STATUS_OK);
241
242         printf("fetching whole rootDSE\n");
243         search.in.filter = "(objectclass=*)";
244         search.in.attributes = NULL;
245
246         status = cldap_search(cldap, tctx, &search);
247         CHECK_STATUS(status, NT_STATUS_OK);
248
249         if (DEBUGLVL(3)) cldap_dump_results(&search);
250
251         printf("fetching currentTime and USN\n");
252         search.in.filter = "(objectclass=*)";
253         search.in.attributes = attrs1;
254
255         status = cldap_search(cldap, tctx, &search);
256         CHECK_STATUS(status, NT_STATUS_OK);
257         
258         if (DEBUGLVL(3)) cldap_dump_results(&search);
259
260         printf("Testing currentTime, USN and netlogon\n");
261         search.in.filter = "(objectclass=*)";
262         search.in.attributes = attrs2;
263
264         status = cldap_search(cldap, tctx, &search);
265         CHECK_STATUS(status, NT_STATUS_OK);
266
267         if (DEBUGLVL(3)) cldap_dump_results(&search);
268
269         printf("Testing objectClass=* and netlogon\n");
270         search.in.filter = "(objectclass2=*)";
271         search.in.attributes = attrs3;
272
273         status = cldap_search(cldap, tctx, &search);
274         CHECK_STATUS(status, NT_STATUS_OK);
275
276         if (DEBUGLVL(3)) cldap_dump_results(&search);
277
278         printf("Testing a false expression\n");
279         search.in.filter = "(&(objectclass=*)(highestCommittedUSN=2))";
280         search.in.attributes = attrs1;
281
282         status = cldap_search(cldap, tctx, &search);
283         CHECK_STATUS(status, NT_STATUS_OK);
284
285         if (DEBUGLVL(3)) cldap_dump_results(&search);   
286
287         return true;    
288 }
289
290 bool torture_cldap(struct torture_context *torture)
291 {
292         bool ret = true;
293         const char *host = torture_setting_string(torture, "host", NULL);
294
295         ret &= test_cldap_netlogon(torture, host);
296         ret &= test_cldap_generic(torture, host);
297
298         return ret;
299 }
300