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