--- loncom/debugging_tools/move_construction_spaces.pl 2011/10/30 16:01:20 1.6 +++ loncom/debugging_tools/move_construction_spaces.pl 2012/06/08 13:08:13 1.9 @@ -5,7 +5,7 @@ # Move Construction Spaces from /home/$user/public_html # to /home/httpd/html/priv/$domain/$user and vice versa # -# $Id: move_construction_spaces.pl,v 1.6 2011/10/30 16:01:20 raeburn Exp $ +# $Id: move_construction_spaces.pl,v 1.9 2012/06/08 13:08:13 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -256,6 +256,7 @@ if ($lonusersdir) { my ($dir,$output); if (opendir($dir,$lonusersdir)) { my @contents = (grep(!/^\.{1,2}$/,readdir($dir))); + closedir($dir); foreach my $item (@contents) { if (-d "$lonusersdir/$item") { if ($item =~ /^$match_domain$/) { @@ -303,7 +304,6 @@ if ($lonusersdir) { } } } - closedir($dir); } else { $output = &mt('Could not open [_1].',"'$lonusersdir'")."\n"; print $output; @@ -318,10 +318,12 @@ if ($londocroot ne '') { my ($dir,$domdir); if (opendir($dir,"$londocroot/res")) { my @contents = (grep(!/^\.{1,2}$/,readdir($dir))); + closedir($dir); foreach my $dom (@contents) { if ((grep(/^\Q$dom\E/,@machinedoms)) && (-d "$londocroot/res/$dom")) { if (opendir($domdir,"$londocroot/res/$dom")) { my @unames = (grep(!/^\.{1,2}$/,readdir($domdir))); + closedir($domdir); foreach my $uname (@unames) { if ($uname =~ /^$match_username$/) { push(@{$pubusers{$uname}},$dom); @@ -341,10 +343,12 @@ if ($action eq 'undo') { my ($dir,$domdir); if (opendir($dir,"$londocroot/priv")) { my @contents = (grep(!/^\.{1,2}/,readdir($dir))); + closedir($dir); foreach my $dom (@contents) { next if (!-d "$londocroot/priv/$dom"); if (opendir($domdir,"$londocroot/priv/$dom")) { my @unames = (grep(!/^\.{1,2}$/,readdir($domdir))); + closedir($domdir); foreach my $uname (@unames) { if ($uname =~ /^$match_username$/) { push(@{$privspaces{$uname}},$dom); @@ -357,6 +361,7 @@ if ($action eq 'undo') { } foreach my $uname (keys(%privspaces)) { if (ref($privspaces{$uname}) eq 'ARRAY') { + my $output; if (@{$privspaces{$uname}} > 1) { my $displaydoms = join(', ',@{$privspaces{$uname}}); print &mt('Same username used for authors in multiple domains.')."\n". @@ -367,28 +372,69 @@ if ($action eq 'undo') { my $choice=; chomp($choice); if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) { - my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice); - print $output; - print $logfh $output; + $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice); } else { print &mt('Invalid choice of domain:')." $choice\n"; - my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; + $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; print $output; print $logfh $output; next; } } elsif (@{$privspaces{$uname}} == 1) { - my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]); - print $output; - print $logfh $output; + $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]); } else { print &mt('Username [_1] found in [_2] was not within a domain', "'$uname'","'$londocroot/priv'")."\n"; - my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; - print $output; - print $logfh $output; + $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; + } + print $output; + print $logfh $output; + } + } + if (-d "$londocroot/priv") { + my $output; + if (opendir(my $dir,"$londocroot/priv")) { + my @doms = grep(!/^\.{1,2}/,readdir($dir)); + closedir($dir); + foreach my $dom (@doms) { + if (opendir(my $domdir,"$londocroot/priv/$dom")) { + my @contents = grep(!/^\.{1,2}/,readdir($domdir)); + closedir($domdir); + if (@contents == 0) { + if (rmdir("$londocroot/priv/$dom")) { + $output = &mt('Removed empty directory: [_1]', + "'$londocroot/priv/$dom'")."\n"; + } else { + $output = &mt('Failed to remove directory: [_1]', + "'$londocroot/priv/$dom'")."\n"; + } + } + } + } + } + my $warning = &mt('WARNING: Access to Construction Spaces in their old locations (i.e., in [_1]) via LON-CAPA with URLs of the form [_2] will not work until the directory at [_3] is moved or deleted.',"'/home//'","'/priv//'","'$londocroot/priv/'")."\n"; + if (opendir(my $dir,"$londocroot/priv")) { + my @contents = (grep(!/^\.{1,2}/,readdir($dir))); + closedir($dir); + if (@contents == 0) { + if (rmdir("$londocroot/priv")) { + $output .= &mt('Removed empty directory: [_1]', + "'$londocroot/priv'")."\n"; + } else { + $output .= &mt('Failed to remove directory: [_1]', + "'$londocroot/priv'")."\n". + $warning."\n"; + } + } else { + $output .= $warning."\n". + &mt('The attempt to remove the directory failed, because it is not empty.')."\n"; } + } else { + $output .= $warning."\n". + &mt('The attempt to open the directory to see its contents failed, hence no attempt was made to remove it.')."\n"; } + print $output; + print $logfh $output; } &stop_logging($logfh); print "\n".&mt('Done')."\n"; @@ -401,7 +447,8 @@ my ($dbh,$dbflag); # Iterate over directories in /home if (opendir(my $dir,"/home")) { - my @possibles = grep(!/^\.{1,2}$/,readdir($dir)); + my @possibles = grep(!/^\.{1,2}$/,readdir($dir)); + closedir($dir); foreach my $item (sort(@possibles)) { next if ($item eq 'www'); if ((-d "/home/$item") && ($item ne '')) { @@ -526,6 +573,7 @@ if (opendir(my $dir,"/home")) { if (opendir(my $homedir,"/home/$author")) { my @contents = grep(!/^\.{1,2}$/,readdir($homedir)); + closedir($homedir); if (@contents == 0) { if (rmdir("/home/$author/")) { $output .= &mt('Removed empty directory: [_1]', @@ -651,7 +699,7 @@ if (opendir(my $dir,"/home")) { if (-e $dompath) { if (move($source_path,$newpath)) { chown($uid,$gid,$newpath); - chmod($newpath,0750); + chmod(0750,$newpath); $output = &mt('Moved [_1] to [_2].', "'$source_path'","'$newpath'")."\n"; } else { @@ -771,11 +819,22 @@ sub move_priv_to_home { if ($uname =~ /^$match_username$/ && $domain =~ /^$match_domain$/) { my $source_path="$londocroot/priv/$domain/$uname"; my $target_path="/home/$uname/public_html"; + my $userpath = &LONCAPA::propath($domain,$uname); + my ($authtype,$userid,$groupid); + if (-e "$userpath/passwd") { + if (open(my $fh, "<$userpath/passwd")) { + ($authtype) = split(/:/,<$fh>); + close($fh); + } + } if (!-e "/home/$uname") { - my (undef,undef,$userid,$groupid) = getpwnam($uname); - if (mkdir("/home/$uname",0750)) { - if ($userid ne '' && $groupid ne '') { + (undef,undef,$userid,$groupid) = getpwnam($uname); + if (mkdir("/home/$uname",0711)) { + if ($authtype eq 'unix' && $userid ne '' && $groupid ne '') { chown($userid,$groupid,"/home/$uname"); + if (&www_in_group($uname)) { + chmod(0710,"/home/$uname"); + } } } else { $output = &mt('Failed to create directory [_1] -- not moving [_2].', @@ -787,8 +846,17 @@ sub move_priv_to_home { if (!-e $target_path) { move($source_path,$target_path); chown($uid,$gid,$target_path); - chmod($target_path,2770); - $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + if ($authtype eq 'unix' && $userid ne '' && $groupid ne '') { + if (&www_in_group($uname)) { + chown($userid,$groupid,$target_path); + } + } + chmod(02770,$target_path); + if (-e $target_path && !-e $source_path) { + $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + } else { + $output = &mt('Failed to move [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + } } else { $output = &mt('Directory [_1] already exists -- not moving [_2].', "'$target_path'","'$source_path'")."\n"; @@ -798,6 +866,30 @@ sub move_priv_to_home { return $output; } +sub www_in_group { + my ($uname) = @_; + my $hasuser; + if (open(PIPE,"/usr/bin/groups www|")) { + my $memberlist = ; + close(PIPE); + chomp($memberlist); + my ($safegroups)=($memberlist=~/:\s*([\s\w]+)$/); + if ($safegroups =~ /\S/) { + my @members = split(/\s+/,$safegroups); + if (grep(/^\Q$uname\E$/,@members)) { + $hasuser = 1; + } else { + my @ugrouplist=grep(!/www|$uname/,@members); + my $gl=join(',',(@ugrouplist,$uname)); + if (system('/usr/sbin/usermod','-G',$gl,'www') == 0) { + $hasuser = 1; + } + } + } + } + return $hasuser; +} + sub get_user_selection { my ($defaultrun) = @_; my $do_action = 0; @@ -842,6 +934,7 @@ sub check_for_restore_files { my ($londaemons,$author,$domain) = @_; if (opendir(my $homedir,"/home/$author")) { my @contents = grep(!/^\.{1,2}$/,readdir($homedir)); + closedir($homedir); if (@contents > 0) { if (grep(/^restore_\d+\.sh$/,@contents)) { if (!-e "$londaemons/logs/moved_construction_spaces") {