#!/bin/sh
HELPERTOOLS=/Library/PrivilegedHelperTools
SERVICE_NAME=org.chromium.chromoting
CONFIG_FILE="$HELPERTOOLS/$SERVICE_NAME.json"
OLD_SCRIPT_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me.sh"
PLIST=/Library/LaunchAgents/org.chromium.chromoting.plist
BROKER_PLIST=/Library/LaunchDaemons/org.chromium.chromoting.broker.plist
PAM_CONFIG=/etc/pam.d/chrome-remote-desktop
ENABLED_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me_enabled"
ENABLED_FILE_BACKUP="$ENABLED_FILE.backup"
HOST_BUNDLE_NAME=@@HOST_BUNDLE_NAME@@
HOST_SERVICE_BINARY="$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/remoting_me2me_host_service"
HOST_LEGACY_BUNDLE_NAME=@@HOST_LEGACY_BUNDLE_NAME@@
NATIVE_MESSAGING_HOST_BUNDLE_NAME=@@NATIVE_MESSAGING_HOST_BUNDLE_NAME@@
REMOTE_ASSISTANCE_HOST_BUNDLE_NAME=@@REMOTE_ASSISTANCE_HOST_BUNDLE_NAME@@
HOST_EXE="$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/remoting_me2me_host"
USERS_TMP_FILE="$HOST_SERVICE_BINARY.users"
BROKER_SERVICE_TARGET="system/org.chromium.chromoting.broker"
KSADMIN=/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Helpers/ksadmin
if [[ ! -x "${KSADMIN}" ]]; then
KSADMIN=/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin
fi
KSUPDATE=https://tools.google.com/service/update2
KSPID=com.google.chrome_remote_desktop
KSPVERSION=@@VERSION@@
function on_error {
logger An error occurred during Chrome Remote Desktop setup.
exit 1
}
function find_login_window_for_user {
local user=$1
ps -ec -u "$user" -o comm,pid | awk '$1 == "loginwindow" { print $2; exit }'
}
function is_el_capitan_or_newer {
local full_version=$(sw_vers -productVersion)
local version
IFS='.' read -a version <<< "${full_version}"
local v0="${version[0]}"
local v1="${version[1]}"
if [[ $v0 -gt 10 || ( $v0 -eq 10 && $v1 -ge 11 ) ]]; then
return 0
else
return 1
fi
}
trap on_error ERR
trap 'rm -f "$USERS_TMP_FILE"' EXIT
logger Running Chrome Remote Desktop postflight script @@VERSION@@
$KSADMIN --register --productid "$KSPID" --version "$KSPVERSION" \
--xcpath "$PLIST" --url "$KSUPDATE"
if [[ -f "$ENABLED_FILE_BACKUP" ]]; then
logger Restoring _enabled file
mv "$ENABLED_FILE_BACKUP" "$ENABLED_FILE"
fi
if [[ -f "$INSTALLER_TEMP/script_backup" ]]; then
logger Restoring original launchd script
mv "$INSTALLER_TEMP/script_backup" "$OLD_SCRIPT_FILE"
fi
update_pam=1
CONTROL_LINE="# If you edit this file, please delete this line."
if [[ -f "$PAM_CONFIG" ]] && ! grep -qF "$CONTROL_LINE" "$PAM_CONFIG"; then
update_pam=0
fi
if [[ "$update_pam" == "1" ]]; then
logger Creating PAM config.
$(cat > "$PAM_CONFIG" <<EOF
# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
auth required pam_deny.so
account required pam_permit.so
password required pam_deny.so
session required pam_deny.so
# This file is auto-updated by the Chrome Remote Desktop installer.
$CONTROL_LINE
EOF
) || true
else
logger PAM config has local edits. Not updating.
fi
rm -rf "$HELPERTOOLS/$HOST_LEGACY_BUNDLE_NAME"
ln -s "$HELPERTOOLS/$HOST_BUNDLE_NAME" "$HELPERTOOLS/$HOST_LEGACY_BUNDLE_NAME"
rm -f "$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/$NATIVE_MESSAGING_HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat"
ln -s "$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat" \
"$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/$NATIVE_MESSAGING_HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat"
rm -f "$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/$REMOTE_ASSISTANCE_HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat"
ln -s "$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat" \
"$HELPERTOOLS/$HOST_BUNDLE_NAME/Contents/MacOS/$REMOTE_ASSISTANCE_HOST_BUNDLE_NAME/Contents/Resources/icudtl.dat"
logger Loading broker service
logger launchctl enable $BROKER_SERVICE_TARGET
launchctl enable $BROKER_SERVICE_TARGET
logger launchctl bootstrap system $BROKER_PLIST
launchctl bootstrap system $BROKER_PLIST
if [[ -n "$USER" && "$USER" != "root" ]]; then
id -u "$USER" >> "$USERS_TMP_FILE"
fi
if [[ -r "$USERS_TMP_FILE" ]]; then
for uid in $(sort "$USERS_TMP_FILE" | uniq); do
logger Starting host service for user "$uid".
load="launchctl load -w -S Aqua $PLIST"
start="launchctl start $SERVICE_NAME"
if is_el_capitan_or_newer; then
bootstrap_user="launchctl asuser $uid"
else
pid="$(find_login_window_for_user "$uid")"
if [[ ! -n "$pid" ]]; then
exit 1
fi
sudo_user="sudo -u #$uid"
bootstrap_user="launchctl bsexec $pid"
fi
logger $bootstrap_user $sudo_user $load
$bootstrap_user $sudo_user $load
logger $bootstrap_user $sudo_user $start
$bootstrap_user $sudo_user $start
done
fi