je vais boire une 🍺
This commit is contained in:
parent
cd99c04ca5
commit
20791a4180
25 changed files with 176 additions and 713 deletions
47
ftp/Makefile
47
ftp/Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2016/08/29 21:32:58 by wescande #+# #+# #
|
||||
# Updated: 2017/04/03 18:19:23 by jhalford ### ########.fr #
|
||||
# Updated: 2017/11/01 17:07:12 by jhalford ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
@ -30,46 +30,38 @@ SRC_DIR = srcs/
|
|||
INC_DIR = includes/
|
||||
OBJ_DIR = objs/
|
||||
|
||||
SERVER_OBJ = $(OBJ_DIR)server.o
|
||||
CLIENT_OBJ = $(OBJ_DIR)client.o $(OBJ_DIR)cli_do_help.o
|
||||
|
||||
SRC_BASE = \
|
||||
cli_do_cd.c\
|
||||
cli_do_debug.c\
|
||||
cli_do_get.c\
|
||||
cli_do_help.c\
|
||||
cli_do_local.c\
|
||||
cli_do_put.c\
|
||||
cli_do_sh.c\
|
||||
client.c\
|
||||
client/client.c\
|
||||
console_msg.c\
|
||||
read_req.c\
|
||||
req_init.c\
|
||||
serv_do_cd.c\
|
||||
serv_do_get.c\
|
||||
serv_do_put.c\
|
||||
serv_do_sh.c\
|
||||
server.c
|
||||
crlf.c\
|
||||
login.c\
|
||||
server/cmd_list.c\
|
||||
server/cmd_port.c\
|
||||
server/cmd_pwd.c\
|
||||
server/cmd_type.c\
|
||||
server/server.c
|
||||
|
||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||
OBJS := $(filter-out $(SERVER_OBJ) $(CLIENT_OBJ), $(OBJS))
|
||||
OBJS = $(addprefix $(OBJ_DIR), $($(notdir SRC_BASE):.c=.o))
|
||||
NB = $(words $(SRC_BASE))
|
||||
INDEX = 0
|
||||
|
||||
SERVER_OBJ := $(filter-out $(OBJ_DIR)client/%,$(OBJS))
|
||||
CLIENT_OBJ := $(filter-out $(OBJ_DIR)server/%,$(OBJS))
|
||||
|
||||
all :
|
||||
@make -C $(LIBFT_DIR)
|
||||
@make -j $(NAME)
|
||||
@make $(NAME)
|
||||
|
||||
server: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(SERVER_OBJ)
|
||||
@$(CC) $(FLAGS) $(OBJS) -o $@ \
|
||||
server: $(LIBFT_LIB) $(OBJ_DIR) $(SERVER_OBJ)
|
||||
@$(CC) $(FLAGS) -o $@ \
|
||||
-I $(INC_DIR) \
|
||||
-I $(LIBFT_INC) \
|
||||
$(SERVER_OBJ) $(LIBFT_LIB)
|
||||
@printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n"
|
||||
|
||||
client: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ)
|
||||
@$(CC) $(FLAAGS) $(OBJS) -o $@ \
|
||||
client: $(LIBFT_LIB) $(OBJ_DIR) $(CLIENT_OBJ)
|
||||
@$(CC) $(FLAGS) -o $@ \
|
||||
-I $(INC_DIR) \
|
||||
-I $(LIBFT_INC) \
|
||||
$(CLIENT_OBJ) $(LIBFT_LIB) \
|
||||
|
|
@ -80,7 +72,8 @@ $(LIBFT_LIB):
|
|||
@make -C $(LIBFT_DIR)
|
||||
|
||||
$(OBJ_DIR) :
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
@mkdir -p $@
|
||||
@mkdir -p $(dir $(OBJS))
|
||||
|
||||
$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
|
||||
@$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB)))))
|
||||
|
|
|
|||
0
ftp/data/file1.txt
Normal file
0
ftp/data/file1.txt
Normal file
|
|
@ -1,23 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_local.c :+: :+: :+: */
|
||||
/* ft_p.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:02:12 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:02:35 by jhalford ### ########.fr */
|
||||
/* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/01 19:14:28 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
#ifndef FTP_H
|
||||
# define FTP_H
|
||||
|
||||
int cli_do_local(int sock, char **av)
|
||||
# define MAXLINE 256
|
||||
|
||||
enum e_ftp
|
||||
{
|
||||
char *command;
|
||||
REQUEST_FILE = 100,
|
||||
REQUEST_PUT,
|
||||
REQUEST_SH,
|
||||
REQUEST_CD,
|
||||
CMD_NOT_SUPPORTED = 150,
|
||||
CMD_SUPPORTED = 160,
|
||||
FILENAME_OK = 700,
|
||||
NO_SUCH_FILE,
|
||||
TRANSFER_START,
|
||||
CD_DIR_NOT_FOUND,
|
||||
CD_RESTRICTED_DIR,
|
||||
ABORT = 800,
|
||||
ERR_READ,
|
||||
ERR_STAT,
|
||||
ERR_MMAP,
|
||||
CMD_SUCCESS = 900,
|
||||
CMD_FAIL,
|
||||
};
|
||||
|
||||
(void)sock;
|
||||
command = ft_strsepjoin(av + 1, ' ');
|
||||
system(command);
|
||||
return (-1);
|
||||
}
|
||||
#include "ftp_server.h"
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,33 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_p.h :+: :+: :+: */
|
||||
/* ftp_client.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */
|
||||
/* Updated: 2017/04/03 18:42:28 by jhalford ### ########.fr */
|
||||
/* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/01 17:12:03 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_P_H
|
||||
# define FT_P_H
|
||||
|
||||
# define FTP_SERVER_USAGE "%s <port>"
|
||||
# define FTP_CLIENT_USAGE "%s <addr> <port>"
|
||||
|
||||
# define MAXLINE 256
|
||||
#ifndef FTP_CLIENT
|
||||
# define FTP_CLIENT
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdio.h>
|
||||
# include <termios.h>
|
||||
# include <readline/readline.h>
|
||||
|
||||
# include <sys/mman.h>
|
||||
# include <sys/wait.h>
|
||||
# include <limits.h>
|
||||
# include "ftp.h"
|
||||
|
||||
# include <pwd.h>
|
||||
# define FTP_CLIENT_USAGE "%s <addr> <port>"
|
||||
|
||||
typedef struct s_cmd_map t_cmd_map;
|
||||
|
||||
|
|
@ -38,48 +33,9 @@ struct s_cmd_map
|
|||
char *help;
|
||||
};
|
||||
|
||||
enum e_ftp
|
||||
{
|
||||
REQUEST_FILE = 100,
|
||||
REQUEST_PUT,
|
||||
REQUEST_SH,
|
||||
REQUEST_CD,
|
||||
CMD_NOT_SUPPORTED = 150,
|
||||
CMD_SUPPORTED = 160,
|
||||
FILENAME_OK = 700,
|
||||
NO_SUCH_FILE,
|
||||
TRANSFER_START,
|
||||
CD_DIR_NOT_FOUND,
|
||||
CD_RESTRICTED_DIR,
|
||||
ABORT = 800,
|
||||
ERR_READ,
|
||||
ERR_STAT,
|
||||
ERR_MMAP,
|
||||
CMD_SUCCESS = 900,
|
||||
CMD_FAIL,
|
||||
};
|
||||
|
||||
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);
|
||||
int ftp_cmd(int sock, int req);
|
||||
|
||||
int ftp_send(int sock, char *msg, size_t size);
|
||||
int ftp_recv(int sock, char buf[], size_t size);
|
||||
|
||||
int serv_do_get(int sock);
|
||||
int serv_do_put(int sock);
|
||||
int serv_do_sh(int sock);
|
||||
int serv_do_cd(int sock);
|
||||
|
||||
int read_req(int sock);
|
||||
t_cmd_map *get_cmd(char *cmd);
|
||||
int console_msg(int level, char *str, ...);
|
||||
|
||||
int cli_do_sh(int sock, char **av);
|
||||
int cli_do_get(int sock, char **av);
|
||||
|
|
@ -90,6 +46,4 @@ int cli_do_help(int sock, char **av);
|
|||
int cli_do_debug(int sock, char **av);
|
||||
int cli_do_local(int sock, char **av);
|
||||
|
||||
int req_init(int sock, int req);
|
||||
|
||||
#endif
|
||||
59
ftp/includes/ftp_server.h
Normal file
59
ftp/includes/ftp_server.h
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ftp_server.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/01 19:23:02 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FTP_SERVER
|
||||
# define FTP_SERVER
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdio.h>
|
||||
# include <sys/mman.h>
|
||||
# include <limits.h>
|
||||
# include <pwd.h>
|
||||
|
||||
# include <arpa/inet.h>
|
||||
|
||||
# include "ftp.h"
|
||||
|
||||
# define FTP_SERVER_USAGE "%s <port>"
|
||||
|
||||
typedef struct s_ftp t_ftp;
|
||||
typedef struct s_ftp_cmd t_ftp_cmd;
|
||||
|
||||
struct s_ftp
|
||||
{
|
||||
struct sockaddr_in data_sin;
|
||||
int data_sock;
|
||||
int cmd_sock;
|
||||
};
|
||||
|
||||
struct s_ftp_cmd
|
||||
{
|
||||
char *name;
|
||||
int (*f)();
|
||||
};
|
||||
|
||||
|
||||
extern char **g_av;
|
||||
extern int g_debug;
|
||||
extern char g_rootdir[PATH_MAX];
|
||||
|
||||
int ftp_send(t_ftp *ftp, char *msg, ...);
|
||||
int ftp_recv(t_ftp *ftp, char **msg);
|
||||
int console_msg(int level, char *str, ...);
|
||||
|
||||
int ftp_login(t_ftp *ftp);
|
||||
int cmd_pwd(t_ftp *ftp, char **av);
|
||||
int cmd_port(t_ftp *ftp, char **av);
|
||||
int cmd_type(t_ftp *ftp, char **av);
|
||||
int cmd_list(t_ftp *ftp, char **av);
|
||||
|
||||
#endif
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */
|
||||
/* Updated: 2017/05/15 17:37:29 by ariard ### ########.fr */
|
||||
/* Updated: 2017/11/01 16:29:49 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */
|
||||
/* Updated: 2017/04/02 15:22:13 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/11/01 17:50:43 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/20 15:52:57 by jhalford #+# #+# */
|
||||
/* Updated: 2017/04/02 15:22:20 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/11/01 17:45:23 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/25 01:38:51 by wescande ### ########.fr */
|
||||
/* Updated: 2017/11/01 18:51:13 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/05 11:53:36 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/22 23:19:24 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/11/01 18:51:28 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:02:04 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:02:04 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int cli_do_cd(int sock, char **av)
|
||||
{
|
||||
if (!av[1] || av[2])
|
||||
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);
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_debug.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:02:43 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:02:44 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int g_debug;
|
||||
|
||||
int cli_do_debug(int sock, char **av)
|
||||
{
|
||||
(void)sock;
|
||||
if (av[1] && av[2])
|
||||
return (console_msg(-1, "usage: debug [ on | off | level ]"));
|
||||
if (!av[1])
|
||||
g_debug = !g_debug;
|
||||
else if (ft_strcmp(av[1], "on") == 0)
|
||||
g_debug = 1;
|
||||
else if (ft_strcmp(av[1], "off") == 0)
|
||||
g_debug = 0;
|
||||
else if (ft_stris(av[1], ft_isdigit))
|
||||
g_debug = ft_atoi(av[1]);
|
||||
else
|
||||
{
|
||||
console_msg(-1, "%s: bad debugging value", av[1]);
|
||||
return (-1);
|
||||
}
|
||||
console_msg(0, "Debugging %s (g_debug = %i)",
|
||||
g_debug ? "on" : "off", g_debug);
|
||||
return (-1);
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_get.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:02:48 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:03:12 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int cli_do_get(int sock, char **av)
|
||||
{
|
||||
int rep;
|
||||
int fd;
|
||||
|
||||
if (!av[1] || av[2])
|
||||
return (console_msg(-1, "usage: get <file>"));
|
||||
if (req_init(sock, REQUEST_FILE))
|
||||
return (1);
|
||||
write(sock, av[1], ft_strlen(av[1]));
|
||||
rep = read_req(sock);
|
||||
if (rep != FILENAME_OK)
|
||||
return (1);
|
||||
rep = htons(TRANSFER_START);
|
||||
if ((fd = open_new(av[1], O_WRONLY | O_TRUNC | O_APPEND)) < 0)
|
||||
rep = htons(ABORT);
|
||||
write(sock, (char*)&rep, sizeof(rep));
|
||||
if (rep != htons(TRANSFER_START))
|
||||
return (1);
|
||||
console_msg(1, "TRANSFER_START");
|
||||
net_get_large(sock, fd);
|
||||
console_msg(1, "TRANSFER DONE");
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_help.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:02:38 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:02:39 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int cli_do_help(int sock, char **av)
|
||||
{
|
||||
int i;
|
||||
t_cmd_map *cmd;
|
||||
|
||||
(void)sock;
|
||||
(void)av;
|
||||
i = 0;
|
||||
while (av[++i])
|
||||
{
|
||||
if (!(cmd = get_cmd(av[i])))
|
||||
console_msg(-1, "?Invalid ? command `%s'", av[i]);
|
||||
else
|
||||
console_msg(0, "%s\t%s", cmd->key, cmd->help);
|
||||
}
|
||||
if (i == 1)
|
||||
{
|
||||
console_msg(0, "Commands are:");
|
||||
i = -1;
|
||||
while (g_cli_cmd[++i].key)
|
||||
console_msg(0, "%s\t%s", g_cli_cmd[i].key, g_cli_cmd[i].help);
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_put.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:04:23 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:05:21 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int cli_do_put(int sock, char **av)
|
||||
{
|
||||
int req;
|
||||
int fd;
|
||||
struct stat stat;
|
||||
char *file;
|
||||
|
||||
if (!av[1] || av[2])
|
||||
return (console_msg(-1, "usage: put <file>"));
|
||||
else if ((fd = open(av[1], O_RDONLY)) < 0)
|
||||
return (console_msg(-1, "no such file"));
|
||||
else if ((fstat(fd, &stat)) < 0)
|
||||
return (console_msg(-1, "fstat error"));
|
||||
else if ((file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
|
||||
== MAP_FAILED)
|
||||
return (console_msg(-1, "mmap error"));
|
||||
if (req_init(sock, REQUEST_PUT))
|
||||
return (1);
|
||||
write(sock, av[1], ft_strlen(av[1]));
|
||||
req = read_req(sock);
|
||||
if (req != TRANSFER_START)
|
||||
return (1);
|
||||
net_send_large(sock, file, stat.st_size);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* client.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:25:25 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int g_debug = 0;
|
||||
|
||||
int g_debug;
|
||||
t_cmd_map g_cli_cmd[] =
|
||||
{
|
||||
{"ls", cli_do_sh, "list contents of remote directory"},
|
||||
{"cd", cli_do_cd, "change remote working directory"},
|
||||
{"get", cli_do_get, "receive file"},
|
||||
{"put", cli_do_put, "send one file"},
|
||||
{"pwd", cli_do_sh, "print working directory on remote machine"},
|
||||
{"quit", NULL, "terminate ftp session and exit"},
|
||||
|
||||
{"?", cli_do_help, "print local help information"},
|
||||
{"l", cli_do_local, "execute a local command"},
|
||||
{"debug", cli_do_debug, "toggle/set debugging mode"},
|
||||
{0, 0, 0},
|
||||
};
|
||||
|
||||
t_cmd_map *get_cmd(char *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (g_cli_cmd[++i].key)
|
||||
{
|
||||
if (ft_strcmp(g_cli_cmd[i].key, cmd) == 0)
|
||||
return (&g_cli_cmd[i]);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int do_client(int sock)
|
||||
{
|
||||
char *input;
|
||||
t_cmd_map *cmd;
|
||||
char **av;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (!(input = readline("ft_p> ")))
|
||||
return (1);
|
||||
if (*input)
|
||||
{
|
||||
av = ft_split_whitespaces(input);
|
||||
if (!(cmd = get_cmd(av[0])))
|
||||
console_msg(-1, "?Invalid command");
|
||||
else if (cmd->f)
|
||||
{
|
||||
if ((cmd->f)(sock, av) >= 0)
|
||||
read_req(sock);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
ft_sstrfree(av);
|
||||
}
|
||||
ft_strdel(&input);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int port;
|
||||
int sock;
|
||||
|
||||
if (ac != 3)
|
||||
ft_usage(FTP_CLIENT_USAGE, av[0]);
|
||||
port = ft_atoi(av[2]);
|
||||
if ((sock = create_client(av[1], port, "tcp")) < 0)
|
||||
{
|
||||
perror(av[0]);
|
||||
return (1);
|
||||
}
|
||||
do_client(sock);
|
||||
close(sock);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -6,13 +6,11 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:05:24 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/11/01 19:14:19 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int g_debug;
|
||||
#include "ftp_server.h"
|
||||
|
||||
int console_msg(int level, char *str, ...)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:04:11 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:06:53 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/11/01 19:30:16 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
#include "ftp_server.h"
|
||||
|
||||
int read_req(int sock)
|
||||
{
|
||||
|
|
@ -18,27 +18,44 @@ int read_req(int sock)
|
|||
return (0);
|
||||
}
|
||||
|
||||
int ftp_recv(int sock, char buf[], size_t size)
|
||||
int ftp_recv(t_ftp *ftp, char **msg)
|
||||
{
|
||||
int ret;
|
||||
char buf[100];
|
||||
|
||||
if ((ret = recv(sock, buf, size, 0)) < 0)
|
||||
if ((ret = recv(ftp->cmd_sock, buf, 100, 0)) < 0)
|
||||
return (0);
|
||||
if (ret >= 2)
|
||||
buf[ret - 2] = 0;
|
||||
else
|
||||
buf[ret] = 0;
|
||||
/* req = ntohs(req); */
|
||||
console_msg(0, "%-5i<--- %s (%i)", getpid(), buf, ret);
|
||||
if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r')
|
||||
{
|
||||
buf[ret - 2] = 0;
|
||||
}
|
||||
else
|
||||
console_msg(0, "recv'd non-crlf message!");
|
||||
*msg = ft_strdup(buf);
|
||||
console_msg(1, "%-5i<--- %s", getpid(), buf);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ftp_send(int sock, char *msg, size_t size)
|
||||
#define ftp_ret(ftp, ...) ftp_send(ftp->cmd_sock, ##VA_ARGS)
|
||||
#define ftp_data(ftp, ...) ftp_send(ftp->data_sock, ##VA_ARGS)
|
||||
|
||||
int ftp_send(int sock, char *msg, ...)
|
||||
{
|
||||
int err;
|
||||
if ((err = send(sock, msg, size, 0)) < 0)
|
||||
char *crlf_tmp;
|
||||
char *crlf_msg;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, msg);
|
||||
ft_vasprintf(&crlf_tmp, msg, ap);
|
||||
crlf_msg = ft_strjoin(crlf_tmp, "\r\n");
|
||||
if ((err = send(ftp->cmd_sock, crlf_msg, ft_strlen(crlf_msg), 0)) < 0)
|
||||
{
|
||||
return (err);
|
||||
msg[ft_strlen(msg) - 1] = 0;
|
||||
console_msg(0, "%-5i---> %s", getpid(), msg);
|
||||
return (0);
|
||||
}
|
||||
console_msg(1, "%-5i---> %s", getpid(), crlf_tmp);
|
||||
ft_strdel(&crlf_tmp);
|
||||
ft_strdel(&crlf_msg);
|
||||
return (ft_atoi(msg));
|
||||
}
|
||||
|
|
@ -1,35 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cli_do_sh.c :+: :+: :+: */
|
||||
/* login.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:05:34 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:06:32 by jhalford ### ########.fr */
|
||||
/* Created: 2017/11/01 17:15:12 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/01 19:11:08 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
#include "ftp_server.h"
|
||||
|
||||
int cli_do_sh(int sock, char **av)
|
||||
int ftp_login(t_ftp *ftp)
|
||||
{
|
||||
char *command;
|
||||
char buf[MAXLINE];
|
||||
int ret;
|
||||
char *msg;
|
||||
|
||||
command = ft_strsepjoin(av, ' ');
|
||||
if (req_init(sock, REQUEST_SH))
|
||||
return (1);
|
||||
console_msg(1, "sending '%s' to server", command);
|
||||
write(sock, command, ft_strlen(command));
|
||||
while ((ret = read(sock, &buf, MAXLINE)))
|
||||
{
|
||||
buf[ret] = 0;
|
||||
ft_printf("%s", buf);
|
||||
if (ret < MAXLINE)
|
||||
break ;
|
||||
}
|
||||
ft_strdel(&command);
|
||||
chdir("data");
|
||||
if (ftp_send(ftp, "220 ready for user"))
|
||||
console_msg(0, "send failed !");
|
||||
ftp_recv(ftp, &msg);
|
||||
ftp_send(ftp, "230 user logged in, proceed");
|
||||
return (0);
|
||||
|
||||
|
||||
if (ft_strncmp(msg, "USER ", 5) != 0)
|
||||
console_msg(0, "expected USER");
|
||||
if (ft_strncmp(msg + 5, "jack", 4) != 0)
|
||||
ftp_send(ftp, "530 user not logged in");
|
||||
else
|
||||
ftp_send(ftp, "331 user name ok, send passwd");
|
||||
ftp_recv(ftp, &msg);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* req_init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:07:00 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:07:01 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int req_init(int sock, int req)
|
||||
{
|
||||
int rep;
|
||||
int net_req;
|
||||
|
||||
net_req = htons(req);
|
||||
tcflush(sock, TCIFLUSH);
|
||||
write(sock, (char*)&net_req, sizeof(net_req));
|
||||
rep = read_req(sock);
|
||||
return (rep != CMD_SUPPORTED);
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* serv_do_cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:08:05 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:08:41 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
char g_rootdir[PATH_MAX];
|
||||
|
||||
int serv_do_cd(int sock)
|
||||
{
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* serv_do_get.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:09:37 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:25:05 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int serv_get_fname(int sock, char **file, struct stat *stat)
|
||||
{
|
||||
int rep;
|
||||
int fd;
|
||||
char fname[MAXLINE];
|
||||
|
||||
ft_bzero(&fname, MAXLINE);
|
||||
rep = htons(FILENAME_OK);
|
||||
if (read(sock, fname, MAXLINE) < 0)
|
||||
rep = htons(ERR_READ);
|
||||
else if ((fd = open(fname, O_RDONLY)) < 0)
|
||||
rep = htons(NO_SUCH_FILE);
|
||||
else if ((fstat(fd, stat)) < 0)
|
||||
rep = htons(ERR_STAT);
|
||||
else if ((*file = mmap(NULL, stat->st_size, PROT_READ, MAP_PRIVATE, fd, 0))
|
||||
== MAP_FAILED)
|
||||
rep = htons(ERR_MMAP);
|
||||
write(sock, (char*)&rep, sizeof(rep));
|
||||
return (rep);
|
||||
}
|
||||
|
||||
int serv_do_get(int sock)
|
||||
{
|
||||
int req;
|
||||
struct stat stat;
|
||||
char *file;
|
||||
|
||||
file = NULL;
|
||||
if (serv_get_fname(sock, &file, &stat))
|
||||
return (CMD_FAIL);
|
||||
DG("GOING TO READ_REQ");
|
||||
req = read_req(sock);
|
||||
if (req != TRANSFER_START)
|
||||
return (CMD_FAIL);
|
||||
net_send_large(sock, file, stat.st_size);
|
||||
if (file)
|
||||
munmap(file, stat.st_size);
|
||||
return (CMD_SUCCESS);
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* serv_do_put.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:07:11 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:07:55 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int serv_do_put(int sock)
|
||||
{
|
||||
char fname[MAXLINE];
|
||||
int rep;
|
||||
int fd;
|
||||
|
||||
ft_bzero(&fname, MAXLINE);
|
||||
rep = htons(TRANSFER_START);
|
||||
if (read(sock, fname, MAXLINE) < 0)
|
||||
rep = htons(ABORT);
|
||||
else if ((fd = open_new(fname, O_WRONLY | O_TRUNC | O_APPEND)) < 0)
|
||||
rep = htons(ABORT);
|
||||
write(sock, (char*)&rep, sizeof(rep));
|
||||
if (rep != htons(TRANSFER_START))
|
||||
return (CMD_FAIL);
|
||||
net_get_large(sock, fd);
|
||||
return (CMD_SUCCESS);
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* serv_do_sh.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:08:49 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:10:22 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
int serv_do_sh(int sock)
|
||||
{
|
||||
char command[MAXLINE];
|
||||
int ret;
|
||||
|
||||
if ((ret = read(sock, command, MAXLINE)) < 0)
|
||||
return (CMD_FAIL);
|
||||
command[ret] = 0;
|
||||
DG("received '%s' command", command);
|
||||
if (fork() == 0)
|
||||
{
|
||||
dup2(sock, 1);
|
||||
system(command);
|
||||
exit(0);
|
||||
}
|
||||
DG("waiting for command");
|
||||
wait(0);
|
||||
DG("finished waiting for command");
|
||||
return (CMD_SUCCESS);
|
||||
}
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* server.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */
|
||||
/* Updated: 2017/10/08 12:25:44 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_p.h"
|
||||
|
||||
#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},
|
||||
{REQUEST_PUT, serv_do_put},
|
||||
{REQUEST_SH, serv_do_sh},
|
||||
{REQUEST_CD, serv_do_cd},
|
||||
{0, 0},
|
||||
};
|
||||
|
||||
int ftp_cmd(int sock, int req)
|
||||
{
|
||||
int i;
|
||||
int msg_ok;
|
||||
|
||||
i = -1;
|
||||
msg_ok = htons(CMD_SUPPORTED);
|
||||
while (g_ftp_cmd[++i].id)
|
||||
{
|
||||
if (g_ftp_cmd[i].id == req)
|
||||
{
|
||||
if (!(g_ftp_cmd[i].f))
|
||||
break ;
|
||||
DG("%i CMD_SUPPORTED", CMD_SUPPORTED);
|
||||
write(sock, (char *)&msg_ok, sizeof(msg_ok));
|
||||
msg_ok = ntohs((g_ftp_cmd[i].f)(sock));
|
||||
write(sock, (char *)&msg_ok, sizeof(msg_ok));
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
msg_ok = htons(CMD_NOT_SUPPORTED);
|
||||
write(sock, (char *)&msg_ok, sizeof(msg_ok));
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ftp_login(int sock)
|
||||
{
|
||||
char buf[100];
|
||||
|
||||
ftp_send(sock, "220 \r\n", 6);
|
||||
ftp_recv(sock, buf, sizeof(buf));
|
||||
if (ft_strncmp(buf, "USER ", 5) != 0)
|
||||
console_msg(0, "expected USER");
|
||||
if (ft_strncmp(buf + 5, "jack", 4) != 0)
|
||||
console_msg(0, "wrong user");
|
||||
DG("sending 331");
|
||||
ftp_send(sock, "331 \r\n", 6);
|
||||
ftp_recv(sock, buf, sizeof(buf));
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ftp_spawn(int sock)
|
||||
{
|
||||
int req;
|
||||
|
||||
ftp_login(sock);
|
||||
while (1)
|
||||
{
|
||||
break ;
|
||||
(void)req;
|
||||
/* DG("==== %i NEW REQUEST ====", req); */
|
||||
/* ftp_cmd(sock, req); */
|
||||
/* DG("==== DONE ===="); */
|
||||
}
|
||||
close(sock);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ftp_daemon(int sock)
|
||||
{
|
||||
int cs;
|
||||
struct sockaddr_in csin;
|
||||
socklen_t cslen;
|
||||
pid_t pid;
|
||||
|
||||
while (1)
|
||||
{
|
||||
cs = accept(sock, (struct sockaddr*)&csin, &cslen);
|
||||
if ((pid = fork()) < 0)
|
||||
return (-1);
|
||||
if (pid == 0)
|
||||
{
|
||||
close(sock);
|
||||
exit(ftp_spawn(cs));
|
||||
}
|
||||
close(cs);
|
||||
}
|
||||
close(sock);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int port;
|
||||
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]);
|
||||
if ((sock = create_server(port, 3, "tcp")) < 0
|
||||
|| ftp_daemon(sock) < 0)
|
||||
{
|
||||
perror(av[0]);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
Loading…
Reference in a new issue