Skip to content

Commit

Permalink
feat: Add optional option to define a home directory for users create…
Browse files Browse the repository at this point in the history
…d with users.conf (#36)

* feat: Add optional option to set custom home directory in multi-user mode
BREAKING: Passwords can no longer contain ':'
  • Loading branch information
Nunction authored Jan 30, 2025
1 parent 3661aef commit da35545
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 13 additions & 4 deletions samba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -116,22 +122,25 @@ 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
echo "Skipping incomplete line: $line"
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
Expand Down
2 changes: 1 addition & 1 deletion users.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#username:UID:groupname:GID:password
#username:UID:groupname:GID:password:homedir
samba:1000:smb:1000:secret

0 comments on commit da35545

Please sign in to comment.