library/src/utility/BP-StringUtilities.cc
// Attempts to read a password or other sensitive information using a mask char.  To
// disable mask characters getting output at all, you can supply a null byte.  Returned
// value is bpcalloced buffer and must be bpfreed().
Attempts to read a password or other sensitive information using a mask char.  To
disable mask characters getting output at all, you can supply a null byte.  Returned
value is bpcalloced buffer and must be bpfreed().
Attempts to read a password or other sensitive information using a mask char . To disable mask characters getting output at all you can supply a null byte . Returned value is bpcalloced buffer and must be bpfreed () .
char * BP_GetPasswordFromTerminal
(
	FILE * read_from,
	size_t max_read_len,
	char char_to_mask_output_with
)
{
	if(!read_from)
		return NULL;
	if(!max_read_len)
		return NULL;
	struct termios preserved_term_settings;
	struct termios modified_term_settings;
	BP_ZERO_STACK(preserved_term_settings);
	BP_ZERO_STACK(modified_term_settings);
	if (tcgetattr (0, &preserved_term_settings))
	{
		printf("\n [!!] tcgetattr failed in BP_GetPasswordFromTerminal.  Exiting prematurely.");
		return NULL;
	}
	memcpy (&modified_term_settings, &preserved_term_settings, sizeof(struct termios));
	modified_term_settings.c_lflag &= ~(ICANON | ECHO);
	modified_term_settings.c_cc[VTIME] = 0;
	modified_term_settings.c_cc[VMIN] = 1;
	if (tcsetattr (0, TCSANOW, &modified_term_settings))
	{
		printf("\n [!!] tcsetattr failed in BP_GetPasswordFromTerminal.  Exiting prematurely.");
		return NULL;
	}
	char read_char = 0x00;
	size_t curr_read_len = 0;
	char *ret_buff = NULL;
	while(1)
	{
		read_char = fgetc(read_from);
		if(read_char == EOF)
			break;
		curr_read_len++;
		if(read_char == '\n')
			break;
		if(read_char == '\r')
			break;
		ret_buff = (char *) bprealloc(ret_buff, curr_read_len+1);
		ret_buff[curr_read_len-1] = read_char;
		if(!BP_StringIsReasonablePrintableString(ret_buff, bpstrlen(ret_buff), BP_FALSE, BP_FALSE))
		{
			tcsetattr (0, TCSANOW, &preserved_term_settings);
			bpfree(ret_buff);
			return NULL;
		}
		if(char_to_mask_output_with)
			fputc (char_to_mask_output_with, stdout);
		if(curr_read_len >= max_read_len)
		{
			break;
		}
	}
	tcsetattr (0, TCSANOW, &preserved_term_settings);
	if(!ret_buff)
	{
		return NULL;
	}
	 at final check point
	if(!BP_StringIsReasonablePrintableString(ret_buff, bpstrlen(ret_buff), BP_FALSE, BP_FALSE))
	{
		bpfree(ret_buff);
		return NULL;
	}
	return ret_buff;
}
char * BP_GetPasswordFromTerminal ( FILE * read_from , size_t max_read_len , char char_to_mask_output_with ) { if ( ! read_from ) return NULL ; if ( ! max_read_len ) return NULL ; struct termios preserved_term_settings ; struct termios modified_term_settings ; BP_ZERO_STACK ( preserved_term_settings ) ; BP_ZERO_STACK ( modified_term_settings ) ; if ( tcgetattr ( 0 , & preserved_term_settings ) ) { printf ( " \n " ) ; return NULL ; } memcpy ( & modified_term_settings , & preserved_term_settings , sizeof ( struct termios ) ) ; modified_term_settings . c_lflag &= ~ ( ICANON | ECHO ) ; modified_term_settings . c_cc [ VTIME ] = 0 ; modified_term_settings . c_cc [ VMIN ] = 1 ; if ( tcsetattr ( 0 , TCSANOW , & modified_term_settings ) ) { printf ( " \n " ) ; return NULL ; } char read_char = 0x00 ; size_t curr_read_len = 0 ; char * ret_buff = NULL ; while ( 1 ) { read_char = fgetc ( read_from ) ; if ( read_char == EOF ) break ; curr_read_len ++ ; if ( read_char == ' \n ' ) break ; if ( read_char == ' \r ' ) break ; ret_buff = ( char * ) bprealloc ( ret_buff , curr_read_len + 1 ) ; ret_buff [ curr_read_len - 1 ] = read_char ; if ( ! BP_StringIsReasonablePrintableString ( ret_buff , bpstrlen ( ret_buff ) , BP_FALSE , BP_FALSE ) ) { tcsetattr ( 0 , TCSANOW , & preserved_term_settings ) ; bpfree ( ret_buff ) ; return NULL ; } if ( char_to_mask_output_with ) fputc ( char_to_mask_output_with , stdout ) ; if ( curr_read_len >= max_read_len ) { break ; } } tcsetattr ( 0 , TCSANOW , & preserved_term_settings ) ; if ( ! ret_buff ) { return NULL ; } if ( ! BP_StringIsReasonablePrintableString ( ret_buff , bpstrlen ( ret_buff ) , BP_FALSE , BP_FALSE ) ) { bpfree ( ret_buff ) ; return NULL ; } return ret_buff ; }
Attempts to read a password or other sensitive information using a mask char.
Attempts to read a password or other sensitive information using a mask char .
// run basic null checks // termios preserving structures // zero out termios // preserve the old terminal information // duplicate // disable keyboard echo // set basic flags // set the terminal information using the revised terminal settings // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Start Terminal Read Loop %%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create var for reading characters one at a time // current read length // buffer to return to caller // iterate through characters till we hit our cap // attempt to read a character from stdin // ensure we haven't hit the end of file char // increment the read length // break if the character is a newline or carriage return // reallocate the buffer // set the read character // ensure string is reasonable/printable // set the terminal information using the revised terminal settings // printf("\n [!!] BP_GetPasswordFromTerminal somehow read input from fgetc which translated into an unreasonable string.  Likely tampering detected. Please ensure your input string for this routine is reasonable."); // write mask character to stdout if it's non-null // break if we hit our max read length // always reset terminal settings // simply return null if the user only enters a newline // ensure string is reasonable/printable at final check point // printf("\n [!!] BP_GetPasswordFromTerminal somehow read input from fgetc which translated into an unreasonable string.  Likely tampering detected. Please ensure your input string for this routine is reasonable."); // return the string