فهرست منبع

Merge branch 'us12' into develop

Added user management and modified server and main.

See merge request !15

Closes #21
Jonas Pflanzer 4 سال پیش
والد
کامیت
327e03c0e6

+ 64 - 0
daemon/include/UserManager.h

@@ -0,0 +1,64 @@
+#ifndef USER_MANAGER_H
+#define USER_MANAGER_H
+
+#include <fstream>
+#include <iostream>
+#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
+
+/**
+ * @Class UserManager
+ *
+ * Handle differnt users with passwords and store them
+ */
+class UserManager {
+public:
+  /**
+   * Makes shure that there is a user storage.
+   * If not. It creates a new one
+   */
+  static void init();
+
+  /**
+   * Checks if a given user with password is valid
+   *
+   * @param user the name of the users
+   * @param pw password in plain text
+   */
+  static bool isAllowed(const std::string &user, const std::string &pw);
+
+  /**
+   * Add a new user and write to storage.
+   *
+   * @param user the name of the users
+   * @param pw password in plain text
+   */
+  static void addUser(const std::string &name, const std::string &pw);
+
+  /**
+   * Delete a new user and delete from storage.
+   *
+   * @param user the name of the users
+   * @param pw password in plain text
+   */
+  static void deleteUser(const std::string &name, const std::string &pw);
+
+private:
+  /**
+   * Read data from file and create map.
+   *
+   * @param user_map pointer to the map
+   */
+  static void readFromFile(std::map<std::string, std::string> &user_map);
+
+  /**
+   * Write data to afile
+   *
+   * @param user_map pointer to the map
+   */
+  static void writeToFile(std::map<std::string, std::string> &user_map);
+};
+
+#endif

+ 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)
 
-add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.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)
 
 # use pkg-config to fix building on debian unstable
 find_package(PkgConfig REQUIRED)

+ 4 - 3
daemon/src/JsonCommander.cpp

@@ -1,4 +1,5 @@
 #include "../include/JsonCommander.h"
+#include "../include/UserManager.h"
 #include "../include/base64.h"
 
 JsonCommander::JsonCommander(FileManager &fileManager)
@@ -335,9 +336,9 @@ JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
     response.json["accept"] = false;
     response.json["error"] = "invalid login request";
   } else if (message["login"].asBool() &&
-             message["user"].asString().compare("user") == 0 &&
-             message["pass"].asString().compare("pass") == 0) {
-    // TODO replace with real credentials check
+             UserManager::isAllowed(message["user"].asString(),
+                                    message["pass"].asString())) {
+    // credential check
     response.action = send;
     response.json["accept"] = true;
     response.json["error"] = "";

+ 1 - 0
daemon/src/Server.cpp

@@ -1,4 +1,5 @@
 #include "../include/Server.h"
+#include "../include/base64.h"
 
 #include <iostream>
 

+ 77 - 0
daemon/src/UserManager.cpp

@@ -0,0 +1,77 @@
+#include "../include/UserManager.h"
+
+// TODO passwords are stored and checked in plain text
+// TODO read userStorage file location from config
+
+void UserManager::init() {
+  std::ifstream ifile("userStorage.txt");
+  if (!ifile) {
+    // create new file if userStorage does not exist
+    std::ofstream file;
+    file.open("userStorage.txt");
+    file << "cat;tac\n";
+    file.close();
+  }
+  ifile.close();
+}
+
+bool UserManager::isAllowed(const std::string &name, const std::string &pw) {
+  std::map<std::string, std::string> user_map;
+  readFromFile(user_map);
+  auto it = user_map.find(name);
+  // check if user exists and pw is equal
+  if (it != user_map.end() && (it->second.compare(pw) == 0)) {
+    return true;
+  }
+  return false;
+}
+
+void UserManager::addUser(const std::string &name, const std::string &pw) {
+  std::map<std::string, std::string> user_map;
+  readFromFile(user_map);
+  auto it = user_map.find(name);
+  // if user exists, do nothing
+  if (it != user_map.end()) {
+    return;
+  }
+  user_map.insert(std::pair<std::string, std::string>(name, pw));
+  writeToFile(user_map);
+}
+
+void UserManager::deleteUser(const std::string &name, const std::string &pw) {
+  // TODO check pw before delete
+  std::map<std::string, std::string> user_map;
+  readFromFile(user_map);
+  auto it = user_map.find(name);
+  if (it == user_map.end()) {
+    return;
+  }
+  user_map.erase(it);
+  writeToFile(user_map);
+}
+
+// read content from file into given map
+void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
+  std::ifstream ifile("userStorage.txt");
+  std::string line;
+  while (getline(ifile, line)) {
+    std::stringstream ss(line);
+    std::string segment;
+    std::vector<std::string> v;
+    while (std::getline(ss, segment, ';')) {
+      v.push_back(segment);
+    }
+    user_map.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
+  }
+  ifile.close();
+}
+
+// write content from map to file
+void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
+  std::ofstream file;
+  file.open("userStorage.txt");
+  for (auto const &x : user_map) {
+    file << x.first << ";" << x.second << std::endl;
+  }
+  file.close();
+}

+ 5 - 0
daemon/src/main.cpp

@@ -3,6 +3,7 @@
 
 #include "../include/Server.h"
 #include "../include/Sniffer.h"
+#include "../include/UserManager.h"
 
 using namespace std;
 
@@ -12,6 +13,10 @@ int main(int argc, char *argv[]) {
     return 0;
   }
 
+  // check if userStorage is add specified location
+  // if not create one
+  UserManager::init();
+
   const string interface = argv[1];
   Sniffer *sniffer = new Sniffer(interface);
   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})
 
 # Add test cpp file
-add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp)
+add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp)
 target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
 
 add_test(