library/src/network/httpd/BP-HTTPdUsers.cc
// Adds a user to the httpd database.
Adds a user to the httpd database.
Adds a user to the httpd database .
BP_ERROR_T BP_HTTPdUserAdd
(
		P_BP_HTTPD_SESSION               httpd,
		BP_HTTPD_USER_ACCESS             access,
		BP_HTTPD_USER_TYPE               type,
		BP_HTTPD_USER_PASSWORD_HASH_TYPE hash_type,
		char *                           username,
		char *                           password,
		char *                           password_salt,
		char *                           email,
		char *                           bio,
		char *                           role,
		BP_BOOL                          password_has_been_prehashed,
		P_BP_JSON                        extra_data
)
{
	if(!httpd)
		return ERR_FAILURE;
	if(!httpd->system_hreg)
		return ERR_FAILURE;
	char * table_path[10] = {0};
	P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL;
	if(!username)
		return ERR_FAILURE;
	if(!password)
		return ERR_FAILURE;
	if(BP_StringIsReasonablePrintableString(username, bpstrlen(username), BP_FALSE, BP_FALSE) != BP_TRUE)
		return ERR_FAILURE;
	if(BP_StringIsReasonablePrintableString(password, bpstrlen(password), BP_FALSE, BP_FALSE) != BP_TRUE)
		return ERR_FAILURE;
	if(email)
	if(BP_StringIsReasonablePrintableString(email,    bpstrlen(email),    BP_FALSE, BP_FALSE) != BP_TRUE)
		return ERR_FAILURE;
	if(bio)
	if(BP_StringIsReasonablePrintableString(bio,      bpstrlen(bio),      BP_FALSE, BP_FALSE) != BP_TRUE)
		return ERR_FAILURE;
	if(role)
	if(BP_StringIsReasonablePrintableString(role,     bpstrlen(role),     BP_FALSE, BP_FALSE) != BP_TRUE)
		return ERR_FAILURE;
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(httpd->system_hreg);
	P_BP_HTTPD_USER user = BP_HTTPdUserLookup
	(
			httpd,
			username,
			NULL,
			NULL,
			NULL,
			NULL
	);
	if(user)
	{
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, ERR_FAILURE);
	}
	table_path[0] = "users";
	table_path[1] = NULL;
	BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath
	(
		httpd->system_hreg,
		table_path,
		username,
		BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB,
		NULL,
		sizeof(BP_HTTPD_USER)
	);
	user = BP_HTTPdUserLookup(httpd,username,NULL,NULL,NULL,NULL);
	if(!user)
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, ERR_FAILURE);
	%%%%
	%%%%
	user->access    = access;
	user->type      = type;
	user->username = bpstrdup(username);
	user->password_salt = bpstrdup(password_salt);
	switch(hash_type)
	{
		case BP_HTTPD_USER_PASSWORD_HASH_TYPE_MD5:
			user->hash_type = hash_type;
			break;
		case BP_HTTPD_USER_PASSWORD_HASH_TYPE_SHA1:
			user->hash_type = hash_type;
			break;
		case BP_HTTPD_USER_PASSWORD_HASH_TYPE_SHA256:
			user->hash_type = hash_type;
			break;
		default:
			break;
	}
	if(password_has_been_prehashed == BP_FALSE)
	{
		user->password_hash = BP_HTTPdUserCreateSaltedHash
		(
			user->hash_type,
			user->password_salt,
			password
		);
	}
	else
	{
		user->password_hash = bpstrdup(password);
	}
	if(email)
		user->email = bpstrdup(email);
	if(bio)
		user->bio = bpstrdup(bio);
	if(role)
		user->role = bpstrdup(role);
	if(extra_data)
		user->externally_managed_extra_json_data = extra_data;
	%%%%
	%%%%
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(httpd->system_hreg);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_HTTPdUserAdd ( P_BP_HTTPD_SESSION httpd , BP_HTTPD_USER_ACCESS access , BP_HTTPD_USER_TYPE type , BP_HTTPD_USER_PASSWORD_HASH_TYPE hash_type , char * username , char * password , char * password_salt , char * email , char * bio , char * role , BP_BOOL password_has_been_prehashed , P_BP_JSON extra_data ) { if ( ! httpd ) return ERR_FAILURE ; if ( ! httpd -> system_hreg ) return ERR_FAILURE ; char * table_path [ 10 ] = { 0 } ; P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL ; if ( ! username ) return ERR_FAILURE ; if ( ! password ) return ERR_FAILURE ; if ( BP_StringIsReasonablePrintableString ( username , bpstrlen ( username ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return ERR_FAILURE ; if ( BP_StringIsReasonablePrintableString ( password , bpstrlen ( password ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return ERR_FAILURE ; if ( email ) if ( BP_StringIsReasonablePrintableString ( email , bpstrlen ( email ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return ERR_FAILURE ; if ( bio ) if ( BP_StringIsReasonablePrintableString ( bio , bpstrlen ( bio ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return ERR_FAILURE ; if ( role ) if ( BP_StringIsReasonablePrintableString ( role , bpstrlen ( role ) , BP_FALSE , BP_FALSE ) != BP_TRUE ) return ERR_FAILURE ; BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( httpd -> system_hreg ) ; P_BP_HTTPD_USER user = BP_HTTPdUserLookup ( httpd , username , NULL , NULL , NULL , NULL ) ; if ( user ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , ERR_FAILURE ) ; } table_path [ 0 ] = " " ; table_path [ 1 ] = NULL ; BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath ( httpd -> system_hreg , table_path , username , BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB , NULL , sizeof ( BP_HTTPD_USER ) ) ; user = BP_HTTPdUserLookup ( httpd , username , NULL , NULL , NULL , NULL ) ; if ( ! user ) BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , ERR_FAILURE ) ; user -> access = access ; user -> type = type ; user -> username = bpstrdup ( username ) ; user -> password_salt = bpstrdup ( password_salt ) ; switch ( hash_type ) { case BP_HTTPD_USER_PASSWORD_HASH_TYPE_MD5 : user -> hash_type = hash_type ; break ; case BP_HTTPD_USER_PASSWORD_HASH_TYPE_SHA1 : user -> hash_type = hash_type ; break ; case BP_HTTPD_USER_PASSWORD_HASH_TYPE_SHA256 : user -> hash_type = hash_type ; break ; default : break ; } if ( password_has_been_prehashed == BP_FALSE ) { user -> password_hash = BP_HTTPdUserCreateSaltedHash ( user -> hash_type , user -> password_salt , password ) ; } else { user -> password_hash = bpstrdup ( password ) ; } if ( email ) user -> email = bpstrdup ( email ) ; if ( bio ) user -> bio = bpstrdup ( bio ) ; if ( role ) user -> role = bpstrdup ( role ) ; if ( extra_data ) user -> externally_managed_extra_json_data = extra_data ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( httpd -> system_hreg ) ; return ERR_SUCCESS ; }
Adds a user to the httpd database.
Adds a user to the httpd database .
// 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 all values // check sanity on non-optional values. // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check if User Exists %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // enter critical section before making changes to the httpd hash // table registry. // attempt to lookup user by username only (cannot have duplicate usernames) // if the user already exists, exit immediately // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt to Create New User Structure %%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the initial path // attempt to add data to the table // attempt to lookup the user after adding // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Fill In User Details %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the specifiers 1 to 1 // set username from dup // generate a random ascii character salt // set password based on hash type // if the password isn't pre-hashed, go ahead and try to hash it // create salted hash // set password hash directly // add email/bio/role // add extra data JSON registry if set // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Unlock and Exit %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // leave critical section before exiting // return indicating success