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 CHECK_ALLOC_DONE(mem) do { \
44 DEBUG(0, ("Out of memory!\n")); \
45 ret = NT_STATUS_NO_MEMORY; \
50 struct idmap_ad_context
{
52 struct posix_schema
*ad_schema
;
53 enum wb_posix_mapping ad_map_type
; /* WB_POSIX_MAP_UNKNOWN */
56 /************************************************************************
57 ***********************************************************************/
59 static ADS_STATUS
ad_idmap_cached_connection(struct idmap_domain
*dom
)
62 struct idmap_ad_context
* ctx
;
64 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
67 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
69 status
= ads_idmap_cached_connection(&ctx
->ads
, dom
->name
);
70 if (!ADS_ERR_OK(status
)) {
74 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
76 /* if we have a valid ADS_STRUCT and the schema model is
77 defined, then we can return here. */
79 if ( ctx
->ad_schema
) {
83 /* Otherwise, set the schema model */
85 if ( (ctx
->ad_map_type
== WB_POSIX_MAP_SFU
) ||
86 (ctx
->ad_map_type
== WB_POSIX_MAP_SFU20
) ||
87 (ctx
->ad_map_type
== WB_POSIX_MAP_RFC2307
) )
89 status
= ads_check_posix_schema_mapping(
90 ctx
, ctx
->ads
, ctx
->ad_map_type
, &ctx
->ad_schema
);
91 if ( !ADS_ERR_OK(status
) ) {
92 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
99 static int idmap_ad_context_destructor(struct idmap_ad_context
*ctx
)
101 if (ctx
->ads
!= NULL
) {
102 /* we own this ADS_STRUCT so make sure it goes away */
103 ctx
->ads
->is_mine
= True
;
104 ads_destroy( &ctx
->ads
);
110 /************************************************************************
111 ***********************************************************************/
113 static NTSTATUS
idmap_ad_initialize(struct idmap_domain
*dom
)
115 struct idmap_ad_context
*ctx
;
117 const char *schema_mode
= NULL
;
119 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
121 DEBUG(0, ("Out of memory!\n"));
122 return NT_STATUS_NO_MEMORY
;
124 talloc_set_destructor(ctx
, idmap_ad_context_destructor
);
126 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
127 if (config_option
== NULL
) {
128 DEBUG(0, ("Out of memory!\n"));
130 return NT_STATUS_NO_MEMORY
;
133 /* default map type */
134 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
137 schema_mode
= lp_parm_const_string(-1, config_option
, "schema_mode", NULL
);
138 if ( schema_mode
&& schema_mode
[0] ) {
139 if ( strequal(schema_mode
, "sfu") )
140 ctx
->ad_map_type
= WB_POSIX_MAP_SFU
;
141 else if ( strequal(schema_mode
, "sfu20" ) )
142 ctx
->ad_map_type
= WB_POSIX_MAP_SFU20
;
143 else if ( strequal(schema_mode
, "rfc2307" ) )
144 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
146 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
150 dom
->private_data
= ctx
;
152 talloc_free(config_option
);
157 /************************************************************************
158 ***********************************************************************/
160 static NTSTATUS
idmap_ad_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
164 struct idmap_ad_context
*ctx
;
166 const char *attrs
[] = { "sAMAccountType",
168 NULL
, /* uidnumber */
169 NULL
, /* gidnumber */
171 LDAPMessage
*res
= NULL
;
172 LDAPMessage
*entry
= NULL
;
178 char *u_filter
= NULL
;
179 char *g_filter
= NULL
;
181 /* initialize the status to avoid suprise */
182 for (i
= 0; ids
[i
]; i
++) {
183 ids
[i
]->status
= ID_UNKNOWN
;
186 /* Only do query if we are online */
187 if (idmap_is_offline()) {
188 return NT_STATUS_FILE_IS_OFFLINE
;
191 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
193 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
194 DEBUG(0, ("Out of memory!\n"));
195 return NT_STATUS_NO_MEMORY
;
198 rc
= ad_idmap_cached_connection(dom
);
199 if (!ADS_ERR_OK(rc
)) {
200 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
201 ret
= NT_STATUS_UNSUCCESSFUL
;
202 /* ret = ads_ntstatus(rc); */
206 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
207 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
211 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
212 switch (ids
[idx
]->xid
.type
) {
215 u_filter
= talloc_asprintf(memctx
, "(&(|"
216 "(sAMAccountType=%d)"
217 "(sAMAccountType=%d)"
218 "(sAMAccountType=%d))(|",
219 ATYPE_NORMAL_ACCOUNT
,
220 ATYPE_WORKSTATION_TRUST
,
221 ATYPE_INTERDOMAIN_TRUST
);
223 u_filter
= talloc_asprintf_append_buffer(u_filter
, "(%s=%lu)",
224 ctx
->ad_schema
->posix_uidnumber_attr
,
225 (unsigned long)ids
[idx
]->xid
.id
);
226 CHECK_ALLOC_DONE(u_filter
);
231 g_filter
= talloc_asprintf(memctx
, "(&(|"
232 "(sAMAccountType=%d)"
233 "(sAMAccountType=%d))(|",
234 ATYPE_SECURITY_GLOBAL_GROUP
,
235 ATYPE_SECURITY_LOCAL_GROUP
);
237 g_filter
= talloc_asprintf_append_buffer(g_filter
, "(%s=%lu)",
238 ctx
->ad_schema
->posix_gidnumber_attr
,
239 (unsigned long)ids
[idx
]->xid
.id
);
240 CHECK_ALLOC_DONE(g_filter
);
244 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
245 ids
[idx
]->status
= ID_UNKNOWN
;
249 filter
= talloc_asprintf(memctx
, "(|");
250 CHECK_ALLOC_DONE(filter
);
252 filter
= talloc_asprintf_append_buffer(filter
, "%s))", u_filter
);
253 CHECK_ALLOC_DONE(filter
);
254 TALLOC_FREE(u_filter
);
257 filter
= talloc_asprintf_append_buffer(filter
, "%s))", g_filter
);
258 CHECK_ALLOC_DONE(filter
);
259 TALLOC_FREE(g_filter
);
261 filter
= talloc_asprintf_append_buffer(filter
, ")");
262 CHECK_ALLOC_DONE(filter
);
264 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
265 if (!ADS_ERR_OK(rc
)) {
266 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
267 ret
= NT_STATUS_UNSUCCESSFUL
;
271 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
272 DEBUG(10, ("No IDs found\n"));
276 for (i
= 0; (i
< count
) && entry
; i
++) {
283 if (i
== 0) { /* first entry */
284 entry
= ads_first_entry(ctx
->ads
, entry
);
285 } else { /* following ones */
286 entry
= ads_next_entry(ctx
->ads
, entry
);
290 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
294 /* first check if the SID is present */
295 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
296 DEBUG(2, ("Could not retrieve SID from entry\n"));
301 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
302 DEBUG(1, ("could not get SAM account type\n"));
306 switch (atype
& 0xF0000000) {
307 case ATYPE_SECURITY_GLOBAL_GROUP
:
308 case ATYPE_SECURITY_LOCAL_GROUP
:
311 case ATYPE_NORMAL_ACCOUNT
:
312 case ATYPE_WORKSTATION_TRUST
:
313 case ATYPE_INTERDOMAIN_TRUST
:
317 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
321 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
322 ctx
->ad_schema
->posix_uidnumber_attr
:
323 ctx
->ad_schema
->posix_gidnumber_attr
,
326 DEBUG(1, ("Could not get unix ID\n"));
330 if (!idmap_unix_id_is_in_range(id
, dom
)) {
331 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
332 id
, dom
->low_id
, dom
->high_id
));
336 map
= idmap_find_map_by_id(&ids
[bidx
], type
, id
);
338 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
342 sid_copy(map
->sid
, &sid
);
345 map
->status
= ID_MAPPED
;
347 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
348 (unsigned long)map
->xid
.id
,
353 ads_msgfree(ctx
->ads
, res
);
356 if (ids
[idx
]) { /* still some values to map */
362 /* mark all unknown/expired ones as unmapped */
363 for (i
= 0; ids
[i
]; i
++) {
364 if (ids
[i
]->status
!= ID_MAPPED
)
365 ids
[i
]->status
= ID_UNMAPPED
;
373 /************************************************************************
374 ***********************************************************************/
376 static NTSTATUS
idmap_ad_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
380 struct idmap_ad_context
*ctx
;
382 const char *attrs
[] = { "sAMAccountType",
384 NULL
, /* attr_uidnumber */
385 NULL
, /* attr_gidnumber */
387 LDAPMessage
*res
= NULL
;
388 LDAPMessage
*entry
= NULL
;
396 /* initialize the status to avoid suprise */
397 for (i
= 0; ids
[i
]; i
++) {
398 ids
[i
]->status
= ID_UNKNOWN
;
401 /* Only do query if we are online */
402 if (idmap_is_offline()) {
403 return NT_STATUS_FILE_IS_OFFLINE
;
406 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
408 if ( (memctx
= talloc_new(ctx
)) == NULL
) {
409 DEBUG(0, ("Out of memory!\n"));
410 return NT_STATUS_NO_MEMORY
;
413 rc
= ad_idmap_cached_connection(dom
);
414 if (!ADS_ERR_OK(rc
)) {
415 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc
)));
416 ret
= NT_STATUS_UNSUCCESSFUL
;
417 /* ret = ads_ntstatus(rc); */
421 if (ctx
->ad_schema
== NULL
) {
422 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
423 ret
= NT_STATUS_UNSUCCESSFUL
;
427 attrs
[2] = ctx
->ad_schema
->posix_uidnumber_attr
;
428 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
431 filter
= talloc_asprintf(memctx
, "(&(|"
432 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
433 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
435 ATYPE_NORMAL_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
,
436 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
);
438 CHECK_ALLOC_DONE(filter
);
441 for (i
= 0; (i
< IDMAP_LDAP_MAX_IDS
) && ids
[idx
]; i
++, idx
++) {
443 ids
[idx
]->status
= ID_UNKNOWN
;
445 sidstr
= ldap_encode_ndr_dom_sid(talloc_tos(), ids
[idx
]->sid
);
446 filter
= talloc_asprintf_append_buffer(filter
, "(objectSid=%s)", sidstr
);
449 CHECK_ALLOC_DONE(filter
);
451 filter
= talloc_asprintf_append_buffer(filter
, "))");
452 CHECK_ALLOC_DONE(filter
);
453 DEBUG(10, ("Filter: [%s]\n", filter
));
455 rc
= ads_search_retry(ctx
->ads
, &res
, filter
, attrs
);
456 if (!ADS_ERR_OK(rc
)) {
457 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc
)));
458 ret
= NT_STATUS_UNSUCCESSFUL
;
462 if ( (count
= ads_count_replies(ctx
->ads
, res
)) == 0 ) {
463 DEBUG(10, ("No IDs found\n"));
467 for (i
= 0; (i
< count
) && entry
; i
++) {
474 if (i
== 0) { /* first entry */
475 entry
= ads_first_entry(ctx
->ads
, entry
);
476 } else { /* following ones */
477 entry
= ads_next_entry(ctx
->ads
, entry
);
481 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
485 /* first check if the SID is present */
486 if (!ads_pull_sid(ctx
->ads
, entry
, "objectSid", &sid
)) {
487 DEBUG(2, ("Could not retrieve SID from entry\n"));
491 map
= idmap_find_map_by_sid(&ids
[bidx
], &sid
);
493 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
498 if (!ads_pull_uint32(ctx
->ads
, entry
, "sAMAccountType", &atype
)) {
499 DEBUG(1, ("could not get SAM account type\n"));
503 switch (atype
& 0xF0000000) {
504 case ATYPE_SECURITY_GLOBAL_GROUP
:
505 case ATYPE_SECURITY_LOCAL_GROUP
:
508 case ATYPE_NORMAL_ACCOUNT
:
509 case ATYPE_WORKSTATION_TRUST
:
510 case ATYPE_INTERDOMAIN_TRUST
:
514 DEBUG(1, ("unrecognized SAM account type %08x\n", atype
));
518 if (!ads_pull_uint32(ctx
->ads
, entry
, (type
==ID_TYPE_UID
) ?
519 ctx
->ad_schema
->posix_uidnumber_attr
:
520 ctx
->ad_schema
->posix_gidnumber_attr
,
523 DEBUG(1, ("Could not get unix ID\n"));
526 if (!idmap_unix_id_is_in_range(id
, dom
)) {
527 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
528 id
, dom
->low_id
, dom
->high_id
));
533 map
->xid
.type
= type
;
535 map
->status
= ID_MAPPED
;
537 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map
->sid
),
538 (unsigned long)map
->xid
.id
,
543 ads_msgfree(ctx
->ads
, res
);
546 if (ids
[idx
]) { /* still some values to map */
552 /* mark all unknown/expired ones as unmapped */
553 for (i
= 0; ids
[i
]; i
++) {
554 if (ids
[i
]->status
!= ID_MAPPED
)
555 ids
[i
]->status
= ID_UNMAPPED
;
564 * nss_info_{sfu,sfu20,rfc2307}
567 /************************************************************************
568 Initialize the {sfu,sfu20,rfc2307} state
569 ***********************************************************************/
571 static const char *wb_posix_map_unknown_string
= "WB_POSIX_MAP_UNKNOWN";
572 static const char *wb_posix_map_template_string
= "WB_POSIX_MAP_TEMPLATE";
573 static const char *wb_posix_map_sfu_string
= "WB_POSIX_MAP_SFU";
574 static const char *wb_posix_map_sfu20_string
= "WB_POSIX_MAP_SFU20";
575 static const char *wb_posix_map_rfc2307_string
= "WB_POSIX_MAP_RFC2307";
576 static const char *wb_posix_map_unixinfo_string
= "WB_POSIX_MAP_UNIXINFO";
578 static const char *ad_map_type_string(enum wb_posix_mapping map_type
)
581 case WB_POSIX_MAP_TEMPLATE
:
582 return wb_posix_map_template_string
;
583 case WB_POSIX_MAP_SFU
:
584 return wb_posix_map_sfu_string
;
585 case WB_POSIX_MAP_SFU20
:
586 return wb_posix_map_sfu20_string
;
587 case WB_POSIX_MAP_RFC2307
:
588 return wb_posix_map_rfc2307_string
;
589 case WB_POSIX_MAP_UNIXINFO
:
590 return wb_posix_map_unixinfo_string
;
592 return wb_posix_map_unknown_string
;
596 static NTSTATUS
nss_ad_generic_init(struct nss_domain_entry
*e
,
597 enum wb_posix_mapping new_ad_map_type
)
599 struct idmap_domain
*dom
;
600 struct idmap_ad_context
*ctx
;
602 if (e
->state
!= NULL
) {
603 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
605 dom
= talloc_zero(e
, struct idmap_domain
);
607 DEBUG(0, ("Out of memory!\n"));
608 return NT_STATUS_NO_MEMORY
;
613 if (e
->domain
!= NULL
) {
614 dom
->name
= talloc_strdup(dom
, e
->domain
);
615 if (dom
->name
== NULL
) {
616 DEBUG(0, ("Out of memory!\n"));
617 return NT_STATUS_NO_MEMORY
;
621 if (dom
->private_data
!= NULL
) {
622 ctx
= talloc_get_type(dom
->private_data
,
623 struct idmap_ad_context
);
625 ctx
= talloc_zero(dom
, struct idmap_ad_context
);
627 DEBUG(0, ("Out of memory!\n"));
628 return NT_STATUS_NO_MEMORY
;
630 ctx
->ad_map_type
= WB_POSIX_MAP_RFC2307
;
631 dom
->private_data
= ctx
;
634 if ((ctx
->ad_map_type
!= WB_POSIX_MAP_UNKNOWN
) &&
635 (ctx
->ad_map_type
!= new_ad_map_type
))
637 DEBUG(2, ("nss_ad_generic_init: "
638 "Warning: overriding previously set posix map type "
639 "%s for domain %s with map type %s.\n",
640 ad_map_type_string(ctx
->ad_map_type
),
642 ad_map_type_string(new_ad_map_type
)));
645 ctx
->ad_map_type
= new_ad_map_type
;
650 static NTSTATUS
nss_sfu_init( struct nss_domain_entry
*e
)
652 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU
);
655 static NTSTATUS
nss_sfu20_init( struct nss_domain_entry
*e
)
657 return nss_ad_generic_init(e
, WB_POSIX_MAP_SFU20
);
660 static NTSTATUS
nss_rfc2307_init( struct nss_domain_entry
*e
)
662 return nss_ad_generic_init(e
, WB_POSIX_MAP_RFC2307
);
666 /************************************************************************
667 ***********************************************************************/
669 static NTSTATUS
nss_ad_get_info( struct nss_domain_entry
*e
,
670 const struct dom_sid
*sid
,
672 const char **homedir
,
677 const char *attrs
[] = {NULL
, /* attr_homedir */
678 NULL
, /* attr_shell */
679 NULL
, /* attr_gecos */
680 NULL
, /* attr_gidnumber */
683 LDAPMessage
*msg_internal
= NULL
;
684 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
685 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
687 struct idmap_domain
*dom
;
688 struct idmap_ad_context
*ctx
;
690 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
691 sid_string_dbg(sid
), e
->domain
?e
->domain
:"NULL"));
693 /* Only do query if we are online */
694 if (idmap_is_offline()) {
695 return NT_STATUS_FILE_IS_OFFLINE
;
698 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
699 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
701 ads_status
= ad_idmap_cached_connection(dom
);
702 if (!ADS_ERR_OK(ads_status
)) {
703 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
706 if (!ctx
->ad_schema
) {
707 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
708 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
711 if (!sid
|| !homedir
|| !shell
|| !gecos
) {
712 return NT_STATUS_INVALID_PARAMETER
;
715 /* Have to do our own query */
717 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
720 attrs
[0] = ctx
->ad_schema
->posix_homedir_attr
;
721 attrs
[1] = ctx
->ad_schema
->posix_shell_attr
;
722 attrs
[2] = ctx
->ad_schema
->posix_gecos_attr
;
723 attrs
[3] = ctx
->ad_schema
->posix_gidnumber_attr
;
725 sidstr
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
726 filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)", sidstr
);
730 nt_status
= NT_STATUS_NO_MEMORY
;
734 ads_status
= ads_search_retry(ctx
->ads
, &msg_internal
, filter
, attrs
);
735 if (!ADS_ERR_OK(ads_status
)) {
736 nt_status
= ads_ntstatus(ads_status
);
740 *homedir
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_homedir_attr
);
741 *shell
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_shell_attr
);
742 *gecos
= ads_pull_string(ctx
->ads
, mem_ctx
, msg_internal
, ctx
->ad_schema
->posix_gecos_attr
);
745 if (!ads_pull_uint32(ctx
->ads
, msg_internal
, ctx
->ad_schema
->posix_gidnumber_attr
, gid
))
749 nt_status
= NT_STATUS_OK
;
753 ads_msgfree(ctx
->ads
, msg_internal
);
759 /**********************************************************************
760 *********************************************************************/
762 static NTSTATUS
nss_ad_map_to_alias(TALLOC_CTX
*mem_ctx
,
763 struct nss_domain_entry
*e
,
767 const char *attrs
[] = {NULL
, /* attr_uid */
770 LDAPMessage
*msg
= NULL
;
771 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
772 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
773 struct idmap_domain
*dom
;
774 struct idmap_ad_context
*ctx
= NULL
;
776 /* Check incoming parameters */
778 if ( !e
|| !e
->domain
|| !name
|| !*alias
) {
779 nt_status
= NT_STATUS_INVALID_PARAMETER
;
783 /* Only do query if we are online */
785 if (idmap_is_offline()) {
786 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
790 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
791 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
793 ads_status
= ad_idmap_cached_connection(dom
);
794 if (!ADS_ERR_OK(ads_status
)) {
795 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
798 if (!ctx
->ad_schema
) {
799 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
803 attrs
[0] = ctx
->ad_schema
->posix_uid_attr
;
805 filter
= talloc_asprintf(mem_ctx
,
806 "(sAMAccountName=%s)",
809 nt_status
= NT_STATUS_NO_MEMORY
;
813 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
814 if (!ADS_ERR_OK(ads_status
)) {
815 nt_status
= ads_ntstatus(ads_status
);
819 *alias
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
, ctx
->ad_schema
->posix_uid_attr
);
822 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
825 nt_status
= NT_STATUS_OK
;
829 talloc_destroy(filter
);
832 ads_msgfree(ctx
->ads
, msg
);
838 /**********************************************************************
839 *********************************************************************/
841 static NTSTATUS
nss_ad_map_from_alias( TALLOC_CTX
*mem_ctx
,
842 struct nss_domain_entry
*e
,
846 const char *attrs
[] = {"sAMAccountName",
849 LDAPMessage
*msg
= NULL
;
850 ADS_STATUS ads_status
= ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
851 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
853 struct idmap_domain
*dom
;
854 struct idmap_ad_context
*ctx
= NULL
;
856 /* Check incoming parameters */
858 if ( !alias
|| !name
) {
859 nt_status
= NT_STATUS_INVALID_PARAMETER
;
863 /* Only do query if we are online */
865 if (idmap_is_offline()) {
866 nt_status
= NT_STATUS_FILE_IS_OFFLINE
;
870 dom
= talloc_get_type(e
->state
, struct idmap_domain
);
871 ctx
= talloc_get_type(dom
->private_data
, struct idmap_ad_context
);
873 ads_status
= ad_idmap_cached_connection(dom
);
874 if (!ADS_ERR_OK(ads_status
)) {
875 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
878 if (!ctx
->ad_schema
) {
879 nt_status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
883 filter
= talloc_asprintf(mem_ctx
,
885 ctx
->ad_schema
->posix_uid_attr
,
888 nt_status
= NT_STATUS_NO_MEMORY
;
892 ads_status
= ads_search_retry(ctx
->ads
, &msg
, filter
, attrs
);
893 if (!ADS_ERR_OK(ads_status
)) {
894 nt_status
= ads_ntstatus(ads_status
);
898 username
= ads_pull_string(ctx
->ads
, mem_ctx
, msg
,
901 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
904 *name
= talloc_asprintf(mem_ctx
, "%s\\%s",
908 nt_status
= NT_STATUS_NO_MEMORY
;
912 nt_status
= NT_STATUS_OK
;
916 talloc_destroy(filter
);
919 ads_msgfree(ctx
->ads
, msg
);
925 /************************************************************************
926 Function dispatch tables for the idmap and nss plugins
927 ***********************************************************************/
929 static struct idmap_methods ad_methods
= {
930 .init
= idmap_ad_initialize
,
931 .unixids_to_sids
= idmap_ad_unixids_to_sids
,
932 .sids_to_unixids
= idmap_ad_sids_to_unixids
,
935 /* The SFU and RFC2307 NSS plugins share everything but the init
936 function which sets the intended schema model to use */
938 static struct nss_info_methods nss_rfc2307_methods
= {
939 .init
= nss_rfc2307_init
,
940 .get_nss_info
= nss_ad_get_info
,
941 .map_to_alias
= nss_ad_map_to_alias
,
942 .map_from_alias
= nss_ad_map_from_alias
,
945 static struct nss_info_methods nss_sfu_methods
= {
946 .init
= nss_sfu_init
,
947 .get_nss_info
= nss_ad_get_info
,
948 .map_to_alias
= nss_ad_map_to_alias
,
949 .map_from_alias
= nss_ad_map_from_alias
,
952 static struct nss_info_methods nss_sfu20_methods
= {
953 .init
= nss_sfu20_init
,
954 .get_nss_info
= nss_ad_get_info
,
955 .map_to_alias
= nss_ad_map_to_alias
,
956 .map_from_alias
= nss_ad_map_from_alias
,
961 /************************************************************************
962 Initialize the plugins
963 ***********************************************************************/
965 NTSTATUS
samba_init_module(void)
967 static NTSTATUS status_idmap_ad
= NT_STATUS_UNSUCCESSFUL
;
968 static NTSTATUS status_nss_rfc2307
= NT_STATUS_UNSUCCESSFUL
;
969 static NTSTATUS status_nss_sfu
= NT_STATUS_UNSUCCESSFUL
;
970 static NTSTATUS status_nss_sfu20
= NT_STATUS_UNSUCCESSFUL
;
972 /* Always register the AD method first in order to get the
973 idmap_domain interface called */
975 if ( !NT_STATUS_IS_OK(status_idmap_ad
) ) {
976 status_idmap_ad
= smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
978 if ( !NT_STATUS_IS_OK(status_idmap_ad
) )
979 return status_idmap_ad
;
982 if ( !NT_STATUS_IS_OK( status_nss_rfc2307
) ) {
983 status_nss_rfc2307
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
984 "rfc2307", &nss_rfc2307_methods
);
985 if ( !NT_STATUS_IS_OK(status_nss_rfc2307
) )
986 return status_nss_rfc2307
;
989 if ( !NT_STATUS_IS_OK( status_nss_sfu
) ) {
990 status_nss_sfu
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
991 "sfu", &nss_sfu_methods
);
992 if ( !NT_STATUS_IS_OK(status_nss_sfu
) )
993 return status_nss_sfu
;
996 if ( !NT_STATUS_IS_OK( status_nss_sfu20
) ) {
997 status_nss_sfu20
= smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION
,
998 "sfu20", &nss_sfu20_methods
);
999 if ( !NT_STATUS_IS_OK(status_nss_sfu20
) )
1000 return status_nss_sfu20
;
1003 return NT_STATUS_OK
;