store | blogs | forums | twitter | facebook | wiki | mailing lists | downloads | support portal
Atomic Secure Linux
It is currently Sun May 19, 2013 10:27 am

» Feed - Atomicorp

All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic Share/Bookmark  [ 1 post ] 
Author Message
 Post subject: Apache user from php
Unread postPosted: Mon Jan 26, 2009 7:18 pm 
Offline
Forum Regular
Forum Regular

Joined: Mon Oct 29, 2007 6:51 pm
Posts: 606
Hello,

I have a fairly simple script I wrote to fix the ownership on apache files to the real web (ftp) user so that they can manage the files perhaps it could be helpful to some one else.

So far as I know it works good, but has one fatal flaw - if the file name has a space in it then the chown command will fail - I'm not sure how to do that nicely and suggestions would be appreciated.

Code:
#!/bin/sh

## Version 1.0
#   By Amin Taheri
#   01/26/2009
#
#  This is a script used to fix ownership of files that are incorrectly owned by the web user
#  from applications such as Joomla where the web site writes files to the disk
#
#   $FIXALL is a basic blind fix - it will just re-chown everything except for plesk-stat when
#   set to 1 (on).  When set to 0 (off) it will do a search for files owned by the web user
#   and only change those files - may be safer to do this way until you fully trust the script
#
#   $DRYRUN is basically test mode - it will just output what it will do instead of actually
#   doing it - no changes are made in this regardless of what fixall is set to
#
#   $PSACONF is the path to your psa.conf file
#   
#   $WEBUSER is set to "apache"  - the user the web server runs as - if yours is different
#   make sure to change that value or this script will not do anything
#
##


#Blindly fix everything?
FIXALL="0"   # 1 = yes | 0 = No - Set Default value

#Detect problems only, dont actually do anything - basically run in test mode
DRYRUN="1"   # 1 = yes | 0 = No - Set Default value

#Path to psa.conf
PSACONF=/etc/psa/psa.conf

#Web User
WEBUSER='apache'

#Temp file to use -- it wil be deleted at the end of the script anyways
TMPFILE=/tmp/apache-user-fix-`date +%m-%d-%y`

#------------- Dont edit beyond this

#Group for Web files - this shouldnt change
GROUP='psacln'

#Determine the Vhost Root
VHOSTROOT=`grep HTTPD_VHOSTS_D $PSACONF | awk '{print $2}'`


#Get List of domains
DOMAINS=$(mysql -Ns -uadmin -p`cat /etc/psa/.psa.shadow` -Dpsa -e"select name from domains where htype='vrt_hst' ")
clear

function CheckDomain
{

   domain=$1
   echo -ne "Processing $domain: "
   
   SUBDOMAINECHO="1"

   #Get FTP user for the domain
   USER=$(mysql -Ns -uadmin -p`cat /etc/psa/.psa.shadow` -Dpsa -e"select u.login from sys_users u, domains d, hosting h where h.sys_user_id = u.id and h.dom_id = d.id and d.name = '$domain'")

   #Blindly change owner on everything?
   if [ "$FIXALL" == "1" ]; then
      
      if [ "$DRYRUN" == "1" ]; then
         echo -ne "\tDry Run: Would run:"
         echo
         echo -e "\tchown -R $USER:$GROUP `ls $VHOSTROOT/$domain/httpdocs | grep -v plesk-stat`"
      else
         echo -ne " - Fixed"
         chown -R $USER:$GROUP `ls $VHOSTROOT/$domain/httpdocs | grep -v plesk-stat`
      fi
      
   else
      find $VHOSTROOT/$domain/httpdocs -user $WEBUSER > $TMPFILE

      #if the domain has files in the temp list then fix those
      if [ `grep -c $domain $TMPFILE` -gt 0 ]; then
      
         if [ "$DRYRUN" == "1" ]; then
         echo -ne "\tDry Run: Would run:"
         echo
            for file in `grep $domain $TMPFILE`; do
               echo -e "\tchown -R $USER:$GROUP \"$file\""
            done
         else
         echo -ne " - Fixed"
            for file in `grep $domain $TMPFILE`; do
               chown -R $USER:$GROUP "$file"
            done
         fi
      
      else
         echo -ne " - Skipped"
      fi
   fi
   echo

   #Get list of sub domains for a given domain name
   SUBDOMAINS=$(mysql -Ns -uadmin -p`cat /etc/psa/.psa.shadow` -Dpsa -e"select s.name from subdomains s, domains d where d.id = s.dom_id and d.name='$domain'")
   
   for subdomain in $SUBDOMAINS; do
      if [ "$SUBDOMAINECHO" == "1" ]; then
         echo -e "\tProcesing sub domains of $domain"
      fi
      
      SUBDOMAINECHO="0"
      
      echo -ne "\t - $subdomain.$domain"

      USER=`ls -la $VHOSTROOT/$domain/subdomains/$subdomain/httpdocs | awk '{print $3}'`

      if [ "$FIXALL" == "1" ]; then
      
         if [ "$DRYRUN" == "1" ]; then
            echo -e "\t\tDry Run: Would run:"
            echo -e "\t\tchown -R $USER:$GROUP `ls $VHOSTROOT/$domain/subdomains/$subdomain/httpdocs | grep -v plesk-stat`"
         else
            echo -e "\t\t - Fixed"
            chown -R $USER:$GROUP `ls $VHOSTROOT/$domain/subdomains/$subdomain/httpdocs | grep -v plesk-stat`
         fi
      
      else
      
         find $VHOSTROOT/$domain/subdomains/$subdomain/httpdocs -user $WEBUSER > $TMPFILE
         
         if [ `grep -c '$domain/subdomains/$subdomain' $TMPFILE` -gt 0 ]; then
         
            if [ "$DRYRUN" == "1" ]; then
               echo -e "\t\tDry Run: Would run:"
               for file in `grep '$domain/subdomains/$subdomain' $TMPFILE`; do
                  echo -e "\t\tchown -R $USER:$GROUP \"$file\""
               done
            else
               echo -e " - Fixed"
               for file in `grep "$domain/subdomains/$subdomain" $TMPFILE`; do
                  chown -R $USER:$GROUP \"$file\"
               done
            fi
         
         else
            echo -e " - Skipped"
         fi
      fi
      
   #Done with Sub Domains
   done



}

#Do Domains
for domain in $DOMAINS; do
   CheckDomain $domain
#Done with Domains
done

#Remove the temp file
rm -Rf $TMPFILE






Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic Share/Bookmark  [ 1 post ] 

» Feed - Atomicorp

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group