def dump(self):
     # This function uses LDIFWriter to properly format an LDIF file.
     fd = LDIFutils.LDIFWriter('SAMSON3', cereconf.LDAP_SAMSON3['file'])
     fd.write_container()
     for e in self.entries:
         fd.write(LDIFutils.entry_string(e['dn'], e['entry'], False))
     fd.close()
Beispiel #2
0
def write_mail_domains():
    """ Gather data and dump to ldif. """
    logger = Factory.get_logger("cronjob")
    logger.debug("Reading domains...")
    domains = sorted(get_email_domains())

    lw = LDIFutils.LDIFWriter("MAIL_DOMAINS")
    dn_suffix = lw.getconf("dn")
    lw.write_container()

    logger.debug("Writing domains...")
    for domain in domains:
        dn = "cn=%s,%s" % (domain, dn_suffix)
        entry = {
            "cn": domain,
            "host": domain,
            "objectClass": ("uioHost", ),
        }
        lw.write_entry(dn, entry)
    logger.debug("Done.")
    lw.close()
def write_mail_dns():
    """ Gather data and dump to ldif. """
    logger = Factory.get_logger('cronjob')

    hosts, cnames, lower2host, hosts_only_mx = get_hosts_and_cnames()

    # email domains (lowercase -> domain), in alphabetical order
    domains = OrderedDict((d.lower(), d) for d in sorted(get_email_domains()))

    domain_wo_mx = set()
    for domain in domains:
        # Verify that domains have an MX-record.
        for arg in cereconf.LDAP_MAIL_DNS['dig_args']:
            zone = arg[0]
            if domain.endswith(zone) and not (domain in hosts_only_mx
                                              or domain in hosts):
                logger.error("email domain without MX defined: %s" % domain)
                domain_wo_mx.add(domain.lower())
        # Valid email domains only requires MX
        if domain in hosts_only_mx:
            hosts_only_mx.remove(domain)

    for host in hosts_only_mx:
        logger.warn(
            "MX defined but no A/AAAA record or valid email domain: %s" % host)

    if domain_wo_mx:
        cause = "{0:d} email domains without mx".format(len(domain_wo_mx))
        logger.error("{0}, this must be rectified manually!".format(cause))
        raise CerebrumError(cause)

    def handle_domain_host(entry, host):
        entry["host"] = (lower2host[host], )
        for cname in hosts[host]:
            if cname not in domains:
                entry["cn"].add(lower2host[cname])
                del cnames[cname]
        del hosts[host]

    lw = LDIFutils.LDIFWriter('MAIL_DNS', filename=None)
    dn_suffix = lw.getconf('dn')
    lw.write_container()

    for domain, output in domains.items():
        dn = "cn=%s,%s" % (output, dn_suffix)
        entry = {"cn": set((output, )), "objectClass": ("uioHost", )}
        try:
            if domain in cnames:
                # This fails `if domain not in hosts`
                entry["cn"].add(lower2host[cnames[domain]])
                handle_domain_host(entry, cnames[domain])
            elif domain in hosts:
                handle_domain_host(entry, domain)
        except Exception:
            logger.error(
                "domain=%r, cnames[domain]=%r, "
                "in hosts=%r, in cnames=%r", domain, cnames.get(domain), domain
                in hosts, domain in cnames)
            raise
        lw.write_entry(dn, entry)

    for host in sorted(hosts.keys()):
        l2h = lower2host[host]
        names = set(lower2host[cname] for cname in hosts[host])
        names.add(l2h)
        lw.write_entry("host=%s,%s" % (l2h, dn_suffix), {
            "host": (l2h, ),
            "cn": names,
            "objectClass": ("uioHost", )
        })

    lw.close()