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 ldapsrv_queue_reply(call, reply);
51 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
53 struct ldap_BindRequest *req = &call->request->r.BindRequest;
54 struct ldapsrv_reply *reply;
55 struct ldap_BindResponse *resp;
56 struct ldapsrv_connection *conn;
59 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 call->conn->connection->event.ctx);
68 if (!NT_STATUS_IS_OK(status)) {
69 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
73 gensec_set_target_service(call->conn->gensec, "ldap");
75 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
76 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
77 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
79 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
82 req->creds.SASL.mechanism, nt_errstr(status)));
88 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
90 return NT_STATUS_NO_MEMORY;
92 resp = &reply->msg->r.BindResponse;
96 if (NT_STATUS_IS_OK(status)) {
97 status = gensec_update(call->conn->gensec, reply,
98 req->creds.SASL.secblob, &resp->SASL.secblob);
101 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
102 result = LDAP_SASL_BIND_IN_PROGRESS;
104 } else if (NT_STATUS_IS_OK(status)) {
105 result = LDAP_SUCCESS;
107 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
108 gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
109 call->conn->enable_wrap = True;
113 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
116 resp->response.resultcode = result;
117 resp->response.dn = NULL;
118 resp->response.errormessage = errstr;
119 resp->response.referral = NULL;
121 ldapsrv_queue_reply(call, reply);
125 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
127 struct ldap_BindRequest *req = &call->request->r.BindRequest;
128 struct ldapsrv_reply *reply;
129 struct ldap_BindResponse *resp;
131 switch (req->mechanism) {
132 case LDAP_AUTH_MECH_SIMPLE:
133 return ldapsrv_BindSimple(call);
134 case LDAP_AUTH_MECH_SASL:
135 return ldapsrv_BindSASL(call);
138 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
140 return NT_STATUS_NO_MEMORY;
143 resp = &reply->msg->r.BindResponse;
144 resp->response.resultcode = 7;
145 resp->response.dn = NULL;
146 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
147 resp->response.referral = NULL;
148 resp->SASL.secblob = data_blob(NULL, 0);
150 ldapsrv_queue_reply(call, reply);
154 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
156 DEBUG(10, ("UnbindRequest\n"));