library/src/databases/bpdb/BP-DB.cc
// Attempt to open a BPDB handle using standard creds.
Attempt to open a BPDB handle using standard creds.
Attempt to open a BPDB handle using standard creds .
P_BPDB BP_DBOpen
(
		char * mariadb_host,
		char * mariadb_database,
		size_t mariadb_port,
		char * mariadb_user,
		char * mariadb_pass,
		char * sqlite_db_file,
		char * sqlite_log_file,
		size_t mariadb_connection_pool_count
)
{
	P_BP_HASH_TABLE_REGISTRY system_hreg = BP_CreateHashTableRegistry();
	if(!system_hreg)
		return NULL;
	P_BPDB bpdb = (P_BPDB) ht_calloc(system_hreg, sizeof(BPDB), 1);
	if(!bpdb)
		return NULL;
	bpdb->system_hreg = system_hreg;
	bpdb->uuid = (char *) ht_calloc(bpdb->system_hreg, BP_UUID_LEN+1, 1);
	BP_GenRandomUUID_StoreInBuff(bpdb->uuid);
	char * table_path[4];
	BP_ZERO_STACK(table_path);
	BP_ERROR_T ret_val = BP_HashRegAddTableByPathQuery(system_hreg, "/mariadb_connections");
	if(!ret_val)
	{
		BP_DBClose(bpdb);
		return NULL;
	}
	ret_val = BP_HashRegAddTableByPathQuery(bpdb->system_hreg, "/tables");
	if(!ret_val)
	{
		BP_DBClose(bpdb);
		return NULL;
	}
	ret_val = BP_HashRegAddTableByPathQuery(bpdb->system_hreg, "/pending_queries");
	if(!ret_val)
	{
		BP_DBClose(bpdb);
		return NULL;
	}
	if(!mariadb_host)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_host was NULL.  This value must be a reasonable printable string.");
		return bpdb;
	}
	else if(!BP_StringIsReasonableForMariaDBHostNameDB(mariadb_host, bpstrlen(mariadb_host)))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_host didn't pass filter checks.  Ensure your mariadb_host is a valid hostname.");
		return bpdb;
	}
	if(!mariadb_port)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_port was set to zero.  We do not consider this a valid port.");
		return bpdb;
	}
	else if(mariadb_port >= 65534)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB mariadb_port is set to > 65534.  This is disallowed.");
		return bpdb;
	}
	if(!mariadb_user)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_user was null.  This value must be set.");
		return bpdb;
	}
	else if(!BP_StringIsReasonableForUsernameMariaDB(mariadb_user, bpstrlen(mariadb_user)))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_user did not pass filter tests.  Ensure your username is normal, reasonable, ascii.");
		return bpdb;
	}
	if(!mariadb_pass)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_pass was null.  This value must be set.  If you want an empty password use an empty string (but seriously, use a password).");
		return bpdb;
	}
	else if(!BP_StringIsReasonableForPasswordMariaDB(mariadb_pass, bpstrlen(mariadb_pass)))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_pass did not pass filter tests.  Ensure your password is normal, reasonable, ascii.");
		return bpdb;
	}
	if(!mariadb_connection_pool_count)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter mariadb_connection_pool_count is zero.  This setting must be a non-zero value.");
		return bpdb;
	}
	else if(mariadb_connection_pool_count >= 8)
	{
		BP_DBAddWarningMsg(bpdb, "BPDB parameter mariadb_connection_pool_count is >= 8.  Setting this many connections on one database can be dangerously high.");
	}
	if(!sqlite_db_file)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter sqlite_db_file is NULL.  The sqlite db is used for caching and must be set.");
		return bpdb;
	}
	else if(!BP_StringIsReasonableForDatabaseFileSQLiteDB(sqlite_db_file, bpstrlen(sqlite_db_file), BP_FALSE))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter sqlite_db_file did not pass filter checks.  Ensure the paramter points to a database file.");
		return bpdb;
	}
	if(!sqlite_log_file)
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter sqlite_log_file is NULL.  The sqlite_log_file holds error/notice/warning log information and must be set.");
		return bpdb;
	}
	else if(!BP_StringIsReasonableForDatabaseFileSQLiteDB(sqlite_log_file, bpstrlen(sqlite_log_file), BP_FALSE))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB parameter sqlite_log_file did not pass filter checks.  Ensure the paramter points to a database file.");
		return bpdb;
	}
	%%%%
	%%%%
	size_t connection_pool_iter = 0;
	for
	(
		;
		connection_pool_iter < mariadb_connection_pool_count;
		connection_pool_iter++
	)
	{
		P_BP_MYSQL_SESSION mariadbi = BP_MySQLConnect(mariadb_host, mariadb_user, mariadb_pass, mariadb_database, mariadb_port);
		if(!mariadbi)
		{
			char tmp_err[256];
			BP_ZERO_STACK(tmp_err);
			sprintf((char *)&tmp_err, "While creating connection pools a failure occured on connection number %zu (out of %zu).  Connect attempt failed.", connection_pool_iter, mariadb_connection_pool_count);
			BP_DBAddErrorMsg(bpdb, (char *)&tmp_err);
			return bpdb;
		}
		if(!BP_DBAddMariadbConnectionToPool(bpdb, mariadbi))
		{
			char tmp_err[512];
			BP_ZERO_STACK(tmp_err);
			sprintf((char *)&tmp_err, "While creating connection pools a failure occured on connection number %zu (out of %zu).  Failed to add created connection to our pool. (potentially out of memory?)", connection_pool_iter, mariadb_connection_pool_count);
			BP_DBAddErrorMsg(bpdb, (char *)&tmp_err);
			return bpdb;
		}
	}
	P_BP_DB_SQLITE sqli = BP_SQLiteOpen(sqlite_db_file, sqlite_log_file);
	if(!sqli)
	{
		char tmp_err[bpstrlen(sqlite_db_file)+256];
		BP_ZERO_STACK(tmp_err);
		sprintf((char *)&tmp_err, "Could not open our sqlite_db: %s", sqlite_db_file);
		BP_DBAddErrorMsg(bpdb, (char *)&tmp_err);
		return bpdb;
	}
	bpdb->sqlite  = sqli;
	 after sqlite handler
	printf("\n [+] BPDB Critical section depth after opening sqlite handle: %u", bpdb->system_hreg->critical_section_lock_depth);
	%%%%%%%%%
	%%%%%%%%%
	if(!BP_DBMySQLAutoLoadAllTables(bpdb))
	{
		BP_DBAddErrorMsg(bpdb, "BPDB setup failed.  Specifically, the BP_DBMySQLAutoLoadAllTables() routine failed.  This means that table metadata has failed to load for this connection.  This makes intelligent syntax generation impossible.  Ensure the user for this database connection can view schema elements for the tables/databases being used.");
		return bpdb;
	}
	%%%%%%%%%
	%%%%%%%%%
	printf("\n [+] BPDB Critical section depth after autoloading all tables: %u", bpdb->system_hreg->critical_section_lock_depth);
	bpdb->init_ok = BP_TRUE;
	return bpdb;
}
P_BPDB BP_DBOpen ( char * mariadb_host , char * mariadb_database , size_t mariadb_port , char * mariadb_user , char * mariadb_pass , char * sqlite_db_file , char * sqlite_log_file , size_t mariadb_connection_pool_count ) { P_BP_HASH_TABLE_REGISTRY system_hreg = BP_CreateHashTableRegistry ( ) ; if ( ! system_hreg ) return NULL ; P_BPDB bpdb = ( P_BPDB ) ht_calloc ( system_hreg , sizeof ( BPDB ) , 1 ) ; if ( ! bpdb ) return NULL ; bpdb -> system_hreg = system_hreg ; bpdb -> uuid = ( char * ) ht_calloc ( bpdb -> system_hreg , BP_UUID_LEN + 1 , 1 ) ; BP_GenRandomUUID_StoreInBuff ( bpdb -> uuid ) ; char * table_path [ 4 ] ; BP_ZERO_STACK ( table_path ) ; BP_ERROR_T ret_val = BP_HashRegAddTableByPathQuery ( system_hreg , " " ) ; if ( ! ret_val ) { BP_DBClose ( bpdb ) ; return NULL ; } ret_val = BP_HashRegAddTableByPathQuery ( bpdb -> system_hreg , " " ) ; if ( ! ret_val ) { BP_DBClose ( bpdb ) ; return NULL ; } ret_val = BP_HashRegAddTableByPathQuery ( bpdb -> system_hreg , " " ) ; if ( ! ret_val ) { BP_DBClose ( bpdb ) ; return NULL ; } if ( ! mariadb_host ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( ! BP_StringIsReasonableForMariaDBHostNameDB ( mariadb_host , bpstrlen ( mariadb_host ) ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } if ( ! mariadb_port ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( mariadb_port >= 65534 ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } if ( ! mariadb_user ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( ! BP_StringIsReasonableForUsernameMariaDB ( mariadb_user , bpstrlen ( mariadb_user ) ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } if ( ! mariadb_pass ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( ! BP_StringIsReasonableForPasswordMariaDB ( mariadb_pass , bpstrlen ( mariadb_pass ) ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } if ( ! mariadb_connection_pool_count ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( mariadb_connection_pool_count >= 8 ) { BP_DBAddWarningMsg ( bpdb , " " ) ; } if ( ! sqlite_db_file ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( ! BP_StringIsReasonableForDatabaseFileSQLiteDB ( sqlite_db_file , bpstrlen ( sqlite_db_file ) , BP_FALSE ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } if ( ! sqlite_log_file ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } else if ( ! BP_StringIsReasonableForDatabaseFileSQLiteDB ( sqlite_log_file , bpstrlen ( sqlite_log_file ) , BP_FALSE ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } size_t connection_pool_iter = 0 ; for ( ; connection_pool_iter < mariadb_connection_pool_count ; connection_pool_iter ++ ) { P_BP_MYSQL_SESSION mariadbi = BP_MySQLConnect ( mariadb_host , mariadb_user , mariadb_pass , mariadb_database , mariadb_port ) ; if ( ! mariadbi ) { char tmp_err [ 256 ] ; BP_ZERO_STACK ( tmp_err ) ; sprintf ( ( char * ) & tmp_err , " " , connection_pool_iter , mariadb_connection_pool_count ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_err ) ; return bpdb ; } if ( ! BP_DBAddMariadbConnectionToPool ( bpdb , mariadbi ) ) { char tmp_err [ 512 ] ; BP_ZERO_STACK ( tmp_err ) ; sprintf ( ( char * ) & tmp_err , " " , connection_pool_iter , mariadb_connection_pool_count ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_err ) ; return bpdb ; } } P_BP_DB_SQLITE sqli = BP_SQLiteOpen ( sqlite_db_file , sqlite_log_file ) ; if ( ! sqli ) { char tmp_err [ bpstrlen ( sqlite_db_file ) + 256 ] ; BP_ZERO_STACK ( tmp_err ) ; sprintf ( ( char * ) & tmp_err , " " , sqlite_db_file ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_err ) ; return bpdb ; } bpdb -> sqlite = sqli ; printf ( " \n " , bpdb -> system_hreg -> critical_section_lock_depth ) ; if ( ! BP_DBMySQLAutoLoadAllTables ( bpdb ) ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return bpdb ; } printf ( " \n " , bpdb -> system_hreg -> critical_section_lock_depth ) ; bpdb -> init_ok = BP_TRUE ; return bpdb ; }
Attempt to open a BPDB handle using standard creds.
Attempt to open a BPDB handle using standard creds .
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create Hash Table Registry/BPDB Struct First %%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Note: This routine will always try to return a P_BPDB structure //       even if init fails.  The reason for this is that we use //       the error reporting facilities of BPDB in order to report BPDB //       failures.  Ensure that you check the bpdb->init_ok flag before //       attempting to use any newly created BPDB. // create system registry // allocate bpdb from system hreg // -- after allocation is confirmed, setup bpdb init state --- // set the sytem hreg recursively (easy cleanup/managment) // allocate space for the uuid // generate the uuid // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Default Table Entries %%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create table path and zero out // add table to the db // return null since this is a critical err // add table to the db // return null since this is a critical err // add table here // return null since this is a critical err // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Parameter Checks %%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // --- mariadb checks ------------- // ensure we have a host // ensure we have a port // ensure we have a user // ensure we have a password // ensure we have a mariadb pool count // note: do not return here since this is only a warning // --- sqlite checks ------------ // ensure we have a database file // ensure we have a log file // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Initialize Connection Pool %%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // add entries in hash table // try to establish mariadb pool // create new mariadb session // create temporary error buffer // create error // add error // adds a mariadb connection to the pool of available connections // create temporary error buffer // create error // add error // dbg: display critical section depth //printf("\n [+] BPDB Critical section depth after adding connection pools: %u", bpdb->system_hreg->critical_section_lock_depth); // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Open Handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create new sqlite session // create temporary error buffer // create error // add error // set sqlite handle // dbg: display critical section depth after sqlite handler // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Autoload MySQL Tables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to autoload all tables from the db // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Mark Init Ok and Return New BPDB %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // display critical section depth after autoloading all // mark the bpdb as having initialized ok // return the new database handle