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.
26 BOOL test_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password)
31 status = torture_ldap_bind(conn, userdn, password);
32 if (!NT_STATUS_IS_OK(status)) {
39 BOOL test_bind_sasl(struct ldap_connection *conn, const char *username, const char *domain, const char *password)
44 printf("Testing sasl bind as user\n");
46 status = torture_ldap_bind_sasl(conn, username, domain, password);
47 if (!NT_STATUS_IS_OK(status)) {
54 BOOL test_multibind(struct ldap_connection *conn, const char *userdn, const char *password)
58 printf("Testing multiple binds on a single connnection as anonymous and user\n");
60 ret = test_bind_simple(conn, NULL, NULL);
62 printf("1st bind as anonymous failed\n");
66 ret = test_bind_simple(conn, userdn, password);
68 printf("2nd bind as authenticated user failed\n");
74 static BOOL test_search_rootDSE(struct ldap_connection *conn, char **basedn)
77 struct ldap_message *msg, *result;
79 printf("Testing RootDSE Search\n");
83 conn->next_msgid = 30;
85 msg = new_ldap_message(conn);
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;
101 if (!ldap_setsearchent(conn, msg, NULL)) {
102 printf("Could not setsearchent\n");
106 result = ldap_getsearchent(conn, NULL);
109 struct ldap_SearchResEntry *r = &result->r.SearchResultEntry;
111 DEBUG(1,("\tdn: %s\n", r->dn));
112 for (i=0; i<r->num_attributes; i++) {
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));
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);
131 ldap_endsearchent(conn, NULL);
136 static BOOL test_compare_sasl(struct ldap_connection *conn, const char *basedn)
139 struct ldap_message *msg, *result;
142 printf("Testing SASL Compare: %s\n", basedn);
148 conn->next_msgid = 55;
150 msg = new_ldap_message(conn);
155 msg->type = LDAP_TAG_CompareRequest;
156 msg->r.CompareRequest.dn = basedn;
157 msg->r.CompareRequest.attribute = talloc_strdup(msg->mem_ctx, "objectClass");
159 msg->r.CompareRequest.value = data_blob_talloc(msg->mem_ctx, val, strlen(val));
161 if (!ldap_sasl_send_msg(conn, msg, NULL)) {
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));
176 if (result->type != LDAP_TAG_CompareResponse) {
183 BOOL torture_ldap_basic(void)
186 struct ldap_connection *conn;
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");
199 mem_ctx = talloc_init("torture_ldap_basic");
201 url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
203 status = torture_ldap_connection(mem_ctx, &conn, url, userdn, secret);
204 if (!NT_STATUS_IS_OK(status)) {
208 /* other basic tests here */
210 if (!test_multibind(conn, userdn, secret)) {
214 if (!test_search_rootDSE(conn, &basedn)) {
218 if (!test_bind_sasl(conn, username, domain, password)) {
222 if (!test_compare_sasl(conn, basedn)) {
226 /* no more test we are closing */
228 talloc_destroy(mem_ctx);
230 torture_ldap_close(conn);