From 4ef257d5ed4a2ccd09798c7760009954e57e2c3e Mon Sep 17 00:00:00 2001
From: Michal Karm Babacek <karm@redhat.com>
Date: Mon, 10 Aug 2015 12:04:25 +0200
Subject: [PATCH] JWS-155

---
 tomcat8.sunbuild | 74 ++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 53 insertions(+), 21 deletions(-)

diff --git a/tomcat8.sunbuild b/tomcat8.sunbuild
index 672b1e7..908ffaf 100644
--- a/tomcat8.sunbuild
+++ b/tomcat8.sunbuild
@@ -70,27 +70,59 @@ EOS
 fi
 
 test ".\$INSTALL_ROOT" = "." && INSTALL_ROOT="\`cd .. && pwd;\`"
-createbasedir /var/log/tomcat8
-for i in temp work
-do
-    createbasedir /var/cache/tomcat8/\$i
-    dirattributes 0775 root tomcat /var/cache/tomcat8/\$i
-done
-dirattributes 0775 root tomcat /var/cache/tomcat8
-setattributes 0775 root tomcat /var/log/tomcat8
-setattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat8
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat8/webapps
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat8/server/webapps
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat8/conf
-setattributes 0660 root tomcat \$INSTALL_ROOT/share/tomcat8/conf/tomcat-users.xml
-if [ -d "\$INSTALL_ROOT/share/tomcat8" ]; then
-    cd "\$INSTALL_ROOT/share/tomcat8"
-    createsymlink /var/log/tomcat8 logs
-    createsymlink /var/cache/tomcat8/temp temp
-    createsymlink /var/cache/tomcat8/work work
-    cd bin
-    createsymlink ../../java/commons-daemon-1.0.15-redhat-1.jar commons-daemon.jar
-    cd ..
+# Privileged execution
+if [ ".$SUGID" = ".root" ]
+then
+    createbasedir "/var/log/tomcat8"
+    for i in temp work
+    do
+        createbasedir "/var/cache/tomcat8/\$i"
+        dirattributes 0775 root tomcat "/var/cache/tomcat8/\$i"
+    done
+    dirattributes 0775 root tomcat "/var/cache/tomcat8"
+    setattributes 0775 root tomcat "/var/log/tomcat8"
+    setattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat8"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat8/webapps"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat8/server/webapps"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat8/conf"
+    setattributes 0660 root tomcat "\$INSTALL_ROOT/share/tomcat8/conf/tomcat-users.xml"
+    if [ -d "\$INSTALL_ROOT/share/tomcat8" ]; then
+        cd "\$INSTALL_ROOT/share/tomcat8"
+        createsymlink "/var/log/tomcat8" logs
+        createsymlink "/var/cache/tomcat8/temp" temp
+        createsymlink "/var/cache/tomcat8/work" work
+        cd bin
+        createsymlink '../../java/commons-daemon-*.jar' commons-daemon.jar
+        cd ..
+    fi
+# Unprivileged execution
+else
+        createbasedir "\$INSTALL_ROOT/var/log/tomcat8"
+        for i in temp work
+        do
+            createbasedir "\$INSTALL_ROOT/var/cache/tomcat8/\$i"
+            dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/cache/tomcat8/\$i"
+        done
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/cache/tomcat8"
+        setattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/log/tomcat8"
+        setattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat8"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat8/webapps"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat8/server/webapps"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat8/conf"
+        setattributes 0660 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat8/conf/tomcat-users.xml"
+        if [ -d "\$INSTALL_ROOT/share/tomcat8" ]; then
+            cd "\$INSTALL_ROOT/share/tomcat8"
+            createsymlink "\$INSTALL_ROOT/var/log/tomcat8" logs
+            createsymlink "\$INSTALL_ROOT/var/cache/tomcat8/temp" temp
+            createsymlink "\$INSTALL_ROOT/var/cache/tomcat8/work" work
+            cd bin
+            createsymlink '../../java/commons-daemon-*.jar' commons-daemon.jar
+            cd ..
+        fi
+
+        # Unprivileged execution, set current user as TOMCAT_USER
+        echo "WARNING: Not a superuser. User \\\`\$SUGID' used as TOMCAT_USER in \\\`\$INSTALL_ROOT/etc/sysconfig/tomcat8'." >&2
+        echo "TOMCAT_USER=\$SUGID" >> "\$INSTALL_ROOT/etc/sysconfig/tomcat8"
 fi
 
 for i in \$INSTALL_ROOT$_sbindir/tomcat8 \\
-- 
1.9.3

