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
41 static char *idmap_fetch_secret(const char *backend, bool alloc,
42 const char *domain, const char *identity)
48 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
50 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
56 strupper_m(tmp); /* make sure the key is case insensitive */
57 ret = secrets_fetch_generic(tmp, identity);
64 struct idmap_ldap_alloc_context {
65 struct smbldap_state *smbldap_state;
71 struct idmap_ldap_context {
72 struct smbldap_state *smbldap_state;
77 struct idmap_ldap_alloc_context *alloc;
78 struct idmap_rw_ops *rw_ops;
81 #define CHECK_ALLOC_DONE(mem) do { \
83 DEBUG(0, ("Out of memory!\n")); \
84 ret = NT_STATUS_NO_MEMORY; \
88 /**********************************************************************
89 IDMAP ALLOC TDB BACKEND
90 **********************************************************************/
92 /*********************************************************************
93 ********************************************************************/
95 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
96 struct smbldap_state *ldap_state,
97 const char *config_option,
98 struct idmap_domain *dom,
101 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
103 const char *tmp = NULL;
104 char *user_dn = NULL;
107 /* assume anonymous if we don't have a specified user */
109 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
113 /* only the alloc backend can pass in a NULL dom */
114 secret = idmap_fetch_secret("ldap", True,
117 secret = idmap_fetch_secret("ldap", False,
122 DEBUG(0, ("get_credentials: Unable to fetch "
123 "auth credentials for %s in %s\n",
124 tmp, (dom==NULL)?"ALLOC":dom->name));
125 ret = NT_STATUS_ACCESS_DENIED;
128 *dn = talloc_strdup(mem_ctx, tmp);
129 CHECK_ALLOC_DONE(*dn);
131 if (!fetch_ldap_pw(&user_dn, &secret)) {
132 DEBUG(2, ("get_credentials: Failed to lookup ldap "
133 "bind creds. Using anonymous connection.\n"));
137 *dn = talloc_strdup(mem_ctx, user_dn);
138 SAFE_FREE( user_dn );
139 CHECK_ALLOC_DONE(*dn);
143 smbldap_set_creds(ldap_state, anon, *dn, secret);
153 /**********************************************************************
154 Verify the sambaUnixIdPool entry in the directory.
155 **********************************************************************/
157 static NTSTATUS verify_idpool(struct idmap_domain *dom)
161 LDAPMessage *result = NULL;
162 LDAPMod **mods = NULL;
163 const char **attr_list;
167 struct idmap_ldap_context *ctx;
169 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
171 mem_ctx = talloc_new(ctx);
172 if (mem_ctx == NULL) {
173 DEBUG(0, ("Out of memory!\n"));
174 return NT_STATUS_NO_MEMORY;
177 filter = talloc_asprintf(mem_ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
178 CHECK_ALLOC_DONE(filter);
180 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
181 CHECK_ALLOC_DONE(attr_list);
183 rc = smbldap_search(ctx->smbldap_state,
191 if (rc != LDAP_SUCCESS) {
192 DEBUG(1, ("Unable to verify the idpool, "
193 "cannot continue initialization!\n"));
194 return NT_STATUS_UNSUCCESSFUL;
197 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
199 ldap_msgfree(result);
202 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
203 filter, ctx->suffix));
204 ret = NT_STATUS_UNSUCCESSFUL;
207 else if (count == 0) {
208 char *uid_str, *gid_str;
210 uid_str = talloc_asprintf(mem_ctx, "%lu",
211 (unsigned long)dom->low_id);
212 gid_str = talloc_asprintf(mem_ctx, "%lu",
213 (unsigned long)dom->low_id);
215 smbldap_set_mod(&mods, LDAP_MOD_ADD,
216 "objectClass", LDAP_OBJ_IDPOOL);
217 smbldap_set_mod(&mods, LDAP_MOD_ADD,
218 get_attr_key2string(idpool_attr_list,
219 LDAP_ATTR_UIDNUMBER),
221 smbldap_set_mod(&mods, LDAP_MOD_ADD,
222 get_attr_key2string(idpool_attr_list,
223 LDAP_ATTR_GIDNUMBER),
226 rc = smbldap_modify(ctx->smbldap_state,
229 ldap_mods_free(mods, True);
231 ret = NT_STATUS_UNSUCCESSFUL;
236 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
238 talloc_free(mem_ctx);
242 /********************************
243 Allocate a new uid or gid
244 ********************************/
246 static NTSTATUS idmap_ldap_allocate_id(struct idmap_domain *dom,
250 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
251 int rc = LDAP_SERVER_DOWN;
253 LDAPMessage *result = NULL;
254 LDAPMessage *entry = NULL;
255 LDAPMod **mods = NULL;
259 const char *dn = NULL;
260 const char **attr_list;
262 struct idmap_ldap_context *ctx;
264 /* Only do query if we are online */
265 if (idmap_is_offline()) {
266 return NT_STATUS_FILE_IS_OFFLINE;
269 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
272 return NT_STATUS_UNSUCCESSFUL;
275 mem_ctx = talloc_new(ctx->alloc);
277 DEBUG(0, ("Out of memory!\n"));
278 return NT_STATUS_NO_MEMORY;
285 type = get_attr_key2string(idpool_attr_list,
286 LDAP_ATTR_UIDNUMBER);
290 type = get_attr_key2string(idpool_attr_list,
291 LDAP_ATTR_GIDNUMBER);
295 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
296 return NT_STATUS_INVALID_PARAMETER;
299 filter = talloc_asprintf(mem_ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
300 CHECK_ALLOC_DONE(filter);
302 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
303 CHECK_ALLOC_DONE(attr_list);
305 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
307 rc = smbldap_search(ctx->alloc->smbldap_state,
309 LDAP_SCOPE_SUBTREE, filter,
310 attr_list, 0, &result);
312 if (rc != LDAP_SUCCESS) {
313 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
317 talloc_autofree_ldapmsg(mem_ctx, result);
319 count = ldap_count_entries(ctx->alloc->smbldap_state->ldap_struct,
322 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
326 entry = ldap_first_entry(ctx->alloc->smbldap_state->ldap_struct,
329 dn = smbldap_talloc_dn(mem_ctx,
330 ctx->alloc->smbldap_state->ldap_struct,
336 id_str = smbldap_talloc_single_attribute(
337 ctx->alloc->smbldap_state->ldap_struct,
338 entry, type, mem_ctx);
339 if (id_str == NULL) {
340 DEBUG(0,("%s attribute not found\n", type));
341 ret = NT_STATUS_UNSUCCESSFUL;
345 xid->id = strtoul(id_str, NULL, 10);
347 /* make sure we still have room to grow */
351 if (xid->id > dom->high_id) {
352 DEBUG(0,("Cannot allocate uid above %lu!\n",
353 (unsigned long)dom->high_id));
359 if (xid->id > dom->high_id) {
360 DEBUG(0,("Cannot allocate gid above %lu!\n",
361 (unsigned long)dom->high_id));
371 new_id_str = talloc_asprintf(mem_ctx, "%lu", (unsigned long)xid->id + 1);
373 DEBUG(0,("Out of memory\n"));
374 ret = NT_STATUS_NO_MEMORY;
378 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
379 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
382 DEBUG(0,("smbldap_set_mod() failed.\n"));
386 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
387 id_str, new_id_str));
389 rc = smbldap_modify(ctx->alloc->smbldap_state, dn, mods);
391 ldap_mods_free(mods, True);
393 if (rc != LDAP_SUCCESS) {
394 DEBUG(1,("Failed to allocate new %s. "
395 "smbldap_modify() failed.\n", type));
402 talloc_free(mem_ctx);
407 * Allocate a new unix-ID.
408 * For now this is for the default idmap domain only.
409 * Should be extended later on.
411 static NTSTATUS idmap_ldap_get_new_id(struct idmap_domain *dom,
416 if (!strequal(dom->name, "*")) {
417 DEBUG(3, ("idmap_ldap_get_new_id: "
418 "Refusing allocation of a new unixid for domain'%s'. "
419 "Currently only supported for the default "
422 return NT_STATUS_NOT_IMPLEMENTED;
425 ret = idmap_ldap_allocate_id(dom, id);
431 /**********************************************************************
432 IDMAP MAPPING LDAP BACKEND
433 **********************************************************************/
435 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
437 smbldap_free_struct(&ctx->smbldap_state);
438 DEBUG(5,("The connection to the LDAP server was closed\n"));
439 /* maybe free the results here --metze */
444 /********************************
445 Initialise idmap database.
446 ********************************/
448 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
449 const struct id_map *map);
451 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
455 struct idmap_ldap_context *ctx = NULL;
456 char *config_option = NULL;
457 const char *tmp = NULL;
459 /* Only do init if we are online */
460 if (idmap_is_offline()) {
461 return NT_STATUS_FILE_IS_OFFLINE;
464 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
466 DEBUG(0, ("Out of memory!\n"));
467 return NT_STATUS_NO_MEMORY;
470 if (strequal(dom->name, "*")) {
471 /* more specific configuration can go here */
473 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
474 if ( ! config_option) {
475 DEBUG(0, ("Out of memory!\n"));
476 ret = NT_STATUS_NO_MEMORY;
481 if (params != NULL) {
482 /* assume location is the only parameter */
483 ctx->url = talloc_strdup(ctx, params);
485 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
488 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
489 ret = NT_STATUS_UNSUCCESSFUL;
493 ctx->url = talloc_strdup(ctx, tmp);
495 CHECK_ALLOC_DONE(ctx->url);
497 trim_char(ctx->url, '\"', '\"');
499 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
500 if ( ! tmp || ! *tmp) {
501 tmp = lp_ldap_idmap_suffix();
503 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
504 ret = NT_STATUS_UNSUCCESSFUL;
509 ctx->suffix = talloc_strdup(ctx, tmp);
510 CHECK_ALLOC_DONE(ctx->suffix);
512 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
513 CHECK_ALLOC_DONE(ctx->rw_ops);
515 ctx->rw_ops->get_new_id = idmap_ldap_get_new_id;
516 ctx->rw_ops->set_mapping = idmap_ldap_set_mapping;
518 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
519 &ctx->smbldap_state);
520 if (!NT_STATUS_IS_OK(ret)) {
521 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
525 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
526 dom, &ctx->user_dn );
527 if ( !NT_STATUS_IS_OK(ret) ) {
528 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
529 "credentials (%s)\n", nt_errstr(ret)));
533 /* set the destructor on the context, so that resource are properly
534 freed if the contexts is released */
536 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
538 dom->private_data = ctx;
540 ret = verify_idpool(dom);
541 if (!NT_STATUS_IS_OK(ret)) {
542 DEBUG(1, ("idmap_ldap_db_init: failed to verify ID pool (%s)\n",
547 talloc_free(config_option);
560 /* TODO: change this: This function cannot be called to modify a mapping,
561 * only set a new one */
563 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
564 const struct id_map *map)
568 struct idmap_ldap_context *ctx;
569 LDAPMessage *entry = NULL;
570 LDAPMod **mods = NULL;
577 /* Only do query if we are online */
578 if (idmap_is_offline()) {
579 return NT_STATUS_FILE_IS_OFFLINE;
582 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
584 switch(map->xid.type) {
586 type = get_attr_key2string(sidmap_attr_list,
587 LDAP_ATTR_UIDNUMBER);
591 type = get_attr_key2string(sidmap_attr_list,
592 LDAP_ATTR_GIDNUMBER);
596 return NT_STATUS_INVALID_PARAMETER;
599 memctx = talloc_new(ctx);
601 DEBUG(0, ("Out of memory!\n"));
602 return NT_STATUS_NO_MEMORY;
605 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
606 CHECK_ALLOC_DONE(id_str);
608 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
609 CHECK_ALLOC_DONE(sid);
611 dn = talloc_asprintf(memctx, "%s=%s,%s",
612 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
615 CHECK_ALLOC_DONE(dn);
617 smbldap_set_mod(&mods, LDAP_MOD_ADD,
618 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
620 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
621 entry, &mods, type, id_str);
623 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
624 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
628 DEBUG(2, ("ERROR: No mods?\n"));
629 ret = NT_STATUS_UNSUCCESSFUL;
633 /* TODO: remove conflicting mappings! */
635 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
637 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
639 rc = smbldap_add(ctx->smbldap_state, dn, mods);
640 ldap_mods_free(mods, True);
642 if (rc != LDAP_SUCCESS) {
643 char *ld_error = NULL;
644 ldap_get_option(ctx->smbldap_state->ldap_struct,
645 LDAP_OPT_ERROR_STRING, &ld_error);
646 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
647 "mapping [%s]\n", sid,
648 (unsigned long)map->xid.id, type));
649 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
650 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
652 ldap_memfree(ld_error);
654 ret = NT_STATUS_UNSUCCESSFUL;
658 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
659 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
669 * Create a new mapping for an unmapped SID, also allocating a new ID.
670 * If possible, this should be run inside a transaction to make the
673 static NTSTATUS idmap_ldap_new_mapping(struct idmap_domain *dom, struct id_map *map)
676 struct idmap_ldap_context *ctx;
678 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
680 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
686 /* max number of ids requested per batch query */
687 #define IDMAP_LDAP_MAX_IDS 30
689 /**********************************
690 lookup a set of unix ids.
691 **********************************/
693 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
694 * in maps for a match */
695 static struct id_map *find_map_by_id(struct id_map **maps,
701 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
702 if (maps[i] == NULL) { /* end of the run */
705 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
713 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
718 struct idmap_ldap_context *ctx;
719 LDAPMessage *result = NULL;
720 LDAPMessage *entry = NULL;
721 const char *uidNumber;
722 const char *gidNumber;
723 const char **attr_list;
732 /* Only do query if we are online */
733 if (idmap_is_offline()) {
734 return NT_STATUS_FILE_IS_OFFLINE;
737 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
739 memctx = talloc_new(ctx);
741 DEBUG(0, ("Out of memory!\n"));
742 return NT_STATUS_NO_MEMORY;
745 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
746 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
748 attr_list = get_attr_list(memctx, sidmap_attr_list);
751 /* if we are requested just one mapping use the simple filter */
753 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
754 LDAP_OBJ_IDMAP_ENTRY,
755 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
756 (unsigned long)ids[0]->xid.id);
757 CHECK_ALLOC_DONE(filter);
758 DEBUG(10, ("Filter: [%s]\n", filter));
760 /* multiple mappings */
764 for (i = 0; ids[i]; i++) {
765 ids[i]->status = ID_UNKNOWN;
772 filter = talloc_asprintf(memctx,
773 "(&(objectClass=%s)(|",
774 LDAP_OBJ_IDMAP_ENTRY);
775 CHECK_ALLOC_DONE(filter);
778 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
779 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
780 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
781 (unsigned long)ids[idx]->xid.id);
782 CHECK_ALLOC_DONE(filter);
784 filter = talloc_asprintf_append_buffer(filter, "))");
785 CHECK_ALLOC_DONE(filter);
786 DEBUG(10, ("Filter: [%s]\n", filter));
792 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
793 filter, attr_list, 0, &result);
795 if (rc != LDAP_SUCCESS) {
796 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
797 ret = NT_STATUS_UNSUCCESSFUL;
801 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
804 DEBUG(10, ("NO SIDs found\n"));
807 for (i = 0; i < count; i++) {
814 if (i == 0) { /* first entry */
815 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
817 } else { /* following ones */
818 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
822 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
827 /* first check if the SID is present */
828 sidstr = smbldap_talloc_single_attribute(
829 ctx->smbldap_state->ldap_struct,
830 entry, LDAP_ATTRIBUTE_SID, memctx);
831 if ( ! sidstr) { /* no sid, skip entry */
832 DEBUG(2, ("WARNING SID not found on entry\n"));
836 /* now try to see if it is a uid, if not try with a gid
837 * (gid is more common, but in case both uidNumber and
838 * gidNumber are returned the SID is mapped to the uid
841 tmp = smbldap_talloc_single_attribute(
842 ctx->smbldap_state->ldap_struct,
843 entry, uidNumber, memctx);
846 tmp = smbldap_talloc_single_attribute(
847 ctx->smbldap_state->ldap_struct,
848 entry, gidNumber, memctx);
850 if ( ! tmp) { /* wow very strange entry, how did it match ? */
851 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
852 "nor gidNumber returned\n", sidstr));
857 id = strtoul(tmp, NULL, 10);
858 if (!idmap_unix_id_is_in_range(id, dom)) {
859 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
861 dom->low_id, dom->high_id));
868 map = find_map_by_id(&ids[bidx], type, id);
870 DEBUG(2, ("WARNING: couldn't match sid (%s) "
871 "with requested ids\n", sidstr));
876 if ( ! string_to_sid(map->sid, sidstr)) {
877 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
882 if (map->status == ID_MAPPED) {
883 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
884 "overwriting mapping %u -> %s with %u -> %s\n",
885 (type == ID_TYPE_UID) ? "UID" : "GID",
886 id, sid_string_dbg(map->sid), id, sidstr));
892 map->status = ID_MAPPED;
894 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
895 (unsigned long)map->xid.id, map->xid.type));
898 /* free the ldap results */
900 ldap_msgfree(result);
904 if (multi && ids[idx]) { /* still some values to map */
910 /* mark all unknwon/expired ones as unmapped */
911 for (i = 0; ids[i]; i++) {
912 if (ids[i]->status != ID_MAPPED)
913 ids[i]->status = ID_UNMAPPED;
921 /**********************************
922 lookup a set of sids.
923 **********************************/
925 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
926 * in maps for a match */
927 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
931 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
932 if (maps[i] == NULL) { /* end of the run */
935 if (dom_sid_equal(maps[i]->sid, sid)) {
943 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
946 LDAPMessage *entry = NULL;
949 struct idmap_ldap_context *ctx;
950 LDAPMessage *result = NULL;
951 const char *uidNumber;
952 const char *gidNumber;
953 const char **attr_list;
962 /* Only do query if we are online */
963 if (idmap_is_offline()) {
964 return NT_STATUS_FILE_IS_OFFLINE;
967 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
969 memctx = talloc_new(ctx);
971 DEBUG(0, ("Out of memory!\n"));
972 return NT_STATUS_NO_MEMORY;
975 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
976 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
978 attr_list = get_attr_list(memctx, sidmap_attr_list);
981 /* if we are requested just one mapping use the simple filter */
983 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
984 LDAP_OBJ_IDMAP_ENTRY,
986 sid_string_talloc(memctx, ids[0]->sid));
987 CHECK_ALLOC_DONE(filter);
988 DEBUG(10, ("Filter: [%s]\n", filter));
990 /* multiple mappings */
994 for (i = 0; ids[i]; i++) {
995 ids[i]->status = ID_UNKNOWN;
1001 TALLOC_FREE(filter);
1002 filter = talloc_asprintf(memctx,
1003 "(&(objectClass=%s)(|",
1004 LDAP_OBJ_IDMAP_ENTRY);
1005 CHECK_ALLOC_DONE(filter);
1008 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
1009 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
1011 sid_string_talloc(memctx,
1013 CHECK_ALLOC_DONE(filter);
1015 filter = talloc_asprintf_append_buffer(filter, "))");
1016 CHECK_ALLOC_DONE(filter);
1017 DEBUG(10, ("Filter: [%s]", filter));
1023 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
1024 filter, attr_list, 0, &result);
1026 if (rc != LDAP_SUCCESS) {
1027 DEBUG(3,("Failure looking up sids (%s)\n",
1028 ldap_err2string(rc)));
1029 ret = NT_STATUS_UNSUCCESSFUL;
1033 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
1036 DEBUG(10, ("NO SIDs found\n"));
1039 for (i = 0; i < count; i++) {
1040 char *sidstr = NULL;
1047 if (i == 0) { /* first entry */
1048 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1050 } else { /* following ones */
1051 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1055 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1060 /* first check if the SID is present */
1061 sidstr = smbldap_talloc_single_attribute(
1062 ctx->smbldap_state->ldap_struct,
1063 entry, LDAP_ATTRIBUTE_SID, memctx);
1064 if ( ! sidstr) { /* no sid ??, skip entry */
1065 DEBUG(2, ("WARNING SID not found on entry\n"));
1069 if ( ! string_to_sid(&sid, sidstr)) {
1070 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1071 TALLOC_FREE(sidstr);
1075 map = find_map_by_sid(&ids[bidx], &sid);
1077 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1079 TALLOC_FREE(sidstr);
1083 /* now try to see if it is a uid, if not try with a gid
1084 * (gid is more common, but in case both uidNumber and
1085 * gidNumber are returned the SID is mapped to the uid
1088 tmp = smbldap_talloc_single_attribute(
1089 ctx->smbldap_state->ldap_struct,
1090 entry, uidNumber, memctx);
1093 tmp = smbldap_talloc_single_attribute(
1094 ctx->smbldap_state->ldap_struct,
1095 entry, gidNumber, memctx);
1097 if ( ! tmp) { /* no ids ?? */
1098 DEBUG(5, ("no uidNumber, "
1099 "nor gidNumber attributes found\n"));
1100 TALLOC_FREE(sidstr);
1104 id = strtoul(tmp, NULL, 10);
1105 if (!idmap_unix_id_is_in_range(id, dom)) {
1106 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1108 dom->low_id, dom->high_id));
1109 TALLOC_FREE(sidstr);
1115 if (map->status == ID_MAPPED) {
1116 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1117 "overwriting mapping %s -> %u with %s -> %u\n",
1118 (type == ID_TYPE_UID) ? "UID" : "GID",
1119 sidstr, map->xid.id, sidstr, id));
1122 TALLOC_FREE(sidstr);
1125 map->xid.type = type;
1127 map->status = ID_MAPPED;
1129 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1130 (unsigned long)map->xid.id, map->xid.type));
1133 /* free the ldap results */
1135 ldap_msgfree(result);
1139 if (multi && ids[idx]) { /* still some values to map */
1144 * try to create new mappings for unmapped sids
1146 for (i = 0; ids[i]; i++) {
1147 if (ids[i]->status != ID_MAPPED) {
1148 ids[i]->status = ID_UNMAPPED;
1149 if (ids[i]->sid != NULL) {
1150 ret = idmap_ldap_new_mapping(dom, ids[i]);
1151 if (!NT_STATUS_IS_OK(ret)) {
1161 talloc_free(memctx);
1165 /**********************************
1166 Close the idmap ldap instance
1167 **********************************/
1169 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1171 struct idmap_ldap_context *ctx;
1173 if (dom->private_data) {
1174 ctx = talloc_get_type(dom->private_data,
1175 struct idmap_ldap_context);
1178 dom->private_data = NULL;
1181 return NT_STATUS_OK;
1184 static struct idmap_methods idmap_ldap_methods = {
1186 .init = idmap_ldap_db_init,
1187 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1188 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1189 .allocate_id = idmap_ldap_get_new_id,
1190 .close_fn = idmap_ldap_close
1193 NTSTATUS idmap_ldap_init(void);
1194 NTSTATUS idmap_ldap_init(void)
1196 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1197 &idmap_ldap_methods);