Skip to content

Commit

Permalink
Add support to run image as non-root
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobo1 committed Dec 1, 2024
1 parent 8c54245 commit 19e0318
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 48 deletions.
66 changes: 42 additions & 24 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ downloadsPath="/downloads"
profilePath="/config"
qbtConfigFile="$profilePath/qBittorrent/config/qBittorrent.conf"

if [ -n "$PUID" ] && [ "$PUID" != "$(id -u qbtUser)" ]; then
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PUID:|g" /etc/passwd
isRoot="0"
if [ "$(id -u)" = "0" ]; then
isRoot="1"
fi

if [ -n "$PGID" ] && [ "$PGID" != "$(id -g qbtUser)" ]; then
sed -i "s|^\(qbtUser:x:[0-9]*\):[0-9]*:|\1:$PGID:|g" /etc/passwd
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PGID:|g" /etc/group
fi
if [ "$isRoot" = "1" ]; then
if [ -n "$PUID" ] && [ "$PUID" != "$(id -u qbtUser)" ]; then
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PUID:|g" /etc/passwd
fi

if [ -n "$PAGID" ]; then
_origIFS="$IFS"
IFS=','
for AGID in $PAGID; do
AGID=$(echo "$AGID" | tr -d '[:space:]"')
addgroup -g "$AGID" "qbtGroup-$AGID"
addgroup qbtUser "qbtGroup-$AGID"
done
IFS="$_origIFS"
if [ -n "$PGID" ] && [ "$PGID" != "$(id -g qbtUser)" ]; then
sed -i "s|^\(qbtUser:x:[0-9]*\):[0-9]*:|\1:$PGID:|g" /etc/passwd
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PGID:|g" /etc/group
fi

if [ -n "$PAGID" ]; then
_origIFS="$IFS"
IFS=','
for AGID in $PAGID; do
AGID=$(echo "$AGID" | tr -d '[:space:]"')
addgroup -g "$AGID" "qbtGroup-$AGID"
addgroup qbtUser "qbtGroup-$AGID"
done
IFS="$_origIFS"
fi
fi

if [ ! -f "$qbtConfigFile" ]; then
Expand Down Expand Up @@ -52,24 +59,35 @@ if [ -z "$QBT_WEBUI_PORT" ]; then
QBT_WEBUI_PORT=8080
fi

# those are owned by root by default
# don't change existing files owner in `$downloadsPath`
if [ -d "$downloadsPath" ]; then
chown qbtUser:qbtUser "$downloadsPath"
fi
if [ -d "$profilePath" ]; then
chown qbtUser:qbtUser -R "$profilePath"
if [ "$isRoot" = "1" ]; then
# those are owned by root by default
# don't change existing files owner in `$downloadsPath`
if [ -d "$downloadsPath" ]; then
chown qbtUser:qbtUser "$downloadsPath"
fi
if [ -d "$profilePath" ]; then
chown qbtUser:qbtUser -R "$profilePath"
fi
fi

# set umask just before starting qbt
if [ -n "$UMASK" ]; then
umask "$UMASK"
fi

exec \
doas -u qbtUser \
if [ "$isRoot" = "1" ]; then
exec \
doas -u qbtUser \
qbittorrent-nox \
"$confirmLegalNotice" \
--profile="$profilePath" \
--webui-port="$QBT_WEBUI_PORT" \
"$@"
else
exec \
qbittorrent-nox \
"$confirmLegalNotice" \
--profile="$profilePath" \
--webui-port="$QBT_WEBUI_PORT" \
"$@"
fi
66 changes: 42 additions & 24 deletions manual_build/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ downloadsPath="/downloads"
profilePath="/config"
qbtConfigFile="$profilePath/qBittorrent/config/qBittorrent.conf"

if [ -n "$PUID" ] && [ "$PUID" != "$(id -u qbtUser)" ]; then
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PUID:|g" /etc/passwd
isRoot="0"
if [ "$(id -u)" = "0" ]; then
isRoot="1"
fi

if [ -n "$PGID" ] && [ "$PGID" != "$(id -g qbtUser)" ]; then
sed -i "s|^\(qbtUser:x:[0-9]*\):[0-9]*:|\1:$PGID:|g" /etc/passwd
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PGID:|g" /etc/group
fi
if [ "$isRoot" = "1" ]; then
if [ -n "$PUID" ] && [ "$PUID" != "$(id -u qbtUser)" ]; then
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PUID:|g" /etc/passwd
fi

if [ -n "$PAGID" ]; then
_origIFS="$IFS"
IFS=','
for AGID in $PAGID; do
AGID=$(echo "$AGID" | tr -d '[:space:]"')
addgroup -g "$AGID" "qbtGroup-$AGID"
addgroup qbtUser "qbtGroup-$AGID"
done
IFS="$_origIFS"
if [ -n "$PGID" ] && [ "$PGID" != "$(id -g qbtUser)" ]; then
sed -i "s|^\(qbtUser:x:[0-9]*\):[0-9]*:|\1:$PGID:|g" /etc/passwd
sed -i "s|^qbtUser:x:[0-9]*:|qbtUser:x:$PGID:|g" /etc/group
fi

if [ -n "$PAGID" ]; then
_origIFS="$IFS"
IFS=','
for AGID in $PAGID; do
AGID=$(echo "$AGID" | tr -d '[:space:]"')
addgroup -g "$AGID" "qbtGroup-$AGID"
addgroup qbtUser "qbtGroup-$AGID"
done
IFS="$_origIFS"
fi
fi

if [ ! -f "$qbtConfigFile" ]; then
Expand Down Expand Up @@ -52,24 +59,35 @@ if [ -z "$QBT_WEBUI_PORT" ]; then
QBT_WEBUI_PORT=8080
fi

# those are owned by root by default
# don't change existing files owner in `$downloadsPath`
if [ -d "$downloadsPath" ]; then
chown qbtUser:qbtUser "$downloadsPath"
fi
if [ -d "$profilePath" ]; then
chown qbtUser:qbtUser -R "$profilePath"
if [ "$isRoot" = "1" ]; then
# those are owned by root by default
# don't change existing files owner in `$downloadsPath`
if [ -d "$downloadsPath" ]; then
chown qbtUser:qbtUser "$downloadsPath"
fi
if [ -d "$profilePath" ]; then
chown qbtUser:qbtUser -R "$profilePath"
fi
fi

# set umask just before starting qbt
if [ -n "$UMASK" ]; then
umask "$UMASK"
fi

exec \
doas -u qbtUser \
if [ "$isRoot" = "1" ]; then
exec \
doas -u qbtUser \
qbittorrent-nox \
"$confirmLegalNotice" \
--profile="$profilePath" \
--webui-port="$QBT_WEBUI_PORT" \
"$@"
else
exec \
qbittorrent-nox \
"$confirmLegalNotice" \
--profile="$profilePath" \
--webui-port="$QBT_WEBUI_PORT" \
"$@"
fi

0 comments on commit 19e0318

Please sign in to comment.