library/src/utility/BP-StringUtilities.cc
// This attempts to determine if a string is reasonable with regard to a string which may become a unix filesystem path.  This routine
// will ensure that the string is a reasonable/printable string (using BP_StringIsReasonablePrintableString) but will run secondary
// character checks to make sure a user isn't sending in malformed path data.  The attempt_to_stat_file parameter will attempt to actually
// touch the file on the filesystem if it's enabled (disabled by default)
This attempts to determine if a string is reasonable with regard to a string which may become a unix filesystem path.  This routine
will ensure that the string is a reasonable/printable string (using BP_StringIsReasonablePrintableString) but will run secondary
character checks to make sure a user isn't sending in malformed path data.  The attempt_to_stat_file parameter will attempt to actually
touch the file on the filesystem if it's enabled (disabled by default)
This attempts to determine if a string is reasonable with regard to a string which may become a unix filesystem path . This routine will ensure that the string is a reasonable / printable string ( using BP_StringIsReasonablePrintableString ) but will run secondary character checks to make sure a user isn ' t sending in malformed path data . The attempt_to_stat_file parameter will attempt to actually touch the file on the filesystem if it ' s enabled ( disabled by default )
BP_BOOL BP_StringIsReasonableForUnixEXT4Directory
(
		char * directory_path_to_analyze,
		size_t len,
		BP_BOOL attempt_to_stat_directory
)
{
	if(!directory_path_to_analyze || !len)
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(directory_path_to_analyze, len, BP_FALSE, BP_FALSE))
		return BP_FALSE;
	BP_BUFFER_ANALYSIS_ASCII_CONFIGURATION ascii_config;
	BP_ZERO_STACK(ascii_config);
	ascii_config.count_ascii                      = BP_TRUE;
	P_BP_BUFFER_ANALYSIS buff_analysis = BP_CreateBufferAnalysis
	(
			(unsigned char *) directory_path_to_analyze,
			(size_t) len,
			&ascii_config
	);
	if(!buff_analysis)
		return BP_FALSE;
	if
	(
			buff_analysis->ascii->char_counts['\\'] ||
			buff_analysis->ascii->char_counts['`' ] ||
			buff_analysis->ascii->char_counts['|' ] ||
			buff_analysis->ascii->char_counts['}' ] ||
			buff_analysis->ascii->char_counts['{' ] ||
			buff_analysis->ascii->char_counts[')' ] ||
			buff_analysis->ascii->char_counts['(' ] ||
			buff_analysis->ascii->char_counts['*' ] ||
			buff_analysis->ascii->char_counts['>' ] ||
			buff_analysis->ascii->char_counts['<' ] ||
			buff_analysis->ascii->char_counts[';' ] ||
			buff_analysis->ascii->char_counts[':' ] ||
			buff_analysis->ascii->char_counts[',' ] ||
			buff_analysis->ascii->char_counts['^' ] ||
			buff_analysis->ascii->char_counts['%' ] ||
			buff_analysis->ascii->char_counts['#' ] ||
			buff_analysis->ascii->char_counts['@' ] ||
			buff_analysis->ascii->char_counts['!' ] ||
			buff_analysis->ascii->char_counts['=' ] ||
			buff_analysis->ascii->char_counts['+' ] ||
			buff_analysis->ascii->char_counts['?' ] ||
			buff_analysis->ascii->char_counts['"' ] ||
			buff_analysis->ascii->char_counts['\''] ||
			buff_analysis->ascii->char_counts['$' ] ||
			buff_analysis->ascii->char_counts['&' ]
	)
	{
		BP_DestroyBufferAnalysis(buff_analysis);
		return BP_FALSE;
	}
	BP_DestroyBufferAnalysis(buff_analysis);
	if(attempt_to_stat_directory)
	{
		struct stat dir_stat_info;
		BP_ZERO_STACK(dir_stat_info);
		if(stat(directory_path_to_analyze, &dir_stat_info))
			return BP_FALSE;
		if(!S_ISDIR(dir_stat_info.st_mode))
			return BP_FALSE;
	}
	return BP_TRUE;
}
BP_BOOL BP_StringIsReasonableForUnixEXT4Directory ( char * directory_path_to_analyze , size_t len , BP_BOOL attempt_to_stat_directory ) { if ( ! directory_path_to_analyze || ! len ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( directory_path_to_analyze , len , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; BP_BUFFER_ANALYSIS_ASCII_CONFIGURATION ascii_config ; BP_ZERO_STACK ( ascii_config ) ; ascii_config . count_ascii = BP_TRUE ; P_BP_BUFFER_ANALYSIS buff_analysis = BP_CreateBufferAnalysis ( ( unsigned char * ) directory_path_to_analyze , ( size_t ) len , & ascii_config ) ; if ( ! buff_analysis ) return BP_FALSE ; if ( buff_analysis -> ascii -> char_counts [ ' \\ ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' \' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] || buff_analysis -> ascii -> char_counts [ ' ' ] ) { BP_DestroyBufferAnalysis ( buff_analysis ) ; return BP_FALSE ; } BP_DestroyBufferAnalysis ( buff_analysis ) ; if ( attempt_to_stat_directory ) { struct stat dir_stat_info ; BP_ZERO_STACK ( dir_stat_info ) ; if ( stat ( directory_path_to_analyze , & dir_stat_info ) ) return BP_FALSE ; if ( ! S_ISDIR ( dir_stat_info . st_mode ) ) return BP_FALSE ; } return BP_TRUE ; }
This attempts to determine if a string is reasonable with regard to a string which may become a unix filesystem path.
This attempts to determine if a string is reasonable with regard to a string which may become a unix filesystem path .
// ensure we have a directory and a length // attempt to verify that the string is reasonable first // create ascii configuration // zero out the structure (automagic macro) // set some flags in the structure to count ascii (we already counted invalid with our initial check) // attempt to generate analysis // ensure we h ave a buffer analysis // ensure buffer doesn't have any special chars which could // play havoc with later parsing. // destroy the buffer analysis // return indicating the path is bad // destroy the buffer analysis // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Stat if Desired %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create structure to hold stat // lstat returns err on non-zero, so return false here if we fail a stat // for arbitrary reasons. // ensure that the destination is a directory // return indicating that the path is suitable