s4-ldap_server: Set remote and local address values into GENSEC
[metze/samba/wip.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 char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
33                                     HRESULT hresult,
34                                     uint32_t DSID,
35                                     NTSTATUS status)
36 {
37         WERROR werr;
38         char *msg = NULL;
39
40         status = nt_status_squash(status);
41         werr = ntstatus_to_werror(status);
42
43         /*
44          * There are 4 lower case hex digits following 'v' at the end,
45          * but different Windows Versions return different values:
46          *
47          * Windows 2008R2 uses 'v1db1'
48          * Windows 2012R2 uses 'v2580'
49          *
50          * We just match Windows 2008R2 as that's what was referenced
51          * in https://bugzilla.samba.org/show_bug.cgi?id=9048
52          */
53         msg = talloc_asprintf(mem_ctx, "%08X: LdapErr: DSID-%08X, comment: "
54                               "AcceptSecurityContext error, data %x, v1db1",
55                               (unsigned)HRES_ERROR_V(hresult),
56                               (unsigned)DSID,
57                               (unsigned)W_ERROR_V(werr));
58
59         return msg;
60 }
61
62
63 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
64 {
65         struct ldap_BindRequest *req = &call->request->r.BindRequest;
66         struct ldapsrv_reply *reply;
67         struct ldap_BindResponse *resp;
68
69         int result;
70         const char *errstr;
71         const char *nt4_domain, *nt4_account;
72
73         struct auth_session_info *session_info;
74
75         NTSTATUS status;
76
77         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
78
79         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
80         if (!reply) {
81                 return NT_STATUS_NO_MEMORY;
82         }
83
84         if (req->dn != NULL &&
85             strlen(req->dn) != 0 &&
86             call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
87             call->conn->sockets.active != call->conn->sockets.tls)
88         {
89                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
90                 result = LDAP_STRONG_AUTH_REQUIRED;
91                 errstr = talloc_asprintf(reply,
92                                          "BindSimple: Transport encryption required.");
93                 goto do_reply;
94         }
95
96         status = crack_auto_name_to_nt4_name(call, call->conn->connection->event.ctx, call->conn->lp_ctx, req->dn, &nt4_domain, &nt4_account);
97         if (NT_STATUS_IS_OK(status)) {
98                 status = authenticate_username_pw(call,
99                                                   call->conn->connection->event.ctx,
100                                                   call->conn->connection->msg_ctx,
101                                                   call->conn->lp_ctx,
102                                                   nt4_domain, nt4_account, 
103                                                   req->creds.password,
104                                                   MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
105                                                   MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
106                                                   &session_info);
107         }
108
109         if (NT_STATUS_IS_OK(status)) {
110                 result = LDAP_SUCCESS;
111                 errstr = NULL;
112
113                 talloc_unlink(call->conn, call->conn->session_info);
114                 call->conn->session_info = talloc_steal(call->conn, session_info);
115
116                 /* don't leak the old LDB */
117                 talloc_unlink(call->conn, call->conn->ldb);
118
119                 status = ldapsrv_backend_Init(call->conn);              
120                 
121                 if (!NT_STATUS_IS_OK(status)) {
122                         result = LDAP_OPERATIONS_ERROR;
123                         errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
124                 }
125         } else {
126                 status = nt_status_squash(status);
127
128                 result = LDAP_INVALID_CREDENTIALS;
129                 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
130                                                 0x0C0903A9, status);
131         }
132
133 do_reply:
134         resp = &reply->msg->r.BindResponse;
135         resp->response.resultcode = result;
136         resp->response.errormessage = errstr;
137         resp->response.dn = NULL;
138         resp->response.referral = NULL;
139         resp->SASL.secblob = NULL;
140
141         ldapsrv_queue_reply(call, reply);
142         return NT_STATUS_OK;
143 }
144
145 struct ldapsrv_sasl_postprocess_context {
146         struct ldapsrv_connection *conn;
147         struct tstream_context *sasl;
148 };
149
150 struct ldapsrv_sasl_postprocess_state {
151         uint8_t dummy;
152 };
153
154 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
155                                                 struct tevent_context *ev,
156                                                 void *private_data)
157 {
158         struct ldapsrv_sasl_postprocess_context *context =
159                 talloc_get_type_abort(private_data,
160                 struct ldapsrv_sasl_postprocess_context);
161         struct tevent_req *req;
162         struct ldapsrv_sasl_postprocess_state *state;
163
164         req = tevent_req_create(mem_ctx, &state,
165                                 struct ldapsrv_sasl_postprocess_state);
166         if (req == NULL) {
167                 return NULL;
168         }
169
170         TALLOC_FREE(context->conn->sockets.sasl);
171         context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
172         context->conn->sockets.active = context->conn->sockets.sasl;
173
174         tevent_req_done(req);
175         return tevent_req_post(req, ev);
176 }
177
178 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
179 {
180         return tevent_req_simple_recv_ntstatus(req);
181 }
182
183 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
184                                      const char *sasl_mech,
185                                      struct gensec_security **_gensec_security)
186 {
187         NTSTATUS status;
188
189         struct gensec_security *gensec_security;
190
191         status = samba_server_gensec_start(conn,
192                                            conn->connection->event.ctx,
193                                            conn->connection->msg_ctx,
194                                            conn->lp_ctx,
195                                            conn->server_credentials,
196                                            "ldap",
197                                            &gensec_security);
198         if (!NT_STATUS_IS_OK(status)) {
199                 return status;
200         }
201
202         status = gensec_set_remote_address(gensec_security,
203                                            conn->connection->remote_address);
204         if (!NT_STATUS_IS_OK(status)) {
205                 return status;
206         }
207
208         status = gensec_set_local_address(gensec_security,
209                                           conn->connection->local_address);
210         if (!NT_STATUS_IS_OK(status)) {
211                 return status;
212         }
213
214         gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
215         gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
216
217         status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
218
219         if (!NT_STATUS_IS_OK(status)) {
220                 return status;
221         }
222
223         *_gensec_security = gensec_security;
224         return status;
225 }
226
227 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
228 {
229         struct ldap_BindRequest *req = &call->request->r.BindRequest;
230         struct ldapsrv_reply *reply;
231         struct ldap_BindResponse *resp;
232         struct ldapsrv_connection *conn;
233         int result = 0;
234         const char *errstr=NULL;
235         NTSTATUS status = NT_STATUS_OK;
236
237         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
238
239         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
240         if (!reply) {
241                 return NT_STATUS_NO_MEMORY;
242         }
243         resp = &reply->msg->r.BindResponse;
244         
245         conn = call->conn;
246
247         /* 
248          * TODO: a SASL bind with a different mechanism
249          *       should cancel an inprogress SASL bind.
250          *       (see RFC 4513)
251          */
252
253         if (!conn->gensec) {
254                 conn->session_info = NULL;
255
256                 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
257                                               &conn->gensec);
258                 if (!NT_STATUS_IS_OK(status)) {
259                         DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
260                                   ldb_binary_encode_string(call, req->creds.SASL.mechanism),
261                                   nt_errstr(status)));
262                         result = LDAP_OPERATIONS_ERROR;
263                         errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s", 
264                                                  nt_errstr(status));
265                 }
266         }
267
268         if (NT_STATUS_IS_OK(status)) {
269                 DATA_BLOB input = data_blob(NULL, 0);
270                 DATA_BLOB output = data_blob(NULL, 0);
271
272                 if (req->creds.SASL.secblob) {
273                         input = *req->creds.SASL.secblob;
274                 }
275
276                 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
277                                           input, &output);
278
279                 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
280                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
281                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
282                 *resp->SASL.secblob = output;
283         } else {
284                 resp->SASL.secblob = NULL;
285         }
286
287         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
288                 result = LDAP_SASL_BIND_IN_PROGRESS;
289                 errstr = NULL;
290         } else if (NT_STATUS_IS_OK(status)) {
291                 struct ldapsrv_sasl_postprocess_context *context = NULL;
292
293                 result = LDAP_SUCCESS;
294                 errstr = NULL;
295
296                 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
297                     gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
298
299                         context = talloc(call, struct ldapsrv_sasl_postprocess_context);
300
301                         if (!context) {
302                                 status = NT_STATUS_NO_MEMORY;
303                         }
304                 }
305
306                 if (context && conn->sockets.tls) {
307                         TALLOC_FREE(context);
308                         status = NT_STATUS_NOT_SUPPORTED;
309                         result = LDAP_UNWILLING_TO_PERFORM;
310                         errstr = talloc_asprintf(reply,
311                                                  "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
312                                                  req->creds.SASL.mechanism);
313                 }
314
315                 if (context && conn->sockets.sasl) {
316                         TALLOC_FREE(context);
317                         status = NT_STATUS_NOT_SUPPORTED;
318                         result = LDAP_UNWILLING_TO_PERFORM;
319                         errstr = talloc_asprintf(reply,
320                                                  "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
321                                                  req->creds.SASL.mechanism);
322                 }
323
324                 if (context) {
325                         context->conn = conn;
326                         status = gensec_create_tstream(context,
327                                                        context->conn->gensec,
328                                                        context->conn->sockets.raw,
329                                                        &context->sasl);
330                         if (NT_STATUS_IS_OK(status)) {
331                                 if (!talloc_reference(context->sasl, conn->gensec)) {
332                                         status = NT_STATUS_NO_MEMORY;
333                                 }
334                         }
335                 } else {
336                         switch (call->conn->require_strong_auth) {
337                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
338                                 break;
339                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
340                                 if (call->conn->sockets.active == call->conn->sockets.tls) {
341                                         break;
342                                 }
343                                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
344                                 result = LDAP_STRONG_AUTH_REQUIRED;
345                                 errstr = talloc_asprintf(reply,
346                                                 "SASL:[%s]: not allowed if TLS is used.",
347                                                  req->creds.SASL.mechanism);
348                                 break;
349                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
350                                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
351                                 result = LDAP_STRONG_AUTH_REQUIRED;
352                                 errstr = talloc_asprintf(reply,
353                                                  "SASL:[%s]: Sign or Seal are required.",
354                                                  req->creds.SASL.mechanism);
355                                 break;
356                         }
357                 }
358
359                 if (result != LDAP_SUCCESS) {
360                 } else if (!NT_STATUS_IS_OK(status)) {
361                         result = LDAP_OPERATIONS_ERROR;
362                         errstr = talloc_asprintf(reply, 
363                                                  "SASL:[%s]: Failed to setup SASL socket: %s", 
364                                                  req->creds.SASL.mechanism, nt_errstr(status));
365                 } else {
366                         struct auth_session_info *old_session_info=NULL;
367
368                         old_session_info = conn->session_info;
369                         conn->session_info = NULL;
370                         status = gensec_session_info(conn->gensec, conn, &conn->session_info);
371                         if (!NT_STATUS_IS_OK(status)) {
372                                 conn->session_info = old_session_info;
373                                 result = LDAP_OPERATIONS_ERROR;
374                                 errstr = talloc_asprintf(reply, 
375                                                          "SASL:[%s]: Failed to get session info: %s", 
376                                                          req->creds.SASL.mechanism, nt_errstr(status));
377                         } else {
378                                 talloc_unlink(conn, old_session_info);
379                                 
380                                 /* don't leak the old LDB */
381                                 talloc_unlink(conn, conn->ldb);
382                                 
383                                 status = ldapsrv_backend_Init(conn);            
384                                 
385                                 if (!NT_STATUS_IS_OK(status)) {
386                                         result = LDAP_OPERATIONS_ERROR;
387                                         errstr = talloc_asprintf(reply, 
388                                                                  "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
389                                                                  req->creds.SASL.mechanism, 
390                                                                  nt_errstr(status));
391                                 }
392                         }
393                 }
394
395                 if (NT_STATUS_IS_OK(status) && context) {
396                         call->postprocess_send = ldapsrv_sasl_postprocess_send;
397                         call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
398                         call->postprocess_private = context;
399                 }
400                 talloc_unlink(conn, conn->gensec);
401                 conn->gensec = NULL;
402         } else {
403                 status = nt_status_squash(status);
404                 if (result == 0) {
405                         result = LDAP_INVALID_CREDENTIALS;
406                         errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
407                                                         0x0C0904DC, status);
408                 }
409                 talloc_unlink(conn, conn->gensec);
410                 conn->gensec = NULL;
411         }
412
413         resp->response.resultcode = result;
414         resp->response.dn = NULL;
415         resp->response.errormessage = errstr;
416         resp->response.referral = NULL;
417
418         ldapsrv_queue_reply(call, reply);
419         return NT_STATUS_OK;
420 }
421
422 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
423 {
424         struct ldap_BindRequest *req = &call->request->r.BindRequest;
425         struct ldapsrv_reply *reply;
426         struct ldap_BindResponse *resp;
427
428         if (call->conn->pending_calls != NULL) {
429                 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
430                 if (!reply) {
431                         return NT_STATUS_NO_MEMORY;
432                 }
433
434                 resp = &reply->msg->r.BindResponse;
435                 resp->response.resultcode = LDAP_BUSY;
436                 resp->response.dn = NULL;
437                 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
438                 resp->response.referral = NULL;
439                 resp->SASL.secblob = NULL;
440
441                 ldapsrv_queue_reply(call, reply);
442                 return NT_STATUS_OK;
443         }
444
445         /* 
446          * TODO: a simple bind should cancel an
447          *       inprogress SASL bind.
448          *       (see RFC 4513)
449          */
450         switch (req->mechanism) {
451                 case LDAP_AUTH_MECH_SIMPLE:
452                         return ldapsrv_BindSimple(call);
453                 case LDAP_AUTH_MECH_SASL:
454                         return ldapsrv_BindSASL(call);
455         }
456
457         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
458         if (!reply) {
459                 return NT_STATUS_NO_MEMORY;
460         }
461
462         resp = &reply->msg->r.BindResponse;
463         resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
464         resp->response.dn = NULL;
465         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
466         resp->response.referral = NULL;
467         resp->SASL.secblob = NULL;
468
469         ldapsrv_queue_reply(call, reply);
470         return NT_STATUS_OK;
471 }
472
473 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
474 {
475         DEBUG(10, ("UnbindRequest\n"));
476         return NT_STATUS_OK;
477 }