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 Initialise idmap database.
244 *****************************************************************************/
246 static int idmap_ldap_alloc_close_destructor(struct idmap_ldap_alloc_context
*ctx
)
248 smbldap_free_struct(&ctx
->smbldap_state
);
249 DEBUG(5,("The connection to the LDAP server was closed\n"));
250 /* maybe free the results here --metze */
254 /********************************
255 Allocate a new uid or gid
256 ********************************/
258 static NTSTATUS
idmap_ldap_allocate_id(struct idmap_domain
*dom
,
262 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
263 int rc
= LDAP_SERVER_DOWN
;
265 LDAPMessage
*result
= NULL
;
266 LDAPMessage
*entry
= NULL
;
267 LDAPMod
**mods
= NULL
;
271 const char *dn
= NULL
;
272 const char **attr_list
;
274 struct idmap_ldap_context
*ctx
;
276 /* Only do query if we are online */
277 if (idmap_is_offline()) {
278 return NT_STATUS_FILE_IS_OFFLINE
;
281 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
284 return NT_STATUS_UNSUCCESSFUL
;
287 mem_ctx
= talloc_new(ctx
->alloc
);
289 DEBUG(0, ("Out of memory!\n"));
290 return NT_STATUS_NO_MEMORY
;
297 type
= get_attr_key2string(idpool_attr_list
,
298 LDAP_ATTR_UIDNUMBER
);
302 type
= get_attr_key2string(idpool_attr_list
,
303 LDAP_ATTR_GIDNUMBER
);
307 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
308 return NT_STATUS_INVALID_PARAMETER
;
311 filter
= talloc_asprintf(mem_ctx
, "(objectClass=%s)", LDAP_OBJ_IDPOOL
);
312 CHECK_ALLOC_DONE(filter
);
314 attr_list
= get_attr_list(mem_ctx
, idpool_attr_list
);
315 CHECK_ALLOC_DONE(attr_list
);
317 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter
));
319 rc
= smbldap_search(ctx
->alloc
->smbldap_state
,
321 LDAP_SCOPE_SUBTREE
, filter
,
322 attr_list
, 0, &result
);
324 if (rc
!= LDAP_SUCCESS
) {
325 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL
));
329 talloc_autofree_ldapmsg(mem_ctx
, result
);
331 count
= ldap_count_entries(ctx
->alloc
->smbldap_state
->ldap_struct
,
334 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL
));
338 entry
= ldap_first_entry(ctx
->alloc
->smbldap_state
->ldap_struct
,
341 dn
= smbldap_talloc_dn(mem_ctx
,
342 ctx
->alloc
->smbldap_state
->ldap_struct
,
348 id_str
= smbldap_talloc_single_attribute(
349 ctx
->alloc
->smbldap_state
->ldap_struct
,
350 entry
, type
, mem_ctx
);
351 if (id_str
== NULL
) {
352 DEBUG(0,("%s attribute not found\n", type
));
353 ret
= NT_STATUS_UNSUCCESSFUL
;
357 xid
->id
= strtoul(id_str
, NULL
, 10);
359 /* make sure we still have room to grow */
363 if (xid
->id
> dom
->high_id
) {
364 DEBUG(0,("Cannot allocate uid above %lu!\n",
365 (unsigned long)dom
->high_id
));
371 if (xid
->id
> dom
->high_id
) {
372 DEBUG(0,("Cannot allocate gid above %lu!\n",
373 (unsigned long)dom
->high_id
));
383 new_id_str
= talloc_asprintf(mem_ctx
, "%lu", (unsigned long)xid
->id
+ 1);
385 DEBUG(0,("Out of memory\n"));
386 ret
= NT_STATUS_NO_MEMORY
;
390 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, type
, id_str
);
391 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, type
, new_id_str
);
394 DEBUG(0,("smbldap_set_mod() failed.\n"));
398 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
399 id_str
, new_id_str
));
401 rc
= smbldap_modify(ctx
->alloc
->smbldap_state
, dn
, mods
);
403 ldap_mods_free(mods
, True
);
405 if (rc
!= LDAP_SUCCESS
) {
406 DEBUG(1,("Failed to allocate new %s. "
407 "smbldap_modify() failed.\n", type
));
414 talloc_free(mem_ctx
);
419 * Allocate a new unix-ID.
420 * For now this is for the default idmap domain only.
421 * Should be extended later on.
423 static NTSTATUS
idmap_ldap_get_new_id(struct idmap_domain
*dom
,
428 if (!strequal(dom
->name
, "*")) {
429 DEBUG(3, ("idmap_ldap_get_new_id: "
430 "Refusing allocation of a new unixid for domain'%s'. "
431 "Currently only supported for the default "
434 return NT_STATUS_NOT_IMPLEMENTED
;
437 ret
= idmap_ldap_allocate_id(dom
, id
);
443 /**********************************************************************
444 IDMAP MAPPING LDAP BACKEND
445 **********************************************************************/
447 static int idmap_ldap_close_destructor(struct idmap_ldap_context
*ctx
)
449 smbldap_free_struct(&ctx
->smbldap_state
);
450 DEBUG(5,("The connection to the LDAP server was closed\n"));
451 /* maybe free the results here --metze */
456 /********************************
457 Initialise idmap database.
458 ********************************/
460 static NTSTATUS
idmap_ldap_set_mapping(struct idmap_domain
*dom
,
461 const struct id_map
*map
);
463 static NTSTATUS
idmap_ldap_db_init(struct idmap_domain
*dom
,
467 struct idmap_ldap_context
*ctx
= NULL
;
468 char *config_option
= NULL
;
469 const char *tmp
= NULL
;
471 /* Only do init if we are online */
472 if (idmap_is_offline()) {
473 return NT_STATUS_FILE_IS_OFFLINE
;
476 ctx
= TALLOC_ZERO_P(dom
, struct idmap_ldap_context
);
478 DEBUG(0, ("Out of memory!\n"));
479 return NT_STATUS_NO_MEMORY
;
482 if (strequal(dom
->name
, "*")) {
483 /* more specific configuration can go here */
485 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
486 if ( ! config_option
) {
487 DEBUG(0, ("Out of memory!\n"));
488 ret
= NT_STATUS_NO_MEMORY
;
493 if (params
!= NULL
) {
494 /* assume location is the only parameter */
495 ctx
->url
= talloc_strdup(ctx
, params
);
497 tmp
= lp_parm_const_string(-1, config_option
, "ldap_url", NULL
);
500 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
501 ret
= NT_STATUS_UNSUCCESSFUL
;
505 ctx
->url
= talloc_strdup(ctx
, tmp
);
507 CHECK_ALLOC_DONE(ctx
->url
);
509 trim_char(ctx
->url
, '\"', '\"');
511 tmp
= lp_parm_const_string(-1, config_option
, "ldap_base_dn", NULL
);
512 if ( ! tmp
|| ! *tmp
) {
513 tmp
= lp_ldap_idmap_suffix();
515 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
516 ret
= NT_STATUS_UNSUCCESSFUL
;
521 ctx
->suffix
= talloc_strdup(ctx
, tmp
);
522 CHECK_ALLOC_DONE(ctx
->suffix
);
524 ctx
->rw_ops
= talloc_zero(ctx
, struct idmap_rw_ops
);
525 CHECK_ALLOC_DONE(ctx
->rw_ops
);
527 ctx
->rw_ops
->get_new_id
= idmap_ldap_get_new_id
;
528 ctx
->rw_ops
->set_mapping
= idmap_ldap_set_mapping
;
530 ret
= smbldap_init(ctx
, winbind_event_context(), ctx
->url
,
531 &ctx
->smbldap_state
);
532 if (!NT_STATUS_IS_OK(ret
)) {
533 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx
->url
));
537 ret
= get_credentials( ctx
, ctx
->smbldap_state
, config_option
,
538 dom
, &ctx
->user_dn
);
539 if ( !NT_STATUS_IS_OK(ret
) ) {
540 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
541 "credentials (%s)\n", nt_errstr(ret
)));
545 /* set the destructor on the context, so that resource are properly
546 freed if the contexts is released */
548 talloc_set_destructor(ctx
, idmap_ldap_close_destructor
);
550 dom
->private_data
= ctx
;
552 ret
= verify_idpool(dom
);
553 if (!NT_STATUS_IS_OK(ret
)) {
554 DEBUG(1, ("idmap_ldap_db_init: failed to verify ID pool (%s)\n",
559 talloc_free(config_option
);
572 /* TODO: change this: This function cannot be called to modify a mapping,
573 * only set a new one */
575 static NTSTATUS
idmap_ldap_set_mapping(struct idmap_domain
*dom
,
576 const struct id_map
*map
)
580 struct idmap_ldap_context
*ctx
;
581 LDAPMessage
*entry
= NULL
;
582 LDAPMod
**mods
= NULL
;
589 /* Only do query if we are online */
590 if (idmap_is_offline()) {
591 return NT_STATUS_FILE_IS_OFFLINE
;
594 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
596 switch(map
->xid
.type
) {
598 type
= get_attr_key2string(sidmap_attr_list
,
599 LDAP_ATTR_UIDNUMBER
);
603 type
= get_attr_key2string(sidmap_attr_list
,
604 LDAP_ATTR_GIDNUMBER
);
608 return NT_STATUS_INVALID_PARAMETER
;
611 memctx
= talloc_new(ctx
);
613 DEBUG(0, ("Out of memory!\n"));
614 return NT_STATUS_NO_MEMORY
;
617 id_str
= talloc_asprintf(memctx
, "%lu", (unsigned long)map
->xid
.id
);
618 CHECK_ALLOC_DONE(id_str
);
620 sid
= talloc_strdup(memctx
, sid_string_talloc(memctx
, map
->sid
));
621 CHECK_ALLOC_DONE(sid
);
623 dn
= talloc_asprintf(memctx
, "%s=%s,%s",
624 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
627 CHECK_ALLOC_DONE(dn
);
629 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
630 "objectClass", LDAP_OBJ_IDMAP_ENTRY
);
632 smbldap_make_mod(ctx
->smbldap_state
->ldap_struct
,
633 entry
, &mods
, type
, id_str
);
635 smbldap_make_mod(ctx
->smbldap_state
->ldap_struct
, entry
, &mods
,
636 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
640 DEBUG(2, ("ERROR: No mods?\n"));
641 ret
= NT_STATUS_UNSUCCESSFUL
;
645 /* TODO: remove conflicting mappings! */
647 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SID_ENTRY
);
649 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn
, sid
, id_str
));
651 rc
= smbldap_add(ctx
->smbldap_state
, dn
, mods
);
652 ldap_mods_free(mods
, True
);
654 if (rc
!= LDAP_SUCCESS
) {
655 char *ld_error
= NULL
;
656 ldap_get_option(ctx
->smbldap_state
->ldap_struct
,
657 LDAP_OPT_ERROR_STRING
, &ld_error
);
658 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
659 "mapping [%s]\n", sid
,
660 (unsigned long)map
->xid
.id
, type
));
661 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
662 ld_error
? ld_error
: "(NULL)", ldap_err2string (rc
)));
664 ldap_memfree(ld_error
);
666 ret
= NT_STATUS_UNSUCCESSFUL
;
670 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
671 "%lu [%s]\n", sid
, (unsigned long)map
->xid
.id
, type
));
681 * Create a new mapping for an unmapped SID, also allocating a new ID.
682 * If possible, this should be run inside a transaction to make the
685 static NTSTATUS
idmap_ldap_new_mapping(struct idmap_domain
*dom
, struct id_map
*map
)
688 struct idmap_ldap_context
*ctx
;
690 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
692 ret
= idmap_rw_new_mapping(dom
, ctx
->rw_ops
, map
);
698 /* max number of ids requested per batch query */
699 #define IDMAP_LDAP_MAX_IDS 30
701 /**********************************
702 lookup a set of unix ids.
703 **********************************/
705 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
706 * in maps for a match */
707 static struct id_map
*find_map_by_id(struct id_map
**maps
,
713 for (i
= 0; i
< IDMAP_LDAP_MAX_IDS
; i
++) {
714 if (maps
[i
] == NULL
) { /* end of the run */
717 if ((maps
[i
]->xid
.type
== type
) && (maps
[i
]->xid
.id
== id
)) {
725 static NTSTATUS
idmap_ldap_unixids_to_sids(struct idmap_domain
*dom
,
730 struct idmap_ldap_context
*ctx
;
731 LDAPMessage
*result
= NULL
;
732 LDAPMessage
*entry
= NULL
;
733 const char *uidNumber
;
734 const char *gidNumber
;
735 const char **attr_list
;
744 /* Only do query if we are online */
745 if (idmap_is_offline()) {
746 return NT_STATUS_FILE_IS_OFFLINE
;
749 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
751 memctx
= talloc_new(ctx
);
753 DEBUG(0, ("Out of memory!\n"));
754 return NT_STATUS_NO_MEMORY
;
757 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
758 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
760 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
763 /* if we are requested just one mapping use the simple filter */
765 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%lu))",
766 LDAP_OBJ_IDMAP_ENTRY
,
767 (ids
[0]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
768 (unsigned long)ids
[0]->xid
.id
);
769 CHECK_ALLOC_DONE(filter
);
770 DEBUG(10, ("Filter: [%s]\n", filter
));
772 /* multiple mappings */
776 for (i
= 0; ids
[i
]; i
++) {
777 ids
[i
]->status
= ID_UNKNOWN
;
784 filter
= talloc_asprintf(memctx
,
785 "(&(objectClass=%s)(|",
786 LDAP_OBJ_IDMAP_ENTRY
);
787 CHECK_ALLOC_DONE(filter
);
790 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
791 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%lu)",
792 (ids
[idx
]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
793 (unsigned long)ids
[idx
]->xid
.id
);
794 CHECK_ALLOC_DONE(filter
);
796 filter
= talloc_asprintf_append_buffer(filter
, "))");
797 CHECK_ALLOC_DONE(filter
);
798 DEBUG(10, ("Filter: [%s]\n", filter
));
804 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
805 filter
, attr_list
, 0, &result
);
807 if (rc
!= LDAP_SUCCESS
) {
808 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc
)));
809 ret
= NT_STATUS_UNSUCCESSFUL
;
813 count
= ldap_count_entries(ctx
->smbldap_state
->ldap_struct
, result
);
816 DEBUG(10, ("NO SIDs found\n"));
819 for (i
= 0; i
< count
; i
++) {
826 if (i
== 0) { /* first entry */
827 entry
= ldap_first_entry(ctx
->smbldap_state
->ldap_struct
,
829 } else { /* following ones */
830 entry
= ldap_next_entry(ctx
->smbldap_state
->ldap_struct
,
834 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
839 /* first check if the SID is present */
840 sidstr
= smbldap_talloc_single_attribute(
841 ctx
->smbldap_state
->ldap_struct
,
842 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
843 if ( ! sidstr
) { /* no sid, skip entry */
844 DEBUG(2, ("WARNING SID not found on entry\n"));
848 /* now try to see if it is a uid, if not try with a gid
849 * (gid is more common, but in case both uidNumber and
850 * gidNumber are returned the SID is mapped to the uid
853 tmp
= smbldap_talloc_single_attribute(
854 ctx
->smbldap_state
->ldap_struct
,
855 entry
, uidNumber
, memctx
);
858 tmp
= smbldap_talloc_single_attribute(
859 ctx
->smbldap_state
->ldap_struct
,
860 entry
, gidNumber
, memctx
);
862 if ( ! tmp
) { /* wow very strange entry, how did it match ? */
863 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
864 "nor gidNumber returned\n", sidstr
));
869 id
= strtoul(tmp
, NULL
, 10);
870 if (!idmap_unix_id_is_in_range(id
, dom
)) {
871 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
873 dom
->low_id
, dom
->high_id
));
880 map
= find_map_by_id(&ids
[bidx
], type
, id
);
882 DEBUG(2, ("WARNING: couldn't match sid (%s) "
883 "with requested ids\n", sidstr
));
888 if ( ! string_to_sid(map
->sid
, sidstr
)) {
889 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
894 if (map
->status
== ID_MAPPED
) {
895 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
896 "overwriting mapping %u -> %s with %u -> %s\n",
897 (type
== ID_TYPE_UID
) ? "UID" : "GID",
898 id
, sid_string_dbg(map
->sid
), id
, sidstr
));
904 map
->status
= ID_MAPPED
;
906 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
907 (unsigned long)map
->xid
.id
, map
->xid
.type
));
910 /* free the ldap results */
912 ldap_msgfree(result
);
916 if (multi
&& ids
[idx
]) { /* still some values to map */
922 /* mark all unknwon/expired ones as unmapped */
923 for (i
= 0; ids
[i
]; i
++) {
924 if (ids
[i
]->status
!= ID_MAPPED
)
925 ids
[i
]->status
= ID_UNMAPPED
;
933 /**********************************
934 lookup a set of sids.
935 **********************************/
937 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
938 * in maps for a match */
939 static struct id_map
*find_map_by_sid(struct id_map
**maps
, struct dom_sid
*sid
)
943 for (i
= 0; i
< IDMAP_LDAP_MAX_IDS
; i
++) {
944 if (maps
[i
] == NULL
) { /* end of the run */
947 if (dom_sid_equal(maps
[i
]->sid
, sid
)) {
955 static NTSTATUS
idmap_ldap_sids_to_unixids(struct idmap_domain
*dom
,
958 LDAPMessage
*entry
= NULL
;
961 struct idmap_ldap_context
*ctx
;
962 LDAPMessage
*result
= NULL
;
963 const char *uidNumber
;
964 const char *gidNumber
;
965 const char **attr_list
;
974 /* Only do query if we are online */
975 if (idmap_is_offline()) {
976 return NT_STATUS_FILE_IS_OFFLINE
;
979 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
981 memctx
= talloc_new(ctx
);
983 DEBUG(0, ("Out of memory!\n"));
984 return NT_STATUS_NO_MEMORY
;
987 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
988 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
990 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
993 /* if we are requested just one mapping use the simple filter */
995 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%s))",
996 LDAP_OBJ_IDMAP_ENTRY
,
998 sid_string_talloc(memctx
, ids
[0]->sid
));
999 CHECK_ALLOC_DONE(filter
);
1000 DEBUG(10, ("Filter: [%s]\n", filter
));
1002 /* multiple mappings */
1006 for (i
= 0; ids
[i
]; i
++) {
1007 ids
[i
]->status
= ID_UNKNOWN
;
1013 TALLOC_FREE(filter
);
1014 filter
= talloc_asprintf(memctx
,
1015 "(&(objectClass=%s)(|",
1016 LDAP_OBJ_IDMAP_ENTRY
);
1017 CHECK_ALLOC_DONE(filter
);
1020 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
1021 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%s)",
1023 sid_string_talloc(memctx
,
1025 CHECK_ALLOC_DONE(filter
);
1027 filter
= talloc_asprintf_append_buffer(filter
, "))");
1028 CHECK_ALLOC_DONE(filter
);
1029 DEBUG(10, ("Filter: [%s]", filter
));
1035 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
1036 filter
, attr_list
, 0, &result
);
1038 if (rc
!= LDAP_SUCCESS
) {
1039 DEBUG(3,("Failure looking up sids (%s)\n",
1040 ldap_err2string(rc
)));
1041 ret
= NT_STATUS_UNSUCCESSFUL
;
1045 count
= ldap_count_entries(ctx
->smbldap_state
->ldap_struct
, result
);
1048 DEBUG(10, ("NO SIDs found\n"));
1051 for (i
= 0; i
< count
; i
++) {
1052 char *sidstr
= NULL
;
1059 if (i
== 0) { /* first entry */
1060 entry
= ldap_first_entry(ctx
->smbldap_state
->ldap_struct
,
1062 } else { /* following ones */
1063 entry
= ldap_next_entry(ctx
->smbldap_state
->ldap_struct
,
1067 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1072 /* first check if the SID is present */
1073 sidstr
= smbldap_talloc_single_attribute(
1074 ctx
->smbldap_state
->ldap_struct
,
1075 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
1076 if ( ! sidstr
) { /* no sid ??, skip entry */
1077 DEBUG(2, ("WARNING SID not found on entry\n"));
1081 if ( ! string_to_sid(&sid
, sidstr
)) {
1082 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1083 TALLOC_FREE(sidstr
);
1087 map
= find_map_by_sid(&ids
[bidx
], &sid
);
1089 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1091 TALLOC_FREE(sidstr
);
1095 /* now try to see if it is a uid, if not try with a gid
1096 * (gid is more common, but in case both uidNumber and
1097 * gidNumber are returned the SID is mapped to the uid
1100 tmp
= smbldap_talloc_single_attribute(
1101 ctx
->smbldap_state
->ldap_struct
,
1102 entry
, uidNumber
, memctx
);
1105 tmp
= smbldap_talloc_single_attribute(
1106 ctx
->smbldap_state
->ldap_struct
,
1107 entry
, gidNumber
, memctx
);
1109 if ( ! tmp
) { /* no ids ?? */
1110 DEBUG(5, ("no uidNumber, "
1111 "nor gidNumber attributes found\n"));
1112 TALLOC_FREE(sidstr
);
1116 id
= strtoul(tmp
, NULL
, 10);
1117 if (!idmap_unix_id_is_in_range(id
, dom
)) {
1118 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1120 dom
->low_id
, dom
->high_id
));
1121 TALLOC_FREE(sidstr
);
1127 if (map
->status
== ID_MAPPED
) {
1128 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1129 "overwriting mapping %s -> %u with %s -> %u\n",
1130 (type
== ID_TYPE_UID
) ? "UID" : "GID",
1131 sidstr
, map
->xid
.id
, sidstr
, id
));
1134 TALLOC_FREE(sidstr
);
1137 map
->xid
.type
= type
;
1139 map
->status
= ID_MAPPED
;
1141 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
1142 (unsigned long)map
->xid
.id
, map
->xid
.type
));
1145 /* free the ldap results */
1147 ldap_msgfree(result
);
1151 if (multi
&& ids
[idx
]) { /* still some values to map */
1156 * try to create new mappings for unmapped sids
1158 for (i
= 0; ids
[i
]; i
++) {
1159 if (ids
[i
]->status
!= ID_MAPPED
) {
1160 ids
[i
]->status
= ID_UNMAPPED
;
1161 if (ids
[i
]->sid
!= NULL
) {
1162 ret
= idmap_ldap_new_mapping(dom
, ids
[i
]);
1163 if (!NT_STATUS_IS_OK(ret
)) {
1173 talloc_free(memctx
);
1177 /**********************************
1178 Close the idmap ldap instance
1179 **********************************/
1181 static NTSTATUS
idmap_ldap_close(struct idmap_domain
*dom
)
1183 struct idmap_ldap_context
*ctx
;
1185 if (dom
->private_data
) {
1186 ctx
= talloc_get_type(dom
->private_data
,
1187 struct idmap_ldap_context
);
1190 dom
->private_data
= NULL
;
1193 return NT_STATUS_OK
;
1196 static struct idmap_methods idmap_ldap_methods
= {
1198 .init
= idmap_ldap_db_init
,
1199 .unixids_to_sids
= idmap_ldap_unixids_to_sids
,
1200 .sids_to_unixids
= idmap_ldap_sids_to_unixids
,
1201 .allocate_id
= idmap_ldap_get_new_id
,
1202 .close_fn
= idmap_ldap_close
1205 NTSTATUS
idmap_ldap_init(void);
1206 NTSTATUS
idmap_ldap_init(void)
1208 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "ldap",
1209 &idmap_ldap_methods
);