CVE-2016-2112: s4:ldap_server: reduce scope of old_session_info variable
[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                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_LDAP_STYLE);
185                         
186                         status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
187                         
188                         if (!NT_STATUS_IS_OK(status)) {
189                                 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
190                                           req->creds.SASL.mechanism, nt_errstr(status)));
191                                 result = LDAP_OPERATIONS_ERROR;
192                                 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s", 
193                                                          req->creds.SASL.mechanism, nt_errstr(status));
194                         }
195                 }
196         }
197
198         if (NT_STATUS_IS_OK(status)) {
199                 DATA_BLOB input = data_blob(NULL, 0);
200                 DATA_BLOB output = data_blob(NULL, 0);
201
202                 if (req->creds.SASL.secblob) {
203                         input = *req->creds.SASL.secblob;
204                 }
205
206                 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
207                                           input, &output);
208
209                 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
210                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
211                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
212                 *resp->SASL.secblob = output;
213         } else {
214                 resp->SASL.secblob = NULL;
215         }
216
217         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
218                 result = LDAP_SASL_BIND_IN_PROGRESS;
219                 errstr = NULL;
220         } else if (NT_STATUS_IS_OK(status)) {
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                 } else if (!NT_STATUS_IS_OK(status)) {
269                         result = LDAP_OPERATIONS_ERROR;
270                         errstr = talloc_asprintf(reply, 
271                                                  "SASL:[%s]: Failed to setup SASL socket: %s", 
272                                                  req->creds.SASL.mechanism, nt_errstr(status));
273                 } else {
274                         struct auth_session_info *old_session_info=NULL;
275
276                         old_session_info = conn->session_info;
277                         conn->session_info = NULL;
278                         status = gensec_session_info(conn->gensec, conn, &conn->session_info);
279                         if (!NT_STATUS_IS_OK(status)) {
280                                 conn->session_info = old_session_info;
281                                 result = LDAP_OPERATIONS_ERROR;
282                                 errstr = talloc_asprintf(reply, 
283                                                          "SASL:[%s]: Failed to get session info: %s", 
284                                                          req->creds.SASL.mechanism, nt_errstr(status));
285                         } else {
286                                 talloc_unlink(conn, old_session_info);
287                                 
288                                 /* don't leak the old LDB */
289                                 talloc_unlink(conn, conn->ldb);
290                                 
291                                 status = ldapsrv_backend_Init(conn);            
292                                 
293                                 if (!NT_STATUS_IS_OK(status)) {
294                                         result = LDAP_OPERATIONS_ERROR;
295                                         errstr = talloc_asprintf(reply, 
296                                                                  "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
297                                                                  req->creds.SASL.mechanism, 
298                                                                  nt_errstr(status));
299                                 }
300                         }
301                 }
302
303                 if (NT_STATUS_IS_OK(status) && context) {
304                         call->postprocess_send = ldapsrv_sasl_postprocess_send;
305                         call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
306                         call->postprocess_private = context;
307                 }
308                 talloc_unlink(conn, conn->gensec);
309                 conn->gensec = NULL;
310         } else {
311                 status = nt_status_squash(status);
312                 if (result == 0) {
313                         result = LDAP_INVALID_CREDENTIALS;
314                         errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
315                 }
316                 talloc_unlink(conn, conn->gensec);
317                 conn->gensec = NULL;
318         }
319
320         resp->response.resultcode = result;
321         resp->response.dn = NULL;
322         resp->response.errormessage = errstr;
323         resp->response.referral = NULL;
324
325         ldapsrv_queue_reply(call, reply);
326         return NT_STATUS_OK;
327 }
328
329 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
330 {
331         struct ldap_BindRequest *req = &call->request->r.BindRequest;
332         struct ldapsrv_reply *reply;
333         struct ldap_BindResponse *resp;
334
335         if (call->conn->pending_calls != NULL) {
336                 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
337                 if (!reply) {
338                         return NT_STATUS_NO_MEMORY;
339                 }
340
341                 resp = &reply->msg->r.BindResponse;
342                 resp->response.resultcode = LDAP_BUSY;
343                 resp->response.dn = NULL;
344                 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
345                 resp->response.referral = NULL;
346                 resp->SASL.secblob = NULL;
347
348                 ldapsrv_queue_reply(call, reply);
349                 return NT_STATUS_OK;
350         }
351
352         /* 
353          * TODO: a simple bind should cancel an
354          *       inprogress SASL bind.
355          *       (see RFC 4513)
356          */
357         switch (req->mechanism) {
358                 case LDAP_AUTH_MECH_SIMPLE:
359                         return ldapsrv_BindSimple(call);
360                 case LDAP_AUTH_MECH_SASL:
361                         return ldapsrv_BindSASL(call);
362         }
363
364         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
365         if (!reply) {
366                 return NT_STATUS_NO_MEMORY;
367         }
368
369         resp = &reply->msg->r.BindResponse;
370         resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
371         resp->response.dn = NULL;
372         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
373         resp->response.referral = NULL;
374         resp->SASL.secblob = NULL;
375
376         ldapsrv_queue_reply(call, reply);
377         return NT_STATUS_OK;
378 }
379
380 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
381 {
382         DEBUG(10, ("UnbindRequest\n"));
383         return NT_STATUS_OK;
384 }