s4:torture - fix up "ldap_basic" test
[samba.git] / source4 / torture / ldap / basic.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP protocol helper functions for SAMBA
4    
5    Copyright (C) Stefan Metzmacher 2004
6    Copyright (C) Simo Sorce 2004
7    Copyright (C) Matthias Dieter Wallnöfer 2009
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    
22 */
23
24 #include "includes.h"
25 #include "libcli/ldap/ldap_client.h"
26 #include "lib/cmdline/popt_common.h"
27
28 #include "torture/torture.h"
29 #include "torture/ldap/proto.h"
30
31 #include "param/param.h"
32
33 static bool test_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password)
34 {
35         NTSTATUS status;
36         bool ret = true;
37
38         status = torture_ldap_bind(conn, userdn, password);
39         if (!NT_STATUS_IS_OK(status)) {
40                 ret = false;
41         }
42
43         return ret;
44 }
45
46 static bool test_bind_sasl(struct torture_context *tctx,
47                            struct ldap_connection *conn, struct cli_credentials *creds)
48 {
49         NTSTATUS status;
50         bool ret = true;
51
52         printf("Testing sasl bind as user\n");
53
54         status = torture_ldap_bind_sasl(conn, creds, tctx->lp_ctx);
55         if (!NT_STATUS_IS_OK(status)) {
56                 ret = false;
57         }
58
59         return ret;
60 }
61
62 static bool test_multibind(struct ldap_connection *conn, const char *userdn, const char *password)
63 {
64         bool ret = true;
65
66         printf("Testing multiple binds on a single connnection as anonymous and user\n");
67
68         ret = test_bind_simple(conn, NULL, NULL);
69         if (!ret) {
70                 printf("1st bind as anonymous failed\n");
71                 return ret;
72         }
73
74         ret = test_bind_simple(conn, userdn, password);
75         if (!ret) {
76                 printf("2nd bind as authenticated user failed\n");
77         }
78
79         return ret;
80 }
81
82 static bool test_search_rootDSE(struct ldap_connection *conn, char **basedn)
83 {
84         bool ret = true;
85         struct ldap_message *msg, *result;
86         struct ldap_request *req;
87         int i;
88         struct ldap_SearchResEntry *r;
89         NTSTATUS status;
90
91         printf("Testing RootDSE Search\n");
92
93         *basedn = NULL;
94
95         msg = new_ldap_message(conn);
96         if (!msg) {
97                 return false;
98         }
99
100         msg->type = LDAP_TAG_SearchRequest;
101         msg->r.SearchRequest.basedn = "";
102         msg->r.SearchRequest.scope = LDAP_SEARCH_SCOPE_BASE;
103         msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
104         msg->r.SearchRequest.timelimit = 0;
105         msg->r.SearchRequest.sizelimit = 0;
106         msg->r.SearchRequest.attributesonly = false;
107         msg->r.SearchRequest.tree = ldb_parse_tree(msg, "(objectclass=*)");
108         msg->r.SearchRequest.num_attributes = 0;
109         msg->r.SearchRequest.attributes = NULL;
110
111         req = ldap_request_send(conn, msg);
112         if (req == NULL) {
113                 printf("Could not setup ldap search\n");
114                 return false;
115         }
116
117         status = ldap_result_one(req, &result, LDAP_TAG_SearchResultEntry);
118         if (!NT_STATUS_IS_OK(status)) {
119                 printf("search failed - %s\n", nt_errstr(status));
120                 return false;
121         }
122
123         printf("received %d replies\n", req->num_replies);
124
125         r = &result->r.SearchResultEntry;
126                 
127         DEBUG(1,("\tdn: %s\n", r->dn));
128         for (i=0; i<r->num_attributes; i++) {
129                 int j;
130                 for (j=0; j<r->attributes[i].num_values; j++) {
131                         DEBUG(1,("\t%s: %d %.*s\n", r->attributes[i].name,
132                                  (int)r->attributes[i].values[j].length,
133                                  (int)r->attributes[i].values[j].length,
134                                  (char *)r->attributes[i].values[j].data));
135                         if (!(*basedn) && 
136                             strcasecmp("defaultNamingContext",r->attributes[i].name)==0) {
137                                 *basedn = talloc_asprintf(conn, "%.*s",
138                                                           (int)r->attributes[i].values[j].length,
139                                                           (char *)r->attributes[i].values[j].data);
140                         }
141                 }
142         }
143
144         talloc_free(req);
145
146         return ret;
147 }
148
149 static bool test_compare_sasl(struct ldap_connection *conn, const char *basedn)
150 {
151         struct ldap_message *msg, *rep;
152         struct ldap_request *req;
153         const char *val;
154         NTSTATUS status;
155
156         printf("Testing SASL Compare: %s\n", basedn);
157
158         if (!basedn) {
159                 return false;
160         }
161
162         msg = new_ldap_message(conn);
163         if (!msg) {
164                 return false;
165         }
166
167         msg->type = LDAP_TAG_CompareRequest;
168         msg->r.CompareRequest.dn = basedn;
169         msg->r.CompareRequest.attribute = talloc_strdup(msg, "objectClass");
170         val = "domain";
171         msg->r.CompareRequest.value = data_blob_talloc(msg, val, strlen(val));
172
173         req = ldap_request_send(conn, msg);
174         if (!req) {
175                 return false;
176         }
177
178         status = ldap_result_one(req, &rep, LDAP_TAG_CompareResponse);
179         if (!NT_STATUS_IS_OK(status)) {
180                 printf("error in ldap compare request - %s\n", nt_errstr(status));
181                 return false;
182         }
183
184         DEBUG(5,("Code: %d DN: [%s] ERROR:[%s] REFERRAL:[%s]\n",
185                 rep->r.CompareResponse.resultcode,
186                 rep->r.CompareResponse.dn,
187                 rep->r.CompareResponse.errormessage,
188                 rep->r.CompareResponse.referral));
189
190         return true;
191 }
192
193 /*
194  * This takes an AD error message and splits it into the WERROR code
195  * (WERR_DS_GENERIC if none found) and the reason (remaining string).
196  */
197 static WERROR ad_error(const char *err_msg, char **reason)
198 {
199         WERROR err = W_ERROR(strtol(err_msg, reason, 16));
200
201         if ((reason != NULL) && (*reason[0] != ':')) {
202                 return WERR_DS_GENERIC_ERROR; /* not an AD std error message */
203         }
204                 
205         if (reason != NULL) {
206                 *reason += 2; /* skip ": " */
207         }
208         return err;
209 }
210
211 static bool test_error_codes(struct torture_context *tctx,
212         struct ldap_connection *conn, const char *basedn)
213 {
214         struct ldap_message *msg, *rep;
215         struct ldap_request *req;
216         const char *err_code_str;
217         char *endptr;
218         WERROR err;
219         NTSTATUS status;
220
221         printf("Testing error codes - to make this test pass against SAMBA 4 you have to specify the target!\n");
222
223         if (!basedn) {
224                 return false;
225         }
226
227         msg = new_ldap_message(conn);
228         if (!msg) {
229                 return false;
230         }
231
232         printf(" Try a wrong addition\n");
233
234         msg->type = LDAP_TAG_AddRequest;
235         msg->r.AddRequest.dn = basedn;
236         msg->r.AddRequest.num_attributes = 0;
237         msg->r.AddRequest.attributes = NULL;
238
239         req = ldap_request_send(conn, msg);
240         if (!req) {
241                 return false;
242         }
243
244         status = ldap_result_one(req, &rep, LDAP_TAG_AddResponse);
245         if (!NT_STATUS_IS_OK(status)) {
246                 printf("error in ldap addition request - %s\n", nt_errstr(status));
247                 return false;
248         }
249
250         if ((rep->r.AddResponse.resultcode == 0)
251                 || (rep->r.AddResponse.errormessage == NULL)
252                 || (strtol(rep->r.AddResponse.errormessage, &endptr,16) <= 0)
253                 || (*endptr != ':')) {
254                 printf("Invalid error message!\n");
255                 return false;
256         }
257
258         err = ad_error(rep->r.AddResponse.errormessage, &endptr);
259         err_code_str = win_errstr(err);
260         printf(" - Errorcode: %s; Reason: %s\n", err_code_str, endptr);
261         if (!torture_setting_bool(tctx, "samba4", false)) {
262                 if ((!W_ERROR_EQUAL(err, WERR_DS_REFERRAL))
263                         || (rep->r.AddResponse.resultcode != 10)) {
264                         return false;
265                 }
266         } else {
267                 if ((!W_ERROR_EQUAL(err, WERR_DS_OBJ_CLASS_VIOLATION))
268                         || (rep->r.AddResponse.resultcode != 65)) {
269                         return false;
270                 }
271         }
272
273         printf(" Try a wrong modification\n");
274
275         msg->type = LDAP_TAG_ModifyRequest;
276         msg->r.ModifyRequest.dn = "";
277         msg->r.ModifyRequest.num_mods = 0;
278         msg->r.ModifyRequest.mods = NULL;
279
280         req = ldap_request_send(conn, msg);
281         if (!req) {
282                 return false;
283         }
284
285         status = ldap_result_one(req, &rep, LDAP_TAG_ModifyResponse);
286         if (!NT_STATUS_IS_OK(status)) {
287                 printf("error in ldap modifification request - %s\n", nt_errstr(status));
288                 return false;
289         }
290
291         if ((rep->r.ModifyResponse.resultcode == 0)
292                 || (rep->r.ModifyResponse.errormessage == NULL)
293                 || (strtol(rep->r.ModifyResponse.errormessage, &endptr,16) <= 0)
294                 || (*endptr != ':')) {
295                 printf("Invalid error message!\n");
296                 return false;
297         }
298
299         err = ad_error(rep->r.ModifyResponse.errormessage, &endptr);
300         err_code_str = win_errstr(err);
301         printf(" - Errorcode: %s; Reason: %s\n", err_code_str, endptr);
302         if (!torture_setting_bool(tctx, "samba4", false)) {
303                 if ((!W_ERROR_EQUAL(err, WERR_INVALID_PARAM))
304                         || (rep->r.ModifyResponse.resultcode != 53)) {
305                         return false;
306                 }
307         } else {
308                 if ((!W_ERROR_EQUAL(err, WERR_DS_OPERATIONS_ERROR))
309                         || (rep->r.ModifyResponse.resultcode != 1)) {
310                         return false;
311                 }
312         }
313
314         printf(" Try a wrong removal\n");
315
316         msg->type = LDAP_TAG_DelRequest;
317         msg->r.DelRequest.dn = "";
318
319         req = ldap_request_send(conn, msg);
320         if (!req) {
321                 return false;
322         }
323
324         status = ldap_result_one(req, &rep, LDAP_TAG_DelResponse);
325         if (!NT_STATUS_IS_OK(status)) {
326                 printf("error in ldap removal request - %s\n", nt_errstr(status));
327                 return false;
328         }
329
330         if ((rep->r.DelResponse.resultcode == 0)
331                 || (rep->r.DelResponse.errormessage == NULL)
332                 || (strtol(rep->r.DelResponse.errormessage, &endptr,16) <= 0)
333                 || (*endptr != ':')) {
334                 printf("Invalid error message!\n");
335                 return false;
336         }
337         
338         err = ad_error(rep->r.DelResponse.errormessage, &endptr);
339         err_code_str = win_errstr(err);
340         printf(" - Errorcode: %s; Reason: %s\n", err_code_str, endptr);
341         if (!torture_setting_bool(tctx, "samba4", false)) {
342                 if ((!W_ERROR_EQUAL(err, WERR_DS_OBJ_NOT_FOUND))
343                         || (rep->r.DelResponse.resultcode != 32)) {
344                         return false;
345                 }
346         } else {
347                 if ((!W_ERROR_EQUAL(err, WERR_DS_INVALID_DN_SYNTAX))
348                         || (rep->r.DelResponse.resultcode != 34)) {
349                         return false;
350                 }
351         }
352
353         return true;
354 }
355
356 bool torture_ldap_basic(struct torture_context *torture)
357 {
358         NTSTATUS status;
359         struct ldap_connection *conn;
360         TALLOC_CTX *mem_ctx;
361         bool ret = true;
362         const char *host = torture_setting_string(torture, "host", NULL);
363         const char *userdn = torture_setting_string(torture, "ldap_userdn", NULL);
364         const char *secret = torture_setting_string(torture, "ldap_secret", NULL);
365         char *url;
366         char *basedn;
367
368         mem_ctx = talloc_init("torture_ldap_basic");
369
370         url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
371
372         status = torture_ldap_connection(torture, &conn, url);
373         if (!NT_STATUS_IS_OK(status)) {
374                 return false;
375         }
376
377         if (!test_search_rootDSE(conn, &basedn)) {
378                 ret = false;
379         }
380
381         /* other bind tests here */
382
383         if (!test_multibind(conn, userdn, secret)) {
384                 ret = false;
385         }
386
387         if (!test_bind_sasl(torture, conn, cmdline_credentials)) {
388                 ret = false;
389         }
390
391         if (!test_compare_sasl(conn, basedn)) {
392                 ret = false;
393         }
394
395         /* error codes test here */
396
397         if (!test_error_codes(torture, conn, basedn)) {
398                 ret = false;
399         }
400
401         /* if there are no more tests we are closing */
402         torture_ldap_close(conn);
403         talloc_free(mem_ctx);
404
405         return ret;
406 }
407