7 Commits

10 changed files with 92 additions and 96 deletions

View File

@@ -1,2 +1,2 @@
all: all:
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher

View File

@@ -1,6 +1,7 @@
[Unit] [Unit]
Description=A service which scan Nextcloud folders Description=A service which scan Nextcloud folders
After=network.target After=network.target docker.service
Requires=docker.service
[Service] [Service]
ExecStart=/usr/bin/ncsambawatcher ExecStart=/usr/bin/ncsambawatcher

18
configs/smb.24.04.conf Normal file
View File

@@ -0,0 +1,18 @@
[global]
vfs objects = full_audit
full_audit:prefix = %u|%I|%m|%S
full_audit:success = mkdirat unlinkat renameat write
full_audit:failure = none
full_audit:facility = local5
full_audit:priority = NOTICE
# Put this line only for the groupfolder's share
[Some gorupfolder share]
full_audit:prefix = %u|%I|%m|__groupfolders/<group-folders-id>
# To disable logs for a specific share
[A share]
vfs objects =

11
src/definitions.h Normal file
View File

@@ -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

View File

@@ -1 +0,0 @@
#include "guarder.h"

View File

@@ -1,66 +0,0 @@
#ifndef _GUARDER_H
#define _GUARDER_H
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
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<bool *>(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<bool *>(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

View File

@@ -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

View File

@@ -1,7 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <set>
#include <array> #include <array>
#include <string> #include <string>
#include <mutex> #include <mutex>
@@ -9,8 +12,6 @@
#include <cstdio> #include <cstdio>
#include "usermanager.h" #include "usermanager.h"
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
userManager manager; userManager manager;
std::condition_variable cv; std::condition_variable cv;
std::mutex mtx; std::mutex mtx;
@@ -27,7 +28,7 @@ void readingThreadFunc()
if (line.find('|') == std::string::npos) if (line.find('|') == std::string::npos)
continue; continue;
std::vector<std::string> x = splitLogFile(line, '|'); std::vector<std::string> x = splitString(line, '|');
std::string user(x.at(USER_LOG_LOCATION)); std::string user(x.at(USER_LOG_LOCATION));
{ {
@@ -45,18 +46,45 @@ void readingThreadFunc()
void scannerThreadFunc() void scannerThreadFunc()
{ {
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
std::vector<pid_t> childrens;
while (true) while (true)
{ {
cv.wait(lock, [] cv.wait(lock, []
{ return manager.isAnybodyInQueue(); }); { return manager.isAnybodyFlagged(); });
std::vector<std::string> scanUsers = manager.getFlaggedUsers(); std::set<std::string> scanUsers = manager.getFlaggedUsers();
manager.unflagAllUsers(); manager.unflagAllUsers();
for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++) lock.unlock();
childrens.clear();
for (const std::string& user : scanUsers)
{ {
system((std::string(SCAN_CMD_USR) + *it).c_str()); pid_t child = fork();
if (child < 0)
{
std::cerr << "Fork failed for: " << user << std::endl;
}
else if (child == 0) // child
{
std::string cmd = userManager::getScanCommandFromUser(user);
execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(nullptr));
std::cerr << "Scan failed" << std::endl;
_exit(EXIT_FAILURE);
}
else // parent
{
childrens.push_back(child);
}
} }
for (const pid_t& pid : childrens)
{
waitpid(pid, nullptr, 0);
}
lock.lock();
} }
} }

View File

@@ -1,9 +1,9 @@
#include "usermanager.h" #include "usermanager.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter = '|') std::vector<std::string> splitString(const std::string& str, char delimiter = '|')
{ {
std::vector<std::string> ret; std::vector<std::string> ret;
std::stringstream ss(input); std::stringstream ss(str);
std::string token; std::string token;
while (std::getline(ss, token, delimiter)) { while (std::getline(ss, token, delimiter)) {
@@ -12,3 +12,13 @@ std::vector<std::string> splitLogFile(const std::string& input, char delimiter =
return ret; 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;
}

View File

@@ -4,11 +4,12 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <set>
#include <sstream> #include <sstream>
#include <mutex> #include <mutex>
#include "locations.h" #include "definitions.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter); std::vector<std::string> splitString(const std::string& input, char delimiter);
class userManager class userManager
{ {
@@ -18,9 +19,11 @@ private:
public: public:
static std::string getScanCommandFromUser(const std::string&);
void addUserFromLogLine(std::string &line) void addUserFromLogLine(std::string &line)
{ {
addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION)); addUser(splitString(line, '|').at(USER_LOG_LOCATION));
} }
void addUser(std::string &user) void addUser(std::string &user)
@@ -71,23 +74,23 @@ public:
} }
} }
std::vector<std::string> getUsers() std::set<std::string> getUsers()
{ {
std::vector<std::string> ret; std::set<std::string> ret;
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it) for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{ {
ret.push_back(it->first); ret.insert(it->first);
} }
return ret; return ret;
} }
std::vector<std::string> getFlaggedUsers() std::set<std::string> getFlaggedUsers()
{ {
std::vector<std::string> ret; std::set<std::string> ret;
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
@@ -95,14 +98,14 @@ public:
{ {
if (it->second) if (it->second)
{ {
ret.push_back(it->first); ret.insert(it->first);
} }
} }
return ret; return ret;
} }
bool isAnybodyInQueue() bool isAnybodyFlagged()
{ {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);