Exemplo n.º 1
0
BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
{
	int ret;
	TDB_DATA keybuf, databuf;
	char* valstr = NULL;
	
	/* fail completely if get null pointers passed */
	SMB_ASSERT(keystr && value);

	if (!gencache_init()) return False;
	
	asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
	if (!valstr)
		return False;

	keybuf.dptr = strdup(keystr);
	keybuf.dsize = strlen(keystr)+1;
	databuf.dptr = strdup(valstr);
	databuf.dsize = strlen(valstr)+1;
	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
	           " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
		   (int)(timeout - time(NULL)), 
		   timeout > time(NULL) ? "ahead" : "in the past"));

	ret = tdb_store(cache, keybuf, databuf, 0);
	SAFE_FREE(valstr);
	SAFE_FREE(keybuf.dptr);
	SAFE_FREE(databuf.dptr);
	
	return ret == 0;
}
Exemplo n.º 2
0
int gencache_lock_entry( const char *key )
{
	if (!gencache_init())
		return -1;
	
	return tdb_lock_bystring(cache, key, 0);
}
Exemplo n.º 3
0
bool namecache_enable(void)
{
    /*
     * Check if name caching disabled by setting the name cache
     * timeout to zero.
     */

    if (lp_name_cache_timeout() == 0) {
        DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
        return False;
    }

    /* Init namecache by calling gencache initialisation */

    if (!gencache_init()) {
        DEBUG(2, ("namecache_enable: "
                  "Couldn't initialise namecache on top of gencache.\n"));
        return False;
    }

    /* I leave it for now, though I don't think we really
     * need this (mimir, 27.09.2002) */
    DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
              "seconds\n", lp_name_cache_timeout()));

    return True;
}
Exemplo n.º 4
0
bool namecache_status_fetch(const char *keyname,
                            int keyname_type,
                            int name_type,
                            const struct sockaddr_storage *keyip,
                            char *srvname_out)
{
    char *key = NULL;
    char *value = NULL;
    time_t timeout;

    if (!gencache_init())
        return False;

    key = namecache_status_record_key(keyname, keyname_type,
                                      name_type, keyip);
    if (!key)
        return False;

    if (!gencache_get(key, &value, &timeout)) {
        DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n",
                  key));
        SAFE_FREE(key);
        return False;
    } else {
        DEBUG(5, ("namecache_status_fetch: key %s -> %s\n",
                  key, value ));
    }

    strlcpy(srvname_out, value, 16);
    SAFE_FREE(key);
    SAFE_FREE(value);
    return True;
}
Exemplo n.º 5
0
bool namecache_status_store(const char *keyname, int keyname_type,
                            int name_type, const struct sockaddr_storage *keyip,
                            const char *srvname)
{
    char *key;
    time_t expiry;
    bool ret;

    if (!gencache_init()) {
        return False;
    }

    key = namecache_status_record_key(keyname, keyname_type,
                                      name_type, keyip);
    if (!key)
        return False;

    expiry = time(NULL) + lp_name_cache_timeout();
    ret = gencache_set(key, srvname, expiry);

    if (ret) {
        DEBUG(5, ("namecache_status_store: entry %s -> %s\n",
                  key, srvname ));
    } else {
        DEBUG(5, ("namecache_status_store: entry %s store failed.\n",
                  key ));
    }

    SAFE_FREE(key);
    return ret;
}
Exemplo n.º 6
0
static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
					const char *domain_name,
					const DATA_BLOB *blob)
{
	time_t expire_time;
	char *key;
	bool ret = false;

	if (!gencache_init()) {
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	key = dsgetdcname_cache_key(mem_ctx, domain_name);
	if (!key) {
		return NT_STATUS_NO_MEMORY;
	}

	expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL;

	if (gencache_lock_entry(key) != 0) {
		return NT_STATUS_LOCK_NOT_GRANTED;
	}

	ret = gencache_set_data_blob(key, blob, expire_time);

	gencache_unlock_entry(key);

	return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
}
Exemplo n.º 7
0
void gencache_unlock_entry( const char *key )
{
	if (!gencache_init())
		return;
	
	tdb_unlock_bystring(cache, key);
	return;
}
Exemplo n.º 8
0
BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
{
	TDB_DATA keybuf, databuf;

	/* fail completely if get null pointers passed */
	SMB_ASSERT(keystr);

	if (!gencache_init())
		return False;
	
	keybuf.dptr = strdup(keystr);
	keybuf.dsize = strlen(keystr)+1;
	databuf = tdb_fetch(cache, keybuf);
	SAFE_FREE(keybuf.dptr);
	
	if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
		char* entry_buf = strndup(databuf.dptr, databuf.dsize);
		char *v;
		time_t t;

		v = (char*)malloc(sizeof(char) * 
				  (databuf.dsize - TIMEOUT_LEN));
				
		SAFE_FREE(databuf.dptr);
		sscanf(entry_buf, CACHE_DATA_FMT, (int*)&t, v);
		SAFE_FREE(entry_buf);

		DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
			   "timeout = %s\n", t > time(NULL) ? "valid" :
			   "expired", keystr, v, ctime(&t)));

		if (valstr)
			*valstr = v;
		else
			SAFE_FREE(v);

		if (timeout)
			*timeout = t;

		return t > time(NULL);

	} else {
		SAFE_FREE(databuf.dptr);

		if (valstr)
			*valstr = NULL;

		if (timeout)
			timeout = NULL;

		DEBUG(10, ("Cache entry with key = %s couldn't be found\n", 
			   keystr));

		return False;
	}
}
Exemplo n.º 9
0
void namecache_flush(void)
{
    if (!gencache_init()) {
        return;
    }

    /*
     * iterate through each NBT cache's entry and flush it
     * by flush_netbios_name function
     */
    gencache_iterate(flush_netbios_name, NULL, "NBT/*");
    DEBUG(5, ("Namecache flushed\n"));
}
Exemplo n.º 10
0
void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
                      void* data, const char* keystr_pattern)
{
	TDB_LIST_NODE *node, *first_node;
	TDB_DATA databuf;
	char *keystr = NULL, *valstr = NULL, *entry = NULL;
	time_t timeout = 0;

	/* fail completely if get null pointers passed */
	SMB_ASSERT(fn && keystr_pattern);

	if (!gencache_init()) return;

	DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
	node = tdb_search_keys(cache, keystr_pattern);
	first_node = node;
	
	while (node) {
		/* ensure null termination of the key string */
		keystr = strndup(node->node_key.dptr, node->node_key.dsize);
		
		/* 
		 * We don't use gencache_get function, because we need to iterate through
		 * all of the entries. Validity verification is up to fn routine.
		 */
		databuf = tdb_fetch(cache, node->node_key);
		if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
			SAFE_FREE(databuf.dptr);
			SAFE_FREE(keystr);
			node = node->next;
			continue;
		}
		entry = strndup(databuf.dptr, databuf.dsize);
		SAFE_FREE(databuf.dptr);
		valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN));
		sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr);
		
		DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
		           keystr, valstr, ctime(&timeout)));
		fn(keystr, valstr, timeout, data);
		
		SAFE_FREE(valstr);
		SAFE_FREE(entry);
		SAFE_FREE(keystr);
		node = node->next;
	}
	
	tdb_search_list_free(first_node);
}
Exemplo n.º 11
0
bool namecache_fetch(const char *name,
                     int name_type,
                     struct ip_service **ip_list,
                     int *num_names)
{
    char *key, *value;
    time_t timeout;

    /* exit now if null pointers were passed as they're required further */
    if (!ip_list || !num_names) {
        return False;
    }

    if (!gencache_init()) {
        return False;
    }

    if (name_type > 255) {
        return False; /* Don't fetch non-real name types. */
    }

    *num_names = 0;

    /*
     * Use gencache interface - lookup the key
     */
    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }

    if (!gencache_get(key, &value, &timeout)) {
        DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
        SAFE_FREE(key);
        return False;
    } else {
        DEBUG(5, ("name %s#%02X found.\n", name, name_type));
    }

    /*
     * Split up the stored value into the list of IP adresses
     */
    *num_names = ipstr_list_parse(value, ip_list);

    SAFE_FREE(key);
    SAFE_FREE(value);

    return *num_names > 0; /* true only if some ip has been fetched */
}
Exemplo n.º 12
0
BOOL gencache_del(const char *keystr)
{
	int ret;
	TDB_DATA keybuf;
	
	/* fail completely if get null pointers passed */
	SMB_ASSERT(keystr);

	if (!gencache_init()) return False;	
	
	keybuf.dptr = strdup(keystr);
	keybuf.dsize = strlen(keystr)+1;
	DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
	ret = tdb_delete(cache, keybuf);
	
	SAFE_FREE(keybuf.dptr);
	return ret == 0;
}
Exemplo n.º 13
0
BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
{
	int ret = -1;
	TDB_DATA keybuf, databuf;
	char *old_valstr, *datastr;
	time_t old_timeout;
	
	/* fail completely if get null pointers passed */
	SMB_ASSERT(keystr && valstr);

	if (!gencache_init()) return False;
			
	/* 
	 * Check whether entry exists in the cache
	 * Don't verify gencache_get exit code, since the entry may be expired
	 */	
	gencache_get(keystr, &old_valstr, &old_timeout);
	
	if (!(old_valstr && old_timeout)) return False;
		
	DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
	           = %s\n", keystr, old_valstr, ctime(&old_timeout)));

	asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
	keybuf.dptr = strdup(keystr);
	keybuf.dsize = strlen(keystr)+1;
	databuf.dptr = strdup(datastr);
	databuf.dsize = strlen(datastr)+1;
	DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
	              ctime(&timeout), (int)(timeout - time(NULL)),
	              timeout > time(NULL) ? "ahead" : "in the past"));

		
	ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);

	SAFE_FREE(datastr);
	SAFE_FREE(old_valstr);
	SAFE_FREE(keybuf.dptr);
	SAFE_FREE(databuf.dptr);
	
	return ret == 0;
}
Exemplo n.º 14
0
static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
					const char *domain_name)
{
	char *key;

	if (!gencache_init()) {
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	key = dsgetdcname_cache_key(mem_ctx, domain_name);
	if (!key) {
		return NT_STATUS_NO_MEMORY;
	}

	if (!gencache_del(key)) {
		return NT_STATUS_UNSUCCESSFUL;
	}

	return NT_STATUS_OK;
}
Exemplo n.º 15
0
bool namecache_delete(const char *name, int name_type)
{
    bool ret;
    char *key;

    if (!gencache_init())
        return False;

    if (name_type > 255) {
        return False; /* Don't fetch non-real name types. */
    }

    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }
    ret = gencache_del(key);
    SAFE_FREE(key);
    return ret;
}
Exemplo n.º 16
0
static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
					const char *domain_name,
					struct GUID *domain_guid,
					uint32_t flags,
					const char *site_name,
					struct netr_DsRGetDCNameInfo **info_p,
					bool *expired)
{
	char *key;
	DATA_BLOB blob;
	enum ndr_err_code ndr_err;
	struct netr_DsRGetDCNameInfo *info;
	struct NETLOGON_SAM_LOGON_RESPONSE_EX r;
	NTSTATUS status;

	if (!gencache_init()) {
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	key = dsgetdcname_cache_key(mem_ctx, domain_name);
	if (!key) {
		return NT_STATUS_NO_MEMORY;
	}

	if (!gencache_get_data_blob(key, &blob, expired)) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
	if (!info) {
		return NT_STATUS_NO_MEMORY;
	}

	ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
		      (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);

	data_blob_free(&blob);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		dsgetdcname_cache_delete(mem_ctx, domain_name);
		return ndr_map_error2ntstatus(ndr_err);
	}

	status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
					       &r, &info);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (DEBUGLEVEL >= 10) {
		NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
	}

	/* check flags */
	if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
		DEBUG(10,("invalid flags\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	if ((flags & DS_IP_REQUIRED) &&
	    (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
	    	return NT_STATUS_INVALID_PARAMETER_MIX;
	}

	*info_p = info;

	return NT_STATUS_OK;
}
Exemplo n.º 17
0
bool namecache_store(const char *name,
                     int name_type,
                     int num_names,
                     struct ip_service *ip_list)
{
    time_t expiry;
    char *key, *value_string;
    int i;
    bool ret;

    /*
     * we use gecache call to avoid annoying debug messages about
     * initialised namecache again and again...
     */
    if (!gencache_init()) {
        return False;
    }

    if (name_type > 255) {
        return False; /* Don't store non-real name types. */
    }

    if ( DEBUGLEVEL >= 5 ) {
        TALLOC_CTX *ctx = talloc_stackframe();
        char *addr = NULL;

        DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
                  num_names, num_names == 1 ? "": "es", name, name_type));

        for (i = 0; i < num_names; i++) {
            addr = print_canonical_sockaddr(ctx,
                                            &ip_list[i].ss);
            if (!addr) {
                continue;
            }
            DEBUGADD(5, ("%s%s", addr,
                         (i == (num_names - 1) ? "" : ",")));

        }
        DEBUGADD(5, ("\n"));
        TALLOC_FREE(ctx);
    }

    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }

    expiry = time(NULL) + lp_name_cache_timeout();

    /*
     * Generate string representation of ip addresses list
     * First, store the number of ip addresses and then
     * place each single ip
     */
    if (!ipstr_list_make(&value_string, ip_list, num_names)) {
        SAFE_FREE(key);
        SAFE_FREE(value_string);
        return false;
    }

    /* set the entry */
    ret = gencache_set(key, value_string, expiry);
    SAFE_FREE(key);
    SAFE_FREE(value_string);
    return ret;
}