2000-04-29 08:05:38 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-29 08:05:38 +04:00
tdb utility functions
2004-02-28 01:15:38 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
Copyright ( C ) Rafal Szczesniak 2002
2000-04-29 08:05:38 +04:00
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 0213 9 , USA .
*/
# include "includes.h"
2002-07-15 14:35:28 +04:00
# include <fnmatch.h>
2000-04-29 08:05:38 +04:00
/* these are little tdb utility functions that are meant to make
dealing with a tdb database a little less cumbersome in Samba */
2002-10-05 02:53:18 +04:00
static SIG_ATOMIC_T gotalarm ;
/***************************************************************
Signal function to tell us we timed out .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void gotalarm_sig ( void )
{
gotalarm = 1 ;
}
2003-01-14 13:39:32 +03:00
/***************************************************************
Make a TDB_DATA and keep the const warning in one place
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-03-12 12:49:23 +03:00
TDB_DATA make_tdb_data ( const char * dptr , size_t dsize )
2003-01-14 13:39:32 +03:00
{
TDB_DATA ret ;
2005-03-31 09:06:04 +04:00
ret . dptr = CONST_DISCARD ( char * , dptr ) ;
2003-01-14 13:39:32 +03:00
ret . dsize = dsize ;
return ret ;
}
2005-03-12 12:49:23 +03:00
TDB_DATA string_tdb_data ( const char * string )
{
return make_tdb_data ( string , strlen ( string ) ) ;
}
2002-10-05 02:53:18 +04:00
/****************************************************************************
Lock a chain with timeout ( in seconds ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-31 02:55:58 +03:00
static int tdb_chainlock_with_timeout_internal ( TDB_CONTEXT * tdb , TDB_DATA key , unsigned int timeout , int rw_type )
2002-10-05 02:53:18 +04:00
{
/* Allow tdb_chainlock to be interrupted by an alarm. */
int ret ;
gotalarm = 0 ;
2005-03-31 09:06:04 +04:00
tdb_set_lock_alarm ( CONST_DISCARD ( sig_atomic_t * , & gotalarm ) ) ;
2002-10-05 02:53:18 +04:00
if ( timeout ) {
CatchSignal ( SIGALRM , SIGNAL_CAST gotalarm_sig ) ;
alarm ( timeout ) ;
}
2002-11-09 06:36:47 +03:00
if ( rw_type = = F_RDLCK )
ret = tdb_chainlock_read ( tdb , key ) ;
else
ret = tdb_chainlock ( tdb , key ) ;
2002-10-05 02:53:18 +04:00
if ( timeout ) {
alarm ( 0 ) ;
CatchSignal ( SIGALRM , SIGNAL_CAST SIG_IGN ) ;
2002-11-09 06:36:47 +03:00
if ( gotalarm ) {
2003-01-31 02:55:58 +03:00
DEBUG ( 0 , ( " tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s \n " ,
2002-11-09 06:36:47 +03:00
timeout , key . dptr , tdb - > name ) ) ;
2003-01-15 21:57:41 +03:00
/* TODO: If we time out waiting for a lock, it might
* be nice to use F_GETLK to get the pid of the
* process currently holding the lock and print that
* as part of the debugging message . - - mbp */
2002-10-05 02:53:18 +04:00
return - 1 ;
2002-11-09 06:36:47 +03:00
}
2002-10-05 02:53:18 +04:00
}
return ret ;
}
2003-01-31 02:55:58 +03:00
/****************************************************************************
Write lock a chain . Return - 1 if timeout or lock failed .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int tdb_chainlock_with_timeout ( TDB_CONTEXT * tdb , TDB_DATA key , unsigned int timeout )
{
return tdb_chainlock_with_timeout_internal ( tdb , key , timeout , F_WRLCK ) ;
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
2002-10-05 02:53:18 +04:00
Lock a chain by string . Return - 1 if timeout or lock failed .
2001-12-04 21:37:14 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int tdb_lock_bystring ( TDB_CONTEXT * tdb , const char * keyval , unsigned int timeout )
2000-10-10 10:43:26 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , strlen ( keyval ) + 1 ) ;
2000-10-10 10:43:26 +04:00
2003-01-31 02:55:58 +03:00
return tdb_chainlock_with_timeout_internal ( tdb , key , timeout , F_WRLCK ) ;
2000-10-10 10:43:26 +04:00
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Unlock a chain by string .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
void tdb_unlock_bystring ( TDB_CONTEXT * tdb , const char * keyval )
2000-10-10 10:43:26 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , strlen ( keyval ) + 1 ) ;
2003-01-15 21:57:41 +03:00
2000-12-06 05:53:36 +03:00
tdb_chainunlock ( tdb , key ) ;
2000-10-10 10:43:26 +04:00
}
2002-11-09 06:36:47 +03:00
/****************************************************************************
Read lock a chain by string . Return - 1 if timeout or lock failed .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int tdb_read_lock_bystring ( TDB_CONTEXT * tdb , const char * keyval , unsigned int timeout )
2002-11-09 06:36:47 +03:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , strlen ( keyval ) + 1 ) ;
2002-11-09 06:36:47 +03:00
2003-01-31 02:55:58 +03:00
return tdb_chainlock_with_timeout_internal ( tdb , key , timeout , F_RDLCK ) ;
2002-11-09 06:36:47 +03:00
}
/****************************************************************************
Read unlock a chain by string .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
void tdb_read_unlock_bystring ( TDB_CONTEXT * tdb , const char * keyval )
2002-11-09 06:36:47 +03:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , strlen ( keyval ) + 1 ) ;
2002-11-09 06:36:47 +03:00
tdb_chainunlock_read ( tdb , key ) ;
}
2002-01-09 04:32:17 +03:00
/****************************************************************************
Fetch a int32 value by a arbitrary blob key , return - 1 if not found .
Output is int32 in native byte order .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int32 tdb_fetch_int32_byblob ( TDB_CONTEXT * tdb , const char * keyval , size_t len )
2002-01-09 04:32:17 +03:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , len ) ;
TDB_DATA data ;
2002-01-09 04:32:17 +03:00
int32 ret ;
data = tdb_fetch ( tdb , key ) ;
2002-11-23 05:52:36 +03:00
if ( ! data . dptr | | data . dsize ! = sizeof ( int32 ) ) {
SAFE_FREE ( data . dptr ) ;
2002-01-09 04:32:17 +03:00
return - 1 ;
2002-11-23 05:52:36 +03:00
}
2002-01-09 04:32:17 +03:00
ret = IVAL ( data . dptr , 0 ) ;
SAFE_FREE ( data . dptr ) ;
return ret ;
}
/****************************************************************************
Fetch a int32 value by string key , return - 1 if not found .
Output is int32 in native byte order .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int32 tdb_fetch_int32 ( TDB_CONTEXT * tdb , const char * keystr )
2002-01-09 04:32:17 +03:00
{
return tdb_fetch_int32_byblob ( tdb , keystr , strlen ( keystr ) + 1 ) ;
}
/****************************************************************************
Store a int32 value by an arbitary blob key , return 0 on success , - 1 on failure .
Input is int32 in native byte order . Output in tdb is in little - endian .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int tdb_store_int32_byblob ( TDB_CONTEXT * tdb , const char * keystr , size_t len , int32 v )
2002-01-09 04:32:17 +03:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keystr , len ) ;
TDB_DATA data ;
2002-01-09 04:32:17 +03:00
int32 v_store ;
SIVAL ( & v_store , 0 , v ) ;
data . dptr = ( void * ) & v_store ;
data . dsize = sizeof ( int32 ) ;
return tdb_store ( tdb , key , data , TDB_REPLACE ) ;
}
/****************************************************************************
Store a int32 value by string key , return 0 on success , - 1 on failure .
Input is int32 in native byte order . Output in tdb is in little - endian .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int tdb_store_int32 ( TDB_CONTEXT * tdb , const char * keystr , int32 v )
2002-01-09 04:32:17 +03:00
{
return tdb_store_int32_byblob ( tdb , keystr , strlen ( keystr ) + 1 , v ) ;
}
2002-04-08 02:04:39 +04:00
/****************************************************************************
Fetch a uint32 value by a arbitrary blob key , return - 1 if not found .
Output is uint32 in native byte order .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
BOOL tdb_fetch_uint32_byblob ( TDB_CONTEXT * tdb , const char * keyval , size_t len , uint32 * value )
2002-04-08 02:04:39 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keyval , len ) ;
TDB_DATA data ;
2002-04-08 02:04:39 +04:00
data = tdb_fetch ( tdb , key ) ;
2002-11-23 05:52:36 +03:00
if ( ! data . dptr | | data . dsize ! = sizeof ( uint32 ) ) {
SAFE_FREE ( data . dptr ) ;
2002-04-08 02:04:39 +04:00
return False ;
2002-11-23 05:52:36 +03:00
}
2002-04-08 02:04:39 +04:00
* value = IVAL ( data . dptr , 0 ) ;
SAFE_FREE ( data . dptr ) ;
return True ;
}
/****************************************************************************
Fetch a uint32 value by string key , return - 1 if not found .
Output is uint32 in native byte order .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
BOOL tdb_fetch_uint32 ( TDB_CONTEXT * tdb , const char * keystr , uint32 * value )
2002-04-08 02:04:39 +04:00
{
return tdb_fetch_uint32_byblob ( tdb , keystr , strlen ( keystr ) + 1 , value ) ;
}
/****************************************************************************
Store a uint32 value by an arbitary blob key , return 0 on success , - 1 on failure .
Input is uint32 in native byte order . Output in tdb is in little - endian .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
BOOL tdb_store_uint32_byblob ( TDB_CONTEXT * tdb , const char * keystr , size_t len , uint32 value )
2002-04-08 02:04:39 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keystr , len ) ;
TDB_DATA data ;
2002-04-08 02:04:39 +04:00
uint32 v_store ;
BOOL ret = True ;
SIVAL ( & v_store , 0 , value ) ;
data . dptr = ( void * ) & v_store ;
data . dsize = sizeof ( uint32 ) ;
if ( tdb_store ( tdb , key , data , TDB_REPLACE ) = = - 1 )
ret = False ;
return ret ;
}
/****************************************************************************
Store a uint32 value by string key , return 0 on success , - 1 on failure .
Input is uint32 in native byte order . Output in tdb is in little - endian .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
BOOL tdb_store_uint32 ( TDB_CONTEXT * tdb , const char * keystr , uint32 value )
2002-04-08 02:04:39 +04:00
{
return tdb_store_uint32_byblob ( tdb , keystr , strlen ( keystr ) + 1 , value ) ;
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Store a buffer by a null terminated string key . Return 0 on success , - 1
on failure .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-07-11 00:37:01 +04:00
int tdb_store_bystring ( TDB_CONTEXT * tdb , const char * keystr , TDB_DATA data , int flags )
2000-05-09 10:22:12 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keystr , strlen ( keystr ) + 1 ) ;
2003-01-15 21:57:41 +03:00
return tdb_store ( tdb , key , data , flags ) ;
2000-05-09 10:22:12 +04:00
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Fetch a buffer using a null terminated string key . Don ' t forget to call
free ( ) on the result dptr .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-25 04:48:28 +04:00
2003-07-11 00:37:01 +04:00
TDB_DATA tdb_fetch_bystring ( TDB_CONTEXT * tdb , const char * keystr )
2000-05-09 10:22:12 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keystr , strlen ( keystr ) + 1 ) ;
2000-05-09 10:22:12 +04:00
2003-01-15 21:57:41 +03:00
return tdb_fetch ( tdb , key ) ;
2000-05-09 10:22:12 +04:00
}
2000-05-24 10:36:58 +04:00
2002-07-15 14:35:28 +04:00
/****************************************************************************
2002-11-07 05:15:35 +03:00
Delete an entry using a null terminated string key .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-07-11 00:37:01 +04:00
int tdb_delete_bystring ( TDB_CONTEXT * tdb , const char * keystr )
2002-07-15 14:35:28 +04:00
{
2003-01-14 13:39:32 +03:00
TDB_DATA key = make_tdb_data ( keystr , strlen ( keystr ) + 1 ) ;
2002-07-15 14:35:28 +04:00
2003-01-15 21:57:41 +03:00
return tdb_delete ( tdb , key ) ;
2002-07-15 14:35:28 +04:00
}
2002-01-09 04:53:19 +03:00
/****************************************************************************
Atomic integer change . Returns old value . To create , set initial value in * oldval .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int32 tdb_change_int32_atomic ( TDB_CONTEXT * tdb , const char * keystr , int32 * oldval , int32 change_val )
2002-01-09 04:53:19 +03:00
{
int32 val ;
int32 ret = - 1 ;
2002-10-05 02:53:18 +04:00
if ( tdb_lock_bystring ( tdb , keystr , 0 ) = = - 1 )
2002-01-09 04:53:19 +03:00
return - 1 ;
if ( ( val = tdb_fetch_int32 ( tdb , keystr ) ) = = - 1 ) {
2002-07-15 14:35:28 +04:00
/* The lookup failed */
if ( tdb_error ( tdb ) ! = TDB_ERR_NOEXIST ) {
2003-05-14 04:46:43 +04:00
/* but not because it didn't exist */
2002-01-09 04:53:19 +03:00
goto err_out ;
2002-07-15 14:35:28 +04:00
}
/* Start with 'old' value */
2002-01-09 04:53:19 +03:00
val = * oldval ;
} else {
2002-07-15 14:35:28 +04:00
/* It worked, set return value (oldval) to tdb data */
2002-01-09 04:53:19 +03:00
* oldval = val ;
}
2002-07-15 14:35:28 +04:00
/* Increment value for storage and return next time */
val + = change_val ;
2002-01-09 04:53:19 +03:00
if ( tdb_store_int32 ( tdb , keystr , val ) = = - 1 )
goto err_out ;
ret = 0 ;
err_out :
tdb_unlock_bystring ( tdb , keystr ) ;
return ret ;
}
2002-04-08 02:04:39 +04:00
/****************************************************************************
Atomic unsigned integer change . Returns old value . To create , set initial value in * oldval .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
BOOL tdb_change_uint32_atomic ( TDB_CONTEXT * tdb , const char * keystr , uint32 * oldval , uint32 change_val )
2002-04-08 02:04:39 +04:00
{
uint32 val ;
BOOL ret = False ;
2002-10-05 02:53:18 +04:00
if ( tdb_lock_bystring ( tdb , keystr , 0 ) = = - 1 )
2002-04-08 02:04:39 +04:00
return False ;
if ( ! tdb_fetch_uint32 ( tdb , keystr , & val ) ) {
2002-07-15 14:35:28 +04:00
/* It failed */
if ( tdb_error ( tdb ) ! = TDB_ERR_NOEXIST ) {
2003-05-14 04:46:43 +04:00
/* and not because it didn't exist */
2002-04-08 02:04:39 +04:00
goto err_out ;
2002-07-15 14:35:28 +04:00
}
2002-04-08 02:04:39 +04:00
2002-07-15 14:35:28 +04:00
/* Start with 'old' value */
2002-04-08 02:04:39 +04:00
val = * oldval ;
} else {
2002-07-15 14:35:28 +04:00
/* it worked, set return value (oldval) to tdb data */
2002-04-08 02:04:39 +04:00
* oldval = val ;
2002-07-15 14:35:28 +04:00
2002-04-08 02:04:39 +04:00
}
2002-07-15 14:35:28 +04:00
/* get a new value to store */
val + = change_val ;
2002-04-08 02:04:39 +04:00
if ( ! tdb_store_uint32 ( tdb , keystr , val ) )
goto err_out ;
ret = True ;
err_out :
tdb_unlock_bystring ( tdb , keystr ) ;
return ret ;
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Useful pair of routines for packing / unpacking data consisting of
integers and strings .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-05-31 17:46:45 +04:00
size_t tdb_pack_va ( char * buf , int bufsize , const char * fmt , va_list ap )
2000-05-24 10:36:58 +04:00
{
2003-04-22 16:13:13 +04:00
uint8 bt ;
2000-05-24 10:36:58 +04:00
uint16 w ;
uint32 d ;
int i ;
void * p ;
2001-05-06 05:28:17 +04:00
int len ;
2000-05-24 10:36:58 +04:00
char * s ;
2001-04-11 01:07:04 +04:00
char c ;
2000-05-24 10:36:58 +04:00
char * buf0 = buf ;
2003-01-03 11:28:12 +03:00
const char * fmt0 = fmt ;
2000-06-04 06:29:45 +04:00
int bufsize0 = bufsize ;
2000-05-24 10:36:58 +04:00
while ( * fmt ) {
switch ( ( c = * fmt + + ) ) {
2003-04-22 16:13:13 +04:00
case ' b ' : /* unsigned 8-bit integer */
len = 1 ;
bt = ( uint8 ) va_arg ( ap , int ) ;
if ( bufsize & & bufsize > = len )
SSVAL ( buf , 0 , bt ) ;
break ;
case ' w ' : /* unsigned 16-bit integer */
2000-05-24 10:36:58 +04:00
len = 2 ;
2000-11-11 00:24:09 +03:00
w = ( uint16 ) va_arg ( ap , int ) ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len )
2000-05-24 10:36:58 +04:00
SSVAL ( buf , 0 , w ) ;
break ;
2003-04-22 16:13:13 +04:00
case ' d ' : /* signed 32-bit integer (standard int in most systems) */
2000-05-24 10:36:58 +04:00
len = 4 ;
d = va_arg ( ap , uint32 ) ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len )
2000-05-24 10:36:58 +04:00
SIVAL ( buf , 0 , d ) ;
break ;
2003-04-22 16:13:13 +04:00
case ' p ' : /* pointer */
2000-05-24 10:36:58 +04:00
len = 4 ;
p = va_arg ( ap , void * ) ;
d = p ? 1 : 0 ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len )
2000-05-24 10:36:58 +04:00
SIVAL ( buf , 0 , d ) ;
break ;
2003-04-22 16:13:13 +04:00
case ' P ' : /* null-terminated string */
2000-08-01 00:41:51 +04:00
s = va_arg ( ap , char * ) ;
w = strlen ( s ) ;
len = w + 1 ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len )
2000-08-01 00:41:51 +04:00
memcpy ( buf , s , len ) ;
break ;
2003-04-22 16:13:13 +04:00
case ' f ' : /* null-terminated string */
2000-05-24 10:36:58 +04:00
s = va_arg ( ap , char * ) ;
w = strlen ( s ) ;
len = w + 1 ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len )
2000-05-24 10:36:58 +04:00
memcpy ( buf , s , len ) ;
break ;
2003-04-22 16:13:13 +04:00
case ' B ' : /* fixed-length string */
2000-05-24 10:36:58 +04:00
i = va_arg ( ap , int ) ;
s = va_arg ( ap , char * ) ;
len = 4 + i ;
2003-04-10 23:08:45 +04:00
if ( bufsize & & bufsize > = len ) {
2000-05-24 10:36:58 +04:00
SIVAL ( buf , 0 , i ) ;
memcpy ( buf + 4 , s , i ) ;
}
break ;
default :
DEBUG ( 0 , ( " Unknown tdb_pack format %c in %s \n " ,
c , fmt ) ) ;
2001-05-06 05:28:17 +04:00
len = 0 ;
2000-05-24 10:36:58 +04:00
break ;
}
buf + = len ;
2003-04-10 23:08:45 +04:00
if ( bufsize )
bufsize - = len ;
if ( bufsize < 0 )
bufsize = 0 ;
2000-05-24 10:36:58 +04:00
}
2005-05-31 17:46:45 +04:00
DEBUG ( 18 , ( " tdb_pack_va(%s, %d) -> %d \n " ,
2000-06-04 06:29:45 +04:00
fmt0 , bufsize0 , ( int ) PTR_DIFF ( buf , buf0 ) ) ) ;
2000-05-24 10:36:58 +04:00
return PTR_DIFF ( buf , buf0 ) ;
}
2005-05-31 17:46:45 +04:00
size_t tdb_pack ( char * buf , int bufsize , const char * fmt , . . . )
{
va_list ap ;
size_t result ;
va_start ( ap , fmt ) ;
result = tdb_pack_va ( buf , bufsize , fmt , ap ) ;
va_end ( ap ) ;
return result ;
}
BOOL tdb_pack_append ( TALLOC_CTX * mem_ctx , uint8_t * * buf , size_t * len ,
const char * fmt , . . . )
{
va_list ap ;
size_t len1 , len2 ;
va_start ( ap , fmt ) ;
len1 = tdb_pack_va ( NULL , 0 , fmt , ap ) ;
va_end ( ap ) ;
if ( mem_ctx ! = NULL )
* buf = TALLOC_REALLOC_ARRAY ( mem_ctx , * buf , uint8_t ,
( * len ) + len1 ) ;
else
* buf = SMB_REALLOC_ARRAY ( * buf , uint8_t , ( * len ) + len1 ) ;
if ( * buf = = NULL )
return False ;
va_start ( ap , fmt ) ;
2005-10-18 07:24:00 +04:00
len2 = tdb_pack_va ( ( char * ) ( * buf ) + ( * len ) , len1 , fmt , ap ) ;
2005-05-31 17:46:45 +04:00
va_end ( ap ) ;
if ( len1 ! = len2 )
return False ;
* len + = len2 ;
return True ;
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Useful pair of routines for packing / unpacking data consisting of
integers and strings .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-04-18 20:41:04 +04:00
2003-01-03 11:28:12 +03:00
int tdb_unpack ( char * buf , int bufsize , const char * fmt , . . . )
2000-05-24 10:36:58 +04:00
{
2001-04-11 01:07:04 +04:00
va_list ap ;
2003-04-22 16:13:13 +04:00
uint8 * bt ;
2000-05-24 10:36:58 +04:00
uint16 * w ;
uint32 * d ;
2001-05-06 05:28:17 +04:00
int len ;
2000-05-24 10:36:58 +04:00
int * i ;
void * * p ;
char * s , * * b ;
2001-04-11 01:07:04 +04:00
char c ;
2000-05-24 10:36:58 +04:00
char * buf0 = buf ;
2003-01-03 11:28:12 +03:00
const char * fmt0 = fmt ;
2000-06-04 06:29:45 +04:00
int bufsize0 = bufsize ;
2000-05-24 10:36:58 +04:00
va_start ( ap , fmt ) ;
while ( * fmt ) {
switch ( ( c = * fmt + + ) ) {
2003-04-22 16:13:13 +04:00
case ' b ' :
len = 1 ;
bt = va_arg ( ap , uint8 * ) ;
if ( bufsize < len )
goto no_space ;
* bt = SVAL ( buf , 0 ) ;
break ;
2000-05-24 10:36:58 +04:00
case ' w ' :
len = 2 ;
w = va_arg ( ap , uint16 * ) ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len )
goto no_space ;
2000-05-24 10:36:58 +04:00
* w = SVAL ( buf , 0 ) ;
break ;
case ' d ' :
len = 4 ;
d = va_arg ( ap , uint32 * ) ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len )
goto no_space ;
2000-05-24 10:36:58 +04:00
* d = IVAL ( buf , 0 ) ;
break ;
case ' p ' :
len = 4 ;
p = va_arg ( ap , void * * ) ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len )
goto no_space ;
2005-09-30 21:13:37 +04:00
/*
* This isn ' t a real pointer - only a token ( 1 or 0 )
* to mark the fact a pointer is present .
*/
* p = ( void * ) ( IVAL ( buf , 0 ) ? ( void * ) 1 : NULL ) ;
2000-05-24 10:36:58 +04:00
break ;
2000-08-01 00:41:51 +04:00
case ' P ' :
s = va_arg ( ap , char * ) ;
len = strlen ( buf ) + 1 ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len | | len > sizeof ( pstring ) )
goto no_space ;
2000-08-01 00:41:51 +04:00
memcpy ( s , buf , len ) ;
break ;
2000-05-24 10:36:58 +04:00
case ' f ' :
s = va_arg ( ap , char * ) ;
len = strlen ( buf ) + 1 ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len | | len > sizeof ( fstring ) )
goto no_space ;
2000-05-24 10:36:58 +04:00
memcpy ( s , buf , len ) ;
break ;
case ' B ' :
i = va_arg ( ap , int * ) ;
b = va_arg ( ap , char * * ) ;
len = 4 ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len )
goto no_space ;
2000-05-27 13:53:11 +04:00
* i = IVAL ( buf , 0 ) ;
2001-05-30 10:23:05 +04:00
if ( ! * i ) {
* b = NULL ;
break ;
}
2000-05-27 13:53:11 +04:00
len + = * i ;
2001-12-04 21:37:14 +03:00
if ( bufsize < len )
goto no_space ;
2004-12-07 21:25:53 +03:00
* b = ( char * ) SMB_MALLOC ( * i ) ;
2001-12-04 21:37:14 +03:00
if ( ! * b )
goto no_space ;
2000-05-27 13:53:11 +04:00
memcpy ( * b , buf + 4 , * i ) ;
2000-05-24 10:36:58 +04:00
break ;
default :
DEBUG ( 0 , ( " Unknown tdb_unpack format %c in %s \n " ,
c , fmt ) ) ;
2001-05-06 05:28:17 +04:00
len = 0 ;
2000-05-24 10:36:58 +04:00
break ;
}
buf + = len ;
bufsize - = len ;
}
va_end ( ap ) ;
2000-06-04 06:29:45 +04:00
2001-12-05 18:45:36 +03:00
DEBUG ( 18 , ( " tdb_unpack(%s, %d) -> %d \n " ,
2000-06-04 06:29:45 +04:00
fmt0 , bufsize0 , ( int ) PTR_DIFF ( buf , buf0 ) ) ) ;
2000-05-24 10:36:58 +04:00
return PTR_DIFF ( buf , buf0 ) ;
no_space :
return - 1 ;
}
2001-05-30 07:42:44 +04:00
2003-04-22 16:13:13 +04:00
/**
* Pack SID passed by pointer
*
* @ param pack_buf pointer to buffer which is to be filled with packed data
* @ param bufsize size of packing buffer
* @ param sid pointer to sid to be packed
*
* @ return length of the packed representation of the whole structure
* */
size_t tdb_sid_pack ( char * pack_buf , int bufsize , DOM_SID * sid )
{
int idx ;
size_t len = 0 ;
if ( ! sid | | ! pack_buf ) return - 1 ;
len + = tdb_pack ( pack_buf + len , bufsize - len , " bb " , sid - > sid_rev_num ,
sid - > num_auths ) ;
for ( idx = 0 ; idx < 6 ; idx + + ) {
len + = tdb_pack ( pack_buf + len , bufsize - len , " b " , sid - > id_auth [ idx ] ) ;
}
for ( idx = 0 ; idx < MAXSUBAUTHS ; idx + + ) {
len + = tdb_pack ( pack_buf + len , bufsize - len , " d " , sid - > sub_auths [ idx ] ) ;
}
return len ;
}
/**
* Unpack SID into a pointer
*
* @ param pack_buf pointer to buffer with packed representation
* @ param bufsize size of the buffer
* @ param sid pointer to sid structure to be filled with unpacked data
*
* @ return size of structure unpacked from buffer
* */
size_t tdb_sid_unpack ( char * pack_buf , int bufsize , DOM_SID * sid )
{
int idx , len = 0 ;
if ( ! sid | | ! pack_buf ) return - 1 ;
len + = tdb_unpack ( pack_buf + len , bufsize - len , " bb " ,
& sid - > sid_rev_num , & sid - > num_auths ) ;
for ( idx = 0 ; idx < 6 ; idx + + ) {
len + = tdb_unpack ( pack_buf + len , bufsize - len , " b " , & sid - > id_auth [ idx ] ) ;
}
for ( idx = 0 ; idx < MAXSUBAUTHS ; idx + + ) {
len + = tdb_unpack ( pack_buf + len , bufsize - len , " d " , & sid - > sub_auths [ idx ] ) ;
}
return len ;
}
/**
* Pack TRUSTED_DOM_PASS passed by pointer
*
* @ param pack_buf pointer to buffer which is to be filled with packed data
* @ param bufsize size of the buffer
* @ param pass pointer to trusted domain password to be packed
*
* @ return length of the packed representation of the whole structure
* */
size_t tdb_trusted_dom_pass_pack ( char * pack_buf , int bufsize , TRUSTED_DOM_PASS * pass )
{
int idx , len = 0 ;
if ( ! pack_buf | | ! pass ) return - 1 ;
/* packing unicode domain name and password */
len + = tdb_pack ( pack_buf + len , bufsize - len , " d " , pass - > uni_name_len ) ;
for ( idx = 0 ; idx < 32 ; idx + + )
len + = tdb_pack ( pack_buf + len , bufsize - len , " w " , pass - > uni_name [ idx ] ) ;
len + = tdb_pack ( pack_buf + len , bufsize - len , " dPd " , pass - > pass_len ,
pass - > pass , pass - > mod_time ) ;
/* packing SID structure */
len + = tdb_sid_pack ( pack_buf + len , bufsize - len , & pass - > domain_sid ) ;
return len ;
}
/**
* Unpack TRUSTED_DOM_PASS passed by pointer
*
* @ param pack_buf pointer to buffer with packed representation
* @ param bufsize size of the buffer
* @ param pass pointer to trusted domain password to be filled with unpacked data
*
* @ return size of structure unpacked from buffer
* */
size_t tdb_trusted_dom_pass_unpack ( char * pack_buf , int bufsize , TRUSTED_DOM_PASS * pass )
{
int idx , len = 0 ;
if ( ! pack_buf | | ! pass ) return - 1 ;
/* unpack unicode domain name and plaintext password */
len + = tdb_unpack ( pack_buf , bufsize - len , " d " , & pass - > uni_name_len ) ;
for ( idx = 0 ; idx < 32 ; idx + + )
len + = tdb_unpack ( pack_buf + len , bufsize - len , " w " , & pass - > uni_name [ idx ] ) ;
len + = tdb_unpack ( pack_buf + len , bufsize - len , " dPd " , & pass - > pass_len , & pass - > pass ,
& pass - > mod_time ) ;
/* unpack domain sid */
len + = tdb_sid_unpack ( pack_buf + len , bufsize - len , & pass - > domain_sid ) ;
return len ;
}
2001-05-30 07:42:44 +04:00
/****************************************************************************
2001-12-04 21:37:14 +03:00
Log tdb messages via DEBUG ( ) .
2001-05-30 07:42:44 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-12-04 21:37:14 +03:00
2001-05-30 07:42:44 +04:00
static void tdb_log ( TDB_CONTEXT * tdb , int level , const char * format , . . . )
{
va_list ap ;
char * ptr = NULL ;
va_start ( ap , format ) ;
vasprintf ( & ptr , format , ap ) ;
va_end ( ap ) ;
2001-12-04 21:37:14 +03:00
if ( ! ptr | | ! * ptr )
return ;
2001-05-30 07:42:44 +04:00
2002-04-10 03:03:17 +04:00
DEBUG ( level , ( " tdb(%s): %s " , tdb - > name ? tdb - > name : " unnamed " , ptr ) ) ;
2002-01-09 04:32:17 +03:00
SAFE_FREE ( ptr ) ;
2001-05-30 07:42:44 +04:00
}
2001-12-04 21:37:14 +03:00
/****************************************************************************
Like tdb_open ( ) but also setup a logging function that redirects to
the samba DEBUG ( ) system .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-30 07:42:44 +04:00
This is another *BIG* change...
Samba now features a pluggable passdb interface, along the same lines as the
one in use in the auth subsystem. In this case, only one backend may be active
at a time by the 'normal' interface, and only one backend per passdb_context is
permitted outside that.
This pluggable interface is designed to allow any number of passdb backends to
be compiled in, with the selection at runtime. The 'passdb backend' paramater
has been created (and documented!) to support this.
As such, configure has been modfied to allow (for example) --with-ldap and the
old smbpasswd to be selected at the same time.
This patch also introduces two new backends: smbpasswd_nua and tdbsam_nua.
These two backends accept 'non unix accounts', where the user does *not* exist
in /etc/passwd. These accounts' don't have UIDs in the unix sense, but to
avoid conflicts in the algroitmic mapping of RIDs, they use the values
specified in the 'non unix account range' paramter - in the same way as the
winbind ranges are specifed.
While I was at it, I cleaned up some of the code in pdb_tdb (code copied
directly from smbpasswd and not really considered properly). Most of this was
to do with % macro expansion on stored data. It isn't easy to get the macros
into the tdb, and the first password change will 'expand' them. tdbsam needs
to use a similar system to pdb_ldap in this regard.
This patch only makes minor adjustments to pdb_nisplus and pdb_ldap, becouse I
don't have the test facilities for these. I plan to incoroprate at least
pdb_ldap into this scheme after consultation with Jerry.
Each (converted) passdb module now no longer has any 'static' variables, and
only exports 1 init function outside its .c file.
The non-unix-account support in this patch has been proven! It is now possible
to join a win2k machine to a Samba PDC without an account in /etc/passwd!
Other changes:
Minor interface adjustments:
pdb_delete_sam_account() now takes a SAM_ACCOUNT, not a char*.
pdb_update_sam_account() no longer takes the 'override' argument that was being
ignored so often (every other passdb backend). Extra checks have been added in
some places.
Minor code changes:
smbpasswd no longer attempts to initialise the passdb at startup, this is
now done on first use.
pdbedit has lost some of its 'machine account' logic, as this behaviour is now
controlled by the passdb subsystem directly.
The samr subsystem no longer calls 'local password change', but does the pdb
interactions directly. This allow the ACB_ flags specifed to be transferred
direct to the backend, without interference.
Doco:
I've updated the doco to reflect some of the changes, and removed some paramters
no longer applicable to HEAD.
(This used to be commit ff354c99c585068af6dc1ff35a1f109a806b326b)
2002-01-20 17:30:58 +03:00
TDB_CONTEXT * tdb_open_log ( const char * name , int hash_size , int tdb_flags ,
2001-05-30 07:42:44 +04:00
int open_flags , mode_t mode )
{
2001-09-06 09:45:07 +04:00
TDB_CONTEXT * tdb ;
2001-12-04 21:37:14 +03:00
if ( ! lp_use_mmap ( ) )
tdb_flags | = TDB_NOMMAP ;
2001-09-06 09:45:07 +04:00
2002-01-18 05:15:04 +03:00
tdb = tdb_open_ex ( name , hash_size , tdb_flags ,
2004-08-25 05:04:02 +04:00
open_flags , mode , tdb_log , NULL ) ;
2001-12-04 21:37:14 +03:00
if ( ! tdb )
return NULL ;
2001-05-30 07:42:44 +04:00
return tdb ;
}
2002-01-04 01:48:48 +03:00
/****************************************************************************
Allow tdb_delete to be used as a tdb_traversal_fn .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int tdb_traverse_delete_fn ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf ,
void * state )
{
return tdb_delete ( the_tdb , key ) ;
}
2002-07-15 14:35:28 +04:00
/**
* Search across the whole tdb for keys that match the given pattern
* return the result as a list of keys
*
* @ param tdb pointer to opened tdb file context
* @ param pattern searching pattern used by fnmatch ( 3 ) functions
*
* @ return list of keys found by looking up with given pattern
* */
TDB_LIST_NODE * tdb_search_keys ( TDB_CONTEXT * tdb , const char * pattern )
{
TDB_DATA key , next ;
TDB_LIST_NODE * list = NULL ;
TDB_LIST_NODE * rec = NULL ;
TDB_LIST_NODE * tmp = NULL ;
for ( key = tdb_firstkey ( tdb ) ; key . dptr ; key = next ) {
/* duplicate key string to ensure null-termination */
2004-12-07 21:25:53 +03:00
char * key_str = ( char * ) SMB_STRNDUP ( key . dptr , key . dsize ) ;
2002-07-15 14:35:28 +04:00
if ( ! key_str ) {
DEBUG ( 0 , ( " tdb_search_keys: strndup() failed! \n " ) ) ;
smb_panic ( " strndup failed! \n " ) ;
}
DEBUG ( 18 , ( " checking %s for match to pattern %s \n " , key_str , pattern ) ) ;
next = tdb_nextkey ( tdb , key ) ;
/* do the pattern checking */
if ( fnmatch ( pattern , key_str , 0 ) = = 0 ) {
2004-12-07 21:25:53 +03:00
rec = SMB_MALLOC_P ( TDB_LIST_NODE ) ;
2002-07-15 14:35:28 +04:00
ZERO_STRUCTP ( rec ) ;
rec - > node_key = key ;
DLIST_ADD_END ( list , rec , tmp ) ;
DEBUG ( 18 , ( " checking %s matched pattern %s \n " , key_str , pattern ) ) ;
} else {
free ( key . dptr ) ;
}
/* free duplicated key string */
free ( key_str ) ;
}
return list ;
2003-09-22 21:53:59 +04:00
}
2002-07-15 14:35:28 +04:00
/**
* Free the list returned by tdb_search_keys
*
* @ param node list of results found by tdb_search_keys
* */
void tdb_search_list_free ( TDB_LIST_NODE * node )
{
TDB_LIST_NODE * next_node ;
while ( node ) {
next_node = node - > next ;
2003-07-01 17:04:50 +04:00
SAFE_FREE ( node - > node_key . dptr ) ;
2002-07-15 14:35:28 +04:00
SAFE_FREE ( node ) ;
node = next_node ;
} ;
2003-09-22 21:53:59 +04:00
}