Change all uses of uint32/16/8 in proto.h to uint32_t/16_t/8_t.
[kamenim/samba-autobuild/.git] / source3 / libsmb / trustdom_cache.c
index 6755de3814bb05c7174ed2b0f2e0af6704d88b33..330fd41e16a55eb562283bb9ba8b224a06392873 100644 (file)
@@ -4,22 +4,27 @@
    Trusted domain names cache on top of gencache.
 
    Copyright (C) Rafal Szczesniak      2002
-   
+
    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 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "../libcli/security/security.h"
+#include "../librpc/gen_ndr/ndr_lsa_c.h"
+#include "libsmb/libsmb.h"
+#include "rpc_client/cli_pipe.h"
+#include "rpc_client/cli_lsarpc.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_ALL    /* there's no proper class yet */
@@ -38,7 +43,6 @@
  * list of trusted domains
  **/
 
 /**
  * Initialise trustdom name caching system. Call gencache
  * initialisation routine to perform necessary activities.
  * @return true upon successful cache initialisation or
  *         false if cache init failed
  **/
+
 bool trustdom_cache_enable(void)
 {
-       /* Init trustdom cache by calling gencache initialisation */
-       if (!gencache_init()) {
-               DEBUG(2, ("trustdomcache_enable: Couldn't initialise trustdom cache on top of gencache.\n"));
-               return False;
-       }
-
        return True;
 }
 
@@ -66,15 +64,9 @@ bool trustdom_cache_enable(void)
  * @return true upon successful cache close or
  *         false if it failed
  **/
+
 bool trustdom_cache_shutdown(void)
 {
-       /* Close trustdom cache by calling gencache shutdown */
-       if (!gencache_shutdown()) {
-               DEBUG(2, ("trustdomcache_shutdown: Couldn't shutdown trustdom cache on top of gencache.\n"));
-               return False;
-       }
-       
        return True;
 }
 
@@ -91,7 +83,7 @@ static char* trustdom_cache_key(const char* name)
 {
        char* keystr = NULL;
        asprintf_strupper_m(&keystr, TDOMKEY_FMT, name);
-       
+
        return keystr;
 }
 
@@ -108,20 +100,13 @@ static char* trustdom_cache_key(const char* name)
  *         false if store attempt failed
  **/
  
-bool trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
-                          time_t timeout)
+bool trustdom_cache_store(const char *name, const char *alt_name,
+                         const struct dom_sid *sid, time_t timeout)
 {
        char *key, *alt_key;
        fstring sid_string;
        bool ret;
 
-       /*
-        * we use gecache call to avoid annoying debug messages
-        * about initialised trustdom 
-        */
-       if (!gencache_init())
-               return False;
-
        DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
                  sid_string_dbg(sid), name));
 
@@ -160,16 +145,12 @@ bool trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
  * @return true if entry is found or
  *         false if has expired/doesn't exist
  **/
-bool trustdom_cache_fetch(const char* name, DOM_SID* sid)
+
+bool trustdom_cache_fetch(const char* name, struct dom_sid* sid)
 {
        char *key = NULL, *value = NULL;
        time_t timeout;
 
-       /* init the cache */
-       if (!gencache_init())
-               return False;
-       
        /* exit now if null pointers were passed as they're required further */
        if (!sid)
                return False;
@@ -178,8 +159,8 @@ bool trustdom_cache_fetch(const char* name, DOM_SID* sid)
        key = trustdom_cache_key(name);
        if (!key)
                return False;
-       
-       if (!gencache_get(key, &value, &timeout)) {
+
+       if (!gencache_get(key, talloc_tos(), &value, &timeout)) {
                DEBUG(5, ("no entry for trusted domain %s found.\n", name));
                SAFE_FREE(key);
                return False;
@@ -188,14 +169,14 @@ bool trustdom_cache_fetch(const char* name, DOM_SID* sid)
                DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
        }
 
-       /* convert sid string representation into DOM_SID structure */
+       /* convert sid string representation into struct dom_sid structure */
        if(! string_to_sid(sid, value)) {
                sid = NULL;
-               SAFE_FREE(value);
+               TALLOC_FREE(value);
                return False;
        }
-       
-       SAFE_FREE(value);
+
+       TALLOC_FREE(value);
        return True;
 }
 
@@ -204,25 +185,21 @@ bool trustdom_cache_fetch(const char* name, DOM_SID* sid)
  fetch the timestamp from the last update 
 *******************************************************************/
 
-uint32 trustdom_cache_fetch_timestamp( void )
+uint32_t trustdom_cache_fetch_timestamp( void )
 {
        char *value = NULL;
        time_t timeout;
-       uint32 timestamp;
+       uint32_t timestamp;
 
-       /* init the cache */
-       if (!gencache_init()) 
-               return False;
-               
-       if (!gencache_get(TDOMTSKEY, &value, &timeout)) {
+       if (!gencache_get(TDOMTSKEY, talloc_tos(), &value, &timeout)) {
                DEBUG(5, ("no timestamp for trusted domain cache located.\n"));
                SAFE_FREE(value);
                return 0;
        } 
 
        timestamp = atoi(value);
-               
-       SAFE_FREE(value);
+
+       TALLOC_FREE(value);
        return timestamp;
 }
 
@@ -230,16 +207,12 @@ uint32 trustdom_cache_fetch_timestamp( void )
  store the timestamp from the last update 
 *******************************************************************/
 
-bool trustdom_cache_store_timestamp( uint32 t, time_t timeout )
+bool trustdom_cache_store_timestamp( uint32_t t, time_t timeout )
 {
        fstring value;
 
-       /* init the cache */
-       if (!gencache_init()) 
-               return False;
-               
        fstr_sprintf(value, "%d", t );
-               
+
        if (!gencache_set(TDOMTSKEY, value, timeout)) {
                DEBUG(5, ("failed to set timestamp for trustdom_cache\n"));
                return False;
@@ -268,9 +241,6 @@ static void flush_trustdom_name(const char* key, const char *value, time_t timeo
 
 void trustdom_cache_flush(void)
 {
-       if (!gencache_init())
-               return;
-
        /* 
         * iterate through each TDOM cache's entry and flush it
         * by flush_trustdom_name function
@@ -279,6 +249,105 @@ void trustdom_cache_flush(void)
        DEBUG(5, ("Trusted domains cache flushed\n"));
 }
 
+/*********************************************************************
+ Enumerate the list of trusted domains from a DC
+*********************************************************************/
+
+static bool enumerate_domain_trusts( TALLOC_CTX *mem_ctx, const char *domain,
+                                     char ***domain_names, uint32_t *num_domains,
+                                    struct dom_sid **sids )
+{
+       struct policy_handle    pol;
+       NTSTATUS status, result;
+       fstring         dc_name;
+       struct sockaddr_storage dc_ss;
+       uint32_t                enum_ctx = 0;
+       struct cli_state *cli = NULL;
+       struct rpc_pipe_client *lsa_pipe = NULL;
+       struct lsa_DomainList dom_list;
+       int i;
+       struct dcerpc_binding_handle *b = NULL;
+
+       *domain_names = NULL;
+       *num_domains = 0;
+       *sids = NULL;
+
+       /* lookup a DC first */
+
+       if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
+               DEBUG(3,("enumerate_domain_trusts: can't locate a DC for domain %s\n",
+                       domain));
+               return False;
+       }
+
+       /* setup the anonymous connection */
+
+       status = cli_full_connection( &cli, lp_netbios_name(), dc_name, &dc_ss, 0, "IPC$", "IPC",
+               "", "", "", 0, Undefined);
+       if ( !NT_STATUS_IS_OK(status) )
+               goto done;
+
+       /* open the LSARPC_PIPE */
+
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
+                                         &lsa_pipe);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+
+       b = lsa_pipe->binding_handle;
+
+       /* get a handle */
+
+       status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
+               LSA_POLICY_VIEW_LOCAL_INFORMATION, &pol);
+       if ( !NT_STATUS_IS_OK(status) )
+               goto done;
+
+       /* Lookup list of trusted domains */
+
+       status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
+                                        &pol,
+                                        &enum_ctx,
+                                        &dom_list,
+                                        (uint32_t)-1,
+                                        &result);
+       if ( !NT_STATUS_IS_OK(status) )
+               goto done;
+       if (!NT_STATUS_IS_OK(result)) {
+               status = result;
+               goto done;
+       }
+
+       *num_domains = dom_list.count;
+
+       *domain_names = talloc_zero_array(mem_ctx, char *, *num_domains);
+       if (!*domain_names) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+
+       *sids = talloc_zero_array(mem_ctx, struct dom_sid, *num_domains);
+       if (!*sids) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+
+       for (i=0; i< *num_domains; i++) {
+               (*domain_names)[i] = discard_const_p(char, dom_list.domains[i].name.string);
+               (*sids)[i] = *dom_list.domains[i].sid;
+       }
+
+done:
+       /* cleanup */
+       if (cli) {
+               DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
+               cli_shutdown( cli );
+       }
+
+       return NT_STATUS_IS_OK(status);
+}
+
 /********************************************************************
  update the trustdom_cache if needed 
 ********************************************************************/
@@ -287,20 +356,20 @@ void trustdom_cache_flush(void)
 void update_trustdom_cache( void )
 {
        char **domain_names;
-       DOM_SID *dom_sids;
-       uint32 num_domains;
-       uint32 last_check;
+       struct dom_sid *dom_sids;
+       uint32_t num_domains;
+       uint32_t last_check;
        int time_diff;
        TALLOC_CTX *mem_ctx = NULL;
        time_t now = time(NULL);
        int i;
-       
+
        /* get the timestamp.  We have to initialise it if the last timestamp == 0 */   
        if ( (last_check = trustdom_cache_fetch_timestamp()) == 0 ) 
                trustdom_cache_store_timestamp(0, now+TRUSTDOM_UPDATE_INTERVAL);
 
        time_diff = (int) (now - last_check);
-       
+
        if ( (time_diff > 0) && (time_diff < TRUSTDOM_UPDATE_INTERVAL) ) {
                DEBUG(10,("update_trustdom_cache: not time to update trustdom_cache yet\n"));
                return;
@@ -310,14 +379,14 @@ void update_trustdom_cache( void )
           smbd from blocking all other smbd daemons while we
           enumerate the trusted domains */
        trustdom_cache_store_timestamp(now, now+TRUSTDOM_UPDATE_INTERVAL);
-               
+
        if ( !(mem_ctx = talloc_init("update_trustdom_cache")) ) {
                DEBUG(0,("update_trustdom_cache: talloc_init() failed!\n"));
                goto done;
        }
 
        /* get the domains and store them */
-       
+
        if ( enumerate_domain_trusts(mem_ctx, lp_workgroup(), &domain_names, 
                &num_domains, &dom_sids)) {
                for ( i=0; i<num_domains; i++ ) {
@@ -333,6 +402,6 @@ void update_trustdom_cache( void )
 
 done:  
        talloc_destroy( mem_ctx );
-       
+
        return;
 }