library/src/blockclass/BP-BlockClass.cc
// Returns err success if a user was able to be retrieved via lookup.  User
// information is at that point stored in this->user_info.
Returns err success if a user was able to be retrieved via lookup.  User
information is at that point stored in this->user_info.
Returns err success if a user was able to be retrieved via lookup . User information is at that point stored in this - > user_info .
BP_ERROR_T Block::authUserInDB(char * username, char *password)
{
	if(!this->mysqlSession)
		return ERR_FAILURE;
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;
	if(!username)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(!password)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(this->user_info)
	{
		this->destroyAuthUserData();
	}
	BP_StringUtils * builder = new BP_StringUtils(NULL);
	builder->BuildString
	(
		"SELECT `id`, `first_name`, `last_name`, `email`, `username`, `created`, `role` FROM `users` where `username` = "
	);
	builder->BuildMySQLHexFromBuff(username, bpstrlen(username));
	builder->BuildString(" and `password` = BINARY SHA1(concat((select `salt` from `users` where `username` = ");
	 again
	builder->BuildMySQLHexFromBuff(username, bpstrlen(username));
	builder->BuildString("), ");
	builder->BuildMySQLHexFromBuff(password, bpstrlen(password));
	builder->BuildString(")) LIMIT 0,1");
	if(this->mysqlPrepareQuery(builder->build_str) != ERR_SUCCESS)
	{
		delete builder;
		this->AddErrorToStack("SQL prepare failed when attempting result retrieval.");
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	size_t id = 0;
	char   first_name_res[512] = {0};
	char   last_name_res [512] = {0};
	char   email_res     [512] = {0};
	char   username_res  [512] = {0};
	char   created_res   [512] = {0};
	char   role_res      [512] = {0};
	this->mysqlAddResult(MYSQL_TYPE_LONG,   (char *) &id,             sizeof(id));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &first_name_res, sizeof(first_name_res));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &last_name_res,  sizeof(last_name_res));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &email_res,      sizeof(email_res));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &username_res,   sizeof(username_res));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &created_res,    sizeof(created_res));
	this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) &role_res,       sizeof(role_res));
	this->mysqlBindResults();
	if(this->mysqlExecuteStatement() != ERR_SUCCESS)
	{
		delete builder;
		this->mysqlDestroyStatement();
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	this->mysqlStoreResults();
	this->mysqlFetchResult();
	BP_MySQLDisplaySession(this->mysqlSession);
	size_t * ptr_id = (size_t *) mysqlSession->bind_results_external_ptr[0].buffer;
	if(*ptr_id)
	{
		this->user_info = (P_BP_USER) tq_calloc(this->tq, sizeof(BP_USER), 1, BPLN);
		size_t col_iter = 0;
		memcpy(&this->user_info->user_id, mysqlSession->bind_results_external_ptr[col_iter].buffer, sizeof(this->user_info->user_id));
		col_iter++;
		this->user_info->first_name = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->last_name = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->email = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->username = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->created = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->role = tq_strndup(this->tq, (char *) mysqlSession->bind_results_external_ptr[col_iter].buffer, mysqlSession->bind_results_external_ptr[col_iter].buffer_length, BPLN);
		col_iter++;
		this->user_info->authenticated = BP_TRUE;
	}
	else
	{
		printf("\n ID FAILED TO RETRIEVE OK?!");
		printf("\n [+] AUTH USER HERE2?: %s", first_name_res);
	}
	this->mysqlDestroyStatement();
	delete builder;
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
}
BP_ERROR_T Block :: authUserInDB ( char * username , char * password ) { if ( ! this -> mysqlSession ) return ERR_FAILURE ; BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION ; if ( ! username ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( ! password ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( this -> user_info ) { this -> destroyAuthUserData ( ) ; } BP_StringUtils * builder = new BP_StringUtils ( NULL ) ; builder -> BuildString ( " " ) ; builder -> BuildMySQLHexFromBuff ( username , bpstrlen ( username ) ) ; builder -> BuildString ( " " ) ; builder -> BuildMySQLHexFromBuff ( username , bpstrlen ( username ) ) ; builder -> BuildString ( " " ) ; builder -> BuildMySQLHexFromBuff ( password , bpstrlen ( password ) ) ; builder -> BuildString ( " " ) ; if ( this -> mysqlPrepareQuery ( builder -> build_str ) != ERR_SUCCESS ) { delete builder ; this -> AddErrorToStack ( " " ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } size_t id = 0 ; char first_name_res [ 512 ] = { 0 } ; char last_name_res [ 512 ] = { 0 } ; char email_res [ 512 ] = { 0 } ; char username_res [ 512 ] = { 0 } ; char created_res [ 512 ] = { 0 } ; char role_res [ 512 ] = { 0 } ; this -> mysqlAddResult ( MYSQL_TYPE_LONG , ( char * ) & id , sizeof ( id ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & first_name_res , sizeof ( first_name_res ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & last_name_res , sizeof ( last_name_res ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & email_res , sizeof ( email_res ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & username_res , sizeof ( username_res ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & created_res , sizeof ( created_res ) ) ; this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) & role_res , sizeof ( role_res ) ) ; this -> mysqlBindResults ( ) ; if ( this -> mysqlExecuteStatement ( ) != ERR_SUCCESS ) { delete builder ; this -> mysqlDestroyStatement ( ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } this -> mysqlStoreResults ( ) ; this -> mysqlFetchResult ( ) ; BP_MySQLDisplaySession ( this -> mysqlSession ) ; size_t * ptr_id = ( size_t * ) mysqlSession -> bind_results_external_ptr [ 0 ] . buffer ; if ( * ptr_id ) { this -> user_info = ( P_BP_USER ) tq_calloc ( this -> tq , sizeof ( BP_USER ) , 1 , BPLN ) ; size_t col_iter = 0 ; memcpy ( & this -> user_info -> user_id , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , sizeof ( this -> user_info -> user_id ) ) ; col_iter ++ ; this -> user_info -> first_name = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> last_name = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> email = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> username = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> created = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> role = tq_strndup ( this -> tq , ( char * ) mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer , mysqlSession -> bind_results_external_ptr [ col_iter ] . buffer_length , BPLN ) ; col_iter ++ ; this -> user_info -> authenticated = BP_TRUE ; } else { printf ( " \n " ) ; printf ( " \n " , first_name_res ) ; } this -> mysqlDestroyStatement ( ) ; delete builder ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; }
Returns err success if a user was able to be retrieved via lookup.
Returns err success if a user was able to be retrieved via lookup .
// enter critical section // destroy the user info if it's already set // destroy auth user data // add the front part of the string // add username // add username again // build subquery // add password finally // finish the query building // attempt to prepare query // `id`, `first_name`, `last_name`, `email`, `username`, `created`, `role` // add parameters // bind the results // execute the prepared statement // delete the builder // destroy the statement (this is required) // exit indicating failure // store the results // fetch one result (max one result) // pointer id // if the poitner id is set we know to copy in data // allocate space for user info // create column iterator for copying data into result // `id`, // `first_name`, // `last_name`, // `email`, // `username`, // `created`, // `role` // set authenticated to true // destroy the statement // delete the query builder before exit // return indicating failure if we can't match a user