#!/bin/bash # ============================================================ # PRACTICE EXAM - BREAK SERVER SCRIPT # Betriebssysteme und Netzwerke - 45 Minute Practice Test # ============================================================ # This script intentionally introduces problems for students # to diagnose and fix during the practice exam # ============================================================ # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' MAGENTA='\033[0;35m' NC='\033[0m' # No Color print_status() { echo -e "${BLUE}[*]${NC} $1" } print_success() { echo -e "${GREEN}[✓]${NC} $1" } print_warning() { echo -e "${YELLOW}[!]${NC} $1" } print_error() { echo -e "${RED}[✗]${NC} $1" } print_break() { echo -e "${MAGENTA}[BREAK]${NC} $1" } # Check if running as root if [[ $EUID -ne 0 ]]; then print_error "This script must be run as root (use sudo)" exit 1 fi # Detect PHP version PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;" 2>/dev/null || echo "8.3") # ============================================================ # PROBLEM 1: Nginx Configuration Error (company.local) # Category: Web Server Configuration # ============================================================ break_nginx_config() { print_break "Breaking company.local (Nginx config error)..." # Introduce a syntax error - remove semicolon cat > /etc/nginx/sites-available/company.local << 'EOF' server { listen 80; server_name company.local; root /var/www/company; index index.html location / { try_files $uri $uri/ =404; } error_log /var/log/nginx/company_error.log; access_log /var/log/nginx/company_access.log; } EOF # Try to reload nginx (will fail silently) nginx -t > /dev/null 2>&1 || true systemctl reload nginx 2>/dev/null || systemctl restart nginx 2>/dev/null || true print_success "Problem 1 active: company.local has a configuration error" } # ============================================================ # PROBLEM 2: File Permissions (shop.local) # Category: File System Permissions # ============================================================ break_file_permissions() { print_break "Breaking shop.local (wrong file permissions)..." # Remove all permissions from the website directory chmod 000 /var/www/shop/index.html print_success "Problem 2 active: shop.local has permission issues" } # ============================================================ # PROBLEM 3: PHP-FPM Service Stopped (api.local) # Category: Service Management (systemd) # ============================================================ break_php_service() { print_break "Breaking api.local (PHP-FPM service stopped)..." # Stop and disable PHP-FPM systemctl stop php${PHP_VERSION}-fpm systemctl disable php${PHP_VERSION}-fpm 2>/dev/null print_success "Problem 3 active: API returns 502 Bad Gateway (PHP-FPM stopped)" } # ============================================================ # PROBLEM 4: Firewall Blocking Docker App # Category: Firewall (UFW) # ============================================================ break_firewall() { print_break "Breaking Docker app access (firewall block)..." # Block port 8888 ufw delete allow 8888/tcp > /dev/null 2>&1 ufw deny 8888/tcp > /dev/null 2>&1 print_success "Problem 4 active: Docker app unreachable (port blocked)" } # ============================================================ # PROBLEM 5: MySQL User Configuration # Category: Database Administration # ============================================================ break_mysql_user() { print_break "Breaking MySQL database access..." # Drop the working user and create one bound to wrong IP mysql -e "DROP USER IF EXISTS 'webuser'@'localhost';" mysql -e "CREATE USER 'webuser'@'10.0.0.1' IDENTIFIED BY 'WebPass123!';" mysql -e "GRANT ALL PRIVILEGES ON practicedb.* TO 'webuser'@'10.0.0.1';" mysql -e "FLUSH PRIVILEGES;" print_success "Problem 5 active: MySQL connection refused (wrong host binding)" } # ============================================================ # RESET FUNCTIONS # ============================================================ reset_nginx_config() { print_status "Resetting company.local configuration..." cat > /etc/nginx/sites-available/company.local << 'EOF' server { listen 80; server_name company.local; root /var/www/company; index index.html; location / { try_files $uri $uri/ =404; } error_log /var/log/nginx/company_error.log; access_log /var/log/nginx/company_access.log; } EOF nginx -t > /dev/null 2>&1 && systemctl reload nginx print_success "company.local restored" } reset_file_permissions() { print_status "Resetting shop.local permissions..." chmod 644 /var/www/shop/index.html chown www-data:www-data /var/www/shop/index.html print_success "shop.local permissions restored" } reset_php_service() { print_status "Resetting PHP-FPM service..." systemctl enable php${PHP_VERSION}-fpm systemctl start php${PHP_VERSION}-fpm print_success "PHP-FPM service restored" } reset_firewall() { print_status "Resetting firewall rules..." ufw delete deny 8888/tcp > /dev/null 2>&1 ufw allow 8888/tcp > /dev/null 2>&1 print_success "Firewall rules restored" } reset_mysql_user() { print_status "Resetting MySQL user..." mysql -e "DROP USER IF EXISTS 'webuser'@'10.0.0.1';" mysql -e "DROP USER IF EXISTS 'webuser'@'localhost';" mysql -e "CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'WebPass123!';" mysql -e "GRANT ALL PRIVILEGES ON practicedb.* TO 'webuser'@'localhost';" mysql -e "FLUSH PRIVILEGES;" print_success "MySQL user restored" } reset_all() { echo "" print_status "Resetting all problems..." echo "" reset_nginx_config reset_file_permissions reset_php_service reset_firewall reset_mysql_user # Restart services systemctl restart nginx echo "" print_success "All problems have been reset!" echo "" } # ============================================================ # START EXAM (ALL PROBLEMS) # ============================================================ start_exam() { echo "" echo -e "${RED}============================================================${NC}" echo -e "${RED} STARTING PRACTICE EXAM - 45 MINUTES${NC}" echo -e "${RED}============================================================${NC}" echo "" print_warning "Introducing 5 problems to the server..." echo "" break_nginx_config break_file_permissions break_php_service break_firewall break_mysql_user # Restart nginx to apply changes systemctl restart nginx 2>/dev/null || true echo "" echo -e "${RED}============================================================${NC}" echo "" print_warning "EXAM STARTED! You have 45 minutes to fix all issues." echo "" echo "The following services should be working when you're done:" echo "" echo " Task 1: http://company.local - Company Portal" echo " Task 2: http://shop.local - Online Shop" echo " Task 3: http://api.local:8080 - REST API" echo " Task 4: http://localhost:8888 - Docker App" echo " Task 5: mysql -u webuser -p'WebPass123!' practicedb" echo "" echo "Use './verify.sh' to check your progress" echo "" echo -e "${RED}============================================================${NC}" echo "" } # ============================================================ # VERIFICATION # ============================================================ verify_all() { echo "" echo "============================================================" echo " VERIFICATION RESULTS" echo "============================================================" echo "" PASSED=0 FAILED=0 # Test company.local echo -n "Task 1 - company.local: " if curl -s -o /dev/null -w "%{http_code}" http://company.local 2>/dev/null | grep -q "200"; then echo -e "${GREEN}PASS${NC}" ((PASSED++)) else echo -e "${RED}FAIL${NC}" ((FAILED++)) fi # Test shop.local echo -n "Task 2 - shop.local: " if curl -s http://shop.local 2>/dev/null | grep -q "Online Shop"; then echo -e "${GREEN}PASS${NC}" ((PASSED++)) else echo -e "${RED}FAIL${NC}" ((FAILED++)) fi # Test API echo -n "Task 3 - api.local:8080: " if curl -s http://api.local:8080 2>/dev/null | grep -q "success"; then echo -e "${GREEN}PASS${NC}" ((PASSED++)) else echo -e "${RED}FAIL${NC}" ((FAILED++)) fi # Test Docker app echo -n "Task 4 - Docker app :8888: " if curl -s http://localhost:8888 2>/dev/null | grep -q "Docker"; then echo -e "${GREEN}PASS${NC}" ((PASSED++)) else echo -e "${RED}FAIL${NC}" ((FAILED++)) fi # Test MySQL echo -n "Task 5 - MySQL connection: " if mysql -u webuser -p'WebPass123!' practicedb -e "SELECT 1;" > /dev/null 2>&1; then echo -e "${GREEN}PASS${NC}" ((PASSED++)) else echo -e "${RED}FAIL${NC}" ((FAILED++)) fi echo "" echo "============================================================" echo "" echo -e "Results: ${GREEN}${PASSED} PASSED${NC} | ${RED}${FAILED} FAILED${NC}" echo "" if [ $FAILED -eq 0 ]; then echo -e "${GREEN}Congratulations! All tasks completed successfully!${NC}" else echo -e "${YELLOW}Keep working - ${FAILED} task(s) still need to be fixed.${NC}" fi echo "" echo "============================================================" } # ============================================================ # MAIN MENU # ============================================================ show_menu() { clear echo "" echo -e "${CYAN}============================================================${NC}" echo -e "${CYAN} PRACTICE EXAM - BREAK SERVER MENU${NC}" echo -e "${CYAN} Betriebssysteme und Netzwerke${NC}" echo -e "${CYAN}============================================================${NC}" echo "" echo " INDIVIDUAL PROBLEMS:" echo "" echo " 1) Break company.local (Nginx config error)" echo " 2) Break shop.local (File permissions)" echo " 3) Break api.local (PHP-FPM service)" echo " 4) Break Docker app (Firewall block)" echo " 5) Break MySQL (User configuration)" echo "" echo " EXAM MODE:" echo "" echo " 6) START EXAM (All 5 problems - 45 minutes)" echo "" echo " UTILITIES:" echo "" echo " 7) Verify all services" echo " 8) RESET all problems" echo " 9) Reset specific problem" echo "" echo " 0) Exit" echo "" echo -e "${CYAN}============================================================${NC}" echo "" } show_reset_menu() { echo "" echo " SELECT PROBLEM TO RESET:" echo "" echo " 1) Reset company.local" echo " 2) Reset shop.local" echo " 3) Reset api.local (PHP-FPM)" echo " 4) Reset Docker app (Firewall)" echo " 5) Reset MySQL user" echo " 6) Back to main menu" echo "" } # ============================================================ # MAIN LOOP # ============================================================ while true; do show_menu read -p " Select option: " choice case $choice in 1) break_nginx_config systemctl restart nginx 2>/dev/null || true read -p "Press Enter to continue..." ;; 2) break_file_permissions read -p "Press Enter to continue..." ;; 3) break_php_service read -p "Press Enter to continue..." ;; 4) break_firewall read -p "Press Enter to continue..." ;; 5) break_mysql_user read -p "Press Enter to continue..." ;; 6) start_exam read -p "Press Enter to continue..." ;; 7) verify_all read -p "Press Enter to continue..." ;; 8) reset_all read -p "Press Enter to continue..." ;; 9) show_reset_menu read -p " Select problem to reset: " reset_choice case $reset_choice in 1) reset_nginx_config ;; 2) reset_file_permissions ;; 3) reset_php_service ;; 4) reset_firewall ;; 5) reset_mysql_user ;; 6) continue ;; *) print_error "Invalid option" ;; esac read -p "Press Enter to continue..." ;; 0) echo "" print_status "Goodbye!" exit 0 ;; *) print_error "Invalid option" read -p "Press Enter to continue..." ;; esac done