looks good. need to come back for norme and final testing

This commit is contained in:
Jack Halford 2017-07-26 18:02:14 +02:00
parent 99972b4f8e
commit e10ab459dc
4 changed files with 28 additions and 3 deletions

View file

@ -25,6 +25,7 @@
# include <sys/mman.h>
# include <sys/wait.h>
# include <limits.h>
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);

View file

@ -6,6 +6,7 @@ int cli_do_cd(int sock, char **av)
return (console_msg(-1, "usage: cd <path>"));
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);
}

View file

@ -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);
}

View file

@ -15,6 +15,7 @@
#define FTP_SERVER_USAGE "%s <port>"
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]);