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