Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 9206c98

Browse files
committed
feat: add path movement control
1 parent adebc95 commit 9206c98

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎include/server.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef struct {
2626
struct sockaddr_in data_sock;
2727
int data_trf_fd;
2828
bool is_auth;
29+
char home[PATH_MAX];
2930
} client_t;
3031

3132
typedef struct {

‎src/commands/dir.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
#include "server.h"
1515

16+
static bool is_path_allowed(char const *home, char const *newpath)
17+
{
18+
char const *real_newpath = realpath(newpath, NULL);
19+
if (real_newpath == NULL)
20+
return false;
21+
return strncmp(home, real_newpath, strlen(home)) == 0;
22+
}
23+
1624
void cmd_pwd_handler(client_t *client, __attribute__((unused)) const char *_)
1725
{
1826
char buffer[512];
@@ -37,6 +45,10 @@ static int check_for_path_errors(const char *args, const char *realpath_buff,
3745
write_msg_to_client(client->control_fd, "550 Error occurred.");
3846
return -1;
3947
}
48+
if (!is_path_allowed(client->home, realpath_buff)) {
49+
write_msg_to_client(client->control_fd, "550 Requested action not taken.");
50+
return -1;
51+
}
4052
return 0;
4153
}
4254

‎src/server.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ static client_t *get_empty_client_slot(server_t *server)
2727
return NULL;
2828
}
2929

30+
static void init_client(client_t *client, const server_t *server)
31+
{
32+
client->data_fd = -1;
33+
client->data_trf_fd = -1;
34+
strcpy(client->username, "");
35+
strcpy(client->home, server->anonymous_default_path);
36+
}
37+
3038
static void searching_new_clients(server_t *server)
3139
{
3240
const struct sockaddr_in client_addr = {0};
@@ -41,9 +49,7 @@ static void searching_new_clients(server_t *server)
4149
client = get_empty_client_slot(server);
4250
client->control_fd = accept(server->server_fd,
4351
(struct sockaddr*) &client_addr, &client_len);
44-
client->data_fd = -1;
45-
client->data_trf_fd = -1;
46-
strcpy(client->username, "");
52+
init_client(client, server);
4753
strcpy(client->currPath, realpath(server->anonymous_default_path,
4854
NULL));
4955
printf("[INFO] New client connection\n");

0 commit comments

Comments
 (0)