From 58f0fe6adc4aaa813dce52e6f95eb8c2c870e8a1 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 25 May 2025 09:49:43 +0000 Subject: [PATCH] Improved file organization --- create_share.sh | 68 ----------------- add_smb_user.sh => functions/add_smb_user.sh | 0 .../create_user_share.sh | 0 functions/list_smb_shares.sh | 28 +++++++ .../list_smb_users.sh | 0 functions/remove_share.sh | 65 ++++++++++++++++ functions/remove_smb_user.sh | 58 ++++++++++++++ gui/dialog_share_create.sh | 42 ++++++---- gui/dialog_share_remove.sh | 76 +++++++++++++++++++ gui/dialog_user_add.sh | 2 +- gui/dialog_user_list.sh | 20 ----- gui/dialog_user_remove.sh | 40 ++++++---- gui/main.sh | 18 ++--- list_smb_shares.sh | 29 ------- remove_share.sh | 37 --------- remove_smb_user.sh | 31 -------- 16 files changed, 286 insertions(+), 228 deletions(-) delete mode 100755 create_share.sh rename add_smb_user.sh => functions/add_smb_user.sh (100%) rename create_user_share.sh => functions/create_user_share.sh (100%) create mode 100755 functions/list_smb_shares.sh rename list_smb_users.sh => functions/list_smb_users.sh (100%) create mode 100755 functions/remove_share.sh create mode 100755 functions/remove_smb_user.sh create mode 100755 gui/dialog_share_remove.sh delete mode 100755 gui/dialog_user_list.sh delete mode 100755 list_smb_shares.sh delete mode 100755 remove_share.sh delete mode 100755 remove_smb_user.sh diff --git a/create_share.sh b/create_share.sh deleted file mode 100755 index 4e8948a..0000000 --- a/create_share.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash - -# Usage check -if [ "$#" -ne 2 ]; then - echo "Usage: $0 " - exit 1 -fi - -FOLDER_PATH="$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 < "$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." diff --git a/add_smb_user.sh b/functions/add_smb_user.sh similarity index 100% rename from add_smb_user.sh rename to functions/add_smb_user.sh diff --git a/create_user_share.sh b/functions/create_user_share.sh similarity index 100% rename from create_user_share.sh rename to functions/create_user_share.sh diff --git a/functions/list_smb_shares.sh b/functions/list_smb_shares.sh new file mode 100755 index 0000000..1e03371 --- /dev/null +++ b/functions/list_smb_shares.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Usage check +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + 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 \ No newline at end of file diff --git a/list_smb_users.sh b/functions/list_smb_users.sh similarity index 100% rename from list_smb_users.sh rename to functions/list_smb_users.sh diff --git a/functions/remove_share.sh b/functions/remove_share.sh new file mode 100755 index 0000000..8ead13d --- /dev/null +++ b/functions/remove_share.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# Script to remove a Samba share, archive its data, and clean up configs +# Usage: ./remove_samba_share.sh + +set -euo pipefail + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + 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 diff --git a/functions/remove_smb_user.sh b/functions/remove_smb_user.sh new file mode 100755 index 0000000..7cb951e --- /dev/null +++ b/functions/remove_smb_user.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Usage: ./remove_samba_user.sh + +# 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 +# #################################################################### \ No newline at end of file diff --git a/gui/dialog_share_create.sh b/gui/dialog_share_create.sh index b97f404..c72e851 100755 --- a/gui/dialog_share_create.sh +++ b/gui/dialog_share_create.sh @@ -1,28 +1,40 @@ #!/bin/bash TMP_FILE=$(mktemp) +TMP_OUT=$(mktemp) +TMP_USERS=$(mktemp) -# Ask for username -dialog --backtitle "Create SMB share" --inputbox "Enter share name:" 10 40 2>"$TMP_FILE" +# 1) Ask for share name +dialog --backtitle "Create SMB share" --inputbox "Enter share name (folder path):" 10 50 2>"$TMP_FILE" SHARE_NAME=$(<"$TMP_FILE") -# Ask for password (input will be hidden) -dialog --backtitle "Create SMB share" --inputbox "Enter associated username:" 10 40 2>"$TMP_FILE" +# 2) Retrieve list of users from external script +bash $SMB_TOOLS_PATH/functions/list_smb_users.sh > "$TMP_USERS" +if [ ! -s "$TMP_USERS" ]; then + dialog --msgbox "No users found by get_users_list.sh" 10 40 + rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" + exit 1 +fi + +# Format list for dialog menu: convert each username into a pair (tag + description) +MENU_ITEMS=() +while read -r user; do + MENU_ITEMS+=("$user" "") # empty description +done < "$TMP_USERS" + +# 3) Ask for username via a menu +dialog --backtitle "Create SMB share" \ + --menu "Select associated username:" 15 50 8 \ + "${MENU_ITEMS[@]}" 2>"$TMP_FILE" USERNAME=$(<"$TMP_FILE") +# 4) Call the external script to create the share +bash $SMB_TOOLS_PATH/functions/create_user_share.sh "$SHARE_NAME" "$USERNAME" >"$TMP_OUT" 2>&1 - -# 3) Call the external script and capture output -# adjust the path (“../add_smb_user.sh”) as needed -TMP_OUT=$(mktemp) -bash ./../create_user_share.sh "$SHARE_NAME" "$USERNAME" >"$TMP_OUT" 2>&1 - -# 4) Display the result (use --msgbox for short, --textbox for multiline) +# 5) Display the result dialog --backtitle "Create SMB share" \ --title "Result" \ --textbox "$TMP_OUT" 20 60 -rm -f "$TMP_OUT" - - -rm -f "$TMP_FILE" +# 6) Cleanup +rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" \ No newline at end of file diff --git a/gui/dialog_share_remove.sh b/gui/dialog_share_remove.sh new file mode 100755 index 0000000..ba2a8e7 --- /dev/null +++ b/gui/dialog_share_remove.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +TMP_FILE=$(mktemp) +TMP_OUT=$(mktemp) + +TMP_USERS=$(mktemp) +TMP_SHARES=$(mktemp) + + +######################################################################################### +# SHOW USERS LIST +# Ask to select a user +######################################################################################### + +# Retrieve list of users from external script +bash $SMB_TOOLS_PATH/functions/list_smb_users.sh > "$TMP_USERS" +if [ ! -s "$TMP_USERS" ]; then + dialog --msgbox "No users" 10 40 + rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" "$TMP_SHARES" + exit 1 +fi + +# Format list for dialog menu: convert each username into a pair (tag + description) +MENU_ITEMS=() +while read -r user; do + MENU_ITEMS+=("$user" "") # empty description +done < "$TMP_USERS" + +# Ask for username via a menu +dialog --backtitle "Create SMB share" \ + --menu "Select associated username:" 15 50 8 \ + "${MENU_ITEMS[@]}" 2>"$TMP_FILE" +USERNAME=$(<"$TMP_FILE") + + +######################################################################################### +# SHOW SHARES LIST +# Ask to select a share assigned to the selected user +######################################################################################### + +# Retrieve list of users from external script +bash $SMB_TOOLS_PATH/functions/list_smb_shares.sh $USERNAME> "$TMP_SHARES" +if [ ! -s "$TMP_SHARES" ]; then + dialog --msgbox "No shares associated with the user" 10 40 + rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" "$TMP_SHARES" + exit 1 +fi + +# Format list for dialog menu: convert each username into a pair (tag + description) +MENU_ITEMS=() +while read -r share; do + MENU_ITEMS+=("$share" "") # empty description +done < "$TMP_SHARES" + +# Ask for username via a menu +dialog --backtitle "Create SMB share" \ + --menu "Select associated username:" 15 50 8 \ + "${MENU_ITEMS[@]}" 2>"$TMP_FILE" +SHARE=$(<"$TMP_FILE") + + +######################################################################################### +# REMOVE SELECTED SHARE +######################################################################################### + + +# Call the external script to create the share +bash $SMB_TOOLS_PATH/functions/remove_share.sh "$SHARE" >"$TMP_OUT" 2>&1 + +# Display the result +dialog --backtitle "Remove SMB share" \ + --title "Result" \ + --textbox "$TMP_OUT" 20 60 + +# Cleanup +rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" "$TMP_SHARES" \ No newline at end of file diff --git a/gui/dialog_user_add.sh b/gui/dialog_user_add.sh index 7f902b9..7bd5a6d 100755 --- a/gui/dialog_user_add.sh +++ b/gui/dialog_user_add.sh @@ -15,7 +15,7 @@ PASSWORD=$(<"$TMP_FILE") # 3) Call the external script and capture output # adjust the path (“../add_smb_user.sh”) as needed TMP_OUT=$(mktemp) -bash ./../add_smb_user.sh "$USERNAME" "$PASSWORD" >"$TMP_OUT" 2>&1 +bash $SMB_TOOLS_PATH/functions/add_smb_user.sh "$USERNAME" "$PASSWORD" >"$TMP_OUT" 2>&1 # 4) Display the result (use --msgbox for short, --textbox for multiline) dialog --backtitle "Add SMB User" \ diff --git a/gui/dialog_user_list.sh b/gui/dialog_user_list.sh deleted file mode 100755 index d0067f6..0000000 --- a/gui/dialog_user_list.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -TMP_FILE=$(mktemp) - -# 3) Call the external script and capture output -# adjust the path (“../add_smb_user.sh”) as needed -TMP_OUT=$(mktemp) -bash ./../list_smb_users.sh >"$TMP_OUT" 2>&1 - -# 4) Display the result (use --msgbox for short, --textbox for multiline) -dialog --backtitle "SMB Users" \ - --title "SMB Users" \ - --textbox "$TMP_OUT" 20 60 - -rm -f "$TMP_OUT" - - -# dialog --msgbox "User '$USERNAME' with given password would be created." 8 50 - -rm -f "$TMP_FILE" diff --git a/gui/dialog_user_remove.sh b/gui/dialog_user_remove.sh index 4818435..39b2549 100755 --- a/gui/dialog_user_remove.sh +++ b/gui/dialog_user_remove.sh @@ -1,26 +1,36 @@ #!/bin/bash TMP_FILE=$(mktemp) +TMP_USERS=$(mktemp) +TMP_OUT=$(mktemp) -# Ask for username -dialog --backtitle "Remove SMB User" --inputbox "Enter username to be removed:" 10 40 2>"$TMP_FILE" +# 1) Get the list of users +bash $SMB_TOOLS_PATH/functions/list_smb_users.sh > "$TMP_USERS" +if [ ! -s "$TMP_USERS" ]; then + dialog --msgbox "No users" 10 40 + rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" + exit 1 +fi + +# 2) Prepare user list for dialog --menu (username + empty description) +MENU_ITEMS=() +while read -r user; do + MENU_ITEMS+=("$user" "") +done < "$TMP_USERS" + +# 3) Ask the user to select one from the list +dialog --backtitle "Remove SMB User" \ + --menu "Select a user to remove:" 15 50 8 \ + "${MENU_ITEMS[@]}" 2>"$TMP_FILE" USERNAME=$(<"$TMP_FILE") +# 4) Run the removal script +bash $SMB_TOOLS_PATH/functions/remove_smb_user.sh "$USERNAME" >"$TMP_OUT" 2>&1 - -# 3) Call the external script and capture output -# adjust the path (“../add_smb_user.sh”) as needed -TMP_OUT=$(mktemp) -bash ./../remove_smb_user.sh "$USERNAME" >"$TMP_OUT" 2>&1 - -# 4) Display the result (use --msgbox for short, --textbox for multiline) +# 5) Show output dialog --backtitle "Remove SMB User" \ --title "Result" \ --textbox "$TMP_OUT" 20 60 -rm -f "$TMP_OUT" - - -# dialog --msgbox "User '$USERNAME' with given password would be created." 8 50 - -rm -f "$TMP_FILE" +# 6) Cleanup +rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT" diff --git a/gui/main.sh b/gui/main.sh index 67145d0..055a4f0 100755 --- a/gui/main.sh +++ b/gui/main.sh @@ -10,10 +10,9 @@ while true; do --menu "Choose an action:" 15 50 6 \ 1 "Add User" \ 2 "Remove User" \ - 3 "List Users" \ - 4 "Create Share" \ - 5 "Remove Share" \ - 6 "Exit" 2>"$TMP_FILE" + 3 "Create Share" \ + 4 "Remove Share" \ + 5 "Exit" 2>"$TMP_FILE" CHOICE=$(<"$TMP_FILE") @@ -25,17 +24,12 @@ while true; do ./dialog_user_remove.sh ;; 3) - ./dialog_user_list.sh - ;; - 4) ./dialog_share_create.sh ;; - 5) - dialog --inputbox "Enter share name to remove:" 10 40 2>"$TMP_FILE" - SHARE=$(<"$TMP_FILE") - dialog --msgbox "Share '$SHARE' would be removed." 8 40 + 4) + ./dialog_share_remove.sh ;; - 6) + 5) break ;; *) diff --git a/list_smb_shares.sh b/list_smb_shares.sh deleted file mode 100755 index 7b16d2a..0000000 --- a/list_smb_shares.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# Define colors -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -SHARE_DIR="/etc/samba/shares.d" - -# Header -printf "${GREEN}%s${NC}\n" "--------------------------------------------------------------------------------------------" -printf "${GREEN}%-20s %-40s %-20s${NC}\n" "SHARE NAME" "PATH" "USER" -printf "${GREEN}%s${NC}\n" "--------------------------------------------------------------------------------------------" - -# 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) - - # Print each row with fixed width columns - printf "%-20s %-40s %-20s\n" "$SHARE_NAME" "$SHARE_PATH" "$VALID_USER" -done \ No newline at end of file diff --git a/remove_share.sh b/remove_share.sh deleted file mode 100755 index bf511f5..0000000 --- a/remove_share.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -# Script to remove a Samba share -# Usage: ./remove_samba_share.sh - -if [ "$#" -ne 1 ]; then - echo "Usage: $0 " - 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" - -# 1. Remove share config file -if [ -f "$SHARE_CONFIG_FILE" ]; then - echo "Removing share config: $SHARE_CONFIG_FILE" - rm -f "$SHARE_CONFIG_FILE" -else - echo "Warning: Share config file does not exist: $SHARE_CONFIG_FILE" -fi - -# 2. Remove include line from smb.conf -INCLUDE_LINE="include = $SHARE_CONFIG_FILE" -if grep -Fxq "$INCLUDE_LINE" "$SMBCONF"; then - echo "Removing include line from $SMBCONF" - # Remove both the comment and the include line if present - sed -i "/# Include share definition for $SHARE_NAME/d" "$SMBCONF" - sed -i "\|$INCLUDE_LINE|d" "$SMBCONF" -else - echo "Include line not found in $SMBCONF" -fi - -# 3. Restart Samba to apply changes -echo "Restarting Samba service..." -systemctl restart smbd && echo "Samba restarted successfully." || echo "Failed to restart Samba. Check logs." diff --git a/remove_smb_user.sh b/remove_smb_user.sh deleted file mode 100755 index 3031998..0000000 --- a/remove_smb_user.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -# Usage: ./remove_samba_user.sh - -# Check for root -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root." - exit 1 -fi - -USERNAME="$1" - -if [[ -z "$USERNAME" ]]; then - read -p "Enter username to remove: " USERNAME -fi - -# 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