1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Samba utility functions
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
2007-11-10 02:09:16 +03:00
Copyright ( C ) Jeremy Allison 2001 - 2007
2001-11-12 03:53:34 +03:00
Copyright ( C ) Simo Sorce 2001
2003-08-01 19:21:20 +04:00
Copyright ( C ) Jim McDonough < jmcd @ us . ibm . com > 2003
2006-04-12 04:07:40 +04:00
Copyright ( C ) James Peach 2006
1996-05-04 11:50:46 +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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2009-11-21 18:17:16 +03:00
1996-05-04 11:50:46 +04:00
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 .
2009-11-21 18:17:16 +03:00
1996-05-04 11:50:46 +04:00
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/>.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2010-08-05 12:49:53 +04:00
# include "popt_common.h"
2010-08-05 04:25:37 +04:00
# include "secrets.h"
2010-08-26 12:14:07 +04:00
# include "ctdbd_conn.h"
1996-05-04 11:50:46 +04:00
2005-04-06 20:28:04 +04:00
extern char * global_clobber_region_function ;
extern unsigned int global_clobber_region_line ;
2004-12-07 21:25:53 +03:00
/* Max allowable allococation - 256mb - 0x10000000 */
# define MAX_ALLOC_SIZE (1024*1024*256)
1998-07-29 07:08:05 +04:00
# if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
1998-08-08 05:15:14 +04:00
# ifdef WITH_NISPLUS_HOME
1999-12-13 16:27:58 +03:00
# ifdef BROKEN_NISPLUS_INCLUDE_FILES
/*
* The following lines are needed due to buggy include files
* in Solaris 2.6 which define GROUP in both / usr / include / sys / acl . h and
* also in / usr / include / rpcsvc / nis . h . The definitions conflict . JRA .
* Also GROUP_OBJ is defined as 0x4 in / usr / include / sys / acl . h and as
* an enum in / usr / include / rpcsvc / nis . h .
*/
# if defined(GROUP)
# undef GROUP
1997-10-20 16:10:58 +04:00
# endif
1999-12-13 16:27:58 +03:00
# if defined(GROUP_OBJ)
# undef GROUP_OBJ
1998-04-10 00:48:48 +04:00
# endif
1997-10-20 16:10:58 +04:00
1999-12-13 16:27:58 +03:00
# endif /* BROKEN_NISPLUS_INCLUDE_FILES */
# include <rpcsvc/nis.h>
# endif /* WITH_NISPLUS_HOME */
# endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
2009-11-23 18:33:26 +03:00
static enum protocol_types Protocol = PROTOCOL_COREPLUS ;
enum protocol_types get_Protocol ( void )
{
return Protocol ;
}
void set_Protocol ( enum protocol_types p )
{
Protocol = p ;
}
1997-09-05 00:26:07 +04:00
static enum remote_arch_types ra_type = RA_UNKNOWN ;
1997-10-19 19:33:25 +04:00
2002-11-13 02:20:50 +03:00
/***********************************************************************
Definitions for all names .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
static char * smb_scope ;
static int smb_num_netbios_names ;
static char * * smb_my_netbios_names ;
/***********************************************************************
Allocate and set scope . Ensure upper case .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool set_global_scope ( const char * scope )
2002-11-13 02:20:50 +03:00
{
SAFE_FREE ( smb_scope ) ;
2004-12-07 21:25:53 +03:00
smb_scope = SMB_STRDUP ( scope ) ;
2002-11-13 02:20:50 +03:00
if ( ! smb_scope )
return False ;
2003-07-03 23:11:31 +04:00
strupper_m ( smb_scope ) ;
2002-11-13 02:20:50 +03:00
return True ;
}
2002-12-06 22:59:04 +03:00
/*********************************************************************
Ensure scope is never null string .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
const char * global_scope ( void )
{
2002-12-06 22:59:04 +03:00
if ( ! smb_scope )
set_global_scope ( " " ) ;
2002-11-13 02:20:50 +03:00
return smb_scope ;
}
static void free_netbios_names_array ( void )
{
int i ;
for ( i = 0 ; i < smb_num_netbios_names ; i + + )
SAFE_FREE ( smb_my_netbios_names [ i ] ) ;
SAFE_FREE ( smb_my_netbios_names ) ;
smb_num_netbios_names = 0 ;
}
2007-10-19 04:40:25 +04:00
static bool allocate_my_netbios_names_array ( size_t number )
2002-11-13 02:20:50 +03:00
{
free_netbios_names_array ( ) ;
smb_num_netbios_names = number + 1 ;
2004-12-07 21:25:53 +03:00
smb_my_netbios_names = SMB_MALLOC_ARRAY ( char * , smb_num_netbios_names ) ;
2002-11-13 02:20:50 +03:00
if ( ! smb_my_netbios_names )
return False ;
memset ( smb_my_netbios_names , ' \0 ' , sizeof ( char * ) * smb_num_netbios_names ) ;
return True ;
}
2007-10-19 04:40:25 +04:00
static bool set_my_netbios_names ( const char * name , int i )
2002-11-13 02:20:50 +03:00
{
SAFE_FREE ( smb_my_netbios_names [ i ] ) ;
2004-12-07 21:25:53 +03:00
smb_my_netbios_names [ i ] = SMB_STRDUP ( name ) ;
2002-11-13 02:20:50 +03:00
if ( ! smb_my_netbios_names [ i ] )
return False ;
2003-07-03 23:11:31 +04:00
strupper_m ( smb_my_netbios_names [ i ] ) ;
2002-11-13 02:20:50 +03:00
return True ;
}
2006-04-08 21:25:31 +04:00
/***********************************************************************
Free memory allocated to global objects
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void gfree_names ( void )
{
2010-05-17 10:25:26 +04:00
gfree_netbios_names ( ) ;
2006-04-08 21:25:31 +04:00
SAFE_FREE ( smb_scope ) ;
free_netbios_names_array ( ) ;
2008-01-07 16:15:48 +03:00
free_local_machine_name ( ) ;
2006-04-08 21:25:31 +04:00
}
void gfree_all ( void )
{
2007-12-07 05:49:39 +03:00
gfree_names ( ) ;
2006-04-08 21:25:31 +04:00
gfree_loadparm ( ) ;
gfree_case_tables ( ) ;
gfree_charcnv ( ) ;
2007-03-16 17:13:46 +03:00
gfree_interfaces ( ) ;
2008-08-13 00:35:15 +04:00
gfree_debugsyms ( ) ;
2006-04-08 21:25:31 +04:00
}
2002-11-13 02:20:50 +03:00
const char * my_netbios_names ( int i )
{
return smb_my_netbios_names [ i ] ;
}
2007-10-19 04:40:25 +04:00
bool set_netbios_aliases ( const char * * str_array )
2002-11-13 02:20:50 +03:00
{
size_t namecount ;
/* Work out the max number of netbios aliases that we have */
for ( namecount = 0 ; str_array & & ( str_array [ namecount ] ! = NULL ) ; namecount + + )
;
if ( global_myname ( ) & & * global_myname ( ) )
namecount + + ;
/* Allocate space for the netbios aliases */
if ( ! allocate_my_netbios_names_array ( namecount ) )
return False ;
/* Use the global_myname string first */
namecount = 0 ;
if ( global_myname ( ) & & * global_myname ( ) ) {
set_my_netbios_names ( global_myname ( ) , namecount ) ;
namecount + + ;
}
if ( str_array ) {
size_t i ;
for ( i = 0 ; str_array [ i ] ! = NULL ; i + + ) {
size_t n ;
2007-10-19 04:40:25 +04:00
bool duplicate = False ;
2002-11-13 02:20:50 +03:00
/* Look for duplicates */
for ( n = 0 ; n < namecount ; n + + ) {
if ( strequal ( str_array [ i ] , my_netbios_names ( n ) ) ) {
duplicate = True ;
break ;
}
}
if ( ! duplicate ) {
if ( ! set_my_netbios_names ( str_array [ i ] , namecount ) )
return False ;
namecount + + ;
}
}
}
return True ;
}
1996-05-04 11:50:46 +04:00
1996-10-24 04:09:08 +04:00
/****************************************************************************
2002-11-13 02:20:50 +03:00
Common name initialization code .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool init_names ( void )
2002-11-13 02:20:50 +03:00
{
int n ;
if ( global_myname ( ) = = NULL | | * global_myname ( ) = = ' \0 ' ) {
2003-02-24 06:09:08 +03:00
if ( ! set_global_myname ( myhostname ( ) ) ) {
2010-08-28 16:13:54 +04:00
DEBUG ( 0 , ( " init_names: malloc fail. \n " ) ) ;
2002-11-13 02:20:50 +03:00
return False ;
}
}
if ( ! set_netbios_aliases ( lp_netbios_aliases ( ) ) ) {
2010-08-28 16:13:54 +04:00
DEBUG ( 0 , ( " init_names: malloc fail. \n " ) ) ;
2002-11-13 02:20:50 +03:00
return False ;
2007-11-10 02:09:16 +03:00
}
2002-11-13 02:20:50 +03:00
2007-11-10 02:09:16 +03:00
set_local_machine_name ( global_myname ( ) , false ) ;
2002-11-13 02:20:50 +03:00
DEBUG ( 5 , ( " Netbios name list:- \n " ) ) ;
2007-11-10 02:09:16 +03:00
for ( n = 0 ; my_netbios_names ( n ) ; n + + ) {
DEBUGADD ( 5 , ( " my_netbios_names[%d]= \" %s \" \n " ,
n , my_netbios_names ( n ) ) ) ;
}
2002-11-13 02:20:50 +03:00
return ( True ) ;
}
2007-12-07 05:49:39 +03:00
/**************************************************************************n
Code to cope with username / password auth options from the commandline .
Used mainly in client tools .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-12-14 15:06:19 +03:00
struct user_auth_info * user_auth_info_init ( TALLOC_CTX * mem_ctx )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
struct user_auth_info * result ;
result = TALLOC_ZERO_P ( mem_ctx , struct user_auth_info ) ;
if ( result = = NULL ) {
return NULL ;
}
result - > signing_state = Undefined ;
return result ;
}
2009-03-17 02:38:15 +03:00
const char * get_cmdline_auth_info_username ( const struct user_auth_info * auth_info )
2008-12-14 15:06:19 +03:00
{
if ( ! auth_info - > username ) {
2007-12-07 05:49:39 +03:00
return " " ;
}
2008-12-14 15:06:19 +03:00
return auth_info - > username ;
2007-12-07 05:49:39 +03:00
}
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_username ( struct user_auth_info * auth_info ,
const char * username )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
TALLOC_FREE ( auth_info - > username ) ;
auth_info - > username = talloc_strdup ( auth_info , username ) ;
if ( ! auth_info - > username ) {
2007-12-07 05:49:39 +03:00
exit ( ENOMEM ) ;
}
}
2009-09-11 00:53:44 +04:00
const char * get_cmdline_auth_info_domain ( const struct user_auth_info * auth_info )
{
if ( ! auth_info - > domain ) {
return " " ;
}
return auth_info - > domain ;
}
void set_cmdline_auth_info_domain ( struct user_auth_info * auth_info ,
const char * domain )
{
TALLOC_FREE ( auth_info - > domain ) ;
auth_info - > domain = talloc_strdup ( auth_info , domain ) ;
if ( ! auth_info - > domain ) {
exit ( ENOMEM ) ;
}
}
2009-03-17 02:38:15 +03:00
const char * get_cmdline_auth_info_password ( const struct user_auth_info * auth_info )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
if ( ! auth_info - > password ) {
2007-12-07 05:49:39 +03:00
return " " ;
}
2008-12-14 15:06:19 +03:00
return auth_info - > password ;
2007-12-07 05:49:39 +03:00
}
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_password ( struct user_auth_info * auth_info ,
const char * password )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
TALLOC_FREE ( auth_info - > password ) ;
2009-03-18 00:53:06 +03:00
if ( password = = NULL ) {
password = " " ;
}
2008-12-14 15:06:19 +03:00
auth_info - > password = talloc_strdup ( auth_info , password ) ;
if ( ! auth_info - > password ) {
2007-12-07 05:49:39 +03:00
exit ( ENOMEM ) ;
}
2008-12-14 15:06:19 +03:00
auth_info - > got_pass = true ;
2007-12-07 05:49:39 +03:00
}
2008-12-14 15:06:19 +03:00
bool set_cmdline_auth_info_signing_state ( struct user_auth_info * auth_info ,
const char * arg )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
auth_info - > signing_state = - 1 ;
2007-12-07 05:49:39 +03:00
if ( strequal ( arg , " off " ) | | strequal ( arg , " no " ) | |
strequal ( arg , " false " ) ) {
2008-12-14 15:06:19 +03:00
auth_info - > signing_state = false ;
2007-12-07 05:49:39 +03:00
} else if ( strequal ( arg , " on " ) | | strequal ( arg , " yes " ) | |
strequal ( arg , " true " ) | | strequal ( arg , " auto " ) ) {
2008-12-14 15:06:19 +03:00
auth_info - > signing_state = true ;
2007-12-07 05:49:39 +03:00
} else if ( strequal ( arg , " force " ) | | strequal ( arg , " required " ) | |
strequal ( arg , " forced " ) ) {
2008-12-14 15:06:19 +03:00
auth_info - > signing_state = Required ;
2007-12-07 05:49:39 +03:00
} else {
return false ;
}
return true ;
}
2009-03-17 02:38:15 +03:00
int get_cmdline_auth_info_signing_state ( const struct user_auth_info * auth_info )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
return auth_info - > signing_state ;
2007-12-07 05:49:39 +03:00
}
2010-01-24 19:07:24 +03:00
void set_cmdline_auth_info_use_ccache ( struct user_auth_info * auth_info , bool b )
{
auth_info - > use_ccache = b ;
}
bool get_cmdline_auth_info_use_ccache ( const struct user_auth_info * auth_info )
{
return auth_info - > use_ccache ;
}
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_use_kerberos ( struct user_auth_info * auth_info ,
bool b )
2008-07-09 04:44:39 +04:00
{
2008-12-14 15:06:19 +03:00
auth_info - > use_kerberos = b ;
2008-07-09 04:44:39 +04:00
}
2009-03-17 02:38:15 +03:00
bool get_cmdline_auth_info_use_kerberos ( const struct user_auth_info * auth_info )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
return auth_info - > use_kerberos ;
2007-12-07 05:49:39 +03:00
}
2009-03-18 00:53:06 +03:00
void set_cmdline_auth_info_fallback_after_kerberos ( struct user_auth_info * auth_info ,
bool b )
{
auth_info - > fallback_after_kerberos = b ;
}
bool get_cmdline_auth_info_fallback_after_kerberos ( const struct user_auth_info * auth_info )
{
return auth_info - > fallback_after_kerberos ;
}
2007-12-07 05:49:39 +03:00
/* This should only be used by lib/popt_common.c JRA */
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_use_krb5_ticket ( struct user_auth_info * auth_info )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
auth_info - > use_kerberos = true ;
auth_info - > got_pass = true ;
2007-12-07 05:49:39 +03:00
}
2008-01-05 11:23:35 +03:00
/* This should only be used by lib/popt_common.c JRA */
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_smb_encrypt ( struct user_auth_info * auth_info )
2008-01-05 11:23:35 +03:00
{
2008-12-14 15:06:19 +03:00
auth_info - > smb_encrypt = true ;
2008-01-05 11:23:35 +03:00
}
2008-12-14 15:06:19 +03:00
void set_cmdline_auth_info_use_machine_account ( struct user_auth_info * auth_info )
2008-05-05 18:58:24 +04:00
{
2008-12-14 15:06:19 +03:00
auth_info - > use_machine_account = true ;
2008-05-05 18:58:24 +04:00
}
2009-03-17 02:38:15 +03:00
bool get_cmdline_auth_info_got_pass ( const struct user_auth_info * auth_info )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
return auth_info - > got_pass ;
2007-12-07 05:49:39 +03:00
}
2009-03-17 02:38:15 +03:00
bool get_cmdline_auth_info_smb_encrypt ( const struct user_auth_info * auth_info )
2008-01-05 11:23:35 +03:00
{
2008-12-14 15:06:19 +03:00
return auth_info - > smb_encrypt ;
2008-01-05 11:23:35 +03:00
}
2009-03-17 02:38:15 +03:00
bool get_cmdline_auth_info_use_machine_account ( const struct user_auth_info * auth_info )
2008-05-05 18:58:24 +04:00
{
2008-12-14 15:06:19 +03:00
return auth_info - > use_machine_account ;
2008-05-05 18:58:24 +04:00
}
2008-12-14 15:06:19 +03:00
struct user_auth_info * get_cmdline_auth_info_copy ( TALLOC_CTX * mem_ctx ,
2009-03-17 02:38:15 +03:00
const struct user_auth_info * src )
2007-12-07 05:49:39 +03:00
{
2008-12-14 15:06:19 +03:00
struct user_auth_info * result ;
result = user_auth_info_init ( mem_ctx ) ;
if ( result = = NULL ) {
return NULL ;
2007-12-07 05:49:39 +03:00
}
2008-12-14 15:06:19 +03:00
* result = * src ;
result - > username = talloc_strdup (
result , get_cmdline_auth_info_username ( src ) ) ;
result - > password = talloc_strdup (
result , get_cmdline_auth_info_password ( src ) ) ;
if ( ( result - > username = = NULL ) | | ( result - > password = = NULL ) ) {
TALLOC_FREE ( result ) ;
return NULL ;
}
return result ;
2007-12-07 05:49:39 +03:00
}
2008-12-14 15:06:19 +03:00
bool set_cmdline_auth_info_machine_account_creds ( struct user_auth_info * auth_info )
2008-05-05 18:58:24 +04:00
{
char * pass = NULL ;
char * account = NULL ;
2008-12-14 15:06:19 +03:00
if ( ! get_cmdline_auth_info_use_machine_account ( auth_info ) ) {
2008-05-05 18:58:24 +04:00
return false ;
}
if ( ! secrets_init ( ) ) {
d_printf ( " ERROR: Unable to open secrets database \n " ) ;
return false ;
}
if ( asprintf ( & account , " %s$@%s " , global_myname ( ) , lp_realm ( ) ) < 0 ) {
return false ;
}
pass = secrets_fetch_machine_password ( lp_workgroup ( ) , NULL , NULL ) ;
if ( ! pass ) {
d_printf ( " ERROR: Unable to fetch machine password for "
" %s in domain %s \n " ,
account , lp_workgroup ( ) ) ;
SAFE_FREE ( account ) ;
return false ;
}
2008-12-14 15:06:19 +03:00
set_cmdline_auth_info_username ( auth_info , account ) ;
set_cmdline_auth_info_password ( auth_info , pass ) ;
2008-05-05 18:58:24 +04:00
SAFE_FREE ( account ) ;
SAFE_FREE ( pass ) ;
return true ;
}
2009-03-18 00:53:06 +03:00
/****************************************************************************
Ensure we have a password if one not given .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void set_cmdline_auth_info_getpass ( struct user_auth_info * auth_info )
{
char * label = NULL ;
char * pass ;
TALLOC_CTX * frame ;
if ( get_cmdline_auth_info_got_pass ( auth_info ) | |
get_cmdline_auth_info_use_kerberos ( auth_info ) ) {
/* Already got one... */
return ;
}
frame = talloc_stackframe ( ) ;
label = talloc_asprintf ( frame , " Enter %s's password: " ,
get_cmdline_auth_info_username ( auth_info ) ) ;
pass = getpass ( label ) ;
if ( pass ) {
set_cmdline_auth_info_password ( auth_info , pass ) ;
}
TALLOC_FREE ( frame ) ;
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2001-11-16 00:50:29 +03:00
Check if a file exists - call vfs_file_exist for samba files .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-16 00:50:29 +03:00
2009-11-27 15:17:05 +03:00
bool file_exist_stat ( const char * fname , SMB_STRUCT_STAT * sbuf ,
bool fake_dir_create_times )
1996-05-04 11:50:46 +04:00
{
2002-11-13 02:20:50 +03:00
SMB_STRUCT_STAT st ;
2001-11-16 00:50:29 +03:00
if ( ! sbuf )
sbuf = & st ;
2009-11-21 18:17:16 +03:00
2009-11-27 15:17:05 +03:00
if ( sys_stat ( fname , sbuf , fake_dir_create_times ) ! = 0 )
2002-11-13 02:20:50 +03:00
return ( False ) ;
1996-05-04 11:50:46 +04:00
2009-05-14 17:34:42 +04:00
return ( ( S_ISREG ( sbuf - > st_ex_mode ) ) | | ( S_ISFIFO ( sbuf - > st_ex_mode ) ) ) ;
1996-05-04 11:50:46 +04:00
}
2008-01-16 12:09:48 +03:00
/*******************************************************************
Check if a unix domain socket exists - call vfs_file_exist for samba files .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool socket_exist ( const char * fname )
{
SMB_STRUCT_STAT st ;
2009-11-27 15:10:48 +03:00
if ( sys_stat ( fname , & st , false ) ! = 0 )
2008-01-16 12:09:48 +03:00
return ( False ) ;
2009-05-14 17:34:42 +04:00
return S_ISSOCK ( st . st_ex_mode ) ;
2008-01-16 12:09:48 +03:00
}
2009-01-27 02:39:40 +03:00
/*******************************************************************
Returns the size in bytes of the named given the stat struct .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
uint64_t get_file_size_stat ( const SMB_STRUCT_STAT * sbuf )
{
2009-05-14 17:34:42 +04:00
return sbuf - > st_ex_size ;
2009-01-27 02:39:40 +03:00
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2002-11-13 02:20:50 +03:00
Returns the size in bytes of the named file .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1999-12-13 16:27:58 +03:00
SMB_OFF_T get_file_size ( char * file_name )
1996-05-04 11:50:46 +04:00
{
2002-11-13 02:20:50 +03:00
SMB_STRUCT_STAT buf ;
2009-05-14 17:34:42 +04:00
buf . st_ex_size = 0 ;
2009-11-27 15:11:17 +03:00
if ( sys_stat ( file_name , & buf , false ) ! = 0 )
2002-11-13 02:20:50 +03:00
return ( SMB_OFF_T ) - 1 ;
2009-01-27 02:39:40 +03:00
return get_file_size_stat ( & buf ) ;
1996-05-04 11:50:46 +04:00
}
/*******************************************************************
2002-11-13 02:20:50 +03:00
Return a string representing an attribute for a file .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-11-09 23:33:37 +03:00
char * attrib_string ( uint16 mode )
1996-05-04 11:50:46 +04:00
{
2007-09-04 14:15:04 +04:00
fstring attrstr ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
attrstr [ 0 ] = 0 ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
if ( mode & aVOLID ) fstrcat ( attrstr , " V " ) ;
if ( mode & aDIR ) fstrcat ( attrstr , " D " ) ;
if ( mode & aARCH ) fstrcat ( attrstr , " A " ) ;
if ( mode & aHIDDEN ) fstrcat ( attrstr , " H " ) ;
if ( mode & aSYSTEM ) fstrcat ( attrstr , " S " ) ;
if ( mode & aRONLY ) fstrcat ( attrstr , " R " ) ;
1996-05-04 11:50:46 +04:00
2007-09-04 14:15:04 +04:00
return talloc_strdup ( talloc_tos ( ) , attrstr ) ;
1996-05-04 11:50:46 +04:00
}
/*******************************************************************
2002-11-13 02:20:50 +03:00
Show a smb message structure .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-11-10 21:14:16 +03:00
void show_msg ( char * buf )
1996-05-04 11:50:46 +04:00
{
1998-11-10 21:14:16 +03:00
int i ;
int bcc = 0 ;
1998-03-03 23:19:14 +03:00
2002-11-13 02:20:50 +03:00
if ( ! DEBUGLVL ( 5 ) )
return ;
2009-11-21 18:17:16 +03:00
1998-11-10 21:14:16 +03:00
DEBUG ( 5 , ( " size=%d \n smb_com=0x%x \n smb_rcls=%d \n smb_reh=%d \n smb_err=%d \n smb_flg=%d \n smb_flg2=%d \n " ,
smb_len ( buf ) ,
( int ) CVAL ( buf , smb_com ) ,
( int ) CVAL ( buf , smb_rcls ) ,
( int ) CVAL ( buf , smb_reh ) ,
( int ) SVAL ( buf , smb_err ) ,
( int ) CVAL ( buf , smb_flg ) ,
( int ) SVAL ( buf , smb_flg2 ) ) ) ;
2002-09-25 19:19:00 +04:00
DEBUGADD ( 5 , ( " smb_tid=%d \n smb_pid=%d \n smb_uid=%d \n smb_mid=%d \n " ,
1998-11-10 21:14:16 +03:00
( int ) SVAL ( buf , smb_tid ) ,
( int ) SVAL ( buf , smb_pid ) ,
( int ) SVAL ( buf , smb_uid ) ,
2002-09-25 19:19:00 +04:00
( int ) SVAL ( buf , smb_mid ) ) ) ;
DEBUGADD ( 5 , ( " smt_wct=%d \n " , ( int ) CVAL ( buf , smb_wct ) ) ) ;
1996-05-04 11:50:46 +04:00
1998-11-10 21:14:16 +03:00
for ( i = 0 ; i < ( int ) CVAL ( buf , smb_wct ) ; i + + )
2002-09-25 19:19:00 +04:00
DEBUGADD ( 5 , ( " smb_vwv[%2d]=%5d (0x%X) \n " , i ,
1998-11-10 21:14:16 +03:00
SVAL ( buf , smb_vwv + 2 * i ) , SVAL ( buf , smb_vwv + 2 * i ) ) ) ;
2009-11-21 18:17:16 +03:00
1998-11-10 21:14:16 +03:00
bcc = ( int ) SVAL ( buf , smb_vwv + 2 * ( CVAL ( buf , smb_wct ) ) ) ;
2002-09-25 19:19:00 +04:00
DEBUGADD ( 5 , ( " smb_bcc=%d \n " , bcc ) ) ;
1998-11-10 21:14:16 +03:00
2002-11-13 02:20:50 +03:00
if ( DEBUGLEVEL < 10 )
return ;
1998-11-10 21:14:16 +03:00
2002-11-13 02:20:50 +03:00
if ( DEBUGLEVEL < 50 )
bcc = MIN ( bcc , 512 ) ;
1998-11-10 21:14:16 +03:00
2007-03-28 17:34:59 +04:00
dump_data ( 10 , ( uint8 * ) smb_buf ( buf ) , bcc ) ;
1998-11-10 21:14:16 +03:00
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2007-12-27 04:12:36 +03:00
Set the length and marker of an encrypted smb packet .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2007-12-27 04:12:36 +03:00
void smb_set_enclen ( char * buf , int len , uint16 enc_ctx_num )
1996-05-04 11:50:46 +04:00
{
2002-11-13 02:20:50 +03:00
_smb_setlen ( buf , len ) ;
1996-05-04 11:50:46 +04:00
2007-10-11 00:34:30 +04:00
SCVAL ( buf , 4 , 0xFF ) ;
2007-12-27 04:12:36 +03:00
SCVAL ( buf , 5 , ' E ' ) ;
SSVAL ( buf , 6 , enc_ctx_num ) ;
1998-11-10 21:14:16 +03:00
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2007-12-27 04:12:36 +03:00
Set the length and marker of an smb packet .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2007-12-27 04:12:36 +03:00
void smb_setlen ( char * buf , int len )
1996-05-04 11:50:46 +04:00
{
2007-12-27 04:12:36 +03:00
_smb_setlen ( buf , len ) ;
SCVAL ( buf , 4 , 0xFF ) ;
SCVAL ( buf , 5 , ' S ' ) ;
SCVAL ( buf , 6 , ' M ' ) ;
SCVAL ( buf , 7 , ' B ' ) ;
2001-02-20 15:45:50 +03:00
}
/*******************************************************************
2002-11-13 02:20:50 +03:00
Setup only the byte count for a smb message .
2001-02-20 15:45:50 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2007-10-11 00:34:30 +04:00
int set_message_bcc ( char * buf , int num_bytes )
2001-02-20 15:45:50 +03:00
{
int num_words = CVAL ( buf , smb_wct ) ;
2007-11-02 19:27:04 +03:00
SSVAL ( buf , smb_vwv + num_words * SIZEOFWORD , num_bytes ) ;
2007-12-27 04:12:36 +03:00
_smb_setlen ( buf , smb_size + num_words * 2 + num_bytes - 4 ) ;
2001-09-17 08:23:48 +04:00
return ( smb_size + num_words * 2 + num_bytes ) ;
1996-05-04 11:50:46 +04:00
}
2007-07-23 14:52:39 +04:00
/*******************************************************************
Add a data blob to the end of a smb_buf , adjusting bcc and smb_len .
Return the bytes added
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
ssize_t message_push_blob ( uint8 * * outbuf , DATA_BLOB blob )
{
size_t newlen = smb_len ( * outbuf ) + 4 + blob . length ;
uint8 * tmp ;
if ( ! ( tmp = TALLOC_REALLOC_ARRAY ( NULL , * outbuf , uint8 , newlen ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return - 1 ;
}
* outbuf = tmp ;
memcpy ( tmp + smb_len ( tmp ) + 4 , blob . data , blob . length ) ;
2007-10-11 00:34:30 +04:00
set_message_bcc ( ( char * ) tmp , smb_buflen ( tmp ) + blob . length ) ;
2007-07-23 14:52:39 +04:00
return blob . length ;
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2002-11-13 02:20:50 +03:00
Reduce a file name , removing . . elements .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2007-11-16 01:19:52 +03:00
static char * dos_clean_name ( TALLOC_CTX * ctx , const char * s )
1996-05-04 11:50:46 +04:00
{
2007-11-16 01:19:52 +03:00
char * p = NULL ;
char * str = NULL ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
DEBUG ( 3 , ( " dos_clean_name [%s] \n " , s ) ) ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
/* remove any double slashes */
2007-11-16 01:19:52 +03:00
str = talloc_all_string_sub ( ctx , s , " \\ \\ " , " \\ " ) ;
if ( ! str ) {
return NULL ;
}
1996-05-04 11:50:46 +04:00
2007-03-09 02:54:57 +03:00
/* Remove leading .\\ characters */
2007-11-16 01:19:52 +03:00
if ( strncmp ( str , " . \\ " , 2 ) = = 0 ) {
trim_string ( str , " . \\ " , NULL ) ;
if ( * str = = 0 ) {
str = talloc_strdup ( ctx , " . \\ " ) ;
if ( ! str ) {
return NULL ;
}
}
2007-03-09 02:54:57 +03:00
}
2007-11-16 01:19:52 +03:00
while ( ( p = strstr_m ( str , " \\ .. \\ " ) ) ! = NULL ) {
char * s1 ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
* p = 0 ;
2007-11-16 01:19:52 +03:00
s1 = p + 3 ;
1996-05-04 11:50:46 +04:00
2007-11-16 01:19:52 +03:00
if ( ( p = strrchr_m ( str , ' \\ ' ) ) ! = NULL ) {
2002-11-13 02:20:50 +03:00
* p = 0 ;
2007-11-16 01:19:52 +03:00
} else {
* str = 0 ;
}
str = talloc_asprintf ( ctx ,
" %s%s " ,
str ,
s1 ) ;
if ( ! str ) {
return NULL ;
}
}
1996-05-04 11:50:46 +04:00
2007-11-16 01:19:52 +03:00
trim_string ( str , NULL , " \\ .. " ) ;
return talloc_all_string_sub ( ctx , str , " \\ . \\ " , " \\ " ) ;
1996-05-04 11:50:46 +04:00
}
/*******************************************************************
2007-11-16 01:19:52 +03:00
Reduce a file name , removing . . elements .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2007-11-16 01:19:52 +03:00
char * unix_clean_name ( TALLOC_CTX * ctx , const char * s )
1996-05-04 11:50:46 +04:00
{
2007-11-16 01:19:52 +03:00
char * p = NULL ;
char * str = NULL ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
DEBUG ( 3 , ( " unix_clean_name [%s] \n " , s ) ) ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
/* remove any double slashes */
2007-11-16 01:19:52 +03:00
str = talloc_all_string_sub ( ctx , s , " // " , " / " ) ;
if ( ! str ) {
return NULL ;
}
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
/* Remove leading ./ characters */
2007-11-16 01:19:52 +03:00
if ( strncmp ( str , " ./ " , 2 ) = = 0 ) {
trim_string ( str , " ./ " , NULL ) ;
if ( * str = = 0 ) {
str = talloc_strdup ( ctx , " ./ " ) ;
if ( ! str ) {
return NULL ;
}
}
2002-11-13 02:20:50 +03:00
}
1997-02-10 23:03:56 +03:00
2007-11-16 01:19:52 +03:00
while ( ( p = strstr_m ( str , " /../ " ) ) ! = NULL ) {
char * s1 ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
* p = 0 ;
2007-11-16 01:19:52 +03:00
s1 = p + 3 ;
1996-05-04 11:50:46 +04:00
2007-11-16 01:19:52 +03:00
if ( ( p = strrchr_m ( str , ' / ' ) ) ! = NULL ) {
2002-11-13 02:20:50 +03:00
* p = 0 ;
2007-11-16 01:19:52 +03:00
} else {
* str = 0 ;
}
str = talloc_asprintf ( ctx ,
" %s%s " ,
str ,
s1 ) ;
if ( ! str ) {
return NULL ;
}
}
1996-05-04 11:50:46 +04:00
2007-11-16 01:19:52 +03:00
trim_string ( str , NULL , " /.. " ) ;
return talloc_all_string_sub ( ctx , str , " /./ " , " / " ) ;
2007-03-09 02:54:57 +03:00
}
2007-11-16 01:19:52 +03:00
char * clean_name ( TALLOC_CTX * ctx , const char * s )
2007-03-09 02:54:57 +03:00
{
2007-11-16 01:19:52 +03:00
char * str = dos_clean_name ( ctx , s ) ;
if ( ! str ) {
return NULL ;
}
return unix_clean_name ( ctx , str ) ;
}
2005-05-18 22:02:15 +04:00
/*******************************************************************
Write data into an fd at a given offset . Ignore seek errors .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
ssize_t write_data_at_offset ( int fd , const char * buffer , size_t N , SMB_OFF_T pos )
{
size_t total = 0 ;
ssize_t ret ;
if ( pos = = ( SMB_OFF_T ) - 1 ) {
return write_data ( fd , buffer , N ) ;
}
# if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
while ( total < N ) {
ret = sys_pwrite ( fd , buffer + total , N - total , pos ) ;
if ( ret = = - 1 & & errno = = ESPIPE ) {
return write_data ( fd , buffer + total , N - total ) ;
}
if ( ret = = - 1 ) {
DEBUG ( 0 , ( " write_data_at_offset: write failure. Error = %s \n " , strerror ( errno ) ) ) ;
return - 1 ;
}
if ( ret = = 0 ) {
return total ;
}
total + = ret ;
pos + = ret ;
}
return ( ssize_t ) total ;
# else
/* Use lseek and write_data. */
if ( sys_lseek ( fd , pos , SEEK_SET ) = = - 1 ) {
if ( errno ! = ESPIPE ) {
return - 1 ;
}
}
return write_data ( fd , buffer , N ) ;
# endif
}
1996-05-04 11:50:46 +04:00
2009-05-06 01:18:50 +04:00
NTSTATUS reinit_after_fork ( struct messaging_context * msg_ctx ,
2010-07-04 18:18:12 +04:00
struct event_context * ev_ctx ,
2010-07-04 18:28:13 +04:00
struct server_id id ,
2010-07-04 18:18:12 +04:00
bool parent_longlived )
2008-04-15 12:36:15 +04:00
{
2009-05-06 01:18:50 +04:00
NTSTATUS status = NT_STATUS_OK ;
2008-04-15 12:36:15 +04:00
/* Reset the state of the random
* number generation system , so
* children do not get the same random
* numbers as each other */
set_need_random_reseed ( ) ;
/* tdb needs special fork handling */
2008-04-23 19:13:50 +04:00
if ( tdb_reopen_all ( parent_longlived ? 1 : 0 ) = = - 1 ) {
2008-04-15 12:36:15 +04:00
DEBUG ( 0 , ( " tdb_reopen_all failed. \n " ) ) ;
2009-05-06 01:18:50 +04:00
status = NT_STATUS_OPEN_FAILED ;
goto done ;
2008-04-15 12:36:15 +04:00
}
2010-03-26 14:07:50 +03:00
if ( ev_ctx & & tevent_re_initialise ( ev_ctx ) ! = 0 ) {
smb_panic ( __location__ " : Failed to re-initialise event context " ) ;
2009-01-14 14:21:43 +03:00
}
2009-01-16 02:52:41 +03:00
if ( msg_ctx ) {
/*
* For clustering , we need to re - init our ctdbd connection after the
* fork
*/
2010-07-04 18:28:13 +04:00
status = messaging_reinit ( msg_ctx , id ) ;
2009-01-16 02:52:41 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " messaging_reinit() failed: %s \n " ,
nt_errstr ( status ) ) ) ;
}
2008-04-15 12:36:15 +04:00
}
2009-05-06 01:18:50 +04:00
done :
return status ;
2008-04-15 12:36:15 +04:00
}
2004-12-07 21:25:53 +03:00
# if defined(PARANOID_MALLOC_CHECKER)
/****************************************************************************
Internal malloc wrapper . Externally visible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void * malloc_ ( size_t size )
{
2007-04-28 18:33:46 +04:00
if ( size = = 0 ) {
return NULL ;
}
2004-12-07 21:25:53 +03:00
# undef malloc
2004-12-10 00:42:00 +03:00
return malloc ( size ) ;
2004-12-07 21:25:53 +03:00
# define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
}
/****************************************************************************
Internal calloc wrapper . Not externally visible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void * calloc_ ( size_t count , size_t size )
{
2007-04-28 18:33:46 +04:00
if ( size = = 0 | | count = = 0 ) {
return NULL ;
}
2004-12-07 21:25:53 +03:00
# undef calloc
2004-12-10 00:42:00 +03:00
return calloc ( count , size ) ;
2004-12-07 21:25:53 +03:00
# define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
}
/****************************************************************************
Internal realloc wrapper . Not externally visible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void * realloc_ ( void * ptr , size_t size )
{
# undef realloc
2004-12-10 00:42:00 +03:00
return realloc ( ptr , size ) ;
2004-12-07 21:25:53 +03:00
# define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
}
# endif /* PARANOID_MALLOC_CHECKER */
2007-02-24 15:40:43 +03:00
/****************************************************************************
Type - safe memalign
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void * memalign_array ( size_t el_size , size_t align , unsigned int count )
{
if ( count > = MAX_ALLOC_SIZE / el_size ) {
return NULL ;
}
return sys_memalign ( align , el_size * count ) ;
}
2004-12-07 21:25:53 +03:00
/****************************************************************************
Type - safe calloc .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void * calloc_array ( size_t size , size_t nmemb )
{
if ( nmemb > = MAX_ALLOC_SIZE / size ) {
return NULL ;
}
2007-04-28 18:33:46 +04:00
if ( size = = 0 | | nmemb = = 0 ) {
return NULL ;
}
2004-12-07 21:25:53 +03:00
# if defined(PARANOID_MALLOC_CHECKER)
return calloc_ ( nmemb , size ) ;
# else
return calloc ( nmemb , size ) ;
# endif
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2001-11-16 00:50:29 +03:00
Expand a pointer to be a particular size .
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
Note that this version of Realloc has an extra parameter that decides
whether to free the passed in storage on allocation failure or if the
new size is zero .
This is designed for use in the typical idiom of :
p = SMB_REALLOC ( p , size )
if ( ! p ) {
return error ;
}
and not to have to keep track of the old ' p ' contents to free later , nor
to worry if the size parameter was zero . In the case where NULL is returned
we guarentee that p has been freed .
If free later semantics are desired , then pass ' free_old_on_error ' as False which
guarentees that the old contents are not freed on error , even if size = = 0. To use
this idiom use :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR ( p , size ) ;
if ( ! tmp ) {
SAFE_FREE ( p ) ;
return error ;
} else {
p = tmp ;
}
Changes were instigated by Coverity error checking . JRA .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-16 00:50:29 +03:00
2007-10-19 04:40:25 +04:00
void * Realloc ( void * p , size_t size , bool free_old_on_error )
1996-05-04 11:50:46 +04:00
{
2002-11-13 02:20:50 +03:00
void * ret = NULL ;
1996-05-31 19:13:29 +04:00
2002-11-13 02:20:50 +03:00
if ( size = = 0 ) {
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( free_old_on_error ) {
SAFE_FREE ( p ) ;
}
DEBUG ( 2 , ( " Realloc asked for 0 bytes \n " ) ) ;
2002-11-13 02:20:50 +03:00
return NULL ;
}
1996-05-31 19:13:29 +04:00
2004-12-07 21:25:53 +03:00
# if defined(PARANOID_MALLOC_CHECKER)
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! p ) {
2004-12-07 21:25:53 +03:00
ret = ( void * ) malloc_ ( size ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
} else {
2004-12-07 21:25:53 +03:00
ret = ( void * ) realloc_ ( p , size ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2004-12-07 21:25:53 +03:00
# else
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! p ) {
2002-11-13 02:20:50 +03:00
ret = ( void * ) malloc ( size ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
} else {
2002-11-13 02:20:50 +03:00
ret = ( void * ) realloc ( p , size ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2004-12-07 21:25:53 +03:00
# endif
1996-05-04 11:50:46 +04:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! ret ) {
if ( free_old_on_error & & p ) {
SAFE_FREE ( p ) ;
}
2002-11-13 02:20:50 +03:00
DEBUG ( 0 , ( " Memory allocation error: failed to expand to %d bytes \n " , ( int ) size ) ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
return ( ret ) ;
1996-05-04 11:50:46 +04:00
}
2005-04-15 17:41:49 +04:00
/****************************************************************************
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
( Hopefully ) efficient array append .
2005-04-15 17:41:49 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
2005-04-15 17:41:49 +04:00
void add_to_large_array ( TALLOC_CTX * mem_ctx , size_t element_size ,
2006-07-11 22:01:26 +04:00
void * element , void * _array , uint32 * num_elements ,
2005-04-15 17:41:49 +04:00
ssize_t * array_size )
{
2006-07-11 22:01:26 +04:00
void * * array = ( void * * ) _array ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( * array_size < 0 ) {
2005-04-15 17:41:49 +04:00
return ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2005-04-15 17:41:49 +04:00
if ( * array = = NULL ) {
2005-04-23 22:07:01 +04:00
if ( * array_size = = 0 ) {
2005-04-15 17:41:49 +04:00
* array_size = 128 ;
2005-04-23 22:07:01 +04:00
}
if ( * array_size > = MAX_ALLOC_SIZE / element_size ) {
goto error ;
}
2005-04-15 17:41:49 +04:00
2006-12-09 05:58:18 +03:00
* array = TALLOC ( mem_ctx , element_size * ( * array_size ) ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( * array = = NULL ) {
2005-04-15 17:41:49 +04:00
goto error ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2005-04-15 17:41:49 +04:00
}
if ( * num_elements = = * array_size ) {
* array_size * = 2 ;
2005-04-23 22:07:01 +04:00
if ( * array_size > = MAX_ALLOC_SIZE / element_size ) {
goto error ;
}
2006-12-09 05:58:18 +03:00
* array = TALLOC_REALLOC ( mem_ctx , * array ,
element_size * ( * array_size ) ) ;
2005-04-15 17:41:49 +04:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( * array = = NULL ) {
2005-04-15 17:41:49 +04:00
goto error ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2005-04-15 17:41:49 +04:00
}
memcpy ( ( char * ) ( * array ) + element_size * ( * num_elements ) ,
element , element_size ) ;
* num_elements + = 1 ;
return ;
error :
* num_elements = 0 ;
* array_size = - 1 ;
}
2002-12-03 22:36:53 +03:00
/****************************************************************************
2007-11-09 05:01:00 +03:00
Get my own domain name , or " " if we have none .
2002-12-03 22:36:53 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-09 04:25:45 +03:00
char * get_mydnsdomname ( TALLOC_CTX * ctx )
2002-12-03 22:36:53 +03:00
{
2007-11-09 04:25:45 +03:00
const char * domname ;
2002-12-03 22:36:53 +03:00
char * p ;
2007-11-09 04:25:45 +03:00
domname = get_mydnsfullname ( ) ;
if ( ! domname ) {
return NULL ;
}
2004-01-30 21:38:48 +03:00
p = strchr_m ( domname , ' . ' ) ;
2003-04-22 11:28:41 +04:00
if ( p ) {
p + + ;
2007-11-09 05:01:00 +03:00
return talloc_strdup ( ctx , p ) ;
2007-11-09 04:58:55 +03:00
} else {
2007-11-09 05:01:00 +03:00
return talloc_strdup ( ctx , " " ) ;
2003-04-22 11:28:41 +04:00
}
2002-12-03 22:36:53 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2001-11-16 00:50:29 +03:00
Interpret a protocol description string , with a default .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-16 00:50:29 +03:00
2003-04-21 17:05:51 +04:00
int interpret_protocol ( const char * str , int def )
1996-05-04 11:50:46 +04:00
{
2002-11-13 02:20:50 +03:00
if ( strequal ( str , " NT1 " ) )
return ( PROTOCOL_NT1 ) ;
if ( strequal ( str , " LANMAN2 " ) )
return ( PROTOCOL_LANMAN2 ) ;
if ( strequal ( str , " LANMAN1 " ) )
return ( PROTOCOL_LANMAN1 ) ;
if ( strequal ( str , " CORE " ) )
return ( PROTOCOL_CORE ) ;
if ( strequal ( str , " COREPLUS " ) )
return ( PROTOCOL_COREPLUS ) ;
if ( strequal ( str , " CORE+ " ) )
return ( PROTOCOL_COREPLUS ) ;
2009-11-21 18:17:16 +03:00
2002-11-13 02:20:50 +03:00
DEBUG ( 0 , ( " Unrecognised protocol level %s \n " , str ) ) ;
2009-11-21 18:17:16 +03:00
2002-11-13 02:20:50 +03:00
return ( def ) ;
1996-05-04 11:50:46 +04:00
}
1996-10-05 14:41:13 +04:00
1998-07-29 07:08:05 +04:00
# if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
1998-07-16 04:06:29 +04:00
/******************************************************************
Remove any mount options such as - rsize = 2048 , wsize = 2048 etc .
Based on a fix from < Thomas . Hepper @ icem . de > .
2007-11-16 01:19:52 +03:00
Returns a malloc ' ed string .
1998-07-16 04:06:29 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-12-05 04:48:38 +03:00
static char * strip_mount_options ( TALLOC_CTX * ctx , const char * str )
1998-07-16 04:06:29 +04:00
{
2007-11-16 01:19:52 +03:00
if ( * str = = ' - ' ) {
2007-11-27 20:09:50 +03:00
const char * p = str ;
2002-11-13 02:20:50 +03:00
while ( * p & & ! isspace ( * p ) )
p + + ;
while ( * p & & isspace ( * p ) )
p + + ;
if ( * p ) {
2007-12-05 04:48:38 +03:00
return talloc_strdup ( ctx , p ) ;
2002-11-13 02:20:50 +03:00
}
}
2007-11-16 01:19:52 +03:00
return NULL ;
1998-07-16 04:06:29 +04:00
}
1998-02-20 22:48:01 +03:00
/*******************************************************************
Patch from jkf @ soton . ac . uk
Split Luke ' s automount_server into YP lookup and string splitter
2007-12-05 04:48:38 +03:00
so can easily implement automount_path ( ) .
Returns a malloc ' ed string .
1998-02-20 22:48:01 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-08-08 05:15:14 +04:00
# ifdef WITH_NISPLUS_HOME
2007-12-05 04:48:38 +03:00
char * automount_lookup ( TALLOC_CTX * ctx , const char * user_name )
1998-04-10 00:48:48 +04:00
{
2007-12-05 04:48:38 +03:00
char * value = NULL ;
2002-11-13 02:20:50 +03:00
char * nis_map = ( char * ) lp_nis_home_map_name ( ) ;
2007-12-05 04:48:38 +03:00
2002-11-13 02:20:50 +03:00
char buffer [ NIS_MAXATTRVAL + 1 ] ;
nis_result * result ;
nis_object * object ;
entry_obj * entry ;
2007-12-05 04:48:38 +03:00
snprintf ( buffer , sizeof ( buffer ) , " [key=%s],%s " , user_name , nis_map ) ;
DEBUG ( 5 , ( " NIS+ querystring: %s \n " , buffer ) ) ;
if ( result = nis_list ( buffer , FOLLOW_PATH | EXPAND_NAME | HARD_LOOKUP , NULL , NULL ) ) {
if ( result - > status ! = NIS_SUCCESS ) {
DEBUG ( 3 , ( " NIS+ query failed: %s \n " , nis_sperrno ( result - > status ) ) ) ;
} else {
object = result - > objects . objects_val ;
if ( object - > zo_data . zo_type = = ENTRY_OBJ ) {
entry = & object - > zo_data . objdata_u . en_data ;
DEBUG ( 5 , ( " NIS+ entry type: %s \n " , entry - > en_type ) ) ;
DEBUG ( 3 , ( " NIS+ result: %s \n " , entry - > en_cols . en_cols_val [ 1 ] . ec_value . ec_value_val ) ) ;
value = talloc_strdup ( ctx ,
entry - > en_cols . en_cols_val [ 1 ] . ec_value . ec_value_val ) ;
if ( ! value ) {
nis_freeresult ( result ) ;
return NULL ;
2002-11-13 02:20:50 +03:00
}
2007-12-05 04:48:38 +03:00
value = talloc_string_sub ( ctx ,
value ,
" & " ,
user_name ) ;
2002-11-13 02:20:50 +03:00
}
}
}
2007-12-05 04:48:38 +03:00
nis_freeresult ( result ) ;
1998-07-16 04:06:29 +04:00
2007-12-05 04:48:38 +03:00
if ( value ) {
value = strip_mount_options ( ctx , value ) ;
DEBUG ( 4 , ( " NIS+ Lookup: %s resulted in %s \n " ,
user_name , value ) ) ;
2007-11-16 01:19:52 +03:00
}
2007-12-05 04:48:38 +03:00
return value ;
1998-04-10 00:48:48 +04:00
}
1998-08-08 05:15:14 +04:00
# else /* WITH_NISPLUS_HOME */
2002-11-13 02:20:50 +03:00
2007-12-05 04:48:38 +03:00
char * automount_lookup ( TALLOC_CTX * ctx , const char * user_name )
1998-02-20 22:48:01 +03:00
{
2007-12-05 04:48:38 +03:00
char * value = NULL ;
1998-02-20 22:48:01 +03:00
2002-11-13 02:20:50 +03:00
int nis_error ; /* returned by yp all functions */
char * nis_result ; /* yp_match inits this */
int nis_result_len ; /* and set this */
char * nis_domain ; /* yp_get_default_domain inits this */
char * nis_map = ( char * ) lp_nis_home_map_name ( ) ;
if ( ( nis_error = yp_get_default_domain ( & nis_domain ) ) ! = 0 ) {
DEBUG ( 3 , ( " YP Error: %s \n " , yperr_string ( nis_error ) ) ) ;
2007-12-05 20:52:35 +03:00
return NULL ;
2002-11-13 02:20:50 +03:00
}
DEBUG ( 5 , ( " NIS Domain: %s \n " , nis_domain ) ) ;
2007-12-05 04:48:38 +03:00
if ( ( nis_error = yp_match ( nis_domain , nis_map , user_name ,
strlen ( user_name ) , & nis_result ,
& nis_result_len ) ) = = 0 ) {
2010-06-22 23:19:05 +04:00
if ( nis_result_len > 0 & & nis_result [ nis_result_len ] = = ' \n ' ) {
nis_result [ nis_result_len ] = ' \0 ' ;
}
2007-12-05 04:48:38 +03:00
value = talloc_strdup ( ctx , nis_result ) ;
if ( ! value ) {
return NULL ;
2002-11-13 02:20:50 +03:00
}
2007-12-05 04:48:38 +03:00
value = strip_mount_options ( ctx , value ) ;
} else if ( nis_error = = YPERR_KEY ) {
DEBUG ( 3 , ( " YP Key not found: while looking up \" %s \" in map \" %s \" \n " ,
user_name , nis_map ) ) ;
DEBUG ( 3 , ( " using defaults for server and home directory \n " ) ) ;
} else {
DEBUG ( 3 , ( " YP Error: \" %s \" while looking up \" %s \" in map \" %s \" \n " ,
yperr_string ( nis_error ) , user_name , nis_map ) ) ;
2002-11-13 02:20:50 +03:00
}
1998-07-16 04:06:29 +04:00
2007-12-05 04:48:38 +03:00
if ( value ) {
DEBUG ( 4 , ( " YP Lookup: %s resulted in %s \n " , user_name , value ) ) ;
}
return value ;
1998-02-20 22:48:01 +03:00
}
1998-08-08 05:15:14 +04:00
# endif /* WITH_NISPLUS_HOME */
1998-02-20 22:48:01 +03:00
# endif
1996-05-04 11:50:46 +04:00
/****************************************************************************
2002-11-13 02:20:50 +03:00
Check if a process exists . Does this work on all unixes ?
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-30 00:24:17 +04:00
2007-10-19 04:40:25 +04:00
bool process_exists ( const struct server_id pid )
1996-05-04 11:50:46 +04:00
{
2006-09-05 11:43:49 +04:00
if ( procid_is_me ( & pid ) ) {
return True ;
}
2007-06-10 21:02:09 +04:00
if ( procid_is_local ( & pid ) ) {
return ( kill ( pid . pid , 0 ) = = 0 | | errno ! = ESRCH ) ;
2005-09-30 21:13:37 +04:00
}
2007-06-10 21:02:09 +04:00
# ifdef CLUSTER_SUPPORT
2010-08-31 18:52:56 +04:00
return ctdbd_process_exists ( messaging_ctdbd_connection ( ) ,
2010-07-04 18:54:04 +04:00
pid . vnn , pid . pid ) ;
2007-06-10 21:02:09 +04:00
# else
return False ;
# endif
2005-09-30 21:13:37 +04:00
}
1999-12-13 16:27:58 +03:00
/*******************************************************************
2000-10-13 05:59:14 +04:00
Convert a uid into a user name .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-10-13 05:59:14 +04:00
2002-07-15 14:35:28 +04:00
const char * uidtoname ( uid_t uid )
1998-12-03 20:41:14 +03:00
{
2007-09-13 01:48:20 +04:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * name = NULL ;
struct passwd * pass = NULL ;
2000-10-13 05:59:14 +04:00
2007-09-13 01:48:20 +04:00
pass = getpwuid_alloc ( ctx , uid ) ;
2002-07-15 14:35:28 +04:00
if ( pass ) {
2007-09-13 01:48:20 +04:00
name = talloc_strdup ( ctx , pass - > pw_name ) ;
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( pass ) ;
2002-07-15 14:35:28 +04:00
} else {
2007-09-13 01:48:20 +04:00
name = talloc_asprintf ( ctx ,
" %ld " ,
( long int ) uid ) ;
2002-07-15 14:35:28 +04:00
}
2007-09-13 01:48:20 +04:00
return name ;
1999-12-13 16:27:58 +03:00
}
1998-12-03 20:41:14 +03:00
1996-05-04 11:50:46 +04:00
/*******************************************************************
2000-10-13 05:59:14 +04:00
Convert a gid into a group name .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-30 00:24:17 +04:00
char * gidtoname ( gid_t gid )
1996-05-04 11:50:46 +04:00
{
2000-10-13 05:59:14 +04:00
struct group * grp ;
grp = getgrgid ( gid ) ;
2007-09-04 14:15:04 +04:00
if ( grp ) {
2007-11-09 06:00:10 +03:00
return talloc_strdup ( talloc_tos ( ) , grp - > gr_name ) ;
2007-09-04 14:15:04 +04:00
}
else {
2007-11-09 06:00:10 +03:00
return talloc_asprintf ( talloc_tos ( ) ,
" %d " ,
( int ) gid ) ;
2007-09-04 14:15:04 +04:00
}
1996-05-04 11:50:46 +04:00
}
1998-11-29 23:03:33 +03:00
/*******************************************************************
2007-11-09 06:00:10 +03:00
Convert a user name into a uid .
1998-11-29 23:03:33 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-10-13 05:59:14 +04:00
2003-07-10 18:12:37 +04:00
uid_t nametouid ( const char * name )
1998-11-29 23:03:33 +03:00
{
1999-12-13 16:27:58 +03:00
struct passwd * pass ;
char * p ;
uid_t u ;
2010-10-20 19:16:23 +04:00
pass = Get_Pwnam_alloc ( talloc_tos ( ) , name ) ;
2002-01-23 15:59:24 +03:00
if ( pass ) {
2002-07-15 14:35:28 +04:00
u = pass - > pw_uid ;
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( pass ) ;
2002-07-15 14:35:28 +04:00
return u ;
2002-01-23 15:59:24 +03:00
}
2002-07-15 14:35:28 +04:00
u = ( uid_t ) strtol ( name , & p , 0 ) ;
if ( ( p ! = name ) & & ( * p = = ' \0 ' ) )
return u ;
1999-12-13 16:27:58 +03:00
return ( uid_t ) - 1 ;
1998-11-29 23:03:33 +03:00
}
1998-10-04 13:42:51 +04:00
/*******************************************************************
2002-07-15 14:35:28 +04:00
Convert a name to a gid_t if possible . Return - 1 if not a group .
1998-10-04 13:42:51 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-10-13 05:59:14 +04:00
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 10:35:11 +03:00
gid_t nametogid ( const char * name )
1998-10-04 13:42:51 +04:00
{
1999-12-13 16:27:58 +03:00
struct group * grp ;
char * p ;
gid_t g ;
2000-10-13 05:59:14 +04:00
g = ( gid_t ) strtol ( name , & p , 0 ) ;
2001-06-26 01:29:33 +04:00
if ( ( p ! = name ) & & ( * p = = ' \0 ' ) )
return g ;
1999-12-13 16:27:58 +03:00
2002-07-15 14:35:28 +04:00
grp = sys_getgrnam ( name ) ;
2001-06-26 01:29:33 +04:00
if ( grp )
return ( grp - > gr_gid ) ;
1999-12-13 16:27:58 +03:00
return ( gid_t ) - 1 ;
1998-10-04 13:42:51 +04:00
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2002-11-13 02:20:50 +03:00
Something really nasty happened - panic !
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2006-04-04 04:27:50 +04:00
void smb_panic ( const char * const why )
1996-05-04 11:50:46 +04:00
{
2003-03-18 04:48:11 +03:00
char * cmd ;
2002-09-25 19:19:00 +04:00
int result ;
2003-03-18 04:48:11 +03:00
# ifdef DEVELOPER
{
2003-04-21 17:05:51 +04:00
2003-03-18 04:48:11 +03:00
if ( global_clobber_region_function ) {
DEBUG ( 0 , ( " smb_panic: clobber_region() last called from [%s(%u)] \n " ,
2003-04-21 17:05:51 +04:00
global_clobber_region_function ,
global_clobber_region_line ) ) ;
2003-03-18 04:48:11 +03:00
}
}
# endif
2003-04-21 17:05:51 +04:00
2006-04-04 04:27:50 +04:00
DEBUG ( 0 , ( " PANIC (pid %llu): %s \n " ,
( unsigned long long ) sys_getpid ( ) , why ) ) ;
log_stack_trace ( ) ;
2003-03-18 04:48:11 +03:00
cmd = lp_panic_action ( ) ;
1998-08-21 15:37:40 +04:00
if ( cmd & & * cmd ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 0 , ( " smb_panic(): calling panic action [%s] \n " , cmd ) ) ;
result = system ( cmd ) ;
if ( result = = - 1 )
DEBUG ( 0 , ( " smb_panic(): fork failed in panic action: %s \n " ,
2003-04-21 17:05:51 +04:00
strerror ( errno ) ) ) ;
2002-09-25 19:19:00 +04:00
else
DEBUG ( 0 , ( " smb_panic(): action returned status %d \n " ,
2003-04-21 17:05:51 +04:00
WEXITSTATUS ( result ) ) ) ;
1998-08-21 15:37:40 +04:00
}
2003-04-15 01:49:49 +04:00
2006-04-04 04:27:50 +04:00
dump_core ( ) ;
}
/*******************************************************************
Print a backtrace of the stack to the debug log . This function
DELIBERATELY LEAKS MEMORY . The expectation is that you should
exit shortly after calling it .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-04-12 04:07:40 +04:00
# ifdef HAVE_LIBUNWIND_H
# include <libunwind.h>
# endif
# ifdef HAVE_EXECINFO_H
# include <execinfo.h>
# endif
2006-04-04 04:27:50 +04:00
# ifdef HAVE_LIBEXC_H
# include <libexc.h>
# endif
void log_stack_trace ( void )
{
2006-04-12 04:07:40 +04:00
# ifdef HAVE_LIBUNWIND
/* Try to use libunwind before any other technique since on ia64
* libunwind correctly walks the stack in more circumstances than
* backtrace .
*/
unw_cursor_t cursor ;
unw_context_t uc ;
unsigned i = 0 ;
char procname [ 256 ] ;
unw_word_t ip , sp , off ;
procname [ sizeof ( procname ) - 1 ] = ' \0 ' ;
if ( unw_getcontext ( & uc ) ! = 0 ) {
goto libunwind_failed ;
}
if ( unw_init_local ( & cursor , & uc ) ! = 0 ) {
goto libunwind_failed ;
}
DEBUG ( 0 , ( " BACKTRACE: \n " ) ) ;
do {
ip = sp = 0 ;
unw_get_reg ( & cursor , UNW_REG_IP , & ip ) ;
unw_get_reg ( & cursor , UNW_REG_SP , & sp ) ;
switch ( unw_get_proc_name ( & cursor ,
procname , sizeof ( procname ) - 1 , & off ) ) {
case 0 :
/* Name found. */
case - UNW_ENOMEM :
/* Name truncated. */
DEBUGADD ( 0 , ( " #%u %s + %#llx [ip=%#llx] [sp=%#llx] \n " ,
i , procname , ( long long ) off ,
( long long ) ip , ( long long ) sp ) ) ;
break ;
default :
/* case -UNW_ENOINFO: */
/* case -UNW_EUNSPEC: */
/* No symbol name found. */
DEBUGADD ( 0 , ( " #%u %s [ip=%#llx] [sp=%#llx] \n " ,
i , " <unknown symbol> " ,
( long long ) ip , ( long long ) sp ) ) ;
}
+ + i ;
} while ( unw_step ( & cursor ) > 0 ) ;
return ;
libunwind_failed :
DEBUG ( 0 , ( " unable to produce a stack trace with libunwind \n " ) ) ;
# elif HAVE_BACKTRACE_SYMBOLS
2006-04-04 04:27:50 +04:00
void * backtrace_stack [ BACKTRACE_STACK_SIZE ] ;
size_t backtrace_size ;
char * * backtrace_strings ;
2003-04-15 01:49:49 +04:00
/* get the backtrace (stack frames) */
backtrace_size = backtrace ( backtrace_stack , BACKTRACE_STACK_SIZE ) ;
backtrace_strings = backtrace_symbols ( backtrace_stack , backtrace_size ) ;
2003-11-03 17:34:25 +03:00
DEBUG ( 0 , ( " BACKTRACE: %lu stack frames: \n " ,
( unsigned long ) backtrace_size ) ) ;
2009-11-21 18:17:16 +03:00
2003-04-15 01:49:49 +04:00
if ( backtrace_strings ) {
2003-05-27 11:26:04 +04:00
int i ;
2003-04-15 01:49:49 +04:00
for ( i = 0 ; i < backtrace_size ; i + + )
DEBUGADD ( 0 , ( " #%u %s \n " , i , backtrace_strings [ i ] ) ) ;
2004-03-21 11:43:41 +03:00
/* Leak the backtrace_strings, rather than risk what free() might do */
2003-04-15 01:49:49 +04:00
}
2004-02-11 22:07:02 +03:00
# elif HAVE_LIBEXC
/* The IRIX libexc library provides an API for unwinding the stack. See
* libexc ( 3 ) for details . Apparantly trace_back_stack leaks memory , but
* since we are about to abort anyway , it hardly matters .
*/
2006-04-04 04:27:50 +04:00
# define NAMESIZE 32 /* Arbitrary */
2006-04-12 04:07:40 +04:00
__uint64_t addrs [ BACKTRACE_STACK_SIZE ] ;
char * names [ BACKTRACE_STACK_SIZE ] ;
char namebuf [ BACKTRACE_STACK_SIZE * NAMESIZE ] ;
2004-02-11 22:07:02 +03:00
2006-04-12 04:07:40 +04:00
int i ;
int levels ;
2004-02-11 22:07:02 +03:00
2006-04-12 04:07:40 +04:00
ZERO_ARRAY ( addrs ) ;
ZERO_ARRAY ( names ) ;
ZERO_ARRAY ( namebuf ) ;
2004-02-11 22:07:02 +03:00
2006-04-12 04:07:40 +04:00
/* We need to be root so we can open our /proc entry to walk
* our stack . It also helps when we want to dump core .
*/
become_root ( ) ;
2005-01-29 00:55:45 +03:00
2006-04-12 04:07:40 +04:00
for ( i = 0 ; i < BACKTRACE_STACK_SIZE ; i + + ) {
names [ i ] = namebuf + ( i * NAMESIZE ) ;
}
2004-02-11 22:07:02 +03:00
2006-04-12 04:07:40 +04:00
levels = trace_back_stack ( 0 , addrs , names ,
BACKTRACE_STACK_SIZE , NAMESIZE - 1 ) ;
2004-02-11 22:07:02 +03:00
2006-04-12 04:07:40 +04:00
DEBUG ( 0 , ( " BACKTRACE: %d stack frames: \n " , levels ) ) ;
for ( i = 0 ; i < levels ; i + + ) {
DEBUGADD ( 0 , ( " #%d 0x%llx %s \n " , i , addrs [ i ] , names [ i ] ) ) ;
}
2004-02-11 22:07:02 +03:00
# undef NAMESIZE
2006-04-12 04:07:40 +04:00
2006-04-04 04:27:50 +04:00
# else
DEBUG ( 0 , ( " unable to produce a stack trace on this platform \n " ) ) ;
2003-04-15 01:49:49 +04:00
# endif
2006-03-28 19:50:13 +04:00
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
2003-04-21 17:05:51 +04:00
A readdir wrapper which just returns the file name .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
2005-08-23 23:05:39 +04:00
const char * readdirname ( SMB_STRUCT_DIR * p )
1996-05-04 11:50:46 +04:00
{
1999-12-13 16:27:58 +03:00
SMB_STRUCT_DIRENT * ptr ;
1998-07-29 07:08:05 +04:00
char * dname ;
1996-05-04 11:50:46 +04:00
2002-11-13 02:20:50 +03:00
if ( ! p )
return ( NULL ) ;
2009-11-21 18:17:16 +03:00
1999-12-13 16:27:58 +03:00
ptr = ( SMB_STRUCT_DIRENT * ) sys_readdir ( p ) ;
2002-11-13 02:20:50 +03:00
if ( ! ptr )
return ( NULL ) ;
1996-05-04 11:50:46 +04:00
1998-07-29 07:08:05 +04:00
dname = ptr - > d_name ;
1996-05-04 11:50:46 +04:00
# ifdef NEXT2
2002-11-13 02:20:50 +03:00
if ( telldir ( p ) < 0 )
return ( NULL ) ;
1996-05-04 11:50:46 +04:00
# endif
2006-03-27 23:50:45 +04:00
# ifdef HAVE_BROKEN_READDIR_NAME
1998-07-29 07:08:05 +04:00
/* using /usr/ucb/cc is BAD */
dname = dname - 2 ;
1996-05-04 11:50:46 +04:00
# endif
2007-09-04 14:15:04 +04:00
return talloc_strdup ( talloc_tos ( ) , dname ) ;
1996-05-04 11:50:46 +04:00
}
1997-09-30 06:38:19 +04:00
/*******************************************************************
Utility function used to decide if the last component
of a path matches a ( possibly wildcarded ) entry in a namelist .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1996-05-04 11:50:46 +04:00
2009-11-23 18:33:53 +03:00
bool is_in_path ( const char * name , name_compare_entry * namelist , bool case_sensitive )
1996-12-10 21:03:44 +03:00
{
2007-07-12 04:42:09 +04:00
const char * last_component ;
2002-11-13 02:20:50 +03:00
/* if we have no list it's obviously not in the path */
if ( ( namelist = = NULL ) | | ( ( namelist ! = NULL ) & & ( namelist [ 0 ] . name = = NULL ) ) ) {
return False ;
}
2004-08-18 21:55:50 +04:00
DEBUG ( 8 , ( " is_in_path: %s \n " , name ) ) ;
2002-11-13 02:20:50 +03:00
/* Get the last component of the unix name. */
2007-07-12 04:42:09 +04:00
last_component = strrchr_m ( name , ' / ' ) ;
if ( ! last_component ) {
last_component = name ;
} else {
last_component + + ; /* Go past '/' */
}
2002-11-13 02:20:50 +03:00
for ( ; namelist - > name ! = NULL ; namelist + + ) {
if ( namelist - > is_wild ) {
2009-11-23 18:34:07 +03:00
if ( mask_match ( last_component , namelist - > name , case_sensitive ) ) {
2002-11-13 02:20:50 +03:00
DEBUG ( 8 , ( " is_in_path: mask match succeeded \n " ) ) ;
return True ;
}
} else {
if ( ( case_sensitive & & ( strcmp ( last_component , namelist - > name ) = = 0 ) ) | |
( ! case_sensitive & & ( StrCaseCmp ( last_component , namelist - > name ) = = 0 ) ) ) {
DEBUG ( 8 , ( " is_in_path: match succeeded \n " ) ) ;
return True ;
}
}
}
DEBUG ( 8 , ( " is_in_path: match not found \n " ) ) ;
return False ;
1997-07-24 21:25:11 +04:00
}
1997-07-06 17:48:10 +04:00
1997-09-30 06:38:19 +04:00
/*******************************************************************
Strip a ' / ' separated list into an array of
name_compare_enties structures suitable for
passing to is_in_path ( ) . We do this for
speed so we can pre - parse all the names in the list
and don ' t do it for each call to is_in_path ( ) .
namelist is modified here and is assumed to be
a copy owned by the caller .
We also check if the entry contains a wildcard to
remove a potentially expensive call to mask_match
if possible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-21 18:17:16 +03:00
2008-04-28 15:35:44 +04:00
void set_namearray ( name_compare_entry * * ppname_array , const char * namelist )
1997-07-24 21:25:11 +04:00
{
2002-11-13 02:20:50 +03:00
char * name_end ;
2009-08-25 21:46:37 +04:00
char * nameptr = ( char * ) namelist ;
2002-11-13 02:20:50 +03:00
int num_entries = 0 ;
int i ;
1997-07-24 21:25:11 +04:00
2002-11-13 02:20:50 +03:00
( * ppname_array ) = NULL ;
1997-07-24 21:25:11 +04:00
2002-11-13 02:20:50 +03:00
if ( ( nameptr = = NULL ) | | ( ( nameptr ! = NULL ) & & ( * nameptr = = ' \0 ' ) ) )
return ;
1997-07-24 21:25:11 +04:00
2002-11-13 02:20:50 +03:00
/* We need to make two passes over the string. The
first to count the number of elements , the second
to split it .
*/
1997-07-24 21:25:11 +04:00
2002-11-13 02:20:50 +03:00
while ( * nameptr ) {
if ( * nameptr = = ' / ' ) {
/* cope with multiple (useless) /s) */
nameptr + + ;
continue ;
}
2009-08-25 21:46:37 +04:00
/* anything left? */
if ( * nameptr = = ' \0 ' )
break ;
1997-07-24 21:25:11 +04:00
2009-08-25 21:46:37 +04:00
/* find the next '/' or consume remaining */
name_end = strchr_m ( nameptr , ' / ' ) ;
2002-11-13 02:20:50 +03:00
if ( name_end = = NULL )
2009-08-25 21:46:37 +04:00
name_end = ( char * ) nameptr + strlen ( nameptr ) ;
2002-11-13 02:20:50 +03:00
/* next segment please */
nameptr = name_end + 1 ;
num_entries + + ;
}
if ( num_entries = = 0 )
return ;
2004-12-07 21:25:53 +03:00
if ( ( ( * ppname_array ) = SMB_MALLOC_ARRAY ( name_compare_entry , num_entries + 1 ) ) = = NULL ) {
2002-11-13 02:20:50 +03:00
DEBUG ( 0 , ( " set_namearray: malloc fail \n " ) ) ;
return ;
}
/* Now copy out the names */
2009-08-25 21:46:37 +04:00
nameptr = ( char * ) namelist ;
2002-11-13 02:20:50 +03:00
i = 0 ;
while ( * nameptr ) {
if ( * nameptr = = ' / ' ) {
/* cope with multiple (useless) /s) */
nameptr + + ;
continue ;
}
2009-08-25 21:46:37 +04:00
/* anything left? */
if ( * nameptr = = ' \0 ' )
2002-11-13 02:20:50 +03:00
break ;
2009-08-25 21:46:37 +04:00
/* find the next '/' or consume remaining */
name_end = strchr_m ( nameptr , ' / ' ) ;
if ( name_end )
* name_end = ' \0 ' ;
else
name_end = nameptr + strlen ( nameptr ) ;
2002-11-13 02:20:50 +03:00
( * ppname_array ) [ i ] . is_wild = ms_has_wild ( nameptr ) ;
2004-12-07 21:25:53 +03:00
if ( ( ( * ppname_array ) [ i ] . name = SMB_STRDUP ( nameptr ) ) = = NULL ) {
2002-11-13 02:20:50 +03:00
DEBUG ( 0 , ( " set_namearray: malloc fail (1) \n " ) ) ;
return ;
}
/* next segment please */
nameptr = name_end + 1 ;
i + + ;
}
2009-11-21 18:17:16 +03:00
2002-11-13 02:20:50 +03:00
( * ppname_array ) [ i ] . name = NULL ;
1996-12-10 21:03:44 +03:00
2002-11-13 02:20:50 +03:00
return ;
1997-07-08 20:54:44 +04:00
}
1997-07-24 21:25:11 +04:00
/****************************************************************************
2002-11-13 02:20:50 +03:00
Routine to free a namearray .
1997-07-24 21:25:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void free_namearray ( name_compare_entry * name_array )
1997-07-08 20:54:44 +04:00
{
2003-01-24 22:19:25 +03:00
int i ;
2002-11-13 02:20:50 +03:00
if ( name_array = = NULL )
return ;
1997-07-24 21:25:11 +04:00
2003-01-24 22:19:25 +03:00
for ( i = 0 ; name_array [ i ] . name ! = NULL ; i + + )
SAFE_FREE ( name_array [ i ] . name ) ;
2002-11-13 02:20:50 +03:00
SAFE_FREE ( name_array ) ;
1997-07-08 20:54:44 +04:00
}
2005-04-27 22:32:37 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_LOCKING
2006-04-10 19:33:04 +04:00
/****************************************************************************
Simple routine to query existing file locks . Cruft in NFS and 64 - > 32 bit mapping
is dealt with in posix . c
Returns True if we have information regarding this lock region ( and returns
F_UNLCK in * ptype if the region is unlocked ) . False if the call failed .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool fcntl_getlock ( int fd , SMB_OFF_T * poffset , SMB_OFF_T * pcount , int * ptype , pid_t * ppid )
2006-04-10 19:33:04 +04:00
{
SMB_STRUCT_FLOCK lock ;
int ret ;
2006-05-05 06:06:37 +04:00
DEBUG ( 8 , ( " fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d \n " ,
fd , ( double ) * poffset , ( double ) * pcount , * ptype ) ) ;
2006-04-10 19:33:04 +04:00
lock . l_type = * ptype ;
lock . l_whence = SEEK_SET ;
lock . l_start = * poffset ;
lock . l_len = * pcount ;
lock . l_pid = 0 ;
ret = sys_fcntl_ptr ( fd , SMB_F_GETLK , & lock ) ;
if ( ret = = - 1 ) {
2006-05-08 07:28:26 +04:00
int sav = errno ;
2006-04-10 19:33:04 +04:00
DEBUG ( 3 , ( " fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s) \n " ,
( double ) * poffset , ( double ) * pcount , * ptype , strerror ( errno ) ) ) ;
2006-05-08 07:28:26 +04:00
errno = sav ;
2006-04-10 19:33:04 +04:00
return False ;
}
* ptype = lock . l_type ;
* poffset = lock . l_start ;
* pcount = lock . l_len ;
* ppid = lock . l_pid ;
2009-11-21 18:17:16 +03:00
2006-04-10 19:33:04 +04:00
DEBUG ( 3 , ( " fcntl_getlock: fd %d is returned info %d pid %u \n " ,
fd , ( int ) lock . l_type , ( unsigned int ) lock . l_pid ) ) ;
return True ;
1996-12-10 21:03:44 +03:00
}
2005-04-27 22:32:37 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_ALL
1997-07-31 22:47:26 +04:00
/*******************************************************************
2002-07-15 14:35:28 +04:00
Is the name specified one of my netbios names .
Returns true if it is equal , false otherwise .
1997-07-31 22:47:26 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2007-10-19 04:40:25 +04:00
bool is_myname ( const char * s )
1997-07-31 22:47:26 +04:00
{
2002-07-15 14:35:28 +04:00
int n ;
2007-10-19 04:40:25 +04:00
bool ret = False ;
1997-07-31 22:47:26 +04:00
2002-11-13 02:20:50 +03:00
for ( n = 0 ; my_netbios_names ( n ) ; n + + ) {
if ( strequal ( my_netbios_names ( n ) , s ) ) {
2002-07-15 14:35:28 +04:00
ret = True ;
2002-11-13 02:20:50 +03:00
break ;
}
2002-07-15 14:35:28 +04:00
}
DEBUG ( 8 , ( " is_myname( \" %s \" ) returns %d \n " , s , ret ) ) ;
return ( ret ) ;
}
2003-02-24 06:09:08 +03:00
/*******************************************************************
Is the name specified our workgroup / domain .
Returns true if it is equal , false otherwise .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool is_myworkgroup ( const char * s )
2003-02-24 06:09:08 +03:00
{
2007-10-19 04:40:25 +04:00
bool ret = False ;
2003-02-24 06:09:08 +03:00
if ( strequal ( s , lp_workgroup ( ) ) ) {
ret = True ;
}
DEBUG ( 8 , ( " is_myworkgroup( \" %s \" ) returns %d \n " , s , ret ) ) ;
return ( ret ) ;
}
2003-03-15 02:06:06 +03:00
/*******************************************************************
we distinguish between 2 K and XP by the " Native Lan Manager " string
WinXP = > " Windows 2002 5.1 "
2007-02-09 22:41:09 +03:00
WinXP 64 bit = > " Windows XP 5.2 "
2003-03-15 02:06:06 +03:00
Win2k = > " Windows 2000 5.0 "
2007-10-02 23:27:25 +04:00
NT4 = > " Windows NT 4.0 "
2003-03-15 02:06:06 +03:00
Win9x = > " Windows 4.0 "
2007-10-02 23:27:25 +04:00
Windows 2003 doesn ' t set the native lan manager string but
2003-12-06 00:51:51 +03:00
they do set the domain to " Windows 2003 5.2 " ( probably a bug ) .
2003-03-15 02:06:06 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void ra_lanman_string ( const char * native_lanman )
2007-10-02 23:27:25 +04:00
{
2003-12-06 00:51:51 +03:00
if ( strcmp ( native_lanman , " Windows 2002 5.1 " ) = = 0 )
2003-03-15 02:06:06 +03:00
set_remote_arch ( RA_WINXP ) ;
2007-02-09 22:41:09 +03:00
else if ( strcmp ( native_lanman , " Windows XP 5.2 " ) = = 0 )
2008-03-31 23:33:53 +04:00
set_remote_arch ( RA_WINXP64 ) ;
2003-12-06 00:51:51 +03:00
else if ( strcmp ( native_lanman , " Windows Server 2003 5.2 " ) = = 0 )
2003-03-15 02:06:06 +03:00
set_remote_arch ( RA_WIN2K3 ) ;
}
2007-11-10 02:09:16 +03:00
static const char * remote_arch_str ;
const char * get_remote_arch_str ( void )
{
if ( ! remote_arch_str ) {
return " UNKNOWN " ;
}
return remote_arch_str ;
}
1997-09-05 00:26:07 +04:00
/*******************************************************************
2002-07-15 14:35:28 +04:00
Set the horrid remote_arch string based on an enum .
1997-09-05 00:26:07 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
1997-09-05 00:26:07 +04:00
void set_remote_arch ( enum remote_arch_types type )
{
2002-07-15 14:35:28 +04:00
ra_type = type ;
switch ( type ) {
case RA_WFWG :
2007-11-10 02:09:16 +03:00
remote_arch_str = " WfWg " ;
2003-12-06 00:51:51 +03:00
break ;
2002-07-15 14:35:28 +04:00
case RA_OS2 :
2007-11-10 02:09:16 +03:00
remote_arch_str = " OS2 " ;
2003-12-06 00:51:51 +03:00
break ;
2002-07-15 14:35:28 +04:00
case RA_WIN95 :
2007-11-10 02:09:16 +03:00
remote_arch_str = " Win95 " ;
2003-12-06 00:51:51 +03:00
break ;
2002-07-15 14:35:28 +04:00
case RA_WINNT :
2007-11-10 02:09:16 +03:00
remote_arch_str = " WinNT " ;
2003-12-06 00:51:51 +03:00
break ;
2002-07-15 14:35:28 +04:00
case RA_WIN2K :
2007-11-10 02:09:16 +03:00
remote_arch_str = " Win2K " ;
2003-12-06 00:51:51 +03:00
break ;
2003-02-28 00:22:36 +03:00
case RA_WINXP :
2007-11-10 02:09:16 +03:00
remote_arch_str = " WinXP " ;
2003-12-06 00:51:51 +03:00
break ;
2008-03-31 23:33:53 +04:00
case RA_WINXP64 :
remote_arch_str = " WinXP64 " ;
break ;
2003-03-12 18:41:39 +03:00
case RA_WIN2K3 :
2007-11-10 02:09:16 +03:00
remote_arch_str = " Win2K3 " ;
2003-12-06 00:51:51 +03:00
break ;
2007-02-09 00:48:09 +03:00
case RA_VISTA :
2007-11-10 02:09:16 +03:00
remote_arch_str = " Vista " ;
2007-02-09 00:48:09 +03:00
break ;
2002-07-15 14:35:28 +04:00
case RA_SAMBA :
2007-11-10 02:09:16 +03:00
remote_arch_str = " Samba " ;
2003-12-06 00:51:51 +03:00
break ;
2004-06-16 03:46:58 +04:00
case RA_CIFSFS :
2007-11-10 02:09:16 +03:00
remote_arch_str = " CIFSFS " ;
2004-06-16 03:46:58 +04:00
break ;
2011-02-25 03:25:30 +03:00
case RA_OSX :
remote_arch_str = " OSX " ;
break ;
2002-07-15 14:35:28 +04:00
default :
ra_type = RA_UNKNOWN ;
2007-11-10 02:09:16 +03:00
remote_arch_str = " UNKNOWN " ;
2002-07-15 14:35:28 +04:00
break ;
}
2003-12-06 00:51:51 +03:00
2007-11-10 02:09:16 +03:00
DEBUG ( 10 , ( " set_remote_arch: Client arch is \' %s \' \n " ,
remote_arch_str ) ) ;
1997-09-05 00:26:07 +04:00
}
/*******************************************************************
Get the remote_arch type .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
1998-04-13 23:24:06 +04:00
enum remote_arch_types get_remote_arch ( void )
1997-09-05 00:26:07 +04:00
{
2002-07-15 14:35:28 +04:00
return ra_type ;
1997-09-05 00:26:07 +04:00
}
1997-09-26 22:55:29 +04:00
2007-12-18 03:20:44 +03:00
const char * tab_depth ( int level , int depth )
1997-10-12 15:46:42 +04:00
{
2008-01-23 02:30:28 +03:00
if ( CHECK_DEBUGLVL ( level ) ) {
2007-12-18 03:20:44 +03:00
dbgtext ( " %*s " , depth * 4 , " " ) ;
2007-11-16 01:19:52 +03:00
}
2007-12-18 03:20:44 +03:00
return " " ;
1997-10-12 15:46:42 +04:00
}
1997-10-20 20:25:19 +04:00
1998-08-14 21:38:29 +04:00
/*****************************************************************************
2002-11-13 02:20:50 +03:00
Provide a checksum on a string
Input : s - the null - terminated character string for which the checksum
will be calculated .
Output : The checksum value calculated for s .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-10-03 17:12:08 +04:00
int str_checksum ( const char * s )
1998-08-14 21:38:29 +04:00
{
2011-03-15 11:30:22 +03:00
TDB_DATA key = string_tdb_data ( s ) ;
return tdb_jenkins_hash ( & key ) ;
2002-11-13 02:20:50 +03:00
}
1998-08-30 08:31:55 +04:00
/*****************************************************************
2002-11-13 02:20:50 +03:00
Zero a memory area then free it . Used to catch bugs faster .
1998-08-30 08:31:55 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-09-11 05:24:30 +04:00
void zero_free ( void * p , size_t size )
1998-08-30 08:31:55 +04:00
{
memset ( p , 0 , size ) ;
2001-09-17 06:19:44 +04:00
SAFE_FREE ( p ) ;
1998-08-30 08:31:55 +04:00
}
1998-10-04 19:54:04 +04:00
1998-10-05 05:57:03 +04:00
/*****************************************************************
2002-11-13 02:20:50 +03:00
Set our open file limit to a requested max and return the limit .
1998-10-05 05:57:03 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-10-16 10:16:10 +04:00
int set_maxfiles ( int requested_max )
1998-10-05 05:57:03 +04:00
{
# if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
struct rlimit rlp ;
1999-12-13 16:27:58 +03:00
int saved_current_limit ;
if ( getrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s \n " ,
strerror ( errno ) ) ) ;
/* just guess... */
return requested_max ;
}
/*
2002-11-13 02:20:50 +03:00
* Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1998-10-19 21:32:10 +04:00
* account for the extra fd we need
* as well as the log files and standard
1999-12-13 16:27:58 +03:00
* handles etc . Save the limit we want to set in case
* we are running on an OS that doesn ' t support this limit ( AIX )
* which always returns RLIM_INFINITY for rlp . rlim_max .
*/
2001-04-13 23:33:26 +04:00
/* Try raising the hard (max) limit to the requested amount. */
# if defined(RLIM_INFINITY)
if ( rlp . rlim_max ! = RLIM_INFINITY ) {
int orig_max = rlp . rlim_max ;
if ( rlp . rlim_max < requested_max )
rlp . rlim_max = requested_max ;
/* This failing is not an error - many systems (Linux) don't
support our default request of 10 , 000 open files . JRA . */
if ( setrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 3 , ( " set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s \n " ,
( int ) rlp . rlim_max , strerror ( errno ) ) ) ;
/* Set failed - restore original value from get. */
rlp . rlim_max = orig_max ;
}
}
# endif
/* Now try setting the soft (current) limit. */
1999-12-13 16:27:58 +03:00
saved_current_limit = rlp . rlim_cur = MIN ( requested_max , rlp . rlim_max ) ;
if ( setrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s \n " ,
( int ) rlp . rlim_cur , strerror ( errno ) ) ) ;
/* just guess... */
return saved_current_limit ;
}
if ( getrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s \n " ,
strerror ( errno ) ) ) ;
/* just guess... */
return saved_current_limit ;
}
# if defined(RLIM_INFINITY)
if ( rlp . rlim_cur = = RLIM_INFINITY )
return saved_current_limit ;
# endif
2002-11-13 02:20:50 +03:00
if ( ( int ) rlp . rlim_cur > saved_current_limit )
1999-12-13 16:27:58 +03:00
return saved_current_limit ;
1998-10-05 05:57:03 +04:00
return rlp . rlim_cur ;
1999-12-13 16:27:58 +03:00
# else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1998-10-16 10:16:10 +04:00
/*
* No way to know - just guess . . .
*/
return requested_max ;
1998-10-05 05:57:03 +04:00
# endif
}
1998-11-11 17:23:55 +03:00
2002-07-15 14:35:28 +04:00
/*****************************************************************
2001-10-22 06:34:41 +04:00
malloc that aborts with smb_panic on fail or zero size .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-12-07 21:25:53 +03:00
void * smb_xmalloc_array ( size_t size , unsigned int count )
2001-10-22 06:34:41 +04:00
{
void * p ;
2007-06-16 01:58:49 +04:00
if ( size = = 0 ) {
smb_panic ( " smb_xmalloc_array: called with zero size " ) ;
}
2004-12-07 21:25:53 +03:00
if ( count > = MAX_ALLOC_SIZE / size ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " smb_xmalloc_array: alloc size too large " ) ;
2004-12-07 21:25:53 +03:00
}
if ( ( p = SMB_MALLOC ( size * count ) ) = = NULL ) {
DEBUG ( 0 , ( " smb_xmalloc_array failed to allocate %lu * %lu bytes \n " ,
( unsigned long ) size , ( unsigned long ) count ) ) ;
2007-06-16 01:58:49 +04:00
smb_panic ( " smb_xmalloc_array: malloc failed " ) ;
2003-03-22 16:47:42 +03:00
}
2001-10-22 06:34:41 +04:00
return p ;
}
2001-12-10 02:56:07 +03:00
/*
vasprintf that aborts on malloc fail
*/
2002-11-13 02:20:50 +03:00
2003-01-03 06:24:23 +03:00
int smb_xvasprintf ( char * * ptr , const char * format , va_list ap )
2001-12-10 02:56:07 +03:00
{
int n ;
2002-07-15 14:35:28 +04:00
va_list ap2 ;
2008-10-17 14:08:15 +04:00
va_copy ( ap2 , ap ) ;
2002-07-15 14:35:28 +04:00
n = vasprintf ( ptr , format , ap2 ) ;
2009-06-11 11:15:28 +04:00
va_end ( ap2 ) ;
2007-06-16 01:58:49 +04:00
if ( n = = - 1 | | ! * ptr ) {
2001-12-10 02:56:07 +03:00
smb_panic ( " smb_xvasprintf: out of memory " ) ;
2007-06-16 01:58:49 +04:00
}
2001-12-10 02:56:07 +03:00
return n ;
}
1999-12-13 16:27:58 +03:00
/*****************************************************************
2002-11-13 02:20:50 +03:00
Get local hostname and cache result .
2007-11-09 05:50:07 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1999-12-13 16:27:58 +03:00
char * myhostname ( void )
1999-12-12 23:03:42 +03:00
{
2007-11-09 05:50:07 +03:00
static char * ret ;
if ( ret = = NULL ) {
2010-09-27 09:05:43 +04:00
ret = get_myname ( NULL ) ;
2007-11-09 05:50:07 +03:00
}
1999-12-13 16:27:58 +03:00
return ret ;
1999-12-12 23:03:42 +03:00
}
1999-12-21 12:25:59 +03:00
2009-01-08 22:18:13 +03:00
/**
* @ brief Returns an absolute path to a file concatenating the provided
* @ a rootpath and @ a basename
*
* @ param name Filename , relative to @ a rootpath
*
* @ retval Pointer to a string containing the full path .
* */
2002-11-13 02:20:50 +03:00
2007-11-09 05:50:07 +03:00
static char * xx_path ( const char * name , const char * rootpath )
1999-12-21 12:25:59 +03:00
{
2007-11-09 05:50:07 +03:00
char * fname = NULL ;
1999-12-21 12:25:59 +03:00
2007-11-09 05:50:07 +03:00
fname = talloc_strdup ( talloc_tos ( ) , rootpath ) ;
if ( ! fname ) {
return NULL ;
}
trim_string ( fname , " " , " / " ) ;
2008-10-17 14:48:19 +04:00
if ( ! directory_exist ( fname ) ) {
2009-01-08 22:18:13 +03:00
if ( ! mkdir ( fname , 0755 ) )
DEBUG ( 1 , ( " Unable to create directory %s for file %s. "
" Error was %s \n " , fname , name , strerror ( errno ) ) ) ;
2007-11-09 05:50:07 +03:00
}
1999-12-21 12:25:59 +03:00
2007-11-09 05:50:07 +03:00
return talloc_asprintf ( talloc_tos ( ) ,
" %s/%s " ,
fname ,
name ) ;
1999-12-21 12:25:59 +03:00
}
2000-01-14 04:41:04 +03:00
2009-01-08 22:18:13 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba lock directory .
*
* @ param name File to find , relative to LOCKDIR .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
2002-11-13 02:20:50 +03:00
2007-11-09 05:50:07 +03:00
char * lock_path ( const char * name )
2002-07-15 14:35:28 +04:00
{
2007-11-09 05:50:07 +03:00
return xx_path ( name , lp_lockdir ( ) ) ;
}
2002-07-15 14:35:28 +04:00
2009-01-08 22:18:13 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba pid directory .
*
* @ param name File to find , relative to PIDDIR .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
2002-07-15 14:35:28 +04:00
2007-11-09 05:50:07 +03:00
char * pid_path ( const char * name )
{
return xx_path ( name , lp_piddir ( ) ) ;
2002-07-15 14:35:28 +04:00
}
2001-11-19 06:35:27 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba lib directory .
*
* @ param name File to find , relative to LIBDIR .
*
2007-12-07 23:02:44 +03:00
* @ retval Pointer to a string containing the full path .
2001-11-19 06:35:27 +03:00
* */
2002-11-13 02:20:50 +03:00
2002-07-15 14:35:28 +04:00
char * lib_path ( const char * name )
2001-10-03 16:18:20 +04:00
{
2007-12-10 22:30:37 +03:00
return talloc_asprintf ( talloc_tos ( ) , " %s/%s " , get_dyn_LIBDIR ( ) , name ) ;
2001-10-03 16:18:20 +04:00
}
2008-08-15 02:44:14 +04:00
/**
* @ brief Returns an absolute path to a file in the Samba modules directory .
*
* @ param name File to find , relative to MODULESDIR .
*
* @ retval Pointer to a string containing the full path .
* */
char * modules_path ( const char * name )
{
return talloc_asprintf ( talloc_tos ( ) , " %s/%s " , get_dyn_MODULESDIR ( ) , name ) ;
}
2007-11-01 20:00:10 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba data directory .
*
* @ param name File to find , relative to CODEPAGEDIR .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
char * data_path ( const char * name )
{
2007-12-10 22:30:37 +03:00
return talloc_asprintf ( talloc_tos ( ) , " %s/%s " , get_dyn_CODEPAGEDIR ( ) , name ) ;
2007-11-01 20:00:10 +03:00
}
2009-01-08 22:18:13 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba state directory .
*
* @ param name File to find , relative to STATEDIR .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
2007-11-02 19:27:04 +03:00
char * state_path ( const char * name )
2007-11-01 20:00:10 +03:00
{
2009-01-08 22:18:13 +03:00
return xx_path ( name , lp_statedir ( ) ) ;
}
/**
* @ brief Returns an absolute path to a file in the Samba cache directory .
*
* @ param name File to find , relative to CACHEDIR .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
char * cache_path ( const char * name )
{
return xx_path ( name , lp_cachedir ( ) ) ;
2007-11-01 20:00:10 +03:00
}
2003-02-14 03:48:28 +03:00
/**
* @ brief Returns the platform specific shared library extension .
*
2007-12-10 22:30:37 +03:00
* @ retval Pointer to a const char * containing the extension .
2003-02-14 03:48:28 +03:00
* */
const char * shlib_ext ( void )
{
2007-12-10 22:30:37 +03:00
return get_dyn_SHLIBEXT ( ) ;
2003-02-14 03:48:28 +03:00
}
2000-01-14 04:41:04 +03:00
/*******************************************************************
Given a filename - get its directory name
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-12-24 15:51:47 +03:00
bool parent_dirname ( TALLOC_CTX * mem_ctx , const char * dir , char * * parent ,
const char * * name )
2006-12-29 00:50:31 +03:00
{
char * p ;
ptrdiff_t len ;
2009-11-21 18:17:16 +03:00
2006-12-29 00:50:31 +03:00
p = strrchr_m ( dir , ' / ' ) ; /* Find final '/', if any */
if ( p = = NULL ) {
if ( ! ( * parent = talloc_strdup ( mem_ctx , " . " ) ) ) {
return False ;
}
if ( name ) {
2009-06-30 18:03:11 +04:00
* name = dir ;
2006-12-29 00:50:31 +03:00
}
return True ;
}
len = p - dir ;
2008-12-24 15:18:35 +03:00
if ( ! ( * parent = ( char * ) TALLOC_MEMDUP ( mem_ctx , dir , len + 1 ) ) ) {
2006-12-29 00:50:31 +03:00
return False ;
}
( * parent ) [ len ] = ' \0 ' ;
if ( name ) {
* name = p + 1 ;
}
return True ;
}
2000-04-24 15:30:24 +04:00
2000-04-30 15:04:28 +04:00
/*******************************************************************
2002-11-13 02:20:50 +03:00
Determine if a pattern contains any Microsoft wildcard characters .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool ms_has_wild ( const char * s )
2000-04-30 15:04:28 +04:00
{
char c ;
2005-06-23 01:20:41 +04:00
if ( lp_posix_pathnames ( ) ) {
/* With posix pathnames no characters are wild. */
return False ;
}
2000-04-30 15:04:28 +04:00
while ( ( c = * s + + ) ) {
switch ( c ) {
case ' * ' :
case ' ? ' :
case ' < ' :
case ' > ' :
case ' " ' :
return True ;
}
}
return False ;
}
2007-10-19 04:40:25 +04:00
bool ms_has_wild_w ( const smb_ucs2_t * s )
2001-11-04 21:26:53 +03:00
{
smb_ucs2_t c ;
2002-01-27 19:37:12 +03:00
if ( ! s ) return False ;
2001-11-04 21:26:53 +03:00
while ( ( c = * s + + ) ) {
switch ( c ) {
case UCS2_CHAR ( ' * ' ) :
case UCS2_CHAR ( ' ? ' ) :
case UCS2_CHAR ( ' < ' ) :
case UCS2_CHAR ( ' > ' ) :
case UCS2_CHAR ( ' " ' ) :
return True ;
}
}
return False ;
}
2000-04-30 15:04:28 +04:00
/*******************************************************************
2002-11-13 02:20:50 +03:00
A wrapper that handles case sensitivity and the special handling
of the " .. " name .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-23 18:34:07 +03:00
bool mask_match ( const char * string , const char * pattern , bool is_case_sensitive )
2000-04-30 15:04:28 +04:00
{
2009-11-21 18:15:16 +03:00
if ( ISDOTDOT ( string ) )
2002-11-13 02:20:50 +03:00
string = " . " ;
2009-11-21 18:15:16 +03:00
if ( ISDOT ( pattern ) )
2002-11-13 02:20:50 +03:00
return False ;
2009-11-21 18:17:16 +03:00
2009-11-23 18:34:07 +03:00
return ms_fnmatch ( pattern , string , Protocol < = PROTOCOL_LANMAN2 , is_case_sensitive ) = = 0 ;
2005-03-24 22:33:02 +03:00
}
/*******************************************************************
A wrapper that handles case sensitivity and the special handling
of the " .. " name . Varient that is only called by old search code which requires
pattern translation .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool mask_match_search ( const char * string , const char * pattern , bool is_case_sensitive )
2005-03-24 22:33:02 +03:00
{
2009-11-21 18:15:16 +03:00
if ( ISDOTDOT ( string ) )
2005-03-24 22:33:02 +03:00
string = " . " ;
2009-11-21 18:15:16 +03:00
if ( ISDOT ( pattern ) )
2005-03-24 22:33:02 +03:00
return False ;
2009-11-21 18:17:16 +03:00
2005-03-24 22:33:02 +03:00
return ms_fnmatch ( pattern , string , True , is_case_sensitive ) = = 0 ;
2000-04-30 15:04:28 +04:00
}
2004-03-19 02:45:15 +03:00
/*******************************************************************
A wrapper that handles a list of patters and calls mask_match ( )
on each . Returns True if any of the patterns match .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-23 18:34:00 +03:00
bool mask_match_list ( const char * string , char * * list , int listLen , bool is_case_sensitive )
2004-03-19 02:45:15 +03:00
{
while ( listLen - - > 0 ) {
2009-11-23 18:34:07 +03:00
if ( mask_match ( string , * list + + , is_case_sensitive ) )
2004-03-19 02:45:15 +03:00
return True ;
}
return False ;
}
2001-10-03 01:58:09 +04:00
/*********************************************************
Recursive routine that is called by unix_wild_match .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-16 22:33:37 +04:00
static bool unix_do_match ( const char * regexp , const char * str )
2001-10-03 01:58:09 +04:00
{
2003-06-17 13:40:35 +04:00
const char * p ;
2001-10-03 01:58:09 +04:00
for ( p = regexp ; * p & & * str ; ) {
switch ( * p ) {
case ' ? ' :
str + + ;
p + + ;
break ;
case ' * ' :
/*
* Look for a character matching
* the one after the ' * ' .
*/
p + + ;
if ( ! * p )
2007-10-16 22:33:37 +04:00
return true ; /* Automatic match */
2001-10-03 01:58:09 +04:00
while ( * str ) {
while ( * str & & ( * p ! = * str ) )
str + + ;
/*
* Patch from weidel @ multichart . de . In the case of the regexp
* ' * XX * ' we want to ensure there are at least 2 ' X ' characters
* in the string after the ' * ' for a match to be made .
*/
{
int matchcount = 0 ;
/*
* Eat all the characters that match , but count how many there were .
*/
while ( * str & & ( * p = = * str ) ) {
str + + ;
matchcount + + ;
}
/*
* Now check that if the regexp had n identical characters that
* matchcount had at least that many matches .
*/
while ( * ( p + 1 ) & & ( * ( p + 1 ) = = * p ) ) {
p + + ;
matchcount - - ;
}
if ( matchcount < = 0 )
2007-10-16 22:33:37 +04:00
return false ;
2001-10-03 01:58:09 +04:00
}
str - - ; /* We've eaten the match char after the '*' */
if ( unix_do_match ( p , str ) )
2007-10-16 22:33:37 +04:00
return true ;
2001-10-03 01:58:09 +04:00
if ( ! * str )
2007-10-16 22:33:37 +04:00
return false ;
2001-10-03 01:58:09 +04:00
else
str + + ;
}
2007-10-16 22:33:37 +04:00
return false ;
2001-10-03 01:58:09 +04:00
default :
if ( * str ! = * p )
2007-10-16 22:33:37 +04:00
return false ;
2001-10-03 01:58:09 +04:00
str + + ;
p + + ;
break ;
}
}
if ( ! * p & & ! * str )
2007-10-16 22:33:37 +04:00
return true ;
2001-10-03 01:58:09 +04:00
if ( ! * p & & str [ 0 ] = = ' . ' & & str [ 1 ] = = 0 )
2007-10-16 22:33:37 +04:00
return true ;
2001-10-03 01:58:09 +04:00
if ( ! * str & & * p = = ' ? ' ) {
while ( * p = = ' ? ' )
p + + ;
return ( ! * p ) ;
}
if ( ! * str & & ( * p = = ' * ' & & p [ 1 ] = = ' \0 ' ) )
2007-10-16 22:33:37 +04:00
return true ;
2001-10-03 01:58:09 +04:00
2007-10-16 22:33:37 +04:00
return false ;
2001-10-03 01:58:09 +04:00
}
2001-07-24 02:06:05 +04:00
/*******************************************************************
2001-10-03 01:58:09 +04:00
Simple case insensitive interface to a UNIX wildcard matcher .
2005-12-16 04:41:12 +03:00
Returns True if match , False if not .
2001-07-24 02:06:05 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-03 01:58:09 +04:00
2007-10-16 22:33:37 +04:00
bool unix_wild_match ( const char * pattern , const char * string )
2001-07-24 02:06:05 +04:00
{
2007-10-16 22:33:37 +04:00
TALLOC_CTX * ctx = talloc_stackframe ( ) ;
char * p2 ;
char * s2 ;
2001-10-03 01:58:09 +04:00
char * p ;
2007-10-16 22:33:37 +04:00
bool ret = false ;
2000-04-30 15:04:28 +04:00
2007-10-16 22:33:37 +04:00
p2 = talloc_strdup ( ctx , pattern ) ;
s2 = talloc_strdup ( ctx , string ) ;
if ( ! p2 | | ! s2 ) {
TALLOC_FREE ( ctx ) ;
return false ;
}
2003-07-03 23:11:31 +04:00
strlower_m ( p2 ) ;
strlower_m ( s2 ) ;
2001-10-03 01:58:09 +04:00
/* Remove any *? and ** from the pattern as they are meaningless */
2007-10-16 22:33:37 +04:00
for ( p = p2 ; * p ; p + + ) {
while ( * p = = ' * ' & & ( p [ 1 ] = = ' ? ' | | p [ 1 ] = = ' * ' ) ) {
memmove ( & p [ 1 ] , & p [ 2 ] , strlen ( & p [ 2 ] ) + 1 ) ;
}
}
2001-10-03 01:58:09 +04:00
2007-10-16 22:33:37 +04:00
if ( strequal ( p2 , " * " ) ) {
TALLOC_FREE ( ctx ) ;
return true ;
}
ret = unix_do_match ( p2 , s2 ) ;
TALLOC_FREE ( ctx ) ;
return ret ;
2001-07-24 02:06:05 +04:00
}
2000-04-30 15:04:28 +04:00
2004-06-22 04:48:59 +04:00
/**********************************************************************
2007-02-27 16:42:15 +03:00
Converts a name to a fully qualified domain name .
2007-10-16 03:11:48 +04:00
Returns true if lookup succeeded , false if not ( then fqdn is set to name )
Note we deliberately use gethostbyname here , not getaddrinfo as we want
to examine the h_aliases and I don ' t know how to do that with getaddrinfo .
2004-06-22 04:48:59 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-07-13 01:02:22 +04:00
2007-10-16 03:11:48 +04:00
bool name_to_fqdn ( fstring fqdn , const char * name )
{
char * full = NULL ;
struct hostent * hp = gethostbyname ( name ) ;
if ( ! hp | | ! hp - > h_name | | ! * hp - > h_name ) {
2004-06-22 04:48:59 +04:00
DEBUG ( 10 , ( " name_to_fqdn: lookup for %s failed. \n " , name ) ) ;
fstrcpy ( fqdn , name ) ;
2007-10-16 03:11:48 +04:00
return false ;
2004-06-22 04:48:59 +04:00
}
2007-10-16 03:11:48 +04:00
/* Find out if the fqdn is returned as an alias
* to cope with / etc / hosts files where the first
* name is not the fqdn but the short name */
if ( hp - > h_aliases & & ( ! strchr_m ( hp - > h_name , ' . ' ) ) ) {
int i ;
for ( i = 0 ; hp - > h_aliases [ i ] ; i + + ) {
if ( strchr_m ( hp - > h_aliases [ i ] , ' . ' ) ) {
full = hp - > h_aliases [ i ] ;
break ;
}
}
}
if ( full & & ( StrCaseCmp ( full , " localhost.localdomain " ) = = 0 ) ) {
DEBUG ( 1 , ( " WARNING: your /etc/hosts file may be broken! \n " ) ) ;
DEBUGADD ( 1 , ( " Specifing the machine hostname for address 127.0.0.1 may lead \n " ) ) ;
DEBUGADD ( 1 , ( " to Kerberos authentication problems as localhost.localdomain \n " ) ) ;
DEBUGADD ( 1 , ( " may end up being used instead of the real machine FQDN. \n " ) ) ;
full = hp - > h_name ;
}
if ( ! full ) {
full = hp - > h_name ;
}
DEBUG ( 10 , ( " name_to_fqdn: lookup for %s -> %s. \n " , name , full ) ) ;
fstrcpy ( fqdn , full ) ;
return true ;
2004-06-22 04:48:59 +04:00
}
2003-06-25 21:41:05 +04:00
2008-09-09 16:34:28 +04:00
/**********************************************************************
Append a DATA_BLOB to a talloc ' ed object
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void * talloc_append_blob ( TALLOC_CTX * mem_ctx , void * buf , DATA_BLOB blob )
{
size_t old_size = 0 ;
char * result ;
if ( blob . length = = 0 ) {
return buf ;
}
if ( buf ! = NULL ) {
old_size = talloc_get_size ( buf ) ;
}
result = ( char * ) TALLOC_REALLOC ( mem_ctx , buf , old_size + blob . length ) ;
if ( result = = NULL ) {
return NULL ;
}
memcpy ( result + old_size , blob . data , blob . length ) ;
return result ;
}
2005-07-08 08:51:27 +04:00
uint32 map_share_mode_to_deny_mode ( uint32 share_access , uint32 private_options )
{
2005-08-16 23:40:13 +04:00
switch ( share_access & ~ FILE_SHARE_DELETE ) {
2005-07-08 08:51:27 +04:00
case FILE_SHARE_NONE :
return DENY_ALL ;
case FILE_SHARE_READ :
return DENY_WRITE ;
case FILE_SHARE_WRITE :
return DENY_READ ;
case FILE_SHARE_READ | FILE_SHARE_WRITE :
return DENY_NONE ;
}
if ( private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS ) {
return DENY_DOS ;
} else if ( private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_FCB ) {
return DENY_FCB ;
}
return ( uint32 ) - 1 ;
}
2005-09-30 21:13:37 +04:00
2007-05-07 13:35:35 +04:00
pid_t procid_to_pid ( const struct server_id * proc )
2005-09-30 21:13:37 +04:00
{
return proc - > pid ;
}
2007-06-10 21:02:09 +04:00
static uint32 my_vnn = NONCLUSTER_VNN ;
void set_my_vnn ( uint32 vnn )
{
DEBUG ( 10 , ( " vnn pid %d = %u \n " , ( int ) sys_getpid ( ) , ( unsigned int ) vnn ) ) ;
my_vnn = vnn ;
}
uint32 get_my_vnn ( void )
{
return my_vnn ;
}
s3: Fix a long-standing problem with recycled PIDs
When a samba server process dies hard, it has no chance to clean up its entries
in locking.tdb, brlock.tdb, connections.tdb and sessionid.tdb.
For locking.tdb and brlock.tdb Samba is robust by checking every time we read
an entry from the database if the corresponding process still exists. If it
does not exist anymore, the entry is deleted. This is not 100% failsafe though:
On systems with a limited PID space there is a non-zero chance that between the
smbd's death and the fresh access, the PID is recycled by another long-running
process. This renders all files that had been locked by the killed smbd
potentially unusable until the new process also dies.
This patch is supposed to fix the problem the following way: Every process ID
in every database is augmented by a random 64-bit number that is stored in a
serverid.tdb. Whenever we need to check if a process still exists we know its
PID and the 64-bit number. We look up the PID in serverid.tdb and compare the
64-bit number. If it's the same, the process still is a valid smbd holding the
lock. If it is different, a new smbd has taken over.
I believe this is safe against an smbd that has died hard and the PID has been
taken over by a non-samba process. This process would not have registered
itself with a fresh 64-bit number in serverid.tdb, so the old one still exists
in serverid.tdb. We protect against this case by the parent smbd taking care of
deregistering PIDs from serverid.tdb and the fact that serverid.tdb is
CLEAR_IF_FIRST.
CLEAR_IF_FIRST does not work in a cluster, so the automatic cleanup does not
work when all smbds are restarted. For this, "net serverid wipe" has to be run
before smbd starts up. As a convenience, "net serverid wipedbs" also cleans up
sessionid.tdb and connections.tdb.
While there, this also cleans up overloading connections.tdb with all the
process entries just for messaging_send_all().
Volker
2010-03-02 19:02:01 +03:00
static uint64_t my_unique_id = 0 ;
void set_my_unique_id ( uint64_t unique_id )
{
my_unique_id = unique_id ;
}
2007-05-07 13:35:35 +04:00
struct server_id pid_to_procid ( pid_t pid )
2005-09-30 21:13:37 +04:00
{
2007-05-07 13:35:35 +04:00
struct server_id result ;
2005-09-30 21:13:37 +04:00
result . pid = pid ;
s3: Fix a long-standing problem with recycled PIDs
When a samba server process dies hard, it has no chance to clean up its entries
in locking.tdb, brlock.tdb, connections.tdb and sessionid.tdb.
For locking.tdb and brlock.tdb Samba is robust by checking every time we read
an entry from the database if the corresponding process still exists. If it
does not exist anymore, the entry is deleted. This is not 100% failsafe though:
On systems with a limited PID space there is a non-zero chance that between the
smbd's death and the fresh access, the PID is recycled by another long-running
process. This renders all files that had been locked by the killed smbd
potentially unusable until the new process also dies.
This patch is supposed to fix the problem the following way: Every process ID
in every database is augmented by a random 64-bit number that is stored in a
serverid.tdb. Whenever we need to check if a process still exists we know its
PID and the 64-bit number. We look up the PID in serverid.tdb and compare the
64-bit number. If it's the same, the process still is a valid smbd holding the
lock. If it is different, a new smbd has taken over.
I believe this is safe against an smbd that has died hard and the PID has been
taken over by a non-samba process. This process would not have registered
itself with a fresh 64-bit number in serverid.tdb, so the old one still exists
in serverid.tdb. We protect against this case by the parent smbd taking care of
deregistering PIDs from serverid.tdb and the fact that serverid.tdb is
CLEAR_IF_FIRST.
CLEAR_IF_FIRST does not work in a cluster, so the automatic cleanup does not
work when all smbds are restarted. For this, "net serverid wipe" has to be run
before smbd starts up. As a convenience, "net serverid wipedbs" also cleans up
sessionid.tdb and connections.tdb.
While there, this also cleans up overloading connections.tdb with all the
process entries just for messaging_send_all().
Volker
2010-03-02 19:02:01 +03:00
result . unique_id = my_unique_id ;
2007-06-10 21:02:09 +04:00
result . vnn = my_vnn ;
2005-09-30 21:13:37 +04:00
return result ;
}
2007-05-07 13:35:35 +04:00
struct server_id procid_self ( void )
2005-09-30 21:13:37 +04:00
{
return pid_to_procid ( sys_getpid ( ) ) ;
}
2007-10-19 04:40:25 +04:00
bool procid_equal ( const struct server_id * p1 , const struct server_id * p2 )
2005-09-30 21:13:37 +04:00
{
2007-06-10 21:02:09 +04:00
if ( p1 - > pid ! = p2 - > pid )
return False ;
if ( p1 - > vnn ! = p2 - > vnn )
return False ;
return True ;
2005-09-30 21:13:37 +04:00
}
2007-10-19 04:40:25 +04:00
bool cluster_id_equal ( const struct server_id * id1 ,
2007-01-31 16:47:37 +03:00
const struct server_id * id2 )
{
2007-05-07 13:35:35 +04:00
return procid_equal ( id1 , id2 ) ;
2007-01-31 16:47:37 +03:00
}
2007-10-19 04:40:25 +04:00
bool procid_is_me ( const struct server_id * pid )
2005-09-30 21:13:37 +04:00
{
2007-06-10 21:02:09 +04:00
if ( pid - > pid ! = sys_getpid ( ) )
return False ;
if ( pid - > vnn ! = my_vnn )
return False ;
return True ;
2005-09-30 21:13:37 +04:00
}
2007-05-07 13:35:35 +04:00
struct server_id interpret_pid ( const char * pid_string )
2005-09-30 21:13:37 +04:00
{
2007-06-10 21:02:09 +04:00
struct server_id result ;
2009-08-17 19:31:01 +04:00
int pid ;
unsigned int vnn ;
if ( sscanf ( pid_string , " %u:%d " , & vnn , & pid ) = = 2 ) {
2007-06-10 21:02:09 +04:00
result . vnn = vnn ;
result . pid = pid ;
}
2009-08-17 19:31:01 +04:00
else if ( sscanf ( pid_string , " %d " , & pid ) = = 1 ) {
2008-05-19 06:20:56 +04:00
result . vnn = get_my_vnn ( ) ;
2007-06-24 17:24:20 +04:00
result . pid = pid ;
}
2007-06-10 21:02:09 +04:00
else {
result . vnn = NONCLUSTER_VNN ;
result . pid = - 1 ;
}
2009-08-17 19:31:01 +04:00
/* Assigning to result.pid may have overflowed
Map negative pid to - 1 : i . e . error */
if ( result . pid < 0 ) {
result . pid = - 1 ;
}
s3: Fix a long-standing problem with recycled PIDs
When a samba server process dies hard, it has no chance to clean up its entries
in locking.tdb, brlock.tdb, connections.tdb and sessionid.tdb.
For locking.tdb and brlock.tdb Samba is robust by checking every time we read
an entry from the database if the corresponding process still exists. If it
does not exist anymore, the entry is deleted. This is not 100% failsafe though:
On systems with a limited PID space there is a non-zero chance that between the
smbd's death and the fresh access, the PID is recycled by another long-running
process. This renders all files that had been locked by the killed smbd
potentially unusable until the new process also dies.
This patch is supposed to fix the problem the following way: Every process ID
in every database is augmented by a random 64-bit number that is stored in a
serverid.tdb. Whenever we need to check if a process still exists we know its
PID and the 64-bit number. We look up the PID in serverid.tdb and compare the
64-bit number. If it's the same, the process still is a valid smbd holding the
lock. If it is different, a new smbd has taken over.
I believe this is safe against an smbd that has died hard and the PID has been
taken over by a non-samba process. This process would not have registered
itself with a fresh 64-bit number in serverid.tdb, so the old one still exists
in serverid.tdb. We protect against this case by the parent smbd taking care of
deregistering PIDs from serverid.tdb and the fact that serverid.tdb is
CLEAR_IF_FIRST.
CLEAR_IF_FIRST does not work in a cluster, so the automatic cleanup does not
work when all smbds are restarted. For this, "net serverid wipe" has to be run
before smbd starts up. As a convenience, "net serverid wipedbs" also cleans up
sessionid.tdb and connections.tdb.
While there, this also cleans up overloading connections.tdb with all the
process entries just for messaging_send_all().
Volker
2010-03-02 19:02:01 +03:00
result . unique_id = 0 ;
2009-08-17 19:31:01 +04:00
return result ;
2005-09-30 21:13:37 +04:00
}
2007-09-04 14:15:04 +04:00
char * procid_str ( TALLOC_CTX * mem_ctx , const struct server_id * pid )
2005-09-30 21:13:37 +04:00
{
2007-06-10 21:02:09 +04:00
if ( pid - > vnn = = NONCLUSTER_VNN ) {
2007-11-09 06:00:10 +03:00
return talloc_asprintf ( mem_ctx ,
" %d " ,
( int ) pid - > pid ) ;
2007-06-10 21:02:09 +04:00
}
else {
2007-11-09 06:00:10 +03:00
return talloc_asprintf ( mem_ctx ,
" %u:%d " ,
( unsigned ) pid - > vnn ,
( int ) pid - > pid ) ;
2007-06-10 21:02:09 +04:00
}
2005-09-30 21:13:37 +04:00
}
2007-09-04 14:15:04 +04:00
char * procid_str_static ( const struct server_id * pid )
2005-09-30 21:13:37 +04:00
{
2007-09-04 14:15:04 +04:00
return procid_str ( talloc_tos ( ) , pid ) ;
2005-09-30 21:13:37 +04:00
}
2007-10-19 04:40:25 +04:00
bool procid_valid ( const struct server_id * pid )
2005-09-30 21:13:37 +04:00
{
return ( pid - > pid ! = - 1 ) ;
}
2007-10-19 04:40:25 +04:00
bool procid_is_local ( const struct server_id * pid )
2005-09-30 21:13:37 +04:00
{
2007-06-10 21:02:09 +04:00
return pid - > vnn = = my_vnn ;
2005-09-30 21:13:37 +04:00
}
2006-05-08 07:20:49 +04:00
2008-11-08 18:48:20 +03:00
/****************************************************************
Check if offset / length fit into bufsize . Should probably be
merged with is_offset_safe , but this would require a rewrite
of lanman . c . Later : - )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool trans_oob ( uint32_t bufsize , uint32_t offset , uint32_t length )
{
if ( ( offset + length < offset ) | | ( offset + length < length ) ) {
/* wrap */
return true ;
}
if ( ( offset > bufsize ) | | ( offset + length > bufsize ) ) {
/* overflow */
return true ;
}
return false ;
}
2007-03-31 02:25:08 +04:00
/****************************************************************
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
Check if an offset into a buffer is safe .
2007-04-03 08:52:09 +04:00
If this returns True it ' s safe to indirect into the byte at
pointer ptr + off .
2007-03-31 02:25:08 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool is_offset_safe ( const char * buf_base , size_t buf_len , char * ptr , size_t off )
2007-03-31 02:25:08 +04:00
{
const char * end_base = buf_base + buf_len ;
2007-04-01 17:50:02 +04:00
char * end_ptr = ptr + off ;
2007-03-31 02:25:08 +04:00
if ( ! buf_base | | ! ptr ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return False ;
2007-03-31 02:25:08 +04:00
}
if ( end_base < buf_base | | end_ptr < ptr ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return False ; /* wrap. */
2007-03-31 02:25:08 +04:00
}
if ( end_ptr < end_base ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return True ;
2007-03-31 02:25:08 +04:00
}
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return False ;
}
/****************************************************************
Return a safe pointer into a buffer , or NULL .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char * get_safe_ptr ( const char * buf_base , size_t buf_len , char * ptr , size_t off )
{
return is_offset_safe ( buf_base , buf_len , ptr , off ) ?
ptr + off : NULL ;
}
/****************************************************************
Return a safe pointer into a string within a buffer , or NULL .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char * get_safe_str_ptr ( const char * buf_base , size_t buf_len , char * ptr , size_t off )
{
if ( ! is_offset_safe ( buf_base , buf_len , ptr , off ) ) {
return NULL ;
}
/* Check if a valid string exists at this offset. */
2007-04-03 00:10:21 +04:00
if ( skip_string ( buf_base , buf_len , ptr + off ) = = NULL ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return NULL ;
}
return ptr + off ;
}
/****************************************************************
Return an SVAL at a pointer , or failval if beyond the end .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int get_safe_SVAL ( const char * buf_base , size_t buf_len , char * ptr , size_t off , int failval )
{
2007-04-03 08:52:09 +04:00
/*
* Note we use off + 1 here , not off + 2 as SVAL accesses ptr [ 0 ] and ptr [ 1 ] ,
* NOT ptr [ 2 ] .
*/
if ( ! is_offset_safe ( buf_base , buf_len , ptr , off + 1 ) ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return failval ;
}
2007-04-03 08:52:09 +04:00
return SVAL ( ptr , off ) ;
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
}
/****************************************************************
Return an IVAL at a pointer , or failval if beyond the end .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int get_safe_IVAL ( const char * buf_base , size_t buf_len , char * ptr , size_t off , int failval )
{
2007-04-03 08:52:09 +04:00
/*
* Note we use off + 3 here , not off + 4 as IVAL accesses
* ptr [ 0 ] ptr [ 1 ] ptr [ 2 ] ptr [ 3 ] NOT ptr [ 4 ] .
*/
if ( ! is_offset_safe ( buf_base , buf_len , ptr , off + 3 ) ) {
r22042: Try and clean up my own mess using the API Volker
suggested. I now use :
BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
Volker, please criticize and comment. Thanks,
Jeremy.
(This used to be commit d47af7c9263f519e7307859b6a696d854c5dfca3)
2007-04-02 23:04:57 +04:00
return failval ;
}
2007-04-03 08:52:09 +04:00
return IVAL ( ptr , off ) ;
2007-03-31 02:25:08 +04:00
}
2007-04-29 04:09:22 +04:00
2007-12-12 02:42:22 +03:00
/****************************************************************
Split DOM \ user into DOM and user . Do not mix with winbind variants of that
call ( they take care of winbind separator and other winbind specific settings ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void split_domain_user ( TALLOC_CTX * mem_ctx ,
const char * full_name ,
char * * domain ,
char * * user )
{
const char * p = NULL ;
p = strchr_m ( full_name , ' \\ ' ) ;
if ( p ! = NULL ) {
* domain = talloc_strndup ( mem_ctx , full_name ,
PTR_DIFF ( p , full_name ) ) ;
* user = talloc_strdup ( mem_ctx , p + 1 ) ;
} else {
* domain = talloc_strdup ( mem_ctx , " " ) ;
* user = talloc_strdup ( mem_ctx , full_name ) ;
}
}
2007-08-29 05:23:31 +04:00
#if 0
2007-09-02 21:50:05 +04:00
Disable these now we have checked all code paths and ensured
2007-08-29 05:23:31 +04:00
NULL returns on zero request . JRA .
2007-04-29 04:09:22 +04:00
/****************************************************************
talloc wrapper functions that guarentee a null pointer return
if size = = 0.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef MAX_TALLOC_SIZE
# define MAX_TALLOC_SIZE 0x10000000
# endif
/*
* talloc and zero memory .
* - returns NULL if size is zero .
*/
void * _talloc_zero_zeronull ( const void * ctx , size_t size , const char * name )
{
void * p ;
if ( size = = 0 ) {
return NULL ;
}
p = talloc_named_const ( ctx , size , name ) ;
if ( p ) {
memset ( p , ' \0 ' , size ) ;
}
return p ;
}
/*
* memdup with a talloc .
* - returns NULL if size is zero .
*/
void * _talloc_memdup_zeronull ( const void * t , const void * p , size_t size , const char * name )
{
void * newp ;
if ( size = = 0 ) {
return NULL ;
}
newp = talloc_named_const ( t , size , name ) ;
if ( newp ) {
memcpy ( newp , p , size ) ;
}
return newp ;
}
/*
* alloc an array , checking for integer overflow in the array size .
* - returns NULL if count or el_size are zero .
*/
void * _talloc_array_zeronull ( const void * ctx , size_t el_size , unsigned count , const char * name )
{
if ( count > = MAX_TALLOC_SIZE / el_size ) {
return NULL ;
}
if ( el_size = = 0 | | count = = 0 ) {
return NULL ;
}
return talloc_named_const ( ctx , el_size * count , name ) ;
}
/*
* alloc an zero array , checking for integer overflow in the array size
* - returns NULL if count or el_size are zero .
*/
void * _talloc_zero_array_zeronull ( const void * ctx , size_t el_size , unsigned count , const char * name )
{
if ( count > = MAX_TALLOC_SIZE / el_size ) {
return NULL ;
}
if ( el_size = = 0 | | count = = 0 ) {
return NULL ;
}
return _talloc_zero ( ctx , el_size * count , name ) ;
}
/*
* Talloc wrapper that returns NULL if size = = 0.
*/
void * talloc_zeronull ( const void * context , size_t size , const char * name )
{
if ( size = = 0 ) {
return NULL ;
}
return talloc_named_const ( context , size , name ) ;
}
2007-08-29 05:23:31 +04:00
# endif
2008-01-20 01:10:09 +03:00
2008-05-08 16:16:50 +04:00
/****************************************************************
strip off leading ' \\ ' from a hostname
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
const char * strip_hostname ( const char * s )
{
if ( ! s ) {
return NULL ;
}
if ( strlen_m ( s ) < 3 ) {
return s ;
}
if ( s [ 0 ] = = ' \\ ' ) s + + ;
if ( s [ 0 ] = = ' \\ ' ) s + + ;
return s ;
}
2009-11-14 13:32:00 +03:00
bool tevent_req_poll_ntstatus ( struct tevent_req * req ,
struct tevent_context * ev ,
NTSTATUS * status )
{
bool ret = tevent_req_poll ( req , ev ) ;
if ( ! ret ) {
* status = map_nt_error_from_unix ( errno ) ;
}
return ret ;
}
2010-10-18 12:09:57 +04:00
bool any_nt_status_not_ok ( NTSTATUS err1 , NTSTATUS err2 , NTSTATUS * result )
{
if ( ! NT_STATUS_IS_OK ( err1 ) ) {
* result = err1 ;
return true ;
}
if ( ! NT_STATUS_IS_OK ( err2 ) ) {
* result = err2 ;
return true ;
}
return false ;
}
2011-02-08 14:55:02 +03:00
int timeval_to_msec ( struct timeval t )
{
return t . tv_sec * 1000 + ( t . tv_usec + 999 ) / 1000 ;
}