action.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 686 $"
00023 __date__ = "$Date: 2008-04-13 19:48:52 +0200 (Sun, 13 Apr 2008) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026
00027 import logging, os
00028
00029
00030
00031 logSys = logging.getLogger("fail2ban.actions.action")
00032
00033
00034
00035
00036
00037
00038
00039
00040 class Action:
00041
00042 def __init__(self, name):
00043 self.__name = name
00044 self.__cInfo = dict()
00045
00046 self.__actionStart = ''
00047
00048 self.__actionBan = ''
00049
00050 self.__actionUnban = ''
00051
00052 self.__actionCheck = ''
00053
00054 self.__actionStop = ''
00055 logSys.debug("Created Action")
00056
00057
00058
00059
00060
00061
00062 def setName(self, name):
00063 self.__name = name
00064
00065
00066
00067
00068
00069
00070 def getName(self):
00071 return self.__name
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 def setCInfo(self, key, value):
00084 self.__cInfo[key] = value
00085
00086
00087
00088
00089
00090
00091 def getCInfo(self, key):
00092 return self.__cInfo[key]
00093
00094
00095
00096
00097
00098
00099 def delCInfo(self, key):
00100 del self.__cInfo[key]
00101
00102
00103
00104
00105
00106
00107 def setActionStart(self, value):
00108 self.__actionStart = value
00109 logSys.debug("Set actionStart = %s" % value)
00110
00111
00112
00113
00114
00115
00116 def getActionStart(self):
00117 return self.__actionStart
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 def execActionStart(self):
00128 startCmd = Action.replaceTag(self.__actionStart, self.__cInfo)
00129 return Action.executeCmd(startCmd)
00130
00131
00132
00133
00134
00135
00136 def setActionBan(self, value):
00137 self.__actionBan = value
00138 logSys.debug("Set actionBan = %s" % value)
00139
00140
00141
00142
00143
00144
00145 def getActionBan(self):
00146 return self.__actionBan
00147
00148
00149
00150
00151
00152
00153 def execActionBan(self, aInfo):
00154 return self.__processCmd(self.__actionBan, aInfo)
00155
00156
00157
00158
00159
00160
00161 def setActionUnban(self, value):
00162 self.__actionUnban = value
00163 logSys.debug("Set actionUnban = %s" % value)
00164
00165
00166
00167
00168
00169
00170 def getActionUnban(self):
00171 return self.__actionUnban
00172
00173
00174
00175
00176
00177
00178 def execActionUnban(self, aInfo):
00179 return self.__processCmd(self.__actionUnban, aInfo)
00180
00181
00182
00183
00184
00185
00186 def setActionCheck(self, value):
00187 self.__actionCheck = value
00188 logSys.debug("Set actionCheck = %s" % value)
00189
00190
00191
00192
00193
00194
00195 def getActionCheck(self):
00196 return self.__actionCheck
00197
00198
00199
00200
00201
00202
00203 def setActionStop(self, value):
00204 self.__actionStop = value
00205 logSys.debug("Set actionStop = %s" % value)
00206
00207
00208
00209
00210
00211
00212 def getActionStop(self):
00213 return self.__actionStop
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 def execActionStop(self):
00224 stopCmd = Action.replaceTag(self.__actionStop, self.__cInfo)
00225 return Action.executeCmd(stopCmd)
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 def replaceTag(query, aInfo):
00236 """ Replace tags in query
00237 """
00238 string = query
00239 for tag in aInfo:
00240 string = string.replace('<' + tag + '>', str(aInfo[tag]))
00241
00242 string = string.replace("<br>", '\n')
00243 return string
00244 replaceTag = staticmethod(replaceTag)
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 def __processCmd(self, cmd, aInfo = None):
00260 """ Executes an OS command.
00261 """
00262 if cmd == "":
00263 logSys.debug("Nothing to do")
00264 return True
00265
00266 checkCmd = Action.replaceTag(self.__actionCheck, self.__cInfo)
00267 if not Action.executeCmd(checkCmd):
00268 logSys.error("Invariant check failed. Trying to restore a sane" +
00269 " environment")
00270 stopCmd = Action.replaceTag(self.__actionStop, self.__cInfo)
00271 Action.executeCmd(stopCmd)
00272 startCmd = Action.replaceTag(self.__actionStart, self.__cInfo)
00273 Action.executeCmd(startCmd)
00274 if not Action.executeCmd(checkCmd):
00275 logSys.fatal("Unable to restore environment")
00276 return False
00277
00278
00279 if not aInfo == None:
00280 realCmd = Action.replaceTag(cmd, aInfo)
00281 else:
00282 realCmd = cmd
00283
00284
00285 realCmd = Action.replaceTag(realCmd, self.__cInfo)
00286
00287 return Action.executeCmd(realCmd)
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 def executeCmd(realCmd):
00303 logSys.debug(realCmd)
00304 try:
00305
00306
00307 retcode = os.system(realCmd)
00308 if retcode == 0:
00309 logSys.debug("%s returned successfully" % realCmd)
00310 return True
00311 else:
00312 logSys.error("%s returned %x" % (realCmd, retcode))
00313 except OSError, e:
00314 logSys.error("%s failed with %s" % (realCmd, e))
00315 return False
00316 executeCmd = staticmethod(executeCmd)
00317