r23779: Change from v2 or later to v3 or later.
[samba.git] / source3 / nsswitch / winbindd_rpc.c
index 11d9fe0dbbd91e8145e1529bc8f33d893dfa446b..f9260e61d8e7ea49a8c0d32c47b14f3d640c1093 100644 (file)
@@ -9,7 +9,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -48,6 +48,12 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
        *num_entries = 0;
        *info = NULL;
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("query_user_list: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result))
                return result;
@@ -140,6 +146,12 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
 
        DEBUG(3,("rpc: enum_dom_groups\n"));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("enum_domain_groups: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+
        status = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(status))
                return status;
@@ -196,6 +208,12 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
 
        DEBUG(3,("rpc: enum_local_groups\n"));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("enum_local_groups: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result))
                return result;
@@ -237,11 +255,12 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
 
 /* convert a single name to a sid in a domain */
 NTSTATUS msrpc_name_to_sid(struct winbindd_domain *domain,
-                           TALLOC_CTX *mem_ctx,
-                           const char *domain_name,
-                           const char *name,
-                           DOM_SID *sid,
-                           enum lsa_SidType *type)
+                          TALLOC_CTX *mem_ctx,
+                          enum winbindd_cmd original_cmd,
+                          const char *domain_name,
+                          const char *name,
+                          DOM_SID *sid,
+                          enum lsa_SidType *type)
 {
        NTSTATUS result;
        DOM_SID *sids = NULL;
@@ -250,19 +269,21 @@ NTSTATUS msrpc_name_to_sid(struct winbindd_domain *domain,
        struct rpc_pipe_client *cli;
        POLICY_HND lsa_policy;
 
-        if(name == NULL || *name=='\0') {
-                DEBUG(3,("rpc: name_to_sid name=%s\n", domain_name));
-                full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
-        } else {
-                DEBUG(3,("rpc: name_to_sid name=%s\\%s\n", domain_name, name));
-                full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
-        }
+       if (name == NULL || *name=='\0') {
+               full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
+       } else if (domain_name == NULL || *domain_name == '\0') {
+               full_name = talloc_asprintf(mem_ctx, "%s", name);
+       } else {
+               full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
+       }
        if (!full_name) {
                DEBUG(0, ("talloc_asprintf failed!\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
-       ws_name_return( full_name, '_' );
+       DEBUG(3,("rpc: name_to_sid name=%s\n", full_name));
+
+       ws_name_return( full_name, WB_REPLACE_CHAR );
 
        DEBUG(3,("name_to_sid [rpc] %s for domain %s\n", full_name?full_name:"", domain_name ));
 
@@ -271,7 +292,7 @@ NTSTATUS msrpc_name_to_sid(struct winbindd_domain *domain,
                return result;
 
        result = rpccli_lsa_lookup_names(cli, mem_ctx, &lsa_policy, 1, 
-                                        (const char**) &full_name, NULL, &sids, &types);
+                                        (const char**) &full_name, NULL, 1, &sids, &types);
         
        if (!NT_STATUS_IS_OK(result))
                return result;
@@ -305,19 +326,26 @@ NTSTATUS msrpc_sid_to_name(struct winbindd_domain *domain,
                        domain->name ));
 
        result = cm_connect_lsa(domain, mem_ctx, &cli, &lsa_policy);
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(2,("msrpc_sid_to_name: cm_connect_lsa() failed (%s)\n",
+                        nt_errstr(result)));           
                return result;
+       }
+       
 
        result = rpccli_lsa_lookup_sids(cli, mem_ctx, &lsa_policy,
                                        1, sid, &domains, &names, &types);
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(result)) {         
+               DEBUG(2,("msrpc_sid_to_name: rpccli_lsa_lookup_sids()  failed (%s)\n",
+                        nt_errstr(result)));           
                return result;
+       }
 
        *type = (enum lsa_SidType)types[0];
        *domain_name = domains[0];
        *name = names[0];
 
-       ws_name_replace( *name, '_' );  
+       ws_name_replace( *name, WB_REPLACE_CHAR );      
                
        DEBUG(5,("Mapped sid to [%s]\\[%s]\n", domains[0], *name));
        return NT_STATUS_OK;
@@ -342,9 +370,13 @@ NTSTATUS msrpc_rids_to_names(struct winbindd_domain *domain,
 
        DEBUG(3, ("rids_to_names [rpc] for domain %s\n", domain->name ));
 
-       sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_rids);
-       if (sids == NULL) {
-               return NT_STATUS_NO_MEMORY;
+       if (num_rids) {
+               sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_rids);
+               if (sids == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       } else {
+               sids = NULL;
        }
 
        for (i=0; i<num_rids; i++) {
@@ -369,7 +401,7 @@ NTSTATUS msrpc_rids_to_names(struct winbindd_domain *domain,
        ret_names = *names;
        for (i=0; i<num_rids; i++) {
                if ((*types)[i] != SID_NAME_UNKNOWN) {
-                       ws_name_replace( ret_names[i], '_' );
+                       ws_name_replace( ret_names[i], WB_REPLACE_CHAR );
                        *domain_name = domains[i];
                }
        }
@@ -397,6 +429,10 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
        if (!sid_peek_check_rid(&domain->sid, user_sid, &user_rid))
                return NT_STATUS_UNSUCCESSFUL;
        
+       user_info->homedir = NULL;
+       user_info->shell = NULL;
+       user_info->primary_gid = (gid_t)-1;
+                                               
        /* try netsamlogon cache first */
                        
        if ( (user = netsamlogon_cache_get( mem_ctx, user_sid )) != NULL ) 
@@ -405,7 +441,7 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
                DEBUG(5,("query_user: Cache lookup succeeded for %s\n", 
                        sid_string_static(user_sid)));
 
-               sid_compose(&user_info->user_sid, &domain->sid, user_rid);
+               sid_compose(&user_info->user_sid, &domain->sid, user->user_rid);
                sid_compose(&user_info->group_sid, &domain->sid,
                            user->group_rid);
                                
@@ -414,12 +450,20 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
                user_info->full_name = unistr2_tdup(mem_ctx,
                                                    &user->uni_full_name);
                
-               user_info->homedir = NULL;
-               user_info->shell = NULL;
-               user_info->primary_gid = (gid_t)-1;
+               TALLOC_FREE(user);
                                                
-               SAFE_FREE(user);
+               return NT_STATUS_OK;
+       }
                                
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("query_user: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+       
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("query_user: No incoming trust for domain %s\n",
+                         domain->name));
                return NT_STATUS_OK;
        }
        
@@ -492,6 +536,15 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                return NT_STATUS_OK;
        }
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("lookup_usergroups: No incoming trust for domain %s\n",
+                         domain->name));
+
+               /* Tell the cache manager not to remember this one */
+
+               return NT_STATUS_SYNCHRONIZATION_REQUIRED;
+       }
+
        /* no cache; hit the wire */
        
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
@@ -548,6 +601,12 @@ NTSTATUS msrpc_lookup_useraliases(struct winbindd_domain *domain,
 
        DEBUG(3,("rpc: lookup_useraliases\n"));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("msrpc_lookup_useraliases: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result))
                return result;
@@ -560,10 +619,13 @@ NTSTATUS msrpc_lookup_useraliases(struct winbindd_domain *domain,
                DEBUG(10,("rpc: lookup_useraliases: entering query %d for %d sids\n", 
                        num_queries, num_query_sids));  
 
-
-               query_sids = TALLOC_ARRAY(mem_ctx, DOM_SID2, num_query_sids);
-               if (query_sids == NULL) {
-                       return NT_STATUS_NO_MEMORY;
+               if (num_query_sids) {
+                       query_sids = TALLOC_ARRAY(mem_ctx, DOM_SID2, num_query_sids);
+                       if (query_sids == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               } else {
+                       query_sids = NULL;
                }
 
                for (i=0; i<num_query_sids; i++) {
@@ -631,6 +693,12 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
        DEBUG(10,("rpc: lookup_groupmem %s sid=%s\n", domain->name,
                  sid_to_string(sid_string, group_sid)));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("lookup_groupmem: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_OK;
+       }
+
        if (!sid_peek_check_rid(&domain->sid, group_sid, &group_rid))
                return NT_STATUS_UNSUCCESSFUL;
 
@@ -815,13 +883,20 @@ static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
 
        DEBUG(10,("rpc: fetch sequence_number for %s\n", domain->name));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("sequence_number: No incoming trust for domain %s\n",
+                         domain->name));
+               *seq = time(NULL);              
+               return NT_STATUS_OK;
+       }
+
        *seq = DOM_SEQUENCE_NONE;
 
        if (!(mem_ctx = talloc_init("sequence_number[rpc]")))
                return NT_STATUS_NO_MEMORY;
 
 #ifdef HAVE_LDAP
-       if ( domain->native_mode ) 
+       if ( domain->active_directory ) 
        {
                int res;
 
@@ -960,6 +1035,12 @@ NTSTATUS msrpc_lockout_policy(struct winbindd_domain *domain,
 
        DEBUG(10,("rpc: fetch lockout policy for %s\n", domain->name));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("msrpc_lockout_policy: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
@@ -992,6 +1073,12 @@ NTSTATUS msrpc_password_policy(struct winbindd_domain *domain,
 
        DEBUG(10,("rpc: fetch password policy for %s\n", domain->name));
 
+       if ( !winbindd_can_contact_domain( domain ) ) {
+               DEBUG(10,("msrpc_password_policy: No incoming trust for domain %s\n",
+                         domain->name));
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+
        result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result)) {
                goto done;