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