From 22f2cbbde82e94f595c39cb49446e9e34f3b3877 Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Thu, 17 Oct 2024 22:26:53 -0500 Subject: [PATCH] Fixed FEAT Fixed PORT memory overflow allocation Added support for specifying directory on LIST/NLST --- src/client.cpp | 25 +++++++++++++++++++++---- src/filer.cpp | 13 +++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 6e06a43..8ec2c32 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -129,8 +129,8 @@ public: } else if (cmd == "PORT") { data_sock = socket(AF_INET, SOCK_STREAM, 0); - unsigned char act_port[2]; - int act_ip[4], port_dec; + unsigned char act_port[2] = {}; + int act_ip[4] = {}, port_dec; char ip_decimal[40]; sscanf(argstr.c_str(), "%d,%d,%d,%d,%d,%d", &act_ip[0], &act_ip[1], &act_ip[2], &act_ip[3], (int*)&act_port[0], (int*)&act_port[1]); sprintf(ip_decimal, "%d.%d.%d.%d", act_ip[0], act_ip[1], act_ip[2], act_ip[3]); @@ -156,7 +156,10 @@ public: submit(550, "Access Denied "+std::to_string(ret)); } } else if (cmd == "FEAT") { - submit(211, ":\r\n211 END"); + submit("211-Extensions supported:"); + submit(" UTF8"); + submit(" SIZE"); + submit(211, "END"); } else if (cmd == "NOOP") { submit(226, "OK"); } else if (cmd == "DELE" || cmd == "RMD") { @@ -171,7 +174,10 @@ public: if (cmd == "LIST" || cmd == "NLST") { submit(150, "Transferring"); - std::string output = filer->list(); + std::string dirname = ""; + if (argstr.find_first_of('/') != std::string::npos) + dirname = argstr.substr(argstr.find_first_of('/')); + std::string output = filer->list(dirname); char out[output.size()] = {0}; strncpy(out, output.c_str(), output.size()); data_submit(out, output.size()); @@ -236,6 +242,17 @@ public: return 0; } + int submit(std::string msg) { + std::string out = msg+"\r\n"; + int bytes = send(control_sock, out.c_str(), out.size(), 0); + if (bytes < 0) { + logger->print(LOGLEVEL_ERROR, "C(%i) !< %s", control_sock, msg.c_str()); + return 1; + } + logger->print(LOGLEVEL_DEBUG, "C(%i) << %s", control_sock, msg.c_str()); + return 0; + } + int submit(int code, std::string msg) { std::string out = std::to_string(code)+" "+msg+"\r\n"; int bytes = send(control_sock, out.c_str(), out.size(), 0); diff --git a/src/filer.cpp b/src/filer.cpp index a6ad322..442044c 100644 --- a/src/filer.cpp +++ b/src/filer.cpp @@ -166,13 +166,12 @@ public: return 0; } - // Gets a list of files and folders within current working directory. - // Outputs in the format of 'ls -lA'. - std::string list() { + std::string list(std::string path) { + fs::path fpath = fullPath(path); std::ostringstream listStream; // Not checking for pwd existence. If it doesn't exist and we // got this far, we fucked up anyway. - for(fs::directory_entry const& p : fs::directory_iterator(fullPath())) { + for(fs::directory_entry const& p : fs::directory_iterator(fpath)) { char *line; // Stole part of this from here: @@ -222,6 +221,12 @@ public: return listStream.str(); } + // Gets a list of files and folders within current working directory. + // Outputs in the format of 'ls -lA'. + std::string list() { + return list(""); + } + private: fs::path root; }; \ No newline at end of file