From 02e82963f97a8414002425fcf14d21da0f844790 Mon Sep 17 00:00:00 2001 From: Nomsplease Date: Wed, 10 Apr 2024 11:13:43 -0400 Subject: [PATCH] Dont edit configs if requested --- Dockerfile | 2 ++ samba.sh | 38 +++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 92ef5fe..8f7a7e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,8 @@ ENV PASS "secret" ENV UID 1000 ENV GID 1000 ENV RW true +ENV CustomConfig false +ENV CONFIG "/etc/samba/smb.conf" HEALTHCHECK --interval=60s --timeout=15s CMD smbclient -L \\localhost -U % -m SMB3 diff --git a/samba.sh b/samba.sh index 756f5bc..871af2c 100644 --- a/samba.sh +++ b/samba.sh @@ -8,10 +8,6 @@ share="/storage" # Create shared directory mkdir -p "$share" || { echo "Failed to create directory $share"; exit 1; } -# Copy config file template -rm -f /etc/samba/smb.custom -cp /etc/samba/smb.conf /etc/samba/smb.custom - # Check if the smb group exists, if not, create it if ! getent group "$group" &>/dev/null; then groupadd "$group" || { echo "Failed to create group $group"; exit 1; } @@ -38,25 +34,29 @@ fi # Change Samba password echo -e "$PASS\n$PASS" | smbpasswd -a -s "$USER" || { echo "Failed to change Samba password for $USER"; exit 1; } -# Update force user and force group in smb.conf -sed -i "s/^\(\s*\)force user =.*/\1force user = $USER/" "/etc/samba/smb.custom" -sed -i "s/^\(\s*\)force group =.*/\1force group = $group/" "/etc/samba/smb.custom" +# Use custom config if not read-only system +if [[ "$CustomConfig" == [Ff0]* ]]; then -# Verify if the RW variable is equal to false (indicating read-only mode) -if [[ "$RW" == [Ff0]* ]]; then + # Update force user and force group in smb.conf + sed -i "s/^\(\s*\)force user =.*/\1force user = $USER/" "$CONFIG" + sed -i "s/^\(\s*\)force group =.*/\1force group = $group/" "$CONFIG" - # Adjust settings in smb.conf to set share to read-only - sed -i "s/^\(\s*\)writable =.*/\1writable = no/" "/etc/samba/smb.custom" - sed -i "s/^\(\s*\)read only =.*/\1read only = yes/" "/etc/samba/smb.custom" + # Verify if the RW variable is equal to false (indicating read-only mode) + if [[ "$RW" == [Ff0]* ]]; then -else + # Adjust settings in smb.conf to set share to read-only + sed -i "s/^\(\s*\)writable =.*/\1writable = no/" "$CONFIG" + sed -i "s/^\(\s*\)read only =.*/\1read only = yes/" "$CONFIG" - # Set permissions for share directory if new (empty), leave untouched if otherwise - if [ -z "$(ls -A "$share")" ]; then - chmod 0770 "$share" || { echo "Failed to set permissions for directory $share"; exit 1; } - chown "$USER:$group" "$share" || { echo "Failed to set ownership for directory $share"; exit 1; } - fi + else + # Set permissions for share directory if new (empty), leave untouched if otherwise + if [ -z "$(ls -A "$share")" ]; then + chmod 0770 "$share" || { echo "Failed to set permissions for directory $share"; exit 1; } + chown "$USER:$group" "$share" || { echo "Failed to set ownership for directory $share"; exit 1; } + fi + + fi fi # Start the Samba daemon with the following options: @@ -64,4 +64,4 @@ fi # --debug-stdout: Send debug output to stdout. # --debuglevel=1: Set debug verbosity level to 1. # --no-process-group: Don't create a new process group for the daemon. -exec smbd --configfile=/etc/samba/smb.custom --foreground --debug-stdout --debuglevel=1 --no-process-group +exec smbd --configfile=$CONFIG --foreground --debug-stdout --debuglevel=1 --no-process-group