library/src/network/tor/BP-Tor.cc
// This attempts to populate the torrc buffer with data set via configurators.
This attempts to populate the torrc buffer with data set via configurators.
This attempts to populate the torrc buffer with data set via configurators .
BP_ERROR_T BP_TorGenerateTorrc
(
		P_BP_TOR tor,
		char *torrc_parent_dir_path,
		char    *torrc_file_name
)
{
	if(!tor)
		return ERR_FAILURE;
	if(!torrc_parent_dir_path)
		return ERR_FAILURE;
	if(!BP_StringIsReasonablePrintableString(torrc_parent_dir_path, bpstrlen(torrc_parent_dir_path), BP_FALSE, BP_FALSE))
		return ERR_FAILURE;
	if(!BP_StringIsReasonableForUnixEXT4Filename(torrc_file_name, bpstrlen(torrc_file_name), BP_FALSE))
	{
		printf("\n Error, torrc file name is bad: %s", torrc_file_name);
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForUnixEXT4Directory(torrc_parent_dir_path, bpstrlen(torrc_parent_dir_path), BP_TRUE))
	{
		printf("\n Error, parent directory path is bad?: %s", torrc_parent_dir_path);
		return ERR_FAILURE;
	}
	tor->torrc_parent_dir_path = (char *) ht_strdup(tor->system_hreg, torrc_parent_dir_path);
	tor->torrc_file_name = (char *) ht_strdup(tor->system_hreg, torrc_file_name);
	char *tor_full_file_path = BP_JoinStrings(tor->torrc_parent_dir_path, tor->torrc_file_name);
	tor->torrc_full_file_path = (char *) ht_strdup(tor->system_hreg, tor_full_file_path);
	bpfree(tor_full_file_path);
	P_BP_LINKL tq = BP_CreateLinkL(".", BP_TRUE);
	if(tor->socks_proxy_port)
	{
		char proxy_port_line[128];
		BP_ZERO_STACK(proxy_port_line);
		sprintf((char *) &proxy_port_line, "SocksPort %u\n", tor->socks_proxy_port);
		tq_strdup(tq, (char *) &proxy_port_line, BPLN);
		char socks_bind_addr_line[128];
		BP_ZERO_STACK(socks_bind_addr_line);
		sprintf((char *) &socks_bind_addr_line, "SocksBindAddress %s\n", tor->socks_bind_address);
		tq_strdup(tq, (char *) &socks_bind_addr_line, BPLN);
	}
	if(tor->service_dir)
	{
		size_t service_dir_len = bpstrlen(tor->service_dir);
		char hidden_service_dir_line[128+service_dir_len];
		BP_ZERO_STACK(hidden_service_dir_line);
		sprintf((char *) &hidden_service_dir_line, "HiddenServiceDir %s\n", tor->service_dir);
		tq_strdup(tq, (char *) &hidden_service_dir_line, BPLN);
		char hidden_service_port_line[512];
		BP_ZERO_STACK(hidden_service_port_line);
		sprintf((char *) &hidden_service_port_line, "HiddenServicePort %u %s:%u\n", tor->service_port, tor->bind_local_addr, tor->bind_local_port);
		tq_strdup(tq, (char *) &hidden_service_port_line, BPLN);
	}
	%%
	%%
	if(tor->data_directory)
	{
		size_t data_dir_len = bpstrlen(tor->data_directory);
		char hidden_service_data_dir_line[128+data_dir_len];
		BP_ZERO_STACK(hidden_service_data_dir_line);
		sprintf((char *) &hidden_service_data_dir_line, "DataDirectory %s\n", tor->data_directory);
		tq_strdup(tq, (char *) &hidden_service_data_dir_line, BPLN);
		char hidden_service_control_port_line[512];
		BP_ZERO_STACK(hidden_service_control_port_line);
		sprintf((char *) &hidden_service_control_port_line, "ControlPort %u\n", tor->control_port);
		tq_strdup(tq, (char *) &hidden_service_control_port_line, BPLN);
	}
	%%%
	%%%
	BP_LinkLFlattenJoinStringElements(tq);
	if(tq->flat_buff)
	{
		if(!BP_StringIsReasonablePrintableString((char *) tq->flat_buff, bpstrlen((char *) tq->flat_buff)-1, BP_TRUE, BP_TRUE))
		{
			printf("\n Failed to Generate Torrc: %s", tq->flat_buff);
			BP_DestroyLinkL(tq, BP_TRUE);
			return ERR_FAILURE;
		}
		tor->torrc_buff = (char *) ht_calloc(tor->system_hreg, tq->flat_buff_n, 1);
		memcpy(tor->torrc_buff, tq->flat_buff, tq->flat_buff_n);
		tor->torrc_size = bpstrlen((char *) tq->flat_buff)-1;
	}
	BP_DestroyLinkL(tq, BP_TRUE);
	unlink(tor->torrc_full_file_path);
	if(!BP_FilePutContents(tor->torrc_full_file_path, tor->torrc_buff, tor->torrc_size, BP_FALSE))
	{
		tor->torrc_written_to_disk = BP_FALSE;
		return ERR_FAILURE;
	}
	tor->torrc_written_to_disk = BP_TRUE;
	return ERR_SUCCESS;
}
BP_ERROR_T BP_TorGenerateTorrc ( P_BP_TOR tor , char * torrc_parent_dir_path , char * torrc_file_name ) { if ( ! tor ) return ERR_FAILURE ; if ( ! torrc_parent_dir_path ) return ERR_FAILURE ; if ( ! BP_StringIsReasonablePrintableString ( torrc_parent_dir_path , bpstrlen ( torrc_parent_dir_path ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; if ( ! BP_StringIsReasonableForUnixEXT4Filename ( torrc_file_name , bpstrlen ( torrc_file_name ) , BP_FALSE ) ) { printf ( " \n " , torrc_file_name ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForUnixEXT4Directory ( torrc_parent_dir_path , bpstrlen ( torrc_parent_dir_path ) , BP_TRUE ) ) { printf ( " \n " , torrc_parent_dir_path ) ; return ERR_FAILURE ; } tor -> torrc_parent_dir_path = ( char * ) ht_strdup ( tor -> system_hreg , torrc_parent_dir_path ) ; tor -> torrc_file_name = ( char * ) ht_strdup ( tor -> system_hreg , torrc_file_name ) ; char * tor_full_file_path = BP_JoinStrings ( tor -> torrc_parent_dir_path , tor -> torrc_file_name ) ; tor -> torrc_full_file_path = ( char * ) ht_strdup ( tor -> system_hreg , tor_full_file_path ) ; bpfree ( tor_full_file_path ) ; P_BP_LINKL tq = BP_CreateLinkL ( " " , BP_TRUE ) ; if ( tor -> socks_proxy_port ) { char proxy_port_line [ 128 ] ; BP_ZERO_STACK ( proxy_port_line ) ; sprintf ( ( char * ) & proxy_port_line , " \n " , tor -> socks_proxy_port ) ; tq_strdup ( tq , ( char * ) & proxy_port_line , BPLN ) ; char socks_bind_addr_line [ 128 ] ; BP_ZERO_STACK ( socks_bind_addr_line ) ; sprintf ( ( char * ) & socks_bind_addr_line , " \n " , tor -> socks_bind_address ) ; tq_strdup ( tq , ( char * ) & socks_bind_addr_line , BPLN ) ; } if ( tor -> service_dir ) { size_t service_dir_len = bpstrlen ( tor -> service_dir ) ; char hidden_service_dir_line [ 128 + service_dir_len ] ; BP_ZERO_STACK ( hidden_service_dir_line ) ; sprintf ( ( char * ) & hidden_service_dir_line , " \n " , tor -> service_dir ) ; tq_strdup ( tq , ( char * ) & hidden_service_dir_line , BPLN ) ; char hidden_service_port_line [ 512 ] ; BP_ZERO_STACK ( hidden_service_port_line ) ; sprintf ( ( char * ) & hidden_service_port_line , " \n " , tor -> service_port , tor -> bind_local_addr , tor -> bind_local_port ) ; tq_strdup ( tq , ( char * ) & hidden_service_port_line , BPLN ) ; } if ( tor -> data_directory ) { size_t data_dir_len = bpstrlen ( tor -> data_directory ) ; char hidden_service_data_dir_line [ 128 + data_dir_len ] ; BP_ZERO_STACK ( hidden_service_data_dir_line ) ; sprintf ( ( char * ) & hidden_service_data_dir_line , " \n " , tor -> data_directory ) ; tq_strdup ( tq , ( char * ) & hidden_service_data_dir_line , BPLN ) ; char hidden_service_control_port_line [ 512 ] ; BP_ZERO_STACK ( hidden_service_control_port_line ) ; sprintf ( ( char * ) & hidden_service_control_port_line , " \n " , tor -> control_port ) ; tq_strdup ( tq , ( char * ) & hidden_service_control_port_line , BPLN ) ; } BP_LinkLFlattenJoinStringElements ( tq ) ; if ( tq -> flat_buff ) { if ( ! BP_StringIsReasonablePrintableString ( ( char * ) tq -> flat_buff , bpstrlen ( ( char * ) tq -> flat_buff ) - 1 , BP_TRUE , BP_TRUE ) ) { printf ( " \n " , tq -> flat_buff ) ; BP_DestroyLinkL ( tq , BP_TRUE ) ; return ERR_FAILURE ; } tor -> torrc_buff = ( char * ) ht_calloc ( tor -> system_hreg , tq -> flat_buff_n , 1 ) ; memcpy ( tor -> torrc_buff , tq -> flat_buff , tq -> flat_buff_n ) ; tor -> torrc_size = bpstrlen ( ( char * ) tq -> flat_buff ) - 1 ; } BP_DestroyLinkL ( tq , BP_TRUE ) ; unlink ( tor -> torrc_full_file_path ) ; if ( ! BP_FilePutContents ( tor -> torrc_full_file_path , tor -> torrc_buff , tor -> torrc_size , BP_FALSE ) ) { tor -> torrc_written_to_disk = BP_FALSE ; return ERR_FAILURE ; } tor -> torrc_written_to_disk = BP_TRUE ; return ERR_SUCCESS ; }
This attempts to populate the torrc buffer with data set via configurators.
This attempts to populate the torrc buffer with data set via configurators .
// ensure we have a tor session // ensure the parent dir path is set // ensure string is reasonable/printable // ensure string is reasonable for a ext4 // ensure string is reasonable (runs stat) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Set Path and Filename %%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set torrc parent directory path (after stat check) // set filename // join name and path to create full-path // duplicate full filepath in system hreg // destroy the joined string // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Socks Proxy Configuration %%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create linkl for flattening buffers into one output buffer // add proxy line if necessary // --- add socks port line ----------- // create/zero out buffer to hold proxy port information // set the port // add proxy port line to our buffer // --- add socks bind address line ----------- // create/zero out buffer to hold proxy port information // set the port // add proxy port line to our buffer // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Service Information %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // --- add hidden service dir line ----------- // create/zero out buffer to hold proxy port information // set the service directory // add service directory line // --- add hidden service port line ----------- // create/zero out buffer to hold proxy port information // set the service directory // add service directory line // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Data Directory and Control Port %%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // --- add hidden service data dir line ----------- // create/zero out buffer to hold data dir information // set the service directory // add service directory line // --- add hidden service port line ----------- // create/zero out buffer to hold proxy port information // set the service directory // add service directory line // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Flatten Elements to Create File %%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to flatten string elements // ensure the buffer is flattened // ensure the torrc is reasonable/printable at this point. // allocate space for tor buffer // copy buffer // set the torrc size from the flat buffer calculation // destroy the linkl and free all associated memory // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Write File To Disk %%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to unlink the file before writing // attempt to write contents to disk // set write indicator to false (overwriting any previous change) // set write indicator to true if torrc was written to disk ok // return indicating success