client get/put, data transfer transfer is STRU file, TYPE A, and MODE stream only

This commit is contained in:
Jack Halford 2017-11-10 17:53:37 +01:00
parent bd413e411a
commit 0c523757c6
26 changed files with 396 additions and 63 deletions

View file

@ -31,10 +31,14 @@ INC_DIR = includes/
OBJ_DIR = objs/ OBJ_DIR = objs/
SRC_BASE = \ SRC_BASE = \
client/cli_cd.c\
client/cli_debug.c\ client/cli_debug.c\
client/cli_get.c\
client/cli_help.c\ client/cli_help.c\
client/cli_ls.c\ client/cli_ls.c\
client/cli_pasv.c\ client/cli_pasv.c\
client/cli_put.c\
client/cli_pwd.c\
client/cli_user.c\ client/cli_user.c\
client/client.c\ client/client.c\
client/console_msg.c\ client/console_msg.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */ /* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:11:15 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:25:37 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -58,9 +58,14 @@ extern int g_debug;
t_cmd_map *get_cmd(char *cmd); t_cmd_map *get_cmd(char *cmd);
int ftp_msg(t_ftp *ftp, char **msg);
int ftp_code(t_ftp *ftp); int ftp_code(t_ftp *ftp);
int ftp_send(int sock, char *msg, ...); int ftp_send(int sock, char *msg, ...);
int ftp_sendraw(int sock, char *msg, ...);
int ftp_recv(int sock, char **msg); int ftp_recv(int sock, char **msg);
int ftp_recvraw(int sock, char **msg);
int dconn_init(t_ftp *ftp); int dconn_init(t_ftp *ftp);
int dconn_open(t_ftp *ftp); int dconn_open(t_ftp *ftp);
int dconn_close(t_ftp *ftp); int dconn_close(t_ftp *ftp);
@ -69,6 +74,7 @@ int console_msg(int level, char *str, ...);
int cli_debug(t_ftp *ftp, char **av); int cli_debug(t_ftp *ftp, char **av);
int cli_user(t_ftp *ftp, char **av); int cli_user(t_ftp *ftp, char **av);
int cli_ls(t_ftp *ftp, char **av); int cli_ls(t_ftp *ftp, char **av);
int cli_pwd(t_ftp *ftp, char **av);
int cli_sh(t_ftp *ftp, char **av); int cli_sh(t_ftp *ftp, char **av);
int cli_get(t_ftp *ftp, char **av); int cli_get(t_ftp *ftp, char **av);
int cli_put(t_ftp *ftp, char **av); int cli_put(t_ftp *ftp, char **av);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */ /* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */
/* Updated: 2017/11/09 10:25:23 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:41:01 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,7 +69,9 @@ extern int g_debug;
extern char g_rootdir[PATH_MAX]; extern char g_rootdir[PATH_MAX];
int ftp_send(int sock, char *msg, ...); int ftp_send(int sock, char *msg, ...);
int ftp_sendraw(int sock, char *msg, ...);
int ftp_recv(int sock, char **msg); int ftp_recv(int sock, char **msg);
int ftp_recvraw(int sock, char **msg);
int dconn_open(t_ftp *ftp); int dconn_open(t_ftp *ftp);
int dconn_close(t_ftp *ftp); int dconn_close(t_ftp *ftp);
int console_msg(int level, char *str, ...); int console_msg(int level, char *str, ...);

View file

@ -193,6 +193,7 @@ str/ft_strndup.c\
str/ft_strnequ.c\ str/ft_strnequ.c\
str/ft_strnew.c\ str/ft_strnew.c\
str/ft_strnstr.c\ str/ft_strnstr.c\
str/ft_strpbrk.c\
str/ft_strrchr.c\ str/ft_strrchr.c\
str/ft_strreplace.c\ str/ft_strreplace.c\
str/ft_strrev.c\ str/ft_strrev.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 13:05:53 by jhalford #+# #+# */ /* Created: 2017/02/18 13:05:53 by jhalford #+# #+# */
/* Updated: 2017/03/07 11:36:58 by ariard ### ########.fr */ /* Updated: 2017/11/10 15:58:52 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,8 +67,11 @@ char *ft_strinsert(char *str, char c, int n);
int ft_strappend(char **dst, char *src); int ft_strappend(char **dst, char *src);
char *ft_strbetween(char *start, char *end); char *ft_strbetween(char *start, char *end);
char *ft_strreplace(char **str, char *start, char *end, char *new); char *ft_strreplace(char **str, char *start, char *end, char *new);
char *ft_strtok(char *s, const char *delim); char *ft_strtok(char *s, const char *delim);
char *ft_strtok_single(char *s, const char *delim);
size_t ft_strcspn(char *s, const char *delim); size_t ft_strcspn(char *s, const char *delim);
char *ft_strpbrk(const char *s1, const char *s2);
char *ft_path_notdir(char *path); char *ft_path_notdir(char *path);
int ft_stris(char *str, int (*f)()); int ft_stris(char *str, int (*f)());

View file

@ -6,12 +6,15 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 15:55:33 by jhalford #+# #+# */ /* Created: 2017/03/20 15:55:33 by jhalford #+# #+# */
/* Updated: 2017/03/20 16:00:37 by jhalford ### ########.fr */ /* Updated: 2017/11/10 15:52:55 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
/*
** span the complement of a string
*/
size_t ft_strcspn(char *s, const char *delim) size_t ft_strcspn(char *s, const char *delim)
{ {
char *str; char *str;

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strpbrk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 15:53:38 by jhalford #+# #+# */
/* Updated: 2017/11/10 16:01:43 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** locate multiple characters in a string
*/
char *ft_strpbrk(const char *s1, const char *s2)
{
char *str;
str = (char *)s1;
while (*str)
if (ft_strchr(s2, *str))
return (str);
else
str++;
return (NULL);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */ /* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */
/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* Updated: 2017/11/10 13:34:15 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,12 +6,15 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 18:08:31 by jhalford #+# #+# */ /* Created: 2017/03/07 18:08:31 by jhalford #+# #+# */
/* Updated: 2017/03/22 22:23:33 by wescande ### ########.fr */ /* Updated: 2017/11/10 15:56:57 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
/*
** token generator
*/
char *ft_strtok(char *s, const char *delim) char *ft_strtok(char *s, const char *delim)
{ {
static char *lasts; static char *lasts;
@ -22,11 +25,39 @@ char *ft_strtok(char *s, const char *delim)
if ((ch = *s++) == 0) if ((ch = *s++) == 0)
return (0); return (0);
while (ft_strchr(delim, ch)) while (ft_strchr(delim, ch))
{
if ((ch = *s++) == 0) if ((ch = *s++) == 0)
return (0); return (0);
}
--s; --s;
lasts = s + ft_strcspn(s, delim); lasts = s + ft_strcspn(s, delim);
if (*lasts != 0) if (*lasts != 0)
*lasts++ = 0; *lasts++ = 0;
return (s); return (s);
} }
/*
** also returns empty strings in the case of multiple delimiters
*/
char *ft_strtok_single(char *str, char const *delims)
{
static char *src = NULL;
char *p;
char *ret = 0;
if (str != NULL)
src = str;
if (src == NULL)
return NULL;
if ((p = strpbrk (src, delims)) != NULL) {
*p = 0;
ret = src;
src = ++p;
}
else if (*src)
{
ret = src;
src = NULL;
}
return ret;
}

29
ftp/srcs/client/cli_cd.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 12:42:21 by jhalford #+# #+# */
/* Updated: 2017/11/10 12:43:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_cd(t_ftp *ftp, char **av)
{
char *msg;
(void)av;
if (!av[1] || av[2])
{
console_msg(-1, "usage: cd <dir>");
return (-1);
}
ftp_cmd(ftp, "CWD %s", av[1]);
ftp_recv(ftp->cmd_sock, &msg);
ft_strdel(&msg);
return (0);
}

46
ftp/srcs/client/cli_get.c Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 13:00:28 by jhalford #+# #+# */
/* Updated: 2017/11/10 17:20:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
static int cleanup(int fd, char *filename)
{
close(fd);
unlink(filename);
return (-1);
}
int cli_get(t_ftp *ftp, char **av)
{
char *msg;
int fd;
if (!av[1] || av[2])
return (console_msg(-1, "usage: get <filename>"));
if ((fd = open(av[1], O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
return (-1);
if (dconn_init(ftp) < 0)
return (cleanup(fd, av[1]));
ftp_cmd(ftp, "RETR %s", av[1]);
if (dconn_open(ftp) < 0)
return (cleanup(fd, av[1]));
while (1)
{
if (ftp_recvraw(ftp->d_sock, &msg))
break ;
write(fd, msg, ft_strlen(msg));
ft_strdel(&msg);
}
close(fd);
dconn_close(ftp);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */ /* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */
/* Updated: 2017/11/09 15:16:47 by jhalford ### ########.fr */ /* Updated: 2017/11/10 13:00:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 11:19:41 by jhalford #+# #+# */ /* Created: 2017/11/09 11:19:41 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:53:24 by jhalford ### ########.fr */ /* Updated: 2017/11/10 16:48:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,13 +20,9 @@ int cli_pasv(t_ftp *ftp)
char buf[INET_ADDRSTRLEN]; char buf[INET_ADDRSTRLEN];
ftp_cmd(ftp, "PASV"); ftp_cmd(ftp, "PASV");
ftp_recv(ftp->cmd_sock, &msg); code = ftp_msg(ftp, &msg);
code = ft_atoi(msg);
if (code != 227) if (code != 227)
{
console_msg(2, "PASV failed (%i)", code);
return (-1); return (-1);
}
hostport = ft_strsplit(msg + 4, ','); hostport = ft_strsplit(msg + 4, ',');
ftp->dconn.sin.sin_family = AF_INET; ftp->dconn.sin.sin_family = AF_INET;
ftp->dconn.sin.sin_port = htons(256 * ft_atoi(hostport[4]) ftp->dconn.sin.sin_port = htons(256 * ft_atoi(hostport[4])
@ -41,5 +37,6 @@ int cli_pasv(t_ftp *ftp)
console_msg(1, "remote dconn @ %s:%i", console_msg(1, "remote dconn @ %s:%i",
inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr, buf, sizeof(struct sockaddr_in)), inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr, buf, sizeof(struct sockaddr_in)),
ntohs(ftp->dconn.sin.sin_port)); ntohs(ftp->dconn.sin.sin_port));
ft_strdel(&msg);
return (0); return (0);
} }

49
ftp/srcs/client/cli_put.c Normal file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 16:20:14 by jhalford #+# #+# */
/* Updated: 2017/11/10 17:27:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_put(t_ftp *ftp, char **av)
{
struct stat buf;
char *file;
int fd;
if (!av[1] || av[2])
return (console_msg(-1, "usage: put <filename>"));
if ((fd = open(av[1], O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (-1);
if (!buf.st_size || (file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0)) == MAP_FAILED)
{
close(fd);
return (-1);
}
close(fd);
if (dconn_init(ftp) < 0)
{
munmap(file, buf.st_size);
return (-1);
}
ftp_cmd(ftp, "STOR %s", av[1]);
if (dconn_open(ftp) < 0)
{
munmap(file, buf.st_size);
return (-1);
}
ftp_sendraw(ftp->d_sock, file);
dconn_close(ftp);
munmap(file, buf.st_size);
return (0);
}

24
ftp/srcs/client/cli_pwd.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 12:33:09 by jhalford #+# #+# */
/* Updated: 2017/11/10 12:42:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_pwd(t_ftp *ftp, char **av)
{
char *msg;
(void)av;
ftp_cmd(ftp, "PWD");
ftp_recv(ftp->cmd_sock, &msg);
ft_strdel(&msg);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */ /* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:53:06 by jhalford ### ########.fr */ /* Updated: 2017/11/10 16:34:57 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,10 +16,10 @@ t_cmd_map g_cli_cmd[] =
{ {
{"ls", cli_ls, "list contents of remote directory"}, {"ls", cli_ls, "list contents of remote directory"},
{"user", cli_user, "authentify user by name"}, {"user", cli_user, "authentify user by name"},
/* {"cd", cli_do_cd, "change remote working directory"}, */ {"cd", cli_cd, "change remote working directory"},
/* {"get", cli_do_get, "receive file"}, */ {"get", cli_get, "receive file"},
/* {"put", cli_do_put, "send one file"}, */ {"put", cli_put, "send one file"},
/* {"pwd", cli_do_sh, "print working directory on remote machine"}, */ {"pwd", cli_pwd, "print working directory on remote machine"},
{"quit", NULL, "terminate ftp session and exit"}, {"quit", NULL, "terminate ftp session and exit"},
{"?", cli_help, "print local help information"}, {"?", cli_help, "print local help information"},

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */ /* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
/* Updated: 2017/11/09 13:47:56 by jhalford ### ########.fr */ /* Updated: 2017/11/10 16:17:59 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,32 +6,68 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/09 15:13:39 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:35:44 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_client.h" #include "ftp_client.h"
/*
** stream mode with file structure --> raw data no EOF
*/
int ftp_recvraw(int sock, char **msg)
{
int ret;
char buf[1024];
if ((ret = recv(sock, buf, 1024, 0)) <= 0)
return (-1);
buf[ret] = 0;
console_msg(2, "recv size=%i", ret);
console_msg(2, "message '%s'", buf);
*msg = ft_strdup(buf);
return (0);
}
int ftp_recv(int sock, char **msg) int ftp_recv(int sock, char **msg)
{ {
int ret; int ret;
char buf[1024]; char buf[1024];
if ((ret = recv(sock, buf, 1024, 0)) < 0) if ((ret = recv(sock, buf, 1024, 0)) < 0)
return (ret); return (-1);
if (ret == 0)
return (-1);
console_msg(2, "recv size=%i", ret);
buf[ret] = 0; buf[ret] = 0;
if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r') if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r')
buf[ret - 2] = 0;
else
{ {
console_msg(-1, "recv'd non-crlf message!"); buf[ret - 2] = 0;
return (1); console_msg(2, "message '%s<CRLF>'", buf);
} }
else
console_msg(2, "message '%s'", buf);
*msg = ft_strdup(buf); *msg = ft_strdup(buf);
console_msg(0, "<--- %s", *msg);
return (0); return (0);
} }
int ftp_sendraw(int sock, char *msg, ...)
{
int err;
char *tmp;
va_list ap;
va_start(ap, msg);
ft_vasprintf(&tmp, msg, ap);
console_msg(1, "---> %s", tmp);
if ((err = send(sock, tmp, ft_strlen(tmp), 0)) < 0)
{
return (err);
}
ft_strdel(&tmp);
return (ft_atoi(msg));
}
int ftp_send(int sock, char *msg, ...) int ftp_send(int sock, char *msg, ...)
{ {
int err; int err;
@ -52,13 +88,23 @@ int ftp_send(int sock, char *msg, ...)
return (ft_atoi(msg)); return (ft_atoi(msg));
} }
int ftp_msg(t_ftp *ftp, char **msg)
{
int code;
if (ftp_recv(ftp->cmd_sock, msg) < 0)
return (-1);
console_msg(0, "<--- %s", *msg);
code = ft_atoi(*msg);
return (code);
}
int ftp_code(t_ftp *ftp) int ftp_code(t_ftp *ftp)
{ {
char *msg; char *msg;
int code; int code;
ftp_recv(ftp->cmd_sock, &msg); code = ftp_msg(ftp, &msg);
code = ft_atoi(msg);
ft_strdel(&msg); ft_strdel(&msg);
return (code); return (code);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */ /* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:53:28 by jhalford ### ########.fr */ /* Updated: 2017/11/10 16:40:24 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,7 +69,8 @@ int dconn_close(t_ftp *ftp)
{ {
int code; int code;
code = ftp_code(ftp); if ((code = ftp_code(ftp)) < 0)
return (-1);
if (code == 226) if (code == 226)
{ {
close(ftp->d_sock); close(ftp->d_sock);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 13:36:24 by jhalford #+# #+# */ /* Created: 2017/11/02 13:36:24 by jhalford #+# #+# */
/* Updated: 2017/11/09 09:58:06 by jhalford ### ########.fr */ /* Updated: 2017/11/10 13:00:01 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,12 +14,26 @@
int cmd_cwd(t_ftp *ftp, char **av) int cmd_cwd(t_ftp *ftp, char **av)
{ {
char old[1024];
char buf[1024];
getcwd(old, 1024);
if (chdir(av[1])) if (chdir(av[1]))
{ {
console_msg(1, "chdir(%s) failed errno=%i", av[1], errno); console_msg(1, "chdir(%s) failed errno=%i", av[1], errno);
ftp_ret(ftp, "550 file not found"); ftp_ret(ftp, "550 directory not found");
} }
else else
ftp_ret(ftp, "200 success"); {
return (1); getcwd(buf, 1024);
if (!ft_strstr(buf, old))
{
console_msg(0, "%s -> %s", buf, old);
chdir(old);
ftp_ret(ftp, "550 unauthorized directory");
}
else
ftp_ret(ftp, "200 success");
}
return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 19:22:12 by jhalford #+# #+# */ /* Created: 2017/11/01 19:22:12 by jhalford #+# #+# */
/* Updated: 2017/11/09 15:09:31 by jhalford ### ########.fr */ /* Updated: 2017/11/10 12:20:02 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 15:32:28 by jhalford #+# #+# */ /* Created: 2017/11/02 15:32:28 by jhalford #+# #+# */
/* Updated: 2017/11/09 15:14:57 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:32:16 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,28 +14,35 @@
int cmd_retr(t_ftp *ftp, char **av) int cmd_retr(t_ftp *ftp, char **av)
{ {
int fd; int fd;
struct stat buf; struct stat buf;
char *file; char *file;
int i;
char **split;
if (dconn_open(ftp) < 0)
return (-1);
if ((fd = open(av[1], O_RDONLY)) < 0 || fstat(fd, &buf) < 0) if ((fd = open(av[1], O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (ftp_ret(ftp, "550 file not found / no access")); return (ftp_ret(ftp, "550 file not found / no access"));
if (buf.st_size) if (buf.st_size)
{ {
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) if ((file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE,
== MAP_FAILED) MAP_PRIVATE, fd, 0)) == MAP_FAILED)
{
close(fd);
return (ftp_ret(ftp, "550 mmap failed")); return (ftp_ret(ftp, "550 mmap failed"));
split = ft_strsplit(file, '\n'); }
i = -1; close(fd);
while (split[++i]) if (dconn_open(ftp) < 0)
ftp_send(ftp->d_sock, split[i]); {
ft_sstrfree(split); munmap(file, buf.st_size);
return (-1);
}
ftp_sendraw(ftp->d_sock, file);
dconn_close(ftp);
munmap(file, buf.st_size); munmap(file, buf.st_size);
} }
dconn_close(ftp); else
{
if (dconn_open(ftp) < 0)
return (-1);
dconn_close(ftp);
}
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 16:01:54 by jhalford #+# #+# */ /* Created: 2017/11/02 16:01:54 by jhalford #+# #+# */
/* Updated: 2017/11/09 15:15:02 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:51:39 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,13 +21,10 @@ int cmd_stor(t_ftp *ftp, char **av)
return (-1); return (-1);
if ((fd = open(av[1], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) if ((fd = open(av[1], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
return (ftp_ret(ftp, "550 couldn't open/create file")); return (ftp_ret(ftp, "550 couldn't open/create file"));
while (1) ftp_recvraw(ftp->d_sock, &msg);
{ write(fd, msg, ft_strlen(msg));
if (ftp_recv(ftp->d_sock, &msg)) ft_strdel(&msg);
break ; close(fd);
write(fd, msg, ft_strlen(msg));
ft_strdel(&msg);
}
dconn_close(ftp); dconn_close(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 18:14:51 by jhalford #+# #+# */ /* Created: 2017/11/01 18:14:51 by jhalford #+# #+# */
/* Updated: 2017/11/02 15:49:12 by jhalford ### ########.fr */ /* Updated: 2017/11/10 13:29:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ int cmd_type(t_ftp *ftp, char **av)
} }
else if (av[1][0] == 'I') else if (av[1][0] == 'I')
{ {
ftp_ret(ftp, "200 type A ok"); ftp_ret(ftp, "200 type I ok");
} }
else else
ftp_ret(ftp, "500 parameter unrecognized"); ftp_ret(ftp, "500 parameter unrecognized");

View file

@ -6,12 +6,28 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:58:30 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:51:32 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_server.h" #include "ftp_server.h"
/*
** stream mode with file structure --> raw data no EOF
*/
int ftp_recvraw(int sock, char **msg)
{
int ret;
char buf[1024];
if ((ret = recv(sock, buf, 1024, 0)) <= 0)
return (-1);
buf[ret] = 0;
console_msg(2, "%-5i<--- raw msg '%s' (%i)", getpid(), buf, ret);
*msg = ft_strdup(buf);
return (0);
}
int ftp_recv(int sock, char **msg) int ftp_recv(int sock, char **msg)
{ {
int ret; int ret;
@ -19,12 +35,17 @@ int ftp_recv(int sock, char **msg)
if ((ret = recv(sock, buf, 1024, 0)) < 0) if ((ret = recv(sock, buf, 1024, 0)) < 0)
return (ret); return (ret);
if (ret == 0)
{
console_msg(0, "%-5i client terminated", getpid());
exit(0);
}
buf[ret] = 0; buf[ret] = 0;
if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r') if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r')
buf[ret - 2] = 0; buf[ret - 2] = 0;
else else
{ {
console_msg(-1, "recv'd non-crlf message!"); console_msg(2, "recv'd non-crlf message '%s'", buf);
return (1); return (1);
} }
*msg = ft_strdup(buf); *msg = ft_strdup(buf);
@ -32,6 +53,24 @@ int ftp_recv(int sock, char **msg)
return (0); return (0);
} }
int ftp_sendraw(int sock, char *msg, ...)
{
int err;
char *tmp;
va_list ap;
va_start(ap, msg);
ft_vasprintf(&tmp, msg, ap);
if ((err = send(sock, tmp, ft_strlen(tmp), 0)) < 0)
{
console_msg(1, "%-5i---> send error '%s'", getpid(), tmp);
return (err);
}
console_msg(0, "%-5i---> %s", getpid(), tmp);
ft_strdel(&tmp);
return (ft_atoi(msg));
}
int ftp_send(int sock, char *msg, ...) int ftp_send(int sock, char *msg, ...)
{ {
int err; int err;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */ /* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/09 10:29:39 by jhalford ### ########.fr */ /* Updated: 2017/11/10 17:45:13 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,6 +64,11 @@ int dconn_open(t_ftp *ftp)
int dconn_close(t_ftp *ftp) int dconn_close(t_ftp *ftp)
{ {
ftp_ret(ftp, "226 closing dataconn"); ftp_ret(ftp, "226 closing dataconn");
if (ftp->data_state == DATA_PASV)
{
close(ftp->dconn.sock);
ftp->data_state = DATA_NONE;
}
close(ftp->d_sock); close(ftp->d_sock);
ftp->d_sock = 0; ftp->d_sock = 0;
return (0); return (0);