r1855: fix compiler warning and output fromatting
[sfrench/samba-autobuild/.git] / source4 / torture / ldap / basic.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
26 BOOL test_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password)
27 {
28         NTSTATUS status;
29         BOOL ret = True;
30
31         status = torture_ldap_bind(conn, userdn, password);
32         if (!NT_STATUS_IS_OK(status)) {
33                 ret = False;
34         }
35
36         return ret;
37 }
38
39 BOOL test_bind_sasl(struct ldap_connection *conn, const char *username, const char *domain, const char *password)
40 {
41         NTSTATUS status;
42         BOOL ret = True;
43
44         printf("Testing sasl bind as user\n");
45
46         status = torture_ldap_bind_sasl(conn, username, domain, password);
47         if (!NT_STATUS_IS_OK(status)) {
48                 ret = False;
49         }
50
51         return ret;
52 }
53
54 BOOL test_multibind(struct ldap_connection *conn, const char *userdn, const char *password)
55 {
56         BOOL ret = True;
57
58         printf("Testing multiple binds on a single connnection as anonymous and user\n");
59
60         ret = test_bind_simple(conn, NULL, NULL);
61         if (!ret) {
62                 printf("1st bind as anonymous failed\n");
63                 return ret;
64         }
65
66         ret = test_bind_simple(conn, userdn, password);
67         if (!ret) {
68                 printf("2nd bind as authenticated user failed\n");
69         }
70
71         return ret;
72 }
73
74 BOOL torture_ldap_basic(int dummy)
75 {
76         NTSTATUS status;
77         struct ldap_connection *conn;
78         TALLOC_CTX *mem_ctx;
79         BOOL ret = True;
80         const char *host = lp_parm_string(-1, "torture", "host");
81         const char *username = lp_parm_string(-1, "torture", "username");
82         const char *domain = lp_workgroup();
83         const char *password = lp_parm_string(-1, "torture", "password");
84         const char *userdn = lp_parm_string(-1, "torture", "ldap_userdn");
85         /*const char *basedn = lp_parm_string(-1, "torture", "ldap_basedn");*/
86         const char *secret = lp_parm_string(-1, "torture", "ldap_secret");
87         char *url;
88
89         mem_ctx = talloc_init("torture_ldap_basic");
90
91         url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
92
93         status = torture_ldap_connection(&conn, url, userdn, secret);
94         if (!NT_STATUS_IS_OK(status)) {
95                 return False;
96         }
97
98         /* other basic tests here */
99
100         if (!test_multibind(conn, userdn, secret)) {
101                 ret = False;
102         }
103
104         if (!test_bind_sasl(conn, username, domain, password)) {
105                 ret = False;
106         }
107
108         /* no more test we are closing */
109
110         talloc_destroy(mem_ctx);
111
112         torture_ldap_close(conn);
113
114         return ret;
115 }
116