ef7fb15179d27314045aff0503e3fca9bf33b5ad
[bbaumbach/samba-autobuild/.git] / source4 / ldap_server / ldap_backend.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    Copyright (C) Matthias Dieter Wallnöfer 2009
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "ldap_server/ldap_server.h"
23 #include "../lib/util/dlinklist.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
27 #include "auth/common_auth.h"
28 #include "param/param.h"
29 #include "smbd/service_stream.h"
30 #include "dsdb/samdb/samdb.h"
31 #include <ldb_errors.h>
32 #include <ldb_module.h>
33 #include "ldb_wrap.h"
34 #include "lib/tsocket/tsocket.h"
35 #include "libcli/ldap/ldap_proto.h"
36
37 static int map_ldb_error(TALLOC_CTX *mem_ctx, int ldb_err,
38         const char *add_err_string, const char **errstring)
39 {
40         WERROR err;
41
42         /* Certain LDB modules need to return very special WERROR codes. Proof
43          * for them here and if they exist skip the rest of the mapping. */
44         if (add_err_string != NULL) {
45                 char *endptr;
46                 strtol(add_err_string, &endptr, 16);
47                 if (endptr != add_err_string) {
48                         *errstring = add_err_string;
49                         return ldb_err;
50                 }
51         }
52
53         /* Otherwise we calculate here a generic, but appropriate WERROR. */
54
55         switch (ldb_err) {
56         case LDB_SUCCESS:
57                 err = WERR_OK;
58         break;
59         case LDB_ERR_OPERATIONS_ERROR:
60                 err = WERR_DS_OPERATIONS_ERROR;
61         break;
62         case LDB_ERR_PROTOCOL_ERROR:
63                 err = WERR_DS_PROTOCOL_ERROR;
64         break;
65         case LDB_ERR_TIME_LIMIT_EXCEEDED:
66                 err = WERR_DS_TIMELIMIT_EXCEEDED;
67         break;
68         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
69                 err = WERR_DS_SIZELIMIT_EXCEEDED;
70         break;
71         case LDB_ERR_COMPARE_FALSE:
72                 err = WERR_DS_COMPARE_FALSE;
73         break;
74         case LDB_ERR_COMPARE_TRUE:
75                 err = WERR_DS_COMPARE_TRUE;
76         break;
77         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
78                 err = WERR_DS_AUTH_METHOD_NOT_SUPPORTED;
79         break;
80         case LDB_ERR_STRONG_AUTH_REQUIRED:
81                 err = WERR_DS_STRONG_AUTH_REQUIRED;
82         break;
83         case LDB_ERR_REFERRAL:
84                 err = WERR_DS_REFERRAL;
85         break;
86         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
87                 err = WERR_DS_ADMIN_LIMIT_EXCEEDED;
88         break;
89         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
90                 err = WERR_DS_UNAVAILABLE_CRIT_EXTENSION;
91         break;
92         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
93                 err = WERR_DS_CONFIDENTIALITY_REQUIRED;
94         break;
95         case LDB_ERR_SASL_BIND_IN_PROGRESS:
96                 err = WERR_DS_BUSY;
97         break;
98         case LDB_ERR_NO_SUCH_ATTRIBUTE:
99                 err = WERR_DS_NO_ATTRIBUTE_OR_VALUE;
100         break;
101         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
102                 err = WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
103         break;
104         case LDB_ERR_INAPPROPRIATE_MATCHING:
105                 err = WERR_DS_INAPPROPRIATE_MATCHING;
106         break;
107         case LDB_ERR_CONSTRAINT_VIOLATION:
108                 err = WERR_DS_CONSTRAINT_VIOLATION;
109         break;
110         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
111                 err = WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS;
112         break;
113         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
114                 err = WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
115         break;
116         case LDB_ERR_NO_SUCH_OBJECT:
117                 err = WERR_DS_NO_SUCH_OBJECT;
118         break;
119         case LDB_ERR_ALIAS_PROBLEM:
120                 err = WERR_DS_ALIAS_PROBLEM;
121         break;
122         case LDB_ERR_INVALID_DN_SYNTAX:
123                 err = WERR_DS_INVALID_DN_SYNTAX;
124         break;
125         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
126                 err = WERR_DS_ALIAS_DEREF_PROBLEM;
127         break;
128         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
129                 err = WERR_DS_INAPPROPRIATE_AUTH;
130         break;
131         case LDB_ERR_INVALID_CREDENTIALS:
132                 err = WERR_ACCESS_DENIED;
133         break;
134         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
135                 err = WERR_DS_INSUFF_ACCESS_RIGHTS;
136         break;
137         case LDB_ERR_BUSY:
138                 err = WERR_DS_BUSY;
139         break;
140         case LDB_ERR_UNAVAILABLE:
141                 err = WERR_DS_UNAVAILABLE;
142         break;
143         case LDB_ERR_UNWILLING_TO_PERFORM:
144                 err = WERR_DS_UNWILLING_TO_PERFORM;
145         break;
146         case LDB_ERR_LOOP_DETECT:
147                 err = WERR_DS_LOOP_DETECT;
148         break;
149         case LDB_ERR_NAMING_VIOLATION:
150                 err = WERR_DS_NAMING_VIOLATION;
151         break;
152         case LDB_ERR_OBJECT_CLASS_VIOLATION:
153                 err = WERR_DS_OBJ_CLASS_VIOLATION;
154         break;
155         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
156                 err = WERR_DS_CANT_ON_NON_LEAF;
157         break;
158         case LDB_ERR_NOT_ALLOWED_ON_RDN:
159                 err = WERR_DS_CANT_ON_RDN;
160         break;
161         case LDB_ERR_ENTRY_ALREADY_EXISTS:
162                 err = WERR_DS_OBJ_STRING_NAME_EXISTS;
163         break;
164         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
165                 err = WERR_DS_CANT_MOD_OBJ_CLASS;
166         break;
167         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
168                 err = WERR_DS_AFFECTS_MULTIPLE_DSAS;
169         break;
170         default:
171                 err = WERR_DS_GENERIC_ERROR;
172         break;
173         }
174
175         *errstring = talloc_asprintf(mem_ctx, "%08X: %s", W_ERROR_V(err),
176                 add_err_string != NULL ? add_err_string : ldb_strerror(ldb_err));
177
178         /* result is 1:1 for now */
179         return ldb_err;
180 }
181
182 /*
183   connect to the sam database
184 */
185 int ldapsrv_backend_Init(struct ldapsrv_connection *conn,
186                               char **errstring)
187 {
188         int ret = samdb_connect_url(conn,
189                                     conn->connection->event.ctx,
190                                     conn->lp_ctx,
191                                     conn->session_info,
192                                     conn->global_catalog ? LDB_FLG_RDONLY : 0,
193                                     "sam.ldb",
194                                     conn->connection->remote_address,
195                                     &conn->ldb,
196                                     errstring);
197         if (ret != LDB_SUCCESS) {
198                 return ret;
199         }
200
201         if (conn->server_credentials) {
202                 char **sasl_mechs = NULL;
203                 const struct gensec_security_ops * const *backends = gensec_security_all();
204                 const struct gensec_security_ops **ops
205                         = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
206                 unsigned int i, j = 0;
207                 for (i = 0; ops && ops[i]; i++) {
208                         if (!lpcfg_parm_bool(conn->lp_ctx,  NULL, "gensec", ops[i]->name, ops[i]->enabled))
209                                 continue;
210
211                         if (ops[i]->sasl_name && ops[i]->server_start) {
212                                 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
213
214                                 if (!sasl_name) {
215                                         return LDB_ERR_OPERATIONS_ERROR;
216                                 }
217                                 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
218                                 if (!sasl_mechs) {
219                                         return LDB_ERR_OPERATIONS_ERROR;
220                                 }
221                                 sasl_mechs[j] = sasl_name;
222                                 talloc_steal(sasl_mechs, sasl_name);
223                                 sasl_mechs[j+1] = NULL;
224                                 j++;
225                         }
226                 }
227                 talloc_unlink(conn, ops);
228
229                 /* ldb can have a different lifetime to conn, so we
230                    need to ensure that sasl_mechs lives as long as the
231                    ldb does */
232                 talloc_steal(conn->ldb, sasl_mechs);
233
234                 ldb_set_opaque(conn->ldb, "supportedSASLMechanisms", sasl_mechs);
235         }
236
237         return LDB_SUCCESS;
238 }
239
240 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
241 {
242         struct ldapsrv_reply *reply;
243
244         reply = talloc_zero(call, struct ldapsrv_reply);
245         if (!reply) {
246                 return NULL;
247         }
248         reply->msg = talloc_zero(reply, struct ldap_message);
249         if (reply->msg == NULL) {
250                 talloc_free(reply);
251                 return NULL;
252         }
253
254         reply->msg->messageid = call->request->messageid;
255         reply->msg->type = type;
256         reply->msg->controls = NULL;
257
258         return reply;
259 }
260
261 /*
262  * Encode a reply to an LDAP client as ASN.1, free the original memory
263  */
264 static NTSTATUS ldapsrv_encode(TALLOC_CTX *mem_ctx,
265                                struct ldapsrv_reply *reply)
266 {
267         bool bret = ldap_encode(reply->msg,
268                                 samba_ldap_control_handlers(),
269                                 &reply->blob,
270                                 mem_ctx);
271         TALLOC_FREE(reply->msg);
272         if (!bret) {
273                 DEBUG(0,("Failed to encode ldap reply of type %d: "
274                          "ldap_encode() failed\n",
275                          reply->msg->type));
276                 return NT_STATUS_NO_MEMORY;
277         }
278
279         talloc_set_name_const(reply->blob.data,
280                               "Outgoing, encoded single LDAP reply");
281
282         return NT_STATUS_OK;
283 }
284
285 /*
286  * Queue a reply (encoding it also), even if it would exceed the
287  * limit.  This allows the error packet with LDAP_SIZE_LIMIT_EXCEEDED
288  * to be sent
289  */
290 static NTSTATUS ldapsrv_queue_reply_forced(struct ldapsrv_call *call,
291                                            struct ldapsrv_reply *reply)
292 {
293         NTSTATUS status = ldapsrv_encode(call, reply);
294
295         if (NT_STATUS_IS_OK(status)) {
296                 DLIST_ADD_END(call->replies, reply);
297         }
298         return status;
299 }
300
301 /*
302  * Queue a reply (encoding it also) but check we do not send more than
303  * LDAP_SERVER_MAX_REPLY_SIZE of responses as a way to limit the
304  * amount of data a client can make us allocate.
305  */
306 NTSTATUS ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
307 {
308         NTSTATUS status = ldapsrv_encode(call, reply);
309
310         if (!NT_STATUS_IS_OK(status)) {
311                 return status;
312         }
313
314         if (call->reply_size > call->reply_size + reply->blob.length
315             || call->reply_size + reply->blob.length > LDAP_SERVER_MAX_REPLY_SIZE) {
316                 DBG_WARNING("Refusing to queue LDAP search response size "
317                             "of more than %zu bytes\n",
318                             LDAP_SERVER_MAX_REPLY_SIZE);
319                 TALLOC_FREE(reply->blob.data);
320                 return NT_STATUS_FILE_TOO_LARGE;
321         }
322
323         call->reply_size += reply->blob.length;
324
325         DLIST_ADD_END(call->replies, reply);
326
327         return status;
328 }
329
330 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
331 {
332         struct ldapsrv_reply *reply;
333         struct ldap_ExtendedResponse *r;
334
335         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
336
337         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
338         if (!reply) {
339                 return NT_STATUS_NO_MEMORY;
340         }
341
342         r = &reply->msg->r.ExtendedResponse;
343         r->response.resultcode = error;
344         r->response.dn = NULL;
345         r->response.errormessage = NULL;
346         r->response.referral = NULL;
347         r->oid = NULL;
348         r->value = NULL;
349
350         ldapsrv_queue_reply(call, reply);
351         return NT_STATUS_OK;
352 }
353
354 static int ldapsrv_add_with_controls(struct ldapsrv_call *call,
355                                      const struct ldb_message *message,
356                                      struct ldb_control **controls,
357                                      struct ldb_result *res)
358 {
359         struct ldb_context *ldb = call->conn->ldb;
360         struct ldb_request *req;
361         int ret;
362
363         ret = ldb_msg_sanity_check(ldb, message);
364         if (ret != LDB_SUCCESS) {
365                 return ret;
366         }
367
368         ret = ldb_build_add_req(&req, ldb, ldb,
369                                         message,
370                                         controls,
371                                         res,
372                                         ldb_modify_default_callback,
373                                         NULL);
374
375         if (ret != LDB_SUCCESS) return ret;
376
377         if (call->conn->global_catalog) {
378                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
379         }
380         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
381
382         ret = ldb_transaction_start(ldb);
383         if (ret != LDB_SUCCESS) {
384                 return ret;
385         }
386
387         if (!call->conn->is_privileged) {
388                 ldb_req_mark_untrusted(req);
389         }
390
391         LDB_REQ_SET_LOCATION(req);
392
393         ret = ldb_request(ldb, req);
394         if (ret == LDB_SUCCESS) {
395                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
396         }
397
398         if (ret == LDB_SUCCESS) {
399                 ret = ldb_transaction_commit(ldb);
400         }
401         else {
402                 ldb_transaction_cancel(ldb);
403         }
404
405         talloc_free(req);
406         return ret;
407 }
408
409 /* create and execute a modify request */
410 static int ldapsrv_mod_with_controls(struct ldapsrv_call *call,
411                                      const struct ldb_message *message,
412                                      struct ldb_control **controls,
413                                      struct ldb_result *res)
414 {
415         struct ldb_context *ldb = call->conn->ldb;
416         struct ldb_request *req;
417         int ret;
418
419         ret = ldb_msg_sanity_check(ldb, message);
420         if (ret != LDB_SUCCESS) {
421                 return ret;
422         }
423
424         ret = ldb_build_mod_req(&req, ldb, ldb,
425                                         message,
426                                         controls,
427                                         res,
428                                         ldb_modify_default_callback,
429                                         NULL);
430
431         if (ret != LDB_SUCCESS) {
432                 return ret;
433         }
434
435         if (call->conn->global_catalog) {
436                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
437         }
438         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
439
440         ret = ldb_transaction_start(ldb);
441         if (ret != LDB_SUCCESS) {
442                 return ret;
443         }
444
445         if (!call->conn->is_privileged) {
446                 ldb_req_mark_untrusted(req);
447         }
448
449         LDB_REQ_SET_LOCATION(req);
450
451         ret = ldb_request(ldb, req);
452         if (ret == LDB_SUCCESS) {
453                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
454         }
455
456         if (ret == LDB_SUCCESS) {
457                 ret = ldb_transaction_commit(ldb);
458         }
459         else {
460                 ldb_transaction_cancel(ldb);
461         }
462
463         talloc_free(req);
464         return ret;
465 }
466
467 /* create and execute a delete request */
468 static int ldapsrv_del_with_controls(struct ldapsrv_call *call,
469                                      struct ldb_dn *dn,
470                                      struct ldb_control **controls,
471                                      struct ldb_result *res)
472 {
473         struct ldb_context *ldb = call->conn->ldb;
474         struct ldb_request *req;
475         int ret;
476
477         ret = ldb_build_del_req(&req, ldb, ldb,
478                                         dn,
479                                         controls,
480                                         res,
481                                         ldb_modify_default_callback,
482                                         NULL);
483
484         if (ret != LDB_SUCCESS) return ret;
485
486         if (call->conn->global_catalog) {
487                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
488         }
489         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
490
491         ret = ldb_transaction_start(ldb);
492         if (ret != LDB_SUCCESS) {
493                 return ret;
494         }
495
496         if (!call->conn->is_privileged) {
497                 ldb_req_mark_untrusted(req);
498         }
499
500         LDB_REQ_SET_LOCATION(req);
501
502         ret = ldb_request(ldb, req);
503         if (ret == LDB_SUCCESS) {
504                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
505         }
506
507         if (ret == LDB_SUCCESS) {
508                 ret = ldb_transaction_commit(ldb);
509         }
510         else {
511                 ldb_transaction_cancel(ldb);
512         }
513
514         talloc_free(req);
515         return ret;
516 }
517
518 static int ldapsrv_rename_with_controls(struct ldapsrv_call *call,
519                                         struct ldb_dn *olddn,
520                                         struct ldb_dn *newdn,
521                                         struct ldb_control **controls,
522                                         struct ldb_result *res)
523 {
524         struct ldb_context *ldb = call->conn->ldb;
525         struct ldb_request *req;
526         int ret;
527
528         ret = ldb_build_rename_req(&req, ldb, ldb,
529                                         olddn,
530                                         newdn,
531                                         controls,
532                                         res,
533                                         ldb_modify_default_callback,
534                                         NULL);
535
536         if (ret != LDB_SUCCESS) return ret;
537
538         if (call->conn->global_catalog) {
539                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
540         }
541         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
542
543         ret = ldb_transaction_start(ldb);
544         if (ret != LDB_SUCCESS) {
545                 return ret;
546         }
547
548         if (!call->conn->is_privileged) {
549                 ldb_req_mark_untrusted(req);
550         }
551
552         LDB_REQ_SET_LOCATION(req);
553
554         ret = ldb_request(ldb, req);
555         if (ret == LDB_SUCCESS) {
556                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
557         }
558
559         if (ret == LDB_SUCCESS) {
560                 ret = ldb_transaction_commit(ldb);
561         }
562         else {
563                 ldb_transaction_cancel(ldb);
564         }
565
566         talloc_free(req);
567         return ret;
568 }
569
570
571
572 struct ldapsrv_context {
573         struct ldapsrv_call *call;
574         int extended_type;
575         bool attributesonly;
576         struct ldb_control **controls;
577         size_t count; /* For notificaiton only */
578 };
579
580 static int ldap_server_search_callback(struct ldb_request *req, struct ldb_reply *ares)
581 {
582         struct ldapsrv_context *ctx = talloc_get_type(req->context, struct ldapsrv_context);
583         struct ldapsrv_call *call = ctx->call;
584         struct ldb_context *ldb = call->conn->ldb;
585         unsigned int j;
586         struct ldapsrv_reply *ent_r = NULL;
587         struct ldap_SearchResEntry *ent;
588         int ret;
589         NTSTATUS status;
590
591         if (!ares) {
592                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
593         }
594         if (ares->error != LDB_SUCCESS) {
595                 return ldb_request_done(req, ares->error);
596         }
597
598         switch (ares->type) {
599         case LDB_REPLY_ENTRY:
600         {
601                 struct ldb_message *msg = ares->message;
602                 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
603                 if (ent_r == NULL) {
604                         return ldb_oom(ldb);
605                 }
606
607                 ctx->count++;
608
609                 /*
610                  * Put the LDAP search response data under ent_r->msg
611                  * so we can free that later once encoded
612                  */
613                 talloc_steal(ent_r->msg, msg);
614
615                 ent = &ent_r->msg->r.SearchResultEntry;
616                 ent->dn = ldb_dn_get_extended_linearized(ent_r, msg->dn,
617                                                          ctx->extended_type);
618                 ent->num_attributes = 0;
619                 ent->attributes = NULL;
620                 if (msg->num_elements == 0) {
621                         goto queue_reply;
622                 }
623                 ent->num_attributes = msg->num_elements;
624                 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
625                 if (ent->attributes == NULL) {
626                         return ldb_oom(ldb);
627                 }
628
629                 for (j=0; j < ent->num_attributes; j++) {
630                         ent->attributes[j].name = msg->elements[j].name;
631                         ent->attributes[j].num_values = 0;
632                         ent->attributes[j].values = NULL;
633                         if (ctx->attributesonly && (msg->elements[j].num_values == 0)) {
634                                 continue;
635                         }
636                         ent->attributes[j].num_values = msg->elements[j].num_values;
637                         ent->attributes[j].values = msg->elements[j].values;
638                 }
639 queue_reply:
640                 status = ldapsrv_queue_reply(call, ent_r);
641                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_TOO_LARGE)) {
642                         ret = ldb_request_done(req,
643                                                LDB_ERR_SIZE_LIMIT_EXCEEDED);
644                         ldb_asprintf_errstring(ldb,
645                                                "LDAP search response size "
646                                                "limited to %zu bytes\n",
647                                                LDAP_SERVER_MAX_REPLY_SIZE);
648                 } else if (!NT_STATUS_IS_OK(status)) {
649                         ret = ldb_request_done(req,
650                                                ldb_operr(ldb));
651                 } else {
652                         ret = LDB_SUCCESS;
653                 }
654                 break;
655         }
656         case LDB_REPLY_REFERRAL:
657         {
658                 struct ldap_SearchResRef *ent_ref;
659
660                 /*
661                  * TODO: This should be handled by the notification
662                  * module not here
663                  */
664                 if (call->notification.busy) {
665                         ret = LDB_SUCCESS;
666                         break;
667                 }
668
669                 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
670                 if (ent_r == NULL) {
671                         return ldb_oom(ldb);
672                 }
673
674                 /*
675                  * Put the LDAP referral data under ent_r->msg
676                  * so we can free that later once encoded
677                  */
678                 talloc_steal(ent_r->msg, ares->referral);
679
680                 ent_ref = &ent_r->msg->r.SearchResultReference;
681                 ent_ref->referral = ares->referral;
682
683                 status = ldapsrv_queue_reply(call, ent_r);
684                 if (!NT_STATUS_IS_OK(status)) {
685                         ret = LDB_ERR_OPERATIONS_ERROR;
686                 } else {
687                         ret = LDB_SUCCESS;
688                 }
689                 break;
690         }
691         case LDB_REPLY_DONE:
692         {
693                 /*
694                  * We don't queue the reply for this one, we let that
695                  * happen outside
696                  */
697                 ctx->controls = talloc_move(ctx, &ares->controls);
698
699                 TALLOC_FREE(ares);
700                 return ldb_request_done(req, LDB_SUCCESS);
701         }
702         default:
703                 /* Doesn't happen */
704                 ret = LDB_ERR_OPERATIONS_ERROR;
705         }
706         TALLOC_FREE(ares);
707
708         return ret;
709 }
710
711
712 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
713 {
714         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
715         struct ldap_Result *done;
716         struct ldapsrv_reply *done_r;
717         TALLOC_CTX *local_ctx;
718         struct ldapsrv_context *callback_ctx = NULL;
719         struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
720         struct ldb_dn *basedn;
721         struct ldb_request *lreq;
722         struct ldb_control *search_control;
723         struct ldb_search_options_control *search_options;
724         struct ldb_control *extended_dn_control;
725         struct ldb_extended_dn_control *extended_dn_decoded = NULL;
726         struct ldb_control *notification_control = NULL;
727         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
728         const char **attrs = NULL;
729         const char *scope_str, *errstr = NULL;
730         int result = -1;
731         int ldb_ret = -1;
732         unsigned int i;
733         int extended_type = 1;
734
735         DEBUG(10, ("SearchRequest"));
736         DEBUGADD(10, (" basedn: %s", req->basedn));
737         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
738
739         local_ctx = talloc_new(call);
740         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
741
742         basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
743         NT_STATUS_HAVE_NO_MEMORY(basedn);
744
745         DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
746         DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
747
748         switch (req->scope) {
749                 case LDAP_SEARCH_SCOPE_BASE:
750                         scope_str = "BASE";
751                         scope = LDB_SCOPE_BASE;
752                         break;
753                 case LDAP_SEARCH_SCOPE_SINGLE:
754                         scope_str = "ONE";
755                         scope = LDB_SCOPE_ONELEVEL;
756                         break;
757                 case LDAP_SEARCH_SCOPE_SUB:
758                         scope_str = "SUB";
759                         scope = LDB_SCOPE_SUBTREE;
760                         break;
761                 default:
762                         result = LDAP_PROTOCOL_ERROR;
763                         map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
764                                 &errstr);
765                         errstr = talloc_asprintf(local_ctx,
766                                 "%s. Invalid scope", errstr);
767                         goto reply;
768         }
769         DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
770
771         if (req->num_attributes >= 1) {
772                 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
773                 NT_STATUS_HAVE_NO_MEMORY(attrs);
774
775                 for (i=0; i < req->num_attributes; i++) {
776                         DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
777                         attrs[i] = req->attributes[i];
778                 }
779                 attrs[i] = NULL;
780         }
781
782         DEBUG(5,("ldb_request %s dn=%s filter=%s\n", 
783                  scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
784
785         callback_ctx = talloc_zero(local_ctx, struct ldapsrv_context);
786         NT_STATUS_HAVE_NO_MEMORY(callback_ctx);
787         callback_ctx->call = call;
788         callback_ctx->extended_type = extended_type;
789         callback_ctx->attributesonly = req->attributesonly;
790
791         ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
792                                           basedn, scope,
793                                           req->tree, attrs,
794                                           call->request->controls,
795                                           callback_ctx,
796                                           ldap_server_search_callback,
797                                           NULL);
798
799         if (ldb_ret != LDB_SUCCESS) {
800                 goto reply;
801         }
802
803         if (call->conn->global_catalog) {
804                 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
805
806                 search_options = NULL;
807                 if (search_control) {
808                         search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
809                         search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
810                 } else {
811                         search_options = talloc(lreq, struct ldb_search_options_control);
812                         NT_STATUS_HAVE_NO_MEMORY(search_options);
813                         search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
814                         ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
815                 }
816         } else {
817                 ldb_request_add_control(lreq, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
818         }
819
820         extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
821
822         if (extended_dn_control) {
823                 if (extended_dn_control->data) {
824                         extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
825                         extended_type = extended_dn_decoded->type;
826                 } else {
827                         extended_type = 0;
828                 }
829         }
830
831         notification_control = ldb_request_get_control(lreq, LDB_CONTROL_NOTIFICATION_OID);
832         if (notification_control != NULL) {
833                 const struct ldapsrv_call *pc = NULL;
834                 size_t count = 0;
835
836                 for (pc = call->conn->pending_calls; pc != NULL; pc = pc->next) {
837                         count += 1;
838                 }
839
840                 if (count >= call->conn->limits.max_notifications) {
841                         DEBUG(10,("SearchRequest: error MaxNotificationPerConn\n"));
842                         result = map_ldb_error(local_ctx,
843                                                LDB_ERR_ADMIN_LIMIT_EXCEEDED,
844                                                "MaxNotificationPerConn reached",
845                                                &errstr);
846                         goto reply;
847                 }
848
849                 /*
850                  * For now we need to do periodic retries on our own.
851                  * As the dsdb_notification module will return after each run.
852                  */
853                 call->notification.busy = true;
854         }
855
856         ldb_set_timeout(samdb, lreq, req->timelimit);
857
858         if (!call->conn->is_privileged) {
859                 ldb_req_mark_untrusted(lreq);
860         }
861
862         LDB_REQ_SET_LOCATION(lreq);
863
864         ldb_ret = ldb_request(samdb, lreq);
865
866         if (ldb_ret != LDB_SUCCESS) {
867                 goto reply;
868         }
869
870         ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
871
872         if (ldb_ret == LDB_SUCCESS) {
873                 if (call->notification.busy) {
874                         /* Move/Add it to the end */
875                         DLIST_DEMOTE(call->conn->pending_calls, call);
876                         call->notification.generation =
877                                 call->conn->service->notification.generation;
878
879                         if (callback_ctx->count != 0) {
880                                 call->notification.generation += 1;
881                                 ldapsrv_notification_retry_setup(call->conn->service,
882                                                                  true);
883                         }
884
885                         talloc_free(local_ctx);
886                         return NT_STATUS_OK;
887                 }
888         }
889
890 reply:
891         DLIST_REMOVE(call->conn->pending_calls, call);
892         call->notification.busy = false;
893
894         done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
895         NT_STATUS_HAVE_NO_MEMORY(done_r);
896
897         done = &done_r->msg->r.SearchResultDone;
898         done->dn = NULL;
899         done->referral = NULL;
900
901         if (result != -1) {
902         } else if (ldb_ret == LDB_SUCCESS) {
903                 if (callback_ctx->controls) {
904                         done_r->msg->controls = callback_ctx->controls;
905                         talloc_steal(done_r->msg, callback_ctx->controls);
906                 }
907                 result = LDB_SUCCESS;
908         } else {
909                 DEBUG(10,("SearchRequest: error\n"));
910                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
911                                        &errstr);
912         }
913
914         done->resultcode = result;
915         done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
916
917         talloc_free(local_ctx);
918
919         return ldapsrv_queue_reply_forced(call, done_r);
920 }
921
922 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
923 {
924         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
925         struct ldap_Result *modify_result;
926         struct ldapsrv_reply *modify_reply;
927         TALLOC_CTX *local_ctx;
928         struct ldb_context *samdb = call->conn->ldb;
929         struct ldb_message *msg = NULL;
930         struct ldb_dn *dn;
931         const char *errstr = NULL;
932         int result = LDAP_SUCCESS;
933         int ldb_ret;
934         unsigned int i,j;
935         struct ldb_result *res = NULL;
936
937         DEBUG(10, ("ModifyRequest"));
938         DEBUGADD(10, (" dn: %s\n", req->dn));
939
940         local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
941         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
942
943         dn = ldb_dn_new(local_ctx, samdb, req->dn);
944         NT_STATUS_HAVE_NO_MEMORY(dn);
945
946         DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
947
948         msg = ldb_msg_new(local_ctx);
949         NT_STATUS_HAVE_NO_MEMORY(msg);
950
951         msg->dn = dn;
952
953         if (req->num_mods > 0) {
954                 msg->num_elements = req->num_mods;
955                 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
956                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
957
958                 for (i=0; i < msg->num_elements; i++) {
959                         msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
960                         msg->elements[i].num_values = 0;
961                         msg->elements[i].values = NULL;
962
963                         switch (req->mods[i].type) {
964                         default:
965                                 result = LDAP_PROTOCOL_ERROR;
966                                 map_ldb_error(local_ctx,
967                                         LDB_ERR_PROTOCOL_ERROR, NULL, &errstr);
968                                 errstr = talloc_asprintf(local_ctx,
969                                         "%s. Invalid LDAP_MODIFY_* type", errstr);
970                                 goto reply;
971                         case LDAP_MODIFY_ADD:
972                                 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
973                                 break;
974                         case LDAP_MODIFY_DELETE:
975                                 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
976                                 break;
977                         case LDAP_MODIFY_REPLACE:
978                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
979                                 break;
980                         }
981
982                         msg->elements[i].num_values = req->mods[i].attrib.num_values;
983                         if (msg->elements[i].num_values > 0) {
984                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
985                                                                        msg->elements[i].num_values);
986                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
987
988                                 for (j=0; j < msg->elements[i].num_values; j++) {
989                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
990                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
991                                 }
992                         }
993                 }
994         }
995
996 reply:
997         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
998         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
999
1000         if (result == LDAP_SUCCESS) {
1001                 res = talloc_zero(local_ctx, struct ldb_result);
1002                 NT_STATUS_HAVE_NO_MEMORY(res);
1003                 ldb_ret = ldapsrv_mod_with_controls(call, msg, call->request->controls, res);
1004                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1005                                        &errstr);
1006         }
1007
1008         modify_result = &modify_reply->msg->r.ModifyResponse;
1009         modify_result->dn = NULL;
1010         if ((res != NULL) && (res->refs != NULL)) {
1011                 modify_result->resultcode = map_ldb_error(local_ctx,
1012                                                           LDB_ERR_REFERRAL,
1013                                                           NULL, &errstr);
1014                 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
1015                 modify_result->referral = talloc_strdup(call, *res->refs);
1016         } else {
1017                 modify_result->resultcode = result;
1018                 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
1019                 modify_result->referral = NULL;
1020         }
1021         talloc_free(local_ctx);
1022
1023         return ldapsrv_queue_reply(call, modify_reply);
1024
1025 }
1026
1027 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
1028 {
1029         struct ldap_AddRequest *req = &call->request->r.AddRequest;
1030         struct ldap_Result *add_result;
1031         struct ldapsrv_reply *add_reply;
1032         TALLOC_CTX *local_ctx;
1033         struct ldb_context *samdb = call->conn->ldb;
1034         struct ldb_message *msg = NULL;
1035         struct ldb_dn *dn;
1036         const char *errstr = NULL;
1037         int result = LDAP_SUCCESS;
1038         int ldb_ret;
1039         unsigned int i,j;
1040         struct ldb_result *res = NULL;
1041
1042         DEBUG(10, ("AddRequest"));
1043         DEBUGADD(10, (" dn: %s\n", req->dn));
1044
1045         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
1046         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1047
1048         dn = ldb_dn_new(local_ctx, samdb, req->dn);
1049         NT_STATUS_HAVE_NO_MEMORY(dn);
1050
1051         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
1052
1053         msg = talloc(local_ctx, struct ldb_message);
1054         NT_STATUS_HAVE_NO_MEMORY(msg);
1055
1056         msg->dn = dn;
1057         msg->num_elements = 0;
1058         msg->elements = NULL;
1059
1060         if (req->num_attributes > 0) {
1061                 msg->num_elements = req->num_attributes;
1062                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
1063                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
1064
1065                 for (i=0; i < msg->num_elements; i++) {
1066                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
1067                         msg->elements[i].flags = 0;
1068                         msg->elements[i].num_values = 0;
1069                         msg->elements[i].values = NULL;
1070                         
1071                         if (req->attributes[i].num_values > 0) {
1072                                 msg->elements[i].num_values = req->attributes[i].num_values;
1073                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
1074                                                                        msg->elements[i].num_values);
1075                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
1076
1077                                 for (j=0; j < msg->elements[i].num_values; j++) {
1078                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
1079                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
1080                                 }
1081                         }
1082                 }
1083         }
1084
1085         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
1086         NT_STATUS_HAVE_NO_MEMORY(add_reply);
1087
1088         if (result == LDAP_SUCCESS) {
1089                 res = talloc_zero(local_ctx, struct ldb_result);
1090                 NT_STATUS_HAVE_NO_MEMORY(res);
1091                 ldb_ret = ldapsrv_add_with_controls(call, msg, call->request->controls, res);
1092                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1093                                        &errstr);
1094         }
1095
1096         add_result = &add_reply->msg->r.AddResponse;
1097         add_result->dn = NULL;
1098         if ((res != NULL) && (res->refs != NULL)) {
1099                 add_result->resultcode =  map_ldb_error(local_ctx,
1100                                                         LDB_ERR_REFERRAL, NULL,
1101                                                         &errstr);
1102                 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
1103                 add_result->referral = talloc_strdup(call, *res->refs);
1104         } else {
1105                 add_result->resultcode = result;
1106                 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
1107                 add_result->referral = NULL;
1108         }
1109         talloc_free(local_ctx);
1110
1111         return ldapsrv_queue_reply(call, add_reply);
1112
1113 }
1114
1115 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
1116 {
1117         struct ldap_DelRequest *req = &call->request->r.DelRequest;
1118         struct ldap_Result *del_result;
1119         struct ldapsrv_reply *del_reply;
1120         TALLOC_CTX *local_ctx;
1121         struct ldb_context *samdb = call->conn->ldb;
1122         struct ldb_dn *dn;
1123         const char *errstr = NULL;
1124         int result = LDAP_SUCCESS;
1125         int ldb_ret;
1126         struct ldb_result *res = NULL;
1127
1128         DEBUG(10, ("DelRequest"));
1129         DEBUGADD(10, (" dn: %s\n", req->dn));
1130
1131         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
1132         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1133
1134         dn = ldb_dn_new(local_ctx, samdb, req->dn);
1135         NT_STATUS_HAVE_NO_MEMORY(dn);
1136
1137         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
1138
1139         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
1140         NT_STATUS_HAVE_NO_MEMORY(del_reply);
1141
1142         if (result == LDAP_SUCCESS) {
1143                 res = talloc_zero(local_ctx, struct ldb_result);
1144                 NT_STATUS_HAVE_NO_MEMORY(res);
1145                 ldb_ret = ldapsrv_del_with_controls(call, dn, call->request->controls, res);
1146                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1147                                        &errstr);
1148         }
1149
1150         del_result = &del_reply->msg->r.DelResponse;
1151         del_result->dn = NULL;
1152         if ((res != NULL) && (res->refs != NULL)) {
1153                 del_result->resultcode = map_ldb_error(local_ctx,
1154                                                        LDB_ERR_REFERRAL, NULL,
1155                                                        &errstr);
1156                 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
1157                 del_result->referral = talloc_strdup(call, *res->refs);
1158         } else {
1159                 del_result->resultcode = result;
1160                 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
1161                 del_result->referral = NULL;
1162         }
1163
1164         talloc_free(local_ctx);
1165
1166         return ldapsrv_queue_reply(call, del_reply);
1167 }
1168
1169 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
1170 {
1171         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
1172         struct ldap_Result *modifydn;
1173         struct ldapsrv_reply *modifydn_r;
1174         TALLOC_CTX *local_ctx;
1175         struct ldb_context *samdb = call->conn->ldb;
1176         struct ldb_dn *olddn, *newdn=NULL, *newrdn;
1177         struct ldb_dn *parentdn = NULL;
1178         const char *errstr = NULL;
1179         int result = LDAP_SUCCESS;
1180         int ldb_ret;
1181         struct ldb_result *res = NULL;
1182
1183         DEBUG(10, ("ModifyDNRequest"));
1184         DEBUGADD(10, (" dn: %s", req->dn));
1185         DEBUGADD(10, (" newrdn: %s\n", req->newrdn));
1186
1187         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
1188         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1189
1190         olddn = ldb_dn_new(local_ctx, samdb, req->dn);
1191         NT_STATUS_HAVE_NO_MEMORY(olddn);
1192
1193         newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
1194         NT_STATUS_HAVE_NO_MEMORY(newrdn);
1195
1196         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
1197         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
1198
1199         if (ldb_dn_get_comp_num(newrdn) == 0) {
1200                 result = LDAP_PROTOCOL_ERROR;
1201                 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
1202                               &errstr);
1203                 goto reply;
1204         }
1205
1206         if (ldb_dn_get_comp_num(newrdn) > 1) {
1207                 result = LDAP_NAMING_VIOLATION;
1208                 map_ldb_error(local_ctx, LDB_ERR_NAMING_VIOLATION, NULL,
1209                               &errstr);
1210                 goto reply;
1211         }
1212
1213         /* we can't handle the rename if we should not remove the old dn */
1214         if (!req->deleteolddn) {
1215                 result = LDAP_UNWILLING_TO_PERFORM;
1216                 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, NULL,
1217                               &errstr);
1218                 errstr = talloc_asprintf(local_ctx,
1219                         "%s. Old RDN must be deleted", errstr);
1220                 goto reply;
1221         }
1222
1223         if (req->newsuperior) {
1224                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
1225                 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
1226         }
1227
1228         if (!parentdn) {
1229                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
1230         }
1231         if (!parentdn) {
1232                 result = LDAP_NO_SUCH_OBJECT;
1233                 map_ldb_error(local_ctx, LDB_ERR_NO_SUCH_OBJECT, NULL, &errstr);
1234                 goto reply;
1235         }
1236
1237         if ( ! ldb_dn_add_child(parentdn, newrdn)) {
1238                 result = LDAP_OTHER;
1239                 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1240                 goto reply;
1241         }
1242         newdn = parentdn;
1243
1244 reply:
1245         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
1246         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
1247
1248         if (result == LDAP_SUCCESS) {
1249                 res = talloc_zero(local_ctx, struct ldb_result);
1250                 NT_STATUS_HAVE_NO_MEMORY(res);
1251                 ldb_ret = ldapsrv_rename_with_controls(call, olddn, newdn, call->request->controls, res);
1252                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1253                                        &errstr);
1254         }
1255
1256         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
1257         modifydn->dn = NULL;
1258         if ((res != NULL) && (res->refs != NULL)) {
1259                 modifydn->resultcode = map_ldb_error(local_ctx,
1260                                                      LDB_ERR_REFERRAL, NULL,
1261                                                      &errstr);;
1262                 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1263                 modifydn->referral = talloc_strdup(call, *res->refs);
1264         } else {
1265                 modifydn->resultcode = result;
1266                 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1267                 modifydn->referral = NULL;
1268         }
1269
1270         talloc_free(local_ctx);
1271
1272         return ldapsrv_queue_reply(call, modifydn_r);
1273 }
1274
1275 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
1276 {
1277         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
1278         struct ldap_Result *compare;
1279         struct ldapsrv_reply *compare_r;
1280         TALLOC_CTX *local_ctx;
1281         struct ldb_context *samdb = call->conn->ldb;
1282         struct ldb_result *res = NULL;
1283         struct ldb_dn *dn;
1284         const char *attrs[1];
1285         const char *errstr = NULL;
1286         const char *filter = NULL;
1287         int result = LDAP_SUCCESS;
1288         int ldb_ret;
1289
1290         DEBUG(10, ("CompareRequest"));
1291         DEBUGADD(10, (" dn: %s\n", req->dn));
1292
1293         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
1294         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1295
1296         dn = ldb_dn_new(local_ctx, samdb, req->dn);
1297         NT_STATUS_HAVE_NO_MEMORY(dn);
1298
1299         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
1300         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
1301                                  (int)req->value.length, req->value.data);
1302         NT_STATUS_HAVE_NO_MEMORY(filter);
1303
1304         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
1305
1306         attrs[0] = NULL;
1307
1308         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
1309         NT_STATUS_HAVE_NO_MEMORY(compare_r);
1310
1311         if (result == LDAP_SUCCESS) {
1312                 ldb_ret = ldb_search(samdb, local_ctx, &res,
1313                                      dn, LDB_SCOPE_BASE, attrs, "%s", filter);
1314                 if (ldb_ret != LDB_SUCCESS) {
1315                         result = map_ldb_error(local_ctx, ldb_ret,
1316                                                ldb_errstring(samdb), &errstr);
1317                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
1318                 } else if (res->count == 0) {
1319                         DEBUG(10,("CompareRequest: doesn't matched\n"));
1320                         result = LDAP_COMPARE_FALSE;
1321                         errstr = NULL;
1322                 } else if (res->count == 1) {
1323                         DEBUG(10,("CompareRequest: matched\n"));
1324                         result = LDAP_COMPARE_TRUE;
1325                         errstr = NULL;
1326                 } else if (res->count > 1) {
1327                         result = LDAP_OTHER;
1328                         map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1329                         errstr = talloc_asprintf(local_ctx,
1330                                 "%s. Too many objects match!", errstr);
1331                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
1332                 }
1333         }
1334
1335         compare = &compare_r->msg->r.CompareResponse;
1336         compare->dn = NULL;
1337         compare->resultcode = result;
1338         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
1339         compare->referral = NULL;
1340
1341         talloc_free(local_ctx);
1342
1343         return ldapsrv_queue_reply(call, compare_r);
1344 }
1345
1346 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
1347 {
1348         struct ldap_AbandonRequest *req = &call->request->r.AbandonRequest;
1349         struct ldapsrv_call *c = NULL;
1350         struct ldapsrv_call *n = NULL;
1351
1352         DEBUG(10, ("AbandonRequest\n"));
1353
1354         for (c = call->conn->pending_calls; c != NULL; c = n) {
1355                 n = c->next;
1356
1357                 if (c->request->messageid != req->messageid) {
1358                         continue;
1359                 }
1360
1361                 DLIST_REMOVE(call->conn->pending_calls, c);
1362                 TALLOC_FREE(c);
1363         }
1364
1365         return NT_STATUS_OK;
1366 }
1367
1368 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
1369 {
1370         unsigned int i;
1371         struct ldap_message *msg = call->request;
1372         NTSTATUS status;
1373
1374         /* Check for undecoded critical extensions */
1375         for (i=0; msg->controls && msg->controls[i]; i++) {
1376                 if (!msg->controls_decoded[i] && 
1377                     msg->controls[i]->critical) {
1378                         DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1379                                   msg->controls[i]->oid));
1380                         return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
1381                 }
1382         }
1383
1384         if (call->conn->authz_logged == false) {
1385                 bool log = true;
1386
1387                 /*
1388                  * We do not want to log anonymous access if the query
1389                  * is just for the rootDSE, or it is a startTLS or a
1390                  * Bind.
1391                  *
1392                  * A rootDSE search could also be done over
1393                  * CLDAP anonymously for example, so these don't
1394                  * really count.
1395                  * Essentially we want to know about
1396                  * access beyond that normally done prior to a
1397                  * bind.
1398                  */
1399
1400                 switch(call->request->type) {
1401                 case LDAP_TAG_BindRequest:
1402                 case LDAP_TAG_UnbindRequest:
1403                 case LDAP_TAG_AbandonRequest:
1404                         log = false;
1405                         break;
1406                 case LDAP_TAG_ExtendedResponse: {
1407                         struct ldap_ExtendedRequest *req = &call->request->r.ExtendedRequest;
1408                         if (strcmp(req->oid, LDB_EXTENDED_START_TLS_OID) == 0) {
1409                                 log = false;
1410                         }
1411                         break;
1412                 }
1413                 case LDAP_TAG_SearchRequest: {
1414                         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
1415                         if (req->scope == LDAP_SEARCH_SCOPE_BASE) {
1416                                 if (req->basedn[0] == '\0') {
1417                                         log = false;
1418                                 }
1419                         }
1420                         break;
1421                 }
1422                 default:
1423                         break;
1424                 }
1425
1426                 if (log) {
1427                         const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
1428                         if (call->conn->sockets.active == call->conn->sockets.tls) {
1429                                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_TLS;
1430                         }
1431
1432                         log_successful_authz_event(call->conn->connection->msg_ctx,
1433                                                    call->conn->connection->lp_ctx,
1434                                                    call->conn->connection->remote_address,
1435                                                    call->conn->connection->local_address,
1436                                                    "LDAP",
1437                                                    "no bind",
1438                                                    transport_protection,
1439                                                    call->conn->session_info);
1440
1441                         call->conn->authz_logged = true;
1442                 }
1443         }
1444
1445         switch(call->request->type) {
1446         case LDAP_TAG_BindRequest:
1447                 return ldapsrv_BindRequest(call);
1448         case LDAP_TAG_UnbindRequest:
1449                 return ldapsrv_UnbindRequest(call);
1450         case LDAP_TAG_SearchRequest:
1451                 return ldapsrv_SearchRequest(call);
1452         case LDAP_TAG_ModifyRequest:
1453                 status = ldapsrv_ModifyRequest(call);
1454                 break;
1455         case LDAP_TAG_AddRequest:
1456                 status = ldapsrv_AddRequest(call);
1457                 break;
1458         case LDAP_TAG_DelRequest:
1459                 status = ldapsrv_DelRequest(call);
1460                 break;
1461         case LDAP_TAG_ModifyDNRequest:
1462                 status = ldapsrv_ModifyDNRequest(call);
1463                 break;
1464         case LDAP_TAG_CompareRequest:
1465                 return ldapsrv_CompareRequest(call);
1466         case LDAP_TAG_AbandonRequest:
1467                 return ldapsrv_AbandonRequest(call);
1468         case LDAP_TAG_ExtendedRequest:
1469                 status = ldapsrv_ExtendedRequest(call);
1470                 break;
1471         default:
1472                 return ldapsrv_unwilling(call, LDAP_PROTOCOL_ERROR);
1473         }
1474
1475         if (NT_STATUS_IS_OK(status)) {
1476                 ldapsrv_notification_retry_setup(call->conn->service, true);
1477         }
1478
1479         return status;
1480 }