Fix a bunch of compiler warnings about wrong format types.
[ira/wip.git] / source3 / winbindd / winbindd_cache.c
index 0f8bd49d0684f7a532367f3eab0bd8281b2e0250..e5a72cbfd9365bcd05b481b885c2872d022f5f4d 100644 (file)
@@ -8,7 +8,7 @@
    Copyright (C) Volker Lendecke 2005
    Copyright (C) Guenther Deschner 2005
    Copyright (C) Michael Adam    2007
-   
+
    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
@@ -25,6 +25,8 @@
 
 #include "includes.h"
 #include "winbindd.h"
+#include "tdb_validate.h"
+#include "../libcli/auth/libcli_auth.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 #define WINBINDD_CACHE_VERSION_KEYSTR "WINBINDD_CACHE_VERSION"
 
 extern struct winbindd_methods reconnect_methods;
-extern bool opt_nocache;
 #ifdef HAVE_ADS
 extern struct winbindd_methods ads_methods;
 #endif
-extern struct winbindd_methods passdb_methods;
+extern struct winbindd_methods builtin_passdb_methods;
 
 /*
  * JRA. KEEP THIS LIST UP TO DATE IF YOU ADD CACHE ENTRIES.
@@ -137,7 +138,7 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
        /* We have to know what type of domain we are dealing with first. */
 
        if (domain->internal) {
-               domain->backend = &passdb_methods;
+               domain->backend = &builtin_passdb_methods;
                domain->initialized = True;
        }
        if ( !domain->initialized ) {
@@ -486,7 +487,9 @@ static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
        time_diff = t - domain->last_seq_check;
 
        /* see if we have to refetch the domain sequence number */
-       if (!force && (time_diff < cache_time)) {
+       if (!force && (time_diff < cache_time) &&
+                       (domain->sequence_number != DOM_SEQUENCE_NONE) &&
+                       NT_STATUS_IS_OK(domain->last_status)) {
                DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));
                goto done;
        }
@@ -495,8 +498,11 @@ static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
        /* this will update the timestamp as well */
        
        status = fetch_cache_seqnum( domain, t );
-       if ( NT_STATUS_IS_OK(status) )
-               goto done;      
+       if (NT_STATUS_IS_OK(status) &&
+                       (domain->sequence_number != DOM_SEQUENCE_NONE) &&
+                       NT_STATUS_IS_OK(domain->last_status)) {
+               goto done;
+       }
 
        /* important! make sure that we know if this is a native 
           mode domain or not.  And that we can contact it. */
@@ -524,7 +530,7 @@ static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
        domain->last_status = status;
        domain->last_seq_check = time(NULL);
        
-       /* save the new sequence number ni the cache */
+       /* save the new sequence number in the cache */
        store_cache_seqnum( domain );
 
 done:
@@ -626,7 +632,7 @@ static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
        char *kstr;
        struct cache_entry *centry;
 
-       if (opt_nocache) {
+       if (!winbindd_use_cache()) {
                return NULL;
        }
 
@@ -828,7 +834,7 @@ static void centry_end(struct cache_entry *centry, const char *format, ...)
        char *kstr;
        TDB_DATA key, data;
 
-       if (opt_nocache) {
+       if (!winbindd_use_cache()) {
                return;
        }
 
@@ -934,6 +940,8 @@ static void wcache_save_lockout_policy(struct winbindd_domain *domain,
        centry_free(centry);
 }
 
+
+
 static void wcache_save_password_policy(struct winbindd_domain *domain,
                                        NTSTATUS status,
                                        struct samr_DomInfo1 *policy)
@@ -957,6 +965,209 @@ static void wcache_save_password_policy(struct winbindd_domain *domain,
        centry_free(centry);
 }
 
+/***************************************************************************
+ ***************************************************************************/
+
+static void wcache_save_username_alias(struct winbindd_domain *domain,
+                                      NTSTATUS status,
+                                      const char *name, const char *alias)
+{
+       struct cache_entry *centry;
+       fstring uname;
+
+       if ( (centry = centry_start(domain, status)) == NULL )
+               return;
+
+       centry_put_string( centry, alias );
+
+       fstrcpy(uname, name);
+       strupper_m(uname);
+       centry_end(centry, "NSS/NA/%s", uname);
+
+       DEBUG(10,("wcache_save_username_alias: %s -> %s\n", name, alias ));
+
+       centry_free(centry);
+}
+
+static void wcache_save_alias_username(struct winbindd_domain *domain,
+                                      NTSTATUS status,
+                                      const char *alias, const char *name)
+{
+       struct cache_entry *centry;
+       fstring uname;
+
+       if ( (centry = centry_start(domain, status)) == NULL )
+               return;
+
+       centry_put_string( centry, name );
+
+       fstrcpy(uname, alias);
+       strupper_m(uname);
+       centry_end(centry, "NSS/AN/%s", uname);
+
+       DEBUG(10,("wcache_save_alias_username: %s -> %s\n", alias, name ));
+
+       centry_free(centry);
+}
+
+/***************************************************************************
+ ***************************************************************************/
+
+NTSTATUS resolve_username_to_alias( TALLOC_CTX *mem_ctx,
+                                   struct winbindd_domain *domain,
+                                   const char *name, char **alias )
+{
+       struct winbind_cache *cache = get_cache(domain);
+       struct cache_entry *centry = NULL;
+       NTSTATUS status;
+       char *upper_name;
+
+       if ( domain->internal )
+               return NT_STATUS_NOT_SUPPORTED;
+
+       if (!cache->tdb)
+               goto do_query;
+
+       if ( (upper_name = SMB_STRDUP(name)) == NULL )
+               return NT_STATUS_NO_MEMORY;
+       strupper_m(upper_name);
+
+       centry = wcache_fetch(cache, domain, "NSS/NA/%s", upper_name);
+
+       SAFE_FREE( upper_name );
+
+       if (!centry)
+               goto do_query;
+
+       status = centry->status;
+
+       if (!NT_STATUS_IS_OK(status)) {
+               centry_free(centry);
+               return status;
+       }
+
+       *alias = centry_string( centry, mem_ctx );
+
+       centry_free(centry);
+
+       DEBUG(10,("resolve_username_to_alias: [Cached] - mapped %s to %s\n",
+                 name, *alias ? *alias : "(none)"));
+
+       return (*alias) ? NT_STATUS_OK : NT_STATUS_OBJECT_NAME_NOT_FOUND;
+
+do_query:
+
+       /* If its not in cache and we are offline, then fail */
+
+       if ( get_global_winbindd_state_offline() || !domain->online ) {
+               DEBUG(8,("resolve_username_to_alias: rejecting query "
+                        "in offline mode\n"));
+               return NT_STATUS_NOT_FOUND;
+       }
+
+       status = nss_map_to_alias( mem_ctx, domain->name, name, alias );
+
+       if ( NT_STATUS_IS_OK( status ) ) {
+               wcache_save_username_alias(domain, status, name, *alias);
+       }
+
+       if ( NT_STATUS_EQUAL( status, NT_STATUS_NONE_MAPPED ) ) {
+               wcache_save_username_alias(domain, status, name, "(NULL)");
+       }
+
+       DEBUG(5,("resolve_username_to_alias: backend query returned %s\n",
+                nt_errstr(status)));
+
+       if ( NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ) {
+               set_domain_offline( domain );
+       }
+
+       return status;
+}
+
+/***************************************************************************
+ ***************************************************************************/
+
+NTSTATUS resolve_alias_to_username( TALLOC_CTX *mem_ctx,
+                                   struct winbindd_domain *domain,
+                                   const char *alias, char **name )
+{
+       struct winbind_cache *cache = get_cache(domain);
+       struct cache_entry *centry = NULL;
+       NTSTATUS status;
+       char *upper_name;
+
+       if ( domain->internal )
+               return  NT_STATUS_NOT_SUPPORTED;
+
+       if (!cache->tdb)
+               goto do_query;
+
+       if ( (upper_name = SMB_STRDUP(alias)) == NULL )
+               return NT_STATUS_NO_MEMORY;
+       strupper_m(upper_name);
+
+       centry = wcache_fetch(cache, domain, "NSS/AN/%s", upper_name);
+
+       SAFE_FREE( upper_name );
+
+       if (!centry)
+               goto do_query;
+
+       status = centry->status;
+
+       if (!NT_STATUS_IS_OK(status)) {
+               centry_free(centry);
+               return status;
+       }
+
+       *name = centry_string( centry, mem_ctx );
+
+       centry_free(centry);
+
+       DEBUG(10,("resolve_alias_to_username: [Cached] - mapped %s to %s\n",
+                 alias, *name ? *name : "(none)"));
+
+       return (*name) ? NT_STATUS_OK : NT_STATUS_OBJECT_NAME_NOT_FOUND;
+
+do_query:
+
+       /* If its not in cache and we are offline, then fail */
+
+       if ( get_global_winbindd_state_offline() || !domain->online ) {
+               DEBUG(8,("resolve_alias_to_username: rejecting query "
+                        "in offline mode\n"));
+               return NT_STATUS_NOT_FOUND;
+       }
+
+       /* an alias cannot contain a domain prefix or '@' */
+
+       if (strchr(alias, '\\') || strchr(alias, '@')) {
+               DEBUG(10,("resolve_alias_to_username: skipping fully "
+                         "qualified name %s\n", alias));
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
+
+       status = nss_map_from_alias( mem_ctx, domain->name, alias, name );
+
+       if ( NT_STATUS_IS_OK( status ) ) {
+               wcache_save_alias_username( domain, status, alias, *name );
+       }
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
+               wcache_save_alias_username(domain, status, alias, "(NULL)");
+       }
+
+       DEBUG(5,("resolve_alias_to_username: backend query returned %s\n",
+                nt_errstr(status)));
+
+       if ( NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ) {
+               set_domain_offline( domain );
+       }
+
+       return status;
+}
+
 NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID *sid)
 {
        struct winbind_cache *cache = get_cache(domain);
@@ -2072,7 +2283,9 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
        for (i=0; i<(*num_domains); i++) {
                (*names)[i] = centry_string(centry, mem_ctx);
                (*alt_names)[i] = centry_string(centry, mem_ctx);
-               centry_sid(centry, mem_ctx, &(*dom_sids)[i]);
+               if (!centry_sid(centry, mem_ctx, &(*dom_sids)[i])) {
+                       sid_copy(&(*dom_sids)[i], &global_sid_NULL);
+               }
        }
 
        status = centry->status;
@@ -2232,7 +2445,9 @@ do_query:
 
        /* and save it */
        refresh_sequence_number(domain, false);
-       wcache_save_password_policy(domain, status, policy);
+       if (NT_STATUS_IS_OK(status)) {
+               wcache_save_password_policy(domain, status, policy);
+       }
 
        return status;
 }
@@ -2255,6 +2470,8 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
 void wcache_invalidate_samlogon(struct winbindd_domain *domain, 
                                struct netr_SamInfo3 *info3)
 {
+        DOM_SID sid;
+        fstring key_str, sid_string;
        struct winbind_cache *cache;
 
        /* dont clear cached U/SID and UG/SID entries when we want to logon
@@ -2268,7 +2485,26 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
                return;
 
        cache = get_cache(domain);
-       netsamlogon_clear_cached_user(cache->tdb, info3);
+
+        if (!cache->tdb) {
+                return;
+        }
+
+       sid_copy(&sid, info3->base.domain_sid);
+       sid_append_rid(&sid, info3->base.rid);
+
+       /* Clear U/SID cache entry */
+       fstr_sprintf(key_str, "U/%s", sid_to_fstring(sid_string, &sid));
+       DEBUG(10, ("wcache_invalidate_samlogon: clearing %s\n", key_str));
+       tdb_delete(cache->tdb, string_tdb_data(key_str));
+
+       /* Clear UG/SID cache entry */
+       fstr_sprintf(key_str, "UG/%s", sid_to_fstring(sid_string, &sid));
+       DEBUG(10, ("wcache_invalidate_samlogon: clearing %s\n", key_str));
+       tdb_delete(cache->tdb, string_tdb_data(key_str));
+
+       /* Samba/winbindd never needs this. */
+       netsamlogon_clear_cached_user(info3);
 }
 
 bool wcache_invalidate_cache(void)
@@ -2302,7 +2538,7 @@ bool init_wcache(void)
                return true;
 
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"),
+       wcache->tdb = tdb_open_log(cache_path("winbindd_cache.tdb"),
                                WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
                                lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST), 
                                O_RDWR|O_CREAT, 0600);
@@ -2345,9 +2581,9 @@ bool initialize_winbindd_cache(void)
                tdb_close(wcache->tdb);
                wcache->tdb = NULL;
 
-               if (unlink(lock_path("winbindd_cache.tdb")) == -1) {
+               if (unlink(cache_path("winbindd_cache.tdb")) == -1) {
                        DEBUG(0,("initialize_winbindd_cache: unlink %s failed %s ",
-                               lock_path("winbindd_cache.tdb"),
+                               cache_path("winbindd_cache.tdb"),
                                strerror(errno) ));
                        return false;
                }
@@ -2389,9 +2625,9 @@ void cache_store_response(pid_t pid, struct winbindd_response *response)
                return;
 
        DEBUG(10, ("Storing response for pid %d, len %d\n",
-                  pid, response->length));
+                  (int)pid, response->length));
 
-       fstr_sprintf(key_str, "DR/%d", pid);
+       fstr_sprintf(key_str, "DR/%d", (int)pid);
        if (tdb_store(wcache->tdb, string_tdb_data(key_str), 
                      make_tdb_data((uint8 *)response, sizeof(*response)),
                      TDB_REPLACE) == -1)
@@ -2405,7 +2641,7 @@ void cache_store_response(pid_t pid, struct winbindd_response *response)
        DEBUG(10, ("Storing extra data: len=%d\n",
                   (int)(response->length - sizeof(*response))));
 
-       fstr_sprintf(key_str, "DE/%d", pid);
+       fstr_sprintf(key_str, "DE/%d", (int)pid);
        if (tdb_store(wcache->tdb, string_tdb_data(key_str),
                      make_tdb_data((uint8 *)response->extra_data.data,
                                    response->length - sizeof(*response)),
@@ -2415,7 +2651,7 @@ void cache_store_response(pid_t pid, struct winbindd_response *response)
        /* We could not store the extra data, make sure the tdb does not
         * contain a main record with wrong dangling extra data */
 
-       fstr_sprintf(key_str, "DR/%d", pid);
+       fstr_sprintf(key_str, "DR/%d", (int)pid);
        tdb_delete(wcache->tdb, string_tdb_data(key_str));
 
        return;
@@ -2429,9 +2665,9 @@ bool cache_retrieve_response(pid_t pid, struct winbindd_response * response)
        if (!init_wcache())
                return false;
 
-       DEBUG(10, ("Retrieving response for pid %d\n", pid));
+       DEBUG(10, ("Retrieving response for pid %d\n", (int)pid));
 
-       fstr_sprintf(key_str, "DR/%d", pid);
+       fstr_sprintf(key_str, "DR/%d", (int)pid);
        data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
 
        if (data.dptr == NULL)
@@ -2453,7 +2689,7 @@ bool cache_retrieve_response(pid_t pid, struct winbindd_response * response)
        DEBUG(10, ("Retrieving extra data length=%d\n",
                   (int)(response->length - sizeof(*response))));
 
-       fstr_sprintf(key_str, "DE/%d", pid);
+       fstr_sprintf(key_str, "DE/%d", (int)pid);
        data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
 
        if (data.dptr == NULL) {
@@ -2480,10 +2716,10 @@ void cache_cleanup_response(pid_t pid)
        if (!init_wcache())
                return;
 
-       fstr_sprintf(key_str, "DR/%d", pid);
+       fstr_sprintf(key_str, "DR/%d", (int)pid);
        tdb_delete(wcache->tdb, string_tdb_data(key_str));
 
-       fstr_sprintf(key_str, "DE/%d", pid);
+       fstr_sprintf(key_str, "DE/%d", (int)pid);
        tdb_delete(wcache->tdb, string_tdb_data(key_str));
 
        return;
@@ -2625,11 +2861,12 @@ void wcache_flush_cache(void)
                tdb_close(wcache->tdb);
                wcache->tdb = NULL;
        }
-       if (opt_nocache)
+       if (!winbindd_use_cache()) {
                return;
+       }
 
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"),
+       wcache->tdb = tdb_open_log(cache_path("winbindd_cache.tdb"),
                                WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
                                lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST), 
                                O_RDWR|O_CREAT, 0600);
@@ -3232,6 +3469,48 @@ static int validate_pwinfo(TALLOC_CTX *mem_ctx, const char *keystr,
        return 0;
 }
 
+static int validate_nss_an(TALLOC_CTX *mem_ctx, const char *keystr,
+                          TDB_DATA dbuf,
+                          struct tdb_validation_status *state)
+{
+       struct cache_entry *centry = create_centry_validate(keystr, dbuf, state);
+
+       if (!centry) {
+               return 1;
+       }
+
+       (void)centry_string( centry, mem_ctx );
+
+       centry_free(centry);
+
+       if (!(state->success)) {
+               return 1;
+       }
+       DEBUG(10,("validate_pwinfo: %s ok\n", keystr));
+       return 0;
+}
+
+static int validate_nss_na(TALLOC_CTX *mem_ctx, const char *keystr,
+                          TDB_DATA dbuf,
+                          struct tdb_validation_status *state)
+{
+       struct cache_entry *centry = create_centry_validate(keystr, dbuf, state);
+
+       if (!centry) {
+               return 1;
+       }
+
+       (void)centry_string( centry, mem_ctx );
+
+       centry_free(centry);
+
+       if (!(state->success)) {
+               return 1;
+       }
+       DEBUG(10,("validate_pwinfo: %s ok\n", keystr));
+       return 0;
+}
+
 static int validate_trustdoms(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
                              struct tdb_validation_status *state)
 {
@@ -3333,6 +3612,8 @@ struct key_val_struct {
        {"NSS/PWINFO/", validate_pwinfo},
        {"TRUSTDOMS/", validate_trustdoms},
        {"TRUSTDOMCACHE/", validate_trustdomcache},
+       {"NSS/NA/", validate_nss_na},
+       {"NSS/AN/", validate_nss_an},
        {"WINBINDD_OFFLINE", validate_offline},
        {WINBINDD_CACHE_VERSION_KEYSTR, validate_cache_version},
        {NULL, NULL}
@@ -3346,12 +3627,17 @@ struct key_val_struct {
 static int cache_traverse_validate_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
 {
        int i;
+       unsigned int max_key_len = 1024;
        struct tdb_validation_status *v_state = (struct tdb_validation_status *)state;
 
        /* Paranoia check. */
-       if (kbuf.dsize > 1024) {
-               DEBUG(0,("cache_traverse_validate_fn: key length too large (%u) > 1024\n\n",
-                               (unsigned int)kbuf.dsize ));
+       if (strncmp("UA/", (const char *)kbuf.dptr, 3) == 0) {
+               max_key_len = 1024 * 1024;
+       }
+       if (kbuf.dsize > max_key_len) {
+               DEBUG(0, ("cache_traverse_validate_fn: key length too large: "
+                         "(%u) > (%u)\n\n",
+                         (unsigned int)kbuf.dsize, (unsigned int)max_key_len));
                return 1;
        }
 
@@ -3409,7 +3695,7 @@ static void validate_panic(const char *const why)
 int winbindd_validate_cache(void)
 {
        int ret = -1;
-       const char *tdb_path = lock_path("winbindd_cache.tdb");
+       const char *tdb_path = cache_path("winbindd_cache.tdb");
        TDB_CONTEXT *tdb = NULL;
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
@@ -3451,7 +3737,7 @@ done:
 int winbindd_validate_cache_nobackup(void)
 {
        int ret = -1;
-       const char *tdb_path = lock_path("winbindd_cache.tdb");
+       const char *tdb_path = cache_path("winbindd_cache.tdb");
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
@@ -3474,6 +3760,20 @@ int winbindd_validate_cache_nobackup(void)
        return ret;
 }
 
+bool winbindd_cache_validate_and_initialize(void)
+{
+       close_winbindd_cache();
+
+       if (lp_winbind_offline_logon()) {
+               if (winbindd_validate_cache() < 0) {
+                       DEBUG(0, ("winbindd cache tdb corrupt and no backup "
+                                 "could be restored.\n"));
+               }
+       }
+
+       return initialize_winbindd_cache();
+}
+
 /*********************************************************************
  ********************************************************************/
 
@@ -3522,8 +3822,11 @@ static bool add_wbdomain_to_tdc_array( struct winbindd_domain *new_dom,
        list[idx].domain_name = talloc_strdup( list, new_dom->name );
        list[idx].dns_name = talloc_strdup( list, new_dom->alt_name );
 
-       if ( !is_null_sid( &new_dom->sid ) )
+       if ( !is_null_sid( &new_dom->sid ) ) {
                sid_copy( &list[idx].sid, &new_dom->sid );
+       } else {
+               sid_copy(&list[idx].sid, &global_sid_NULL);
+       }
 
        if ( new_dom->domain_flags != 0x0 )
                list[idx].trust_flags = new_dom->domain_flags;  
@@ -3556,7 +3859,9 @@ static TDB_DATA make_tdc_key( const char *domain_name )
        }
               
                
-       asprintf( &keystr, "TRUSTDOMCACHE/%s", domain_name );
+       if (asprintf( &keystr, "TRUSTDOMCACHE/%s", domain_name ) == -1) {
+               return key;
+       }
        key = string_term_tdb_data(keystr);
        
        return key;     
@@ -3926,7 +4231,14 @@ do_query:
        nt_status = nss_get_info( domain->name, user_sid, ctx, ads, msg, 
                                  homedir, shell, gecos, p_gid );
 
+       DEBUG(10, ("nss_get_info returned %s\n", nt_errstr(nt_status)));
+
        if ( NT_STATUS_IS_OK(nt_status) ) {
+               DEBUG(10, ("result:\n\thomedir = '%s'\n", *homedir));
+                DEBUGADD(10, ("\tshell = '%s'\n", *shell));
+                DEBUGADD(10, ("\tgecos = '%s'\n", *gecos));
+                DEBUGADD(10, ("\tgid = '%u'\n", (unsigned int)*p_gid));
+
                wcache_save_user_pwinfo( domain, nt_status, user_sid,
                                         *homedir, *shell, *gecos, *p_gid );
        }