2 * idmap_ad: map between Active Directory and RFC 2307 or "Services for Unix" (SFU) Accounts
4 * Unix SMB/CIFS implementation.
6 * Winbind ADS backend functions
8 * Copyright (C) Andrew Tridgell 2001
9 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
10 * Copyright (C) Gerald (Jerry) Carter 2004-2007
11 * Copyright (C) Luke Howard 2001-2004
12 * Copyright (C) Michael Adam 2008,2010
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include "../libds/common/flags.h"
32 #include "libads/ldap_schema.h"
36 #include "../libcli/ldap/ldap_ndr.h"
37 #include "../libcli/security/security.h"
40 #define DBGC_CLASS DBGC_IDMAP
42 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
44 #define IDMAP_AD_MAX_IDS 30
45 #define CHECK_ALLOC_DONE(mem) do { \
47 DEBUG(0, ("Out of memory!\n")); \
48 ret = NT_STATUS_NO_MEMORY; \
53 struct idmap_ad_context
{
55 struct posix_schema
*ad_schema
;
56 enum wb_posix_mapping ad_map_type
; /* WB_POSIX_MAP_UNKNOWN */
59 /************************************************************************
60 ***********************************************************************/
62 static ADS_STATUS
ad_idmap_cached_connection_internal(struct idmap_domain
*dom
)
67 struct sockaddr_storage dc_ip
;
68 struct idmap_ad_context
*ctx
;
69 char *ldap_server
= NULL
;
71 struct winbindd_domain
*wb_dom
;
73 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
76 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
78 if (ctx
->ads
!= NULL
) {
81 time_t now
= time(NULL
);
85 expire
= MIN(ads
->auth
.tgt_expire
, ads
->auth
.tgs_expire
);
87 /* check for a valid structure */
88 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
89 (uint32
)expire
-(uint32
)now
, (uint32
) expire
, (uint32
) now
));
91 if ( ads
->config
.realm
&& (expire
> time(NULL
))) {
94 /* we own this ADS_STRUCT so make sure it goes away */
95 DEBUG(7,("Deleting expired krb5 credential cache\n"));
98 ads_kdestroy(WINBIND_CCACHE_NAME
);
103 /* we don't want this to affect the users ccache */
104 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME
, 1);
107 * At this point we only have the NetBIOS domain name.
108 * Check if we can get server nam and realm from SAF cache
109 * and the domain list.
111 ldap_server
= saf_fetch(dom
->name
);
112 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server
?ldap_server
:""));
114 wb_dom
= find_domain_from_name_noinit(dom
->name
);
115 if (wb_dom
== NULL
) {
116 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
120 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
121 " domain '%s'\n", wb_dom
->alt_name
, dom
->name
));
122 realm
= wb_dom
->alt_name
;
125 if ( (ads
= ads_init(realm
, dom
->name
, ldap_server
)) == NULL
) {
126 DEBUG(1,("ads_init failed\n"));
127 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
130 /* the machine acct password might have change - fetch it every time */
131 SAFE_FREE(ads
->auth
.password
);
132 ads
->auth
.password
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
134 SAFE_FREE(ads
->auth
.realm
);
135 ads
->auth
.realm
= SMB_STRDUP(lp_realm());
137 /* setup server affinity */
139 get_dc_name(dom
->name
, realm
, dc_name
, &dc_ip
);
141 status
= ads_connect(ads
);
142 if (!ADS_ERR_OK(status
)) {
143 DEBUG(1, ("ad_idmap_cached_connection_internal: failed to "
149 ads
->is_mine
= False
;
156 /************************************************************************
157 ***********************************************************************/
159 static ADS_STATUS
ad_idmap_cached_connection(struct idmap_domain
*dom
)
162 struct idmap_ad_context
* ctx
;
164 status
= ad_idmap_cached_connection_internal(dom
);
165 if (!ADS_ERR_OK(status
)) {
169 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
171 /* if we have a valid ADS_STRUCT and the schema model is
172 defined, then we can return here. */
174 if ( ctx
->ad_schema
) {
178 /* Otherwise, set the schema model */
180 if ( (ctx
->ad_map_type
== WB_POSIX_MAP_SFU
) ||
181 (ctx
->ad_map_type
== WB_POSIX_MAP_SFU20
) ||
182 (ctx
->ad_map_type
== WB_POSIX_MAP_RFC2307
) )
184 status
= ads_check_posix_schema_mapping(
185 ctx
, ctx
->ads
, ctx
->ad_map_type
, &ctx
->ad_schema
);
186 if ( !ADS_ERR_OK(status
) ) {
187 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
194 static int idmap_ad_context_destructor(struct idmap_ad_context
*ctx
)
196 if (ctx
->ads
!= NULL
) {
197 /* we own this ADS_STRUCT so make sure it goes away */
198 ctx
->ads
->is_mine
= True
;
199 ads_destroy( &ctx
->ads
);
205 /************************************************************************
206 ***********************************************************************/
208 static NTSTATUS
idmap_ad_initialize(struct idmap_domain
*dom
)
210 struct idmap_ad_context
*ctx
;
212 const char *schema_mode
= NULL
;
214 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
216 DEBUG(0, ("Out of memory!\n"));
217 return NT_STATUS_NO_MEMORY
;
219 talloc_set_destructor(ctx
, idmap_ad_context_destructor
);
221 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
222 if (config_option
== NULL
) {
223 DEBUG(0, ("Out of memory!\n"));
225 return NT_STATUS_NO_MEMORY
;
228 /* default map type */
229 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
232 schema_mode
= lp_parm_const_string(-1, config_option
, "schema_mode", NULL
);
233 if ( schema_mode
&& schema_mode
[0] ) {
234 if ( strequal(schema_mode
, "sfu") )
235 ctx
->ad_map_type
= WB_POSIX_MAP_SFU
;
236 else if ( strequal(schema_mode
, "sfu20" ) )
237 ctx
->ad_map_type
= WB_POSIX_MAP_SFU20
;
238 else if ( strequal(schema_mode
, "rfc2307" ) )
239 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
241 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
245 dom
->private_data
= ctx
;
247 talloc_free(config_option
);
252 /************************************************************************
253 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
254 ***********************************************************************/
256 static struct id_map
*find_map_by_id(struct id_map
**maps
, enum id_type type
, uint32_t id
)
260 for (i
= 0; maps
[i
] && i
<IDMAP_AD_MAX_IDS
; i
++) {
261 if ((maps
[i
]->xid
.type
== type
) && (maps
[i
]->xid
.id
== id
)) {
269 /************************************************************************
270 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
271 ***********************************************************************/
273 static struct id_map
*find_map_by_sid(struct id_map
**maps
, struct dom_sid
*sid
)
277 for (i
= 0; maps
[i
] && i
<IDMAP_AD_MAX_IDS
; i
++) {
278 if (dom_sid_equal(maps
[i
]->sid
, sid
)) {
286 /************************************************************************
287 ***********************************************************************/
289 static NTSTATUS
idmap_ad_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
293 struct idmap_ad_context
*ctx
;
295 const char *attrs
[] = { "sAMAccountType",
297 NULL
, /* uidnumber */
298 NULL
, /* gidnumber */
300 LDAPMessage
*res
= NULL
;
301 LDAPMessage
*entry
= NULL
;
307 char *u_filter
= NULL
;
308 char *g_filter
= NULL
;
310 /* initialize the status to avoid suprise */
311 for (i
= 0; ids
[i
]; i
++) {
312 ids
[i
]->status
= ID_UNKNOWN
;
315 /* Only do query if we are online */
316 if (idmap_is_offline()) {
317 return NT_STATUS_FILE_IS_OFFLINE
;
320 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
322 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
323 DEBUG(0, ("Out of memory!\n"));
324 return NT_STATUS_NO_MEMORY
;
327 rc
= ad_idmap_cached_connection(dom
);
328 if (!ADS_ERR_OK(rc
)) {
329 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
330 ret
= NT_STATUS_UNSUCCESSFUL
;
331 /* ret = ads_ntstatus(rc); */
335 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
336 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
340 for (i
= 0; (i
< IDMAP_AD_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
341 switch (ids
[idx
]->xid
.type
) {
344 u_filter
= talloc_asprintf(memctx
, "(&(|"
345 "(sAMAccountType=%d)"
346 "(sAMAccountType=%d)"
347 "(sAMAccountType=%d))(|",
348 ATYPE_NORMAL_ACCOUNT
,
349 ATYPE_WORKSTATION_TRUST
,
350 ATYPE_INTERDOMAIN_TRUST
);
352 u_filter
= talloc_asprintf_append_buffer(u_filter
, "(%s=%lu)",
353 ctx
->ad_schema
->posix_uidnumber_attr
,
354 (unsigned long)ids
[idx
]->xid
.id
);
355 CHECK_ALLOC_DONE(u_filter
);
360 g_filter
= talloc_asprintf(memctx
, "(&(|"
361 "(sAMAccountType=%d)"
362 "(sAMAccountType=%d))(|",
363 ATYPE_SECURITY_GLOBAL_GROUP
,
364 ATYPE_SECURITY_LOCAL_GROUP
);
366 g_filter
= talloc_asprintf_append_buffer(g_filter
, "(%s=%lu)",
367 ctx
->ad_schema
->posix_gidnumber_attr
,
368 (unsigned long)ids
[idx
]->xid
.id
);
369 CHECK_ALLOC_DONE(g_filter
);
373 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
374 ids
[idx
]->status
= ID_UNKNOWN
;
378 filter
= talloc_asprintf(memctx
, "(|");
379 CHECK_ALLOC_DONE(filter
);
381 filter
= talloc_asprintf_append_buffer(filter
, "%s))", u_filter
);
382 CHECK_ALLOC_DONE(filter
);
383 TALLOC_FREE(u_filter
);
386 filter
= talloc_asprintf_append_buffer(filter
, "%s))", g_filter
);
387 CHECK_ALLOC_DONE(filter
);
388 TALLOC_FREE(g_filter
);
390 filter
= talloc_asprintf_append_buffer(filter
, ")");
391 CHECK_ALLOC_DONE(filter
);
393 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
394 if (!ADS_ERR_OK(rc
)) {
395 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
396 ret
= NT_STATUS_UNSUCCESSFUL
;
400 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
401 DEBUG(10, ("No IDs found\n"));
405 for (i
= 0; (i
< count
) && entry
; i
++) {
412 if (i
== 0) { /* first entry */
413 entry
= ads_first_entry(ctx
->ads
, entry
);
414 } else { /* following ones */
415 entry
= ads_next_entry(ctx
->ads
, entry
);
419 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
423 /* first check if the SID is present */
424 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
425 DEBUG(2, ("Could not retrieve SID from entry\n"));
430 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
431 DEBUG(1, ("could not get SAM account type\n"));
435 switch (atype
& 0xF0000000) {
436 case ATYPE_SECURITY_GLOBAL_GROUP
:
437 case ATYPE_SECURITY_LOCAL_GROUP
:
440 case ATYPE_NORMAL_ACCOUNT
:
441 case ATYPE_WORKSTATION_TRUST
:
442 case ATYPE_INTERDOMAIN_TRUST
:
446 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
450 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
451 ctx
->ad_schema
->posix_uidnumber_attr
:
452 ctx
->ad_schema
->posix_gidnumber_attr
,
455 DEBUG(1, ("Could not get unix ID\n"));
459 if (!idmap_unix_id_is_in_range(id
, dom
)) {
460 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
461 id
, dom
->low_id
, dom
->high_id
));
465 map
= find_map_by_id(&ids
[bidx
], type
, id
);
467 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
471 sid_copy(map
->sid
, &sid
);
474 map
->status
= ID_MAPPED
;
476 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
477 (unsigned long)map
->xid
.id
,
482 ads_msgfree(ctx
->ads
, res
);
485 if (ids
[idx
]) { /* still some values to map */
491 /* mark all unknown/expired ones as unmapped */
492 for (i
= 0; ids
[i
]; i
++) {
493 if (ids
[i
]->status
!= ID_MAPPED
)
494 ids
[i
]->status
= ID_UNMAPPED
;
502 /************************************************************************
503 ***********************************************************************/
505 static NTSTATUS
idmap_ad_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
509 struct idmap_ad_context
*ctx
;
511 const char *attrs
[] = { "sAMAccountType",
513 NULL
, /* attr_uidnumber */
514 NULL
, /* attr_gidnumber */
516 LDAPMessage
*res
= NULL
;
517 LDAPMessage
*entry
= NULL
;
525 /* initialize the status to avoid suprise */
526 for (i
= 0; ids
[i
]; i
++) {
527 ids
[i
]->status
= ID_UNKNOWN
;
530 /* Only do query if we are online */
531 if (idmap_is_offline()) {
532 return NT_STATUS_FILE_IS_OFFLINE
;
535 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
537 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
538 DEBUG(0, ("Out of memory!\n"));
539 return NT_STATUS_NO_MEMORY
;
542 rc
= ad_idmap_cached_connection(dom
);
543 if (!ADS_ERR_OK(rc
)) {
544 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
545 ret
= NT_STATUS_UNSUCCESSFUL
;
546 /* ret = ads_ntstatus(rc); */
550 if (ctx
->ad_schema
== NULL
) {
551 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
552 ret
= NT_STATUS_UNSUCCESSFUL
;
556 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
557 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
560 filter
= talloc_asprintf(memctx
, "(&(|"
561 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
562 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
564 ATYPE_NORMAL_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
,
565 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
);
567 CHECK_ALLOC_DONE(filter
);
570 for (i
= 0; (i
< IDMAP_AD_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
572 ids
[idx
]->status
= ID_UNKNOWN
;
574 sidstr
= ldap_encode_ndr_dom_sid(talloc_tos(), ids
[idx
]->sid
);
575 filter
= talloc_asprintf_append_buffer(filter
, "(objectSid=%s)", sidstr
);
578 CHECK_ALLOC_DONE(filter
);
580 filter
= talloc_asprintf_append_buffer(filter
, "))");
581 CHECK_ALLOC_DONE(filter
);
582 DEBUG(10, ("Filter: [%s]\n", filter
));
584 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
585 if (!ADS_ERR_OK(rc
)) {
586 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
587 ret
= NT_STATUS_UNSUCCESSFUL
;
591 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
592 DEBUG(10, ("No IDs found\n"));
596 for (i
= 0; (i
< count
) && entry
; i
++) {
603 if (i
== 0) { /* first entry */
604 entry
= ads_first_entry(ctx
->ads
, entry
);
605 } else { /* following ones */
606 entry
= ads_next_entry(ctx
->ads
, entry
);
610 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
614 /* first check if the SID is present */
615 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
616 DEBUG(2, ("Could not retrieve SID from entry\n"));
620 map
= find_map_by_sid(&ids
[bidx
], &sid
);
622 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
627 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
628 DEBUG(1, ("could not get SAM account type\n"));
632 switch (atype
& 0xF0000000) {
633 case ATYPE_SECURITY_GLOBAL_GROUP
:
634 case ATYPE_SECURITY_LOCAL_GROUP
:
637 case ATYPE_NORMAL_ACCOUNT
:
638 case ATYPE_WORKSTATION_TRUST
:
639 case ATYPE_INTERDOMAIN_TRUST
:
643 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
647 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
648 ctx
->ad_schema
->posix_uidnumber_attr
:
649 ctx
->ad_schema
->posix_gidnumber_attr
,
652 DEBUG(1, ("Could not get unix ID\n"));
655 if (!idmap_unix_id_is_in_range(id
, dom
)) {
656 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
657 id
, dom
->low_id
, dom
->high_id
));
662 map
->xid
.type
= type
;
664 map
->status
= ID_MAPPED
;
666 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
667 (unsigned long)map
->xid
.id
,
672 ads_msgfree(ctx
->ads
, res
);
675 if (ids
[idx
]) { /* still some values to map */
681 /* mark all unknown/expired ones as unmapped */
682 for (i
= 0; ids
[i
]; i
++) {
683 if (ids
[i
]->status
!= ID_MAPPED
)
684 ids
[i
]->status
= ID_UNMAPPED
;
693 * nss_info_{sfu,sfu20,rfc2307}
696 /************************************************************************
697 Initialize the {sfu,sfu20,rfc2307} state
698 ***********************************************************************/
700 static const char *wb_posix_map_unknown_string
= "WB_POSIX_MAP_UNKNOWN";
701 static const char *wb_posix_map_template_string
= "WB_POSIX_MAP_TEMPLATE";
702 static const char *wb_posix_map_sfu_string
= "WB_POSIX_MAP_SFU";
703 static const char *wb_posix_map_sfu20_string
= "WB_POSIX_MAP_SFU20";
704 static const char *wb_posix_map_rfc2307_string
= "WB_POSIX_MAP_RFC2307";
705 static const char *wb_posix_map_unixinfo_string
= "WB_POSIX_MAP_UNIXINFO";
707 static const char *ad_map_type_string(enum wb_posix_mapping map_type
)
710 case WB_POSIX_MAP_TEMPLATE
:
711 return wb_posix_map_template_string
;
712 case WB_POSIX_MAP_SFU
:
713 return wb_posix_map_sfu_string
;
714 case WB_POSIX_MAP_SFU20
:
715 return wb_posix_map_sfu20_string
;
716 case WB_POSIX_MAP_RFC2307
:
717 return wb_posix_map_rfc2307_string
;
718 case WB_POSIX_MAP_UNIXINFO
:
719 return wb_posix_map_unixinfo_string
;
721 return wb_posix_map_unknown_string
;
725 static NTSTATUS
nss_ad_generic_init(struct nss_domain_entry
*e
,
726 enum wb_posix_mapping new_ad_map_type
)
728 struct idmap_domain
*dom
;
729 struct idmap_ad_context
*ctx
;
731 if (e
->state
!= NULL
) {
732 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
734 dom
= talloc_zero(e
, struct idmap_domain
);
736 DEBUG(0, ("Out of memory!\n"));
737 return NT_STATUS_NO_MEMORY
;
742 if (e
->domain
!= NULL
) {
743 dom
->name
= talloc_strdup(dom
, e
->domain
);
744 if (dom
->name
== NULL
) {
745 DEBUG(0, ("Out of memory!\n"));
746 return NT_STATUS_NO_MEMORY
;
750 if (dom
->private_data
!= NULL
) {
751 ctx
= talloc_get_type(dom
->private_data
,
752 struct idmap_ad_context
);
754 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
756 DEBUG(0, ("Out of memory!\n"));
757 return NT_STATUS_NO_MEMORY
;
759 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
760 dom
->private_data
= ctx
;
763 if ((ctx
->ad_map_type
!= WB_POSIX_MAP_UNKNOWN
) &&
764 (ctx
->ad_map_type
!= new_ad_map_type
))
766 DEBUG(2, ("nss_ad_generic_init: "
767 "Warning: overriding previously set posix map type "
768 "%s for domain %s with map type %s.\n",
769 ad_map_type_string(ctx
->ad_map_type
),
771 ad_map_type_string(new_ad_map_type
)));
774 ctx
->ad_map_type
= new_ad_map_type
;
779 static NTSTATUS
nss_sfu_init( struct nss_domain_entry
*e
)
781 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU
);
784 static NTSTATUS
nss_sfu20_init( struct nss_domain_entry
*e
)
786 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU20
);
789 static NTSTATUS
nss_rfc2307_init( struct nss_domain_entry
*e
)
791 return nss_ad_generic_init(e
, WB_POSIX_MAP_RFC2307
);
795 /************************************************************************
796 ***********************************************************************/
798 static NTSTATUS
nss_ad_get_info( struct nss_domain_entry
*e
,
799 const struct dom_sid
*sid
,
801 const char **homedir
,
806 const char *attrs
[] = {NULL
, /* attr_homedir */
807 NULL
, /* attr_shell */
808 NULL
, /* attr_gecos */
809 NULL
, /* attr_gidnumber */
812 LDAPMessage
*msg_internal
= NULL
;
813 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
814 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
816 struct idmap_domain
*dom
;
817 struct idmap_ad_context
*ctx
;
819 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
820 sid_string_dbg(sid
), e
->domain
?e
->domain
:"NULL"));
822 /* Only do query if we are online */
823 if (idmap_is_offline()) {
824 return NT_STATUS_FILE_IS_OFFLINE
;
827 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
828 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
830 ads_status
= ad_idmap_cached_connection(dom
);
831 if (!ADS_ERR_OK(ads_status
)) {
832 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
835 if (!ctx
->ad_schema
) {
836 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
837 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
840 if (!sid
|| !homedir
|| !shell
|| !gecos
) {
841 return NT_STATUS_INVALID_PARAMETER
;
844 /* Have to do our own query */
846 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
849 attrs
[0] = ctx
->ad_schema
->posix_homedir_attr
;
850 attrs
[1] = ctx
->ad_schema
->posix_shell_attr
;
851 attrs
[2] = ctx
->ad_schema
->posix_gecos_attr
;
852 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
854 sidstr
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
855 filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)", sidstr
);
859 nt_status
= NT_STATUS_NO_MEMORY
;
863 ads_status
= ads_search_retry(ctx
->ads
, &msg_internal
, filter
, attrs
);
864 if (!ADS_ERR_OK(ads_status
)) {
865 nt_status
= ads_ntstatus(ads_status
);
869 *homedir
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_homedir_attr
);
870 *shell
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_shell_attr
);
871 *gecos
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_gecos_attr
);
874 if (!ads_pull_uint32(ctx
->ads
, msg_internal
, ctx
->ad_schema
->posix_gidnumber_attr
, gid
))
878 nt_status
= NT_STATUS_OK
;
882 ads_msgfree(ctx
->ads
, msg_internal
);
888 /**********************************************************************
889 *********************************************************************/
891 static NTSTATUS
nss_ad_map_to_alias(TALLOC_CTX
*mem_ctx
,
892 struct nss_domain_entry
*e
,
896 const char *attrs
[] = {NULL
, /* attr_uid */
899 LDAPMessage
*msg
= NULL
;
900 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
901 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
902 struct idmap_domain
*dom
;
903 struct idmap_ad_context
*ctx
= NULL
;
905 /* Check incoming parameters */
907 if ( !e
|| !e
->domain
|| !name
|| !*alias
) {
908 nt_status
= NT_STATUS_INVALID_PARAMETER
;
912 /* Only do query if we are online */
914 if (idmap_is_offline()) {
915 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
919 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
920 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
922 ads_status
= ad_idmap_cached_connection(dom
);
923 if (!ADS_ERR_OK(ads_status
)) {
924 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
927 if (!ctx
->ad_schema
) {
928 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
932 attrs
[0] = ctx
->ad_schema
->posix_uid_attr
;
934 filter
= talloc_asprintf(mem_ctx
,
935 "(sAMAccountName=%s)",
938 nt_status
= NT_STATUS_NO_MEMORY
;
942 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
943 if (!ADS_ERR_OK(ads_status
)) {
944 nt_status
= ads_ntstatus(ads_status
);
948 *alias
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_uid_attr
);
951 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
954 nt_status
= NT_STATUS_OK
;
958 talloc_destroy(filter
);
961 ads_msgfree(ctx
->ads
, msg
);
967 /**********************************************************************
968 *********************************************************************/
970 static NTSTATUS
nss_ad_map_from_alias( TALLOC_CTX
*mem_ctx
,
971 struct nss_domain_entry
*e
,
975 const char *attrs
[] = {"sAMAccountName",
978 LDAPMessage
*msg
= NULL
;
979 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
980 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
982 struct idmap_domain
*dom
;
983 struct idmap_ad_context
*ctx
= NULL
;
985 /* Check incoming parameters */
987 if ( !alias
|| !name
) {
988 nt_status
= NT_STATUS_INVALID_PARAMETER
;
992 /* Only do query if we are online */
994 if (idmap_is_offline()) {
995 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
999 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
1000 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
1002 ads_status
= ad_idmap_cached_connection(dom
);
1003 if (!ADS_ERR_OK(ads_status
)) {
1004 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1007 if (!ctx
->ad_schema
) {
1008 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
1012 filter
= talloc_asprintf(mem_ctx
,
1014 ctx
->ad_schema
->posix_uid_attr
,
1017 nt_status
= NT_STATUS_NO_MEMORY
;
1021 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
1022 if (!ADS_ERR_OK(ads_status
)) {
1023 nt_status
= ads_ntstatus(ads_status
);
1027 username
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
,
1030 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1033 *name
= talloc_asprintf(mem_ctx
, "%s\\%s",
1037 nt_status
= NT_STATUS_NO_MEMORY
;
1041 nt_status
= NT_STATUS_OK
;
1045 talloc_destroy(filter
);
1048 ads_msgfree(ctx
->ads
, msg
);
1054 /************************************************************************
1055 Function dispatch tables for the idmap and nss plugins
1056 ***********************************************************************/
1058 static struct idmap_methods ad_methods
= {
1059 .init
= idmap_ad_initialize
,
1060 .unixids_to_sids
= idmap_ad_unixids_to_sids
,
1061 .sids_to_unixids
= idmap_ad_sids_to_unixids
,
1064 /* The SFU and RFC2307 NSS plugins share everything but the init
1065 function which sets the intended schema model to use */
1067 static struct nss_info_methods nss_rfc2307_methods
= {
1068 .init
= nss_rfc2307_init
,
1069 .get_nss_info
= nss_ad_get_info
,
1070 .map_to_alias
= nss_ad_map_to_alias
,
1071 .map_from_alias
= nss_ad_map_from_alias
,
1074 static struct nss_info_methods nss_sfu_methods
= {
1075 .init
= nss_sfu_init
,
1076 .get_nss_info
= nss_ad_get_info
,
1077 .map_to_alias
= nss_ad_map_to_alias
,
1078 .map_from_alias
= nss_ad_map_from_alias
,
1081 static struct nss_info_methods nss_sfu20_methods
= {
1082 .init
= nss_sfu20_init
,
1083 .get_nss_info
= nss_ad_get_info
,
1084 .map_to_alias
= nss_ad_map_to_alias
,
1085 .map_from_alias
= nss_ad_map_from_alias
,
1090 /************************************************************************
1091 Initialize the plugins
1092 ***********************************************************************/
1094 NTSTATUS
samba_init_module(void)
1096 static NTSTATUS status_idmap_ad
= NT_STATUS_UNSUCCESSFUL
;
1097 static NTSTATUS status_nss_rfc2307
= NT_STATUS_UNSUCCESSFUL
;
1098 static NTSTATUS status_nss_sfu
= NT_STATUS_UNSUCCESSFUL
;
1099 static NTSTATUS status_nss_sfu20
= NT_STATUS_UNSUCCESSFUL
;
1101 /* Always register the AD method first in order to get the
1102 idmap_domain interface called */
1104 if ( !NT_STATUS_IS_OK(status_idmap_ad
) ) {
1105 status_idmap_ad
= smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
1107 if ( !NT_STATUS_IS_OK(status_idmap_ad
) )
1108 return status_idmap_ad
;
1111 if ( !NT_STATUS_IS_OK( status_nss_rfc2307
) ) {
1112 status_nss_rfc2307
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1113 "rfc2307", &nss_rfc2307_methods
);
1114 if ( !NT_STATUS_IS_OK(status_nss_rfc2307
) )
1115 return status_nss_rfc2307
;
1118 if ( !NT_STATUS_IS_OK( status_nss_sfu
) ) {
1119 status_nss_sfu
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1120 "sfu", &nss_sfu_methods
);
1121 if ( !NT_STATUS_IS_OK(status_nss_sfu
) )
1122 return status_nss_sfu
;
1125 if ( !NT_STATUS_IS_OK( status_nss_sfu20
) ) {
1126 status_nss_sfu20
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1127 "sfu20", &nss_sfu20_methods
);
1128 if ( !NT_STATUS_IS_OK(status_nss_sfu20
) )
1129 return status_nss_sfu20
;
1132 return NT_STATUS_OK
;