2 Unix SMB/CIFS mplementation.
3 LDAP protocol helper functions for SAMBA
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2004
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.
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.
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.
25 #include "libcli/ldap/ldap.h"
26 #include "lib/cmdline/popt_common.h"
28 BOOL test_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password)
33 status = torture_ldap_bind(conn, userdn, password);
34 if (!NT_STATUS_IS_OK(status)) {
41 BOOL test_bind_sasl(struct ldap_connection *conn, const char *username, const char *domain, const char *password)
46 printf("Testing sasl bind as user\n");
48 status = torture_ldap_bind_sasl(conn, username, domain, password);
49 if (!NT_STATUS_IS_OK(status)) {
56 BOOL test_multibind(struct ldap_connection *conn, const char *userdn, const char *password)
60 printf("Testing multiple binds on a single connnection as anonymous and user\n");
62 ret = test_bind_simple(conn, NULL, NULL);
64 printf("1st bind as anonymous failed\n");
68 ret = test_bind_simple(conn, userdn, password);
70 printf("2nd bind as authenticated user failed\n");
76 static BOOL test_search_rootDSE(struct ldap_connection *conn, char **basedn)
79 struct ldap_message *msg, *result;
81 printf("Testing RootDSE Search\n");
85 conn->next_msgid = 30;
87 msg = new_ldap_message(conn);
92 msg->type = LDAP_TAG_SearchRequest;
93 msg->r.SearchRequest.basedn = "";
94 msg->r.SearchRequest.scope = LDAP_SEARCH_SCOPE_BASE;
95 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
96 msg->r.SearchRequest.timelimit = 0;
97 msg->r.SearchRequest.sizelimit = 0;
98 msg->r.SearchRequest.attributesonly = False;
99 msg->r.SearchRequest.filter = talloc_strdup(msg->mem_ctx, "(objectclass=*)");
100 msg->r.SearchRequest.num_attributes = 0;
101 msg->r.SearchRequest.attributes = NULL;
103 if (!ldap_setsearchent(conn, msg, NULL)) {
104 printf("Could not setsearchent\n");
108 result = ldap_getsearchent(conn, NULL);
111 struct ldap_SearchResEntry *r = &result->r.SearchResultEntry;
113 DEBUG(1,("\tdn: %s\n", r->dn));
114 for (i=0; i<r->num_attributes; i++) {
116 for (j=0; j<r->attributes[i].num_values; j++) {
117 DEBUG(1,("\t%s: %d %.*s\n", r->attributes[i].name,
118 r->attributes[i].values[j].length,
119 r->attributes[i].values[j].length,
120 (char *)r->attributes[i].values[j].data));
122 strcasecmp("defaultNamingContext",r->attributes[i].name)==0) {
123 *basedn = talloc_asprintf(conn->mem_ctx, "%.*s",
124 r->attributes[i].values[j].length,
125 (char *)r->attributes[i].values[j].data);
133 ldap_endsearchent(conn, NULL);
138 static BOOL test_compare_sasl(struct ldap_connection *conn, const char *basedn)
141 struct ldap_message *msg, *result;
144 printf("Testing SASL Compare: %s\n", basedn);
150 conn->next_msgid = 55;
152 msg = new_ldap_message(conn);
157 msg->type = LDAP_TAG_CompareRequest;
158 msg->r.CompareRequest.dn = basedn;
159 msg->r.CompareRequest.attribute = talloc_strdup(msg->mem_ctx, "objectClass");
161 msg->r.CompareRequest.value = data_blob_talloc(msg->mem_ctx, val, strlen(val));
163 if (!ldap_sasl_send_msg(conn, msg, NULL)) {
167 DEBUG(5,("Code: %d DN: [%s] ERROR:[%s] REFERRAL:[%s]\n",
168 msg->r.CompareResponse.resultcode,
169 msg->r.CompareResponse.dn,
170 msg->r.CompareResponse.errormessage,
171 msg->r.CompareResponse.referral));
178 if (result->type != LDAP_TAG_CompareResponse) {
185 BOOL torture_ldap_basic(void)
188 struct ldap_connection *conn;
191 const char *host = lp_parm_string(-1, "torture", "host");
192 const char *username = cli_credentials_get_username(cmdline_credentials);
193 const char *domain = cli_credentials_get_domain(cmdline_credentials);
194 const char *password = cli_credentials_get_password(cmdline_credentials);
195 const char *userdn = lp_parm_string(-1, "torture", "ldap_userdn");
196 /*const char *basedn = lp_parm_string(-1, "torture", "ldap_basedn");*/
197 const char *secret = lp_parm_string(-1, "torture", "ldap_secret");
201 mem_ctx = talloc_init("torture_ldap_basic");
203 url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
205 status = torture_ldap_connection(mem_ctx, &conn, url, userdn, secret);
206 if (!NT_STATUS_IS_OK(status)) {
210 /* other basic tests here */
212 if (!test_multibind(conn, userdn, secret)) {
216 if (!test_search_rootDSE(conn, &basedn)) {
220 if (!test_bind_sasl(conn, username, domain, password)) {
224 if (!test_compare_sasl(conn, basedn)) {
228 /* no more test we are closing */
230 talloc_free(mem_ctx);
232 torture_ldap_close(conn);