r9754: Upgrading with the command line utility now works, at least partially (-:
[kai/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 #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         ldapsrv_queue_reply(call, reply);
48         return NT_STATUS_OK;
49 }
50
51 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
52 {
53         struct ldap_BindRequest *req = &call->request->r.BindRequest;
54         struct ldapsrv_reply *reply;
55         struct ldap_BindResponse *resp;
56         struct ldapsrv_connection *conn;
57         int result;
58         const char *errstr;
59         NTSTATUS status = NT_STATUS_OK;
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                                              call->conn->connection->event.ctx);
68                 if (!NT_STATUS_IS_OK(status)) {
69                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
70                         return status;
71                 }
72                 
73                 gensec_set_target_service(call->conn->gensec, "ldap");
74
75                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
76                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
77                 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
78
79                 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
80                 if (!NT_STATUS_IS_OK(status)) {
81                         DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n", 
82                                 req->creds.SASL.mechanism, nt_errstr(status)));
83                         goto reply;
84                 }
85         }
86
87 reply:
88         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
89         if (!reply) {
90                 return NT_STATUS_NO_MEMORY;
91         }
92         resp = &reply->msg->r.BindResponse;
93         
94         conn = call->conn;
95
96         if (NT_STATUS_IS_OK(status)) {
97                 status = gensec_update(call->conn->gensec, reply,
98                                        req->creds.SASL.secblob, &resp->SASL.secblob);
99         }
100
101         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
102                 result = LDAP_SASL_BIND_IN_PROGRESS;
103                 errstr = NULL;
104         } else if (NT_STATUS_IS_OK(status)) {
105                 result = LDAP_SUCCESS;
106                 errstr = NULL;
107                 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
108                     gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
109                         call->conn->enable_wrap = True;
110                 }
111         } else {
112                 result = 49;
113                 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
114         }
115
116         resp->response.resultcode = result;
117         resp->response.dn = NULL;
118         resp->response.errormessage = errstr;
119         resp->response.referral = NULL;
120
121         ldapsrv_queue_reply(call, reply);
122         return NT_STATUS_OK;
123 }
124
125 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
126 {
127         struct ldap_BindRequest *req = &call->request->r.BindRequest;
128         struct ldapsrv_reply *reply;
129         struct ldap_BindResponse *resp;
130
131         switch (req->mechanism) {
132                 case LDAP_AUTH_MECH_SIMPLE:
133                         return ldapsrv_BindSimple(call);
134                 case LDAP_AUTH_MECH_SASL:
135                         return ldapsrv_BindSASL(call);
136         }
137
138         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
139         if (!reply) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         resp = &reply->msg->r.BindResponse;
144         resp->response.resultcode = 7;
145         resp->response.dn = NULL;
146         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
147         resp->response.referral = NULL;
148         resp->SASL.secblob = data_blob(NULL, 0);
149
150         ldapsrv_queue_reply(call, reply);
151         return NT_STATUS_OK;
152 }
153
154 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
155 {
156         DEBUG(10, ("UnbindRequest\n"));
157         return NT_STATUS_OK;
158 }