#!/bin/sh downloadsPath="/downloads" profilePath="/config" qbtConfigFile="$profilePath/qBittorrent/config/qBittorrent.conf" isRoot="0" if [ "$(id -u)" = "0" ]; then isRoot="1" 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 "$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 mkdir -p "$(dirname $qbtConfigFile)" cat << EOF > "$qbtConfigFile" [BitTorrent] Session\DefaultSavePath=$downloadsPath Session\Port=6881 Session\TempPath=$downloadsPath/temp [Preferences] WebUI\Port=8080 General\Locale=en EOF fi argLegalNotice="" _legalNotice=$(echo "$QBT_LEGAL_NOTICE" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]') if [ "$_legalNotice" = "confirm" ]; then argLegalNotice="--confirm-legal-notice" else # for backward compatibility # TODO: remove in next major version release _eula=$(echo "$QBT_EULA" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]') if [ "$_eula" = "accept" ]; then echo "QBT_EULA=accept is deprecated and will be removed soon. The replacement is QBT_LEGAL_NOTICE=confirm" argLegalNotice="--confirm-legal-notice" fi fi argTorrentingPort="" if [ -n "$QBT_TORRENTING_PORT" ]; then argTorrentingPort="--torrenting-port=$QBT_TORRENTING_PORT" fi argWebUIPort="" if [ -n "$QBT_WEBUI_PORT" ]; then argWebUIPort="--webui-port=$QBT_WEBUI_PORT" fi 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 if [ -n "$QBT_USERNAME" ] && [ -n "$QBT_PASSWORD" ]; then user="$QBT_USERNAME" pass="$QBT_PASSWORD" salt_hex=$(openssl rand -hex 16) hash_bin=$(openssl kdf -binary \ -keylen 64 \ -kdfopt digest:SHA512 \ -kdfopt pass:"$pass" \ -kdfopt hexsalt:"$salt_hex" \ -kdfopt iter:100000 PBKDF2) hash_b64=$(echo -n "$hash_bin" | base64 -w 0) # convert salt to base64 salt_b64=$(echo "$salt_hex" | xxd -r -p | base64) pbkdf2="@ByteArray(${salt_b64}:${hash_b64})" # ensure enabled line exists if grep -q "^WebUI\\Enabled=" "$qbtConfigFile"; then sed -i "s|^WebUI\\Enabled=.*|WebUI\\Enabled=true|" "$qbtConfigFile" else echo "WebUI\\Enabled=true" >> "$qbtConfigFile" fi # ensure username line exists if grep -q "^WebUI\\Username=" "$qbtConfigFile"; then sed -i "s|^WebUI\\Username=.*|WebUI\\Username=$user|" "$qbtConfigFile" else echo "WebUI\\Username=$user" >> "$qbtConfigFile" fi # ensure password line exists if grep -q "^WebUI\\Password_PBKDF2=" "$qbtConfigFile"; then sed -i "s|^WebUI\\Password_PBKDF2=.*|WebUI\\Password_PBKDF2=$pbkdf2|" "$qbtConfigFile" else echo "WebUI\\Password_PBKDF2=$pbkdf2" >> "$qbtConfigFile" fi fi if [ -n "$QBT_DISABLE_NETWORK" ]; then # Ensure the [BitTorrent] section exists if ! grep -qxF '[BitTorrent]' "$qbtConfigFile"; then echo "[BitTorrent]" >> "$qbtConfigFile" fi # List of options to disable network (POSIX style) for opt in \ "Session\\\\DHTEnabled=false" \ "Session\\\\LSDEnabled=false" \ "Session\\\\MaxActiveDownloads=0" \ "Session\\\\MaxActiveUploads=0" \ "Session\\\\MaxConnections=0" \ "Session\\\\MaxConnectionsPerTorrent=0" \ "Session\\\\PeXEnabled=false"; do # If option exists, replace it; else append after [BitTorrent] if grep -q "^${opt%%=*}" "$qbtConfigFile"; then sed -i "s|^${opt%%=*}=.*|$opt|" "$qbtConfigFile" else sed -i "/^\[BitTorrent\]/a $opt" "$qbtConfigFile" fi done fi if [ "$isRoot" = "1" ]; then exec \ doas -u qbtUser \ qbittorrent-nox \ "$argLegalNotice" \ --profile="$profilePath" \ "$argTorrentingPort" \ "$argWebUIPort" \ "$@" else exec \ qbittorrent-nox \ "$argLegalNotice" \ --profile="$profilePath" \ "$argTorrentingPort" \ "$argWebUIPort" \ "$@" fi