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