_create_folder_fedora.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. #
  3. # _create_folder_fedora.sh — Create a new folder in /var/www/html with correct permissions (Fedora)
  4. # Invoked via create_folder.sh; not meant to be run directly.
  5. #
  6. # This script must be run as root (su).
  7. #
  8. set -euo pipefail
  9. # ── Colors for output ─────────────────────────────────────────────────────
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. NC='\033[0m' # No Color
  14. # ── Check for root privileges ─────────────────────────────────────────────
  15. if [[ $EUID -ne 0 ]]; then
  16. echo -e "${RED}ERROR: This script must be run as root (su / sudo).${NC}" >&2
  17. exit 1
  18. fi
  19. # ── Check for mandatory parameter ─────────────────────────────────────────
  20. if [[ $# -lt 1 || -z "$1" ]]; then
  21. echo -e "${RED}ERROR: Folder name is a mandatory parameter.${NC}" >&2
  22. echo -e "Usage: $0 <foldername>" >&2
  23. exit 1
  24. fi
  25. FOLDER_NAME="$1"
  26. BASE_DIR="/var/www/html"
  27. NEW_FOLDER="$BASE_DIR/$FOLDER_NAME"
  28. # Check if target already exists
  29. if [[ -e "$NEW_FOLDER" ]]; then
  30. echo -e "${RED}ERROR: Target path '$NEW_FOLDER' already exists.${NC}" >&2
  31. exit 1
  32. fi
  33. # ── Determine regular user ────────────────────────────────────────────────
  34. REGULAR_USER=""
  35. if [[ -n "${SUDO_USER:-}" ]]; then
  36. REGULAR_USER="$SUDO_USER"
  37. elif command -v logname &>/dev/null && logname &>/dev/null; then
  38. REGULAR_USER=$(logname)
  39. fi
  40. # Fallback to the owner of the script directory if needed
  41. if [[ -z "$REGULAR_USER" || "$REGULAR_USER" == "root" ]]; then
  42. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  43. REGULAR_USER=$(stat -c '%U' "$SCRIPT_DIR" 2>/dev/null || echo "")
  44. fi
  45. # Final fallback
  46. if [[ -z "$REGULAR_USER" || "$REGULAR_USER" == "root" ]]; then
  47. REGULAR_USER=$(awk -F: '$3 >= 1000 && $3 != 65534 {print $1; exit}' /etc/passwd)
  48. fi
  49. # Ensure we found a valid regular user
  50. if [[ -z "$REGULAR_USER" ]]; then
  51. echo -e "${RED}ERROR: Could not determine a regular user for ownership.${NC}" >&2
  52. exit 1
  53. fi
  54. # ── Configuration ─────────────────────────────────────────────────────────
  55. APACHE_USER="apache"
  56. APACHE_GROUP="apache"
  57. echo "→ Creating folder: $NEW_FOLDER"
  58. mkdir -p "$NEW_FOLDER"
  59. echo "→ Setting permissions and ownership..."
  60. # Set ownership to regular user (matching the existing script's behavior)
  61. chown -R "$REGULAR_USER:$REGULAR_USER" "$NEW_FOLDER"
  62. # Set base permissions
  63. chmod 0755 "$NEW_FOLDER"
  64. # Set ACLs so both the regular user and Apache can read/write
  65. setfacl -R -m u:"$REGULAR_USER":rwx,u:"$APACHE_USER":rwx "$NEW_FOLDER"
  66. setfacl -R -d -m u:"$REGULAR_USER":rwx,u:"$APACHE_USER":rwx "$NEW_FOLDER"
  67. # Also allow the apache group to access
  68. setfacl -R -m g:"$APACHE_GROUP":r-x "$NEW_FOLDER"
  69. setfacl -R -d -m g:"$APACHE_GROUP":r-x "$NEW_FOLDER"
  70. # ── Configure SELinux permissions ─────────────────────────────────────────
  71. if command -v getenforce &>/dev/null; then
  72. SELINUX_STATUS=$(getenforce 2>/dev/null || echo "Disabled")
  73. if [[ "$SELINUX_STATUS" != "Disabled" && "$SELINUX_STATUS" != "Permissive" ]]; then
  74. echo "→ Configuring SELinux permissions..."
  75. if command -v semanage &>/dev/null; then
  76. semanage fcontext -a -t httpd_sys_rw_content_t "$NEW_FOLDER(/.*)?" 2>/dev/null || \
  77. semanage fcontext -m -t httpd_sys_rw_content_t "$NEW_FOLDER(/.*)?" 2>/dev/null || true
  78. fi
  79. restorecon -Rv "$NEW_FOLDER" &>/dev/null || true
  80. fi
  81. fi
  82. echo -e "${GREEN}✓ Folder '$FOLDER_NAME' created successfully in $BASE_DIR!${NC}"