Exemple #1
0
def create_zone(server, zone, username, password):
    parser = OptionParser()
    sambaopts = SambaOptions(parser)
    credopts = CredentialsOptions(parser)
    credopts.creds.parse_string(username)
    credopts.creds.set_password(password)
    credopts.ask_for_password = False
    credopts.machine_pass = False
    lp = sambaopts.get_loadparm()
    lp.set('realm', __domain_name(server))
    lp.set('debug level', '0')
    output = StringIO()
    cmd = dns.cmd_zonecreate()
    cmd.outf = output
    try:
        cmd.run(server, zone, 'longhorn', sambaopts, credopts)
    except CommandError as e:
        return str(e)
    except (NTSTATUSError, WERRORError) as e:
        return e.args[1]
    return output.getvalue().strip()
Exemple #2
0
def join(domain, role, dns_backend, username, password):
    '''Join domain as either member or backup domain controller
    param string domain         NetBIOS domain name to use
    param string role           possible values: MEMBER, DC, RODC, SUBDOMAIN
    param string dns_backend    The DNS server backend
    param string username       Username
    param string password       Password
    return string               Error message, or an empty string
    '''
    parser = OptionParser()
    sambaopts = SambaOptions(parser)
    credopts = CredentialsOptions(parser)
    credopts.creds.parse_string(username)
    credopts.creds.set_password(password)
    credopts.ask_for_password = False
    credopts.machine_pass = False

    outlog = YaSTIO(ycpbuiltins.y2debug)
    errlog = YaSTIO(ycpbuiltins.y2error)

    join = dm.cmd_domain_join(errf=errlog)
    join.logger = get_samba_logger(name="provision",
                                   stream=outlog,
                                   verbose=True,
                                   quiet=False,
                                   fmt="%(message)s")

    try:
        join.run(sambaopts=sambaopts,
                 credopts=credopts,
                 domain=domain,
                 role=role,
                 dns_backend=dns_backend)
    except Exception as e:
        template = "{0} exception:\n{1}"
        message = template.format(type(e).__name__, str(e))
        return [False, message]
    return [True, outlog.getvalue()]
Exemple #3
0
def zonelist(server, username, password):
    parser = OptionParser()
    sambaopts = SambaOptions(parser)
    credopts = CredentialsOptions(parser)
    credopts.creds.parse_string(username)
    credopts.creds.set_password(password)
    credopts.ask_for_password = False
    credopts.machine_pass = False
    lp = sambaopts.get_loadparm()
    lp.set('realm', __domain_name(server))
    lp.set('debug level', '0')
    output = StringIO()
    cmd = dns.cmd_zonelist()
    cmd.outf = output
    try:
        cmd.run(server, 'longhorn', sambaopts=sambaopts, credopts=credopts)
    except (CommandError, NTSTATUSError, WERRORError):
        return {}
    res = {}
    for zone in output.getvalue().split('\n\n'):
        if re.match('\d+ zone\(s\) found', zone.strip()):
            continue
        zone_map = {}
        zone_name = None
        for line in zone.split('\n'):
            m = re.match('([^:]+):(.*)', line)
            if m:
                key = m.group(1).strip()
                val = m.group(2).strip()
                if key == 'pszZoneName':
                    zone_name = val
                elif key in ['Flags', 'dwDpFlags']:
                    zone_map[key] = val.split()
                else:
                    zone_map[key] = val
        if zone_name:
            res[zone_name] = zone_map
    return res
Exemple #4
0
def query(server, zone, name, rtype, username, password):
    global results
    parser = OptionParser()
    sambaopts = SambaOptions(parser)
    credopts = CredentialsOptions(parser)
    credopts.creds.parse_string(username)
    credopts.creds.set_password(password)
    credopts.ask_for_password = False
    credopts.machine_pass = False
    lp = sambaopts.get_loadparm()
    lp.set('realm', __domain_name(server))
    lp.set('debug level', '0')
    cmd = dns.cmd_query()

    def fetch_dnsrecords(_, records):
        global results
        results = records

    dns.print_dnsrecords = fetch_dnsrecords
    try:
        cmd.run(server,
                zone,
                name,
                rtype,
                sambaopts=sambaopts,
                credopts=credopts)
    except (CommandError, NTSTATUSError, WERRORError):
        return {}
    records = {}
    for rec in results.rec:
        records[rec.dnsNodeName.str] = {}
        records[rec.dnsNodeName.str]['records'] = []
        records[rec.dnsNodeName.str]['dwChildCount'] = rec.dwChildCount
        for dns_rec in rec.records:
            record = {}
            if dns_rec.wType in [dnsp.DNS_TYPE_A, dnsp.DNS_TYPE_AAAA]:
                record = {'data': dns_rec.data}
            elif dns_rec.wType in [
                    dnsp.DNS_TYPE_PTR, dnsp.DNS_TYPE_NS, dnsp.DNS_TYPE_CNAME
            ]:
                record = {'data': dns_rec.data.str}
            elif dns_rec.wType == dnsp.DNS_TYPE_SOA:
                record = {
                    'serial': dns_rec.data.dwSerialNo,
                    'refresh': dns_rec.data.dwRefresh,
                    'retry': dns_rec.data.dwRetry,
                    'expire': dns_rec.data.dwExpire,
                    'minttl': dns_rec.data.dwMinimumTtl,
                    'ns': dns_rec.data.NamePrimaryServer.str,
                    'email': dns_rec.data.ZoneAdministratorEmail.str
                }
            elif dns_rec.wType == dnsp.DNS_TYPE_MX:
                record = {
                    'nameExchange': dns_rec.data.nameExchange.str,
                    'preference': dns_rec.data.wPreference
                }
            elif dns_rec.wType == dnsp.DNS_TYPE_SRV:
                record = {
                    'nameTarget': dns_rec.data.nameTarget.str,
                    'port': dns_rec.data.wPort,
                    'priority': dns_rec.data.wPriority,
                    'weight': dns_rec.data.wWeight
                }
            elif dns_rec.wType == dnsp.DNS_TYPE_TXT:
                record = {'data': [name.str for name in dns_rec.data.str]}
            record.update({
                'type': dns_rec.wType,
                'flags': dns_rec.dwFlags,
                'serial': dns_rec.dwSerial,
                'ttl': dns_rec.dwTtlSeconds
            })
            records[rec.dnsNodeName.str]['records'].append(record)
    for dnsNodeName in records.keys():
        if records[dnsNodeName]['dwChildCount'] > 0:
            records[dnsNodeName]['children'] = query(
                server, zone,
                '%s.%s' % (dnsNodeName, name if name != '@' else '%s.' % zone),
                rtype, username, password)
    return records