2004-10-16 17:47:00 +04:00
/*
Unix SMB / CIFS implementation .
database wrap functions
Copyright ( C ) Andrew Tridgell 2004
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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2004-10-16 17:47:00 +04:00
( 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
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-10-16 17:47:00 +04:00
*/
/*
the stupidity of the unix fcntl locking design forces us to never
allow a database file to be opened twice in the same process . These
wrappers provide convenient access to a tdb or ldb , taking advantage
of talloc destructors to ensure that only a single open is done
*/
# include "includes.h"
2006-08-30 15:29:34 +04:00
# include "lib/util/dlinklist.h"
2005-08-28 06:37:14 +04:00
# include "lib/events/events.h"
2005-02-10 06:48:43 +03:00
# include "lib/tdb/include/tdb.h"
2004-11-16 12:00:52 +03:00
# include "lib/ldb/include/ldb.h"
2006-04-30 22:45:43 +04:00
# include "lib/ldb/include/ldb_errors.h"
2007-09-11 20:06:32 +04:00
# include "lib/ldb-samba/ldif_handlers.h"
2005-02-10 10:22:25 +03:00
# include "db_wrap.h"
2007-06-21 14:18:20 +04:00
# include "dsdb/samdb/samdb.h"
2007-09-08 16:42:09 +04:00
# include "param/param.h"
2004-10-16 17:47:00 +04:00
static struct tdb_wrap * tdb_list ;
/*
this is used to catch debug messages from ldb
*/
static void ldb_wrap_debug ( void * context , enum ldb_debug_level level ,
const char * fmt , va_list ap ) PRINTF_ATTRIBUTE ( 3 , 0 ) ;
static void ldb_wrap_debug ( void * context , enum ldb_debug_level level ,
const char * fmt , va_list ap )
{
2007-08-06 05:36:49 +04:00
int samba_level ;
2004-10-16 17:47:00 +04:00
char * s = NULL ;
2007-08-06 05:36:49 +04:00
switch ( level ) {
case LDB_DEBUG_FATAL :
samba_level = 0 ;
break ;
case LDB_DEBUG_ERROR :
samba_level = 1 ;
break ;
case LDB_DEBUG_WARNING :
samba_level = 2 ;
break ;
case LDB_DEBUG_TRACE :
samba_level = 5 ;
break ;
} ;
2004-10-16 17:47:00 +04:00
vasprintf ( & s , fmt , ap ) ;
if ( ! s ) return ;
DEBUG ( level , ( " ldb: %s \n " , s ) ) ;
free ( s ) ;
}
2006-02-04 10:57:57 +03:00
char * wrap_casefold ( void * context , void * mem_ctx , const char * s )
2006-02-04 04:27:47 +03:00
{
return strupper_talloc ( mem_ctx , s ) ;
}
2006-10-17 05:18:32 +04:00
/* check for memory leaks on the ldb context */
2006-10-16 01:31:58 +04:00
static int ldb_wrap_destructor ( struct ldb_context * ldb )
{
2006-10-17 05:18:32 +04:00
size_t * startup_blocks = ( size_t * ) ldb_get_opaque ( ldb , " startup_blocks " ) ;
if ( startup_blocks & &
2007-08-06 07:01:59 +04:00
talloc_total_blocks ( ldb ) > * startup_blocks + 400 ) {
2006-10-17 05:18:32 +04:00
DEBUG ( 0 , ( " WARNING: probable memory leak in ldb %s - %lu blocks (startup %lu) %lu bytes \n " ,
2006-10-16 01:31:58 +04:00
( char * ) ldb_get_opaque ( ldb , " wrap_url " ) ,
( unsigned long ) talloc_total_blocks ( ldb ) ,
2006-10-17 05:18:32 +04:00
( unsigned long ) * startup_blocks ,
2006-10-16 01:31:58 +04:00
( unsigned long ) talloc_total_size ( ldb ) ) ) ;
#if 0
talloc_report_full ( ldb , stdout ) ;
2007-08-06 07:01:59 +04:00
call_backtrace ( ) ;
smb_panic ( " probable memory leak in ldb " ) ;
2006-10-16 01:31:58 +04:00
# endif
}
return 0 ;
}
2004-10-16 17:47:00 +04:00
/*
wrapped connection to a ldb database
2005-02-27 14:35:47 +03:00
to close just talloc_free ( ) the returned ldb_context
2006-05-04 17:06:08 +04:00
TODO : We need an error_string parameter
2004-10-16 17:47:00 +04:00
*/
2005-02-27 14:35:47 +03:00
struct ldb_context * ldb_wrap_connect ( TALLOC_CTX * mem_ctx ,
2007-10-01 22:52:55 +04:00
struct loadparm_context * lp_ctx ,
2005-06-18 11:44:36 +04:00
const char * url ,
2005-12-14 10:22:25 +03:00
struct auth_session_info * session_info ,
struct cli_credentials * credentials ,
2005-06-18 11:44:36 +04:00
unsigned int flags ,
const char * options [ ] )
2004-10-16 17:47:00 +04:00
{
2005-02-27 14:35:47 +03:00
struct ldb_context * ldb ;
2005-06-18 11:44:36 +04:00
int ret ;
2005-06-20 09:04:45 +04:00
struct event_context * ev ;
2005-06-26 04:12:44 +04:00
char * real_url = NULL ;
2006-10-17 05:18:32 +04:00
size_t * startup_blocks ;
2004-10-16 17:47:00 +04:00
2005-07-20 04:59:38 +04:00
ldb = ldb_init ( mem_ctx ) ;
2005-02-27 14:35:47 +03:00
if ( ldb = = NULL ) {
2004-10-16 17:47:00 +04:00
return NULL ;
}
2005-06-20 09:04:45 +04:00
2007-09-11 19:42:19 +04:00
ldb_set_modules_dir ( ldb ,
2007-10-01 22:52:55 +04:00
talloc_asprintf ( ldb , " %s/ldb " , lp_modulesdir ( lp_ctx ) ) ) ;
2007-09-11 19:42:19 +04:00
2005-06-20 09:04:45 +04:00
/* we want to use the existing event context if possible. This
relies on the fact that in smbd , everything is a child of
the main event_context */
2005-08-28 06:37:14 +04:00
ev = event_context_find ( ldb ) ;
2005-06-24 04:18:20 +04:00
2005-12-11 11:27:14 +03:00
if ( ldb_set_opaque ( ldb , " EventContext " , ev ) ) {
2005-12-14 10:22:25 +03:00
talloc_free ( ldb ) ;
return NULL ;
}
if ( ldb_set_opaque ( ldb , " sessionInfo " , session_info ) ) {
talloc_free ( ldb ) ;
return NULL ;
}
if ( ldb_set_opaque ( ldb , " credentials " , credentials ) ) {
talloc_free ( ldb ) ;
2005-12-11 11:27:14 +03:00
return NULL ;
}
2007-06-21 14:18:20 +04:00
2007-10-01 22:52:55 +04:00
if ( strcmp ( lp_sam_url ( lp_ctx ) , url ) = = 0 ) {
2007-06-21 14:18:20 +04:00
dsdb_set_global_schema ( ldb ) ;
}
2005-12-11 11:27:14 +03:00
2005-06-24 04:18:20 +04:00
ret = ldb_register_samba_handlers ( ldb ) ;
if ( ret = = - 1 ) {
talloc_free ( ldb ) ;
return NULL ;
2005-06-20 09:04:45 +04:00
}
2005-06-26 04:12:44 +04:00
2007-08-06 05:36:49 +04:00
ldb_set_debug ( ldb , ldb_wrap_debug , NULL ) ;
ldb_set_utf8_fns ( ldb , NULL , wrap_casefold ) ;
2007-10-01 22:52:55 +04:00
real_url = private_path ( ldb , lp_ctx , url ) ;
2005-06-26 04:12:44 +04:00
if ( real_url = = NULL ) {
talloc_free ( ldb ) ;
return NULL ;
}
2005-09-22 08:57:35 +04:00
/* allow admins to force non-sync ldb for all databases */
2007-10-01 22:52:55 +04:00
if ( lp_parm_bool ( lp_ctx , NULL , " ldb " , " nosync " , false ) ) {
2005-09-22 08:57:35 +04:00
flags | = LDB_FLG_NOSYNC ;
}
2006-09-27 10:02:07 +04:00
/* we usually want Samba databases to be private. If we later
find we need one public , we will need to add a parameter to
ldb_wrap_connect ( ) */
ldb_set_create_perms ( ldb , 0600 ) ;
2005-06-18 11:44:36 +04:00
2005-06-26 04:12:44 +04:00
ret = ldb_connect ( ldb , real_url , flags , options ) ;
2006-04-30 22:45:43 +04:00
if ( ret ! = LDB_SUCCESS ) {
2005-06-18 11:44:36 +04:00
talloc_free ( ldb ) ;
return NULL ;
}
2006-02-04 04:27:47 +03:00
2006-10-17 05:18:32 +04:00
/* setup for leak detection */
2006-10-16 01:31:58 +04:00
ldb_set_opaque ( ldb , " wrap_url " , real_url ) ;
2006-10-17 05:18:32 +04:00
startup_blocks = talloc ( ldb , size_t ) ;
* startup_blocks = talloc_total_blocks ( ldb ) ;
ldb_set_opaque ( ldb , " startup_blocks " , startup_blocks ) ;
2006-10-16 01:31:58 +04:00
talloc_set_destructor ( ldb , ldb_wrap_destructor ) ;
2005-02-27 14:35:47 +03:00
return ldb ;
2004-10-16 17:47:00 +04:00
}
/*
Log tdb messages via DEBUG ( ) .
*/
2006-07-03 10:40:56 +04:00
static void tdb_wrap_log ( TDB_CONTEXT * tdb , enum tdb_debug_level level ,
2004-10-16 17:47:00 +04:00
const char * format , . . . ) PRINTF_ATTRIBUTE ( 3 , 4 ) ;
2006-07-03 10:40:56 +04:00
static void tdb_wrap_log ( TDB_CONTEXT * tdb , enum tdb_debug_level level ,
2004-10-16 17:47:00 +04:00
const char * format , . . . )
{
va_list ap ;
char * ptr = NULL ;
2006-07-03 10:40:56 +04:00
int debug_level ;
2004-10-16 17:47:00 +04:00
va_start ( ap , format ) ;
vasprintf ( & ptr , format , ap ) ;
va_end ( ap ) ;
2006-07-03 10:40:56 +04:00
switch ( level ) {
case TDB_DEBUG_FATAL :
debug_level = 0 ;
break ;
case TDB_DEBUG_ERROR :
debug_level = 1 ;
break ;
case TDB_DEBUG_WARNING :
debug_level = 2 ;
break ;
case TDB_DEBUG_TRACE :
debug_level = 5 ;
break ;
default :
debug_level = 0 ;
}
2004-10-16 17:47:00 +04:00
if ( ptr ! = NULL ) {
2005-09-16 07:52:42 +04:00
const char * name = tdb_name ( tdb ) ;
2006-07-03 10:40:56 +04:00
DEBUG ( debug_level , ( " tdb(%s): %s " , name ? name : " unnamed " , ptr ) ) ;
2004-10-16 17:47:00 +04:00
free ( ptr ) ;
}
}
/* destroy the last connection to a tdb */
2006-05-24 11:32:17 +04:00
static int tdb_wrap_destructor ( struct tdb_wrap * w )
2004-10-16 17:47:00 +04:00
{
tdb_close ( w - > tdb ) ;
DLIST_REMOVE ( tdb_list , w ) ;
return 0 ;
}
/*
wrapped connection to a tdb database
to close just talloc_free ( ) the tdb_wrap pointer
*/
struct tdb_wrap * tdb_wrap_open ( TALLOC_CTX * mem_ctx ,
const char * name , int hash_size , int tdb_flags ,
int open_flags , mode_t mode )
{
struct tdb_wrap * w ;
2006-07-10 16:51:36 +04:00
struct tdb_logging_context log_ctx ;
log_ctx . log_fn = tdb_wrap_log ;
2004-10-16 17:47:00 +04:00
for ( w = tdb_list ; w ; w = w - > next ) {
if ( strcmp ( name , w - > name ) = = 0 ) {
return talloc_reference ( mem_ctx , w ) ;
}
}
2005-01-27 10:08:20 +03:00
w = talloc ( mem_ctx , struct tdb_wrap ) ;
2004-10-16 17:47:00 +04:00
if ( w = = NULL ) {
return NULL ;
}
w - > name = talloc_strdup ( w , name ) ;
w - > tdb = tdb_open_ex ( name , hash_size , tdb_flags ,
2006-07-10 16:51:36 +04:00
open_flags , mode , & log_ctx , NULL ) ;
2004-10-16 17:47:00 +04:00
if ( w - > tdb = = NULL ) {
talloc_free ( w ) ;
return NULL ;
}
talloc_set_destructor ( w , tdb_wrap_destructor ) ;
DLIST_ADD ( tdb_list , w ) ;
return w ;
}