[X2Go-Commits] [x2goserver] 01/08: x2goserver/bin/x2gostartagent: fetch hostname via "hostname -s" and do not rely on the HOSTNAME variable.

git-admin at x2go.org git-admin at x2go.org
Wed Oct 11 08:17:34 CEST 2017


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch release/4.0.1.x
in repository x2goserver.

commit b73480d381289b8fa8a4ae024f0f2a64b0e44928
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Wed Oct 11 06:56:07 2017 +0200

    x2goserver/bin/x2gostartagent: fetch hostname via "hostname -s" and do not rely on the HOSTNAME variable.
    
    The latter is only set automatically by bash if it's not already part of
    the environment.
    
    We might get "garbage" in this way (and one user actually did.)
---
 debian/changelog              |  4 ++++
 x2goserver/bin/x2gostartagent | 46 ++++++++++++++++++++++++++++++++-----------
 2 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 61051c9..e1964a7 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -29,6 +29,10 @@ x2goserver (4.0.1.21-0x2go1) UNRELEASED; urgency=medium
       loginctl utility before calling x2goagent. Fixes: #1198.
     - x2goserver-xsession/etc/Xsession: support Devuan just like Debian, give
       useful error message in case the OS is unknown.
+    - x2goserver/bin/x2gostartagent: fetch hostname via "hostname -s" and do
+      not rely on the HOSTNAME variable. The latter is only set automatically
+      by bash if it's not already part of the environment. We might get
+      "garbage" in this way (and one user actually did.)
   * x2goserver.spec:
     - Add mandatory perl-generators Build-Requires as per
       https://fedoraproject.org/wiki/Changes/Build_Root_Without_Perl
diff --git a/x2goserver/bin/x2gostartagent b/x2goserver/bin/x2gostartagent
index 5ac265c..96783e7 100755
--- a/x2goserver/bin/x2gostartagent
+++ b/x2goserver/bin/x2gostartagent
@@ -119,6 +119,31 @@ elif ! echo $HOME | iconv -f ASCII -t ASCII 1>/dev/null 2>/dev/null; then
 	exit -5
 fi
 
+# ${HOSTNAME} should be automatically set by bash via gethostname(2), IFF this
+# variable is not already set in the environment.
+#
+# This leads to two problems:
+#   - export HOSTNAME="malbox"; x2gostartagent will override the actual system
+#     host name and lead to authorization failures when connecting to
+#     x2goagent/nxagent later on.
+#   - even if the above is not the case, the value returned by gethostname(2)
+#     could either be a FQDN, the short name or anything in between. glibc
+#     seems to return the short name on Linux, since it calls uname(2), which
+#     typically does not include a domain, but *BSD seems to default to
+#     the FQDN. We explicitly need the short name.
+#
+# Workaround: use hostname -s, which luckily is portable enough to be available
+# on a wide variety of systems.
+typeset current_host_name=""
+current_host_name="$(hostname -s)"
+
+if [[ "${?}" -ne "0" ]]; then
+	typeset msg="Unable to retrieve machine's hostname. This is required. Aborting session startup."
+	echo "${msg}"
+	"${X2GO_LIB_PATH}/x2gosyslog" "${0}" "err" "${msg}"
+	exit 1
+fi
+
 X2GO_ROOT="${HOME}/.x2go"
 export NX_ROOT=$X2GO_ROOT
 
@@ -204,8 +229,8 @@ if [ "$X2GO_STYPE" == "S" ]; then
 		X2GO_PORT=`echo $OUTPUT | awk '{print $1}'`
 
 		$X2GO_LIB_PATH/x2gosyslog "$0" "debug" "received shadow session information: cookie: $X2GO_COOKIE, port: $X2GO_PORT"
-		xauth -f "$XAUTHORITY" add "${HOSTNAME}/unix:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
-		xauth -f "$XAUTHORITY" add "${HOSTNAME}:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
+		xauth -f "$XAUTHORITY" add "${current_host_name}/unix:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
+		xauth -f "$XAUTHORITY" add "${current_host_name}:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
 
 		echo $X2GO_PORT
 		echo $X2GO_COOKIE
@@ -246,7 +271,7 @@ elif  [ "$X2GO_STYPE" == "S" ]; then
 fi
 
 if [ "$X2GO_CLIENT" == "" ]; then
-	X2GO_CLIENT="$HOSTNAME"
+	X2GO_CLIENT="${current_host_name}"
 fi
 
 # define the full path to the ss utility
@@ -255,11 +280,11 @@ ss=$(PATH="$PATH:/usr/sbin:/sbin" type -P ss);
 while [ "$OUTPUT"  != "inserted" ]; do
 
 	typeset -a used_displays
-	IFS='' read -ar used_displays < <("${X2GO_LIB_PATH}/x2gogetdisplays" "${HOSTNAME}")
+	IFS='' read -ar used_displays < <("${X2GO_LIB_PATH}/x2gogetdisplays" "${current_host_name}")
 
 	#Get all used in system ports from X2Go database and ss output
 	USED_PORTS=$(
-	    "$X2GO_LIB_PATH/x2gogetports" "$HOSTNAME";
+	    "$X2GO_LIB_PATH/x2gogetports" "${current_host_name}";
 	    "$ss" -nt -all |
 	    awk '{ n=split($0,lines,"\n"); for(i=1;i<=n;i++){split (lines[i],words," ");delim=split(words[4],ports,":"); if(delim>1)printf ("|%s|\n",ports[delim])} }';
 	);
@@ -307,7 +332,7 @@ while [ "$OUTPUT"  != "inserted" ]; do
 		# sanitize session name
 		SESSION_NAME=`echo "$SESSION_NAME" | perl -pe  "s/[^a-zA-Z0-9\.\_\-\@]//g"`
 
-		OUTPUT=`$X2GO_LIB_PATH/x2goinsertsession "$X2GO_PORT" "$HOSTNAME" "$SESSION_NAME"`
+		OUTPUT=`$X2GO_LIB_PATH/x2goinsertsession "$X2GO_PORT" "${current_host_name}" "$SESSION_NAME"`
 	fi 
 done
 
@@ -319,7 +344,7 @@ while [ "$GR_PORT" == "" ] || [ "$SOUND_PORT" == "" ] || [ "$FS_PORT" == "" ]; d
 
 		#Get all used in system ports from X2Go database and ss output
 		USED_PORTS=$(
-		    "$X2GO_LIB_PATH/x2gogetports" "$HOSTNAME";
+		    "$X2GO_LIB_PATH/x2gogetports" "${current_host_name}";
 		    "$ss" -nt -all |
 		    awk '{ n=split($0,lines,"\n"); for(i=1;i<=n;i++){split (lines[i],words," ");delim=split(words[4],ports,":"); if(delim>1)printf ("|%s|\n",ports[delim])} }';
 		);
@@ -330,7 +355,7 @@ while [ "$GR_PORT" == "" ] || [ "$SOUND_PORT" == "" ] || [ "$FS_PORT" == "" ]; d
 		#check if port in /etc/services
 		SERV=`grep $SSH_PORT /etc/services`
 		if [ "$SERV" == "" ]; then
-			OUTPUT=`$X2GO_LIB_PATH/x2goinsertport "$HOSTNAME" "$SESSION_NAME" "$SSH_PORT"`
+			OUTPUT=`$X2GO_LIB_PATH/x2goinsertport "${current_host_name}" "$SESSION_NAME" "$SSH_PORT"`
 		fi
 	done
 
@@ -402,13 +427,12 @@ grep PPid /proc/$PPID/status > ${SESSION_DIR}/sshd.pid
 
 X2GO_COOKIE=`mcookie`
 
-
 PATH="${PATH}:${X2GO_BIN}/"
 export PATH
 
 
-xauth -f "$XAUTHORITY" add "${HOSTNAME}/unix:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
-xauth -f "$XAUTHORITY" add "${HOSTNAME}:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
+xauth -f "$XAUTHORITY" add "${current_host_name}/unix:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
+xauth -f "$XAUTHORITY" add "${current_host_name}:${X2GO_PORT}" MIT-MAGIC-COOKIE-1 "${X2GO_COOKIE}"
 
 
 option_geometry=""

--
Alioth's /srv/git/code.x2go.org/x2goserver.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goserver.git


More information about the x2go-commits mailing list