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"
25 #include "smbd/service_stream.h"
27 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
29 struct ldap_BindRequest *req = &call->request->r.BindRequest;
30 struct ldapsrv_reply *reply;
31 struct ldap_BindResponse *resp;
33 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
35 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
37 return NT_STATUS_NO_MEMORY;
40 resp = &reply->msg->r.BindResponse;
41 resp->response.resultcode = 0;
42 resp->response.dn = NULL;
43 resp->response.errormessage = NULL;
44 resp->response.referral = NULL;
45 resp->SASL.secblob = data_blob(NULL, 0);
47 return ldapsrv_queue_reply(call, reply);
50 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
52 struct ldap_BindRequest *req = &call->request->r.BindRequest;
53 struct ldapsrv_reply *reply;
54 struct ldap_BindResponse *resp;
55 struct ldapsrv_connection *conn;
58 NTSTATUS status = NT_STATUS_OK;
62 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
64 if (!call->conn->gensec) {
65 call->conn->session_info = NULL;
67 status = gensec_server_start(call->conn, &call->conn->gensec,
68 call->conn->connection->event.ctx);
69 if (!NT_STATUS_IS_OK(status)) {
70 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
74 gensec_set_target_service(call->conn->gensec, "ldap");
76 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
77 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
80 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
81 if (!NT_STATUS_IS_OK(status)) {
82 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
83 req->creds.SASL.mechanism, nt_errstr(status)));
89 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
91 return NT_STATUS_NO_MEMORY;
93 resp = &reply->msg->r.BindResponse;
97 if (NT_STATUS_IS_OK(status)) {
98 status = gensec_update(call->conn->gensec, reply,
99 req->creds.SASL.secblob, &resp->SASL.secblob);
102 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
103 result = LDAP_SASL_BIND_IN_PROGRESS;
105 } else if (NT_STATUS_IS_OK(status)) {
106 result = LDAP_SUCCESS;
110 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
113 resp->response.resultcode = result;
114 resp->response.dn = NULL;
115 resp->response.errormessage = errstr;
116 resp->response.referral = NULL;
118 sasl_status = status;
119 status = ldapsrv_queue_reply(call, reply);
120 if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
124 status = ldapsrv_do_responses(call->conn);
125 if (!NT_STATUS_IS_OK(status)) {
129 ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
131 return NT_STATUS_NO_MEMORY;
133 ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
134 if (NT_STATUS_IS_OK(status)) {
135 status = gensec_session_info(conn->gensec, &conn->session_info);
141 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
143 struct ldap_BindRequest *req = &call->request->r.BindRequest;
144 struct ldapsrv_reply *reply;
145 struct ldap_BindResponse *resp;
147 switch (req->mechanism) {
148 case LDAP_AUTH_MECH_SIMPLE:
149 return ldapsrv_BindSimple(call);
150 case LDAP_AUTH_MECH_SASL:
151 return ldapsrv_BindSASL(call);
154 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
156 return NT_STATUS_NO_MEMORY;
159 resp = &reply->msg->r.BindResponse;
160 resp->response.resultcode = 7;
161 resp->response.dn = NULL;
162 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
163 resp->response.referral = NULL;
164 resp->SASL.secblob = data_blob(NULL, 0);
166 return ldapsrv_queue_reply(call, reply);
169 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
171 DEBUG(10, ("UnbindRequest\n"));