ascension

Migrate DNS zones to the GNU Name System
Log | Files | Refs | README | LICENSE

keyfile.py (1721B)


      1 """
      2 This file is part of Ascension.
      3 Copyright (C) 2018-2022 GNUnet e.V.
      4 
      5 Ascension is free software: you can redistribute it and/or modify it
      6 under the terms of the GNU Affero General Public License as published
      7 by the Free Software Foundation, either version 3 of the License,
      8 or (at your option) any later version.
      9 
     10 Ascension is distributed in the hope that it will be useful, but
     11 WITHOUT ANY WARRANTY; without even the implied warranty of
     12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13 Affero General Public License for more details.
     14 
     15 You should have received a copy of the GNU Affero General Public License
     16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 SPDX-License-Identifier: AGPL3.0-or-later
     19 
     20 Author: rexxnor
     21 """
     22 
     23 import shlex
     24 
     25 import dns.name
     26 import dns.tsig
     27 
     28 #@staticmethod
     29 def parse_bind_keyfile(keyring: str) -> dict[dns.name.Name, dns.tsig.Key]:
     30     """
     31     Reads a BIND style keyfile and creates a dictionary in the form of:
     32     dict(dns.name.Name: dns.tsig.Key)
     33     """
     34     if not keyring:
     35         return None
     36 
     37     with open(keyring, 'r', encoding='utf-8') as keyfile:
     38         ast = shlex.shlex(keyfile.read())
     39 
     40     ast.whitespace_split = True
     41 
     42     keydict = {}
     43 
     44     while True:
     45         while ast.get_token() == "key":
     46             keyname = dns.name.from_text(ast.get_token())
     47             _ = ast.get_token()
     48             if ast.get_token() == "algorithm":
     49                 keyalgo = ast.get_token().strip(";")
     50             if ast.get_token() == "secret":
     51                 keysecret = ast.get_token().strip(";")
     52             keydict[keyname] = dns.tsig.Key(keyname, keysecret, keyalgo)
     53         if ast.get_token():
     54             continue
     55         break
     56     return keydict