Skip to content

Commit 8c78125

Browse files
authored
fix: Dont change permissions on existing files
1 parent 9312ced commit 8c78125

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

samba.sh

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ set -Eeuo pipefail
55
group="smb"
66
share="/storage"
77

8+
# Create shared directory
9+
mkdir -p "$share" || { echo "Failed to create directory $share"; exit 1; }
10+
11+
# Copy config file template
12+
rm -f /etc/samba/smb.custom
13+
cp /etc/samba/smb.conf /etc/samba/smb.custom
14+
815
# Check if the smb group exists, if not, create it
916
if ! getent group "$group" &>/dev/null; then
1017
groupadd "$group" || { echo "Failed to create group $group"; exit 1; }
@@ -21,37 +28,36 @@ OldGID=$(getent group "$group" | cut -d: -f3)
2128

2229
# Change the UID and GID of the user and group if necessary
2330
if [[ "$OldUID" != "$UID" ]]; then
24-
usermod -u "$UID" "$USER" || { echo "Failed to change UID for $USER"; exit 1; }
31+
usermod -o -u "$UID" "$USER" || { echo "Failed to change UID for $USER"; exit 1; }
2532
fi
2633

2734
if [[ "$OldGID" != "$GID" ]]; then
28-
groupmod -g "$GID" "$group" || { echo "Failed to change GID for group $group"; exit 1; }
35+
groupmod -o -g "$GID" "$group" || { echo "Failed to change GID for group $group"; exit 1; }
2936
fi
3037

31-
# Change ownership of files and directories
32-
find / -path "$share" -prune -o -group "$OldGID" -exec chgrp -h "$group" {} \;
33-
find / -path "$share" -prune -o -user "$OldUID" -exec chown -h "$USER" {} \;
34-
3538
# Change Samba password
3639
echo -e "$PASS\n$PASS" | smbpasswd -a -s "$USER" || { echo "Failed to change Samba password for $USER"; exit 1; }
3740

38-
rm -f /etc/samba/smb.custom
39-
cp /etc/samba/smb.conf /etc/samba/smb.custom
40-
4141
# Update force user and force group in smb.conf
4242
sed -i "s/^\(\s*\)force user =.*/\1force user = $USER/" "/etc/samba/smb.custom"
4343
sed -i "s/^\(\s*\)force group =.*/\1force group = $group/" "/etc/samba/smb.custom"
4444

45-
# Verify if the RW variable is not equal to true (indicating read-only mode) and adjust settings accordingly
46-
if [[ "$RW" != "true" ]]; then
45+
# Verify if the RW variable is equal to false (indicating read-only mode)
46+
if [[ "$RW" == [Ff0]* ]]; then
47+
48+
# Adjust settings in smb.conf to set share to read-only
4749
sed -i "s/^\(\s*\)writable =.*/\1writable = no/" "/etc/samba/smb.custom"
4850
sed -i "s/^\(\s*\)read only =.*/\1read only = yes/" "/etc/samba/smb.custom"
49-
fi
5051

51-
# Create shared directory and set permissions
52-
mkdir -p "$share" || { echo "Failed to create directory $share"; exit 1; }
53-
chmod -R 0770 "$share" || { echo "Failed to set permissions for directory $share"; exit 1; }
54-
chown -R "$USER:$group" "$share" || { echo "Failed to set ownership for directory $share"; exit 1; }
52+
else
53+
54+
# Set permissions for share directory if new (empty), leave untouched if otherwise
55+
if [ -z "$(ls -A "$share")" ]; then
56+
chmod 0770 "$share" || { echo "Failed to set permissions for directory $share"; exit 1; }
57+
chown "$USER:$group" "$share" || { echo "Failed to set ownership for directory $share"; exit 1; }
58+
fi
59+
60+
fi
5561

5662
# Start the Samba daemon with the following options:
5763
# --foreground: Run in the foreground instead of daemonizing.

0 commit comments

Comments
 (0)