Skip to content

Commit 0ce0d1c

Browse files
committed
pull before pushing client functions
2 parents 1846ac0 + 0d1fc51 commit 0ce0d1c

File tree

7 files changed

+179
-24
lines changed

7 files changed

+179
-24
lines changed

account_login.c

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,47 @@
77
#include "account_login.h"
88
#include "helper.h"
99

10+
#define ADMIN_PERMISSION (1)
11+
#define NO_PERMISSION (0)
12+
13+
#define STACK_COOKIE_VALUE (0x012345678)
14+
1015
char created_uname[0x100] = {0};
16+
int default_persmissions = NO_PERMISSION;
1117
char created_pass[0x100] = {0};
1218

19+
int current_logged_in_permissions = 0;
1320
char currently_logged_in_uname[0x100] = {0};
21+
char secret_admin_password[0x10] = {0};
1422

15-
bool check_auth(char* uname, char* passwd, bool* auth_success) {
16-
// stub
17-
return false;
18-
}
1923

20-
void get_clientstr_login_details(char* conn_str, char** uname_out, char** passwd_out) {
21-
// stub
24+
bool check_user_auth(char* uname, char* passwd, bool* auth_success) {
25+
if ((0 == strncmp(uname, created_uname, sizeof(created_uname)))
26+
&& (0 == strncmp(passwd, created_pass, sizeof(created_pass)))) {
27+
*auth_success = true;
28+
}
29+
return true;
2230
}
2331

24-
25-
void set_logged_in(char* conn_str, char* uname, char* passwd) {
26-
// stub
32+
void set_login_details(char* conn_str, char* uname, char* passwd, int permissions) {
33+
current_logged_in_permissions = permissions;
34+
memcpy(currently_logged_in_uname, uname, sizeof(currently_logged_in_uname));
2735
}
2836

37+
bool handle_get_currently_logged_in_uname(int client_fd, char* client_str) {
38+
respond_str_to_client(client_fd, currently_logged_in_uname);
39+
return true;
40+
}
2941

30-
// vuln: VERSION1 stackoverflow to overwrite auth_success
3142
bool handle_login(int client_fd, char* client_str) {
3243
bool auth_success = false;
44+
int stack_cookie_1;
45+
int operation = 0;
3346
char uname[0x100];
3447
char passwd[0x100];
3548

49+
stack_cookie_1 = STACK_COOKIE_VALUE;
50+
3651
if (!get_str_from_client(client_fd, uname)) {
3752
printf("handle_login error: get_str_from_client failed\n");
3853
return false;
@@ -43,28 +58,52 @@ bool handle_login(int client_fd, char* client_str) {
4358
return false;
4459
}
4560

46-
if (!check_auth(uname, passwd, &auth_success)) {
47-
printf("check_auth error\n");
61+
if (!check_user_auth(uname, passwd, &auth_success)) {
62+
printf("check_user_auth error\n");
4863
return false;
4964
}
5065

5166
if (auth_success) {
52-
log_verbose("logging in %s as %s\n", client_str, uname);
53-
set_logged_in(client_str, uname, passwd);
67+
log_verbose("no perm logging in %s as %s\n", client_str, uname);
68+
set_login_details(client_str, uname, passwd, default_persmissions);
69+
}
70+
71+
if (STACK_COOKIE_VALUE != stack_cookie_1) {
72+
printf("hacker tried to buffer overflow! :O exploding!\n");
73+
handle_logout(client_fd, client_str);
74+
return false;
5475
}
76+
return true;
77+
}
78+
79+
bool handle_login_admin(int client_fd, char* client_str) {
80+
char passwd[0x100];
5581

56-
// mav: if admin, logg out
82+
if (!get_str_from_client(client_fd, passwd)) {
83+
printf("handle_login_admin error: get_str_from_client failed\n");
84+
return false;
85+
}
5786

87+
if (0 != strcmp(passwd, secret_admin_password)) {
88+
log_verbose("admin login attempted and failed! hacker or typo?!\n");
89+
return false;
90+
}
91+
92+
memcpy(currently_logged_in_uname, "admin", 6);
93+
current_logged_in_permissions = ADMIN_PERMISSION;
5894
return true;
5995
}
6096

97+
6198
bool handle_logout(int client_fd, char* client_str) {
6299
currently_logged_in_uname[0] = '\0';
100+
current_logged_in_permissions = 0;
63101
return true;
64102
}
65103

66104
bool handle_create_user(int client_fd, char* client_str) {
67105
// stub
106+
// read corrently (but no null terminator) into created_uname, created_passwd
68107
return false;
69108
}
70109

@@ -75,6 +114,11 @@ bool handle_admin_run_cmd(int client_fd, char* client_str) {
75114
return false;
76115
}
77116

117+
if (current_logged_in_permissions != ADMIN_PERMISSION) {
118+
printf("hacker detected! and hacker averted ;)\n");
119+
return false;
120+
}
121+
78122
char cmd[0x1000];
79123
if (!get_str_from_client(client_fd, cmd)) {
80124
printf("handle_admin_run_cmd error: get_str_from_client failed\n");

account_login.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdbool.h>
55

66
bool handle_login(int client_fd, char* client_str);
7+
bool handle_get_currently_logged_in_uname(int client_fd, char* client_str);
78
bool handle_admin_run_cmd(int client_fd, char* client_str);
89
bool handle_create_user(int client_fd, char* client_str);
910
bool handle_logout(int client_fd, char* client_str);

game_dispatcher.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99

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

15+
// includes for dispatchers
16+
#include "account_login.h"
17+
1418
typedef bool (*pkt_handler)(int client_fd, char* client_str);
1519

16-
pkt_handler handlers[] = {handle_get_version};
20+
21+
pkt_handler handlers[] = {
22+
handle_get_version,
23+
handle_add_winner,
24+
handle_set_intro,
25+
handle_set_outro,
26+
handle_report_winners
27+
};
1728

1829
void handle_client(int client_fd, char* client_str) {
1930
int pkt_type;
@@ -43,7 +54,7 @@ void handle_client(int client_fd, char* client_str) {
4354

4455
// handle packet type
4556
printf("dispatching\n");
46-
bool dispatch_status = handlers[pkt_type](client_fd, client_str); // vuln: oob run any func lol
57+
bool dispatch_status = handlers[pkt_type](client_fd, client_str);
4758
printf("dispatch_status: %d\n", dispatch_status);
4859
}
4960
end:

high_score.c

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,101 @@
22
#include <string.h>
33
#include <stdlib.h>
44
#include <unistd.h>
5+
#include <arpa/inet.h>
56

67
#include "tictactoe.h"
78
#include "high_score.h"
89

9-
void add_winner(int client_fd, enum Player winner) {
10-
//TODO
11-
}
10+
//void add_winner(int client_fd, enum Player winner) {
11+
// //TODO
12+
//}
13+
14+
#define MAX_BUF_SIZE 0x100
15+
char winners_list[MAX_BUF_SIZE];
16+
char intro[MAX_BUF_SIZE];
17+
char outro[MAX_BUF_SIZE];
18+
19+
void init_msgs()
20+
{
21+
memset(winners_list, sizeof(winners_list), 0);
22+
memset(intro, sizeof(winners_list), 0);
23+
memset(outro, sizeof(winners_list), 0);
24+
25+
strcpy(intro, "Special Congratulations to our Winners:\n" );
26+
strcpy(outro, "And Better Luck to our other participants:\n" );
27+
}
28+
29+
char * handle_read(int client_fd, int * num_read)
30+
{
31+
char * dst_buf = malloc(MAX_BUF_SIZE);
32+
int bytes_read = recv(client_fd, dst_buf, MAX_BUF_SIZE, 0);
33+
if (bytes_read == -1) {
34+
perror("recv from get_str_from_client in len");
35+
}
36+
if (bytes_read == 0) {
37+
printf("client disconnected in str len in get_str_from_client\n");
38+
return false;
39+
}
40+
if (bytes_read != sizeof(dst_buf)) {
41+
printf("client didn't send enough bytes for string length in get_str_from_client\n");
42+
return false;
43+
}
44+
45+
*num_read = bytes_read;
46+
return dst_buf;
47+
}
48+
49+
int display_to_user(int client_fd, char * msg_buf)
50+
{
51+
int err = send(client_fd, msg_buf, strlen(msg_buf), 0);
52+
if (err < 0) {
53+
perror("send len in respond_str_to_client");
54+
}
55+
err = send(client_fd, msg_buf, strlen(msg_buf), 0);
56+
if (err < 0) {
57+
perror("send str in respond_str_to_client");
58+
}
59+
return err;
60+
}
61+
62+
bool handle_add_winner(int client_fd, char* client_str) {
63+
char buff[MAX_BUF_SIZE];
64+
int num_read = 0;
65+
char * user_str = handle_read(client_fd, &num_read);
66+
int bytes_written = snprintf(buff, num_read, "%s\n", user_str);
67+
free(user_str);
68+
69+
bytes_written = snprintf(winners_list, MAX_BUF_SIZE, "%s", buff);
70+
return true;
71+
}
72+
73+
bool handle_set_intro(int client_fd, char* client_str) {
74+
int num_read = 0;
75+
char * user_str = handle_read(client_fd, &num_read);
76+
strncpy(intro, user_str, num_read);
77+
free(user_str);
78+
return true;
79+
}
80+
81+
bool handle_set_outro(int client_fd, char* client_str) {
82+
int num_read = 0;
83+
char * user_str = handle_read(client_fd, &num_read);
84+
strncpy(intro, user_str, num_read);
85+
free(user_str);
86+
return true;
87+
}
88+
89+
bool handle_report_winners(int client_fd, char* client_str) {
90+
char msg_to_user[MAX_BUF_SIZE * 3];
91+
int bytes_written = snprintf(msg_to_user, sizeof(msg_to_user), "%s\n", intro);
92+
bytes_written = snprintf(msg_to_user + bytes_written, sizeof(msg_to_user), "%s\n", winners_list);
93+
bytes_written = snprintf(msg_to_user + bytes_written, sizeof(msg_to_user), "%s\n", outro);
94+
95+
display_to_user(client_fd, msg_to_user);
96+
return true;
97+
}
98+
99+
//bool handle_get_score(int client_fd, char* client_str) {
100+
//
101+
//}
102+

high_score.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include <stdbool.h>
55
#include "tictactoe.h"
66

7-
void add_winner(int client_fd, enum Player winner); //use a username or cookie or session id instead?
7+
//void add_winner(int client_fd, enum Player winner); //use a username or cookie or session id instead?
8+
void init_msgs();
89

9-
#endif
10+
bool handle_add_winner(int client_fd, char* client_str); // Collect information about the winner from the user
11+
bool handle_set_intro(int client_fd, char* client_str);
12+
bool handle_set_outro(int client_fd, char* client_str);
13+
bool handle_report_winners(int client_fd, char* client_str);
14+
15+
#endif

main_game_server.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#include <arpa/inet.h>
99

1010
#include "helper.h"
11+
#include "high_score.h"
1112
#include "game_dispatcher.h"
1213

1314
#define MAX_CONNECTIONS (10)
1415

1516
int main (int argc, char *argv[]) {
1617
uses_assumed_sizes();
18+
init_msgs();
1719

1820
int server_fd, client_fd, err;
1921
struct sockaddr_in server, client;
@@ -63,4 +65,4 @@ int main (int argc, char *argv[]) {
6365
}
6466

6567
return 0;
66-
}
68+
}

tictactoe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ bool handle_get_winner(int client_fd, char* client_str) {
8989

9090
return true;//what to return for status?
9191

92-
}
92+
}

0 commit comments

Comments
 (0)