no sudo on osx

This commit is contained in:
AJ ONeal 2018-06-24 02:36:09 -06:00
parent fa29aa3faf
commit 83d4df0762
1 changed files with 128 additions and 60 deletions

View File

@ -32,12 +32,20 @@ set -u
### http_bash exported by get.sh ### http_bash exported by get.sh
TELEBIT_VERSION=${TELEBIT_VERSION:-master} TELEBIT_VERSION=${TELEBIT_VERSION:-master}
TELEBIT_USERSPACE=${TELEBIT_USERSPACE:-no}
my_email=${1:-} my_email=${1:-}
my_relay=${2:-} my_relay=${2:-}
my_servernames=${3:-} my_servernames=${3:-}
my_secret=${4:-} my_secret=${4:-}
my_user="${TELEBIT_USER:-telebit}"
my_group="${TELEBIT_GROUP:-telebit}" cur_user="$(id -u -n)"
TELEBIT_USER="${TELEBIT_USER:-$cur_user}"
my_user="$TELEBIT_USER"
cur_group="$(id -g -n)"
TELEBIT_GROUP="${TELEBIT_GROUP:-$cur_group}"
my_group="$TELEBIT_GROUP"
my_app_pkg_name="cloud.telebit.remote" my_app_pkg_name="cloud.telebit.remote"
my_app="telebit" my_app="telebit"
my_daemon="telebitd" my_daemon="telebitd"
@ -78,17 +86,25 @@ fi
echo "" echo ""
TELEBIT_REAL_PATH=${TELEBIT_PATH:-}
if [ $(id -u) -ne 0 ] && [ "$TELEBIT_USER" == "$cur_user" ]; then
TELEBIT_USERSPACE="yes"
if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
echo 'TELEBIT_PATH="'${TELEBIT_REAL_PATH:-}'"'
TELEBIT_REAL_PATH=$HOME/Applications/$my_app
fi
else
TELEBIT_USERSPACE="no"
if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
echo 'TELEBIT_PATH="'${TELEBIT_REAL_PATH:-}'"'
TELEBIT_REAL_PATH=/opt/$my_app
fi
fi
TELEBIT_PATH="$TELEBIT_REAL_PATH"
TELEBIT_TMP="$TELEBIT_REAL_PATH"
my_tmp="$(mktemp -d)" my_tmp="$(mktemp -d)"
#TELEBIT_TMP="$my_tmp/telebit" #TELEBIT_TMP="$my_tmp/telebit"
TELEBIT_REAL_PATH=${TELEBIT_PATH:-}
TELEBIT_TMP=${TELEBIT_PATH:-}
if [ -z "${TELEBIT_PATH:-}" ]; then
echo 'TELEBIT_PATH="'${TELEBIT_PATH:-}'"'
TELEBIT_PATH=/opt/$my_app
TELEBIT_REAL_PATH=/opt/$my_app
TELEBIT_TMP=/opt/$my_app
#TELEBIT_PATH=$HOME/Applications/$my_app
fi
echo "Installing $my_name to '$TELEBIT_REAL_PATH'" echo "Installing $my_name to '$TELEBIT_REAL_PATH'"
# v10.2+ has much needed networking fixes, but breaks ursa. v9.x has severe networking bugs. v8.x has working ursa, but requires tls workarounds" # v10.2+ has much needed networking fixes, but breaks ursa. v9.x has severe networking bugs. v8.x has working ursa, but requires tls workarounds"
@ -154,6 +170,9 @@ else
fi fi
set -e set -e
#
# TODO create slim packages that contain all the deps on each os and cpu
#
pushd $TELEBIT_TMP >/dev/null pushd $TELEBIT_TMP >/dev/null
echo " - installing telebit npm dependencies to '$TELEBIT_REAL_PATH'..." echo " - installing telebit npm dependencies to '$TELEBIT_REAL_PATH'..."
echo " (are you noticing a pattern of where things are installed?)" echo " (are you noticing a pattern of where things are installed?)"
@ -163,6 +182,16 @@ popd >/dev/null
echo " - configuring telebit..." echo " - configuring telebit..."
echo "" echo ""
###############################################
#
# TODO convert to node script
#
# Now that node is installed and the telebit
# packeage is downloaded, everything can be
# run from node, except things requiring sudo
#
###############################################
# telebit remote # telebit remote
echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_app" echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_app"
echo "$my_node $TELEBIT_REAL_PATH/bin/$my_bin "'"$@"' >> "$TELEBIT_TMP/bin/$my_app" echo "$my_node $TELEBIT_REAL_PATH/bin/$my_bin "'"$@"' >> "$TELEBIT_TMP/bin/$my_app"
@ -176,9 +205,12 @@ chmod a+x "$TELEBIT_TMP/bin/$my_daemon"
# Create uninstall script based on the install script variables # Create uninstall script based on the install script variables
cat << EOF > $TELEBIT_TMP/bin/${my_app}_uninstall cat << EOF > $TELEBIT_TMP/bin/${my_app}_uninstall
#!/bin/bash #!/bin/bash
set -x
if [ "$(type -p launchctl)" ]; then if [ "$(type -p launchctl)" ]; then
sudo launchctl unload -w /Library/LaunchDaemons/${my_app_pkg_name}.plist sudo launchctl unload -w /Library/LaunchDaemons/${my_app_pkg_name}.plist
sudo rm -rf /Library/LaunchDaemons/${my_app_pkg_name}.plist sudo rm -f /Library/LaunchDaemons/${my_app_pkg_name}.plist
launchctl unload -w ~/Library/LaunchAgents/${my_app_pkg_name}.plist
rm -f ~/Library/LaunchAgents/${my_app_pkg_name}.plist
fi fi
if [ "$(type -p systemctl)" ]; then if [ "$(type -p systemctl)" ]; then
sudo systemctl disable $my_app >/dev/null; sudo systemctl stop $my_app sudo systemctl disable $my_app >/dev/null; sudo systemctl stop $my_app
@ -194,11 +226,6 @@ echo""
echo "(your password may be required to complete the installation)" echo "(your password may be required to complete the installation)"
echo"" echo""
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app"
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon"
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon
#set +e #set +e
#if type -p setcap >/dev/null 2>&1; then #if type -p setcap >/dev/null 2>&1; then
# #echo "Setting permissions to allow $my_app to run on port 80 and port 443 without sudo or root" # #echo "Setting permissions to allow $my_app to run on port 80 and port 443 without sudo or root"
@ -216,9 +243,10 @@ if type -p dscl >/dev/null 2>/dev/null; then
if [ -n "$(dscl . list /users | grep ^$my_user)" ] && [ -n "$(dscl . list /groups | grep ^$my_group)" ]; then if [ -n "$(dscl . list /users | grep ^$my_user)" ] && [ -n "$(dscl . list /groups | grep ^$my_group)" ]; then
my_skip="yes" my_skip="yes"
fi fi
elif [ -n "$(cat $my_root/etc/passwd | grep $my_user)" ]; then elif [ -n "$(cat $my_root/etc/passwd | grep $my_user)" ] && [ -n "$(cat $my_root/etc/group | grep $my_group)" ]; then
my_skip="yes" my_skip="yes"
fi fi
if [ -z "$my_skip" ]; then if [ -z "$my_skip" ]; then
if type -p adduser >/dev/null 2>/dev/null; then if type -p adduser >/dev/null 2>/dev/null; then
$real_sudo_cmd adduser --home $TELEBIT_REAL_PATH --gecos '' --disabled-password $my_user >/dev/null 2>&1 $real_sudo_cmd adduser --home $TELEBIT_REAL_PATH --gecos '' --disabled-password $my_user >/dev/null 2>&1
@ -243,32 +271,82 @@ set -e
export TELEBIT_USER=$my_user export TELEBIT_USER=$my_user
export TELEBIT_GROUP=$my_group export TELEBIT_GROUP=$my_group
export TELEBIT_PATH export TELEBIT_PATH
TELEBIT_CONFIG=$HOME/.config/$my_app.yml
# TODO check both expected sock paths in client by default
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
TELEBIT_TMP_CONFIGD=$HOME/.config/$my_daemon.yml
TELEBIT_CONFIGD=$HOME/.config/$my_daemon.yml
TELEBIT_SOCK=$HOME/.local/share/telebit/var/run/telebit.sock
else
TELEBIT_TMP_CONFIGD=$TELEBIT_TMP/etc/$my_daemon.yml
TELEBIT_CONFIGD=$TELEBIT_REAL_PATH/etc/$my_daemon.yml
TELEBIT_SOCK=$TELEBIT_REAL_PATH/var/run/telebit.sock
fi
$my_node $TELEBIT_TMP/usr/share/template-launcher.js $my_node $TELEBIT_TMP/usr/share/template-launcher.js
# TODO don't create this in TMP_PATH if it exists in TELEBIT_REAL_PATH
mkdir -p "$(dirname $TELEBIT_TMP_CONFIGD)"
if [ ! -e "$TELEBIT_CONFIGD" ]; then
echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_TMP_CONFIGD"
cat $TELEBIT_REAL_PATH/usr/share/$my_daemon.tpl.yml >> "$TELEBIT_TMP_CONFIGD"
fi
mkdir -p "$(dirname $TELEBIT_CONFIG)"
if [ ! -e "$TELEBIT_CONFIG" ]; then
echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_CONFIG"
fi
# TODO # TODO
# Backup final directory, if it exists # Backup final directory, if it exists
# Move everything over to final directory # Move everything over to final directory
# Restore config files, if they exist # Restore config files, if they exist
# rewrite system service file with real variables # rewrite system service file with real variables
# This should only affect non-USERSPACE installs
#echo "${soft_sudo_cmde}chown -R $my_user '$TELEBIT_REAL_PATH'
$soft_sudo_cmd chown -R $my_user "$TELEBIT_REAL_PATH"
# ~/.config/systemd/user/ # ~/.config/systemd/user/
# %h/.config/telebit/telebit.yml # %h/.config/telebit/telebit.yml
echo " - adding $my_app as a system service" echo " - adding $my_app as a system service"
# TODO detect with type -p # TODO detect with type -p
my_system_launcher="" my_system_launcher=""
my_app_launchd_service=""
if [ -d "/Library/LaunchDaemons" ]; then if [ -d "/Library/LaunchDaemons" ]; then
my_system_launcher="launchd" my_system_launcher="launchd"
my_app_launchd_service="Library/LaunchDaemons/${my_app_pkg_name}.plist"
echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service /$my_app_launchd_service"
$real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service" "/$my_app_launchd_service"
echo " > ${real_sudo_cmde}chown root:wheel $my_root/$my_app_launchd_service"
$real_sudo_cmd chown root:wheel "$my_root/$my_app_launchd_service" if [ "yes" == "$TELEBIT_USERSPACE" ]; then
echo " > ${real_sudo_cmde}launchctl unload -w $my_root/$my_app_launchd_service >/dev/null 2>/dev/null" my_app_launchd_service="$HOME/Library/LaunchAgents/${my_app_pkg_name}.plist"
$real_sudo_cmd launchctl unload -w "$my_root/$my_app_launchd_service" >/dev/null 2>/dev/null echo " > $rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
$rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service" "$my_app_launchd_service"
echo " > chown $(id -u -n):$(id -g -n) $my_app_launchd_service"
chown $(id -u -n):$(id -g -n) "$my_app_launchd_service"
echo " > launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
else
my_app_launchd_service="$my_root/Library/LaunchDaemons/${my_app_pkg_name}.plist"
echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
$real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service" "$my_app_launchd_service"
echo " > ${real_sudo_cmde}chown root:wheel $my_app_launchd_service"
$real_sudo_cmd chown root:wheel "$my_app_launchd_service"
echo " > ${real_sudo_cmde}launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
$real_sudo_cmd launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
fi
elif [ -d "$my_root/etc/systemd/system" ]; then elif [ -d "$my_root/etc/systemd/system" ]; then
my_system_launcher="systemd" my_system_launcher="systemd"
# TODO handle Linux userspace systemd
echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service" echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service"
$real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service" "/etc/systemd/system/$my_app.service" $real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service" "/etc/systemd/system/$my_app.service"
@ -291,78 +369,68 @@ if [ "systemd" == "$my_system_launcher" ]; then
my_stopper="${real_sudo_cmde}systemctl stop $my_app" my_stopper="${real_sudo_cmde}systemctl stop $my_app"
echo "Edit the config and restart, if desired:" echo "Edit the config and restart, if desired:"
echo "" echo ""
echo " ${real_sudo_cmde}$my_edit $TELEBIT_REAL_PATH/etc/$my_app.yml" echo " ${real_sudo_cmde}$my_edit $TELEBIT_CONFIGD"
echo " ${real_sudo_cmde}systemctl restart $my_app" echo " ${real_sudo_cmde}systemctl restart $my_app"
echo "" echo ""
echo "Or disabled the service and start manually:" echo "Or disabled the service and start manually:"
echo "" echo ""
echo " ${real_sudo_cmde}systemctl stop $my_app" echo " ${real_sudo_cmde}systemctl stop $my_app"
echo " ${real_sudo_cmde}systemctl disable $my_app" echo " ${real_sudo_cmde}systemctl disable $my_app"
echo " $my_daemon --config $TELEBIT_REAL_PATH/etc/$my_daemon.yml" echo " $my_daemon --config $TELEBIT_CONFIGD"
elif [ "launchd" == "$my_system_launcher" ]; then elif [ "launchd" == "$my_system_launcher" ]; then
my_stopper="${real_sudo_cmde}launchctl unload $my_root/$my_app_launchd_service" my_stopper="${real_sudo_cmde}launchctl unload $my_app_launchd_service"
echo "Edit the config and restart, if desired:" echo "Edit the config and restart, if desired:"
echo "" echo ""
echo " ${real_sudo_cmde}$my_edit $TELEBIT_REAL_PATH/etc/$my_app.yml" echo " ${real_sudo_cmde}$my_edit $TELEBIT_CONFGD"
echo " ${real_sudo_cmde}launchctl unload $my_root/$my_app_launchd_service" echo " ${real_sudo_cmde}launchctl unload $my_app_launchd_service"
echo " ${real_sudo_cmde}launchctl load -w $my_root/$my_app_launchd_service" echo " ${real_sudo_cmde}launchctl load -w $my_app_launchd_service"
echo "" echo ""
echo "Or disabled the service and start manually:" echo "Or disabled the service and start manually:"
echo "" echo ""
echo " ${real_sudo_cmde}launchctl unload -w $my_root/$my_app_launchd_service" echo " ${real_sudo_cmde}launchctl unload -w $my_app_launchd_service"
echo " $my_daemon --config $TELEBIT_REAL_PATH/etc/$my_daemon.yml" echo " $my_daemon --config $TELEBIT_CONFIGD"
else else
my_stopper="not started" my_stopper="not started"
echo "Edit the config, if desired:"
echo ""
echo " ${soft_sudo_cmde}$my_edit $my_config"
echo "" echo ""
echo "Run the service manually (we couldn't detect your system service to do that automatically):" echo "Run the service manually (we couldn't detect your system service to do that automatically):"
echo "" echo ""
echo " $my_daemon --config $my_config" echo " $my_daemon --config $TELEBIT_CONFIGD"
echo " $my_app --config $TELEBIT_CONFIG"
fi fi
sleep 2 sleep 2
# TODO don't create this in TMP_PATH if it exists in TELEBIT_REAL_PATH
my_config="$TELEBIT_REAL_PATH/etc/$my_daemon.yml"
mkdir -p "$(dirname $my_config)"
if [ ! -e "$my_config" ]; then
echo "sock: $TELEBIT_REAL_PATH/var/run/telebit.sock" >> "$my_config"
cat $TELEBIT_REAL_PATH/usr/share/$my_daemon.tpl.yml >> "$my_config"
fi
my_config="$HOME/.config/$my_app/$my_app.yml"
mkdir -p "$(dirname $my_config)"
if [ ! -e "$my_config" ]; then
echo "sock: $TELEBIT_REAL_PATH/var/run/telebit.sock" >> "$my_config"
fi
#echo "${soft_sudo_cmde}chown -R $my_user '$TELEBIT_REAL_PATH'
$soft_sudo_cmd chown -R $my_user "$TELEBIT_REAL_PATH"
############################### ###############################
# Actually Launch the Service # # Actually Launch the Service #
############################### ###############################
echo "" echo ""
if [ "launchd" == "$my_system_launcher" ]; then if [ "launchd" == "$my_system_launcher" ]; then
echo " > ${real_sudo_cmde}launchctl load -w $my_root/$my_app_launchd_service"
$real_sudo_cmd launchctl load -w "$my_root/$my_app_launchd_service" if [ "yes" == "$TELEBIT_USERSPACE" ]; then
echo " > launchctl load -w $my_app_launchd_service"
launchctl load -w "$my_app_launchd_service"
else
echo " > ${real_sudo_cmde}launchctl load -w $my_app_launchd_service"
$real_sudo_cmd launchctl load -w "$my_app_launchd_service"
fi
fi fi
if [ "systemd" == "$my_system_launcher" ]; then if [ "systemd" == "$my_system_launcher" ]; then
echo " > ${real_sudo_cmde}systemctl start $my_app" echo " > ${real_sudo_cmde}systemctl start $my_app"
$real_sudo_cmd systemctl restart $my_app $real_sudo_cmd systemctl restart $my_app
fi fi
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app"
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon"
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon
echo " > telebit init --tty" echo " > telebit init --tty"
echo "" echo ""
sleep 0.25 sleep 0.25