r11225: Remove pointless goto.
[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 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
27 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
28 {
29         struct ldap_BindRequest *req = &call->request->r.BindRequest;
30         struct ldapsrv_reply *reply;
31         struct ldap_BindResponse *resp;
32
33         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
34
35         /* When we add authentication here, we also need to handle telling the backends */
36
37         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
38         if (!reply) {
39                 return NT_STATUS_NO_MEMORY;
40         }
41
42         resp = &reply->msg->r.BindResponse;
43         resp->response.resultcode = 0;
44         resp->response.dn = NULL;
45         resp->response.errormessage = NULL;
46         resp->response.referral = NULL;
47         resp->SASL.secblob = data_blob(NULL, 0);
48
49         ldapsrv_queue_reply(call, reply);
50         return NT_STATUS_OK;
51 }
52
53 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
54 {
55         struct ldap_BindRequest *req = &call->request->r.BindRequest;
56         struct ldapsrv_reply *reply;
57         struct ldap_BindResponse *resp;
58         struct ldapsrv_connection *conn;
59         int result;
60         const char *errstr;
61         NTSTATUS status = NT_STATUS_OK;
62
63         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
64
65         if (!call->conn->gensec) {
66                 struct cli_credentials *server_credentials;
67                 call->conn->session_info = NULL;
68
69                 status = gensec_server_start(call->conn, &call->conn->gensec,
70                                              call->conn->connection->event.ctx);
71                 if (!NT_STATUS_IS_OK(status)) {
72                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
73                         return status;
74                 }
75                 
76                 gensec_set_target_service(call->conn->gensec, "ldap");
77
78                 server_credentials 
79                         = cli_credentials_init(call);
80                 if (!server_credentials) {
81                         DEBUG(1, ("Failed to init server credentials\n"));
82                         return NT_STATUS_NO_MEMORY;
83                 }
84                 
85                 cli_credentials_set_conf(server_credentials);
86                 status = cli_credentials_set_machine_account(server_credentials);
87                 if (!NT_STATUS_IS_OK(status)) {
88                         DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
89                         talloc_free(server_credentials);
90                         server_credentials = NULL;
91                 }
92                 
93                 gensec_set_credentials(call->conn->gensec, server_credentials);
94
95                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
96                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
97                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
98
99                 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
100                 if (!NT_STATUS_IS_OK(status)) {
101                         DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
102                                 req->creds.SASL.mechanism, nt_errstr(status)));
103                 }
104         }
105
106         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
107         if (!reply) {
108                 return NT_STATUS_NO_MEMORY;
109         }
110         resp = &reply->msg->r.BindResponse;
111         
112         conn = call->conn;
113
114         if (NT_STATUS_IS_OK(status)) {
115                 status = gensec_update(call->conn->gensec, reply,
116                                        req->creds.SASL.secblob, &resp->SASL.secblob);
117         }
118
119         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
120                 result = LDAP_SASL_BIND_IN_PROGRESS;
121                 errstr = NULL;
122         } else if (NT_STATUS_IS_OK(status)) {
123                 struct ldapsrv_partition *part;
124                 struct auth_session_info *old_session_info;
125
126                 result = LDAP_SUCCESS;
127                 errstr = NULL;
128                 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
129                     gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
130                         call->conn->enable_wrap = True;
131                 }
132                 old_session_info = call->conn->session_info;
133                 call->conn->session_info = NULL;
134                 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
135                 if (!NT_STATUS_IS_OK(status)) {
136                         call->conn->session_info = old_session_info;
137                         result = LDAP_OPERATIONS_ERROR;
138                         errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
139                 } else {
140                         talloc_free(old_session_info);
141                         for (part = call->conn->partitions; part; part = part->next) {
142                                 if (!part->ops->Bind) {
143                                         continue;
144                                 }
145                                 status = part->ops->Bind(part, conn);
146                                 if (!NT_STATUS_IS_OK(status)) {
147                                         result = LDAP_OPERATIONS_ERROR;
148                                         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));
149                                 }
150                         }
151                 }
152         } else {
153                 status = auth_nt_status_squash(status);
154                 result = LDAP_INVALID_CREDENTIALS;
155                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
156         }
157
158         resp->response.resultcode = result;
159         resp->response.dn = NULL;
160         resp->response.errormessage = errstr;
161         resp->response.referral = NULL;
162
163         ldapsrv_queue_reply(call, reply);
164         return NT_STATUS_OK;
165 }
166
167 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
168 {
169         struct ldap_BindRequest *req = &call->request->r.BindRequest;
170         struct ldapsrv_reply *reply;
171         struct ldap_BindResponse *resp;
172
173         switch (req->mechanism) {
174                 case LDAP_AUTH_MECH_SIMPLE:
175                         return ldapsrv_BindSimple(call);
176                 case LDAP_AUTH_MECH_SASL:
177                         return ldapsrv_BindSASL(call);
178         }
179
180         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
181         if (!reply) {
182                 return NT_STATUS_NO_MEMORY;
183         }
184
185         resp = &reply->msg->r.BindResponse;
186         resp->response.resultcode = 7;
187         resp->response.dn = NULL;
188         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
189         resp->response.referral = NULL;
190         resp->SASL.secblob = data_blob(NULL, 0);
191
192         ldapsrv_queue_reply(call, reply);
193         return NT_STATUS_OK;
194 }
195
196 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
197 {
198         DEBUG(10, ("UnbindRequest\n"));
199         return NT_STATUS_OK;
200 }