r12510: Change the DCE/RPC interfaces to take a pointer to a
[kai/samba.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 "asn_1.h"
26 #include "libcli/ldap/ldap.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 {
44         NTSTATUS status;
45
46         status = ldap_bind_sasl(conn, creds);
47         if (!NT_STATUS_IS_OK(status)) {
48                 printf("Failed sasl bind with provided credentials - %s\n", 
49                        nt_errstr(status));
50         }
51  
52         return status;
53 }
54
55 /* open a ldap connection to a server */
56 NTSTATUS torture_ldap_connection(TALLOC_CTX *mem_ctx, struct ldap_connection **conn, 
57                                 const char *url)
58 {
59         NTSTATUS status;
60
61         if (!url) {
62                 printf("You must specify a url string\n");
63                 return NT_STATUS_INVALID_PARAMETER;
64         }
65
66         *conn = ldap_new_connection(mem_ctx, NULL);
67
68         status = ldap_connect(*conn, url);
69         if (!NT_STATUS_IS_OK(status)) {
70                 printf("Failed to connect to ldap server '%s' - %s\n",
71                        url, nt_errstr(status));
72         }
73
74         return status;
75 }
76
77 /* open a ldap connection to a server */
78 NTSTATUS torture_ldap_connection2(TALLOC_CTX *mem_ctx, struct ldap_connection **conn, 
79                                 const char *url, const char *userdn, const char *password)
80 {
81         NTSTATUS status;
82
83         status = torture_ldap_connection(mem_ctx, conn, url);
84         NT_STATUS_NOT_OK_RETURN(status);
85
86         status = ldap_bind_simple(*conn, userdn, password);
87         if (!NT_STATUS_IS_OK(status)) {
88                 printf("Failed a simple ldap bind - %s\n", ldap_errstr(*conn, status));
89         }
90  
91         return status;
92 }
93
94 /* close an ldap connection to a server */
95 NTSTATUS torture_ldap_close(struct ldap_connection *conn)
96 {
97         talloc_free(conn);
98         return NT_STATUS_OK;
99 }