Skip to content

Commit

Permalink
Merge branch 'fix/permissions-after-unpack' of github.com:nzbgetcom/n…
Browse files Browse the repository at this point in the history
…zbget into feature/test-fix-permissions
  • Loading branch information
phnzb committed Apr 12, 2024
2 parents 2f40327 + 5531cd1 commit 4ec66f3
Show file tree
Hide file tree
Showing 31 changed files with 1,879 additions and 901 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ project(

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g0 -pthread -g -Weverything -Wno-c++98-compat" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g0 -pthread -g -DDEBUG -Weverything -Wno-c++98-compat" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-O0 -g0 -pthread -g -Wall -Wextra" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-O0 -g0 -pthread -g -DDEBUG -Wall -Wextra" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "/Od /Zi /MTd /MP /W4 /EHs /DDEBUG /D_DEBUG /DWIN32 /wd4800 /wd4267" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} winmm.lib Dbghelp.lib libcpmtd.lib" CACHE STRING "" FORCE)
Expand Down
5 changes: 3 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ webui_FILES = \
webui/messages.js \
webui/status.js \
webui/style.css \
webui/light-theme.css \
webui/dark-theme.css \
webui/upload.js \
webui/util.js \
webui/config.js \
Expand All @@ -365,8 +367,7 @@ webui_FILES = \
webui/lib/raphael.min.js \
webui/lib/elycharts.js \
webui/lib/elycharts.min.js \
webui/img/house-16.ico \
webui/img/download-16.ico \
webui/lib/material-icons.woff2 \
webui/img/icons.png \
webui/img/icons-2x.png \
webui/img/transmit.gif \
Expand Down
20 changes: 16 additions & 4 deletions daemon/postprocess/Unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2013-2018 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down Expand Up @@ -714,11 +715,11 @@ bool UnpackController::Cleanup()
{
// moving files back
DirBrowser dir(m_unpackDir);
const char* destDir = !m_finalDir.Empty() ? *m_finalDir : *m_destDir;
while (const char* filename = dir.Next())
{
BString<1024> srcFile("%s%c%s", *m_unpackDir, PATH_SEPARATOR, filename);
BString<1024> dstFile("%s%c%s", !m_finalDir.Empty() ? *m_finalDir : *m_destDir,
PATH_SEPARATOR, *FileSystem::MakeValidFilename(filename));
BString<1024> dstFile("%s%c%s", destDir, PATH_SEPARATOR, *FileSystem::MakeValidFilename(filename));

// silently overwrite existing files
FileSystem::DeleteFile(dstFile);
Expand All @@ -732,8 +733,19 @@ bool UnpackController::Cleanup()
ok = false;
}

#ifndef WIN32
// Fixing file or directory permissions overridden by the unpacker
FileSystem::SetFileOrDirPermissionsWithUMask(dstFile.Str(), g_Options->GetUMask());
#endif

extractedFiles.push_back(filename);
}

#ifndef WIN32
// Fixing directory permissions overridden by the unpacker
FileSystem::SetDirPermissionsWithUMask(destDir, g_Options->GetUMask());
#endif

}

CString errmsg;
Expand Down
57 changes: 57 additions & 0 deletions daemon/util/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +22,7 @@
#include "nzbget.h"
#include "FileSystem.h"
#include "Util.h"
#include "Log.h"

const char* RESERVED_DEVICE_NAMES[] = { "CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
Expand Down Expand Up @@ -956,6 +958,61 @@ void FileSystem::FixExecPermission(const char* filename)
chmod(filename, buffer.st_mode);
}
}

bool FileSystem::SetFileOrDirPermissionsWithUMask(const char* filename, int umask)
{
struct stat buffer;
int ec = stat(filename, &buffer);
if (ec != 0)
{

#ifdef DEBUG
debug("Failed to read information for %s. Errno: %i", filename, ec);
#endif

return false;
}

if (S_ISDIR(buffer.st_mode))
{
return SetDirPermissionsWithUMask(filename, umask);
}

return SetFilePermissionsWithUMask(filename, umask);
}

bool FileSystem::SetFilePermissionsWithUMask(const char* filepath, int umask)
{
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // 0666
return SetPermissionsWithUMask(filepath, mode, umask);
}

bool FileSystem::SetDirPermissionsWithUMask(const char* filename, int umask)
{
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; // 0777
return SetPermissionsWithUMask(filename, mode, umask);
}

bool FileSystem::SetPermissionsWithUMask(const char* filename, mode_t mode, int umask)
{
mode_t permissions = mode & ~umask;
int ec = chmod(filename, permissions);
if (ec == 0)
{

#ifdef DEBUG
debug("Permissions %o was set for %s", permissions, filename);
#endif

return true;
}

#ifdef DEBUG
debug("Failed to set permissions for %s. Errno %i", filename, ec);
#endif

return false;
}
#endif

#ifdef WIN32
Expand Down
7 changes: 6 additions & 1 deletion daemon/util/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down Expand Up @@ -61,6 +62,10 @@ class FileSystem
#ifndef WIN32
static CString ExpandHomePath(const char* filename);
static void FixExecPermission(const char* filename);
static bool SetFileOrDirPermissionsWithUMask(const char* filename, int umask);
static bool SetFilePermissionsWithUMask(const char* filename, int umask);
static bool SetDirPermissionsWithUMask(const char* filename, int umask);
static bool SetPermissionsWithUMask(const char* filename, mode_t mode, int umask);
#endif
static CString ExpandFileName(const char* filename);
static CString GetExeFileName(const char* argv0);
Expand Down
26 changes: 15 additions & 11 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ if [ -f /downloads/nzbget.lock ]; then
rm /downloads/nzbget.lock
fi

# change userid and groupid
PUID=${PUID:-1000}
PGID=${PGID:-1000}
groupmod -o -g "$PGID" users >/dev/null
usermod -o -u "$PUID" user >/dev/null

# create default config if not exist
if [ ! -f /config/nzbget.conf ]; then
cp /app/nzbget/share/nzbget/nzbget.conf /config/nzbget.conf
chown user:users /config/nzbget.conf
fi

# create scripts dir
if [ ! -d /downloads/scripts ]; then
mkdir -p /downloads/scripts
chown user:users /downloads/scripts
fi

# parse env vars to options
Expand All @@ -25,21 +38,12 @@ if [ ! -z "${NZBGET_PASS}" ]; then
OPTIONS="${OPTIONS}-o ControlPassword=${NZBGET_PASS} "
fi

# create scripts dir
mkdir -p /downloads/scripts

# change userid and groupid
PUID=${PUID:-1000}
PGID=${PGID:-1000}
groupmod -o -g "$PGID" users >/dev/null
usermod -o -u "$PUID" user >/dev/null

chown -R user:users /config || CONFIG_CHOWN_STATUS=$?
chown user:users /config || CONFIG_CHOWN_STATUS=$?
if [ ! -z $CONFIG_CHOWN_STATUS ]; then
echo "*** Could not set permissions on /config ; this container may not work as expected ***"
fi

chown -R user:users /downloads || DOWNLOADS_CHOWN_STATUS=$?
chown user:users /downloads || DOWNLOADS_CHOWN_STATUS=$?
if [ ! -z $DOWNLOADS_CHOWN_STATUS ]; then
echo "*** Could not set permissions on /downloads ; this container may not work as expected ***"
fi
Expand Down
Binary file modified osx/Resources/Images/mainicon.icns
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/util/JsonTest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* CCopyright (C) 2023 Denis <denis@nzbget.com>
* Copyright (C) 2023 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
Loading

0 comments on commit 4ec66f3

Please sign in to comment.