Add README.md and some scripts to get DHCPD hosts into unbound for DNS

This commit is contained in:
Tony Blyler 2021-05-10 00:16:32 -04:00
parent 68a324be03
commit c3d133a75e
4 changed files with 141 additions and 0 deletions

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# openbsd-tools
My personal OpenBSD-specific scripts, probably for my router.

28
cron/unbound-dhcpd-updater Executable file
View file

@ -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

View file

@ -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

View file

@ -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"