feat: Shared files inherit ownership from parent folder (#1465)

This commit is contained in:
Kroese 2025-10-10 06:28:20 +02:00 committed by GitHub
parent 45956f786f
commit 135fd38778
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,8 +4,6 @@ set -Eeuo pipefail
: "${SAMBA:="Y"}" # Enable Samba : "${SAMBA:="Y"}" # Enable Samba
: "${SAMBA_LEVEL:="1"}" # Logging level : "${SAMBA_LEVEL:="1"}" # Logging level
: "${SAMBA_DEBUG:="N"}" # Disable debug : "${SAMBA_DEBUG:="N"}" # Disable debug
: "${SAMBA_UID:="1000"}" # Samba user ID
: "${SAMBA_GID:="1000"}" # Samba group ID
tmp="/tmp/smb" tmp="/tmp/smb"
rm -rf "$tmp" rm -rf "$tmp"
@ -37,6 +35,7 @@ else
fi fi
html "Initializing shared folder..." html "Initializing shared folder..."
SAMBA_CONFIG="/etc/samba/smb.conf"
[[ "$DEBUG" == [Yy1]* ]] && echo "Starting Samba daemon..." [[ "$DEBUG" == [Yy1]* ]] && echo "Starting Samba daemon..."
addShare() { addShare() {
@ -44,9 +43,8 @@ addShare() {
local ref="$2" local ref="$2"
local name="$3" local name="$3"
local comment="$4" local comment="$4"
local user="$5" local cfg="$5"
local group="$6" local owner=""
local cfg="$7"
mkdir -p "$dir" || return 1 mkdir -p "$dir" || return 1
@ -55,13 +53,12 @@ addShare() {
fi fi
if [ -z "$(ls -A "$dir")" ]; then if [ -z "$(ls -A "$dir")" ]; then
if ! chmod 2777 "$dir"; then
if ! chmod 777 "$dir"; then
error "Failed to set permissions for directory $dir" && return 1 error "Failed to set permissions for directory $dir" && return 1
fi fi
owner=$(stat -c %u "$dir")
if [[ "$user" != "root" || "$group" != "root" ]]; then if [[ "$owner" == "0" ]]; then
if ! chown "$user:$group" "$dir" ; then if ! chown "1000:1000" "$dir"; then
error "Failed to set ownership for directory $dir" && return 1 error "Failed to set ownership for directory $dir" && return 1
fi fi
fi fi
@ -69,119 +66,47 @@ addShare() {
if [[ "$dir" == "$tmp" ]]; then if [[ "$dir" == "$tmp" ]]; then
{ echo "--------------------------------------------------------" { echo "--------------------------------------------------------"
echo " $APP for $ENGINE v$(</run/version)..." echo " $APP for $ENGINE v$(</run/version)..."
echo " For support visit $SUPPORT" echo " For support visit $SUPPORT"
echo "--------------------------------------------------------" echo "--------------------------------------------------------"
echo "" echo ""
echo "Using this folder you can exchange files with the host machine." echo "Using this folder you can exchange files with the host machine."
echo "" echo ""
echo "To select a folder on the host for this purpose, include the following bind mount in your compose file:" echo "To select a folder on the host for this purpose, include the following bind mount in your compose file:"
echo "" echo ""
echo " volumes:" echo " volumes:"
echo " - \"./example:${ref}\"" echo " - \"./example:${ref}\""
echo "" echo ""
echo "Or in your run command:" echo "Or in your run command:"
echo "" echo ""
echo " -v \"\${PWD:-.}/example:${ref}\"" echo " -v \"\${PWD:-.}/example:${ref}\""
echo "" echo ""
echo "Replace the example path ./example with your desired shared folder, which then will become visible here." echo "Replace the example path ./example with your desired shared folder, which then will become visible here."
echo "" echo ""
} | unix2dos > "$dir/readme.txt" } | unix2dos > "$dir/readme.txt"
fi fi
{ echo "" { echo ""
echo "[$name]" echo "[$name]"
echo " path = $dir" echo " path = $dir"
echo " comment = $comment" echo " comment = $comment"
echo " writable = yes" echo " writable = yes"
echo " guest ok = yes" echo " guest ok = yes"
echo " guest only = yes" echo " guest only = yes"
echo " force user = $user"
echo " force group = $group"
} >> "$cfg" } >> "$cfg"
return 0 return 0
} }
addUser() {
local username="$1"
local uid="$2"
local groupname="$3"
local gid="$4"
local password="$1"
local cfg="$5"
# Check if the group exists, if not, create it
if ! getent group "$groupname" &>/dev/null; then
if ! groupadd -o -g "$gid" "$groupname" > /dev/null; then
error "Failed to create group $groupname" && return 1
fi
else
# Check if the gid is right, if not, change it
local current_gid
current_gid=$(getent group "$groupname" | cut -d: -f3)
if [[ "$current_gid" != "$gid" ]]; then
if ! groupmod -o -g "$gid" "$groupname" > /dev/null; then
error "Failed to update GID for group $groupname" && return 1
fi
fi
fi
# Check if the user already exists, if not, create it
if ! id "$username" &>/dev/null; then
if ! adduser --gid "$gid" --uid "$uid" --comment "$username" --no-create-home --disabled-login "$username"; then
error "Failed to create user $username" && return 1
fi
else
# Check if the uid is right, if not, change it
local current_uid
current_uid=$(id -u "$username")
if [[ "$current_uid" != "$uid" ]]; then
if ! usermod -o -u "$uid" "$username" > /dev/null; then
error "Failed to update UID for user $username" && return 1
fi
fi
# Update user's group
if ! usermod -g "$groupname" "$username" > /dev/null; then
echo "Failed to update group for user $username" && return 1
fi
fi
# Check if the user is a samba user
pdb_output=$(pdbedit -s "$cfg" -L)
if echo "$pdb_output" | grep -q "^$username:"; then
# skip samba password update if password is * or !
if [[ "$password" != "*" && "$password" != "!" ]]; then
# If the user is a samba user, update its password in case it changed
if ! echo -e "$password\n$password" | smbpasswd -c "$cfg" -s "$username" > /dev/null; then
error "Failed to update Samba password for $username" && return 1
fi
fi
else
# If the user is not a samba user, create it and set a password
if ! echo -e "$password\n$password" | smbpasswd -a -c "$cfg" -s "$username" > /dev/null; then
error "Failed to add Samba user $username" && return 1
fi
fi
return 0
}
SAMBA_USER="root"
SAMBA_GROUP="root"
SAMBA_CONFIG="/etc/samba/smb.conf"
{ echo "[global]" { echo "[global]"
echo " server string = Dockur" echo " server string = Dockur"
echo " netbios name = $hostname" echo " netbios name = $hostname"
echo " workgroup = WORKGROUP" echo " workgroup = WORKGROUP"
echo " interfaces = $interfaces" echo " interfaces = $interfaces"
echo " bind interfaces only = yes" echo " bind interfaces only = yes"
echo " socket address = $socket"
echo " security = user" echo " security = user"
echo " guest account = nobody" echo " guest account = nobody"
echo " map to guest = Bad User" echo " map to guest = Bad User"
@ -189,25 +114,21 @@ SAMBA_CONFIG="/etc/samba/smb.conf"
echo " follow symlinks = yes" echo " follow symlinks = yes"
echo " wide links = yes" echo " wide links = yes"
echo " unix extensions = no" echo " unix extensions = no"
echo " socket address = $socket" echo " inherit owner = yes"
echo " create mask = 0666"
echo " directory mask = 02777"
echo " force user = root"
echo " force group = root"
echo " force create mode = 0666"
echo " force directory mode = 02777"
echo "" echo ""
echo " # disable printing services" echo " # Disable printing services"
echo " load printers = no" echo " load printers = no"
echo " printing = bsd" echo " printing = bsd"
echo " printcap name = /dev/null" echo " printcap name = /dev/null"
echo " disable spoolss = yes" echo " disable spoolss = yes"
} > "$SAMBA_CONFIG" } > "$SAMBA_CONFIG"
# Setup user and group
if [[ "$SAMBA_UID" != "1000" || "$SAMBA_GID" != "1000" ]]; then
SAMBA_USER="samba"
SAMBA_GROUP="samba"
! addUser "$SAMBA_USER" "$SAMBA_UID" "$SAMBA_GROUP" "$SAMBA_GID" "$SAMBA_CONFIG" && return 0
fi
# Add shared folders # Add shared folders
share="/shared" share="/shared"
[ ! -d "$share" ] && [ -d "$STORAGE/shared" ] && share="$STORAGE/shared" [ ! -d "$share" ] && [ -d "$STORAGE/shared" ] && share="$STORAGE/shared"
@ -218,33 +139,26 @@ share="/shared"
m1="Failed to add shared folder" m1="Failed to add shared folder"
m2="Please check its permissions." m2="Please check its permissions."
if ! addShare "$share" "/shared" "Data" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG"; then if ! addShare "$share" "/shared" "Data" "Shared" "$SAMBA_CONFIG"; then
error "$m1 '$share'. $m2" && return 0 error "$m1 '$share'. $m2" && return 0
fi fi
if [ -d "/shared2" ]; then if [ -d "/shared2" ]; then
addShare "/shared2" "/shared2" "Data2" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/shared2'. $m2" addShare "/shared2" "/shared2" "Data2" "Shared" "$SAMBA_CONFIG" || error "$m1 '/shared2'. $m2"
else else
if [ -d "/data2" ]; then if [ -d "/data2" ]; then
addShare "/data2" "/shared2" "Data2" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/data2'. $m2." addShare "/data2" "/shared2" "Data2" "Shared" "$SAMBA_CONFIG" || error "$m1 '/data2'. $m2."
fi fi
fi fi
if [ -d "/shared3" ]; then if [ -d "/shared3" ]; then
addShare "/shared3" "/shared3" "Data3" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/shared3'. $m2" addShare "/shared3" "/shared3" "Data3" "Shared" "$SAMBA_CONFIG" || error "$m1 '/shared3'. $m2"
else else
if [ -d "/data3" ]; then if [ -d "/data3" ]; then
addShare "/data3" "/shared3" "Data3" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/data3'. $m2" addShare "/data3" "/shared3" "Data3" "Shared" "$SAMBA_CONFIG" || error "$m1 '/data3'. $m2"
fi fi
fi fi
IFS=',' read -r -a dirs <<< "${SHARES:-}"
for dir in "${dirs[@]}"; do
[ ! -d "$dir" ] && continue
dir_name=$(basename "$dir")
addShare "$dir" "/shared" "$dir_name" "Shared $dir_name" || error "Failed to create shared folder for $dir!"
done
# Create directories if missing # Create directories if missing
mkdir -p /var/lib/samba/sysvol mkdir -p /var/lib/samba/sysvol
mkdir -p /var/lib/samba/private mkdir -p /var/lib/samba/private