From c3d133a75e764b6628644d20594031794799037f Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Mon, 10 May 2021 00:16:32 -0400 Subject: [PATCH] Add README.md and some scripts to get DHCPD hosts into unbound for DNS --- README.md | 3 + cron/unbound-dhcpd-updater | 28 +++++++++ dhcpd/list_active_assignments | 95 +++++++++++++++++++++++++++++++ unbound/local-data-file-generator | 15 +++++ 4 files changed, 141 insertions(+) create mode 100644 README.md create mode 100755 cron/unbound-dhcpd-updater create mode 100644 dhcpd/list_active_assignments create mode 100755 unbound/local-data-file-generator diff --git a/README.md b/README.md new file mode 100644 index 0000000..7374924 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# openbsd-tools + +My personal OpenBSD-specific scripts, probably for my router. diff --git a/cron/unbound-dhcpd-updater b/cron/unbound-dhcpd-updater new file mode 100755 index 0000000..56f8fac --- /dev/null +++ b/cron/unbound-dhcpd-updater @@ -0,0 +1,28 @@ +#!/bin/sh +set -eu + +readonly DHCPD_CONF_FILE='/etc/dhcpd.conf' +readonly DHCPD_LEASES_FILE='/var/db/dhcpd.leases' +readonly UNBOUND_LOCAL_DATA_FILE='/var/unbound/etc/local-data-dhcpd.conf' +UNBOUND_LOCAL_DATA_FILE_TMP="$(mktemp)" +readonly UNBOUND_LOCAL_DATA_FILE_TMP + +get_file_modify_time() { + stat -f %m "$1" +} + +if [ -e "$UNBOUND_LOCAL_DATA_FILE" ]; then + UNBOUND_LOCAL_DATA_FILE_MODIFY_TIME="$(get_file_modify_time "$UNBOUND_LOCAL_DATA_FILE")" + + if [ "$UNBOUND_LOCAL_DATA_FILE_MODIFY_TIME" -gt "$(get_file_modify_time "$DHCPD_CONF_FILE")" ]; then + if [ "$UNBOUND_LOCAL_DATA_FILE_MODIFY_TIME" -gt "$(get_file_modify_time "$DHCPD_LEASES_FILE")" ]; then + exit 0 + fi + fi +fi + +../dhcpd/list_active_assignments | ../unbound/local-data-file-generator > "$UNBOUND_LOCAL_DATA_FILE_TMP" + +mv "$UNBOUND_LOCAL_DATA_FILE_TMP" "$UNBOUND_LOCAL_DATA_FILE" + +rcctl reload unbound diff --git a/dhcpd/list_active_assignments b/dhcpd/list_active_assignments new file mode 100644 index 0000000..4684324 --- /dev/null +++ b/dhcpd/list_active_assignments @@ -0,0 +1,95 @@ +#!/bin/sh +set -eu + +readonly DHCPD_CONF_FILE="${DHCPD_CONF_FILE:-/etc/dhcpd.conf}" +readonly DHCPD_LEASES_FILE="${DHCPD_LEASES_FILE:-/var/db/dhcpd.leases}" + +DHCPD_CONF_FILE_AWK_SCRIPT=$(cat << 'EOF' +/^[ \t]*host[ \t]+[^ \t]+[ \t]+\{[ \t]*$/ { + ip = "" + hostname = $2 + next +} + +/^[ \t]*fixed-address[ \t]+([0-9]{1,3}\.){3}[0-9]{1,3}[ \t]*;[ \t]*$/ { + ip = substr($2, 1, length($2)-1) + next +} + +/\}/ { + if (hostname && ip) { + print hostname "\t" ip + } + + hostname = "" + ip = "" + next +} +EOF +) + +readonly DHCPD_CONF_FILE_AWK_SCRIPT + +DHCPD_LEASES_FILE_AWK_SCRIPT=$(cat << 'EOF' +function parse_datetime(datetime) { + command = "date -j -f \"%w %Y/%m/%d %H:%M:%S %Z;\" +%s \"" datetime "\"" + if (command | getline unix_time < 0) { + exit 1 + } + + close(command) + + return unix_time +} + +/^[ \t]*lease[ \t]+([0-9]{1,3}\.){3}[0-9]{1,3}[ \t]+\{[ \t]*$/ { + ip = $2 + next +} + +/^[ \t]*(starts|ends)[ \t]+[0-6][ \t]+[0-9]+\/[0-9]+\/[0-9]+[ \t]+[0-9]+:[0-9]+:[0-9]+([ \t]+[a-zA-Z0-9]+)?[ \t]*;[ \t]*$/ { + if ($1 == "starts") { + $1 = "" + # remove the leading space caused by setting $1 to "" + starts = parse_datetime(substr($0, 2)) + } else { + $1 = "" + # remove the leading space caused by setting $1 to "" + ends = parse_datetime(substr($0, 2)) + } + + next +} + +/^[ \t]*(client-)?hostname[ \t]+\"[^\"]+\"[ \t]*;[ \t]*$/ { + hostname = gensub("\"|;", "", "g", $2) + next +} + +/^[ \t]*abandoned[ \t]*;[ \t]*$/ { + abandoned = 1 + next +} + +/\}/ { + now = systime() + + if (!abandoned && hostname && ip && starts && ends && now >= starts && now < ends) { + print hostname "\t" ip + } + + hostname = "" + ip = "" + starts = 0 + ends = 0 + abandoned = 0 +} +EOF +) + +readonly DHCPD_LEASES_FILE_AWK_SCRIPT + +( + awk "${DHCPD_CONF_FILE_AWK_SCRIPT}" "${DHCPD_CONF_FILE}" + awk "${DHCPD_LEASES_FILE_AWK_SCRIPT}" "${DHCPD_LEASES_FILE}" +) | sort -u | column -c 2 -t diff --git a/unbound/local-data-file-generator b/unbound/local-data-file-generator new file mode 100755 index 0000000..01559b8 --- /dev/null +++ b/unbound/local-data-file-generator @@ -0,0 +1,15 @@ +#!/bin/sh + +set -eu + +AWK_SCRIPT=$(cat << 'EOF' +/^[ \t]*[^ \t]+[ \t]+([0-9]{1,3}\.){3}[0-9]{1,3}[ \t]*$/ { + print "local-data: \"" $1 " A " $2 "\"" + print "local-data-ptr: \"" $2 " " $1 "\"" +} +EOF +) + +readonly AWK_SCRIPT + +awk "$AWK_SCRIPT"