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 /* When we add authentication here, we also need to handle telling the backends */
37 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
39 return NT_STATUS_NO_MEMORY;
42 resp = &reply->msg->r.BindResponse;
43 resp->response.resultcode = 0;
44 resp->response.dn = NULL;
45 resp->response.errormessage = NULL;
46 resp->response.referral = NULL;
47 resp->SASL.secblob = data_blob(NULL, 0);
49 ldapsrv_queue_reply(call, reply);
53 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
55 struct ldap_BindRequest *req = &call->request->r.BindRequest;
56 struct ldapsrv_reply *reply;
57 struct ldap_BindResponse *resp;
58 struct ldapsrv_connection *conn;
61 NTSTATUS status = NT_STATUS_OK;
63 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
65 if (!call->conn->gensec) {
66 call->conn->session_info = NULL;
68 status = gensec_server_start(call->conn, &call->conn->gensec,
69 call->conn->connection->event.ctx);
70 if (!NT_STATUS_IS_OK(status)) {
71 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
75 gensec_set_target_service(call->conn->gensec, "ldap");
77 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
78 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
79 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
81 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
84 req->creds.SASL.mechanism, nt_errstr(status)));
90 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
92 return NT_STATUS_NO_MEMORY;
94 resp = &reply->msg->r.BindResponse;
98 if (NT_STATUS_IS_OK(status)) {
99 status = gensec_update(call->conn->gensec, reply,
100 req->creds.SASL.secblob, &resp->SASL.secblob);
103 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
104 result = LDAP_SASL_BIND_IN_PROGRESS;
106 } else if (NT_STATUS_IS_OK(status)) {
107 struct ldapsrv_partition *part;
108 struct auth_session_info *old_session_info;
110 result = LDAP_SUCCESS;
112 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
113 gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
114 call->conn->enable_wrap = True;
116 old_session_info = call->conn->session_info;
117 call->conn->session_info = NULL;
118 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
119 if (!NT_STATUS_IS_OK(status)) {
120 call->conn->session_info = old_session_info;
121 result = LDAP_OPERATIONS_ERROR;
122 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
124 talloc_free(old_session_info);
125 for (part = call->conn->partitions; part; part = part->next) {
126 if (!part->ops->Bind) {
129 status = part->ops->Bind(part, conn);
130 if (!NT_STATUS_IS_OK(status)) {
131 result = LDAP_OPERATIONS_ERROR;
132 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to advise partition %s of new credentials: %s", req->creds.SASL.mechanism, part->base_dn, nt_errstr(status));
137 status = auth_nt_status_squash(status);
138 result = LDAP_INVALID_CREDENTIALS;
139 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
142 resp->response.resultcode = result;
143 resp->response.dn = NULL;
144 resp->response.errormessage = errstr;
145 resp->response.referral = NULL;
147 ldapsrv_queue_reply(call, reply);
151 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
153 struct ldap_BindRequest *req = &call->request->r.BindRequest;
154 struct ldapsrv_reply *reply;
155 struct ldap_BindResponse *resp;
157 switch (req->mechanism) {
158 case LDAP_AUTH_MECH_SIMPLE:
159 return ldapsrv_BindSimple(call);
160 case LDAP_AUTH_MECH_SASL:
161 return ldapsrv_BindSASL(call);
164 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
166 return NT_STATUS_NO_MEMORY;
169 resp = &reply->msg->r.BindResponse;
170 resp->response.resultcode = 7;
171 resp->response.dn = NULL;
172 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
173 resp->response.referral = NULL;
174 resp->SASL.secblob = data_blob(NULL, 0);
176 ldapsrv_queue_reply(call, reply);
180 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
182 DEBUG(10, ("UnbindRequest\n"));