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