r15356: Remove unused 'flags' argument from socket_send() and friends.
[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 #include "ldap_server/ldap_server.h"
23 #include "dlinklist.h"
24 #include "libcli/ldap/ldap.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "dsdb/samdb/samdb.h"
28
29 #define VALID_DN_SYNTAX(dn,i) do {\
30         if (!(dn)) {\
31                 return NT_STATUS_NO_MEMORY;\
32         } else if ((dn)->comp_num < (i)) {\
33                 result = LDAP_INVALID_DN_SYNTAX;\
34                 errstr = "Invalid DN (" #i " components needed for '" #dn "')";\
35                 goto reply;\
36         }\
37 } while(0)
38
39 static int map_ldb_error(struct ldb_context *ldb, int err, const char **errstring)
40 {
41         *errstring = ldb_errstring(ldb);
42         
43         /* its 1:1 for now */
44         return err;
45 }
46
47 /*
48   connect to the sam database
49 */
50 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn) 
51 {
52         conn->ldb = samdb_connect(conn, conn->session_info);
53         if (conn->ldb == NULL) {
54                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
55         }
56         ldb_set_opaque(conn->ldb, "server_credentials", conn->server_credentials);
57
58         return NT_STATUS_OK;
59 }
60
61 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
62 {
63         struct ldapsrv_reply *reply;
64
65         reply = talloc(call, struct ldapsrv_reply);
66         if (!reply) {
67                 return NULL;
68         }
69         reply->msg = talloc(reply, struct ldap_message);
70         if (reply->msg == NULL) {
71                 talloc_free(reply);
72                 return NULL;
73         }
74
75         reply->msg->messageid = call->request->messageid;
76         reply->msg->type = type;
77         reply->msg->controls = NULL;
78
79         return reply;
80 }
81
82 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
83 {
84         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
85 }
86
87 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
88 {
89         struct ldapsrv_reply *reply;
90         struct ldap_ExtendedResponse *r;
91
92         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
93
94         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
95         if (!reply) {
96                 return NT_STATUS_NO_MEMORY;
97         }
98
99         r = &reply->msg->r.ExtendedResponse;
100         r->response.resultcode = error;
101         r->response.dn = NULL;
102         r->response.errormessage = NULL;
103         r->response.referral = NULL;
104         r->oid = NULL;
105         r->value = NULL;
106
107         ldapsrv_queue_reply(call, reply);
108         return NT_STATUS_OK;
109 }
110
111 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
112 {
113         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
114         struct ldap_SearchResEntry *ent;
115         struct ldap_Result *done;
116         struct ldapsrv_reply *ent_r, *done_r;
117         void *local_ctx;
118         struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
119         struct ldb_dn *basedn;
120         struct ldb_result *res = NULL;
121         struct ldb_request *lreq;
122         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
123         const char **attrs = NULL;
124         const char *errstr = NULL;
125         int success_limit = 1;
126         int result = -1;
127         int ldb_ret = -1;
128         int i, j;
129
130         DEBUG(10, ("SearchRequest"));
131         DEBUGADD(10, (" basedn: %s", req->basedn));
132         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
133
134         local_ctx = talloc_new(call);
135         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
136
137         basedn = ldb_dn_explode(local_ctx, req->basedn);
138         VALID_DN_SYNTAX(basedn, 0);
139
140         DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
141         DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
142
143         switch (req->scope) {
144                 case LDAP_SEARCH_SCOPE_BASE:
145                         DEBUG(10,("SearchRequest: scope: [BASE]\n"));
146                         scope = LDB_SCOPE_BASE;
147                         success_limit = 0;
148                         break;
149                 case LDAP_SEARCH_SCOPE_SINGLE:
150                         DEBUG(10,("SearchRequest: scope: [ONE]\n"));
151                         scope = LDB_SCOPE_ONELEVEL;
152                         success_limit = 0;
153                         break;
154                 case LDAP_SEARCH_SCOPE_SUB:
155                         DEBUG(10,("SearchRequest: scope: [SUB]\n"));
156                         scope = LDB_SCOPE_SUBTREE;
157                         success_limit = 0;
158                         break;
159         }
160
161         if (req->num_attributes >= 1) {
162                 attrs = talloc_array(samdb, const char *, req->num_attributes+1);
163                 NT_STATUS_HAVE_NO_MEMORY(attrs);
164
165                 for (i=0; i < req->num_attributes; i++) {
166                         DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
167                         attrs[i] = req->attributes[i];
168                 }
169                 attrs[i] = NULL;
170         }
171
172         DEBUG(5,("ldb_request dn=%s filter=%s\n", 
173                  req->basedn, ldb_filter_from_tree(call, req->tree)));
174
175         lreq = talloc(local_ctx, struct ldb_request);
176         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
177         
178         lreq->operation = LDB_REQ_SEARCH;
179         lreq->op.search.base = basedn;
180         lreq->op.search.scope = scope;
181         lreq->op.search.tree = req->tree;
182         lreq->op.search.attrs = attrs;
183
184         lreq->controls = call->request->controls;
185
186         ldb_ret = ldb_request(samdb, lreq);
187
188         /* Ensure we don't keep the search results around for too long */
189         res = talloc_steal(local_ctx, lreq->op.search.res);
190
191         if (ldb_ret == LDB_SUCCESS) {
192                 for (i = 0; i < res->count; i++) {
193                         ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
194                         NT_STATUS_HAVE_NO_MEMORY(ent_r);
195
196                         ent = &ent_r->msg->r.SearchResultEntry;
197                         ent->dn = ldb_dn_linearize(ent_r, res->msgs[i]->dn);
198                         ent->num_attributes = 0;
199                         ent->attributes = NULL;
200                         if (res->msgs[i]->num_elements == 0) {
201                                 goto queue_reply;
202                         }
203                         ent->num_attributes = res->msgs[i]->num_elements;
204                         ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
205                         NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
206                         for (j=0; j < ent->num_attributes; j++) {
207                                 ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name);
208                                 ent->attributes[j].num_values = 0;
209                                 ent->attributes[j].values = NULL;
210                                 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
211                                         continue;
212                                 }
213                                 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
214                                 ent->attributes[j].values = res->msgs[i]->elements[j].values;
215                                 talloc_steal(ent->attributes, res->msgs[i]->elements[j].values);
216                         }
217 queue_reply:
218                         ldapsrv_queue_reply(call, ent_r);
219                 }
220         }
221
222 reply:
223         done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
224         NT_STATUS_HAVE_NO_MEMORY(done_r);
225
226         done = &done_r->msg->r.SearchResultDone;
227         done->dn = NULL;
228         done->referral = NULL;
229
230         if (result != -1) {
231         } else if (ldb_ret == LDB_SUCCESS) {
232                 if (res->count >= success_limit) {
233                         DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
234                         result = LDAP_SUCCESS;
235                         errstr = NULL;
236                 } else if (res->count == 0) {
237                         DEBUG(10,("SearchRequest: no results\n"));
238                         result = LDAP_NO_SUCH_OBJECT;
239                         errstr = ldb_errstring(samdb);
240                 }
241                 if (res->controls) {
242                         done_r->msg->controls = res->controls;
243                         talloc_steal(done_r, res->controls);
244                 }
245         } else {
246                 DEBUG(10,("SearchRequest: error\n"));
247                 result = map_ldb_error(samdb, ldb_ret, &errstr);
248         }
249
250         done->resultcode = result;
251         done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
252
253         talloc_free(local_ctx);
254
255         ldapsrv_queue_reply(call, done_r);
256         return NT_STATUS_OK;
257 }
258
259 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
260 {
261         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
262         struct ldap_Result *modify_result;
263         struct ldapsrv_reply *modify_reply;
264         void *local_ctx;
265         struct ldb_context *samdb = call->conn->ldb;
266         struct ldb_message *msg = NULL;
267         struct ldb_dn *dn;
268         const char *errstr = NULL;
269         int result = LDAP_SUCCESS;
270         int ldb_ret;
271         int i,j;
272
273         DEBUG(10, ("ModifyRequest"));
274         DEBUGADD(10, (" dn: %s", req->dn));
275
276         local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
277         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
278
279         dn = ldb_dn_explode(local_ctx, req->dn);
280         VALID_DN_SYNTAX(dn, 1);
281
282         DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
283
284         msg = talloc(local_ctx, struct ldb_message);
285         NT_STATUS_HAVE_NO_MEMORY(msg);
286
287         msg->dn = dn;
288         msg->private_data = NULL;
289         msg->num_elements = 0;
290         msg->elements = NULL;
291
292         if (req->num_mods > 0) {
293                 msg->num_elements = req->num_mods;
294                 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
295                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
296
297                 for (i=0; i < msg->num_elements; i++) {
298                         msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
299                         msg->elements[i].num_values = 0;
300                         msg->elements[i].values = NULL;
301
302                         switch (req->mods[i].type) {
303                         default:
304                                 result = LDAP_PROTOCOL_ERROR;
305                                 errstr = "Invalid LDAP_MODIFY_* type";
306                                 goto reply;
307                         case LDAP_MODIFY_ADD:
308                                 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
309                                 break;
310                         case LDAP_MODIFY_DELETE:
311                                 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
312                                 break;
313                         case LDAP_MODIFY_REPLACE:
314                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
315                                 break;
316                         }
317
318                         msg->elements[i].num_values = req->mods[i].attrib.num_values;
319                         if (msg->elements[i].num_values > 0) {
320                                 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
321                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
322
323                                 for (j=0; j < msg->elements[i].num_values; j++) {
324                                         if (!(req->mods[i].attrib.values[j].length > 0)) {
325                                                 result = LDAP_OTHER;
326                                                 errstr = "Empty attribute values are not allowed";
327                                                 goto reply;
328                                         }
329                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
330                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
331                                 }
332                         }
333                 }
334         } else {
335                 result = LDAP_OTHER;
336                 errstr = "No mods are not allowed";
337                 goto reply;
338         }
339
340 reply:
341         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
342         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
343
344         if (result == LDAP_SUCCESS) {
345                 ldb_ret = ldb_modify(samdb, msg);
346                 result = map_ldb_error(samdb, ldb_ret, &errstr);
347         }
348
349         modify_result = &modify_reply->msg->r.AddResponse;
350         modify_result->dn = NULL;
351         modify_result->resultcode = result;
352         modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
353         modify_result->referral = NULL;
354
355         talloc_free(local_ctx);
356
357         ldapsrv_queue_reply(call, modify_reply);
358         return NT_STATUS_OK;
359
360 }
361
362 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
363 {
364         struct ldap_AddRequest *req = &call->request->r.AddRequest;
365         struct ldap_Result *add_result;
366         struct ldapsrv_reply *add_reply;
367         void *local_ctx;
368         struct ldb_context *samdb = call->conn->ldb;
369         struct ldb_message *msg = NULL;
370         struct ldb_dn *dn;
371         const char *errstr = NULL;
372         int result = LDAP_SUCCESS;
373         int ldb_ret;
374         int i,j;
375
376         DEBUG(10, ("AddRequest"));
377         DEBUGADD(10, (" dn: %s", req->dn));
378
379         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
380         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
381
382         dn = ldb_dn_explode(local_ctx, req->dn);
383         VALID_DN_SYNTAX(dn,1);
384
385         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
386
387         msg = talloc(local_ctx, struct ldb_message);
388         NT_STATUS_HAVE_NO_MEMORY(msg);
389
390         msg->dn = dn;
391         msg->private_data = NULL;
392         msg->num_elements = 0;
393         msg->elements = NULL;
394
395         if (req->num_attributes > 0) {
396                 msg->num_elements = req->num_attributes;
397                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
398                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
399
400                 for (i=0; i < msg->num_elements; i++) {
401                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
402                         msg->elements[i].flags = 0;
403                         msg->elements[i].num_values = 0;
404                         msg->elements[i].values = NULL;
405                         
406                         if (req->attributes[i].num_values > 0) {
407                                 msg->elements[i].num_values = req->attributes[i].num_values;
408                                 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
409                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
410
411                                 for (j=0; j < msg->elements[i].num_values; j++) {
412                                         if (!(req->attributes[i].values[j].length > 0)) {
413                                                 result = LDAP_OTHER;
414                                                 errstr = "Empty attribute values are not allowed";
415                                                 goto reply;
416                                         }
417                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
418                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
419                                 }
420                         } else {
421                                 result = LDAP_OTHER;
422                                 errstr = "No attribute values are not allowed";
423                                 goto reply;
424                         }
425                 }
426         } else {
427                 result = LDAP_OTHER;
428                 errstr = "No attributes are not allowed";
429                 goto reply;
430         }
431
432 reply:
433         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
434         NT_STATUS_HAVE_NO_MEMORY(add_reply);
435
436         if (result == LDAP_SUCCESS) {
437                 ldb_ret = ldb_add(samdb, msg);
438                 result = map_ldb_error(samdb, ldb_ret, &errstr);
439         }
440
441         add_result = &add_reply->msg->r.AddResponse;
442         add_result->dn = NULL;
443         add_result->resultcode = result;
444         add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
445         add_result->referral = NULL;
446
447         talloc_free(local_ctx);
448
449         ldapsrv_queue_reply(call, add_reply);
450         return NT_STATUS_OK;
451
452 }
453
454 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
455 {
456         struct ldap_DelRequest *req = &call->request->r.DelRequest;
457         struct ldap_Result *del_result;
458         struct ldapsrv_reply *del_reply;
459         void *local_ctx;
460         struct ldb_context *samdb = call->conn->ldb;
461         struct ldb_dn *dn;
462         const char *errstr = NULL;
463         int result = LDAP_SUCCESS;
464         int ldb_ret;
465
466         DEBUG(10, ("DelRequest"));
467         DEBUGADD(10, (" dn: %s", req->dn));
468
469         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
470         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
471
472         dn = ldb_dn_explode(local_ctx, req->dn);
473         VALID_DN_SYNTAX(dn,1);
474
475         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
476
477 reply:
478         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
479         NT_STATUS_HAVE_NO_MEMORY(del_reply);
480
481         if (result == LDAP_SUCCESS) {
482                 ldb_ret = ldb_delete(samdb, dn);
483                 result = map_ldb_error(samdb, ldb_ret, &errstr);
484         }
485
486         del_result = &del_reply->msg->r.DelResponse;
487         del_result->dn = NULL;
488         del_result->resultcode = result;
489         del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
490         del_result->referral = NULL;
491
492         talloc_free(local_ctx);
493
494         ldapsrv_queue_reply(call, del_reply);
495         return NT_STATUS_OK;
496 }
497
498 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
499 {
500         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
501         struct ldap_Result *modifydn;
502         struct ldapsrv_reply *modifydn_r;
503         void *local_ctx;
504         struct ldb_context *samdb = call->conn->ldb;
505         struct ldb_dn *olddn, *newdn, *newrdn;
506         struct ldb_dn *parentdn = NULL;
507         const char *errstr = NULL;
508         int result = LDAP_SUCCESS;
509         int ldb_ret;
510
511         DEBUG(10, ("ModifyDNRequrest"));
512         DEBUGADD(10, (" dn: %s", req->dn));
513         DEBUGADD(10, (" newrdn: %s", req->newrdn));
514
515         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
516         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
517
518         olddn = ldb_dn_explode(local_ctx, req->dn);
519         VALID_DN_SYNTAX(olddn, 2);
520
521         newrdn = ldb_dn_explode(local_ctx, req->newrdn);
522         VALID_DN_SYNTAX(newrdn, 1);
523
524         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
525         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
526
527         /* we can't handle the rename if we should not remove the old dn */
528         if (!req->deleteolddn) {
529                 result = LDAP_UNWILLING_TO_PERFORM;
530                 errstr = "Old RDN must be deleted";
531                 goto reply;
532         }
533
534         if (newrdn->comp_num > 1) {
535                 result = LDAP_NAMING_VIOLATION;
536                 errstr = "Error new RDN invalid";
537                 goto reply;
538         }
539
540         if (req->newsuperior) {
541                 parentdn = ldb_dn_explode(local_ctx, req->newsuperior);
542                 VALID_DN_SYNTAX(parentdn, 0);
543                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
544                 
545                 if (parentdn->comp_num < 1) {
546                         result = LDAP_AFFECTS_MULTIPLE_DSAS;
547                         errstr = "Error new Superior DN invalid";
548                         goto reply;
549                 }
550         }
551
552         if (!parentdn) {
553                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
554                 NT_STATUS_HAVE_NO_MEMORY(parentdn);
555         }
556
557         newdn = ldb_dn_make_child(local_ctx, ldb_dn_get_rdn(local_ctx, newrdn), parentdn);
558         NT_STATUS_HAVE_NO_MEMORY(newdn);
559
560 reply:
561         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
562         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
563
564         if (result == LDAP_SUCCESS) {
565                 ldb_ret = ldb_rename(samdb, olddn, newdn);
566                 result = map_ldb_error(samdb, ldb_ret, &errstr);
567         }
568
569         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
570         modifydn->dn = NULL;
571         modifydn->resultcode = result;
572         modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
573         modifydn->referral = NULL;
574
575         talloc_free(local_ctx);
576
577         ldapsrv_queue_reply(call, modifydn_r);
578         return NT_STATUS_OK;
579 }
580
581 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
582 {
583         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
584         struct ldap_Result *compare;
585         struct ldapsrv_reply *compare_r;
586         void *local_ctx;
587         struct ldb_context *samdb = call->conn->ldb;
588         struct ldb_result *res = NULL;
589         struct ldb_dn *dn;
590         const char *attrs[1];
591         const char *errstr = NULL;
592         const char *filter = NULL;
593         int result = LDAP_SUCCESS;
594         int ldb_ret;
595
596         DEBUG(10, ("CompareRequest"));
597         DEBUGADD(10, (" dn: %s", req->dn));
598
599         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
600         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
601
602         dn = ldb_dn_explode(local_ctx, req->dn);
603         VALID_DN_SYNTAX(dn, 1);
604
605         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
606         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
607                                  (int)req->value.length, req->value.data);
608         NT_STATUS_HAVE_NO_MEMORY(filter);
609
610         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
611
612         attrs[0] = NULL;
613
614 reply:
615         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
616         NT_STATUS_HAVE_NO_MEMORY(compare_r);
617
618         if (result == LDAP_SUCCESS) {
619                 ldb_ret = ldb_search(samdb, dn, LDB_SCOPE_BASE, filter, attrs, &res);
620                 talloc_steal(samdb, res);
621                 if (ldb_ret != LDB_SUCCESS) {
622                         result = map_ldb_error(samdb, ldb_ret, &errstr);
623                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
624                 } else if (res->count == 0) {
625                         DEBUG(10,("CompareRequest: doesn't matched\n"));
626                         result = LDAP_COMPARE_FALSE;
627                         errstr = NULL;
628                 } else if (res->count == 1) {
629                         DEBUG(10,("CompareRequest: matched\n"));
630                         result = LDAP_COMPARE_TRUE;
631                         errstr = NULL;
632                 } else if (res->count > 1) {
633                         result = LDAP_OTHER;
634                         errstr = "too many objects match";
635                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
636                 }
637         }
638
639         compare = &compare_r->msg->r.CompareResponse;
640         compare->dn = NULL;
641         compare->resultcode = result;
642         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
643         compare->referral = NULL;
644
645         talloc_free(local_ctx);
646
647         ldapsrv_queue_reply(call, compare_r);
648         return NT_STATUS_OK;
649 }
650
651 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
652 {
653 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
654         DEBUG(10, ("AbandonRequest\n"));
655         return NT_STATUS_OK;
656 }
657
658 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
659 {
660 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
661         struct ldapsrv_reply *reply;
662
663         DEBUG(10, ("Extended\n"));
664
665         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
666         if (!reply) {
667                 return NT_STATUS_NO_MEMORY;
668         }
669
670         ZERO_STRUCT(reply->msg->r);
671
672         ldapsrv_queue_reply(call, reply);
673         return NT_STATUS_OK;
674 }
675
676 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
677 {
678         switch(call->request->type) {
679         case LDAP_TAG_BindRequest:
680                 return ldapsrv_BindRequest(call);
681         case LDAP_TAG_UnbindRequest:
682                 return ldapsrv_UnbindRequest(call);
683         case LDAP_TAG_SearchRequest:
684                 return ldapsrv_SearchRequest(call);
685         case LDAP_TAG_ModifyRequest:
686                 return ldapsrv_ModifyRequest(call);
687         case LDAP_TAG_AddRequest:
688                 return ldapsrv_AddRequest(call);
689         case LDAP_TAG_DelRequest:
690                 return ldapsrv_DelRequest(call);
691         case LDAP_TAG_ModifyDNRequest:
692                 return ldapsrv_ModifyDNRequest(call);
693         case LDAP_TAG_CompareRequest:
694                 return ldapsrv_CompareRequest(call);
695         case LDAP_TAG_AbandonRequest:
696                 return ldapsrv_AbandonRequest(call);
697         case LDAP_TAG_ExtendedRequest:
698                 return ldapsrv_ExtendedRequest(call);
699         default:
700                 return ldapsrv_unwilling(call, 2);
701         }
702 }
703
704