r2891: call rootDSE only with LDAP_SEARCH_SCOPE_BASE
[bbaumbach/samba-autobuild/.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, enum ldap_scope scope)
49 {
50         if (scope == LDAP_SEARCH_SCOPE_BASE
51             && strcasecmp("", dn) == 0) {
52                 return conn->service->rootDSE;
53         }
54
55         return conn->service->default_partition;
56 }
57
58 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
59 {
60         struct ldapsrv_reply *reply;
61         struct ldap_ExtendedResponse *r;
62
63         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
64
65         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
66         if (!reply) {
67                 return NT_STATUS_NO_MEMORY;
68         }
69
70         r = &reply->msg.r.ExtendedResponse;
71         r->response.resultcode = error;
72         r->response.dn = NULL;
73         r->response.errormessage = NULL;
74         r->response.referral = NULL;
75         r->name = NULL;
76         r->value.data = NULL;
77         r->value.length = 0;
78
79         return ldapsrv_queue_reply(call, reply);
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", req->filter));
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                 return ldapsrv_queue_reply(call, done_r);
109         }
110
111         return part->ops->Search(part, call, req);
112 }
113
114 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
115 {
116         struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
117         struct ldapsrv_partition *part;
118
119         DEBUG(10, ("ModifyRequest"));
120         DEBUGADD(10, (" dn: %s", req->dn));
121
122         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
123
124         if (!part->ops->Modify) {
125                 return ldapsrv_unwilling(call, 53);
126         }
127
128         return part->ops->Modify(part, call, req);
129 }
130
131 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
132 {
133         struct ldap_AddRequest *req = &call->request.r.AddRequest;
134         struct ldapsrv_partition *part;
135
136         DEBUG(10, ("AddRequest"));
137         DEBUGADD(10, (" dn: %s", req->dn));
138
139         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
140
141         if (!part->ops->Add) {
142                 return ldapsrv_unwilling(call, 53);
143         }
144
145         return part->ops->Add(part, call, req);
146 }
147
148 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
149 {
150         struct ldap_DelRequest *req = &call->request.r.DelRequest;
151         struct ldapsrv_partition *part;
152
153         DEBUG(10, ("DelRequest"));
154         DEBUGADD(10, (" dn: %s", req->dn));
155
156         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
157
158         if (!part->ops->Del) {
159                 return ldapsrv_unwilling(call, 53);
160         }
161
162         return part->ops->Del(part, call, req);
163 }
164
165 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
166 {
167         struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
168         struct ldapsrv_partition *part;
169
170         DEBUG(10, ("ModifyDNRequrest"));
171         DEBUGADD(10, (" dn: %s", req->dn));
172         DEBUGADD(10, (" newrdn: %s", req->newrdn));
173
174         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
175
176         if (!part->ops->ModifyDN) {
177                 return ldapsrv_unwilling(call, 53);
178         }
179
180         return part->ops->ModifyDN(part, call, req);
181 }
182
183 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
184 {
185         struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
186         struct ldapsrv_partition *part;
187
188         DEBUG(10, ("CompareRequest"));
189         DEBUGADD(10, (" dn: %s", req->dn));
190
191         part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
192
193         if (!part->ops->Compare) {
194                 return ldapsrv_unwilling(call, 53);
195         }
196
197         return part->ops->Compare(part, call, req);
198 }
199
200 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
201 {
202 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
203         DEBUG(10, ("AbandonRequest\n"));
204         return NT_STATUS_OK;
205 }
206
207 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
208 {
209 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
210         struct ldapsrv_reply *reply;
211
212         DEBUG(10, ("Extended\n"));
213
214         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
215         if (!reply) {
216                 return NT_STATUS_NO_MEMORY;
217         }
218
219         ZERO_STRUCT(reply->msg.r);
220
221         return ldapsrv_queue_reply(call, reply);
222 }
223
224 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
225 {
226         switch(call->request.type) {
227         case LDAP_TAG_BindRequest:
228                 return ldapsrv_BindRequest(call);
229         case LDAP_TAG_UnbindRequest:
230                 return ldapsrv_UnbindRequest(call);
231         case LDAP_TAG_SearchRequest:
232                 return ldapsrv_SearchRequest(call);
233         case LDAP_TAG_ModifyRequest:
234                 return ldapsrv_ModifyRequest(call);
235         case LDAP_TAG_AddRequest:
236                 return ldapsrv_AddRequest(call);
237         case LDAP_TAG_DelRequest:
238                 return ldapsrv_DelRequest(call);
239         case LDAP_TAG_ModifyDNRequest:
240                 return ldapsrv_ModifyDNRequest(call);
241         case LDAP_TAG_CompareRequest:
242                 return ldapsrv_CompareRequest(call);
243         case LDAP_TAG_AbandonRequest:
244                 return ldapsrv_AbandonRequest(call);
245         case LDAP_TAG_ExtendedRequest:
246                 return ldapsrv_ExtendedRequest(call);
247         default:
248                 return ldapsrv_unwilling(call, 2);
249         }
250 }