r11955: got rid of the old rootDSE code in the ldap server.
[kai/samba.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 #include "ldap_server/ldap_server.h"
23 #include "dlinklist.h"
24 #include "libcli/ldap/ldap.h"
25
26
27 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
28 {
29         struct ldapsrv_reply *reply;
30
31         reply = talloc(call, struct ldapsrv_reply);
32         if (!reply) {
33                 return NULL;
34         }
35         reply->msg = talloc(reply, struct ldap_message);
36         if (reply->msg == NULL) {
37                 talloc_free(reply);
38                 return NULL;
39         }
40
41         reply->msg->messageid = call->request->messageid;
42         reply->msg->type = type;
43
44         return reply;
45 }
46
47 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
48 {
49         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
50 }
51
52 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn, uint8_t scope)
53 {
54         return conn->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         ldapsrv_queue_reply(call, reply);
79         return NT_STATUS_OK;
80 }
81
82 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
83 {
84         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
85         struct ldapsrv_partition *part;
86
87         DEBUG(10, ("SearchRequest"));
88         DEBUGADD(10, (" basedn: %s", req->basedn));
89         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
90
91         part = ldapsrv_get_partition(call->conn, req->basedn, req->scope);
92
93         if (!part->ops->Search) {
94                 struct ldap_Result *done;
95                 struct ldapsrv_reply *done_r;
96
97                 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
98                 if (!done_r) {
99                         return NT_STATUS_NO_MEMORY;
100                 }
101
102                 done = &done_r->msg->r.SearchResultDone;
103                 done->resultcode = 53;
104                 done->dn = NULL;
105                 done->errormessage = NULL;
106                 done->referral = NULL;
107
108                 ldapsrv_queue_reply(call, done_r);
109                 return NT_STATUS_OK;
110         }
111
112         return part->ops->Search(part, call, req);
113 }
114
115 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
116 {
117         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
118         struct ldapsrv_partition *part;
119
120         DEBUG(10, ("ModifyRequest"));
121         DEBUGADD(10, (" dn: %s", req->dn));
122
123         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
124
125         if (!part->ops->Modify) {
126                 return ldapsrv_unwilling(call, 53);
127         }
128
129         return part->ops->Modify(part, call, req);
130 }
131
132 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
133 {
134         struct ldap_AddRequest *req = &call->request->r.AddRequest;
135         struct ldapsrv_partition *part;
136
137         DEBUG(10, ("AddRequest"));
138         DEBUGADD(10, (" dn: %s", req->dn));
139
140         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
141
142         if (!part->ops->Add) {
143                 return ldapsrv_unwilling(call, 53);
144         }
145
146         return part->ops->Add(part, call, req);
147 }
148
149 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
150 {
151         struct ldap_DelRequest *req = &call->request->r.DelRequest;
152         struct ldapsrv_partition *part;
153
154         DEBUG(10, ("DelRequest"));
155         DEBUGADD(10, (" dn: %s", req->dn));
156
157         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
158
159         if (!part->ops->Del) {
160                 return ldapsrv_unwilling(call, 53);
161         }
162
163         return part->ops->Del(part, call, req);
164 }
165
166 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
167 {
168         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
169         struct ldapsrv_partition *part;
170
171         DEBUG(10, ("ModifyDNRequrest"));
172         DEBUGADD(10, (" dn: %s", req->dn));
173         DEBUGADD(10, (" newrdn: %s", req->newrdn));
174
175         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
176
177         if (!part->ops->ModifyDN) {
178                 return ldapsrv_unwilling(call, 53);
179         }
180
181         return part->ops->ModifyDN(part, call, req);
182 }
183
184 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
185 {
186         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
187         struct ldapsrv_partition *part;
188
189         DEBUG(10, ("CompareRequest"));
190         DEBUGADD(10, (" dn: %s", req->dn));
191
192         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
193
194         if (!part->ops->Compare) {
195                 return ldapsrv_unwilling(call, 53);
196         }
197
198         return part->ops->Compare(part, call, req);
199 }
200
201 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
202 {
203 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
204         DEBUG(10, ("AbandonRequest\n"));
205         return NT_STATUS_OK;
206 }
207
208 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
209 {
210 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
211         struct ldapsrv_reply *reply;
212
213         DEBUG(10, ("Extended\n"));
214
215         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
216         if (!reply) {
217                 return NT_STATUS_NO_MEMORY;
218         }
219
220         ZERO_STRUCT(reply->msg->r);
221
222         ldapsrv_queue_reply(call, reply);
223         return NT_STATUS_OK;
224 }
225
226 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
227 {
228         switch(call->request->type) {
229         case LDAP_TAG_BindRequest:
230                 return ldapsrv_BindRequest(call);
231         case LDAP_TAG_UnbindRequest:
232                 return ldapsrv_UnbindRequest(call);
233         case LDAP_TAG_SearchRequest:
234                 return ldapsrv_SearchRequest(call);
235         case LDAP_TAG_ModifyRequest:
236                 return ldapsrv_ModifyRequest(call);
237         case LDAP_TAG_AddRequest:
238                 return ldapsrv_AddRequest(call);
239         case LDAP_TAG_DelRequest:
240                 return ldapsrv_DelRequest(call);
241         case LDAP_TAG_ModifyDNRequest:
242                 return ldapsrv_ModifyDNRequest(call);
243         case LDAP_TAG_CompareRequest:
244                 return ldapsrv_CompareRequest(call);
245         case LDAP_TAG_AbandonRequest:
246                 return ldapsrv_AbandonRequest(call);
247         case LDAP_TAG_ExtendedRequest:
248                 return ldapsrv_ExtendedRequest(call);
249         default:
250                 return ldapsrv_unwilling(call, 2);
251         }
252 }
253
254