Skip to content

Commit

Permalink
Dont edit configs if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomsplease committed Apr 10, 2024
1 parent fc0ddcd commit 02e8296
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 19 additions & 19 deletions samba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -38,30 +34,34 @@ 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:
# --foreground: Run in the foreground instead of daemonizing.
# --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

0 comments on commit 02e8296

Please sign in to comment.