示例#1
0
            purge = 1
        if opt in ('-s', '--sync'):
            sync_mode = 1
        if opt == '--daemon':
            daemon = 1
        if opt == '--upgrade099':
            upgrade099 = 1
        if opt == '--version':
            print "DenyHosts version:", VERSION
            sys.exit(0)

    prefs = Prefs(config_file)    
            
    first_time = 0
    try:
        os.makedirs(prefs.get('WORK_DIR'))
        first_time = 1
    except Exception, e:
        if e[0] != 17:
            print e
            sys.exit(1)

    setup_logging(prefs, enable_debug, verbose, daemon)
    
    if not logfiles or daemon:
        logfiles = [prefs.get('SECURE_LOG')]
    elif len(logfiles) > 1:
        ignore_offset = 1

    if not prefs.get('ADMIN_EMAIL'): noemail = 1
示例#2
0
class UtilsTest(unittest.TestCase):
    def setUp(self):
        self.true_strings = ['1', 't', 'true', 'y', 'yes']
        self.false_strings = ['', 'false', 'ye', 'tr']
        self.prefs = Prefs()
        self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__),
                                                   'data/utils/')
        self.prefs._Prefs__data['DAEMON_LOG'] = ospj(
            self.prefs.get('WORK_DIR'), 'daemon.log')
        self.prefs._Prefs__data['ADMIN_EMAIL'] = '*****@*****.**'
        self.prefs._Prefs__data['SMTP_FROM'] = '*****@*****.**'
        self.prefs._Prefs__data['SMTP_SUBJECT'] = 'Denyhosts Mailinator'
        self.prefs._Prefs__data['SMTP_DATE_FORMAT'] = '%Y%m%d'
        self.prefs._Prefs__data['SMTP_HOST'] = 'localhost'
        self.prefs._Prefs__data['SMTP_PORT'] = 2500
        self.prefs._Prefs__data['SMTP_SSL'] = 'False'

    def test_is_true(self):
        for string in self.true_strings:
            self.assertTrue(util.is_true(string))

        for string in self.false_strings:
            self.assertFalse(util.is_true(string))

    def test_is_false(self):
        for string in self.true_strings:
            self.assertFalse(util.is_false(string))

        for string in self.false_strings:
            self.assertTrue(util.is_false(string))

    def test_parse_host(self):
        for line, expected in HOST_TEST_DATA:
            self.assertEqual(util.parse_host(line), expected)

    def test_valid_ip(self):
        for ip, expected in TEST_IPS:
            self.assertEqual(util.is_valid_ip_address(ip), expected)

    def test_setup_logging_with_debug(self):
        self.assertIsNone(util.setup_logging(self.prefs, True, False, False))

    def test_setup_logging_no_debug(self):
        self.assertIsNone(util.setup_logging(self.prefs, False, False, False))

    def test_seconds(self):
        self.assertEqual(util.calculate_seconds(1), 1)

    def test_invalid_seconds_format(self):
        with self.assertRaises(Exception) as cm:
            util.calculate_seconds(datetime.now())
            self.assertEqual(cm.exception, 'Error')

    def test_seconds_zero_false(self):
        with self.assertRaises(Exception) as cm:
            util.calculate_seconds('0', False)
            self.assertEqual(cm.exception, 'Error')

    def test_send_email_success(self):
        self.assertIsNone(util.send_email(self.prefs,
                                          'testing report success'))

    def test_send_email_error(self):
        self.assertIsNone(util.send_email(self.prefs,
                                          'testing report failure'))

    def test_whitespace(self):
        self.assertEqual(
            util.normalize_whitespace('testing whitespace  for   denyhosts'),
            'testing whitespace for denyhosts')

    def test_get_hostname_bug_145(self):
        self.assertEqual(util.gethostbyname('68.ip-51-38-131.eu'),
                         '51.38.131.68')
示例#3
0
            purge = 1
        if opt in ('-s', '--sync'):
            sync_mode = 1
        if opt == '--daemon':
            daemon = 1
        if opt == '--upgrade099':
            upgrade099 = 1
        if opt == '--version':
            print "DenyHosts version:", VERSION
            sys.exit(0)

    prefs = Prefs(config_file)    
            
    first_time = 0
    try:
        os.makedirs(prefs.get('WORK_DIR'))
        first_time = 1
    except Exception, e:
        if e[0] != 17:
            print e
            sys.exit(1)

    setup_logging(prefs, enable_debug, verbose, daemon)
    
    if not logfiles or daemon:
        logfiles = [prefs.get('SECURE_LOG')]
    elif len(logfiles) > 1:
        ignore_offset = 1

    if not prefs.get('ADMIN_EMAIL'): noemail = 1
示例#4
0
            purge = 1
        if opt in ("-s", "--sync"):
            sync_mode = 1
        if opt == "--daemon":
            daemon = 1
        if opt == "--upgrade099":
            upgrade099 = 1
        if opt == "--version":
            print "DenyHosts version:", VERSION
            sys.exit(0)

    prefs = Prefs(config_file)

    first_time = 0
    try:
        os.makedirs(prefs.get("WORK_DIR"))
        first_time = 1
    except Exception, e:
        if e[0] != 17:
            print e
            sys.exit(1)

    setup_logging(prefs, enable_debug, verbose, daemon)

    if not logfiles or daemon:
        logfiles = [prefs.get("SECURE_LOG")]
    elif len(logfiles) > 1:
        ignore_offset = 1

    if not prefs.get("ADMIN_EMAIL"):
        noemail = 1
示例#5
0
            purgeip = 1
        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)

    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)

    # On some operating systems the file /etc/hosts.deny (or
    # whatever HOSTS_DENY is set to, may not exist. We should
    # "touch" the file to make sure it is there to avoid errors later.
    try:
        host_filename = prefs.get("HOSTS_DENY")
        if host_filename:
            fp = open(prefs.get("HOSTS_DENY"), "a")
            fp.close()
示例#6
0
class UtilsTest(unittest.TestCase):
    def setUp(self):
        self.true_strings = ['1', 't', 'true', 'y', 'yes']
        self.false_strings = ['', 'false', 'ye', 'tr']
        self.prefs = Prefs()
        self.prefs._Prefs__data['WORK_DIR'] = ospj(dirname(__file__),
                                                   'data/utils/')
        self.prefs._Prefs__data['DAEMON_LOG'] = ospj(
            self.prefs.get('WORK_DIR'), 'daemon.log')
        self.prefs._Prefs__data['ADMIN_EMAIL'] = '*****@*****.**'
        self.prefs._Prefs__data['SMTP_FROM'] = '*****@*****.**'
        self.prefs._Prefs__data['SMTP_SUBJECT'] = 'Denyhosts Mailinator'
        self.prefs._Prefs__data['SMTP_DATE_FORMAT'] = '%Y%m%d'
        self.prefs._Prefs__data['SMTP_HOST'] = 'localhost'
        self.prefs._Prefs__data['SMTP_PORT'] = 2500

    def test_is_true(self):
        for string in self.true_strings:
            self.assertTrue(util.is_true(string))

        for string in self.false_strings:
            self.assertFalse(util.is_true(string))

    def test_is_false(self):
        for string in self.true_strings:
            self.assertFalse(util.is_false(string))

        for string in self.false_strings:
            self.assertTrue(util.is_false(string))

    def test_parse_host(self):
        for line, expected in HOST_TEST_DATA:
            self.assertEqual(util.parse_host(line), expected)

    def test_mail_command(self):
        executable = str(sys.executable).replace(os.sep,
                                                 '/')  # To support windows
        self.assertEqual(
            0, send_mail_by_command(executable, ["--version"], "", print))
        self.assertNotEquals(
            0,
            send_mail_by_command(executable, ["--option-that-does-not-exist"],
                                 "", print))
        self.assertEquals(
            42,
            send_mail_by_command(
                executable +
                ' -c "import sys; c = sys.stdin.read(); exit(int(c))"', [],
                '42', print))
        self.assertEquals(
            3,
            send_mail_by_command(
                executable + ' -c "import sys; c = sys.argv[3]; exit(int(c))"',
                ["1", "2", "3"], "spam", print))

    def test_valid_ip(self):
        for ip, expected in TEST_IPS:
            self.assertEqual(util.is_valid_ip_address(ip), expected)

    def test_setup_logging_with_debug(self):
        self.assertIsNone(util.setup_logging(self.prefs, True, False, False))

    def test_setup_logging_no_debug(self):
        self.assertIsNone(util.setup_logging(self.prefs, False, False, False))

    def test_seconds(self):
        self.assertEqual(util.calculate_seconds(1), 1)

    def test_invalid_seconds_format(self):
        with self.assertRaises(Exception) as cm:
            util.calculate_seconds(datetime.now())
            self.assertEqual(cm.exception, 'Error')

    def test_seconds_zero_false(self):
        with self.assertRaises(Exception) as cm:
            util.calculate_seconds('0', False)
            self.assertEqual(cm.exception, 'Error')

    def test_send_email_success(self):
        self.assertIsNone(util.send_email(self.prefs, 'testing report'))

    def test_send_email_error(self):
        self.prefs._Prefs__data['SMTP_PORT'] = 25
        self.assertIsNone(util.send_email(self.prefs, 'testing report'))

    def test_whitespace(self):
        self.assertEqual(
            util.normalize_whitespace('testing whitespace  for   denyhosts'),
            'testing whitespace for denyhosts')