From 5c12b924151ceba022c67e7aeaff491926c41df6 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:07:14 +0800 Subject: [PATCH 01/12] Added multi-user mode to handle the creation of multiple users and add passwords to make them Samba users. --- Dockerfile | 2 + compose.yml | 1 + samba.sh | 184 +++++++++++++++++++++++++++++++----------------- samba_user.conf | 2 + 4 files changed, 126 insertions(+), 63 deletions(-) create mode 100644 samba_user.conf diff --git a/Dockerfile b/Dockerfile index fb8c78a..ce40c80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,8 @@ ENV UID=1000 ENV GID=1000 ENV RW=true +ENV MULTI_USER=false + HEALTHCHECK --interval=60s --timeout=15s CMD smbclient -L \\localhost -U % -m SMB3 ENTRYPOINT ["/sbin/tini", "--", "/usr/bin/samba.sh"] diff --git a/compose.yml b/compose.yml index f6dffd5..40eedc6 100644 --- a/compose.yml +++ b/compose.yml @@ -8,6 +8,7 @@ services: RW: true # Optional, default true UID: 1000 # Optional, default 1000 GID: 1000 # Optional, default 1000 + MULTI_USER: false ports: - 445:445 volumes: diff --git a/samba.sh b/samba.sh index 11bce56..32c7cdd 100644 --- a/samba.sh +++ b/samba.sh @@ -1,84 +1,142 @@ #!/usr/bin/env bash set -Eeuo pipefail -# Set variables for group and share directory -group="smb" -share="/storage" -secret="/run/secrets/pass" - -# Create shared directory -mkdir -p "$share" || { echo "Failed to create directory $share"; exit 1; } - -# 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; } -fi - -# Check if the user already exists, if not, create it -if ! id "$USER" &>/dev/null; then - adduser -S -D -H -h /tmp -s /sbin/nologin -G "$group" -g 'Samba User' "$USER" || { echo "Failed to create user $USER"; exit 1; } -fi +# This function checks for the existence of a specified Samba user and group. If the user does not exist, +# it creates a new user with the provided username, user ID (UID), group name, group ID (GID), and password. +# If the user already exists, it updates the user's UID and group association as necessary, +# 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_or_update_samba_user() { + local username="$1" + local uid="$2" + local groupname="$3" + local gid="$4" + local password="$5" + + # Check if the smb group exists, if not, create it + if ! getent group "$groupname" &>/dev/null; then + echo "Group $groupname does not exist, creating group..." + groupadd -o -g "$gid" "$groupname" || { echo "Failed to create group $groupname"; return 1; } + else + # Check if the gid right,if not, change it + local current_gid + current_gid=$(getent group "$groupname" | cut -d: -f3) + if [[ "$current_gid" != "$gid" ]]; then + echo "Group $groupname exists but GID differs, updating GID..." + groupmod -o -g "$gid" "$groupname" || { echo "Failed to update GID for group $groupname"; return 1; } + fi + fi -# Get the current user and group IDs -OldUID=$(id -u "$USER") -OldGID=$(getent group "$group" | cut -d: -f3) + # Check if the user already exists, if not, create it + if ! id "$username" &>/dev/null; then + echo "User $username does not exist, creating user..." + adduser -S -D -H -h /tmp -s /sbin/nologin -G "$groupname" -o -u "$uid" -g "Samba User" "$username" || { echo "Failed to create user $username"; return 1; } + else + # Check if the uid right,if not, change it + local current_uid + current_uid=$(id -u "$username") + if [[ "$current_uid" != "$uid" ]]; then + echo "User $username exists but UID differs, updating UID..." + usermod -o -u "$uid" "$username" || { echo "Failed to update UID for user $username"; return 1; } + fi -# Change the UID and GID of the user and group if necessary -if [[ "$OldUID" != "$UID" ]]; then - usermod -o -u "$UID" "$USER" || { echo "Failed to change UID for $USER"; exit 1; } -fi + # Update user's group + usermod -g "$groupname" "$username" || { echo "Failed to update group for user $username"; return 1; } + fi -if [[ "$OldGID" != "$GID" ]]; then - groupmod -o -g "$GID" "$group" || { echo "Failed to change GID for group $group"; exit 1; } -fi + # Check if the user is a samba user + if pdbedit -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; } + echo "Password for existing Samba user $username has been updated." + else + # if the user is not a samba user, create it and set a password + echo -e "$password\n$password" | smbpasswd -a -s "$username" || { echo "Failed to add Samba user $username"; return 1; } + echo "User $username has been added to Samba and password set." + fi +} -# Check if an external config file was supplied +# External config file config="/etc/samba/smb.conf" +user_config="/etc/samba/smb_user.conf" + +# Check if multi-user mode is enabled +if [[ "$MULTI_USER" == true ]]; then + # Check if the user configuration file exists, note: config and user_config must be exist!!! + if [[ -f "$user_config" && -f "$config" ]]; then + while read -r line; do + # Skip lines that are comments or empty + [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue + + # Split each line by colon and assign to variables + username=$(echo "$line" | cut -d: -f1) + uid=$(echo "$line" | cut -d: -f2) + groupname=$(echo "$line" | cut -d: -f3) + gid=$(echo "$line" | cut -d: -f4) + password=$(echo "$line" | cut -d: -f5) + + # Check if all required fields are present + if [[ -z "$username" || -z "$uid" || -z "$groupname" || -z "$gid" || -z "$password" ]]; then + echo "Skipping incomplete line: $line" + continue + fi + + # Call the function with extracted values + add_or_update_samba_user "$username" "$uid" "$groupname" "$gid" "$password" + done < "$user_config" + else + echo "/etc/samba/smb_user.conf not found, setting MULTI_USER to false." + MULTI_USER=false + fi +fi -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" - - # Generate a config file from template - rm -f "$config" - cp "$template" "$config" +# Handle single user case +if [[ "$MULTI_USER" == false ]]; then + # Set variables for group and share directory + group="smb" + share="/storage" + secret="/run/secrets/pass" - # 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" + # Create shared directory + mkdir -p "$share" || { echo "Failed to create directory $share"; exit 1; } - # Verify if the RW variable is equal to false (indicating read-only mode) - if [[ "$RW" == [Ff0]* ]]; then + # Check if the secret file exists and if its size is greater than zero + if [ -s "$secret" ]; then + PASS=$(cat "$secret") + fi - # 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" + add_or_update_samba_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 - - # 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; } + config="/etc/samba/smb.tmp" + template="/etc/samba/smb.default" + + # Generate a config file from template + rm -f "$config" + cp "$template" "$config" + + # 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" + + # Verify if the RW variable is equal to false (indicating read-only mode) + if [[ "$RW" == [Ff0]* ]]; then + # 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 fi -# Check if the secret file exists and if its size is greater than zero -if [ -s "$secret" ]; then - PASS=$(cat "$secret") -fi - -# Change Samba password -echo -e "$PASS\n$PASS" | smbpasswd -a -c "$config" -s "$USER" > /dev/null || { echo "Failed to change Samba password for $USER"; exit 1; } - # Start the Samba daemon with the following options: # --foreground: Run in the foreground instead of daemonizing. # --debug-stdout: Send debug output to stdout. diff --git a/samba_user.conf b/samba_user.conf new file mode 100644 index 0000000..d9560aa --- /dev/null +++ b/samba_user.conf @@ -0,0 +1,2 @@ +#username:UID:groupname:GID:password +samba:1000:smb:1000:secret \ No newline at end of file From 8558c2639e0ad9f43c90a2e61296fcf192a97886 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:37:27 +0800 Subject: [PATCH 02/12] Enhance the README content. --- readme.md | 14 ++++++++++++++ samba_user.conf => smb_user.conf | 0 2 files changed, 14 insertions(+) rename samba_user.conf => smb_user.conf (100%) diff --git a/readme.md b/readme.md index a995d62..a4fb1f0 100644 --- a/readme.md +++ b/readme.md @@ -58,6 +58,20 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl - /example/smb.conf:/etc/samba/smb.conf ``` + * ### How do I use multiple Samba users? + + If you want to use multiple Samba users, you can enable multi-user mode by setting MULTI_USER to true in the environment and modifying the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file in this repository. You can also modify the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) to implement different Samba policies for different users. To do this, completely override the default configuration and bind your custom config to the container as follows: + + ```yaml + environment: + MULTI_USER: true + volumes: + - /example/smb.conf:/etc/samba/smb.conf + - /example/smb_user.conf:/etc/samba/smb_user.conf + ``` + + Note: In the smb_user.conf file, user configurations must follow a specific format. Each line should contain the user information in the following order: username:uid:groupname:gid:password. Each line represents the configuration for a single user, and the parameters must be separated by colons":". + ## Stars 🌟 [![Stars](https://starchart.cc/dockur/samba.svg?variant=adaptive)](https://starchart.cc/dockur/samba) diff --git a/samba_user.conf b/smb_user.conf similarity index 100% rename from samba_user.conf rename to smb_user.conf From 9ab68716277a050311df4a18ba602b8f2de50c75 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:39:30 +0800 Subject: [PATCH 03/12] Enhance the README content. --- readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a4fb1f0..bcfab23 100644 --- a/readme.md +++ b/readme.md @@ -70,7 +70,11 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl - /example/smb_user.conf:/etc/samba/smb_user.conf ``` - Note: In the smb_user.conf file, user configurations must follow a specific format. Each line should contain the user information in the following order: username:uid:groupname:gid:password. Each line represents the configuration for a single user, and the parameters must be separated by colons":". + Note: In the smb_user.conf file, user configurations must follow a specific format. Each line should contain the user information in the following order: + ```yaml + username:uid:groupname:gid:password + ``` + Each line represents the configuration for a single user, and the parameters must be separated by colons":". ## Stars 🌟 [![Stars](https://starchart.cc/dockur/samba.svg?variant=adaptive)](https://starchart.cc/dockur/samba) From 821975d92ae8c2b1394d82a82730388c3961c3d3 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:41:16 +0800 Subject: [PATCH 04/12] Enhance the README content. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index bcfab23..c19b8b9 100644 --- a/readme.md +++ b/readme.md @@ -70,7 +70,7 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl - /example/smb_user.conf:/etc/samba/smb_user.conf ``` - Note: In the smb_user.conf file, user configurations must follow a specific format. Each line should contain the user information in the following order: + Note: In the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file, user configurations must follow a specific format. Each line should contain the user information in the following order: ```yaml username:uid:groupname:gid:password ``` From 447d48c38b1ffc839c9fc2807fd72980c948ca49 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:49:24 +0800 Subject: [PATCH 05/12] Fix some errors in the script. --- samba.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/samba.sh b/samba.sh index 32c7cdd..5720afd 100644 --- a/samba.sh +++ b/samba.sh @@ -69,11 +69,11 @@ if [[ "$MULTI_USER" == true ]]; then [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue # Split each line by colon and assign to variables - username=$(echo "$line" | cut -d: -f1) - uid=$(echo "$line" | cut -d: -f2) - groupname=$(echo "$line" | cut -d: -f3) - gid=$(echo "$line" | cut -d: -f4) - password=$(echo "$line" | cut -d: -f5) + username=$(echo "$line" | cut -d':' -f1) + uid=$(echo "$line" | cut -d':' -f2) + groupname=$(echo "$line" | cut -d':' -f3) + gid=$(echo "$line" | cut -d':' -f4) + password=$(echo "$line" | cut -d':' -f5) # Check if all required fields are present if [[ -z "$username" || -z "$uid" || -z "$groupname" || -z "$gid" || -z "$password" ]]; then From 05d1c4af941d122e3ec74d81ac755a91d03a82c1 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 16:52:27 +0800 Subject: [PATCH 06/12] Enhance the README content. --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index c19b8b9..71e0344 100644 --- a/readme.md +++ b/readme.md @@ -39,17 +39,17 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl ## Configuration ⚙️ - * ### How do I modify the credentials? + * ### How to modify the credentials? You can set the `USER` and `PASS` environment variables to modify the credentials from their default values: user `samba` with password `secret`. - * ### How do I modify the permissions? + * ### How to modify the permissions? You can set `UID` and `GID` environment variables to change the user and group ID. To mark the share as read-only, add the variable `RW: false`. - * ### How do I modify other settings? + * ### How to modify other settings? If you need more advanced features, you can completely override the default configuration by modifying the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) file in this repo, and binding your custom config to the container like this: @@ -58,7 +58,7 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl - /example/smb.conf:/etc/samba/smb.conf ``` - * ### How do I use multiple Samba users? + * ### How to use multiple Samba users? If you want to use multiple Samba users, you can enable multi-user mode by setting MULTI_USER to true in the environment and modifying the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file in this repository. You can also modify the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) to implement different Samba policies for different users. To do this, completely override the default configuration and bind your custom config to the container as follows: From 0480669bcd66e04e11c4739fa466f0b156168899 Mon Sep 17 00:00:00 2001 From: HanMengLin <1044138071@qq.com> Date: Wed, 9 Oct 2024 17:14:29 +0800 Subject: [PATCH 07/12] Enhance the README content. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 71e0344..5c49c62 100644 --- a/readme.md +++ b/readme.md @@ -70,7 +70,7 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl - /example/smb_user.conf:/etc/samba/smb_user.conf ``` - Note: In the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file, user configurations must follow a specific format. Each line should contain the user information in the following order: + Note: In this mode, you will need to manage the ownership and permissions of the shared folders yourself. In the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file, user configurations must follow a specific format. Each line should contain the user information in the following order: ```yaml username:uid:groupname:gid:password ``` From 1ae89604716ca7b295cd09815b0e9d542209f185 Mon Sep 17 00:00:00 2001 From: HanLiQL <1044138071@qq.com> Date: Thu, 10 Oct 2024 00:18:14 +0800 Subject: [PATCH 08/12] Fix some errors in the script. --- samba.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samba.sh b/samba.sh index 5720afd..9a91192 100644 --- a/samba.sh +++ b/samba.sh @@ -30,7 +30,7 @@ add_or_update_samba_user() { # Check if the user already exists, if not, create it if ! id "$username" &>/dev/null; then echo "User $username does not exist, creating user..." - adduser -S -D -H -h /tmp -s /sbin/nologin -G "$groupname" -o -u "$uid" -g "Samba User" "$username" || { echo "Failed to create user $username"; return 1; } + adduser -S -D -H -h /tmp -s /sbin/nologin -G "$groupname" -u "$uid" -g "Samba User" "$username" || { echo "Failed to create user $username"; return 1; } else # Check if the uid right,if not, change it local current_uid From 5bc8c516b5178adfb342b6382daf9968948349e1 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 9 Oct 2024 21:27:57 +0200 Subject: [PATCH 09/12] Update Dockerfile --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index ce40c80..fb8c78a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,8 +24,6 @@ ENV UID=1000 ENV GID=1000 ENV RW=true -ENV MULTI_USER=false - HEALTHCHECK --interval=60s --timeout=15s CMD smbclient -L \\localhost -U % -m SMB3 ENTRYPOINT ["/sbin/tini", "--", "/usr/bin/samba.sh"] From d6531753a59c16956d6ec4b37e7e123ab637b668 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 9 Oct 2024 21:28:11 +0200 Subject: [PATCH 10/12] Update compose.yml --- compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/compose.yml b/compose.yml index 40eedc6..f6dffd5 100644 --- a/compose.yml +++ b/compose.yml @@ -8,7 +8,6 @@ services: RW: true # Optional, default true UID: 1000 # Optional, default 1000 GID: 1000 # Optional, default 1000 - MULTI_USER: false ports: - 445:445 volumes: From f30fd3f5e9512ed8f982b2f61add36ae0b76ac94 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 9 Oct 2024 21:43:22 +0200 Subject: [PATCH 11/12] Update readme.md --- readme.md | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/readme.md b/readme.md index 5c49c62..3952fb6 100644 --- a/readme.md +++ b/readme.md @@ -39,42 +39,45 @@ docker run -it --rm -p 445:445 -e "USER=samba" -e "PASS=secret" -v "/home/exampl ## Configuration ⚙️ - * ### How to modify the credentials? +### How do I modify the credentials? - You can set the `USER` and `PASS` environment variables to modify the credentials from their default values: user `samba` with password `secret`. +You can set the `USER` and `PASS` environment variables to modify the credentials from their default values: user `samba` with password `secret`. - * ### How to modify the permissions? +### How do I modify the permissions? - You can set `UID` and `GID` environment variables to change the user and group ID. +You can set `UID` and `GID` environment variables to change the user and group ID. - To mark the share as read-only, add the variable `RW: false`. +To mark the share as read-only, add the variable `RW: false`. - * ### How to modify other settings? +### How do I modify other settings? - If you need more advanced features, you can completely override the default configuration by modifying the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) file in this repo, and binding your custom config to the container like this: +If you need more advanced features, you can completely override the default configuration by modifying the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) file in this repo, and binding your custom config to the container like this: - ```yaml - volumes: - - /example/smb.conf:/etc/samba/smb.conf - ``` +```yaml +volumes: + - /example/smb.conf:/etc/samba/smb.conf +``` - * ### How to use multiple Samba users? +### How do I use multiple users? - If you want to use multiple Samba users, you can enable multi-user mode by setting MULTI_USER to true in the environment and modifying the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file in this repository. You can also modify the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) to implement different Samba policies for different users. To do this, completely override the default configuration and bind your custom config to the container as follows: +If you want to use multiple Samba users, you can enable multi-user mode by binding the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file to the container as follows: - ```yaml - environment: - MULTI_USER: true - volumes: - - /example/smb.conf:/etc/samba/smb.conf - - /example/smb_user.conf:/etc/samba/smb_user.conf - ``` - - Note: In this mode, you will need to manage the ownership and permissions of the shared folders yourself. In the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file, user configurations must follow a specific format. Each line should contain the user information in the following order: - ```yaml - username:uid:groupname:gid:password - ``` - Each line represents the configuration for a single user, and the parameters must be separated by colons":". +```yaml +volumes: + - /example/smb.conf:/etc/samba/smb.conf + - /example/smb_user.conf:/etc/samba/smb_user.conf +``` + +You can also modify the [smb.conf](https://github.com/dockur/samba/blob/master/smb.conf) to implement different Samba policies for different users. + +> [!NOTE] +> In this mode, you will need to manage the ownership and permissions of the shared folders yourself. +> +> In the [smb_user.conf](https://github.com/dockur/samba/blob/master/smb_user.conf) file, user configurations must follow a specific format. Each line should contain the user information in the following order: +>```yaml +>username:uid:groupname:gid:password +>``` +>Each line represents the configuration for a single user, and the parameters must be separated by colons. ## Stars 🌟 [![Stars](https://starchart.cc/dockur/samba.svg?variant=adaptive)](https://starchart.cc/dockur/samba) From 6bbf5598c19aa93a866485190b92956f397e3370 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 9 Oct 2024 21:58:19 +0200 Subject: [PATCH 12/12] Update samba.sh --- samba.sh | 64 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/samba.sh b/samba.sh index 9a91192..6cf505d 100644 --- a/samba.sh +++ b/samba.sh @@ -6,7 +6,7 @@ set -Eeuo pipefail # If the user already exists, it updates the user's UID and group association as necessary, # 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_or_update_samba_user() { +add_user() { local username="$1" local uid="$2" local groupname="$3" @@ -60,38 +60,39 @@ add_or_update_samba_user() { config="/etc/samba/smb.conf" user_config="/etc/samba/smb_user.conf" +# Check if the user configuration file exists +if [[ -f "$user_config" ]] && [[ ! -f "$config" ]]; then + echo "File $config not found, disabling multi-user mode." +fi + # Check if multi-user mode is enabled -if [[ "$MULTI_USER" == true ]]; then - # Check if the user configuration file exists, note: config and user_config must be exist!!! - if [[ -f "$user_config" && -f "$config" ]]; then - while read -r line; do - # Skip lines that are comments or empty - [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue - - # Split each line by colon and assign to variables - username=$(echo "$line" | cut -d':' -f1) - uid=$(echo "$line" | cut -d':' -f2) - groupname=$(echo "$line" | cut -d':' -f3) - gid=$(echo "$line" | cut -d':' -f4) - password=$(echo "$line" | cut -d':' -f5) - - # Check if all required fields are present - if [[ -z "$username" || -z "$uid" || -z "$groupname" || -z "$gid" || -z "$password" ]]; then - echo "Skipping incomplete line: $line" - continue - fi +if [[ -f "$user_config" ]] && [[ -f "$config" ]]; then - # Call the function with extracted values - add_or_update_samba_user "$username" "$uid" "$groupname" "$gid" "$password" - done < "$user_config" - else - echo "/etc/samba/smb_user.conf not found, setting MULTI_USER to false." - MULTI_USER=false - fi -fi + while read -r line; do + + # Skip lines that are comments or empty + [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue + + # Split each line by colon and assign to variables + username=$(echo "$line" | cut -d':' -f1) + uid=$(echo "$line" | cut -d':' -f2) + groupname=$(echo "$line" | cut -d':' -f3) + gid=$(echo "$line" | cut -d':' -f4) + password=$(echo "$line" | cut -d':' -f5) + + # Check if all required fields are present + if [[ -z "$username" || -z "$uid" || -z "$groupname" || -z "$gid" || -z "$password" ]]; then + echo "Skipping incomplete line: $line" + continue + fi + + # Call the function with extracted values + add_user "$username" "$uid" "$groupname" "$gid" "$password" + + done < "$user_config" + +else -# Handle single user case -if [[ "$MULTI_USER" == false ]]; then # Set variables for group and share directory group="smb" share="/storage" @@ -105,7 +106,7 @@ if [[ "$MULTI_USER" == false ]]; then PASS=$(cat "$secret") fi - add_or_update_samba_user "$USER" "$UID" "$group" "$GID" "$PASS" + add_user "$USER" "$UID" "$group" "$GID" "$PASS" if [ -f "$config" ]; then # Inform the user we are using a custom configuration file. @@ -135,6 +136,7 @@ if [[ "$MULTI_USER" == false ]]; then fi fi fi + fi # Start the Samba daemon with the following options: