winbindd_cache.c

来自「samba-3.0.22.tar.gz 编译smb服务器的源码」· C语言 代码 · 共 1,621 行 · 第 1/3 页

C
1,621
字号
/*    Unix SMB/CIFS implementation.   Winbind cache backend functions   Copyright (C) Andrew Tridgell 2001   Copyright (C) Gerald Carter   2003   Copyright (C) Volker Lendecke 2005         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   (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, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"#include "winbindd.h"#undef DBGC_CLASS#define DBGC_CLASS DBGC_WINBINDstruct winbind_cache {	TDB_CONTEXT *tdb;};struct cache_entry {	NTSTATUS status;	uint32 sequence_number;	uint8 *data;	uint32 len, ofs;};#define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)static struct winbind_cache *wcache;/* flush the cache */void wcache_flush_cache(void){	extern BOOL opt_nocache;	if (!wcache)		return;	if (wcache->tdb) {		tdb_close(wcache->tdb);		wcache->tdb = NULL;	}	if (opt_nocache)		return;	wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000, 				   TDB_CLEAR_IF_FIRST, O_RDWR|O_CREAT, 0600);	if (!wcache->tdb) {		DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));	}	DEBUG(10,("wcache_flush_cache success\n"));}void winbindd_check_cache_size(time_t t){	static time_t last_check_time;	struct stat st;	if (last_check_time == (time_t)0)		last_check_time = t;	if (t - last_check_time < 60 && t - last_check_time > 0)		return;	if (wcache == NULL || wcache->tdb == NULL) {		DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));		return;	}	if (fstat(wcache->tdb->fd, &st) == -1) {		DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno) ));		return;	}	if (st.st_size > WINBINDD_MAX_CACHE_SIZE) {		DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",			(unsigned long)st.st_size,			(unsigned long)WINBINDD_MAX_CACHE_SIZE));		wcache_flush_cache();	}}/* get the winbind_cache structure */static struct winbind_cache *get_cache(struct winbindd_domain *domain){	struct winbind_cache *ret = wcache;	struct winbindd_domain *our_domain = domain;	/* we have to know what type of domain we are dealing with first */	if ( !domain->initialized )		set_dc_type_and_flags( domain );	/* 	   OK.  listen up becasue I'm only going to say this once.	   We have the following scenarios to consider	   (a) trusted AD domains on a Samba DC,	   (b) trusted AD domains and we are joined to a non-kerberos domain	   (c) trusted AD domains and we are joined to a kerberos (AD) domain	   For (a) we can always contact the trusted domain using krb5 	   since we have the domain trust account password	   For (b) we can only use RPC since we have no way of 	   getting a krb5 ticket in our own domain	   For (c) we can always use krb5 since we have a kerberos trust	   --jerry	 */	if (!domain->backend) {		extern struct winbindd_methods reconnect_methods;#ifdef HAVE_ADS		extern struct winbindd_methods ads_methods;		/* find our domain first so we can figure out if we 		   are joined to a kerberized domain */		if ( !domain->primary )			our_domain = find_our_domain();		if ( (our_domain->active_directory || IS_DC) && domain->active_directory ) {			DEBUG(5,("get_cache: Setting ADS methods for domain %s\n", domain->name));			domain->backend = &ads_methods;		} else {#endif	/* HAVE_ADS */			DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n", domain->name));			domain->backend = &reconnect_methods;#ifdef HAVE_ADS		}#endif	/* HAVE_ADS */	}	if (ret)		return ret;		ret = SMB_XMALLOC_P(struct winbind_cache);	ZERO_STRUCTP(ret);	wcache = ret;	wcache_flush_cache();	return ret;}/*  free a centry structure*/static void centry_free(struct cache_entry *centry){	if (!centry)		return;	SAFE_FREE(centry->data);	free(centry);}/*  pull a uint32 from a cache entry */static uint32 centry_uint32(struct cache_entry *centry){	uint32 ret;	if (centry->len - centry->ofs < 4) {		DEBUG(0,("centry corruption? needed 4 bytes, have %d\n", 			 centry->len - centry->ofs));		smb_panic("centry_uint32");	}	ret = IVAL(centry->data, centry->ofs);	centry->ofs += 4;	return ret;}/*  pull a uint8 from a cache entry */static uint8 centry_uint8(struct cache_entry *centry){	uint8 ret;	if (centry->len - centry->ofs < 1) {		DEBUG(0,("centry corruption? needed 1 bytes, have %d\n", 			 centry->len - centry->ofs));		smb_panic("centry_uint32");	}	ret = CVAL(centry->data, centry->ofs);	centry->ofs += 1;	return ret;}/* pull a string from a cache entry, using the supplied   talloc context */static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx){	uint32 len;	char *ret;	len = centry_uint8(centry);	if (len == 0xFF) {		/* a deliberate NULL string */		return NULL;	}	if (centry->len - centry->ofs < len) {		DEBUG(0,("centry corruption? needed %d bytes, have %d\n", 			 len, centry->len - centry->ofs));		smb_panic("centry_string");	}	if (mem_ctx != NULL)		ret = TALLOC(mem_ctx, len+1);	else		ret = SMB_MALLOC(len+1);	if (!ret) {		smb_panic("centry_string out of memory\n");	}	memcpy(ret,centry->data + centry->ofs, len);	ret[len] = 0;	centry->ofs += len;	return ret;}/* pull a string from a cache entry, using the supplied   talloc context */static BOOL centry_sid(struct cache_entry *centry, DOM_SID *sid){	char *sid_string;	sid_string = centry_string(centry, NULL);	if (!string_to_sid(sid, sid_string)) {		return False;	}	SAFE_FREE(sid_string);	return True;}/* the server is considered down if it can't give us a sequence number */static BOOL wcache_server_down(struct winbindd_domain *domain){	BOOL ret;	if (!wcache->tdb)		return False;	ret = (domain->sequence_number == DOM_SEQUENCE_NONE);	if (ret)		DEBUG(10,("wcache_server_down: server for Domain %s down\n", 			domain->name ));	return ret;}static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now ){	TDB_DATA data;	fstring key;	uint32 time_diff;		if (!wcache->tdb) {		DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));		return NT_STATUS_UNSUCCESSFUL;	}			fstr_sprintf( key, "SEQNUM/%s", domain->name );		data = tdb_fetch_bystring( wcache->tdb, key );	if ( !data.dptr || data.dsize!=8 ) {		DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key ));		return NT_STATUS_UNSUCCESSFUL;	}		domain->sequence_number = IVAL(data.dptr, 0);	domain->last_seq_check  = IVAL(data.dptr, 4);		SAFE_FREE(data.dptr);	/* have we expired? */		time_diff = now - domain->last_seq_check;	if ( time_diff > lp_winbind_cache_time() ) {		DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",			domain->name, domain->sequence_number,			(uint32)domain->last_seq_check));		return NT_STATUS_UNSUCCESSFUL;	}	DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n", 		domain->name, domain->sequence_number, 		(uint32)domain->last_seq_check));	return NT_STATUS_OK;}static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain ){	TDB_DATA data, key;	fstring key_str;	char buf[8];		if (!wcache->tdb) {		DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));		return NT_STATUS_UNSUCCESSFUL;	}			fstr_sprintf( key_str, "SEQNUM/%s", domain->name );	key.dptr = key_str;	key.dsize = strlen(key_str)+1;		SIVAL(buf, 0, domain->sequence_number);	SIVAL(buf, 4, domain->last_seq_check);	data.dptr = buf;	data.dsize = 8;		if ( tdb_store( wcache->tdb, key, data, TDB_REPLACE) == -1 ) {		DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str ));		return NT_STATUS_UNSUCCESSFUL;	}	DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n", 		domain->name, domain->sequence_number, 		(uint32)domain->last_seq_check));		return NT_STATUS_OK;}/*  refresh the domain sequence number. If force is True  then always refresh it, no matter how recently we fetched it*/static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force){	NTSTATUS status;	unsigned time_diff;	time_t t = time(NULL);	unsigned cache_time = lp_winbind_cache_time();	get_cache( domain );#if 0	/* JERRY -- disable as the default cache time is now 5 minutes */	/* trying to reconnect is expensive, don't do it too often */	if (domain->sequence_number == DOM_SEQUENCE_NONE) {		cache_time *= 8;	}#endif	time_diff = t - domain->last_seq_check;	/* see if we have to refetch the domain sequence number */	if (!force && (time_diff < cache_time)) {		DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));		goto done;	}		/* try to get the sequence number from the tdb cache first */	/* this will update the timestamp as well */		status = fetch_cache_seqnum( domain, t );	if ( NT_STATUS_IS_OK(status) )		goto done;		/* important! make sure that we know if this is a native 	   mode domain or not */	status = domain->backend->sequence_number(domain, &domain->sequence_number);	if (!NT_STATUS_IS_OK(status)) {		domain->sequence_number = DOM_SEQUENCE_NONE;	}		domain->last_status = status;	domain->last_seq_check = time(NULL);		/* save the new sequence number ni the cache */	store_cache_seqnum( domain );done:	DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n", 		   domain->name, domain->sequence_number));	return;}/*  decide if a cache entry has expired*/static BOOL centry_expired(struct winbindd_domain *domain, const char *keystr, struct cache_entry *centry){	/* if the server is OK and our cache entry came from when it was down then	   the entry is invalid */	if (domain->sequence_number != DOM_SEQUENCE_NONE && 	    centry->sequence_number == DOM_SEQUENCE_NONE) {		DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",			keystr, domain->name ));		return True;	}	/* if the server is down or the cache entry is not older than the	   current sequence number then it is OK */	if (wcache_server_down(domain) || 	    centry->sequence_number == domain->sequence_number) {		DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",			keystr, domain->name ));		return False;	}	DEBUG(10,("centry_expired: Key %s for domain %s expired\n",		keystr, domain->name ));	/* it's expired */	return True;}/*  fetch an entry from the cache, with a varargs key. auto-fetch the sequence  number and return status*/static struct cache_entry *wcache_fetch(struct winbind_cache *cache, 					struct winbindd_domain *domain,					const char *format, ...) PRINTF_ATTRIBUTE(3,4);static struct cache_entry *wcache_fetch(struct winbind_cache *cache, 					struct winbindd_domain *domain,					const char *format, ...){	va_list ap;	char *kstr;	TDB_DATA data;	struct cache_entry *centry;	TDB_DATA key;	refresh_sequence_number(domain, False);	va_start(ap, format);	smb_xvasprintf(&kstr, format, ap);	va_end(ap);		key.dptr = kstr;	key.dsize = strlen(kstr);	data = tdb_fetch(wcache->tdb, key);	if (!data.dptr) {		/* a cache miss */		free(kstr);		return NULL;	}	centry = SMB_XMALLOC_P(struct cache_entry);	centry->data = (unsigned char *)data.dptr;	centry->len = data.dsize;	centry->ofs = 0;	if (centry->len < 8) {		/* huh? corrupt cache? */		DEBUG(10,("wcache_fetch: Corrupt cache for key %s domain %s (len < 8) ?\n",			kstr, domain->name ));		centry_free(centry);		free(kstr);		return NULL;	}		centry->status = NT_STATUS(centry_uint32(centry));	centry->sequence_number = centry_uint32(centry);	if (centry_expired(domain, kstr, centry)) {		DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",			 kstr, domain->name ));		centry_free(centry);		free(kstr);		return NULL;	}	DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",		 kstr, domain->name ));	free(kstr);	return centry;}/*  make sure we have at least len bytes available in a centry */static void centry_expand(struct cache_entry *centry, uint32 len){	uint8 *p;	if (centry->len - centry->ofs >= len)		return;	centry->len *= 2;	p = SMB_REALLOC(centry->data, centry->len);	if (!p) {		DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));		smb_panic("out of memory in centry_expand");	}	centry->data = p;}/*  push a uint32 into a centry */static void centry_put_uint32(struct cache_entry *centry, uint32 v){	centry_expand(centry, 4);	SIVAL(centry->data, centry->ofs, v);	centry->ofs += 4;}/*  push a uint8 into a centry */static void centry_put_uint8(struct cache_entry *centry, uint8 v){	centry_expand(centry, 1);	SCVAL(centry->data, centry->ofs, v);	centry->ofs += 1;}/*    push a string into a centry  */static void centry_put_string(struct cache_entry *centry, const char *s){	int len;	if (!s) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?