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"
32 #include "libads/ldap_schema.h"
36 #include "../libcli/security/dom_sid.h"
39 #define DBGC_CLASS DBGC_IDMAP
41 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
43 #define IDMAP_AD_MAX_IDS 30
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 NTSTATUS init_module(void);
60 /************************************************************************
61 ***********************************************************************/
63 static ADS_STATUS ad_idmap_cached_connection_internal(struct idmap_domain *dom)
69 struct sockaddr_storage dc_ip;
70 struct idmap_ad_context *ctx;
71 char *ldap_server = NULL;
73 struct winbindd_domain *wb_dom;
75 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
78 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
80 if (ctx->ads != NULL) {
83 time_t now = time(NULL);
87 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
89 /* check for a valid structure */
90 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
91 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
93 if ( ads->config.realm && (expire > time(NULL))) {
96 /* we own this ADS_STRUCT so make sure it goes away */
97 DEBUG(7,("Deleting expired krb5 credential cache\n"));
100 ads_kdestroy(WINBIND_CCACHE_NAME);
102 TALLOC_FREE( ctx->ad_schema );
107 /* we don't want this to affect the users ccache */
108 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
112 * At this point we only have the NetBIOS domain name.
113 * Check if we can get server nam and realm from SAF cache
114 * and the domain list.
116 ldap_server = saf_fetch(dom->name);
117 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server?ldap_server:""));
119 wb_dom = find_domain_from_name_noinit(dom->name);
120 if (wb_dom == NULL) {
121 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
125 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
126 " domain '%s'\n", wb_dom->alt_name, dom->name));
127 realm = wb_dom->alt_name;
130 if ( (ads = ads_init(realm, dom->name, ldap_server)) == NULL ) {
131 DEBUG(1,("ads_init failed\n"));
132 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
135 /* the machine acct password might have change - fetch it every time */
136 SAFE_FREE(ads->auth.password);
137 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
139 SAFE_FREE(ads->auth.realm);
140 ads->auth.realm = SMB_STRDUP(lp_realm());
142 /* setup server affinity */
144 get_dc_name(dom->name, realm, dc_name, &dc_ip );
146 status = ads_connect(ads);
147 if (!ADS_ERR_OK(status)) {
148 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
153 ads->is_mine = False;
160 /************************************************************************
161 ***********************************************************************/
163 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
166 struct idmap_ad_context * ctx;
168 status = ad_idmap_cached_connection_internal(dom);
169 if (!ADS_ERR_OK(status)) {
173 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
175 /* if we have a valid ADS_STRUCT and the schema model is
176 defined, then we can return here. */
178 if ( ctx->ad_schema ) {
182 /* Otherwise, set the schema model */
184 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
185 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
186 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
188 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
189 if ( !ADS_ERR_OK(status) ) {
190 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
197 /************************************************************************
198 ***********************************************************************/
200 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
203 struct idmap_ad_context *ctx;
205 const char *schema_mode = NULL;
207 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
209 DEBUG(0, ("Out of memory!\n"));
210 return NT_STATUS_NO_MEMORY;
213 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
214 if (config_option == NULL) {
215 DEBUG(0, ("Out of memory!\n"));
217 return NT_STATUS_NO_MEMORY;
220 /* default map type */
221 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
224 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
225 if ( schema_mode && schema_mode[0] ) {
226 if ( strequal(schema_mode, "sfu") )
227 ctx->ad_map_type = WB_POSIX_MAP_SFU;
228 else if ( strequal(schema_mode, "sfu20" ) )
229 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
230 else if ( strequal(schema_mode, "rfc2307" ) )
231 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
233 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
237 dom->private_data = ctx;
239 talloc_free(config_option);
244 /************************************************************************
245 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
246 ***********************************************************************/
248 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
252 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
253 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
261 /************************************************************************
262 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
263 ***********************************************************************/
265 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
269 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
270 if (dom_sid_equal(maps[i]->sid, sid)) {
278 /************************************************************************
279 ***********************************************************************/
281 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
285 struct idmap_ad_context *ctx;
287 const char *attrs[] = { "sAMAccountType",
289 NULL, /* uidnumber */
290 NULL, /* gidnumber */
292 LDAPMessage *res = NULL;
293 LDAPMessage *entry = NULL;
299 char *u_filter = NULL;
300 char *g_filter = NULL;
302 /* initialize the status to avoid suprise */
303 for (i = 0; ids[i]; i++) {
304 ids[i]->status = ID_UNKNOWN;
307 /* Only do query if we are online */
308 if (idmap_is_offline()) {
309 return NT_STATUS_FILE_IS_OFFLINE;
312 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
314 if ( (memctx = talloc_new(ctx)) == NULL ) {
315 DEBUG(0, ("Out of memory!\n"));
316 return NT_STATUS_NO_MEMORY;
319 rc = ad_idmap_cached_connection(dom);
320 if (!ADS_ERR_OK(rc)) {
321 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
322 ret = NT_STATUS_UNSUCCESSFUL;
323 /* ret = ads_ntstatus(rc); */
327 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
328 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
332 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
333 switch (ids[idx]->xid.type) {
336 u_filter = talloc_asprintf(memctx, "(&(|"
337 "(sAMAccountType=%d)"
338 "(sAMAccountType=%d)"
339 "(sAMAccountType=%d))(|",
340 ATYPE_NORMAL_ACCOUNT,
341 ATYPE_WORKSTATION_TRUST,
342 ATYPE_INTERDOMAIN_TRUST);
344 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
345 ctx->ad_schema->posix_uidnumber_attr,
346 (unsigned long)ids[idx]->xid.id);
347 CHECK_ALLOC_DONE(u_filter);
352 g_filter = talloc_asprintf(memctx, "(&(|"
353 "(sAMAccountType=%d)"
354 "(sAMAccountType=%d))(|",
355 ATYPE_SECURITY_GLOBAL_GROUP,
356 ATYPE_SECURITY_LOCAL_GROUP);
358 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
359 ctx->ad_schema->posix_gidnumber_attr,
360 (unsigned long)ids[idx]->xid.id);
361 CHECK_ALLOC_DONE(g_filter);
365 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
366 ids[idx]->status = ID_UNKNOWN;
370 filter = talloc_asprintf(memctx, "(|");
371 CHECK_ALLOC_DONE(filter);
373 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
374 CHECK_ALLOC_DONE(filter);
375 TALLOC_FREE(u_filter);
378 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
379 CHECK_ALLOC_DONE(filter);
380 TALLOC_FREE(g_filter);
382 filter = talloc_asprintf_append_buffer(filter, ")");
383 CHECK_ALLOC_DONE(filter);
385 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
386 if (!ADS_ERR_OK(rc)) {
387 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
388 ret = NT_STATUS_UNSUCCESSFUL;
392 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
393 DEBUG(10, ("No IDs found\n"));
397 for (i = 0; (i < count) && entry; i++) {
404 if (i == 0) { /* first entry */
405 entry = ads_first_entry(ctx->ads, entry);
406 } else { /* following ones */
407 entry = ads_next_entry(ctx->ads, entry);
411 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
415 /* first check if the SID is present */
416 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
417 DEBUG(2, ("Could not retrieve SID from entry\n"));
422 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
423 DEBUG(1, ("could not get SAM account type\n"));
427 switch (atype & 0xF0000000) {
428 case ATYPE_SECURITY_GLOBAL_GROUP:
429 case ATYPE_SECURITY_LOCAL_GROUP:
432 case ATYPE_NORMAL_ACCOUNT:
433 case ATYPE_WORKSTATION_TRUST:
434 case ATYPE_INTERDOMAIN_TRUST:
438 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
442 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
443 ctx->ad_schema->posix_uidnumber_attr :
444 ctx->ad_schema->posix_gidnumber_attr,
447 DEBUG(1, ("Could not get unix ID\n"));
451 if (!idmap_unix_id_is_in_range(id, dom)) {
452 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
453 id, dom->low_id, dom->high_id));
457 map = find_map_by_id(&ids[bidx], type, id);
459 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
463 sid_copy(map->sid, &sid);
466 map->status = ID_MAPPED;
468 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
469 (unsigned long)map->xid.id,
474 ads_msgfree(ctx->ads, res);
477 if (ids[idx]) { /* still some values to map */
483 /* mark all unknown/expired ones as unmapped */
484 for (i = 0; ids[i]; i++) {
485 if (ids[i]->status != ID_MAPPED)
486 ids[i]->status = ID_UNMAPPED;
494 /************************************************************************
495 ***********************************************************************/
497 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
501 struct idmap_ad_context *ctx;
503 const char *attrs[] = { "sAMAccountType",
505 NULL, /* attr_uidnumber */
506 NULL, /* attr_gidnumber */
508 LDAPMessage *res = NULL;
509 LDAPMessage *entry = NULL;
517 /* initialize the status to avoid suprise */
518 for (i = 0; ids[i]; i++) {
519 ids[i]->status = ID_UNKNOWN;
522 /* Only do query if we are online */
523 if (idmap_is_offline()) {
524 return NT_STATUS_FILE_IS_OFFLINE;
527 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
529 if ( (memctx = talloc_new(ctx)) == NULL ) {
530 DEBUG(0, ("Out of memory!\n"));
531 return NT_STATUS_NO_MEMORY;
534 rc = ad_idmap_cached_connection(dom);
535 if (!ADS_ERR_OK(rc)) {
536 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
537 ret = NT_STATUS_UNSUCCESSFUL;
538 /* ret = ads_ntstatus(rc); */
542 if (ctx->ad_schema == NULL) {
543 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
544 ret = NT_STATUS_UNSUCCESSFUL;
548 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
549 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
552 filter = talloc_asprintf(memctx, "(&(|"
553 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
554 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
556 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
557 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
559 CHECK_ALLOC_DONE(filter);
562 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
564 ids[idx]->status = ID_UNKNOWN;
566 sidstr = sid_binstring(talloc_tos(), ids[idx]->sid);
567 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
570 CHECK_ALLOC_DONE(filter);
572 filter = talloc_asprintf_append_buffer(filter, "))");
573 CHECK_ALLOC_DONE(filter);
574 DEBUG(10, ("Filter: [%s]\n", filter));
576 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
577 if (!ADS_ERR_OK(rc)) {
578 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
579 ret = NT_STATUS_UNSUCCESSFUL;
583 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
584 DEBUG(10, ("No IDs found\n"));
588 for (i = 0; (i < count) && entry; i++) {
595 if (i == 0) { /* first entry */
596 entry = ads_first_entry(ctx->ads, entry);
597 } else { /* following ones */
598 entry = ads_next_entry(ctx->ads, entry);
602 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
606 /* first check if the SID is present */
607 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
608 DEBUG(2, ("Could not retrieve SID from entry\n"));
612 map = find_map_by_sid(&ids[bidx], &sid);
614 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
619 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
620 DEBUG(1, ("could not get SAM account type\n"));
624 switch (atype & 0xF0000000) {
625 case ATYPE_SECURITY_GLOBAL_GROUP:
626 case ATYPE_SECURITY_LOCAL_GROUP:
629 case ATYPE_NORMAL_ACCOUNT:
630 case ATYPE_WORKSTATION_TRUST:
631 case ATYPE_INTERDOMAIN_TRUST:
635 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
639 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
640 ctx->ad_schema->posix_uidnumber_attr :
641 ctx->ad_schema->posix_gidnumber_attr,
644 DEBUG(1, ("Could not get unix ID\n"));
647 if (!idmap_unix_id_is_in_range(id, dom)) {
648 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
649 id, dom->low_id, dom->high_id));
654 map->xid.type = type;
656 map->status = ID_MAPPED;
658 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
659 (unsigned long)map->xid.id,
664 ads_msgfree(ctx->ads, res);
667 if (ids[idx]) { /* still some values to map */
673 /* mark all unknwoni/expired ones as unmapped */
674 for (i = 0; ids[i]; i++) {
675 if (ids[i]->status != ID_MAPPED)
676 ids[i]->status = ID_UNMAPPED;
684 /************************************************************************
685 ***********************************************************************/
687 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
689 struct idmap_ad_context * ctx;
691 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
693 if (ctx->ads != NULL) {
694 /* we own this ADS_STRUCT so make sure it goes away */
695 ctx->ads->is_mine = True;
696 ads_destroy( &ctx->ads );
700 TALLOC_FREE( ctx->ad_schema );
706 * nss_info_{sfu,sfu20,rfc2307}
709 /************************************************************************
710 Initialize the {sfu,sfu20,rfc2307} state
711 ***********************************************************************/
713 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
714 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
715 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
716 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
717 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
718 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
720 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
723 case WB_POSIX_MAP_TEMPLATE:
724 return wb_posix_map_template_string;
725 case WB_POSIX_MAP_SFU:
726 return wb_posix_map_sfu_string;
727 case WB_POSIX_MAP_SFU20:
728 return wb_posix_map_sfu20_string;
729 case WB_POSIX_MAP_RFC2307:
730 return wb_posix_map_rfc2307_string;
731 case WB_POSIX_MAP_UNIXINFO:
732 return wb_posix_map_unixinfo_string;
734 return wb_posix_map_unknown_string;
738 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
739 enum wb_posix_mapping new_ad_map_type)
741 struct idmap_domain *dom;
742 struct idmap_ad_context *ctx;
744 if (e->state != NULL) {
745 dom = talloc_get_type(e->state, struct idmap_domain);
747 dom = TALLOC_ZERO_P(e, struct idmap_domain);
749 DEBUG(0, ("Out of memory!\n"));
750 return NT_STATUS_NO_MEMORY;
755 if (e->domain != NULL) {
756 dom->name = talloc_strdup(dom, e->domain);
757 if (dom->name == NULL) {
758 DEBUG(0, ("Out of memory!\n"));
759 return NT_STATUS_NO_MEMORY;
763 if (dom->private_data != NULL) {
764 ctx = talloc_get_type(dom->private_data,
765 struct idmap_ad_context);
767 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
769 DEBUG(0, ("Out of memory!\n"));
770 return NT_STATUS_NO_MEMORY;
772 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
773 dom->private_data = ctx;
776 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
777 (ctx->ad_map_type != new_ad_map_type))
779 DEBUG(2, ("nss_ad_generic_init: "
780 "Warning: overriding previously set posix map type "
781 "%s for domain %s with map type %s.\n",
782 ad_map_type_string(ctx->ad_map_type),
784 ad_map_type_string(new_ad_map_type)));
787 ctx->ad_map_type = new_ad_map_type;
792 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
794 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
797 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
799 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
802 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
804 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
808 /************************************************************************
809 ***********************************************************************/
811 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
812 const struct dom_sid *sid,
816 const char **homedir,
821 const char *attrs[] = {NULL, /* attr_homedir */
822 NULL, /* attr_shell */
823 NULL, /* attr_gecos */
824 NULL, /* attr_gidnumber */
827 LDAPMessage *msg_internal = NULL;
828 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
829 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
831 struct idmap_domain *dom;
832 struct idmap_ad_context *ctx;
834 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
835 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
837 /* Only do query if we are online */
838 if (idmap_is_offline()) {
839 return NT_STATUS_FILE_IS_OFFLINE;
842 dom = talloc_get_type(e->state, struct idmap_domain);
843 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
845 ads_status = ad_idmap_cached_connection(dom);
846 if (!ADS_ERR_OK(ads_status)) {
847 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
850 if (!ctx->ad_schema) {
851 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
852 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
855 if (!sid || !homedir || !shell || !gecos) {
856 return NT_STATUS_INVALID_PARAMETER;
859 /* See if we can use the ADS connection struct swe were given */
862 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
863 "LDAP message (%p)\n", msg));
865 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
866 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
867 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
870 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
874 nt_status = NT_STATUS_OK;
878 /* Have to do our own query */
880 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
883 attrs[0] = ctx->ad_schema->posix_homedir_attr;
884 attrs[1] = ctx->ad_schema->posix_shell_attr;
885 attrs[2] = ctx->ad_schema->posix_gecos_attr;
886 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
888 sidstr = sid_binstring(mem_ctx, sid);
889 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
893 nt_status = NT_STATUS_NO_MEMORY;
897 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
898 if (!ADS_ERR_OK(ads_status)) {
899 nt_status = ads_ntstatus(ads_status);
903 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
904 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
905 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
908 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
912 nt_status = NT_STATUS_OK;
916 ads_msgfree(ctx->ads, msg_internal);
922 /**********************************************************************
923 *********************************************************************/
925 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
926 struct nss_domain_entry *e,
930 const char *attrs[] = {NULL, /* attr_uid */
933 LDAPMessage *msg = NULL;
934 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
935 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
936 struct idmap_domain *dom;
937 struct idmap_ad_context *ctx = NULL;
939 /* Check incoming parameters */
941 if ( !e || !e->domain || !name || !*alias) {
942 nt_status = NT_STATUS_INVALID_PARAMETER;
946 /* Only do query if we are online */
948 if (idmap_is_offline()) {
949 nt_status = NT_STATUS_FILE_IS_OFFLINE;
953 dom = talloc_get_type(e->state, struct idmap_domain);
954 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
956 ads_status = ad_idmap_cached_connection(dom);
957 if (!ADS_ERR_OK(ads_status)) {
958 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
961 if (!ctx->ad_schema) {
962 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
966 attrs[0] = ctx->ad_schema->posix_uid_attr;
968 filter = talloc_asprintf(mem_ctx,
969 "(sAMAccountName=%s)",
972 nt_status = NT_STATUS_NO_MEMORY;
976 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
977 if (!ADS_ERR_OK(ads_status)) {
978 nt_status = ads_ntstatus(ads_status);
982 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
985 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
988 nt_status = NT_STATUS_OK;
992 talloc_destroy(filter);
995 ads_msgfree(ctx->ads, msg);
1001 /**********************************************************************
1002 *********************************************************************/
1004 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
1005 struct nss_domain_entry *e,
1009 const char *attrs[] = {"sAMAccountName",
1011 char *filter = NULL;
1012 LDAPMessage *msg = NULL;
1013 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1014 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1016 struct idmap_domain *dom;
1017 struct idmap_ad_context *ctx = NULL;
1019 /* Check incoming parameters */
1021 if ( !alias || !name) {
1022 nt_status = NT_STATUS_INVALID_PARAMETER;
1026 /* Only do query if we are online */
1028 if (idmap_is_offline()) {
1029 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1033 dom = talloc_get_type(e->state, struct idmap_domain);
1034 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1036 ads_status = ad_idmap_cached_connection(dom);
1037 if (!ADS_ERR_OK(ads_status)) {
1038 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1041 if (!ctx->ad_schema) {
1042 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1046 filter = talloc_asprintf(mem_ctx,
1048 ctx->ad_schema->posix_uid_attr,
1051 nt_status = NT_STATUS_NO_MEMORY;
1055 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1056 if (!ADS_ERR_OK(ads_status)) {
1057 nt_status = ads_ntstatus(ads_status);
1061 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1064 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1067 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1071 nt_status = NT_STATUS_NO_MEMORY;
1075 nt_status = NT_STATUS_OK;
1079 talloc_destroy(filter);
1082 ads_msgfree(ctx->ads, msg);
1089 /************************************************************************
1090 ***********************************************************************/
1092 static NTSTATUS nss_ad_close( void )
1094 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1096 return NT_STATUS_OK;
1099 /************************************************************************
1100 Function dispatch tables for the idmap and nss plugins
1101 ***********************************************************************/
1103 static struct idmap_methods ad_methods = {
1104 .init = idmap_ad_initialize,
1105 .unixids_to_sids = idmap_ad_unixids_to_sids,
1106 .sids_to_unixids = idmap_ad_sids_to_unixids,
1107 .close_fn = idmap_ad_close
1110 /* The SFU and RFC2307 NSS plugins share everything but the init
1111 function which sets the intended schema model to use */
1113 static struct nss_info_methods nss_rfc2307_methods = {
1114 .init = nss_rfc2307_init,
1115 .get_nss_info = nss_ad_get_info,
1116 .map_to_alias = nss_ad_map_to_alias,
1117 .map_from_alias = nss_ad_map_from_alias,
1118 .close_fn = nss_ad_close
1121 static struct nss_info_methods nss_sfu_methods = {
1122 .init = nss_sfu_init,
1123 .get_nss_info = nss_ad_get_info,
1124 .map_to_alias = nss_ad_map_to_alias,
1125 .map_from_alias = nss_ad_map_from_alias,
1126 .close_fn = nss_ad_close
1129 static struct nss_info_methods nss_sfu20_methods = {
1130 .init = nss_sfu20_init,
1131 .get_nss_info = nss_ad_get_info,
1132 .map_to_alias = nss_ad_map_to_alias,
1133 .map_from_alias = nss_ad_map_from_alias,
1134 .close_fn = nss_ad_close
1139 /************************************************************************
1140 Initialize the plugins
1141 ***********************************************************************/
1143 NTSTATUS idmap_ad_init(void)
1145 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1146 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1147 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1148 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1150 /* Always register the AD method first in order to get the
1151 idmap_domain interface called */
1153 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1154 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1156 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1157 return status_idmap_ad;
1160 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1161 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1162 "rfc2307", &nss_rfc2307_methods );
1163 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1164 return status_nss_rfc2307;
1167 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1168 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1169 "sfu", &nss_sfu_methods );
1170 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1171 return status_nss_sfu;
1174 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1175 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1176 "sfu20", &nss_sfu20_methods );
1177 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1178 return status_nss_sfu20;
1181 return NT_STATUS_OK;