aboutsummaryrefslogtreecommitdiffstats
path: root/service_management.sh
blob: dcfa26ca26fdff369bed9c79b26285a30274ab7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#
# Service Management Utility
# 
# WARNING: This script is provided for educational purposes ONLY.
# Unauthorized use to interrupt services is illegal and unethical.
#
# PURPOSE: Demonstrates techniques for monitoring and controlling services
# with proper authentication and logging for legitimate administration.

# Configuration
LOG_FILE="/var/log/maintenance.log"
AUTH_USERS_FILE="authorized_users.txt"

# Check if running as root
if [ "$EUID" -ne 0 ]; then
  echo "This script requires root privileges"
  exit 1
fi

# Functions
log_action() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - $USER - $1" | tee -a "$LOG_FILE"
}

is_authorized() {
  if [ ! -f "$AUTH_USERS_FILE" ]; then
    echo "Error: Authorization file not found"
    return 1
  fi
  
  grep -q "^$USER$" "$AUTH_USERS_FILE"
  return $?
}

show_usage() {
  echo "Usage: $0 [options] <action> <service>"
  echo "Options:"
  echo "  -h, --help     Show this help message"
  echo "  -l, --list     List running services"
  echo "  -f, --firewall Manage firewall rules"
  echo ""
  echo "Actions:"
  echo "  start          Start a service"
  echo "  stop           Stop a service"
  echo "  restart        Restart a service"
  echo "  status         Show service status"
  echo ""
  echo "Examples:"
  echo "  $0 stop nginx"
  echo "  $0 --firewall block 80"
}

list_services() {
  log_action "Listed running services"
  systemctl list-units --type=service --state=running
}

manage_service() {
  local action=$1
  local service=$2
  
  if ! systemctl list-unit-files --type=service | grep -q "$service"; then
    echo "Error: Service $service not found"
    return 1
  fi
  
  log_action "Executing: systemctl $action $service"
  systemctl "$action" "$service"
  
  echo "Service $service $action operation completed"
  log_action "Completed $action on $service"
}

manage_firewall() {
  local action=$1
  local port=$2
  
  if [ "$action" == "block" ]; then
    log_action "Blocking port $port"
    ufw deny "$port"
  elif [ "$action" == "allow" ]; then
    log_action "Allowing port $port"
    ufw allow "$port"
  else
    echo "Error: Unknown firewall action"
    return 1
  fi
  
  echo "Firewall rule applied for port $port"
}

# Main script
if ! is_authorized; then
  echo "Error: User $USER not authorized to run this script"
  log_action "UNAUTHORIZED ACCESS ATTEMPT"
  exit 1
fi

if [ $# -lt 1 ]; then
  show_usage
  exit 1
fi

case "$1" in
  -h|--help)
    show_usage
    ;;
  -l|--list)
    list_services
    ;;
  -f|--firewall)
    if [ $# -lt 3 ]; then
      echo "Error: Missing arguments for firewall management"
      show_usage
      exit 1
    fi
    manage_firewall "$2" "$3"
    ;;
  start|stop|restart|status)
    if [ $# -lt 2 ]; then
      echo "Error: Missing service name"
      show_usage
      exit 1
    fi
    manage_service "$1" "$2"
    ;;
  *)
    echo "Error: Unknown action $1"
    show_usage
    exit 1
    ;;
esac

exit 0