2 Unix SMB/CIFS implementation.
3 LDAP server SIMPLE LDB implementation
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Simo Sorce 2004
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "ldap_server/ldap_server.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "auth/auth.h"
28 #include "dsdb/samdb/samdb.h"
30 #define VALID_DN_SYNTAX(dn,i) do {\
32 return NT_STATUS_NO_MEMORY;\
33 } else if ((dn)->comp_num < (i)) {\
34 result = LDAP_INVALID_DN_SYNTAX;\
35 errstr = "Invalid DN (" #i " components needed for '" #dn "')";\
42 map an error code from ldb to ldap
44 static int sldb_map_error(struct ldapsrv_partition *partition, int ldb_ret,
47 struct ldb_context *samdb = talloc_get_type(partition->private,
49 *errstr = ldb_errstring(samdb);
56 connect to the sam database
58 NTSTATUS sldb_Init(struct ldapsrv_partition *partition, struct ldapsrv_connection *conn)
60 TALLOC_CTX *mem_ctx = talloc_new(partition);
61 struct ldb_context *ldb;
62 ldb = samdb_connect(mem_ctx, conn->session_info);
65 return NT_STATUS_INTERNAL_DB_CORRUPTION;
67 talloc_steal(partition, ldb);
68 partition->private = ldb;
74 Re-connect to the ldb after a bind (this does not handle the bind
75 itself, but just notes the change in credentials)
77 NTSTATUS sldb_Bind(struct ldapsrv_partition *partition, struct ldapsrv_connection *conn)
79 struct ldb_context *samdb = partition->private;
81 status = sldb_Init(partition, conn);
82 if (NT_STATUS_IS_OK(status)) {
83 /* don't leak the old LDB */
89 static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
90 struct ldap_SearchRequest *r)
93 struct ldb_dn *basedn;
94 struct ldap_Result *done;
95 struct ldap_SearchResEntry *ent;
96 struct ldapsrv_reply *ent_r, *done_r;
97 int result = LDAP_SUCCESS;
98 struct ldb_context *samdb;
99 struct ldb_result *res = NULL;
101 int success_limit = 1;
102 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
103 const char **attrs = NULL;
104 const char *errstr = NULL;
105 struct ldb_request lreq;
107 local_ctx = talloc_named(call, 0, "sldb_Search local memory context");
108 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
110 samdb = talloc_get_type(partition->private, struct ldb_context);
112 basedn = ldb_dn_explode(local_ctx, r->basedn);
113 VALID_DN_SYNTAX(basedn, 0);
115 DEBUG(10, ("sldb_Search: basedn: [%s]\n", r->basedn));
116 DEBUG(10, ("sldb_Search: filter: [%s]\n", ldb_filter_from_tree(call, r->tree)));
119 case LDAP_SEARCH_SCOPE_BASE:
120 DEBUG(10,("sldb_Search: scope: [BASE]\n"));
121 scope = LDB_SCOPE_BASE;
124 case LDAP_SEARCH_SCOPE_SINGLE:
125 DEBUG(10,("sldb_Search: scope: [ONE]\n"));
126 scope = LDB_SCOPE_ONELEVEL;
129 case LDAP_SEARCH_SCOPE_SUB:
130 DEBUG(10,("sldb_Search: scope: [SUB]\n"));
131 scope = LDB_SCOPE_SUBTREE;
136 if (r->num_attributes >= 1) {
137 attrs = talloc_array(samdb, const char *, r->num_attributes+1);
138 NT_STATUS_HAVE_NO_MEMORY(attrs);
140 for (i=0; i < r->num_attributes; i++) {
141 DEBUG(10,("sldb_Search: attrs: [%s]\n",r->attributes[i]));
142 attrs[i] = r->attributes[i];
147 DEBUG(5,("ldb_request dn=%s filter=%s\n",
148 r->basedn, ldb_filter_from_tree(call, r->tree)));
151 lreq.operation = LDB_REQ_SEARCH;
152 lreq.op.search.base = basedn;
153 lreq.op.search.scope = scope;
154 lreq.op.search.tree = r->tree;
155 lreq.op.search.attrs = attrs;
157 ret = ldb_request(samdb, &lreq);
159 res = talloc_steal(samdb, lreq.op.search.res);
161 if (ret == LDB_SUCCESS) {
162 for (i = 0; i < res->count; i++) {
163 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
164 NT_STATUS_HAVE_NO_MEMORY(ent_r);
166 ent = &ent_r->msg->r.SearchResultEntry;
167 ent->dn = ldb_dn_linearize(ent_r, res->msgs[i]->dn);
168 ent->num_attributes = 0;
169 ent->attributes = NULL;
170 if (res->msgs[i]->num_elements == 0) {
173 ent->num_attributes = res->msgs[i]->num_elements;
174 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
175 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
176 for (j=0; j < ent->num_attributes; j++) {
177 ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name);
178 ent->attributes[j].num_values = 0;
179 ent->attributes[j].values = NULL;
180 if (r->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
183 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
184 ent->attributes[j].values = talloc_array(ent->attributes,
185 DATA_BLOB, ent->attributes[j].num_values);
186 NT_STATUS_HAVE_NO_MEMORY(ent->attributes[j].values);
187 for (y=0; y < ent->attributes[j].num_values; y++) {
188 ent->attributes[j].values[y].length = res->msgs[i]->elements[j].values[y].length;
189 ent->attributes[j].values[y].data = talloc_steal(ent->attributes[j].values,
190 res->msgs[i]->elements[j].values[y].data);
194 ldapsrv_queue_reply(call, ent_r);
199 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
200 NT_STATUS_HAVE_NO_MEMORY(done_r);
202 if (ret == LDB_SUCCESS) {
203 if (res->count >= success_limit) {
204 DEBUG(10,("sldb_Search: results: [%d]\n", res->count));
205 result = LDAP_SUCCESS;
207 } else if (res->count == 0) {
208 DEBUG(10,("sldb_Search: no results\n"));
209 result = LDAP_NO_SUCH_OBJECT;
210 errstr = ldb_errstring(samdb);
213 DEBUG(10,("sldb_Search: error\n"));
215 errstr = ldb_errstring(samdb);
218 done = &done_r->msg->r.SearchResultDone;
220 done->resultcode = result;
221 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
222 done->referral = NULL;
224 talloc_free(local_ctx);
226 ldapsrv_queue_reply(call, done_r);
230 static NTSTATUS sldb_Add(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
231 struct ldap_AddRequest *r)
235 struct ldap_Result *add_result;
236 struct ldapsrv_reply *add_reply;
238 struct ldb_context *samdb;
239 struct ldb_message *msg = NULL;
240 int result = LDAP_SUCCESS;
241 const char *errstr = NULL;
244 local_ctx = talloc_named(call, 0, "sldb_Add local memory context");
245 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
247 samdb = talloc_get_type(partition->private, struct ldb_context);
249 dn = ldb_dn_explode(local_ctx, r->dn);
250 VALID_DN_SYNTAX(dn,1);
252 DEBUG(10, ("sldb_add: dn: [%s]\n", r->dn));
254 msg = talloc(local_ctx, struct ldb_message);
255 NT_STATUS_HAVE_NO_MEMORY(msg);
258 msg->private_data = NULL;
259 msg->num_elements = 0;
260 msg->elements = NULL;
262 if (r->num_attributes > 0) {
263 msg->num_elements = r->num_attributes;
264 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
265 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
267 for (i=0; i < msg->num_elements; i++) {
268 msg->elements[i].name = discard_const_p(char, r->attributes[i].name);
269 msg->elements[i].flags = 0;
270 msg->elements[i].num_values = 0;
271 msg->elements[i].values = NULL;
273 if (r->attributes[i].num_values > 0) {
274 msg->elements[i].num_values = r->attributes[i].num_values;
275 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
276 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
278 for (j=0; j < msg->elements[i].num_values; j++) {
279 if (!(r->attributes[i].values[j].length > 0)) {
281 errstr = "Empty attribute values are not allowed";
284 msg->elements[i].values[j].length = r->attributes[i].values[j].length;
285 msg->elements[i].values[j].data = r->attributes[i].values[j].data;
289 errstr = "No attribute values are not allowed";
295 errstr = "No attributes are not allowed";
300 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
301 NT_STATUS_HAVE_NO_MEMORY(add_reply);
303 if (result == LDAP_SUCCESS) {
304 ldb_ret = ldb_add(samdb, msg);
305 result = sldb_map_error(partition, ldb_ret, &errstr);
308 add_result = &add_reply->msg->r.AddResponse;
309 add_result->dn = NULL;
310 add_result->resultcode = result;
311 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
312 add_result->referral = NULL;
314 talloc_free(local_ctx);
316 ldapsrv_queue_reply(call, add_reply);
320 static NTSTATUS sldb_Del(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
321 struct ldap_DelRequest *r)
325 struct ldap_Result *del_result;
326 struct ldapsrv_reply *del_reply;
328 struct ldb_context *samdb;
329 const char *errstr = NULL;
330 int result = LDAP_SUCCESS;
332 local_ctx = talloc_named(call, 0, "sldb_Del local memory context");
333 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
335 samdb = talloc_get_type(partition->private, struct ldb_context);
337 dn = ldb_dn_explode(local_ctx, r->dn);
338 VALID_DN_SYNTAX(dn,1);
340 DEBUG(10, ("sldb_Del: dn: [%s]\n", r->dn));
343 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
344 NT_STATUS_HAVE_NO_MEMORY(del_reply);
346 if (result == LDAP_SUCCESS) {
347 ldb_ret = ldb_delete(samdb, dn);
348 result = sldb_map_error(partition, ldb_ret, &errstr);
351 del_result = &del_reply->msg->r.DelResponse;
352 del_result->dn = NULL;
353 del_result->resultcode = result;
354 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
355 del_result->referral = NULL;
357 talloc_free(local_ctx);
359 ldapsrv_queue_reply(call, del_reply);
363 static NTSTATUS sldb_Modify(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
364 struct ldap_ModifyRequest *r)
368 struct ldap_Result *modify_result;
369 struct ldapsrv_reply *modify_reply;
371 struct ldb_context *samdb;
372 struct ldb_message *msg = NULL;
373 int result = LDAP_SUCCESS;
374 const char *errstr = NULL;
377 local_ctx = talloc_named(call, 0, "sldb_Modify local memory context");
378 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
380 samdb = talloc_get_type(partition->private, struct ldb_context);
382 dn = ldb_dn_explode(local_ctx, r->dn);
383 VALID_DN_SYNTAX(dn, 1);
385 DEBUG(10, ("sldb_modify: dn: [%s]\n", r->dn));
387 msg = talloc(local_ctx, struct ldb_message);
388 NT_STATUS_HAVE_NO_MEMORY(msg);
391 msg->private_data = NULL;
392 msg->num_elements = 0;
393 msg->elements = NULL;
395 if (r->num_mods > 0) {
396 msg->num_elements = r->num_mods;
397 msg->elements = talloc_array(msg, struct ldb_message_element, r->num_mods);
398 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
400 for (i=0; i < msg->num_elements; i++) {
401 msg->elements[i].name = discard_const_p(char, r->mods[i].attrib.name);
402 msg->elements[i].num_values = 0;
403 msg->elements[i].values = NULL;
405 switch (r->mods[i].type) {
407 result = LDAP_PROTOCOL_ERROR;
408 errstr = "Invalid LDAP_MODIFY_* type";
410 case LDAP_MODIFY_ADD:
411 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
413 case LDAP_MODIFY_DELETE:
414 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
416 case LDAP_MODIFY_REPLACE:
417 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
421 msg->elements[i].num_values = r->mods[i].attrib.num_values;
422 if (msg->elements[i].num_values > 0) {
423 msg->elements[i].values = talloc_array(msg, struct ldb_val, msg->elements[i].num_values);
424 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
426 for (j=0; j < msg->elements[i].num_values; j++) {
427 if (!(r->mods[i].attrib.values[j].length > 0)) {
429 errstr = "Empty attribute values are not allowed";
432 msg->elements[i].values[j].length = r->mods[i].attrib.values[j].length;
433 msg->elements[i].values[j].data = r->mods[i].attrib.values[j].data;
439 errstr = "No mods are not allowed";
444 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
445 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
447 if (result == LDAP_SUCCESS) {
448 ldb_ret = ldb_modify(samdb, msg);
449 result = sldb_map_error(partition, ldb_ret, &errstr);
452 modify_result = &modify_reply->msg->r.AddResponse;
453 modify_result->dn = NULL;
454 modify_result->resultcode = result;
455 modify_result->errormessage = (errstr?talloc_strdup(modify_reply,errstr):NULL);
456 modify_result->referral = NULL;
458 talloc_free(local_ctx);
460 ldapsrv_queue_reply(call, modify_reply);
464 static NTSTATUS sldb_Compare(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
465 struct ldap_CompareRequest *r)
469 struct ldap_Result *compare;
470 struct ldapsrv_reply *compare_r;
471 int result = LDAP_SUCCESS;
472 struct ldb_context *samdb;
473 struct ldb_result *res = NULL;
474 const char *attrs[1];
475 const char *errstr = NULL;
476 const char *filter = NULL;
479 local_ctx = talloc_named(call, 0, "sldb_Compare local_memory_context");
480 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
482 samdb = talloc_get_type(partition->private, struct ldb_context);
484 dn = ldb_dn_explode(local_ctx, r->dn);
485 VALID_DN_SYNTAX(dn, 1);
487 DEBUG(10, ("sldb_Compare: dn: [%s]\n", r->dn));
488 filter = talloc_asprintf(local_ctx, "(%s=%*s)", r->attribute,
489 (int)r->value.length, r->value.data);
490 NT_STATUS_HAVE_NO_MEMORY(filter);
492 DEBUGADD(10, ("sldb_Compare: attribute: [%s]\n", filter));
497 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
498 NT_STATUS_HAVE_NO_MEMORY(compare_r);
500 if (result == LDAP_SUCCESS) {
501 ret = ldb_search(samdb, dn, LDB_SCOPE_BASE, filter, attrs, &res);
502 talloc_steal(samdb, res);
503 if (ret != LDB_SUCCESS) {
505 errstr = ldb_errstring(samdb);
506 DEBUG(10,("sldb_Compare: error: %s\n", errstr));
507 } else if (res->count == 0) {
508 DEBUG(10,("sldb_Compare: doesn't matched\n"));
509 result = LDAP_COMPARE_FALSE;
511 } else if (res->count == 1) {
512 DEBUG(10,("sldb_Compare: matched\n"));
513 result = LDAP_COMPARE_TRUE;
515 } else if (res->count > 1) {
517 errstr = "too many objects match";
518 DEBUG(10,("sldb_Compare: %d results: %s\n", res->count, errstr));
522 compare = &compare_r->msg->r.CompareResponse;
524 compare->resultcode = result;
525 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
526 compare->referral = NULL;
528 talloc_free(local_ctx);
530 ldapsrv_queue_reply(call, compare_r);
534 static NTSTATUS sldb_ModifyDN(struct ldapsrv_partition *partition, struct ldapsrv_call *call, struct ldap_ModifyDNRequest *r)
537 struct ldb_dn *olddn, *newdn, *newrdn;
538 struct ldb_dn *parentdn = NULL;
539 struct ldap_Result *modifydn;
540 struct ldapsrv_reply *modifydn_r;
542 struct ldb_context *samdb;
543 const char *errstr = NULL;
544 int result = LDAP_SUCCESS;
546 local_ctx = talloc_named(call, 0, "sldb_ModifyDN local memory context");
547 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
549 samdb = talloc_get_type(partition->private, struct ldb_context);
551 olddn = ldb_dn_explode(local_ctx, r->dn);
552 VALID_DN_SYNTAX(olddn, 2);
554 newrdn = ldb_dn_explode(local_ctx, r->newrdn);
555 VALID_DN_SYNTAX(newrdn, 1);
557 DEBUG(10, ("sldb_ModifyDN: olddn: [%s]\n", r->dn));
558 DEBUG(10, ("sldb_ModifyDN: newrdn: [%s]\n", r->newrdn));
560 /* we can't handle the rename if we should not remove the old dn */
561 if (!r->deleteolddn) {
562 result = LDAP_UNWILLING_TO_PERFORM;
563 errstr = "Old RDN must be deleted";
567 if (newrdn->comp_num > 1) {
568 result = LDAP_NAMING_VIOLATION;
569 errstr = "Error new RDN invalid";
573 if (r->newsuperior) {
574 parentdn = ldb_dn_explode(local_ctx, r->newsuperior);
575 VALID_DN_SYNTAX(parentdn, 0);
576 DEBUG(10, ("sldb_ModifyDN: newsuperior: [%s]\n", r->newsuperior));
578 if (parentdn->comp_num < 1) {
579 result = LDAP_AFFECTS_MULTIPLE_DSAS;
580 errstr = "Error new Superior DN invalid";
586 parentdn = ldb_dn_get_parent(local_ctx, olddn);
587 NT_STATUS_HAVE_NO_MEMORY(parentdn);
590 newdn = ldb_dn_make_child(local_ctx, ldb_dn_get_rdn(local_ctx, newrdn), parentdn);
591 NT_STATUS_HAVE_NO_MEMORY(newdn);
594 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
595 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
597 if (result == LDAP_SUCCESS) {
598 ldb_ret = ldb_rename(samdb, olddn, newdn);
599 result = sldb_map_error(partition, ldb_ret, &errstr);
602 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
604 modifydn->resultcode = result;
605 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
606 modifydn->referral = NULL;
608 talloc_free(local_ctx);
610 ldapsrv_queue_reply(call, modifydn_r);
614 static const struct ldapsrv_partition_ops sldb_ops = {
617 .Search = sldb_Search,
620 .Modify = sldb_Modify,
621 .Compare = sldb_Compare,
622 .ModifyDN = sldb_ModifyDN
625 const struct ldapsrv_partition_ops *ldapsrv_get_sldb_partition_ops(void)