#!/usr/bin/perl #version 1.2 #ssullivan@liquidweb.com #Fix common suPHP/FCGI permission/ownership issues on cPanel servers. use strict; use XML::Simple; use LWP::UserAgent; my $cpanelVerify = `/sbin/chkconfig --list | grep cpanel`; my $rootCheck = `id`; if ( $rootCheck =~/uid=0/) { if ( $cpanelVerify =~/cpanel/) { print "cPanel found!", "\n"; print "Generating hash..."; system("QUERY_STRING=\\\"regen=1\\\" /usr/local/cpanel/whostmgr/bin/whostmgr ./setrhash &> /dev/null"); my $hash = '/root/.accesshash'; if (-e $hash) { print "success!\n"; my $hash = `cat /root/.accesshash`; $hash =~ s/\n//g; my $auth = "WHM root:" . $hash; my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => "http://127.0.0.1:2086/xml-api/listaccts" ); $request->header( Authorization => $auth ); my $response = $ua->request($request); my $list = $response->content; open(outfile, ">suphpfix.xml"); print outfile $list; close (outfile); print "Checking for htscanner..."; my $htscannerChk = `php -i |grep -i htscanner |grep htscanner.config_file|cut -f 1 -d "="`; my $htscannerReturnChk = ""; if (!$htscannerChk) { print "not found! \n"; $htscannerReturnChk = "NotInstalled"; } else { print "found! \n"; $htscannerReturnChk = "Installed"; } my $domainCnt = `cat suphpfix.xml | grep -c ""`; if ($domainCnt == "1") { print "Found one account.\n"; #create and read xml my $xml = new XML::Simple; my $data = $xml->XMLin("suphpfix.xml"); print "-----------------------------------", "\n"; my $domain = $data->{acct}->{domain}; my $partition = $data->{acct}->{partition}; my $user = $data->{acct}->{user}; my $docRoot = "/$partition/$user/public_html"; print "Working with: ", $domain, "\n"; print "Found document root: ", $docRoot, "\n"; print "Removing group and world write in ", $docRoot, "\n"; system("find $docRoot -perm +022 -exec chmod go-w {} \\\;"); print "Done!", "\n"; print "Checking directory permissions in ", $docRoot, "\n"; system("find $docRoot -type d -exec chmod 755 {} \\\;"); print "Done!", "\n"; print "Setting ownerships to ", $user, ".", $user, " in ", $docRoot, "\n"; system("chown -R $user.$user $docRoot"); system("chown $user.nobody $docRoot"); print "Done!", "\n"; if ($htscannerReturnChk eq "Installed") { print "NOTICE: htscanner found, keeping php tweaks. \n"; } elsif ($htscannerReturnChk eq "NotInstalled") { print "Commenting out php tweaks from .htaccess if present...", "\n"; system("find $docRoot -name .htaccess -exec sed -i 's/^php_/#php_/g' {} \\\;"); print "Done!", "\n"; } print "Done with ", $domain, "!", "\n"; print "------------------------------", "\n"; system("rm -rf suphpfix.xml"); print "Completed. \n"; } else { print "Discovered multiple accounts.\n"; # create, read into xml array my $xml = new XML::Simple (KeyAttr=>[]); my $data = $xml->XMLin("suphpfix.xml"); my $count = 0; while ($count < $domainCnt) { print "-----------------------------------", "\n"; my $domain = $data->{acct}->[$count]->{domain}; my $partition = $data->{acct}->[$count]->{partition}; my $user = $data->{acct}->[$count]->{user}; my $docRoot = "/$partition/$user/public_html"; print "Working with: ", $domain, "\n"; print "Found document root: ", $docRoot, "\n"; print "Removing group and world write in ", $docRoot, "\n"; system("find $docRoot -perm +022 -exec chmod go-w {} \\\;"); print "Done!", "\n"; print "Checking directory permissions in ", $docRoot, "\n"; system("find $docRoot -type d -exec chmod 755 {} \\\;"); print "Done!", "\n"; print "Setting ownerships to ", $user, ".", $user, " in ", $docRoot, "\n"; system("chown -R $user.$user $docRoot"); system("chown $user.nobody $docRoot"); print "Done!", "\n"; if ($htscannerReturnChk eq "Installed") { print "NOTICE: htscanner found, keeping php tweaks. \n"; } elsif ($htscannerReturnChk eq "NotInstalled") { print "Commenting out php tweaks from .htaccess if present...", "\n"; system("find $docRoot -name .htaccess -exec sed -i 's/^php_/#php_/g' {} \\\;"); print "Done!", "\n"; } print "Done with ", $domain, "!", "\n"; print "------------------------------", "\n"; $count ++; print "Completed: $count / $domainCnt \n"; } system("rm -rf suphpfix.xml"); print "Completed. \n"; } } else { print "failed!\n"; print "WARNING: Failed to automatically generate hash! Please try logging into WHM and click `Setup Remote Access Key` and then re-run this script. Also please report this problem to Scott Sullivan. \n"; exit; } } else { print "cPanel not found, exiting..."; exit; } } else { print "You must be root to run this, exiting...", "\n"; exit; }