[BACK]Return to kasp2policy.py CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / external / mpl / bind / dist / contrib / kasp

Annotation of src/external/mpl/bind/dist/contrib/kasp/kasp2policy.py, Revision 1.1.1.1.2.2

1.1.1.1.2.2! pgoyette    1: #!/usr/bin/python
        !             2: ############################################################################
        !             3: # Copyright (C) 2015  Internet Systems Consortium, Inc. ("ISC")
        !             4: #
        !             5: # Permission to use, copy, modify, and/or distribute this software for any
        !             6: # purpose with or without fee is hereby granted, provided that the above
        !             7: # copyright notice and this permission notice appear in all copies.
        !             8: #
        !             9: # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
        !            10: # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
        !            11: # AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
        !            12: # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
        !            13: # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
        !            14: # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
        !            15: # PERFORMANCE OF THIS SOFTWARE.
        !            16: ############################################################################
        !            17: # kasp2policy.py
        !            18: # This translates the Keys section of a KASP XML file into a dnssec.policy
        !            19: # file that can be used by dnssec-keymgr.
        !            20: ############################################################################
        !            21:
        !            22: from xml.etree import cElementTree as ET
        !            23: from collections import defaultdict
        !            24: from isc import dnskey
        !            25: import ply.yacc as yacc
        !            26: import ply.lex as lex
        !            27: import re
        !            28:
        !            29: ############################################################################
        !            30: # Translate KASP duration values into seconds
        !            31: ############################################################################
        !            32: class kasptime:
        !            33:     class ktlex:
        !            34:         tokens = ( 'P', 'T', 'Y', 'M', 'D', 'H', 'S', 'NUM' )
        !            35:
        !            36:         t_P = r'(?i)P'
        !            37:         t_T = r'(?i)T'
        !            38:         t_Y = r'(?i)Y'
        !            39:         t_M = r'(?i)M'
        !            40:         t_D = r'(?i)D'
        !            41:         t_H = r'(?i)H'
        !            42:         t_S = r'(?i)S'
        !            43:
        !            44:         def t_NUM(self, t):
        !            45:             r'\d+'
        !            46:             t.value = int(t.value)
        !            47:             return t
        !            48:
        !            49:         def t_error(self, t):
        !            50:             print("Illegal character '%s'" % t.value[0])
        !            51:             t.lexer.skip(1)
        !            52:
        !            53:         def __init__(self):
        !            54:             self.lexer = lex.lex(object=self)
        !            55:
        !            56:     def __init__(self):
        !            57:         self.lexer = self.ktlex()
        !            58:         self.tokens = self.lexer.tokens
        !            59:         self.parser = yacc.yacc(debug=False, write_tables=False, module=self)
        !            60:
        !            61:     def parse(self, text):
        !            62:         self.lexer.lexer.lineno = 0
        !            63:         return self.parser.parse(text)
        !            64:
        !            65:     def p_ktime_4(self, p):
        !            66:         "ktime : P periods T times"
        !            67:         p[0] = p[2] + p[4]
        !            68:
        !            69:     def p_ktime_3(self, p):
        !            70:         "ktime : P T times"
        !            71:         p[0] = p[3]
        !            72:
        !            73:     def p_ktime_2(self, p):
        !            74:         "ktime : P periods"
        !            75:         p[0] = p[2]
        !            76:
        !            77:     def p_periods_1(self, p):
        !            78:         "periods : period"
        !            79:         p[0] = p[1]
        !            80:
        !            81:     def p_periods_2(self, p):
        !            82:         "periods : periods period"
        !            83:         p[0] = p[1] + p[2]
        !            84:
        !            85:     def p_times_1(self, p):
        !            86:         "times : time"
        !            87:         p[0] = p[1]
        !            88:
        !            89:     def p_times_2(self, p):
        !            90:         "times : times time"
        !            91:         p[0] = p[1] + p[2]
        !            92:
        !            93:     def p_period(self, p):
        !            94:         '''period : NUM Y
        !            95:                   | NUM M
        !            96:                   | NUM D'''
        !            97:         if p[2].lower() == 'y':
        !            98:             p[0] = int(p[1]) * 31536000
        !            99:         elif p[2].lower() == 'm':
        !           100:             p[0] = int(p[1]) * 2592000
        !           101:         elif p[2].lower() == 'd':
        !           102:             p[0] += int(p[1]) * 86400
        !           103:
        !           104:     def p_time(self, p):
        !           105:         '''time : NUM H
        !           106:                 | NUM M
        !           107:                 | NUM S'''
        !           108:         if p[2].lower() == 'h':
        !           109:             p[0] = int(p[1]) * 3600
        !           110:         elif p[2].lower() == 'm':
        !           111:             p[0] = int(p[1]) * 60
        !           112:         elif p[2].lower() == 's':
        !           113:             p[0] = int(p[1])
        !           114:
        !           115:     def p_error(self, p):
        !           116:         print("Syntax error")
        !           117:
        !           118: ############################################################################
        !           119: # Load the contents of a KASP XML file as a python dictionary
        !           120: ############################################################################
        !           121: class kasp():
        !           122:     @staticmethod
        !           123:     def _todict(t):
        !           124:         d = {t.tag: {} if t.attrib else None}
        !           125:         children = list(t)
        !           126:         if children:
        !           127:             dd = defaultdict(list)
        !           128:             for dc in map(kasp._todict, children):
        !           129:                 for k, v in dc.iteritems():
        !           130:                     dd[k].append(v)
        !           131:             d = {t.tag:
        !           132:                     {k:v[0] if len(v) == 1 else v for k, v in dd.iteritems()}}
        !           133:         if t.attrib:
        !           134:             d[t.tag].update(('@' + k, v) for k, v in t.attrib.iteritems())
        !           135:         if t.text:
        !           136:             text = t.text.strip()
        !           137:             if children or t.attrib:
        !           138:                 if text:
        !           139:                     d[t.tag]['#text'] = text
        !           140:             else:
        !           141:                 d[t.tag] = text
        !           142:         return d
        !           143:
        !           144:     def __init__(self, filename):
        !           145:         self._dict = kasp._todict(ET.parse(filename).getroot())
        !           146:
        !           147:     def __getitem__(self, key):
        !           148:         return self._dict[key]
        !           149:
        !           150:     def __len__(self):
        !           151:         return len(self._dict)
        !           152:
        !           153:     def __iter__(self):
        !           154:         return self._dict.__iter__()
        !           155:
        !           156:     def __repr__(self):
        !           157:         return repr(self._dict)
        !           158:
        !           159: ############################################################################
        !           160: # Load the contents of a KASP XML file as a python dictionary
        !           161: ############################################################################
        !           162: if __name__ == "__main__":
        !           163:     from pprint import *
        !           164:     import sys
        !           165:
        !           166:     if len(sys.argv) < 2:
        !           167:         print("Usage: kasp2policy <filename>")
        !           168:         exit(1)
        !           169:
        !           170:     try:
        !           171:         kinfo = kasp(sys.argv[1])
        !           172:     except:
        !           173:         print("%s: unable to load KASP file '%s'" % (sys.argv[0], sys.argv[1]))
        !           174:         exit(1)
        !           175:
        !           176:     kt = kasptime()
        !           177:     first = True
        !           178:
        !           179:     for p in kinfo['KASP']['Policy']:
        !           180:         if not p['@name'] or not p['Keys']: continue
        !           181:         if not first:
        !           182:             print("")
        !           183:         first = False
        !           184:         if p['Description']:
        !           185:             d = p['Description'].strip()
        !           186:             print("# %s" % re.sub(r"\n\s*", "\n# ", d))
        !           187:         print("policy %s {" % p['@name'])
        !           188:         ksk = p['Keys']['KSK']
        !           189:         zsk = p['Keys']['ZSK']
        !           190:         kalg = ksk['Algorithm']
        !           191:         zalg = zsk['Algorithm']
        !           192:         algnum = kalg['#text'] or zalg['#text']
        !           193:         if algnum:
        !           194:             print("\talgorithm %s;" % dnskey.algstr(int(algnum)))
        !           195:         if p['Keys']['TTL']:
        !           196:             print("\tkeyttl %d;" % kt.parse(p['Keys']['TTL']))
        !           197:         if kalg['@length']:
        !           198:             print("\tkey-size ksk %d;" % int(kalg['@length']))
        !           199:         if zalg['@length']:
        !           200:             print("\tkey-size zsk %d;" % int(zalg['@length']))
        !           201:         if ksk['Lifetime']:
        !           202:             print("\troll-period ksk %d;" % kt.parse(ksk['Lifetime']))
        !           203:         if zsk['Lifetime']:
        !           204:             print("\troll-period zsk %d;" % kt.parse(zsk['Lifetime']))
        !           205:         if ksk['Standby']:
        !           206:             print("\tstandby ksk %d;" % int(ksk['Standby']))
        !           207:         if zsk['Standby']:
        !           208:             print("\tstandby zsk %d;" % int(zsk['Standby']))
        !           209:         print("};")

CVSweb <webmaster@jp.NetBSD.org>