/usr/share/denyhosts/DenyHosts/sync.py is in denyhosts 2.10-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import logging
import os
import time
from xmlrpclib import ServerProxy, Transport
from httplib import HTTP
from constants import SYNC_TIMESTAMP, SYNC_HOSTS, SYNC_HOSTS_TMP, SYNC_RECEIVED_HOSTS
logger = logging.getLogger("sync")
debug, info, error, exception = logger.debug, logger.info, logger.error, logger.exception
def get_plural(items):
if len(items) != 1:
return "s"
else:
return ""
class ProxiedTransport(Transport):
def set_proxy(self, proxy):
self.proxy = proxy
def make_connection(self, host):
self.realhost = host
h = HTTP(self.proxy)
return h
def send_request(self, connection, handler, request_body):
connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
def send_host(self, connection, host):
connection.putheader('Host', self.realhost)
class Sync(object):
def __init__(self, prefs):
self.__prefs = prefs
self.__work_dir = prefs.get('WORK_DIR')
self.__connected = False
self.__hosts_added = []
def xmlrpc_connect(self):
try:
proxy_server = self.__prefs.get('SYNC_PROXY_SERVER')
if proxy_server is None:
self.__server = ServerProxy(self.__prefs.get('SYNC_SERVER'))
else:
p = ProxiedTransport()
p.set_proxy(proxy_server)
self.__server = ServerProxy(self.__prefs.get('SYNC_SERVER'), transport=p)
self.__connected = True
except Exception, e:
error(str(e))
self.__connected = False
return self.__connected
def xmlrpc_disconnect(self):
if self.__connected:
try:
#self.__server.close()
self.__server = None
except Exception:
pass
self.__connected = False
def get_sync_timestamp(self):
try:
fp = open(os.path.join(self.__work_dir,
SYNC_TIMESTAMP))
timestamp = fp.readline()
timestamp = long(timestamp.strip())
return timestamp
except Exception, e:
error(str(e))
return 0l
def set_sync_timestamp(self, timestamp):
try:
fp = open(os.path.join(self.__work_dir,
SYNC_TIMESTAMP), "w")
fp.write(timestamp)
except Exception, e:
error(e)
def send_new_hosts(self):
debug("send_new_hosts()")
self.__hosts_added = []
try:
src_file = os.path.join(self.__work_dir, SYNC_HOSTS)
dest_file = os.path.join(self.__work_dir, SYNC_HOSTS_TMP)
os.rename(src_file, dest_file)
except OSError:
return False
hosts = []
fp = open(dest_file, "r")
for line in fp.readlines():
hosts.append(line.strip())
fp.close()
try:
self.__send_new_hosts(hosts)
info("sent %d new host%s", len(hosts), get_plural(hosts))
self.__hosts_added = hosts
except Exception:
os.rename(dest_file, src_file)
return False
try:
os.unlink(dest_file)
except OSError:
pass
return True
def __send_new_hosts(self, hosts):
if not self.__connected and not self.xmlrpc_connect():
error("Could not initiate xmlrpc connection")
return
try:
self.__server.add_hosts(hosts)
except Exception, e:
exception(e)
def receive_new_hosts(self):
debug("receive_new_hosts()")
if not self.__connected and not self.xmlrpc_connect():
error("Could not initiate xmlrpc connection")
return
timestamp = self.get_sync_timestamp()
try:
data = self.__server.get_new_hosts(timestamp,
self.__prefs.get("SYNC_DOWNLOAD_THRESHOLD"),
self.__hosts_added,
self.__prefs.get("SYNC_DOWNLOAD_RESILIENCY"))
timestamp = data['timestamp']
self.set_sync_timestamp(timestamp)
hosts = data['hosts']
info("received %d new host%s", len(hosts), get_plural(hosts))
self.__save_received_hosts(hosts, timestamp)
return hosts
except Exception, e:
exception(e)
return None
def __save_received_hosts(self, hosts, timestamp):
try:
fp = open(os.path.join(self.__work_dir, SYNC_RECEIVED_HOSTS), "a")
except IOError, e:
error(e)
return
timestr = time.ctime(float(timestamp))
for host in hosts:
fp.write("%s:%s\n" % (host, timestr))
fp.close()
|