2002-06-15 15:15:31 +04:00
/*
Unix SMB / CIFS implementation .
Authenticate against a remote domain
Copyright ( C ) Andrew Tridgell 1992 - 2002
Copyright ( C ) Andrew Bartlett 2002
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-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2002-06-15 15:15:31 +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 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2002-06-15 15:15:31 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2012-03-11 00:33:11 +04:00
# include "lib/tdb_wrap/tdb_wrap.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2011-10-12 16:01:08 +04:00
# include "lib/param/param.h"
2002-06-15 15:15:31 +04:00
/* For reasons known only to MS, many of their NT/Win2k versions
need serialised access only . Two connections at the same time
may ( in certain situations ) cause connections to be reset ,
or access to be denied .
2013-01-15 16:17:00 +04:00
This locking allows smbd ' s multithread architecture to look
2002-06-15 15:15:31 +04:00
like the single - connection that NT makes . */
2008-03-10 23:08:29 +03:00
struct named_mutex {
struct tdb_wrap * tdb ;
char * name ;
} ;
2002-06-15 15:15:31 +04:00
2008-03-10 23:08:29 +03:00
static int unlock_named_mutex ( struct named_mutex * mutex )
2002-06-15 15:15:31 +04:00
{
2008-03-10 23:08:29 +03:00
tdb_unlock_bystring ( mutex - > tdb - > tdb , mutex - > name ) ;
return 0 ;
}
struct named_mutex * grab_named_mutex ( TALLOC_CTX * mem_ctx , const char * name ,
int timeout )
{
struct named_mutex * result ;
2011-10-12 16:01:08 +04:00
struct loadparm_context * lp_ctx ;
2014-03-26 18:35:31 +04:00
const char * fname ;
2008-03-10 23:08:29 +03:00
result = talloc ( mem_ctx , struct named_mutex ) ;
if ( result = = NULL ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return NULL ;
2002-06-15 15:15:31 +04:00
}
2008-03-10 23:08:29 +03:00
2012-06-27 17:24:39 +04:00
lp_ctx = loadparm_init_s3 ( result , loadparm_s3_helpers ( ) ) ;
2011-10-12 16:01:08 +04:00
if ( lp_ctx = = NULL ) {
DEBUG ( 0 , ( " loadparm_init_s3 failed \n " ) ) ;
talloc_free ( result ) ;
return NULL ;
}
2008-03-10 23:08:29 +03:00
result - > name = talloc_strdup ( result , name ) ;
if ( result - > name = = NULL ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
2002-06-15 15:15:31 +04:00
}
2014-03-26 18:35:31 +04:00
fname = lock_path ( " mutex.tdb " ) ;
2014-03-26 18:41:03 +04:00
result - > tdb = tdb_wrap_open ( result , fname ,
lpcfg_tdb_hash_size ( lp_ctx , fname ) ,
lpcfg_tdb_flags ( lp_ctx ,
TDB_DEFAULT |
TDB_CLEAR_IF_FIRST |
TDB_INCOMPATIBLE_HASH ) ,
O_RDWR | O_CREAT , 0600 ) ;
2011-10-12 16:01:08 +04:00
talloc_unlink ( result , lp_ctx ) ;
2008-03-10 23:08:29 +03:00
if ( result - > tdb = = NULL ) {
DEBUG ( 1 , ( " Could not open mutex.tdb: %s \n " ,
strerror ( errno ) ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2002-06-15 15:15:31 +04:00
2008-03-10 23:08:29 +03:00
if ( tdb_lock_bystring_with_timeout ( result - > tdb - > tdb , name ,
2011-06-20 13:10:33 +04:00
timeout ) ! = 0 ) {
2008-03-10 23:08:29 +03:00
DEBUG ( 1 , ( " Could not get the lock for %s \n " , name ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
2002-06-15 15:15:31 +04:00
}
2008-03-10 23:08:29 +03:00
talloc_set_destructor ( result , unlock_named_mutex ) ;
return result ;
2002-06-15 15:15:31 +04:00
}