diff --git a/readme.md b/readme.md index 4f15bac..9bce7ae 100644 --- a/readme.md +++ b/readme.md @@ -107,6 +107,18 @@ volumes: - /example/users.conf:/etc/samba/users.conf ``` +Each line inside that file contains a `:` separated list of attributes describing the user to be created. + +`username:UID:groupname:GID:password:homedir` + +where: +- `username` The textual name of the user. +- `UID` The numerical id of the user. +- `groupname` The textual name of the primary user group. +- `GID` The numerical id of the primary user group. +- `password` The clear text password of the user. The password can not contain `:`,`\n` or `\r`. +- `homedir` Optional field for setting the home directory of the user. + ## Stars 🌟 [![Stars](https://starchart.cc/dockur/samba.svg?variant=adaptive)](https://starchart.cc/dockur/samba) diff --git a/samba.sh b/samba.sh index 81f8f02..49b219f 100644 --- a/samba.sh +++ b/samba.sh @@ -13,6 +13,7 @@ add_user() { local groupname="$4" local gid="$5" local password="$6" + local homedir="$7" # Check if the smb group exists, if not, create it if ! getent group "$groupname" &>/dev/null; then @@ -31,7 +32,12 @@ add_user() { # Check if the user already exists, if not, create it if ! id "$username" &>/dev/null; then [[ "$username" != "$USER" ]] && echo "User $username does not exist, creating user..." - 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; } + extra_args=() + # Check if home directory already exists, if so do not create home during user creation + if [ -d "$homedir" ]; then + extra_args=("${extra_args[@]}" -H) + fi + adduser "${extra_args[@]}" -S -D -h "$homedir" -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 @@ -116,7 +122,7 @@ if [ -f "$users" ] && [ -s "$users" ]; then [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue # Split each line by colon and assign to variables - IFS=':' read -r username uid groupname gid password <<< "$line" + IFS=':' read -r username uid groupname gid password homedir <<< "$line" # Check if all required fields are present if [[ -z "$username" || -z "$uid" || -z "$groupname" || -z "$gid" || -z "$password" ]]; then @@ -124,14 +130,17 @@ if [ -f "$users" ] && [ -s "$users" ]; then continue fi + # Default homedir if not explicitly set for user + [[ -z "$homedir" ]] && homedir="$share" + # Call the function with extracted values - add_user "$config" "$username" "$uid" "$groupname" "$gid" "$password" || { echo "Failed to add user $username"; exit 1; } + add_user "$config" "$username" "$uid" "$groupname" "$gid" "$password" "$homedir" || { echo "Failed to add user $username"; exit 1; } done < <(tr -d '\r' < "$users") else - add_user "$config" "$USER" "$UID" "$group" "$GID" "$PASS" || { echo "Failed to add user $USER"; exit 1; } + add_user "$config" "$USER" "$UID" "$group" "$GID" "$PASS" "$share" || { echo "Failed to add user $USER"; exit 1; } if [[ "$RW" != [Ff0]* ]]; then # Set permissions for share directory if new (empty), leave untouched if otherwise diff --git a/users.conf b/users.conf index 73d6574..ba8898b 100644 --- a/users.conf +++ b/users.conf @@ -1,2 +1,2 @@ -#username:UID:groupname:GID:password +#username:UID:groupname:GID:password:homedir samba:1000:smb:1000:secret