#!/usr/bin/perl
###########
# Perms
# OC: Jacob Perkins
# Maintainer: James Lavoy
# Contributer: Robert Davis, Michael Streeter
# permissions fixer.
# http://git.toolbox.hostgator.com/perms/pages/Home
# http://git.toolbox.hostgator.com/perms
# Please submit all bug reports at bugs.hostgator.com
#
# (C) 2013 - HostGator.com, LLC
###########
# I fully acknowledge that this code is a complete mess. I will refactor it as I have time. If you're up the challenge let me know :)

use strict;
use Cwd;
use File::Find;
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;
use Fcntl ':mode';
use Getopt::Std;
my %opt;
my $opt_string = 'v';
getopts("$opt_string", \%opt);
use IO::Handle;
STDERR->autoflush(1);
STDOUT->autoflush(1);


print "[*] Perms v2.0\n";
my $user = shift;
my $dso = 1;
my $mysqlpass;
my @docroots;
my @domains;
my $home = "home";
my $runtime = time();
my ($uid, $gid, $nobodygid, $mailgid) = getids($user);

sub checks {
	my $user = shift;
	my $cwd = cwd();
	my $userfromcwd;

	my ( undef, $rundir, $cwduser ) = split(/\//, $cwd);
	if ( ! $user ) {
		$userfromcwd = '0';
		$user = $cwduser;
		($uid, $gid, $nobodygid, $mailgid) = getids($user);
		if ( ! -f "/etc/psa/.psa.shadow" ) {
			if ( $user =~ /^$/ ) {
				die "[!] Does not appear to be running in a users directory, and no user specified.\n";
			}
		}
	}

	if ( $user eq qw{.} or $user eq qw{..} ) { die "[!] Silly rabbit, periods are for kids.\n" }
	if ( $user =~ /^virtfs$/ ) { die "[!] Virtfs? You've got to be kidding :|\n" }
	if ( $user =~ /^hgtransfer$/ ) { die "[!] Stopping, don't run this in hgtransfer.\n" }
	if ( $userfromcwd != '0' ) {
		if ( ! -f "/etc/psa/.psa.shadow") {
			if ( $rundir !~ /^home/ ) {
				die "[!] Not running from a home directory and no user specified.\n";
			}
		}
	}

	if ( -d "/usr/local/cpanel" ) {
		if ( ! -f "/var/cpanel/users/$user" ) { die "[!] Not a valid user!\n" }
		open(my $DAT, "-|", "/usr/local/cpanel/bin/rebuild_phpconf --current");
			while(<$DAT>) {
				if ( /^PHP[45]\sSAPI:\sdso/ ) {
					$dso = '0';
					print "[!] This server is running php in DSO. Using DSO perms.\n";
				}
			}
		close($DAT);
		builddomains($user);
		runcpanel($user);
	} elsif ( -f "/etc/psa/.psa.shadow" ) {
		runplesk($user);
	} else {
		die "[!] Unable to determine panel.\n";
	}
}

sub builddomains {
	my $user = shift;
	open(my $DAT, "<", "/etc/userdomains") or die;
		while(<$DAT>) {
			if ( /^([a-zA-Z0-9\.\-]+):\s$user$/ ) {
				push(@domains, $1);
			}
		}
	close($DAT);
}

sub getids {
	my $user = shift;
	my ( undef, undef, $uid, $gid ) = getpwnam($user);
	my ( undef, undef, undef, $nobodygid ) = getpwnam("nobody");
	my ( undef, undef, undef, $mailgid ) = getpwnam("mail");
	return($uid, $gid, $nobodygid, $mailgid);
}

sub runplesk {
	chomp($mysqlpass = `cat /etc/psa/.psa.shadow`);
	my $dir = cwd();
	my $user = find_username($dir);
	if ( $user eq "Error" ) {
		die "[!] Unable to determine user.\n";
	}
	my $primarydomain = find_primarydomain($dir);
	if ( length($primarydomain) == 0 ) {
		die "[!] Unable to determine primary domain.\n";
	}
	if (find_directories($user, \@docroots) > 0 ) {
		die "[!] Unable to determine directories for user.\n";
	}
	if (find_domains($primarydomain, \@domains) > 0 ) {
		die "[!] Unable to determines domain for user.\n";
	}
	# removing due to plesk breaking it
	#print "[*] Setting domains to CGI:\n";
	#foreach my $domain ( @domains ) {
	#	print "\t- $domain\n";
	#	system("/usr/local/psa/bin/domain -u $domain -php_handler_type cgi");
	#}
	print "[*] Setting perms for $primarydomain (user $user):\n";
	foreach my $docroot ( @docroots ) {
		print "\t- $docroot\n";
		update_perms($user, $docroot);
	}
}

sub find_username {
	my $dir = shift;
	my $user = `mysql -ss -u'admin' -p'$mysqlpass' -e"select sys_users.login from sys_users inner join hosting on hosting.sys_user_id=sys_users.id where hosting.www_root = '$dir' limit 1;" psa`;
	if (( $? >> 8 ) > 0 ) {
		return ($? >> 8);
	}
	chomp($user);
	if (length($user) == 0 ) {
		print "[!] Unable to determine user from working directory.\n\tPlease run from one of the following directories.\n";
		system("mysql -t -u'admin' -p'$mysqlpass' -e\"select domains.name as Domain, hosting.www_root as 'Document Root' from domains inner join hosting on domains.id=hosting.dom_id;\" psa");
		exit;
	}
	return $user;
}

sub find_primarydomain {
	my $dir = shift;
	my $domain;
	my @docroots = `mysql -ss -u'admin' -p'$mysqlpass' -e"select www_root from hosting;" psa`;
	my $webspaceid;
	foreach my $line (@docroots) {
		chomp($line);
		my $choppedline = chopbottomdir($line);
		if ( $dir =~ /$choppedline/ ) {
			$webspaceid = `mysql -ss -u'admin' -p'$mysqlpass' -e"select domains.webspace_id from domains inner join hosting on domains.id=hosting.dom_id where hosting.www_root = '$line' limit 1;" psa`;
			if ( $webspaceid == 0 ) {
				$domain = `mysql -ss -u'admin' -p'$mysqlpass' -e"select domains.name from domains inner join hosting on domains.id=hosting.dom_id where hosting.www_root = '$line';" psa`;
			} else {
				$domain = `mysql -ss -u'admin' -p'$mysqlpass' -e"select domains.name from domains where domains.id=$webspaceid;" psa`;
			}
		}
	}
	chomp($domain);
	return $domain;
}

sub chopbottomdir {
	my $fullpath = shift;
	my $loc = 0;
	my $lastloc = 0;
	while ($loc >= 0) {
		$lastloc = $loc;
		$loc = index($fullpath, "/", $loc + 1);
	}
	my $dirpath = substr($fullpath, 0, $lastloc);
	return $dirpath;
}

sub find_directories {
	my ( $user, $output ) = @_;
	@{$output} = `mysql -ss -u'admin' -p'$mysqlpass' -e"select hosting.www_root from hosting inner join sys_users on hosting.sys_user_id = sys_users.id where sys_users.login = '$user';" psa`;
	foreach my $line (@{$output}) {
		chomp($line);
	}
	return ($? >> 8 );
}

sub find_domains {
	my ( $domain, $domains ) = @_;
	@{$domains} = `mysql -ss -u'admin' -p'$mysqlpass' -e"select name from domains where webspace_id = (select id from domains where name = '$domain') union select name from domains where name = '$domain';" psa`;
	foreach my $line (@{$domains}) {
		chomp($line);
	}
	return ( $? >> 8 );
}

sub update_perms {
	my ( $user, $dir ) = @_;
	system("chown -R $user:psacln $dir");
	system("chown $user:psaserv $dir");
	print "[*] Correcting permissions on files...";
	File::Find::find({wanted => \&pleskfilewanted}, $dir);
	print "DONE\n";
	print "[*] Correcting permissions on directories...";
	File::Find::find({wanted => \&pleskdirwanted}, $dir);
	print "DONE\n";
	print "[*] Correcting permissions on executables...";
	File::Find::find({wanted => \&pleskexecwanted}, $dir);
	print "DONE\n";
	chmod(0755, $dir);
	print "[*] DONE.\n";
}

sub runcpanel {
	my $user = shift;
	if ( ! -d "/home/$user" ) {
		foreach my $i ( 1 .. 9 ) {
			if ( -d "/home$i/$user/" ) {
				$home = "home$i";
				last;
			}
		}
		if ( $home eq "home" ) {
			die "[!] User does not appear to have a home directory on this server.\n";
		}
	}
	homefix($user);
}

sub homefix {
	my $user = shift;
	if ( ! -d "/$home/$user/public_html" ) {
		print "[*] No public_html found, making it.\n";
		mkdir("/$home/$user/public_html");
	}
	# fix homedir
	print "[*] Correcting home directory...";
	random_perm("/$home/$user", 0711, $user, $user);
	# fix random dirs and files
	my @files = </$home/$user/*>;
	foreach my $path (@files) {
		my @split = split(/\//, $path);
		my $file = pop(@split);
		if ( $file !~ /^(.|..)$/ ) {
			if ( -f $path ) {
				random_perm($path, 0644, $user, $user);
			} elsif ( -d $path ) {
				my @dirs = split(/\//, $path);
				my $check = pop(@dirs);
				if ( $check !~ /^(etc|mail|public_html|tmp|www|access-logs)/ ) {
					File::Find::find({wanted => \&random_find}, $path);
				}
				if ( $check eq 'bin' ) {
					File::Find::find({wanted => \&execwanted}, $path);
				}
			}
		}
	}
	# fix random hidden dirs and files
	@files = </$home/$user/.*>;
	foreach my $path (@files) {
		my @split = split(/\//, $path);
		my $file = pop(@split);
		if ( $file !~ /^(.|..)$/ ) {
			if ( -f $path ) {
				if ( $path =~ /(\.accesshash|\.contactemail)$/ ) {
					random_perm($path, 0600, $user, $user);
				} else { 
					random_perm($path, 0644, $user, $user);
				}
			} elsif ( -d $path ) {
				my @dirs = split(/\//, $path);
				my $check = pop(@dirs);
				if ( $check !~ /^(etc|mail|public_html|tmp|www|access-logs|\.ssh|\.security|\.autorespond)$/ ) {
					File::Find::find({wanted => \&random_find}, $path);
				}
			}
		}
	}
	print "DONE\n";
	# fix .cpanel
	random_perm("/$home/$user/.cpanel", 0700, $user, $user);
	# fix .cphorde
	random_perm("/$home/$user/.cphorde", 0700, $user, $user);
	# fix .autorespond
	random_perm("/$home/$user/.autorespond", 0700, $user, $user);
	# fix .htpasswds
	random_perm("/$home/$user/.htpasswds", 0750, $user, "nobody");
	# fix quickinstall
        if ( -d "/$home/$user/.cpanel/quickinstall" ) {
                random_perm("/$home/$user/.cpanel//quickinstall", "0750", $user, $user);
                File::Find::find({wanted => \&fantasticowanted}, "/$home/$user/.cpanel/quickinstall");
        }

	# fix tmp
	tmp_perm($user);
	random_perm("/$home/$user/tmp", 0755, $user, $user);
	# fix mysql
	mysql($user);
	# fix public_html
	public_html_perms($user);
	# fix .ssh
	File::Find::find({wanted => \&sshwanted}, "/$home/$user/.ssh");
	if ( ! -l "/$home/$user/www" ) {
		system("ln -s /$home/$user/public_html /$home/$user/www");
		system("chown -h $user:$user /$home/$user/www");
	}
	if ( ! -l "/$home/$user/access-logs" ) {
		if ( ! -d "/usr/local/apache/domlogs/$user") {
			system("mkdir -p /usr/local/apache/domlogs/$user");
			system("chown root.$user /usr/local/apache/domlogs/$user");
			my $mode = '0750';
			chmod($mode, "/usr/local/apache/domlogs/$user");
		}
		system("ln -s /usr/local/apache/domlogs/$user /$home/$user/access-logs");
		system("chown -h $user:$user /$home/$user/access-logs");
	}
	# fix www and accesslogs symlinks
	system("chown -h $user:$user /$home/$user/www"); 
	system("chown -h $user:$user /$home/$user/access-logs"); 
	# fix addon domains
	print "[*] Correcting addon domains...";
	addon_perms($user);
	random_perm("/$home/$user/public_html", 0750, $user, 'nobody');
	print "DONE\n";
	# fix mail
	print "[*] Correcting mail directory...";
	#system("/scripts/mailperm $user");
	File::Find::find({wanted => \&mail_wanted}, "/$home/$user/mail/");
	File::Find::find({wanted => \&etcwanted}, "/$home/$user/etc/");
	random_perm("/$home/$user/mail/", 0751, $user, $user);
	print "DONE\n";
	# fix fantastico
	if ( -d "/$home/$user/.fantasticodata" ) {
		random_perm("/$home/$user/.fantasticodata", "0750", $user, $user);
		File::Find::find({wanted => \&fantasticowanted}, "/$home/$user/.fantasticodata");
	}
	# fix valiases
	print "[*] Fixing valiases...";
	valias_perms($user);
	print "DONE\n";
	# fix transfers
	transfers($user);
	print "[*] DONE.\n";
}

sub public_html_perms {
	my $user = shift;
	print "[*] Correcting directory permissions...";
	File::Find::find({wanted => \&dirwanted}, "/$home/$user/public_html/");
	print "DONE\n";
	print "[*] Correcting file permissions...";
	File::Find::find({wanted => \&filewanted}, "/$home/$user/public_html/");
	print "DONE\n";
	print "[*] Correcting executable permissions...";
	File::Find::find({wanted => \&execwanted}, "/$home/$user/public_html/");
	print "DONE\n";
	print "[*] Correcting permissions on .htpasswds...";
	File::Find::find({wanted => \&htwanted}, "/$home/$user/.htpasswds/");
	print "DONE\n";
}

sub tmp_perm {
	my $user = shift;
	print "[*] Correcting tmp permissions...";
	File::Find::find({wanted => \&dirwanted}, "/$home/$user/tmp/");
	File::Find::find({wanted => \&filewanted}, "/$home/$user/tmp");
	print "DONE\n";
}

sub random_perm {
	my ( $file, $mode, $user, $group, $mtime, $ctime ) = @_;
	if ( $opt{'v'} ) { print "- chmoding $file, $mode\n" }
	chmod($mode, $file);
	my ( $randompermuid, $randompermgid ) = ( $uid, $gid );
	if ( $group eq 'nobody' ) { $randompermgid = $nobodygid }
	if ( $group eq 'mail' ) { $randompermgid = $mailgid }
	if ( $opt{'v'} ) { print "- chowning $file, $randompermuid, $randompermgid\n" }
	chown($randompermuid, $randompermgid, $file);
	logit($user, $name, $mtime, $ctime);
}

sub dirwanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($checkuid, $checkgid, $nobodygid) = ($uid, $gid, $nobodygid);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -d _ ) {
		if ( $dso == 0 ) {
			if ((substr(sprintf("%o",($mode)), -4)) != '0775' ) {
				if ( $opt{'v'} ) { print "- chmoding $name, 0775\n" }
				chmod(0775, $name);
				logit($user, $name, $mtime, $ctime);
			}
		} else {
			if ((substr(sprintf("%o",($mode)), -4)) != '0755' ) {
				if ( $opt{'v'} ) { print "- chmoding $name, 0755\n" }
				chmod(0755, $name);
				logit($user, $name, $mtime, $ctime);
			}
		}
		if ( $dso == 0 ) {
			if ( ( $uid ne $checkuid ) || ( $gid ne $nobodygid ) || ( ( $uid ne $checkuid ) && ( $gid ne $nobodygid ) ) ) {
				if ( $opt{'v'} ) { print "- chowning $name, $checkuid, $nobodygid\n" }
				chown($checkuid, $nobodygid, $name);
				logit($user, $name, $mtime, $ctime);
			}
		} else {
			if ( ( $uid ne $checkuid ) || ( $gid ne $checkgid ) || ( ( $uid ne $checkuid ) && ( $gid ne $checkgid ) ) ) {
				if ( $opt{'v'} ) { print "- chowning $name, $checkuid, $checkgid\n" }
				chown($checkuid, $checkgid, $name);
				logit($user, $name, $mtime, $ctime);
			}
		}
	}
}

sub filewanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($checkuid, $checkgid, $nobodygid) = ($uid, $gid, $nobodygid );
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -f _ ) {
		if ( $dso == 0 ) {
			if ((substr(sprintf("%o",($mode)), -4)) != '0664' ) {
				if ( $opt{'v'} ) { print "- chmoding $name, 0664\n" }
				chmod(0664, $name);
				logit($user, $name, $mtime, $ctime);
			}
		} else {
			if ((substr(sprintf("%o",($mode)), -4)) != '0644' ) {
				if ( $opt{'v'} ) { print "- chmoding $name, 0644\n" }
				chmod(0644, $name);
				logit($user, $name, $mtime, $ctime);
			}
		}
		if ( $dso == 0 ) {
			if ( ( $uid ne $checkuid ) || ( $gid ne $nobodygid ) || ( ( $uid ne $checkuid ) && ( $gid ne $nobodygid ) ) ) {
				if ( $opt{'v'} ) { print "- chowning $name, $checkuid, $nobodygid\n" }
				chown($checkuid, $nobodygid, $name);
				logit($user, $name, $mtime, $ctime);
			}
		} else {
			if ( ( $uid ne $checkuid ) || ( $gid ne $checkgid ) || ( ( $uid ne $checkuid ) && ( $gid ne $checkgid ) ) ) {
				if ( $opt{'v'} ) { print "- chowning $name, $checkuid, $nobodygid\n" }
				chown($checkuid, $checkgid, $name);
				logit($user, $name, $mtime, $ctime);
			}
		}
	}
}

sub pleskfilewanted {
	my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
	if ( -f _ ) {
		if ((substr(sprintf("%o",($mode)), -4)) != '0644' ) {
			chmod(0644, $name);
		}
	}
}

sub pleskdirwanted {
	my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
	if ( -d _ ) {
		if ((substr(sprintf("%o",($mode)),-4)) != '0755' ) {
			chmod(0755, $name);
		}
	}
}

sub pleskexecwanted {
	my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
	if ( ( -f _ ) && ( /^.*\.(pl|cgi|fcgi|sh|py)$/ ) ) {
		chmod(0755, $name);
	}
}

sub execwanted {
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( ( -f _ ) && ( /^.*\.(pl|cgi|fcgi|sh|py)$/ ) ) {
		if ( $opt{'v'} ) { print "- chmoding $name, 0755\n" }
		chmod(0755, $name);
		logit($user, $name, $mtime, $ctime);
	}
}

sub etcwanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -f _ ) {
		if ( $name =~ /(quota|passwd)/ ) {
			random_perm($name, 0640, $user, "mail", $mtime, $ctime);
		} elsif ( $name =~ /shadow/ ) {
			random_perm($name, 0640, $user, $user, $mtime, $ctime);
		} elsif ( $name =~ /ftpquota/ ) {
			random_perm($name, 0644, $user, $user, $mtime, $ctime);
		} else {
			random_perm($name, 0644, $user, $user, $mtime, $ctime);
		}
	} elsif ( -d _ ) {
		if ( $name =~ /\@pwcache/ ) {
			random_perm($name, 0700, $user, $user, $mtime, $ctime);
		} else {
			random_perm($name, 0750, $user, "mail", $mtime, $ctime);
		}
	}
}

sub sshwanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -f _ ) {
		if ( $name =~ /authorized_keys/ ) {
			random_perm($name, 0644, $user, $user, $mtime, $ctime);
		} elsif ( $name =~ /\.pub$/ ) {
			random_perm($name, 0644, $user, $user, $mtime, $ctime);
		} else {
			random_perm($name, 0600, $user, $user, $mtime, $ctime);
		}
	}
	if ( -d _ ) {
		random_perm($name, 0700, $user, $user, $mtime, $ctime);
	}
}

sub htwanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -d _ ) {
		random_perm($name, 0750, $user, $user, $mtime, $ctime);
	} elsif ( -f _ ) {
		random_perm($name, 0640, $user, $user, $mtime, $ctime);
	}
}

sub fantasticowanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -d _ ) {
		random_perm($name, 0750, $user, $user, $mtime, $ctime);
	} elsif ( -f _ ) {
		random_perm($name, 0640, $user, $user, $mtime, $ctime);
	}
}

sub mail_wanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gidundef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -f _ ) {
		random_perm($name, 0660, $user, $user, $mtime, $ctime);
	}
	if ( -d _ ) {
		my @dirs = split(/\//, $name);
		my $dirname = pop(@dirs);
		$dirname =~ s/\(/\\\(/g;
		$dirname =~ s/\)/\\\)/g;
		$dirname =~ s/\[/\\\[/g;
		$dirname =~ s/\]/\\\]/g;
		if ( grep(/^$dirname$/, @domains) ) {
			random_perm($name, 0751, $user, $user, $mtime, $ctime);
		} else {
			random_perm($name, 0700, $user, $user, $mtime, $ctime);
		}
	}
}

sub valias_perms {
	my $user = shift;
	foreach my $domain ( @domains ) {
		if ( -f "/etc/valiases/$domain" ) {
			random_perm("/etc/valiases/$domain", 0640, $user, "mail");
		}
	}
}

sub random_find {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	if ( -f _ ) {
		random_perm($name, 0644, $user, $user, $mtime, $ctime);
	} elsif ( -d _ ) {
		random_perm($name, 0755, $user, $user, $mtime, $ctime);
	}
}

sub addon_perms {
	my $user = shift;
	open(my $DAT, "<", "/usr/local/apache/conf/httpd.conf");
		while(<$DAT>) {
			if ( /DocumentRoot\s+(\/$home\/$user\/\S+)$/ ) {
				random_perm($1, 0755, $user, $user);
			}
		}
	close($DAT);
}

sub transfers {
	my $user = shift;
	my @xferfiles = ("hgtransfer", "hgtransfers", "xfer", "xfers");
	foreach my $file ( @xferfiles ) {
		if ( -d "/$home/$user/$file" ) {
			print "[*] Found transfers directory, making root:root\n";
			random_perm($file, 0755, 0, 0);
		}
	}
}

sub mysql {
	my $user = shift;
	if ( -d "/$home/$user/MYSQL_DATA" ) {
		print "[*] Found MYSQL_DATA File. Making mysql:mysql\n";
		random_perm("/$home/$user/MYSQL_DATA", 0755, "mysql", "mysql");
		File::Find::find({wanted => \&mysqldatawanted}, "/$home/$user/MYSQL_DATA/");
	}
}

sub mysqldatawanted {
	my ( undef, undef, $user ) = split(/\//, $File::Find::dir);
	my ($checkuid, $checkgid, $nobodygid) = ($uid, $gid, $nobodygid);
	my ($dev,$ino,$mode,$nlink,$uid,$gid,undef,undef,undef,$mtime,$ctime) = lstat($_);
	my ( undef, undef, $mysqluid, $mysqlgid ) = getpwnam("mysql");
	chown($mysqluid, $mysqlgid, $name);
	logit($user, $name, $mtime, $ctime);
}

sub logit {
	my ( $user, $name, $mtime, $ctime ) = @_;
	if (( ! defined($name) ) || ( ! defined($mtime) ) || ( ! defined($ctime) )) { return }
	open(my $WAT, '>>', "/$home/$user/.permslog");
		print $WAT "$runtime\t$name\t$mtime\t$ctime\n";
	close($WAT);
}

checks($user);
