| 1 | #!/bin/sh -e |
| 2 | |
| 3 | UNBOUND_CONF="/etc/unbound/unbound.conf" |
| 4 | UNBOUND_BASE_DIR="${UNBOUND_CONF%/*}" |
| 5 | CHROOT_DIR="$(unbound-checkconf -o chroot)" |
| 6 | |
| 7 | DNS_ROOT_KEY_FILE="/usr/share/dns/root.key" |
| 8 | ROOT_TRUST_ANCHOR_FILE="/var/lib/unbound/root.key" |
| 9 | |
| 10 | # Override these variables by editing or creating /etc/default/unbound. |
| 11 | RESOLVCONF=true |
| 12 | ROOT_TRUST_ANCHOR_UPDATE=true |
| 13 | |
| 14 | if [ -f /etc/default/unbound ]; then |
| 15 | . /etc/default/unbound |
| 16 | |
| 17 | case "$RESOLVCONF" in false|0|no) |
| 18 | RESOLVCONF=false ;; |
| 19 | esac |
| 20 | |
| 21 | case "$ROOT_TRUST_ANCHOR_UPDATE" in false|0|no) |
| 22 | ROOT_TRUST_ANCHOR_UPDATE=false ;; |
| 23 | esac |
| 24 | fi |
| 25 | |
| 26 | want_resolvconf() { |
| 27 | resolvconf=/usr/sbin/resolvconf |
| 28 | [ false != "$RESOLVCONF" -a -x $resolvconf ] || return 1 |
| 29 | if [ -L $resolvconf ]; then |
| 30 | case $(readlink $resolvconf) in |
| 31 | (*resolvectl) return 1;; # systemd's resolvconf |
| 32 | esac |
| 33 | fi |
| 34 | return 0 |
| 35 | } |
| 36 | |
| 37 | do_resolvconf_start() { |
| 38 | want_resolvconf || return 0 |
| 39 | unbound-checkconf $CHROOT_DIR/$UNBOUND_CONF -o interface | { |
| 40 | default=yes |
| 41 | while read interface; do |
| 42 | default= |
| 43 | # XXXX here, only localhost and all-zero addresses are handled |
| 44 | # in case some other IP is specified it will not work |
| 45 | case "$interface" in |
| 46 | ( 0.0.0.0 | 127.0.0.1 ) echo "nameserver 127.0.0.1" ;; |
| 47 | ( ::0 | ::1 ) echo "nameserver ::1" ;; |
| 48 | esac |
| 49 | done |
| 50 | [ -z "$default" ] || |
| 51 | # unbound defaults to listening on localhost |
| 52 | echo "nameserver 127.0.0.1" |
| 53 | } | $resolvconf -a lo.unbound |
| 54 | } |
| 55 | |
| 56 | do_resolvconf_stop() { |
| 57 | want_resolvconf || return 0 |
| 58 | $resolvconf -d lo.unbound |
| 59 | } |
| 60 | |
| 61 | do_chroot_setup() { |
| 62 | [ -n "$CHROOT_DIR" -a -d "$CHROOT_DIR" ] || return 0 |
| 63 | if [ "$CHROOT_DIR" != "$UNBOUND_BASE_DIR" ]; then |
| 64 | # we probably should not do the force-recreate but just a refresh |
| 65 | rm -rf "$CHROOT_DIR/$UNBOUND_BASE_DIR" |
| 66 | mkdir -p "$CHROOT_DIR/$UNBOUND_BASE_DIR" |
| 67 | tar -C "$UNBOUND_BASE_DIR" -c . | |
| 68 | tar -C "$CHROOT_DIR/$UNBOUND_BASE_DIR" -x |
| 69 | fi |
| 70 | if [ -S "/run/systemd/notify" ]; then |
| 71 | if [ ! -e "$CHROOT_DIR/run/systemd/notify" ]; then |
| 72 | mkdir -p "$CHROOT_DIR/run/systemd" |
| 73 | touch "$CHROOT_DIR/run/systemd/notify" |
| 74 | fi |
| 75 | if ! mountpoint -q "$CHROOT_DIR/run/systemd/notify"; then |
| 76 | mount --bind "/run/systemd/notify" "$CHROOT_DIR/run/systemd/notify" |
| 77 | fi |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | do_chroot_teardown() { |
| 82 | if [ -n "$CHROOT_DIR" -a -d "$CHROOT_DIR" ] && |
| 83 | mountpoint -q "$CHROOT_DIR/run/systemd/notify"; then |
| 84 | umount "$CHROOT_DIR/run/systemd/notify" |
| 85 | fi |
| 86 | } |
| 87 | |
| 88 | do_root_trust_anchor_update() { |
| 89 | [ false != "$ROOT_TRUST_ANCHOR_UPDATE" -a \ |
| 90 | -n "$ROOT_TRUST_ANCHOR_FILE" -a \ |
| 91 | -r "$DNS_ROOT_KEY_FILE" ] || return |
| 92 | |
| 93 | if [ ! -e "$ROOT_TRUST_ANCHOR_FILE" ] || |
| 94 | # we do not want to copy if unbound's file is more recent |
| 95 | [ "$DNS_ROOT_KEY_FILE" -nt "$ROOT_TRUST_ANCHOR_FILE" ]; then |
| 96 | |
| 97 | echo "Updating $ROOT_TRUST_ANCHOR_FILE from $DNS_ROOT_KEY_FILE" |
| 98 | # Copy to temp first and do mv only when done to ensure |
| 99 | # the file is in # good condition. Can use install(1) here |
| 100 | # to set correct owner but need mv anyway, and doing either |
| 101 | # as root in an untrusted dir seems risky. |
| 102 | setpriv --reuid=unbound --regid=unbound --clear-groups \ |
| 103 | sh -c "\ |
| 104 | cp --remove-destination --preserve \ |
| 105 | \"$DNS_ROOT_KEY_FILE\" \"$ROOT_TRUST_ANCHOR_FILE.tmp\" && \ |
| 106 | mv -f \"$ROOT_TRUST_ANCHOR_FILE.tmp\" \"$ROOT_TRUST_ANCHOR_FILE\"" |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | case "$1" in |
| 111 | ( resolvconf_start \ |
| 112 | | resolvconf_stop \ |
| 113 | | chroot_setup \ |
| 114 | | chroot_teardown \ |
| 115 | | root_trust_anchor_update \ |
| 116 | ) |
| 117 | do_$1 |
| 118 | ;; |
| 119 | |
| 120 | (*) |
| 121 | echo "Usage: $0 {resolvconf_start|resolvconf_stop|chroot_setup|chroot_teardown|root_trust_anchor_update}" >&2 |
| 122 | exit 1 |
| 123 | ;; |
| 124 | esac |