コード例 #1
0
def getRecordFromLine(line, fl_class):
    if fl_class == 'NS':
        return records.NS({'string': line})
    elif fl_class == 'A':
        return records.A({'string': line})
    elif fl_class == 'PTR':
        return records.PTR({'string': line})
コード例 #2
0
def fromJSON(reqJSON, log):
    """
    Convert JSON dict to dict of records objects.

    @type   reqJSON:    JSON dict (must contain key of 'ns_records', 'a_records', or 'ptr_records')
    @param  reqJSON:    JSON to be converted
    @type   log:        logger
    @param  log:        Logger object for logging

    @return Dict containing retrieved records of style: {
        'ns_records': SortedSet([records.NS]),
        'a_records': SortedSet([records.A]),
        'ptr_records': SortedSet([records.PTR])
    }
    """
    ns_records = SortedSet([])
    a_records = SortedSet([])
    ptr_records = SortedSet([])

    if 'ns_records' in reqJSON:
        log.debug(reqJSON['ns_records'])
        for ns in reqJSON['ns_records']:
            ns_records.add(
                records.NS({
                    'name': ns['name'],
                    'ip': ns['ip'],
                    'disabled': ns['disabled']
                }))
    if 'a_records' in reqJSON:
        log.debug(reqJSON['a_records'])
        for a in reqJSON['a_records']:
            a_records.add(
                records.A({
                    'addr': a['addr'],
                    'ip': a['ip'],
                    'disabled': a['disabled']
                }))
    if 'ptr_records' in reqJSON:
        log.debug(reqJSON['ptr_records'])
        for p in reqJSON['ptr_records']:
            ptr_records.add(
                records.PTR({
                    'ip': p['ip'],
                    'addr': p['addr'],
                    'disabled': p['disabled']
                }))

    return {
        'ns_records': ns_records,
        'a_records': a_records,
        'ptr_records': ptr_records
    }
コード例 #3
0
def fromJSONset(reqJSON, recordType, log):
    """
    Converts JSON set to set of records objects.

    @type   reqJSON:    JSON set
    @param  reqJSON:    JSON to be converted
    @type   recordType: string
    @param  recordType: Type of records to convert (must be "NS", "A", or "PTR")
    @type   log:        logger
    @param  log:        Logger object for logging

    @return Dict containing retrieved records of style: {'ns_records': SortedSet([records.NS]), 'a_records': SortedSet([records.A]), 'ptr_records': SortedSet([records.PTR])}
    """
    ret = SortedSet([])
    for rec in reqJSON:
        if recordType == "NS":
            ret.add(
                records.NS({
                    'name': rec['name'],
                    'ip': rec['ip'],
                    'disabled': rec['disabled']
                }))
        elif recordType == "A":
            ret.add(
                records.A({
                    'addr': rec['addr'],
                    'ip': rec['ip'],
                    'disabled': rec['disabled']
                }))
        elif recordType == "PTR":
            ret.add(
                records.PTR({
                    'ip': rec['ip'],
                    'addr': rec['addr'],
                    'disabled': rec['disabled']
                }))
    return ret
コード例 #4
0
def dbToFile(params, ns_file, a_file, ptr_file, log):
    """
    Read DB and dumps directly to the given conf files.

    @type   params:     dict
    @param  params:     DB connection parameters. Dict of style: {
                            'host': '',
                            'port': -1,
                            'user': '',
                            'password': '',
                            'db': ''
                        }
    @type   ns_file:    string
    @param  ns_file:    Address of the NS record's conf file
    """
    conn = pymysql.connect(host=params['host'],
                           port=params['port'],
                           user=params['user'],
                           password=params['password'],
                           db=params['db'])
    cur = conn.cursor()

    cur.execute("SELECT * FROM records")

    # Remove old conf files
    try:
        os.remove(ns_file)
    except:
        log.warning("NS file (" + ns_file + ") has already been deleted.")
    try:
        os.remove(a_file)
    except:
        log.warning("A file (" + a_file + ") has already been deleted.")
    try:
        os.remove(ptr_file)
    except:
        log.warning("PTR file (" + ptr_file + ") has already been deleted.")

    # Recreate conf files
    ns_f = open(ns_file, 'a')
    a_f = open(a_file, 'a')
    ptr_f = open(ptr_file, 'a')

    # Write the headers
    ns_f.write("######### NS Records #########\n\n")
    a_f.write("######### A Records #########\n\n")
    ptr_f.write("######### PTR Records #########\n\n")

    # Close the files
    ns_f.close()
    a_f.close()
    ptr_f.close()

    temp = None
    try:
        # Loop through records and write each record to the corresponding conf file
        for record in cur:
            temp = record
            if record[3] == "NS":
                if 'storage.byu.edu' not in record[2]:
                    continue
                temp = records.NS({
                    'name': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                addToFile([temp], ns_file, {}, log)
            elif record[3] == "A":
                temp = records.A({
                    'addr': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                addToFile([temp], a_file, {}, log)
            elif record[3] == "PTR":
                temp = records.PTR({
                    'ip': record[2],
                    'addr': record[4],
                    'disabled': record[8]
                })
                addToFile([temp], ptr_file, {}, log)
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        error = ''.join('!! ' + line for line in lines)
        raise RuntimeError(
            "Database dump to file failed\n" + error +
            "\n\nFailed for the following record:\n" + temp.__str__() +
            "\nOnce this is fixed restart the service with 'systemctl start copycat-master'"
        )

    cur.close()
    conn.close()
コード例 #5
0
def fromDBadds(params, log):
    """
    Retrieve records from database that have been deleted (found in records_add table).

    Also truncates the table after records have been retrieved.

    @type   params:     dict
    @param  params:     DB connection parameters. Dict of style: {
                            'host': '',
                            'port': -1,
                            'user': '',
                            'password': '',
                            'db': ''
                        }

    @return Dict containing retrieved records of style: {
        'ns_records': SortedSet([records.NS]),
        'a_records': SortedSet([records.A]),
        'ptr_records': SortedSet([records.PTR])
    }
    """
    ns_records = SortedSet([])
    a_records = SortedSet([])
    ptr_records = SortedSet([])

    conn = pymysql.connect(host=params['host'],
                           port=params['port'],
                           user=params['user'],
                           password=params['password'],
                           db=params['db'])
    cur = conn.cursor()

    cur.execute("SELECT * FROM records_add")

    for record in ResultIter(cur):
        try:
            if record[3] == "NS":
                temp = records.NS({
                    'name': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                ns_records.add(temp)
            elif record[3] == "A":
                temp = records.A({
                    'addr': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                a_records.add(temp)
            elif record[3] == "PTR":
                temp = records.PTR({
                    'ip': record[2],
                    'addr': record[4],
                    'disabled': record[8]
                })
                ptr_records.add(temp)
        except:
            log.warning("Could not properly evaluate the following record:")
            log.warning(record)
            continue

    # Delete records after retrieved
    cur.execute("TRUNCATE records_add")

    cur.close()
    conn.close()

    return {
        'ns_records': ns_records,
        'a_records': a_records,
        'ptr_records': ptr_records
    }
コード例 #6
0
def fromDB(params, full):
    """
    Retrieve records from database.

    @type   params:     dict
    @param  params:     DB connection parameters. Dict of style: {
                            'host': '',
                            'port': -1,
                            'user': '',
                            'password': '',
                            'db': ''
                        }
    @type   full:       boolean
    @param  full:       Whether to retrieve latest records or all records

    @return Dict containing retrieved records of style: {
        'ns_records': SortedSet([records.NS]),
        'a_records': SortedSet([records.A]),
        'ptr_records': SortedSet([records.PTR])
    }
    """
    ns_records = SortedSet([])
    a_records = SortedSet([])
    ptr_records = SortedSet([])

    conn = pymysql.connect(host=params['host'],
                           port=params['port'],
                           user=params['user'],
                           password=params['password'],
                           db=params['db'])
    cur = conn.cursor()

    if full:
        cur.execute("SELECT * FROM records")
    else:
        cur.execute("SELECT * FROM records WHERE change_date >= " +
                    (datetime.now() - timedelta(days=1)).strftime("%Y%m%d") +
                    "00")

    row = None
    try:
        for record in ResultIter(cur):
            row = record
            if record[3] == "NS":
                if 'storage.byu.edu' not in record[2]:
                    continue
                temp = records.NS({
                    'name': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                ns_records.add(temp)
            elif record[3] == "A":
                temp = records.A({
                    'addr': record[2],
                    'ip': record[4],
                    'disabled': record[8]
                })
                a_records.add(temp)
            elif record[3] == "PTR":
                temp = records.PTR({
                    'ip': record[2],
                    'addr': record[4],
                    'disabled': record[8]
                })
                ptr_records.add(temp)
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        error = ''.join('!! ' + line for line in lines)
        raise RuntimeError(
            "Database retrieval failed\n" + error +
            "\n\nFailed for the following record:\n" + printRecord(row) +
            "\nOnce this is fixed restart the service with 'systemctl start copycat-master'"
        )

    cur.close()
    conn.close()

    return {
        'ns_records': ns_records,
        'a_records': a_records,
        'ptr_records': ptr_records
    }