Skip to content

Commit

Permalink
fix: Pass config to pdbedit
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Oct 9, 2024
1 parent d1ef59b commit eadb15d
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions samba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ set -Eeuo pipefail
# and updates the password in the Samba database. The function ensures that the group also exists,
# creating it if necessary, and modifies the group ID if it differs from the provided value.
add_user() {
local username="$1"
local uid="$2"
local groupname="$3"
local gid="$4"
local password="$5"
local cfg="$1"
local username="$2"
local uid="$3"
local groupname="$4"
local gid="$5"
local password="$6"

# Check if the smb group exists, if not, create it
if ! getent group "$groupname" &>/dev/null; then
Expand Down Expand Up @@ -45,7 +46,7 @@ add_user() {
fi

# Check if the user is a samba user
if pdbedit -L | grep -q "^$username:"; then
if pdbedit -s "$cfg" -L | grep -q "^$username:"; then
# if the user is a samba user, change its password
echo -e "$password\n$password" | smbpasswd -s "$username" || { echo "Failed to update Samba password for $username"; return 1; }
[[ "$username" != "samba" ]] && echo "Password for existing Samba user $username has been updated."
Expand Down Expand Up @@ -87,7 +88,7 @@ if [[ -f "$user_config" ]] && [[ -f "$config" ]]; then
fi

# Call the function with extracted values
add_user "$username" "$uid" "$groupname" "$gid" "$password"
add_user "$config" "$username" "$uid" "$groupname" "$gid" "$password"

done < "$user_config"

Expand All @@ -106,12 +107,13 @@ else
PASS=$(cat "$secret")
fi

add_user "$USER" "$UID" "$group" "$GID" "$PASS"

if [ -f "$config" ]; then

# Inform the user we are using a custom configuration file.
echo "Using provided configuration file: $config."

else

config="/etc/samba/smb.tmp"
template="/etc/samba/smb.default"

Expand All @@ -128,12 +130,17 @@ 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"
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

add_user "$config" "$USER" "$UID" "$group" "$GID" "$PASS"

if [[ "$RW" != [Ff0]* ]]; then
# 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

Expand Down

0 comments on commit eadb15d

Please sign in to comment.