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