library/src/network/httpd/BP-HTTPdUsers.cc
// Attempts to lookup a user within the table.
Attempts to lookup a user within the table.
Attempts to lookup a user within the table .
P_BP_HTTPD_USER BP_HTTPdUserLookup
(
		P_BP_HTTPD_SESSION httpd,
		char * username,
		char * password,
		char * email,
		char * bio,
		char * role
)
{
	if(!httpd)
		return NULL;
	if(!httpd->system_hreg)
		return NULL;
	char * table_path[10] = {0};
	P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL;
	if(!username)
		return NULL;
	if(BP_StringIsReasonablePrintableString(username, bpstrlen(username), BP_FALSE, BP_FALSE) != BP_TRUE)
		return NULL;
	if(password)
	if(BP_StringIsReasonablePrintableString(password, bpstrlen(password), BP_FALSE, BP_FALSE) != BP_TRUE)
		return NULL;
	if(email)
	if(BP_StringIsReasonablePrintableString(email,    bpstrlen(email),    BP_FALSE, BP_FALSE) != BP_TRUE)
		return NULL;
	if(bio)
	if(BP_StringIsReasonablePrintableString(bio,      bpstrlen(bio),      BP_FALSE, BP_FALSE) != BP_TRUE)
		return NULL;
	if(role)
	if(BP_StringIsReasonablePrintableString(role,     bpstrlen(role),     BP_FALSE, BP_FALSE) != BP_TRUE)
		return NULL;
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(httpd->system_hreg);
	P_BP_HASH_TABLE_KEYED_ENTRY lookup_entry = NULL;
	table_path[0] = "users";
	table_path[1] = username;
	table_path[2] = NULL;
	lookup_entry = BP_HashRegLookupTableEntryByPath(httpd->system_hreg,table_path);
	if(!lookup_entry)
	{
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
	}
	P_BP_HTTPD_USER user = (P_BP_HTTPD_USER) lookup_entry->data;
	%%%%%
	%%%%%
	if(password)
	{
		if(!user->password_hash)
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
		char * salted_pw = BP_HTTPdUserCreateSaltedHash
		(
				user->hash_type,
				user->password_salt,
				password
		);
		if(!salted_pw)
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
		if(strcmp(salted_pw, user->password_hash) != 0)
		{
			if(salted_pw)
				bpfree(salted_pw);
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
		if(salted_pw)
			bpfree(salted_pw);
	}
	%%%%%
	%%%%%
	if(email)
	{
		if(!user->email)
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		if(strcmp(email, user->email) != 0)
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
	}
	if(bio)
	{
		if(!user->bio)
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		if(strcmp(bio, user->bio) != 0)
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
	}
	if(role)
	{
		if(!user->role)
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		if(strcmp(role, user->role) != 0)
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, NULL);
		}
	}
	%%%%%
	%%%%%
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(httpd->system_hreg);
	return user;
}
P_BP_HTTPD_USER BP_HTTPdUserLookup ( P_BP_HTTPD_SESSION httpd , char * username , char * password , char * email , char * bio , char * role ) { if ( ! httpd ) return NULL ; if ( ! httpd -> system_hreg ) return NULL ; char * table_path [ 10 ] = { 0 } ; P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL ; if ( ! username ) return NULL ; if ( BP_StringIsReasonablePrintableString ( username , bpstrlen ( username ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return NULL ; if ( password ) if ( BP_StringIsReasonablePrintableString ( password , bpstrlen ( password ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return NULL ; if ( email ) if ( BP_StringIsReasonablePrintableString ( email , bpstrlen ( email ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return NULL ; if ( bio ) if ( BP_StringIsReasonablePrintableString ( bio , bpstrlen ( bio ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return NULL ; if ( role ) if ( BP_StringIsReasonablePrintableString ( role , bpstrlen ( role ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return NULL ; BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( httpd -> system_hreg ) ; P_BP_HASH_TABLE_KEYED_ENTRY lookup_entry = NULL ; table_path [ 0 ] = " " ; table_path [ 1 ] = username ; table_path [ 2 ] = NULL ; lookup_entry = BP_HashRegLookupTableEntryByPath ( httpd -> system_hreg , table_path ) ; if ( ! lookup_entry ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } P_BP_HTTPD_USER user = ( P_BP_HTTPD_USER ) lookup_entry -> data ; if ( password ) { if ( ! user -> password_hash ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } char * salted_pw = BP_HTTPdUserCreateSaltedHash ( user -> hash_type , user -> password_salt , password ) ; if ( ! salted_pw ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } if ( strcmp ( salted_pw , user -> password_hash ) != 0 ) { if ( salted_pw ) bpfree ( salted_pw ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } if ( salted_pw ) bpfree ( salted_pw ) ; } if ( email ) { if ( ! user -> email ) BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; if ( strcmp ( email , user -> email ) != 0 ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } } if ( bio ) { if ( ! user -> bio ) BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; if ( strcmp ( bio , user -> bio ) != 0 ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } } if ( role ) { if ( ! user -> role ) BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; if ( strcmp ( role , user -> role ) != 0 ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , NULL ) ; } } BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( httpd -> system_hreg ) ; return user ; }
Attempts to lookup a user within the table.
Attempts to lookup a user within the table .
// ensure we have a session structure and a registry to work with // declare dynamic table path // declare the lookup pointer // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Sanity Checks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Ensure we have a username and password.  These are non-optional. // check sanity on non-optional values // check sanity on optional values. // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check if User Exists %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // enter critical section before making changes to the httpd hash // table registry. // declare the lookup entry // set the table path for lookup // attempt to get entry // ensure we can lookup the new entry // set user // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%% Run Password Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // run password check // if there is no password set, and we're looking for a record with a password // exit. // attempt to create a salted hash // if the salted password couldn't be generated, exit // run simple string comparison to determine if hashes match (safe due to // prechecking) // destroy salted password // exit // destroy salted password // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Email / Bio / Role Checks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // ensure we have an email // compare email // ensure we have an bio // compare bio // ensure we have an role // compare role // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Unlock and Exit %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // leave critical section before exiting // return null if the record cannot be found