gensec: clarify memory ownership for gensec_session_info() and gensec_session_key()
[samba.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "ldap_server/ldap_server.h"
22 #include "auth/auth.h"
23 #include "smbd/service.h"
24 #include <ldb.h>
25 #include <ldb_errors.h>
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/gensec/gensec_tstream.h"
29 #include "param/param.h"
30 #include "../lib/util/tevent_ntstatus.h"
31
32 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
33 {
34         struct ldap_BindRequest *req = &call->request->r.BindRequest;
35         struct ldapsrv_reply *reply;
36         struct ldap_BindResponse *resp;
37
38         int result;
39         const char *errstr;
40         const char *nt4_domain, *nt4_account;
41
42         struct auth_session_info *session_info;
43
44         NTSTATUS status;
45
46         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
47
48         status = crack_auto_name_to_nt4_name(call, call->conn->connection->event.ctx, call->conn->lp_ctx, req->dn, &nt4_domain, &nt4_account);
49         if (NT_STATUS_IS_OK(status)) {
50                 status = authenticate_username_pw(call,
51                                                   call->conn->connection->event.ctx,
52                                                   call->conn->connection->msg_ctx,
53                                                   call->conn->lp_ctx,
54                                                   nt4_domain, nt4_account, 
55                                                   req->creds.password,
56                                                   MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
57                                                   MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
58                                                   &session_info);
59         }
60
61         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
62         if (!reply) {
63                 return NT_STATUS_NO_MEMORY;
64         }
65
66         if (NT_STATUS_IS_OK(status)) {
67                 result = LDAP_SUCCESS;
68                 errstr = NULL;
69
70                 talloc_unlink(call->conn, call->conn->session_info);
71                 call->conn->session_info = talloc_steal(call->conn, session_info);
72
73                 /* don't leak the old LDB */
74                 talloc_unlink(call->conn, call->conn->ldb);
75
76                 status = ldapsrv_backend_Init(call->conn);              
77                 
78                 if (!NT_STATUS_IS_OK(status)) {
79                         result = LDAP_OPERATIONS_ERROR;
80                         errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
81                 }
82         } else {
83                 status = nt_status_squash(status);
84
85                 result = LDAP_INVALID_CREDENTIALS;
86                 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
87         }
88
89         resp = &reply->msg->r.BindResponse;
90         resp->response.resultcode = result;
91         resp->response.errormessage = errstr;
92         resp->response.dn = NULL;
93         resp->response.referral = NULL;
94         resp->SASL.secblob = NULL;
95
96         ldapsrv_queue_reply(call, reply);
97         return NT_STATUS_OK;
98 }
99
100 struct ldapsrv_sasl_postprocess_context {
101         struct ldapsrv_connection *conn;
102         struct tstream_context *sasl;
103 };
104
105 struct ldapsrv_sasl_postprocess_state {
106         uint8_t dummy;
107 };
108
109 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
110                                                 struct tevent_context *ev,
111                                                 void *private_data)
112 {
113         struct ldapsrv_sasl_postprocess_context *context =
114                 talloc_get_type_abort(private_data,
115                 struct ldapsrv_sasl_postprocess_context);
116         struct tevent_req *req;
117         struct ldapsrv_sasl_postprocess_state *state;
118
119         req = tevent_req_create(mem_ctx, &state,
120                                 struct ldapsrv_sasl_postprocess_state);
121         if (req == NULL) {
122                 return NULL;
123         }
124
125         TALLOC_FREE(context->conn->sockets.sasl);
126         context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
127         context->conn->sockets.active = context->conn->sockets.sasl;
128
129         tevent_req_done(req);
130         return tevent_req_post(req, ev);
131 }
132
133 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
134 {
135         return tevent_req_simple_recv_ntstatus(req);
136 }
137
138 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
139 {
140         struct ldap_BindRequest *req = &call->request->r.BindRequest;
141         struct ldapsrv_reply *reply;
142         struct ldap_BindResponse *resp;
143         struct ldapsrv_connection *conn;
144         int result = 0;
145         const char *errstr=NULL;
146         NTSTATUS status = NT_STATUS_OK;
147
148         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
149
150         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
151         if (!reply) {
152                 return NT_STATUS_NO_MEMORY;
153         }
154         resp = &reply->msg->r.BindResponse;
155         
156         conn = call->conn;
157
158         /* 
159          * TODO: a SASL bind with a different mechanism
160          *       should cancel an inprogress SASL bind.
161          *       (see RFC 4513)
162          */
163
164         if (!conn->gensec) {
165                 conn->session_info = NULL;
166
167                 status = samba_server_gensec_start(conn,
168                                                    conn->connection->event.ctx,
169                                                    conn->connection->msg_ctx,
170                                                    conn->lp_ctx,
171                                                    conn->server_credentials,
172                                                    "ldap",
173                                                    &conn->gensec);
174                 if (!NT_STATUS_IS_OK(status)) {
175                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
176                         result = LDAP_OPERATIONS_ERROR;
177                         errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s", 
178                                                  nt_errstr(status));
179                 } else {
180
181                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
182                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
183                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
184                         
185                         status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
186                         
187                         if (!NT_STATUS_IS_OK(status)) {
188                                 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
189                                           req->creds.SASL.mechanism, nt_errstr(status)));
190                                 result = LDAP_OPERATIONS_ERROR;
191                                 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s", 
192                                                          req->creds.SASL.mechanism, nt_errstr(status));
193                         }
194                 }
195         }
196
197         if (NT_STATUS_IS_OK(status)) {
198                 DATA_BLOB input = data_blob(NULL, 0);
199                 DATA_BLOB output = data_blob(NULL, 0);
200
201                 if (req->creds.SASL.secblob) {
202                         input = *req->creds.SASL.secblob;
203                 }
204
205                 status = gensec_update(conn->gensec, reply,
206                                        input, &output);
207
208                 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
209                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
210                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
211                 *resp->SASL.secblob = output;
212         } else {
213                 resp->SASL.secblob = NULL;
214         }
215
216         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
217                 result = LDAP_SASL_BIND_IN_PROGRESS;
218                 errstr = NULL;
219         } else if (NT_STATUS_IS_OK(status)) {
220                 struct auth_session_info *old_session_info=NULL;
221                 struct ldapsrv_sasl_postprocess_context *context = NULL;
222
223                 result = LDAP_SUCCESS;
224                 errstr = NULL;
225
226                 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
227                     gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
228
229                         context = talloc(call, struct ldapsrv_sasl_postprocess_context);
230
231                         if (!context) {
232                                 status = NT_STATUS_NO_MEMORY;
233                         }
234                 }
235
236                 if (context && conn->sockets.tls) {
237                         TALLOC_FREE(context);
238                         status = NT_STATUS_NOT_SUPPORTED;
239                         result = LDAP_UNWILLING_TO_PERFORM;
240                         errstr = talloc_asprintf(reply,
241                                                  "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
242                                                  req->creds.SASL.mechanism);
243                 }
244
245                 if (context && conn->sockets.sasl) {
246                         TALLOC_FREE(context);
247                         status = NT_STATUS_NOT_SUPPORTED;
248                         result = LDAP_UNWILLING_TO_PERFORM;
249                         errstr = talloc_asprintf(reply,
250                                                  "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
251                                                  req->creds.SASL.mechanism);
252                 }
253
254                 if (context) {
255                         context->conn = conn;
256                         status = gensec_create_tstream(context,
257                                                        context->conn->gensec,
258                                                        context->conn->sockets.raw,
259                                                        &context->sasl);
260                         if (NT_STATUS_IS_OK(status)) {
261                                 if (!talloc_reference(context->sasl, conn->gensec)) {
262                                         status = NT_STATUS_NO_MEMORY;
263                                 }
264                         }
265                 }
266
267                 if (result != LDAP_SUCCESS) {
268                         conn->session_info = old_session_info;
269                 } else if (!NT_STATUS_IS_OK(status)) {
270                         conn->session_info = old_session_info;
271                         result = LDAP_OPERATIONS_ERROR;
272                         errstr = talloc_asprintf(reply, 
273                                                  "SASL:[%s]: Failed to setup SASL socket: %s", 
274                                                  req->creds.SASL.mechanism, nt_errstr(status));
275                 } else {
276
277                         old_session_info = conn->session_info;
278                         conn->session_info = NULL;
279                         status = gensec_session_info(conn->gensec, conn, &conn->session_info);
280                         if (!NT_STATUS_IS_OK(status)) {
281                                 conn->session_info = old_session_info;
282                                 result = LDAP_OPERATIONS_ERROR;
283                                 errstr = talloc_asprintf(reply, 
284                                                          "SASL:[%s]: Failed to get session info: %s", 
285                                                          req->creds.SASL.mechanism, nt_errstr(status));
286                         } else {
287                                 talloc_unlink(conn, old_session_info);
288                                 
289                                 /* don't leak the old LDB */
290                                 talloc_unlink(conn, conn->ldb);
291                                 
292                                 status = ldapsrv_backend_Init(conn);            
293                                 
294                                 if (!NT_STATUS_IS_OK(status)) {
295                                         result = LDAP_OPERATIONS_ERROR;
296                                         errstr = talloc_asprintf(reply, 
297                                                                  "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
298                                                                  req->creds.SASL.mechanism, 
299                                                                  nt_errstr(status));
300                                 }
301                         }
302                 }
303
304                 if (NT_STATUS_IS_OK(status) && context) {
305                         call->postprocess_send = ldapsrv_sasl_postprocess_send;
306                         call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
307                         call->postprocess_private = context;
308                 }
309                 talloc_unlink(conn, conn->gensec);
310                 conn->gensec = NULL;
311         } else {
312                 status = nt_status_squash(status);
313                 if (result == 0) {
314                         result = LDAP_INVALID_CREDENTIALS;
315                         errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
316                 }
317                 talloc_unlink(conn, conn->gensec);
318                 conn->gensec = NULL;
319         }
320
321         resp->response.resultcode = result;
322         resp->response.dn = NULL;
323         resp->response.errormessage = errstr;
324         resp->response.referral = NULL;
325
326         ldapsrv_queue_reply(call, reply);
327         return NT_STATUS_OK;
328 }
329
330 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
331 {
332         struct ldap_BindRequest *req = &call->request->r.BindRequest;
333         struct ldapsrv_reply *reply;
334         struct ldap_BindResponse *resp;
335
336         /* 
337          * TODO: we should fail the bind request
338          *       if there're any pending requests.
339          *
340          *       also a simple bind should cancel an
341          *       inprogress SASL bind.
342          *       (see RFC 4513)
343          */
344         switch (req->mechanism) {
345                 case LDAP_AUTH_MECH_SIMPLE:
346                         return ldapsrv_BindSimple(call);
347                 case LDAP_AUTH_MECH_SASL:
348                         return ldapsrv_BindSASL(call);
349         }
350
351         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
352         if (!reply) {
353                 return NT_STATUS_NO_MEMORY;
354         }
355
356         resp = &reply->msg->r.BindResponse;
357         resp->response.resultcode = 7;
358         resp->response.dn = NULL;
359         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
360         resp->response.referral = NULL;
361         resp->SASL.secblob = NULL;
362
363         ldapsrv_queue_reply(call, reply);
364         return NT_STATUS_OK;
365 }
366
367 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
368 {
369         DEBUG(10, ("UnbindRequest\n"));
370         return NT_STATUS_OK;
371 }