s4:LDB/LDAP - Re-allow renames
[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                                         if (!(req->mods[i].attrib.values[j].length > 0)) {
538                                                 result = LDAP_OTHER;
539
540                                                 map_ldb_error(local_ctx,
541                                                         LDB_ERR_OTHER, &errstr);
542                                                 errstr = talloc_asprintf(local_ctx,
543                                                         "%s. Empty attribute values not allowed", errstr);
544                                                 goto reply;
545                                         }
546                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
547                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
548                                 }
549                         }
550                 }
551         } else {
552                 result = LDAP_OTHER;
553                 map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
554                 errstr = talloc_asprintf(local_ctx,
555                         "%s. No mods are not allowed", errstr);
556                 goto reply;
557         }
558
559 reply:
560         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
561         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
562
563         if (result == LDAP_SUCCESS) {
564                 ldb_ret = ldb_modify(samdb, msg);
565                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
566         }
567
568         modify_result = &modify_reply->msg->r.AddResponse;
569         modify_result->dn = NULL;
570         modify_result->resultcode = result;
571         modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
572         modify_result->referral = NULL;
573
574         talloc_free(local_ctx);
575
576         ldapsrv_queue_reply(call, modify_reply);
577         return NT_STATUS_OK;
578
579 }
580
581 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
582 {
583         struct ldap_AddRequest *req = &call->request->r.AddRequest;
584         struct ldap_Result *add_result;
585         struct ldapsrv_reply *add_reply;
586         TALLOC_CTX *local_ctx;
587         struct ldb_context *samdb = call->conn->ldb;
588         struct ldb_message *msg = NULL;
589         struct ldb_dn *dn;
590         const char *errstr = NULL;
591         int result = LDAP_SUCCESS;
592         int ldb_ret;
593         int i,j;
594
595         DEBUG(10, ("AddRequest"));
596         DEBUGADD(10, (" dn: %s", req->dn));
597
598         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
599         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
600
601         dn = ldb_dn_new(local_ctx, samdb, req->dn);
602         VALID_DN_SYNTAX(dn);
603
604         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
605
606         msg = talloc(local_ctx, struct ldb_message);
607         NT_STATUS_HAVE_NO_MEMORY(msg);
608
609         msg->dn = dn;
610         msg->num_elements = 0;
611         msg->elements = NULL;
612
613         if (req->num_attributes > 0) {
614                 msg->num_elements = req->num_attributes;
615                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
616                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
617
618                 for (i=0; i < msg->num_elements; i++) {
619                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
620                         msg->elements[i].flags = 0;
621                         msg->elements[i].num_values = 0;
622                         msg->elements[i].values = NULL;
623                         
624                         if (req->attributes[i].num_values > 0) {
625                                 msg->elements[i].num_values = req->attributes[i].num_values;
626                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
627                                                                        msg->elements[i].num_values);
628                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
629
630                                 for (j=0; j < msg->elements[i].num_values; j++) {
631                                         if (!(req->attributes[i].values[j].length > 0)) {
632                                                 result = LDAP_OTHER;
633                                                 map_ldb_error(local_ctx,
634                                                         LDB_ERR_OTHER, &errstr);
635                                                 errstr = talloc_asprintf(local_ctx,
636                                                         "%s. Empty attribute values not allowed", errstr);
637                                                 goto reply;
638                                         }
639                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
640                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
641                                 }
642                         } else {
643                                 result = LDAP_OTHER;
644                                 map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
645                                 errstr = talloc_asprintf(local_ctx,
646                                         "%s. No attribute values are not allowed", errstr);
647                                 goto reply;
648                         }
649                 }
650         } else {
651                 result = LDAP_OTHER;
652                 map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
653                 errstr = talloc_asprintf(local_ctx, 
654                         "%s. No attributes are not allowed", errstr);
655                 goto reply;
656         }
657
658 reply:
659         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
660         NT_STATUS_HAVE_NO_MEMORY(add_reply);
661
662         if (result == LDAP_SUCCESS) {
663                 ldb_ret = ldb_add(samdb, msg);
664                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
665         }
666
667         add_result = &add_reply->msg->r.AddResponse;
668         add_result->dn = NULL;
669         add_result->resultcode = result;
670         add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
671         add_result->referral = NULL;
672
673         talloc_free(local_ctx);
674
675         ldapsrv_queue_reply(call, add_reply);
676         return NT_STATUS_OK;
677
678 }
679
680 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
681 {
682         struct ldap_DelRequest *req = &call->request->r.DelRequest;
683         struct ldap_Result *del_result;
684         struct ldapsrv_reply *del_reply;
685         TALLOC_CTX *local_ctx;
686         struct ldb_context *samdb = call->conn->ldb;
687         struct ldb_dn *dn;
688         const char *errstr = NULL;
689         int result = LDAP_SUCCESS;
690         int ldb_ret;
691
692         DEBUG(10, ("DelRequest"));
693         DEBUGADD(10, (" dn: %s", req->dn));
694
695         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
696         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
697
698         dn = ldb_dn_new(local_ctx, samdb, req->dn);
699         VALID_DN_SYNTAX(dn);
700
701         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
702
703 reply:
704         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
705         NT_STATUS_HAVE_NO_MEMORY(del_reply);
706
707         if (result == LDAP_SUCCESS) {
708                 ldb_ret = ldb_delete(samdb, dn);
709                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
710         }
711
712         del_result = &del_reply->msg->r.DelResponse;
713         del_result->dn = NULL;
714         del_result->resultcode = result;
715         del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
716         del_result->referral = NULL;
717
718         talloc_free(local_ctx);
719
720         ldapsrv_queue_reply(call, del_reply);
721         return NT_STATUS_OK;
722 }
723
724 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
725 {
726         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
727         struct ldap_Result *modifydn;
728         struct ldapsrv_reply *modifydn_r;
729         TALLOC_CTX *local_ctx;
730         struct ldb_context *samdb = call->conn->ldb;
731         struct ldb_dn *olddn, *newdn=NULL, *newrdn;
732         struct ldb_dn *parentdn = NULL;
733         const char *errstr = NULL;
734         int result = LDAP_SUCCESS;
735         int ldb_ret;
736
737         DEBUG(10, ("ModifyDNRequest"));
738         DEBUGADD(10, (" dn: %s", req->dn));
739         DEBUGADD(10, (" newrdn: %s", req->newrdn));
740
741         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
742         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
743
744         olddn = ldb_dn_new(local_ctx, samdb, req->dn);
745         VALID_DN_SYNTAX(olddn);
746
747         newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
748         VALID_DN_SYNTAX(newrdn);
749
750         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
751         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
752
753         if (ldb_dn_get_comp_num(newrdn) != 1) {
754                 result = LDAP_INVALID_DN_SYNTAX;
755                 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, &errstr);
756                 goto reply;
757         }
758
759         /* we can't handle the rename if we should not remove the old dn */
760         if (!req->deleteolddn) {
761                 result = LDAP_UNWILLING_TO_PERFORM;
762                 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, &errstr);
763                 errstr = talloc_asprintf(local_ctx,
764                         "%s. Old RDN must be deleted", errstr);
765                 goto reply;
766         }
767
768         if (req->newsuperior) {
769                 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
770                 VALID_DN_SYNTAX(parentdn);
771                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
772                 
773                 if (ldb_dn_get_comp_num(parentdn) < 1) {
774                         result = LDAP_AFFECTS_MULTIPLE_DSAS;
775                         map_ldb_error(local_ctx, LDB_ERR_AFFECTS_MULTIPLE_DSAS,
776                                 &errstr);
777                         errstr = talloc_asprintf(local_ctx,
778                                 "%s. Error new Superior DN invalid", errstr);
779                         goto reply;
780                 }
781         }
782
783         if (!parentdn) {
784                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
785                 NT_STATUS_HAVE_NO_MEMORY(parentdn);
786         }
787
788         if ( ! ldb_dn_add_child(parentdn, newrdn)) {
789                 result = LDAP_OTHER;
790                 goto reply;
791         }
792         newdn = parentdn;
793
794 reply:
795         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
796         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
797
798         if (result == LDAP_SUCCESS) {
799                 ldb_ret = ldb_rename(samdb, olddn, newdn);
800                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
801         }
802
803         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
804         modifydn->dn = NULL;
805         modifydn->resultcode = result;
806         modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
807         modifydn->referral = NULL;
808
809         talloc_free(local_ctx);
810
811         ldapsrv_queue_reply(call, modifydn_r);
812         return NT_STATUS_OK;
813 }
814
815 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
816 {
817         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
818         struct ldap_Result *compare;
819         struct ldapsrv_reply *compare_r;
820         TALLOC_CTX *local_ctx;
821         struct ldb_context *samdb = call->conn->ldb;
822         struct ldb_result *res = NULL;
823         struct ldb_dn *dn;
824         const char *attrs[1];
825         const char *errstr = NULL;
826         const char *filter = NULL;
827         int result = LDAP_SUCCESS;
828         int ldb_ret;
829
830         DEBUG(10, ("CompareRequest"));
831         DEBUGADD(10, (" dn: %s", req->dn));
832
833         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
834         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
835
836         dn = ldb_dn_new(local_ctx, samdb, req->dn);
837         VALID_DN_SYNTAX(dn);
838
839         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
840         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
841                                  (int)req->value.length, req->value.data);
842         NT_STATUS_HAVE_NO_MEMORY(filter);
843
844         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
845
846         attrs[0] = NULL;
847
848 reply:
849         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
850         NT_STATUS_HAVE_NO_MEMORY(compare_r);
851
852         if (result == LDAP_SUCCESS) {
853                 ldb_ret = ldb_search(samdb, local_ctx, &res,
854                                      dn, LDB_SCOPE_BASE, attrs, "%s", filter);
855                 if (ldb_ret != LDB_SUCCESS) {
856                         result = map_ldb_error(local_ctx, ldb_ret, &errstr);
857                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
858                 } else if (res->count == 0) {
859                         DEBUG(10,("CompareRequest: doesn't matched\n"));
860                         result = LDAP_COMPARE_FALSE;
861                         errstr = NULL;
862                 } else if (res->count == 1) {
863                         DEBUG(10,("CompareRequest: matched\n"));
864                         result = LDAP_COMPARE_TRUE;
865                         errstr = NULL;
866                 } else if (res->count > 1) {
867                         result = LDAP_OTHER;
868                         map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
869                         errstr = talloc_asprintf(local_ctx,
870                                 "%s. Too many objects match!", errstr);
871                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
872                 }
873         }
874
875         compare = &compare_r->msg->r.CompareResponse;
876         compare->dn = NULL;
877         compare->resultcode = result;
878         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
879         compare->referral = NULL;
880
881         talloc_free(local_ctx);
882
883         ldapsrv_queue_reply(call, compare_r);
884         return NT_STATUS_OK;
885 }
886
887 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
888 {
889 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
890         DEBUG(10, ("AbandonRequest\n"));
891         return NT_STATUS_OK;
892 }
893
894 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
895 {
896         int i;
897         struct ldap_message *msg = call->request;
898         /* Check for undecoded critical extensions */
899         for (i=0; msg->controls && msg->controls[i]; i++) {
900                 if (!msg->controls_decoded[i] && 
901                     msg->controls[i]->critical) {
902                         DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
903                                   msg->controls[i]->oid));
904                         return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
905                 }
906         }
907
908         switch(call->request->type) {
909         case LDAP_TAG_BindRequest:
910                 return ldapsrv_BindRequest(call);
911         case LDAP_TAG_UnbindRequest:
912                 return ldapsrv_UnbindRequest(call);
913         case LDAP_TAG_SearchRequest:
914                 return ldapsrv_SearchRequest(call);
915         case LDAP_TAG_ModifyRequest:
916                 return ldapsrv_ModifyRequest(call);
917         case LDAP_TAG_AddRequest:
918                 return ldapsrv_AddRequest(call);
919         case LDAP_TAG_DelRequest:
920                 return ldapsrv_DelRequest(call);
921         case LDAP_TAG_ModifyDNRequest:
922                 return ldapsrv_ModifyDNRequest(call);
923         case LDAP_TAG_CompareRequest:
924                 return ldapsrv_CompareRequest(call);
925         case LDAP_TAG_AbandonRequest:
926                 return ldapsrv_AbandonRequest(call);
927         case LDAP_TAG_ExtendedRequest:
928                 return ldapsrv_ExtendedRequest(call);
929         default:
930                 return ldapsrv_unwilling(call, 2);
931         }
932 }