2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
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.
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.
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.
22 #include "ldap_server/ldap_server.h"
23 #include "auth/auth.h"
24 #include "libcli/ldap/ldap.h"
26 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
28 struct ldap_BindRequest *req = &call->request.r.BindRequest;
29 struct ldapsrv_reply *reply;
30 struct ldap_BindResponse *resp;
32 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
34 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
36 return NT_STATUS_NO_MEMORY;
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);
46 return ldapsrv_queue_reply(call, reply);
49 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
51 struct ldap_BindRequest *req = &call->request.r.BindRequest;
52 struct ldapsrv_reply *reply;
53 struct ldap_BindResponse *resp;
54 struct ldapsrv_connection *conn;
57 NTSTATUS status = NT_STATUS_OK;
61 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
63 if (!call->conn->gensec) {
64 call->conn->session_info = NULL;
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)));
72 gensec_set_target_service(call->conn->gensec, "ldap");
74 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
75 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
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");
81 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
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)));
91 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
93 return NT_STATUS_NO_MEMORY;
95 resp = &reply->msg.r.BindResponse;
99 if (NT_STATUS_IS_OK(status)) {
100 status = gensec_update(call->conn->gensec, reply,
101 req->creds.SASL.secblob, &resp->SASL.secblob);
104 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
105 result = LDAP_SASL_BIND_IN_PROGRESS;
107 } else if (NT_STATUS_IS_OK(status)) {
108 result = LDAP_SUCCESS;
112 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
115 resp->response.resultcode = result;
116 resp->response.dn = NULL;
117 resp->response.errormessage = errstr;
118 resp->response.referral = NULL;
120 sasl_status = status;
121 status = ldapsrv_queue_reply(call, reply);
122 if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
126 status = ldapsrv_do_responses(call->conn);
127 if (!NT_STATUS_IS_OK(status)) {
131 ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
133 return NT_STATUS_NO_MEMORY;
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);
143 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
145 struct ldap_BindRequest *req = &call->request.r.BindRequest;
146 struct ldapsrv_reply *reply;
147 struct ldap_BindResponse *resp;
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);
156 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
158 return NT_STATUS_NO_MEMORY;
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);
168 return ldapsrv_queue_reply(call, reply);
171 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
173 DEBUG(10, ("UnbindRequest\n"));