#!/usr/bin/awk -f function read_whitelist(n) { while ( getline < "unbound.never_block" ) { gsub(/[[:space:]]*/,"",$0); whitelist[$0] = n++; log_info("whitelisting " $0); } } function timestamp(zone) { zone = strftime("%z"); if (zone != "") { zone = sprintf("%s:%s", substr(zone,0,3),substr(zone,4)); } return sprintf("%s:%s", strftime("%FT%H:%M:%S"), zone) } function logger(level,message,zone) { printf("%s %s %s\n", timestamp(), level, message) > "/dev/stderr"; } function log_info(message) { logger("INFO", message); } function log_warning(message) { logger("WARN", message); } function acceptable_local_zone() { if (NF > 4) { log_warning(sprintf("line %d: too many words, expected 3", NF)); return 0; } if ($1 != "local-zone") { log_warning(sprintf("line %d: invalid directive: %s", NR, $1)); return 0; } if ($3 != "always_null") { log_warning(sprintf("line %d: invalid local-zone type: %s", NR, $3)); return 0; } if ($2 in whitelist) { log_warning(sprintf("line %d: whitelisted zone found: %s", NR, $2)); return 0; } return 1; } BEGIN { read_whitelist(); FS="[[:space:]\":]+"; log_info(sprintf("read %d whitelist domains", length(whitelist))); printf("# generated by extract-oisd.awk at %s\n", timestamp()); } /^[[:space:]]*#/ { next; } /^[[:space:]]*$/ { next; } /^server:$/ { print; servers++; next; } acceptable_local_zone() != 1 { next; } { print; localzones++; } END { log_info(sprintf("servers = %d, local-zones = %d\n", servers, localzones)); }