s4:ldap_server - make it "signed-safe"
[garming/samba-autobuild/.git] / source4 / ldap_server / ldap_backend.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    Copyright (C) Matthias Dieter Wallnöfer 2009
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "ldap_server/ldap_server.h"
23 #include "../lib/util/dlinklist.h"
24 #include "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 /* create and execute a modify request */
175 static int ldb_mod_req_with_controls(struct ldb_context *ldb,
176                                      const struct ldb_message *message,
177                                      struct ldb_control **controls)
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                                         NULL,
191                                         ldb_op_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 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
319 {
320         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
321         struct ldap_SearchResEntry *ent;
322         struct ldap_Result *done;
323         struct ldapsrv_reply *ent_r, *done_r;
324         TALLOC_CTX *local_ctx;
325         struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
326         struct ldb_dn *basedn;
327         struct ldb_result *res = NULL;
328         struct ldb_request *lreq;
329         struct ldb_control *search_control;
330         struct ldb_search_options_control *search_options;
331         struct ldb_control *extended_dn_control;
332         struct ldb_extended_dn_control *extended_dn_decoded = NULL;
333         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
334         const char **attrs = NULL;
335         const char *scope_str, *errstr = NULL;
336         int success_limit = 1;
337         int result = -1;
338         int ldb_ret = -1;
339         unsigned int i, j;
340         int extended_type = 1;
341
342         DEBUG(10, ("SearchRequest"));
343         DEBUGADD(10, (" basedn: %s", req->basedn));
344         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
345
346         local_ctx = talloc_new(call);
347         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
348
349         basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
350         VALID_DN_SYNTAX(basedn);
351
352         DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
353         DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
354
355         switch (req->scope) {
356                 case LDAP_SEARCH_SCOPE_BASE:
357                         scope_str = "BASE";
358                         scope = LDB_SCOPE_BASE;
359                         success_limit = 0;
360                         break;
361                 case LDAP_SEARCH_SCOPE_SINGLE:
362                         scope_str = "ONE";
363                         scope = LDB_SCOPE_ONELEVEL;
364                         success_limit = 0;
365                         break;
366                 case LDAP_SEARCH_SCOPE_SUB:
367                         scope_str = "SUB";
368                         scope = LDB_SCOPE_SUBTREE;
369                         success_limit = 0;
370                         break;
371                 default:
372                         result = LDAP_PROTOCOL_ERROR;
373                         map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR,
374                                 &errstr);
375                         errstr = talloc_asprintf(local_ctx,
376                                 "%s. Invalid scope", errstr);
377                         goto reply;
378         }
379         DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
380
381         if (req->num_attributes >= 1) {
382                 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
383                 NT_STATUS_HAVE_NO_MEMORY(attrs);
384
385                 for (i=0; i < req->num_attributes; i++) {
386                         DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
387                         attrs[i] = req->attributes[i];
388                 }
389                 attrs[i] = NULL;
390         }
391
392         DEBUG(5,("ldb_request %s dn=%s filter=%s\n", 
393                  scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
394
395         res = talloc_zero(local_ctx, struct ldb_result);
396         NT_STATUS_HAVE_NO_MEMORY(res);
397
398         ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
399                                           basedn, scope,
400                                           req->tree, attrs,
401                                           call->request->controls,
402                                           res, ldb_search_default_callback,
403                                           NULL);
404
405         if (ldb_ret != LDB_SUCCESS) {
406                 goto reply;
407         }
408
409         if (call->conn->global_catalog) {
410                 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
411
412                 search_options = NULL;
413                 if (search_control) {
414                         search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
415                         search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
416                 } else {
417                         search_options = talloc(lreq, struct ldb_search_options_control);
418                         NT_STATUS_HAVE_NO_MEMORY(search_options);
419                         search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
420                         ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
421                 }
422         }
423
424         extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
425
426         if (extended_dn_control) {
427                 if (extended_dn_control->data) {
428                         extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
429                         extended_type = extended_dn_decoded->type;
430                 } else {
431                         extended_type = 0;
432                 }
433         }
434
435         ldb_set_timeout(samdb, lreq, req->timelimit);
436
437         ldb_ret = ldb_request(samdb, lreq);
438
439         if (ldb_ret != LDB_SUCCESS) {
440                 goto reply;
441         }
442
443         ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
444
445         if (ldb_ret == LDB_SUCCESS) {
446                 for (i = 0; i < res->count; i++) {
447                         ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
448                         NT_STATUS_HAVE_NO_MEMORY(ent_r);
449
450                         /* Better to have the whole message kept here,
451                          * than to find someone further up didn't put
452                          * a value in the right spot in the talloc tree */
453                         talloc_steal(ent_r, res->msgs[i]);
454                         
455                         ent = &ent_r->msg->r.SearchResultEntry;
456                         ent->dn = ldb_dn_get_extended_linearized(ent_r, res->msgs[i]->dn, extended_type);
457                         ent->num_attributes = 0;
458                         ent->attributes = NULL;
459                         if (res->msgs[i]->num_elements == 0) {
460                                 goto queue_reply;
461                         }
462                         ent->num_attributes = res->msgs[i]->num_elements;
463                         ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
464                         NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
465                         for (j=0; j < ent->num_attributes; j++) {
466                                 ent->attributes[j].name = res->msgs[i]->elements[j].name;
467                                 ent->attributes[j].num_values = 0;
468                                 ent->attributes[j].values = NULL;
469                                 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
470                                         continue;
471                                 }
472                                 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
473                                 ent->attributes[j].values = res->msgs[i]->elements[j].values;
474                         }
475 queue_reply:
476                         ldapsrv_queue_reply(call, ent_r);
477                 }
478
479                 /* Send back referrals if they do exist (search operations) */
480                 if (res->refs != NULL) {
481                         char **ref;
482                         struct ldap_SearchResRef *ent_ref;
483
484                         for (ref = res->refs; *ref != NULL; ++ref) {
485                                 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
486                                 NT_STATUS_HAVE_NO_MEMORY(ent_r);
487
488                                 /* Better to have the whole referrals kept here,
489                                  * than to find someone further up didn't put
490                                  * a value in the right spot in the talloc tree
491                                  */
492                                 talloc_steal(ent_r, *ref);
493
494                                 ent_ref = &ent_r->msg->r.SearchResultReference;
495                                 ent_ref->referral = *ref;
496
497                                 ldapsrv_queue_reply(call, ent_r);
498                         }
499                 }
500         }
501
502 reply:
503         done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
504         NT_STATUS_HAVE_NO_MEMORY(done_r);
505
506         done = &done_r->msg->r.SearchResultDone;
507         done->dn = NULL;
508         done->referral = NULL;
509
510         if (result != -1) {
511         } else if (ldb_ret == LDB_SUCCESS) {
512                 if (res->count >= success_limit) {
513                         DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
514                         result = LDAP_SUCCESS;
515                         errstr = NULL;
516                 }
517                 if (res->controls) {
518                         done_r->msg->controls = res->controls;
519                         talloc_steal(done_r, res->controls);
520                 }
521         } else {
522                 DEBUG(10,("SearchRequest: error\n"));
523                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
524         }
525
526         done->resultcode = result;
527         done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
528
529         talloc_free(local_ctx);
530
531         ldapsrv_queue_reply(call, done_r);
532         return NT_STATUS_OK;
533 }
534
535 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
536 {
537         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
538         struct ldap_Result *modify_result;
539         struct ldapsrv_reply *modify_reply;
540         TALLOC_CTX *local_ctx;
541         struct ldb_context *samdb = call->conn->ldb;
542         struct ldb_message *msg = NULL;
543         struct ldb_dn *dn;
544         const char *errstr = NULL;
545         int result = LDAP_SUCCESS;
546         int ldb_ret;
547         unsigned int i,j;
548
549         DEBUG(10, ("ModifyRequest"));
550         DEBUGADD(10, (" dn: %s", req->dn));
551
552         local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
553         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
554
555         dn = ldb_dn_new(local_ctx, samdb, req->dn);
556         VALID_DN_SYNTAX(dn);
557
558         DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
559
560         msg = talloc(local_ctx, struct ldb_message);
561         NT_STATUS_HAVE_NO_MEMORY(msg);
562
563         msg->dn = dn;
564         msg->num_elements = 0;
565         msg->elements = NULL;
566
567         if (req->num_mods > 0) {
568                 msg->num_elements = req->num_mods;
569                 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
570                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
571
572                 for (i=0; i < msg->num_elements; i++) {
573                         msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
574                         msg->elements[i].num_values = 0;
575                         msg->elements[i].values = NULL;
576
577                         switch (req->mods[i].type) {
578                         default:
579                                 result = LDAP_PROTOCOL_ERROR;
580                                 map_ldb_error(local_ctx,
581                                         LDB_ERR_PROTOCOL_ERROR, &errstr);
582                                 errstr = talloc_asprintf(local_ctx,
583                                         "%s. Invalid LDAP_MODIFY_* type", errstr);
584                                 goto reply;
585                         case LDAP_MODIFY_ADD:
586                                 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
587                                 break;
588                         case LDAP_MODIFY_DELETE:
589                                 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
590                                 break;
591                         case LDAP_MODIFY_REPLACE:
592                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
593                                 break;
594                         }
595
596                         msg->elements[i].num_values = req->mods[i].attrib.num_values;
597                         if (msg->elements[i].num_values > 0) {
598                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
599                                                                        msg->elements[i].num_values);
600                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
601
602                                 for (j=0; j < msg->elements[i].num_values; j++) {
603                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
604                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
605                                 }
606                         }
607                 }
608         }
609
610 reply:
611         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
612         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
613
614         if (result == LDAP_SUCCESS) {
615                 ldb_ret = ldb_mod_req_with_controls(samdb, msg, call->request->controls);
616                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
617         }
618
619         modify_result = &modify_reply->msg->r.AddResponse;
620         modify_result->dn = NULL;
621         modify_result->resultcode = result;
622         modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
623         modify_result->referral = NULL;
624
625         talloc_free(local_ctx);
626
627         ldapsrv_queue_reply(call, modify_reply);
628         return NT_STATUS_OK;
629
630 }
631
632 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
633 {
634         struct ldap_AddRequest *req = &call->request->r.AddRequest;
635         struct ldap_Result *add_result;
636         struct ldapsrv_reply *add_reply;
637         TALLOC_CTX *local_ctx;
638         struct ldb_context *samdb = call->conn->ldb;
639         struct ldb_message *msg = NULL;
640         struct ldb_dn *dn;
641         const char *errstr = NULL;
642         int result = LDAP_SUCCESS;
643         int ldb_ret;
644         unsigned int i,j;
645
646         DEBUG(10, ("AddRequest"));
647         DEBUGADD(10, (" dn: %s", req->dn));
648
649         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
650         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
651
652         dn = ldb_dn_new(local_ctx, samdb, req->dn);
653         VALID_DN_SYNTAX(dn);
654
655         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
656
657         msg = talloc(local_ctx, struct ldb_message);
658         NT_STATUS_HAVE_NO_MEMORY(msg);
659
660         msg->dn = dn;
661         msg->num_elements = 0;
662         msg->elements = NULL;
663
664         if (req->num_attributes > 0) {
665                 msg->num_elements = req->num_attributes;
666                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
667                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
668
669                 for (i=0; i < msg->num_elements; i++) {
670                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
671                         msg->elements[i].flags = 0;
672                         msg->elements[i].num_values = 0;
673                         msg->elements[i].values = NULL;
674                         
675                         if (req->attributes[i].num_values > 0) {
676                                 msg->elements[i].num_values = req->attributes[i].num_values;
677                                 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
678                                                                        msg->elements[i].num_values);
679                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
680
681                                 for (j=0; j < msg->elements[i].num_values; j++) {
682                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
683                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
684                                 }
685                         }
686                 }
687         }
688
689 reply:
690         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
691         NT_STATUS_HAVE_NO_MEMORY(add_reply);
692
693         if (result == LDAP_SUCCESS) {
694                 ldb_ret = ldb_add(samdb, msg);
695                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
696         }
697
698         add_result = &add_reply->msg->r.AddResponse;
699         add_result->dn = NULL;
700         add_result->resultcode = result;
701         add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
702         add_result->referral = NULL;
703
704         talloc_free(local_ctx);
705
706         ldapsrv_queue_reply(call, add_reply);
707         return NT_STATUS_OK;
708
709 }
710
711 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
712 {
713         struct ldap_DelRequest *req = &call->request->r.DelRequest;
714         struct ldap_Result *del_result;
715         struct ldapsrv_reply *del_reply;
716         TALLOC_CTX *local_ctx;
717         struct ldb_context *samdb = call->conn->ldb;
718         struct ldb_dn *dn;
719         const char *errstr = NULL;
720         int result = LDAP_SUCCESS;
721         int ldb_ret;
722
723         DEBUG(10, ("DelRequest"));
724         DEBUGADD(10, (" dn: %s", req->dn));
725
726         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
727         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
728
729         dn = ldb_dn_new(local_ctx, samdb, req->dn);
730         VALID_DN_SYNTAX(dn);
731
732         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
733
734 reply:
735         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
736         NT_STATUS_HAVE_NO_MEMORY(del_reply);
737
738         if (result == LDAP_SUCCESS) {
739                 ldb_ret = ldb_delete(samdb, dn);
740                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
741         }
742
743         del_result = &del_reply->msg->r.DelResponse;
744         del_result->dn = NULL;
745         del_result->resultcode = result;
746         del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
747         del_result->referral = NULL;
748
749         talloc_free(local_ctx);
750
751         ldapsrv_queue_reply(call, del_reply);
752         return NT_STATUS_OK;
753 }
754
755 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
756 {
757         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
758         struct ldap_Result *modifydn;
759         struct ldapsrv_reply *modifydn_r;
760         TALLOC_CTX *local_ctx;
761         struct ldb_context *samdb = call->conn->ldb;
762         struct ldb_dn *olddn, *newdn=NULL, *newrdn;
763         struct ldb_dn *parentdn = NULL;
764         const char *errstr = NULL;
765         int result = LDAP_SUCCESS;
766         int ldb_ret;
767
768         DEBUG(10, ("ModifyDNRequest"));
769         DEBUGADD(10, (" dn: %s", req->dn));
770         DEBUGADD(10, (" newrdn: %s", req->newrdn));
771
772         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
773         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
774
775         olddn = ldb_dn_new(local_ctx, samdb, req->dn);
776         VALID_DN_SYNTAX(olddn);
777
778         newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
779         VALID_DN_SYNTAX(newrdn);
780
781         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
782         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
783
784         if (ldb_dn_get_comp_num(newrdn) != 1) {
785                 result = LDAP_INVALID_DN_SYNTAX;
786                 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, &errstr);
787                 goto reply;
788         }
789
790         /* we can't handle the rename if we should not remove the old dn */
791         if (!req->deleteolddn) {
792                 result = LDAP_UNWILLING_TO_PERFORM;
793                 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, &errstr);
794                 errstr = talloc_asprintf(local_ctx,
795                         "%s. Old RDN must be deleted", errstr);
796                 goto reply;
797         }
798
799         if (req->newsuperior) {
800                 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
801                 VALID_DN_SYNTAX(parentdn);
802                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
803                 
804                 if (ldb_dn_get_comp_num(parentdn) < 1) {
805                         result = LDAP_AFFECTS_MULTIPLE_DSAS;
806                         map_ldb_error(local_ctx, LDB_ERR_AFFECTS_MULTIPLE_DSAS,
807                                 &errstr);
808                         errstr = talloc_asprintf(local_ctx,
809                                 "%s. Error new Superior DN invalid", errstr);
810                         goto reply;
811                 }
812         }
813
814         if (!parentdn) {
815                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
816                 NT_STATUS_HAVE_NO_MEMORY(parentdn);
817         }
818
819         if ( ! ldb_dn_add_child(parentdn, newrdn)) {
820                 result = LDAP_OTHER;
821                 goto reply;
822         }
823         newdn = parentdn;
824
825 reply:
826         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
827         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
828
829         if (result == LDAP_SUCCESS) {
830                 ldb_ret = ldb_rename(samdb, olddn, newdn);
831                 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
832         }
833
834         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
835         modifydn->dn = NULL;
836         modifydn->resultcode = result;
837         modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
838         modifydn->referral = NULL;
839
840         talloc_free(local_ctx);
841
842         ldapsrv_queue_reply(call, modifydn_r);
843         return NT_STATUS_OK;
844 }
845
846 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
847 {
848         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
849         struct ldap_Result *compare;
850         struct ldapsrv_reply *compare_r;
851         TALLOC_CTX *local_ctx;
852         struct ldb_context *samdb = call->conn->ldb;
853         struct ldb_result *res = NULL;
854         struct ldb_dn *dn;
855         const char *attrs[1];
856         const char *errstr = NULL;
857         const char *filter = NULL;
858         int result = LDAP_SUCCESS;
859         int ldb_ret;
860
861         DEBUG(10, ("CompareRequest"));
862         DEBUGADD(10, (" dn: %s", req->dn));
863
864         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
865         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
866
867         dn = ldb_dn_new(local_ctx, samdb, req->dn);
868         VALID_DN_SYNTAX(dn);
869
870         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
871         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
872                                  (int)req->value.length, req->value.data);
873         NT_STATUS_HAVE_NO_MEMORY(filter);
874
875         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
876
877         attrs[0] = NULL;
878
879 reply:
880         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
881         NT_STATUS_HAVE_NO_MEMORY(compare_r);
882
883         if (result == LDAP_SUCCESS) {
884                 ldb_ret = ldb_search(samdb, local_ctx, &res,
885                                      dn, LDB_SCOPE_BASE, attrs, "%s", filter);
886                 if (ldb_ret != LDB_SUCCESS) {
887                         result = map_ldb_error(local_ctx, ldb_ret, &errstr);
888                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
889                 } else if (res->count == 0) {
890                         DEBUG(10,("CompareRequest: doesn't matched\n"));
891                         result = LDAP_COMPARE_FALSE;
892                         errstr = NULL;
893                 } else if (res->count == 1) {
894                         DEBUG(10,("CompareRequest: matched\n"));
895                         result = LDAP_COMPARE_TRUE;
896                         errstr = NULL;
897                 } else if (res->count > 1) {
898                         result = LDAP_OTHER;
899                         map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
900                         errstr = talloc_asprintf(local_ctx,
901                                 "%s. Too many objects match!", errstr);
902                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
903                 }
904         }
905
906         compare = &compare_r->msg->r.CompareResponse;
907         compare->dn = NULL;
908         compare->resultcode = result;
909         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
910         compare->referral = NULL;
911
912         talloc_free(local_ctx);
913
914         ldapsrv_queue_reply(call, compare_r);
915         return NT_STATUS_OK;
916 }
917
918 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
919 {
920 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
921         DEBUG(10, ("AbandonRequest\n"));
922         return NT_STATUS_OK;
923 }
924
925 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
926 {
927         unsigned int i;
928         struct ldap_message *msg = call->request;
929         /* Check for undecoded critical extensions */
930         for (i=0; msg->controls && msg->controls[i]; i++) {
931                 if (!msg->controls_decoded[i] && 
932                     msg->controls[i]->critical) {
933                         DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
934                                   msg->controls[i]->oid));
935                         return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
936                 }
937         }
938
939         switch(call->request->type) {
940         case LDAP_TAG_BindRequest:
941                 return ldapsrv_BindRequest(call);
942         case LDAP_TAG_UnbindRequest:
943                 return ldapsrv_UnbindRequest(call);
944         case LDAP_TAG_SearchRequest:
945                 return ldapsrv_SearchRequest(call);
946         case LDAP_TAG_ModifyRequest:
947                 return ldapsrv_ModifyRequest(call);
948         case LDAP_TAG_AddRequest:
949                 return ldapsrv_AddRequest(call);
950         case LDAP_TAG_DelRequest:
951                 return ldapsrv_DelRequest(call);
952         case LDAP_TAG_ModifyDNRequest:
953                 return ldapsrv_ModifyDNRequest(call);
954         case LDAP_TAG_CompareRequest:
955                 return ldapsrv_CompareRequest(call);
956         case LDAP_TAG_AbandonRequest:
957                 return ldapsrv_AbandonRequest(call);
958         case LDAP_TAG_ExtendedRequest:
959                 return ldapsrv_ExtendedRequest(call);
960         default:
961                 return ldapsrv_unwilling(call, 2);
962         }
963 }