File:  [LON-CAPA] / loncom / debugging_tools / move_construction_spaces.pl
Revision 1.4: download - view: text, annotated - select for diffs
Thu Oct 27 03:43:53 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Early out if configuration information unavailable for:
  lonUsersDir, lonDocRoot, or lonDaemons perlvars.
- Remove duplicated retrieval of configuration information.

    1: #!/usr/bin/perl
    2: #
    3: # The LearningOnline Network
    4: #
    5: # Move Construction Spaces from /home/$user/public_html
    6: # to /home/httpd/html/priv/$domain/$user and vice versa
    7: #
    8: # $Id: move_construction_spaces.pl,v 1.4 2011/10/27 03:43:53 raeburn Exp $
    9: #
   10: # Copyright Michigan State University Board of Trustees
   11: #
   12: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   13: #
   14: # LON-CAPA is free software; you can redistribute it and/or modify
   15: # it under the terms of the GNU General Public License as published by
   16: # the Free Software Foundation; either version 2 of the License, or
   17: # (at your option) any later version.
   18: #
   19: # LON-CAPA is distributed in the hope that it will be useful,
   20: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   21: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   22: # GNU General Public License for more details.
   23: #
   24: # You should have received a copy of the GNU General Public License
   25: # along with LON-CAPA; if not, write to the Free Software
   26: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   27: #
   28: # /home/httpd/html/adm/gpl.txt
   29: #
   30: # http://www.lon-capa.org/
   31: #
   32: #################################################
   33: 
   34: use strict;
   35: use lib '/home/httpd/lib/perl/';
   36: use LONCAPA::Configuration;
   37: use LONCAPA qw(:DEFAULT :match);
   38: use Apache::lonlocal;
   39: use File::Copy;
   40: use GDBM_File;
   41: 
   42: my $lang = &Apache::lonlocal::choose_language();
   43: &Apache::lonlocal::get_language_handle(undef,$lang);
   44: 
   45: if ($< != 0) {
   46:     print &mt('You must be root in order to move Construction Spaces.')."\n".
   47:           &mt('Stopping')."\n";
   48:     exit;
   49: }
   50: 
   51: my $perlvar=&LONCAPA::Configuration::read_conf();
   52: my ($lonuserdir,$londocroot,$londaemons);
   53: if (ref($perlvar) eq 'HASH') {
   54:     $lonuserdir = $perlvar->{'lonUsersDir'};
   55:     $londocroot = $perlvar->{'lonDocRoot'};
   56:     $londaemons = $perlvar->{'lonDaemons'};
   57: }
   58: undef($perlvar);
   59: 
   60: if ($lonuserdir eq '') {
   61:     print &mt('Could not determine location of [_1] directory.',"'lonUsersDir'")."\n".
   62:           &mt('Stopping')."\n";
   63:     exit;
   64: }
   65: 
   66: if ($londocroot eq '') {
   67:     print &mt('Could not determine location of [_1] directory.',"'lonDocRoot'")."\n".
   68:           &mt('Stopping')."\n";
   69:     exit;
   70: }
   71: 
   72: my $distro;
   73: if ($londaemons eq '') {
   74:     print &mt('Could not determine location of [_1] directory.',"'lonDaemons'")."\n".
   75:           &mt('Stopping')."\n";
   76:     exit;
   77: } else {
   78:     if (-e "$londaemons/distprobe") {
   79:         if (open(PIPE,"perl $londaemons/distprobe|")) {
   80:             $distro = <PIPE>;
   81:             close(PIPE);
   82:         }
   83:     }
   84: }
   85: 
   86: if ($distro eq '') {
   87:     print &mt('Could not determine Linux distro.')."\n".
   88:           &mt('Stopping')."\n";
   89:     exit;
   90: } else {
   91:     my $stopapachecmd = '/etc/init.d/httpd stop';
   92:     my $apacheprocess = '/usr/sbin/httpd';
   93:     my $stopapachecmd = '/etc/init.d/httpd stop';
   94:     my $proc_owner = 'root';
   95:     if ($distro =~ /^(suse|sles)/) {
   96:         if ($distro =~ /^(suse|sles)9/) {
   97:             $stopapachecmd = '/etc/init.d/apache stop';
   98:         } else {
   99:             $apacheprocess = '/usr/sbin/httpd2';
  100:             $stopapachecmd = '/etc/init.d/apache2 stop';
  101:         }
  102:     } elsif ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
  103:         $apacheprocess = '/usr/sbin/apache2';
  104:         $stopapachecmd = '/etc/init.d/apache2 stop';
  105:     } elsif ($distro =~ /^(?:fedora)(\d+)/) {
  106:         my $version = $1;
  107:         if ($version >= 16) {
  108:             $stopapachecmd = '/bin/systemctl stop httpd.service';
  109:         }
  110:     }
  111:     if (open(PIPE,"ps -ef |grep '$apacheprocess' |grep -v grep 2>&1 |")) {
  112:         my $status = <PIPE>;
  113:         close(PIPE);
  114:         chomp($status);
  115:         if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
  116:             print "\n".
  117:                   &mt('You need to stop the Apache daemon before moving Construction Spaces.')."\n".
  118:                   &mt('To do so use the following command: [_1]',"\n\n$stopapachecmd")."\n\n".
  119:                   &mt('Now stopping the move_construction_spaces.pl script.')."\n";
  120:             exit;
  121:         }
  122:     } else {
  123:         print &mt('Could not determine if Apache daemon is running.')."\n";
  124:     }
  125: }
  126: 
  127: my $stoploncontrol = '/etc/init.d/loncontrol stop';
  128: if (open(PIPE,"ps -ef |grep lond |grep -v grep 2>&1 |")) {
  129:     my $status = <PIPE>;
  130:     close(PIPE);
  131:     chomp($status);
  132:     if ($status =~ /^www\s+\d+\s+/) {
  133:         print "\n".
  134:               &mt('You need to stop the LON-CAPA daemons before moving Construction Spaces.')."\n".
  135:               &mt('To do so use the following command: [_1]',"\n\n$stoploncontrol")."\n\n".
  136:               &mt('Now stopping the move_construction_spaces.pl script.')."\n";
  137:         exit;        
  138:     }
  139: }
  140: 
  141: # Abort if more than one argument.
  142: 
  143: my $parameter=$ARGV[0];
  144: $parameter =~ s/^\s+//;
  145: $parameter =~ s/\s+$//;
  146: 
  147: if ((@ARGV > 1) || (($parameter ne '') && ($parameter !~ /^(move|undo)$/))) {
  148:     print &mt('usage: [_1]','move_construction_spaces.pl [move|undo]')."\n\n".
  149:           &mt('You should enter either no arguments, or just one argument -- either move or undo.')."\n".
  150:           &mt("move - to move authors' Construction Spaces from: [_1] to [_2].",
  151:               "'/home'","'$londocroot/priv/'")."\n".
  152:           &mt('undo - to reverse those changes and move Construction Spaces back from: [_1] to [_2].',
  153:               "'$londocroot/priv/'","'/home'")."\n".
  154:           &mt('no argument to do a dry run of the move option, without actually moving anything.')."\n";
  155:     exit;
  156: }
  157: 
  158: print "\n".&mt("Moving authors' Construction Spaces.")."\n".
  159:       "-----------------------------\n\n".
  160:       &mt('If run without an argument, the script will report what it would do when moving Construction Spaces from [_1] to [_2].',
  161:           "'/home'","'$londocroot/priv/'")."\n\n".
  162:       &mt('If there are ambiguities (i.e., the same username belongs to two domains), this will be flagged, and you will be able to decide how to proceed.')."\n";
  163: 
  164: my (undef,undef,$uid,$gid) = getpwnam('www');
  165: my ($action) = ($parameter=~/^(move|undo)$/);
  166: if ($action eq '') {
  167:     $action = 'dryrun';
  168: }
  169: 
  170: if ($action eq 'dryrun') {
  171:     print "\n".
  172:           &mt('Running in exploratory mode ...')."\n\n".
  173:           &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',
  174:               "'move'","'$londocroot/priv'","\n\nperl move_construction_spaces.pl move")."\n\n\n".
  175:           &mt('Run with argument [_1] to move Construction spaces back to [_2], i.e., [_3]',
  176:               "'undo'","'/home'","\n\nperl move_construction_spaces.pl undo")."\n\n\n".
  177:           &mt('Continue? ~[y/N~] ');
  178:     if (!&get_user_selection()) {
  179:         exit;
  180:     } else {
  181:         print "\n";
  182:     }
  183: } else {
  184:     print "\n *** ".&mt('Running in a mode where changes will be made.')." ***\n";
  185:     if ($action eq 'move') {
  186:         print "\n".
  187:               &mt('Mode is [_1] -- directories will be moved to [_2].',
  188:                   "'$action'","'$londocroot/priv'")."\n";
  189:     } else {
  190:         print "\n".
  191:               &mt('Mode is [_1] -- directories will be moved back to [_2].',
  192:                   "'$action'","'/home'")."\n";
  193:     }
  194:     print &mt('Continue? ~[y/N~] ');
  195:     if (!&get_user_selection()) {
  196:         exit;
  197:     } else {
  198:         print "\n";
  199:     }
  200: }
  201: 
  202: my $logfh;
  203: if ($action ne 'dryrun') {
  204:     if (!open($logfh,">>$londaemons/logs/move_construction_spaces.log")) {
  205:         print &mt('Could not open log file: [_1] for writing.',
  206:                   "'$londaemons/logs/move_construction_spaces.log'")."\n".
  207:               &mt('Stopping.')."\n";
  208:     } else {
  209:         &start_logging($logfh,$action);
  210:     }
  211: }
  212: 
  213: # Authors hosted on this server
  214: my %allauthors;
  215: my %pubusers;
  216: 
  217: if ($action eq 'move') {
  218:     my $output;
  219:     if (-d "$londocroot/priv") {
  220:         $output = &mt('New Construction Spaces directory: [_1] already exists.',
  221:                       "'$londocroot/priv'")."\n";
  222:         print $output;
  223:         print $logfh $output;
  224:     } else {
  225:         $output = &mt('Creating new directory: [_1] for Construction Spaces.',
  226:                       "'$londocroot/priv'")."\n";
  227:         if (mkdir("$londocroot/priv",0750)) {
  228:             if (chown($uid,$gid,"$londocroot/priv")) {
  229:                 $output .= &mt('Creation Successful')."\n";
  230:                 print $output;
  231:                 print $logfh $output;
  232:             } else {
  233:                 $output .= &mt('Failed to change ownership to [_1].',"'$uid:$gid'")."\n";
  234:                 print $output;
  235:                 &stop_logging($logfh,$output);
  236:                 print &mt('Stopping')."\n";
  237:                 exit;
  238:             }
  239:         } else {
  240:             $output .=  &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n";
  241:             print $output;
  242:             &stop_logging($logfh,$output);
  243:             print &mt('Stopping')."\n";
  244:             exit;
  245:         }
  246:     }
  247: }
  248: 
  249: my @machinedoms;
  250: if ($lonuserdir) {
  251:     my ($dir,$output);
  252:     if (opendir($dir,$lonuserdir)) {
  253:         my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
  254:         foreach my $item (@contents) {
  255:             if (-d "$lonuserdir/$item") {
  256:                 if ($item =~ /^$match_domain$/) {
  257:                     my $domain = $item;
  258:                     unless (grep(/^\Q$domain\E$/,@machinedoms)) {
  259:                         push(@machinedoms,$domain);  
  260:                     }
  261:                     my $dom_target="$londocroot/priv/$domain";
  262:                     if ($action eq 'move') {
  263:                         if (!-e $dom_target) {
  264:                             if (mkdir($dom_target,0755)) {
  265:                                 chown($uid,$gid,$dom_target);
  266:                                 $output = &mt('Made [_1].',"'$dom_target'")."\n";
  267:                                 print $output;
  268:                                 print $logfh $output;
  269:                             } else {
  270:                                 $output = &mt('Failed to make [_1].',"'$dom_target'")."\n";
  271:                                 print $output;
  272:                                 print $logfh $output;
  273:                                 &stop_logging($logfh,$output);
  274:                                 print &mt('Stopping')."\n";
  275:                                 exit;
  276:                             }
  277:                         } elsif ($action eq 'dryrun') {
  278:                             print &mt('Would make [_1].',"'$dom_target'")."\n";
  279:                         }
  280:                     }
  281:                     my %authors=();
  282:                     my $fname = "$lonuserdir/$domain/nohist_domainroles.db";
  283:                     my $dbref;
  284:                     if (-e $fname) {
  285:                         $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
  286:                     }
  287:                     if (!$dbref) {
  288:                         print &mt('Unable to tie to [_1].',"'$fname'")."\n";
  289:                     } elsif (ref($dbref) eq 'HASH') {
  290:                         foreach my $key (keys(%{$dbref})) {
  291:                             $key = &unescape($key);
  292:                             if ($key =~ /^au\:($match_username)\Q:$domain\E/) {
  293:                                 push(@{$allauthors{$1}},$domain);
  294:                             }
  295:                         }
  296:                         &LONCAPA::locking_hash_untie($dbref);
  297:                     }
  298:                 }
  299:             }
  300:         }
  301:         closedir($dir);
  302:     } else {
  303:         $output = &mt('Could not open [_1].',"'$lonuserdir'")."\n";
  304:         print $output;
  305:         &stop_logging($logfh,$output);
  306:         print &mt('Stopping')."\n";
  307:         exit;
  308:     }
  309: }
  310: 
  311: if ($londocroot ne '') {
  312:     if (-d "$londocroot/res") {
  313:         my ($dir,$domdir);
  314:         if (opendir($dir,"$londocroot/res")) {
  315:             my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
  316:             foreach my $dom (@contents) {
  317:                 if ((grep(/^\Q$dom\E/,@machinedoms)) && (-d "$londocroot/res/$dom")) {
  318:                     if (opendir($domdir,"$londocroot/res/$dom")) {
  319:                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
  320:                         foreach my $uname (@unames) {
  321:                             if ($uname =~ /^$match_username$/) {
  322:                                 push(@{$pubusers{$uname}},$dom);
  323:                             }
  324:                         }
  325:                     }
  326:                 }
  327:             }
  328:         }
  329:     }
  330: }
  331: 
  332: if ($action eq 'undo') {
  333:     my %privspaces;
  334:     if ($londocroot ne '') {
  335:         if (-d "$londocroot/priv") {
  336:             my ($dir,$domdir);
  337:             if (opendir($dir,"$londocroot/priv")) {
  338:                 my @contents = (grep(!/^\.{1,2}/,readdir($dir)));
  339:                 foreach my $dom (@contents) {
  340:                     next if (!-d "$londocroot/priv/$dom");
  341:                     if (opendir($domdir,"$londocroot/priv/$dom")) {
  342:                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
  343:                         foreach my $uname (@unames) {
  344:                             if ($uname =~ /^$match_username$/) {
  345:                                 push(@{$privspaces{$uname}},$dom);
  346:                             }
  347:                         }
  348:                     }
  349:                 }
  350:             }
  351:         }
  352:     }
  353:     foreach my $uname (keys(%privspaces)) {
  354:         if (ref($privspaces{$uname}) eq 'ARRAY') {
  355:             if (@{$privspaces{$uname}} > 1) {
  356:                 my $displaydoms = join(', ',@{$privspaces{$uname}});
  357:                 print &mt('Same username used for authors in multiple domains.')."\n".
  358:                       &mt('This configuration is not supported where Construction Spaces are located in [_1].','/home').".\n".
  359:                       &mt('You will be able to move files for just one of the domains, choose which one.')."\n".
  360:                       &mt('The domains to choose from are: [_1].',"'$displaydoms'")."\n".
  361:                       &mt('Enter choice: ');
  362:                 my $choice=<STDIN>;
  363:                 chomp($choice);
  364:                 if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
  365:                     my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice);
  366:                     print $output;
  367:                     print $logfh $output;
  368:                 } else {
  369:                     print &mt('Invalid choice of domain:')." $choice\n";
  370:                     my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
  371:                     print $output;
  372:                     print $logfh $output;
  373:                     next;
  374:                 }
  375:             } elsif (@{$privspaces{$uname}} == 1) {
  376:                 my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]);
  377:                 print $output;
  378:                 print $logfh $output;
  379:             } else {
  380:                 print &mt('Username [_1] found in [_2] was not within a domain',
  381:                           "'$uname'","'$londocroot/priv'")."\n";
  382:                 my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
  383:                 print $output;
  384:                 print $logfh $output;
  385:             }
  386:         }
  387:     }
  388:     &stop_logging($logfh);
  389:     print "\n".&mt('Done')."\n";
  390:     exit;
  391: }
  392: 
  393: # Iterate over directories in /home
  394: if (opendir(my $dir,"/home")) {
  395:     foreach my $item (grep(!/^\.{1,2}$/,readdir($dir))) {
  396:         next if ($item eq 'www');
  397:         if ((-d "/home/$item") && ($item ne '')) {
  398: # Is there a public_html-directory?
  399:             if (-d "/home/$item/public_html") {
  400:                 my $author = $item;
  401:                 my ($domain,$skipped,$output);
  402:                 if (ref($pubusers{$author}) eq 'ARRAY') {
  403:                     ($domain,$skipped) = &choose_domain($action,$author,$pubusers{$author});
  404:                 }
  405:                 if (($domain eq '') && (!$skipped)) {
  406:                     if (ref($allauthors{$author}) eq 'ARRAY') {
  407:                         ($domain,$skipped) = &choose_domain($action,$author,$allauthors{$author});
  408:                     }
  409:                 }
  410:                 my $source_path="/home/$author/public_html";
  411:                 if ($domain) { 
  412:                     my $target_path="$londocroot/priv/$domain/$author";
  413:                     if ($action eq 'move') {
  414:                         if (move($source_path,$target_path)) {
  415:                             chown($uid,$gid,$target_path);
  416:                             chmod($target_path,0750);
  417:                             $output = &mt('Moved [_1] to [_2].',
  418:                                           "'$source_path'","'$target_path'")."\n";
  419:                             my (undef,undef,$userid,$groupid) = getpwnam($author);
  420:                             if ($userid eq '' && $groupid eq '' && $author ne '') {
  421:                                 &check_for_restore_files($londaemons,$author,$domain);
  422:                                 if (opendir(my $homedir,"/home/$author")) {
  423:                                     my @contents = 
  424:                                         grep(!/^\.{1,2}$/,readdir($homedir));
  425:                                     if (@contents == 0) {
  426:                                         if (rmdir("/home/$author/")) {
  427:                                             $output .= &mt('Removed empty directory: [_1]',
  428:                                                            "'/home/$author/'")."\n";
  429:                                         } else {
  430:                                             $output .= &mt('Failed to remove directory: [_1]',
  431:                                                            "'/home/$author/'")."\n";
  432:                                         }
  433:                                     } else {
  434:                                         $output .= &mt('Not removing directory [_1] as it still contains: [_2]',
  435:                                                    "'/home/$author/'",
  436:                                                    "\n".join("\n",@contents)."\n");
  437:                                     }
  438:                                 }
  439:                             } else {
  440:                                 $output .= &mt('Not removing directory [_1] for UNIX user account',
  441:                                                "'/home/$author/'")."\n";  
  442:                             }
  443:                         } else {
  444:                             $output = &mt('Failed to move [_1] to [_2].',
  445:                                           "'$source_path'","'$target_path'")."\n";
  446:                         }
  447:                         print $output;
  448:                         print $logfh $output;
  449:                     } elsif ($action eq 'dryrun') {
  450:                         print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
  451:                     }
  452:                 } elsif ($skipped) {
  453:                     if ($action ne 'dryrun') {
  454:                         print $logfh &mt('Skipping this user: [_1].',"'$author'")."\n"; 
  455:                     }
  456:                 } else {
  457:                     print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".
  458:                           &mt('Enter [_1]: do nothing, continue.','1')."\n".
  459:                           &mt('Enter [_2]: stop.','2')."\n".
  460:                           &mt('or enter domain for user to be placed into')."\n".
  461:                           &mt('Your input: ');
  462:                     my $choice=<STDIN>;
  463:                     chomp($choice);
  464:                     if ($choice ==1) {
  465:                         print $logfh &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n";
  466:                     }
  467:                     if ($choice == 2) {
  468:                         print &mt('Stopped.')."\n";
  469:                         if ($action ne 'dryrun') {
  470:                             my $output = &mt('Stopped by user because of author without domain: [_1].',
  471:                                              "'$author'")/"\n";
  472:                             &stop_logging($logfh,$output); 
  473:                         }
  474:                         exit;
  475:                     } 
  476:                     if ($choice =~ /^$match_domain$/) {
  477:                         my $dompath="$londocroot/priv/$choice";
  478:                         my $newpath="$londocroot/priv/$choice/$author";
  479:                         unless (-e $dompath) {
  480:                             print '*** '.&mt('WARNING: [_1] does not yet exist.',"'$dompath'")."\n";
  481:                         }
  482:                         if ($action eq 'move') {
  483:                             unless (-e $dompath) {
  484:                                 $output .= &mt('Making [_1].',"'$dompath'")."\n";
  485:                                 if (mkdir($dompath,0755)) {
  486:                                     chown($uid,$gid,$dompath);
  487:                                 }
  488:                             }
  489:                             if (-e $dompath) {
  490:                                 if (move($source_path,$newpath)) {
  491:                                     chown($uid,$gid,$newpath);
  492:                                     chmod($newpath,0750);
  493:                                     $output = &mt('Moved [_1] to [_2].',
  494:                                                   "'$source_path'","'$newpath'")."\n";
  495:                                 } else {
  496:                                     $output = &mt('Failed to move [_1] to [_2].',
  497:                                                   "'$source_path'","'$newpath'")."\n"; 
  498:                                 }
  499:                                 print $output;
  500:                                 print $logfh $output;
  501:                             } else {
  502:                                 $output = &mt('Failed to move [_1] to [_2] -- missing [_3].',
  503:                                               "'$source_path'","'$newpath'","'$dompath'")."\n";
  504:                             }
  505:                         } elsif ($action eq 'dryrun') {
  506:                            print &mt('Would make author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
  507:                            unless (-e $dompath) {
  508:                                print &mt('Would make [_1].',"'$dompath'")."\n";
  509:                            }
  510:                            print &mt('Would make [_1].',"'$newpath'")."\n";
  511:                         }
  512:                     }
  513:                 }
  514:             }
  515:         }
  516:     }
  517: }
  518: if ($action ne 'dryrun') {
  519:     &stop_logging($logfh);
  520: }
  521: print "\n".&mt('Done.')."\n";
  522: 
  523: sub choose_domain { 
  524:     my ($action,$author,$domarrayref) = @_;
  525:     my ($domain,$skipped);
  526:     if (ref($domarrayref) eq 'ARRAY') {
  527:          if (@{$domarrayref} > 1) {
  528:              print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".
  529:                    &mt('Enter a number to choose what action to take.')."\n";
  530:              my $num = 1;
  531:              for (my $i=0; $i<@{$domarrayref}; $i++) {
  532:                  print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n";
  533:                  $num ++;
  534:              }
  535:              print &mt('To skip this user enter: [_1].',$num)."\n".
  536:                    &mt('Your choice:').' ';
  537:              my $choice=<STDIN>;
  538:              chomp($choice);
  539:              if ($choice =~ /^\d+$/) {
  540:                  if (($choice == $num) || ($choice > $num)) {
  541:                      $skipped = 1;       
  542:                  } elsif (($choice < $num) && ($choice > 0)) {
  543:                      $domain = $domarrayref->[$choice-1];
  544:                  } else {
  545:                      print &mt('Invalid choice:')." $choice\n";
  546:                      $skipped = 1;
  547:                  }
  548:              } else {
  549:                  print &mt('Invalid choice:')." $choice\n";
  550:                  $skipped = 1;
  551:              }
  552:          } elsif (@{$domarrayref} == 1) {
  553:              $domain = $domarrayref->[0];
  554:          }
  555:     }
  556:     return ($domain,$skipped);
  557: }
  558: 
  559: sub move_priv_to_home {
  560:     my ($londocroot,$uid,$gid,$uname,$domain) = @_;
  561:     my $output;
  562:     if ($uname =~ /^$match_username$/ && $domain =~ /^$match_domain$/) {
  563:         my $source_path="$londocroot/priv/$domain/$uname";
  564:         my $target_path="/home/$uname/public_html";
  565:         if (!-e "/home/$uname") {
  566:             my (undef,undef,$userid,$groupid) = getpwnam($uname);
  567:             if (mkdir("/home/$uname",0750)) {
  568:                 if ($userid ne '' && $groupid ne '') {
  569:                     chown($userid,$groupid,"/home/$uname");
  570:                 }
  571:             } else {
  572:                 $output = &mt('Failed to create directory [_1] -- not moving [_2].',
  573:                           "'/home/$uname'","'$source_path'")."\n";
  574:                 return $output;
  575:             }
  576:         }
  577:         if (-e "/home/$uname") {
  578:             if (!-e $target_path) {
  579:                 move($source_path,$target_path);
  580:                 chown($uid,$gid,$target_path);
  581:                 chmod($target_path,2770);
  582:                 $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
  583:             } else {
  584:                 $output = &mt('Directory [_1] already exists -- not moving [_2].',
  585:                               "'$target_path'","'$source_path'")."\n";
  586:             }
  587:         }
  588:     }
  589:     return $output;
  590: }
  591: 
  592: sub get_user_selection {
  593:     my ($defaultrun) = @_;
  594:     my $do_action = 0;
  595:     my $choice = <STDIN>;
  596:     chomp($choice);
  597:     $choice =~ s/(^\s+|\s+$)//g;
  598:     my $yes = &mt('y');
  599:     if ($defaultrun) {
  600:         if (($choice eq '') || ($choice =~ /^\Q$yes\E/i)) {
  601:             $do_action = 1;
  602:         }
  603:     } else {
  604:         if ($choice =~ /^\Q$yes\E/i) {
  605:             $do_action = 1;
  606:         }
  607:     }
  608:     return $do_action;
  609: }
  610: 
  611: sub start_logging {
  612:     my ($fh,$action) = @_;
  613:     my $start = localtime(time);
  614:     print $fh "*****************************************************\n".
  615:               &mt('[_1] - mode is [_2].',
  616:                   'move_construction_spaces.pl',"'$action'")."\n".
  617:               &mt('Started -- time: [_1]',$start)."\n".
  618:               "*****************************************************\n\n";
  619:     return;
  620: }
  621: 
  622: sub stop_logging {
  623:     my ($fh) = @_;
  624:     my $end = localtime(time);
  625:     print $fh "*****************************************************\n".
  626:                &mt('Ended -- time: [_1]',$end)."\n".
  627:               "*****************************************************\n\n\n";
  628:     close($fh);
  629:     return;
  630: }
  631: 
  632: sub check_for_restore_files {
  633:     my ($londaemons,$author,$domain) = @_;
  634:     if (opendir(my $homedir,"/home/$author")) {
  635:         my @contents = grep(!/^\.{1,2}$/,readdir($homedir));
  636:         if (@contents > 0) {
  637:             if (grep(/^restore_\d+\.sh$/,@contents)) {
  638:                 if (!-e "$londaemons/logs/moved_construction_spaces") { 
  639:                     mkdir("$londaemons/logs/moved_construction_spaces",0755);
  640:                 }
  641:                 if (!-e "$londaemons/logs/moved_construction_spaces/$domain") {
  642:                     mkdir("$londaemons/logs/moved_construction_spaces/$domain",0755);
  643:                 }
  644:                 if (-e "$londaemons/logs/moved_construction_spaces/$domain") {
  645:                     if (open(my $restorefh,">>$londaemons/logs/moved_construction_spaces/$domain/$author")) {
  646:                         foreach my $item (@contents) {
  647:                             if ($item =~ /^restore_\d+\.sh$/) {
  648:                                 my @stats = stat("/home/$author/$item");
  649:                                 my $lastmod = $stats[9];
  650:                                 if (open(my $fh,"</home/$author/$item")) {
  651:                                     print $restorefh
  652:                                           "*******************************\n".
  653:                                           "$item -- ".localtime(time)."\n".
  654:                                           "*******************************\n";
  655:                                     while (<$fh>) {
  656:                                         print $restorefh $_;
  657:                                     }
  658:                                     print $restorefh
  659:                                           "*******************************\n\n";
  660:                                     close($fh);
  661:                                     unlink("/home/$author/$item");
  662:                                 }
  663:                             }
  664:                         }
  665:                         close($restorefh); 
  666:                     }
  667:                 }
  668:             }
  669:         }
  670:     }
  671:     return;
  672: }
  673: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>