def create_temp(self, data): purged_hosts = [] banned = self.purge_counter.get_banned_for_life() try: fp = open(self.temp_file, "w") os.chmod(self.temp_file, 0644) offset = 0 num_lines = len(data) while offset < num_lines: line = data[offset] offset += 1 if not line.startswith(DENY_DELIMITER): fp.write(line) continue else: if offset == num_lines: warn("DenyHosts comment line at end of file") fp.write(line) continue timestamp = None try: rest = line.lstrip(DENY_DELIMITER) timestamp, host_verify = rest.split(ENTRY_DELIMITER) tm = time.strptime(timestamp) except Exception, e: warn("Parse error -- Ignorning timestamp: %s for: %s", timestamp, line) warn("exception: %s", str(e)) # ignoring bad time string fp.write(line) continue epoch = long(time.mktime(tm)) #print entry, epoch, self.cutoff if self.cutoff > epoch: # this entry should be purged entry = data[offset] if host_verify != entry: warn("%s purge verification failed: %s vs. %s", self.deny_file, host_verify.rstrip(), entry.rstrip()) fp.write(line) continue host = parse_host(entry) if host and host not in banned: # purge purged_hosts.append(host) # increment offset past purged line offset += 1 continue else: fp.write(line) continue fp.close()
def get_denied_hosts(self): self.__denied_hosts = {} for line in open(self.__prefs.get('HOSTS_DENY'), "r"): if line[0] not in ('#', '\n'): idx = line.find('#') if idx != 1: line = line[:idx] try: host = parse_host(line) self.__denied_hosts[host] = 0 if host in self.__allowed_hosts: self.__allowed_hosts.add_warned_host(host) except Exception: pass new_warned_hosts = self.__allowed_hosts.get_new_warned_hosts() if new_warned_hosts: self.__allowed_hosts.save_warned_hosts() text = """WARNING: The following hosts appear in %s but should be allowed based on your %s file""" % (self.__prefs.get("HOSTS_DENY"), os.path.join(self.__prefs.get("WORK_DIR"), ALLOWED_HOSTS)) self.__report.add_section(text, new_warned_hosts) self.__allowed_hosts.clear_warned_hosts()