Kaynağa Gözat

Merge branch 'us13' into develop

US13: Server Configuration

Closes #18

See merge request tobias.wach/ccats!18
Jonas Pflanzer 5 yıl önce
ebeveyn
işleme
c19a1ff944

+ 17 - 0
daemon/Daemon-Config-Reference.md

@@ -0,0 +1,17 @@
+# Daemon configuration
+
+The daemon is configurable by config.txt.
+This file must be in the same directory as the binary.
+
+### Configuration Values
+`port` : The port where the server listens. Must be a valid port.
+`interface` : The sniffer interface you want to use.
+`userdatabase` : The file where userdata is stored in format: user;password
+
+### Example for config.txt
+```
+port=1234
+interface=lo
+userdatabase=user Storage.txt
+filedirectory=./files/
+```

+ 34 - 0
daemon/include/Config.h

@@ -0,0 +1,34 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <fstream>
+#include <map>
+#include <sstream>
+#include <vector>
+
+/**
+ * Namespace which loads and holds configuration data
+ */
+namespace Config {
+
+/**
+ * Load data from file into storage
+ *
+ * @param filename the name of the file which holds the data
+ */
+bool init(const std::string &filename);
+
+/**
+ * Load data from file into storage
+ *
+ * @param key get value from storage by key
+ */
+std::string getValue(const std::string &key);
+
+/**
+ * Map which holds the configuration data
+ */
+extern std::map<std::string, std::string> storage;
+}; // namespace Config
+
+#endif

+ 1 - 1
daemon/include/FileManager.h

@@ -11,7 +11,7 @@
  */
  */
 class FileManager {
 class FileManager {
 private:
 private:
-  const std::string fileDirectory = "./files/";
+  const std::string fileDirectory;
 
 
   /**
   /**
    * file stream for get command
    * file stream for get command

+ 1 - 1
daemon/include/JsonCommander.h

@@ -19,7 +19,7 @@ public:
    * closeAndSend - send the json answer and close the socket
    * closeAndSend - send the json answer and close the socket
    * close        - close the socket
    * close        - close the socket
    */
    */
-  enum Action { send, closeAndSend, close };
+  enum Action { close, send, closeAndSend };
 
 
   /**
   /**
    * Response for commands
    * Response for commands

+ 6 - 1
daemon/include/UserManager.h

@@ -19,7 +19,7 @@ public:
    * Makes shure that there is a user storage.
    * Makes shure that there is a user storage.
    * If not. It creates a new one
    * If not. It creates a new one
    */
    */
-  static void init();
+  static void init(const std::string &file);
 
 
   /**
   /**
    * Checks if a given user with password is valid
    * Checks if a given user with password is valid
@@ -46,6 +46,11 @@ public:
   static void deleteUser(const std::string &name, const std::string &pw);
   static void deleteUser(const std::string &name, const std::string &pw);
 
 
 private:
 private:
+  /**
+   * Name of the file which holds the user data
+   */
+  static std::string filename;
+
   /**
   /**
    * Read data from file and create map.
    * Read data from file and create map.
    *
    *

+ 1 - 1
daemon/src/CMakeLists.txt

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
 
 
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 
-add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.cpp src/UserManager.cpp)
+add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.cpp src/UserManager.cpp src/Config.cpp)
 
 
 # use pkg-config to fix building on debian unstable
 # use pkg-config to fix building on debian unstable
 find_package(PkgConfig REQUIRED)
 find_package(PkgConfig REQUIRED)

+ 36 - 0
daemon/src/Config.cpp

@@ -0,0 +1,36 @@
+#include "../include/Config.h"
+
+namespace Config {
+std::map<std::string, std::string> storage;
+};
+
+bool Config::init(const std::string &filename) {
+  std::ifstream ifile(filename);
+  std::string line;
+
+  if (ifile.is_open()) {
+    while (getline(ifile, line)) {
+      std::stringstream ss(line);
+      std::string segment;
+      std::vector<std::string> v;
+      while (getline(ss, segment, '=')) {
+        v.push_back(segment);
+      }
+      if (v.size() != 2) {
+        return false;
+      }
+      storage.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
+    }
+    return true;
+  }
+  return false;
+}
+
+std::string Config::getValue(const std::string &key) {
+  auto it = storage.find(key);
+  if (it != storage.end()) {
+    return it->second;
+  }
+
+  return "";
+}

+ 3 - 1
daemon/src/FileManager.cpp

@@ -1,6 +1,8 @@
 #include "../include/FileManager.h"
 #include "../include/FileManager.h"
 
 
-FileManager::FileManager() {}
+#include "../include/Config.h"
+
+FileManager::FileManager() : fileDirectory(Config::getValue("filedirectory")) {}
 
 
 FileManager::~FileManager() {
 FileManager::~FileManager() {
   cancelPut();
   cancelPut();

+ 3 - 0
daemon/src/JsonCommander.cpp

@@ -335,6 +335,9 @@ JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
     response.action = closeAndSend;
     response.action = closeAndSend;
     response.json["accept"] = false;
     response.json["accept"] = false;
     response.json["error"] = "invalid login request";
     response.json["error"] = "invalid login request";
+  } else if (message["cancel"].asBool()) {
+    response.action = close;
+
   } else if (message["login"].asBool() &&
   } else if (message["login"].asBool() &&
              UserManager::isAllowed(message["user"].asString(),
              UserManager::isAllowed(message["user"].asString(),
                                     message["pass"].asString())) {
                                     message["pass"].asString())) {

+ 3 - 1
daemon/src/Server.cpp

@@ -1,4 +1,5 @@
 #include "../include/Server.h"
 #include "../include/Server.h"
+#include "../include/Config.h"
 #include "../include/base64.h"
 #include "../include/base64.h"
 
 
 #include <iostream>
 #include <iostream>
@@ -172,7 +173,8 @@ void Server::start_accept() {
 }
 }
 
 
 Server::Server(io_service &io_service)
 Server::Server(io_service &io_service)
-    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
+    : acceptor_(io_service,
+                tcp::endpoint(tcp::v4(), std::stoi(Config::getValue("port")))) {
   start_accept();
   start_accept();
 }
 }
 
 

+ 15 - 9
daemon/src/UserManager.cpp

@@ -3,14 +3,20 @@
 // TODO passwords are stored and checked in plain text
 // TODO passwords are stored and checked in plain text
 // TODO read userStorage file location from config
 // TODO read userStorage file location from config
 
 
-void UserManager::init() {
-  std::ifstream ifile("userStorage.txt");
-  if (!ifile) {
+// initialize static filename to empty string
+std::string UserManager::filename = "";
+
+void UserManager::init(const std::string &file) {
+  filename = file;
+  std::ifstream ifile(filename);
+  if (!ifile.is_open()) {
     // create new file if userStorage does not exist
     // create new file if userStorage does not exist
-    std::ofstream file;
-    file.open("userStorage.txt");
-    file << "cat;tac\n";
-    file.close();
+    std::ofstream ofile;
+    ofile.open(filename);
+    ofile << "user;pass\n";
+    ofile.close();
+    std::cout << "Created \"" << filename << "\" and added the default user"
+              << std::endl;
   }
   }
   ifile.close();
   ifile.close();
 }
 }
@@ -52,7 +58,7 @@ void UserManager::deleteUser(const std::string &name, const std::string &pw) {
 
 
 // read content from file into given map
 // read content from file into given map
 void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
 void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
-  std::ifstream ifile("userStorage.txt");
+  std::ifstream ifile(filename);
   std::string line;
   std::string line;
   while (getline(ifile, line)) {
   while (getline(ifile, line)) {
     std::stringstream ss(line);
     std::stringstream ss(line);
@@ -69,7 +75,7 @@ void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
 // write content from map to file
 // write content from map to file
 void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
 void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
   std::ofstream file;
   std::ofstream file;
-  file.open("userStorage.txt");
+  file.open(filename);
   for (auto const &x : user_map) {
   for (auto const &x : user_map) {
     file << x.first << ";" << x.second << std::endl;
     file << x.first << ";" << x.second << std::endl;
   }
   }

+ 8 - 5
daemon/src/main.cpp

@@ -1,6 +1,7 @@
 #include <iostream>
 #include <iostream>
 #include <thread>
 #include <thread>
 
 
+#include "../include/Config.h"
 #include "../include/Server.h"
 #include "../include/Server.h"
 #include "../include/Sniffer.h"
 #include "../include/Sniffer.h"
 #include "../include/UserManager.h"
 #include "../include/UserManager.h"
@@ -8,16 +9,18 @@
 using namespace std;
 using namespace std;
 
 
 int main(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
-  if (argc < 2) {
-    cout << "Usage: " << argv[0] << " <interface>" << endl << endl;
-    return 0;
+  // load config int namespace
+  if (!Config::init("config.txt")) {
+    cerr << "configuration could not be loaded properly" << endl;
+    exit(EXIT_FAILURE);
   }
   }
 
 
+  const string interface = Config::getValue("interface");
+
   // check if userStorage is add specified location
   // check if userStorage is add specified location
   // if not create one
   // if not create one
-  UserManager::init();
+  UserManager::init(Config::getValue("userdatabase"));
 
 
-  const string interface = argv[1];
   Sniffer *sniffer = new Sniffer(interface);
   Sniffer *sniffer = new Sniffer(interface);
   thread snifferThread(&Sniffer::startSniffing, sniffer);
   thread snifferThread(&Sniffer::startSniffing, sniffer);
 
 

+ 1 - 1
daemon/test/CMakeLists.txt

@@ -20,7 +20,7 @@ find_package(GTest REQUIRED)
 include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})
 include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})
 
 
 # Add test cpp file
 # Add test cpp file
-add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp)
+add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp src/Config.cpp)
 target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
 target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
 
 
 add_test(
 add_test(