]> Eric's Git Repo - listv4.git/commitdiff
Added configurable root directory
authorEric Wertz <ericwertz@Erics-MacBook-Pro.local>
Sat, 31 May 2025 16:36:54 +0000 (12:36 -0400)
committerEric Wertz <ericwertz@Erics-MacBook-Pro.local>
Sat, 31 May 2025 16:36:54 +0000 (12:36 -0400)
backend/HTTPResponse.cpp
backend/User.cpp
backend/User.h
backend/decrypt.cpp
backend/main.cpp
backend/util.cpp
backend/util.h

index c7b43da6890bce8e697c9a1c4a2c1ecc7ff4d23d..e2c2344941a19b2c7eced1a26e2d3c81a182a9b3 100644 (file)
@@ -21,9 +21,10 @@ CHTTPResponse::CHTTPResponse(const CHTTPRequest& request) : _request(request)
     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) + "/";
     }
index 04c4eb0956eb162dde9590c2c2189515b1a9bade..42b16b3201d223d0e96494137ed0bb93b43a046c 100644 (file)
@@ -13,7 +13,7 @@
 
 #define SESSION_ID_LENGTH 32
 
-CUser::CUser() : _auth_db("data/auth.db") 
+CUser::CUser() : _auth_db(g_rootDir + "/data/auth.db") 
 {
 
 }
@@ -75,7 +75,7 @@ void CUser::authenticate( std::vector<unsigned char>& fullSessionKeyFromCookie )
         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);
@@ -169,7 +169,7 @@ void CUser::create(const std::string& jsonRequest)
     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
index 63854bc6d1e564f5b40c4080570181003d00c277..4e62c9edecf8a613d1bfae38b9efdb9f2458a22c 100644 (file)
@@ -22,6 +22,7 @@ public:
     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;
index 2458b29983af27fd40bde57dc899da963f394c26..157e01261fc46a085955691ad5684b523e2e3452 100644 (file)
@@ -5,51 +5,89 @@
 #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.
@@ -131,7 +169,7 @@ int main() {
         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 + "\"";
index aba716fdb4e13a699dc5fa97df9bc4a4f23d576a..0d24b3e1748ac37af05501d330a4d1f0f8b3e6cb 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "HTTPRequest.h"
 #include "HTTPRequestRouter.h"
+#include "util.h"
 
 std::map<int,std::thread> threadMap;
 std::vector<int> completeThreads;
@@ -54,11 +55,13 @@ void print_usage(const char* program_name) {
     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++) {
@@ -75,6 +78,15 @@ int main(int argc, char* argv[]) {
                 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;
@@ -85,6 +97,9 @@ int main(int argc, char* argv[]) {
         }
     }
 
+    // Set the global root directory
+    g_rootDir = root_dir;
+
     int server_socket;
     struct sockaddr_in server_address;
 
index bd10c1a73bdc5ae18ed10ef6170b85451dd25d79..bf955f2abc575a6242a750289593fa721648a490 100644 (file)
@@ -15,6 +15,9 @@
 #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;
index 7d776101eb189b92e2f3778d4559edb0fb5d36b4..f6265041034c41107134e4c197d48338fb9548dd 100644 (file)
@@ -13,6 +13,9 @@
 #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 );