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
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"
33 #define DBGC_CLASS DBGC_IDMAP
35 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
37 #define IDMAP_AD_MAX_IDS 30
38 #define CHECK_ALLOC_DONE(mem) do { \
40 DEBUG(0, ("Out of memory!\n")); \
41 ret = NT_STATUS_NO_MEMORY; \
46 struct idmap_ad_context
{
47 uint32_t filter_low_id
;
48 uint32_t filter_high_id
;
50 struct posix_schema
*ad_schema
;
51 enum wb_posix_mapping ad_map_type
; /* WB_POSIX_MAP_UNKNOWN */
54 NTSTATUS
init_module(void);
56 /************************************************************************
57 ***********************************************************************/
59 static ADS_STATUS
ad_idmap_cached_connection_internal(struct idmap_domain
*dom
)
65 struct sockaddr_storage dc_ip
;
66 struct idmap_ad_context
*ctx
;
67 char *ldap_server
= NULL
;
69 struct winbindd_domain
*wb_dom
;
71 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
74 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
76 if (ctx
->ads
!= NULL
) {
79 time_t now
= time(NULL
);
83 expire
= MIN(ads
->auth
.tgt_expire
, ads
->auth
.tgs_expire
);
85 /* check for a valid structure */
86 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
87 (uint32
)expire
-(uint32
)now
, (uint32
) expire
, (uint32
) now
));
89 if ( ads
->config
.realm
&& (expire
> time(NULL
))) {
92 /* we own this ADS_STRUCT so make sure it goes away */
93 DEBUG(7,("Deleting expired krb5 credential cache\n"));
96 ads_kdestroy(WINBIND_CCACHE_NAME
);
98 TALLOC_FREE( ctx
->ad_schema
);
103 /* we don't want this to affect the users ccache */
104 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME
, 1);
108 * At this point we only have the NetBIOS domain name.
109 * Check if we can get server nam and realm from SAF cache
110 * and the domain list.
112 ldap_server
= saf_fetch(dom
->name
);
113 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server
?ldap_server
:""));
115 wb_dom
= find_domain_from_name_noinit(dom
->name
);
116 if (wb_dom
== NULL
) {
117 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
121 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
122 " domain '%s'\n", wb_dom
->alt_name
, dom
->name
));
123 realm
= wb_dom
->alt_name
;
126 if ( (ads
= ads_init(realm
, dom
->name
, ldap_server
)) == NULL
) {
127 DEBUG(1,("ads_init failed\n"));
128 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
131 /* the machine acct password might have change - fetch it every time */
132 SAFE_FREE(ads
->auth
.password
);
133 ads
->auth
.password
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
135 SAFE_FREE(ads
->auth
.realm
);
136 ads
->auth
.realm
= SMB_STRDUP(lp_realm());
138 /* setup server affinity */
140 get_dc_name(dom
->name
, realm
, dc_name
, &dc_ip
);
142 status
= ads_connect(ads
);
143 if (!ADS_ERR_OK(status
)) {
144 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
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(NULL
, ctx
->ads
, ctx
->ad_map_type
, &ctx
->ad_schema
);
185 if ( !ADS_ERR_OK(status
) ) {
186 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
193 /************************************************************************
194 ***********************************************************************/
196 static NTSTATUS
idmap_ad_initialize(struct idmap_domain
*dom
,
199 struct idmap_ad_context
*ctx
;
201 const char *range
= NULL
;
202 const char *schema_mode
= NULL
;
204 if ( (ctx
= TALLOC_ZERO_P(dom
, struct idmap_ad_context
)) == NULL
) {
205 DEBUG(0, ("Out of memory!\n"));
206 return NT_STATUS_NO_MEMORY
;
209 if ( (config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
)) == NULL
) {
210 DEBUG(0, ("Out of memory!\n"));
212 return NT_STATUS_NO_MEMORY
;
216 range
= lp_parm_const_string(-1, config_option
, "range", NULL
);
217 if (range
&& range
[0]) {
218 if ((sscanf(range
, "%u - %u", &ctx
->filter_low_id
, &ctx
->filter_high_id
) != 2) ||
219 (ctx
->filter_low_id
> ctx
->filter_high_id
)) {
220 DEBUG(1, ("ERROR: invalid filter range [%s]", range
));
221 ctx
->filter_low_id
= 0;
222 ctx
->filter_high_id
= 0;
226 /* default map type */
227 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
230 schema_mode
= lp_parm_const_string(-1, config_option
, "schema_mode", NULL
);
231 if ( schema_mode
&& schema_mode
[0] ) {
232 if ( strequal(schema_mode
, "sfu") )
233 ctx
->ad_map_type
= WB_POSIX_MAP_SFU
;
234 else if ( strequal(schema_mode
, "sfu20" ) )
235 ctx
->ad_map_type
= WB_POSIX_MAP_SFU20
;
236 else if ( strequal(schema_mode
, "rfc2307" ) )
237 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
239 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
243 dom
->private_data
= ctx
;
245 talloc_free(config_option
);
250 /************************************************************************
251 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
252 ***********************************************************************/
254 static struct id_map
*find_map_by_id(struct id_map
**maps
, enum id_type type
, uint32_t id
)
258 for (i
= 0; maps
[i
] && i
<IDMAP_AD_MAX_IDS
; i
++) {
259 if ((maps
[i
]->xid
.type
== type
) && (maps
[i
]->xid
.id
== id
)) {
267 /************************************************************************
268 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
269 ***********************************************************************/
271 static struct id_map
*find_map_by_sid(struct id_map
**maps
, struct dom_sid
*sid
)
275 for (i
= 0; maps
[i
] && i
<IDMAP_AD_MAX_IDS
; i
++) {
276 if (sid_equal(maps
[i
]->sid
, sid
)) {
284 /************************************************************************
285 ***********************************************************************/
287 static NTSTATUS
idmap_ad_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
291 struct idmap_ad_context
*ctx
;
293 const char *attrs
[] = { "sAMAccountType",
295 NULL
, /* uidnumber */
296 NULL
, /* gidnumber */
298 LDAPMessage
*res
= NULL
;
299 LDAPMessage
*entry
= NULL
;
305 char *u_filter
= NULL
;
306 char *g_filter
= NULL
;
308 /* initialize the status to avoid suprise */
309 for (i
= 0; ids
[i
]; i
++) {
310 ids
[i
]->status
= ID_UNKNOWN
;
313 /* Only do query if we are online */
314 if (idmap_is_offline()) {
315 return NT_STATUS_FILE_IS_OFFLINE
;
318 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
320 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
321 DEBUG(0, ("Out of memory!\n"));
322 return NT_STATUS_NO_MEMORY
;
325 rc
= ad_idmap_cached_connection(dom
);
326 if (!ADS_ERR_OK(rc
)) {
327 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
328 ret
= NT_STATUS_UNSUCCESSFUL
;
329 /* ret = ads_ntstatus(rc); */
333 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
334 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
338 for (i
= 0; (i
< IDMAP_AD_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
339 switch (ids
[idx
]->xid
.type
) {
342 u_filter
= talloc_asprintf(memctx
, "(&(|"
343 "(sAMAccountType=%d)"
344 "(sAMAccountType=%d)"
345 "(sAMAccountType=%d))(|",
346 ATYPE_NORMAL_ACCOUNT
,
347 ATYPE_WORKSTATION_TRUST
,
348 ATYPE_INTERDOMAIN_TRUST
);
350 u_filter
= talloc_asprintf_append_buffer(u_filter
, "(%s=%lu)",
351 ctx
->ad_schema
->posix_uidnumber_attr
,
352 (unsigned long)ids
[idx
]->xid
.id
);
353 CHECK_ALLOC_DONE(u_filter
);
358 g_filter
= talloc_asprintf(memctx
, "(&(|"
359 "(sAMAccountType=%d)"
360 "(sAMAccountType=%d))(|",
361 ATYPE_SECURITY_GLOBAL_GROUP
,
362 ATYPE_SECURITY_LOCAL_GROUP
);
364 g_filter
= talloc_asprintf_append_buffer(g_filter
, "(%s=%lu)",
365 ctx
->ad_schema
->posix_gidnumber_attr
,
366 (unsigned long)ids
[idx
]->xid
.id
);
367 CHECK_ALLOC_DONE(g_filter
);
371 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
372 ids
[idx
]->status
= ID_UNKNOWN
;
376 filter
= talloc_asprintf(memctx
, "(|");
377 CHECK_ALLOC_DONE(filter
);
379 filter
= talloc_asprintf_append_buffer(filter
, "%s))", u_filter
);
380 CHECK_ALLOC_DONE(filter
);
381 TALLOC_FREE(u_filter
);
384 filter
= talloc_asprintf_append_buffer(filter
, "%s))", g_filter
);
385 CHECK_ALLOC_DONE(filter
);
386 TALLOC_FREE(g_filter
);
388 filter
= talloc_asprintf_append_buffer(filter
, ")");
389 CHECK_ALLOC_DONE(filter
);
391 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
392 if (!ADS_ERR_OK(rc
)) {
393 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
394 ret
= NT_STATUS_UNSUCCESSFUL
;
398 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
399 DEBUG(10, ("No IDs found\n"));
403 for (i
= 0; (i
< count
) && entry
; i
++) {
410 if (i
== 0) { /* first entry */
411 entry
= ads_first_entry(ctx
->ads
, entry
);
412 } else { /* following ones */
413 entry
= ads_next_entry(ctx
->ads
, entry
);
417 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
421 /* first check if the SID is present */
422 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
423 DEBUG(2, ("Could not retrieve SID from entry\n"));
428 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
429 DEBUG(1, ("could not get SAM account type\n"));
433 switch (atype
& 0xF0000000) {
434 case ATYPE_SECURITY_GLOBAL_GROUP
:
435 case ATYPE_SECURITY_LOCAL_GROUP
:
438 case ATYPE_NORMAL_ACCOUNT
:
439 case ATYPE_WORKSTATION_TRUST
:
440 case ATYPE_INTERDOMAIN_TRUST
:
444 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
448 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
449 ctx
->ad_schema
->posix_uidnumber_attr
:
450 ctx
->ad_schema
->posix_gidnumber_attr
,
453 DEBUG(1, ("Could not get unix ID\n"));
458 (ctx
->filter_low_id
&& (id
< ctx
->filter_low_id
)) ||
459 (ctx
->filter_high_id
&& (id
> ctx
->filter_high_id
))) {
460 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
461 id
, ctx
->filter_low_id
, ctx
->filter_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
= sid_binstring(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"));
656 (ctx
->filter_low_id
&& (id
< ctx
->filter_low_id
)) ||
657 (ctx
->filter_high_id
&& (id
> ctx
->filter_high_id
))) {
658 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
659 id
, ctx
->filter_low_id
, ctx
->filter_high_id
));
664 map
->xid
.type
= type
;
666 map
->status
= ID_MAPPED
;
668 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
669 (unsigned long)map
->xid
.id
,
674 ads_msgfree(ctx
->ads
, res
);
677 if (ids
[idx
]) { /* still some values to map */
683 /* mark all unknwoni/expired ones as unmapped */
684 for (i
= 0; ids
[i
]; i
++) {
685 if (ids
[i
]->status
!= ID_MAPPED
)
686 ids
[i
]->status
= ID_UNMAPPED
;
694 /************************************************************************
695 ***********************************************************************/
697 static NTSTATUS
idmap_ad_close(struct idmap_domain
*dom
)
699 struct idmap_ad_context
* ctx
;
701 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
703 if (ctx
->ads
!= NULL
) {
704 /* we own this ADS_STRUCT so make sure it goes away */
705 ctx
->ads
->is_mine
= True
;
706 ads_destroy( &ctx
->ads
);
710 TALLOC_FREE( ctx
->ad_schema
);
716 * nss_info_{sfu,sfu20,rfc2307}
719 /************************************************************************
720 Initialize the {sfu,sfu20,rfc2307} state
721 ***********************************************************************/
723 static const char *wb_posix_map_unknown_string
= "WB_POSIX_MAP_UNKNOWN";
724 static const char *wb_posix_map_template_string
= "WB_POSIX_MAP_TEMPLATE";
725 static const char *wb_posix_map_sfu_string
= "WB_POSIX_MAP_SFU";
726 static const char *wb_posix_map_sfu20_string
= "WB_POSIX_MAP_SFU20";
727 static const char *wb_posix_map_rfc2307_string
= "WB_POSIX_MAP_RFC2307";
728 static const char *wb_posix_map_unixinfo_string
= "WB_POSIX_MAP_UNIXINFO";
730 static const char *ad_map_type_string(enum wb_posix_mapping map_type
)
733 case WB_POSIX_MAP_TEMPLATE
:
734 return wb_posix_map_template_string
;
735 case WB_POSIX_MAP_SFU
:
736 return wb_posix_map_sfu_string
;
737 case WB_POSIX_MAP_SFU20
:
738 return wb_posix_map_sfu20_string
;
739 case WB_POSIX_MAP_RFC2307
:
740 return wb_posix_map_rfc2307_string
;
741 case WB_POSIX_MAP_UNIXINFO
:
742 return wb_posix_map_unixinfo_string
;
744 return wb_posix_map_unknown_string
;
748 static NTSTATUS
nss_ad_generic_init(struct nss_domain_entry
*e
,
749 enum wb_posix_mapping new_ad_map_type
)
751 struct idmap_domain
*dom
;
752 struct idmap_ad_context
*ctx
;
754 if (e
->state
!= NULL
) {
755 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
757 dom
= TALLOC_ZERO_P(e
, struct idmap_domain
);
759 DEBUG(0, ("Out of memory!\n"));
760 return NT_STATUS_NO_MEMORY
;
765 if (e
->domain
!= NULL
) {
766 dom
->name
= talloc_strdup(dom
, e
->domain
);
767 if (dom
->name
== NULL
) {
768 DEBUG(0, ("Out of memory!\n"));
769 return NT_STATUS_NO_MEMORY
;
773 if (dom
->private_data
!= NULL
) {
774 ctx
= talloc_get_type(dom
->private_data
,
775 struct idmap_ad_context
);
777 ctx
= TALLOC_ZERO_P(dom
, struct idmap_ad_context
);
779 DEBUG(0, ("Out of memory!\n"));
780 return NT_STATUS_NO_MEMORY
;
782 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
783 dom
->private_data
= ctx
;
786 if ((ctx
->ad_map_type
!= WB_POSIX_MAP_UNKNOWN
) &&
787 (ctx
->ad_map_type
!= new_ad_map_type
))
789 DEBUG(2, ("nss_ad_generic_init: "
790 "Warning: overriding previously set posix map type "
791 "%s for domain %s with map type %s.\n",
792 ad_map_type_string(ctx
->ad_map_type
),
794 ad_map_type_string(new_ad_map_type
)));
797 ctx
->ad_map_type
= new_ad_map_type
;
802 static NTSTATUS
nss_sfu_init( struct nss_domain_entry
*e
)
804 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU
);
807 static NTSTATUS
nss_sfu20_init( struct nss_domain_entry
*e
)
809 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU20
);
812 static NTSTATUS
nss_rfc2307_init( struct nss_domain_entry
*e
)
814 return nss_ad_generic_init(e
, WB_POSIX_MAP_RFC2307
);
818 /************************************************************************
819 ***********************************************************************/
821 static NTSTATUS
nss_ad_get_info( struct nss_domain_entry
*e
,
822 const struct dom_sid
*sid
,
826 const char **homedir
,
831 const char *attrs
[] = {NULL
, /* attr_homedir */
832 NULL
, /* attr_shell */
833 NULL
, /* attr_gecos */
834 NULL
, /* attr_gidnumber */
837 LDAPMessage
*msg_internal
= NULL
;
838 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
839 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
841 struct idmap_domain
*dom
;
842 struct idmap_ad_context
*ctx
;
844 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
845 sid_string_dbg(sid
), e
->domain
?e
->domain
:"NULL"));
847 /* Only do query if we are online */
848 if (idmap_is_offline()) {
849 return NT_STATUS_FILE_IS_OFFLINE
;
852 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
853 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
855 ads_status
= ad_idmap_cached_connection(dom
);
856 if (!ADS_ERR_OK(ads_status
)) {
857 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
860 if (!ctx
->ad_schema
) {
861 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
862 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
865 if (!sid
|| !homedir
|| !shell
|| !gecos
) {
866 return NT_STATUS_INVALID_PARAMETER
;
869 /* See if we can use the ADS connection struct swe were given */
872 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
873 "LDAP message (%p)\n", msg
));
875 *homedir
= ads_pull_string( ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_homedir_attr
);
876 *shell
= ads_pull_string( ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_shell_attr
);
877 *gecos
= ads_pull_string( ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_gecos_attr
);
880 if ( !ads_pull_uint32(ads
, msg
, ctx
->ad_schema
->posix_gidnumber_attr
, gid
) )
884 nt_status
= NT_STATUS_OK
;
888 /* Have to do our own query */
890 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
893 attrs
[0] = ctx
->ad_schema
->posix_homedir_attr
;
894 attrs
[1] = ctx
->ad_schema
->posix_shell_attr
;
895 attrs
[2] = ctx
->ad_schema
->posix_gecos_attr
;
896 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
898 sidstr
= sid_binstring(mem_ctx
, sid
);
899 filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)", sidstr
);
903 nt_status
= NT_STATUS_NO_MEMORY
;
907 ads_status
= ads_search_retry(ctx
->ads
, &msg_internal
, filter
, attrs
);
908 if (!ADS_ERR_OK(ads_status
)) {
909 nt_status
= ads_ntstatus(ads_status
);
913 *homedir
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_homedir_attr
);
914 *shell
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_shell_attr
);
915 *gecos
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_gecos_attr
);
918 if (!ads_pull_uint32(ctx
->ads
, msg_internal
, ctx
->ad_schema
->posix_gidnumber_attr
, gid
))
922 nt_status
= NT_STATUS_OK
;
926 ads_msgfree(ctx
->ads
, msg_internal
);
932 /**********************************************************************
933 *********************************************************************/
935 static NTSTATUS
nss_ad_map_to_alias(TALLOC_CTX
*mem_ctx
,
936 struct nss_domain_entry
*e
,
940 const char *attrs
[] = {NULL
, /* attr_uid */
943 LDAPMessage
*msg
= NULL
;
944 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
945 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
946 struct idmap_domain
*dom
;
947 struct idmap_ad_context
*ctx
= NULL
;
949 /* Check incoming parameters */
951 if ( !e
|| !e
->domain
|| !name
|| !*alias
) {
952 nt_status
= NT_STATUS_INVALID_PARAMETER
;
956 /* Only do query if we are online */
958 if (idmap_is_offline()) {
959 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
963 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
964 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
966 ads_status
= ad_idmap_cached_connection(dom
);
967 if (!ADS_ERR_OK(ads_status
)) {
968 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
971 if (!ctx
->ad_schema
) {
972 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
976 attrs
[0] = ctx
->ad_schema
->posix_uid_attr
;
978 filter
= talloc_asprintf(mem_ctx
,
979 "(sAMAccountName=%s)",
982 nt_status
= NT_STATUS_NO_MEMORY
;
986 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
987 if (!ADS_ERR_OK(ads_status
)) {
988 nt_status
= ads_ntstatus(ads_status
);
992 *alias
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_uid_attr
);
995 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
998 nt_status
= NT_STATUS_OK
;
1002 talloc_destroy(filter
);
1005 ads_msgfree(ctx
->ads
, msg
);
1011 /**********************************************************************
1012 *********************************************************************/
1014 static NTSTATUS
nss_ad_map_from_alias( TALLOC_CTX
*mem_ctx
,
1015 struct nss_domain_entry
*e
,
1019 const char *attrs
[] = {"sAMAccountName",
1021 char *filter
= NULL
;
1022 LDAPMessage
*msg
= NULL
;
1023 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
1024 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1026 struct idmap_domain
*dom
;
1027 struct idmap_ad_context
*ctx
= NULL
;
1029 /* Check incoming parameters */
1031 if ( !alias
|| !name
) {
1032 nt_status
= NT_STATUS_INVALID_PARAMETER
;
1036 /* Only do query if we are online */
1038 if (idmap_is_offline()) {
1039 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
1043 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
1044 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
1046 ads_status
= ad_idmap_cached_connection(dom
);
1047 if (!ADS_ERR_OK(ads_status
)) {
1048 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1051 if (!ctx
->ad_schema
) {
1052 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
1056 filter
= talloc_asprintf(mem_ctx
,
1058 ctx
->ad_schema
->posix_uid_attr
,
1061 nt_status
= NT_STATUS_NO_MEMORY
;
1065 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
1066 if (!ADS_ERR_OK(ads_status
)) {
1067 nt_status
= ads_ntstatus(ads_status
);
1071 username
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
,
1074 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1077 *name
= talloc_asprintf(mem_ctx
, "%s\\%s",
1081 nt_status
= NT_STATUS_NO_MEMORY
;
1085 nt_status
= NT_STATUS_OK
;
1089 talloc_destroy(filter
);
1092 ads_msgfree(ctx
->ads
, msg
);
1099 /************************************************************************
1100 ***********************************************************************/
1102 static NTSTATUS
nss_ad_close( void )
1104 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1106 return NT_STATUS_OK
;
1109 /************************************************************************
1110 Function dispatch tables for the idmap and nss plugins
1111 ***********************************************************************/
1113 static struct idmap_methods ad_methods
= {
1114 .init
= idmap_ad_initialize
,
1115 .unixids_to_sids
= idmap_ad_unixids_to_sids
,
1116 .sids_to_unixids
= idmap_ad_sids_to_unixids
,
1117 .close_fn
= idmap_ad_close
1120 /* The SFU and RFC2307 NSS plugins share everything but the init
1121 function which sets the intended schema model to use */
1123 static struct nss_info_methods nss_rfc2307_methods
= {
1124 .init
= nss_rfc2307_init
,
1125 .get_nss_info
= nss_ad_get_info
,
1126 .map_to_alias
= nss_ad_map_to_alias
,
1127 .map_from_alias
= nss_ad_map_from_alias
,
1128 .close_fn
= nss_ad_close
1131 static struct nss_info_methods nss_sfu_methods
= {
1132 .init
= nss_sfu_init
,
1133 .get_nss_info
= nss_ad_get_info
,
1134 .map_to_alias
= nss_ad_map_to_alias
,
1135 .map_from_alias
= nss_ad_map_from_alias
,
1136 .close_fn
= nss_ad_close
1139 static struct nss_info_methods nss_sfu20_methods
= {
1140 .init
= nss_sfu20_init
,
1141 .get_nss_info
= nss_ad_get_info
,
1142 .map_to_alias
= nss_ad_map_to_alias
,
1143 .map_from_alias
= nss_ad_map_from_alias
,
1144 .close_fn
= nss_ad_close
1149 /************************************************************************
1150 Initialize the plugins
1151 ***********************************************************************/
1153 NTSTATUS
idmap_ad_init(void)
1155 static NTSTATUS status_idmap_ad
= NT_STATUS_UNSUCCESSFUL
;
1156 static NTSTATUS status_nss_rfc2307
= NT_STATUS_UNSUCCESSFUL
;
1157 static NTSTATUS status_nss_sfu
= NT_STATUS_UNSUCCESSFUL
;
1158 static NTSTATUS status_nss_sfu20
= NT_STATUS_UNSUCCESSFUL
;
1160 /* Always register the AD method first in order to get the
1161 idmap_domain interface called */
1163 if ( !NT_STATUS_IS_OK(status_idmap_ad
) ) {
1164 status_idmap_ad
= smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
1166 if ( !NT_STATUS_IS_OK(status_idmap_ad
) )
1167 return status_idmap_ad
;
1170 if ( !NT_STATUS_IS_OK( status_nss_rfc2307
) ) {
1171 status_nss_rfc2307
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1172 "rfc2307", &nss_rfc2307_methods
);
1173 if ( !NT_STATUS_IS_OK(status_nss_rfc2307
) )
1174 return status_nss_rfc2307
;
1177 if ( !NT_STATUS_IS_OK( status_nss_sfu
) ) {
1178 status_nss_sfu
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1179 "sfu", &nss_sfu_methods
);
1180 if ( !NT_STATUS_IS_OK(status_nss_sfu
) )
1181 return status_nss_sfu
;
1184 if ( !NT_STATUS_IS_OK( status_nss_sfu20
) ) {
1185 status_nss_sfu20
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1186 "sfu20", &nss_sfu20_methods
);
1187 if ( !NT_STATUS_IS_OK(status_nss_sfu20
) )
1188 return status_nss_sfu20
;
1191 return NT_STATUS_OK
;