r19522: Remove gensec and credentials dependency from the rootdse module (less
[metze/samba/wip.git] / source4 / ldap_server / ldap_backend.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/db_wrap.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30
31 #define VALID_DN_SYNTAX(dn,i) do {\
32         if (!(dn)) {\
33                 return NT_STATUS_NO_MEMORY;\
34         } else if ((dn)->comp_num < (i)) {\
35                 result = LDAP_INVALID_DN_SYNTAX;\
36                 errstr = "Invalid DN (" #i " components needed for '" #dn "')";\
37                 goto reply;\
38         }\
39 } while(0)
40
41 static int map_ldb_error(struct ldb_context *ldb, int err, const char **errstring)
42 {
43         *errstring = ldb_errstring(ldb);
44         
45         /* its 1:1 for now */
46         return err;
47 }
48
49 /*
50   connect to the sam database
51 */
52 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn) 
53 {
54         conn->ldb = ldb_wrap_connect(conn, lp_sam_url(), conn->session_info,
55                                      NULL, conn->global_catalog ? LDB_FLG_RDONLY : 0, NULL);
56         if (conn->ldb == NULL) {
57                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
58         }
59
60         if (conn->server_credentials) {
61                 char **sasl_mechs = NULL;
62                 struct gensec_security_ops **backends = gensec_security_all();
63                 enum credentials_use_kerberos use_kerberos
64                         = cli_credentials_get_kerberos_state(conn->server_credentials);
65                 struct gensec_security_ops **ops
66                         = gensec_use_kerberos_mechs(conn, backends, use_kerberos);
67                 int i, j = 0;
68                 for (i = 0; ops && ops[i]; i++) {
69                         if (ops[i]->sasl_name && ops[i]->server_start) {
70                                 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
71
72                                 if (!sasl_name) {
73                                         return NT_STATUS_NO_MEMORY;
74                                 }
75                                 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
76                                 if (!sasl_mechs) {
77                                         return NT_STATUS_NO_MEMORY;
78                                 }
79                                 sasl_mechs[j] = sasl_name;
80                                 talloc_steal(sasl_mechs, sasl_name);
81                                 sasl_mechs[j+1] = NULL;
82                                 j++;
83                         }
84                 }
85                 talloc_free(ops);
86                 ldb_set_opaque(conn->ldb, "supportedSASLMechanims", sasl_mechs);
87         }
88
89         if (conn->global_catalog) {
90                 ldb_set_opaque(conn->ldb, "global_catalog", (void *)(-1));
91         }
92
93         return NT_STATUS_OK;
94 }
95
96 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
97 {
98         struct ldapsrv_reply *reply;
99
100         reply = talloc(call, struct ldapsrv_reply);
101         if (!reply) {
102                 return NULL;
103         }
104         reply->msg = talloc(reply, struct ldap_message);
105         if (reply->msg == NULL) {
106                 talloc_free(reply);
107                 return NULL;
108         }
109
110         reply->msg->messageid = call->request->messageid;
111         reply->msg->type = type;
112         reply->msg->controls = NULL;
113
114         return reply;
115 }
116
117 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
118 {
119         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
120 }
121
122 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
123 {
124         struct ldapsrv_reply *reply;
125         struct ldap_ExtendedResponse *r;
126
127         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
128
129         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
130         if (!reply) {
131                 return NT_STATUS_NO_MEMORY;
132         }
133
134         r = &reply->msg->r.ExtendedResponse;
135         r->response.resultcode = error;
136         r->response.dn = NULL;
137         r->response.errormessage = NULL;
138         r->response.referral = NULL;
139         r->oid = NULL;
140         r->value = NULL;
141
142         ldapsrv_queue_reply(call, reply);
143         return NT_STATUS_OK;
144 }
145
146 static int ldapsrv_SearchCallback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
147 {
148         struct ldb_result *res;
149         int n;
150         
151         if (!context || !ares) {
152                 DEBUG(3, ("NULL Context or Ares in ldapsrv_SearchCallback"));
153                 return LDB_ERR_OPERATIONS_ERROR;
154         }       
155
156         res = talloc_get_type(context, struct ldb_result);
157
158         if (ares->type == LDB_REPLY_ENTRY) {
159                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
160                 if (! res->msgs) {
161                         goto error;
162                 }
163
164                 res->msgs[res->count + 1] = NULL;
165
166                 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
167                 if (! res->msgs[res->count]) {
168                         goto error;
169                 }
170
171                 res->count++;
172         }
173
174         if (ares->type == LDB_REPLY_REFERRAL) {
175                 if (res->refs) {
176                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
177                 } else {
178                         n = 0;
179                 }
180
181                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
182                 if (! res->refs) {
183                         goto error;
184                 }
185
186                 res->refs[n] = talloc_steal(res->refs, ares->referral);
187                 res->refs[n + 1] = NULL;
188         }
189
190         if (ares->controls) {
191                 res->controls = talloc_steal(res, ares->controls);
192                 if (! res->controls) {
193                         goto error;
194                 }
195         }
196
197         talloc_free(ares);
198         return LDB_SUCCESS;
199
200 error:
201         talloc_free(ares);
202         return LDB_ERR_OPERATIONS_ERROR;
203 }
204
205 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
206 {
207         struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
208         struct ldap_SearchResEntry *ent;
209         struct ldap_Result *done;
210         struct ldapsrv_reply *ent_r, *done_r;
211         void *local_ctx;
212         struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
213         struct ldb_dn *basedn;
214         struct ldb_result *res = NULL;
215         struct ldb_request *lreq;
216         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
217         const char **attrs = NULL;
218         const char *errstr = NULL;
219         int success_limit = 1;
220         int result = -1;
221         int ldb_ret = -1;
222         int i, j;
223
224         DEBUG(10, ("SearchRequest"));
225         DEBUGADD(10, (" basedn: %s", req->basedn));
226         DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
227
228         local_ctx = talloc_new(call);
229         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
230
231         basedn = ldb_dn_explode(local_ctx, req->basedn);
232         VALID_DN_SYNTAX(basedn, 0);
233
234         DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
235         DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
236
237         switch (req->scope) {
238                 case LDAP_SEARCH_SCOPE_BASE:
239                         DEBUG(10,("SearchRequest: scope: [BASE]\n"));
240                         scope = LDB_SCOPE_BASE;
241                         success_limit = 0;
242                         break;
243                 case LDAP_SEARCH_SCOPE_SINGLE:
244                         DEBUG(10,("SearchRequest: scope: [ONE]\n"));
245                         scope = LDB_SCOPE_ONELEVEL;
246                         success_limit = 0;
247                         break;
248                 case LDAP_SEARCH_SCOPE_SUB:
249                         DEBUG(10,("SearchRequest: scope: [SUB]\n"));
250                         scope = LDB_SCOPE_SUBTREE;
251                         success_limit = 0;
252                         break;
253                 default:
254                         result = LDAP_PROTOCOL_ERROR;
255                         errstr = "Invalid scope";
256                         break;
257         }
258
259         if (req->num_attributes >= 1) {
260                 attrs = talloc_array(samdb, const char *, req->num_attributes+1);
261                 NT_STATUS_HAVE_NO_MEMORY(attrs);
262
263                 for (i=0; i < req->num_attributes; i++) {
264                         DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
265                         attrs[i] = req->attributes[i];
266                 }
267                 attrs[i] = NULL;
268         }
269
270         DEBUG(5,("ldb_request dn=%s filter=%s\n", 
271                  req->basedn, ldb_filter_from_tree(call, req->tree)));
272
273         lreq = talloc(local_ctx, struct ldb_request);
274         NT_STATUS_HAVE_NO_MEMORY(lreq);
275
276         res = talloc_zero(local_ctx, struct ldb_result);
277         NT_STATUS_HAVE_NO_MEMORY(res);
278         
279         lreq->operation = LDB_SEARCH;
280         lreq->op.search.base = basedn;
281         lreq->op.search.scope = scope;
282         lreq->op.search.tree = req->tree;
283         lreq->op.search.attrs = attrs;
284
285         lreq->controls = call->request->controls;
286
287         lreq->context = res;
288         lreq->callback = ldapsrv_SearchCallback;
289
290         /* Copy the timeout from the incoming call */
291         ldb_set_timeout(samdb, lreq, req->timelimit);
292
293         ldb_ret = ldb_request(samdb, lreq);
294
295         if (ldb_ret != LDB_SUCCESS) {
296                 goto reply;
297         }
298
299         ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
300
301         if (ldb_ret == LDB_SUCCESS) {
302                 for (i = 0; i < res->count; i++) {
303                         ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
304                         NT_STATUS_HAVE_NO_MEMORY(ent_r);
305
306                         ent = &ent_r->msg->r.SearchResultEntry;
307                         ent->dn = ldb_dn_linearize(ent_r, res->msgs[i]->dn);
308                         ent->num_attributes = 0;
309                         ent->attributes = NULL;
310                         if (res->msgs[i]->num_elements == 0) {
311                                 goto queue_reply;
312                         }
313                         ent->num_attributes = res->msgs[i]->num_elements;
314                         ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
315                         NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
316                         for (j=0; j < ent->num_attributes; j++) {
317                                 ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name);
318                                 ent->attributes[j].num_values = 0;
319                                 ent->attributes[j].values = NULL;
320                                 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
321                                         continue;
322                                 }
323                                 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
324                                 ent->attributes[j].values = res->msgs[i]->elements[j].values;
325                                 talloc_steal(ent->attributes, res->msgs[i]->elements[j].values);
326                         }
327 queue_reply:
328                         ldapsrv_queue_reply(call, ent_r);
329                 }
330         }
331
332 reply:
333         done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
334         NT_STATUS_HAVE_NO_MEMORY(done_r);
335
336         done = &done_r->msg->r.SearchResultDone;
337         done->dn = NULL;
338         done->referral = NULL;
339
340         if (result != -1) {
341         } else if (ldb_ret == LDB_SUCCESS) {
342                 if (res->count >= success_limit) {
343                         DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
344                         result = LDAP_SUCCESS;
345                         errstr = NULL;
346                 } else if (res->count == 0) {
347                         DEBUG(10,("SearchRequest: no results\n"));
348                         result = LDAP_NO_SUCH_OBJECT;
349                         errstr = ldb_errstring(samdb);
350                 }
351                 if (res->controls) {
352                         done_r->msg->controls = res->controls;
353                         talloc_steal(done_r, res->controls);
354                 }
355         } else {
356                 DEBUG(10,("SearchRequest: error\n"));
357                 result = map_ldb_error(samdb, ldb_ret, &errstr);
358         }
359
360         done->resultcode = result;
361         done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
362
363         talloc_free(local_ctx);
364
365         ldapsrv_queue_reply(call, done_r);
366         return NT_STATUS_OK;
367 }
368
369 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
370 {
371         struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
372         struct ldap_Result *modify_result;
373         struct ldapsrv_reply *modify_reply;
374         void *local_ctx;
375         struct ldb_context *samdb = call->conn->ldb;
376         struct ldb_message *msg = NULL;
377         struct ldb_dn *dn;
378         const char *errstr = NULL;
379         int result = LDAP_SUCCESS;
380         int ldb_ret;
381         int i,j;
382
383         DEBUG(10, ("ModifyRequest"));
384         DEBUGADD(10, (" dn: %s", req->dn));
385
386         local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
387         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
388
389         dn = ldb_dn_explode(local_ctx, req->dn);
390         VALID_DN_SYNTAX(dn, 1);
391
392         DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
393
394         msg = talloc(local_ctx, struct ldb_message);
395         NT_STATUS_HAVE_NO_MEMORY(msg);
396
397         msg->dn = dn;
398         msg->private_data = NULL;
399         msg->num_elements = 0;
400         msg->elements = NULL;
401
402         if (req->num_mods > 0) {
403                 msg->num_elements = req->num_mods;
404                 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
405                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
406
407                 for (i=0; i < msg->num_elements; i++) {
408                         msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
409                         msg->elements[i].num_values = 0;
410                         msg->elements[i].values = NULL;
411
412                         switch (req->mods[i].type) {
413                         default:
414                                 result = LDAP_PROTOCOL_ERROR;
415                                 errstr = "Invalid LDAP_MODIFY_* type";
416                                 goto reply;
417                         case LDAP_MODIFY_ADD:
418                                 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
419                                 break;
420                         case LDAP_MODIFY_DELETE:
421                                 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
422                                 break;
423                         case LDAP_MODIFY_REPLACE:
424                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
425                                 break;
426                         }
427
428                         msg->elements[i].num_values = req->mods[i].attrib.num_values;
429                         if (msg->elements[i].num_values > 0) {
430                                 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
431                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
432
433                                 for (j=0; j < msg->elements[i].num_values; j++) {
434                                         if (!(req->mods[i].attrib.values[j].length > 0)) {
435                                                 result = LDAP_OTHER;
436                                                 errstr = "Empty attribute values are not allowed";
437                                                 goto reply;
438                                         }
439                                         msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
440                                         msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;                   
441                                 }
442                         }
443                 }
444         } else {
445                 result = LDAP_OTHER;
446                 errstr = "No mods are not allowed";
447                 goto reply;
448         }
449
450 reply:
451         modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
452         NT_STATUS_HAVE_NO_MEMORY(modify_reply);
453
454         if (result == LDAP_SUCCESS) {
455                 ldb_ret = ldb_modify(samdb, msg);
456                 result = map_ldb_error(samdb, ldb_ret, &errstr);
457         }
458
459         modify_result = &modify_reply->msg->r.AddResponse;
460         modify_result->dn = NULL;
461         modify_result->resultcode = result;
462         modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
463         modify_result->referral = NULL;
464
465         talloc_free(local_ctx);
466
467         ldapsrv_queue_reply(call, modify_reply);
468         return NT_STATUS_OK;
469
470 }
471
472 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
473 {
474         struct ldap_AddRequest *req = &call->request->r.AddRequest;
475         struct ldap_Result *add_result;
476         struct ldapsrv_reply *add_reply;
477         void *local_ctx;
478         struct ldb_context *samdb = call->conn->ldb;
479         struct ldb_message *msg = NULL;
480         struct ldb_dn *dn;
481         const char *errstr = NULL;
482         int result = LDAP_SUCCESS;
483         int ldb_ret;
484         int i,j;
485
486         DEBUG(10, ("AddRequest"));
487         DEBUGADD(10, (" dn: %s", req->dn));
488
489         local_ctx = talloc_named(call, 0, "AddRequest local memory context");
490         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
491
492         dn = ldb_dn_explode(local_ctx, req->dn);
493         VALID_DN_SYNTAX(dn,1);
494
495         DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
496
497         msg = talloc(local_ctx, struct ldb_message);
498         NT_STATUS_HAVE_NO_MEMORY(msg);
499
500         msg->dn = dn;
501         msg->private_data = NULL;
502         msg->num_elements = 0;
503         msg->elements = NULL;
504
505         if (req->num_attributes > 0) {
506                 msg->num_elements = req->num_attributes;
507                 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
508                 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
509
510                 for (i=0; i < msg->num_elements; i++) {
511                         msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
512                         msg->elements[i].flags = 0;
513                         msg->elements[i].num_values = 0;
514                         msg->elements[i].values = NULL;
515                         
516                         if (req->attributes[i].num_values > 0) {
517                                 msg->elements[i].num_values = req->attributes[i].num_values;
518                                 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
519                                 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
520
521                                 for (j=0; j < msg->elements[i].num_values; j++) {
522                                         if (!(req->attributes[i].values[j].length > 0)) {
523                                                 result = LDAP_OTHER;
524                                                 errstr = "Empty attribute values are not allowed";
525                                                 goto reply;
526                                         }
527                                         msg->elements[i].values[j].length = req->attributes[i].values[j].length;
528                                         msg->elements[i].values[j].data = req->attributes[i].values[j].data;                    
529                                 }
530                         } else {
531                                 result = LDAP_OTHER;
532                                 errstr = "No attribute values are not allowed";
533                                 goto reply;
534                         }
535                 }
536         } else {
537                 result = LDAP_OTHER;
538                 errstr = "No attributes are not allowed";
539                 goto reply;
540         }
541
542 reply:
543         add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
544         NT_STATUS_HAVE_NO_MEMORY(add_reply);
545
546         if (result == LDAP_SUCCESS) {
547                 ldb_ret = ldb_add(samdb, msg);
548                 result = map_ldb_error(samdb, ldb_ret, &errstr);
549         }
550
551         add_result = &add_reply->msg->r.AddResponse;
552         add_result->dn = NULL;
553         add_result->resultcode = result;
554         add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
555         add_result->referral = NULL;
556
557         talloc_free(local_ctx);
558
559         ldapsrv_queue_reply(call, add_reply);
560         return NT_STATUS_OK;
561
562 }
563
564 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
565 {
566         struct ldap_DelRequest *req = &call->request->r.DelRequest;
567         struct ldap_Result *del_result;
568         struct ldapsrv_reply *del_reply;
569         void *local_ctx;
570         struct ldb_context *samdb = call->conn->ldb;
571         struct ldb_dn *dn;
572         const char *errstr = NULL;
573         int result = LDAP_SUCCESS;
574         int ldb_ret;
575
576         DEBUG(10, ("DelRequest"));
577         DEBUGADD(10, (" dn: %s", req->dn));
578
579         local_ctx = talloc_named(call, 0, "DelRequest local memory context");
580         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
581
582         dn = ldb_dn_explode(local_ctx, req->dn);
583         VALID_DN_SYNTAX(dn,1);
584
585         DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
586
587 reply:
588         del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
589         NT_STATUS_HAVE_NO_MEMORY(del_reply);
590
591         if (result == LDAP_SUCCESS) {
592                 ldb_ret = ldb_delete(samdb, dn);
593                 result = map_ldb_error(samdb, ldb_ret, &errstr);
594         }
595
596         del_result = &del_reply->msg->r.DelResponse;
597         del_result->dn = NULL;
598         del_result->resultcode = result;
599         del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
600         del_result->referral = NULL;
601
602         talloc_free(local_ctx);
603
604         ldapsrv_queue_reply(call, del_reply);
605         return NT_STATUS_OK;
606 }
607
608 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
609 {
610         struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
611         struct ldap_Result *modifydn;
612         struct ldapsrv_reply *modifydn_r;
613         void *local_ctx;
614         struct ldb_context *samdb = call->conn->ldb;
615         struct ldb_dn *olddn, *newdn=NULL, *newrdn;
616         struct ldb_dn *parentdn = NULL;
617         const char *errstr = NULL;
618         int result = LDAP_SUCCESS;
619         int ldb_ret;
620
621         DEBUG(10, ("ModifyDNRequrest"));
622         DEBUGADD(10, (" dn: %s", req->dn));
623         DEBUGADD(10, (" newrdn: %s", req->newrdn));
624
625         local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
626         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
627
628         olddn = ldb_dn_explode(local_ctx, req->dn);
629         VALID_DN_SYNTAX(olddn, 2);
630
631         newrdn = ldb_dn_explode(local_ctx, req->newrdn);
632         VALID_DN_SYNTAX(newrdn, 1);
633
634         DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
635         DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
636
637         /* we can't handle the rename if we should not remove the old dn */
638         if (!req->deleteolddn) {
639                 result = LDAP_UNWILLING_TO_PERFORM;
640                 errstr = "Old RDN must be deleted";
641                 goto reply;
642         }
643
644         if (newrdn->comp_num > 1) {
645                 result = LDAP_NAMING_VIOLATION;
646                 errstr = "Error new RDN invalid";
647                 goto reply;
648         }
649
650         if (req->newsuperior) {
651                 parentdn = ldb_dn_explode(local_ctx, req->newsuperior);
652                 VALID_DN_SYNTAX(parentdn, 0);
653                 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
654                 
655                 if (parentdn->comp_num < 1) {
656                         result = LDAP_AFFECTS_MULTIPLE_DSAS;
657                         errstr = "Error new Superior DN invalid";
658                         goto reply;
659                 }
660         }
661
662         if (!parentdn) {
663                 parentdn = ldb_dn_get_parent(local_ctx, olddn);
664                 NT_STATUS_HAVE_NO_MEMORY(parentdn);
665         }
666
667         newdn = ldb_dn_make_child(local_ctx, ldb_dn_get_rdn(local_ctx, newrdn), parentdn);
668         NT_STATUS_HAVE_NO_MEMORY(newdn);
669
670 reply:
671         modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
672         NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
673
674         if (result == LDAP_SUCCESS) {
675                 ldb_ret = ldb_rename(samdb, olddn, newdn);
676                 result = map_ldb_error(samdb, ldb_ret, &errstr);
677         }
678
679         modifydn = &modifydn_r->msg->r.ModifyDNResponse;
680         modifydn->dn = NULL;
681         modifydn->resultcode = result;
682         modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
683         modifydn->referral = NULL;
684
685         talloc_free(local_ctx);
686
687         ldapsrv_queue_reply(call, modifydn_r);
688         return NT_STATUS_OK;
689 }
690
691 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
692 {
693         struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
694         struct ldap_Result *compare;
695         struct ldapsrv_reply *compare_r;
696         void *local_ctx;
697         struct ldb_context *samdb = call->conn->ldb;
698         struct ldb_result *res = NULL;
699         struct ldb_dn *dn;
700         const char *attrs[1];
701         const char *errstr = NULL;
702         const char *filter = NULL;
703         int result = LDAP_SUCCESS;
704         int ldb_ret;
705
706         DEBUG(10, ("CompareRequest"));
707         DEBUGADD(10, (" dn: %s", req->dn));
708
709         local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
710         NT_STATUS_HAVE_NO_MEMORY(local_ctx);
711
712         dn = ldb_dn_explode(local_ctx, req->dn);
713         VALID_DN_SYNTAX(dn, 1);
714
715         DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
716         filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute, 
717                                  (int)req->value.length, req->value.data);
718         NT_STATUS_HAVE_NO_MEMORY(filter);
719
720         DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
721
722         attrs[0] = NULL;
723
724 reply:
725         compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
726         NT_STATUS_HAVE_NO_MEMORY(compare_r);
727
728         if (result == LDAP_SUCCESS) {
729                 ldb_ret = ldb_search(samdb, dn, LDB_SCOPE_BASE, filter, attrs, &res);
730                 talloc_steal(samdb, res);
731                 if (ldb_ret != LDB_SUCCESS) {
732                         result = map_ldb_error(samdb, ldb_ret, &errstr);
733                         DEBUG(10,("CompareRequest: error: %s\n", errstr));
734                 } else if (res->count == 0) {
735                         DEBUG(10,("CompareRequest: doesn't matched\n"));
736                         result = LDAP_COMPARE_FALSE;
737                         errstr = NULL;
738                 } else if (res->count == 1) {
739                         DEBUG(10,("CompareRequest: matched\n"));
740                         result = LDAP_COMPARE_TRUE;
741                         errstr = NULL;
742                 } else if (res->count > 1) {
743                         result = LDAP_OTHER;
744                         errstr = "too many objects match";
745                         DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
746                 }
747         }
748
749         compare = &compare_r->msg->r.CompareResponse;
750         compare->dn = NULL;
751         compare->resultcode = result;
752         compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
753         compare->referral = NULL;
754
755         talloc_free(local_ctx);
756
757         ldapsrv_queue_reply(call, compare_r);
758         return NT_STATUS_OK;
759 }
760
761 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
762 {
763 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
764         DEBUG(10, ("AbandonRequest\n"));
765         return NT_STATUS_OK;
766 }
767
768 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
769 {
770         switch(call->request->type) {
771         case LDAP_TAG_BindRequest:
772                 return ldapsrv_BindRequest(call);
773         case LDAP_TAG_UnbindRequest:
774                 return ldapsrv_UnbindRequest(call);
775         case LDAP_TAG_SearchRequest:
776                 return ldapsrv_SearchRequest(call);
777         case LDAP_TAG_ModifyRequest:
778                 return ldapsrv_ModifyRequest(call);
779         case LDAP_TAG_AddRequest:
780                 return ldapsrv_AddRequest(call);
781         case LDAP_TAG_DelRequest:
782                 return ldapsrv_DelRequest(call);
783         case LDAP_TAG_ModifyDNRequest:
784                 return ldapsrv_ModifyDNRequest(call);
785         case LDAP_TAG_CompareRequest:
786                 return ldapsrv_CompareRequest(call);
787         case LDAP_TAG_AbandonRequest:
788                 return ldapsrv_AbandonRequest(call);
789         case LDAP_TAG_ExtendedRequest:
790                 return ldapsrv_ExtendedRequest(call);
791         default:
792                 return ldapsrv_unwilling(call, 2);
793         }
794 }