From 6696e5757b870338905ea5ef2a16f275ecfef1f9 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Mon, 20 Jan 2025 15:01:30 +0800 Subject: [PATCH 1/2] fix: Add compatibility for CRLF-formatted users.conf files 1.Previously, when processing users.conf files in CRLF format, the Bash script treated the '\r' at the end of lines as part of the password. This caused incorrect passwords and login failures. 2.This issue has now been fixed by removing all '\r' characters, enhancing compatibility. 3.It is still recommended to use LF format for both smb.conf and users.conf files. --- samba.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samba.sh b/samba.sh index 885dc8e..caebe14 100644 --- a/samba.sh +++ b/samba.sh @@ -126,7 +126,7 @@ if [ -f "$users" ] && [ -s "$users" ]; then # Call the function with extracted values add_user "$config" "$username" "$uid" "$groupname" "$gid" "$password" || { echo "Failed to add user $username"; exit 1; } - done < "$users" + done < <(tr -d '\r' < "$users") else From 0f7392d5a800a68a75d4568f8d25476418311906 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Mon, 20 Jan 2025 16:05:31 +0800 Subject: [PATCH 2/2] fix: Fixed the bug where the user would be re-added after a restart. 1.Use pdb_output to first capture the result of pdbedit -s "$cfg" -L, and then check if the user exists in the output. Without this step, the first user loaded may be incorrectly identified as not existing, causing it to be re-added and triggering the log output: "User $username has been added to Samba and password set." --- samba.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samba.sh b/samba.sh index caebe14..81f8f02 100644 --- a/samba.sh +++ b/samba.sh @@ -46,7 +46,8 @@ add_user() { fi # Check if the user is a samba user - if pdbedit -s "$cfg" -L | grep -q "^$username:"; then + pdb_output=$(pdbedit -s "$cfg" -L) #Do not combine the two commands into one, as this could lead to issues with the execution order and proper passing of variables. + if echo "$pdb_output" | grep -q "^$username:"; then # If the user is a samba user, update its password in case it changed echo -e "$password\n$password" | smbpasswd -c "$cfg" -s "$username" > /dev/null || { echo "Failed to update Samba password for $username"; return 1; } else