Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions init_users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ while IFS=':' read -r username password; do

done < <(grep -E '^[^#\[].*:.*' /etc/samba/users.conf)

# Set proper ownership for each user's directory
echo "Setting proper ownership for user directories..."
Copy link

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The while loop reads from /etc/samba/users.conf without proper error handling. If the file doesn't exist or is empty, the loop will fail silently. Consider adding a check to ensure the file exists before processing.

Suggested change
echo "Setting proper ownership for user directories..."
echo "Setting proper ownership for user directories..."
if [ ! -s /etc/samba/users.conf ]; then
echo "Error: /etc/samba/users.conf does not exist or is empty."
exit 1
fi

Copilot uses AI. Check for mistakes.
while read -r line; do
# Look for share section headers [sharename]
if [[ $line =~ ^\[([^]]+)\]$ ]]; then
share_name="${BASH_REMATCH[1]}"
share_dir="/mount/storage/$share_name"

if [ -d "$share_dir" ] && id "$share_name" &>/dev/null; then
Copy link

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks if the user exists but doesn't validate if the user was actually created in the previous loop. This could lead to inconsistent behavior if users.conf contains share sections that don't correspond to users defined in the same file.

Suggested change
if [ -d "$share_dir" ] && id "$share_name" &>/dev/null; then
# Only set ownership if share_name is in the created users list
if [ -d "$share_dir" ] && grep -qx "$share_name" "$created_users_tmp"; then

Copilot uses AI. Check for mistakes.
echo "Setting ownership for $share_dir to $share_name:$share_name"
chown -R "$share_name:$share_name" "$share_dir"
chmod -R 775 "$share_dir"
fi
fi
done < /etc/samba/users.conf
Comment on lines +59 to +71
Copy link

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern assumes share names in brackets correspond to usernames, but this may not always be true in Samba configurations. Share names and usernames can be different. Consider verifying that the share_name is actually a valid user before proceeding.

Suggested change
while read -r line; do
# Look for share section headers [sharename]
if [[ $line =~ ^\[([^]]+)\]$ ]]; then
share_name="${BASH_REMATCH[1]}"
share_dir="/mount/storage/$share_name"
if [ -d "$share_dir" ] && id "$share_name" &>/dev/null; then
echo "Setting ownership for $share_dir to $share_name:$share_name"
chown -R "$share_name:$share_name" "$share_dir"
chmod -R 775 "$share_dir"
fi
fi
done < /etc/samba/users.conf
# Parse users.conf to map shares to valid users and set ownership accordingly
current_share=""
valid_user=""
while IFS= read -r line; do
# Look for share section headers [sharename]
if [[ $line =~ ^\[([^]]+)\]$ ]]; then
# If we have a previous share and valid user, set ownership
if [[ -n "$current_share" && -n "$valid_user" ]]; then
share_dir="/mount/storage/$current_share"
if [ -d "$share_dir" ] && id "$valid_user" &>/dev/null; then
echo "Setting ownership for $share_dir to $valid_user:$valid_user"
chown -R "$valid_user:$valid_user" "$share_dir"
chmod -R 775 "$share_dir"
fi
fi
current_share="${BASH_REMATCH[1]}"
valid_user=""
elif [[ $line =~ ^[[:space:]]*valid[[:space:]]+users[[:space:]]*=[[:space:]]*([^[:space:]]+) ]]; then
# Extract the first username from the valid users line
valid_user="${BASH_REMATCH[1]}"
fi
done < /etc/samba/users.conf
# After the loop, handle the last share section
if [[ -n "$current_share" && -n "$valid_user" ]]; then
share_dir="/mount/storage/$current_share"
if [ -d "$share_dir" ] && id "$valid_user" &>/dev/null; then
echo "Setting ownership for $share_dir to $valid_user:$valid_user"
chown -R "$valid_user:$valid_user" "$share_dir"
chmod -R 775 "$share_dir"
fi
fi

Copilot uses AI. Check for mistakes.

echo "User and directory initialization complete."
5 changes: 0 additions & 5 deletions start_samba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ echo "Using HOST_UID=$HOST_UID and HOST_GID=$HOST_GID"
# Initialize users
/init_users.sh

# Set proper permissions for storage directory
# Use host user/group IDs for better compatibility
chown -R $HOST_UID:$HOST_GID /mount/storage
chmod -R 775 /mount/storage

# Create log directory
mkdir -p /var/log/samba

Expand Down