r19507: Merge my DSO fixes branch. Building Samba's libraries as shared libraries
[ira/wip.git] / source4 / torture / ldap / common.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 #include "libcli/ldap/ldap.h"
26 #include "torture/torture.h"
27 #include "torture/ldap/proto.h"
28
29 NTSTATUS torture_ldap_bind(struct ldap_connection *conn, const char *userdn, const char *password)
30 {
31         NTSTATUS status;
32
33         status = ldap_bind_simple(conn, userdn, password);
34         if (!NT_STATUS_IS_OK(status)) {
35                 printf("Failed to bind with provided credentials - %s\n", 
36                        nt_errstr(status));
37         }
38
39         return status;
40 }
41
42 _PUBLIC_ NTSTATUS torture_ldap_bind_sasl(struct ldap_connection *conn, 
43                                 struct cli_credentials *creds)
44 {
45         NTSTATUS status;
46
47         status = ldap_bind_sasl(conn, creds);
48         if (!NT_STATUS_IS_OK(status)) {
49                 printf("Failed sasl bind with provided credentials - %s\n", 
50                        nt_errstr(status));
51         }
52  
53         return status;
54 }
55
56 /* open a ldap connection to a server */
57 _PUBLIC_ NTSTATUS torture_ldap_connection(TALLOC_CTX *mem_ctx, struct ldap_connection **conn, 
58                                 const char *url)
59 {
60         NTSTATUS status;
61
62         if (!url) {
63                 printf("You must specify a url string\n");
64                 return NT_STATUS_INVALID_PARAMETER;
65         }
66
67         *conn = ldap4_new_connection(mem_ctx, NULL);
68
69         status = ldap_connect(*conn, url);
70         if (!NT_STATUS_IS_OK(status)) {
71                 printf("Failed to connect to ldap server '%s' - %s\n",
72                        url, nt_errstr(status));
73         }
74
75         return status;
76 }
77
78 /* open a ldap connection to a server */
79 NTSTATUS torture_ldap_connection2(TALLOC_CTX *mem_ctx, struct ldap_connection **conn, 
80                                 const char *url, const char *userdn, const char *password)
81 {
82         NTSTATUS status;
83
84         status = torture_ldap_connection(mem_ctx, conn, url);
85         NT_STATUS_NOT_OK_RETURN(status);
86
87         status = ldap_bind_simple(*conn, userdn, password);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("Failed a simple ldap bind - %s\n", ldap_errstr(*conn, status));
90         }
91  
92         return status;
93 }
94
95 /* close an ldap connection to a server */
96 NTSTATUS torture_ldap_close(struct ldap_connection *conn)
97 {
98         talloc_free(conn);
99         return NT_STATUS_OK;
100 }
101
102 NTSTATUS torture_ldap_init(void)
103 {
104         struct torture_suite *suite = torture_suite_create(
105                                                                                 talloc_autofree_context(),
106                                                                                 "LDAP");
107         torture_suite_add_simple_test(suite, "BENCH-CLDAP", 
108                                                                   torture_bench_cldap);
109         torture_suite_add_simple_test(suite, "BASIC", torture_ldap_basic);
110         torture_suite_add_simple_test(suite, "CLDAP", torture_cldap);
111         torture_suite_add_simple_test(suite, "SCHEMA", torture_ldap_schema);
112
113         suite->description = talloc_strdup(
114                                                         suite, "LDAP and CLDAP tests");
115
116         torture_register_suite(suite);
117
118         return NT_STATUS_OK;
119 }