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