Merge commit 'release-4-0-0alpha1' into v4-0-test
[ira/wip.git] / source / 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     
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/ldap/ldap_client.h"
25 #include "lib/cmdline/popt_common.h"
26
27 #include "torture/torture.h"
28 #include "torture/ldap/proto.h"
29
30 static bool test_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password)
31 {
32         NTSTATUS status;
33         bool ret = true;
34
35         status = torture_ldap_bind(conn, userdn, password);
36         if (!NT_STATUS_IS_OK(status)) {
37                 ret = false;
38         }
39
40         return ret;
41 }
42
43 static bool test_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
44 {
45         NTSTATUS status;
46         bool ret = true;
47
48         printf("Testing sasl bind as user\n");
49
50         status = torture_ldap_bind_sasl(conn, creds);
51         if (!NT_STATUS_IS_OK(status)) {
52                 ret = false;
53         }
54
55         return ret;
56 }
57
58 static bool test_multibind(struct ldap_connection *conn, const char *userdn, const char *password)
59 {
60         bool ret = true;
61
62         printf("Testing multiple binds on a single connnection as anonymous and user\n");
63
64         ret = test_bind_simple(conn, NULL, NULL);
65         if (!ret) {
66                 printf("1st bind as anonymous failed\n");
67                 return ret;
68         }
69
70         ret = test_bind_simple(conn, userdn, password);
71         if (!ret) {
72                 printf("2nd bind as authenticated user failed\n");
73         }
74
75         return ret;
76 }
77
78 static bool test_search_rootDSE(struct ldap_connection *conn, char **basedn)
79 {
80         bool ret = true;
81         struct ldap_message *msg, *result;
82         struct ldap_request *req;
83         int i;
84         struct ldap_SearchResEntry *r;
85         NTSTATUS status;
86
87         printf("Testing RootDSE Search\n");
88
89         *basedn = NULL;
90
91         msg = new_ldap_message(conn);
92         if (!msg) {
93                 return false;
94         }
95
96         msg->type = LDAP_TAG_SearchRequest;
97         msg->r.SearchRequest.basedn = "";
98         msg->r.SearchRequest.scope = LDAP_SEARCH_SCOPE_BASE;
99         msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
100         msg->r.SearchRequest.timelimit = 0;
101         msg->r.SearchRequest.sizelimit = 0;
102         msg->r.SearchRequest.attributesonly = false;
103         msg->r.SearchRequest.tree = ldb_parse_tree(msg, "(objectclass=*)");
104         msg->r.SearchRequest.num_attributes = 0;
105         msg->r.SearchRequest.attributes = NULL;
106
107         req = ldap_request_send(conn, msg);
108         if (req == NULL) {
109                 printf("Could not setup ldap search\n");
110                 return false;
111         }
112
113         status = ldap_result_one(req, &result, LDAP_TAG_SearchResultEntry);
114         if (!NT_STATUS_IS_OK(status)) {
115                 printf("search failed - %s\n", nt_errstr(status));
116                 return false;
117         }
118
119         printf("received %d replies\n", req->num_replies);
120
121         r = &result->r.SearchResultEntry;
122                 
123         DEBUG(1,("\tdn: %s\n", r->dn));
124         for (i=0; i<r->num_attributes; i++) {
125                 int j;
126                 for (j=0; j<r->attributes[i].num_values; j++) {
127                         DEBUG(1,("\t%s: %d %.*s\n", r->attributes[i].name,
128                                  (int)r->attributes[i].values[j].length,
129                                  (int)r->attributes[i].values[j].length,
130                                  (char *)r->attributes[i].values[j].data));
131                         if (!(*basedn) && 
132                             strcasecmp("defaultNamingContext",r->attributes[i].name)==0) {
133                                 *basedn = talloc_asprintf(conn, "%.*s",
134                                                           (int)r->attributes[i].values[j].length,
135                                                           (char *)r->attributes[i].values[j].data);
136                         }
137                 }
138         }
139
140         talloc_free(req);
141
142         return ret;
143 }
144
145 static bool test_compare_sasl(struct ldap_connection *conn, const char *basedn)
146 {
147         struct ldap_message *msg, *rep;
148         struct ldap_request *req;
149         const char *val;
150         NTSTATUS status;
151
152         printf("Testing SASL Compare: %s\n", basedn);
153
154         if (!basedn) {
155                 return false;
156         }
157
158         msg = new_ldap_message(conn);
159         if (!msg) {
160                 return false;
161         }
162
163         msg->type = LDAP_TAG_CompareRequest;
164         msg->r.CompareRequest.dn = basedn;
165         msg->r.CompareRequest.attribute = talloc_strdup(msg, "objectClass");
166         val = "domain";
167         msg->r.CompareRequest.value = data_blob_talloc(msg, val, strlen(val));
168
169         req = ldap_request_send(conn, msg);
170         if (!req) {
171                 return false;
172         }
173
174         status = ldap_result_one(req, &rep, LDAP_TAG_CompareResponse);
175         if (!NT_STATUS_IS_OK(status)) {
176                 printf("error in ldap compare request - %s\n", nt_errstr(status));
177                 return false;
178         }
179
180         DEBUG(5,("Code: %d DN: [%s] ERROR:[%s] REFERRAL:[%s]\n",
181                 rep->r.CompareResponse.resultcode,
182                 rep->r.CompareResponse.dn,
183                 rep->r.CompareResponse.errormessage,
184                 rep->r.CompareResponse.referral));
185
186         return true;
187 }
188
189
190 bool torture_ldap_basic(struct torture_context *torture)
191 {
192         NTSTATUS status;
193         struct ldap_connection *conn;
194         TALLOC_CTX *mem_ctx;
195         bool ret = true;
196         const char *host = torture_setting_string(torture, "host", NULL);
197         const char *userdn = torture_setting_string(torture, "ldap_userdn", NULL);
198         const char *secret = torture_setting_string(torture, "ldap_secret", NULL);
199         char *url;
200         char *basedn;
201
202         mem_ctx = talloc_init("torture_ldap_basic");
203
204         url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
205
206         status = torture_ldap_connection(mem_ctx, &conn, url);
207         if (!NT_STATUS_IS_OK(status)) {
208                 return false;
209         }
210
211         if (!test_search_rootDSE(conn, &basedn)) {
212                 ret = false;
213         }
214
215         /* other basic tests here */
216
217         if (!test_multibind(conn, userdn, secret)) {
218                 ret = false;
219         }
220
221         if (!test_bind_sasl(conn, cmdline_credentials)) {
222                 ret = false;
223         }
224
225         if (!test_compare_sasl(conn, basedn)) {
226                 ret = false;
227         }
228
229         /* no more test we are closing */
230         torture_ldap_close(conn);
231         talloc_free(mem_ctx);
232
233
234         return ret;
235 }
236