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/>.
32 #define DBGC_CLASS DBGC_IDMAP
34 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
36 #define IDMAP_AD_MAX_IDS 30
37 #define CHECK_ALLOC_DONE(mem) do { \
39 DEBUG(0, ("Out of memory!\n")); \
40 ret = NT_STATUS_NO_MEMORY; \
45 struct idmap_ad_context {
47 struct posix_schema *ad_schema;
48 enum wb_posix_mapping ad_map_type; /* WB_POSIX_MAP_UNKNOWN */
51 NTSTATUS init_module(void);
53 /************************************************************************
54 ***********************************************************************/
56 static ADS_STATUS ad_idmap_cached_connection_internal(struct idmap_domain *dom)
62 struct sockaddr_storage dc_ip;
63 struct idmap_ad_context *ctx;
64 char *ldap_server = NULL;
66 struct winbindd_domain *wb_dom;
68 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
71 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
73 if (ctx->ads != NULL) {
76 time_t now = time(NULL);
80 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
82 /* check for a valid structure */
83 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
84 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
86 if ( ads->config.realm && (expire > time(NULL))) {
89 /* we own this ADS_STRUCT so make sure it goes away */
90 DEBUG(7,("Deleting expired krb5 credential cache\n"));
93 ads_kdestroy(WINBIND_CCACHE_NAME);
95 TALLOC_FREE( ctx->ad_schema );
100 /* we don't want this to affect the users ccache */
101 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
105 * At this point we only have the NetBIOS domain name.
106 * Check if we can get server nam and realm from SAF cache
107 * and the domain list.
109 ldap_server = saf_fetch(dom->name);
110 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server?ldap_server:""));
112 wb_dom = find_domain_from_name_noinit(dom->name);
113 if (wb_dom == NULL) {
114 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
118 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
119 " domain '%s'\n", wb_dom->alt_name, dom->name));
120 realm = wb_dom->alt_name;
123 if ( (ads = ads_init(realm, dom->name, ldap_server)) == NULL ) {
124 DEBUG(1,("ads_init failed\n"));
125 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
128 /* the machine acct password might have change - fetch it every time */
129 SAFE_FREE(ads->auth.password);
130 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
132 SAFE_FREE(ads->auth.realm);
133 ads->auth.realm = SMB_STRDUP(lp_realm());
135 /* setup server affinity */
137 get_dc_name(dom->name, realm, dc_name, &dc_ip );
139 status = ads_connect(ads);
140 if (!ADS_ERR_OK(status)) {
141 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
146 ads->is_mine = False;
153 /************************************************************************
154 ***********************************************************************/
156 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
159 struct idmap_ad_context * ctx;
161 status = ad_idmap_cached_connection_internal(dom);
162 if (!ADS_ERR_OK(status)) {
166 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
168 /* if we have a valid ADS_STRUCT and the schema model is
169 defined, then we can return here. */
171 if ( ctx->ad_schema ) {
175 /* Otherwise, set the schema model */
177 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
178 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
179 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
181 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
182 if ( !ADS_ERR_OK(status) ) {
183 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
190 /************************************************************************
191 ***********************************************************************/
193 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
196 struct idmap_ad_context *ctx;
198 const char *schema_mode = NULL;
200 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
202 DEBUG(0, ("Out of memory!\n"));
203 return NT_STATUS_NO_MEMORY;
206 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
207 if (config_option == NULL) {
208 DEBUG(0, ("Out of memory!\n"));
210 return NT_STATUS_NO_MEMORY;
213 /* default map type */
214 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
217 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
218 if ( schema_mode && schema_mode[0] ) {
219 if ( strequal(schema_mode, "sfu") )
220 ctx->ad_map_type = WB_POSIX_MAP_SFU;
221 else if ( strequal(schema_mode, "sfu20" ) )
222 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
223 else if ( strequal(schema_mode, "rfc2307" ) )
224 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
226 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
230 dom->private_data = ctx;
232 talloc_free(config_option);
237 /************************************************************************
238 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
239 ***********************************************************************/
241 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
245 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
246 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
254 /************************************************************************
255 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
256 ***********************************************************************/
258 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
262 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
263 if (sid_equal(maps[i]->sid, sid)) {
271 /************************************************************************
272 ***********************************************************************/
274 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
278 struct idmap_ad_context *ctx;
280 const char *attrs[] = { "sAMAccountType",
282 NULL, /* uidnumber */
283 NULL, /* gidnumber */
285 LDAPMessage *res = NULL;
286 LDAPMessage *entry = NULL;
292 char *u_filter = NULL;
293 char *g_filter = NULL;
295 /* initialize the status to avoid suprise */
296 for (i = 0; ids[i]; i++) {
297 ids[i]->status = ID_UNKNOWN;
300 /* Only do query if we are online */
301 if (idmap_is_offline()) {
302 return NT_STATUS_FILE_IS_OFFLINE;
305 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
307 if ( (memctx = talloc_new(ctx)) == NULL ) {
308 DEBUG(0, ("Out of memory!\n"));
309 return NT_STATUS_NO_MEMORY;
312 rc = ad_idmap_cached_connection(dom);
313 if (!ADS_ERR_OK(rc)) {
314 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
315 ret = NT_STATUS_UNSUCCESSFUL;
316 /* ret = ads_ntstatus(rc); */
320 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
321 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
325 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
326 switch (ids[idx]->xid.type) {
329 u_filter = talloc_asprintf(memctx, "(&(|"
330 "(sAMAccountType=%d)"
331 "(sAMAccountType=%d)"
332 "(sAMAccountType=%d))(|",
333 ATYPE_NORMAL_ACCOUNT,
334 ATYPE_WORKSTATION_TRUST,
335 ATYPE_INTERDOMAIN_TRUST);
337 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
338 ctx->ad_schema->posix_uidnumber_attr,
339 (unsigned long)ids[idx]->xid.id);
340 CHECK_ALLOC_DONE(u_filter);
345 g_filter = talloc_asprintf(memctx, "(&(|"
346 "(sAMAccountType=%d)"
347 "(sAMAccountType=%d))(|",
348 ATYPE_SECURITY_GLOBAL_GROUP,
349 ATYPE_SECURITY_LOCAL_GROUP);
351 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
352 ctx->ad_schema->posix_gidnumber_attr,
353 (unsigned long)ids[idx]->xid.id);
354 CHECK_ALLOC_DONE(g_filter);
358 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
359 ids[idx]->status = ID_UNKNOWN;
363 filter = talloc_asprintf(memctx, "(|");
364 CHECK_ALLOC_DONE(filter);
366 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
367 CHECK_ALLOC_DONE(filter);
368 TALLOC_FREE(u_filter);
371 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
372 CHECK_ALLOC_DONE(filter);
373 TALLOC_FREE(g_filter);
375 filter = talloc_asprintf_append_buffer(filter, ")");
376 CHECK_ALLOC_DONE(filter);
378 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
379 if (!ADS_ERR_OK(rc)) {
380 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
381 ret = NT_STATUS_UNSUCCESSFUL;
385 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
386 DEBUG(10, ("No IDs found\n"));
390 for (i = 0; (i < count) && entry; i++) {
397 if (i == 0) { /* first entry */
398 entry = ads_first_entry(ctx->ads, entry);
399 } else { /* following ones */
400 entry = ads_next_entry(ctx->ads, entry);
404 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
408 /* first check if the SID is present */
409 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
410 DEBUG(2, ("Could not retrieve SID from entry\n"));
415 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
416 DEBUG(1, ("could not get SAM account type\n"));
420 switch (atype & 0xF0000000) {
421 case ATYPE_SECURITY_GLOBAL_GROUP:
422 case ATYPE_SECURITY_LOCAL_GROUP:
425 case ATYPE_NORMAL_ACCOUNT:
426 case ATYPE_WORKSTATION_TRUST:
427 case ATYPE_INTERDOMAIN_TRUST:
431 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
435 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
436 ctx->ad_schema->posix_uidnumber_attr :
437 ctx->ad_schema->posix_gidnumber_attr,
440 DEBUG(1, ("Could not get unix ID\n"));
444 if (!idmap_unix_id_is_in_range(id, dom)) {
445 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
446 id, dom->low_id, dom->high_id));
450 map = find_map_by_id(&ids[bidx], type, id);
452 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
456 sid_copy(map->sid, &sid);
459 map->status = ID_MAPPED;
461 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
462 (unsigned long)map->xid.id,
467 ads_msgfree(ctx->ads, res);
470 if (ids[idx]) { /* still some values to map */
476 /* mark all unknown/expired ones as unmapped */
477 for (i = 0; ids[i]; i++) {
478 if (ids[i]->status != ID_MAPPED)
479 ids[i]->status = ID_UNMAPPED;
487 /************************************************************************
488 ***********************************************************************/
490 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
494 struct idmap_ad_context *ctx;
496 const char *attrs[] = { "sAMAccountType",
498 NULL, /* attr_uidnumber */
499 NULL, /* attr_gidnumber */
501 LDAPMessage *res = NULL;
502 LDAPMessage *entry = NULL;
510 /* initialize the status to avoid suprise */
511 for (i = 0; ids[i]; i++) {
512 ids[i]->status = ID_UNKNOWN;
515 /* Only do query if we are online */
516 if (idmap_is_offline()) {
517 return NT_STATUS_FILE_IS_OFFLINE;
520 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
522 if ( (memctx = talloc_new(ctx)) == NULL ) {
523 DEBUG(0, ("Out of memory!\n"));
524 return NT_STATUS_NO_MEMORY;
527 rc = ad_idmap_cached_connection(dom);
528 if (!ADS_ERR_OK(rc)) {
529 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
530 ret = NT_STATUS_UNSUCCESSFUL;
531 /* ret = ads_ntstatus(rc); */
535 if (ctx->ad_schema == NULL) {
536 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
537 ret = NT_STATUS_UNSUCCESSFUL;
541 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
542 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
545 filter = talloc_asprintf(memctx, "(&(|"
546 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
547 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
549 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
550 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
552 CHECK_ALLOC_DONE(filter);
555 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
557 ids[idx]->status = ID_UNKNOWN;
559 sidstr = sid_binstring(ids[idx]->sid);
560 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
563 CHECK_ALLOC_DONE(filter);
565 filter = talloc_asprintf_append_buffer(filter, "))");
566 CHECK_ALLOC_DONE(filter);
567 DEBUG(10, ("Filter: [%s]\n", filter));
569 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
570 if (!ADS_ERR_OK(rc)) {
571 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
572 ret = NT_STATUS_UNSUCCESSFUL;
576 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
577 DEBUG(10, ("No IDs found\n"));
581 for (i = 0; (i < count) && entry; i++) {
588 if (i == 0) { /* first entry */
589 entry = ads_first_entry(ctx->ads, entry);
590 } else { /* following ones */
591 entry = ads_next_entry(ctx->ads, entry);
595 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
599 /* first check if the SID is present */
600 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
601 DEBUG(2, ("Could not retrieve SID from entry\n"));
605 map = find_map_by_sid(&ids[bidx], &sid);
607 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
612 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
613 DEBUG(1, ("could not get SAM account type\n"));
617 switch (atype & 0xF0000000) {
618 case ATYPE_SECURITY_GLOBAL_GROUP:
619 case ATYPE_SECURITY_LOCAL_GROUP:
622 case ATYPE_NORMAL_ACCOUNT:
623 case ATYPE_WORKSTATION_TRUST:
624 case ATYPE_INTERDOMAIN_TRUST:
628 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
632 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
633 ctx->ad_schema->posix_uidnumber_attr :
634 ctx->ad_schema->posix_gidnumber_attr,
637 DEBUG(1, ("Could not get unix ID\n"));
640 if (!idmap_unix_id_is_in_range(id, dom)) {
641 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
642 id, dom->low_id, dom->high_id));
647 map->xid.type = type;
649 map->status = ID_MAPPED;
651 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
652 (unsigned long)map->xid.id,
657 ads_msgfree(ctx->ads, res);
660 if (ids[idx]) { /* still some values to map */
666 /* mark all unknwoni/expired ones as unmapped */
667 for (i = 0; ids[i]; i++) {
668 if (ids[i]->status != ID_MAPPED)
669 ids[i]->status = ID_UNMAPPED;
677 /************************************************************************
678 ***********************************************************************/
680 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
682 struct idmap_ad_context * ctx;
684 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
686 if (ctx->ads != NULL) {
687 /* we own this ADS_STRUCT so make sure it goes away */
688 ctx->ads->is_mine = True;
689 ads_destroy( &ctx->ads );
693 TALLOC_FREE( ctx->ad_schema );
699 * nss_info_{sfu,sfu20,rfc2307}
702 /************************************************************************
703 Initialize the {sfu,sfu20,rfc2307} state
704 ***********************************************************************/
706 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
707 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
708 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
709 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
710 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
711 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
713 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
716 case WB_POSIX_MAP_TEMPLATE:
717 return wb_posix_map_template_string;
718 case WB_POSIX_MAP_SFU:
719 return wb_posix_map_sfu_string;
720 case WB_POSIX_MAP_SFU20:
721 return wb_posix_map_sfu20_string;
722 case WB_POSIX_MAP_RFC2307:
723 return wb_posix_map_rfc2307_string;
724 case WB_POSIX_MAP_UNIXINFO:
725 return wb_posix_map_unixinfo_string;
727 return wb_posix_map_unknown_string;
731 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
732 enum wb_posix_mapping new_ad_map_type)
734 struct idmap_domain *dom;
735 struct idmap_ad_context *ctx;
737 if (e->state != NULL) {
738 dom = talloc_get_type(e->state, struct idmap_domain);
740 dom = TALLOC_ZERO_P(e, struct idmap_domain);
742 DEBUG(0, ("Out of memory!\n"));
743 return NT_STATUS_NO_MEMORY;
748 if (e->domain != NULL) {
749 dom->name = talloc_strdup(dom, e->domain);
750 if (dom->name == NULL) {
751 DEBUG(0, ("Out of memory!\n"));
752 return NT_STATUS_NO_MEMORY;
756 if (dom->private_data != NULL) {
757 ctx = talloc_get_type(dom->private_data,
758 struct idmap_ad_context);
760 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
762 DEBUG(0, ("Out of memory!\n"));
763 return NT_STATUS_NO_MEMORY;
765 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
766 dom->private_data = ctx;
769 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
770 (ctx->ad_map_type != new_ad_map_type))
772 DEBUG(2, ("nss_ad_generic_init: "
773 "Warning: overriding previously set posix map type "
774 "%s for domain %s with map type %s.\n",
775 ad_map_type_string(ctx->ad_map_type),
777 ad_map_type_string(new_ad_map_type)));
780 ctx->ad_map_type = new_ad_map_type;
785 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
787 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
790 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
792 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
795 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
797 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
801 /************************************************************************
802 ***********************************************************************/
804 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
814 const char *attrs[] = {NULL, /* attr_homedir */
815 NULL, /* attr_shell */
816 NULL, /* attr_gecos */
817 NULL, /* attr_gidnumber */
820 LDAPMessage *msg_internal = NULL;
821 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
822 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
824 struct idmap_domain *dom;
825 struct idmap_ad_context *ctx;
827 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
828 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
830 /* Only do query if we are online */
831 if (idmap_is_offline()) {
832 return NT_STATUS_FILE_IS_OFFLINE;
835 dom = talloc_get_type(e->state, struct idmap_domain);
836 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
838 ads_status = ad_idmap_cached_connection(dom);
839 if (!ADS_ERR_OK(ads_status)) {
840 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
843 if (!ctx->ad_schema) {
844 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
845 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
848 if (!sid || !homedir || !shell || !gecos) {
849 return NT_STATUS_INVALID_PARAMETER;
852 /* See if we can use the ADS connection struct swe were given */
855 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
856 "LDAP message (%p)\n", msg));
858 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
859 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
860 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
863 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
867 nt_status = NT_STATUS_OK;
871 /* Have to do our own query */
873 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
876 attrs[0] = ctx->ad_schema->posix_homedir_attr;
877 attrs[1] = ctx->ad_schema->posix_shell_attr;
878 attrs[2] = ctx->ad_schema->posix_gecos_attr;
879 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
881 sidstr = sid_binstring(sid);
882 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
886 nt_status = NT_STATUS_NO_MEMORY;
890 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
891 if (!ADS_ERR_OK(ads_status)) {
892 nt_status = ads_ntstatus(ads_status);
896 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
897 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
898 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
901 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
905 nt_status = NT_STATUS_OK;
909 ads_msgfree(ctx->ads, msg_internal);
915 /**********************************************************************
916 *********************************************************************/
918 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
919 struct nss_domain_entry *e,
923 const char *attrs[] = {NULL, /* attr_uid */
926 LDAPMessage *msg = NULL;
927 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
928 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
929 struct idmap_domain *dom;
930 struct idmap_ad_context *ctx = NULL;
932 /* Check incoming parameters */
934 if ( !e || !e->domain || !name || !*alias) {
935 nt_status = NT_STATUS_INVALID_PARAMETER;
939 /* Only do query if we are online */
941 if (idmap_is_offline()) {
942 nt_status = NT_STATUS_FILE_IS_OFFLINE;
946 dom = talloc_get_type(e->state, struct idmap_domain);
947 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
949 ads_status = ad_idmap_cached_connection(dom);
950 if (!ADS_ERR_OK(ads_status)) {
951 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
954 if (!ctx->ad_schema) {
955 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
959 attrs[0] = ctx->ad_schema->posix_uid_attr;
961 filter = talloc_asprintf(mem_ctx,
962 "(sAMAccountName=%s)",
965 nt_status = NT_STATUS_NO_MEMORY;
969 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
970 if (!ADS_ERR_OK(ads_status)) {
971 nt_status = ads_ntstatus(ads_status);
975 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
978 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
981 nt_status = NT_STATUS_OK;
985 talloc_destroy(filter);
988 ads_msgfree(ctx->ads, msg);
994 /**********************************************************************
995 *********************************************************************/
997 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
998 struct nss_domain_entry *e,
1002 const char *attrs[] = {"sAMAccountName",
1004 char *filter = NULL;
1005 LDAPMessage *msg = NULL;
1006 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1007 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1009 struct idmap_domain *dom;
1010 struct idmap_ad_context *ctx = NULL;
1012 /* Check incoming parameters */
1014 if ( !alias || !name) {
1015 nt_status = NT_STATUS_INVALID_PARAMETER;
1019 /* Only do query if we are online */
1021 if (idmap_is_offline()) {
1022 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1026 dom = talloc_get_type(e->state, struct idmap_domain);
1027 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1029 ads_status = ad_idmap_cached_connection(dom);
1030 if (!ADS_ERR_OK(ads_status)) {
1031 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1034 if (!ctx->ad_schema) {
1035 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1039 filter = talloc_asprintf(mem_ctx,
1041 ctx->ad_schema->posix_uid_attr,
1044 nt_status = NT_STATUS_NO_MEMORY;
1048 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1049 if (!ADS_ERR_OK(ads_status)) {
1050 nt_status = ads_ntstatus(ads_status);
1054 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1057 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1060 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1064 nt_status = NT_STATUS_NO_MEMORY;
1068 nt_status = NT_STATUS_OK;
1072 talloc_destroy(filter);
1075 ads_msgfree(ctx->ads, msg);
1082 /************************************************************************
1083 ***********************************************************************/
1085 static NTSTATUS nss_ad_close( void )
1087 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1089 return NT_STATUS_OK;
1092 /************************************************************************
1093 Function dispatch tables for the idmap and nss plugins
1094 ***********************************************************************/
1096 static struct idmap_methods ad_methods = {
1097 .init = idmap_ad_initialize,
1098 .unixids_to_sids = idmap_ad_unixids_to_sids,
1099 .sids_to_unixids = idmap_ad_sids_to_unixids,
1100 .close_fn = idmap_ad_close
1103 /* The SFU and RFC2307 NSS plugins share everything but the init
1104 function which sets the intended schema model to use */
1106 static struct nss_info_methods nss_rfc2307_methods = {
1107 .init = nss_rfc2307_init,
1108 .get_nss_info = nss_ad_get_info,
1109 .map_to_alias = nss_ad_map_to_alias,
1110 .map_from_alias = nss_ad_map_from_alias,
1111 .close_fn = nss_ad_close
1114 static struct nss_info_methods nss_sfu_methods = {
1115 .init = nss_sfu_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_sfu20_methods = {
1123 .init = nss_sfu20_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
1132 /************************************************************************
1133 Initialize the plugins
1134 ***********************************************************************/
1136 NTSTATUS idmap_ad_init(void)
1138 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1139 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1140 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1141 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1143 /* Always register the AD method first in order to get the
1144 idmap_domain interface called */
1146 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1147 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1149 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1150 return status_idmap_ad;
1153 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1154 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1155 "rfc2307", &nss_rfc2307_methods );
1156 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1157 return status_nss_rfc2307;
1160 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1161 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1162 "sfu", &nss_sfu_methods );
1163 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1164 return status_nss_sfu;
1167 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1168 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1169 "sfu20", &nss_sfu20_methods );
1170 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1171 return status_nss_sfu20;
1174 return NT_STATUS_OK;