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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #define DBGC_CLASS DBGC_IDMAP
37 static char *idmap_fetch_secret(const char *backend
, bool alloc
,
38 const char *domain
, const char *identity
)
44 r
= asprintf(&tmp
, "IDMAP_ALLOC_%s", backend
);
46 r
= asprintf(&tmp
, "IDMAP_%s_%s", backend
, domain
);
52 strupper_m(tmp
); /* make sure the key is case insensitive */
53 ret
= secrets_fetch_generic(tmp
, identity
);
60 struct idmap_ldap_context
{
61 struct smbldap_state
*smbldap_state
;
68 struct idmap_ldap_alloc_context
{
69 struct smbldap_state
*smbldap_state
;
75 #define CHECK_ALLOC_DONE(mem) do { \
77 DEBUG(0, ("Out of memory!\n")); \
78 ret = NT_STATUS_NO_MEMORY; \
82 /**********************************************************************
83 IDMAP ALLOC TDB BACKEND
84 **********************************************************************/
86 static struct idmap_ldap_alloc_context
*idmap_alloc_ldap
;
88 /*********************************************************************
89 ********************************************************************/
91 static NTSTATUS
get_credentials( TALLOC_CTX
*mem_ctx
,
92 struct smbldap_state
*ldap_state
,
93 const char *config_option
,
94 struct idmap_domain
*dom
,
97 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
99 const char *tmp
= NULL
;
100 char *user_dn
= NULL
;
103 /* assume anonymous if we don't have a specified user */
105 tmp
= lp_parm_const_string(-1, config_option
, "ldap_user_dn", NULL
);
109 /* only the alloc backend can pass in a NULL dom */
110 secret
= idmap_fetch_secret("ldap", True
,
113 secret
= idmap_fetch_secret("ldap", False
,
118 DEBUG(0, ("get_credentials: Unable to fetch "
119 "auth credentials for %s in %s\n",
120 tmp
, (dom
==NULL
)?"ALLOC":dom
->name
));
121 ret
= NT_STATUS_ACCESS_DENIED
;
124 *dn
= talloc_strdup(mem_ctx
, tmp
);
125 CHECK_ALLOC_DONE(*dn
);
127 if (!fetch_ldap_pw(&user_dn
, &secret
)) {
128 DEBUG(2, ("get_credentials: Failed to lookup ldap "
129 "bind creds. Using anonymous connection.\n"));
133 *dn
= talloc_strdup(mem_ctx
, user_dn
);
134 SAFE_FREE( user_dn
);
135 CHECK_ALLOC_DONE(*dn
);
139 smbldap_set_creds(ldap_state
, anon
, *dn
, secret
);
149 /**********************************************************************
150 Verify the sambaUnixIdPool entry in the directory.
151 **********************************************************************/
153 static NTSTATUS
verify_idpool(struct idmap_domain
*dom
)
157 LDAPMessage
*result
= NULL
;
158 LDAPMod
**mods
= NULL
;
159 const char **attr_list
;
164 if ( ! idmap_alloc_ldap
) {
165 return NT_STATUS_UNSUCCESSFUL
;
168 ctx
= talloc_new(idmap_alloc_ldap
);
170 DEBUG(0, ("Out of memory!\n"));
171 return NT_STATUS_NO_MEMORY
;
174 filter
= talloc_asprintf(ctx
, "(objectclass=%s)", LDAP_OBJ_IDPOOL
);
175 CHECK_ALLOC_DONE(filter
);
177 attr_list
= get_attr_list(ctx
, idpool_attr_list
);
178 CHECK_ALLOC_DONE(attr_list
);
180 rc
= smbldap_search(idmap_alloc_ldap
->smbldap_state
,
181 idmap_alloc_ldap
->suffix
,
188 if (rc
!= LDAP_SUCCESS
) {
189 DEBUG(1, ("Unable to verify the idpool, "
190 "cannot continue initialization!\n"));
191 return NT_STATUS_UNSUCCESSFUL
;
194 count
= ldap_count_entries(idmap_alloc_ldap
->smbldap_state
->ldap_struct
,
197 ldap_msgfree(result
);
200 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
201 filter
, idmap_alloc_ldap
->suffix
));
202 ret
= NT_STATUS_UNSUCCESSFUL
;
205 else if (count
== 0) {
206 char *uid_str
, *gid_str
;
208 uid_str
= talloc_asprintf(ctx
, "%lu",
209 (unsigned long)dom
->low_id
);
210 gid_str
= talloc_asprintf(ctx
, "%lu",
211 (unsigned long)dom
->low_id
);
213 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
214 "objectClass", LDAP_OBJ_IDPOOL
);
215 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
216 get_attr_key2string(idpool_attr_list
,
217 LDAP_ATTR_UIDNUMBER
),
219 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
220 get_attr_key2string(idpool_attr_list
,
221 LDAP_ATTR_GIDNUMBER
),
224 rc
= smbldap_modify(idmap_alloc_ldap
->smbldap_state
,
225 idmap_alloc_ldap
->suffix
,
227 ldap_mods_free(mods
, True
);
229 ret
= NT_STATUS_UNSUCCESSFUL
;
234 ret
= (rc
== LDAP_SUCCESS
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
240 /*****************************************************************************
241 Initialise idmap database.
242 *****************************************************************************/
244 static NTSTATUS
idmap_ldap_alloc_init(struct idmap_domain
*dom
,
247 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
250 /* Only do init if we are online */
251 if (idmap_is_offline()) {
252 return NT_STATUS_FILE_IS_OFFLINE
;
255 idmap_alloc_ldap
= TALLOC_ZERO_P(NULL
, struct idmap_ldap_alloc_context
);
256 CHECK_ALLOC_DONE( idmap_alloc_ldap
);
258 if (params
&& *params
) {
259 /* assume location is the only parameter */
260 idmap_alloc_ldap
->url
= talloc_strdup(idmap_alloc_ldap
, params
);
262 tmp
= lp_parm_const_string(-1, "idmap alloc config",
266 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
267 ret
= NT_STATUS_UNSUCCESSFUL
;
271 idmap_alloc_ldap
->url
= talloc_strdup(idmap_alloc_ldap
, tmp
);
273 CHECK_ALLOC_DONE( idmap_alloc_ldap
->url
);
275 trim_char(idmap_alloc_ldap
->url
, '\"', '\"');
277 tmp
= lp_parm_const_string(-1, "idmap alloc config",
278 "ldap_base_dn", NULL
);
279 if ( ! tmp
|| ! *tmp
) {
280 tmp
= lp_ldap_idmap_suffix();
282 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
283 ret
= NT_STATUS_UNSUCCESSFUL
;
288 idmap_alloc_ldap
->suffix
= talloc_strdup(idmap_alloc_ldap
, tmp
);
289 CHECK_ALLOC_DONE( idmap_alloc_ldap
->suffix
);
291 ret
= smbldap_init(idmap_alloc_ldap
, winbind_event_context(),
292 idmap_alloc_ldap
->url
,
293 &idmap_alloc_ldap
->smbldap_state
);
294 if (!NT_STATUS_IS_OK(ret
)) {
295 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
296 idmap_alloc_ldap
->url
));
300 ret
= get_credentials( idmap_alloc_ldap
,
301 idmap_alloc_ldap
->smbldap_state
,
302 "idmap alloc config", NULL
,
303 &idmap_alloc_ldap
->user_dn
);
304 if ( !NT_STATUS_IS_OK(ret
) ) {
305 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
306 "credentials (%s)\n", nt_errstr(ret
)));
310 /* see if the idmap suffix and sub entries exists */
312 ret
= verify_idpool(dom
);
315 if ( !NT_STATUS_IS_OK( ret
) )
316 TALLOC_FREE( idmap_alloc_ldap
);
321 /********************************
322 Allocate a new uid or gid
323 ********************************/
325 static NTSTATUS
idmap_ldap_allocate_id(struct idmap_domain
*dom
,
329 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
330 int rc
= LDAP_SERVER_DOWN
;
332 LDAPMessage
*result
= NULL
;
333 LDAPMessage
*entry
= NULL
;
334 LDAPMod
**mods
= NULL
;
338 const char *dn
= NULL
;
339 const char **attr_list
;
342 /* Only do query if we are online */
343 if (idmap_is_offline()) {
344 return NT_STATUS_FILE_IS_OFFLINE
;
347 if ( ! idmap_alloc_ldap
) {
348 return NT_STATUS_UNSUCCESSFUL
;
351 ctx
= talloc_new(idmap_alloc_ldap
);
353 DEBUG(0, ("Out of memory!\n"));
354 return NT_STATUS_NO_MEMORY
;
361 type
= get_attr_key2string(idpool_attr_list
,
362 LDAP_ATTR_UIDNUMBER
);
366 type
= get_attr_key2string(idpool_attr_list
,
367 LDAP_ATTR_GIDNUMBER
);
371 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
372 return NT_STATUS_INVALID_PARAMETER
;
375 filter
= talloc_asprintf(ctx
, "(objectClass=%s)", LDAP_OBJ_IDPOOL
);
376 CHECK_ALLOC_DONE(filter
);
378 attr_list
= get_attr_list(ctx
, idpool_attr_list
);
379 CHECK_ALLOC_DONE(attr_list
);
381 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter
));
383 rc
= smbldap_search(idmap_alloc_ldap
->smbldap_state
,
384 idmap_alloc_ldap
->suffix
,
385 LDAP_SCOPE_SUBTREE
, filter
,
386 attr_list
, 0, &result
);
388 if (rc
!= LDAP_SUCCESS
) {
389 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL
));
393 talloc_autofree_ldapmsg(ctx
, result
);
395 count
= ldap_count_entries(idmap_alloc_ldap
->smbldap_state
->ldap_struct
,
398 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL
));
402 entry
= ldap_first_entry(idmap_alloc_ldap
->smbldap_state
->ldap_struct
,
405 dn
= smbldap_talloc_dn(ctx
,
406 idmap_alloc_ldap
->smbldap_state
->ldap_struct
,
412 if ( ! (id_str
= smbldap_talloc_single_attribute(idmap_alloc_ldap
->smbldap_state
->ldap_struct
,
413 entry
, type
, ctx
))) {
414 DEBUG(0,("%s attribute not found\n", type
));
415 ret
= NT_STATUS_UNSUCCESSFUL
;
419 xid
->id
= strtoul(id_str
, NULL
, 10);
421 /* make sure we still have room to grow */
425 if (xid
->id
> dom
->high_id
) {
426 DEBUG(0,("Cannot allocate uid above %lu!\n",
427 (unsigned long)dom
->high_id
));
433 if (xid
->id
> dom
->high_id
) {
434 DEBUG(0,("Cannot allocate gid above %lu!\n",
435 (unsigned long)dom
->high_id
));
445 new_id_str
= talloc_asprintf(ctx
, "%lu", (unsigned long)xid
->id
+ 1);
447 DEBUG(0,("Out of memory\n"));
448 ret
= NT_STATUS_NO_MEMORY
;
452 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, type
, id_str
);
453 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, type
, new_id_str
);
456 DEBUG(0,("smbldap_set_mod() failed.\n"));
460 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
461 id_str
, new_id_str
));
463 rc
= smbldap_modify(idmap_alloc_ldap
->smbldap_state
, dn
, mods
);
465 ldap_mods_free(mods
, True
);
467 if (rc
!= LDAP_SUCCESS
) {
468 DEBUG(1,("Failed to allocate new %s. "
469 "smbldap_modify() failed.\n", type
));
480 /**********************************
481 Close idmap ldap alloc
482 **********************************/
484 static NTSTATUS
idmap_ldap_alloc_close(void)
486 if (idmap_alloc_ldap
) {
487 smbldap_free_struct(&idmap_alloc_ldap
->smbldap_state
);
488 DEBUG(5,("The connection to the LDAP server was closed\n"));
489 /* maybe free the results here --metze */
490 TALLOC_FREE(idmap_alloc_ldap
);
496 /**********************************************************************
497 IDMAP MAPPING LDAP BACKEND
498 **********************************************************************/
500 static int idmap_ldap_close_destructor(struct idmap_ldap_context
*ctx
)
502 smbldap_free_struct(&ctx
->smbldap_state
);
503 DEBUG(5,("The connection to the LDAP server was closed\n"));
504 /* maybe free the results here --metze */
509 /********************************
510 Initialise idmap database.
511 ********************************/
513 static NTSTATUS
idmap_ldap_db_init(struct idmap_domain
*dom
,
517 struct idmap_ldap_context
*ctx
= NULL
;
518 char *config_option
= NULL
;
519 const char *tmp
= NULL
;
521 /* Only do init if we are online */
522 if (idmap_is_offline()) {
523 return NT_STATUS_FILE_IS_OFFLINE
;
526 ctx
= TALLOC_ZERO_P(dom
, struct idmap_ldap_context
);
528 DEBUG(0, ("Out of memory!\n"));
529 return NT_STATUS_NO_MEMORY
;
532 if (strequal(dom
->name
, "*")) {
533 /* more specific configuration can go here */
535 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
536 if ( ! config_option
) {
537 DEBUG(0, ("Out of memory!\n"));
538 ret
= NT_STATUS_NO_MEMORY
;
543 if (params
!= NULL
) {
544 /* assume location is the only parameter */
545 ctx
->url
= talloc_strdup(ctx
, params
);
547 tmp
= lp_parm_const_string(-1, config_option
, "ldap_url", NULL
);
550 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
551 ret
= NT_STATUS_UNSUCCESSFUL
;
555 ctx
->url
= talloc_strdup(ctx
, tmp
);
557 CHECK_ALLOC_DONE(ctx
->url
);
559 trim_char(ctx
->url
, '\"', '\"');
561 tmp
= lp_parm_const_string(-1, config_option
, "ldap_base_dn", NULL
);
562 if ( ! tmp
|| ! *tmp
) {
563 tmp
= lp_ldap_idmap_suffix();
565 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
566 ret
= NT_STATUS_UNSUCCESSFUL
;
571 ctx
->suffix
= talloc_strdup(ctx
, tmp
);
572 CHECK_ALLOC_DONE(ctx
->suffix
);
574 ret
= smbldap_init(ctx
, winbind_event_context(), ctx
->url
,
575 &ctx
->smbldap_state
);
576 if (!NT_STATUS_IS_OK(ret
)) {
577 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx
->url
));
581 ret
= get_credentials( ctx
, ctx
->smbldap_state
, config_option
,
582 dom
, &ctx
->user_dn
);
583 if ( !NT_STATUS_IS_OK(ret
) ) {
584 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
585 "credentials (%s)\n", nt_errstr(ret
)));
589 /* set the destructor on the context, so that resource are properly
590 freed if the contexts is released */
592 talloc_set_destructor(ctx
, idmap_ldap_close_destructor
);
594 dom
->private_data
= ctx
;
596 talloc_free(config_option
);
605 /* max number of ids requested per batch query */
606 #define IDMAP_LDAP_MAX_IDS 30
608 /**********************************
609 lookup a set of unix ids.
610 **********************************/
612 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
613 * in maps for a match */
614 static struct id_map
*find_map_by_id(struct id_map
**maps
,
620 for (i
= 0; i
< IDMAP_LDAP_MAX_IDS
; i
++) {
621 if (maps
[i
] == NULL
) { /* end of the run */
624 if ((maps
[i
]->xid
.type
== type
) && (maps
[i
]->xid
.id
== id
)) {
632 static NTSTATUS
idmap_ldap_unixids_to_sids(struct idmap_domain
*dom
,
637 struct idmap_ldap_context
*ctx
;
638 LDAPMessage
*result
= NULL
;
639 LDAPMessage
*entry
= NULL
;
640 const char *uidNumber
;
641 const char *gidNumber
;
642 const char **attr_list
;
651 /* Only do query if we are online */
652 if (idmap_is_offline()) {
653 return NT_STATUS_FILE_IS_OFFLINE
;
656 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
658 memctx
= talloc_new(ctx
);
660 DEBUG(0, ("Out of memory!\n"));
661 return NT_STATUS_NO_MEMORY
;
664 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
665 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
667 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
670 /* if we are requested just one mapping use the simple filter */
672 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%lu))",
673 LDAP_OBJ_IDMAP_ENTRY
,
674 (ids
[0]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
675 (unsigned long)ids
[0]->xid
.id
);
676 CHECK_ALLOC_DONE(filter
);
677 DEBUG(10, ("Filter: [%s]\n", filter
));
679 /* multiple mappings */
683 for (i
= 0; ids
[i
]; i
++) {
684 ids
[i
]->status
= ID_UNKNOWN
;
691 filter
= talloc_asprintf(memctx
,
692 "(&(objectClass=%s)(|",
693 LDAP_OBJ_IDMAP_ENTRY
);
694 CHECK_ALLOC_DONE(filter
);
697 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
698 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%lu)",
699 (ids
[idx
]->xid
.type
==ID_TYPE_UID
)?uidNumber
:gidNumber
,
700 (unsigned long)ids
[idx
]->xid
.id
);
701 CHECK_ALLOC_DONE(filter
);
703 filter
= talloc_asprintf_append_buffer(filter
, "))");
704 CHECK_ALLOC_DONE(filter
);
705 DEBUG(10, ("Filter: [%s]\n", filter
));
711 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
712 filter
, attr_list
, 0, &result
);
714 if (rc
!= LDAP_SUCCESS
) {
715 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc
)));
716 ret
= NT_STATUS_UNSUCCESSFUL
;
720 count
= ldap_count_entries(ctx
->smbldap_state
->ldap_struct
, result
);
723 DEBUG(10, ("NO SIDs found\n"));
726 for (i
= 0; i
< count
; i
++) {
733 if (i
== 0) { /* first entry */
734 entry
= ldap_first_entry(ctx
->smbldap_state
->ldap_struct
,
736 } else { /* following ones */
737 entry
= ldap_next_entry(ctx
->smbldap_state
->ldap_struct
,
741 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
746 /* first check if the SID is present */
747 sidstr
= smbldap_talloc_single_attribute(
748 ctx
->smbldap_state
->ldap_struct
,
749 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
750 if ( ! sidstr
) { /* no sid, skip entry */
751 DEBUG(2, ("WARNING SID not found on entry\n"));
755 /* now try to see if it is a uid, if not try with a gid
756 * (gid is more common, but in case both uidNumber and
757 * gidNumber are returned the SID is mapped to the uid
760 tmp
= smbldap_talloc_single_attribute(
761 ctx
->smbldap_state
->ldap_struct
,
762 entry
, uidNumber
, memctx
);
765 tmp
= smbldap_talloc_single_attribute(
766 ctx
->smbldap_state
->ldap_struct
,
767 entry
, gidNumber
, memctx
);
769 if ( ! tmp
) { /* wow very strange entry, how did it match ? */
770 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
771 "nor gidNumber returned\n", sidstr
));
776 id
= strtoul(tmp
, NULL
, 10);
777 if (!idmap_unix_id_is_in_range(id
, dom
)) {
778 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
780 dom
->low_id
, dom
->high_id
));
787 map
= find_map_by_id(&ids
[bidx
], type
, id
);
789 DEBUG(2, ("WARNING: couldn't match sid (%s) "
790 "with requested ids\n", sidstr
));
795 if ( ! string_to_sid(map
->sid
, sidstr
)) {
796 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
801 if (map
->status
== ID_MAPPED
) {
802 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
803 "overwriting mapping %u -> %s with %u -> %s\n",
804 (type
== ID_TYPE_UID
) ? "UID" : "GID",
805 id
, sid_string_dbg(map
->sid
), id
, sidstr
));
811 map
->status
= ID_MAPPED
;
813 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
814 (unsigned long)map
->xid
.id
, map
->xid
.type
));
817 /* free the ldap results */
819 ldap_msgfree(result
);
823 if (multi
&& ids
[idx
]) { /* still some values to map */
829 /* mark all unknwon/expired ones as unmapped */
830 for (i
= 0; ids
[i
]; i
++) {
831 if (ids
[i
]->status
!= ID_MAPPED
)
832 ids
[i
]->status
= ID_UNMAPPED
;
840 /**********************************
841 lookup a set of sids.
842 **********************************/
844 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
845 * in maps for a match */
846 static struct id_map
*find_map_by_sid(struct id_map
**maps
, struct dom_sid
*sid
)
850 for (i
= 0; i
< IDMAP_LDAP_MAX_IDS
; i
++) {
851 if (maps
[i
] == NULL
) { /* end of the run */
854 if (sid_equal(maps
[i
]->sid
, sid
)) {
862 static NTSTATUS
idmap_ldap_sids_to_unixids(struct idmap_domain
*dom
,
865 LDAPMessage
*entry
= NULL
;
868 struct idmap_ldap_context
*ctx
;
869 LDAPMessage
*result
= NULL
;
870 const char *uidNumber
;
871 const char *gidNumber
;
872 const char **attr_list
;
881 /* Only do query if we are online */
882 if (idmap_is_offline()) {
883 return NT_STATUS_FILE_IS_OFFLINE
;
886 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
888 memctx
= talloc_new(ctx
);
890 DEBUG(0, ("Out of memory!\n"));
891 return NT_STATUS_NO_MEMORY
;
894 uidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_UIDNUMBER
);
895 gidNumber
= get_attr_key2string(idpool_attr_list
, LDAP_ATTR_GIDNUMBER
);
897 attr_list
= get_attr_list(memctx
, sidmap_attr_list
);
900 /* if we are requested just one mapping use the simple filter */
902 filter
= talloc_asprintf(memctx
, "(&(objectClass=%s)(%s=%s))",
903 LDAP_OBJ_IDMAP_ENTRY
,
905 sid_string_talloc(memctx
, ids
[0]->sid
));
906 CHECK_ALLOC_DONE(filter
);
907 DEBUG(10, ("Filter: [%s]\n", filter
));
909 /* multiple mappings */
913 for (i
= 0; ids
[i
]; i
++) {
914 ids
[i
]->status
= ID_UNKNOWN
;
921 filter
= talloc_asprintf(memctx
,
922 "(&(objectClass=%s)(|",
923 LDAP_OBJ_IDMAP_ENTRY
);
924 CHECK_ALLOC_DONE(filter
);
927 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
928 filter
= talloc_asprintf_append_buffer(filter
, "(%s=%s)",
930 sid_string_talloc(memctx
,
932 CHECK_ALLOC_DONE(filter
);
934 filter
= talloc_asprintf_append_buffer(filter
, "))");
935 CHECK_ALLOC_DONE(filter
);
936 DEBUG(10, ("Filter: [%s]", filter
));
942 rc
= smbldap_search(ctx
->smbldap_state
, ctx
->suffix
, LDAP_SCOPE_SUBTREE
,
943 filter
, attr_list
, 0, &result
);
945 if (rc
!= LDAP_SUCCESS
) {
946 DEBUG(3,("Failure looking up sids (%s)\n",
947 ldap_err2string(rc
)));
948 ret
= NT_STATUS_UNSUCCESSFUL
;
952 count
= ldap_count_entries(ctx
->smbldap_state
->ldap_struct
, result
);
955 DEBUG(10, ("NO SIDs found\n"));
958 for (i
= 0; i
< count
; i
++) {
966 if (i
== 0) { /* first entry */
967 entry
= ldap_first_entry(ctx
->smbldap_state
->ldap_struct
,
969 } else { /* following ones */
970 entry
= ldap_next_entry(ctx
->smbldap_state
->ldap_struct
,
974 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
979 /* first check if the SID is present */
980 sidstr
= smbldap_talloc_single_attribute(
981 ctx
->smbldap_state
->ldap_struct
,
982 entry
, LDAP_ATTRIBUTE_SID
, memctx
);
983 if ( ! sidstr
) { /* no sid ??, skip entry */
984 DEBUG(2, ("WARNING SID not found on entry\n"));
988 if ( ! string_to_sid(&sid
, sidstr
)) {
989 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
994 map
= find_map_by_sid(&ids
[bidx
], &sid
);
996 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1002 /* now try to see if it is a uid, if not try with a gid
1003 * (gid is more common, but in case both uidNumber and
1004 * gidNumber are returned the SID is mapped to the uid
1007 tmp
= smbldap_talloc_single_attribute(
1008 ctx
->smbldap_state
->ldap_struct
,
1009 entry
, uidNumber
, memctx
);
1012 tmp
= smbldap_talloc_single_attribute(
1013 ctx
->smbldap_state
->ldap_struct
,
1014 entry
, gidNumber
, memctx
);
1016 if ( ! tmp
) { /* no ids ?? */
1017 DEBUG(5, ("no uidNumber, "
1018 "nor gidNumber attributes found\n"));
1019 TALLOC_FREE(sidstr
);
1023 id
= strtoul(tmp
, NULL
, 10);
1024 if (!idmap_unix_id_is_in_range(id
, dom
)) {
1025 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1027 dom
->low_id
, dom
->high_id
));
1028 TALLOC_FREE(sidstr
);
1034 if (map
->status
== ID_MAPPED
) {
1035 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1036 "overwriting mapping %s -> %u with %s -> %u\n",
1037 (type
== ID_TYPE_UID
) ? "UID" : "GID",
1038 sidstr
, map
->xid
.id
, sidstr
, id
));
1041 TALLOC_FREE(sidstr
);
1044 map
->xid
.type
= type
;
1046 map
->status
= ID_MAPPED
;
1048 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
1049 (unsigned long)map
->xid
.id
, map
->xid
.type
));
1052 /* free the ldap results */
1054 ldap_msgfree(result
);
1058 if (multi
&& ids
[idx
]) { /* still some values to map */
1064 /* mark all unknwon/expired ones as unmapped */
1065 for (i
= 0; ids
[i
]; i
++) {
1066 if (ids
[i
]->status
!= ID_MAPPED
)
1067 ids
[i
]->status
= ID_UNMAPPED
;
1071 talloc_free(memctx
);
1075 /**********************************
1077 **********************************/
1079 /* TODO: change this: This function cannot be called to modify a mapping,
1080 * only set a new one */
1082 static NTSTATUS
idmap_ldap_set_mapping(struct idmap_domain
*dom
,
1083 const struct id_map
*map
)
1087 struct idmap_ldap_context
*ctx
;
1088 LDAPMessage
*entry
= NULL
;
1089 LDAPMod
**mods
= NULL
;
1096 /* Only do query if we are online */
1097 if (idmap_is_offline()) {
1098 return NT_STATUS_FILE_IS_OFFLINE
;
1101 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ldap_context
);
1103 switch(map
->xid
.type
) {
1105 type
= get_attr_key2string(sidmap_attr_list
,
1106 LDAP_ATTR_UIDNUMBER
);
1110 type
= get_attr_key2string(sidmap_attr_list
,
1111 LDAP_ATTR_GIDNUMBER
);
1115 return NT_STATUS_INVALID_PARAMETER
;
1118 memctx
= talloc_new(ctx
);
1120 DEBUG(0, ("Out of memory!\n"));
1121 return NT_STATUS_NO_MEMORY
;
1124 id_str
= talloc_asprintf(memctx
, "%lu", (unsigned long)map
->xid
.id
);
1125 CHECK_ALLOC_DONE(id_str
);
1127 sid
= talloc_strdup(memctx
, sid_string_talloc(memctx
, map
->sid
));
1128 CHECK_ALLOC_DONE(sid
);
1130 dn
= talloc_asprintf(memctx
, "%s=%s,%s",
1131 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
1134 CHECK_ALLOC_DONE(dn
);
1136 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1137 "objectClass", LDAP_OBJ_IDMAP_ENTRY
);
1139 smbldap_make_mod(ctx
->smbldap_state
->ldap_struct
,
1140 entry
, &mods
, type
, id_str
);
1142 smbldap_make_mod(ctx
->smbldap_state
->ldap_struct
, entry
, &mods
,
1143 get_attr_key2string(sidmap_attr_list
, LDAP_ATTR_SID
),
1147 DEBUG(2, ("ERROR: No mods?\n"));
1148 ret
= NT_STATUS_UNSUCCESSFUL
;
1152 /* TODO: remove conflicting mappings! */
1154 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SID_ENTRY
);
1156 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn
, sid
, id_str
));
1158 rc
= smbldap_add(ctx
->smbldap_state
, dn
, mods
);
1159 ldap_mods_free(mods
, True
);
1161 if (rc
!= LDAP_SUCCESS
) {
1162 char *ld_error
= NULL
;
1163 ldap_get_option(ctx
->smbldap_state
->ldap_struct
,
1164 LDAP_OPT_ERROR_STRING
, &ld_error
);
1165 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1166 "mapping [%s]\n", sid
,
1167 (unsigned long)map
->xid
.id
, type
));
1168 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1169 ld_error
? ld_error
: "(NULL)", ldap_err2string (rc
)));
1171 ldap_memfree(ld_error
);
1173 ret
= NT_STATUS_UNSUCCESSFUL
;
1177 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1178 "%lu [%s]\n", sid
, (unsigned long)map
->xid
.id
, type
));
1183 talloc_free(memctx
);
1187 /**********************************
1188 Close the idmap ldap instance
1189 **********************************/
1191 static NTSTATUS
idmap_ldap_close(struct idmap_domain
*dom
)
1193 struct idmap_ldap_context
*ctx
;
1195 if (dom
->private_data
) {
1196 ctx
= talloc_get_type(dom
->private_data
,
1197 struct idmap_ldap_context
);
1200 dom
->private_data
= NULL
;
1203 return NT_STATUS_OK
;
1206 static struct idmap_methods idmap_ldap_methods
= {
1208 .init
= idmap_ldap_db_init
,
1209 .unixids_to_sids
= idmap_ldap_unixids_to_sids
,
1210 .sids_to_unixids
= idmap_ldap_sids_to_unixids
,
1211 .allocate_id
= idmap_ldap_get_new_id
,
1212 .close_fn
= idmap_ldap_close
1215 NTSTATUS
idmap_ldap_init(void);
1216 NTSTATUS
idmap_ldap_init(void)
1218 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "ldap",
1219 &idmap_ldap_methods
);