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"
37 #include "../libcli/ldap/ldap_ndr.h"
40 #define DBGC_CLASS DBGC_IDMAP
42 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
44 #define IDMAP_AD_MAX_IDS 30
45 #define CHECK_ALLOC_DONE(mem) do { \
47 DEBUG(0, ("Out of memory!\n")); \
48 ret = NT_STATUS_NO_MEMORY; \
53 struct idmap_ad_context {
55 struct posix_schema *ad_schema;
56 enum wb_posix_mapping ad_map_type; /* WB_POSIX_MAP_UNKNOWN */
59 NTSTATUS init_module(void);
61 /************************************************************************
62 ***********************************************************************/
64 static ADS_STATUS ad_idmap_cached_connection_internal(struct idmap_domain *dom)
70 struct sockaddr_storage dc_ip;
71 struct idmap_ad_context *ctx;
72 char *ldap_server = NULL;
74 struct winbindd_domain *wb_dom;
76 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
79 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
81 if (ctx->ads != NULL) {
84 time_t now = time(NULL);
88 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
90 /* check for a valid structure */
91 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
92 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
94 if ( ads->config.realm && (expire > time(NULL))) {
97 /* we own this ADS_STRUCT so make sure it goes away */
98 DEBUG(7,("Deleting expired krb5 credential cache\n"));
101 ads_kdestroy(WINBIND_CCACHE_NAME);
103 TALLOC_FREE( ctx->ad_schema );
108 /* we don't want this to affect the users ccache */
109 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
113 * At this point we only have the NetBIOS domain name.
114 * Check if we can get server nam and realm from SAF cache
115 * and the domain list.
117 ldap_server = saf_fetch(dom->name);
118 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server?ldap_server:""));
120 wb_dom = find_domain_from_name_noinit(dom->name);
121 if (wb_dom == NULL) {
122 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
126 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
127 " domain '%s'\n", wb_dom->alt_name, dom->name));
128 realm = wb_dom->alt_name;
131 if ( (ads = ads_init(realm, dom->name, ldap_server)) == NULL ) {
132 DEBUG(1,("ads_init failed\n"));
133 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
136 /* the machine acct password might have change - fetch it every time */
137 SAFE_FREE(ads->auth.password);
138 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
140 SAFE_FREE(ads->auth.realm);
141 ads->auth.realm = SMB_STRDUP(lp_realm());
143 /* setup server affinity */
145 get_dc_name(dom->name, realm, dc_name, &dc_ip );
147 status = ads_connect(ads);
148 if (!ADS_ERR_OK(status)) {
149 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
154 ads->is_mine = False;
161 /************************************************************************
162 ***********************************************************************/
164 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
167 struct idmap_ad_context * ctx;
169 status = ad_idmap_cached_connection_internal(dom);
170 if (!ADS_ERR_OK(status)) {
174 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
176 /* if we have a valid ADS_STRUCT and the schema model is
177 defined, then we can return here. */
179 if ( ctx->ad_schema ) {
183 /* Otherwise, set the schema model */
185 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
186 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
187 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
189 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
190 if ( !ADS_ERR_OK(status) ) {
191 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
198 /************************************************************************
199 ***********************************************************************/
201 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
204 struct idmap_ad_context *ctx;
206 const char *schema_mode = NULL;
208 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
210 DEBUG(0, ("Out of memory!\n"));
211 return NT_STATUS_NO_MEMORY;
214 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
215 if (config_option == NULL) {
216 DEBUG(0, ("Out of memory!\n"));
218 return NT_STATUS_NO_MEMORY;
221 /* default map type */
222 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
225 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
226 if ( schema_mode && schema_mode[0] ) {
227 if ( strequal(schema_mode, "sfu") )
228 ctx->ad_map_type = WB_POSIX_MAP_SFU;
229 else if ( strequal(schema_mode, "sfu20" ) )
230 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
231 else if ( strequal(schema_mode, "rfc2307" ) )
232 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
234 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
238 dom->private_data = ctx;
240 talloc_free(config_option);
245 /************************************************************************
246 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
247 ***********************************************************************/
249 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
253 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
254 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
262 /************************************************************************
263 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
264 ***********************************************************************/
266 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
270 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
271 if (dom_sid_equal(maps[i]->sid, sid)) {
279 /************************************************************************
280 ***********************************************************************/
282 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
286 struct idmap_ad_context *ctx;
288 const char *attrs[] = { "sAMAccountType",
290 NULL, /* uidnumber */
291 NULL, /* gidnumber */
293 LDAPMessage *res = NULL;
294 LDAPMessage *entry = NULL;
300 char *u_filter = NULL;
301 char *g_filter = NULL;
303 /* initialize the status to avoid suprise */
304 for (i = 0; ids[i]; i++) {
305 ids[i]->status = ID_UNKNOWN;
308 /* Only do query if we are online */
309 if (idmap_is_offline()) {
310 return NT_STATUS_FILE_IS_OFFLINE;
313 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
315 if ( (memctx = talloc_new(ctx)) == NULL ) {
316 DEBUG(0, ("Out of memory!\n"));
317 return NT_STATUS_NO_MEMORY;
320 rc = ad_idmap_cached_connection(dom);
321 if (!ADS_ERR_OK(rc)) {
322 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
323 ret = NT_STATUS_UNSUCCESSFUL;
324 /* ret = ads_ntstatus(rc); */
328 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
329 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
333 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
334 switch (ids[idx]->xid.type) {
337 u_filter = talloc_asprintf(memctx, "(&(|"
338 "(sAMAccountType=%d)"
339 "(sAMAccountType=%d)"
340 "(sAMAccountType=%d))(|",
341 ATYPE_NORMAL_ACCOUNT,
342 ATYPE_WORKSTATION_TRUST,
343 ATYPE_INTERDOMAIN_TRUST);
345 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
346 ctx->ad_schema->posix_uidnumber_attr,
347 (unsigned long)ids[idx]->xid.id);
348 CHECK_ALLOC_DONE(u_filter);
353 g_filter = talloc_asprintf(memctx, "(&(|"
354 "(sAMAccountType=%d)"
355 "(sAMAccountType=%d))(|",
356 ATYPE_SECURITY_GLOBAL_GROUP,
357 ATYPE_SECURITY_LOCAL_GROUP);
359 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
360 ctx->ad_schema->posix_gidnumber_attr,
361 (unsigned long)ids[idx]->xid.id);
362 CHECK_ALLOC_DONE(g_filter);
366 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
367 ids[idx]->status = ID_UNKNOWN;
371 filter = talloc_asprintf(memctx, "(|");
372 CHECK_ALLOC_DONE(filter);
374 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
375 CHECK_ALLOC_DONE(filter);
376 TALLOC_FREE(u_filter);
379 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
380 CHECK_ALLOC_DONE(filter);
381 TALLOC_FREE(g_filter);
383 filter = talloc_asprintf_append_buffer(filter, ")");
384 CHECK_ALLOC_DONE(filter);
386 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
387 if (!ADS_ERR_OK(rc)) {
388 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
389 ret = NT_STATUS_UNSUCCESSFUL;
393 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
394 DEBUG(10, ("No IDs found\n"));
398 for (i = 0; (i < count) && entry; i++) {
405 if (i == 0) { /* first entry */
406 entry = ads_first_entry(ctx->ads, entry);
407 } else { /* following ones */
408 entry = ads_next_entry(ctx->ads, entry);
412 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
416 /* first check if the SID is present */
417 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
418 DEBUG(2, ("Could not retrieve SID from entry\n"));
423 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
424 DEBUG(1, ("could not get SAM account type\n"));
428 switch (atype & 0xF0000000) {
429 case ATYPE_SECURITY_GLOBAL_GROUP:
430 case ATYPE_SECURITY_LOCAL_GROUP:
433 case ATYPE_NORMAL_ACCOUNT:
434 case ATYPE_WORKSTATION_TRUST:
435 case ATYPE_INTERDOMAIN_TRUST:
439 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
443 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
444 ctx->ad_schema->posix_uidnumber_attr :
445 ctx->ad_schema->posix_gidnumber_attr,
448 DEBUG(1, ("Could not get unix ID\n"));
452 if (!idmap_unix_id_is_in_range(id, dom)) {
453 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
454 id, dom->low_id, dom->high_id));
458 map = find_map_by_id(&ids[bidx], type, id);
460 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
464 sid_copy(map->sid, &sid);
467 map->status = ID_MAPPED;
469 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
470 (unsigned long)map->xid.id,
475 ads_msgfree(ctx->ads, res);
478 if (ids[idx]) { /* still some values to map */
484 /* mark all unknown/expired ones as unmapped */
485 for (i = 0; ids[i]; i++) {
486 if (ids[i]->status != ID_MAPPED)
487 ids[i]->status = ID_UNMAPPED;
495 /************************************************************************
496 ***********************************************************************/
498 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
502 struct idmap_ad_context *ctx;
504 const char *attrs[] = { "sAMAccountType",
506 NULL, /* attr_uidnumber */
507 NULL, /* attr_gidnumber */
509 LDAPMessage *res = NULL;
510 LDAPMessage *entry = NULL;
518 /* initialize the status to avoid suprise */
519 for (i = 0; ids[i]; i++) {
520 ids[i]->status = ID_UNKNOWN;
523 /* Only do query if we are online */
524 if (idmap_is_offline()) {
525 return NT_STATUS_FILE_IS_OFFLINE;
528 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
530 if ( (memctx = talloc_new(ctx)) == NULL ) {
531 DEBUG(0, ("Out of memory!\n"));
532 return NT_STATUS_NO_MEMORY;
535 rc = ad_idmap_cached_connection(dom);
536 if (!ADS_ERR_OK(rc)) {
537 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
538 ret = NT_STATUS_UNSUCCESSFUL;
539 /* ret = ads_ntstatus(rc); */
543 if (ctx->ad_schema == NULL) {
544 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
545 ret = NT_STATUS_UNSUCCESSFUL;
549 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
550 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
553 filter = talloc_asprintf(memctx, "(&(|"
554 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
555 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
557 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
558 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
560 CHECK_ALLOC_DONE(filter);
563 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
565 ids[idx]->status = ID_UNKNOWN;
567 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), ids[idx]->sid);
568 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
571 CHECK_ALLOC_DONE(filter);
573 filter = talloc_asprintf_append_buffer(filter, "))");
574 CHECK_ALLOC_DONE(filter);
575 DEBUG(10, ("Filter: [%s]\n", filter));
577 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
578 if (!ADS_ERR_OK(rc)) {
579 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
580 ret = NT_STATUS_UNSUCCESSFUL;
584 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
585 DEBUG(10, ("No IDs found\n"));
589 for (i = 0; (i < count) && entry; i++) {
596 if (i == 0) { /* first entry */
597 entry = ads_first_entry(ctx->ads, entry);
598 } else { /* following ones */
599 entry = ads_next_entry(ctx->ads, entry);
603 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
607 /* first check if the SID is present */
608 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
609 DEBUG(2, ("Could not retrieve SID from entry\n"));
613 map = find_map_by_sid(&ids[bidx], &sid);
615 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
620 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
621 DEBUG(1, ("could not get SAM account type\n"));
625 switch (atype & 0xF0000000) {
626 case ATYPE_SECURITY_GLOBAL_GROUP:
627 case ATYPE_SECURITY_LOCAL_GROUP:
630 case ATYPE_NORMAL_ACCOUNT:
631 case ATYPE_WORKSTATION_TRUST:
632 case ATYPE_INTERDOMAIN_TRUST:
636 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
640 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
641 ctx->ad_schema->posix_uidnumber_attr :
642 ctx->ad_schema->posix_gidnumber_attr,
645 DEBUG(1, ("Could not get unix ID\n"));
648 if (!idmap_unix_id_is_in_range(id, dom)) {
649 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
650 id, dom->low_id, dom->high_id));
655 map->xid.type = type;
657 map->status = ID_MAPPED;
659 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
660 (unsigned long)map->xid.id,
665 ads_msgfree(ctx->ads, res);
668 if (ids[idx]) { /* still some values to map */
674 /* mark all unknwoni/expired ones as unmapped */
675 for (i = 0; ids[i]; i++) {
676 if (ids[i]->status != ID_MAPPED)
677 ids[i]->status = ID_UNMAPPED;
685 /************************************************************************
686 ***********************************************************************/
688 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
690 struct idmap_ad_context * ctx;
692 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
694 if (ctx->ads != NULL) {
695 /* we own this ADS_STRUCT so make sure it goes away */
696 ctx->ads->is_mine = True;
697 ads_destroy( &ctx->ads );
701 TALLOC_FREE( ctx->ad_schema );
707 * nss_info_{sfu,sfu20,rfc2307}
710 /************************************************************************
711 Initialize the {sfu,sfu20,rfc2307} state
712 ***********************************************************************/
714 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
715 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
716 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
717 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
718 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
719 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
721 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
724 case WB_POSIX_MAP_TEMPLATE:
725 return wb_posix_map_template_string;
726 case WB_POSIX_MAP_SFU:
727 return wb_posix_map_sfu_string;
728 case WB_POSIX_MAP_SFU20:
729 return wb_posix_map_sfu20_string;
730 case WB_POSIX_MAP_RFC2307:
731 return wb_posix_map_rfc2307_string;
732 case WB_POSIX_MAP_UNIXINFO:
733 return wb_posix_map_unixinfo_string;
735 return wb_posix_map_unknown_string;
739 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
740 enum wb_posix_mapping new_ad_map_type)
742 struct idmap_domain *dom;
743 struct idmap_ad_context *ctx;
745 if (e->state != NULL) {
746 dom = talloc_get_type(e->state, struct idmap_domain);
748 dom = TALLOC_ZERO_P(e, struct idmap_domain);
750 DEBUG(0, ("Out of memory!\n"));
751 return NT_STATUS_NO_MEMORY;
756 if (e->domain != NULL) {
757 dom->name = talloc_strdup(dom, e->domain);
758 if (dom->name == NULL) {
759 DEBUG(0, ("Out of memory!\n"));
760 return NT_STATUS_NO_MEMORY;
764 if (dom->private_data != NULL) {
765 ctx = talloc_get_type(dom->private_data,
766 struct idmap_ad_context);
768 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
770 DEBUG(0, ("Out of memory!\n"));
771 return NT_STATUS_NO_MEMORY;
773 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
774 dom->private_data = ctx;
777 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
778 (ctx->ad_map_type != new_ad_map_type))
780 DEBUG(2, ("nss_ad_generic_init: "
781 "Warning: overriding previously set posix map type "
782 "%s for domain %s with map type %s.\n",
783 ad_map_type_string(ctx->ad_map_type),
785 ad_map_type_string(new_ad_map_type)));
788 ctx->ad_map_type = new_ad_map_type;
793 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
795 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
798 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
800 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
803 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
805 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
809 /************************************************************************
810 ***********************************************************************/
812 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
813 const struct dom_sid *sid,
817 const char **homedir,
822 const char *attrs[] = {NULL, /* attr_homedir */
823 NULL, /* attr_shell */
824 NULL, /* attr_gecos */
825 NULL, /* attr_gidnumber */
828 LDAPMessage *msg_internal = NULL;
829 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
830 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
832 struct idmap_domain *dom;
833 struct idmap_ad_context *ctx;
835 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
836 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
838 /* Only do query if we are online */
839 if (idmap_is_offline()) {
840 return NT_STATUS_FILE_IS_OFFLINE;
843 dom = talloc_get_type(e->state, struct idmap_domain);
844 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
846 ads_status = ad_idmap_cached_connection(dom);
847 if (!ADS_ERR_OK(ads_status)) {
848 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
851 if (!ctx->ad_schema) {
852 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
853 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
856 if (!sid || !homedir || !shell || !gecos) {
857 return NT_STATUS_INVALID_PARAMETER;
860 /* See if we can use the ADS connection struct swe were given */
863 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
864 "LDAP message (%p)\n", msg));
866 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
867 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
868 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
871 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
875 nt_status = NT_STATUS_OK;
879 /* Have to do our own query */
881 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
884 attrs[0] = ctx->ad_schema->posix_homedir_attr;
885 attrs[1] = ctx->ad_schema->posix_shell_attr;
886 attrs[2] = ctx->ad_schema->posix_gecos_attr;
887 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
889 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
890 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
894 nt_status = NT_STATUS_NO_MEMORY;
898 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
899 if (!ADS_ERR_OK(ads_status)) {
900 nt_status = ads_ntstatus(ads_status);
904 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
905 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
906 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
909 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
913 nt_status = NT_STATUS_OK;
917 ads_msgfree(ctx->ads, msg_internal);
923 /**********************************************************************
924 *********************************************************************/
926 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
927 struct nss_domain_entry *e,
931 const char *attrs[] = {NULL, /* attr_uid */
934 LDAPMessage *msg = NULL;
935 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
936 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
937 struct idmap_domain *dom;
938 struct idmap_ad_context *ctx = NULL;
940 /* Check incoming parameters */
942 if ( !e || !e->domain || !name || !*alias) {
943 nt_status = NT_STATUS_INVALID_PARAMETER;
947 /* Only do query if we are online */
949 if (idmap_is_offline()) {
950 nt_status = NT_STATUS_FILE_IS_OFFLINE;
954 dom = talloc_get_type(e->state, struct idmap_domain);
955 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
957 ads_status = ad_idmap_cached_connection(dom);
958 if (!ADS_ERR_OK(ads_status)) {
959 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
962 if (!ctx->ad_schema) {
963 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
967 attrs[0] = ctx->ad_schema->posix_uid_attr;
969 filter = talloc_asprintf(mem_ctx,
970 "(sAMAccountName=%s)",
973 nt_status = NT_STATUS_NO_MEMORY;
977 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
978 if (!ADS_ERR_OK(ads_status)) {
979 nt_status = ads_ntstatus(ads_status);
983 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
986 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
989 nt_status = NT_STATUS_OK;
993 talloc_destroy(filter);
996 ads_msgfree(ctx->ads, msg);
1002 /**********************************************************************
1003 *********************************************************************/
1005 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
1006 struct nss_domain_entry *e,
1010 const char *attrs[] = {"sAMAccountName",
1012 char *filter = NULL;
1013 LDAPMessage *msg = NULL;
1014 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1015 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1017 struct idmap_domain *dom;
1018 struct idmap_ad_context *ctx = NULL;
1020 /* Check incoming parameters */
1022 if ( !alias || !name) {
1023 nt_status = NT_STATUS_INVALID_PARAMETER;
1027 /* Only do query if we are online */
1029 if (idmap_is_offline()) {
1030 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1034 dom = talloc_get_type(e->state, struct idmap_domain);
1035 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1037 ads_status = ad_idmap_cached_connection(dom);
1038 if (!ADS_ERR_OK(ads_status)) {
1039 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1042 if (!ctx->ad_schema) {
1043 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1047 filter = talloc_asprintf(mem_ctx,
1049 ctx->ad_schema->posix_uid_attr,
1052 nt_status = NT_STATUS_NO_MEMORY;
1056 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1057 if (!ADS_ERR_OK(ads_status)) {
1058 nt_status = ads_ntstatus(ads_status);
1062 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1065 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1068 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1072 nt_status = NT_STATUS_NO_MEMORY;
1076 nt_status = NT_STATUS_OK;
1080 talloc_destroy(filter);
1083 ads_msgfree(ctx->ads, msg);
1090 /************************************************************************
1091 ***********************************************************************/
1093 static NTSTATUS nss_ad_close( void )
1095 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1097 return NT_STATUS_OK;
1100 /************************************************************************
1101 Function dispatch tables for the idmap and nss plugins
1102 ***********************************************************************/
1104 static struct idmap_methods ad_methods = {
1105 .init = idmap_ad_initialize,
1106 .unixids_to_sids = idmap_ad_unixids_to_sids,
1107 .sids_to_unixids = idmap_ad_sids_to_unixids,
1108 .close_fn = idmap_ad_close
1111 /* The SFU and RFC2307 NSS plugins share everything but the init
1112 function which sets the intended schema model to use */
1114 static struct nss_info_methods nss_rfc2307_methods = {
1115 .init = nss_rfc2307_init,
1116 .get_nss_info = nss_ad_get_info,
1117 .map_to_alias = nss_ad_map_to_alias,
1118 .map_from_alias = nss_ad_map_from_alias,
1119 .close_fn = nss_ad_close
1122 static struct nss_info_methods nss_sfu_methods = {
1123 .init = nss_sfu_init,
1124 .get_nss_info = nss_ad_get_info,
1125 .map_to_alias = nss_ad_map_to_alias,
1126 .map_from_alias = nss_ad_map_from_alias,
1127 .close_fn = nss_ad_close
1130 static struct nss_info_methods nss_sfu20_methods = {
1131 .init = nss_sfu20_init,
1132 .get_nss_info = nss_ad_get_info,
1133 .map_to_alias = nss_ad_map_to_alias,
1134 .map_from_alias = nss_ad_map_from_alias,
1135 .close_fn = nss_ad_close
1140 /************************************************************************
1141 Initialize the plugins
1142 ***********************************************************************/
1144 NTSTATUS idmap_ad_init(void)
1146 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1147 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1148 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1149 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1151 /* Always register the AD method first in order to get the
1152 idmap_domain interface called */
1154 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1155 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1157 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1158 return status_idmap_ad;
1161 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1162 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1163 "rfc2307", &nss_rfc2307_methods );
1164 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1165 return status_nss_rfc2307;
1168 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1169 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1170 "sfu", &nss_sfu_methods );
1171 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1172 return status_nss_sfu;
1175 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1176 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1177 "sfu20", &nss_sfu20_methods );
1178 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1179 return status_nss_sfu20;
1182 return NT_STATUS_OK;