Ejemplo n.º 1
0
remove_from_one_table(sql_table='mailaddr',
                      index_column='id',
                      removed_values=invalid_mailaddr_ids)

logger.info('')
logger.info('Remained records:')
logger.info('')
logger.info('      `msgs`: %-7.d' % len(msgs_mail_ids))
logger.info('`quarantine`: %-7.d' %
            (len(quar_mail_ids) - len(invalid_quar_mail_ids)))
logger.info('     `maddr`: %-7.d' % (len(maddr_ids) - len(invalid_maddr_ids)))
logger.info('  `mailaddr`: %-7.d' %
            (len(mailaddr_ids) - len(invalid_mailaddr_ids)))

if counter_msgs \
        or counter_msgrcpt \
        or invalid_quar_mail_ids \
        or invalid_maddr_ids \
        or invalid_mailaddr_ids:
    msg = 'Removed records: '
    msg += '%d in msgs, ' % counter_msgs
    msg += '%d in msgrcpt, ' % counter_msgrcpt
    msg += '%d in quarantine, ' % len(invalid_quar_mail_ids)
    msg += '%d in maddr, ' % len(invalid_maddr_ids)
    msg += '%d in mailaddr.' % len(invalid_mailaddr_ids)

    ira_tool_lib.log_to_iredadmin(msg,
                                  admin='cleanup_amavisd_db',
                                  event='cleanup_db')
    logger.info('Log cleanup status.')
Ejemplo n.º 2
0
def delete_mailbox(conn, record):
    rid = record.id
    username = str(record.username)
    maildir = record.maildir
    timestamp = str(record.timestamp)
    delete_date = record.delete_date

    if not delete_without_timestamp:
        _dir = maildir.rstrip('/')

        if len(_dir) <= 21:
            # Why 21 chars:
            #   - 20 chars: "-<timestamp>". e.g. "-2014.03.26.15.07.25"
            #   - username contains at least 1 char
            logger.error(
                "<<< ERROR >>> Seems no timestamp in maildir path (%s), too risky to remove this mailbox. skip."
                % maildir)
            return False

        try:
            # Extract timestamp string, make sure it's a valid time format.
            ts = _dir[-20:-1]
            time.strptime(ts, '%Y.%m.%d.%H.%M.%S')
        except:
            logger.error(
                "<<< ERROR >>> No valid timestamp in maildir path (%s), too risky to remove this mailbox. skip."
                % maildir)
            return False

    # check directory
    if os.path.isdir(maildir):
        # Make sure directory is owned by vmail:vmail
        _dir_stat = os.stat(maildir)
        _dir_uid = _dir_stat.st_uid

        # Get uid/gid of vmail user
        owner = pwd.getpwuid(_dir_uid).pw_name
        if owner != 'vmail':
            logger.error(
                '<<< ERROR >> Directory is not owned by `vmail` user: uid -> %d, user -> %s.'
                % (_dir_uid, owner))
            return False

        try:
            msg = 'Deleted mailbox (%s): %s.' % (username, maildir)
            msg += ' Account was deleted at %s.' % (timestamp)
            if delete_date:
                msg += ' Mailbox was scheduled to be removed on %s.' % (
                    delete_date)
            else:
                msg += ' Mailbox was scheduled to be removed as soon as possible.'

            logger.info(msg)

            # Delete mailbox
            shutil.rmtree(maildir)

            # Log this deletion.
            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)))
Ejemplo n.º 3
0
def delete_mailbox(conn_deleted_mailboxes,
                   conn_vmail,
                   record,
                   all_maildirs=None):
    rid = record.id
    username = str(record.username).lower()
    timestamp = str(record.timestamp)
    delete_date = record.delete_date

    maildir = record.maildir
    maildir = maildir.replace('//', '/')  # Remove duplicate '/'

    if delete_without_timestamp:
        # Make sure no other mailbox is stored under the maildir.
        if all_maildirs:
            if not maildir.endswith('/'):
                maildir += '/'

            print("Removing: %s" % maildir)
            for mdir in all_maildirs:
                print("- startswith:", mdir.startswith(maildir))
                print("- equal:", mdir == maildir)
                if mdir.startswith(maildir) or (mdir == maildir):
                    logger.error(
                        "<<< ABORT, CRITICAL >>> Trying to remove mailbox (%s) owned by user (%s), but there is another mailbox (%s) stored under this directory. Aborted."
                        % (maildir, username, mdir))
                    return False
    else:
        _dir = maildir.rstrip('/')

        if len(_dir) <= 21:
            # Why 21 chars:
            #   - 20 chars: "-<timestamp>". e.g. "-2014.03.26.15.07.25"
            #   - username contains at least 1 char
            logger.error(
                "<<< SKIP >>> Seems no timestamp in maildir path (%s), too risky to remove this mailbox."
                % maildir)
            return False

        try:
            # Extract timestamp string, make sure it's a valid time format.
            ts = _dir[-19:]
            time.strptime(ts, '%Y.%m.%d.%H.%M.%S')
        except Exception as e:
            logger.debug(
                "<<< WARNING >>> Invalid or missing timestamp in maildir path (%s), skip."
                % maildir)
            logger.debug("<<< WARNING >>> Error message: %s." % repr(e))
            return False

    # check maildir path
    if os.path.isdir(maildir):
        # Make sure directory is owned by vmail:vmail
        _dir_stat = os.stat(maildir)
        _dir_uid = _dir_stat.st_uid

        # Get uid/gid of vmail user
        owner = pwd.getpwuid(_dir_uid).pw_name
        if owner != 'vmail':
            logger.error(
                '<<< ERROR >> Directory is not owned by `vmail` user: uid -> %d, user -> %s.'
                % (_dir_uid, owner))
            return False

        try:
            msg = '[%s] %s.' % (username, maildir)
            msg += ' Account was deleted at %s.' % (timestamp)
            if delete_date:
                msg += ' Mailbox was scheduled to be removed on %s.' % (
                    delete_date)
            else:
                msg += ' Mailbox was scheduled to be removed as soon as possible.'

            logger.info(msg)

            # Delete mailbox
            shutil.rmtree(maildir)

            # Log this deletion.
            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)
Ejemplo n.º 4
0
    """
    DELETE FROM msgrcpt
    WHERE NOT EXISTS (SELECT 1 FROM msgs WHERE mail_id=msgrcpt.mail_id)
    """
)

logger.info("Delete unreferenced records from table `quarantine`.")
conn.query(
    """
    DELETE FROM quarantine
    WHERE NOT EXISTS (SELECT 1 FROM msgs WHERE mail_id=quarantine.mail_id)
    """
)

logger.info("Delete unreferenced records from table `maddr`.")
conn.query(
    """
    DELETE FROM maddr
    WHERE NOT EXISTS (SELECT 1 FROM msgs WHERE sid=id)
        AND NOT EXISTS (SELECT 1 FROM msgrcpt WHERE rid=id)
    """
)

logger.info("Delete unreferenced records from table `mailaddr`.")
conn.query("""DELETE FROM mailaddr WHERE NOT EXISTS (SELECT 1 FROM wblist WHERE sid=id)""")

if counter_msgs or counter_msgrcpt:
    msg = "Cleanup Amavisd database: delete %d records in msgs, %d in msgrcpt." % (counter_msgs, counter_msgrcpt)
    ira_tool_lib.log_to_iredadmin(msg, admin="cleanup_amavisd_db", event="cleanup_db")
    logger.info("Log cleanup status.")
Ejemplo n.º 5
0
            msg = 'Deleted mailbox (%s): %s.' % (username, maildir)
            msg += ' Account was deleted at %s.' % (timestamp)
            if delete_date:
                msg += ' Mailbox was scheduled to be removed on %s.' % (
                    delete_date)
            else:
                msg += ' Mailbox was scheduled to be removed as soon as possible.'

            logger.info(msg)

            # Delete mailbox
            shutil.rmtree(maildir)

            # Log this deletion.
            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')