summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 336036a)
raw | patch | inline | side by side (parent: 336036a)
author | Eric Wertz <ericwertz@Erics-MacBook-Pro.local> | |
Sat, 31 May 2025 16:36:54 +0000 (12:36 -0400) | ||
committer | Eric Wertz <ericwertz@Erics-MacBook-Pro.local> | |
Sat, 31 May 2025 16:36:54 +0000 (12:36 -0400) |
index c7b43da6890bce8e697c9a1c4a2c1ecc7ff4d23d..e2c2344941a19b2c7eced1a26e2d3c81a182a9b3 100644 (file)
--- a/backend/HTTPResponse.cpp
+++ b/backend/HTTPResponse.cpp
if (_wwwDir == "")
{
char resolved_path[PATH_MAX];
- if( realpath("www/", resolved_path) == NULL )
+ std::string www_path = g_rootDir + "/www";
+ if( realpath(www_path.c_str(), resolved_path) == NULL )
{
- die("Failed to resolve path");
+ die("Failed to resolve path: %s", www_path.c_str());
}
_wwwDir = std::string(resolved_path) + "/";
}
diff --git a/backend/User.cpp b/backend/User.cpp
index 04c4eb0956eb162dde9590c2c2189515b1a9bade..42b16b3201d223d0e96494137ed0bb93b43a046c 100644 (file)
--- a/backend/User.cpp
+++ b/backend/User.cpp
#define SESSION_ID_LENGTH 32
-CUser::CUser() : _auth_db("data/auth.db")
+CUser::CUser() : _auth_db(g_rootDir + "/data/auth.db")
{
}
throw HTTPUnauthorizedException("Failed to decrypt database key: incorrect key length after decryption.");
}
- _user_db.setDbPath("data/user/" + _uuid + ".db");
+ _user_db.setDbPath(g_rootDir + "/data/user/" + _uuid + ".db");
try {
_user_db.connect();
_user_db.setKey(plaintext_db_key);
insert_user_query.execute();
// Configure and connect the user-specific database
- _user_db.setDbPath("data/user/" + _uuid + ".db");
+ _user_db.setDbPath(g_rootDir + "/data/user/" + _uuid + ".db");
try {
_user_db.connect(); // Ensure this method attempts to create the DB if not exists
_user_db.setKey(dbKey_plaintext); // Set the encryption key for the new user DB
diff --git a/backend/User.h b/backend/User.h
index 63854bc6d1e564f5b40c4080570181003d00c277..4e62c9edecf8a613d1bfae38b9efdb9f2458a22c 100644 (file)
--- a/backend/User.h
+++ b/backend/User.h
const std::string& getUsername() const { return _username; }
const std::string& getUUID() const { return _uuid; }
CDatabase& getDatabase() { return _user_db; }
+
private:
std::string _username;
std::string _key;
diff --git a/backend/decrypt.cpp b/backend/decrypt.cpp
index 2458b29983af27fd40bde57dc899da963f394c26..157e01261fc46a085955691ad5684b523e2e3452 100644 (file)
--- a/backend/decrypt.cpp
+++ b/backend/decrypt.cpp
#include <stdexcept> // For std::runtime_error
#include <cstdlib> // For std::system
#include <sstream> // For std::ostringstream
+#include <cstring>
#include "sqlite3.h" // For sqlite3 access
#include "util.h" // For hashString, decryptBytes, ENCRYPT_KEY_LENGTH, ENCRYPT_HASH_SALT_LENGTH
#include "Database.h" // For CDatabase
+#include "HTTPExceptions.h"
// Function to print a vector of unsigned char as a hex string
void print_hex(const std::vector<unsigned char>& data) {
- std::cout << "0x";
for (unsigned char byte : data) {
- std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
+ printf("%02x", byte);
}
- std::cout << std::dec << std::endl; // Reset to decimal for subsequent output
+ std::cout << std::endl;
}
// Function to convert a vector of unsigned char to a hex string (no 0x prefix)
std::string vector_to_hex_string(const std::vector<unsigned char>& data) {
std::ostringstream oss;
for (unsigned char byte : data) {
- oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
+ oss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
}
return oss.str();
}
-int main() {
- std::string username;
- std::string password;
+void print_usage(const char* program_name) {
+ std::cout << "Usage: " << program_name << " [options] <username> <password>\n";
+ std::cout << "Options:\n";
+ std::cout << " -d <dir> Root directory for database operations (default: current directory)\n";
+ std::cout << " -h Show this help message\n";
+}
- std::cout << "Enter username: ";
- std::cin >> username;
- std::cout << "Enter password: ";
- std::cin >> password;
+int main(int argc, char* argv[]) {
+ std::string username, password;
+
+ // Parse command line arguments
+ int arg_index = 1;
+ while (arg_index < argc) {
+ if (strcmp(argv[arg_index], "-d") == 0) {
+ if (arg_index + 1 < argc) {
+ g_rootDir = argv[arg_index + 1];
+ arg_index += 2;
+ } else {
+ std::cerr << "Error: -d flag requires a directory path.\n";
+ print_usage(argv[0]);
+ return 1;
+ }
+ } else if (strcmp(argv[arg_index], "-h") == 0) {
+ print_usage(argv[0]);
+ return 0;
+ } else if (argv[arg_index][0] == '-') {
+ std::cerr << "Error: Unknown argument " << argv[arg_index] << "\n";
+ print_usage(argv[0]);
+ return 1;
+ } else {
+ // Non-flag arguments are username and password
+ if (username.empty()) {
+ username = argv[arg_index];
+ } else if (password.empty()) {
+ password = argv[arg_index];
+ } else {
+ std::cerr << "Error: Too many arguments.\n";
+ print_usage(argv[0]);
+ return 1;
+ }
+ arg_index++;
+ }
+ }
- CDatabase auth_db("data/auth.db");
- try {
- auth_db.connect();
- } catch (const std::exception& e) {
- std::cerr << "Error connecting to auth database: " << e.what() << std::endl;
+ if (username.empty() || password.empty()) {
+ std::cerr << "Error: Username and password are required.\n";
+ print_usage(argv[0]);
return 1;
}
- std::string fetched_username;
- std::vector<unsigned char> key_from_db;
- std::string uuid_from_db; // Not strictly needed for decryption but fetched in User::login
-
try {
+ // Connect to auth database
+ CDatabase auth_db(g_rootDir + "/data/auth.db");
+ auth_db.connect();
+
+ std::string fetched_username;
+ std::vector<unsigned char> key_from_db;
+ std::string uuid_from_db; // Not strictly needed for decryption but fetched in User::login
+
// Hash password (without salt for the initial lookup, similar to CUser::login)
// The CUser::login uses a fixed size buffer and then calls hashString.
// We need to replicate that or ensure hashString behaves correctly.
std::string hex_key_string = vector_to_hex_string(dbKey_decrypted);
std::cout << "Decrypted Key (Hex for command): " << hex_key_string << std::endl;
- std::string user_db_path = "data/user/" + uuid_from_db + ".db";
+ std::string user_db_path = g_rootDir + "/data/user/" + uuid_from_db + ".db";
// Construct the command for sqlite3mc with PRAGMA commands and keep shell open
std::string command = "data/sqlite3mc \"file:" + user_db_path + "?cipher=aes256cbc&hexkey=" + hex_key_string + "\"";
diff --git a/backend/main.cpp b/backend/main.cpp
index aba716fdb4e13a699dc5fa97df9bc4a4f23d576a..0d24b3e1748ac37af05501d330a4d1f0f8b3e6cb 100644 (file)
--- a/backend/main.cpp
+++ b/backend/main.cpp
#include "HTTPRequest.h"
#include "HTTPRequestRouter.h"
+#include "util.h"
std::map<int,std::thread> threadMap;
std::vector<int> completeThreads;
std::cout << "Usage: " << program_name << " [options]\n";
std::cout << "Options:\n";
std::cout << " -p <port> Port to listen on (default: 9000)\n";
+ std::cout << " -d <dir> Root directory for file operations (default: current directory)\n";
std::cout << " -h Show this help message\n";
}
int main(int argc, char* argv[]) {
int port = 9000; // Default port
+ std::string root_dir = "."; // Default to current directory
// Parse command line arguments
for (int i = 1; i < argc; i++) {
print_usage(argv[0]);
return 1;
}
+ } else if (strcmp(argv[i], "-d") == 0) {
+ if (i + 1 < argc) {
+ root_dir = argv[i + 1];
+ i++; // Skip the next argument since we've used it as the directory
+ } else {
+ std::cerr << "Error: -d flag requires a directory path.\n";
+ print_usage(argv[0]);
+ return 1;
+ }
} else if (strcmp(argv[i], "-h") == 0) {
print_usage(argv[0]);
return 0;
}
}
+ // Set the global root directory
+ g_rootDir = root_dir;
+
int server_socket;
struct sockaddr_in server_address;
diff --git a/backend/util.cpp b/backend/util.cpp
index bd10c1a73bdc5ae18ed10ef6170b85451dd25d79..bf955f2abc575a6242a750289593fa721648a490 100644 (file)
--- a/backend/util.cpp
+++ b/backend/util.cpp
#include "UTCDateTime.h"
#include "HTTPExceptions.h"
+// Global root directory for file operations (default to current directory)
+std::string g_rootDir = ".";
+
void die( const char* str, ... )
{
va_list args;
diff --git a/backend/util.h b/backend/util.h
index 7d776101eb189b92e2f3778d4559edb0fb5d36b4..f6265041034c41107134e4c197d48338fb9548dd 100644 (file)
--- a/backend/util.h
+++ b/backend/util.h
#define ENCRYPT_HASH_LENGTH 32 // SHA-256
#define ENCRYPT_HASH_SALT_LENGTH 16
+// Global root directory for file operations
+extern std::string g_rootDir;
+
void die( const char* str, ... );
std::string string_format( std::string format, ... );
std::string print_progress( int count, int total );