r2890: fix segfault when call is destroyed and we dereference it
[Samba/gebeck_regimport.git] / source4 / ldap_server / ldap_bind.c
blob840ff5d4e8abe7d6fd956353118cd8471d0e1b03
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP server
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.
21 #include "includes.h"
24 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
26 struct ldap_BindRequest *req = &call->request.r.BindRequest;
27 struct ldapsrv_reply *reply;
28 struct ldap_BindResponse *resp;
30 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
32 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
33 if (!reply) {
34 return NT_STATUS_NO_MEMORY;
37 resp = &reply->msg.r.BindResponse;
38 resp->response.resultcode = 0;
39 resp->response.dn = NULL;
40 resp->response.errormessage = NULL;
41 resp->response.referral = NULL;
42 resp->SASL.secblob = data_blob(NULL, 0);
44 return ldapsrv_queue_reply(call, reply);
47 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
49 struct ldap_BindRequest *req = &call->request.r.BindRequest;
50 struct ldapsrv_reply *reply;
51 struct ldap_BindResponse *resp;
52 int result;
53 const char *errstr;
54 NTSTATUS status = NT_STATUS_OK;
55 NTSTATUS sasl_status;
56 /*BOOL ret;*/
58 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
60 if (!call->conn->gensec) {
61 call->conn->session_info = NULL;
63 status = gensec_server_start(call->conn, &call->conn->gensec);
64 if (!NT_STATUS_IS_OK(status)) {
65 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
66 return status;
69 /*gensec_want_feature(call->conn->gensec, GENSEC_WANT_SIGN|GENSEC_WANT_SEAL);*/
71 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
72 if (!NT_STATUS_IS_OK(status)) {
73 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
74 req->creds.SASL.mechanism, nt_errstr(status)));
75 goto reply;
79 reply:
80 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
81 if (!reply) {
82 return NT_STATUS_NO_MEMORY;
84 resp = &reply->msg.r.BindResponse;
86 if (NT_STATUS_IS_OK(status)) {
87 status = gensec_update(call->conn->gensec, reply,
88 req->creds.SASL.secblob, &resp->SASL.secblob);
91 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
92 result = LDAP_SASL_BIND_IN_PROGRESS;
93 errstr = NULL;
94 } else if (NT_STATUS_IS_OK(status)) {
95 result = LDAP_SUCCESS;
96 errstr = NULL;
97 } else {
98 result = 49;
99 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
102 resp->response.resultcode = result;
103 resp->response.dn = NULL;
104 resp->response.errormessage = errstr;
105 resp->response.referral = NULL;
107 sasl_status = status;
108 status = ldapsrv_queue_reply(call, reply);
109 if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
110 return status;
113 status = ldapsrv_do_responses(call->conn);
114 if (!NT_STATUS_IS_OK(status)) {
115 return status;
118 /* ret = ldapsrv_append_to_buf(&call->conn->sasl_out_buffer, call->conn->out_buffer.data, call->conn->out_buffer.length);
119 if (!ret) {
120 return NT_STATUS_NO_MEMORY;
122 ldapsrv_consumed_from_buf(&call->conn->out_buffer, call->conn->out_buffer.length);
124 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
125 if (!NT_STATUS_IS_OK(status)) {
126 return status;
130 return status;
133 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
135 struct ldap_BindRequest *req = &call->request.r.BindRequest;
136 struct ldapsrv_reply *reply;
137 struct ldap_BindResponse *resp;
139 switch (req->mechanism) {
140 case LDAP_AUTH_MECH_SIMPLE:
141 return ldapsrv_BindSimple(call);
142 case LDAP_AUTH_MECH_SASL:
143 return ldapsrv_BindSASL(call);
146 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
147 if (!reply) {
148 return NT_STATUS_NO_MEMORY;
151 resp = &reply->msg.r.BindResponse;
152 resp->response.resultcode = 7;
153 resp->response.dn = NULL;
154 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
155 resp->response.referral = NULL;
156 resp->SASL.secblob = data_blob(NULL, 0);
158 return ldapsrv_queue_reply(call, reply);
161 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
163 DEBUG(10, ("UnbindRequest\n"));
164 return NT_STATUS_OK;