File:  [LON-CAPA] / loncom / debugging_tools / move_construction_spaces.pl
Revision 1.3: download - view: text, annotated - select for diffs
Thu Oct 27 03:31:50 2011 UTC (12 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add LON-CAPA boilerplate (GPL etc.).
- Check if Apache is running -- if so provide directions on stopping httpd, and exit.
- Check if lond is running -- if so provide directions on stopping loncontrol, and exit.
- Add logging to /home/httpd/perl/logs/move_construction_spaces.log
- Moving from /home to /home/httpd/html/priv
  - Checks for "restore_O.sh" files in /home/<user> created by lchtmldir
    - writes content to /home/httpd/perl/logs/move_construction_spaces/<domain>/<user>
    - unlinks restore_O.sh file
  - Removes empty /home/<user> directory unless <user> has UNIX account on the server.

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

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