Update samba.sh

This commit is contained in:
Kroese 2025-10-09 18:44:11 +02:00 committed by GitHub
parent 45956f786f
commit 0da3ac29e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -69,37 +69,37 @@ 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 user = $user"
echo " force group = $group" echo " force group = $group"
} >> "$cfg" } >> "$cfg"
return 0 return 0
@ -114,66 +114,72 @@ addUser() {
local password="$1" local password="$1"
local cfg="$5" local cfg="$5"
# Check if the group exists, if not, create it if [[ "$groupname" != "root" && "$gid" != "0" ]]; then
if ! getent group "$groupname" &>/dev/null; then
if ! groupadd -o -g "$gid" "$groupname" > /dev/null; then # Check if the group exists, if not, create it
error "Failed to create group $groupname" && return 1 if ! getent group "$groupname" &>/dev/null; then
fi if ! groupadd -o -g "$gid" "$groupname" > /dev/null; then
else error "Failed to create group $groupname" && return 1
# Check if the gid is right, if not, change it fi
local current_gid else
current_gid=$(getent group "$groupname" | cut -d: -f3) # Check if the gid is right, if not, change it
if [[ "$current_gid" != "$gid" ]]; then local current_gid
if ! groupmod -o -g "$gid" "$groupname" > /dev/null; then current_gid=$(getent group "$groupname" | cut -d: -f3)
error "Failed to update GID for group $groupname" && return 1 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
fi fi
fi fi
# Check if the user already exists, if not, create it if [[ "$username" != "root" && "$uid" != "0" ]]; then
if ! id "$username" &>/dev/null; then
if ! adduser --gid "$gid" --uid "$uid" --comment "$username" --no-create-home --disabled-login "$username"; then # Check if the user already exists, if not, create it
error "Failed to create user $username" && return 1 if ! id "$username" &>/dev/null; then
fi if ! adduser --gid "$gid" --uid "$uid" --comment "$username" --no-create-home --disabled-login "$username"; then
else error "Failed to create user $username" && return 1
# Check if the uid is right, if not, change it fi
local current_uid else
current_uid=$(id -u "$username") # Check if the uid is right, if not, change it
if [[ "$current_uid" != "$uid" ]]; then local current_uid
if ! usermod -o -u "$uid" "$username" > /dev/null; then current_uid=$(id -u "$username")
error "Failed to update UID for user $username" && return 1 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
fi fi
# Update user's group # Check if the user is a samba user
if ! usermod -g "$groupname" "$username" > /dev/null; then pdb_output=$(pdbedit -s "$cfg" -L)
echo "Failed to update group for user $username" && return 1
fi
fi
# Check if the user is a samba user if echo "$pdb_output" | grep -q "^$username:"; then
pdb_output=$(pdbedit -s "$cfg" -L) # skip samba password update if password is * or !
if [[ "$password" != "*" && "$password" != "!" ]]; then
if echo "$pdb_output" | grep -q "^$username:"; then # If the user is a samba user, update its password in case it changed
# skip samba password update if password is * or ! if ! echo -e "$password\n$password" | smbpasswd -c "$cfg" -s "$username" > /dev/null; then
if [[ "$password" != "*" && "$password" != "!" ]]; then error "Failed to update Samba password for $username" && return 1
# If the user is a samba user, update its password in case it changed fi
if ! echo -e "$password\n$password" | smbpasswd -c "$cfg" -s "$username" > /dev/null; then fi
error "Failed to update Samba password for $username" && return 1 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
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 fi
return 0 return 0
} }
SAMBA_USER="root"
SAMBA_GROUP="root"
SAMBA_CONFIG="/etc/samba/smb.conf" SAMBA_CONFIG="/etc/samba/smb.conf"
{ echo "[global]" { echo "[global]"
@ -199,14 +205,10 @@ SAMBA_CONFIG="/etc/samba/smb.conf"
} > "$SAMBA_CONFIG" } > "$SAMBA_CONFIG"
# Setup user and group # Setup user and group
if [[ "$SAMBA_UID" != "1000" || "$SAMBA_GID" != "1000" ]]; then [[ "$SAMBA_UID" == "0" ]] && SAMBA_USER="root" || SAMBA_USER="samba"
[[ "$SAMBA_GID" == "0" ]] && SAMBA_GROUP="root" || SAMBA_GROUP="samba"
SAMBA_USER="samba" ! addUser "$SAMBA_USER" "$SAMBA_UID" "$SAMBA_GROUP" "$SAMBA_GID" "$SAMBA_CONFIG" && return 0
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"