diff --git a/ftp/includes/ft_p.h b/ftp/includes/ft_p.h index a1441292..b5f55ebc 100644 --- a/ftp/includes/ft_p.h +++ b/ftp/includes/ft_p.h @@ -25,6 +25,7 @@ # include # include +# include typedef struct s_cmd_map t_cmd_map; @@ -46,6 +47,8 @@ enum e_ftp FILENAME_OK = 700, NO_SUCH_FILE, TRANSFER_START, + CD_DIR_NOT_FOUND, + CD_RESTRICTED_DIR, ABORT = 800, ERR_READ, ERR_STAT, @@ -57,6 +60,8 @@ enum e_ftp extern char **g_av; extern int g_debug; extern t_cmd_map g_cli_cmd[]; +extern char g_rootdir[PATH_MAX]; + int ftp_daemon(int sock); int ftp_spawn(int sock); diff --git a/ftp/srcs/cli_do_cd.c b/ftp/srcs/cli_do_cd.c index b448b166..43e954e4 100644 --- a/ftp/srcs/cli_do_cd.c +++ b/ftp/srcs/cli_do_cd.c @@ -6,6 +6,7 @@ int cli_do_cd(int sock, char **av) return (console_msg(-1, "usage: cd ")); if (req_init(sock, REQUEST_CD)) return (1); + console_msg(0, "sending %s", av[1]); write(sock, av[1], ft_strlen(av[1])); return (0); } diff --git a/ftp/srcs/serv_do_cd.c b/ftp/srcs/serv_do_cd.c index 0f8e98b7..39f840fc 100644 --- a/ftp/srcs/serv_do_cd.c +++ b/ftp/srcs/serv_do_cd.c @@ -1,17 +1,34 @@ #include "ft_p.h" +char g_rootdir[PATH_MAX]; + int serv_do_cd(int sock) { - char *oldpwd; char path[MAXLINE]; + char *abspath; int ret; + char *ok; if ((ret = read(sock, path, MAXLINE)) < 0) return (CMD_FAIL); + path[ret] = 0; DG("received 'cd %s' command", path); - oldpwd = getcwd(NULL, 0); - (void)oldpwd; + ok = NULL; + if (!(abspath = realpath(path, ok))) + return (CD_DIR_NOT_FOUND); + + ft_strcpy(path, abspath); + if (!*abspath) + return (CD_DIR_NOT_FOUND); + + ft_strdel(&abspath); + + DG("absolute path is '%s'", path); + DG("root dir is '%s'", g_rootdir); + + if (!ft_strstr(path, g_rootdir)) + return (CD_RESTRICTED_DIR); return(chdir(path) ? CMD_FAIL : CMD_SUCCESS); } diff --git a/ftp/srcs/server.c b/ftp/srcs/server.c index 6027a47c..f253758a 100644 --- a/ftp/srcs/server.c +++ b/ftp/srcs/server.c @@ -15,6 +15,7 @@ #define FTP_SERVER_USAGE "%s " char **g_av = NULL; +char g_rootdir[PATH_MAX]; t_itof g_ftp_cmd[] = { {REQUEST_FILE, serv_do_get}, @@ -94,6 +95,7 @@ int main(int ac, char **av) int sock; g_av = av; + getcwd(g_rootdir, PATH_MAX); if (ac != 2) ft_usage(FTP_SERVER_USAGE, av[0]); port = ft_atoi(av[1]);