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"
2011-02-25 19:14:22 +03:00
# include "system/passwd.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2010-08-26 12:14:07 +04:00
# include "ctdbd_conn.h"
2011-03-02 18:06:32 +03:00
# include "../lib/util/util_pw.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-06-20 11:26:15 +04:00
# include <ccan/hash/hash.h>
2011-07-29 18:36:58 +04:00
# include "libcli/security/security.h"
2012-08-22 14:36:22 +04:00
# include "serverid.h"
1996-05-04 11:50:46 +04:00
2011-10-04 12:47:46 +04:00
# ifdef HAVE_SYS_PRCTL_H
# include <sys/prctl.h>
# endif
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
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_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
}
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
}
2012-07-09 23:26:56 +04:00
/****************************************************************************
Check two stats have identical dev and ino fields .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool check_same_dev_ino ( const SMB_STRUCT_STAT * sbuf1 ,
const SMB_STRUCT_STAT * sbuf2 )
{
if ( sbuf1 - > st_ex_dev ! = sbuf2 - > st_ex_dev | |
sbuf1 - > st_ex_ino ! = sbuf2 - > st_ex_ino ) {
return false ;
}
return true ;
}
/****************************************************************************
Check if a stat struct is identical for use .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool check_same_stat ( const SMB_STRUCT_STAT * sbuf1 ,
const SMB_STRUCT_STAT * sbuf2 )
{
if ( sbuf1 - > st_ex_uid ! = sbuf2 - > st_ex_uid | |
sbuf1 - > st_ex_gid ! = sbuf2 - > st_ex_gid | |
! check_same_dev_ino ( sbuf1 , sbuf2 ) ) {
return false ;
}
return true ;
}
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
2011-05-05 21:41:59 +04:00
void show_msg ( const 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
2011-05-06 00:42:05 +04:00
dump_data ( 10 , ( const uint8 * ) smb_buf_const ( buf ) , bcc ) ;
1998-11-10 21:14:16 +03:00
}
1996-05-04 11:50:46 +04:00
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 ;
2011-06-07 05:10:15 +04:00
if ( ! ( tmp = talloc_realloc ( NULL , * outbuf , uint8 , newlen ) ) ) {
2007-07-23 14:52:39 +04:00
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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-04-05 08:53:08 +04:00
ssize_t write_data_at_offset ( int fd , const char * buffer , size_t N , off_t pos )
2005-05-18 22:02:15 +04:00
{
size_t total = 0 ;
ssize_t ret ;
2012-04-05 08:53:08 +04:00
if ( pos = = ( off_t ) - 1 ) {
2005-05-18 22:02:15 +04:00
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. */
2012-03-28 05:37:04 +04:00
if ( lseek ( fd , pos , SEEK_SET ) = = - 1 ) {
2005-05-18 22:02:15 +04:00
if ( errno ! = ESPIPE ) {
return - 1 ;
}
}
return write_data ( fd , buffer , N ) ;
# endif
}
2012-03-15 19:29:27 +04:00
static int reinit_after_fork_pipe [ 2 ] = { - 1 , - 1 } ;
NTSTATUS init_before_fork ( void )
{
int ret ;
ret = pipe ( reinit_after_fork_pipe ) ;
if ( ret = = - 1 ) {
NTSTATUS status ;
status = map_nt_error_from_unix_common ( errno ) ;
DEBUG ( 0 , ( " Error creating child_pipe: %s \n " ,
nt_errstr ( status ) ) ) ;
return status ;
}
return NT_STATUS_OK ;
}
/**
* Detect died parent by detecting EOF on the pipe
*/
static void reinit_after_fork_pipe_handler ( struct tevent_context * ev ,
struct tevent_fd * fde ,
uint16_t flags ,
void * private_data )
{
char c ;
2012-03-16 00:24:57 +04:00
if ( sys_read ( reinit_after_fork_pipe [ 0 ] , & c , 1 ) ! = 1 ) {
2012-03-15 19:29:27 +04:00
/*
* we have reached EOF on stdin , which means the
* parent has exited . Shutdown the server
*/
( void ) kill ( getpid ( ) , SIGTERM ) ;
}
}
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 ,
2013-02-18 13:00:26 +04:00
struct tevent_context * ev_ctx ,
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
2012-03-15 19:29:27 +04:00
if ( reinit_after_fork_pipe [ 1 ] ! = - 1 ) {
close ( reinit_after_fork_pipe [ 1 ] ) ;
reinit_after_fork_pipe [ 1 ] = - 1 ;
}
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 */
2011-06-20 13:10:33 +04:00
if ( tdb_reopen_all ( parent_longlived ? 1 : 0 ) ! = 0 ) {
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
}
2012-03-15 19:29:27 +04:00
if ( reinit_after_fork_pipe [ 0 ] ! = - 1 ) {
struct tevent_fd * fde ;
fde = tevent_add_fd ( ev_ctx , ev_ctx /* TALLOC_CTX */ ,
reinit_after_fork_pipe [ 0 ] , TEVENT_FD_READ ,
reinit_after_fork_pipe_handler , NULL ) ;
if ( fde = = NULL ) {
smb_panic ( __location__ " : Failed to add reinit_after_fork pipe event " ) ;
}
}
2009-01-16 02:52:41 +03:00
if ( msg_ctx ) {
/*
* For clustering , we need to re - init our ctdbd connection after the
* fork
*/
2011-12-12 17:55:54 +04:00
status = messaging_reinit ( msg_ctx ) ;
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
}
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 */
2012-07-18 09:37:23 +04:00
char * nis_map = lp_nis_home_map_name ( talloc_tos ( ) ) ;
2002-11-13 02:20:50 +03:00
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
2007-10-19 04:40:25 +04:00
bool process_exists ( const struct server_id pid )
1996-05-04 11:50:46 +04:00
{
2012-08-22 14:36:22 +04:00
return serverid_exists ( & pid ) ;
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
2012-03-24 18:25:48 +04:00
grp = 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
2011-03-22 08:17:39 +03:00
void smb_panic_s3 ( const char * 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 ;
2006-04-04 04:27:50 +04:00
DEBUG ( 0 , ( " PANIC (pid %llu): %s \n " ,
2012-03-24 23:17:08 +04:00
( unsigned long long ) getpid ( ) , why ) ) ;
2006-04-04 04:27:50 +04:00
log_stack_trace ( ) ;
2011-10-04 12:47:46 +04:00
# if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
/*
* Make sure all children can attach a debugger .
*/
prctl ( PR_SET_PTRACER , getpid ( ) , 0 , 0 , 0 ) ;
# endif
2012-07-18 09:37:23 +04:00
cmd = lp_panic_action ( talloc_tos ( ) ) ;
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
2012-03-28 06:22:03 +04:00
const char * readdirname ( DIR * p )
1996-05-04 11:50:46 +04:00
{
2012-03-28 06:18:14 +04:00
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
2012-03-28 06:18:14 +04:00
ptr = ( struct dirent * ) 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 ) ) | |
2011-05-13 22:21:30 +04:00
( ! case_sensitive & & ( strcasecmp_m ( last_component , namelist - > name ) = = 0 ) ) ) {
2002-11-13 02:20:50 +03:00
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 ( ) .
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
2011-05-06 03:32:01 +04:00
void set_namearray ( name_compare_entry * * ppname_array , const char * namelist_in )
1997-07-24 21:25:11 +04:00
{
2002-11-13 02:20:50 +03:00
char * name_end ;
2011-05-06 03:32:01 +04:00
char * namelist ;
char * nameptr ;
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
2011-05-06 03:32:01 +04:00
if ( ( namelist_in = = NULL ) | | ( ( namelist_in ! = NULL ) & & ( * namelist_in = = ' \0 ' ) ) )
2002-11-13 02:20:50 +03:00
return ;
1997-07-24 21:25:11 +04:00
2011-05-06 03:32:01 +04:00
namelist = talloc_strdup ( talloc_tos ( ) , namelist_in ) ;
if ( namelist = = NULL ) {
DEBUG ( 0 , ( " set_namearray: talloc fail \n " ) ) ;
return ;
}
nameptr = namelist ;
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 + + ;
}
2011-05-06 03:32:01 +04:00
if ( num_entries = = 0 ) {
talloc_free ( namelist ) ;
2002-11-13 02:20:50 +03:00
return ;
2011-05-06 03:32:01 +04:00
}
2002-11-13 02:20:50 +03:00
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 " ) ) ;
2011-05-06 03:32:01 +04:00
talloc_free ( namelist ) ;
2002-11-13 02:20:50 +03:00
return ;
}
/* Now copy out the names */
2011-05-06 00:42:05 +04:00
nameptr = 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 " ) ) ;
2011-05-06 03:32:01 +04:00
talloc_free ( namelist ) ;
2002-11-13 02:20:50 +03:00
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
2011-05-06 03:32:01 +04:00
talloc_free ( namelist ) ;
2002-11-13 02:20:50 +03:00
return ;
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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-04-05 08:53:08 +04:00
bool fcntl_getlock ( int fd , off_t * poffset , off_t * pcount , int * ptype , pid_t * ppid )
2006-04-10 19:33:04 +04:00
{
2012-03-28 06:26:34 +04:00
struct flock lock ;
2006-04-10 19:33:04 +04:00
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 ;
2012-03-28 06:32:54 +04:00
ret = sys_fcntl_ptr ( fd , F_GETLK , & lock ) ;
2006-04-10 19:33:04 +04:00
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-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-06-20 11:26:15 +04:00
if ( s = = NULL )
return 0 ;
return hash ( s , strlen ( s ) , 0 ) ;
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
2011-06-09 09:27:53 +04:00
/*****************************************************************
Get local hostname and cache result .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char * myhostname_upper ( void )
{
char * name ;
static char * ret ;
if ( ret = = NULL ) {
name = get_myname ( talloc_tos ( ) ) ;
ret = strupper_talloc ( NULL , name ) ;
talloc_free ( name ) ;
}
return ret ;
}
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
2013-01-24 19:39:05 +04:00
return talloc_asprintf_append ( fname , " /%s " , 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 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
}
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 ;
2011-06-07 06:13:26 +04: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 ;
}
2012-08-09 04:01:00 +04:00
if ( ! strlower_m ( p2 ) ) {
TALLOC_FREE ( ctx ) ;
return false ;
}
if ( ! strlower_m ( s2 ) ) {
TALLOC_FREE ( ctx ) ;
return false ;
}
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 ;
}
}
}
2011-05-13 22:21:30 +04:00
if ( full & & ( strcasecmp_m ( full , " localhost.localdomain " ) = = 0 ) ) {
2007-10-16 03:11:48 +04:00
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 )
{
2012-03-24 23:17:08 +04:00
DEBUG ( 10 , ( " vnn pid %d = %u \n " , ( int ) getpid ( ) , ( unsigned int ) vnn ) ) ;
2007-06-10 21:02:09 +04:00
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 ;
2011-05-02 04:27:36 +04:00
result . task_id = 0 ;
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
{
2012-03-24 23:17:08 +04:00
return pid_to_procid ( getpid ( ) ) ;
2005-09-30 21:13:37 +04:00
}
2011-07-21 10:29:38 +04:00
static struct idr_context * task_id_tree ;
static int free_task_id ( struct server_id * server_id )
{
idr_remove ( task_id_tree , server_id - > task_id ) ;
return 0 ;
}
/* Return a server_id with a unique task_id element. Free the
* returned pointer to de - allocate the task_id via a talloc destructor
* ( ie , use talloc_free ( ) ) */
struct server_id * new_server_id_task ( TALLOC_CTX * mem_ctx )
{
struct server_id * server_id ;
int task_id ;
if ( ! task_id_tree ) {
task_id_tree = idr_init ( NULL ) ;
if ( ! task_id_tree ) {
return NULL ;
}
}
server_id = talloc ( mem_ctx , struct server_id ) ;
if ( ! server_id ) {
return NULL ;
}
* server_id = procid_self ( ) ;
/* 0 is the default server_id, so we need to start with 1 */
task_id = idr_get_new_above ( task_id_tree , server_id , 1 , INT32_MAX ) ;
if ( task_id = = - 1 ) {
talloc_free ( server_id ) ;
return NULL ;
}
talloc_set_destructor ( server_id , free_task_id ) ;
server_id - > task_id = task_id ;
return server_id ;
}
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
{
2012-03-24 23:17:08 +04:00
if ( pid - > pid ! = getpid ( ) )
2007-06-10 21:02:09 +04:00
return False ;
2011-05-02 04:27:36 +04:00
if ( pid - > task_id ! = 0 )
return False ;
2007-06-10 21:02:09 +04:00
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
{
2012-08-17 14:49:26 +04:00
return server_id_from_string ( get_my_vnn ( ) , pid_string ) ;
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
{
2011-06-08 08:05:55 +04:00
return server_id_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
{
2011-06-08 06:47:59 +04:00
return ( pid - > pid ! = ( uint64_t ) - 1 ) ;
2005-09-30 21:13:37 +04:00
}
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
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 ) ;
}
}
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
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 ;
}
2011-04-30 00:34:56 +04:00
/*******************************************************************
Check a given DOS pathname is valid for a share .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char * valid_share_pathname ( TALLOC_CTX * ctx , const char * dos_pathname )
{
char * ptr = NULL ;
if ( ! dos_pathname ) {
return NULL ;
}
ptr = talloc_strdup ( ctx , dos_pathname ) ;
if ( ! ptr ) {
return NULL ;
}
/* Convert any '\' paths to '/' */
unix_format ( ptr ) ;
ptr = unix_clean_name ( ctx , ptr ) ;
if ( ! ptr ) {
return NULL ;
}
/* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
if ( strlen ( ptr ) > 2 & & ptr [ 1 ] = = ' : ' & & ptr [ 0 ] ! = ' / ' )
ptr + = 2 ;
/* Only absolute paths allowed. */
if ( * ptr ! = ' / ' )
return NULL ;
return ptr ;
}
2011-07-29 18:14:39 +04:00
/*******************************************************************
Return True if the filename is one of the special executable types .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool is_executable ( const char * fname )
{
if ( ( fname = strrchr_m ( fname , ' . ' ) ) ) {
if ( strequal ( fname , " .com " ) | |
strequal ( fname , " .dll " ) | |
strequal ( fname , " .exe " ) | |
strequal ( fname , " .sym " ) ) {
return True ;
}
}
return False ;
}
2011-07-29 18:36:58 +04:00
/****************************************************************************
Open a file with a share mode - old openX method - map into NTCreate .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool map_open_params_to_ntcreate ( const char * smb_base_fname ,
int deny_mode , int open_func ,
uint32 * paccess_mask ,
uint32 * pshare_mode ,
uint32 * pcreate_disposition ,
uint32 * pcreate_options ,
uint32_t * pprivate_flags )
{
uint32 access_mask ;
uint32 share_mode ;
uint32 create_disposition ;
uint32 create_options = FILE_NON_DIRECTORY_FILE ;
uint32_t private_flags = 0 ;
DEBUG ( 10 , ( " map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
" open_func = 0x%x \n " ,
smb_base_fname , ( unsigned int ) deny_mode ,
( unsigned int ) open_func ) ) ;
/* Create the NT compatible access_mask. */
switch ( GET_OPENX_MODE ( deny_mode ) ) {
case DOS_OPEN_EXEC : /* Implies read-only - used to be FILE_READ_DATA */
case DOS_OPEN_RDONLY :
access_mask = FILE_GENERIC_READ ;
break ;
case DOS_OPEN_WRONLY :
access_mask = FILE_GENERIC_WRITE ;
break ;
case DOS_OPEN_RDWR :
case DOS_OPEN_FCB :
access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE ;
break ;
default :
DEBUG ( 10 , ( " map_open_params_to_ntcreate: bad open mode = 0x%x \n " ,
( unsigned int ) GET_OPENX_MODE ( deny_mode ) ) ) ;
return False ;
}
/* Create the NT compatible create_disposition. */
switch ( open_func ) {
case OPENX_FILE_EXISTS_FAIL | OPENX_FILE_CREATE_IF_NOT_EXIST :
create_disposition = FILE_CREATE ;
break ;
case OPENX_FILE_EXISTS_OPEN :
create_disposition = FILE_OPEN ;
break ;
case OPENX_FILE_EXISTS_OPEN | OPENX_FILE_CREATE_IF_NOT_EXIST :
create_disposition = FILE_OPEN_IF ;
break ;
case OPENX_FILE_EXISTS_TRUNCATE :
create_disposition = FILE_OVERWRITE ;
break ;
case OPENX_FILE_EXISTS_TRUNCATE | OPENX_FILE_CREATE_IF_NOT_EXIST :
create_disposition = FILE_OVERWRITE_IF ;
break ;
default :
/* From samba4 - to be confirmed. */
if ( GET_OPENX_MODE ( deny_mode ) = = DOS_OPEN_EXEC ) {
create_disposition = FILE_CREATE ;
break ;
}
DEBUG ( 10 , ( " map_open_params_to_ntcreate: bad "
" open_func 0x%x \n " , ( unsigned int ) open_func ) ) ;
return False ;
}
/* Create the NT compatible share modes. */
switch ( GET_DENY_MODE ( deny_mode ) ) {
case DENY_ALL :
share_mode = FILE_SHARE_NONE ;
break ;
case DENY_WRITE :
share_mode = FILE_SHARE_READ ;
break ;
case DENY_READ :
share_mode = FILE_SHARE_WRITE ;
break ;
case DENY_NONE :
share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
break ;
case DENY_DOS :
private_flags | = NTCREATEX_OPTIONS_PRIVATE_DENY_DOS ;
if ( is_executable ( smb_base_fname ) ) {
share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
} else {
if ( GET_OPENX_MODE ( deny_mode ) = = DOS_OPEN_RDONLY ) {
share_mode = FILE_SHARE_READ ;
} else {
share_mode = FILE_SHARE_NONE ;
}
}
break ;
case DENY_FCB :
private_flags | = NTCREATEX_OPTIONS_PRIVATE_DENY_FCB ;
share_mode = FILE_SHARE_NONE ;
break ;
default :
DEBUG ( 10 , ( " map_open_params_to_ntcreate: bad deny_mode 0x%x \n " ,
( unsigned int ) GET_DENY_MODE ( deny_mode ) ) ) ;
return False ;
}
DEBUG ( 10 , ( " map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
" share_mode = 0x%x, create_disposition = 0x%x, "
" create_options = 0x%x private_flags = 0x%x \n " ,
smb_base_fname ,
( unsigned int ) access_mask ,
( unsigned int ) share_mode ,
( unsigned int ) create_disposition ,
( unsigned int ) create_options ,
( unsigned int ) private_flags ) ) ;
if ( paccess_mask ) {
* paccess_mask = access_mask ;
}
if ( pshare_mode ) {
* pshare_mode = share_mode ;
}
if ( pcreate_disposition ) {
* pcreate_disposition = create_disposition ;
}
if ( pcreate_options ) {
* pcreate_options = create_options ;
}
if ( pprivate_flags ) {
* pprivate_flags = private_flags ;
}
return True ;
}
2012-07-04 02:32:10 +04:00
/*************************************************************************
Return a talloced copy of a struct security_unix_token . NULL on fail .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct security_unix_token * copy_unix_token ( TALLOC_CTX * ctx , const struct security_unix_token * tok )
{
struct security_unix_token * cpy ;
cpy = talloc ( ctx , struct security_unix_token ) ;
if ( ! cpy ) {
return NULL ;
}
cpy - > uid = tok - > uid ;
cpy - > gid = tok - > gid ;
cpy - > ngroups = tok - > ngroups ;
if ( tok - > ngroups ) {
/* Make this a talloc child of cpy. */
cpy - > groups = ( gid_t * ) talloc_memdup (
cpy , tok - > groups , tok - > ngroups * sizeof ( gid_t ) ) ;
if ( ! cpy - > groups ) {
TALLOC_FREE ( cpy ) ;
return NULL ;
}
} else {
cpy - > groups = NULL ;
}
return cpy ;
}