* Split BackupPC_Admin into a set of modules, one for each major action.
[BackupPC.git] / configure.pl
1 #!/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # configure.pl: Configuration and installation program for BackupPC
5 #
6 # DESCRIPTION
7 #
8 #   This script should be run as root:
9 #
10 #        perl configure.pl
11 #
12 #   The installation steps are described as the script runs.
13 #
14 # AUTHOR
15 #   Craig Barratt <cbarratt@users.sourceforge.net>
16 #
17 # COPYRIGHT
18 #   Copyright (C) 2001-2003  Craig Barratt
19 #
20 #   This program is free software; you can redistribute it and/or modify
21 #   it under the terms of the GNU General Public License as published by
22 #   the Free Software Foundation; either version 2 of the License, or
23 #   (at your option) any later version.
24 #
25 #   This program is distributed in the hope that it will be useful,
26 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
27 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 #   GNU General Public License for more details.
29 #
30 #   You should have received a copy of the GNU General Public License
31 #   along with this program; if not, write to the Free Software
32 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 #
34 #========================================================================
35 #
36 # Version 2.0.0_CVS, released 18 Jan 2003.
37 #
38 # See http://backuppc.sourceforge.net.
39 #
40 #========================================================================
41
42 use strict;
43 no  utf8;
44 use vars qw(%Conf %OrigConf);
45 use lib "./lib";
46
47 my @Packages = qw(ExtUtils::MakeMaker File::Path File::Spec File::Copy
48                   DirHandle Digest::MD5 Data::Dumper Getopt::Std
49                   BackupPC::Lib BackupPC::FileZIO);
50
51 foreach my $pkg ( @Packages ) {
52     eval "use $pkg";
53     next if ( !$@ );
54     die <<EOF;
55
56 BackupPC needs the package $pkg.  Please install $pkg
57 before installing BackupPC.
58
59 EOF
60 }
61
62 if ( $< != 0 ) {
63     print <<EOF;
64
65 This configure script should be run as root, rather than uid $<.
66 Provided uid $< has sufficient permissions to create the data and
67 install directories, then it should be ok to proceed.  Otherwise,
68 please quit and restart as root.
69
70 EOF
71     exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
72 }
73
74 print <<EOF;
75
76 Is this a new installation or upgrade for BackupPC?  If this is
77 an upgrade please tell me the full path of the existing BackupPC
78 configuration file (eg: /xxxx/conf/config.pl).  Otherwise, just
79 hit return.
80
81 EOF
82
83 #
84 # Check if this is an upgrade, in which case read the existing
85 # config file to get all the defaults.
86 #
87 my $ConfigPath = "";
88 while ( 1 ) {
89     $ConfigPath = prompt("--> Full path to existing conf/config.pl",
90                                     $ConfigPath);
91     last if ( $ConfigPath eq ""
92             || ($ConfigPath =~ /^\// && -r $ConfigPath && -w $ConfigPath) );
93     my $problem = "is not an absolute path";
94     $problem = "is not writable" if ( !-w $ConfigPath );
95     $problem = "is not readable" if ( !-r $ConfigPath );
96     $problem = "doesn't exist"   if ( !-f $ConfigPath );
97     print("The file '$ConfigPath' $problem.\n");
98 }
99 my $bpc;
100 if ( $ConfigPath ne "" && -r $ConfigPath ) {
101     (my $topDir = $ConfigPath) =~ s{/[^/]+/[^/]+$}{};
102     die("BackupPC::Lib->new failed\n")
103             if ( !($bpc = BackupPC::Lib->new($topDir, ".", 1)) );
104     %Conf = $bpc->Conf();
105     %OrigConf = %Conf;
106     $Conf{TopDir} = $topDir;
107     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}, 1);
108     if ( $err eq "" ) {
109         print <<EOF;
110
111 BackupPC is running on $Conf{ServerHost}.  You need to stop BackupPC
112 before you can upgrade the code.  Depending upon your installation,
113 you could run "/etc/init.d/backuppc stop".
114
115 EOF
116         exit(1);
117     }
118 }
119
120 #
121 # These are the programs whose paths we need to find
122 #
123 my %Programs = (
124     perl       => "PerlPath",
125     'gtar/tar' => "TarClientPath",
126     smbclient  => "SmbClientPath",
127     nmblookup  => "NmbLookupPath",
128     rsync      => "RsyncClientPath",
129     ping       => "PingPath",
130     df         => "DfPath",
131     'ssh/ssh2' => "SshPath",
132     sendmail   => "SendmailPath",
133     hostname   => "HostnamePath",
134 );
135
136 foreach my $prog ( sort(keys(%Programs)) ) {
137     my $path;
138     foreach my $subProg ( split(/\//, $prog) ) {
139         $path ||= FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
140                               $subProg);
141     }
142     $Conf{$Programs{$prog}} ||= $path;
143 }
144
145 while ( 1 ) {
146     print <<EOF;
147
148 I found the following locations for these programs:
149
150 EOF
151     foreach my $prog ( sort(keys(%Programs)) ) {
152         printf("    %-11s => %s\n", $prog, $Conf{$Programs{$prog}});
153     }
154     print "\n";
155     last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
156     foreach my $prog ( sort(keys(%Programs)) ) {
157         $Conf{$Programs{$prog}} = prompt("--> $prog path",
158                                          $Conf{$Programs{$prog}});
159     }
160 }
161
162 my $Perl56 = system($Conf{PerlPath}
163                         . q{ -e 'exit($^V && $^V ge v5.6.0 ? 1 : 0);'});
164
165 if ( !$Perl56 ) {
166     print <<EOF;
167
168 BackupPC needs perl version 5.6.0 or later.  $Conf{PerlPath} appears
169 to be an older version.  Please upgrade to a newer version of perl
170 and re-run this configure script.
171
172 EOF
173     exit(1);
174 }
175
176 print <<EOF;
177
178 Please tell me the hostname of the machine that BackupPC will run on.
179
180 EOF
181 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
182         if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
183 $Conf{ServerHost} = prompt("--> BackupPC will run on host", $Conf{ServerHost});
184
185 print <<EOF;
186
187 BackupPC should run as a dedicated user with limited privileges.  You
188 need to create a user.  This user will need read/write permission on
189 the main data directory and read/execute permission on the install
190 directory (these directories will be setup shortly).
191
192 The primary group for this user should also be chosen carefully.
193 By default the install directories will have group write permission.
194 The data directories and files will have group read permission but
195 no other permission.
196
197 EOF
198 my($name, $passwd, $Uid, $Gid);
199 while ( 1 ) {
200     $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
201                                  $Conf{BackupPCUser} || "backuppc");
202     ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
203     last if ( $name ne "" );
204     print <<EOF;
205
206 getpwnam() says that user $Conf{BackupPCUser} doesn't exist.  Please check the
207 name and verify that this user is in the passwd file.
208
209 EOF
210 }
211
212 print <<EOF;
213
214 Please specify an install directory for BackupPC.  This is where the
215 BackupPC scripts, library and documentation will be installed.
216
217 EOF
218
219 while ( 1 ) {
220     $Conf{InstallDir} = prompt("--> Install directory (full path)",
221                                $Conf{InstallDir});
222     last if ( $Conf{InstallDir} =~ /^\// );
223 }
224
225 print <<EOF;
226
227 Please specify a data directory for BackupPC.  This is where the
228 configuration files, LOG files and all the PC backups are stored.
229 This file system needs to be big enough to accommodate all the
230 PCs you expect to backup (eg: at least 1-2GB per machine).
231
232 EOF
233
234 while ( 1 ) {
235     $Conf{TopDir} = prompt("--> Data directory (full path)", $Conf{TopDir});
236     last if ( $Conf{TopDir} =~ /^\// );
237 }
238
239 if ( !defined($Conf{CompressLevel}) ) {
240     $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
241     if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
242         print <<EOF;
243
244 BackupPC can compress pool files, providing around a 40% reduction in pool
245 size (your mileage may vary). Specify the compression level (0 turns
246 off compression, and 1 to 9 represent good/fastest to best/slowest).
247 The recommended values are 0 (off) or 3 (reasonable compression and speed).
248 Increasing the compression level to 5 will use around 20% more cpu time
249 and give perhaps 2-3% more compression.
250
251 EOF
252     } elsif ( $ConfigPath eq "" ) {
253         print <<EOF;
254
255 BackupPC can compress pool files, but it needs the Compress::Zlib
256 package installed (see www.cpan.org). Compression will provide around a
257 40% reduction in pool size, at the expense of cpu time.  You can leave
258 compression off and run BackupPC without compression, in which case you
259 should leave the compression level at 0 (which means off).  You could
260 install Compress::Zlib and turn compression on later, but read the
261 documentation first about how to do this.  Or the better choice is
262 to quit, install Compress::Zlib, and re-run configure.pl.
263
264 EOF
265     } elsif ( $Conf{CompressLevel} ) {
266         $Conf{CompressLevel} = 0;
267         print <<EOF;
268
269 BackupPC now supports pool file compression.  Since you are upgrading
270 BackupPC you probably have existing uncompressed backups.  You have
271 several choices if you want to turn on compression.  You can run
272 the script BackupPC_compressPool to convert everything to compressed
273 form.  Or you can simply turn on compression, so that new backups
274 will be compressed.  This will increase the pool storage requirement,
275 since both uncompressed and compressed copies of files will be stored.
276 But eventually the old uncompressed backups will expire, recovering
277 the pool storage.  Please see the documentation for more details.
278
279 If you are not sure what to do, leave the Compression Level at 0,
280 which disables compression.  You can always read the documentation
281 and turn it on later.
282
283 EOF
284     } else {
285         $Conf{CompressLevel} = 0;
286         print <<EOF;
287
288 BackupPC now supports pool file compression, but it needs the
289 Compress::Zlib module (see www.cpan.org).  For now, leave
290 the compression level set at 0 to disable compression.  If you
291 want you can install Compress::Zlib and turn compression on.
292 Please see the documentation for more details about converting
293 old backups to compressed form.
294
295 EOF
296     }
297     while ( 1 ) {
298         $Conf{CompressLevel}
299                     = prompt("--> Compression level", $Conf{CompressLevel});
300         last if ( $Conf{CompressLevel} =~ /^\d+$/ );
301     }
302 }
303
304 print <<EOF;
305
306 BackupPC has a powerful CGI perl interface that runs under Apache.
307 A single executable needs to be installed in a cgi-bin directory.
308 This executable needs to run as set-uid $Conf{BackupPCUser}, or
309 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
310
311 Leave this path empty if you don't want to install the CGI interface.
312
313 EOF
314
315 while ( 1 ) {
316     $Conf{CgiDir} = prompt("--> CGI bin directory (full path)", $Conf{CgiDir});
317     last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
318 }
319
320 if ( $Conf{CgiDir} ne "" ) {
321
322     print <<EOF;
323
324 BackupPC's CGI script needs to display various GIF images that
325 should be stored where Apache can serve them.  They should be
326 placed somewher under Apache's DocumentRoot.  BackupPC also
327 needs to know the URL to access these images.  Example:
328
329     Apache image directory:  /usr/local/apache/htdocs/BackupPC
330     URL for image directory: /BackupPC
331
332 The URL for the image directory should start with a slash.
333
334 EOF
335     while ( 1 ) {
336         $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
337                                         $Conf{CgiImageDir});
338         last if ( $Conf{CgiImageDir} =~ /^\// );
339     }
340     while ( 1 ) {
341         $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host; starts with '/')",
342                                         $Conf{CgiImageDirURL});
343         last if ( $Conf{CgiImageDirURL} =~ /^\// );
344     }
345 }
346
347 print <<EOF;
348
349 Ok, we're about to:
350
351   - install the binaries, lib and docs in $Conf{InstallDir},
352   - create the data directory $Conf{TopDir},
353   - create/update the config.pl file $Conf{TopDir}/conf,
354   - optionally install the cgi-bin interface.
355
356 EOF
357
358 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
359
360 #
361 # Create install directories
362 #
363 foreach my $dir ( qw(bin doc
364                      lib/BackupPC/CGI
365                      lib/BackupPC/Lang
366                      lib/BackupPC/Xfer
367                      lib/BackupPC/Zip
368                  ) ) {
369     next if ( -d "$Conf{InstallDir}/$dir" );
370     mkpath("$Conf{InstallDir}/$dir", 0, 0775);
371     if ( !-d "$Conf{InstallDir}/$dir"
372             || !chown($Uid, $Gid, "$Conf{InstallDir}/$dir") ) {
373         die("Failed to create or chown $Conf{InstallDir}/$dir\n");
374     } else {
375         print("Created $Conf{InstallDir}/$dir\n");
376     }
377 }
378
379 #
380 # Create CGI image directory
381 #
382 foreach my $dir ( ($Conf{CgiImageDir}) ) {
383     next if ( $dir eq "" || -d $dir );
384     mkpath($dir, 0, 0775);
385     if ( !-d $dir || !chown($Uid, $Gid, $dir) ) {
386         die("Failed to create or chown $dir");
387     } else {
388         print("Created $dir\n");
389     }
390 }
391
392 #
393 # Create $TopDir's top-level directories
394 #
395 foreach my $dir ( qw(. conf pool cpool pc trash log) ) {
396     mkpath("$Conf{TopDir}/$dir", 0, 0750) if ( !-d "$Conf{TopDir}/$dir" );
397     if ( !-d "$Conf{TopDir}/$dir"
398             || !chown($Uid, $Gid, "$Conf{TopDir}/$dir") ) {
399         die("Failed to create or chown $Conf{TopDir}/$dir\n");
400     } else {
401         print("Created $Conf{TopDir}/$dir\n");
402     }
403 }
404
405 printf("Installing binaries in $Conf{InstallDir}/bin\n");
406 foreach my $prog ( qw(BackupPC BackupPC_dump BackupPC_link BackupPC_nightly
407         BackupPC_sendEmail BackupPC_tarCreate BackupPC_trashClean
408         BackupPC_tarExtract BackupPC_compressPool BackupPC_zcat
409         BackupPC_restore BackupPC_serverMesg BackupPC_zipCreate ) ) {
410     InstallFile("bin/$prog", "$Conf{InstallDir}/bin/$prog", 0555);
411 }
412
413 #
414 # Remove unused binaries from older versions
415 #
416 unlink("$Conf{InstallDir}/bin/BackupPC_queueAll");
417
418 printf("Installing library in $Conf{InstallDir}/lib\n");
419 foreach my $lib ( qw(
420         BackupPC/Lib.pm
421         BackupPC/FileZIO.pm
422         BackupPC/Attrib.pm
423         BackupPC/PoolWrite.pm
424         BackupPC/View.pm
425         BackupPC/Xfer/Tar.pm
426         BackupPC/Xfer/Smb.pm
427         BackupPC/Xfer/Rsync.pm
428         BackupPC/Xfer/RsyncFileIO.pm
429         BackupPC/Zip/FileMember.pm
430         BackupPC/Lang/en.pm
431         BackupPC/Lang/fr.pm
432         BackupPC/Lang/es.pm
433         BackupPC/Lang/de.pm
434         BackupPC/CGI/Browse.pm
435         BackupPC/CGI/DirHistory.pm
436         BackupPC/CGI/EmailSummary.pm
437         BackupPC/CGI/GeneralInfo.pm
438         BackupPC/CGI/HostInfo.pm
439         BackupPC/CGI/Lib.pm
440         BackupPC/CGI/LOGlist.pm
441         BackupPC/CGI/Queue.pm
442         BackupPC/CGI/RestoreFile.pm
443         BackupPC/CGI/RestoreInfo.pm
444         BackupPC/CGI/Restore.pm
445         BackupPC/CGI/StartStopBackup.pm
446         BackupPC/CGI/Summary.pm
447         BackupPC/CGI/View.pm
448     ) ) {
449     InstallFile("lib/$lib", "$Conf{InstallDir}/lib/$lib", 0444);
450 }
451
452 if ( $Conf{CgiImageDir} ne "" ) {
453     printf("Installing images in $Conf{CgiImageDir}\n");
454     foreach my $img ( <images/*> ) {
455         (my $destImg = $img) =~ s{^images/}{};
456         InstallFile($img, "$Conf{CgiImageDir}/$destImg", 0444, 1);
457     }
458 }
459
460 printf("Making init.d scripts\n");
461 foreach my $init ( qw(gentoo-backuppc gentoo-backuppc.conf linux-backuppc
462                       solaris-backuppc debian-backuppc suse-backuppc) ) {
463     InstallFile("init.d/src/$init", "init.d/$init", 0444);
464 }
465
466 printf("Installing docs in $Conf{InstallDir}/doc\n");
467 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
468     InstallFile("doc/$doc", "$Conf{InstallDir}/doc/$doc", 0444);
469 }
470
471 printf("Installing config.pl and hosts in $Conf{TopDir}/conf\n");
472 InstallFile("conf/hosts", "$Conf{TopDir}/conf/hosts", 0644)
473                     if ( !-f "$Conf{TopDir}/conf/hosts" );
474
475 #
476 # Now do the config file.  If there is an existing config file we
477 # merge in the new config file, adding any new configuration
478 # parameters and deleting ones that are no longer needed.
479 #
480 my $dest = "$Conf{TopDir}/conf/config.pl";
481 my ($newConf, $newVars) = ConfigParse("conf/config.pl");
482 my ($oldConf, $oldVars);
483 if ( -f $dest ) {
484     ($oldConf, $oldVars) = ConfigParse($dest);
485     $newConf = ConfigMerge($oldConf, $oldVars, $newConf, $newVars);
486 }
487 $Conf{EMailFromUserName}  ||= $Conf{BackupPCUser};
488 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
489
490 #
491 # Update various config parameters
492 #
493
494 #
495 # Guess $Conf{CgiURL}
496 #
497 if ( !defined($Conf{CgiURL}) ) {
498     if ( $Conf{CgiDir} =~ m{cgi-bin(/.*)} ) {
499         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin$1/BackupPC_Admin'";
500     } else {
501         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin/BackupPC_Admin'";
502     }
503 }
504
505 #
506 # The smbclient commands have moved from hard-coded to the config file.
507 # $Conf{SmbClientArgs} no longer exists, so merge it into the new
508 # commands if it still exists.
509 #
510 if ( defined($Conf{SmbClientArgs}) ) {
511     if ( $Conf{SmbClientArgs} ne "" ) {
512         foreach my $param ( qw(SmbClientRestoreCmd SmbClientFullCmd
513                                 SmbClientIncrCmd) ) {
514             $newConf->[$newVars->{$param}]{text}
515                             =~ s/(-E\s+-N)/$1 $Conf{SmbClientArgs}/;
516         }
517     }
518     delete($Conf{SmbClientArgs});
519 }
520
521 #
522 # IncrFill should now be off
523 #
524 $Conf{IncrFill} = 0;
525
526 #
527 # Figure out sensible arguments for the ping command
528 #
529 if ( defined($Conf{PingArgs}) ) {
530     $Conf{PingCmd} = '$pingPath ' . $Conf{PingArgs};
531 } elsif ( !defined($Conf{PingCmd}) ) {
532     if ( $^O eq "solaris" || $^O eq "sunos" ) {
533         $Conf{PingCmd} = '$pingPath -s $host 56 1';
534     } elsif ( ($^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd")
535             && !system("$Conf{PingClientPath} -c 1 -w 3 localhost") ) {
536         $Conf{PingCmd} = '$pingPath -c 1 -w 3 $host';
537     } else {
538         $Conf{PingCmd} = '$pingPath -c 1 $host';
539     }
540     delete($Conf{PingArgs});
541 }
542
543 #
544 # Figure out sensible arguments for the df command
545 #
546 if ( !defined($Conf{DfCmd}) ) {
547     if ( $^O eq "solaris" || $^O eq "sunos" ) {
548         $Conf{DfCmd} = '$dfPath -k $topDir';
549     }
550 }
551
552 #
553 # $Conf{SmbClientTimeout} is now $Conf{ClientTimeout}
554 #
555 if ( defined($Conf{SmbClientTimeout}) ) {
556     $Conf{ClientTimeout} = $Conf{SmbClientTimeout};
557     delete($Conf{SmbClientTimeout});
558 }
559
560 my $confCopy = "$dest.pre-__VERSION__";
561 if ( -f $dest && !-f $confCopy ) {
562     #
563     # Make copy of config file, preserving ownership and modes
564     #
565     printf("Making backup copy of $dest -> $confCopy\n");
566     my @stat = stat($dest);
567     my $mode = $stat[2];
568     my $uid  = $stat[4];
569     my $gid  = $stat[5];
570     die("can't copy($dest, $confCopy)\n")  unless copy($dest, $confCopy);
571     die("can't chown $uid, $gid $confCopy\n")
572                                            unless chown($uid, $gid, $confCopy);
573     die("can't chmod $mode $confCopy\n")   unless chmod($mode, $confCopy);
574 }
575 open(OUT, ">", $dest) || die("can't open $dest for writing\n");
576 binmode(OUT);
577 my $blockComment;
578 foreach my $var ( @$newConf ) {
579     if ( length($blockComment)
580           && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
581         $var->{text} = substr($var->{text}, length($blockComment));
582         $blockComment = undef;
583     }
584     $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
585     $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
586                 defined($Conf{$1}) && ref($Conf{$1}) eq ""
587                                    && $Conf{$1} ne $OrigConf{$1}
588                                    ? "\$Conf{$1}$2$Conf{$1}$4"
589                                    : "\$Conf{$1}$2$3$4"/emg;
590     print OUT $var->{text};
591 }
592 close(OUT);
593 if ( !defined($oldConf) ) {
594     die("can't chmod 0640 mode $dest\n")  unless chmod(0640, $dest);
595     die("can't chown $Uid, $Gid $dest\n") unless chown($Uid, $Gid, $dest);
596 }
597
598 if ( $Conf{CgiDir} ne "" ) {
599     printf("Installing cgi script BackupPC_Admin in $Conf{CgiDir}\n");
600     mkpath("$Conf{CgiDir}", 0, 0755);
601     InstallFile("cgi-bin/BackupPC_Admin", "$Conf{CgiDir}/BackupPC_Admin",
602                 04554);
603 }
604
605 print <<EOF;
606
607 Ok, it looks like we are finished.  There are several more things you
608 will need to do:
609
610   - Browse through the config file, $Conf{TopDir}/conf/config.pl,
611     and make sure all the settings are correct.  In particular, you
612     will need to set the smb share password and user name, backup
613     policies and check the email message headers and bodies.
614
615   - Edit the list of hosts to backup in $Conf{TopDir}/conf/hosts.
616
617   - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
618     Please pay special attention to the security section.
619
620   - Verify that the CGI script BackupPC_Admin runs correctly.  You might
621     need to change the permissions or group ownership of BackupPC_Admin.
622
623   - BackupPC should be ready to start.  Don't forget to run it
624     as user $Conf{BackupPCUser}!  The installation also contains an
625     init.d/backuppc script that can be copied to /etc/init.d
626     so that BackupPC can auto-start on boot.  See init.d/README.
627
628 Enjoy!
629 EOF
630
631 if ( $ENV{LANG} =~ /utf/i && $^V ge v5.8.0 ) {
632     print <<EOF;
633
634 WARNING: Your LANG environment variable is set to $ENV{LANG}, which
635 doesn't behave well with this version of perl.  Please set the
636 LANG environment variable to en_US before running BackupPC.
637
638 On RH-8 this setting is in the file /etc/sysconfig/i18n, or you
639 could set it in BackupPC's init.d script.
640 EOF
641 }
642
643 exit(0);
644
645 ###########################################################################
646 # Subroutines
647 ###########################################################################
648
649 sub InstallFile
650 {
651     my($prog, $dest, $mode, $binary) = @_;
652     my $first = 1;
653     my($uid, $gid) = ($Uid, $Gid);
654
655     if ( -f $dest ) {
656         #
657         # preserve ownership and modes of files that already exist
658         #
659         my @stat = stat($dest);
660         $mode = $stat[2];
661         $uid  = $stat[4];
662         $gid  = $stat[5];
663     }
664     unlink($dest) if ( -f $dest );
665     if ( $binary ) {
666         die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
667     } else {
668         open(PROG, $prog)     || die("can't open $prog for reading\n");
669         open(OUT, ">", $dest) || die("can't open $dest for writing\n");
670         binmode(PROG);
671         binmode(OUT);
672         while ( <PROG> ) {
673             s/__INSTALLDIR__/$Conf{InstallDir}/g;
674             s/__TOPDIR__/$Conf{TopDir}/g;
675             s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
676             s/__CGIDIR__/$Conf{CgiDir}/g;
677             if ( $first && /^#.*bin\/perl/ ) {
678                 if ( $Perl56 ) {
679                     #
680                     # perl56 and later is taint ok
681                     #
682                     print OUT "#!$Conf{PerlPath} -T\n";
683                 } else {
684                     #
685                     # prior to perl56, File::Find fails taint checks,
686                     # so we run without -T.  It's still safe.
687                     #
688                     print OUT "#!$Conf{PerlPath}\n";
689                 }
690             } else {
691                 print OUT;
692             }
693             $first = 0;
694         }
695         close(PROG);
696         close(OUT);
697     }
698     die("can't chown $uid, $gid $dest") unless chown($uid, $gid, $dest);
699     die("can't chmod $mode $dest")      unless chmod($mode, $dest);
700 }
701
702 sub FindProgram
703 {
704     my($path, $prog) = @_;
705     foreach my $dir ( split(/:/, $path) ) {
706         my $file = File::Spec->catfile($dir, $prog);
707         return $file if ( -x $file );
708     }
709 }
710
711 sub ConfigParse
712 {
713     my($file) = @_;
714     open(C, $file) || die("can't open $file");
715     binmode(C);
716     my($out, @conf, $var);
717     my $comment = 1;
718     my $allVars = {};
719     while ( <C> ) {
720         if ( /^#/ ) {
721             if ( $comment ) {
722                 $out .= $_;
723             } else {
724                 if ( $out ne "" ) {
725                     $allVars->{$var} = @conf if ( defined($var) );
726                     push(@conf, {
727                         text => $out,
728                         var => $var,
729                     });
730                 }
731                 $var = undef;
732                 $comment = 1;
733                 $out = $_;
734             }
735         } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
736             $comment = 0;
737             if ( defined($var) ) {
738                 $allVars->{$var} = @conf if ( defined($var) );
739                 push(@conf, {
740                     text => $out,
741                     var => $var,
742                 });
743                 $out = $_;
744             } else {
745                 $out .= $_;
746             }
747             $var = $1;
748         } else {
749             $out .= $_;
750         }
751     }
752     if ( $out ne "" ) {
753         $allVars->{$var} = @conf if ( defined($var) );
754         push(@conf, {
755             text => $out,
756             var  => $var,
757         });
758     }
759     close(C);
760     return (\@conf, $allVars);
761 }
762
763 sub ConfigMerge
764 {
765     my($old, $oldVars, $new, $newVars) = @_;
766     my $posn = 0;
767     my $res;
768
769     #
770     # Find which config parameters are not needed any longer
771     #
772     foreach my $var ( @$old ) {
773         next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
774         #print(STDERR "Deleting old config parameter $var->{var}\n");
775         $var->{delete} = 1;
776     }
777     #
778     # Find which config parameters are new
779     #
780     foreach my $var ( @$new ) {
781         next if ( !defined($var->{var}) );
782         if ( defined($oldVars->{$var->{var}}) ) {
783             $posn = $oldVars->{$var->{var}};
784         } else {
785             #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
786             push(@{$old->[$posn]{new}}, $var);
787         }
788     }
789     #
790     # Create merged config file
791     #
792     foreach my $var ( @$old ) {
793         next if ( $var->{delete} );
794         push(@$res, $var);
795         foreach my $new ( @{$var->{new}} ) {
796             push(@$res, $new);
797         }
798     }
799     return $res;
800 }