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