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