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