22 Commits

Author SHA1 Message Date
e95d9bb552 Change ubuntu image to node
All checks were successful
Build / build (push) Successful in 11s
2025-06-02 16:34:25 +02:00
754cc29a83 Use Github's checkout instead of apt git
Some checks failed
Build / build (push) Failing after 3s
2025-06-02 16:30:25 +02:00
bd9ccd33ef Cloning project in CI
Some checks failed
Build / build (push) Failing after 27s
2025-06-02 16:27:29 +02:00
c0a1b0bbd5 Separate the ci jobs
Some checks failed
Build / build (push) Failing after 23s
2025-06-02 16:21:20 +02:00
cd52871632 Remove sudo from ci.yml
Some checks failed
CI / build (push) Failing after 1s
CI / release (push) Has been skipped
2025-06-02 13:55:27 +02:00
a2058ce2d6 Update ci.yml
Some checks failed
CI / build (push) Failing after 1s
CI / release (push) Has been skipped
2025-06-02 13:53:54 +02:00
bad5123b02 Update ci
Some checks failed
CI / build (push) Failing after 21s
CI / release (push) Has been skipped
2025-06-02 13:45:37 +02:00
15fe1b0e9b Update ci
Some checks failed
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
2025-06-02 13:42:25 +02:00
2951b48680 Added CI
Some checks failed
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
2025-06-02 13:39:06 +02:00
c494e9697c Modify config files 2025-06-02 13:18:02 +02:00
13f18f99ea Better makefile (AI created) 2025-06-02 13:02:52 +02:00
d58a241abb Merge pull request 'Nextcloud's Groupfolder app support' (#3) from groupfolder-extension-support into main
Reviewed-on: #3
2025-06-01 13:06:45 +02:00
2a9d92a3e3 Delete guarder class 2025-06-01 13:03:24 +02:00
ba4cc3faa3 Support groupfolder scan 2025-06-01 13:03:12 +02:00
029d5716be Added smb.conf file 2025-06-01 12:37:53 +02:00
bcd071018f Merge pull request 'Replace multi-process logic to multi-thread logic' (#2) from process-to-threads into main
Reviewed-on: #2
2025-06-01 12:18:06 +02:00
1113a21b2d Update ncsambawatcher.service 2025-06-01 12:15:52 +02:00
c57b022278 Replace scan to a child process 2025-06-01 12:12:59 +02:00
f886dc7162 Multi-process logic changed to multi-thread logic 2025-05-31 15:00:38 +02:00
23d0216a27 Usermanager protected with lock_guards 2025-05-31 13:26:44 +02:00
119285ed71 Merge pull request 'Guarder protected with semaphor' (#1) from semaphor-protection into main
Reviewed-on: #1
2025-05-31 13:10:46 +02:00
dc5c983dd7 Guarder protected with semaphor 2025-05-31 13:00:34 +02:00
14 changed files with 299 additions and 168 deletions

View File

@@ -0,0 +1,30 @@
name: Build
on:
push: {}
defaults:
run:
shell: bash
working-directory: .
jobs:
build:
runs-on: ubuntu
container:
image: node:20
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install tools
run: apt update && apt install -y build-essential
- name: Compile project
run: make
- name: Save build output
uses: actions/upload-artifact@v3
with:
name: ncsambawatcher
path: ./ncsambawatcher

View File

@@ -0,0 +1,49 @@
name: Release
on:
push:
tags:
- v.*
defaults:
run:
shell: bash
working-directory: .
jobs:
release:
runs-on: ubuntu
needs: build
container:
image: node:20
steps:
steps:
- name: Install build tools
run: apt update && apt install -y zip
- name: Download compiled binary
uses: actions/download-artifact@v3
with:
name: ncsambawatcher
- name: Copy files
run: |
mkdir build
cp ncsambawatcher build/ncsambawatcher
cp configs/ncsambawatcher.config.default build/ncsambawatcher.config
cp init.sh config/init.sh
- name: Create release zip
run: |
cd build
zip ../ncsambawatcher.zip ./*
- name: Publish release
uses: actions/create-release@v1
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
with:
tag_name: ${{ github.ref_name }}
release_name: Release ${{ github.ref_name }}
files: |
ncsambawatcher.zip

2
.gitignore vendored
View File

@@ -90,3 +90,5 @@ settings.json
watch.c watch.c
ncwatchfile ncwatchfile
ncsambawatcher ncsambawatcher
obj/

View File

@@ -1,2 +1,31 @@
all: # Compiler and flags
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher CXX := g++
CXXFLAGS := -std=c++17 -Wall -Wextra -O2
# Directories
SRC_DIR := src
OBJ_DIR := obj
BUILD_DIR := .
TARGET := $(BUILD_DIR)/ncsambawatcher
# Create list of source and object files
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(SRCS:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
# Default target
all: $(TARGET)
# Link object files into final binary
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^
# Compile .cpp to .o into obj/
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(OBJ_DIR)
$(CXX) $(CXXFLAGS) -c $< -o $@
# Clean build artifacts
clean:
rm -f $(OBJ_DIR)/*.o $(TARGET)
.PHONY: all clean

View File

@@ -0,0 +1 @@
NEXTCLOUD_CONTAINER_NAME=nextcloud

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

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

@@ -0,0 +1,48 @@
[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
# Example usershare
[<username>] #CHANGEME
path = /path/to/nextcloud/data/<username>/files/ #CHANGEME
valid users = <username> #CHANGEME
force user = www-data
force group = www-data
create mask = 0755
force create mode = 0755
directory mask = 0755
force directory mode = 0755
guest ok = no
public = no
writable = yes
browsable = yes
hide dot files = no
inherit owner = yes
hide unreadable = no
# Example groupfolder share
[Sharename]
path = /path/to/nextcloud/data/__groupfolders/<groupfolder-id> #CHANGEME
valid users = usernames #CHANGEME
force user = www-data
force group = www-data
create mask = 0755
force create mode = 0755
directory mask = 0755
force directory mode = 0755
guest ok = no
public = no
writable = yes
browsable = yes
hide dot files = no
inherit owner = yes
hide unreadable = no
full_audit:prefix = %u|%I|%m|__groupfolders/<groupfolder-id> #CHANGEME
# To disable logs for a specific share, add this line to that share
[Sharename]
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,50 +0,0 @@
#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();
}
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

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,62 +1,23 @@
#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 <vector> #include <mutex>
#include <unistd.h> #include <condition_variable>
#include <sys/types.h> #include <cstdio>
#include <syslog.h>
#include <fcntl.h>
#include <signal.h>
#include "locations.h"
#include "usermanager.h" #include "usermanager.h"
#include "guarder.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 flushManagerToPipe() void readingThreadFunc()
{ {
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;
@@ -67,53 +28,73 @@ int main()
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));
{
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();
} }
}
fclose(logpipe);
close(p1[1]); // write void scannerThreadFunc()
} {
else // child std::unique_lock<std::mutex> lock(mtx);
{ std::vector<pid_t> childrens;
close(p1[1]); // write while (true)
{
int size; cv.wait(lock, []
char *buffer = nullptr; { return manager.isAnybodyFlagged(); });
while (read(p1[0], &size, sizeof(int))) std::set<std::string> scanUsers = manager.getFlaggedUsers();
{ manager.unflagAllUsers();
if (buffer == nullptr)
buffer = new char[size]; lock.unlock();
childrens.clear();
read(p1[0], buffer, size * sizeof(char));
std::string name(buffer); for (const std::string& user : scanUsers)
{
std::cout << "Scan received for: " << name << std::endl; pid_t child = fork();
guard.setFlagOn(); if (child < 0)
system((std::string(SCAN_CMD_USR) + name).data()); {
guard.setFlagOff(); std::cerr << "Fork failed for: " << user << std::endl;
}
kill(parent, SCAN_DONE_SIG); else if (child == 0) // child
{
if (buffer != nullptr) std::string cmd = userManager::getScanCommandFromUser(user);
{ execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(nullptr));
delete[] buffer; std::cerr << "Scan failed" << std::endl;
buffer = nullptr; _exit(EXIT_FAILURE);
} }
} else // parent
{
close(p1[0]); // read childrens.push_back(child);
} }
}
closelog();
for (const pid_t& pid : childrens)
return EXIT_SUCCESS; {
waitpid(pid, nullptr, 0);
}
lock.lock();
}
}
int main()
{
std::thread readingThread(readingThreadFunc);
std::thread scannerThread(scannerThreadFunc);
readingThread.join();
scannerThread.join();
return 0;
} }

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,25 +4,31 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <set>
#include <sstream> #include <sstream>
#include "locations.h" #include <mutex>
#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
{ {
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(splitLogFile(line, '|').at(USER_LOG_LOCATION)); addUser(splitString(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;
@@ -31,16 +37,19 @@ 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;
@@ -49,6 +58,7 @@ 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;
@@ -57,38 +67,56 @@ 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::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);
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);
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.push_back(it->first); ret.insert(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