Compare commits
1 Commits
v1.0
...
multi-proc
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c441cde92 |
2
Makefile
2
Makefile
@@ -1,2 +1,2 @@
|
|||||||
all:
|
all:
|
||||||
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher
|
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=A service which scan Nextcloud folders
|
Description=A service which scan Nextcloud folders
|
||||||
After=network.target docker.service
|
After=network.target
|
||||||
Requires=docker.service
|
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/ncsambawatcher
|
ExecStart=/usr/bin/ncsambawatcher
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
[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 =
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#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
|
|
||||||
1
src/guarder.cpp
Normal file
1
src/guarder.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "guarder.h"
|
||||||
52
src/guarder.h
Normal file
52
src/guarder.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef _GUARDER_H
|
||||||
|
#define _GUARDER_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
|
||||||
|
class guarder{
|
||||||
|
private:
|
||||||
|
const int shmid;
|
||||||
|
|
||||||
|
void setFlag(bool value)
|
||||||
|
{
|
||||||
|
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
||||||
|
*flag = value;
|
||||||
|
shmdt(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT))
|
||||||
|
{
|
||||||
|
setFlagOff();
|
||||||
|
}
|
||||||
|
|
||||||
|
guarder(guarder &g) : shmid(g.shmid) {}
|
||||||
|
|
||||||
|
void setFlagOff()
|
||||||
|
{
|
||||||
|
setFlag(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setFlagOn()
|
||||||
|
{
|
||||||
|
setFlag(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isFlagOn()
|
||||||
|
{
|
||||||
|
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
||||||
|
bool ret = *flag;
|
||||||
|
shmdt(flag);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
~guarder()
|
||||||
|
{
|
||||||
|
shmctl(shmid, IPC_RMID, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _GUARDER_H
|
||||||
8
src/locations.h
Normal file
8
src/locations.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _LOCATIONS_H
|
||||||
|
#define _LOCATIONS_H
|
||||||
|
|
||||||
|
#define LOGFILE "journalctl -u smbd --since now -f"
|
||||||
|
|
||||||
|
#define USER_LOG_LOCATION 3
|
||||||
|
|
||||||
|
#endif // _LOCATIONS_H
|
||||||
144
src/main.cpp
144
src/main.cpp
@@ -1,23 +1,63 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/wait.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
|
||||||
#include <set>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <vector>
|
||||||
#include <condition_variable>
|
#include <unistd.h>
|
||||||
#include <cstdio>
|
#include <sys/types.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include "locations.h"
|
||||||
#include "usermanager.h"
|
#include "usermanager.h"
|
||||||
|
#include "guarder.h"
|
||||||
|
#include "processManager.h"
|
||||||
|
|
||||||
|
#define MAXNAMESIZE 255
|
||||||
|
#define SCAN_DONE_SIG SIGRTMIN
|
||||||
|
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
|
||||||
|
|
||||||
|
int p1[2];
|
||||||
|
guarder guard;
|
||||||
userManager manager;
|
userManager manager;
|
||||||
std::condition_variable cv;
|
|
||||||
std::mutex mtx;
|
|
||||||
|
|
||||||
void readingThreadFunc()
|
void flushManagerToPipe()
|
||||||
{
|
{
|
||||||
|
if (!guard.isFlagOn())
|
||||||
|
{
|
||||||
|
std::vector<std::string> users = manager.getFlaggedUsers();
|
||||||
|
|
||||||
|
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
|
{
|
||||||
|
int size = it->size();
|
||||||
|
write(p1[1], &size, sizeof(int));
|
||||||
|
write(p1[1], it->data(), size * sizeof(char));
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.unflagAllUsers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handler(int sig)
|
||||||
|
{
|
||||||
|
if (sig == SCAN_DONE_SIG)
|
||||||
|
{
|
||||||
|
flushManagerToPipe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||||
|
pipe(p1);
|
||||||
|
|
||||||
|
pid_t parent = getpid();
|
||||||
|
pid_t child = fork();
|
||||||
|
|
||||||
|
if (child > 0) // parent
|
||||||
|
{
|
||||||
|
signal(SCAN_DONE_SIG, handler);
|
||||||
|
close(p1[0]); // read
|
||||||
|
|
||||||
FILE *logpipe = popen(LOGFILE, "r");
|
FILE *logpipe = popen(LOGFILE, "r");
|
||||||
std::array<char, 256> buffer;
|
std::array<char, 256> buffer;
|
||||||
|
|
||||||
@@ -28,73 +68,53 @@ void readingThreadFunc()
|
|||||||
if (line.find('|') == std::string::npos)
|
if (line.find('|') == std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
std::vector<std::string> x = splitString(line, '|');
|
std::vector<std::string> x = splitLogFile(line, '|');
|
||||||
std::string user(x.at(USER_LOG_LOCATION));
|
std::string user(x.at(USER_LOG_LOCATION));
|
||||||
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
manager.addUser(user);
|
manager.addUser(user);
|
||||||
manager.setUserFlagged(user);
|
manager.setUserFlagged(user);
|
||||||
}
|
|
||||||
|
|
||||||
cv.notify_one();
|
|
||||||
|
|
||||||
std::cout << "User find: " << user << std::endl;
|
std::cout << "User find: " << user << std::endl;
|
||||||
}
|
|
||||||
|
flushManagerToPipe();
|
||||||
}
|
}
|
||||||
|
|
||||||
void scannerThreadFunc()
|
fclose(logpipe);
|
||||||
|
close(p1[1]); // write
|
||||||
|
}
|
||||||
|
else // child
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
close(p1[1]); // write
|
||||||
std::vector<pid_t> childrens;
|
|
||||||
while (true)
|
int size;
|
||||||
|
char *buffer = nullptr;
|
||||||
|
processManager pm;
|
||||||
|
|
||||||
|
while (read(p1[0], &size, sizeof(int)))
|
||||||
{
|
{
|
||||||
cv.wait(lock, []
|
if (buffer == nullptr)
|
||||||
{ return manager.isAnybodyFlagged(); });
|
buffer = new char[size];
|
||||||
|
|
||||||
std::set<std::string> scanUsers = manager.getFlaggedUsers();
|
read(p1[0], buffer, size * sizeof(char));
|
||||||
manager.unflagAllUsers();
|
std::string name(buffer);
|
||||||
|
std::string cmd = std::string(SCAN_CMD_USR) + name;
|
||||||
|
|
||||||
lock.unlock();
|
std::cout << "Scan received for: " << name << std::endl;
|
||||||
childrens.clear();
|
|
||||||
|
|
||||||
for (const std::string& user : scanUsers)
|
pm.runTask(name, cmd);
|
||||||
|
|
||||||
|
kill(parent, SCAN_DONE_SIG);
|
||||||
|
|
||||||
|
if (buffer != nullptr)
|
||||||
{
|
{
|
||||||
pid_t child = fork();
|
delete[] buffer;
|
||||||
|
buffer = nullptr;
|
||||||
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)
|
close(p1[0]); // read
|
||||||
{
|
|
||||||
waitpid(pid, nullptr, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.lock();
|
closelog();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
return EXIT_SUCCESS;
|
||||||
{
|
|
||||||
std::thread readingThread(readingThreadFunc);
|
|
||||||
std::thread scannerThread(scannerThreadFunc);
|
|
||||||
|
|
||||||
readingThread.join();
|
|
||||||
scannerThread.join();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
41
src/processManager.h
Normal file
41
src/processManager.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef _PROCCESSMANAGER_H
|
||||||
|
#define _PROCCESSMANAGER_H
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include "guarder.h"
|
||||||
|
#include "usermanager.h"
|
||||||
|
|
||||||
|
class processManager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::map<std::string, guarder> running;
|
||||||
|
|
||||||
|
bool getIdTag(std::string &id)
|
||||||
|
{
|
||||||
|
return running[id].isFlagOn();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void runTask(std::string &id, std::string &cmdCommand)
|
||||||
|
{
|
||||||
|
if (!getIdTag(id))
|
||||||
|
{
|
||||||
|
running[id].setFlagOn();
|
||||||
|
pid_t child = fork();
|
||||||
|
|
||||||
|
if (child == 0) // child
|
||||||
|
{
|
||||||
|
system(cmdCommand.c_str());
|
||||||
|
running[id].setFlagOff();
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _PROCCESSMANAGER_H
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#include "usermanager.h"
|
#include "usermanager.h"
|
||||||
|
|
||||||
std::vector<std::string> splitString(const std::string& str, char delimiter = '|')
|
std::vector<std::string> splitLogFile(const std::string& input, char delimiter = '|')
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
std::stringstream ss(str);
|
std::stringstream ss(input);
|
||||||
std::string token;
|
std::string token;
|
||||||
|
|
||||||
while (std::getline(ss, token, delimiter)) {
|
while (std::getline(ss, token, delimiter)) {
|
||||||
@@ -12,13 +12,3 @@ std::vector<std::string> splitString(const std::string& str, 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;
|
|
||||||
}
|
|
||||||
@@ -4,31 +4,25 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <set>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <mutex>
|
#include "locations.h"
|
||||||
#include "definitions.h"
|
|
||||||
|
|
||||||
std::vector<std::string> splitString(const std::string& input, char delimiter);
|
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
|
||||||
|
|
||||||
class userManager
|
class userManager
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::map<std::string, bool> users;
|
std::map<std::string, bool> users;
|
||||||
std::mutex mtx;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static std::string getScanCommandFromUser(const std::string&);
|
|
||||||
|
|
||||||
void addUserFromLogLine(std::string &line)
|
void addUserFromLogLine(std::string &line)
|
||||||
{
|
{
|
||||||
addUser(splitString(line, '|').at(USER_LOG_LOCATION));
|
addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION));
|
||||||
}
|
}
|
||||||
|
|
||||||
void addUser(std::string &user)
|
void addUser(std::string &user)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (users.count(user) == 0)
|
if (users.count(user) == 0)
|
||||||
{
|
{
|
||||||
users[user] = false;
|
users[user] = false;
|
||||||
@@ -37,19 +31,16 @@ public:
|
|||||||
|
|
||||||
void removeUser(std::string &user)
|
void removeUser(std::string &user)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
users.erase(user);
|
users.erase(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isContains(std::string &user)
|
bool isContains(std::string &user)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
return users.count(user) == 1;
|
return users.count(user) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUserFlagged(std::string &user)
|
void setUserFlagged(std::string &user)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (users.count(user) == 1)
|
if (users.count(user) == 1)
|
||||||
{
|
{
|
||||||
users[user] = true;
|
users[user] = true;
|
||||||
@@ -58,7 +49,6 @@ public:
|
|||||||
|
|
||||||
void setUserUnflagged(std::string &user)
|
void setUserUnflagged(std::string &user)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (users.count(user) == 1)
|
if (users.count(user) == 1)
|
||||||
{
|
{
|
||||||
users[user] = false;
|
users[user] = false;
|
||||||
@@ -67,56 +57,38 @@ public:
|
|||||||
|
|
||||||
void unflagAllUsers()
|
void unflagAllUsers()
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
it->second = false;
|
it->second = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<std::string> getUsers()
|
std::vector<std::string> getUsers()
|
||||||
{
|
{
|
||||||
std::set<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
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.insert(it->first);
|
ret.push_back(it->first);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<std::string> getFlaggedUsers()
|
std::vector<std::string> getFlaggedUsers()
|
||||||
{
|
{
|
||||||
std::set<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (it->second)
|
if (it->second)
|
||||||
{
|
{
|
||||||
ret.insert(it->first);
|
ret.push_back(it->first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isAnybodyFlagged()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
|
||||||
{
|
|
||||||
if (it->second)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _USERMAN_H
|
#endif // _USERMAN_H
|
||||||
Reference in New Issue
Block a user