r12880: Remove ldap partitions useless now and probably we
[samba.git] / source / ldap_server / ldap_bind.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "ldap_server/ldap_server.h"
23 #include "auth/auth.h"
24 #include "libcli/ldap/ldap.h"
25 #include "smbd/service_stream.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "dsdb/samdb/samdb.h"
29
30 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
31 {
32         struct ldap_BindRequest *req = &call->request->r.BindRequest;
33         struct ldapsrv_reply *reply;
34         struct ldap_BindResponse *resp;
35
36         int result;
37         const char *errstr;
38         const char *nt4_domain, *nt4_account;
39
40         struct auth_session_info *session_info;
41
42         NTSTATUS status;
43
44         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
45
46         status = crack_dn_to_nt4_name(call, req->dn, &nt4_domain, &nt4_account);
47         if (NT_STATUS_IS_OK(status)) {
48                 status = authenticate_username_pw(call, nt4_domain, nt4_account, 
49                                                   req->creds.password, &session_info);
50         }
51
52         /* When we add authentication here, we also need to handle telling the backends */
53
54         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
55         if (!reply) {
56                 return NT_STATUS_NO_MEMORY;
57         }
58
59         if (NT_STATUS_IS_OK(status)) {
60                 result = LDAP_SUCCESS;
61                 errstr = NULL;
62
63                 talloc_free(call->conn->session_info);
64                 call->conn->session_info = session_info;
65
66                 /* don't leak the old LDB */
67                 talloc_free(call->conn->ldb);
68
69                 status = ldapsrv_backend_Init(call->conn);              
70                 
71                 if (!NT_STATUS_IS_OK(status)) {
72                         result = LDAP_OPERATIONS_ERROR;
73                         errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
74                 }
75         } else {
76                 status = auth_nt_status_squash(status);
77
78                 result = LDAP_INVALID_CREDENTIALS;
79                 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
80         }
81
82         resp = &reply->msg->r.BindResponse;
83         resp->response.resultcode = result;
84         resp->response.errormessage = errstr;
85         resp->response.dn = NULL;
86         resp->response.referral = NULL;
87
88         /* This looks wrong... */
89         resp->SASL.secblob = data_blob(NULL, 0);
90
91         ldapsrv_queue_reply(call, reply);
92         return NT_STATUS_OK;
93 }
94
95 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
96 {
97         struct ldap_BindRequest *req = &call->request->r.BindRequest;
98         struct ldapsrv_reply *reply;
99         struct ldap_BindResponse *resp;
100         struct ldapsrv_connection *conn;
101         int result;
102         const char *errstr;
103         NTSTATUS status = NT_STATUS_OK;
104
105         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
106
107         if (!call->conn->gensec) {
108                 call->conn->session_info = NULL;
109
110                 status = gensec_server_start(call->conn, &call->conn->gensec,
111                                              call->conn->connection->event.ctx);
112                 if (!NT_STATUS_IS_OK(status)) {
113                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
114                         return status;
115                 }
116                 
117                 gensec_set_target_service(call->conn->gensec, "ldap");
118
119                 gensec_set_credentials(call->conn->gensec, call->conn->server_credentials);
120
121                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
122                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
123                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
124
125                 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
126                 if (!NT_STATUS_IS_OK(status)) {
127                         DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
128                                 req->creds.SASL.mechanism, nt_errstr(status)));
129                 }
130         }
131
132         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
133         if (!reply) {
134                 return NT_STATUS_NO_MEMORY;
135         }
136         resp = &reply->msg->r.BindResponse;
137         
138         conn = call->conn;
139
140         if (NT_STATUS_IS_OK(status)) {
141                 status = gensec_update(call->conn->gensec, reply,
142                                        req->creds.SASL.secblob, &resp->SASL.secblob);
143         }
144
145         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
146                 result = LDAP_SASL_BIND_IN_PROGRESS;
147                 errstr = NULL;
148         } else if (NT_STATUS_IS_OK(status)) {
149                 struct auth_session_info *old_session_info;
150
151                 result = LDAP_SUCCESS;
152                 errstr = NULL;
153                 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
154                     gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
155                         call->conn->enable_wrap = True;
156                 }
157                 old_session_info = call->conn->session_info;
158                 call->conn->session_info = NULL;
159                 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
160                 if (!NT_STATUS_IS_OK(status)) {
161                         call->conn->session_info = old_session_info;
162                         result = LDAP_OPERATIONS_ERROR;
163                         errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
164                 } else {
165                         talloc_free(old_session_info);
166
167                         /* don't leak the old LDB */
168                         talloc_free(call->conn->ldb);
169
170                         status = ldapsrv_backend_Init(call->conn);              
171                         
172                         if (!NT_STATUS_IS_OK(status)) {
173                                 result = LDAP_OPERATIONS_ERROR;
174                                 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to advise samdb of new credentials: %s", req->creds.SASL.mechanism, nt_errstr(status));
175                         }
176                 }
177         } else {
178                 status = auth_nt_status_squash(status);
179                 result = LDAP_INVALID_CREDENTIALS;
180                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
181         }
182
183         resp->response.resultcode = result;
184         resp->response.dn = NULL;
185         resp->response.errormessage = errstr;
186         resp->response.referral = NULL;
187
188         ldapsrv_queue_reply(call, reply);
189         return NT_STATUS_OK;
190 }
191
192 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
193 {
194         struct ldap_BindRequest *req = &call->request->r.BindRequest;
195         struct ldapsrv_reply *reply;
196         struct ldap_BindResponse *resp;
197
198         switch (req->mechanism) {
199                 case LDAP_AUTH_MECH_SIMPLE:
200                         return ldapsrv_BindSimple(call);
201                 case LDAP_AUTH_MECH_SASL:
202                         return ldapsrv_BindSASL(call);
203         }
204
205         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
206         if (!reply) {
207                 return NT_STATUS_NO_MEMORY;
208         }
209
210         resp = &reply->msg->r.BindResponse;
211         resp->response.resultcode = 7;
212         resp->response.dn = NULL;
213         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
214         resp->response.referral = NULL;
215         resp->SASL.secblob = data_blob(NULL, 0);
216
217         ldapsrv_queue_reply(call, reply);
218         return NT_STATUS_OK;
219 }
220
221 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
222 {
223         DEBUG(10, ("UnbindRequest\n"));
224         return NT_STATUS_OK;
225 }