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