Improved file organization

This commit is contained in:
root 2025-05-25 09:49:43 +00:00
parent 4d692b51b5
commit 58f0fe6adc
16 changed files with 286 additions and 228 deletions

View File

@ -1,68 +0,0 @@
#!/bin/bash
# Usage check
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <folder_path> <username>"
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 <<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
View 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

65
functions/remove_share.sh Executable file
View 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
View 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
# ####################################################################

View File

@ -1,28 +1,40 @@
#!/bin/bash #!/bin/bash
TMP_FILE=$(mktemp) TMP_FILE=$(mktemp)
TMP_OUT=$(mktemp)
TMP_USERS=$(mktemp)
# Ask for username # 1) Ask for share name
dialog --backtitle "Create SMB share" --inputbox "Enter share name:" 10 40 2>"$TMP_FILE" dialog --backtitle "Create SMB share" --inputbox "Enter share name (folder path):" 10 50 2>"$TMP_FILE"
SHARE_NAME=$(<"$TMP_FILE") SHARE_NAME=$(<"$TMP_FILE")
# Ask for password (input will be hidden) # 2) Retrieve list of users from external script
dialog --backtitle "Create SMB share" --inputbox "Enter associated username:" 10 40 2>"$TMP_FILE" 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") 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
# 5) Display the result
# 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)
dialog --backtitle "Create SMB share" \ dialog --backtitle "Create SMB share" \
--title "Result" \ --title "Result" \
--textbox "$TMP_OUT" 20 60 --textbox "$TMP_OUT" 20 60
rm -f "$TMP_OUT" # 6) Cleanup
rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT"
rm -f "$TMP_FILE"

76
gui/dialog_share_remove.sh Executable file
View File

@ -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"

View File

@ -15,7 +15,7 @@ PASSWORD=$(<"$TMP_FILE")
# 3) Call the external script and capture output # 3) Call the external script and capture output
# adjust the path (“../add_smb_user.sh”) as needed # adjust the path (“../add_smb_user.sh”) as needed
TMP_OUT=$(mktemp) 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) # 4) Display the result (use --msgbox for short, --textbox for multiline)
dialog --backtitle "Add SMB User" \ dialog --backtitle "Add SMB User" \

View File

@ -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"

View File

@ -1,26 +1,36 @@
#!/bin/bash #!/bin/bash
TMP_FILE=$(mktemp) TMP_FILE=$(mktemp)
TMP_USERS=$(mktemp)
TMP_OUT=$(mktemp)
# Ask for username # 1) Get the list of users
dialog --backtitle "Remove SMB User" --inputbox "Enter username to be removed:" 10 40 2>"$TMP_FILE" 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") USERNAME=$(<"$TMP_FILE")
# 4) Run the removal script
bash $SMB_TOOLS_PATH/functions/remove_smb_user.sh "$USERNAME" >"$TMP_OUT" 2>&1
# 5) Show output
# 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)
dialog --backtitle "Remove SMB User" \ dialog --backtitle "Remove SMB User" \
--title "Result" \ --title "Result" \
--textbox "$TMP_OUT" 20 60 --textbox "$TMP_OUT" 20 60
rm -f "$TMP_OUT" # 6) Cleanup
rm -f "$TMP_FILE" "$TMP_USERS" "$TMP_OUT"
# dialog --msgbox "User '$USERNAME' with given password would be created." 8 50
rm -f "$TMP_FILE"

View File

@ -10,10 +10,9 @@ while true; do
--menu "Choose an action:" 15 50 6 \ --menu "Choose an action:" 15 50 6 \
1 "Add User" \ 1 "Add User" \
2 "Remove User" \ 2 "Remove User" \
3 "List Users" \ 3 "Create Share" \
4 "Create Share" \ 4 "Remove Share" \
5 "Remove Share" \ 5 "Exit" 2>"$TMP_FILE"
6 "Exit" 2>"$TMP_FILE"
CHOICE=$(<"$TMP_FILE") CHOICE=$(<"$TMP_FILE")
@ -25,17 +24,12 @@ while true; do
./dialog_user_remove.sh ./dialog_user_remove.sh
;; ;;
3) 3)
./dialog_user_list.sh
;;
4)
./dialog_share_create.sh ./dialog_share_create.sh
;; ;;
5) 4)
dialog --inputbox "Enter share name to remove:" 10 40 2>"$TMP_FILE" ./dialog_share_remove.sh
SHARE=$(<"$TMP_FILE")
dialog --msgbox "Share '$SHARE' would be removed." 8 40
;; ;;
6) 5)
break break
;; ;;
*) *)

View File

@ -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

View File

@ -1,37 +0,0 @@
#!/bin/bash
# Script to remove a Samba share
# Usage: ./remove_samba_share.sh <share_name>
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"
# 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."

View File

@ -1,31 +0,0 @@
#!/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
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