364 lines
10 KiB
Go
364 lines
10 KiB
Go
package guesttools
|
|
|
|
import "github.com/Nwflower/reims-go/internal/host"
|
|
|
|
func ResolveEffectiveTimezone(setting, hostTZ string) string {
|
|
switch setting {
|
|
case "off":
|
|
return ""
|
|
case "", "host":
|
|
setting = hostTZ
|
|
}
|
|
if host.ValidateIANATimezone(setting) {
|
|
return setting
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func effectiveTimezone(setting string) string {
|
|
if setting == "" || setting == "host" {
|
|
return ResolveEffectiveTimezone(setting, host.DetectHostTimezone())
|
|
}
|
|
return ResolveEffectiveTimezone(setting, "")
|
|
}
|
|
|
|
// SetTimezone updates the setting served on the guest's next config poll.
|
|
func (s *Server) SetTimezone(vmKey, token, setting string) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if vm := s.vms[normalizeVMRegistryKey(vmKey)]; vm != nil && vm.token == token && vm.guestTimezone != setting {
|
|
vm.guestTimezone = setting
|
|
vm.timezoneError = ""
|
|
}
|
|
}
|
|
|
|
const guestTimezonePath = "/private/etc/reims-timezone/set-timezone.sh"
|
|
const guestTimezoneSudoers = "/private/etc/sudoers.d/reims-timezone-"
|
|
|
|
const guestTimezoneValidation = `
|
|
valid_timezone() {
|
|
case "$1" in
|
|
""|-*|/*|*/|*//*|*/*/*/*|*[!ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+/-]*) return 1 ;;
|
|
esac
|
|
[ "${#1}" -le 64 ]
|
|
}
|
|
`
|
|
|
|
const guestTimezoneAgent = `
|
|
TIMEZONE_HELPER="` + guestTimezonePath + `"
|
|
timezone_error=""
|
|
timezone_helper_ready=false
|
|
timezone_check_at=0
|
|
` + guestTimezoneValidation + `
|
|
current_timezone() (
|
|
link=$(/usr/bin/readlink /etc/localtime) || return 0
|
|
case "$link" in
|
|
*/zoneinfo/*) tz=${link#*/zoneinfo/} ;;
|
|
*) return 0 ;;
|
|
esac
|
|
if valid_timezone "$tz"; then
|
|
printf '%s\n' "$tz"
|
|
fi
|
|
)
|
|
|
|
sync_timezone() {
|
|
desired_timezone=$(config_timezone "$1") || desired_timezone=""
|
|
timezone_error=""
|
|
if [ -z "$desired_timezone" ] || [ ! -x "$TIMEZONE_HELPER" ]; then
|
|
timezone_helper_ready=false
|
|
timezone_check_at=0
|
|
return 0
|
|
fi
|
|
timezone_now=$(/bin/date +%s)
|
|
if [ "$timezone_now" -ge "$timezone_check_at" ]; then
|
|
timezone_helper_ready=false
|
|
if /usr/bin/sudo -n -l -- "$TIMEZONE_HELPER" >/dev/null 2>&1; then
|
|
timezone_helper_ready=true
|
|
fi
|
|
timezone_check_at=$((timezone_now + 30))
|
|
fi
|
|
if [ "$timezone_helper_ready" = true ] && [ "$desired_timezone" != "$(current_timezone)" ]; then
|
|
if ! /usr/bin/sudo -n -- "$TIMEZONE_HELPER" "$desired_timezone" >/dev/null 2>&1; then
|
|
timezone_error=apply_failed
|
|
fi
|
|
fi
|
|
}
|
|
`
|
|
|
|
const guestTimezoneConfig = `
|
|
# The bridge emits compact JSON and timezone IDs contain no JSON escapes.
|
|
config_timezone() (
|
|
case "$1" in
|
|
*'"timezone":"'*)
|
|
value=${1#*'"timezone":"'}
|
|
printf '%s\n' "${value%%\"*}"
|
|
;;
|
|
*) return 1 ;;
|
|
esac
|
|
)
|
|
`
|
|
|
|
const guestTimezoneSelection = guestTimezoneConfig + `
|
|
requested_timezone() (
|
|
cfg=$(/usr/bin/curl -fsS -m 3 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/config" 2>/dev/null) || return 1
|
|
config_timezone "$cfg"
|
|
)
|
|
`
|
|
|
|
const guestTimezoneFile = guestTimezoneValidation + `
|
|
resolve_timezone_path() (
|
|
if [ -x /usr/bin/realpath ]; then
|
|
/usr/bin/realpath "$1"
|
|
return $?
|
|
fi
|
|
path=$1
|
|
hops=0
|
|
while :; do
|
|
dir=$(CDPATH= cd -P "$(/usr/bin/dirname "$path")" 2>/dev/null && pwd -P) || return 1
|
|
path="$dir/$(/usr/bin/basename "$path")"
|
|
if [ ! -L "$path" ]; then
|
|
[ -e "$path" ] || return 1
|
|
printf '%s\n' "$path"
|
|
return 0
|
|
fi
|
|
[ "$hops" -lt 40 ] || return 1
|
|
hops=$((hops + 1))
|
|
target=$(/usr/bin/readlink "$path") || return 1
|
|
case "$target" in
|
|
/*) path=$target ;;
|
|
*) path="$dir/$target" ;;
|
|
esac
|
|
done
|
|
)
|
|
|
|
timezone_file() (
|
|
[ "$1" != Local ] && valid_timezone "$1" || return 1
|
|
for zone_dir in /var/db/timezone/zoneinfo /usr/share/zoneinfo; do
|
|
tz_file="$zone_dir/$1"
|
|
[ -f "$tz_file" ] || continue
|
|
real_dir=$(resolve_timezone_path "$zone_dir") || continue
|
|
real_file=$(resolve_timezone_path "$tz_file") || continue
|
|
case "$real_file" in
|
|
"$real_dir/"*)
|
|
[ "$(/bin/dd if="$real_file" bs=4 count=1 2>/dev/null)" = TZif ] || continue
|
|
printf '%s\n' "$tz_file"
|
|
return 0
|
|
;;
|
|
esac
|
|
done
|
|
return 1
|
|
)
|
|
`
|
|
|
|
const guestTimezoneHelper = `#!/bin/sh
|
|
set -eu
|
|
export LC_ALL=C
|
|
` + guestTimezoneFile + `
|
|
if [ "$#" -ne 1 ] || ! tz_file=$(timezone_file "$1"); then
|
|
echo "Usage: set-timezone.sh IANA_TIMEZONE (with an installed zoneinfo file)" >&2
|
|
exit 1
|
|
fi
|
|
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool NO
|
|
/bin/ln -sfn "$tz_file" /etc/localtime
|
|
/usr/bin/killall -HUP timed 2>/dev/null || true
|
|
`
|
|
|
|
const guestTimezoneSystemPaths = `
|
|
case "$1" in
|
|
""|*[!0-9]*) exit 1 ;;
|
|
esac
|
|
helper="` + guestTimezonePath + `"
|
|
rule_prefix="` + guestTimezoneSudoers + `"
|
|
sudoers="$rule_prefix$1"
|
|
helper_dir=${helper%/*}
|
|
sudoers_dir=${sudoers%/*}
|
|
|
|
secure_path() {
|
|
if [ ! -L "$1" ] && test "$2" "$1"; then
|
|
owner=$(/usr/bin/stat -f '%u' "$1") || return 1
|
|
mode=$(/usr/bin/stat -f '%Lp' "$1") || return 1
|
|
acl=$(/bin/ls -lde "$1") || return 1
|
|
if [ "$owner" = 0 ] && [ "$((0$mode & $3))" -eq 0 ] &&
|
|
! printf '%s\n' "$acl" | /usr/bin/grep -Eq '^[[:space:]]*[0-9]+: .* allow '; then
|
|
return 0
|
|
fi
|
|
fi
|
|
echo "Reims Guest Tools cannot use $1" >&2
|
|
return 1
|
|
}
|
|
|
|
lock_timezone_files() {
|
|
lock="$sudoers_dir/.reims-timezone.lock"
|
|
if ! /bin/mkdir "$lock"; then
|
|
echo "Could not lock Reims timezone files. Please retry." >&2
|
|
return 1
|
|
fi
|
|
trap 'exit 1' HUP INT TERM
|
|
}
|
|
`
|
|
|
|
const guestApplyTimezone = `#!/bin/sh
|
|
set -eu
|
|
export LC_ALL=C
|
|
SRC="/Volumes/REIMSTOOLS"
|
|
TIMEZONE_HELPER="` + guestTimezonePath + `"
|
|
` + guestTimezoneFile + guestTimezoneSelection + `
|
|
case "$#" in
|
|
0)
|
|
. "$SRC/config"
|
|
if ! tz=$(requested_timezone); then
|
|
echo "Could not read the current timezone policy from the Reims host bridge at $REIMS_HOST_URL." >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
1) tz=$1 ;;
|
|
*)
|
|
echo "Usage: Apply-Timezone.command [IANA_TIMEZONE]" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
if [ -z "$tz" ] && [ "$#" -eq 0 ]; then
|
|
echo "Reims Guest Tools has no timezone to apply."
|
|
exit 0
|
|
fi
|
|
if ! timezone_file "$tz" >/dev/null; then
|
|
echo "Invalid IANA timezone: $tz" >&2
|
|
exit 1
|
|
fi
|
|
|
|
/usr/bin/sudo /bin/sh -s -- "$(/usr/bin/id -u)" <<'ROOT'
|
|
set -eu
|
|
export LC_ALL=C
|
|
` + guestTimezoneSystemPaths + `
|
|
secure_path /private -d 022
|
|
secure_path /private/etc -d 022
|
|
if [ ! -e "$sudoers_dir" ] && [ ! -L "$sudoers_dir" ]; then
|
|
/usr/bin/install -d -o root -g wheel -m 0755 "$sudoers_dir"
|
|
fi
|
|
secure_path "$sudoers_dir" -d 022
|
|
lock_timezone_files
|
|
trap '/bin/rmdir "$lock"' EXIT
|
|
if [ ! -e "$helper_dir" ] && [ ! -L "$helper_dir" ]; then
|
|
/usr/bin/install -d -o root -g wheel -m 0755 "$helper_dir"
|
|
fi
|
|
secure_path "$helper_dir" -d 022
|
|
|
|
for backup in "$helper.bak" "$sudoers.bak"; do
|
|
if [ -e "$backup" ] || [ -L "$backup" ]; then
|
|
echo "An earlier Reims timezone backup remains at $backup." >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
backup_file() {
|
|
if [ -e "$1" ] || [ -L "$1" ]; then
|
|
secure_path "$1" -f 022 && /bin/ln "$1" "$1.bak"
|
|
fi
|
|
}
|
|
|
|
restore_file() {
|
|
if [ -f "$1.bak" ]; then
|
|
if [ "$1" -ef "$1.bak" ]; then
|
|
/bin/rm -f "$1.bak"
|
|
else
|
|
/bin/mv -f "$1.bak" "$1"
|
|
fi
|
|
elif [ "$2" = 1 ]; then
|
|
/bin/rm -f "$1"
|
|
fi
|
|
}
|
|
|
|
rule_tmp=""
|
|
helper_tmp=""
|
|
helper_replacing=0
|
|
rule_replacing=0
|
|
committed=0
|
|
cleanup_timezone_install() {
|
|
result=$?
|
|
trap '' HUP INT TERM
|
|
set +e
|
|
if [ "$committed" = 0 ]; then
|
|
echo "Could not configure Reims timezone sudo access." >&2
|
|
restore_file "$sudoers" "$rule_replacing" || echo "Could not restore $sudoers" >&2
|
|
restore_file "$helper" "$helper_replacing" || echo "Could not restore $helper" >&2
|
|
else
|
|
/bin/rm -f "$helper.bak" "$sudoers.bak" || result=1
|
|
fi
|
|
if [ -n "$helper_tmp" ]; then
|
|
/bin/rm -f "$helper_tmp" || result=1
|
|
fi
|
|
if [ -n "$rule_tmp" ]; then
|
|
/bin/rm -f "$rule_tmp" || result=1
|
|
fi
|
|
/bin/rmdir "$lock" || result=1
|
|
exit "$result"
|
|
}
|
|
trap cleanup_timezone_install EXIT
|
|
|
|
rule_tmp=$(/usr/bin/mktemp "$sudoers_dir/.reims-timezone.XXXXXX")
|
|
# A numeric sudoers user ID avoids interpreting special characters in a username.
|
|
printf '#%s ALL=(root) NOPASSWD: %s\n' "$1" "$helper" >"$rule_tmp"
|
|
/usr/sbin/chown root:wheel "$rule_tmp"
|
|
/bin/chmod 0440 "$rule_tmp"
|
|
/usr/sbin/visudo -cf "$rule_tmp"
|
|
|
|
helper_tmp=$(/usr/bin/mktemp "$helper_dir/.set-timezone.XXXXXX")
|
|
/usr/bin/install -o root -g wheel -m 0755 /Volumes/REIMSTOOLS/set-timezone.sh "$helper_tmp"
|
|
backup_file "$helper"
|
|
backup_file "$sudoers"
|
|
helper_replacing=1
|
|
/bin/mv -f "$helper_tmp" "$helper"
|
|
rule_replacing=1
|
|
/bin/mv -f "$rule_tmp" "$sudoers"
|
|
/usr/sbin/visudo -cf "$sudoers"
|
|
committed=1
|
|
ROOT
|
|
|
|
/usr/bin/sudo -n -- "$TIMEZONE_HELPER" "$tz"
|
|
/bin/date
|
|
`
|
|
|
|
const guestTimezoneUninstall = `
|
|
if [ "$#" -eq 0 ]; then
|
|
TIMEZONE_HELPER="` + guestTimezonePath + `"
|
|
TIMEZONE_SUDOERS="` + guestTimezoneSudoers + `$uid"
|
|
if [ -e "$TIMEZONE_SUDOERS" ] || [ -L "$TIMEZONE_SUDOERS" ] ||
|
|
[ -e "$TIMEZONE_HELPER" ] || [ -L "$TIMEZONE_HELPER" ]; then
|
|
if ! /usr/bin/sudo /bin/sh -s -- "$uid" <<'ROOT'
|
|
set -eu
|
|
export LC_ALL=C
|
|
` + guestTimezoneSystemPaths + `
|
|
secure_path /private -d 022
|
|
secure_path /private/etc -d 022
|
|
if [ -e "$sudoers_dir" ] || [ -L "$sudoers_dir" ]; then
|
|
secure_path "$sudoers_dir" -d 022
|
|
else
|
|
/usr/bin/install -d -o root -g wheel -m 0755 "$sudoers_dir"
|
|
fi
|
|
lock_timezone_files
|
|
trap '/bin/rmdir "$lock"' EXIT
|
|
/bin/rm -f "$sudoers"
|
|
for rule in "$rule_prefix"*; do
|
|
if [ -e "$rule" ] || [ -L "$rule" ]; then
|
|
exit 0
|
|
fi
|
|
done
|
|
if [ -e "$helper" ] || [ -L "$helper" ]; then
|
|
secure_path "$helper_dir" -d 022
|
|
/bin/rm -f "$helper"
|
|
fi
|
|
ROOT
|
|
then
|
|
for path in "$TIMEZONE_SUDOERS" "$TIMEZONE_HELPER"; do
|
|
if [ -e "$path" ] || [ -L "$path" ]; then
|
|
echo "Remaining Reims timezone file: $path" >&2
|
|
fi
|
|
done
|
|
echo "Could not remove Reims timezone sync. Run this uninstaller again to retry." >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
fi
|
|
`
|