def index(request): "displays the status" addrs = [] act = 3 if not request.user.is_superuser: addrs = request.session["user_filter"]["addresses"] act = request.session["user_filter"]["account_type"] data = MessageStats.objects.get(request.user, addrs, act) val1, val2, val3 = os.getloadavg() load = "%.2f %.2f %.2f" % (val1, val2, val3) scanners = get_processes("MailScanner") mas = get_config_option("MTA") mta = get_processes(mas) clamd = get_processes("clamd") pipe1 = subprocess.Popen("uptime", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) upt = pipe1.stdout.read().split() uptime = upt[2] + " " + upt[3].rstrip(",") return render_to_response( "status/index.html", {"data": data, "load": load, "scanners": scanners, "mta": mta, "av": clamd, "uptime": uptime}, context_instance=RequestContext(request), )
def search_quarantine(date, message_id): """search_quarantine""" qdir = get_config_option('Quarantine Dir') date = "%s" % date date = date.replace('-', '') file_name = get_message_path(qdir, date, message_id) return file_name
def handle_noargs(self, **options): import os, shutil from django.conf import settings from baruwa.utils.misc import get_config_option from baruwa.messages.models import Message days_to_retain = getattr(settings, 'QUARANTINE_DAYS_TO_KEEP', 0) quarantine_dir = get_config_option('QuarantineDir') if (quarantine_dir.startswith('/etc') or quarantine_dir.startswith('/lib') or quarantine_dir.startswith('/home') or quarantine_dir.startswith('/bin') or quarantine_dir.startswith('..')): return False if (not os.path.exists(quarantine_dir)) or (days_to_retain == 0): return False ignore_dirs = ['spam', 'mcp', 'nonspam'] dirs = [ f for f in os.listdir(quarantine_dir) if os.path.isdir( os.path.join(quarantine_dir, f) ) and REGEX.match(f) and should_be_pruned(f, days_to_retain) ] dirs.sort() for direc in dirs: process_path = os.path.join(quarantine_dir, direc) print "== Processing directory "+process_path+" ==" ids = [f for f in os.listdir(process_path) if f not in ignore_dirs] if os.path.exists(os.path.join(process_path, 'spam')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'spam'))]) if os.path.exists(os.path.join(process_path, 'mcp')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'mcp'))]) if os.path.exists(os.path.join(process_path, 'nonspam')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'nonspam'))]) print ids Message.objects.filter(pk__in=ids).update(isquarantined=0) if (os.path.isabs(process_path) and (not os.path.islink(process_path))): print "== Removing directory "+process_path+" ==" try: shutil.rmtree(process_path) except: print "Failed to remove "+process_path else: print "The directory "+process_path+" is a sym link skipping"
def index(request): "displays the status" addrs = [] act = 3 inq = MailQueueItem.objects.filter(direction=1) outq = MailQueueItem.objects.filter(direction=2) if not request.user.is_superuser: addrs = request.session['user_filter']['addresses'] act = request.session['user_filter']['account_type'] if act == 2: query = Q() for addr in addrs: atdomain = "@%s" % addr query = query | Q( Q(**{'from_address__iendswith': atdomain}) | Q(**{'to_address__iendswith': atdomain})) inq = inq.filter(query) outq = outq.filter(query) if act == 3: inq = inq.filter( Q(from_address__in=addrs) | Q(to_address__in=addrs)) outq = outq.filter( Q(from_address__in=addrs) | Q(to_address__in=addrs)) data = MessageStats.objects.get(request.user, addrs, act) inq = inq.aggregate(count=Count('messageid')) outq = outq.aggregate(count=Count('messageid')) val1, val2, val3 = os.getloadavg() load = "%.2f %.2f %.2f" % (val1, val2, val3) scanners = get_processes('MailScanner') mas = get_config_option('MTA') mta = get_processes(mas) clamd = get_processes('clamd') pipe1 = subprocess.Popen(['uptime'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) upt = pipe1.communicate()[0].split() uptime = upt[2] + ' ' + upt[3].rstrip(',') return render_to_response('mail/status/index.html', { 'data': data, 'load': load, 'scanners': scanners, 'mta': mta, 'av': clamd, 'uptime': uptime, 'outq': outq['count'], 'inq': inq['count'] }, context_instance=RequestContext(request))
def handle_noargs(self, **options): from django.conf import settings from baruwa.utils.misc import get_config_option from baruwa.messages.models import Message days_to_retain = getattr(settings, 'QUARANTINE_DAYS_TO_KEEP', 0) quarantine_dir = get_config_option('QuarantineDir') if (quarantine_dir.startswith('/etc') or quarantine_dir.startswith('/lib') or quarantine_dir.startswith('/home') or quarantine_dir.startswith('/bin') or quarantine_dir.startswith('..')): return False if (not os.path.exists(quarantine_dir)) or (days_to_retain == 0): return False ignore_dirs = ['spam', 'mcp', 'nonspam'] dirs = [ f for f in os.listdir(quarantine_dir) if os.path.isdir( os.path.join(quarantine_dir, f) ) and REGEX.match(f) and should_be_pruned(f, days_to_retain) ] dirs.sort() for direc in dirs: process_path = os.path.join(quarantine_dir, direc) print _("== Processing directory %(path)s ==") % {'path': process_path} ids = [f for f in os.listdir(process_path) if f not in ignore_dirs] if os.path.exists(os.path.join(process_path, 'spam')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'spam'))]) if os.path.exists(os.path.join(process_path, 'mcp')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'mcp'))]) if os.path.exists(os.path.join(process_path, 'nonspam')): ids.extend( [f for f in os.listdir(os.path.join(process_path, 'nonspam'))]) print ids Message.objects.filter(pk__in=ids).update(isquarantined=0) if (os.path.isabs(process_path) and (not os.path.islink(process_path))): print _("== Removing directory %(path)s ==") % {'path': process_path} try: shutil.rmtree(process_path) except shutil.Error: print _("Failed to remove %(path)s") % {'path': process_path} else: print _("The directory %(path)s is a sym link skipping") % {'path': process_path}
def index(request): "displays the status" addrs = [] act = 3 inq = MailQueueItem.objects.filter(direction=1) outq = MailQueueItem.objects.filter(direction=2) if not request.user.is_superuser: addrs = request.session['user_filter']['addresses'] act = request.session['user_filter']['account_type'] if act == 2: query = Q() for addr in addrs: atdomain = "@%s" % addr query = query | Q(Q(**{'from_address__iendswith': atdomain}) | Q(**{'to_address__iendswith': atdomain})) inq = inq.filter(query) outq = outq.filter(query) if act == 3: inq = inq.filter(Q(from_address__in=addrs) | Q(to_address__in=addrs)) outq = outq.filter(Q(from_address__in=addrs) | Q(to_address__in=addrs)) data = MessageStats.objects.get(request.user, addrs, act) inq = inq.aggregate(count=Count('messageid')) outq = outq.aggregate(count=Count('messageid')) val1, val2, val3 = os.getloadavg() load = "%.2f %.2f %.2f" % (val1, val2, val3) scanners = get_processes('MailScanner') mas = get_config_option('MTA') mta = get_processes(mas) clamd = get_processes('clamd') pipe1 = subprocess.Popen( 'uptime', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) upt = pipe1.stdout.read().split() uptime = upt[2] + ' ' + upt[3].rstrip(',') return render_to_response('status/index.html', {'data': data, 'load': load, 'scanners': scanners, 'mta': mta, 'av': clamd, 'uptime': uptime, 'outq': outq['count'], 'inq': inq['count']}, context_instance=RequestContext(request))
def handle(self, *args, **options): if len(args) != 0: raise CommandError(_("Command doesn't accept any arguments")) mtas = ['exim', 'sendmail', 'postfix'] mta = options.get('mta') if not mta in mtas: raise CommandError(_("Only the following %(mta)s " "MTA's are supported") % {mta: ' '.join(mtas)}) def runquery(queue, direction, ids): "run querys" for item in queue: item['direction'] = direction if len(item['to_address']) == 1: if item['messageid'] in ids: mqitem = MailQueueItem.objects.get( messageid=item['messageid']) for key in ['attempts', 'lastattempt']: setattr(mqitem, key, item[key]) else: item['to_address'] = item['to_address'][0] mqitem = MailQueueItem(**item) mqitem.save() else: addrs = item['to_address'] for addr in addrs: item['to_address'] = addr if item['messageid'] in ids: mqitem = MailQueueItem.objects.filter( messageid=item['messageid'], to_address=item['to_address']).all()[0] for key in ['attempts', 'lastattempt']: setattr(mqitem, key, item[key]) else: mqitem = MailQueueItem(**item) mqitem.save() inqdir = get_config_option('IncomingQueueDir') outqdir = get_config_option('OutgoingQueueDir') inqueue = Mailq(mta, inqdir) print _("== Delete flaged queue items from the inbound queue ==") ditems = MailQueueItem.objects.values('messageid').filter( flag__gt=0, direction=1).all() inrm = inqueue.delete([item['messageid'] for item in ditems]) MailQueueItem.objects.filter(messageid__in=[ item['msgid'] for item in inrm]).delete() print _("== Deleted %(num)d items from the inbound queue ==") % { 'num': len(inrm)} inqueue.process() outqueue = Mailq(mta, outqdir) print _("== Delete flaged queue items from the outbound queue ==") ditems = MailQueueItem.objects.values('messageid').filter( flag__gt=0, direction=2).all() outrm = outqueue.delete([item['messageid'] for item in ditems]) MailQueueItem.objects.filter(messageid__in=[ item['msgid'] for item in outrm]).delete() print _("== Deleted %(num)d items from the outbound queue ==") % { 'num': len(outrm)} outqueue.process() allids = [item['messageid'] for item in inqueue] allids.extend([item['messageid'] for item in outqueue]) dbids = [item['messageid'] for item in MailQueueItem.objects.values('messageid').all()] remids = [item for item in dbids if not item in allids] preids = [item for item in dbids if not item in remids] if remids: print (_("== Deleting %(items)d queue items from DB ==") % {'items': len(remids)}) MailQueueItem.objects.filter(messageid__in=remids).delete() if inqueue: print (_("== Processing the inbound queue: %(queue)s with %(items)d items ==") % {'queue': inqdir, 'items': len(inqueue)}) runquery(inqueue, 1, preids) else: print _("== Skipping the inbound queue 0 items found ==") if outqueue: print (_("== Processing the outbound queue: %(queue)s with %(items)d items ==") % {'queue': outqdir, 'items': len(outqueue)}) runquery(outqueue, 2, preids) else: print _("== Skipping the outbound queue 0 items found ==")
def tds_action(value, from_address, to_address): "get actions" return_value = '' srules = [] if value == 1: option = 'Spam Actions' else: option = 'High Scoring Spam Actions' return_value = get_config_option(option) if re.match( r'^/.*[^/]$', return_value) or re.match(r'(^%[a-z-]+%)(.*)', return_value): match = re.match(r'(^%[a-z-]+%)(.*)', return_value) if match: the_dir = get_config_option(match.groups()[0]) return_value = the_dir + match.groups()[1] rules = read_rules(return_value) for rule in rules: sec = {} match = re.match(r'^(\S+)\s+(\S+)(\s+(.*))?$', rule) if match: (direction, rule, throwaway, value) = match.groups() throwaway = None match2 = re.match(r'^and\s+(\S+)\s+(\S+)(\s+(.+))?$', value) if match2: (direction2, rule2, throwaway, value2) = match2.groups() throwaway = None sec = {'direction': direction2, 'rule': clean_regex(rule2), 'action': value2} srules.append({'direction': direction, 'rule': clean_regex(rule), 'action': value, 'secondary': sec}) for item in srules: if item["secondary"]: to_regex = '' from_regex = '' if re.match( r'from\:|fromorto\:', item["direction"], re.IGNORECASE): from_regex = item['rule'] if re.match( r'from\:|fromorto\:', item["secondary"]["direction"], re.IGNORECASE): from_regex = item['secondary']['rule'] if re.match( r'to\:|fromorto\:', item["direction"], re.IGNORECASE): to_regex = item['rule'] if re.match( r'to\:|fromorto\:', item["secondary"]["direction"], re.IGNORECASE): to_regex = item['secondary']['rule'] if to_regex == '' or from_regex == '': return 'blank regex ' + to_regex + ' manoamano ' + from_regex if (re.match(to_regex, to_address, re.IGNORECASE) and re.match(from_regex, from_address, re.IGNORECASE)): return item["secondary"]["action"] else: comb_regex = '' to_regex = '' if re.match(r'to\:', item["direction"], re.IGNORECASE): to_regex = item['rule'] if re.match(to_regex, to_address, re.IGNORECASE): return item['action'] if re.match(r'from\:|fromorto\:', item["direction"], re.IGNORECASE): comb_regex = item['rule'] if (re.match(comb_regex, from_address, re.IGNORECASE) or re.match(comb_regex, to_address, re.IGNORECASE)): return item["action"] return _('I do not know how to read it') else: return return_value return return_value
def tds_action(value, from_address, to_address): "get actions" return_value = '' srules = [] if value == 1: option = 'Spam Actions' else: option = 'High Scoring Spam Actions' return_value = get_config_option(option) if re.match(r'^/.*[^/]$', return_value) or re.match( r'(^%[a-z-]+%)(.*)', return_value): match = re.match(r'(^%[a-z-]+%)(.*)', return_value) if match: the_dir = get_config_option(match.groups()[0]) return_value = the_dir + match.groups()[1] rules = read_rules(return_value) for rule in rules: sec = {} match = re.match(r'^(\S+)\s+(\S+)(\s+(.*))?$', rule) if match: (direction, rule, throwaway, value) = match.groups() throwaway = None match2 = re.match(r'^and\s+(\S+)\s+(\S+)(\s+(.+))?$', value) if match2: (direction2, rule2, throwaway, value2) = match2.groups() throwaway = None sec = { 'direction': direction2, 'rule': clean_regex(rule2), 'action': value2 } srules.append({ 'direction': direction, 'rule': clean_regex(rule), 'action': value, 'secondary': sec }) for item in srules: if item["secondary"]: to_regex = '' from_regex = '' if re.match(r'from\:|fromorto\:', item["direction"], re.IGNORECASE): from_regex = item['rule'] if re.match(r'from\:|fromorto\:', item["secondary"]["direction"], re.IGNORECASE): from_regex = item['secondary']['rule'] if re.match(r'to\:|fromorto\:', item["direction"], re.IGNORECASE): to_regex = item['rule'] if re.match(r'to\:|fromorto\:', item["secondary"]["direction"], re.IGNORECASE): to_regex = item['secondary']['rule'] if to_regex == '' or from_regex == '': return 'blank regex ' + to_regex + ' manoamano ' + from_regex if (re.match(to_regex, to_address, re.IGNORECASE) and re.match(from_regex, from_address, re.IGNORECASE)): return item["secondary"]["action"] else: comb_regex = '' to_regex = '' if re.match(r'to\:', item["direction"], re.IGNORECASE): to_regex = item['rule'] if re.match(to_regex, to_address, re.IGNORECASE): return item['action'] if re.match(r'from\:|fromorto\:', item["direction"], re.IGNORECASE): comb_regex = item['rule'] if (re.match(comb_regex, from_address, re.IGNORECASE) or re.match(comb_regex, to_address, re.IGNORECASE)): return item["action"] return _('I do not know how to read it') else: return return_value return return_value