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 CHECK_ALLOC_DONE(mem) do { \
46 DEBUG(0, ("Out of memory!\n")); \
47 ret = NT_STATUS_NO_MEMORY; \
52 struct idmap_ad_context
{
54 struct posix_schema
*ad_schema
;
55 enum wb_posix_mapping ad_map_type
; /* WB_POSIX_MAP_UNKNOWN */
58 /************************************************************************
59 ***********************************************************************/
61 static ADS_STATUS
ad_idmap_cached_connection_internal(struct idmap_domain
*dom
)
66 struct sockaddr_storage dc_ip
;
67 struct idmap_ad_context
*ctx
;
68 char *ldap_server
= NULL
;
70 struct winbindd_domain
*wb_dom
;
72 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
75 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
77 if (ctx
->ads
!= NULL
) {
80 time_t now
= time(NULL
);
84 expire
= MIN(ads
->auth
.tgt_expire
, ads
->auth
.tgs_expire
);
86 /* check for a valid structure */
87 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
88 (uint32
)expire
-(uint32
)now
, (uint32
) expire
, (uint32
) now
));
90 if ( ads
->config
.realm
&& (expire
> time(NULL
))) {
93 /* we own this ADS_STRUCT so make sure it goes away */
94 DEBUG(7,("Deleting expired krb5 credential cache\n"));
97 ads_kdestroy(WINBIND_CCACHE_NAME
);
102 /* we don't want this to affect the users ccache */
103 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME
, 1);
106 * At this point we only have the NetBIOS domain name.
107 * Check if we can get server nam and realm from SAF cache
108 * and the domain list.
110 ldap_server
= saf_fetch(dom
->name
);
111 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server
?ldap_server
:""));
113 wb_dom
= find_domain_from_name_noinit(dom
->name
);
114 if (wb_dom
== NULL
) {
115 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
119 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
120 " domain '%s'\n", wb_dom
->alt_name
, dom
->name
));
121 realm
= wb_dom
->alt_name
;
124 if ( (ads
= ads_init(realm
, dom
->name
, ldap_server
)) == NULL
) {
125 DEBUG(1,("ads_init failed\n"));
126 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
129 /* the machine acct password might have change - fetch it every time */
130 SAFE_FREE(ads
->auth
.password
);
131 ads
->auth
.password
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
133 SAFE_FREE(ads
->auth
.realm
);
134 ads
->auth
.realm
= SMB_STRDUP(lp_realm());
136 /* setup server affinity */
138 get_dc_name(dom
->name
, realm
, dc_name
, &dc_ip
);
140 status
= ads_connect(ads
);
141 if (!ADS_ERR_OK(status
)) {
142 DEBUG(1, ("ad_idmap_cached_connection_internal: failed to "
148 ads
->is_mine
= False
;
155 /************************************************************************
156 ***********************************************************************/
158 static ADS_STATUS
ad_idmap_cached_connection(struct idmap_domain
*dom
)
161 struct idmap_ad_context
* ctx
;
163 status
= ad_idmap_cached_connection_internal(dom
);
164 if (!ADS_ERR_OK(status
)) {
168 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
170 /* if we have a valid ADS_STRUCT and the schema model is
171 defined, then we can return here. */
173 if ( ctx
->ad_schema
) {
177 /* Otherwise, set the schema model */
179 if ( (ctx
->ad_map_type
== WB_POSIX_MAP_SFU
) ||
180 (ctx
->ad_map_type
== WB_POSIX_MAP_SFU20
) ||
181 (ctx
->ad_map_type
== WB_POSIX_MAP_RFC2307
) )
183 status
= ads_check_posix_schema_mapping(
184 ctx
, 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 static int idmap_ad_context_destructor(struct idmap_ad_context
*ctx
)
195 if (ctx
->ads
!= NULL
) {
196 /* we own this ADS_STRUCT so make sure it goes away */
197 ctx
->ads
->is_mine
= True
;
198 ads_destroy( &ctx
->ads
);
204 /************************************************************************
205 ***********************************************************************/
207 static NTSTATUS
idmap_ad_initialize(struct idmap_domain
*dom
)
209 struct idmap_ad_context
*ctx
;
211 const char *schema_mode
= NULL
;
213 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
215 DEBUG(0, ("Out of memory!\n"));
216 return NT_STATUS_NO_MEMORY
;
218 talloc_set_destructor(ctx
, idmap_ad_context_destructor
);
220 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
221 if (config_option
== NULL
) {
222 DEBUG(0, ("Out of memory!\n"));
224 return NT_STATUS_NO_MEMORY
;
227 /* default map type */
228 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
231 schema_mode
= lp_parm_const_string(-1, config_option
, "schema_mode", NULL
);
232 if ( schema_mode
&& schema_mode
[0] ) {
233 if ( strequal(schema_mode
, "sfu") )
234 ctx
->ad_map_type
= WB_POSIX_MAP_SFU
;
235 else if ( strequal(schema_mode
, "sfu20" ) )
236 ctx
->ad_map_type
= WB_POSIX_MAP_SFU20
;
237 else if ( strequal(schema_mode
, "rfc2307" ) )
238 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
240 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
244 dom
->private_data
= ctx
;
246 talloc_free(config_option
);
251 /************************************************************************
252 ***********************************************************************/
254 static NTSTATUS
idmap_ad_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
258 struct idmap_ad_context
*ctx
;
260 const char *attrs
[] = { "sAMAccountType",
262 NULL
, /* uidnumber */
263 NULL
, /* gidnumber */
265 LDAPMessage
*res
= NULL
;
266 LDAPMessage
*entry
= NULL
;
272 char *u_filter
= NULL
;
273 char *g_filter
= NULL
;
275 /* initialize the status to avoid suprise */
276 for (i
= 0; ids
[i
]; i
++) {
277 ids
[i
]->status
= ID_UNKNOWN
;
280 /* Only do query if we are online */
281 if (idmap_is_offline()) {
282 return NT_STATUS_FILE_IS_OFFLINE
;
285 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
287 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
288 DEBUG(0, ("Out of memory!\n"));
289 return NT_STATUS_NO_MEMORY
;
292 rc
= ad_idmap_cached_connection(dom
);
293 if (!ADS_ERR_OK(rc
)) {
294 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
295 ret
= NT_STATUS_UNSUCCESSFUL
;
296 /* ret = ads_ntstatus(rc); */
300 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
301 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
305 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
306 switch (ids
[idx
]->xid
.type
) {
309 u_filter
= talloc_asprintf(memctx
, "(&(|"
310 "(sAMAccountType=%d)"
311 "(sAMAccountType=%d)"
312 "(sAMAccountType=%d))(|",
313 ATYPE_NORMAL_ACCOUNT
,
314 ATYPE_WORKSTATION_TRUST
,
315 ATYPE_INTERDOMAIN_TRUST
);
317 u_filter
= talloc_asprintf_append_buffer(u_filter
, "(%s=%lu)",
318 ctx
->ad_schema
->posix_uidnumber_attr
,
319 (unsigned long)ids
[idx
]->xid
.id
);
320 CHECK_ALLOC_DONE(u_filter
);
325 g_filter
= talloc_asprintf(memctx
, "(&(|"
326 "(sAMAccountType=%d)"
327 "(sAMAccountType=%d))(|",
328 ATYPE_SECURITY_GLOBAL_GROUP
,
329 ATYPE_SECURITY_LOCAL_GROUP
);
331 g_filter
= talloc_asprintf_append_buffer(g_filter
, "(%s=%lu)",
332 ctx
->ad_schema
->posix_gidnumber_attr
,
333 (unsigned long)ids
[idx
]->xid
.id
);
334 CHECK_ALLOC_DONE(g_filter
);
338 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
339 ids
[idx
]->status
= ID_UNKNOWN
;
343 filter
= talloc_asprintf(memctx
, "(|");
344 CHECK_ALLOC_DONE(filter
);
346 filter
= talloc_asprintf_append_buffer(filter
, "%s))", u_filter
);
347 CHECK_ALLOC_DONE(filter
);
348 TALLOC_FREE(u_filter
);
351 filter
= talloc_asprintf_append_buffer(filter
, "%s))", g_filter
);
352 CHECK_ALLOC_DONE(filter
);
353 TALLOC_FREE(g_filter
);
355 filter
= talloc_asprintf_append_buffer(filter
, ")");
356 CHECK_ALLOC_DONE(filter
);
358 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
359 if (!ADS_ERR_OK(rc
)) {
360 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
361 ret
= NT_STATUS_UNSUCCESSFUL
;
365 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
366 DEBUG(10, ("No IDs found\n"));
370 for (i
= 0; (i
< count
) && entry
; i
++) {
377 if (i
== 0) { /* first entry */
378 entry
= ads_first_entry(ctx
->ads
, entry
);
379 } else { /* following ones */
380 entry
= ads_next_entry(ctx
->ads
, entry
);
384 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
388 /* first check if the SID is present */
389 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
390 DEBUG(2, ("Could not retrieve SID from entry\n"));
395 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
396 DEBUG(1, ("could not get SAM account type\n"));
400 switch (atype
& 0xF0000000) {
401 case ATYPE_SECURITY_GLOBAL_GROUP
:
402 case ATYPE_SECURITY_LOCAL_GROUP
:
405 case ATYPE_NORMAL_ACCOUNT
:
406 case ATYPE_WORKSTATION_TRUST
:
407 case ATYPE_INTERDOMAIN_TRUST
:
411 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
415 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
416 ctx
->ad_schema
->posix_uidnumber_attr
:
417 ctx
->ad_schema
->posix_gidnumber_attr
,
420 DEBUG(1, ("Could not get unix ID\n"));
424 if (!idmap_unix_id_is_in_range(id
, dom
)) {
425 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
426 id
, dom
->low_id
, dom
->high_id
));
430 map
= idmap_find_map_by_id(&ids
[bidx
], type
, id
);
432 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
436 sid_copy(map
->sid
, &sid
);
439 map
->status
= ID_MAPPED
;
441 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
442 (unsigned long)map
->xid
.id
,
447 ads_msgfree(ctx
->ads
, res
);
450 if (ids
[idx
]) { /* still some values to map */
456 /* mark all unknown/expired ones as unmapped */
457 for (i
= 0; ids
[i
]; i
++) {
458 if (ids
[i
]->status
!= ID_MAPPED
)
459 ids
[i
]->status
= ID_UNMAPPED
;
467 /************************************************************************
468 ***********************************************************************/
470 static NTSTATUS
idmap_ad_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
474 struct idmap_ad_context
*ctx
;
476 const char *attrs
[] = { "sAMAccountType",
478 NULL
, /* attr_uidnumber */
479 NULL
, /* attr_gidnumber */
481 LDAPMessage
*res
= NULL
;
482 LDAPMessage
*entry
= NULL
;
490 /* initialize the status to avoid suprise */
491 for (i
= 0; ids
[i
]; i
++) {
492 ids
[i
]->status
= ID_UNKNOWN
;
495 /* Only do query if we are online */
496 if (idmap_is_offline()) {
497 return NT_STATUS_FILE_IS_OFFLINE
;
500 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
502 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
503 DEBUG(0, ("Out of memory!\n"));
504 return NT_STATUS_NO_MEMORY
;
507 rc
= ad_idmap_cached_connection(dom
);
508 if (!ADS_ERR_OK(rc
)) {
509 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
510 ret
= NT_STATUS_UNSUCCESSFUL
;
511 /* ret = ads_ntstatus(rc); */
515 if (ctx
->ad_schema
== NULL
) {
516 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
517 ret
= NT_STATUS_UNSUCCESSFUL
;
521 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
522 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
525 filter
= talloc_asprintf(memctx
, "(&(|"
526 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
527 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
529 ATYPE_NORMAL_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
,
530 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
);
532 CHECK_ALLOC_DONE(filter
);
535 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
537 ids
[idx
]->status
= ID_UNKNOWN
;
539 sidstr
= ldap_encode_ndr_dom_sid(talloc_tos(), ids
[idx
]->sid
);
540 filter
= talloc_asprintf_append_buffer(filter
, "(objectSid=%s)", sidstr
);
543 CHECK_ALLOC_DONE(filter
);
545 filter
= talloc_asprintf_append_buffer(filter
, "))");
546 CHECK_ALLOC_DONE(filter
);
547 DEBUG(10, ("Filter: [%s]\n", filter
));
549 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
550 if (!ADS_ERR_OK(rc
)) {
551 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
552 ret
= NT_STATUS_UNSUCCESSFUL
;
556 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
557 DEBUG(10, ("No IDs found\n"));
561 for (i
= 0; (i
< count
) && entry
; i
++) {
568 if (i
== 0) { /* first entry */
569 entry
= ads_first_entry(ctx
->ads
, entry
);
570 } else { /* following ones */
571 entry
= ads_next_entry(ctx
->ads
, entry
);
575 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
579 /* first check if the SID is present */
580 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
581 DEBUG(2, ("Could not retrieve SID from entry\n"));
585 map
= idmap_find_map_by_sid(&ids
[bidx
], &sid
);
587 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
592 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
593 DEBUG(1, ("could not get SAM account type\n"));
597 switch (atype
& 0xF0000000) {
598 case ATYPE_SECURITY_GLOBAL_GROUP
:
599 case ATYPE_SECURITY_LOCAL_GROUP
:
602 case ATYPE_NORMAL_ACCOUNT
:
603 case ATYPE_WORKSTATION_TRUST
:
604 case ATYPE_INTERDOMAIN_TRUST
:
608 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
612 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
613 ctx
->ad_schema
->posix_uidnumber_attr
:
614 ctx
->ad_schema
->posix_gidnumber_attr
,
617 DEBUG(1, ("Could not get unix ID\n"));
620 if (!idmap_unix_id_is_in_range(id
, dom
)) {
621 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
622 id
, dom
->low_id
, dom
->high_id
));
627 map
->xid
.type
= type
;
629 map
->status
= ID_MAPPED
;
631 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
632 (unsigned long)map
->xid
.id
,
637 ads_msgfree(ctx
->ads
, res
);
640 if (ids
[idx
]) { /* still some values to map */
646 /* mark all unknown/expired ones as unmapped */
647 for (i
= 0; ids
[i
]; i
++) {
648 if (ids
[i
]->status
!= ID_MAPPED
)
649 ids
[i
]->status
= ID_UNMAPPED
;
658 * nss_info_{sfu,sfu20,rfc2307}
661 /************************************************************************
662 Initialize the {sfu,sfu20,rfc2307} state
663 ***********************************************************************/
665 static const char *wb_posix_map_unknown_string
= "WB_POSIX_MAP_UNKNOWN";
666 static const char *wb_posix_map_template_string
= "WB_POSIX_MAP_TEMPLATE";
667 static const char *wb_posix_map_sfu_string
= "WB_POSIX_MAP_SFU";
668 static const char *wb_posix_map_sfu20_string
= "WB_POSIX_MAP_SFU20";
669 static const char *wb_posix_map_rfc2307_string
= "WB_POSIX_MAP_RFC2307";
670 static const char *wb_posix_map_unixinfo_string
= "WB_POSIX_MAP_UNIXINFO";
672 static const char *ad_map_type_string(enum wb_posix_mapping map_type
)
675 case WB_POSIX_MAP_TEMPLATE
:
676 return wb_posix_map_template_string
;
677 case WB_POSIX_MAP_SFU
:
678 return wb_posix_map_sfu_string
;
679 case WB_POSIX_MAP_SFU20
:
680 return wb_posix_map_sfu20_string
;
681 case WB_POSIX_MAP_RFC2307
:
682 return wb_posix_map_rfc2307_string
;
683 case WB_POSIX_MAP_UNIXINFO
:
684 return wb_posix_map_unixinfo_string
;
686 return wb_posix_map_unknown_string
;
690 static NTSTATUS
nss_ad_generic_init(struct nss_domain_entry
*e
,
691 enum wb_posix_mapping new_ad_map_type
)
693 struct idmap_domain
*dom
;
694 struct idmap_ad_context
*ctx
;
696 if (e
->state
!= NULL
) {
697 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
699 dom
= talloc_zero(e
, struct idmap_domain
);
701 DEBUG(0, ("Out of memory!\n"));
702 return NT_STATUS_NO_MEMORY
;
707 if (e
->domain
!= NULL
) {
708 dom
->name
= talloc_strdup(dom
, e
->domain
);
709 if (dom
->name
== NULL
) {
710 DEBUG(0, ("Out of memory!\n"));
711 return NT_STATUS_NO_MEMORY
;
715 if (dom
->private_data
!= NULL
) {
716 ctx
= talloc_get_type(dom
->private_data
,
717 struct idmap_ad_context
);
719 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
721 DEBUG(0, ("Out of memory!\n"));
722 return NT_STATUS_NO_MEMORY
;
724 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
725 dom
->private_data
= ctx
;
728 if ((ctx
->ad_map_type
!= WB_POSIX_MAP_UNKNOWN
) &&
729 (ctx
->ad_map_type
!= new_ad_map_type
))
731 DEBUG(2, ("nss_ad_generic_init: "
732 "Warning: overriding previously set posix map type "
733 "%s for domain %s with map type %s.\n",
734 ad_map_type_string(ctx
->ad_map_type
),
736 ad_map_type_string(new_ad_map_type
)));
739 ctx
->ad_map_type
= new_ad_map_type
;
744 static NTSTATUS
nss_sfu_init( struct nss_domain_entry
*e
)
746 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU
);
749 static NTSTATUS
nss_sfu20_init( struct nss_domain_entry
*e
)
751 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU20
);
754 static NTSTATUS
nss_rfc2307_init( struct nss_domain_entry
*e
)
756 return nss_ad_generic_init(e
, WB_POSIX_MAP_RFC2307
);
760 /************************************************************************
761 ***********************************************************************/
763 static NTSTATUS
nss_ad_get_info( struct nss_domain_entry
*e
,
764 const struct dom_sid
*sid
,
766 const char **homedir
,
771 const char *attrs
[] = {NULL
, /* attr_homedir */
772 NULL
, /* attr_shell */
773 NULL
, /* attr_gecos */
774 NULL
, /* attr_gidnumber */
777 LDAPMessage
*msg_internal
= NULL
;
778 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
779 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
781 struct idmap_domain
*dom
;
782 struct idmap_ad_context
*ctx
;
784 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
785 sid_string_dbg(sid
), e
->domain
?e
->domain
:"NULL"));
787 /* Only do query if we are online */
788 if (idmap_is_offline()) {
789 return NT_STATUS_FILE_IS_OFFLINE
;
792 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
793 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
795 ads_status
= ad_idmap_cached_connection(dom
);
796 if (!ADS_ERR_OK(ads_status
)) {
797 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
800 if (!ctx
->ad_schema
) {
801 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
802 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
805 if (!sid
|| !homedir
|| !shell
|| !gecos
) {
806 return NT_STATUS_INVALID_PARAMETER
;
809 /* Have to do our own query */
811 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
814 attrs
[0] = ctx
->ad_schema
->posix_homedir_attr
;
815 attrs
[1] = ctx
->ad_schema
->posix_shell_attr
;
816 attrs
[2] = ctx
->ad_schema
->posix_gecos_attr
;
817 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
819 sidstr
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
820 filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)", sidstr
);
824 nt_status
= NT_STATUS_NO_MEMORY
;
828 ads_status
= ads_search_retry(ctx
->ads
, &msg_internal
, filter
, attrs
);
829 if (!ADS_ERR_OK(ads_status
)) {
830 nt_status
= ads_ntstatus(ads_status
);
834 *homedir
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_homedir_attr
);
835 *shell
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_shell_attr
);
836 *gecos
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_gecos_attr
);
839 if (!ads_pull_uint32(ctx
->ads
, msg_internal
, ctx
->ad_schema
->posix_gidnumber_attr
, gid
))
843 nt_status
= NT_STATUS_OK
;
847 ads_msgfree(ctx
->ads
, msg_internal
);
853 /**********************************************************************
854 *********************************************************************/
856 static NTSTATUS
nss_ad_map_to_alias(TALLOC_CTX
*mem_ctx
,
857 struct nss_domain_entry
*e
,
861 const char *attrs
[] = {NULL
, /* attr_uid */
864 LDAPMessage
*msg
= NULL
;
865 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
866 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
867 struct idmap_domain
*dom
;
868 struct idmap_ad_context
*ctx
= NULL
;
870 /* Check incoming parameters */
872 if ( !e
|| !e
->domain
|| !name
|| !*alias
) {
873 nt_status
= NT_STATUS_INVALID_PARAMETER
;
877 /* Only do query if we are online */
879 if (idmap_is_offline()) {
880 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
884 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
885 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
887 ads_status
= ad_idmap_cached_connection(dom
);
888 if (!ADS_ERR_OK(ads_status
)) {
889 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
892 if (!ctx
->ad_schema
) {
893 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
897 attrs
[0] = ctx
->ad_schema
->posix_uid_attr
;
899 filter
= talloc_asprintf(mem_ctx
,
900 "(sAMAccountName=%s)",
903 nt_status
= NT_STATUS_NO_MEMORY
;
907 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
908 if (!ADS_ERR_OK(ads_status
)) {
909 nt_status
= ads_ntstatus(ads_status
);
913 *alias
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_uid_attr
);
916 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
919 nt_status
= NT_STATUS_OK
;
923 talloc_destroy(filter
);
926 ads_msgfree(ctx
->ads
, msg
);
932 /**********************************************************************
933 *********************************************************************/
935 static NTSTATUS
nss_ad_map_from_alias( TALLOC_CTX
*mem_ctx
,
936 struct nss_domain_entry
*e
,
940 const char *attrs
[] = {"sAMAccountName",
943 LDAPMessage
*msg
= NULL
;
944 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
945 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
947 struct idmap_domain
*dom
;
948 struct idmap_ad_context
*ctx
= NULL
;
950 /* Check incoming parameters */
952 if ( !alias
|| !name
) {
953 nt_status
= NT_STATUS_INVALID_PARAMETER
;
957 /* Only do query if we are online */
959 if (idmap_is_offline()) {
960 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
964 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
965 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
967 ads_status
= ad_idmap_cached_connection(dom
);
968 if (!ADS_ERR_OK(ads_status
)) {
969 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
972 if (!ctx
->ad_schema
) {
973 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
977 filter
= talloc_asprintf(mem_ctx
,
979 ctx
->ad_schema
->posix_uid_attr
,
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 username
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
,
995 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
998 *name
= talloc_asprintf(mem_ctx
, "%s\\%s",
1002 nt_status
= NT_STATUS_NO_MEMORY
;
1006 nt_status
= NT_STATUS_OK
;
1010 talloc_destroy(filter
);
1013 ads_msgfree(ctx
->ads
, msg
);
1019 /************************************************************************
1020 Function dispatch tables for the idmap and nss plugins
1021 ***********************************************************************/
1023 static struct idmap_methods ad_methods
= {
1024 .init
= idmap_ad_initialize
,
1025 .unixids_to_sids
= idmap_ad_unixids_to_sids
,
1026 .sids_to_unixids
= idmap_ad_sids_to_unixids
,
1029 /* The SFU and RFC2307 NSS plugins share everything but the init
1030 function which sets the intended schema model to use */
1032 static struct nss_info_methods nss_rfc2307_methods
= {
1033 .init
= nss_rfc2307_init
,
1034 .get_nss_info
= nss_ad_get_info
,
1035 .map_to_alias
= nss_ad_map_to_alias
,
1036 .map_from_alias
= nss_ad_map_from_alias
,
1039 static struct nss_info_methods nss_sfu_methods
= {
1040 .init
= nss_sfu_init
,
1041 .get_nss_info
= nss_ad_get_info
,
1042 .map_to_alias
= nss_ad_map_to_alias
,
1043 .map_from_alias
= nss_ad_map_from_alias
,
1046 static struct nss_info_methods nss_sfu20_methods
= {
1047 .init
= nss_sfu20_init
,
1048 .get_nss_info
= nss_ad_get_info
,
1049 .map_to_alias
= nss_ad_map_to_alias
,
1050 .map_from_alias
= nss_ad_map_from_alias
,
1055 /************************************************************************
1056 Initialize the plugins
1057 ***********************************************************************/
1059 NTSTATUS
samba_init_module(void)
1061 static NTSTATUS status_idmap_ad
= NT_STATUS_UNSUCCESSFUL
;
1062 static NTSTATUS status_nss_rfc2307
= NT_STATUS_UNSUCCESSFUL
;
1063 static NTSTATUS status_nss_sfu
= NT_STATUS_UNSUCCESSFUL
;
1064 static NTSTATUS status_nss_sfu20
= NT_STATUS_UNSUCCESSFUL
;
1066 /* Always register the AD method first in order to get the
1067 idmap_domain interface called */
1069 if ( !NT_STATUS_IS_OK(status_idmap_ad
) ) {
1070 status_idmap_ad
= smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
1072 if ( !NT_STATUS_IS_OK(status_idmap_ad
) )
1073 return status_idmap_ad
;
1076 if ( !NT_STATUS_IS_OK( status_nss_rfc2307
) ) {
1077 status_nss_rfc2307
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1078 "rfc2307", &nss_rfc2307_methods
);
1079 if ( !NT_STATUS_IS_OK(status_nss_rfc2307
) )
1080 return status_nss_rfc2307
;
1083 if ( !NT_STATUS_IS_OK( status_nss_sfu
) ) {
1084 status_nss_sfu
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1085 "sfu", &nss_sfu_methods
);
1086 if ( !NT_STATUS_IS_OK(status_nss_sfu
) )
1087 return status_nss_sfu
;
1090 if ( !NT_STATUS_IS_OK( status_nss_sfu20
) ) {
1091 status_nss_sfu20
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
1092 "sfu20", &nss_sfu20_methods
);
1093 if ( !NT_STATUS_IS_OK(status_nss_sfu20
) )
1094 return status_nss_sfu20
;
1097 return NT_STATUS_OK
;