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 #include "libcli/ldap/ldap.h"
28 NTSTATUS torture_ldap_bind(struct ldap_connection *conn, const char *userdn, const char *password)
30 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
34 printf("We need a valid ldap_connection structure and be connected\n");
38 result = ldap_bind_simple(conn, userdn, password);
39 if (result != LDAP_SUCCESS) {
40 printf("Failed to bind with provided credentials\n");
41 /* FIXME: what abut actually implementing an ldap_connection_free() function ?
49 NTSTATUS torture_ldap_bind_sasl(struct ldap_connection *conn, const char *username, const char *domain, const char *password)
51 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
55 printf("We need a valid ldap_connection structure and be connected\n");
59 result = ldap_bind_sasl(conn, username, domain, password);
60 if (result != LDAP_SUCCESS) {
61 printf("Failed to bind with provided credentials and SASL mechanism\n");
62 /* FIXME: what abut actually implementing an ldap_connection_free() function ?
70 /* open a ldap connection to a server */
71 NTSTATUS torture_ldap_connection(TALLOC_CTX *mem_ctx, struct ldap_connection **conn,
72 const char *url, const char *userdn, const char *password)
74 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
78 printf("You must specify a url string\n");
79 return NT_STATUS_INVALID_PARAMETER;
82 *conn = ldap_connect(mem_ctx, url);
84 printf("Failed to initialize ldap_connection structure\n");
88 ret = ldap_bind_simple(*conn, userdn, password);
89 if (ret != LDAP_SUCCESS) {
90 printf("Failed to connect with url [%s]\n", url);
91 /* FIXME: what abut actually implementing an ldap_connection_free() function ?
99 /* close an ldap connection to a server */
100 NTSTATUS torture_ldap_close(struct ldap_connection *conn)
102 /* FIXME: what about actually implementing ldap_close() ?
111 static ssize_t write_data(int fd, char *buffer, size_t N)
117 ret = sys_write(fd,buffer + total,N - total);
120 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
129 return (ssize_t)total;
134 Read data from the client, reading exactly N bytes
136 static ssize_t read_data(int fd, char *buffer, size_t N)
143 ret = sys_read(fd,buffer + total,N - total);
146 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n",
147 (int)(N - total), strerror(errno) ));
152 DEBUG(0,("read_data: read failure for %d. Error = %s\n",
153 (int)(N - total), strerror(errno) ));
159 return (ssize_t)total;
162 BOOL ldap_sasl_send_msg(struct ldap_connection *conn, struct ldap_message *msg,
163 const struct timeval *endtime)
171 struct asn1_data asn1;
174 msg->messageid = conn->next_msgid++;
176 if (!ldap_encode(msg, &request))
179 status = gensec_wrap(conn->gensec,
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(0,("gensec_wrap: %s\n",nt_errstr(status)));
188 RSIVAL(length, 0, wrapped.length);
190 result = (write_data(conn->sock, length, 4) == 4);
194 result = (write_data(conn->sock, wrapped.data, wrapped.length) == wrapped.length);
198 wrapped = data_blob(NULL, 0x4000);
199 data_blob_clear(&wrapped);
201 result = (read_data(conn->sock, length, 4) == 4);
205 len = RIVAL(length,0);
207 result = (read_data(conn->sock, wrapped.data, MIN(wrapped.length,len)) == len);
211 wrapped.length = len;
213 status = gensec_unwrap(conn->gensec,
217 if (!NT_STATUS_IS_OK(status)) {
218 DEBUG(0,("gensec_unwrap: %s\n",nt_errstr(status)));
222 mem_ctx = msg->mem_ctx;
224 msg->mem_ctx = mem_ctx;
226 asn1_load(&asn1, request);
227 if (!ldap_decode(&asn1, msg)) {