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 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_cached_connection_internal: failed to "
155 ads->is_mine = False;
162 /************************************************************************
163 ***********************************************************************/
165 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
168 struct idmap_ad_context * ctx;
170 status = ad_idmap_cached_connection_internal(dom);
171 if (!ADS_ERR_OK(status)) {
175 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
177 /* if we have a valid ADS_STRUCT and the schema model is
178 defined, then we can return here. */
180 if ( ctx->ad_schema ) {
184 /* Otherwise, set the schema model */
186 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
187 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
188 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
190 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
191 if ( !ADS_ERR_OK(status) ) {
192 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
199 /************************************************************************
200 ***********************************************************************/
202 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
205 struct idmap_ad_context *ctx;
207 const char *schema_mode = NULL;
209 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
211 DEBUG(0, ("Out of memory!\n"));
212 return NT_STATUS_NO_MEMORY;
215 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
216 if (config_option == NULL) {
217 DEBUG(0, ("Out of memory!\n"));
219 return NT_STATUS_NO_MEMORY;
222 /* default map type */
223 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
226 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
227 if ( schema_mode && schema_mode[0] ) {
228 if ( strequal(schema_mode, "sfu") )
229 ctx->ad_map_type = WB_POSIX_MAP_SFU;
230 else if ( strequal(schema_mode, "sfu20" ) )
231 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
232 else if ( strequal(schema_mode, "rfc2307" ) )
233 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
235 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
239 dom->private_data = ctx;
241 talloc_free(config_option);
246 /************************************************************************
247 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
248 ***********************************************************************/
250 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
254 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
255 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
263 /************************************************************************
264 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
265 ***********************************************************************/
267 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
271 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
272 if (dom_sid_equal(maps[i]->sid, sid)) {
280 /************************************************************************
281 ***********************************************************************/
283 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
287 struct idmap_ad_context *ctx;
289 const char *attrs[] = { "sAMAccountType",
291 NULL, /* uidnumber */
292 NULL, /* gidnumber */
294 LDAPMessage *res = NULL;
295 LDAPMessage *entry = NULL;
301 char *u_filter = NULL;
302 char *g_filter = NULL;
304 /* initialize the status to avoid suprise */
305 for (i = 0; ids[i]; i++) {
306 ids[i]->status = ID_UNKNOWN;
309 /* Only do query if we are online */
310 if (idmap_is_offline()) {
311 return NT_STATUS_FILE_IS_OFFLINE;
314 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
316 if ( (memctx = talloc_new(ctx)) == NULL ) {
317 DEBUG(0, ("Out of memory!\n"));
318 return NT_STATUS_NO_MEMORY;
321 rc = ad_idmap_cached_connection(dom);
322 if (!ADS_ERR_OK(rc)) {
323 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
324 ret = NT_STATUS_UNSUCCESSFUL;
325 /* ret = ads_ntstatus(rc); */
329 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
330 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
334 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
335 switch (ids[idx]->xid.type) {
338 u_filter = talloc_asprintf(memctx, "(&(|"
339 "(sAMAccountType=%d)"
340 "(sAMAccountType=%d)"
341 "(sAMAccountType=%d))(|",
342 ATYPE_NORMAL_ACCOUNT,
343 ATYPE_WORKSTATION_TRUST,
344 ATYPE_INTERDOMAIN_TRUST);
346 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
347 ctx->ad_schema->posix_uidnumber_attr,
348 (unsigned long)ids[idx]->xid.id);
349 CHECK_ALLOC_DONE(u_filter);
354 g_filter = talloc_asprintf(memctx, "(&(|"
355 "(sAMAccountType=%d)"
356 "(sAMAccountType=%d))(|",
357 ATYPE_SECURITY_GLOBAL_GROUP,
358 ATYPE_SECURITY_LOCAL_GROUP);
360 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
361 ctx->ad_schema->posix_gidnumber_attr,
362 (unsigned long)ids[idx]->xid.id);
363 CHECK_ALLOC_DONE(g_filter);
367 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
368 ids[idx]->status = ID_UNKNOWN;
372 filter = talloc_asprintf(memctx, "(|");
373 CHECK_ALLOC_DONE(filter);
375 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
376 CHECK_ALLOC_DONE(filter);
377 TALLOC_FREE(u_filter);
380 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
381 CHECK_ALLOC_DONE(filter);
382 TALLOC_FREE(g_filter);
384 filter = talloc_asprintf_append_buffer(filter, ")");
385 CHECK_ALLOC_DONE(filter);
387 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
388 if (!ADS_ERR_OK(rc)) {
389 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
390 ret = NT_STATUS_UNSUCCESSFUL;
394 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
395 DEBUG(10, ("No IDs found\n"));
399 for (i = 0; (i < count) && entry; i++) {
406 if (i == 0) { /* first entry */
407 entry = ads_first_entry(ctx->ads, entry);
408 } else { /* following ones */
409 entry = ads_next_entry(ctx->ads, entry);
413 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
417 /* first check if the SID is present */
418 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
419 DEBUG(2, ("Could not retrieve SID from entry\n"));
424 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
425 DEBUG(1, ("could not get SAM account type\n"));
429 switch (atype & 0xF0000000) {
430 case ATYPE_SECURITY_GLOBAL_GROUP:
431 case ATYPE_SECURITY_LOCAL_GROUP:
434 case ATYPE_NORMAL_ACCOUNT:
435 case ATYPE_WORKSTATION_TRUST:
436 case ATYPE_INTERDOMAIN_TRUST:
440 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
444 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
445 ctx->ad_schema->posix_uidnumber_attr :
446 ctx->ad_schema->posix_gidnumber_attr,
449 DEBUG(1, ("Could not get unix ID\n"));
453 if (!idmap_unix_id_is_in_range(id, dom)) {
454 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
455 id, dom->low_id, dom->high_id));
459 map = find_map_by_id(&ids[bidx], type, id);
461 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
465 sid_copy(map->sid, &sid);
468 map->status = ID_MAPPED;
470 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
471 (unsigned long)map->xid.id,
476 ads_msgfree(ctx->ads, res);
479 if (ids[idx]) { /* still some values to map */
485 /* mark all unknown/expired ones as unmapped */
486 for (i = 0; ids[i]; i++) {
487 if (ids[i]->status != ID_MAPPED)
488 ids[i]->status = ID_UNMAPPED;
496 /************************************************************************
497 ***********************************************************************/
499 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
503 struct idmap_ad_context *ctx;
505 const char *attrs[] = { "sAMAccountType",
507 NULL, /* attr_uidnumber */
508 NULL, /* attr_gidnumber */
510 LDAPMessage *res = NULL;
511 LDAPMessage *entry = NULL;
519 /* initialize the status to avoid suprise */
520 for (i = 0; ids[i]; i++) {
521 ids[i]->status = ID_UNKNOWN;
524 /* Only do query if we are online */
525 if (idmap_is_offline()) {
526 return NT_STATUS_FILE_IS_OFFLINE;
529 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
531 if ( (memctx = talloc_new(ctx)) == NULL ) {
532 DEBUG(0, ("Out of memory!\n"));
533 return NT_STATUS_NO_MEMORY;
536 rc = ad_idmap_cached_connection(dom);
537 if (!ADS_ERR_OK(rc)) {
538 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
539 ret = NT_STATUS_UNSUCCESSFUL;
540 /* ret = ads_ntstatus(rc); */
544 if (ctx->ad_schema == NULL) {
545 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
546 ret = NT_STATUS_UNSUCCESSFUL;
550 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
551 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
554 filter = talloc_asprintf(memctx, "(&(|"
555 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
556 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
558 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
559 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
561 CHECK_ALLOC_DONE(filter);
564 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
566 ids[idx]->status = ID_UNKNOWN;
568 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), ids[idx]->sid);
569 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
572 CHECK_ALLOC_DONE(filter);
574 filter = talloc_asprintf_append_buffer(filter, "))");
575 CHECK_ALLOC_DONE(filter);
576 DEBUG(10, ("Filter: [%s]\n", filter));
578 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
579 if (!ADS_ERR_OK(rc)) {
580 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
581 ret = NT_STATUS_UNSUCCESSFUL;
585 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
586 DEBUG(10, ("No IDs found\n"));
590 for (i = 0; (i < count) && entry; i++) {
597 if (i == 0) { /* first entry */
598 entry = ads_first_entry(ctx->ads, entry);
599 } else { /* following ones */
600 entry = ads_next_entry(ctx->ads, entry);
604 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
608 /* first check if the SID is present */
609 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
610 DEBUG(2, ("Could not retrieve SID from entry\n"));
614 map = find_map_by_sid(&ids[bidx], &sid);
616 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
621 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
622 DEBUG(1, ("could not get SAM account type\n"));
626 switch (atype & 0xF0000000) {
627 case ATYPE_SECURITY_GLOBAL_GROUP:
628 case ATYPE_SECURITY_LOCAL_GROUP:
631 case ATYPE_NORMAL_ACCOUNT:
632 case ATYPE_WORKSTATION_TRUST:
633 case ATYPE_INTERDOMAIN_TRUST:
637 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
641 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
642 ctx->ad_schema->posix_uidnumber_attr :
643 ctx->ad_schema->posix_gidnumber_attr,
646 DEBUG(1, ("Could not get unix ID\n"));
649 if (!idmap_unix_id_is_in_range(id, dom)) {
650 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
651 id, dom->low_id, dom->high_id));
656 map->xid.type = type;
658 map->status = ID_MAPPED;
660 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
661 (unsigned long)map->xid.id,
666 ads_msgfree(ctx->ads, res);
669 if (ids[idx]) { /* still some values to map */
675 /* mark all unknwoni/expired ones as unmapped */
676 for (i = 0; ids[i]; i++) {
677 if (ids[i]->status != ID_MAPPED)
678 ids[i]->status = ID_UNMAPPED;
686 /************************************************************************
687 ***********************************************************************/
689 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
691 struct idmap_ad_context * ctx;
693 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
695 if (ctx->ads != NULL) {
696 /* we own this ADS_STRUCT so make sure it goes away */
697 ctx->ads->is_mine = True;
698 ads_destroy( &ctx->ads );
702 TALLOC_FREE( ctx->ad_schema );
708 * nss_info_{sfu,sfu20,rfc2307}
711 /************************************************************************
712 Initialize the {sfu,sfu20,rfc2307} state
713 ***********************************************************************/
715 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
716 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
717 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
718 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
719 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
720 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
722 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
725 case WB_POSIX_MAP_TEMPLATE:
726 return wb_posix_map_template_string;
727 case WB_POSIX_MAP_SFU:
728 return wb_posix_map_sfu_string;
729 case WB_POSIX_MAP_SFU20:
730 return wb_posix_map_sfu20_string;
731 case WB_POSIX_MAP_RFC2307:
732 return wb_posix_map_rfc2307_string;
733 case WB_POSIX_MAP_UNIXINFO:
734 return wb_posix_map_unixinfo_string;
736 return wb_posix_map_unknown_string;
740 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
741 enum wb_posix_mapping new_ad_map_type)
743 struct idmap_domain *dom;
744 struct idmap_ad_context *ctx;
746 if (e->state != NULL) {
747 dom = talloc_get_type(e->state, struct idmap_domain);
749 dom = TALLOC_ZERO_P(e, struct idmap_domain);
751 DEBUG(0, ("Out of memory!\n"));
752 return NT_STATUS_NO_MEMORY;
757 if (e->domain != NULL) {
758 dom->name = talloc_strdup(dom, e->domain);
759 if (dom->name == NULL) {
760 DEBUG(0, ("Out of memory!\n"));
761 return NT_STATUS_NO_MEMORY;
765 if (dom->private_data != NULL) {
766 ctx = talloc_get_type(dom->private_data,
767 struct idmap_ad_context);
769 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
771 DEBUG(0, ("Out of memory!\n"));
772 return NT_STATUS_NO_MEMORY;
774 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
775 dom->private_data = ctx;
778 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
779 (ctx->ad_map_type != new_ad_map_type))
781 DEBUG(2, ("nss_ad_generic_init: "
782 "Warning: overriding previously set posix map type "
783 "%s for domain %s with map type %s.\n",
784 ad_map_type_string(ctx->ad_map_type),
786 ad_map_type_string(new_ad_map_type)));
789 ctx->ad_map_type = new_ad_map_type;
794 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
796 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
799 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
801 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
804 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
806 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
810 /************************************************************************
811 ***********************************************************************/
813 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
814 const struct dom_sid *sid,
818 const char **homedir,
823 const char *attrs[] = {NULL, /* attr_homedir */
824 NULL, /* attr_shell */
825 NULL, /* attr_gecos */
826 NULL, /* attr_gidnumber */
829 LDAPMessage *msg_internal = NULL;
830 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
831 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
833 struct idmap_domain *dom;
834 struct idmap_ad_context *ctx;
836 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
837 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
839 /* Only do query if we are online */
840 if (idmap_is_offline()) {
841 return NT_STATUS_FILE_IS_OFFLINE;
844 dom = talloc_get_type(e->state, struct idmap_domain);
845 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
847 ads_status = ad_idmap_cached_connection(dom);
848 if (!ADS_ERR_OK(ads_status)) {
849 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
852 if (!ctx->ad_schema) {
853 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
854 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
857 if (!sid || !homedir || !shell || !gecos) {
858 return NT_STATUS_INVALID_PARAMETER;
861 /* See if we can use the ADS connection struct swe were given */
865 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
866 "LDAP message (%p)\n", msg));
868 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
869 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
870 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
873 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
877 nt_status = NT_STATUS_OK;
882 /* Have to do our own query */
884 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
887 attrs[0] = ctx->ad_schema->posix_homedir_attr;
888 attrs[1] = ctx->ad_schema->posix_shell_attr;
889 attrs[2] = ctx->ad_schema->posix_gecos_attr;
890 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
892 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
893 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
897 nt_status = NT_STATUS_NO_MEMORY;
901 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
902 if (!ADS_ERR_OK(ads_status)) {
903 nt_status = ads_ntstatus(ads_status);
907 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
908 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
909 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
912 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
916 nt_status = NT_STATUS_OK;
920 ads_msgfree(ctx->ads, msg_internal);
926 /**********************************************************************
927 *********************************************************************/
929 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
930 struct nss_domain_entry *e,
934 const char *attrs[] = {NULL, /* attr_uid */
937 LDAPMessage *msg = NULL;
938 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
939 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
940 struct idmap_domain *dom;
941 struct idmap_ad_context *ctx = NULL;
943 /* Check incoming parameters */
945 if ( !e || !e->domain || !name || !*alias) {
946 nt_status = NT_STATUS_INVALID_PARAMETER;
950 /* Only do query if we are online */
952 if (idmap_is_offline()) {
953 nt_status = NT_STATUS_FILE_IS_OFFLINE;
957 dom = talloc_get_type(e->state, struct idmap_domain);
958 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
960 ads_status = ad_idmap_cached_connection(dom);
961 if (!ADS_ERR_OK(ads_status)) {
962 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
965 if (!ctx->ad_schema) {
966 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
970 attrs[0] = ctx->ad_schema->posix_uid_attr;
972 filter = talloc_asprintf(mem_ctx,
973 "(sAMAccountName=%s)",
976 nt_status = NT_STATUS_NO_MEMORY;
980 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
981 if (!ADS_ERR_OK(ads_status)) {
982 nt_status = ads_ntstatus(ads_status);
986 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
989 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
992 nt_status = NT_STATUS_OK;
996 talloc_destroy(filter);
999 ads_msgfree(ctx->ads, msg);
1005 /**********************************************************************
1006 *********************************************************************/
1008 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
1009 struct nss_domain_entry *e,
1013 const char *attrs[] = {"sAMAccountName",
1015 char *filter = NULL;
1016 LDAPMessage *msg = NULL;
1017 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1018 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1020 struct idmap_domain *dom;
1021 struct idmap_ad_context *ctx = NULL;
1023 /* Check incoming parameters */
1025 if ( !alias || !name) {
1026 nt_status = NT_STATUS_INVALID_PARAMETER;
1030 /* Only do query if we are online */
1032 if (idmap_is_offline()) {
1033 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1037 dom = talloc_get_type(e->state, struct idmap_domain);
1038 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1040 ads_status = ad_idmap_cached_connection(dom);
1041 if (!ADS_ERR_OK(ads_status)) {
1042 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1045 if (!ctx->ad_schema) {
1046 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1050 filter = talloc_asprintf(mem_ctx,
1052 ctx->ad_schema->posix_uid_attr,
1055 nt_status = NT_STATUS_NO_MEMORY;
1059 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1060 if (!ADS_ERR_OK(ads_status)) {
1061 nt_status = ads_ntstatus(ads_status);
1065 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1068 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1071 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1075 nt_status = NT_STATUS_NO_MEMORY;
1079 nt_status = NT_STATUS_OK;
1083 talloc_destroy(filter);
1086 ads_msgfree(ctx->ads, msg);
1093 /************************************************************************
1094 ***********************************************************************/
1096 static NTSTATUS nss_ad_close( void )
1098 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1100 return NT_STATUS_OK;
1103 /************************************************************************
1104 Function dispatch tables for the idmap and nss plugins
1105 ***********************************************************************/
1107 static struct idmap_methods ad_methods = {
1108 .init = idmap_ad_initialize,
1109 .unixids_to_sids = idmap_ad_unixids_to_sids,
1110 .sids_to_unixids = idmap_ad_sids_to_unixids,
1111 .close_fn = idmap_ad_close
1114 /* The SFU and RFC2307 NSS plugins share everything but the init
1115 function which sets the intended schema model to use */
1117 static struct nss_info_methods nss_rfc2307_methods = {
1118 .init = nss_rfc2307_init,
1119 .get_nss_info = nss_ad_get_info,
1120 .map_to_alias = nss_ad_map_to_alias,
1121 .map_from_alias = nss_ad_map_from_alias,
1122 .close_fn = nss_ad_close
1125 static struct nss_info_methods nss_sfu_methods = {
1126 .init = nss_sfu_init,
1127 .get_nss_info = nss_ad_get_info,
1128 .map_to_alias = nss_ad_map_to_alias,
1129 .map_from_alias = nss_ad_map_from_alias,
1130 .close_fn = nss_ad_close
1133 static struct nss_info_methods nss_sfu20_methods = {
1134 .init = nss_sfu20_init,
1135 .get_nss_info = nss_ad_get_info,
1136 .map_to_alias = nss_ad_map_to_alias,
1137 .map_from_alias = nss_ad_map_from_alias,
1138 .close_fn = nss_ad_close
1143 /************************************************************************
1144 Initialize the plugins
1145 ***********************************************************************/
1147 NTSTATUS idmap_ad_init(void)
1149 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1150 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1151 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1152 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1154 /* Always register the AD method first in order to get the
1155 idmap_domain interface called */
1157 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1158 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1160 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1161 return status_idmap_ad;
1164 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1165 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1166 "rfc2307", &nss_rfc2307_methods );
1167 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1168 return status_nss_rfc2307;
1171 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1172 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1173 "sfu", &nss_sfu_methods );
1174 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1175 return status_nss_sfu;
1178 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1179 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1180 "sfu20", &nss_sfu20_methods );
1181 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1182 return status_nss_sfu20;
1185 return NT_STATUS_OK;