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