Blame SOURCES/chrony.helper

f132bc
#!/bin/bash
f132bc
# This script configures running chronyd to use NTP servers obtained from
f132bc
# DHCP and _ntp._udp DNS SRV records. Files with servers from DHCP are managed
f132bc
# externally (e.g. by a dhclient script). Files with servers from DNS SRV
f132bc
# records are updated here using the dig utility. The script can also list
f132bc
# and set static sources in the chronyd configuration file.
f132bc
f132bc
chronyc=/usr/bin/chronyc
f132bc
chrony_conf=/etc/chrony.conf
f132bc
chrony_service=chronyd.service
f132bc
helper_dir=/var/run/chrony-helper
f132bc
added_servers_file=$helper_dir/added_servers
f132bc
f132bc
network_sysconfig_file=/etc/sysconfig/network
f132bc
dhclient_servers_files=/var/lib/dhclient/chrony.servers.*
f132bc
dnssrv_servers_files=$helper_dir/dnssrv@*
f132bc
dnssrv_timer_prefix=chrony-dnssrv@
f132bc
f132bc
chrony_command() {
f132bc
    $chronyc -a -n -m "$1"
f132bc
}
f132bc
f132bc
is_running() {
f132bc
    chrony_command "tracking" &> /dev/null
f132bc
}
f132bc
f132bc
is_update_needed() {
f132bc
    for file in $dhclient_servers_files $dnssrv_servers_files \
f132bc
            $added_servers_file; do
f132bc
        [ -e "$file" ] && return 0
f132bc
    done
f132bc
    return 1
f132bc
}
f132bc
f132bc
update_daemon() {
f132bc
    local all_servers_with_args all_servers added_servers
f132bc
f132bc
    if ! is_running; then
f132bc
        rm -f $added_servers_file
f132bc
        return 0
f132bc
    fi
f132bc
f132bc
    all_servers_with_args=$(
f132bc
        cat $dhclient_servers_files $dnssrv_servers_files 2> /dev/null)
f132bc
f132bc
    all_servers=$(
f132bc
        echo "$all_servers_with_args" |
f132bc
            while read server serverargs; do
f132bc
                echo "$server"
f132bc
            done | sort -u)
f132bc
    added_servers=$( (
f132bc
        cat $added_servers_file 2> /dev/null
f132bc
        echo "$all_servers_with_args" |
f132bc
            while read server serverargs; do
f132bc
                [ -z "$server" ] && continue
f132bc
                chrony_command "add server $server $serverargs" &> /dev/null &&
f132bc
                    echo "$server"
f132bc
            done) | sort -u)
f132bc
f132bc
    comm -23 <(echo -n "$added_servers") <(echo -n "$all_servers") |
f132bc
        while read server; do
f132bc
            chrony_command "delete $server" &> /dev/null
f132bc
        done
f132bc
f132bc
    added_servers=$(comm -12 <(echo -n "$added_servers") <(echo -n "$all_servers"))
f132bc
f132bc
    [ -n "$added_servers" ] && echo "$added_servers" > $added_servers_file ||
f132bc
        rm -f $added_servers_file
f132bc
}
f132bc
f132bc
get_dnssrv_servers() {
f132bc
    local name=$1
f132bc
f132bc
    if ! command -v dig &> /dev/null; then
f132bc
        echo "Missing dig (DNS lookup utility)" >&2
f132bc
        return 1
f132bc
    fi
f132bc
f132bc
    (
f132bc
        . $network_sysconfig_file &> /dev/null
f132bc
f132bc
        output=$(dig "$name" srv +short +ndots=2 +search 2> /dev/null)
f132bc
        [ $? -ne 0 ] && return 0
f132bc
f132bc
        echo "$output" | while read prio weight port target; do
f132bc
            server=${target%.}
f132bc
            [ -z "$server" ] && continue
f132bc
            echo "$server port $port ${NTPSERVERARGS:-iburst}"
f132bc
        done
f132bc
    )
f132bc
}
f132bc
f132bc
check_dnssrv_name() {
f132bc
    local name=$1
f132bc
f132bc
    if [ -z "$name" ]; then
f132bc
        echo "No DNS SRV name specified" >&2
f132bc
        return 1
f132bc
    fi
f132bc
f132bc
    if [ "${name:0:9}" != _ntp._udp ]; then
f132bc
        echo "DNS SRV name $name doesn't start with _ntp._udp" >&2
f132bc
        return 1
f132bc
    fi
f132bc
}
f132bc
f132bc
update_dnssrv_servers() {
f132bc
    local name=$1
f132bc
    local srv_file=$helper_dir/dnssrv@$name servers
f132bc
f132bc
    check_dnssrv_name "$name" || return 1
f132bc
f132bc
    servers=$(get_dnssrv_servers "$name")
f132bc
    [ -n "$servers" ] && echo "$servers" > "$srv_file" || rm -f "$srv_file"
f132bc
}
f132bc
f132bc
set_dnssrv_timer() {
f132bc
    local state=$1 name=$2
f132bc
    local srv_file=$helper_dir/dnssrv@$name servers
f132bc
    local timer=$dnssrv_timer_prefix$(systemd-escape "$name").timer
f132bc
f132bc
    check_dnssrv_name "$name" || return 1
f132bc
f132bc
    if [ "$state" = enable ]; then
f132bc
        systemctl enable "$timer"
f132bc
        systemctl start "$timer"
f132bc
    elif [ "$state" = disable ]; then
f132bc
        systemctl stop "$timer"
f132bc
        systemctl disable "$timer"
f132bc
        rm -f "$srv_file"
f132bc
    fi
f132bc
}
f132bc
f132bc
list_dnssrv_timers() {
f132bc
    systemctl --all --full -t timer list-units | grep "^$dnssrv_timer_prefix" | \
f132bc
            sed "s|^$dnssrv_timer_prefix\(.*\)\.timer.*|\1|" |
f132bc
        while read -r name; do
f132bc
            systemd-escape --unescape "$name"
f132bc
        done
f132bc
}
f132bc
f132bc
prepare_helper_dir() {
f132bc
    mkdir -p $helper_dir
f132bc
    exec 100> $helper_dir/lock
f132bc
    if ! flock -w 20 100; then
f132bc
        echo "Failed to lock $helper_dir" >&2
f132bc
        return 1
f132bc
    fi
f132bc
}
f132bc
f132bc
is_source_line() {
f132bc
    local pattern="^[ \t]*(server|pool|peer|refclock)[ \t]+[^ \t]+"
f132bc
    [[ "$1" =~ $pattern ]]
f132bc
}
f132bc
f132bc
list_static_sources() {
f132bc
    while read line; do
f132bc
        is_source_line "$line" && echo "$line" || :
f132bc
    done < $chrony_conf
f132bc
}
f132bc
f132bc
set_static_sources() {
f132bc
    local new_config tmp_conf
f132bc
f132bc
    new_config=$(
f132bc
        sources=$(
f132bc
            while read line; do
f132bc
                is_source_line "$line" && echo "$line"
f132bc
            done)
f132bc
f132bc
        while read line; do
f132bc
            if ! is_source_line "$line"; then
f132bc
                echo "$line"
f132bc
                continue
f132bc
            fi
f132bc
f132bc
            tmp_sources=$(
f132bc
                local removed=0
f132bc
f132bc
                echo "$sources" | while read line2; do
f132bc
                    [ "$removed" -ne 0 -o "$line" != "$line2" ] && \
f132bc
                        echo "$line2" || removed=1
f132bc
                done)
f132bc
f132bc
            [ "$sources" == "$tmp_sources" ] && continue
f132bc
            sources=$tmp_sources
f132bc
            echo "$line"
f132bc
        done < $chrony_conf
f132bc
f132bc
        echo "$sources"
f132bc
    )
f132bc
f132bc
    tmp_conf=${chrony_conf}.tmp
f132bc
f132bc
    cp -a $chrony_conf $tmp_conf &&
f132bc
        echo "$new_config" > $tmp_conf &&
f132bc
        mv $tmp_conf $chrony_conf || return 1
f132bc
f132bc
    systemctl try-restart $chrony_service
f132bc
}
f132bc
f132bc
print_help() {
f132bc
    echo "Usage: $0 COMMAND"
f132bc
    echo
f132bc
    echo "Commands:"
f132bc
    echo "	update-daemon"
f132bc
    echo "	update-dnssrv-servers NAME"
f132bc
    echo "	enable-dnssrv NAME"
f132bc
    echo "	disable-dnssrv NAME"
f132bc
    echo "	list-dnssrv"
f132bc
    echo "	list-static-sources"
f132bc
    echo "	set-static-sources < sources.list"
f132bc
    echo "	is-running"
f132bc
    echo "	command CHRONYC-COMMAND"
f132bc
}
f132bc
f132bc
case "$1" in
f132bc
    update-daemon|add-dhclient-servers|remove-dhclient-servers)
f132bc
        is_update_needed || exit 0
f132bc
        prepare_helper_dir && update_daemon
f132bc
        ;;
f132bc
    update-dnssrv-servers)
f132bc
        prepare_helper_dir && update_dnssrv_servers "$2" && update_daemon
f132bc
        ;;
f132bc
    enable-dnssrv)
f132bc
        set_dnssrv_timer enable "$2"
f132bc
        ;;
f132bc
    disable-dnssrv)
f132bc
        set_dnssrv_timer disable "$2" && prepare_helper_dir && update_daemon
f132bc
        ;;
f132bc
    list-dnssrv)
f132bc
        list_dnssrv_timers
f132bc
        ;;
f132bc
    list-static-sources)
f132bc
        list_static_sources
f132bc
        ;;
f132bc
    set-static-sources)
f132bc
        set_static_sources
f132bc
        ;;
f132bc
    is-running)
f132bc
        is_running
f132bc
        ;;
f132bc
    command|forced-command)
f132bc
        chrony_command "$2"
f132bc
        ;;
f132bc
    *)
f132bc
        print_help
f132bc
        exit 2
f132bc
esac
f132bc
f132bc
exit $?