Finds the plan file and returns its contents

This commit is contained in:
pmb
2025-06-24 15:58:30 -07:00
parent f71f1c9b11
commit e648b8e319
3 changed files with 143 additions and 86 deletions
+47 -5
View File
@@ -1,7 +1,12 @@
#include "handler.hpp" #include "handler.hpp"
#include <filesystem>
#include <fstream>
#include <string_view>
std::string process(const std::string &username){ const std::filesystem::path kPATH{"/var/finger/users/"};
try{
std::string process(const std::string &username) {
try {
// Check for directory traversal patterns // Check for directory traversal patterns
if (username.find("../") != std::string::npos || if (username.find("../") != std::string::npos ||
username.find("..\\") != std::string::npos || username.find("..\\") != std::string::npos ||
@@ -16,12 +21,49 @@ std::string process(const std::string &username){
throw InvalidInput("Directory traversal detected in username"); throw InvalidInput("Directory traversal detected in username");
} }
if (username.find("/") != std::string::npos){ if (username.find("/") != std::string::npos) {
throw InvalidInput("Path detected in username"); throw InvalidInput("Path detected in username");
} }
} } catch (InvalidInput &e) {
catch(InvalidInput&e){
return std::string("InvalidInput: ") + e.what() + std::string("\r\n"); return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
} }
// Attempt to open the plan file (if any) and return the contents as a string
std::filesystem::path planPath = kPATH / username;
// Check if the plan file exists
if (!std::filesystem::exists(planPath)) {
// If no plan file exists, return just the username
return username; return username;
}
// Try to read the plan file
try {
std::ifstream planFile(planPath);
if (!planFile.is_open()) {
return username;
}
std::string content;
std::string line;
while (std::getline(planFile, line)) {
content += line + "\n";
}
// Return the plan content with proper line endings
if (!content.empty() && content.back() == '\n') {
content.pop_back(); // Remove the last newline
content += "\r\n";
return content;
} else if (!content.empty()) {
content += "\r\n";
return content;
} else {
// If file exists but is empty, return just the username
return username;
}
} catch (...) {
// If there's any error reading the file, just return the username
return username;
}
} }
+19 -4
View File
@@ -22,18 +22,33 @@ namespace this_coro = boost::asio::this_coro;
awaitable<std::string> dofinger(const std::string &username) { awaitable<std::string> dofinger(const std::string &username) {
co_return process(username); co_return process(username);
//co_return std::string("this is some finger information for ") + username + std::string("\r\n"); // co_return std::string("this is some finger information for ") + username +
// std::string("\r\n");
} }
awaitable<void> echo(tcp::socket socket) { awaitable<void> echo(tcp::socket socket) {
try { try {
char data[1024]; char data[1024];
auto bytes_read =
co_await socket.async_read_some(boost::asio::buffer(data), deferred); co_await socket.async_read_some(boost::asio::buffer(data), deferred);
auto response = co_await dofinger(std::string(data)); // strip \r\n from data
co_await async_write(socket, boost::asio::buffer(response), deferred); std::string username(data, bytes_read);
// Remove trailing \r\n characters
while (!username.empty() &&
(username.back() == '\r' || username.back() == '\n')) {
username.pop_back();
}
auto response = co_await dofinger(username);
if (response.compare(std::string(username)) == 0) {
// No plan found
co_await async_write(
socket, boost::asio::buffer(std::string("No plan found\r\n")),
deferred);
co_return; co_return;
} }
catch (std::exception &e) { co_await async_write(socket, boost::asio::buffer(response), deferred);
co_return;
} catch (std::exception &e) {
std::printf("echo Exception: %s\n", e.what()); std::printf("echo Exception: %s\n", e.what());
} }
} }