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