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