r2877: the Bind and Unbind function are already moved...
[ira/wip.git] / source4 / ldap_server / ldap_backend.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
23
24 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
25 {
26         struct ldapsrv_reply *reply;
27
28         reply = talloc_p(call, struct ldapsrv_reply);
29         if (!reply) {
30                 return NULL;
31         }
32
33         reply->prev = reply->next = NULL;
34         reply->state = LDAPSRV_REPLY_STATE_NEW;
35         reply->msg.messageid = call->request.messageid;
36         reply->msg.type = type;
37         reply->msg.mem_ctx = reply;
38
39         return reply;
40 }
41
42 NTSTATUS ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
43 {
44         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
45         return NT_STATUS_OK;
46 }
47
48 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn)
49 {
50         if (strcasecmp("", dn) == 0) {
51                 return conn->service->rootDSE;
52         }
53
54         return conn->service->default_partition;
55 }
56
57 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
58 {
59         struct ldapsrv_reply *reply;
60         struct ldap_ExtendedResponse *r;
61
62         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
63
64         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
65         if (!reply) {
66                 return NT_STATUS_NO_MEMORY;
67         }
68
69         r = &reply->msg.r.ExtendedResponse;
70         r->response.resultcode = error;
71         r->response.dn = NULL;
72         r->response.errormessage = NULL;
73         r->response.referral = NULL;
74         r->name = NULL;
75         r->value.data = NULL;
76         r->value.length = 0;
77
78         return ldapsrv_queue_reply(call, reply);
79 }
80
81 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
82 {
83         struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
84         struct ldapsrv_partition *part;
85
86         DEBUG(10, ("SearchRequest"));
87         DEBUGADD(10, (" basedn: %s", req->basedn));
88         DEBUGADD(10, (" filter: %s\n", req->filter));
89
90         part = ldapsrv_get_partition(call->conn, req->basedn);
91
92         if (!part->ops->Search) {
93                 struct ldap_Result *done;
94                 struct ldapsrv_reply *done_r;
95
96                 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
97                 if (!done_r) {
98                         return NT_STATUS_NO_MEMORY;
99                 }
100
101                 done = &done_r->msg.r.SearchResultDone;
102                 done->resultcode = 53;
103                 done->dn = NULL;
104                 done->errormessage = NULL;
105                 done->referral = NULL;
106
107                 return ldapsrv_queue_reply(call, done_r);
108         }
109
110         return part->ops->Search(part, call, req);
111 }
112
113 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
114 {
115         struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
116         struct ldapsrv_partition *part;
117
118         DEBUG(10, ("ModifyRequest"));
119         DEBUGADD(10, (" dn: %s", req->dn));
120
121         part = ldapsrv_get_partition(call->conn, req->dn);
122
123         if (!part->ops->Modify) {
124                 return ldapsrv_unwilling(call, 53);
125         }
126
127         return part->ops->Modify(part, call, req);
128 }
129
130 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
131 {
132         struct ldap_AddRequest *req = &call->request.r.AddRequest;
133         struct ldapsrv_partition *part;
134
135         DEBUG(10, ("AddRequest"));
136         DEBUGADD(10, (" dn: %s", req->dn));
137
138         part = ldapsrv_get_partition(call->conn, req->dn);
139
140         if (!part->ops->Add) {
141                 return ldapsrv_unwilling(call, 53);
142         }
143
144         return part->ops->Add(part, call, req);
145 }
146
147 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
148 {
149         struct ldap_DelRequest *req = &call->request.r.DelRequest;
150         struct ldapsrv_partition *part;
151
152         DEBUG(10, ("DelRequest"));
153         DEBUGADD(10, (" dn: %s", req->dn));
154
155         part = ldapsrv_get_partition(call->conn, req->dn);
156
157         if (!part->ops->Del) {
158                 return ldapsrv_unwilling(call, 53);
159         }
160
161         return part->ops->Del(part, call, req);
162 }
163
164 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
165 {
166         struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
167         struct ldapsrv_partition *part;
168
169         DEBUG(10, ("ModifyDNRequrest"));
170         DEBUGADD(10, (" dn: %s", req->dn));
171         DEBUGADD(10, (" newrdn: %s", req->newrdn));
172
173         part = ldapsrv_get_partition(call->conn, req->dn);
174
175         if (!part->ops->ModifyDN) {
176                 return ldapsrv_unwilling(call, 53);
177         }
178
179         return part->ops->ModifyDN(part, call, req);
180 }
181
182 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
183 {
184         struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
185         struct ldapsrv_partition *part;
186
187         DEBUG(10, ("CompareRequest"));
188         DEBUGADD(10, (" dn: %s", req->dn));
189
190         part = ldapsrv_get_partition(call->conn, req->dn);
191
192         if (!part->ops->Compare) {
193                 return ldapsrv_unwilling(call, 53);
194         }
195
196         return part->ops->Compare(part, call, req);
197 }
198
199 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
200 {
201 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
202         DEBUG(10, ("AbandonRequest\n"));
203         return NT_STATUS_OK;
204 }
205
206 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
207 {
208 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
209         struct ldapsrv_reply *reply;
210
211         DEBUG(10, ("Extended\n"));
212
213         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
214         if (!reply) {
215                 return NT_STATUS_NO_MEMORY;
216         }
217
218         ZERO_STRUCT(reply->msg.r);
219
220         return ldapsrv_queue_reply(call, reply);
221 }
222
223 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
224 {
225         switch(call->request.type) {
226         case LDAP_TAG_BindRequest:
227                 return ldapsrv_BindRequest(call);
228         case LDAP_TAG_UnbindRequest:
229                 return ldapsrv_UnbindRequest(call);
230         case LDAP_TAG_SearchRequest:
231                 return ldapsrv_SearchRequest(call);
232         case LDAP_TAG_ModifyRequest:
233                 return ldapsrv_ModifyRequest(call);
234         case LDAP_TAG_AddRequest:
235                 return ldapsrv_AddRequest(call);
236         case LDAP_TAG_DelRequest:
237                 return ldapsrv_DelRequest(call);
238         case LDAP_TAG_ModifyDNRequest:
239                 return ldapsrv_ModifyDNRequest(call);
240         case LDAP_TAG_CompareRequest:
241                 return ldapsrv_CompareRequest(call);
242         case LDAP_TAG_AbandonRequest:
243                 return ldapsrv_AbandonRequest(call);
244         case LDAP_TAG_ExtendedRequest:
245                 return ldapsrv_ExtendedRequest(call);
246         default:
247                 return ldapsrv_unwilling(call, 2);
248         }
249 }