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

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