979a4b528344251d285f0690488590083c811f8a
[jelmer/samba4-debian.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 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 "libcli/ldap/ldap.h"
24 #include "smbd/service.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/socket.h"
30 #include "param/param.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, global_loadparm, 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                                                   global_loadparm,
54                                                   nt4_domain, nt4_account, 
55                                                   req->creds.password,
56                                                   &session_info);
57         }
58
59         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
60         if (!reply) {
61                 return NT_STATUS_NO_MEMORY;
62         }
63
64         if (NT_STATUS_IS_OK(status)) {
65                 result = LDAP_SUCCESS;
66                 errstr = NULL;
67
68                 talloc_free(call->conn->session_info);
69                 call->conn->session_info = session_info;
70                 talloc_steal(call->conn, session_info);
71
72                 /* don't leak the old LDB */
73                 talloc_free(call->conn->ldb);
74
75                 status = ldapsrv_backend_Init(call->conn);              
76                 
77                 if (!NT_STATUS_IS_OK(status)) {
78                         result = LDAP_OPERATIONS_ERROR;
79                         errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
80                 }
81         } else {
82                 status = auth_nt_status_squash(status);
83
84                 result = LDAP_INVALID_CREDENTIALS;
85                 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
86         }
87
88         resp = &reply->msg->r.BindResponse;
89         resp->response.resultcode = result;
90         resp->response.errormessage = errstr;
91         resp->response.dn = NULL;
92         resp->response.referral = NULL;
93         resp->SASL.secblob = NULL;
94
95         ldapsrv_queue_reply(call, reply);
96         return NT_STATUS_OK;
97 }
98
99 struct ldapsrv_sasl_context {
100         struct ldapsrv_connection *conn;
101         struct socket_context *sasl_socket;
102 };
103
104 static void ldapsrv_set_sasl(void *private) 
105 {
106         struct ldapsrv_sasl_context *ctx = talloc_get_type(private, struct ldapsrv_sasl_context);
107         talloc_steal(ctx->conn->connection, ctx->sasl_socket);
108         talloc_unlink(ctx->conn->connection, ctx->conn->connection->socket);
109
110         ctx->conn->sockets.sasl = ctx->sasl_socket;
111         ctx->conn->connection->socket = ctx->sasl_socket;
112         packet_set_socket(ctx->conn->packet, ctx->conn->connection->socket);
113 }
114
115 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
116 {
117         struct ldap_BindRequest *req = &call->request->r.BindRequest;
118         struct ldapsrv_reply *reply;
119         struct ldap_BindResponse *resp;
120         struct ldapsrv_connection *conn;
121         int result = 0;
122         const char *errstr=NULL;
123         NTSTATUS status = NT_STATUS_OK;
124
125         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
126
127         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
128         if (!reply) {
129                 return NT_STATUS_NO_MEMORY;
130         }
131         resp = &reply->msg->r.BindResponse;
132         
133         conn = call->conn;
134
135         /* 
136          * TODO: a SASL bind with a different mechanism
137          *       should cancel an inprogress SASL bind.
138          *       (see RFC 4513)
139          */
140
141         if (!conn->gensec) {
142                 conn->session_info = NULL;
143
144                 status = gensec_server_start(conn,
145                                              conn->connection->event.ctx,
146                                              conn->connection->msg_ctx,
147                                              &conn->gensec);
148                 if (!NT_STATUS_IS_OK(status)) {
149                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
150                         result = LDAP_OPERATIONS_ERROR;
151                         errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s", 
152                                                  nt_errstr(status));
153                 } else {
154                 
155                         gensec_set_target_service(conn->gensec, "ldap");
156                         
157                         gensec_set_credentials(conn->gensec, conn->server_credentials);
158                         
159                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
160                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
161                         gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
162                         
163                         status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
164                         
165                         if (!NT_STATUS_IS_OK(status)) {
166                                 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
167                                           req->creds.SASL.mechanism, nt_errstr(status)));
168                                 result = LDAP_OPERATIONS_ERROR;
169                                 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s", 
170                                                          req->creds.SASL.mechanism, nt_errstr(status));
171                         }
172                 }
173         }
174
175         if (NT_STATUS_IS_OK(status)) {
176                 DATA_BLOB input = data_blob(NULL, 0);
177                 DATA_BLOB output = data_blob(NULL, 0);
178
179                 if (req->creds.SASL.secblob) {
180                         input = *req->creds.SASL.secblob;
181                 }
182
183                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
184                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
185
186                 status = gensec_update(conn->gensec, reply,
187                                        input, &output);
188
189                 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
190                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
191                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
192                 *resp->SASL.secblob = output;
193         } else {
194                 resp->SASL.secblob = NULL;
195         }
196
197         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
198                 result = LDAP_SASL_BIND_IN_PROGRESS;
199                 errstr = NULL;
200         } else if (NT_STATUS_IS_OK(status)) {
201                 struct auth_session_info *old_session_info=NULL;
202                 struct ldapsrv_sasl_context *ctx;
203
204                 result = LDAP_SUCCESS;
205                 errstr = NULL;
206
207                 ctx = talloc(call, struct ldapsrv_sasl_context); 
208
209                 if (!ctx) {
210                         status = NT_STATUS_NO_MEMORY;
211                 } else {
212                         ctx->conn = conn;
213                         status = gensec_socket_init(conn->gensec, 
214                                                     conn->connection->socket,
215                                                     conn->connection->event.ctx, 
216                                                     stream_io_handler_callback,
217                                                     conn->connection,
218                                                     &ctx->sasl_socket);
219                 } 
220
221                 if (!ctx || !NT_STATUS_IS_OK(status)) {
222                         conn->session_info = old_session_info;
223                         result = LDAP_OPERATIONS_ERROR;
224                         errstr = talloc_asprintf(reply, 
225                                                  "SASL:[%s]: Failed to setup SASL socket: %s", 
226                                                  req->creds.SASL.mechanism, nt_errstr(status));
227                 } else {
228
229                         call->send_callback = ldapsrv_set_sasl;
230                         call->send_private = ctx;
231                 
232                         old_session_info = conn->session_info;
233                         conn->session_info = NULL;
234                         status = gensec_session_info(conn->gensec, &conn->session_info);
235                         if (!NT_STATUS_IS_OK(status)) {
236                                 conn->session_info = old_session_info;
237                                 result = LDAP_OPERATIONS_ERROR;
238                                 errstr = talloc_asprintf(reply, 
239                                                          "SASL:[%s]: Failed to get session info: %s", 
240                                                          req->creds.SASL.mechanism, nt_errstr(status));
241                         } else {
242                                 talloc_free(old_session_info);
243                                 talloc_steal(conn, conn->session_info);
244                                 
245                                 /* don't leak the old LDB */
246                                 talloc_free(conn->ldb);
247                                 
248                                 status = ldapsrv_backend_Init(conn);            
249                                 
250                                 if (!NT_STATUS_IS_OK(status)) {
251                                         result = LDAP_OPERATIONS_ERROR;
252                                         errstr = talloc_asprintf(reply, 
253                                                                  "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
254                                                                  req->creds.SASL.mechanism, 
255                                                                  nt_errstr(status));
256                                 }
257                         }
258                 }
259         } else {
260                 status = auth_nt_status_squash(status);
261                 if (result == 0) {
262                         result = LDAP_INVALID_CREDENTIALS;
263                         errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
264                 }
265         }
266
267         resp->response.resultcode = result;
268         resp->response.dn = NULL;
269         resp->response.errormessage = errstr;
270         resp->response.referral = NULL;
271
272         ldapsrv_queue_reply(call, reply);
273         return NT_STATUS_OK;
274 }
275
276 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
277 {
278         struct ldap_BindRequest *req = &call->request->r.BindRequest;
279         struct ldapsrv_reply *reply;
280         struct ldap_BindResponse *resp;
281
282         /* 
283          * TODO: we should fail the bind request
284          *       if there're any pending requests.
285          *
286          *       also a simple bind should cancel an
287          *       inprogress SASL bind.
288          *       (see RFC 4513)
289          */
290         switch (req->mechanism) {
291                 case LDAP_AUTH_MECH_SIMPLE:
292                         return ldapsrv_BindSimple(call);
293                 case LDAP_AUTH_MECH_SASL:
294                         return ldapsrv_BindSASL(call);
295         }
296
297         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
298         if (!reply) {
299                 return NT_STATUS_NO_MEMORY;
300         }
301
302         resp = &reply->msg->r.BindResponse;
303         resp->response.resultcode = 7;
304         resp->response.dn = NULL;
305         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
306         resp->response.referral = NULL;
307         resp->SASL.secblob = NULL;
308
309         ldapsrv_queue_reply(call, reply);
310         return NT_STATUS_OK;
311 }
312
313 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
314 {
315         DEBUG(10, ("UnbindRequest\n"));
316         return NT_STATUS_OK;
317 }