2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Gerald Carter 2003
9 Copyright (C) Simo Sorce 2003-2007
10 Copyright (C) Michael Adam 2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "../libcli/security/security.h"
34 #define DBGC_CLASS DBGC_IDMAP
40 #include "passdb/pdb_ldap_schema.h"
42 struct idmap_ldap_context {
43 struct smbldap_state *smbldap_state;
48 struct idmap_rw_ops *rw_ops;
51 #define CHECK_ALLOC_DONE(mem) do { \
53 DEBUG(0, ("Out of memory!\n")); \
54 ret = NT_STATUS_NO_MEMORY; \
58 /**********************************************************************
59 IDMAP ALLOC TDB BACKEND
60 **********************************************************************/
62 /*********************************************************************
63 ********************************************************************/
65 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
66 struct smbldap_state *ldap_state,
67 struct idmap_domain *dom,
70 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
72 const char *tmp = NULL;
76 /* assume anonymous if we don't have a specified user */
78 tmp = idmap_config_const_string(dom->name, "ldap_user_dn", NULL);
81 secret = idmap_fetch_secret("ldap", dom->name, tmp);
83 DEBUG(0, ("get_credentials: Unable to fetch "
84 "auth credentials for %s in %s\n",
85 tmp, (dom==NULL)?"ALLOC":dom->name));
86 ret = NT_STATUS_ACCESS_DENIED;
89 *dn = talloc_strdup(mem_ctx, tmp);
90 CHECK_ALLOC_DONE(*dn);
92 if (!fetch_ldap_pw(&user_dn, &secret)) {
93 DEBUG(2, ("get_credentials: Failed to lookup ldap "
94 "bind creds. Using anonymous connection.\n"));
98 *dn = talloc_strdup(mem_ctx, user_dn);
100 CHECK_ALLOC_DONE(*dn);
104 smbldap_set_creds(ldap_state, anon, *dn, secret);
114 /**********************************************************************
115 Verify the sambaUnixIdPool entry in the directory.
116 **********************************************************************/
118 static NTSTATUS verify_idpool(struct idmap_domain *dom)
122 LDAPMessage *result = NULL;
123 LDAPMod **mods = NULL;
124 const char **attr_list;
128 struct idmap_ldap_context *ctx;
130 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
132 mem_ctx = talloc_new(ctx);
133 if (mem_ctx == NULL) {
134 DEBUG(0, ("Out of memory!\n"));
135 return NT_STATUS_NO_MEMORY;
138 filter = talloc_asprintf(mem_ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
139 CHECK_ALLOC_DONE(filter);
141 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
142 CHECK_ALLOC_DONE(attr_list);
144 rc = smbldap_search(ctx->smbldap_state,
152 if (rc != LDAP_SUCCESS) {
153 DEBUG(1, ("Unable to verify the idpool, "
154 "cannot continue initialization!\n"));
155 return NT_STATUS_UNSUCCESSFUL;
158 count = ldap_count_entries(smbldap_get_ldap(ctx->smbldap_state),
161 ldap_msgfree(result);
164 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
165 filter, ctx->suffix));
166 ret = NT_STATUS_UNSUCCESSFUL;
169 else if (count == 0) {
170 char *uid_str, *gid_str;
172 uid_str = talloc_asprintf(mem_ctx, "%lu",
173 (unsigned long)dom->low_id);
174 gid_str = talloc_asprintf(mem_ctx, "%lu",
175 (unsigned long)dom->low_id);
177 smbldap_set_mod(&mods, LDAP_MOD_ADD,
178 "objectClass", LDAP_OBJ_IDPOOL);
179 smbldap_set_mod(&mods, LDAP_MOD_ADD,
180 get_attr_key2string(idpool_attr_list,
181 LDAP_ATTR_UIDNUMBER),
183 smbldap_set_mod(&mods, LDAP_MOD_ADD,
184 get_attr_key2string(idpool_attr_list,
185 LDAP_ATTR_GIDNUMBER),
188 rc = smbldap_modify(ctx->smbldap_state,
191 ldap_mods_free(mods, True);
193 ret = NT_STATUS_UNSUCCESSFUL;
198 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
200 talloc_free(mem_ctx);
204 /********************************
205 Allocate a new uid or gid
206 ********************************/
208 static NTSTATUS idmap_ldap_allocate_id_internal(struct idmap_domain *dom,
212 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
213 int rc = LDAP_SERVER_DOWN;
215 LDAPMessage *result = NULL;
216 LDAPMessage *entry = NULL;
217 LDAPMod **mods = NULL;
221 const char *dn = NULL;
222 const char **attr_list;
224 struct idmap_ldap_context *ctx;
226 /* Only do query if we are online */
227 if (idmap_is_offline()) {
228 return NT_STATUS_FILE_IS_OFFLINE;
231 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
233 mem_ctx = talloc_new(ctx);
235 DEBUG(0, ("Out of memory!\n"));
236 return NT_STATUS_NO_MEMORY;
243 type = get_attr_key2string(idpool_attr_list,
244 LDAP_ATTR_UIDNUMBER);
248 type = get_attr_key2string(idpool_attr_list,
249 LDAP_ATTR_GIDNUMBER);
253 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
254 return NT_STATUS_INVALID_PARAMETER;
257 filter = talloc_asprintf(mem_ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
258 CHECK_ALLOC_DONE(filter);
260 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
261 CHECK_ALLOC_DONE(attr_list);
263 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
265 rc = smbldap_search(ctx->smbldap_state,
267 LDAP_SCOPE_SUBTREE, filter,
268 attr_list, 0, &result);
270 if (rc != LDAP_SUCCESS) {
271 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
275 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
277 count = ldap_count_entries(smbldap_get_ldap(ctx->smbldap_state),
280 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
284 entry = ldap_first_entry(smbldap_get_ldap(ctx->smbldap_state), result);
286 dn = smbldap_talloc_dn(mem_ctx,
287 smbldap_get_ldap(ctx->smbldap_state),
293 id_str = smbldap_talloc_single_attribute(
294 smbldap_get_ldap(ctx->smbldap_state),
295 entry, type, mem_ctx);
296 if (id_str == NULL) {
297 DEBUG(0,("%s attribute not found\n", type));
298 ret = NT_STATUS_UNSUCCESSFUL;
302 xid->id = strtoul(id_str, NULL, 10);
304 /* make sure we still have room to grow */
308 if (xid->id > dom->high_id) {
309 DEBUG(0,("Cannot allocate uid above %lu!\n",
310 (unsigned long)dom->high_id));
316 if (xid->id > dom->high_id) {
317 DEBUG(0,("Cannot allocate gid above %lu!\n",
318 (unsigned long)dom->high_id));
328 new_id_str = talloc_asprintf(mem_ctx, "%lu", (unsigned long)xid->id + 1);
330 DEBUG(0,("Out of memory\n"));
331 ret = NT_STATUS_NO_MEMORY;
335 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
336 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
339 DEBUG(0,("smbldap_set_mod() failed.\n"));
343 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
344 id_str, new_id_str));
346 rc = smbldap_modify(ctx->smbldap_state, dn, mods);
348 ldap_mods_free(mods, True);
350 if (rc != LDAP_SUCCESS) {
351 DEBUG(1,("Failed to allocate new %s. "
352 "smbldap_modify() failed.\n", type));
359 talloc_free(mem_ctx);
364 * Allocate a new unix-ID.
365 * For now this is for the default idmap domain only.
366 * Should be extended later on.
368 static NTSTATUS idmap_ldap_allocate_id(struct idmap_domain *dom,
373 if (!strequal(dom->name, "*")) {
374 DEBUG(3, ("idmap_ldap_allocate_id: "
375 "Refusing allocation of a new unixid for domain'%s'. "
376 "This is only supported for the default "
379 return NT_STATUS_NOT_IMPLEMENTED;
382 ret = idmap_ldap_allocate_id_internal(dom, id);
388 /**********************************************************************
389 IDMAP MAPPING LDAP BACKEND
390 **********************************************************************/
392 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
394 smbldap_free_struct(&ctx->smbldap_state);
395 DEBUG(5,("The connection to the LDAP server was closed\n"));
396 /* maybe free the results here --metze */
401 /********************************
402 Initialise idmap database.
403 ********************************/
405 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
406 const struct id_map *map);
408 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom)
411 struct idmap_ldap_context *ctx = NULL;
412 const char *tmp = NULL;
414 /* Only do init if we are online */
415 if (idmap_is_offline()) {
416 return NT_STATUS_FILE_IS_OFFLINE;
419 ctx = talloc_zero(dom, struct idmap_ldap_context);
421 DEBUG(0, ("Out of memory!\n"));
422 return NT_STATUS_NO_MEMORY;
425 tmp = idmap_config_const_string(dom->name, "ldap_url", NULL);
428 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
429 ret = NT_STATUS_UNSUCCESSFUL;
433 ctx->url = talloc_strdup(ctx, tmp);
435 trim_char(ctx->url, '\"', '\"');
437 tmp = idmap_config_const_string(dom->name, "ldap_base_dn", NULL);
438 if ( ! tmp || ! *tmp) {
439 tmp = lp_ldap_idmap_suffix(talloc_tos());
441 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
442 ret = NT_STATUS_UNSUCCESSFUL;
447 ctx->suffix = talloc_strdup(ctx, tmp);
448 CHECK_ALLOC_DONE(ctx->suffix);
450 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
451 CHECK_ALLOC_DONE(ctx->rw_ops);
453 ctx->rw_ops->get_new_id = idmap_ldap_allocate_id_internal;
454 ctx->rw_ops->set_mapping = idmap_ldap_set_mapping;
456 /* get_credentials deals with setting up creds */
458 ret = smbldap_init(ctx, global_event_context(), ctx->url,
459 false, NULL, NULL, &ctx->smbldap_state);
460 if (!NT_STATUS_IS_OK(ret)) {
461 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
465 ret = get_credentials( ctx, ctx->smbldap_state,
466 dom, &ctx->user_dn );
467 if ( !NT_STATUS_IS_OK(ret) ) {
468 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
469 "credentials (%s)\n", nt_errstr(ret)));
474 * Set the destructor on the context, so that resources are
475 * properly freed when the context is released.
477 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
479 dom->private_data = ctx;
481 ret = verify_idpool(dom);
482 if (!NT_STATUS_IS_OK(ret)) {
483 DEBUG(1, ("idmap_ldap_db_init: failed to verify ID pool (%s)\n",
500 /* TODO: change this: This function cannot be called to modify a mapping,
501 * only set a new one */
503 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
504 const struct id_map *map)
508 struct idmap_ldap_context *ctx;
509 LDAPMessage *entry = NULL;
510 LDAPMod **mods = NULL;
513 struct dom_sid_buf sid;
517 /* Only do query if we are online */
518 if (idmap_is_offline()) {
519 return NT_STATUS_FILE_IS_OFFLINE;
522 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
524 switch(map->xid.type) {
526 type = get_attr_key2string(sidmap_attr_list,
527 LDAP_ATTR_UIDNUMBER);
531 type = get_attr_key2string(sidmap_attr_list,
532 LDAP_ATTR_GIDNUMBER);
536 return NT_STATUS_INVALID_PARAMETER;
539 memctx = talloc_new(ctx);
541 DEBUG(0, ("Out of memory!\n"));
542 return NT_STATUS_NO_MEMORY;
545 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
546 CHECK_ALLOC_DONE(id_str);
548 dn = talloc_asprintf(memctx, "%s=%s,%s",
549 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
550 dom_sid_str_buf(map->sid, &sid),
552 CHECK_ALLOC_DONE(dn);
554 smbldap_set_mod(&mods, LDAP_MOD_ADD,
555 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
557 smbldap_make_mod(smbldap_get_ldap(ctx->smbldap_state),
558 entry, &mods, type, id_str);
560 smbldap_make_mod(smbldap_get_ldap(ctx->smbldap_state), entry, &mods,
561 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
565 DEBUG(2, ("ERROR: No mods?\n"));
566 ret = NT_STATUS_UNSUCCESSFUL;
570 /* TODO: remove conflicting mappings! */
572 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
574 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid.buf, id_str));
576 rc = smbldap_add(ctx->smbldap_state, dn, mods);
577 ldap_mods_free(mods, True);
579 if (rc != LDAP_SUCCESS) {
580 char *ld_error = NULL;
581 ldap_get_option(smbldap_get_ldap(ctx->smbldap_state),
582 LDAP_OPT_ERROR_STRING, &ld_error);
583 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
584 "mapping [%s]\n", sid.buf,
585 (unsigned long)map->xid.id, type));
586 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
587 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
589 ldap_memfree(ld_error);
591 ret = NT_STATUS_UNSUCCESSFUL;
595 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
596 "%lu [%s]\n", sid.buf, (unsigned long)map->xid.id, type));
606 * Create a new mapping for an unmapped SID, also allocating a new ID.
607 * If possible, this should be run inside a transaction to make the
610 static NTSTATUS idmap_ldap_new_mapping(struct idmap_domain *dom, struct id_map *map)
613 struct idmap_ldap_context *ctx;
615 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
617 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
622 /**********************************
623 lookup a set of unix ids.
624 **********************************/
626 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
631 struct idmap_ldap_context *ctx;
632 LDAPMessage *result = NULL;
633 LDAPMessage *entry = NULL;
634 const char *uidNumber;
635 const char *gidNumber;
636 const char **attr_list;
645 /* Only do query if we are online */
646 if (idmap_is_offline()) {
647 return NT_STATUS_FILE_IS_OFFLINE;
650 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
652 memctx = talloc_new(ctx);
654 DEBUG(0, ("Out of memory!\n"));
655 return NT_STATUS_NO_MEMORY;
658 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
659 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
661 attr_list = get_attr_list(memctx, sidmap_attr_list);
664 /* if we are requested just one mapping use the simple filter */
666 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
667 LDAP_OBJ_IDMAP_ENTRY,
668 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
669 (unsigned long)ids[0]->xid.id);
670 CHECK_ALLOC_DONE(filter);
671 DEBUG(10, ("Filter: [%s]\n", filter));
673 /* multiple mappings */
677 for (i = 0; ids[i]; i++) {
678 ids[i]->status = ID_UNKNOWN;
685 filter = talloc_asprintf(memctx,
686 "(&(objectClass=%s)(|",
687 LDAP_OBJ_IDMAP_ENTRY);
688 CHECK_ALLOC_DONE(filter);
691 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
692 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
693 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
694 (unsigned long)ids[idx]->xid.id);
695 CHECK_ALLOC_DONE(filter);
697 filter = talloc_asprintf_append_buffer(filter, "))");
698 CHECK_ALLOC_DONE(filter);
699 DEBUG(10, ("Filter: [%s]\n", filter));
705 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
706 filter, attr_list, 0, &result);
708 if (rc != LDAP_SUCCESS) {
709 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
710 ret = NT_STATUS_UNSUCCESSFUL;
714 count = ldap_count_entries(smbldap_get_ldap(ctx->smbldap_state),
718 DEBUG(10, ("NO SIDs found\n"));
721 for (i = 0; i < count; i++) {
728 if (i == 0) { /* first entry */
729 entry = ldap_first_entry(
730 smbldap_get_ldap(ctx->smbldap_state), result);
731 } else { /* following ones */
732 entry = ldap_next_entry(
733 smbldap_get_ldap(ctx->smbldap_state), entry);
736 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
741 /* first check if the SID is present */
742 sidstr = smbldap_talloc_single_attribute(
743 smbldap_get_ldap(ctx->smbldap_state),
744 entry, LDAP_ATTRIBUTE_SID, memctx);
745 if ( ! sidstr) { /* no sid, skip entry */
746 DEBUG(2, ("WARNING SID not found on entry\n"));
750 /* now try to see if it is a uid, if not try with a gid
751 * (gid is more common, but in case both uidNumber and
752 * gidNumber are returned the SID is mapped to the uid
755 tmp = smbldap_talloc_single_attribute(
756 smbldap_get_ldap(ctx->smbldap_state),
757 entry, uidNumber, memctx);
760 tmp = smbldap_talloc_single_attribute(
761 smbldap_get_ldap(ctx->smbldap_state),
762 entry, gidNumber, memctx);
764 if ( ! tmp) { /* wow very strange entry, how did it match ? */
765 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
766 "nor gidNumber returned\n", sidstr));
771 id = strtoul(tmp, NULL, 10);
772 if (!idmap_unix_id_is_in_range(id, dom)) {
773 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
775 dom->low_id, dom->high_id));
782 map = idmap_find_map_by_id(&ids[bidx], type, id);
784 DEBUG(2, ("WARNING: couldn't match sid (%s) "
785 "with requested ids\n", sidstr));
790 if ( ! string_to_sid(map->sid, sidstr)) {
791 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
796 if (map->status == ID_MAPPED) {
797 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
798 "overwriting mapping %u -> %s with %u -> %s\n",
799 (type == ID_TYPE_UID) ? "UID" : "GID",
800 id, sid_string_dbg(map->sid), id, sidstr));
806 map->status = ID_MAPPED;
808 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
809 (unsigned long)map->xid.id, map->xid.type));
812 /* free the ldap results */
814 ldap_msgfree(result);
818 if (multi && ids[idx]) { /* still some values to map */
824 /* mark all unknwon/expired ones as unmapped */
825 for (i = 0; ids[i]; i++) {
826 if (ids[i]->status != ID_MAPPED)
827 ids[i]->status = ID_UNMAPPED;
835 /**********************************
836 lookup a set of sids.
837 **********************************/
839 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
842 LDAPMessage *entry = NULL;
845 struct idmap_ldap_context *ctx;
846 LDAPMessage *result = NULL;
847 const char *uidNumber;
848 const char *gidNumber;
849 const char **attr_list;
858 /* Only do query if we are online */
859 if (idmap_is_offline()) {
860 return NT_STATUS_FILE_IS_OFFLINE;
863 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
865 memctx = talloc_new(ctx);
867 DEBUG(0, ("Out of memory!\n"));
868 return NT_STATUS_NO_MEMORY;
871 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
872 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
874 attr_list = get_attr_list(memctx, sidmap_attr_list);
877 struct dom_sid_buf buf;
878 /* if we are requested just one mapping use the simple filter */
880 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
881 LDAP_OBJ_IDMAP_ENTRY,
883 dom_sid_str_buf(ids[0]->sid, &buf));
884 CHECK_ALLOC_DONE(filter);
885 DEBUG(10, ("Filter: [%s]\n", filter));
887 /* multiple mappings */
891 for (i = 0; ids[i]; i++) {
892 ids[i]->status = ID_UNKNOWN;
899 filter = talloc_asprintf(memctx,
900 "(&(objectClass=%s)(|",
901 LDAP_OBJ_IDMAP_ENTRY);
902 CHECK_ALLOC_DONE(filter);
905 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
906 struct dom_sid_buf buf;
907 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
909 dom_sid_str_buf(ids[idx]->sid, &buf));
910 CHECK_ALLOC_DONE(filter);
912 filter = talloc_asprintf_append_buffer(filter, "))");
913 CHECK_ALLOC_DONE(filter);
914 DEBUG(10, ("Filter: [%s]", filter));
920 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
921 filter, attr_list, 0, &result);
923 if (rc != LDAP_SUCCESS) {
924 DEBUG(3,("Failure looking up sids (%s)\n",
925 ldap_err2string(rc)));
926 ret = NT_STATUS_UNSUCCESSFUL;
930 count = ldap_count_entries(smbldap_get_ldap(ctx->smbldap_state),
934 DEBUG(10, ("NO SIDs found\n"));
937 for (i = 0; i < count; i++) {
945 if (i == 0) { /* first entry */
946 entry = ldap_first_entry(
947 smbldap_get_ldap(ctx->smbldap_state), result);
948 } else { /* following ones */
949 entry = ldap_next_entry(
950 smbldap_get_ldap(ctx->smbldap_state), entry);
953 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
958 /* first check if the SID is present */
959 sidstr = smbldap_talloc_single_attribute(
960 smbldap_get_ldap(ctx->smbldap_state),
961 entry, LDAP_ATTRIBUTE_SID, memctx);
962 if ( ! sidstr) { /* no sid ??, skip entry */
963 DEBUG(2, ("WARNING SID not found on entry\n"));
967 if ( ! string_to_sid(&sid, sidstr)) {
968 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
973 map = idmap_find_map_by_sid(&ids[bidx], &sid);
975 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
981 /* now try to see if it is a uid, if not try with a gid
982 * (gid is more common, but in case both uidNumber and
983 * gidNumber are returned the SID is mapped to the uid
986 tmp = smbldap_talloc_single_attribute(
987 smbldap_get_ldap(ctx->smbldap_state),
988 entry, uidNumber, memctx);
991 tmp = smbldap_talloc_single_attribute(
992 smbldap_get_ldap(ctx->smbldap_state),
993 entry, gidNumber, memctx);
995 if ( ! tmp) { /* no ids ?? */
996 DEBUG(5, ("no uidNumber, "
997 "nor gidNumber attributes found\n"));
1002 id = strtoul(tmp, NULL, 10);
1003 if (!idmap_unix_id_is_in_range(id, dom)) {
1004 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1006 dom->low_id, dom->high_id));
1007 TALLOC_FREE(sidstr);
1013 if (map->status == ID_MAPPED) {
1014 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1015 "overwriting mapping %s -> %u with %s -> %u\n",
1016 (type == ID_TYPE_UID) ? "UID" : "GID",
1017 sidstr, map->xid.id, sidstr, id));
1020 TALLOC_FREE(sidstr);
1023 map->xid.type = type;
1025 map->status = ID_MAPPED;
1027 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1028 (unsigned long)map->xid.id, map->xid.type));
1031 /* free the ldap results */
1033 ldap_msgfree(result);
1037 if (multi && ids[idx]) { /* still some values to map */
1042 * try to create new mappings for unmapped sids
1044 for (i = 0; ids[i]; i++) {
1045 if (ids[i]->status != ID_MAPPED) {
1046 ids[i]->status = ID_UNMAPPED;
1047 if (ids[i]->sid != NULL) {
1048 ret = idmap_ldap_new_mapping(dom, ids[i]);
1049 if (!NT_STATUS_IS_OK(ret)) {
1059 talloc_free(memctx);
1063 /**********************************
1064 Close the idmap ldap instance
1065 **********************************/
1067 static struct idmap_methods idmap_ldap_methods = {
1069 .init = idmap_ldap_db_init,
1070 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1071 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1072 .allocate_id = idmap_ldap_allocate_id,
1075 NTSTATUS idmap_ldap_init(TALLOC_CTX *);
1076 NTSTATUS idmap_ldap_init(TALLOC_CTX *ctx)
1078 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1079 &idmap_ldap_methods);