8c0eb02f042784b076ac4007ffd9acc5c76821f7
[kai/samba-autobuild/.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 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 "torture/smbtorture.h"
26 #include "torture/ldap/proto.h"
27
28 NTSTATUS torture_ldap_bind(struct ldap_connection *conn, const char *userdn, const char *password)
29 {
30         NTSTATUS status;
31
32         status = ldap_bind_simple(conn, userdn, password);
33         if (!NT_STATUS_IS_OK(status)) {
34                 printf("Failed to bind with provided credentials - %s\n", 
35                        nt_errstr(status));
36         }
37
38         return status;
39 }
40
41 NTSTATUS torture_ldap_bind_sasl(struct ldap_connection *conn, 
42                                 struct cli_credentials *creds, 
43                                 struct loadparm_context *lp_ctx)
44 {
45         NTSTATUS status;
46
47         status = ldap_bind_sasl(conn, creds, lp_ctx);
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 NTSTATUS torture_ldap_connection(struct torture_context *tctx, 
58                                           struct ldap_connection **conn, 
59                                           const char *url)
60 {
61         NTSTATUS status;
62
63         if (!url) {
64                 printf("You must specify a url string\n");
65                 return NT_STATUS_INVALID_PARAMETER;
66         }
67
68         *conn = ldap4_new_connection(tctx, tctx->lp_ctx, tctx->ev);
69
70         status = ldap_connect(*conn, url);
71         if (!NT_STATUS_IS_OK(status)) {
72                 printf("Failed to connect to ldap server '%s' - %s\n",
73                        url, nt_errstr(status));
74         }
75
76         return status;
77 }
78
79 /* open a ldap connection to a server */
80 NTSTATUS torture_ldap_connection2(struct torture_context *tctx, struct ldap_connection **conn, 
81                                 const char *url, const char *userdn, const char *password)
82 {
83         NTSTATUS status;
84
85         status = torture_ldap_connection(tctx, conn, url);
86         NT_STATUS_NOT_OK_RETURN(status);
87
88         status = ldap_bind_simple(*conn, userdn, password);
89         if (!NT_STATUS_IS_OK(status)) {
90                 printf("Failed a simple ldap bind - %s\n", ldap_errstr(*conn, tctx, status));
91         }
92  
93         return status;
94 }
95
96 /* close an ldap connection to a server */
97 NTSTATUS torture_ldap_close(struct ldap_connection *conn)
98 {
99         struct ldap_message *msg;
100         struct ldap_request *req;
101         NTSTATUS status;
102
103         printf("Testing the most important error code -> error message conversions!\n");
104
105         msg = new_ldap_message(conn);
106         if (!msg) {
107                 talloc_free(conn);
108                 return NT_STATUS_NO_MEMORY;
109         }
110
111         printf(" Try a AbandonRequest for an old message id\n");
112
113         msg->type = LDAP_TAG_UnbindRequest;
114
115         req = ldap_request_send(conn, msg);
116         if (!req) {
117                 talloc_free(conn);
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         status = ldap_request_wait(req);
122         if (!NT_STATUS_IS_OK(status)) {
123                 printf("error in ldap unbind request - %s\n", nt_errstr(status));
124                 talloc_free(conn);
125                 return status;
126         }
127
128         talloc_free(conn);
129         return NT_STATUS_OK;
130 }
131
132 NTSTATUS torture_ldap_init(void)
133 {
134         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "LDAP");
135         torture_suite_add_simple_test(suite, "BENCH-CLDAP", torture_bench_cldap);
136         torture_suite_add_simple_test(suite, "BASIC", torture_ldap_basic);
137         torture_suite_add_simple_test(suite, "SORT", torture_ldap_sort);
138         torture_suite_add_simple_test(suite, "CLDAP", torture_cldap);
139         torture_suite_add_simple_test(suite, "SCHEMA", torture_ldap_schema);
140         torture_suite_add_simple_test(suite, "UPTODATEVECTOR", torture_ldap_uptodatevector);
141         torture_suite_add_simple_test(suite, "NESTED-SEARCH", test_ldap_nested_search);
142
143         suite->description = talloc_strdup(suite, "LDAP and CLDAP tests");
144
145         torture_register_suite(suite);
146
147         return NT_STATUS_OK;
148 }