s3:winbindd: put winbindd_cache.tdb into cache_dir, not lock_dir.
[metze/samba/wip.git] / source3 / winbindd / winbindd_cache.c
index 5abd207e793c52330d238d75151d4a3259119f3b..a0df81eb6e2645bf15bc9b94f2b9f6a1b3d113b2 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
@@ -37,7 +37,7 @@ 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 +137,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 +486,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 +497,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 +529,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:
@@ -934,6 +939,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 +964,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 +2282,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;
@@ -2257,6 +2469,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
@@ -2270,7 +2484,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)
@@ -2304,7 +2537,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);
@@ -2347,9 +2580,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;
                }
@@ -2631,7 +2864,7 @@ void wcache_flush_cache(void)
                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);
@@ -3234,6 +3467,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)
 {
@@ -3335,6 +3610,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}
@@ -3348,23 +3625,18 @@ 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) {
-               if (strncmp("UA/", (const char *)kbuf.dptr, 3) == 0) {
-                       unsigned int max_key_len = 1024*1024;
-                       if (kbuf.dsize > max_key_len) {
-                               DEBUG(0,("cache_traverse_validate_fn: UA key to large (%u) > (%u)\n\n",
-                                        (unsigned int)kbuf.dsize, (unsigned int)max_key_len ));
-                               return 1;
-                       } 
-               } else  {
-
-                               DEBUG(0,("cache_traverse_validate_fn: key length too large (%u) > 1024\n\n",
-                                        (unsigned int)kbuf.dsize ));
-                               return 1;
-                       }
+       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;
        }
 
        for (i = 0; key_val[i].keyname; i++) {
@@ -3421,7 +3693,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"));
@@ -3463,7 +3735,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;
@@ -3548,8 +3820,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;  
@@ -3582,7 +3857,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;     
@@ -3952,7 +4229,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", *p_gid));
+
                wcache_save_user_pwinfo( domain, nt_status, user_sid,
                                         *homedir, *shell, *gecos, *p_gid );
        }