s4-ldap_server: Split gensec setup into a helper function
[sfrench/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 "param/param.h"
28 #include "smbd/service_stream.h"
29 #include "dsdb/samdb/samdb.h"
30 #include <ldb_errors.h>
31 #include <ldb_module.h>
32 #include "ldb_wrap.h"
33
34 static int map_ldb_error(TALLOC_CTX *mem_ctx, int ldb_err,
35         const char *add_err_string, const char **errstring)
36 {
37         WERROR err;
38
39         /* Certain LDB modules need to return very special WERROR codes. Proof
40          * for them here and if they exist skip the rest of the mapping. */
41         if (add_err_string != NULL) {
42                 char *endptr;
43                 strtol(add_err_string, &endptr, 16);
44                 if (endptr != add_err_string) {
45                         *errstring = add_err_string;
46                         return ldb_err;
47                 }
48         }
49
50         /* Otherwise we calculate here a generic, but appropriate WERROR. */
51
52         switch (ldb_err) {
53         case LDB_SUCCESS:
54                 err = WERR_OK;
55         break;
56         case LDB_ERR_OPERATIONS_ERROR:
57                 err = WERR_DS_OPERATIONS_ERROR;
58         break;
59         case LDB_ERR_PROTOCOL_ERROR:
60                 err = WERR_DS_PROTOCOL_ERROR;
61         break;
62         case LDB_ERR_TIME_LIMIT_EXCEEDED:
63                 err = WERR_DS_TIMELIMIT_EXCEEDED;
64         break;
65         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
66                 err = WERR_DS_SIZELIMIT_EXCEEDED;
67         break;
68         case LDB_ERR_COMPARE_FALSE:
69                 err = WERR_DS_COMPARE_FALSE;
70         break;
71         case LDB_ERR_COMPARE_TRUE:
72                 err = WERR_DS_COMPARE_TRUE;
73         break;
74         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
75                 err = WERR_DS_AUTH_METHOD_NOT_SUPPORTED;
76         break;
77         case LDB_ERR_STRONG_AUTH_REQUIRED:
78                 err = WERR_DS_STRONG_AUTH_REQUIRED;
79         break;
80         case LDB_ERR_REFERRAL:
81                 err = WERR_DS_REFERRAL;
82         break;
83         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
84                 err = WERR_DS_ADMIN_LIMIT_EXCEEDED;
85         break;
86         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
87                 err = WERR_DS_UNAVAILABLE_CRIT_EXTENSION;
88         break;
89         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
90                 err = WERR_DS_CONFIDENTIALITY_REQUIRED;
91         break;
92         case LDB_ERR_SASL_BIND_IN_PROGRESS:
93                 err = WERR_DS_BUSY;
94         break;
95         case LDB_ERR_NO_SUCH_ATTRIBUTE:
96                 err = WERR_DS_NO_ATTRIBUTE_OR_VALUE;
97         break;
98         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
99                 err = WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
100         break;
101         case LDB_ERR_INAPPROPRIATE_MATCHING:
102                 err = WERR_DS_INAPPROPRIATE_MATCHING;
103         break;
104         case LDB_ERR_CONSTRAINT_VIOLATION:
105                 err = WERR_DS_CONSTRAINT_VIOLATION;
106         break;
107         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
108                 err = WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS;
109         break;
110         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
111                 err = WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
112         break;
113         case LDB_ERR_NO_SUCH_OBJECT:
114                 err = WERR_DS_NO_SUCH_OBJECT;
115         break;
116         case LDB_ERR_ALIAS_PROBLEM:
117                 err = WERR_DS_ALIAS_PROBLEM;
118         break;
119         case LDB_ERR_INVALID_DN_SYNTAX:
120                 err = WERR_DS_INVALID_DN_SYNTAX;
121         break;
122         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
123                 err = WERR_DS_ALIAS_DEREF_PROBLEM;
124         break;
125         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
126                 err = WERR_DS_INAPPROPRIATE_AUTH;
127         break;
128         case LDB_ERR_INVALID_CREDENTIALS:
129                 err = WERR_ACCESS_DENIED;
130         break;
131         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
132                 err = WERR_DS_INSUFF_ACCESS_RIGHTS;
133         break;
134         case LDB_ERR_BUSY:
135                 err = WERR_DS_BUSY;
136         break;
137         case LDB_ERR_UNAVAILABLE:
138                 err = WERR_DS_UNAVAILABLE;
139         break;
140         case LDB_ERR_UNWILLING_TO_PERFORM:
141                 err = WERR_DS_UNWILLING_TO_PERFORM;
142         break;
143         case LDB_ERR_LOOP_DETECT:
144                 err = WERR_DS_LOOP_DETECT;
145         break;
146         case LDB_ERR_NAMING_VIOLATION:
147                 err = WERR_DS_NAMING_VIOLATION;
148         break;
149         case LDB_ERR_OBJECT_CLASS_VIOLATION:
150                 err = WERR_DS_OBJ_CLASS_VIOLATION;
151         break;
152         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
153                 err = WERR_DS_CANT_ON_NON_LEAF;
154         break;
155         case LDB_ERR_NOT_ALLOWED_ON_RDN:
156                 err = WERR_DS_CANT_ON_RDN;
157         break;
158         case LDB_ERR_ENTRY_ALREADY_EXISTS:
159                 err = WERR_DS_OBJ_STRING_NAME_EXISTS;
160         break;
161         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
162                 err = WERR_DS_CANT_MOD_OBJ_CLASS;
163         break;
164         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
165                 err = WERR_DS_AFFECTS_MULTIPLE_DSAS;
166         break;
167         default:
168                 err = WERR_DS_GENERIC_ERROR;
169         break;
170         }
171
172         *errstring = talloc_asprintf(mem_ctx, "%08X: %s", W_ERROR_V(err),
173                 add_err_string != NULL ? add_err_string : ldb_strerror(ldb_err));
174
175         /* result is 1:1 for now */
176         return ldb_err;
177 }
178
179 /*
180   connect to the sam database
181 */
182 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn) 
183 {
184         conn->ldb = samdb_connect(conn, 
185                                      conn->connection->event.ctx,
186                                      conn->lp_ctx,
187                                      conn->session_info,
188                                      conn->global_catalog ? LDB_FLG_RDONLY : 0);
189         if (conn->ldb == NULL) {
190                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
191         }
192
193         if (conn->server_credentials) {
194                 char **sasl_mechs = NULL;
195                 const struct gensec_security_ops * const *backends = gensec_security_all();
196                 const struct gensec_security_ops **ops
197                         = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
198                 unsigned int i, j = 0;
199                 for (i = 0; ops && ops[i]; i++) {
200                         if (!lpcfg_parm_bool(conn->lp_ctx,  NULL, "gensec", ops[i]->name, ops[i]->enabled))
201                                 continue;
202
203                         if (ops[i]->sasl_name && ops[i]->server_start) {
204                                 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
205
206                                 if (!sasl_name) {
207                                         return NT_STATUS_NO_MEMORY;
208                                 }
209                                 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
210                                 if (!sasl_mechs) {
211                                         return NT_STATUS_NO_MEMORY;
212                                 }
213                                 sasl_mechs[j] = sasl_name;
214                                 talloc_steal(sasl_mechs, sasl_name);
215                                 sasl_mechs[j+1] = NULL;
216                                 j++;
217                         }
218                 }
219                 talloc_unlink(conn, ops);
220
221                 /* ldb can have a different lifetime to conn, so we
222                    need to ensure that sasl_mechs lives as long as the
223                    ldb does */
224                 talloc_steal(conn->ldb, sasl_mechs);
225
226                 ldb_set_opaque(conn->ldb, "supportedSASLMechanisms", sasl_mechs);
227         }
228
229         ldb_set_opaque(conn->ldb, "remoteAddress",
230                        conn->connection->remote_address);
231
232         return NT_STATUS_OK;
233 }
234
235 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
236 {
237         struct ldapsrv_reply *reply;
238
239         reply = talloc(call, struct ldapsrv_reply);
240         if (!reply) {
241                 return NULL;
242         }
243         reply->msg = talloc(reply, struct ldap_message);
244         if (reply->msg == NULL) {
245                 talloc_free(reply);
246                 return NULL;
247         }
248
249         reply->msg->messageid = call->request->messageid;
250         reply->msg->type = type;
251         reply->msg->controls = NULL;
252
253         return reply;
254 }
255
256 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
257 {
258         DLIST_ADD_END(call->replies, reply);
259 }
260
261 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
262 {
263         struct ldapsrv_reply *reply;
264         struct ldap_ExtendedResponse *r;
265
266         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
267
268         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
269         if (!reply) {
270                 return NT_STATUS_NO_MEMORY;
271         }
272
273         r = &reply->msg->r.ExtendedResponse;
274         r->response.resultcode = error;
275         r->response.dn = NULL;
276         r->response.errormessage = NULL;
277         r->response.referral = NULL;
278         r->oid = NULL;
279         r->value = NULL;
280
281         ldapsrv_queue_reply(call, reply);
282         return NT_STATUS_OK;
283 }
284
285 static int ldapsrv_add_with_controls(struct ldapsrv_call *call,
286                                      const struct ldb_message *message,
287                                      struct ldb_control **controls,
288                                      struct ldb_result *res)
289 {
290         struct ldb_context *ldb = call->conn->ldb;
291         struct ldb_request *req;
292         int ret;
293
294         ret = ldb_msg_sanity_check(ldb, message);
295         if (ret != LDB_SUCCESS) {
296                 return ret;
297         }
298
299         ret = ldb_build_add_req(&req, ldb, ldb,
300                                         message,
301                                         controls,
302                                         res,
303                                         ldb_modify_default_callback,
304                                         NULL);
305
306         if (ret != LDB_SUCCESS) return ret;
307
308         if (call->conn->global_catalog) {
309                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
310         }
311         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
312
313         ret = ldb_transaction_start(ldb);
314         if (ret != LDB_SUCCESS) {
315                 return ret;
316         }
317
318         if (!call->conn->is_privileged) {
319                 ldb_req_mark_untrusted(req);
320         }
321
322         LDB_REQ_SET_LOCATION(req);
323
324         ret = ldb_request(ldb, req);
325         if (ret == LDB_SUCCESS) {
326                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
327         }
328
329         if (ret == LDB_SUCCESS) {
330                 ret = ldb_transaction_commit(ldb);
331         }
332         else {
333                 ldb_transaction_cancel(ldb);
334         }
335
336         talloc_free(req);
337         return ret;
338 }
339
340 /* create and execute a modify request */
341 static int ldapsrv_mod_with_controls(struct ldapsrv_call *call,
342                                      const struct ldb_message *message,
343                                      struct ldb_control **controls,
344                                      struct ldb_result *res)
345 {
346         struct ldb_context *ldb = call->conn->ldb;
347         struct ldb_request *req;
348         int ret;
349
350         ret = ldb_msg_sanity_check(ldb, message);
351         if (ret != LDB_SUCCESS) {
352                 return ret;
353         }
354
355         ret = ldb_build_mod_req(&req, ldb, ldb,
356                                         message,
357                                         controls,
358                                         res,
359                                         ldb_modify_default_callback,
360                                         NULL);
361
362         if (ret != LDB_SUCCESS) {
363                 return ret;
364         }
365
366         if (call->conn->global_catalog) {
367                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
368         }
369         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
370
371         ret = ldb_transaction_start(ldb);
372         if (ret != LDB_SUCCESS) {
373                 return ret;
374         }
375
376         if (!call->conn->is_privileged) {
377                 ldb_req_mark_untrusted(req);
378         }
379
380         LDB_REQ_SET_LOCATION(req);
381
382         ret = ldb_request(ldb, req);
383         if (ret == LDB_SUCCESS) {
384                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
385         }
386
387         if (ret == LDB_SUCCESS) {
388                 ret = ldb_transaction_commit(ldb);
389         }
390         else {
391                 ldb_transaction_cancel(ldb);
392         }
393
394         talloc_free(req);
395         return ret;
396 }
397
398 /* create and execute a delete request */
399 static int ldapsrv_del_with_controls(struct ldapsrv_call *call,
400                                      struct ldb_dn *dn,
401                                      struct ldb_control **controls,
402                                      struct ldb_result *res)
403 {
404         struct ldb_context *ldb = call->conn->ldb;
405         struct ldb_request *req;
406         int ret;
407
408         ret = ldb_build_del_req(&req, ldb, ldb,
409                                         dn,
410                                         controls,
411                                         res,
412                                         ldb_modify_default_callback,
413                                         NULL);
414
415         if (ret != LDB_SUCCESS) return ret;
416
417         if (call->conn->global_catalog) {
418                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
419         }
420         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
421
422         ret = ldb_transaction_start(ldb);
423         if (ret != LDB_SUCCESS) {
424                 return ret;
425         }
426
427         if (!call->conn->is_privileged) {
428                 ldb_req_mark_untrusted(req);
429         }
430
431         LDB_REQ_SET_LOCATION(req);
432
433         ret = ldb_request(ldb, req);
434         if (ret == LDB_SUCCESS) {
435                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
436         }
437
438         if (ret == LDB_SUCCESS) {
439                 ret = ldb_transaction_commit(ldb);
440         }
441         else {
442                 ldb_transaction_cancel(ldb);
443         }
444
445         talloc_free(req);
446         return ret;
447 }
448
449 static int ldapsrv_rename_with_controls(struct ldapsrv_call *call,
450                                         struct ldb_dn *olddn,
451                                         struct ldb_dn *newdn,
452                                         struct ldb_control **controls,
453                                         struct ldb_result *res)
454 {
455         struct ldb_context *ldb = call->conn->ldb;
456         struct ldb_request *req;
457         int ret;
458
459         ret = ldb_build_rename_req(&req, ldb, ldb,
460                                         olddn,
461                                         newdn,
462                                         controls,
463                                         res,
464                                         ldb_modify_default_callback,
465                                         NULL);
466
467         if (ret != LDB_SUCCESS) return ret;
468
469         if (call->conn->global_catalog) {
470                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
471         }
472         ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
473
474         ret = ldb_transaction_start(ldb);
475         if (ret != LDB_SUCCESS) {
476                 return ret;
477         }
478
479         if (!call->conn->is_privileged) {
480                 ldb_req_mark_untrusted(req);
481         }
482
483         LDB_REQ_SET_LOCATION(req);
484
485         ret = ldb_request(ldb, req);
486         if (ret == LDB_SUCCESS) {
487                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
488         }
489
490         if (ret == LDB_SUCCESS) {
491                 ret = ldb_transaction_commit(ldb);
492         }
493         else {
494                 ldb_transaction_cancel(ldb);
495         }
496
497         talloc_free(req);
498         return ret;
499 }
500
501 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
502 {
503         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
504         struct ldap_SearchResEntry *ent;
505         struct ldap_Result *done;
506         struct ldapsrv_reply *ent_r, *done_r;
507         TALLOC_CTX *local_ctx;
508         struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
509         struct ldb_dn *basedn;
510         struct ldb_result *res = NULL;
511         struct ldb_request *lreq;
512         struct ldb_control *search_control;
513         struct ldb_search_options_control *search_options;
514         struct ldb_control *extended_dn_control;
515         struct ldb_extended_dn_control *extended_dn_decoded = NULL;
516         struct ldb_control *notification_control = NULL;
517         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
518         const char **attrs = NULL;
519         const char *scope_str, *errstr = NULL;
520         int success_limit = 1;
521         int result = -1;
522         int ldb_ret = -1;
523         unsigned int i, j;
524         int extended_type = 1;
525
526         DEBUG(10, ("SearchRequest"));
527         DEBUGADD(10, (" basedn: %s", req->basedn));
528         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
529
530         local_ctx = talloc_new(call);
531         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
532
533         basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
534         NT_STATUS_HAVE_NO_MEMORY(basedn);
535
536         DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
537         DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
538
539         switch (req->scope) {
540                 case LDAP_SEARCH_SCOPE_BASE:
541                         scope_str = "BASE";
542                         scope = LDB_SCOPE_BASE;
543                         success_limit = 0;
544                         break;
545                 case LDAP_SEARCH_SCOPE_SINGLE:
546                         scope_str = "ONE";
547                         scope = LDB_SCOPE_ONELEVEL;
548                         success_limit = 0;
549                         break;
550                 case LDAP_SEARCH_SCOPE_SUB:
551                         scope_str = "SUB";
552                         scope = LDB_SCOPE_SUBTREE;
553                         success_limit = 0;
554                         break;
555                 default:
556                         result = LDAP_PROTOCOL_ERROR;
557                         map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
558                                 &errstr);
559                         errstr = talloc_asprintf(local_ctx,
560                                 "%s. Invalid scope", errstr);
561                         goto reply;
562         }
563         DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
564
565         if (req->num_attributes >= 1) {
566                 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
567                 NT_STATUS_HAVE_NO_MEMORY(attrs);
568
569                 for (i=0; i < req->num_attributes; i++) {
570                         DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
571                         attrs[i] = req->attributes[i];
572                 }
573                 attrs[i] = NULL;
574         }
575
576         DEBUG(5,("ldb_request %s dn=%s filter=%s\n", 
577                  scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
578
579         res = talloc_zero(local_ctx, struct ldb_result);
580         NT_STATUS_HAVE_NO_MEMORY(res);
581
582         ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
583                                           basedn, scope,
584                                           req->tree, attrs,
585                                           call->request->controls,
586                                           res, ldb_search_default_callback,
587                                           NULL);
588
589         if (ldb_ret != LDB_SUCCESS) {
590                 goto reply;
591         }
592
593         if (call->conn->global_catalog) {
594                 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
595
596                 search_options = NULL;
597                 if (search_control) {
598                         search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
599                         search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
600                 } else {
601                         search_options = talloc(lreq, struct ldb_search_options_control);
602                         NT_STATUS_HAVE_NO_MEMORY(search_options);
603                         search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
604                         ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
605                 }
606         } else {
607                 ldb_request_add_control(lreq, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
608         }
609
610         extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
611
612         if (extended_dn_control) {
613                 if (extended_dn_control->data) {
614                         extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
615                         extended_type = extended_dn_decoded->type;
616                 } else {
617                         extended_type = 0;
618                 }
619         }
620
621         notification_control = ldb_request_get_control(lreq, LDB_CONTROL_NOTIFICATION_OID);
622         if (notification_control != NULL) {
623                 const struct ldapsrv_call *pc = NULL;
624                 size_t count = 0;
625
626                 for (pc = call->conn->pending_calls; pc != NULL; pc = pc->next) {
627                         count += 1;
628                 }
629
630                 if (count >= call->conn->limits.max_notifications) {
631                         DEBUG(10,("SearchRequest: error MaxNotificationPerConn\n"));
632                         result = map_ldb_error(local_ctx,
633                                                LDB_ERR_ADMIN_LIMIT_EXCEEDED,
634                                                "MaxNotificationPerConn reached",
635                                                &errstr);
636                         goto reply;
637                 }
638
639                 /*
640                  * For now we need to do periodic retries on our own.
641                  * As the dsdb_notification module will return after each run.
642                  */
643                 call->notification.busy = true;
644         }
645
646         ldb_set_timeout(samdb, lreq, req->timelimit);
647
648         if (!call->conn->is_privileged) {
649                 ldb_req_mark_untrusted(lreq);
650         }
651
652         LDB_REQ_SET_LOCATION(lreq);
653
654         ldb_ret = ldb_request(samdb, lreq);
655
656         if (ldb_ret != LDB_SUCCESS) {
657                 goto reply;
658         }
659
660         ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
661
662         if (ldb_ret == LDB_SUCCESS) {
663                 for (i = 0; i < res->count; i++) {
664                         ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
665                         NT_STATUS_HAVE_NO_MEMORY(ent_r);
666
667                         /* Better to have the whole message kept here,
668                          * than to find someone further up didn't put
669                          * a value in the right spot in the talloc tree */
670                         talloc_steal(ent_r, res->msgs[i]);
671                         
672                         ent = &ent_r->msg->r.SearchResultEntry;
673                         ent->dn = ldb_dn_get_extended_linearized(ent_r, res->msgs[i]->dn, extended_type);
674                         ent->num_attributes = 0;
675                         ent->attributes = NULL;
676                         if (res->msgs[i]->num_elements == 0) {
677                                 goto queue_reply;
678                         }
679                         ent->num_attributes = res->msgs[i]->num_elements;
680                         ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
681                         NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
682                         for (j=0; j < ent->num_attributes; j++) {
683                                 ent->attributes[j].name = res->msgs[i]->elements[j].name;
684                                 ent->attributes[j].num_values = 0;
685                                 ent->attributes[j].values = NULL;
686                                 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
687                                         continue;
688                                 }
689                                 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
690                                 ent->attributes[j].values = res->msgs[i]->elements[j].values;
691                         }
692 queue_reply:
693                         ldapsrv_queue_reply(call, ent_r);
694                 }
695
696                 if (call->notification.busy) {
697                         /* Move/Add it to the end */
698                         DLIST_DEMOTE(call->conn->pending_calls, call);
699                         call->notification.generation =
700                                 call->conn->service->notification.generation;
701
702                         if (res->count != 0) {
703                                 call->notification.generation += 1;
704                                 ldapsrv_notification_retry_setup(call->conn->service,
705                                                                  true);
706                         }
707
708                         talloc_free(local_ctx);
709                         return NT_STATUS_OK;
710                 }
711
712                 /* Send back referrals if they do exist (search operations) */
713                 if (res->refs != NULL) {
714                         char **ref;
715                         struct ldap_SearchResRef *ent_ref;
716
717                         for (ref = res->refs; *ref != NULL; ++ref) {
718                                 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
719                                 NT_STATUS_HAVE_NO_MEMORY(ent_r);
720
721                                 /* Better to have the whole referrals kept here,
722                                  * than to find someone further up didn't put
723                                  * a value in the right spot in the talloc tree
724                                  */
725                                 talloc_steal(ent_r, *ref);
726
727                                 ent_ref = &ent_r->msg->r.SearchResultReference;
728                                 ent_ref->referral = *ref;
729
730                                 ldapsrv_queue_reply(call, ent_r);
731                         }
732                 }
733         }
734
735 reply:
736         DLIST_REMOVE(call->conn->pending_calls, call);
737         call->notification.busy = false;
738
739         done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
740         NT_STATUS_HAVE_NO_MEMORY(done_r);
741
742         done = &done_r->msg->r.SearchResultDone;
743         done->dn = NULL;
744         done->referral = NULL;
745
746         if (result != -1) {
747         } else if (ldb_ret == LDB_SUCCESS) {
748                 if (res->count >= success_limit) {
749                         DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
750                         result = LDAP_SUCCESS;
751                         errstr = NULL;
752                 }
753                 if (res->controls) {
754                         done_r->msg->controls = res->controls;
755                         talloc_steal(done_r, res->controls);
756                 }
757         } else {
758                 DEBUG(10,("SearchRequest: error\n"));
759                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
760                                        &errstr);
761         }
762
763         done->resultcode = result;
764         done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
765
766         talloc_free(local_ctx);
767
768         ldapsrv_queue_reply(call, done_r);
769         return NT_STATUS_OK;
770 }
771
772 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
773 {
774         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
775         struct ldap_Result *modify_result;
776         struct ldapsrv_reply *modify_reply;
777         TALLOC_CTX *local_ctx;
778         struct ldb_context *samdb = call->conn->ldb;
779         struct ldb_message *msg = NULL;
780         struct ldb_dn *dn;
781         const char *errstr = NULL;
782         int result = LDAP_SUCCESS;
783         int ldb_ret;
784         unsigned int i,j;
785         struct ldb_result *res = NULL;
786
787         DEBUG(10, ("ModifyRequest"));
788         DEBUGADD(10, (" dn: %s\n", req->dn));
789
790         local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
791         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
792
793         dn = ldb_dn_new(local_ctx, samdb, req->dn);
794         NT_STATUS_HAVE_NO_MEMORY(dn);
795
796         DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
797
798         msg = talloc(local_ctx, struct ldb_message);
799         NT_STATUS_HAVE_NO_MEMORY(msg);
800
801         msg->dn = dn;
802         msg->num_elements = 0;
803         msg->elements = NULL;
804
805         if (req->num_mods > 0) {
806                 msg->num_elements = req->num_mods;
807                 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
808                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
809
810                 for (i=0; i < msg->num_elements; i++) {
811                         msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
812                         msg->elements[i].num_values = 0;
813                         msg->elements[i].values = NULL;
814
815                         switch (req->mods[i].type) {
816                         default:
817                                 result = LDAP_PROTOCOL_ERROR;
818                                 map_ldb_error(local_ctx,
819                                         LDB_ERR_PROTOCOL_ERROR, NULL, &errstr);
820                                 errstr = talloc_asprintf(local_ctx,
821                                         "%s. Invalid LDAP_MODIFY_* type", errstr);
822                                 goto reply;
823                         case LDAP_MODIFY_ADD:
824                                 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
825                                 break;
826                         case LDAP_MODIFY_DELETE:
827                                 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
828                                 break;
829                         case LDAP_MODIFY_REPLACE:
830                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
831                                 break;
832                         }
833
834                         msg->elements[i].num_values = req->mods[i].attrib.num_values;
835                         if (msg->elements[i].num_values > 0) {
836                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
837                                                                        msg->elements[i].num_values);
838                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
839
840                                 for (j=0; j < msg->elements[i].num_values; j++) {
841                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
842                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
843                                 }
844                         }
845                 }
846         }
847
848 reply:
849         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
850         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
851
852         if (result == LDAP_SUCCESS) {
853                 res = talloc_zero(local_ctx, struct ldb_result);
854                 NT_STATUS_HAVE_NO_MEMORY(res);
855                 ldb_ret = ldapsrv_mod_with_controls(call, msg, call->request->controls, res);
856                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
857                                        &errstr);
858         }
859
860         modify_result = &modify_reply->msg->r.ModifyResponse;
861         modify_result->dn = NULL;
862         if ((res != NULL) && (res->refs != NULL)) {
863                 modify_result->resultcode = map_ldb_error(local_ctx,
864                                                           LDB_ERR_REFERRAL,
865                                                           NULL, &errstr);
866                 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
867                 modify_result->referral = talloc_strdup(call, *res->refs);
868         } else {
869                 modify_result->resultcode = result;
870                 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
871                 modify_result->referral = NULL;
872         }
873         talloc_free(local_ctx);
874
875         ldapsrv_queue_reply(call, modify_reply);
876         return NT_STATUS_OK;
877
878 }
879
880 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
881 {
882         struct ldap_AddRequest *req = &call->request->r.AddRequest;
883         struct ldap_Result *add_result;
884         struct ldapsrv_reply *add_reply;
885         TALLOC_CTX *local_ctx;
886         struct ldb_context *samdb = call->conn->ldb;
887         struct ldb_message *msg = NULL;
888         struct ldb_dn *dn;
889         const char *errstr = NULL;
890         int result = LDAP_SUCCESS;
891         int ldb_ret;
892         unsigned int i,j;
893         struct ldb_result *res = NULL;
894
895         DEBUG(10, ("AddRequest"));
896         DEBUGADD(10, (" dn: %s\n", req->dn));
897
898         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
899         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
900
901         dn = ldb_dn_new(local_ctx, samdb, req->dn);
902         NT_STATUS_HAVE_NO_MEMORY(dn);
903
904         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
905
906         msg = talloc(local_ctx, struct ldb_message);
907         NT_STATUS_HAVE_NO_MEMORY(msg);
908
909         msg->dn = dn;
910         msg->num_elements = 0;
911         msg->elements = NULL;
912
913         if (req->num_attributes > 0) {
914                 msg->num_elements = req->num_attributes;
915                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
916                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
917
918                 for (i=0; i < msg->num_elements; i++) {
919                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
920                         msg->elements[i].flags = 0;
921                         msg->elements[i].num_values = 0;
922                         msg->elements[i].values = NULL;
923                         
924                         if (req->attributes[i].num_values > 0) {
925                                 msg->elements[i].num_values = req->attributes[i].num_values;
926                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
927                                                                        msg->elements[i].num_values);
928                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
929
930                                 for (j=0; j < msg->elements[i].num_values; j++) {
931                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
932                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
933                                 }
934                         }
935                 }
936         }
937
938         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
939         NT_STATUS_HAVE_NO_MEMORY(add_reply);
940
941         if (result == LDAP_SUCCESS) {
942                 res = talloc_zero(local_ctx, struct ldb_result);
943                 NT_STATUS_HAVE_NO_MEMORY(res);
944                 ldb_ret = ldapsrv_add_with_controls(call, msg, call->request->controls, res);
945                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
946                                        &errstr);
947         }
948
949         add_result = &add_reply->msg->r.AddResponse;
950         add_result->dn = NULL;
951         if ((res != NULL) && (res->refs != NULL)) {
952                 add_result->resultcode =  map_ldb_error(local_ctx,
953                                                         LDB_ERR_REFERRAL, NULL,
954                                                         &errstr);
955                 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
956                 add_result->referral = talloc_strdup(call, *res->refs);
957         } else {
958                 add_result->resultcode = result;
959                 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
960                 add_result->referral = NULL;
961         }
962         talloc_free(local_ctx);
963
964         ldapsrv_queue_reply(call, add_reply);
965         return NT_STATUS_OK;
966
967 }
968
969 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
970 {
971         struct ldap_DelRequest *req = &call->request->r.DelRequest;
972         struct ldap_Result *del_result;
973         struct ldapsrv_reply *del_reply;
974         TALLOC_CTX *local_ctx;
975         struct ldb_context *samdb = call->conn->ldb;
976         struct ldb_dn *dn;
977         const char *errstr = NULL;
978         int result = LDAP_SUCCESS;
979         int ldb_ret;
980         struct ldb_result *res = NULL;
981
982         DEBUG(10, ("DelRequest"));
983         DEBUGADD(10, (" dn: %s\n", req->dn));
984
985         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
986         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
987
988         dn = ldb_dn_new(local_ctx, samdb, req->dn);
989         NT_STATUS_HAVE_NO_MEMORY(dn);
990
991         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
992
993         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
994         NT_STATUS_HAVE_NO_MEMORY(del_reply);
995
996         if (result == LDAP_SUCCESS) {
997                 res = talloc_zero(local_ctx, struct ldb_result);
998                 NT_STATUS_HAVE_NO_MEMORY(res);
999                 ldb_ret = ldapsrv_del_with_controls(call, dn, call->request->controls, res);
1000                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1001                                        &errstr);
1002         }
1003
1004         del_result = &del_reply->msg->r.DelResponse;
1005         del_result->dn = NULL;
1006         if ((res != NULL) && (res->refs != NULL)) {
1007                 del_result->resultcode = map_ldb_error(local_ctx,
1008                                                        LDB_ERR_REFERRAL, NULL,
1009                                                        &errstr);
1010                 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
1011                 del_result->referral = talloc_strdup(call, *res->refs);
1012         } else {
1013                 del_result->resultcode = result;
1014                 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
1015                 del_result->referral = NULL;
1016         }
1017
1018         talloc_free(local_ctx);
1019
1020         ldapsrv_queue_reply(call, del_reply);
1021         return NT_STATUS_OK;
1022 }
1023
1024 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
1025 {
1026         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
1027         struct ldap_Result *modifydn;
1028         struct ldapsrv_reply *modifydn_r;
1029         TALLOC_CTX *local_ctx;
1030         struct ldb_context *samdb = call->conn->ldb;
1031         struct ldb_dn *olddn, *newdn=NULL, *newrdn;
1032         struct ldb_dn *parentdn = NULL;
1033         const char *errstr = NULL;
1034         int result = LDAP_SUCCESS;
1035         int ldb_ret;
1036         struct ldb_result *res = NULL;
1037
1038         DEBUG(10, ("ModifyDNRequest"));
1039         DEBUGADD(10, (" dn: %s", req->dn));
1040         DEBUGADD(10, (" newrdn: %s\n", req->newrdn));
1041
1042         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
1043         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1044
1045         olddn = ldb_dn_new(local_ctx, samdb, req->dn);
1046         NT_STATUS_HAVE_NO_MEMORY(olddn);
1047
1048         newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
1049         NT_STATUS_HAVE_NO_MEMORY(newrdn);
1050
1051         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
1052         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
1053
1054         if (ldb_dn_get_comp_num(newrdn) == 0) {
1055                 result = LDAP_PROTOCOL_ERROR;
1056                 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
1057                               &errstr);
1058                 goto reply;
1059         }
1060
1061         if (ldb_dn_get_comp_num(newrdn) > 1) {
1062                 result = LDAP_NAMING_VIOLATION;
1063                 map_ldb_error(local_ctx, LDB_ERR_NAMING_VIOLATION, NULL,
1064                               &errstr);
1065                 goto reply;
1066         }
1067
1068         /* we can't handle the rename if we should not remove the old dn */
1069         if (!req->deleteolddn) {
1070                 result = LDAP_UNWILLING_TO_PERFORM;
1071                 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, NULL,
1072                               &errstr);
1073                 errstr = talloc_asprintf(local_ctx,
1074                         "%s. Old RDN must be deleted", errstr);
1075                 goto reply;
1076         }
1077
1078         if (req->newsuperior) {
1079                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
1080                 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
1081         }
1082
1083         if (!parentdn) {
1084                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
1085         }
1086         if (!parentdn) {
1087                 result = LDAP_NO_SUCH_OBJECT;
1088                 map_ldb_error(local_ctx, LDB_ERR_NO_SUCH_OBJECT, NULL, &errstr);
1089                 goto reply;
1090         }
1091
1092         if ( ! ldb_dn_add_child(parentdn, newrdn)) {
1093                 result = LDAP_OTHER;
1094                 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1095                 goto reply;
1096         }
1097         newdn = parentdn;
1098
1099 reply:
1100         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
1101         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
1102
1103         if (result == LDAP_SUCCESS) {
1104                 res = talloc_zero(local_ctx, struct ldb_result);
1105                 NT_STATUS_HAVE_NO_MEMORY(res);
1106                 ldb_ret = ldapsrv_rename_with_controls(call, olddn, newdn, call->request->controls, res);
1107                 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1108                                        &errstr);
1109         }
1110
1111         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
1112         modifydn->dn = NULL;
1113         if ((res != NULL) && (res->refs != NULL)) {
1114                 modifydn->resultcode = map_ldb_error(local_ctx,
1115                                                      LDB_ERR_REFERRAL, NULL,
1116                                                      &errstr);;
1117                 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1118                 modifydn->referral = talloc_strdup(call, *res->refs);
1119         } else {
1120                 modifydn->resultcode = result;
1121                 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1122                 modifydn->referral = NULL;
1123         }
1124
1125         talloc_free(local_ctx);
1126
1127         ldapsrv_queue_reply(call, modifydn_r);
1128         return NT_STATUS_OK;
1129 }
1130
1131 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
1132 {
1133         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
1134         struct ldap_Result *compare;
1135         struct ldapsrv_reply *compare_r;
1136         TALLOC_CTX *local_ctx;
1137         struct ldb_context *samdb = call->conn->ldb;
1138         struct ldb_result *res = NULL;
1139         struct ldb_dn *dn;
1140         const char *attrs[1];
1141         const char *errstr = NULL;
1142         const char *filter = NULL;
1143         int result = LDAP_SUCCESS;
1144         int ldb_ret;
1145
1146         DEBUG(10, ("CompareRequest"));
1147         DEBUGADD(10, (" dn: %s\n", req->dn));
1148
1149         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
1150         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1151
1152         dn = ldb_dn_new(local_ctx, samdb, req->dn);
1153         NT_STATUS_HAVE_NO_MEMORY(dn);
1154
1155         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
1156         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
1157                                  (int)req->value.length, req->value.data);
1158         NT_STATUS_HAVE_NO_MEMORY(filter);
1159
1160         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
1161
1162         attrs[0] = NULL;
1163
1164         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
1165         NT_STATUS_HAVE_NO_MEMORY(compare_r);
1166
1167         if (result == LDAP_SUCCESS) {
1168                 ldb_ret = ldb_search(samdb, local_ctx, &res,
1169                                      dn, LDB_SCOPE_BASE, attrs, "%s", filter);
1170                 if (ldb_ret != LDB_SUCCESS) {
1171                         result = map_ldb_error(local_ctx, ldb_ret,
1172                                                ldb_errstring(samdb), &errstr);
1173                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
1174                 } else if (res->count == 0) {
1175                         DEBUG(10,("CompareRequest: doesn't matched\n"));
1176                         result = LDAP_COMPARE_FALSE;
1177                         errstr = NULL;
1178                 } else if (res->count == 1) {
1179                         DEBUG(10,("CompareRequest: matched\n"));
1180                         result = LDAP_COMPARE_TRUE;
1181                         errstr = NULL;
1182                 } else if (res->count > 1) {
1183                         result = LDAP_OTHER;
1184                         map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1185                         errstr = talloc_asprintf(local_ctx,
1186                                 "%s. Too many objects match!", errstr);
1187                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
1188                 }
1189         }
1190
1191         compare = &compare_r->msg->r.CompareResponse;
1192         compare->dn = NULL;
1193         compare->resultcode = result;
1194         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
1195         compare->referral = NULL;
1196
1197         talloc_free(local_ctx);
1198
1199         ldapsrv_queue_reply(call, compare_r);
1200         return NT_STATUS_OK;
1201 }
1202
1203 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
1204 {
1205         struct ldap_AbandonRequest *req = &call->request->r.AbandonRequest;
1206         struct ldapsrv_call *c = NULL;
1207         struct ldapsrv_call *n = NULL;
1208
1209         DEBUG(10, ("AbandonRequest\n"));
1210
1211         for (c = call->conn->pending_calls; c != NULL; c = n) {
1212                 n = c->next;
1213
1214                 if (c->request->messageid != req->messageid) {
1215                         continue;
1216                 }
1217
1218                 DLIST_REMOVE(call->conn->pending_calls, c);
1219                 TALLOC_FREE(c);
1220         }
1221
1222         return NT_STATUS_OK;
1223 }
1224
1225 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
1226 {
1227         unsigned int i;
1228         struct ldap_message *msg = call->request;
1229         NTSTATUS status;
1230
1231         /* Check for undecoded critical extensions */
1232         for (i=0; msg->controls && msg->controls[i]; i++) {
1233                 if (!msg->controls_decoded[i] && 
1234                     msg->controls[i]->critical) {
1235                         DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1236                                   msg->controls[i]->oid));
1237                         return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
1238                 }
1239         }
1240
1241         switch(call->request->type) {
1242         case LDAP_TAG_BindRequest:
1243                 return ldapsrv_BindRequest(call);
1244         case LDAP_TAG_UnbindRequest:
1245                 return ldapsrv_UnbindRequest(call);
1246         case LDAP_TAG_SearchRequest:
1247                 return ldapsrv_SearchRequest(call);
1248         case LDAP_TAG_ModifyRequest:
1249                 status = ldapsrv_ModifyRequest(call);
1250                 break;
1251         case LDAP_TAG_AddRequest:
1252                 status = ldapsrv_AddRequest(call);
1253                 break;
1254         case LDAP_TAG_DelRequest:
1255                 status = ldapsrv_DelRequest(call);
1256                 break;
1257         case LDAP_TAG_ModifyDNRequest:
1258                 status = ldapsrv_ModifyDNRequest(call);
1259                 break;
1260         case LDAP_TAG_CompareRequest:
1261                 return ldapsrv_CompareRequest(call);
1262         case LDAP_TAG_AbandonRequest:
1263                 return ldapsrv_AbandonRequest(call);
1264         case LDAP_TAG_ExtendedRequest:
1265                 status = ldapsrv_ExtendedRequest(call);
1266                 break;
1267         default:
1268                 return ldapsrv_unwilling(call, LDAP_PROTOCOL_ERROR);
1269         }
1270
1271         if (NT_STATUS_IS_OK(status)) {
1272                 ldapsrv_notification_retry_setup(call->conn->service, true);
1273         }
1274
1275         return status;
1276 }