forked from denyhosts/denyhosts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon-control-dist
More file actions
executable file
·178 lines (144 loc) · 4.73 KB
/
Copy pathdaemon-control-dist
File metadata and controls
executable file
·178 lines (144 loc) · 4.73 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
import os # do not edit
import sys # do not edit
import platform # do not edit
import signal # do not edit
import time # do not edit
# denyhosts Bring up/down the DenyHosts daemon
#
# chkconfig: 2345 98 02
# description: Activates/Deactivates the
# DenyHosts daemon to block ssh attempts
#
###############################################
###############################################
# Edit these to suit your configuration #
###############################################
DENYHOSTS_BIN = "/usr/sbin/denyhosts.py"
DENYHOSTS_LOCK = "/run/denyhosts.pid"
DENYHOSTS_CFG = "/etc/denyhosts.conf"
PYTHON_BIN = "/usr/bin/env python"
distro = platform.uname()[3]
distro2 = platform.uname()[2]
if distro.find('Debian') >= 0:
DENYHOSTS_BIN = "/usr/local/bin/denyhosts.py"
elif distro.find('FreeBSD') >= 0:
DENYHOSTS_BIN = "/usr/local/bin/denyhosts.py"
else:
#centos
if distro2.find('el6') >= 0 or distro2.find('el7') >= 0:
DENYHOSTS_BIN = "/usr/bin/denyhosts.py"
###############################################
###############################################
# Do not edit below #
###############################################
###############################################
DENYHOSTS_BIN = '{0} {1}'.format(PYTHON_BIN, DENYHOSTS_BIN)
# make sure 'ps' command is accessible (which should be
# in either /usr/bin or /bin. Modify the PATH so
# popen can find it
env = os.environ.get('PATH', "")
os.environ['PATH'] = '/usr/bin:/bin:{0}'.format(env)
STATE_NOT_RUNNING = -1
STATE_LOCK_EXISTS = -2
def usage():
print(
'Usage: {0} start [args...] | stop | restart [args...] | status | debug | condrestart [args...]'.format(
sys.argv[0]
)
)
print()
print('For a list of valid "args" refer to:')
print("$ denyhosts.py --help")
print()
sys.exit(0)
def getpid():
try:
fp = open(DENYHOSTS_LOCK, "r")
pid = int(fp.readline().rstrip())
fp.close()
except Exception as e:
return STATE_NOT_RUNNING
if not sys.platform.startswith('freebsd') and os.access("/proc", os.F_OK):
# proc filesystem exists, look for pid
if os.access(os.path.join("/proc", str(pid)), os.F_OK):
return pid
else:
return STATE_LOCK_EXISTS
else:
# proc filesystem doesn't exist (or it doesn't contain PIDs), use 'ps'
p = os.popen("ps -p %d" % pid, "r")
p.readline() # get the header line
pid_running = p.readline()
# pid_running will be '' if no process is found
if pid_running:
return pid
else:
return STATE_LOCK_EXISTS
def start(*sargs):
scmd = '{0} --daemon '.format(DENYHOSTS_BIN)
if sargs:
scmd += ' '.join(sargs)
print('starting DenyHosts: {0}'.format(scmd))
os.system(scmd)
def stop():
pid = getpid()
if pid >= 0:
os.kill(pid, signal.SIGTERM)
print('sent DenyHosts SIGTERM')
else:
print('DenyHosts is not running')
def debug():
pid = getpid()
if pid >= 0:
os.kill(pid, signal.SIGUSR1)
print('sent DenyHosts SIGUSR1')
else:
print('DenyHosts is not running')
def status():
pid = getpid()
if pid == STATE_LOCK_EXISTS:
print('{0} exists but DenyHosts is not running'.format(DENYHOSTS_LOCK))
elif pid == STATE_NOT_RUNNING:
print('Denyhosts is not running')
else:
print('DenyHosts is running with pid = {0}'.format(pid))
def condrestart(*cargs):
pid = getpid()
if pid >= 0:
restart(*cargs)
def restart(*rargs):
stop()
time.sleep(1)
start(*rargs)
if __name__ == '__main__':
cases = {'start': start,
'stop': stop,
'debug': debug,
'status': status,
'condrestart': condrestart,
'restart': restart}
try:
args = sys.argv[2:]
except Exception:
args = []
try:
# arg 1 should contain one of the cases above
option = sys.argv[1]
except Exception:
# try to infer context (from an /etc/init.d/ script, perhaps)
procname = os.path.basename(sys.argv[0])
infer_dict = {'K': 'stop',
'S': 'start'}
option = infer_dict.get(procname[0])
if not option:
usage()
try:
if option in ('start', 'restart', 'condrestart'):
anystartswith = lambda prefix, xs: any(map(lambda x: x.startswith(prefix), xs))
if not anystartswith('--config', args) and '-c' not in args:
args.append("--config=%s" % DENYHOSTS_CFG)
cmd = cases[option]
getattr(sys.modules[__name__], 'cmd')(*args)
except Exception:
usage()