2011-06-24 13:36:06 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
config=/etc/chrony.conf
|
|
|
|
keyfile=/etc/chrony.keys
|
|
|
|
chronyc=/usr/bin/chronyc
|
|
|
|
dhclient_servers=/var/lib/dhclient/chrony.servers.*
|
|
|
|
service_name=chronyd.service
|
|
|
|
|
|
|
|
get_key() {
|
|
|
|
awk '/^[ \t]*'$1'\>/ { print $2; exit }' < $keyfile
|
|
|
|
}
|
|
|
|
|
|
|
|
get_commandkeyid() {
|
|
|
|
awk '/^[ \t]*commandkey\>/ { keyid=$2 } END { print keyid }' < $config
|
|
|
|
}
|
|
|
|
|
|
|
|
chrony_command() {
|
|
|
|
commandkeyid=$(get_commandkeyid)
|
|
|
|
[ -z "$commandkeyid" ] && return 1
|
|
|
|
commandkey=$(get_key $commandkeyid)
|
|
|
|
[ -z "$commandkey" ] && return 2
|
|
|
|
|
|
|
|
$chronyc <<EOF
|
|
|
|
password $commandkey
|
|
|
|
$1
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_commandkey() {
|
|
|
|
commandkeyid=$(get_commandkeyid)
|
|
|
|
[ -z "$commandkeyid" ] && return 1
|
|
|
|
commandkey=$(get_key $commandkeyid)
|
|
|
|
[ -z "$commandkey" ] || return 0
|
|
|
|
|
2011-09-06 13:45:43 +00:00
|
|
|
commandkey=$(tr -c -d '[\041-\176]' < /dev/urandom | head -c 16)
|
2011-06-24 13:36:06 +00:00
|
|
|
[ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile
|
|
|
|
}
|
|
|
|
|
|
|
|
add_dhclient_servers() {
|
|
|
|
command=$(cat $dhclient_servers 2> /dev/null |
|
|
|
|
while read server serverargs; do
|
|
|
|
echo "add server $server $serverargs"
|
|
|
|
done)
|
|
|
|
if [ -n "$command" ]; then
|
|
|
|
chrony_command "$command" &> /dev/null
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
is_running() {
|
2011-09-06 13:50:07 +00:00
|
|
|
systemctl status $service_name &> /dev/null
|
2011-06-24 13:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
generate-commandkey)
|
|
|
|
generate_commandkey
|
|
|
|
;;
|
|
|
|
add-dhclient-servers)
|
|
|
|
add_dhclient_servers
|
|
|
|
;;
|
|
|
|
command)
|
|
|
|
is_running && chrony_command "$2"
|
|
|
|
;;
|
2011-09-06 13:50:07 +00:00
|
|
|
forced-command)
|
|
|
|
chrony_command "$2"
|
|
|
|
;;
|
2011-06-24 13:36:06 +00:00
|
|
|
*)
|
2011-09-06 13:50:07 +00:00
|
|
|
echo $"Usage: $0 {generate-commandkey|add-dhclient-servers|command|forced-command}"
|
2011-06-24 13:36:06 +00:00
|
|
|
exit 2
|
|
|
|
esac
|
|
|
|
exit $?
|
|
|
|
|