r5305: removed libcli/ldap/ldap.h from includes.h
[kamenim/samba.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
26 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
27 {
28         struct ldap_BindRequest *req = &call->request.r.BindRequest;
29         struct ldapsrv_reply *reply;
30         struct ldap_BindResponse *resp;
31
32         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
33
34         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
35         if (!reply) {
36                 return NT_STATUS_NO_MEMORY;
37         }
38
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);
45
46         return ldapsrv_queue_reply(call, reply);
47 }
48
49 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
50 {
51         struct ldap_BindRequest *req = &call->request.r.BindRequest;
52         struct ldapsrv_reply *reply;
53         struct ldap_BindResponse *resp;
54         struct ldapsrv_connection *conn;
55         int result;
56         const char *errstr;
57         NTSTATUS status = NT_STATUS_OK;
58         NTSTATUS sasl_status;
59 /*      BOOL ret;
60 */
61         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
62
63         if (!call->conn->gensec) {
64                 call->conn->session_info = NULL;
65
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)));
69                         return status;
70                 }
71                 
72                 gensec_set_target_service(call->conn->gensec, "ldap");
73
74                 /*gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
75                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
76                 */
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)));
81                         goto reply;
82                 }
83         }
84
85 reply:
86         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
87         if (!reply) {
88                 return NT_STATUS_NO_MEMORY;
89         }
90         resp = &reply->msg.r.BindResponse;
91         
92         conn = call->conn;
93
94         if (NT_STATUS_IS_OK(status)) {
95                 status = gensec_update(call->conn->gensec, reply,
96                                         req->creds.SASL.secblob, &resp->SASL.secblob);
97         }
98
99         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
100                 result = LDAP_SASL_BIND_IN_PROGRESS;
101                 errstr = NULL;
102         } else if (NT_STATUS_IS_OK(status)) {
103                 result = LDAP_SUCCESS;
104                 errstr = NULL;
105         } else {
106                 result = 49;
107                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
108         }
109
110         resp->response.resultcode = result;
111         resp->response.dn = NULL;
112         resp->response.errormessage = errstr;
113         resp->response.referral = NULL;
114
115         sasl_status = status;
116         status = ldapsrv_queue_reply(call, reply);
117         if (!NT_STATUS_IS_OK(sasl_status) || !NT_STATUS_IS_OK(status)) {
118                 return status;
119         }
120
121         status = ldapsrv_do_responses(call->conn);
122         if (!NT_STATUS_IS_OK(status)) {
123                 return status;
124         }
125
126 /*      ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, conn->out_buffer.data, conn->out_buffer.length);
127         if (!ret) {
128                 return NT_STATUS_NO_MEMORY;
129         }
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);
133         }
134 */
135         return status;
136 }
137
138 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
139 {
140         struct ldap_BindRequest *req = &call->request.r.BindRequest;
141         struct ldapsrv_reply *reply;
142         struct ldap_BindResponse *resp;
143
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);
149         }
150
151         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
152         if (!reply) {
153                 return NT_STATUS_NO_MEMORY;
154         }
155
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);
162
163         return ldapsrv_queue_reply(call, reply);
164 }
165
166 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
167 {
168         DEBUG(10, ("UnbindRequest\n"));
169         return NT_STATUS_OK;
170 }