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 #define DBGC_CLASS DBGC_IDMAP
38 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
40 #define IDMAP_AD_MAX_IDS 30
41 #define CHECK_ALLOC_DONE(mem) do { \
43 DEBUG(0, ("Out of memory!\n")); \
44 ret = NT_STATUS_NO_MEMORY; \
49 struct idmap_ad_context {
50 uint32_t filter_low_id;
51 uint32_t filter_high_id;
53 struct posix_schema *ad_schema;
54 enum wb_posix_mapping ad_map_type; /* WB_POSIX_MAP_UNKNOWN */
57 NTSTATUS init_module(void);
59 /************************************************************************
60 ***********************************************************************/
62 static ADS_STATUS ad_idmap_cached_connection_internal(struct idmap_domain *dom)
68 struct sockaddr_storage dc_ip;
69 struct idmap_ad_context *ctx;
70 char *ldap_server = NULL;
72 struct winbindd_domain *wb_dom;
74 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
77 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
79 if (ctx->ads != NULL) {
82 time_t now = time(NULL);
86 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
88 /* check for a valid structure */
89 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
90 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
92 if ( ads->config.realm && (expire > time(NULL))) {
95 /* we own this ADS_STRUCT so make sure it goes away */
96 DEBUG(7,("Deleting expired krb5 credential cache\n"));
99 ads_kdestroy(WINBIND_CCACHE_NAME);
101 TALLOC_FREE( ctx->ad_schema );
106 /* we don't want this to affect the users ccache */
107 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
111 * At this point we only have the NetBIOS domain name.
112 * Check if we can get server nam and realm from SAF cache
113 * and the domain list.
115 ldap_server = saf_fetch(dom->name);
116 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server?ldap_server:""));
118 wb_dom = find_domain_from_name_noinit(dom->name);
119 if (wb_dom == NULL) {
120 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
124 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
125 " domain '%s'\n", wb_dom->alt_name, dom->name));
126 realm = wb_dom->alt_name;
129 if ( (ads = ads_init(realm, dom->name, ldap_server)) == NULL ) {
130 DEBUG(1,("ads_init failed\n"));
131 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
134 /* the machine acct password might have change - fetch it every time */
135 SAFE_FREE(ads->auth.password);
136 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
138 SAFE_FREE(ads->auth.realm);
139 ads->auth.realm = SMB_STRDUP(lp_realm());
141 /* setup server affinity */
143 get_dc_name(dom->name, realm, dc_name, &dc_ip );
145 status = ads_connect(ads);
146 if (!ADS_ERR_OK(status)) {
147 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
152 ads->is_mine = False;
159 /************************************************************************
160 ***********************************************************************/
162 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
165 struct idmap_ad_context * ctx;
167 status = ad_idmap_cached_connection_internal(dom);
168 if (!ADS_ERR_OK(status)) {
172 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
174 /* if we have a valid ADS_STRUCT and the schema model is
175 defined, then we can return here. */
177 if ( ctx->ad_schema ) {
181 /* Otherwise, set the schema model */
183 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
184 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
185 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
187 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
188 if ( !ADS_ERR_OK(status) ) {
189 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
196 /************************************************************************
197 ***********************************************************************/
199 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
202 struct idmap_ad_context *ctx;
204 const char *range = NULL;
205 const char *schema_mode = NULL;
207 if ( (ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context)) == NULL ) {
208 DEBUG(0, ("Out of memory!\n"));
209 return NT_STATUS_NO_MEMORY;
212 if ( (config_option = talloc_asprintf(ctx, "idmap config %s", dom->name)) == NULL ) {
213 DEBUG(0, ("Out of memory!\n"));
215 return NT_STATUS_NO_MEMORY;
219 range = lp_parm_const_string(-1, config_option, "range", NULL);
220 if (range && range[0]) {
221 if ((sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
222 (ctx->filter_low_id > ctx->filter_high_id)) {
223 DEBUG(1, ("ERROR: invalid filter range [%s]", range));
224 ctx->filter_low_id = 0;
225 ctx->filter_high_id = 0;
229 /* default map type */
230 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
233 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
234 if ( schema_mode && schema_mode[0] ) {
235 if ( strequal(schema_mode, "sfu") )
236 ctx->ad_map_type = WB_POSIX_MAP_SFU;
237 else if ( strequal(schema_mode, "sfu20" ) )
238 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
239 else if ( strequal(schema_mode, "rfc2307" ) )
240 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
242 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
246 dom->private_data = ctx;
248 talloc_free(config_option);
253 /************************************************************************
254 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
255 ***********************************************************************/
257 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
261 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
262 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
270 /************************************************************************
271 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
272 ***********************************************************************/
274 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
278 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
279 if (sid_equal(maps[i]->sid, sid)) {
287 /************************************************************************
288 ***********************************************************************/
290 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
294 struct idmap_ad_context *ctx;
296 const char *attrs[] = { "sAMAccountType",
298 NULL, /* uidnumber */
299 NULL, /* gidnumber */
301 LDAPMessage *res = NULL;
302 LDAPMessage *entry = NULL;
308 char *u_filter = NULL;
309 char *g_filter = NULL;
311 /* initialize the status to avoid suprise */
312 for (i = 0; ids[i]; i++) {
313 ids[i]->status = ID_UNKNOWN;
316 /* Only do query if we are online */
317 if (idmap_is_offline()) {
318 return NT_STATUS_FILE_IS_OFFLINE;
321 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
323 if ( (memctx = talloc_new(ctx)) == NULL ) {
324 DEBUG(0, ("Out of memory!\n"));
325 return NT_STATUS_NO_MEMORY;
328 rc = ad_idmap_cached_connection(dom);
329 if (!ADS_ERR_OK(rc)) {
330 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
331 ret = NT_STATUS_UNSUCCESSFUL;
332 /* ret = ads_ntstatus(rc); */
336 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
337 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
341 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
342 switch (ids[idx]->xid.type) {
345 u_filter = talloc_asprintf(memctx, "(&(|"
346 "(sAMAccountType=%d)"
347 "(sAMAccountType=%d)"
348 "(sAMAccountType=%d))(|",
349 ATYPE_NORMAL_ACCOUNT,
350 ATYPE_WORKSTATION_TRUST,
351 ATYPE_INTERDOMAIN_TRUST);
353 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
354 ctx->ad_schema->posix_uidnumber_attr,
355 (unsigned long)ids[idx]->xid.id);
356 CHECK_ALLOC_DONE(u_filter);
361 g_filter = talloc_asprintf(memctx, "(&(|"
362 "(sAMAccountType=%d)"
363 "(sAMAccountType=%d))(|",
364 ATYPE_SECURITY_GLOBAL_GROUP,
365 ATYPE_SECURITY_LOCAL_GROUP);
367 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
368 ctx->ad_schema->posix_gidnumber_attr,
369 (unsigned long)ids[idx]->xid.id);
370 CHECK_ALLOC_DONE(g_filter);
374 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
375 ids[idx]->status = ID_UNKNOWN;
379 filter = talloc_asprintf(memctx, "(|");
380 CHECK_ALLOC_DONE(filter);
382 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
383 CHECK_ALLOC_DONE(filter);
384 TALLOC_FREE(u_filter);
387 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
388 CHECK_ALLOC_DONE(filter);
389 TALLOC_FREE(g_filter);
391 filter = talloc_asprintf_append_buffer(filter, ")");
392 CHECK_ALLOC_DONE(filter);
394 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
395 if (!ADS_ERR_OK(rc)) {
396 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
397 ret = NT_STATUS_UNSUCCESSFUL;
401 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
402 DEBUG(10, ("No IDs found\n"));
406 for (i = 0; (i < count) && entry; i++) {
413 if (i == 0) { /* first entry */
414 entry = ads_first_entry(ctx->ads, entry);
415 } else { /* following ones */
416 entry = ads_next_entry(ctx->ads, entry);
420 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
424 /* first check if the SID is present */
425 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
426 DEBUG(2, ("Could not retrieve SID from entry\n"));
431 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
432 DEBUG(1, ("could not get SAM account type\n"));
436 switch (atype & 0xF0000000) {
437 case ATYPE_SECURITY_GLOBAL_GROUP:
438 case ATYPE_SECURITY_LOCAL_GROUP:
441 case ATYPE_NORMAL_ACCOUNT:
442 case ATYPE_WORKSTATION_TRUST:
443 case ATYPE_INTERDOMAIN_TRUST:
447 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
451 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
452 ctx->ad_schema->posix_uidnumber_attr :
453 ctx->ad_schema->posix_gidnumber_attr,
456 DEBUG(1, ("Could not get unix ID\n"));
461 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
462 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
463 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
464 id, ctx->filter_low_id, ctx->filter_high_id));
468 map = find_map_by_id(&ids[bidx], type, id);
470 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
474 sid_copy(map->sid, &sid);
477 map->status = ID_MAPPED;
479 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
480 (unsigned long)map->xid.id,
485 ads_msgfree(ctx->ads, res);
488 if (ids[idx]) { /* still some values to map */
494 /* mark all unknown/expired ones as unmapped */
495 for (i = 0; ids[i]; i++) {
496 if (ids[i]->status != ID_MAPPED)
497 ids[i]->status = ID_UNMAPPED;
505 /************************************************************************
506 ***********************************************************************/
508 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
512 struct idmap_ad_context *ctx;
514 const char *attrs[] = { "sAMAccountType",
516 NULL, /* attr_uidnumber */
517 NULL, /* attr_gidnumber */
519 LDAPMessage *res = NULL;
520 LDAPMessage *entry = NULL;
528 /* initialize the status to avoid suprise */
529 for (i = 0; ids[i]; i++) {
530 ids[i]->status = ID_UNKNOWN;
533 /* Only do query if we are online */
534 if (idmap_is_offline()) {
535 return NT_STATUS_FILE_IS_OFFLINE;
538 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
540 if ( (memctx = talloc_new(ctx)) == NULL ) {
541 DEBUG(0, ("Out of memory!\n"));
542 return NT_STATUS_NO_MEMORY;
545 rc = ad_idmap_cached_connection(dom);
546 if (!ADS_ERR_OK(rc)) {
547 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
548 ret = NT_STATUS_UNSUCCESSFUL;
549 /* ret = ads_ntstatus(rc); */
553 if (ctx->ad_schema == NULL) {
554 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
555 ret = NT_STATUS_UNSUCCESSFUL;
559 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
560 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
563 filter = talloc_asprintf(memctx, "(&(|"
564 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
565 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
567 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
568 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
570 CHECK_ALLOC_DONE(filter);
573 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
575 ids[idx]->status = ID_UNKNOWN;
577 sidstr = sid_binstring(talloc_tos(), ids[idx]->sid);
578 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
581 CHECK_ALLOC_DONE(filter);
583 filter = talloc_asprintf_append_buffer(filter, "))");
584 CHECK_ALLOC_DONE(filter);
585 DEBUG(10, ("Filter: [%s]\n", filter));
587 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
588 if (!ADS_ERR_OK(rc)) {
589 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
590 ret = NT_STATUS_UNSUCCESSFUL;
594 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
595 DEBUG(10, ("No IDs found\n"));
599 for (i = 0; (i < count) && entry; i++) {
606 if (i == 0) { /* first entry */
607 entry = ads_first_entry(ctx->ads, entry);
608 } else { /* following ones */
609 entry = ads_next_entry(ctx->ads, entry);
613 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
617 /* first check if the SID is present */
618 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
619 DEBUG(2, ("Could not retrieve SID from entry\n"));
623 map = find_map_by_sid(&ids[bidx], &sid);
625 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
630 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
631 DEBUG(1, ("could not get SAM account type\n"));
635 switch (atype & 0xF0000000) {
636 case ATYPE_SECURITY_GLOBAL_GROUP:
637 case ATYPE_SECURITY_LOCAL_GROUP:
640 case ATYPE_NORMAL_ACCOUNT:
641 case ATYPE_WORKSTATION_TRUST:
642 case ATYPE_INTERDOMAIN_TRUST:
646 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
650 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
651 ctx->ad_schema->posix_uidnumber_attr :
652 ctx->ad_schema->posix_gidnumber_attr,
655 DEBUG(1, ("Could not get unix ID\n"));
659 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
660 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
661 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
662 id, ctx->filter_low_id, ctx->filter_high_id));
667 map->xid.type = type;
669 map->status = ID_MAPPED;
671 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
672 (unsigned long)map->xid.id,
677 ads_msgfree(ctx->ads, res);
680 if (ids[idx]) { /* still some values to map */
686 /* mark all unknwoni/expired ones as unmapped */
687 for (i = 0; ids[i]; i++) {
688 if (ids[i]->status != ID_MAPPED)
689 ids[i]->status = ID_UNMAPPED;
697 /************************************************************************
698 ***********************************************************************/
700 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
702 struct idmap_ad_context * ctx;
704 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
706 if (ctx->ads != NULL) {
707 /* we own this ADS_STRUCT so make sure it goes away */
708 ctx->ads->is_mine = True;
709 ads_destroy( &ctx->ads );
713 TALLOC_FREE( ctx->ad_schema );
719 * nss_info_{sfu,sfu20,rfc2307}
722 /************************************************************************
723 Initialize the {sfu,sfu20,rfc2307} state
724 ***********************************************************************/
726 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
727 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
728 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
729 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
730 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
731 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
733 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
736 case WB_POSIX_MAP_TEMPLATE:
737 return wb_posix_map_template_string;
738 case WB_POSIX_MAP_SFU:
739 return wb_posix_map_sfu_string;
740 case WB_POSIX_MAP_SFU20:
741 return wb_posix_map_sfu20_string;
742 case WB_POSIX_MAP_RFC2307:
743 return wb_posix_map_rfc2307_string;
744 case WB_POSIX_MAP_UNIXINFO:
745 return wb_posix_map_unixinfo_string;
747 return wb_posix_map_unknown_string;
751 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
752 enum wb_posix_mapping new_ad_map_type)
754 struct idmap_domain *dom;
755 struct idmap_ad_context *ctx;
757 if (e->state != NULL) {
758 dom = talloc_get_type(e->state, struct idmap_domain);
760 dom = TALLOC_ZERO_P(e, struct idmap_domain);
762 DEBUG(0, ("Out of memory!\n"));
763 return NT_STATUS_NO_MEMORY;
768 if (e->domain != NULL) {
769 dom->name = talloc_strdup(dom, e->domain);
770 if (dom->name == NULL) {
771 DEBUG(0, ("Out of memory!\n"));
772 return NT_STATUS_NO_MEMORY;
776 if (dom->private_data != NULL) {
777 ctx = talloc_get_type(dom->private_data,
778 struct idmap_ad_context);
780 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
782 DEBUG(0, ("Out of memory!\n"));
783 return NT_STATUS_NO_MEMORY;
785 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
786 dom->private_data = ctx;
789 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
790 (ctx->ad_map_type != new_ad_map_type))
792 DEBUG(2, ("nss_ad_generic_init: "
793 "Warning: overriding previously set posix map type "
794 "%s for domain %s with map type %s.\n",
795 ad_map_type_string(ctx->ad_map_type),
797 ad_map_type_string(new_ad_map_type)));
800 ctx->ad_map_type = new_ad_map_type;
805 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
807 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
810 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
812 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
815 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
817 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
821 /************************************************************************
822 ***********************************************************************/
824 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
825 const struct dom_sid *sid,
829 const char **homedir,
834 const char *attrs[] = {NULL, /* attr_homedir */
835 NULL, /* attr_shell */
836 NULL, /* attr_gecos */
837 NULL, /* attr_gidnumber */
840 LDAPMessage *msg_internal = NULL;
841 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
842 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
844 struct idmap_domain *dom;
845 struct idmap_ad_context *ctx;
847 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
848 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
850 /* Only do query if we are online */
851 if (idmap_is_offline()) {
852 return NT_STATUS_FILE_IS_OFFLINE;
855 dom = talloc_get_type(e->state, struct idmap_domain);
856 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
858 ads_status = ad_idmap_cached_connection(dom);
859 if (!ADS_ERR_OK(ads_status)) {
860 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
863 if (!ctx->ad_schema) {
864 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
865 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
868 if (!sid || !homedir || !shell || !gecos) {
869 return NT_STATUS_INVALID_PARAMETER;
872 /* See if we can use the ADS connection struct swe were given */
875 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
876 "LDAP message (%p)\n", msg));
878 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
879 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
880 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
883 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
887 nt_status = NT_STATUS_OK;
891 /* Have to do our own query */
893 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
896 attrs[0] = ctx->ad_schema->posix_homedir_attr;
897 attrs[1] = ctx->ad_schema->posix_shell_attr;
898 attrs[2] = ctx->ad_schema->posix_gecos_attr;
899 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
901 sidstr = sid_binstring(mem_ctx, sid);
902 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
906 nt_status = NT_STATUS_NO_MEMORY;
910 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
911 if (!ADS_ERR_OK(ads_status)) {
912 nt_status = ads_ntstatus(ads_status);
916 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
917 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
918 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
921 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
925 nt_status = NT_STATUS_OK;
929 ads_msgfree(ctx->ads, msg_internal);
935 /**********************************************************************
936 *********************************************************************/
938 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
939 struct nss_domain_entry *e,
943 const char *attrs[] = {NULL, /* attr_uid */
946 LDAPMessage *msg = NULL;
947 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
948 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
949 struct idmap_domain *dom;
950 struct idmap_ad_context *ctx = NULL;
952 /* Check incoming parameters */
954 if ( !e || !e->domain || !name || !*alias) {
955 nt_status = NT_STATUS_INVALID_PARAMETER;
959 /* Only do query if we are online */
961 if (idmap_is_offline()) {
962 nt_status = NT_STATUS_FILE_IS_OFFLINE;
966 dom = talloc_get_type(e->state, struct idmap_domain);
967 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
969 ads_status = ad_idmap_cached_connection(dom);
970 if (!ADS_ERR_OK(ads_status)) {
971 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
974 if (!ctx->ad_schema) {
975 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
979 attrs[0] = ctx->ad_schema->posix_uid_attr;
981 filter = talloc_asprintf(mem_ctx,
982 "(sAMAccountName=%s)",
985 nt_status = NT_STATUS_NO_MEMORY;
989 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
990 if (!ADS_ERR_OK(ads_status)) {
991 nt_status = ads_ntstatus(ads_status);
995 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
998 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1001 nt_status = NT_STATUS_OK;
1005 talloc_destroy(filter);
1008 ads_msgfree(ctx->ads, msg);
1014 /**********************************************************************
1015 *********************************************************************/
1017 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
1018 struct nss_domain_entry *e,
1022 const char *attrs[] = {"sAMAccountName",
1024 char *filter = NULL;
1025 LDAPMessage *msg = NULL;
1026 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1027 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1029 struct idmap_domain *dom;
1030 struct idmap_ad_context *ctx = NULL;
1032 /* Check incoming parameters */
1034 if ( !alias || !name) {
1035 nt_status = NT_STATUS_INVALID_PARAMETER;
1039 /* Only do query if we are online */
1041 if (idmap_is_offline()) {
1042 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1046 dom = talloc_get_type(e->state, struct idmap_domain);
1047 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1049 ads_status = ad_idmap_cached_connection(dom);
1050 if (!ADS_ERR_OK(ads_status)) {
1051 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1054 if (!ctx->ad_schema) {
1055 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1059 filter = talloc_asprintf(mem_ctx,
1061 ctx->ad_schema->posix_uid_attr,
1064 nt_status = NT_STATUS_NO_MEMORY;
1068 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1069 if (!ADS_ERR_OK(ads_status)) {
1070 nt_status = ads_ntstatus(ads_status);
1074 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1077 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1080 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1084 nt_status = NT_STATUS_NO_MEMORY;
1088 nt_status = NT_STATUS_OK;
1092 talloc_destroy(filter);
1095 ads_msgfree(ctx->ads, msg);
1102 /************************************************************************
1103 ***********************************************************************/
1105 static NTSTATUS nss_ad_close( void )
1107 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1109 return NT_STATUS_OK;
1112 /************************************************************************
1113 Function dispatch tables for the idmap and nss plugins
1114 ***********************************************************************/
1116 static struct idmap_methods ad_methods = {
1117 .init = idmap_ad_initialize,
1118 .unixids_to_sids = idmap_ad_unixids_to_sids,
1119 .sids_to_unixids = idmap_ad_sids_to_unixids,
1120 .close_fn = idmap_ad_close
1123 /* The SFU and RFC2307 NSS plugins share everything but the init
1124 function which sets the intended schema model to use */
1126 static struct nss_info_methods nss_rfc2307_methods = {
1127 .init = nss_rfc2307_init,
1128 .get_nss_info = nss_ad_get_info,
1129 .map_to_alias = nss_ad_map_to_alias,
1130 .map_from_alias = nss_ad_map_from_alias,
1131 .close_fn = nss_ad_close
1134 static struct nss_info_methods nss_sfu_methods = {
1135 .init = nss_sfu_init,
1136 .get_nss_info = nss_ad_get_info,
1137 .map_to_alias = nss_ad_map_to_alias,
1138 .map_from_alias = nss_ad_map_from_alias,
1139 .close_fn = nss_ad_close
1142 static struct nss_info_methods nss_sfu20_methods = {
1143 .init = nss_sfu20_init,
1144 .get_nss_info = nss_ad_get_info,
1145 .map_to_alias = nss_ad_map_to_alias,
1146 .map_from_alias = nss_ad_map_from_alias,
1147 .close_fn = nss_ad_close
1152 /************************************************************************
1153 Initialize the plugins
1154 ***********************************************************************/
1156 NTSTATUS idmap_ad_init(void)
1158 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1159 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1160 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1161 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1163 /* Always register the AD method first in order to get the
1164 idmap_domain interface called */
1166 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1167 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1169 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1170 return status_idmap_ad;
1173 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1174 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1175 "rfc2307", &nss_rfc2307_methods );
1176 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1177 return status_nss_rfc2307;
1180 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1181 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1182 "sfu", &nss_sfu_methods );
1183 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1184 return status_nss_sfu;
1187 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1188 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1189 "sfu20", &nss_sfu20_methods );
1190 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1191 return status_nss_sfu20;
1194 return NT_STATUS_OK;