Annotation of loncom/debugging_tools/move_construction_spaces.pl, revision 1.4
1.1 raeburn 1: #!/usr/bin/perl
2: #
1.3 raeburn 3: # The LearningOnline Network
4: #
1.1 raeburn 5: # Move Construction Spaces from /home/$user/public_html
6: # to /home/httpd/html/priv/$domain/$user and vice versa
1.3 raeburn 7: #
1.4 ! raeburn 8: # $Id: move_construction_spaces.pl,v 1.3 2011/10/27 03:31:50 raeburn Exp $
1.3 raeburn 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: #################################################
1.1 raeburn 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);
1.2 raeburn 44:
45: if ($< != 0) {
1.3 raeburn 46: print &mt('You must be root in order to move Construction Spaces.')."\n".
47: &mt('Stopping')."\n";
1.2 raeburn 48: exit;
49: }
50:
51: my $perlvar=&LONCAPA::Configuration::read_conf();
1.3 raeburn 52: my ($lonuserdir,$londocroot,$londaemons);
1.2 raeburn 53: if (ref($perlvar) eq 'HASH') {
54: $lonuserdir = $perlvar->{'lonUsersDir'};
55: $londocroot = $perlvar->{'lonDocRoot'};
1.3 raeburn 56: $londaemons = $perlvar->{'lonDaemons'};
1.2 raeburn 57: }
58: undef($perlvar);
1.1 raeburn 59:
1.4 ! raeburn 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:
1.3 raeburn 72: my $distro;
1.4 ! raeburn 73: if ($londaemons eq '') {
! 74: print &mt('Could not determine location of [_1] directory.',"'lonDaemons'")."\n".
! 75: &mt('Stopping')."\n";
! 76: exit;
! 77: } else {
1.3 raeburn 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:
1.1 raeburn 141: # Abort if more than one argument.
1.2 raeburn 142:
143: my $parameter=$ARGV[0];
144: $parameter =~ s/^\s+//;
145: $parameter =~ s/\s+$//;
146:
147: if ((@ARGV > 1) || (($parameter ne '') && ($parameter !~ /^(move|undo)$/))) {
1.1 raeburn 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".
1.2 raeburn 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".
1.1 raeburn 154: &mt('no argument to do a dry run of the move option, without actually moving anything.')."\n";
155: exit;
156: }
157:
1.2 raeburn 158: print "\n".&mt("Moving authors' Construction Spaces.")."\n".
1.1 raeburn 159: "-----------------------------\n\n".
1.2 raeburn 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";
1.1 raeburn 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') {
1.2 raeburn 171: print "\n".
1.3 raeburn 172: &mt('Running in exploratory mode ...')."\n\n".
1.2 raeburn 173: &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',
1.3 raeburn 174: "'move'","'$londocroot/priv'","\n\nperl move_construction_spaces.pl move")."\n\n\n".
1.2 raeburn 175: &mt('Run with argument [_1] to move Construction spaces back to [_2], i.e., [_3]',
1.3 raeburn 176: "'undo'","'/home'","\n\nperl move_construction_spaces.pl undo")."\n\n\n".
1.2 raeburn 177: &mt('Continue? ~[y/N~] ');
178: if (!&get_user_selection()) {
179: exit;
1.3 raeburn 180: } else {
181: print "\n";
1.2 raeburn 182: }
1.1 raeburn 183: } else {
1.3 raeburn 184: print "\n *** ".&mt('Running in a mode where changes will be made.')." ***\n";
1.1 raeburn 185: if ($action eq 'move') {
1.2 raeburn 186: print "\n".
187: &mt('Mode is [_1] -- directories will be moved to [_2].',
188: "'$action'","'$londocroot/priv'")."\n";
1.1 raeburn 189: } else {
1.2 raeburn 190: print "\n".
191: &mt('Mode is [_1] -- directories will be moved back to [_2].',
192: "'$action'","'/home'")."\n";
1.1 raeburn 193: }
194: print &mt('Continue? ~[y/N~] ');
195: if (!&get_user_selection()) {
196: exit;
1.3 raeburn 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);
1.1 raeburn 210: }
211: }
212:
213: # Authors hosted on this server
214: my %allauthors;
215: my %pubusers;
216:
217: if ($action eq 'move') {
1.3 raeburn 218: my $output;
1.1 raeburn 219: if (-d "$londocroot/priv") {
1.3 raeburn 220: $output = &mt('New Construction Spaces directory: [_1] already exists.',
221: "'$londocroot/priv'")."\n";
222: print $output;
223: print $logfh $output;
1.1 raeburn 224: } else {
1.3 raeburn 225: $output = &mt('Creating new directory: [_1] for Construction Spaces.',
226: "'$londocroot/priv'")."\n";
227: if (mkdir("$londocroot/priv",0750)) {
1.1 raeburn 228: if (chown($uid,$gid,"$londocroot/priv")) {
1.3 raeburn 229: $output .= &mt('Creation Successful')."\n";
230: print $output;
231: print $logfh $output;
1.1 raeburn 232: } else {
1.3 raeburn 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";
1.1 raeburn 237: exit;
238: }
239: } else {
1.3 raeburn 240: $output .= &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n";
241: print $output;
242: &stop_logging($logfh,$output);
243: print &mt('Stopping')."\n";
1.2 raeburn 244: exit;
1.1 raeburn 245: }
246: }
247: }
248:
249: my @machinedoms;
250: if ($lonuserdir) {
1.3 raeburn 251: my ($dir,$output);
1.1 raeburn 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: }
1.4 ! raeburn 261: my $dom_target="$londocroot/priv/$domain";
1.1 raeburn 262: if ($action eq 'move') {
263: if (!-e $dom_target) {
264: if (mkdir($dom_target,0755)) {
265: chown($uid,$gid,$dom_target);
1.3 raeburn 266: $output = &mt('Made [_1].',"'$dom_target'")."\n";
267: print $output;
268: print $logfh $output;
1.1 raeburn 269: } else {
1.3 raeburn 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";
1.1 raeburn 275: exit;
276: }
277: } elsif ($action eq 'dryrun') {
1.2 raeburn 278: print &mt('Would make [_1].',"'$dom_target'")."\n";
1.1 raeburn 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) {
1.2 raeburn 288: print &mt('Unable to tie to [_1].',"'$fname'")."\n";
1.1 raeburn 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 {
1.3 raeburn 303: $output = &mt('Could not open [_1].',"'$lonuserdir'")."\n";
304: print $output;
305: &stop_logging($logfh,$output);
306: print &mt('Stopping')."\n";
1.1 raeburn 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) {
1.2 raeburn 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: ');
1.1 raeburn 362: my $choice=<STDIN>;
363: chomp($choice);
364: if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
1.3 raeburn 365: my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice);
366: print $output;
367: print $logfh $output;
1.1 raeburn 368: } else {
1.3 raeburn 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;
1.1 raeburn 373: next;
374: }
375: } elsif (@{$privspaces{$uname}} == 1) {
1.3 raeburn 376: my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]);
377: print $output;
378: print $logfh $output;
1.1 raeburn 379: } else {
1.2 raeburn 380: print &mt('Username [_1] found in [_2] was not within a domain',
381: "'$uname'","'$londocroot/priv'")."\n";
1.3 raeburn 382: my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
383: print $output;
384: print $logfh $output;
1.1 raeburn 385: }
386: }
387: }
1.3 raeburn 388: &stop_logging($logfh);
389: print "\n".&mt('Done')."\n";
1.1 raeburn 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');
1.3 raeburn 397: if ((-d "/home/$item") && ($item ne '')) {
1.1 raeburn 398: # Is there a public_html-directory?
399: if (-d "/home/$item/public_html") {
400: my $author = $item;
1.3 raeburn 401: my ($domain,$skipped,$output);
1.1 raeburn 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: }
1.3 raeburn 410: my $source_path="/home/$author/public_html";
1.1 raeburn 411: if ($domain) {
412: my $target_path="$londocroot/priv/$domain/$author";
413: if ($action eq 'move') {
1.3 raeburn 414: if (move($source_path,$target_path)) {
1.1 raeburn 415: chown($uid,$gid,$target_path);
1.3 raeburn 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;
1.1 raeburn 449: } elsif ($action eq 'dryrun') {
1.2 raeburn 450: print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
1.1 raeburn 451: }
1.3 raeburn 452: } elsif ($skipped) {
453: if ($action ne 'dryrun') {
454: print $logfh &mt('Skipping this user: [_1].',"'$author'")."\n";
455: }
456: } else {
1.2 raeburn 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: ');
1.1 raeburn 462: my $choice=<STDIN>;
463: chomp($choice);
1.3 raeburn 464: if ($choice ==1) {
465: print $logfh &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n";
466: }
1.2 raeburn 467: if ($choice == 2) {
468: print &mt('Stopped.')."\n";
1.3 raeburn 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: }
1.2 raeburn 474: exit;
475: }
1.1 raeburn 476: if ($choice =~ /^$match_domain$/) {
477: my $dompath="$londocroot/priv/$choice";
478: my $newpath="$londocroot/priv/$choice/$author";
479: unless (-e $dompath) {
1.2 raeburn 480: print '*** '.&mt('WARNING: [_1] does not yet exist.',"'$dompath'")."\n";
1.1 raeburn 481: }
482: if ($action eq 'move') {
483: unless (-e $dompath) {
1.3 raeburn 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";
1.1 raeburn 504: }
505: } elsif ($action eq 'dryrun') {
1.2 raeburn 506: print &mt('Would make author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
1.1 raeburn 507: unless (-e $dompath) {
1.2 raeburn 508: print &mt('Would make [_1].',"'$dompath'")."\n";
1.1 raeburn 509: }
1.2 raeburn 510: print &mt('Would make [_1].',"'$newpath'")."\n";
1.1 raeburn 511: }
512: }
513: }
514: }
515: }
516: }
517: }
1.3 raeburn 518: if ($action ne 'dryrun') {
519: &stop_logging($logfh);
520: }
521: print "\n".&mt('Done.')."\n";
1.1 raeburn 522:
523: sub choose_domain {
524: my ($action,$author,$domarrayref) = @_;
525: my ($domain,$skipped);
526: if (ref($domarrayref) eq 'ARRAY') {
527: if (@{$domarrayref} > 1) {
1.2 raeburn 528: print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".
529: &mt('Enter a number to choose what action to take.')."\n";
1.1 raeburn 530: my $num = 1;
531: for (my $i=0; $i<@{$domarrayref}; $i++) {
1.2 raeburn 532: print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n";
1.1 raeburn 533: $num ++;
534: }
1.2 raeburn 535: print &mt('To skip this user enter: [_1].',$num)."\n".
536: &mt('Your choice:').' ';
1.1 raeburn 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 {
1.2 raeburn 545: print &mt('Invalid choice:')." $choice\n";
1.1 raeburn 546: $skipped = 1;
547: }
548: } else {
1.2 raeburn 549: print &mt('Invalid choice:')." $choice\n";
1.1 raeburn 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 {
1.3 raeburn 560: my ($londocroot,$uid,$gid,$uname,$domain) = @_;
561: my $output;
1.1 raeburn 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") {
1.3 raeburn 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: }
1.1 raeburn 571: } else {
1.3 raeburn 572: $output = &mt('Failed to create directory [_1] -- not moving [_2].',
1.2 raeburn 573: "'/home/$uname'","'$source_path'")."\n";
1.3 raeburn 574: return $output;
1.1 raeburn 575: }
576: }
1.3 raeburn 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: }
1.1 raeburn 587: }
588: }
1.3 raeburn 589: return $output;
1.1 raeburn 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:
1.3 raeburn 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>