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