cd9363
From 1919a8ab86c99b47ba86dc697abcdf3343b0aafa Mon Sep 17 00:00:00 2001
cd9363
From: Jan Grulich <jgrulich@redhat.com>
cd9363
Date: Tue, 1 Feb 2022 14:31:05 +0100
cd9363
Subject: Add vncsession-restore script to restore SELinux context
cd9363
cd9363
The vncsession-restore script is used in the ExecStartPre option
cd9363
for systemd service file in order to properly start the session
cd9363
in case the policy is updated (e.g. after Tigervnc update).
cd9363
cd9363
diff --git a/unix/vncserver/CMakeLists.txt b/unix/vncserver/CMakeLists.txt
cd9363
index ae69dc09..04eb6fc4 100644
cd9363
--- a/unix/vncserver/CMakeLists.txt
cd9363
+++ b/unix/vncserver/CMakeLists.txt
cd9363
@@ -2,6 +2,7 @@ add_executable(vncsession vncsession.c)
cd9363
 target_link_libraries(vncsession ${PAM_LIBS} ${SELINUX_LIBS})
cd9363
 
cd9363
 configure_file(vncserver@.service.in vncserver@.service @ONLY)
cd9363
+configure_file(vncsession-restore.in vncsession-restore @ONLY)
cd9363
 configure_file(vncsession-start.in vncsession-start @ONLY)
cd9363
 configure_file(vncserver.in vncserver @ONLY)
cd9363
 configure_file(vncsession.man.in vncsession.man @ONLY)
cd9363
@@ -20,4 +21,5 @@ install(FILES HOWTO.md DESTINATION ${CMAKE_INSTALL_FULL_DOCDIR})
cd9363
 if(INSTALL_SYSTEMD_UNITS)
cd9363
   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/vncserver@.service DESTINATION ${CMAKE_INSTALL_FULL_UNITDIR})
cd9363
   install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/vncsession-start DESTINATION ${CMAKE_INSTALL_FULL_LIBEXECDIR})
cd9363
+  install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/vncsession-restore DESTINATION ${CMAKE_INSTALL_FULL_LIBEXECDIR})
cd9363
 endif()
cd9363
diff --git a/unix/vncserver/vncserver@.service.in b/unix/vncserver/vncserver@.service.in
cd9363
index 39f81b73..a83e05a3 100644
cd9363
--- a/unix/vncserver/vncserver@.service.in
cd9363
+++ b/unix/vncserver/vncserver@.service.in
cd9363
@@ -35,6 +35,7 @@ After=syslog.target network.target
cd9363
 
cd9363
 [Service]
cd9363
 Type=forking
cd9363
+ExecStartPre=+@CMAKE_INSTALL_FULL_LIBEXECDIR@/vncsession-restore %i
cd9363
 ExecStart=@CMAKE_INSTALL_FULL_LIBEXECDIR@/vncsession-start %i
cd9363
 PIDFile=/run/vncsession-%i.pid
cd9363
 SELinuxContext=system_u:system_r:vnc_session_t:s0
cd9363
diff --git a/unix/vncserver/vncsession-restore.in b/unix/vncserver/vncsession-restore.in
cd9363
new file mode 100644
cd9363
index 00000000..d3abc57d
cd9363
--- /dev/null
cd9363
+++ b/unix/vncserver/vncsession-restore.in
cd9363
@@ -0,0 +1,68 @@
cd9363
+#!/bin/bash
cd9363
+#
cd9363
+#  Copyright 2022 Jan Grulich <jgrulich@redhat.com>
cd9363
+#
cd9363
+#  This is free software; you can redistribute it and/or modify
cd9363
+#  it under the terms of the GNU General Public License as published by
cd9363
+#  the Free Software Foundation; either version 2 of the License, or
cd9363
+#  (at your option) any later version.
cd9363
+#
cd9363
+#  This software is distributed in the hope that it will be useful,
cd9363
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
cd9363
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cd9363
+#  GNU General Public License for more details.
cd9363
+#
cd9363
+#  You should have received a copy of the GNU General Public License
cd9363
+#  along with this software; if not, write to the Free Software
cd9363
+#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
cd9363
+#  USA.
cd9363
+#
cd9363
+
cd9363
+USERSFILE="@CMAKE_INSTALL_FULL_SYSCONFDIR@/tigervnc/vncserver.users"
cd9363
+
cd9363
+if [ $# -ne 1 ]; then
cd9363
+	echo "Syntax:" >&2
cd9363
+	echo "    $0 <display>" >&2
cd9363
+	exit 1
cd9363
+fi
cd9363
+
cd9363
+if [ ! -f "${USERSFILE}" ]; then
cd9363
+	echo "Users file ${USERSFILE} missing" >&2
cd9363
+	exit 1
cd9363
+fi
cd9363
+
cd9363
+DISPLAY="$1"
cd9363
+
cd9363
+USER=`grep "^ *${DISPLAY}=" "${USERSFILE}" 2>/dev/null | head -1 | cut -d = -f 2- | sed 's/ *$//g'`
cd9363
+
cd9363
+if [ -z "${USER}" ]; then
cd9363
+	echo "No user configured for display ${DISPLAY}" >&2
cd9363
+	exit 1
cd9363
+fi
cd9363
+
cd9363
+USER_HOMEDIR=`getent passwd ${USER} | cut -f6 -d:`
cd9363
+
cd9363
+if [ -z "${USER_HOMEDIR}" ]; then
cd9363
+	echo "Failed to get home directory for ${USER}" >&2
cd9363
+	exit 1
cd9363
+fi
cd9363
+
cd9363
+if [ ! -d "${USER_HOMEDIR}/.vnc" ]; then
cd9363
+	exit 0
cd9363
+fi
cd9363
+
cd9363
+MATCHPATHCON=`which matchpathcon`
cd9363
+
cd9363
+if [ $? -eq 0 ]; then
cd9363
+	${MATCHPATHCON} -V "${USER_HOMEDIR}/.vnc" &>/dev/null
cd9363
+	if [ $? -eq 0 ]; then
cd9363
+		exit 0
cd9363
+	fi
cd9363
+fi
cd9363
+
cd9363
+RESTORECON=`which restorecon`
cd9363
+
cd9363
+if [ $? -eq 0 ]; then
cd9363
+	exec "${RESTORECON}" -R "${USER_HOMEDIR}/.vnc" >&2
cd9363
+	return $?
cd9363
+fi