bind9-parser
You got named.conf? Itching to read it and work with it ... in Python?
Now we can parse named.conf with relative ease using Python. Could even output this as JSON so ANY language can read named.conf.
PyParsing is our friend, and there are some 2,400 BNF syntax elements for named.conf ... in Python3!
Features
- Pythonized
named.conf settings (DONE)
- JSON output (DONE)
- Schema lookup (DONE)
- offline local search engine on all Bind9 clauses, statements, and keywords. (DONE)
- Python chaining the setters/getters of
view/zone clauses (DONE)
- Outputting
named.conf (IN-PROGRES)
Introduction
bind9_parser is a pythonized token constructor of named.conf configuration file
used in ISC Bind9 DNS name server daemon.
bind9_parser parses a text-based named.conf containing ISC Bind9 configuration settings, such as:
options {
recursion yes;
};
zone example.test IN {
file "db.example.test.master";
};
bind9_parser constructs a token tree of all settings found in the named.conf.
named_conf_tokens = {
'options': [{'recursion': 'yes'}],
'zone': [
{
'zone_name': 'example.test',
'class': 'IN',
'file': 'db.example.test.master'
} ] }
Tokenize named.conf variable consists of dict and list to ameliorate and preserve the many 1:1
, 1:M, 1:*, and N:M relationships that are defined between its clause and statement keywords.
Design
Token parser is chosen here for the primary purpose of performing automated checking of its valid settings. No concrete
syntax tree (and certainly no abstract syntax tree either).
This is about as simple as getting and setting configurations with like an .INI file, but with the complexity of named.conf
instead.
Token Parser Design
PyParsing library (v2.9+) is used to ensure accurate token extractions of named.conf settings and generates a single
variable containing the entire file.
Latest (and some 2,500-odd) EBNFs for ISC Bind9 named.conf have been incorporated and all of its clauses and
statements each have their own independently-usable ParseElement (an aspect of EBNF but in PyParsing parlance) that can be used to analyze even
smaller text portion of its configuration file.
Since there are no abstraction within named.conf (except maybe for the addresses_match_list), there is no need for
an AST.
No support for concrete syntax tree (CST) means no saving of comment lines, formatting, nor extraneous whitespaces. Sole
purpose of this design is to get all the raw named.conf settings. No CST support also means no way to reconstruct the
original file containing such annotation.
It is all about extracting the settings. Writing it back out into a named.conf-style file has become a secondary goal
here because too many passive security tools awaits this (pending) outputter() feature.
Python Design
The token tree consists of a Pythonized dict/list that is fully readable by various built-in JSON APIs.
There is a work-in-progress DESIGN document that will:
- tokenize
named.conf (DONE)
- Python chaining the setters/getters of
view/zone clauses (DONE)
- Outputting
named.conf from its tokenized Python variable (IN-PROGRES)
DESIGN-work-in-progress.md
Examples
A working example of:
acl MY_BASTION_HOSTS { 4.4.4.4; 3.3.3.3; 2.2.2.2; 1.1.1.1; };
controls { inet 128.0.0.9 port 8006 allow { 128.0.0.10; 128.0.0.11;} read-only yes; };
dlz your_IBM_2 { database "RSDMS"; search no; };
dyndb "example-ldap" "/usr/lib64/bind/ldap.so" {
uri "ldap://ldap.example.com";
base "cn=dns, dc=example,dc=com";
auth_method "none"; };
key dyndns { algorithm hmac-sha512; secret ABCDEFG; };
logging { channel salesfolks { file "/tmp/sales.log" size 5M; severity info; print-time no;};
channel accounting { file "/tmp/acct.log" size 30M; severity info; print-time no; };
channel badguys { file "/tmp/alert" size 255G; severity debug 77; print-time yes;}; };
managed-keys { www1.www.example.com initial-key 1 1 1 "ASBASDASD"; };
masters bastion_host_group { bastion_hosts22; hidden_bastion; };
zone red { file "/var/lib/bind9/public/masters/db.example.com"; };
server 3.4.5.6 { bogus yes; edns no; edns-udp-size 102; edns-version 2;
keys my_key_name_to_private_dns; max-udp-size 32768; notify-source *; notify-source-v6 *;
padding 53; provide-ixfr yes; query-source *; query-source address *; query-source-v6 *;
request-expire yes; request-ixfr yes; request-nsid yes; send-cookie yes; tcp-keepalive yes;
tcp-only yes; transfer-format one-answer; transfer-source *; transfer-source-v6 *; transfers 36; };
trusted-keys { abc 1 1 1 "ASBASDASD";};
zone green { file "/var/lib/bind9/public/masters/db.green.com"; };
masters dmz_masters port 7553 dscp 5 { 10.0.0.1 key priv_dns_chan_key5; };
results in producing this variable:
named_conf_elements = {
'acl': [{'acl_name': 'MY_BASTION_HOSTS',
'aml_series': [{'aml': [{'ip4_addr': '4.4.4.4'},
{'ip4_addr': '3.3.3.3'},
{'ip4_addr': '2.2.2.2'},
{'ip4_addr': '1.1.1.1'}]}]}],
'controls': [{'inet': {'allow': {'aml': [{'ip4_addr': '128.0.0.10'},
{'ip4_addr': '128.0.0.11'}]},
'control_server_addr': '128.0.0.9',
'ip_port_w': '8006',
'read-only': 'yes'}}],
'dlz': [{'db_args': 'RSDMS',
'dlz_name': 'your_IBM_2',
'search': 'no'}],
'dyndb': [{'db_name': '"example-ldap"',
'driver_parameters': 'uri '
'"ldap://ldap.example.com"; \n'
' base "cn=dns, '
'dc=example,dc=com"; \n'
' auth_method '
'"none"; ',
'module_filename': '/usr/lib64/bind/ldap.so'}],
'key': [{'algorithm': 'hmac-sha512',
'key_id': 'dyndns',
'secret': 'ABCDEFG'}],
'logging': {'channels': [{'channel_name': 'salesfolks',
'path_name': '/tmp/sales.log',
'print_time': 'no',
'severity': ['info'],
'size_spec': [5, 'M']},
{'channel_name': 'accounting',
'path_name': '/tmp/acct.log',
'print_time': 'no',
'severity': ['info'],
'size_spec': [30, 'M']},
{'channel_name': 'badguys',
'path_name': '/tmp/alert',
'print_time': 'yes',
'severity': {'debug': {'debug_level': 77}},
'size_spec': [255, 'G']}]},
'managed_keys': [{'algorithm_id': 1,
'flags': 1,
'key_secret': '"ASBASDASD"',
'protocol_id': 1,
'rr_domain': 'www1.www.example.com'}],
'primaries': [{'dscp_port': 5,
'ip_port': '7553',
'primary_id': 'dmz_masters',
'primary_list': [{'ip4_addr': '10.0.0.1',
'key_id': 'priv_dns_chan_key5'}]}],
'server': [{'configs': {'bogus': 'yes',
'edns': 'no',
'edns_udp_size': 102,
'edns_version': 2,
'keys': 'my_key_name_to_private_dns',
'max_udp_size': 32768,
'notify_source': {'ip4_addr': '*'},
'notify_source_v6': {'ip6_addr_w': '*'},
'padding': 53,
'provide_ixfr': 'yes',
'query_source': {'ip4_addr_w': '*'},
'query_source_v6': {'ip6_addr_w': '*'},
'request_expire': 'yes',
'request_ixfr': 'yes',
'request_nsid': 'yes',
'send_cookie': 'yes',
'tcp_keepalive': 'yes',
'tcp_only': 'yes',
'transfer_format': 'one-answer',
'transfer_source': {'ip4_addr_w': '*'},
'transfer_source_v6': {'ip6_addr': '*'},
'transfers': 36},
'ip_addr': '3.4.5.6'}],
'trusted_keys': [{'algorithm_id': '1',
'domain': 'abc',
'key_id': '1',
'protocol_type': '1',
'pubkey_base64': 'ASBASDASD'}],
'zones': [{'file': '/var/lib/bind9/public/masters/db.example.com',
'zone_name': 'red'},
{'file': '/var/lib/bind9/public/masters/db.green.com',
'zone_name': 'green'}]}
)
Full-Blown Examples