-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit_users.sh
More file actions
executable file
·73 lines (61 loc) · 2.42 KB
/
Copy pathinit_users.sh
File metadata and controls
executable file
·73 lines (61 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# Initialize Samba users from configuration
echo "Initializing Samba users and directories..."
# Create mount directories based on share configurations under /mount/storage
echo "Creating user directories under /mount/storage..."
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" ]; then
echo "Directory already exists: $share_dir"
else
echo "Creating directory: $share_dir"
mkdir -p "$share_dir"
fi
fi
done < /etc/samba/users.conf
# Read user credentials from users.conf and create system users
echo "Creating Samba users..."
while IFS=':' read -r username password; do
# Skip comments and empty lines
if [[ $username =~ ^#.*$ ]] || [[ -z "$username" ]] || [[ $username == "["* ]]; then
continue
fi
echo "Processing user: $username"
# Create system user if it doesn't exist
if ! id "$username" &>/dev/null; then
echo "Creating system user: $username"
useradd -M -s /bin/false "$username"
else
echo "System user already exists: $username"
fi
# Check if Samba user already exists
if pdbedit -L | grep -q "^$username:"; then
echo "Samba user already exists: $username"
# Update password in case it changed
echo -e "$password\n$password" | smbpasswd -s "$username"
else
echo "Creating Samba user: $username"
# Set Samba password
echo -e "$password\n$password" | smbpasswd -a -s "$username"
fi
# Ensure the user is enabled
smbpasswd -e "$username"
done < <(grep -E '^[^#\[].*:.*' /etc/samba/users.conf)
# Set proper ownership for each user's directory
echo "Setting proper ownership for user directories..."
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
echo "User and directory initialization complete."