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