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