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

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

diff --git a/tomcat7.sunbuild b/tomcat7.sunbuild
index eb9a720..bf9a7cd 100644
--- a/tomcat7.sunbuild
+++ b/tomcat7.sunbuild
@@ -70,27 +70,59 @@ EOS
 fi
 
 test ".\$INSTALL_ROOT" = "." && INSTALL_ROOT="\`cd .. && pwd;\`"
-createbasedir /var/log/tomcat7
-for i in temp work
-do
-    createbasedir /var/cache/tomcat7/\$i
-    dirattributes 0775 root tomcat /var/cache/tomcat7/\$i
-done
-dirattributes 0775 root tomcat /var/cache/tomcat7
-setattributes 0775 root tomcat /var/log/tomcat7
-setattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat7
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat7/webapps
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat7/server/webapps
-dirattributes 0775 root tomcat \$INSTALL_ROOT/share/tomcat7/conf
-setattributes 0660 root tomcat \$INSTALL_ROOT/share/tomcat7/conf/tomcat-users.xml
-if [ -d "\$INSTALL_ROOT/share/tomcat7" ]; then
-    cd "\$INSTALL_ROOT/share/tomcat7"
-    createsymlink /var/log/tomcat7 logs
-    createsymlink /var/cache/tomcat7/temp temp
-    createsymlink /var/cache/tomcat7/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/tomcat7"
+    for i in temp work
+    do
+        createbasedir "/var/cache/tomcat7/\$i"
+        dirattributes 0775 root tomcat "/var/cache/tomcat7/\$i"
+    done
+    dirattributes 0775 root tomcat "/var/cache/tomcat7"
+    setattributes 0775 root tomcat "/var/log/tomcat7"
+    setattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat7"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat7/webapps"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat7/server/webapps"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT/share/tomcat7/conf"
+    setattributes 0660 root tomcat "\$INSTALL_ROOT/share/tomcat7/conf/tomcat-users.xml"
+    if [ -d "\$INSTALL_ROOT/share/tomcat7" ]; then
+        cd "\$INSTALL_ROOT/share/tomcat7"
+        createsymlink "/var/log/tomcat7" logs
+        createsymlink "/var/cache/tomcat7/temp" temp
+        createsymlink "/var/cache/tomcat7/work" work
+        cd bin
+        createsymlink '../../java/commons-daemon-*.jar' commons-daemon.jar
+        cd ..
+    fi
+# Unprivileged execution
+else
+        createbasedir "\$INSTALL_ROOT/var/log/tomcat7"
+        for i in temp work
+        do
+            createbasedir "\$INSTALL_ROOT/var/cache/tomcat7/\$i"
+            dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/cache/tomcat7/\$i"
+        done
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/cache/tomcat7"
+        setattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/var/log/tomcat7"
+        setattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat7"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat7/webapps"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat7/server/webapps"
+        dirattributes 0775 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat7/conf"
+        setattributes 0660 \$SUGID \$SUGID "\$INSTALL_ROOT/share/tomcat7/conf/tomcat-users.xml"
+        if [ -d "\$INSTALL_ROOT/share/tomcat7" ]; then
+            cd "\$INSTALL_ROOT/share/tomcat7"
+            createsymlink "\$INSTALL_ROOT/var/log/tomcat7" logs
+            createsymlink "\$INSTALL_ROOT/var/cache/tomcat7/temp" temp
+            createsymlink "\$INSTALL_ROOT/var/cache/tomcat7/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/tomcat7'." >&2
+        echo "TOMCAT_USER=\$SUGID" >> "\$INSTALL_ROOT/etc/sysconfig/tomcat7"
 fi
 
 for i in \$INSTALL_ROOT$_sbindir/tomcat7 \\
-- 
1.9.3

