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
, server_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
;
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 sid
= talloc_strdup(memctx
, sid_string_talloc(memctx
, map
->sid
));
549 CHECK_ALLOC_DONE(sid
);
551 dn
= talloc_asprintf(memctx
, "%s=%s,%s",
552 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
555 CHECK_ALLOC_DONE(dn
);
557 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
558 "objectClass", LDAP_OBJ_IDMAP_ENTRY
);
560 smbldap_make_mod(smbldap_get_ldap(ctx
->smbldap_state
),
561 entry
, &mods
, type
, id_str
);
563 smbldap_make_mod(smbldap_get_ldap(ctx
->smbldap_state
), entry
, &mods
,
564 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
568 DEBUG(2, ("ERROR: No mods?\n"));
569 ret
= NT_STATUS_UNSUCCESSFUL
;
573 /* TODO: remove conflicting mappings! */
575 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SID_ENTRY
);
577 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn
, sid
, id_str
));
579 rc
= smbldap_add(ctx
->smbldap_state
, dn
, mods
);
580 ldap_mods_free(mods
, True
);
582 if (rc
!= LDAP_SUCCESS
) {
583 char *ld_error
= NULL
;
584 ldap_get_option(smbldap_get_ldap(ctx
->smbldap_state
),
585 LDAP_OPT_ERROR_STRING
, &ld_error
);
586 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
587 "mapping [%s]\n", sid
,
588 (unsigned long)map
->xid
.id
, type
));
589 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
590 ld_error
? ld_error
: "(NULL)", ldap_err2string (rc
)));
592 ldap_memfree(ld_error
);
594 ret
= NT_STATUS_UNSUCCESSFUL
;
598 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
599 "%lu [%s]\n", sid
, (unsigned long)map
->xid
.id
, type
));
609 * Create a new mapping for an unmapped SID, also allocating a new ID.
610 * If possible, this should be run inside a transaction to make the
613 static NTSTATUS
idmap_ldap_new_mapping(struct idmap_domain
*dom
, struct id_map
*map
)
616 struct idmap_ldap_context
*ctx
;
618 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
620 ret
= idmap_rw_new_mapping(dom
, ctx
->rw_ops
, map
);
625 /**********************************
626 lookup a set of unix ids.
627 **********************************/
629 static NTSTATUS
idmap_ldap_unixids_to_sids(struct idmap_domain
*dom
,
634 struct idmap_ldap_context
*ctx
;
635 LDAPMessage
*result
= NULL
;
636 LDAPMessage
*entry
= NULL
;
637 const char *uidNumber
;
638 const char *gidNumber
;
639 const char **attr_list
;
648 /* Only do query if we are online */
649 if (idmap_is_offline()) {
650 return NT_STATUS_FILE_IS_OFFLINE
;
653 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
655 memctx
= talloc_new(ctx
);
657 DEBUG(0, ("Out of memory!\n"));
658 return NT_STATUS_NO_MEMORY
;
661 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
662 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
664 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
667 /* if we are requested just one mapping use the simple filter */
669 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%lu))",
670 LDAP_OBJ_IDMAP_ENTRY
,
671 (ids
[0]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
672 (unsigned long)ids
[0]->xid
.id
);
673 CHECK_ALLOC_DONE(filter
);
674 DEBUG(10, ("Filter: [%s]\n", filter
));
676 /* multiple mappings */
680 for (i
= 0; ids
[i
]; i
++) {
681 ids
[i
]->status
= ID_UNKNOWN
;
688 filter
= talloc_asprintf(memctx
,
689 "(&(objectClass=%s)(|",
690 LDAP_OBJ_IDMAP_ENTRY
);
691 CHECK_ALLOC_DONE(filter
);
694 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
695 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%lu)",
696 (ids
[idx
]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
697 (unsigned long)ids
[idx
]->xid
.id
);
698 CHECK_ALLOC_DONE(filter
);
700 filter
= talloc_asprintf_append_buffer(filter
, "))");
701 CHECK_ALLOC_DONE(filter
);
702 DEBUG(10, ("Filter: [%s]\n", filter
));
708 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
709 filter
, attr_list
, 0, &result
);
711 if (rc
!= LDAP_SUCCESS
) {
712 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc
)));
713 ret
= NT_STATUS_UNSUCCESSFUL
;
717 count
= ldap_count_entries(smbldap_get_ldap(ctx
->smbldap_state
),
721 DEBUG(10, ("NO SIDs found\n"));
724 for (i
= 0; i
< count
; i
++) {
731 if (i
== 0) { /* first entry */
732 entry
= ldap_first_entry(
733 smbldap_get_ldap(ctx
->smbldap_state
), result
);
734 } else { /* following ones */
735 entry
= ldap_next_entry(
736 smbldap_get_ldap(ctx
->smbldap_state
), entry
);
739 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
744 /* first check if the SID is present */
745 sidstr
= smbldap_talloc_single_attribute(
746 smbldap_get_ldap(ctx
->smbldap_state
),
747 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
748 if ( ! sidstr
) { /* no sid, skip entry */
749 DEBUG(2, ("WARNING SID not found on entry\n"));
753 /* now try to see if it is a uid, if not try with a gid
754 * (gid is more common, but in case both uidNumber and
755 * gidNumber are returned the SID is mapped to the uid
758 tmp
= smbldap_talloc_single_attribute(
759 smbldap_get_ldap(ctx
->smbldap_state
),
760 entry
, uidNumber
, memctx
);
763 tmp
= smbldap_talloc_single_attribute(
764 smbldap_get_ldap(ctx
->smbldap_state
),
765 entry
, gidNumber
, memctx
);
767 if ( ! tmp
) { /* wow very strange entry, how did it match ? */
768 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
769 "nor gidNumber returned\n", sidstr
));
774 id
= strtoul(tmp
, NULL
, 10);
775 if (!idmap_unix_id_is_in_range(id
, dom
)) {
776 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
778 dom
->low_id
, dom
->high_id
));
785 map
= idmap_find_map_by_id(&ids
[bidx
], type
, id
);
787 DEBUG(2, ("WARNING: couldn't match sid (%s) "
788 "with requested ids\n", sidstr
));
793 if ( ! string_to_sid(map
->sid
, sidstr
)) {
794 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
799 if (map
->status
== ID_MAPPED
) {
800 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
801 "overwriting mapping %u -> %s with %u -> %s\n",
802 (type
== ID_TYPE_UID
) ? "UID" : "GID",
803 id
, sid_string_dbg(map
->sid
), id
, sidstr
));
809 map
->status
= ID_MAPPED
;
811 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
812 (unsigned long)map
->xid
.id
, map
->xid
.type
));
815 /* free the ldap results */
817 ldap_msgfree(result
);
821 if (multi
&& ids
[idx
]) { /* still some values to map */
827 /* mark all unknwon/expired ones as unmapped */
828 for (i
= 0; ids
[i
]; i
++) {
829 if (ids
[i
]->status
!= ID_MAPPED
)
830 ids
[i
]->status
= ID_UNMAPPED
;
838 /**********************************
839 lookup a set of sids.
840 **********************************/
842 static NTSTATUS
idmap_ldap_sids_to_unixids(struct idmap_domain
*dom
,
845 LDAPMessage
*entry
= NULL
;
848 struct idmap_ldap_context
*ctx
;
849 LDAPMessage
*result
= NULL
;
850 const char *uidNumber
;
851 const char *gidNumber
;
852 const char **attr_list
;
861 /* Only do query if we are online */
862 if (idmap_is_offline()) {
863 return NT_STATUS_FILE_IS_OFFLINE
;
866 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
868 memctx
= talloc_new(ctx
);
870 DEBUG(0, ("Out of memory!\n"));
871 return NT_STATUS_NO_MEMORY
;
874 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
875 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
877 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
880 /* if we are requested just one mapping use the simple filter */
882 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%s))",
883 LDAP_OBJ_IDMAP_ENTRY
,
885 sid_string_talloc(memctx
, ids
[0]->sid
));
886 CHECK_ALLOC_DONE(filter
);
887 DEBUG(10, ("Filter: [%s]\n", filter
));
889 /* multiple mappings */
893 for (i
= 0; ids
[i
]; i
++) {
894 ids
[i
]->status
= ID_UNKNOWN
;
901 filter
= talloc_asprintf(memctx
,
902 "(&(objectClass=%s)(|",
903 LDAP_OBJ_IDMAP_ENTRY
);
904 CHECK_ALLOC_DONE(filter
);
907 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
908 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%s)",
910 sid_string_talloc(memctx
,
912 CHECK_ALLOC_DONE(filter
);
914 filter
= talloc_asprintf_append_buffer(filter
, "))");
915 CHECK_ALLOC_DONE(filter
);
916 DEBUG(10, ("Filter: [%s]", filter
));
922 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
923 filter
, attr_list
, 0, &result
);
925 if (rc
!= LDAP_SUCCESS
) {
926 DEBUG(3,("Failure looking up sids (%s)\n",
927 ldap_err2string(rc
)));
928 ret
= NT_STATUS_UNSUCCESSFUL
;
932 count
= ldap_count_entries(smbldap_get_ldap(ctx
->smbldap_state
),
936 DEBUG(10, ("NO SIDs found\n"));
939 for (i
= 0; i
< count
; i
++) {
947 if (i
== 0) { /* first entry */
948 entry
= ldap_first_entry(
949 smbldap_get_ldap(ctx
->smbldap_state
), result
);
950 } else { /* following ones */
951 entry
= ldap_next_entry(
952 smbldap_get_ldap(ctx
->smbldap_state
), entry
);
955 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
960 /* first check if the SID is present */
961 sidstr
= smbldap_talloc_single_attribute(
962 smbldap_get_ldap(ctx
->smbldap_state
),
963 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
964 if ( ! sidstr
) { /* no sid ??, skip entry */
965 DEBUG(2, ("WARNING SID not found on entry\n"));
969 if ( ! string_to_sid(&sid
, sidstr
)) {
970 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
975 map
= idmap_find_map_by_sid(&ids
[bidx
], &sid
);
977 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
983 /* now try to see if it is a uid, if not try with a gid
984 * (gid is more common, but in case both uidNumber and
985 * gidNumber are returned the SID is mapped to the uid
988 tmp
= smbldap_talloc_single_attribute(
989 smbldap_get_ldap(ctx
->smbldap_state
),
990 entry
, uidNumber
, memctx
);
993 tmp
= smbldap_talloc_single_attribute(
994 smbldap_get_ldap(ctx
->smbldap_state
),
995 entry
, gidNumber
, memctx
);
997 if ( ! tmp
) { /* no ids ?? */
998 DEBUG(5, ("no uidNumber, "
999 "nor gidNumber attributes found\n"));
1000 TALLOC_FREE(sidstr
);
1004 id
= strtoul(tmp
, NULL
, 10);
1005 if (!idmap_unix_id_is_in_range(id
, dom
)) {
1006 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1008 dom
->low_id
, dom
->high_id
));
1009 TALLOC_FREE(sidstr
);
1015 if (map
->status
== ID_MAPPED
) {
1016 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1017 "overwriting mapping %s -> %u with %s -> %u\n",
1018 (type
== ID_TYPE_UID
) ? "UID" : "GID",
1019 sidstr
, map
->xid
.id
, sidstr
, id
));
1022 TALLOC_FREE(sidstr
);
1025 map
->xid
.type
= type
;
1027 map
->status
= ID_MAPPED
;
1029 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
1030 (unsigned long)map
->xid
.id
, map
->xid
.type
));
1033 /* free the ldap results */
1035 ldap_msgfree(result
);
1039 if (multi
&& ids
[idx
]) { /* still some values to map */
1044 * try to create new mappings for unmapped sids
1046 for (i
= 0; ids
[i
]; i
++) {
1047 if (ids
[i
]->status
!= ID_MAPPED
) {
1048 ids
[i
]->status
= ID_UNMAPPED
;
1049 if (ids
[i
]->sid
!= NULL
) {
1050 ret
= idmap_ldap_new_mapping(dom
, ids
[i
]);
1051 if (!NT_STATUS_IS_OK(ret
)) {
1061 talloc_free(memctx
);
1065 /**********************************
1066 Close the idmap ldap instance
1067 **********************************/
1069 static struct idmap_methods idmap_ldap_methods
= {
1071 .init
= idmap_ldap_db_init
,
1072 .unixids_to_sids
= idmap_ldap_unixids_to_sids
,
1073 .sids_to_unixids
= idmap_ldap_sids_to_unixids
,
1074 .allocate_id
= idmap_ldap_allocate_id
,
1077 NTSTATUS
idmap_ldap_init(TALLOC_CTX
*);
1078 NTSTATUS
idmap_ldap_init(TALLOC_CTX
*ctx
)
1080 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "ldap",
1081 &idmap_ldap_methods
);