r7633: this patch started as an attempt to make the dcerpc code use a given
[abartlet/samba.git/.git] / source4 / ldap_server / ldap_bind.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "ldap_server/ldap_server.h"
23 #include "auth/auth.h"
24 #include "libcli/ldap/ldap.h"
25 #include "smbd/service_stream.h"
26
27 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
28 {
29         struct ldap_BindRequest *req = &call->request->r.BindRequest;
30         struct ldapsrv_reply *reply;
31         struct ldap_BindResponse *resp;
32
33         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
34
35         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
36         if (!reply) {
37                 return NT_STATUS_NO_MEMORY;
38         }
39
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);
46
47         return ldapsrv_queue_reply(call, reply);
48 }
49
50 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
51 {
52         struct ldap_BindRequest *req = &call->request->r.BindRequest;
53         struct ldapsrv_reply *reply;
54         struct ldap_BindResponse *resp;
55         struct ldapsrv_connection *conn;
56         int result;
57         const char *errstr;
58         NTSTATUS status = NT_STATUS_OK;
59         NTSTATUS sasl_status;
60         BOOL ret;
61
62         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
63
64         if (!call->conn->gensec) {
65                 call->conn->session_info = NULL;
66
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)));
71                         return status;
72                 }
73                 
74                 gensec_set_target_service(call->conn->gensec, "ldap");
75
76                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
77                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
78                 
79
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)));
84                         goto reply;
85                 }
86         }
87
88 reply:
89         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
90         if (!reply) {
91                 return NT_STATUS_NO_MEMORY;
92         }
93         resp = &reply->msg->r.BindResponse;
94         
95         conn = call->conn;
96
97         if (NT_STATUS_IS_OK(status)) {
98                 status = gensec_update(call->conn->gensec, reply,
99                                        req->creds.SASL.secblob, &resp->SASL.secblob);
100         }
101
102         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
103                 result = LDAP_SASL_BIND_IN_PROGRESS;
104                 errstr = NULL;
105         } else if (NT_STATUS_IS_OK(status)) {
106                 result = LDAP_SUCCESS;
107                 errstr = NULL;
108         } else {
109                 result = 49;
110                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
111         }
112
113         resp->response.resultcode = result;
114         resp->response.dn = NULL;
115         resp->response.errormessage = errstr;
116         resp->response.referral = NULL;
117
118         sasl_status = status;
119         status = ldapsrv_queue_reply(call, reply);
120         if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
121                 return status;
122         }
123
124         status = ldapsrv_do_responses(call->conn);
125         if (!NT_STATUS_IS_OK(status)) {
126                 return status;
127         }
128
129         ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
130         if (!ret) {
131                 return NT_STATUS_NO_MEMORY;
132         }
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);
136         }
137
138         return status;
139 }
140
141 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
142 {
143         struct ldap_BindRequest *req = &call->request->r.BindRequest;
144         struct ldapsrv_reply *reply;
145         struct ldap_BindResponse *resp;
146
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);
152         }
153
154         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
155         if (!reply) {
156                 return NT_STATUS_NO_MEMORY;
157         }
158
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);
165
166         return ldapsrv_queue_reply(call, reply);
167 }
168
169 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
170 {
171         DEBUG(10, ("UnbindRequest\n"));
172         return NT_STATUS_OK;
173 }