Blame SOURCES/tigervnc-vncsession-restore-script-systemd-service.patch

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