allowedhosts.py :  » Network » DenyHosts » DenyHosts-2.6 » DenyHosts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » DenyHosts 
DenyHosts » DenyHosts 2.6 » DenyHosts » allowedhosts.py
import os
from socket import getfqdn,gethostbyname
import logging


from constants import ALLOWED_HOSTS,ALLOWED_WARNED_HOSTS
from regex import ALLOWED_REGEX
from util import is_true

debug = logging.getLogger("AllowedHosts").debug

class AllowedHosts:
    def __init__(self, prefs):
        debug("initializing AllowedHosts")
        work_dir = prefs.get("WORK_DIR")
        self.hostname_lookup = is_true(prefs.get("ALLOWED_HOSTS_HOSTNAME_LOOKUP"))
        self.allowed_path = os.path.join(work_dir, ALLOWED_HOSTS)
        self.warned_path = os.path.join(work_dir, ALLOWED_WARNED_HOSTS)
        self.allowed_hosts = {}
        self.warned_hosts = {}
        self.new_warned_hosts = []
        self.load_hosts()
        self.load_warned_hosts()
        debug("done initializing AllowedHosts")

    def __contains__(self, ip_addr):
        if self.allowed_hosts.has_key(ip_addr): return 1
        else: return 0

    def dump(self):
        print "Dumping AllowedHosts"
        print self.allowed_hosts.keys()

        
    def load_hosts(self):
        try:
            fp = open(self.allowed_path, "r")
        except Exception, e:
            debug("Could not open %s - %s", self.allowed_path, str(e))
            return

        for line in fp:
            line = line.strip()
            if not line or line[0] == '#': continue
            
            m = ALLOWED_REGEX.match(line)
            debug("line: %s - regex match?   %s", line, m != None)
            if m:
                # line contains an ip address
                first3 = m.group('first_3bits')
                fourth = m.group('fourth')
                wildcard = m.group('ip_wildcard')
                ip_range = m.group('ip_range')

                if fourth:
                    self.allowed_hosts["%s%s" % (first3, fourth)] = 1
                    self.add_hostname("%s%s" % (first3, fourth))
                elif wildcard:
                    for i in range(256):
                        self.allowed_hosts["%s%s" % (first3, i)] = 1
                        self.add_hostname("%s%s" % (first3, i))
                else:
                    start, end = ip_range.split("-")
                    for i in range(int(start), int(end)):
                        self.allowed_hosts["%s%d" % (first3, i)] = 1
                        self.add_hostname("%s%s" % (first3, i))
            else:
                # assume that line contains hostname
                self.allowed_hosts[line] = 1
                try:
                    # lookup ip address of host
                    ip = gethostbyname(line)
                    self.allowed_hosts[ip] = 1
                except:
                    pass
            
        fp.close()
        debug("allowed_hosts: %s", self.allowed_hosts.keys())


    def add_hostname(self, ip_addr):
        if not self.hostname_lookup:
            return
        else:
            hostname = getfqdn(ip_addr)
            if hostname != ip_addr:
                self.allowed_hosts[hostname] = 1


    def add_warned_host(self, host):
        #debug("warned_hosts: %s", self.warned_hosts.keys())

        if host not in self.warned_hosts:
            debug("%s not in warned hosts" % host)
            self.new_warned_hosts.append(host)
            self.warned_hosts[host] = None

            
    def get_new_warned_hosts(self):
        return self.new_warned_hosts
    

    def load_warned_hosts(self):
        try:
            fp = open(self.warned_path, "r")
            for line in fp:
                self.warned_hosts[line.strip()] = None
            fp.close()
        except:
            pass


    def save_warned_hosts(self):
        if not self.new_warned_hosts: return
        try:
            fp = open(self.warned_path, "a")
            for host in self.new_warned_hosts:
                fp.write("%s\n" % host)
            fp.close()
        except Exception, e:
            print e


    def clear_warned_hosts(self):
        self.new_warned_hosts = []

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.