r7568: enable the NTLMSSP bulk data sign/seal code for out ldap server. This
[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
26 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
27 {
28         struct ldap_BindRequest *req = &call->request.r.BindRequest;
29         struct ldapsrv_reply *reply;
30         struct ldap_BindResponse *resp;
31
32         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
33
34         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
35         if (!reply) {
36                 return NT_STATUS_NO_MEMORY;
37         }
38
39         resp = &reply->msg.r.BindResponse;
40         resp->response.resultcode = 0;
41         resp->response.dn = NULL;
42         resp->response.errormessage = NULL;
43         resp->response.referral = NULL;
44         resp->SASL.secblob = data_blob(NULL, 0);
45
46         return ldapsrv_queue_reply(call, reply);
47 }
48
49 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
50 {
51         struct ldap_BindRequest *req = &call->request.r.BindRequest;
52         struct ldapsrv_reply *reply;
53         struct ldap_BindResponse *resp;
54         struct ldapsrv_connection *conn;
55         int result;
56         const char *errstr;
57         NTSTATUS status = NT_STATUS_OK;
58         NTSTATUS sasl_status;
59         BOOL ret;
60
61         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
62
63         if (!call->conn->gensec) {
64                 call->conn->session_info = NULL;
65
66                 status = gensec_server_start(call->conn, &call->conn->gensec);
67                 if (!NT_STATUS_IS_OK(status)) {
68                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
69                         return status;
70                 }
71                 
72                 gensec_set_target_service(call->conn->gensec, "ldap");
73
74                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
75                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
76                 
77                 if (req->creds.SASL.secblob.length >= 7 &&
78                     strncmp(req->creds.SASL.secblob.data, "NTLMSSP", 7) == 0) {
79                         status = gensec_start_mech_by_sasl_name(call->conn->gensec, "NTLM");
80                 } else {
81                         status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
82                 }
83                 if (!NT_STATUS_IS_OK(status)) {
84                         DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
85                                 req->creds.SASL.mechanism, nt_errstr(status)));
86                         goto reply;
87                 }
88         }
89
90 reply:
91         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
92         if (!reply) {
93                 return NT_STATUS_NO_MEMORY;
94         }
95         resp = &reply->msg.r.BindResponse;
96         
97         conn = call->conn;
98
99         if (NT_STATUS_IS_OK(status)) {
100                 status = gensec_update(call->conn->gensec, reply,
101                                        req->creds.SASL.secblob, &resp->SASL.secblob);
102         }
103
104         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
105                 result = LDAP_SASL_BIND_IN_PROGRESS;
106                 errstr = NULL;
107         } else if (NT_STATUS_IS_OK(status)) {
108                 result = LDAP_SUCCESS;
109                 errstr = NULL;
110         } else {
111                 result = 49;
112                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
113         }
114
115         resp->response.resultcode = result;
116         resp->response.dn = NULL;
117         resp->response.errormessage = errstr;
118         resp->response.referral = NULL;
119
120         sasl_status = status;
121         status = ldapsrv_queue_reply(call, reply);
122         if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
123                 return status;
124         }
125
126         status = ldapsrv_do_responses(call->conn);
127         if (!NT_STATUS_IS_OK(status)) {
128                 return status;
129         }
130
131         ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
132         if (!ret) {
133                 return NT_STATUS_NO_MEMORY;
134         }
135         ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
136         if (NT_STATUS_IS_OK(status)) {
137                 status = gensec_session_info(conn->gensec, &conn->session_info);
138         }
139
140         return status;
141 }
142
143 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
144 {
145         struct ldap_BindRequest *req = &call->request.r.BindRequest;
146         struct ldapsrv_reply *reply;
147         struct ldap_BindResponse *resp;
148
149         switch (req->mechanism) {
150                 case LDAP_AUTH_MECH_SIMPLE:
151                         return ldapsrv_BindSimple(call);
152                 case LDAP_AUTH_MECH_SASL:
153                         return ldapsrv_BindSASL(call);
154         }
155
156         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
157         if (!reply) {
158                 return NT_STATUS_NO_MEMORY;
159         }
160
161         resp = &reply->msg.r.BindResponse;
162         resp->response.resultcode = 7;
163         resp->response.dn = NULL;
164         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
165         resp->response.referral = NULL;
166         resp->SASL.secblob = data_blob(NULL, 0);
167
168         return ldapsrv_queue_reply(call, reply);
169 }
170
171 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
172 {
173         DEBUG(10, ("UnbindRequest\n"));
174         return NT_STATUS_OK;
175 }