Advertisement
Haron_Prime

Untitled

Nov 21st, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4.  
  5. import DenyHosts.python_version
  6.  
  7. import getopt
  8. import traceback
  9. import logging
  10.  
  11. from DenyHosts.util import die, setup_logging, is_true
  12. from DenyHosts.lockfile import LockFile
  13. from DenyHosts.prefs import Prefs
  14. from DenyHosts.version import VERSION
  15. from DenyHosts.deny_hosts import DenyHosts
  16. from DenyHosts.denyfileutil import Purge, Migrate, UpgradeTo099
  17. from DenyHosts.constants import *
  18. from DenyHosts.sync import Sync
  19.  
  20. #################################################################################
  21.  
  22.  
  23.  
  24. def usage():
  25. print ("Usage:")
  26. print ("%s [-f logfile | --file=logfile] [ -c configfile | --config=configfile] [-i | --ignore] [-n | --noemail] [--purge] [--migrate] [--daemon] [--sync] [--version]") % sys.argv[0]
  27. print
  28. print
  29. print (" --file: The name of log file to parse")
  30. print (" --ignore: Ignore last processed offset (start processing from beginning)")
  31. print (" --noemail: Do not send an email report")
  32. print (" --unlock: if lockfile exists, remove it and run as normal")
  33. print (" --migrate: migrate your HOSTS_DENY file so that it is suitable for --purge")
  34. print (" --purge: expire entries older than your PURGE_DENY setting")
  35. print (" --daemon: run DenyHosts in daemon mode")
  36. print (" --sync: run DenyHosts synchronization mode")
  37. print (" --version: Prints the version of DenyHosts and exits")
  38.  
  39. print
  40. print ("Note: multiple --file args can be processed. "),
  41. print ("If multiple files are provided, --ignore is implied")
  42. print
  43. print ("When run in --daemon mode the following flags are ignored:")
  44. print (" --file, --purge, --migrate, --sync, --verbose")
  45.  
  46.  
  47. #################################################################################
  48.  
  49.  
  50.  
  51.  
  52. #################################################################################
  53.  
  54.  
  55. if __name__ == '__main__':
  56. logfiles = []
  57. config_file = CONFIG_FILE
  58. ignore_offset = 0
  59. noemail = 0
  60. verbose = 0
  61. migrate = 0
  62. purge = 0
  63. sync_mode = 0
  64. daemon = 0
  65. enable_debug = 0
  66. upgrade099 = 0
  67. args = sys.argv[1:]
  68. try:
  69. (opts, getopts) = getopt.getopt(args, 'f:c:dinuvps?hV',
  70. ["file=", "ignore", "verbose", "debug",
  71. "help", "noemail", "config=", "version",
  72. "migrate", "purge", "daemon", "sync",
  73. "upgrade099"])
  74. except:
  75. print ("\nInvalid command line option detected.")
  76. usage()
  77. sys.exit(1)
  78.  
  79. for opt, arg in opts:
  80. if opt in ('-h', '-?', '--help'):
  81. usage()
  82. sys.exit(0)
  83. if opt in ('-f', '--file'):
  84. logfiles.append(arg)
  85. if opt in ('-i', '--ignore'):
  86. ignore_offset = 1
  87. if opt in ('-n', '--noemail'):
  88. noemail = 1
  89. if opt in ('-v', '--verbose'):
  90. verbose = 1
  91. if opt in ('-d', '--debug'):
  92. enable_debug = 1
  93. if opt in ('-c', '--config'):
  94. config_file = arg
  95. if opt in ('-m', '--migrate'):
  96. migrate = 1
  97. if opt in ('-p', '--purge'):
  98. purge = 1
  99. if opt in ('-s', '--sync'):
  100. sync_mode = 1
  101. if opt == '--daemon':
  102. daemon = 1
  103. if opt == '--upgrade099':
  104. upgrade099 = 1
  105. if opt == '--version':
  106. print ("DenyHosts version:", VERSION)
  107. sys.exit(0)
  108.  
  109. prefs = Prefs(config_file)
  110.  
  111. first_time = 0
  112. try:
  113. os.makedirs(prefs.get('WORK_DIR'))
  114. first_time = 1
  115. except Exception as e:
  116. if e[0] != 17:
  117. print (e)
  118. sys.exit(1)
  119.  
  120. setup_logging(prefs, enable_debug, verbose, daemon)
  121.  
  122. if not logfiles or daemon:
  123. logfiles = [prefs.get('SECURE_LOG')]
  124. elif len(logfiles) > 1:
  125. ignore_offset = 1
  126.  
  127. if not prefs.get('ADMIN_EMAIL'): noemail = 1
  128.  
  129. lock_file = LockFile(prefs.get('LOCK_FILE'))
  130.  
  131. lock_file.create()
  132.  
  133. if upgrade099 and not daemon:
  134. if not prefs.get('PURGE_DENY'):
  135. lock_file.remove()
  136. die("You have supplied the --upgrade099 flag, however you have not set PURGE_DENY in your configuration file")
  137. else:
  138. u = UpgradeTo099(prefs.get("HOSTS_DENY"))
  139.  
  140. if migrate and not daemon:
  141. if not prefs.get('PURGE_DENY'):
  142. lock_file.remove()
  143. die("You have supplied the --migrate flag however you have not set PURGE_DENY in your configuration file.")
  144. else:
  145. m = Migrate(prefs.get("HOSTS_DENY"))
  146.  
  147. if purge and not daemon:
  148. purge_time = prefs.get('PURGE_DENY')
  149. if not purge_time:
  150. lock_file.remove()
  151. die("You have provided the --purge flag however you have not set PURGE_DENY in your configuration file.")
  152. else:
  153. try:
  154. p = Purge(prefs,
  155. purge_time)
  156.  
  157. except Exception as e:
  158. lock_file.remove()
  159. die(str(e))
  160.  
  161. try:
  162. for f in logfiles:
  163. dh = DenyHosts(f, prefs, lock_file, ignore_offset,
  164. first_time, noemail, daemon)
  165. except SystemExit as e:
  166. pass
  167. except Exception as e:
  168. traceback.print_exc(file=sys.stdout)
  169. print ("\nDenyHosts exited abnormally")
  170.  
  171.  
  172. if sync_mode and not daemon:
  173. if not prefs.get('SYNC_SERVER'):
  174. lock_file.remove()
  175. die("You have provided the --sync flag however your configuration file is missing a value for SYNC_SERVER.")
  176. sync_upload = is_true(prefs.get("SYNC_UPLOAD"))
  177. sync_download = is_true(prefs.get("SYNC_DOWNLOAD"))
  178. if not sync_upload and not sync_download:
  179. lock_file.remove()
  180. die("You have provided the --sync flag however your configuration file has SYNC_UPLOAD and SYNC_DOWNLOAD set to false.")
  181. try:
  182. sync = Sync(prefs)
  183. if sync_upload:
  184. timestamp = sync.send_new_hosts()
  185. if sync_download:
  186. new_hosts = sync.receive_new_hosts()
  187. if new_hosts:
  188. info("received new hosts: %s", str(new_hosts))
  189. sync.get_denied_hosts()
  190. sync.update_hosts_deny(new_hosts)
  191. sync.xmlrpc_disconnect()
  192. except Exception as e:
  193. lock_file.remove()
  194. die("Error synchronizing data", e)
  195.  
  196. # remove lock file on exit
  197. lock_file.remove()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement