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