22#include  <stdbool.h> 
33#include  <stdlib.h> 
44#include  <string.h> 
5- 
5+ #include   <time.h> 
66
77#include  "account_login.h" 
88#include  "helper.h" 
1313#define  STACK_COOKIE_VALUE  (0x012345678)
1414
1515char  created_uname [0x100 ] =  {0 };
16- int  default_persmissions  =  NO_PERMISSION ;
1716char  created_pass [0x100 ] =  {0 };
17+ int  default_persmissions  =  NO_PERMISSION ;
1818
1919int  current_logged_in_permissions  =  0 ;
2020char  currently_logged_in_uname [0x100 ] =  {0 };
21+ 
2122char  secret_admin_password [0x10 ] =  {0 };
2223
24+ // create random password with digits 0-9 
25+ void  set_random_admin_password () {
26+ 	srand (time (NULL ));
27+ 
28+ 	for  (int  i  =  0 ; i  <  sizeof (secret_admin_password ); ++ i )
29+ 	{
30+ 		secret_admin_password [i ] =  rand ()%10  +  '0' ;
31+ 	}
32+ }
2333
2434bool  check_user_auth (char *  uname , char *  passwd , bool *  auth_success ) {
2535	if  ((0  ==  strncmp (uname , created_uname , sizeof (created_uname ))) 
@@ -42,7 +52,6 @@ bool handle_get_currently_logged_in_uname(int client_fd, char* client_str) {
4252bool  handle_login (int  client_fd , char *  client_str ) {
4353	bool  auth_success  =  false;
4454	int  stack_cookie_1 ;
45- 	int  operation  =  0 ;
4655	char  uname [0x100 ];
4756	char  passwd [0x100 ];
4857
@@ -79,8 +88,8 @@ bool handle_login(int client_fd, char* client_str) {
7988bool  handle_login_admin (int  client_fd , char *  client_str ) {
8089	char  passwd [0x100 ];
8190
82- 	if  (!get_str_from_client (client_fd , passwd )) {
83- 		printf ("handle_login_admin error: get_str_from_client  failed\n" );
91+ 	if  (!get_buffer_from_client (client_fd , passwd ,  sizeof ( passwd ) )) {
92+ 		printf ("handle_login_admin error: get_buffer_from_client  failed\n" );
8493		return  false;
8594	}
8695
@@ -91,20 +100,31 @@ bool handle_login_admin(int client_fd, char* client_str) {
91100
92101	memcpy (currently_logged_in_uname , "admin" , 6 );
93102	current_logged_in_permissions  =  ADMIN_PERMISSION ;
103+ 	printf ("admin login success!\n" );
94104	return  true;
95105}
96106
97107
98108bool  handle_logout (int  client_fd , char *  client_str ) {
99109	currently_logged_in_uname [0 ] =  '\0' ;
100110	current_logged_in_permissions  =  0 ;
111+ 	printf ("logout success!\n" );
101112	return  true;
102113}
103114
104115bool  handle_create_user (int  client_fd , char *  client_str ) {
105- 	// stub 
106- 	// read corrently (but no null terminator) into created_uname, created_passwd  
107- 	return  false;
116+ 	if  (!get_buffer_from_client (client_fd , created_uname , sizeof (created_uname ))) {
117+ 		printf ("handle_create_user error: get_buffer_from_client failed\n" );
118+ 		return  false;
119+ 	}
120+ 
121+ 	if  (!get_buffer_from_client (client_fd , created_pass , sizeof (created_pass ))) {
122+ 		printf ("handle_create_user error: get_buffer_from_client failed\n" );
123+ 		return  false;
124+ 	}
125+ 
126+ 	printf ("create user success!\n" );
127+ 	return  true;
108128}
109129
110130bool  handle_admin_run_cmd (int  client_fd , char *  client_str ) {
@@ -125,6 +145,7 @@ bool handle_admin_run_cmd(int client_fd, char* client_str) {
125145		return  false;
126146	}
127147
148+ 	printf ("will run cmd '%s'\n" , cmd );
128149	system (cmd );
129150	return  true;
130151}
0 commit comments