Improved file organization
This commit is contained in:
46
functions/add_smb_user.sh
Executable file
46
functions/add_smb_user.sh
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage: ./create_samba_user.sh <username> <password>
|
||||
|
||||
# Check for root
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "This script must be run as root."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Input arguments or prompt
|
||||
USERNAME="$1"
|
||||
PASSWORD="$2"
|
||||
|
||||
# If no username or password provided, exit with error
|
||||
if [[ -z "$USERNAME" || -z "$PASSWORD" ]]; then
|
||||
echo "Usage: $0 <username> <password>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create system user without extra fields
|
||||
if id "$USERNAME" &>/dev/null; then
|
||||
echo "User $USERNAME already exists."
|
||||
else
|
||||
useradd --comment "" --shell /bin/bash --create-home "$USERNAME"
|
||||
echo "$USERNAME:$PASSWORD" | chpasswd
|
||||
echo "System user $USERNAME created."
|
||||
fi
|
||||
|
||||
# Add to Samba
|
||||
(echo "$PASSWORD"; echo "$PASSWORD") | smbpasswd -s -a "$USERNAME"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo "Samba user $USERNAME added successfully."
|
||||
else
|
||||
echo "Failed to add Samba user."
|
||||
fi
|
||||
|
||||
log_file="$SMB_TOOLS_PATH/filebrowser/addusers.txt"
|
||||
|
||||
if [ -f "$log_file" ]; then
|
||||
echo "$USERNAME:$PASSWORD" >> "$log_file"
|
||||
fi
|
||||
|
||||
# Non-blocking docker restart
|
||||
docker restart filebrowser </dev/null &>/dev/null &
|
||||
68
functions/create_user_share.sh
Executable file
68
functions/create_user_share.sh
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage check
|
||||
if [ "$#" -ne 2 ]; then
|
||||
echo "Usage: $0 <folder_name> <username>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
FOLDER_PATH="/data/$2/$1"
|
||||
USERNAME="$2"
|
||||
SMBCONF="/etc/samba/smb.conf"
|
||||
SHARE_CONFIG_DIR="/etc/samba/shares.d"
|
||||
|
||||
# 1. Check if user exists
|
||||
if ! id "$USERNAME" &>/dev/null; then
|
||||
echo "Error: User '$USERNAME' does not exist."
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# 2. Create folder if it doesn't exist, set ownership and permissions
|
||||
if [ ! -d "$FOLDER_PATH" ]; then
|
||||
echo "Creating directory: $FOLDER_PATH"
|
||||
mkdir -p "$FOLDER_PATH"
|
||||
chown "$USERNAME":"$USERNAME" "$FOLDER_PATH"
|
||||
chmod 770 "$FOLDER_PATH"
|
||||
else
|
||||
echo "Directory already exists: $FOLDER_PATH"
|
||||
fi
|
||||
|
||||
# 3. Determine share name from folder name
|
||||
SHARE_NAME=$(basename "$FOLDER_PATH")
|
||||
|
||||
# 4. Ensure shares.d directory exists
|
||||
if [ ! -d "$SHARE_CONFIG_DIR" ]; then
|
||||
mkdir -p "$SHARE_CONFIG_DIR"
|
||||
fi
|
||||
|
||||
# 5. Create Samba share config file
|
||||
SHARE_CONFIG_FILE="$SHARE_CONFIG_DIR/$SHARE_NAME.conf"
|
||||
if [ -f "$SHARE_CONFIG_FILE" ]; then
|
||||
echo "Warning: Share config already exists: $SHARE_CONFIG_FILE"
|
||||
else
|
||||
cat <<EOF > "$SHARE_CONFIG_FILE"
|
||||
[$SHARE_NAME]
|
||||
path = $FOLDER_PATH
|
||||
valid users = $USERNAME
|
||||
read only = no
|
||||
browseable = yes
|
||||
guest ok = no
|
||||
create mask = 0660
|
||||
directory mask = 0770
|
||||
EOF
|
||||
echo "Created Samba share config: $SHARE_CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# 6. Append include directive to main smb.conf if not already present
|
||||
INCLUDE_LINE="include = $SHARE_CONFIG_FILE"
|
||||
if ! grep -Fxq "$INCLUDE_LINE" "$SMBCONF"; then
|
||||
echo "Appending include directive to $SMBCONF"
|
||||
echo "\n# Include share definition for $SHARE_NAME" >> "$SMBCONF"
|
||||
echo "$INCLUDE_LINE" >> "$SMBCONF"
|
||||
else
|
||||
echo "Include directive already present in $SMBCONF"
|
||||
fi
|
||||
|
||||
# 7. Restart Samba service to apply changes
|
||||
echo "Restarting Samba service..."
|
||||
systemctl restart smbd && echo "Samba restarted successfully." || echo "Failed to restart Samba. Check logs."
|
||||
28
functions/list_smb_shares.sh
Executable file
28
functions/list_smb_shares.sh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage check
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "Usage: $0 <username>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
USERNAME="$1"
|
||||
SHARE_DIR="/etc/samba/shares.d"
|
||||
|
||||
# Loop through share config files
|
||||
for file in "$SHARE_DIR"/*.conf; do
|
||||
[ -e "$file" ] || continue # Skip if no .conf files
|
||||
|
||||
SHARE_NAME=$(basename "$file" .conf)
|
||||
PATH_LINE=$(grep -i "^ *path *= *" "$file")
|
||||
USER_LINE=$(grep -i "^ *valid users *= *" "$file")
|
||||
|
||||
# Extract values
|
||||
SHARE_PATH=$(echo "$PATH_LINE" | cut -d'=' -f2- | xargs)
|
||||
VALID_USER=$(echo "$USER_LINE" | cut -d'=' -f2- | xargs)
|
||||
|
||||
# Check if this share belongs to the specified user
|
||||
if [[ "$VALID_USER" == *"$USERNAME"* ]]; then
|
||||
printf "%s\n" "$SHARE_NAME"
|
||||
fi
|
||||
done
|
||||
12
functions/list_smb_users.sh
Executable file
12
functions/list_smb_users.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if pdbedit is installed
|
||||
if ! command -v pdbedit &> /dev/null; then
|
||||
echo -e "${YELLOW}Error:${NC} pdbedit is not installed. Install Samba first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# List users with color
|
||||
pdbedit -L | cut -d: -f1 | while read -r user; do
|
||||
echo -e "${user}"
|
||||
done
|
||||
65
functions/remove_share.sh
Executable file
65
functions/remove_share.sh
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script to remove a Samba share, archive its data, and clean up configs
|
||||
# Usage: ./remove_samba_share.sh <share_name>
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "Usage: $0 <share_name>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SHARE_NAME="$1"
|
||||
SHARE_CONFIG_DIR="/etc/samba/shares.d"
|
||||
SMBCONF="/etc/samba/smb.conf"
|
||||
SHARE_CONFIG_FILE="$SHARE_CONFIG_DIR/$SHARE_NAME.conf"
|
||||
TRASH_BASE="/data/trash"
|
||||
|
||||
# 1. Read the .conf file if it exists
|
||||
if [ ! -f "$SHARE_CONFIG_FILE" ]; then
|
||||
echo "Error: Share config file does not exist: $SHARE_CONFIG_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Found share config: $SHARE_CONFIG_FILE"
|
||||
|
||||
# Extract path and username
|
||||
SHARE_PATH=$(grep -i '^[[:space:]]*path[[:space:]]*=' "$SHARE_CONFIG_FILE" | cut -d'=' -f2- | xargs)
|
||||
VALID_USER=$(grep -i '^[[:space:]]*valid users[[:space:]]*=' "$SHARE_CONFIG_FILE" | cut -d'=' -f2- | xargs)
|
||||
|
||||
# Validate extracted data
|
||||
if [ -z "$SHARE_PATH" ] || [ -z "$VALID_USER" ]; then
|
||||
echo "Error: Could not extract share path or user from config."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Move share folder if it exists
|
||||
if [ -d "$SHARE_PATH" ]; then
|
||||
USER_TRASH_DIR="$TRASH_BASE/$VALID_USER"
|
||||
DEST="$USER_TRASH_DIR/$(basename "$SHARE_PATH")"
|
||||
|
||||
echo "Moving share folder '$SHARE_PATH' to '$DEST'"
|
||||
mkdir -p "$USER_TRASH_DIR"
|
||||
mv "$SHARE_PATH" "$DEST"
|
||||
else
|
||||
echo "Note: Share path '$SHARE_PATH' does not exist or is not a directory — skipping move."
|
||||
fi
|
||||
|
||||
# Remove the config file
|
||||
echo "Removing share config file: $SHARE_CONFIG_FILE"
|
||||
rm -f "$SHARE_CONFIG_FILE"
|
||||
|
||||
# Remove include line and related comment from smb.conf
|
||||
INCLUDE_LINE="include = $SHARE_CONFIG_FILE"
|
||||
sed -i "/# Include share definition for $SHARE_NAME/d" "$SMBCONF"
|
||||
sed -i "\|$INCLUDE_LINE|d" "$SMBCONF"
|
||||
|
||||
# Restart Samba
|
||||
echo "Restarting Samba service..."
|
||||
if systemctl restart smbd; then
|
||||
echo "Samba restarted successfully."
|
||||
else
|
||||
echo "Error restarting Samba. Please check logs."
|
||||
exit 2
|
||||
fi
|
||||
58
functions/remove_smb_user.sh
Executable file
58
functions/remove_smb_user.sh
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage: ./remove_samba_user.sh <username>
|
||||
|
||||
# Check for root
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "This script must be run as root."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
####################################################################
|
||||
# Parse username
|
||||
####################################################################
|
||||
|
||||
USERNAME="$1"
|
||||
|
||||
if [[ -z "$USERNAME" ]]; then
|
||||
read -p "Enter username to remove: " USERNAME
|
||||
fi
|
||||
|
||||
####################################################################
|
||||
# Remove shares
|
||||
# All the shares associated to the user are removed
|
||||
####################################################################
|
||||
|
||||
bash $SMB_TOOLS_PATH/functions/list_smb_shares.sh $USERNAME | while IFS= read -r line; do
|
||||
# Skip empty lines
|
||||
[ -z "$line" ] && continue
|
||||
|
||||
bash $SMB_TOOLS_PATH/functions/remove_share.sh "$line"
|
||||
done
|
||||
|
||||
# ####################################################################
|
||||
# # Remove samba user
|
||||
# ####################################################################
|
||||
|
||||
if pdbedit -L | grep -q "^$USERNAME:"; then
|
||||
smbpasswd -x "$USERNAME"
|
||||
echo "Samba user $USERNAME removed."
|
||||
else
|
||||
echo "Samba user $USERNAME does not exist."
|
||||
fi
|
||||
|
||||
# ####################################################################
|
||||
# # Remove system user
|
||||
# ####################################################################
|
||||
|
||||
if id "$USERNAME" &>/dev/null; then
|
||||
userdel -r "$USERNAME"
|
||||
echo "System user $USERNAME removed (including home directory)."
|
||||
else
|
||||
echo "System user $USERNAME does not exist."
|
||||
fi
|
||||
|
||||
# ####################################################################
|
||||
# # Remove filebrowser
|
||||
# ####################################################################
|
||||
Reference in New Issue
Block a user