Skip to content

Commit 46b8919

Browse files
committed
2 parents d3997e6 + 9197b8e commit 46b8919

File tree

6 files changed

+88
-13
lines changed

6 files changed

+88
-13
lines changed

account_login.c

+29-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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"
@@ -13,13 +13,23 @@
1313
#define STACK_COOKIE_VALUE (0x012345678)
1414

1515
char created_uname[0x100] = {0};
16-
int default_persmissions = NO_PERMISSION;
1716
char created_pass[0x100] = {0};
17+
int default_persmissions = NO_PERMISSION;
1818

1919
int current_logged_in_permissions = 0;
2020
char currently_logged_in_uname[0x100] = {0};
21+
2122
char 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

2434
bool 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) {
4252
bool 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) {
7988
bool 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

98108
bool 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

104115
bool 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

110130
bool 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
}

account_login.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
#include <stdbool.h>
55

6-
bool handle_login(int client_fd, char* client_str);
76
bool handle_get_currently_logged_in_uname(int client_fd, char* client_str);
8-
bool handle_admin_run_cmd(int client_fd, char* client_str);
9-
bool handle_create_user(int client_fd, char* client_str);
7+
bool handle_login(int client_fd, char* client_str);
8+
bool handle_login_admin(int client_fd, char* client_str);
109
bool handle_logout(int client_fd, char* client_str);
10+
bool handle_create_user(int client_fd, char* client_str);
11+
bool handle_admin_run_cmd(int client_fd, char* client_str);
12+
13+
void set_random_admin_password();
1114

1215
#endif

game_dispatcher.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99

1010
#include "globals.h"
1111
#include "helper.h"
12-
#include "high_score.h"
1312
#include "game_dispatcher.h"
1413

1514
// includes for dispatchers
1615
#include "account_login.h"
16+
#include "high_score.h"
1717

1818
typedef bool (*pkt_handler)(int client_fd, char* client_str);
1919

2020

2121
pkt_handler handlers[] = {
22+
/* general */
2223
handle_get_version,
24+
25+
/* login */
26+
handle_get_currently_logged_in_uname,
27+
handle_login,
28+
handle_login_admin,
29+
handle_logout,
30+
handle_create_user,
31+
handle_admin_run_cmd,
32+
33+
/* high scores */
2334
handle_add_winner,
2435
handle_set_intro,
2536
handle_set_outro,
26-
handle_report_winners
37+
handle_report_winners,
2738
};
2839

2940
void handle_client(int client_fd, char* client_str) {

helper.c

+37
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ int get_int_from_client(int client_fd) {
7373
return number;
7474
}
7575

76+
bool get_buffer_from_client(int client_fd, char* output, int output_size) {
77+
int client_send_len;
78+
int bytes_read = recv(client_fd, &client_send_len, sizeof(client_send_len), 0);
79+
80+
if (bytes_read == -1) {
81+
perror("recv from get_str_from_client in len");
82+
}
83+
if (bytes_read == 0) {
84+
printf("client disconnected in str len in get_str_from_client\n");
85+
return false;
86+
}
87+
if (bytes_read != sizeof(client_send_len)) {
88+
printf("client didn't send enough bytes for string length in get_str_from_client\n");
89+
return false;
90+
}
91+
92+
client_send_len = ntohl(client_send_len);
93+
if (client_send_len > output_size) {
94+
printf("can't read %d bytes :( (expecting less than %d)\n", client_send_len, output_size);
95+
return false;
96+
}
97+
98+
bytes_read = recv(client_fd, output, client_send_len, 0);
99+
if (bytes_read == -1) {
100+
perror("recv in get_buffer_from_client in data");
101+
}
102+
if (bytes_read == 0) {
103+
printf("client disconnected in get_buffer_from_client\n");
104+
return false;
105+
}
106+
if (bytes_read != client_send_len) {
107+
printf("client didn't send enough bytes in get_buffer_from_client\n");
108+
return false;
109+
}
110+
return true;
111+
}
112+
76113
void uses_assumed_sizes() {
77114
if (sizeof(int) != 4) {
78115
printf("oh no! expected sizeof(int) to be size 4\n");

helper.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void respond_str_to_client(int fd, char* str);
1111

1212
int get_int_from_client(int fd);
1313
bool get_str_from_client(int fd, char* str);
14+
bool get_buffer_from_client(int client_fd, char* output, int output_size);
1415

1516
// C heleprs
1617
void uses_assumed_sizes();

main_game_server.c

+2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
#include "helper.h"
1111
#include "high_score.h"
12+
#include "account_login.h"
1213
#include "game_dispatcher.h"
1314

1415
#define MAX_CONNECTIONS (10)
1516

1617
int main (int argc, char *argv[]) {
1718
uses_assumed_sizes();
1819
init_msgs();
20+
set_random_admin_password();
1921

2022
int server_fd, client_fd, err;
2123
struct sockaddr_in server, client;

0 commit comments

Comments
 (0)