diff --git a/Makefile b/Makefile index 7fa5e5e..fc542f6 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ all: - g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher \ No newline at end of file + g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher \ No newline at end of file diff --git a/src/definitions.h b/src/definitions.h new file mode 100644 index 0000000..4e755b9 --- /dev/null +++ b/src/definitions.h @@ -0,0 +1,11 @@ +#ifndef _LOCATIONS_H +#define _LOCATIONS_H + +#define LOGFILE "journalctl -u smbd --since now -f" + +#define USER_LOG_LOCATION 3 + +#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path=" +#define SCAN_CMD_GRP "docker exec --user www-data nextcloud /var/www/html/occ groupfolder:scan " + +#endif // _LOCATIONS_H \ No newline at end of file diff --git a/src/guarder.cpp b/src/guarder.cpp deleted file mode 100644 index 78fb53b..0000000 --- a/src/guarder.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "guarder.h" \ No newline at end of file diff --git a/src/guarder.h b/src/guarder.h deleted file mode 100644 index 3579548..0000000 --- a/src/guarder.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef _GUARDER_H -#define _GUARDER_H - -#include -#include -#include -#include -#include - -class guarder{ -private: - const int shmid; - const int semid; - - void semaphoreOp(int op) { - struct sembuf sb{}; - sb.sem_num = 0; - sb.sem_op = op; - sb.sem_flg = 0; - semop(semid, &sb, 1); - } - - void setFlag(bool value) - { - semaphoreOp(-1); - bool *flag = static_cast(shmat(shmid, nullptr, 0)); - *flag = value; - shmdt(flag); - semaphoreOp(1); - } - -public: - guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT)), semid(semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT)) - { - setFlagOff(); - } - - void setFlagOff() - { - setFlag(false); - } - - void setFlagOn() - { - setFlag(true); - } - - bool isFlagOn() - { - semaphoreOp(-1); - bool *flag = static_cast(shmat(shmid, nullptr, 0)); - bool ret = *flag; - shmdt(flag); - semaphoreOp(1); - return ret; - } - - ~guarder() - { - shmctl(shmid, IPC_RMID, nullptr); - semctl(semid, 0, IPC_RMID); - } - -}; - -#endif // _GUARDER_H \ No newline at end of file diff --git a/src/locations.h b/src/locations.h deleted file mode 100644 index 4b3bab0..0000000 --- a/src/locations.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _LOCATIONS_H -#define _LOCATIONS_H - -#define LOGFILE "journalctl -u smbd --since now -f" - -#define USER_LOG_LOCATION 3 - -#endif // _LOCATIONS_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0a16c9a..8974326 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,8 +12,6 @@ #include #include "usermanager.h" -#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path=" - userManager manager; std::condition_variable cv; std::mutex mtx; @@ -30,7 +28,7 @@ void readingThreadFunc() if (line.find('|') == std::string::npos) continue; - std::vector x = splitLogFile(line, '|'); + std::vector x = splitString(line, '|'); std::string user(x.at(USER_LOG_LOCATION)); { @@ -70,7 +68,7 @@ void scannerThreadFunc() } else if (child == 0) // child { - std::string cmd = (std::string(SCAN_CMD_USR) + user); + std::string cmd = userManager::getScanCommandFromUser(user); execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast(nullptr)); std::cerr << "Scan failed" << std::endl; _exit(EXIT_FAILURE); diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 8fcaaf4..8d1bf13 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -1,9 +1,9 @@ #include "usermanager.h" -std::vector splitLogFile(const std::string& input, char delimiter = '|') +std::vector splitString(const std::string& str, char delimiter = '|') { std::vector ret; - std::stringstream ss(input); + std::stringstream ss(str); std::string token; while (std::getline(ss, token, delimiter)) { @@ -11,4 +11,14 @@ std::vector splitLogFile(const std::string& input, char delimiter = } return ret; +} + +std::string userManager::getScanCommandFromUser(const std::string &user) +{ + if (user.find("__groupfolder") != std::string::npos) + { + return std::string(SCAN_CMD_GRP) + splitString(user, '/').back(); + } + + return std::string(SCAN_CMD_USR) + user; } \ No newline at end of file diff --git a/src/usermanager.h b/src/usermanager.h index 5a7ea14..ff4c4b4 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -7,9 +7,9 @@ #include #include #include -#include "locations.h" +#include "definitions.h" -std::vector splitLogFile(const std::string& input, char delimiter); +std::vector splitString(const std::string& input, char delimiter); class userManager { @@ -19,9 +19,11 @@ private: public: + static std::string getScanCommandFromUser(const std::string&); + void addUserFromLogLine(std::string &line) { - addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION)); + addUser(splitString(line, '|').at(USER_LOG_LOCATION)); } void addUser(std::string &user)