r12542: Move some more prototypes out to seperate headers
[mat/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 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 "dsdb/samdb/samdb.h"
27
28 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
29 {
30         struct ldap_BindRequest *req = &call->request->r.BindRequest;
31         struct ldapsrv_reply *reply;
32         struct ldap_BindResponse *resp;
33
34         int result;
35         const char *errstr;
36         const char *nt4_domain, *nt4_account;
37
38         struct auth_session_info *session_info;
39
40         NTSTATUS status;
41
42         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
43
44         status = crack_dn_to_nt4_name(call, req->dn, &nt4_domain, &nt4_account);
45         if (NT_STATUS_IS_OK(status)) {
46                 status = authenticate_username_pw(call, nt4_domain, nt4_account, 
47                                                   req->creds.password, &session_info);
48         }
49
50         /* When we add authentication here, we also need to handle telling the backends */
51
52         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
53         if (!reply) {
54                 return NT_STATUS_NO_MEMORY;
55         }
56
57         if (NT_STATUS_IS_OK(status)) {
58                 struct ldapsrv_partition *part;
59                 result = LDAP_SUCCESS;
60                 errstr = NULL;
61
62                 talloc_free(call->conn->session_info);
63                 call->conn->session_info = session_info;
64                 for (part = call->conn->partitions; part; part = part->next) {
65                         if (!part->ops->Bind) {
66                                 continue;
67                         }
68                         status = part->ops->Bind(part, call->conn);
69                         if (!NT_STATUS_IS_OK(status)) {
70                                 result = LDAP_OPERATIONS_ERROR;
71                                 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise partition %s of new credentials: %s", part->base_dn, nt_errstr(status));
72                         }
73                 }
74         } else {
75                 status = auth_nt_status_squash(status);
76
77                 result = LDAP_INVALID_CREDENTIALS;
78                 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
79         }
80
81         resp = &reply->msg->r.BindResponse;
82         resp->response.resultcode = result;
83         resp->response.errormessage = errstr;
84         resp->response.dn = NULL;
85         resp->response.referral = NULL;
86
87         /* This looks wrong... */
88         resp->SASL.secblob = data_blob(NULL, 0);
89
90         ldapsrv_queue_reply(call, reply);
91         return NT_STATUS_OK;
92 }
93
94 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
95 {
96         struct ldap_BindRequest *req = &call->request->r.BindRequest;
97         struct ldapsrv_reply *reply;
98         struct ldap_BindResponse *resp;
99         struct ldapsrv_connection *conn;
100         int result;
101         const char *errstr;
102         NTSTATUS status = NT_STATUS_OK;
103
104         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
105
106         if (!call->conn->gensec) {
107                 struct cli_credentials *server_credentials;
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                 server_credentials 
120                         = cli_credentials_init(call);
121                 if (!server_credentials) {
122                         DEBUG(1, ("Failed to init server credentials\n"));
123                         return NT_STATUS_NO_MEMORY;
124                 }
125                 
126                 cli_credentials_set_conf(server_credentials);
127                 status = cli_credentials_set_machine_account(server_credentials);
128                 if (!NT_STATUS_IS_OK(status)) {
129                         DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
130                         talloc_free(server_credentials);
131                         server_credentials = NULL;
132                 }
133                 
134                 gensec_set_credentials(call->conn->gensec, server_credentials);
135
136                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
137                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
138                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
139
140                 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
141                 if (!NT_STATUS_IS_OK(status)) {
142                         DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
143                                 req->creds.SASL.mechanism, nt_errstr(status)));
144                 }
145         }
146
147         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
148         if (!reply) {
149                 return NT_STATUS_NO_MEMORY;
150         }
151         resp = &reply->msg->r.BindResponse;
152         
153         conn = call->conn;
154
155         if (NT_STATUS_IS_OK(status)) {
156                 status = gensec_update(call->conn->gensec, reply,
157                                        req->creds.SASL.secblob, &resp->SASL.secblob);
158         }
159
160         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
161                 result = LDAP_SASL_BIND_IN_PROGRESS;
162                 errstr = NULL;
163         } else if (NT_STATUS_IS_OK(status)) {
164                 struct ldapsrv_partition *part;
165                 struct auth_session_info *old_session_info;
166
167                 result = LDAP_SUCCESS;
168                 errstr = NULL;
169                 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
170                     gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
171                         call->conn->enable_wrap = True;
172                 }
173                 old_session_info = call->conn->session_info;
174                 call->conn->session_info = NULL;
175                 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
176                 if (!NT_STATUS_IS_OK(status)) {
177                         call->conn->session_info = old_session_info;
178                         result = LDAP_OPERATIONS_ERROR;
179                         errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
180                 } else {
181                         talloc_free(old_session_info);
182                         for (part = call->conn->partitions; part; part = part->next) {
183                                 if (!part->ops->Bind) {
184                                         continue;
185                                 }
186                                 status = part->ops->Bind(part, conn);
187                                 if (!NT_STATUS_IS_OK(status)) {
188                                         result = LDAP_OPERATIONS_ERROR;
189                                         errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to advise partition %s of new credentials: %s", req->creds.SASL.mechanism, part->base_dn, nt_errstr(status));
190                                 }
191                         }
192                 }
193         } else {
194                 status = auth_nt_status_squash(status);
195                 result = LDAP_INVALID_CREDENTIALS;
196                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
197         }
198
199         resp->response.resultcode = result;
200         resp->response.dn = NULL;
201         resp->response.errormessage = errstr;
202         resp->response.referral = NULL;
203
204         ldapsrv_queue_reply(call, reply);
205         return NT_STATUS_OK;
206 }
207
208 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
209 {
210         struct ldap_BindRequest *req = &call->request->r.BindRequest;
211         struct ldapsrv_reply *reply;
212         struct ldap_BindResponse *resp;
213
214         switch (req->mechanism) {
215                 case LDAP_AUTH_MECH_SIMPLE:
216                         return ldapsrv_BindSimple(call);
217                 case LDAP_AUTH_MECH_SASL:
218                         return ldapsrv_BindSASL(call);
219         }
220
221         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
222         if (!reply) {
223                 return NT_STATUS_NO_MEMORY;
224         }
225
226         resp = &reply->msg->r.BindResponse;
227         resp->response.resultcode = 7;
228         resp->response.dn = NULL;
229         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
230         resp->response.referral = NULL;
231         resp->SASL.secblob = data_blob(NULL, 0);
232
233         ldapsrv_queue_reply(call, reply);
234         return NT_STATUS_OK;
235 }
236
237 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
238 {
239         DEBUG(10, ("UnbindRequest\n"));
240         return NT_STATUS_OK;
241 }