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 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
78 if (!NT_STATUS_IS_OK(status)) {
79 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
80 req->creds.SASL.mechanism, nt_errstr(status)));
86 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
88 return NT_STATUS_NO_MEMORY;
90 resp = &reply->msg.r.BindResponse;
94 if (NT_STATUS_IS_OK(status)) {
95 status = gensec_update(call->conn->gensec, reply,
96 req->creds.SASL.secblob, &resp->SASL.secblob);
99 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
100 result = LDAP_SASL_BIND_IN_PROGRESS;
102 } else if (NT_STATUS_IS_OK(status)) {
103 result = LDAP_SUCCESS;
107 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
110 resp->response.resultcode = result;
111 resp->response.dn = NULL;
112 resp->response.errormessage = errstr;
113 resp->response.referral = NULL;
115 sasl_status = status;
116 status = ldapsrv_queue_reply(call, reply);
117 if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
121 status = ldapsrv_do_responses(call->conn);
122 if (!NT_STATUS_IS_OK(status)) {
126 /* ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
128 return NT_STATUS_NO_MEMORY;
130 ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
131 if (NT_STATUS_IS_OK(status)) {
132 status = gensec_session_info(conn->gensec, &conn->session_info);
138 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
140 struct ldap_BindRequest *req = &call->request.r.BindRequest;
141 struct ldapsrv_reply *reply;
142 struct ldap_BindResponse *resp;
144 switch (req->mechanism) {
145 case LDAP_AUTH_MECH_SIMPLE:
146 return ldapsrv_BindSimple(call);
147 case LDAP_AUTH_MECH_SASL:
148 return ldapsrv_BindSASL(call);
151 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
153 return NT_STATUS_NO_MEMORY;
156 resp = &reply->msg.r.BindResponse;
157 resp->response.resultcode = 7;
158 resp->response.dn = NULL;
159 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
160 resp->response.referral = NULL;
161 resp->SASL.secblob = data_blob(NULL, 0);
163 return ldapsrv_queue_reply(call, reply);
166 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
168 DEBUG(10, ("UnbindRequest\n"));