Example #1
0
def is_local_domain(conn,
                    domain,
                    include_alias_domain=True,
                    include_backupmx=True):
    if not utils.is_domain(domain):
        return False

    if utils.is_server_hostname(domain):
        return True

    try:
        _filter = '(&(objectClass=mailDomain)(accountStatus=active)'

        if include_alias_domain:
            _filter += '(|(domainName=%s)(domainAliasName=%s))' % (domain, domain)
        else:
            _filter += '(domainName=%s)' % domain

        if not include_backupmx:
            _filter += '(!(domainBackupMX=yes))'

        _filter += ')'

        qr = conn.search_s(settings.ldap_basedn,
                           1,   # 1 == ldap.SCOPE_ONELEVEL
                           _filter,
                           ['dn'])
        if qr:
            return True
    except ldap.NO_SUCH_OBJECT:
        return False
    except Exception as e:
        logger.error("<!> Error while querying local domain: {0}".format(repr(e)))
        return False
Example #2
0
def is_local_domain(conn,
                    domain,
                    include_alias_domain=True,
                    include_backupmx=True):
    """Check whether given domain name is hosted on localhost and not disabled.

    @conn -- SQL connection cursor
    @domain -- a domain name
    @include_backupmx -- whether we should include backup mx domain names in
                         query result.
    """
    if not utils.is_domain(domain):
        return False

    if utils.is_server_hostname(domain):
        return True

    sql_quote_domain = sqlquote(domain)
    try:
        # include backup mx domains by default.
        sql_backupmx = ''
        if not include_backupmx:
            sql_backupmx = 'AND backupmx=0'

        sql = """SELECT domain
                   FROM domain
                  WHERE domain=%s AND active=1 %s
                  LIMIT 1""" % (sql_quote_domain, sql_backupmx)
        logger.debug("[SQL] query local domain ({}): \n{}".format(domain, sql))

        qr = conn.execute(sql)
        sql_record = qr.fetchone()
        logger.debug("SQL query result: {}".format(repr(sql_record)))

        if sql_record:
            return True
    except Exception as e:
        logger.error("<!> Error while querying domain: {}".format(repr(e)))

    # Query alias domain
    try:
        if include_alias_domain:
            sql = """SELECT alias_domain.alias_domain
                       FROM alias_domain, domain
                      WHERE domain.active=1
                            AND domain.domain=alias_domain.target_domain
                            AND alias_domain.alias_domain=%s
                      LIMIT 1""" % sql_quote_domain

            logger.debug("[SQL] query alias domain ({}): \n{}".format(
                domain, repr(sql)))

            qr = conn.execute(sql)
            sql_record = qr.fetchone()
            logger.debug("[SQL] query result: {}".format(repr(sql_record)))

            if sql_record:
                return True
    except Exception as e:
        logger.error("<!> Error while querying alias domain: {}".format(
            repr(e)))

    return False