~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source4/torture/ldap/basic.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

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