예제 #1
0
 def test_send_hosts(self):
     sync = Sync(self.prefs)
     self.assertEqual(sync.receive_new_hosts(), self.test_hosts)
     filename = ospj(self.work_dir, SYNC_RECEIVED_HOSTS)
     with open(filename) as f:
         hosts = [line.strip().split(':')[0] for line in f]
     self.assertEqual(self.test_hosts, hosts)
예제 #2
0
 def test_connect_disconnect(self):
     sync = Sync(self.prefs)
     self.assertTrue(sync.xmlrpc_connect())
     self.assertFalse(sync._Sync__server is None)
     self.assertTrue(sync._Sync__connected)
     sync.xmlrpc_disconnect()
     self.assertTrue(sync._Sync__server is None)
     self.assertFalse(sync._Sync__connected)
예제 #3
0
class SyncTestDynamicTimestamp(unittest.TestCase):
    """
    Tests that we can set the timestamp on the filesystem. Separated
    into a different test class to avoid clobbering the static test data
    for SyncTestStaticTimestamp.
    """
    def setUp(self):
        self.prefs = Prefs()
        self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__), 'data/sync/dynamic')
        self.sync = Sync(self.prefs)
        self.value = randint(0, 1e9)

    def test_set_sync_timestamp(self):
        self.sync.set_sync_timestamp(str(self.value))
        path = ospj(self.prefs._Prefs__data['WORK_DIR'], SYNC_TIMESTAMP)
        with open(path) as f:
            saved_timestamp = int(f.read().strip())
        self.assertEqual(self.value, saved_timestamp)
예제 #4
0
class SyncTestStaticTimestamp(unittest.TestCase):
    """
    Tests that we can read the sync timestamp from the filesystem.
    """
    def setUp(self):
        self.prefs = Prefs()
        self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__), 'data/sync/static')
        self.sync = Sync(self.prefs)

    def test_get_sync_timestamp(self):
        timestamp = 427850432
        self.assertEqual(self.sync.get_sync_timestamp(), timestamp)
예제 #5
0
    except Exception, e:
        traceback.print_exc(file=sys.stdout)
        print "\nDenyHosts exited abnormally"


    if sync_mode and not daemon:
        if not prefs.get('SYNC_SERVER'):
            lock_file.remove()
            die("You have provided the --sync flag however your configuration file is missing a value for SYNC_SERVER.")
        sync_upload = is_true(prefs.get("SYNC_UPLOAD"))
        sync_download = is_true(prefs.get("SYNC_DOWNLOAD"))
        if not sync_upload and not sync_download:
           lock_file.remove()
           die("You have provided the --sync flag however your configuration file has SYNC_UPLOAD and SYNC_DOWNLOAD set to false.")
        try:  
            sync = Sync(prefs)
            if sync_upload:
                timestamp = sync.send_new_hosts()
            if sync_download: 
                new_hosts = sync.receive_new_hosts()
                if new_hosts:
                    info("received new hosts: %s", str(new_hosts))
                    sync.get_denied_hosts()
                    sync.update_hosts_deny(new_hosts)
            sync.xmlrpc_disconnect()
        except Exception, e:
            lock_file.remove()
            die("Error synchronizing data", e)
        
    # remove lock file on exit
    lock_file.remove()
예제 #6
0
 def test_send_hosts(self):
     sync = Sync(self.prefs)
     self.assertFalse(sync._Sync__hosts_added)
     self.assertTrue(sync.send_new_hosts())
     self.assertEqual(sync._Sync__hosts_added, self.test_hosts)
예제 #7
0
 def test_connect_false(self):
     syncserver = self.prefs._Prefs__data['SYNC_SERVER']
     self.prefs._Prefs__data['SYNC_SERVER'] = None
     sync = Sync(self.prefs)
     self.assertFalse(sync.xmlrpc_connect())
     self.prefs._Prefs__data['SYNC_SERVER'] = syncserver
예제 #8
0
 def setUp(self):
     self.prefs = Prefs()
     self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__),
                                                'data/sync/dynamic')
     self.sync = Sync(self.prefs)
     self.value = randint(0, 1e9)
예제 #9
0
 def setUp(self):
     self.prefs = Prefs()
     self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__),
                                                'data/sync/static')
     self.sync = Sync(self.prefs)
예제 #10
0
 def test_send_hosts(self):
     sync = Sync(self.prefs)
     self.assertFalse(sync._Sync__hosts_added)
     self.assertTrue(sync.send_new_hosts())
     self.assertEqual(sync._Sync__hosts_added, self.test_hosts)
예제 #11
0
 def setUp(self):
     self.prefs = Prefs()
     self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__), 'data/sync/dynamic')
     self.sync = Sync(self.prefs)
     self.value = randint(0, 1e9)
예제 #12
0
 def setUp(self):
     self.prefs = Prefs()
     self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__), 'data/sync/static')
     self.sync = Sync(self.prefs)
예제 #13
0
        print("\nDenyHosts exited abnormally")

    if sync_mode and not (daemon or foreground):
        if not prefs.get('SYNC_SERVER'):
            lock_file.remove()
            die("You have provided the --sync flag however your configuration file is missing a value for SYNC_SERVER.")
        sync_upload = is_true(prefs.get("SYNC_UPLOAD"))
        sync_download = is_true(prefs.get("SYNC_DOWNLOAD"))
        if not sync_upload and not sync_download:
            lock_file.remove()
            die(
                "You have provided the --sync flag"
                " however your configuration file has SYNC_UPLOAD and SYNC_DOWNLOAD set to false."
            )
        try:
            sync = Sync(prefs)
            if sync_upload:
                timestamp = sync.send_new_hosts()
            if sync_download:
                new_hosts = sync.receive_new_hosts()
                if new_hosts:
                    # MMR: What is 'info' here?
                    info("received new hosts: %s", str(new_hosts))
                    # TODO sync method's don't exist what should these be doing?
                    sync.get_denied_hosts()
                    sync.update_hosts_deny(new_hosts)

                    dh.get_denied_hosts()
                    dh.update_hosts_deny(new_hosts)
            sync.xmlrpc_disconnect()
        except Exception as e:
예제 #14
0
        if opt == '--upgrade099':
            upgrade099 = 1
        if opt == '--version':
            print("DenyHosts version:", VERSION)
            sys.exit(0)

    # This is generally expected to be in the environment, but there's no
    # non-hackish way to get systemd to set it, so just hack it in here.
    os.environ['HOSTNAME'] = platform.node()

    prefs = Prefs(config_file)
    iptables = prefs.get('IPTABLES')

    if prefs.get('SYNC_SERVER'):
        try:
            sync = Sync(prefs)
            sync.send_release_used(VERSION)
            del sync
        except:
            # more than likely sync server doesn't have the option yet
            pass

    first_time = 0
    try:
        if not os.path.exists(prefs.get('WORK_DIR')):
            os.makedirs(prefs.get('WORK_DIR'))
            first_time = 1
    except Exception as e:
        if e[0] != 17:
            print(e)
            sys.exit(1)