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"
2017-01-01 23:00:55 +03:00
# include "lib/util/server_id.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"
2015-10-16 12:36:48 +03:00
# include "messages_dgm.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"
2015-10-12 16:57:34 +03:00
# include "lib/util/sys_rw.h"
# include "lib/util/sys_rw_data.h"
2015-09-23 21:14:05 +03:00
# include "lib/util/util_process.h"
2016-12-21 10:38:25 +03:00
# include "lib/dbwrap/dbwrap_ctdb.h"
2018-10-18 22:53:36 +03:00
# include "lib/gencache.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))
2018-01-19 11:33:21 +03:00
/* rpc/xdr.h uses TRUE and FALSE */
# ifdef TRUE
# undef TRUE
# endif
# ifdef FALSE
# undef FALSE
# endif
# include "system/nis.h"
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
2015-04-25 05:22:21 +03:00
dump_data ( 10 , ( const uint8_t * ) 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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-04-25 05:22:21 +03:00
ssize_t message_push_blob ( uint8_t * * outbuf , DATA_BLOB blob )
2007-07-23 14:52:39 +04:00
{
size_t newlen = smb_len ( * outbuf ) + 4 + blob . length ;
2015-04-25 05:22:21 +03:00
uint8_t * tmp ;
2007-07-23 14:52:39 +04:00
2015-05-10 02:33:10 +03:00
if ( ! ( tmp = talloc_realloc ( NULL , * outbuf , uint8_t , 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
*/
2018-03-22 14:32:15 +03:00
TALLOC_FREE ( fde ) ;
2012-03-15 19:29:27 +04:00
( 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 ,
2015-09-23 21:14:05 +03:00
bool parent_longlived ,
const char * comment )
2008-04-15 12:36:15 +04:00
{
2009-05-06 01:18:50 +04:00
NTSTATUS status = NT_STATUS_OK ;
2016-12-21 10:38:25 +03:00
int ret ;
2008-04-15 12:36:15 +04:00
2019-01-11 15:13:04 +03:00
/*
* The main process thread should never
* allow per_thread_cwd_enable ( ) to be
* called .
*/
per_thread_cwd_disable ( ) ;
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
/* 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
}
2014-09-21 12:29:49 +04:00
if ( ev_ctx ! = NULL ) {
tevent_set_trace_callback ( ev_ctx , NULL , NULL ) ;
if ( 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 ) ) ) ;
}
2016-12-21 10:38:25 +03:00
if ( lp_clustering ( ) ) {
ret = ctdb_async_ctx_reinit (
NULL , messaging_tevent_context ( msg_ctx ) ) ;
if ( ret ! = 0 ) {
DBG_ERR ( " db_ctdb_async_ctx_reinit failed: %s \n " ,
strerror ( errno ) ) ;
return map_nt_error_from_unix ( ret ) ;
}
}
2008-04-15 12:36:15 +04:00
}
2015-09-23 21:14:05 +03:00
if ( comment ) {
prctl_set_comment ( comment ) ;
}
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 ,
2015-04-25 05:22:21 +03:00
void * element , void * _array , uint32_t * 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
}
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
{
2019-11-04 19:50:59 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2007-12-05 04:48:38 +03:00
char * value = NULL ;
2019-11-04 19:50:59 +03:00
char * nis_map = ( char * ) lp_homedir_map ( talloc_tos ( ) , lp_sub ) ;
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
{
2019-11-04 19:50:59 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
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 */
2019-11-04 19:50:59 +03:00
char * nis_map = lp_homedir_map ( talloc_tos ( ) , lp_sub ) ;
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
{
2019-11-05 13:04:39 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
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
2019-11-05 13:04:39 +03:00
cmd = lp_panic_action ( talloc_tos ( ) , lp_sub ) ;
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 ( ) ;
}
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 ;
2014-04-07 15:46:42 +04:00
char * namelist_end ;
2011-05-06 03:32:01 +04:00
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 ;
2014-04-07 15:46:42 +04:00
namelist_end = & namelist [ strlen ( 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
2014-04-07 15:46:42 +04:00
while ( nameptr < = namelist_end ) {
2002-11-13 02:20:50 +03:00
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 , ' / ' ) ;
2014-04-08 21:38:33 +04:00
if ( name_end = = NULL ) {
/* Point nameptr at the terminating '\0' */
nameptr + = strlen ( nameptr ) ;
} else {
/* next segment please */
nameptr = name_end + 1 ;
}
2002-11-13 02:20:50 +03:00
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 ;
2014-04-07 15:46:42 +04:00
while ( nameptr < = namelist_end ) {
2002-11-13 02:20:50 +03:00
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 , ' / ' ) ;
2014-04-08 21:38:33 +04:00
if ( name_end ! = NULL ) {
2009-08-25 21:46:37 +04:00
* name_end = ' \0 ' ;
2014-04-08 21:38:33 +04:00
}
2009-08-25 21:46:37 +04:00
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 ;
}
2014-04-08 21:38:33 +04:00
if ( name_end = = NULL ) {
/* Point nameptr at the terminating '\0' */
nameptr + = strlen ( nameptr ) ;
} else {
/* next segment please */
nameptr = name_end + 1 ;
}
2002-11-13 02:20:50 +03:00
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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-05-12 21:57:36 +03:00
bool fcntl_getlock ( int fd , int op , 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 ;
2016-05-12 21:57:36 +03:00
DEBUG ( 8 , ( " fcntl_getlock fd=%d op=%d offset=%.0f count=%.0f type=%d \n " ,
fd , op , ( 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 ;
2016-05-12 21:57:36 +03:00
ret = sys_fcntl_ptr ( fd , op , & 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
}
2016-05-12 22:14:28 +03:00
# if defined(HAVE_OFD_LOCKS)
2019-01-30 20:45:34 +03:00
int map_process_lock_to_ofd_lock ( int op )
2016-05-12 22:14:28 +03:00
{
switch ( op ) {
case F_GETLK :
case F_OFD_GETLK :
op = F_OFD_GETLK ;
break ;
case F_SETLK :
case F_OFD_SETLK :
op = F_OFD_SETLK ;
break ;
case F_SETLKW :
case F_OFD_SETLKW :
op = F_OFD_SETLKW ;
break ;
default :
return - 1 ;
}
return op ;
}
# else /* HAVE_OFD_LOCKS */
2019-01-30 20:45:34 +03:00
int map_process_lock_to_ofd_lock ( int op )
2016-05-12 22:14:28 +03:00
{
return op ;
}
# endif /* HAVE_OFD_LOCKS */
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 + + ) {
2015-01-12 20:12:13 +03:00
const char * nbt_name = my_netbios_names ( n ) ;
2015-08-04 17:50:43 +03:00
if ( strncasecmp_m ( nbt_name , s , MAX_NETBIOSNAME_LEN - 1 ) = = 0 ) {
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 ) ;
}
2016-04-13 18:39:26 +03:00
static const char * remote_arch_strings [ ] = {
[ RA_UNKNOWN ] = " UNKNOWN " ,
[ RA_WFWG ] = " WfWg " ,
[ RA_OS2 ] = " OS2 " ,
[ RA_WIN95 ] = " Win95 " ,
[ RA_WINNT ] = " WinNT " ,
[ RA_WIN2K ] = " Win2K " ,
[ RA_WINXP ] = " WinXP " ,
[ RA_WIN2K3 ] = " Win2K3 " ,
[ RA_VISTA ] = " Vista " ,
[ RA_SAMBA ] = " Samba " ,
[ RA_CIFSFS ] = " CIFSFS " ,
[ RA_WINXP64 ] = " WinXP64 " ,
[ RA_OSX ] = " OSX " ,
} ;
2007-11-10 02:09:16 +03:00
const char * get_remote_arch_str ( void )
{
2016-04-13 18:39:26 +03:00
if ( ra_type > = ARRAY_SIZE ( remote_arch_strings ) ) {
/*
* set_remote_arch ( ) already checks this so ra_type
* should be in the allowed range , but anyway , let ' s
* do another bound check here .
*/
DBG_ERR ( " Remote arch info out of sync [%d] missing \n " , ra_type ) ;
ra_type = RA_UNKNOWN ;
2007-11-10 02:09:16 +03:00
}
2016-04-13 18:39:26 +03:00
return remote_arch_strings [ ra_type ] ;
2007-11-10 02:09:16 +03:00
}
2016-04-13 18:55:11 +03:00
enum remote_arch_types get_remote_arch_from_str ( const char * remote_arch_string )
{
int i ;
for ( i = 0 ; i < ARRAY_SIZE ( remote_arch_strings ) ; i + + ) {
if ( strcmp ( remote_arch_string , remote_arch_strings [ i ] ) = = 0 ) {
return i ;
}
}
return RA_UNKNOWN ;
}
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 )
{
2016-04-13 18:39:26 +03:00
if ( ra_type > = ARRAY_SIZE ( remote_arch_strings ) ) {
/*
* This protects against someone adding values to enum
* remote_arch_types without updating
* remote_arch_strings array .
*/
DBG_ERR ( " Remote arch info out of sync [%d] missing \n " , ra_type ) ;
2002-07-15 14:35:28 +04:00
ra_type = RA_UNKNOWN ;
2016-04-13 18:39:26 +03:00
return ;
2002-07-15 14:35:28 +04:00
}
2003-12-06 00:51:51 +03:00
2016-04-13 18:39:26 +03:00
ra_type = type ;
2007-11-10 02:09:16 +03:00
DEBUG ( 10 , ( " set_remote_arch: Client arch is \' %s \' \n " ,
2016-04-13 18:39:26 +03:00
get_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
2016-04-13 18:42:55 +03:00
# define RA_CACHE_TTL 7*24*3600
static bool remote_arch_cache_key ( const struct GUID * client_guid ,
fstring key )
{
struct GUID_txt_buf guid_buf ;
const char * guid_string = NULL ;
guid_string = GUID_buf_string ( client_guid , & guid_buf ) ;
if ( guid_string = = NULL ) {
return false ;
}
fstr_sprintf ( key , " RA/%s " , guid_string ) ;
return true ;
}
struct ra_parser_state {
bool found ;
enum remote_arch_types ra ;
} ;
2018-10-13 14:41:59 +03:00
static void ra_parser ( const struct gencache_timeout * t ,
DATA_BLOB blob ,
void * priv_data )
2016-04-13 18:42:55 +03:00
{
struct ra_parser_state * state = ( struct ra_parser_state * ) priv_data ;
const char * ra_str = NULL ;
2018-10-13 14:41:59 +03:00
if ( gencache_timeout_expired ( t ) ) {
2016-04-13 18:42:55 +03:00
return ;
}
if ( ( blob . length = = 0 ) | | ( blob . data [ blob . length - 1 ] ! = ' \0 ' ) ) {
DBG_ERR ( " Remote arch cache key not a string \n " ) ;
return ;
}
ra_str = ( const char * ) blob . data ;
DBG_INFO ( " Got remote arch [%s] from cache \n " , ra_str ) ;
state - > ra = get_remote_arch_from_str ( ra_str ) ;
state - > found = true ;
return ;
}
static bool remote_arch_cache_get ( const struct GUID * client_guid )
{
bool ok ;
fstring ra_key ;
struct ra_parser_state state = ( struct ra_parser_state ) {
. found = false ,
. ra = RA_UNKNOWN ,
} ;
ok = remote_arch_cache_key ( client_guid , ra_key ) ;
if ( ! ok ) {
return false ;
}
ok = gencache_parse ( ra_key , ra_parser , & state ) ;
if ( ! ok | | ! state . found ) {
return true ;
}
if ( state . ra = = RA_UNKNOWN ) {
return true ;
}
set_remote_arch ( state . ra ) ;
return true ;
}
static bool remote_arch_cache_set ( const struct GUID * client_guid )
{
bool ok ;
fstring ra_key ;
const char * ra_str = NULL ;
if ( get_remote_arch ( ) = = RA_UNKNOWN ) {
return true ;
}
ok = remote_arch_cache_key ( client_guid , ra_key ) ;
if ( ! ok ) {
return false ;
}
ra_str = get_remote_arch_str ( ) ;
if ( ra_str = = NULL ) {
return false ;
}
ok = gencache_set ( ra_key , ra_str , time ( NULL ) + RA_CACHE_TTL ) ;
if ( ! ok ) {
return false ;
}
return true ;
}
bool remote_arch_cache_update ( const struct GUID * client_guid )
{
bool ok ;
if ( get_remote_arch ( ) = = RA_UNKNOWN ) {
become_root ( ) ;
ok = remote_arch_cache_get ( client_guid ) ;
unbecome_root ( ) ;
return ok ;
}
become_root ( ) ;
ok = remote_arch_cache_set ( client_guid ) ;
unbecome_root ( ) ;
return ok ;
}
bool remote_arch_cache_delete ( const struct GUID * client_guid )
{
bool ok ;
fstring ra_key ;
ok = remote_arch_cache_key ( client_guid , ra_key ) ;
if ( ! ok ) {
return false ;
}
become_root ( ) ;
ok = gencache_del ( ra_key ) ;
unbecome_root ( ) ;
if ( ! ok ) {
return false ;
}
return true ;
}
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
{
2015-03-12 18:00:58 +03:00
TDB_DATA key ;
2011-06-20 11:26:15 +04:00
if ( s = = NULL )
return 0 ;
2015-03-12 18:00:58 +03:00
key = ( TDB_DATA ) { . dptr = discard_const_p ( uint8_t , s ) ,
. dsize = strlen ( s ) } ;
return tdb_jenkins_hash ( & key ) ;
2002-11-13 02:20:50 +03:00
}
1998-08-30 08:31:55 +04:00
/*****************************************************************
2002-11-13 02:20:50 +03:00
Zero a memory area then free it . Used to catch bugs faster .
1998-08-30 08:31:55 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-09-11 05:24:30 +04:00
void zero_free ( void * p , size_t size )
1998-08-30 08:31:55 +04:00
{
memset ( p , 0 , size ) ;
2001-09-17 06:19:44 +04:00
SAFE_FREE ( p ) ;
1998-08-30 08:31:55 +04:00
}
1998-10-04 19:54:04 +04:00
1998-10-05 05:57:03 +04:00
/*****************************************************************
2002-11-13 02:20:50 +03:00
Set our open file limit to a requested max and return the limit .
1998-10-05 05:57:03 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-13 02:20:50 +03:00
1998-10-16 10:16:10 +04:00
int set_maxfiles ( int requested_max )
1998-10-05 05:57:03 +04:00
{
# if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
struct rlimit rlp ;
1999-12-13 16:27:58 +03:00
int saved_current_limit ;
if ( getrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s \n " ,
strerror ( errno ) ) ) ;
/* just guess... */
return requested_max ;
}
/*
2002-11-13 02:20:50 +03:00
* Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1998-10-19 21:32:10 +04:00
* account for the extra fd we need
* as well as the log files and standard
1999-12-13 16:27:58 +03:00
* handles etc . Save the limit we want to set in case
* we are running on an OS that doesn ' t support this limit ( AIX )
* which always returns RLIM_INFINITY for rlp . rlim_max .
*/
2001-04-13 23:33:26 +04:00
/* Try raising the hard (max) limit to the requested amount. */
# if defined(RLIM_INFINITY)
if ( rlp . rlim_max ! = RLIM_INFINITY ) {
int orig_max = rlp . rlim_max ;
if ( rlp . rlim_max < requested_max )
rlp . rlim_max = requested_max ;
/* This failing is not an error - many systems (Linux) don't
support our default request of 10 , 000 open files . JRA . */
if ( setrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 3 , ( " set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s \n " ,
( int ) rlp . rlim_max , strerror ( errno ) ) ) ;
/* Set failed - restore original value from get. */
rlp . rlim_max = orig_max ;
}
}
# endif
/* Now try setting the soft (current) limit. */
1999-12-13 16:27:58 +03:00
saved_current_limit = rlp . rlim_cur = MIN ( requested_max , rlp . rlim_max ) ;
if ( setrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s \n " ,
( int ) rlp . rlim_cur , strerror ( errno ) ) ) ;
/* just guess... */
return saved_current_limit ;
}
if ( getrlimit ( RLIMIT_NOFILE , & rlp ) ) {
DEBUG ( 0 , ( " set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s \n " ,
strerror ( errno ) ) ) ;
/* just guess... */
return saved_current_limit ;
}
# if defined(RLIM_INFINITY)
if ( rlp . rlim_cur = = RLIM_INFINITY )
return saved_current_limit ;
# endif
2002-11-13 02:20:50 +03:00
if ( ( int ) rlp . rlim_cur > saved_current_limit )
1999-12-13 16:27:58 +03:00
return saved_current_limit ;
1998-10-05 05:57:03 +04:00
return rlp . rlim_cur ;
1999-12-13 16:27:58 +03:00
# else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1998-10-16 10:16:10 +04:00
/*
* No way to know - just guess . . .
*/
return requested_max ;
1998-10-05 05:57:03 +04:00
# endif
}
1998-11-11 17:23:55 +03:00
2002-07-15 14:35:28 +04:00
/*****************************************************************
2001-10-22 06:34:41 +04:00
malloc that aborts with smb_panic on fail or zero size .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-12-07 21:25:53 +03:00
void * smb_xmalloc_array ( size_t size , unsigned int count )
2001-10-22 06:34:41 +04:00
{
void * p ;
2007-06-16 01:58:49 +04:00
if ( size = = 0 ) {
smb_panic ( " smb_xmalloc_array: called with zero size " ) ;
}
2004-12-07 21:25:53 +03:00
if ( count > = MAX_ALLOC_SIZE / size ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " smb_xmalloc_array: alloc size too large " ) ;
2004-12-07 21:25:53 +03:00
}
if ( ( p = SMB_MALLOC ( size * count ) ) = = NULL ) {
DEBUG ( 0 , ( " smb_xmalloc_array failed to allocate %lu * %lu bytes \n " ,
( unsigned long ) size , ( unsigned long ) count ) ) ;
2007-06-16 01:58:49 +04:00
smb_panic ( " smb_xmalloc_array: malloc failed " ) ;
2003-03-22 16:47:42 +03:00
}
2001-10-22 06:34:41 +04:00
return p ;
}
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 )
{
static char * ret ;
if ( ret = = NULL ) {
2013-08-03 02:03:39 +04:00
char * name = get_myname ( NULL ) ;
if ( name = = NULL ) {
return NULL ;
}
2011-06-09 09:27:53 +04:00
ret = strupper_talloc ( NULL , name ) ;
talloc_free ( name ) ;
}
return ret ;
}
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
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
2016-10-25 13:28:44 +03:00
return ms_fnmatch_protocol ( pattern , string , Protocol , 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 ;
}
2004-06-22 04:48:59 +04:00
/**********************************************************************
2015-05-12 19:40:29 +03:00
Converts a name to a fully qualified domain name .
Returns true if lookup succeeded , false if not ( then fqdn is set to name )
Uses getaddrinfo ( ) with AI_CANONNAME flag to obtain the official
canonical name of the host . getaddrinfo ( ) may use a variety of sources
including / etc / hosts to obtain the domainname . It expects aliases in
/ etc / hosts to NOT be the FQDN . The FQDN should come first .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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 ;
2015-05-12 19:40:29 +03:00
struct addrinfo hints ;
struct addrinfo * result ;
int s ;
/* Configure hints to obtain canonical name */
memset ( & hints , 0 , sizeof ( struct addrinfo ) ) ;
hints . ai_family = AF_UNSPEC ; /* Allow IPv4 or IPv6 */
hints . ai_socktype = SOCK_DGRAM ; /* Datagram socket */
hints . ai_flags = AI_CANONNAME ; /* Get host's FQDN */
hints . ai_protocol = 0 ; /* Any protocol */
2007-10-16 03:11:48 +04:00
2015-05-12 19:40:29 +03:00
s = getaddrinfo ( name , NULL , & hints , & result ) ;
if ( s ! = 0 ) {
DEBUG ( 1 , ( " getaddrinfo: %s \n " , gai_strerror ( s ) ) ) ;
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
}
2015-05-12 19:40:29 +03:00
full = result - > ai_canonname ;
2007-10-16 03:11:48 +04:00
2015-05-12 19:40:29 +03:00
/* Find out if the FQDN is returned as an alias
2007-10-16 03:11:48 +04:00
* to cope with / etc / hosts files where the first
2015-05-12 19:40:29 +03:00
* name is not the FQDN but the short name .
* getaddrinfo provides no easy way of handling aliases
* in / etc / hosts . Users should make sure the FQDN
* comes first in / etc / hosts . */
if ( full & & ( ! strchr_m ( full , ' . ' ) ) ) {
DEBUG ( 1 , ( " WARNING: your /etc/hosts file may be broken! \n " ) ) ;
DEBUGADD ( 1 , ( " Full qualified domain names (FQDNs) should not be specified \n " ) ) ;
DEBUGADD ( 1 , ( " as an alias in /etc/hosts. FQDN should be the first name \n " ) ) ;
DEBUGADD ( 1 , ( " prior to any aliases. \n " ) ) ;
2007-10-16 03:11:48 +04:00
}
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 " ) ) ;
2014-03-30 06:05:45 +04:00
DEBUGADD ( 1 , ( " Specifying the machine hostname for address 127.0.0.1 may lead \n " ) ) ;
2007-10-16 03:11:48 +04:00
DEBUGADD ( 1 , ( " to Kerberos authentication problems as localhost.localdomain \n " ) ) ;
DEBUGADD ( 1 , ( " may end up being used instead of the real machine FQDN. \n " ) ) ;
}
DEBUG ( 10 , ( " name_to_fqdn: lookup for %s -> %s. \n " , name , full ) ) ;
fstrcpy ( fqdn , full ) ;
2015-05-12 19:40:29 +03:00
freeaddrinfo ( result ) ; /* No longer needed */
2007-10-16 03:11:48 +04:00
return true ;
2004-06-22 04:48:59 +04:00
}
2003-06-25 21:41:05 +04:00
2015-04-25 05:22:21 +03:00
uint32_t map_share_mode_to_deny_mode ( uint32_t share_access , uint32_t private_options )
2005-07-08 08:51:27 +04:00
{
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 ;
}
2015-05-10 02:33:10 +03:00
return ( uint32_t ) - 1 ;
2005-07-08 08:51:27 +04:00
}
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-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 ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-09-20 20:51:15 +03:00
bool split_domain_user ( TALLOC_CTX * mem_ctx ,
2007-12-12 02:42:22 +03:00
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 ) ) ;
2016-09-20 20:51:15 +03:00
if ( * domain = = NULL ) {
return false ;
}
2007-12-12 02:42:22 +03:00
* user = talloc_strdup ( mem_ctx , p + 1 ) ;
2016-09-20 20:51:15 +03:00
if ( * user = = NULL ) {
TALLOC_FREE ( * domain ) ;
return false ;
}
2007-12-12 02:42:22 +03:00
} else {
2016-09-20 20:51:15 +03:00
* domain = NULL ;
2007-12-12 02:42:22 +03:00
* user = talloc_strdup ( mem_ctx , full_name ) ;
2016-09-20 20:51:15 +03:00
if ( * user = = NULL ) {
return false ;
}
2007-12-12 02:42:22 +03:00
}
2016-09-20 20:51:15 +03:00
return true ;
2007-12-12 02:42:22 +03:00
}
2008-05-08 16:16:50 +04:00
/****************************************************************
strip off leading ' \\ ' from a hostname
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
const char * strip_hostname ( const char * s )
{
if ( ! s ) {
return NULL ;
}
if ( strlen_m ( s ) < 3 ) {
return s ;
}
if ( s [ 0 ] = = ' \\ ' ) s + + ;
if ( s [ 0 ] = = ' \\ ' ) s + + ;
return s ;
}
2009-11-14 13:32:00 +03:00
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 ,
2015-04-25 05:22:21 +03:00
uint32_t * paccess_mask ,
uint32_t * pshare_mode ,
uint32_t * pcreate_disposition ,
uint32_t * pcreate_options ,
2011-07-29 18:36:58 +04:00
uint32_t * pprivate_flags )
{
2015-04-25 05:22:21 +03:00
uint32_t access_mask ;
uint32_t share_mode ;
uint32_t create_disposition ;
uint32_t create_options = FILE_NON_DIRECTORY_FILE ;
2011-07-29 18:36:58 +04:00
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 ;
}
2013-11-13 03:32:42 +04:00
2018-03-16 21:01:20 +03:00
/****************************************************************************
Return a root token
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct security_unix_token * root_unix_token ( TALLOC_CTX * mem_ctx )
{
struct security_unix_token * t = NULL ;
t = talloc_zero ( mem_ctx , struct security_unix_token ) ;
if ( t = = NULL ) {
return NULL ;
}
/*
* This is not needed , but lets make it explicit , not implicit .
*/
* t = ( struct security_unix_token ) {
. uid = 0 ,
. gid = 0 ,
. ngroups = 0 ,
. groups = NULL
} ;
return t ;
}
2019-01-09 19:04:34 +03:00
char * utok_string ( TALLOC_CTX * mem_ctx , const struct security_unix_token * tok )
{
char * str ;
uint32_t i ;
str = talloc_asprintf (
mem_ctx ,
" uid=%ju, gid=%ju, % " PRIu32 " groups: " ,
( uintmax_t ) ( tok - > uid ) ,
( uintmax_t ) ( tok - > gid ) ,
tok - > ngroups ) ;
if ( str = = NULL ) {
return NULL ;
}
for ( i = 0 ; i < tok - > ngroups ; i + + ) {
char * tmp ;
tmp = talloc_asprintf_append_buffer (
str , " %ju " , ( uintmax_t ) tok - > groups [ i ] ) ;
if ( tmp = = NULL ) {
TALLOC_FREE ( str ) ;
return NULL ;
}
str = tmp ;
}
return str ;
}
2013-11-13 03:32:42 +04:00
/****************************************************************************
Check that a file matches a particular file type .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool dir_check_ftype ( uint32_t mode , uint32_t dirtype )
{
uint32_t mask ;
/* Check the "may have" search bits. */
if ( ( ( mode & ~ dirtype ) &
( FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_SYSTEM |
FILE_ATTRIBUTE_DIRECTORY ) ) ! = 0 ) {
return false ;
}
/* Check the "must have" bits,
which are the may have bits shifted eight */
/* If must have bit is set, the file/dir can
not be returned in search unless the matching
file attribute is set */
mask = ( ( dirtype > > 8 ) & ( FILE_ATTRIBUTE_DIRECTORY |
FILE_ATTRIBUTE_ARCHIVE |
FILE_ATTRIBUTE_READONLY |
FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_SYSTEM ) ) ; /* & 0x37 */
if ( mask ) {
if ( ( mask & ( mode & ( FILE_ATTRIBUTE_DIRECTORY |
FILE_ATTRIBUTE_ARCHIVE |
FILE_ATTRIBUTE_READONLY |
FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_SYSTEM ) ) ) = = mask ) {
/* check if matching attribute present */
return true ;
} else {
return false ;
}
}
return true ;
}