From dc38b1d15df2d07f65c0f3c8f8f944fbcc89a574 Mon Sep 17 00:00:00 2001
From: "Paul Emm. Katsoulakis"
 <34388743+paulkatsoulakis@users.noreply.github.com>
Date: Tue, 13 Aug 2019 14:40:38 +0300
Subject: [PATCH] netdata/installer: Fix error running kickstart as a
 non-privileged user (#6642)

* netdata/packaging: fix kickstart clean up process and others
1) when TMPDIR fails to be created (cd wont work), fail over to /tmp otherwise its going to write on / which is, well, wrong (we had rm based on TMPDIR)
2) make sure we remove the directories too when clean up and always include sudo variable, to handle situations of non-root executions
3) When deleting the dir, be extra sure it's not / or the /tmp itself

* netdata/packaging: sh friendly conditionals

* netdata/packaging: fix md5sum on documentation

* netdata/installer: enter the tmpdir in the failover case too

* netdata/installer: simplify - we dont expect mktemp to fail, if it does then something is wrong so dont make cd a soft error. simplify conditionals also

* netdata/installer: update md5sum
---
 packaging/installer/README.md             | 4 ++--
 packaging/installer/kickstart-static64.sh | 8 +++++---
 packaging/installer/kickstart.sh          | 6 ++++--
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/packaging/installer/README.md b/packaging/installer/README.md
index b80bdfd897..5b2a9c4e5a 100644
--- a/packaging/installer/README.md
+++ b/packaging/installer/README.md
@@ -45,7 +45,7 @@ $ bash <(curl -Ss https://my-netdata.io/kickstart.sh)
 Verify the integrity of the script with this:
 
 ``` bash
-[ "8a2b054081a108dff915994ce77f2f2d" = "$(curl -Ss https://my-netdata.io/kickstart.sh | md5sum | cut -d ' ' -f 1)" ] && echo "OK, VALID" || echo "FAILED, INVALID"
+[ "b6d16c171ccad073b86327246151d875" = "$(curl -Ss https://my-netdata.io/kickstart.sh | md5sum | cut -d ' ' -f 1)" ] && echo "OK, VALID" || echo "FAILED, INVALID"
 ```
 *It should print `OK, VALID` if the script is the one we ship.*
 
@@ -101,7 +101,7 @@ $ bash <(curl -Ss https://my-netdata.io/kickstart-static64.sh)
 Verify the integrity of the script with this:
 
 ```bash
-[ "8779d8717ccaa8dac18d599502eef591" = "$(curl -Ss https://my-netdata.io/kickstart-static64.sh | md5sum | cut -d ' ' -f 1)" ] && echo "OK, VALID" || echo "FAILED, INVALID"
+[ "4415e8c13e529a795abb953a9be14ad5" = "$(curl -Ss https://my-netdata.io/kickstart-static64.sh | md5sum | cut -d ' ' -f 1)" ] && echo "OK, VALID" || echo "FAILED, INVALID"
 ```
 
 *It should print `OK, VALID` if the script is the one we ship.*
diff --git a/packaging/installer/kickstart-static64.sh b/packaging/installer/kickstart-static64.sh
index a9f11238c2..34ca116da7 100755
--- a/packaging/installer/kickstart-static64.sh
+++ b/packaging/installer/kickstart-static64.sh
@@ -189,7 +189,7 @@ done
 
 # ---------------------------------------------------------------------------------------------------------------------
 TMPDIR=$(create_tmp_directory)
-cd "${TMPDIR}" || :
+cd "${TMPDIR}"
 
 set_tarball_urls "${RELEASE_CHANNEL}"
 progress "Downloading static netdata binary: ${NETDATA_TARBALL_URL}"
@@ -202,12 +202,14 @@ fi
 
 # ---------------------------------------------------------------------------------------------------------------------
 progress "Installing netdata"
-
 run ${sudo} sh "${TMPDIR}/netdata-latest.gz.run" ${opts} ${inner_opts}
 
 #shellcheck disable=SC2181
 if [ $? -eq 0 ]; then
-	rm "${TMPDIR}/netdata-latest.gz.run"
+	run ${sudo} rm "${TMPDIR}/netdata-latest.gz.run"
+	if [ ! "${TMPDIR}" = "/" ] && [ -d "${TMPDIR}" ]; then
+		run ${sudo} rm -rf "${TMPDIR}"
+	fi
 else
 	echo >&2 "NOTE: did not remove: ${TMPDIR}/netdata-latest.gz.run"
 fi
diff --git a/packaging/installer/kickstart.sh b/packaging/installer/kickstart.sh
index d396f139e3..bab95fda2e 100755
--- a/packaging/installer/kickstart.sh
+++ b/packaging/installer/kickstart.sh
@@ -304,7 +304,7 @@ if [ "${INTERACTIVE}" = "0" ]; then
 fi
 
 TMPDIR=$(create_tmp_directory)
-cd ${TMPDIR} || :
+cd "${TMPDIR}"
 
 dependencies
 
@@ -328,7 +328,9 @@ cd netdata-* || fatal "Cannot cd to netdata source tree"
 if [ -x netdata-installer.sh ]; then
 	progress "Installing netdata..."
 	run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || fatal "netdata-installer.sh exited with error"
-	rm -rf "${TMPDIR}" >/dev/null 2>&1
+	if [ -d "${TMPDIR}" ] && [ ! "${TMPDIR}" = "/" ]; then
+		run ${sudo} rm -rf "${TMPDIR}" >/dev/null 2>&1
+	fi
 else
 	fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh). Leaving all files in ${TMPDIR}"
 fi