Example #1
0
                           close_connection=False)
    if qr[0]:
        ldap_conn = qr[1]

    # Query mail domains
    qr = ldap_conn.search_s(settings.ldap_basedn, ldap.SCOPE_SUBTREE,
                            "(objectClass=mailDomain)",
                            ['domainName', 'domainAliasName'])

    for r in qr:
        entry = r[1]
        all_domains += entry.get('domainName', [])
        all_domains += entry.get('domainAliasName', [])

else:
    conn = ira_tool_lib.get_db_conn('vmail')

    # Get all mail domains
    qr = conn.select('domain', what='domain')
    for r in qr:
        all_domains.append(str(r.domain).lower())

    # Get all alias domains
    qr = conn.select('alias_domain', what='alias_domain')
    for r in qr:
        all_domains.append(str(r.alias_domain).lower())

logger.info('Found %d domain(s).' % len(all_domains))

# Add all mail domains as Cluebringer internal domains.
conn = ira_tool_lib.get_db_conn('policyd')
Example #2
0
if not (settings.amavisd_enable_logging or settings.amavisd_enable_quarantine):
    sys.exit("Amavisd is not enabled. SKIP.")

backend = settings.backend
logger.info('Backend: %s' % backend)
logger.info('SQL server: %s:%d' %
            (settings.amavisd_db_host, int(settings.amavisd_db_port)))

db_settings = iredutils.get_settings_from_db(params=[
    'amavisd_remove_quarantined_in_days', 'amavisd_remove_maillog_in_days'
])
keep_quar_days = db_settings['amavisd_remove_quarantined_in_days']
keep_inout_days = db_settings['amavisd_remove_maillog_in_days']
query_size_limit = settings.AMAVISD_CLEANUP_QUERY_SIZE_LIMIT

conn_amavisd = ira_tool_lib.get_db_conn('amavisd')

if settings.backend in ['mysql', 'ldap']:
    # Querying (SELECT) without locking. Require MySQL 5.0+ and InnoDB.
    #
    # Since we're dealing with sql records created days ago, no new records
    # will be inserted with that date, it's safe to use dirty read.
    logger.info('Enable dirty read for querying without locking SQL tables.')
    try:
        conn_amavisd.query('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED')
    except Exception as e:
        logger.error('Cannot enable dirty read: %s' % repr(e))


# Removing records from single table.
def remove_from_one_table(sql_table, index_column, removed_values):
            users += [(_email, _pw)]
        else:
            print '[SKIP] line %d: no valid email address: %s' % (line_num,
                                                                  _line)
    f.close()
else:
    usage()

total = len(users)
logger.info('%d users in total.' % total)

count = 1
if backend == 'ldap':
    import ldap
    from libs.ldaplib.ldaputils import convert_keyword_to_dn
    conn = get_db_conn('ldap')

    for (_email, _pw) in users:
        logger.info('(%d/%d) Updating %s' % (count, total, _email))

        dn = convert_keyword_to_dn(_email, accountType='user')
        pw_hash = generate_password_hash(_pw)
        mod_attrs = [(ldap.MOD_REPLACE, 'userPassword', [pw_hash])]
        try:
            conn.modify_s(dn, mod_attrs)
        except Exception, e:
            print '<<< ERROR >>>', e
elif backend in ['mysql', 'pgsql']:
    conn = get_db_conn('vmail')
    for (_email, _pw) in users:
        logger.info('(%d/%d) Updating %s' % (count, total, _email))
Example #4
0
else:
    usage()

total = len(users)
logger.info('{} users in total.'.format(total))

count = 1
if backend == 'ldap':
    from libs.ldaplib.core import LDAPWrap
    from libs.ldaplib.ldaputils import rdn_value_to_user_dn, mod_replace
    _wrap = LDAPWrap()
    conn = _wrap.conn

    for (_email, _quota) in users:
        logger.info('(%d/%d) Updating %s -> %s' %
                    (count, total, _email, _quota))
        dn = rdn_value_to_user_dn(_email)
        mod_attrs = mod_replace('mailQuota', _quota)
        try:
            conn.modify_s(dn, mod_attrs)
        except Exception as e:
            print("<<< ERROR >>> {}".format(e))
elif backend in ['mysql', 'pgsql']:
    conn = get_db_conn('vmail')
    for (_email, _quota) in users:
        logger.info('(%d/%d) Updating %s -> %s' %
                    (count, total, _email, _quota))
        conn.update('mailbox',
                    quota=int(_quota),
                    where="username='******'" % _email)
            ira_tool_lib.log_to_iredadmin(msg,
                                          admin='cron_delete_mailboxes',
                                          username=username,
                                          event='delete_mailboxes')
        except Exception, e:
            logger.error('<<< ERROR >> while deleting mailbox (%s -> %s): %s' %
                         (username, maildir, repr(e)))

    # Delete record.
    delete_record(conn=conn, rid=rid)


# Establish SQL connection.
try:
    if settings.backend == 'ldap':
        conn = ira_tool_lib.get_db_conn('iredadmin')
    else:
        conn = ira_tool_lib.get_db_conn('vmail')
except Exception, e:
    sys.exit(
        '<<< ERROR >>> Cannot connect to SQL database, aborted. Error: %s' %
        repr(e))

# Get pathes of all maildirs.
sql_where = 'delete_date <= %s' % web.sqlquote(web.sqlliteral('NOW()'))
if delete_null_date:
    sql_where = '(delete_date <= %s) OR (delete_date IS NULL)' % web.sqlquote(
        web.sqlliteral('NOW()'))

qr = conn.select('deleted_mailboxes', where=sql_where)
Example #6
0
rootdir = os.path.abspath(os.path.dirname(__file__)) + '/../'
sys.path.insert(0, rootdir)

import settings
from tools.ira_tool_lib import debug, logger, sql_dbn, get_db_conn, sql_count_id

web.config.debug = debug

backend = settings.backend
logger.info('Backend: %s' % backend)
logger.info('SQL server: %s:%d' %
            (settings.iredadmin_db_host, int(settings.iredadmin_db_port)))

query_size_limit = 100

conn_iredadmin = get_db_conn('iredadmin')

#
# iredadmin.log
#
_days = settings.IREDADMIN_LOG_KEPT_DAYS
logger.info('Delete old admin activity log (> %d days)' % _days)

if sql_dbn == 'mysql':
    sql_where = """timestamp < DATE_SUB(NOW(), INTERVAL %d DAY)""" % _days
elif sql_dbn == 'postgres':
    sql_where = """timestamp < CURRENT_TIMESTAMP - INTERVAL '%d DAYS'""" % _days
else:
    logger.error('Invalid SQL backend: %s' % sql_dbn)
    sys.exit()
Example #7
0
output_dir = sys.argv[1]
if not os.path.isdir(output_dir):
    sys.exit("Output directory doesn't exist: %s" % output_dir)

os.environ['LC_ALL'] = 'C'

rootdir = os.path.abspath(os.path.dirname(__file__)) + '/../'
sys.path.insert(0, rootdir)

from tools.ira_tool_lib import debug, get_db_conn

web.config.debug = debug

now = int(time.time())
conn_amavisd = get_db_conn('amavisd')
conn_iredadmin = get_db_conn('iredadmin')

# Get last time
last_time = 0
try:
    qr = conn_iredadmin.select('tracking',
                               what='v',
                               where="k='dump_quarantined_mail'",
                               limit=1)
    if qr:
        last_time = int(qr[0].v)
except:
    pass

# Get value of all `quarantine.mail_id`.
Example #8
0
rootdir = os.path.abspath(os.path.dirname(__file__)) + "/../"
sys.path.insert(0, rootdir)

import settings
from tools import ira_tool_lib

web.config.debug = ira_tool_lib.debug
logger = ira_tool_lib.logger

backend = settings.backend
logger.info("Backend: %s" % backend)

query_size_limit = 100

conn = ira_tool_lib.get_db_conn("amavisd")

# Delete old quarantined mails from table 'msgs'. It will also
# delete records in table 'quarantine'.
logger.info("Delete quarantined mails which older than %d days" % settings.AMAVISD_REMOVE_QUARANTINED_IN_DAYS)
counter_msgs = 0
while True:
    if ira_tool_lib.sql_dbn == "mysql":
        sql_where = (
            """quar_type = 'Q' AND time_num < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %d DAY))"""
            % settings.AMAVISD_REMOVE_QUARANTINED_IN_DAYS
        )
    elif ira_tool_lib.sql_dbn == "postgres":
        sql_where = (
            """quar_type = 'Q' AND time_iso < CURRENT_TIMESTAMP - INTERVAL '%d DAYS'"""
            % settings.AMAVISD_REMOVE_QUARANTINED_IN_DAYS
Example #9
0
            ira_tool_lib.log_to_iredadmin(msg,
                                          admin='cron_delete_mailboxes',
                                          username=username,
                                          event='delete_mailboxes')
        except Exception as e:
            logger.error('<<< ERROR >> while deleting mailbox (%s -> %s): %s' %
                         (username, maildir, repr(e)))

    # Delete record.
    delete_record(conn_deleted_mailboxes=conn_deleted_mailboxes, rid=rid)


# Establish SQL connection.
try:
    if settings.backend == 'ldap':
        conn_deleted_mailboxes = ira_tool_lib.get_db_conn('iredadmin')

        from libs.ldaplib.core import LDAPWrap
        _wrap = LDAPWrap()
        conn_vmail = _wrap.conn
    else:
        conn_deleted_mailboxes = ira_tool_lib.get_db_conn('vmail')
        conn_vmail = conn_deleted_mailboxes
except Exception as e:
    sys.exit(
        '<<< ERROR >>> Cannot connect to SQL database, aborted. Error: %s' %
        repr(e))

# Get pathes of all maildirs.
sql_where = 'delete_date <= %s' % web.sqlquote(web.sqlliteral('NOW()'))
if delete_null_date:
Example #10
0
# Author: Zhang Huangbin <*****@*****.**>
# Purpose: Delete all records in SQL table "iredadmin.sessions" to force
#          all admins to re-login.

import os
import sys
import web

os.environ["LC_ALL"] = "C"

rootdir = os.path.abspath(os.path.dirname(__file__)) + "/../"
sys.path.insert(0, rootdir)

from tools import ira_tool_lib

web.config.debug = ira_tool_lib.debug
logger = ira_tool_lib.logger

conn = ira_tool_lib.get_db_conn("iredadmin")

logger.info("Delete all existing sessions, admins are forced to re-login to iRedAdmin.")
conn.query("DELETE FROM sessions")
rootdir = os.path.abspath(os.path.dirname(__file__)) + '/../'
sys.path.insert(0, rootdir)

import settings
from libs.amavisd import is_valid_amavisd_address, wblist
from tools import ira_tool_lib

web.config.debug = ira_tool_lib.debug
logger = ira_tool_lib.logger

# Check database name to make sure it's Cluebringer
if settings.policyd_db_name != 'cluebringer':
    sys.exit('Error: not a Cluebringer database.')

logger.info('Establish SQL connection.')
conn = ira_tool_lib.get_db_conn('policyd')

logger.info('Query white/blacklist info.')

# Converted wblist
wl = []
bl = []

# value of sql column: policy_groups.id
wl_id = None
bl_id = None
wb_ids = []

# query whitelist and/or blacklist. possible values: 'wl', 'bl'.
query_lists = []
    qr = verify_bind_dn_pw(dn=settings.ldap_bind_dn, password=settings.ldap_bind_password, close_connection=False)
    if qr[0]:
        ldap_conn = qr[1]

    # Query mail domains
    qr = ldap_conn.search_s(
        settings.ldap_basedn, ldap.SCOPE_SUBTREE, "(objectClass=mailDomain)", ["domainName", "domainAliasName"]
    )

    for r in qr:
        entry = r[1]
        all_domains += entry.get("domainName", [])
        all_domains += entry.get("domainAliasName", [])

else:
    conn = ira_tool_lib.get_db_conn("vmail")

    # Get all mail domains
    qr = conn.select("domain", what="domain")
    for r in qr:
        all_domains.append(str(r.domain).lower())

    # Get all alias domains
    qr = conn.select("alias_domain", what="alias_domain")
    for r in qr:
        all_domains.append(str(r.alias_domain).lower())

logger.info("Found %d domain(s)." % len(all_domains))

# Add all mail domains as Cluebringer internal domains.
conn = ira_tool_lib.get_db_conn("policyd")