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