checkds.py 7.45 KB
Newer Older
Evan Hunt's avatar
Evan Hunt committed
1 2 3 4 5
#!/usr/bin/python
import argparse
import pprint
import os

Evan Hunt's avatar
Evan Hunt committed
6 7 8 9
######################################################################
# DSRR class:
# Delegation Signer (DS) resource record
######################################################################
Evan Hunt's avatar
Evan Hunt committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class DSRR:
    hashalgs = {1: 'SHA-1', 2: 'SHA-256', 3: 'GOST'}
    rrname=''
    rrclass='IN'
    rrtype='DS'
    keyid=None
    keyalg=None
    hashalg=None
    digest=''
    ttl=0

    def __init__(self, rrtext):
        if not rrtext:
            return

        fields = rrtext.split()
        if len(fields) < 7:
            return

        self.rrname = fields[0].lower()
        fields = fields[1:]
        if fields[0].upper() in ['IN','CH','HS']:
            self.rrclass = fields[0].upper()
            fields = fields[1:]
        else:
            self.ttl = int(fields[0])
            self.rrclass = fields[1].upper()
            fields = fields[2:]

        if fields[0].upper() != 'DS':
            raise Exception

        self.rrtype = 'DS'
        self.keyid = int(fields[1])
        self.keyalg = int(fields[2])
        self.hashalg = int(fields[3])
        self.digest = ''.join(fields[4:]).upper()

    def __repr__(self):
        return('%s %s %s %d %d %d %s' %
                (self.rrname, self.rrclass, self.rrtype, self.keyid,
                self.keyalg, self.hashalg, self.digest))

    def __eq__(self, other):
        return self.__repr__() == other.__repr__()

Evan Hunt's avatar
Evan Hunt committed
56 57 58 59
######################################################################
# DLVRR class:
# DNSSEC Lookaside Validation (DLV) resource record
######################################################################
Evan Hunt's avatar
Evan Hunt committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
class DLVRR:
    hashalgs = {1: 'SHA-1', 2: 'SHA-256', 3: 'GOST'}
    parent=''
    dlvname=''
    rrname='IN'
    rrclass='IN'
    rrtype='DLV'
    keyid=None
    keyalg=None
    hashalg=None
    digest=''
    ttl=0

    def __init__(self, rrtext, dlvname = 'dlv.isc.org'):
        if not rrtext:
            return

        fields = rrtext.split()
        if len(fields) < 7:
            return

        self.dlvname = dlvname.lower()
        parent = fields[0].lower().strip('.').split('.')
        parent.reverse()
        dlv = dlvname.split('.')
        dlv.reverse()
        while len(dlv) != 0 and len(parent) != 0 and parent[0] == dlv[0]:
            parent = parent[1:]
            dlv = dlv[1:]
        if len(dlv) != 0:
            raise Exception
        parent.reverse()
        self.parent = '.'.join(parent)
        self.rrname = self.parent + '.' + self.dlvname
        
        fields = fields[1:]
        if fields[0].upper() in ['IN','CH','HS']:
            self.rrclass = fields[0].upper()
            fields = fields[1:]
        else:
            self.ttl = int(fields[0])
            self.rrclass = fields[1].upper()
            fields = fields[2:]

        if fields[0].upper() != 'DLV':
            raise Exception

        self.rrtype = 'DLV'
        self.keyid = int(fields[1])
        self.keyalg = int(fields[2])
        self.hashalg = int(fields[3])
        self.digest = ''.join(fields[4:]).upper()

    def __repr__(self):
        return('%s %s %s %d %d %d %s' %
                (self.rrname, self.rrclass, self.rrtype,
                self.keyid, self.keyalg, self.hashalg, self.digest))

    def __eq__(self, other):
        return self.__repr__() == other.__repr__()

######################################################################
Evan Hunt's avatar
Evan Hunt committed
122 123 124 125 126
# checkds:
# Fetch DS RRset for the given zone from the DNS; fetch DNSKEY
# RRset from the masterfile if specified, or from DNS if not.
# Generate a set of expected DS records from the DNSKEY RRset,
# and report on congruency.
Evan Hunt's avatar
Evan Hunt committed
127
######################################################################
Evan Hunt's avatar
Evan Hunt committed
128
def checkds(zone, masterfile = None):
Evan Hunt's avatar
Evan Hunt committed
129 130 131 132 133 134 135 136
    dslist=[]
    fp=os.popen("/usr/local/bin/dig +noall +answer -t ds " + zone)
    for line in fp:
        dslist.append(DSRR(line))
    dslist = sorted(dslist, key=lambda ds: (ds.keyid, ds.keyalg, ds.hashalg))
    fp.close()

    dsklist=[]
Evan Hunt's avatar
Evan Hunt committed
137 138 139 140 141 142 143 144

    if masterfile:
        fp = os.popen("/usr/local/sbin/dnssec-dsfromkey -f %s %s " %
                      (masterfile, zone))
    else:
        fp = os.popen("/usr/local/bin/dig +noall +answer -t dnskey " + zone +
                      " | /usr/local/sbin/dnssec-dsfromkey -f - " + zone)

Evan Hunt's avatar
Evan Hunt committed
145 146
    for line in fp:
        dsklist.append(DSRR(line))
Evan Hunt's avatar
Evan Hunt committed
147

Evan Hunt's avatar
Evan Hunt committed
148 149 150 151 152 153 154 155 156 157 158 159 160
    fp.close()

    found = False
    for ds in dsklist:
        if ds in dslist:
            print ("DS for KSK %s/%03d/%05d (%s) found in parent" %
                   (ds.rrname.strip('.'), ds.keyalg,
                    ds.keyid, DSRR.hashalgs[ds.hashalg]))
            found = True

    if not found:
        print ("No DS records found covering %s/DNSKEY" % zone)

Evan Hunt's avatar
Evan Hunt committed
161
    return found
Evan Hunt's avatar
Evan Hunt committed
162

Evan Hunt's avatar
Evan Hunt committed
163 164 165 166 167 168 169 170
######################################################################
# checkdlv:
# Fetch DLV RRset for the given zone from the DNS; fetch DNSKEY
# RRset from the masterfile if specified, or from DNS if not.
# Generate a set of expected DLV records from the DNSKEY RRset,
# and report on congruency.
######################################################################
def checkdlv(zone, lookaside, masterfile = None):
Evan Hunt's avatar
Evan Hunt committed
171 172 173 174 175 176 177 178 179 180 181 182 183
    dlvlist=[]
    fp=os.popen("/usr/local/bin/dig +noall +answer -t dlv " +
                zone + '.' + lookaside)
    for line in fp:
        dlvlist.append(DLVRR(line))
    dlvlist = sorted(dlvlist,
                     key=lambda dlv: (dlv.keyid, dlv.keyalg, dlv.hashalg))
    fp.close()

    #
    # Fetch DNSKEY records from DNS and generate DLV records from them
    #
    dlvklist=[]
Evan Hunt's avatar
Evan Hunt committed
184 185 186 187 188 189 190 191
    if masterfile:
        fp = os.popen("/usr/local/sbin/dnssec-dsfromkey -f %s -l %s %s " %
                      (masterfile, lookaside, zone))
    else:
        fp = os.popen("/usr/local/bin/dig +noall +answer -t dnskey %s "
                      " | /usr/local/sbin/dnssec-dsfromkey -f - -l %s %s"
                      % (zone, lookaside, zone))

Evan Hunt's avatar
Evan Hunt committed
192 193
    for line in fp:
        dlvklist.append(DLVRR(line))
Evan Hunt's avatar
Evan Hunt committed
194

Evan Hunt's avatar
Evan Hunt committed
195 196
    fp.close()

Evan Hunt's avatar
Evan Hunt committed
197
    found = False
Evan Hunt's avatar
Evan Hunt committed
198 199 200 201 202 203 204 205 206 207
    for dlv in dlvklist:
        if dlv in dlvlist:
            print ("DLV for KSK %s/%03d/%05d (%s) found in %s" %
                   (dlv.parent, dlv.keyalg, dlv.keyid,
                    DLVRR.hashalgs[dlv.hashalg], dlv.dlvname))
            found = True

    if not found:
        print ("No DLV records found covering %s/DNSKEY" % zone)

Evan Hunt's avatar
Evan Hunt committed
208 209
    return found

Evan Hunt's avatar
Evan Hunt committed
210

Evan Hunt's avatar
Evan Hunt committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
######################################################################
# parse_args:
# Read command line arguments, set global 'args' structure
######################################################################
def parse_args():
    global args
    parser = argparse.ArgumentParser(description='checkds: checks DS coverage')

    parser.add_argument('zone', type=str, help='zone to check')
    parser.add_argument('-f', '--file', dest='masterfile', type=str,
                        help='zone master file')
    parser.add_argument('-l', '--lookaside', dest='lookaside', type=str,
                        help='DLV lookaside zone')
    parser.add_argument('-v', '--version', action='version', version='9.9.1')
    args = parser.parse_args()

    args.zone = args.zone.strip('.')
    if args.lookaside:
        lookaside = args.lookaside.strip('.')

######################################################################
# Main
######################################################################
def main():
    parse_args()

    if args.lookaside:
        found = checkdlv(args.zone, args.lookaside, args.masterfile)
    else:
        found = checkds(args.zone, args.masterfile)

    exit(0 if found else 1)
Evan Hunt's avatar
Evan Hunt committed
243 244 245

if __name__ == "__main__":
    main()