From 652c7e581f0592d2ab2d5080af7c3f86c541e600 Mon Sep 17 00:00:00 2001
From: Michal Karm Babacek <karm@redhat.com>
Date: Mon, 10 Aug 2015 11:59:55 +0200
Subject: [PATCH] JWS-155

---
 jws-application-servers.sunbuild | 87 +++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 36 deletions(-)

diff --git a/jws-application-servers.sunbuild b/jws-application-servers.sunbuild
index df01e4e..45a67ae 100644
--- a/jws-application-servers.sunbuild
+++ b/jws-application-servers.sunbuild
@@ -102,8 +102,8 @@ if [ ".\`id -nu\`" = .root ]
 then
     SUGID=root
 else
-    "This script must bust be run as superuser" >&2
-    exit 1
+    SUGID="\`id -nu\`"
+    echo "WARNING: This script should be run as superuser to create user \\\`tomcat' and directories in \\\`/var/'." >&2
 fi
 
 check_java_home="no"
@@ -171,9 +171,9 @@ fi
 #
 if [ -d "\$INSTALL_ROOT" ]
 then
-    if [ -f "\$INSTALL_ROOT/etc/.postinstall.done" ]
+    if [ -f "\$INSTALL_ROOT/etc/.postinstall.tomcat.done" ]
     then
-        "Package \\\`$XB_BUILD_PKG' already installed in : \\\`\$INSTALL_ROOT'" >&2
+        echo "Skipping post install. Package is already installed in : \\\`\$INSTALL_ROOT'" >&2
         exit 17
     else
     (
@@ -194,40 +194,48 @@ else
 fi
 export LD_LIBRARY_PATH
 
+# Privileged execution
 # Add the "tomcat" user
-if [ ".\$tomcat_uid" = . ]; then
-    tomcat_uid=91
-    tomcat_uiv=true
-else
-    tomcat_uiv=false
-fi
-
-tomcat_osu="\`id -u tomcat 2>/dev/null\`"
-tomcat_osg="\`id -g tomcat 2>/dev/null\`"
-if [ ".\$tomcat_osg" = . ]; then
-    tomcat_osg=\$tomcat_uid
-    /usr/sbin/groupadd -g \$tomcat_osg tomcat 2> /dev/null || true
-    if [ \$? -eq 0 ]; then
-        echo "Tomcat group (id=\$tomcat_osg) created."
+if [ ".\$SUGID" = ".root" ]
+then
+    # Add the "tomcat" user
+    if [ ".\$tomcat_uid" = . ]; then
+        tomcat_uid=91
+        tomcat_uiv=true
+    else
+        tomcat_uiv=false
     fi
-elif \$tomcat_uiv; then
-    echo "Tomcat group (id=\$tomcat_osu) already exists."
-fi
-
-createbasedir "\$INSTALL_ROOT$_localstatedir/tomcat"
-dirattributes 0775 root tomcat "\$INSTALL_ROOT$_localstatedir/tomcat"
-
-if [ ".\$tomcat_osu" = . ]; then
-    tomcat_osu=\$tomcat_uid
-    /usr/sbin/useradd -c "Apache Tomcat" -u \$tomcat_osu -g tomcat \\
-        -s /bin/sh -m -d "\$INSTALL_ROOT$_localstatedir/tomcat" tomcat 2> /dev/null || true
-    if [ \$? -eq 0 ]; then
-        echo "Tomcat  user (id=\$tomcat_osu) created."
+    
+    tomcat_osu="\`id -u tomcat 2>/dev/null\`"
+    tomcat_osg="\`id -g tomcat 2>/dev/null\`"
+    if [ ".\$tomcat_osg" = . ]; then
+        tomcat_osg=\$tomcat_uid
+        /usr/sbin/groupadd -g \$tomcat_osg tomcat 2> /dev/null || true
+        if [ \$? -eq 0 ]; then
+            echo "Tomcat group (id=\$tomcat_osg) created."
+        fi
+    elif \$tomcat_uiv; then
+        echo "Tomcat group (id=\$tomcat_osu) already exists."
+    fi
+    
+    createbasedir "\$INSTALL_ROOT$_localstatedir/tomcat"
+    dirattributes 0775 root tomcat "\$INSTALL_ROOT$_localstatedir/tomcat"
+    
+    if [ ".\$tomcat_osu" = . ]; then
+        tomcat_osu=\$tomcat_uid
+        /usr/sbin/useradd -c "Apache Tomcat" -u \$tomcat_osu -g tomcat \\
+            -s /bin/sh -m -d "\$INSTALL_ROOT$_localstatedir/tomcat" tomcat 2> /dev/null || true
+        if [ \$? -eq 0 ]; then
+            echo "Tomcat  user (id=\$tomcat_osu) created."
+        fi
+    elif \$tomcat_uiv; then
+        echo "Tomcat user  (id=\$tomcat_osu) already exists."
     fi
-elif \$tomcat_uiv; then
-    echo "Tomcat user  (id=\$tomcat_osu) already exists."
+# Unprivileged execution
+else
+    echo "WARNING: Not a superuser. User and group \\\`tomcat' will not be created. User \\\`\$SUGID' used instead." >&2
+    echo "WARNING: Not a superuser. Directories in \\\`/var/' will not be created. Using \\\`\$INSTALL_ROOT/var/' instead." >&2
 fi
-
 EOF
 
 cat "$RPM_ROOT_DIR/etc/.postinstall.tomcat7" >> .postinstall.tomcat
@@ -240,9 +248,14 @@ cat << EOF >> .postinstall.tomcat
 #
 # Configure Solaris runtime linker environment
 #
-if [ -x /usr/bin/crle ]
+if [ ".\$SUGID" = ".root" ]
 then
-    /usr/bin/crle -u $crlo \$INSTALL_ROOT$_libdir
+    if [ -x /usr/bin/crle ]
+    then
+        /usr/bin/crle -u $crlo \$INSTALL_ROOT$_libdir
+    fi
+else
+    echo "WARNING: Not a superuser. Solaris runtime linker environment \\\`crle' was not updated." >&2
 fi
 
 if [ "\$check_java_home" = "yes" ]
@@ -263,6 +276,8 @@ then
     fi
 fi
 
+touch "\$INSTALL_ROOT/etc/.postinstall.tomcat.done"
+echo "Done"
 # End of $summary post install script
 EOF
 
-- 
1.9.3

