"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/bin/python/isc/keydict.py.in" (4 Sep 2020, 2848 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 ############################################################################
    2 # Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3 #
    4 # This Source Code Form is subject to the terms of the Mozilla Public
    5 # License, v. 2.0. If a copy of the MPL was not distributed with this
    6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7 #
    8 # See the COPYRIGHT file distributed with this work for additional
    9 # information regarding copyright ownership.
   10 ############################################################################
   11 
   12 from collections import defaultdict
   13 from . import dnskey
   14 import os
   15 import glob
   16 
   17 
   18 ########################################################################
   19 # Class keydict
   20 ########################################################################
   21 class keydict:
   22     """ A dictionary of keys, indexed by name, algorithm, and key id """
   23 
   24     _keydict = defaultdict(lambda: defaultdict(dict))
   25     _defttl = None
   26     _missing = []
   27 
   28     def __init__(self, dp=None, **kwargs):
   29         self._defttl = kwargs.get('keyttl', None)
   30         zones = kwargs.get('zones', None)
   31 
   32         if not zones:
   33             path = kwargs.get('path',None) or '.'
   34             self.readall(path)
   35         else:
   36             for zone in zones:
   37                 if 'path' in kwargs and kwargs['path'] is not None:
   38                     path = kwargs['path']
   39                 else:
   40                     path = dp and dp.policy(zone).directory or '.'
   41                 if not self.readone(path, zone):
   42                     self._missing.append(zone)
   43 
   44     def readall(self, path):
   45         files = glob.glob(os.path.join(path, '*.private'))
   46 
   47         for infile in files:
   48             key = dnskey(infile, path, self._defttl)
   49             self._keydict[key.name][key.alg][key.keyid] = key
   50 
   51     def readone(self, path, zone):
   52         if not zone.endswith('.'):
   53             zone += '.'
   54         match='K' + zone + '+*.private'
   55         files = glob.glob(os.path.join(path, match))
   56 
   57         found = False
   58         for infile in files:
   59             key = dnskey(infile, path, self._defttl)
   60             if key.fullname != zone: # shouldn't ever happen
   61                 continue
   62             keyname=key.name if zone != '.' else '.'
   63             self._keydict[keyname][key.alg][key.keyid] = key
   64             found = True
   65 
   66         return found
   67 
   68     def __iter__(self):
   69         for zone, algorithms in self._keydict.items():
   70             for alg, keys in algorithms.items():
   71                 for key in keys.values():
   72                     yield key
   73 
   74     def __getitem__(self, name):
   75         return self._keydict[name]
   76 
   77     def zones(self):
   78         return (self._keydict.keys())
   79 
   80     def algorithms(self, zone):
   81         return (self._keydict[zone].keys())
   82 
   83     def keys(self, zone, alg):
   84         return (self._keydict[zone][alg].keys())
   85 
   86     def missing(self):
   87         return (self._missing)