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