additional changes to it.pm (post 3.0.0beta2)
[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 #   To read about the command-line options for this configure script:
13 #
14 #        perldoc configure.pl
15 #
16 #   The installation steps are described as the script runs.
17 #
18 # AUTHOR
19 #   Craig Barratt <cbarratt@users.sourceforge.net>
20 #
21 # COPYRIGHT
22 #   Copyright (C) 2001-2006  Craig Barratt
23 #
24 #   This program is free software; you can redistribute it and/or modify
25 #   it under the terms of the GNU General Public License as published by
26 #   the Free Software Foundation; either version 2 of the License, or
27 #   (at your option) any later version.
28 #
29 #   This program is distributed in the hope that it will be useful,
30 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
31 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32 #   GNU General Public License for more details.
33 #
34 #   You should have received a copy of the GNU General Public License
35 #   along with this program; if not, write to the Free Software
36 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37 #
38 #========================================================================
39 #
40 # Version 3.0.0alpha, released 8 Jul 2006.
41 #
42 # See http://backuppc.sourceforge.net.
43 #
44 #========================================================================
45
46 use strict;
47 no  utf8;
48 use vars qw(%Conf %OrigConf);
49 use lib "./lib";
50
51 my @Packages = qw(File::Path File::Spec File::Copy DirHandle Digest::MD5
52                   Data::Dumper Getopt::Std Getopt::Long Encode Pod::Usage
53                   BackupPC::Lib BackupPC::FileZIO);
54
55 foreach my $pkg ( @Packages ) {
56     eval "use $pkg";
57     next if ( !$@ );
58     if ( $pkg =~ /BackupPC/ ) {
59         die <<EOF;
60
61 Error loading $pkg: $@
62 BackupPC cannot load the package $pkg, which is included in the
63 BackupPC distribution.  This probably means you did not cd to the
64 unpacked BackupPC distribution before running configure.pl, eg:
65
66     cd BackupPC-__VERSION__
67     ./configure.pl
68
69 Please try again.
70
71 EOF
72     }
73     die <<EOF;
74
75 BackupPC needs the package $pkg.  Please install $pkg
76 before installing BackupPC.
77
78 EOF
79 }
80
81 my $EncodeVersion = eval($Encode::VERSION);
82 if ( $EncodeVersion < 1.99 ) {
83     print("\nError: you need to upgrade the Encode package;"
84         . " I found $EncodeVersion and BackupPC needs >= 1.99\n\n");
85     exit(1);
86 }
87
88 my %opts;
89 $opts{"set-perms"} = 1;
90 if ( !GetOptions(
91             \%opts,
92             "batch",
93             "backuppc-user=s",
94             "bin-path=s%",
95             "cgi-dir=s",
96             "compress-level=i",
97             "config-path=s",
98             "config-dir=s",
99             "data-dir=s",
100             "dest-dir=s",
101             "fhs!",
102             "help|?",
103             "hostname=s",
104             "html-dir=s",
105             "html-dir-url=s",
106             "install-dir=s",
107             "log-dir=s",
108             "man",
109             "set-perms!",
110             "uid-ignore!",
111         ) || @ARGV ) {
112     pod2usage(2);
113 }
114 pod2usage(1) if ( $opts{help} );
115 pod2usage(-exitstatus => 0, -verbose => 2) if $opts{man};
116
117 my $DestDir = $opts{"dest-dir"};
118 $DestDir = "" if ( $DestDir eq "/" );
119
120 if ( !$opts{"uid-ignore"} && $< != 0 ) {
121     print <<EOF;
122
123 This configure script should be run as root, rather than uid $<.
124 Provided uid $< has sufficient permissions to create the data and
125 install directories, then it should be ok to proceed.  Otherwise,
126 please quit and restart as root.
127
128 EOF
129     exit(1) if ( prompt("--> Do you want to continue?",
130                        "y") !~ /y/i );
131     exit(1) if ( $opts{batch} && !$opts{"uid-ignore"} );
132 }
133
134 #
135 # Whether we use the file system hierarchy conventions or not.
136 # Older versions did not.  BackupPC used to be installed in
137 # two main directories (in addition to CGI and html pages)
138 #
139 #    TopDir       which includes subdirs conf, log, pc, pool, cpool
140 #                
141 #    InstallDir   which includes subdirs bin, lib, doc
142 #
143 # With FHS enabled (which is the default for new installations)
144 # the config files move to /etc/BackupPC and log files to /var/log:
145 #
146 #    /etc/BackupPC/config.pl  main config file (was $TopDir/conf/config.pl)
147 #    /etc/BackupPC/hosts      hosts file (was $TopDir/conf/hosts)
148 #    /etc/BackupPC/pc/HOST.pl per-pc config file (was $TopDir/pc/HOST/config.pl)
149 #    /var/log/BackupPC        log files (was $TopDir/log)
150 #    /var/log/BackupPC        Pid, status and email info (was $TopDir/log)
151 #
152
153 #
154 # Check if this is an upgrade, in which case read the existing
155 # config file to get all the defaults.
156 #
157 my $ConfigPath = "";
158 my $ConfigFileOK = 1;
159 while ( 1 ) {
160     if ( $ConfigFileOK && -f "/etc/BackupPC/config.pl" ) {
161         $ConfigPath = "/etc/BackupPC/config.pl";
162         $opts{fhs} = 1 if ( !defined($opts{fhs}) );
163         print <<EOF;
164
165 Found /etc/BackupPC/config.pl, so this is an upgrade of an
166 existing BackupPC installation.  We will verify some existing
167 information, but you will probably not need to make any
168 changes - just hit ENTER to each question.
169 EOF
170     } else {
171         print <<EOF;
172
173 Is this a new installation or upgrade for BackupPC?  If this is
174 an upgrade please tell me the full path of the existing BackupPC
175 configuration file (eg: /etc/BackupPC/config.pl).  Otherwise, just
176 hit return.
177
178 EOF
179         $ConfigPath = prompt("--> Full path to existing main config.pl",
180                              $ConfigPath,
181                              "config-path");
182     }
183     last if ( $ConfigPath eq ""
184             || ($ConfigPath =~ /^\// && -f $ConfigPath && -w $ConfigPath) );
185     my $problem = "is not an absolute path";
186     $problem = "is not writable"        if ( !-w $ConfigPath );
187     $problem = "is not readable"        if ( !-r $ConfigPath );
188     $problem = "is not a regular file"  if ( !-f $ConfigPath );
189     $problem = "doesn't exist"          if ( !-e $ConfigPath );
190     print("The file '$ConfigPath' $problem.\n");
191     if ( $opts{batch} ) {
192         print("Need to specify a valid --config-path for upgrade\n");
193         exit(1);
194     }
195     $ConfigFileOK = 0;
196 }
197 $opts{fhs} = 1 if ( !defined($opts{fhs}) && $ConfigPath eq "" );
198 $opts{fhs} = 0 if ( !defined($opts{fhs}) );
199
200 my $bpc;
201 if ( $ConfigPath ne "" && -r $ConfigPath ) {
202     (my $confDir = $ConfigPath) =~ s{/[^/]+$}{};
203     die("BackupPC::Lib->new failed\n")
204             if ( !($bpc = BackupPC::Lib->new(".", ".", $confDir, 1)) );
205     %Conf = $bpc->Conf();
206     %OrigConf = %Conf;
207     if ( !$opts{fhs} ) {
208         ($Conf{TopDir} = $ConfigPath) =~ s{/[^/]+/[^/]+$}{}
209                     if ( $Conf{TopDir} eq '' );
210         $bpc->{LogDir} = $Conf{LogDir}  = "$Conf{TopDir}/log"
211                     if ( $Conf{LogDir} eq '' );
212     }
213     $bpc->{ConfDir} = $Conf{ConfDir} = $confDir;
214     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}, 1);
215     if ( $err eq "" ) {
216         print <<EOF;
217
218 BackupPC is running on $Conf{ServerHost}.  You need to stop BackupPC
219 before you can upgrade the code.  Depending upon your installation,
220 you could run "/etc/init.d/backuppc stop".
221
222 EOF
223         exit(1);
224     }
225 }
226
227 #
228 # Create defaults for FHS setup
229 #
230 if ( $opts{fhs} ) {
231     $Conf{TopDir}       ||= "/data/BackupPC";
232     $Conf{ConfDir}      ||= $opts{"config-dir"} || "/etc/BackupPC";
233     $Conf{InstallDir}   ||= "/usr/local/BackupPC";
234     $Conf{LogDir}       ||= $opts{"log-dir"} || "/var/log/BackupPC";
235 }
236
237 #
238 # These are the programs whose paths we need to find
239 #
240 my %Programs = (
241     perl           => "PerlPath",
242     'gtar/tar'     => "TarClientPath",
243     smbclient      => "SmbClientPath",
244     nmblookup      => "NmbLookupPath",
245     rsync          => "RsyncClientPath",
246     ping           => "PingPath",
247     df             => "DfPath",
248     'ssh/ssh2'     => "SshPath",
249     sendmail       => "SendmailPath",
250     hostname       => "HostnamePath",
251     split          => "SplitPath",
252     par2           => "ParPath",
253     cat            => "CatPath",
254     gzip           => "GzipPath",
255     bzip2          => "Bzip2Path",
256 );
257
258 foreach my $prog ( sort(keys(%Programs)) ) {
259     my $path;
260     foreach my $subProg ( split(/\//, $prog) ) {
261         $path = FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
262                             $subProg) if ( !length($path) );
263     }
264     $Conf{$Programs{$prog}} = $path if ( !length($Conf{$Programs{$prog}}) );
265 }
266
267 while ( 1 ) {
268     print <<EOF;
269
270 I found the following locations for these programs:
271
272 EOF
273     foreach my $prog ( sort(keys(%Programs)) ) {
274         printf("    %-12s => %s\n", $prog, $Conf{$Programs{$prog}});
275     }
276     print "\n";
277     last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
278     foreach my $prog ( sort(keys(%Programs)) ) {
279         $Conf{$Programs{$prog}} = prompt("--> $prog path",
280                                          $Conf{$Programs{$prog}});
281     }
282 }
283
284 my $Perl58 = system($Conf{PerlPath}
285                         . q{ -e 'exit($^V && $^V ge v5.8.0 ? 1 : 0);'});
286
287 if ( !$Perl58 ) {
288     print <<EOF;
289
290 BackupPC needs perl version 5.8.0 or later.  $Conf{PerlPath} appears
291 to be an older version.  Please upgrade to a newer version of perl
292 and re-run this configure script.
293
294 EOF
295     exit(1);
296 }
297
298 print <<EOF;
299
300 Please tell me the hostname of the machine that BackupPC will run on.
301
302 EOF
303 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
304         if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
305 $Conf{ServerHost} = prompt("--> BackupPC will run on host",
306                            $Conf{ServerHost},
307                            "hostname");
308
309 print <<EOF;
310
311 BackupPC should run as a dedicated user with limited privileges.  You
312 need to create a user.  This user will need read/write permission on
313 the main data directory and read/execute permission on the install
314 directory (these directories will be setup shortly).
315
316 The primary group for this user should also be chosen carefully.
317 The data directories and files will have group read permission,
318 so group members can access backup files.
319
320 EOF
321 my($name, $passwd, $Uid, $Gid);
322 while ( 1 ) {
323     $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
324                                  $Conf{BackupPCUser} || "backuppc",
325                                  "backuppc-user");
326     if ( $opts{"set-perms"} ) {
327         ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
328         last if ( $name ne "" );
329         print <<EOF;
330
331 getpwnam() says that user $Conf{BackupPCUser} doesn't exist.  Please
332 check the name and verify that this user is in the passwd file.
333
334 EOF
335         exit(1) if ( $opts{batch} );
336     }
337 }
338
339 print <<EOF;
340
341 Please specify an install directory for BackupPC.  This is where the
342 BackupPC scripts, library and documentation will be installed.
343
344 EOF
345
346 while ( 1 ) {
347     $Conf{InstallDir} = prompt("--> Install directory (full path)",
348                                $Conf{InstallDir},
349                                "install-dir");
350     last if ( $Conf{InstallDir} =~ /^\// );
351     if ( $opts{batch} ) {
352         print("Need to specify --install-dir for new installation\n");
353         exit(1);
354     }
355 }
356
357 print <<EOF;
358
359 Please specify a data directory for BackupPC.  This is where all the
360 PC backup data is stored.  This file system needs to be big enough to
361 accommodate all the PCs you expect to backup (eg: at least several GB
362 per machine).
363
364 EOF
365
366 while ( 1 ) {
367     $Conf{TopDir} = prompt("--> Data directory (full path)",
368                            $Conf{TopDir},
369                            "data-dir");
370     last if ( $Conf{TopDir} =~ /^\// );
371     if ( $opts{batch} ) {
372         print("Need to specify --data-dir for new installation\n");
373         exit(1);
374     }
375 }
376
377 $Conf{CompressLevel} = $opts{"compress-level"}
378                             if ( defined($opts{"compress-level"}) );
379
380 if ( !defined($Conf{CompressLevel}) ) {
381     $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
382     if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
383         print <<EOF;
384
385 BackupPC can compress pool files, providing around a 40% reduction in pool
386 size (your mileage may vary). Specify the compression level (0 turns
387 off compression, and 1 to 9 represent good/fastest to best/slowest).
388 The recommended values are 0 (off) or 3 (reasonable compression and speed).
389 Increasing the compression level to 5 will use around 20% more cpu time
390 and give perhaps 2-3% more compression.
391
392 EOF
393     } elsif ( $ConfigPath eq "" ) {
394         print <<EOF;
395
396 BackupPC can compress pool files, but it needs the Compress::Zlib
397 package installed (see www.cpan.org). Compression will provide around a
398 40% reduction in pool size, at the expense of cpu time.  You can leave
399 compression off and run BackupPC without compression, in which case you
400 should leave the compression level at 0 (which means off).  You could
401 install Compress::Zlib and turn compression on later, but read the
402 documentation first about how to do this.  Or the better choice is
403 to quit, install Compress::Zlib, and re-run configure.pl.
404
405 EOF
406     } elsif ( $Conf{CompressLevel} ) {
407         $Conf{CompressLevel} = 0;
408         print <<EOF;
409
410 BackupPC now supports pool file compression.  Since you are upgrading
411 BackupPC you probably have existing uncompressed backups.  You have
412 several choices if you want to turn on compression.  You can run
413 the script BackupPC_compressPool to convert everything to compressed
414 form.  Or you can simply turn on compression, so that new backups
415 will be compressed.  This will increase the pool storage requirement,
416 since both uncompressed and compressed copies of files will be stored.
417 But eventually the old uncompressed backups will expire, recovering
418 the pool storage.  Please see the documentation for more details.
419
420 If you are not sure what to do, leave the Compression Level at 0,
421 which disables compression.  You can always read the documentation
422 and turn it on later.
423
424 EOF
425     } else {
426         $Conf{CompressLevel} = 0;
427         print <<EOF;
428
429 BackupPC now supports pool file compression, but it needs the
430 Compress::Zlib module (see www.cpan.org).  For now, leave
431 the compression level set at 0 to disable compression.  If you
432 want you can install Compress::Zlib and turn compression on.
433 Please see the documentation for more details about converting
434 old backups to compressed form.
435
436 EOF
437     }
438     while ( 1 ) {
439         $Conf{CompressLevel}
440                     = prompt("--> Compression level", $Conf{CompressLevel});
441         last if ( $Conf{CompressLevel} =~ /^\d+$/ );
442     }
443 }
444
445 print <<EOF;
446
447 BackupPC has a powerful CGI perl interface that runs under Apache.
448 A single executable needs to be installed in a cgi-bin directory.
449 This executable needs to run as set-uid $Conf{BackupPCUser}, or
450 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
451
452 Leave this path empty if you don't want to install the CGI interface.
453
454 EOF
455
456 while ( 1 ) {
457     $Conf{CgiDir} = prompt("--> CGI bin directory (full path)",
458                            $Conf{CgiDir},
459                            "cgi-dir");
460     last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
461     if ( $opts{batch} ) {
462         print("Need to specify --cgi-dir for new installation\n");
463         exit(1);
464     }
465 }
466
467 if ( $Conf{CgiDir} ne "" ) {
468
469     print <<EOF;
470
471 BackupPC's CGI script needs to display various GIF images that
472 should be stored where Apache can serve them.  They should be
473 placed somewher under Apache's DocumentRoot.  BackupPC also
474 needs to know the URL to access these images.  Example:
475
476     Apache image directory:  /usr/local/apache/htdocs/BackupPC
477     URL for image directory: /BackupPC
478
479 The URL for the image directory should start with a slash.
480
481 EOF
482     while ( 1 ) {
483         $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
484                                     $Conf{CgiImageDir},
485                                     "html-dir");
486         last if ( $Conf{CgiImageDir} =~ /^\// );
487         if ( $opts{batch} ) {
488             print("Need to specify --html-dir for new installation\n");
489             exit(1);
490         }
491     }
492     while ( 1 ) {
493         $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host; starts with '/')",
494                                         $Conf{CgiImageDirURL},
495                                         "html-dir-url");
496         last if ( $Conf{CgiImageDirURL} =~ /^\// );
497         if ( $opts{batch} ) {
498             print("Need to specify --html-dir-url for new installation\n");
499             exit(1);
500         }
501     }
502 }
503
504 print <<EOF;
505
506 Ok, we're about to:
507
508   - install the binaries, lib and docs in $Conf{InstallDir},
509   - create the data directory $Conf{TopDir},
510   - create/update the config.pl file $Conf{ConfDir}/config.pl,
511   - optionally install the cgi-bin interface.
512
513 EOF
514
515 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
516
517 #
518 # Create install directories
519 #
520 foreach my $dir ( qw(bin doc
521                      lib/BackupPC/CGI
522                      lib/BackupPC/Config
523                      lib/BackupPC/Lang
524                      lib/BackupPC/Storage
525                      lib/BackupPC/Xfer
526                      lib/BackupPC/Zip
527                  ) ) {
528     next if ( -d "$DestDir$Conf{InstallDir}/$dir" );
529     mkpath("$DestDir$Conf{InstallDir}/$dir", 0, 0755);
530     if ( !-d "$DestDir$Conf{InstallDir}/$dir"
531             || !my_chown($Uid, $Gid, "$DestDir$Conf{InstallDir}/$dir") ) {
532         die("Failed to create or chown $DestDir$Conf{InstallDir}/$dir\n");
533     } else {
534         print("Created $DestDir$Conf{InstallDir}/$dir\n");
535     }
536 }
537
538 #
539 # Create CGI image directory
540 #
541 foreach my $dir ( ($Conf{CgiImageDir}) ) {
542     next if ( $dir eq "" || -d $dir );
543     mkpath("$DestDir$dir", 0, 0755);
544     if ( !-d "$DestDir$dir" || !my_chown($Uid, $Gid, "$DestDir$dir") ) {
545         die("Failed to create or chown $DestDir$dir");
546     } else {
547         print("Created $DestDir$dir\n");
548     }
549 }
550
551 #
552 # Create other directories
553 #
554 foreach my $dir ( (
555             "$Conf{TopDir}",
556             "$Conf{TopDir}/pool",
557             "$Conf{TopDir}/cpool",
558             "$Conf{TopDir}/pc",
559             "$Conf{TopDir}/trash",
560             "$Conf{ConfDir}",
561             "$Conf{LogDir}",
562         ) ) {
563     mkpath("$DestDir$dir", 0, 0750) if ( !-d "$DestDir$dir" );
564     if ( !-d "$DestDir$dir"
565             || !my_chown($Uid, $Gid, "$DestDir$dir") ) {
566         die("Failed to create or chown $DestDir$dir\n");
567     } else {
568         print("Created $DestDir$dir\n");
569     }
570 }
571
572 printf("Installing binaries in $DestDir$Conf{InstallDir}/bin\n");
573 foreach my $prog ( qw(
574         __CONFIGURE_BIN_LIST__
575     ) ) {
576     InstallFile($prog, "$DestDir$Conf{InstallDir}/$prog", 0555);
577 }
578
579 printf("Installing library in $DestDir$Conf{InstallDir}/lib\n");
580 foreach my $lib ( qw(
581         __CONFIGURE_LIB_LIST__
582     ) ) {
583     InstallFile($lib, "$DestDir$Conf{InstallDir}/$lib", 0444);
584 }
585
586 if ( $Conf{CgiImageDir} ne "" ) {
587     printf("Installing images in $DestDir$Conf{CgiImageDir}\n");
588     foreach my $img ( <images/*> ) {
589         (my $destImg = $img) =~ s{^images/}{};
590         InstallFile($img, "$DestDir$Conf{CgiImageDir}/$destImg", 0444, 1);
591     }
592
593     #
594     # Install new CSS file, making a backup copy if necessary
595     #
596     my $cssBackup = "$DestDir$Conf{CgiImageDir}/BackupPC_stnd.css.pre-__VERSION__";
597     if ( -f "$DestDir$Conf{CgiImageDir}/BackupPC_stnd.css" && !-f $cssBackup ) {
598         rename("$DestDir$Conf{CgiImageDir}/BackupPC_stnd.css", $cssBackup);
599     }
600     InstallFile("conf/BackupPC_stnd.css",
601                 "$DestDir$Conf{CgiImageDir}/BackupPC_stnd.css", 0444, 0);
602     InstallFile("conf/BackupPC_stnd_orig.css",
603                 "$DestDir$Conf{CgiImageDir}/BackupPC_stnd_orig.css", 0444, 0);
604 }
605
606 printf("Making init.d scripts\n");
607 foreach my $init ( qw(gentoo-backuppc gentoo-backuppc.conf linux-backuppc
608                       solaris-backuppc debian-backuppc suse-backuppc
609                       slackware-backuppc ) ) {
610     InstallFile("init.d/src/$init", "init.d/$init", 0444);
611 }
612
613 printf("Installing docs in $DestDir$Conf{InstallDir}/doc\n");
614 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
615     InstallFile("doc/$doc", "$DestDir$Conf{InstallDir}/doc/$doc", 0444);
616 }
617
618 printf("Installing config.pl and hosts in $DestDir$Conf{ConfDir}\n");
619 InstallFile("conf/hosts", "$DestDir$Conf{ConfDir}/hosts", 0644)
620                     if ( !-f "$DestDir$Conf{ConfDir}/hosts" );
621
622 #
623 # Now do the config file.  If there is an existing config file we
624 # merge in the new config file, adding any new configuration
625 # parameters and deleting ones that are no longer needed.
626 #
627 my $dest = "$DestDir$Conf{ConfDir}/config.pl";
628 my ($distConf, $distVars) = ConfigParse("conf/config.pl");
629 my ($oldConf, $oldVars);
630 my ($newConf, $newVars) = ($distConf, $distVars);
631 if ( -f $dest ) {
632     ($oldConf, $oldVars) = ConfigParse($dest);
633     ($newConf, $newVars) = ConfigMerge($oldConf, $oldVars, $distConf, $distVars);
634 }
635
636 #
637 # Update various config parameters.  The old config is in Conf{}
638 # and the new config is an array in text form in $newConf->[].
639 #
640 $Conf{EMailFromUserName}  ||= $Conf{BackupPCUser};
641 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
642
643 #
644 # Guess $Conf{CgiURL}
645 #
646 if ( !defined($Conf{CgiURL}) ) {
647     if ( $Conf{CgiDir} =~ m{cgi-bin(/.*)} ) {
648         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin$1/BackupPC_Admin'";
649     } else {
650         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin/BackupPC_Admin'";
651     }
652 }
653
654 #
655 # The smbclient commands have moved from hard-coded to the config file.
656 # $Conf{SmbClientArgs} no longer exists, so merge it into the new
657 # commands if it still exists.
658 #
659 if ( defined($Conf{SmbClientArgs}) ) {
660     if ( $Conf{SmbClientArgs} ne "" ) {
661         foreach my $param ( qw(SmbClientRestoreCmd SmbClientFullCmd
662                                 SmbClientIncrCmd) ) {
663             $newConf->[$newVars->{$param}]{text}
664                             =~ s/(-E\s+-N)/$1 $Conf{SmbClientArgs}/;
665         }
666     }
667     delete($Conf{SmbClientArgs});
668 }
669
670 #
671 # CSS is now stored in a file rather than a big config variable.
672 #
673 delete($Conf{CSSstylesheet});
674
675 #
676 # The blackout timing settings are now stored in a list of hashes, rather
677 # than three scalar parameters.
678 #
679 if ( defined($Conf{BlackoutHourBegin}) ) {
680     $Conf{BlackoutPeriods} = [
681          {
682              hourBegin => $Conf{BlackoutHourBegin},
683              hourEnd   => $Conf{BlackoutHourEnd},
684              weekDays  => $Conf{BlackoutWeekDays},
685          } 
686     ];
687     delete($Conf{BlackoutHourBegin});
688     delete($Conf{BlackoutHourEnd});
689     delete($Conf{BlackoutWeekDays});
690 }
691
692 #
693 # $Conf{RsyncLogLevel} has been replaced by $Conf{XferLogLevel}
694 #
695 if ( defined($Conf{RsyncLogLevel}) ) {
696     $Conf{XferLogLevel} = $Conf{RsyncLogLevel};
697     delete($Conf{RsyncLogLevel});
698 }
699
700 #
701 # In 2.1.0 the default for $Conf{CgiNavBarAdminAllHosts} is now 1
702 #
703 $Conf{CgiNavBarAdminAllHosts} = 1;
704
705 #
706 # IncrFill should now be off
707 #
708 $Conf{IncrFill} = 0;
709
710 #
711 # Figure out sensible arguments for the ping command
712 #
713 if ( defined($Conf{PingArgs}) ) {
714     $Conf{PingCmd} = '$pingPath ' . $Conf{PingArgs};
715 } elsif ( !defined($Conf{PingCmd}) ) {
716     if ( $^O eq "solaris" || $^O eq "sunos" ) {
717         $Conf{PingCmd} = '$pingPath -s $host 56 1';
718     } elsif ( ($^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd")
719             && !system("$Conf{PingPath} -c 1 -w 3 localhost") ) {
720         $Conf{PingCmd} = '$pingPath -c 1 -w 3 $host';
721     } else {
722         $Conf{PingCmd} = '$pingPath -c 1 $host';
723     }
724     delete($Conf{PingArgs});
725 }
726
727 #
728 # Figure out sensible arguments for the df command
729 #
730 if ( !defined($Conf{DfCmd}) ) {
731     if ( $^O eq "solaris" || $^O eq "sunos" ) {
732         $Conf{DfCmd} = '$dfPath -k $topDir';
733     }
734 }
735
736 #
737 # $Conf{SmbClientTimeout} is now $Conf{ClientTimeout}
738 #
739 if ( defined($Conf{SmbClientTimeout}) ) {
740     $Conf{ClientTimeout} = $Conf{SmbClientTimeout};
741     delete($Conf{SmbClientTimeout});
742 }
743
744 #
745 # Replace --devices with -D in RsyncArgs and RsyncRestoreArgs
746 #
747 foreach my $param ( qw(RsyncArgs RsyncRestoreArgs) ) {
748     next if ( !defined($newVars->{$param}) );
749     $newConf->[$newVars->{$param}]{text} =~ s/--devices/-D/g;
750 }
751
752 #
753 # Merge any new user-editable parameters into CgiUserConfigEdit
754 # by copying the old settings forward.
755 #
756 if ( defined($Conf{CgiUserConfigEdit}) ) {
757     #
758     # This is a real hack.  The config file merging is done in text
759     # form without actually instantiating the new conf structure.
760     # So we need to extract the new hash of settings, update it,
761     # and merge the text.  Ugh...
762     #
763     my $new;
764     my $str = $distConf->[$distVars->{CgiUserConfigEdit}]{text};
765
766     $str =~ s/^\s*\$Conf\{.*?\}\s*=\s*/\$new = /m;
767     eval($str);
768     foreach my $p ( keys(%$new) ) {
769         $new->{$p} = $Conf{CgiUserConfigEdit}{$p}
770                 if ( defined($Conf{CgiUserConfigEdit}{$p}) );
771     }
772     $Conf{CgiUserConfigEdit} = $new;
773     my $d = Data::Dumper->new([$new], [*value]);
774     $d->Indent(1);
775     $d->Terse(1);
776     my $value = $d->Dump;
777     $value =~ s/(.*)\n/$1;\n/s;
778     $newConf->[$newVars->{CgiUserConfigEdit}]{text}
779             =~ s/(\s*\$Conf\{.*?\}\s*=\s*).*/$1$value/s;
780 }
781
782 #
783 # Now backup and write the config file
784 #
785 my $confCopy = "$dest.pre-__VERSION__";
786 if ( -f $dest && !-f $confCopy ) {
787     #
788     # Make copy of config file, preserving ownership and modes
789     #
790     printf("Making backup copy of $dest -> $confCopy\n");
791     my @stat = stat($dest);
792     my $mode = $stat[2];
793     my $uid  = $stat[4];
794     my $gid  = $stat[5];
795     die("can't copy($dest, $confCopy)\n")
796                                 unless copy($dest, $confCopy);
797     die("can't chown $uid, $gid $confCopy\n")
798                                 unless my_chown($uid, $gid, $confCopy);
799     die("can't chmod $mode $confCopy\n")
800                                 unless my_chmod($mode, $confCopy);
801 }
802 open(OUT, ">", $dest) || die("can't open $dest for writing\n");
803 binmode(OUT);
804 my $blockComment;
805 foreach my $var ( @$newConf ) {
806     if ( length($blockComment)
807           && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
808         $var->{text} = substr($var->{text}, length($blockComment));
809         $blockComment = undef;
810     }
811     $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
812     $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
813                 defined($Conf{$1}) && ref($Conf{$1}) eq ""
814                                    && $Conf{$1} ne $OrigConf{$1}
815                                    ? "\$Conf{$1}$2$Conf{$1}$4"
816                                    : "\$Conf{$1}$2$3$4"/emg;
817     print OUT $var->{text};
818 }
819 close(OUT);
820 if ( !defined($oldConf) ) {
821     die("can't chmod 0640 mode $dest\n")  unless my_chmod(0640, $dest);
822     die("can't chown $Uid, $Gid $dest\n") unless my_chown($Uid, $Gid, $dest);
823 }
824
825 if ( $Conf{CgiDir} ne "" ) {
826     printf("Installing cgi script BackupPC_Admin in $DestDir$Conf{CgiDir}\n");
827     mkpath("$DestDir$Conf{CgiDir}", 0, 0755);
828     InstallFile("cgi-bin/BackupPC_Admin", "$DestDir$Conf{CgiDir}/BackupPC_Admin",
829                 04554);
830 }
831
832 print <<EOF;
833
834 Ok, it looks like we are finished.  There are several more things you
835 will need to do:
836
837   - Browse through the config file, $Conf{ConfDir}/config.pl,
838     and make sure all the settings are correct.  In particular,
839     you will need to set \$Conf{CgiAdminUsers} so you have
840     administration privileges in the CGI interface.
841
842   - Edit the list of hosts to backup in $Conf{ConfDir}/hosts.
843
844   - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
845     Please pay special attention to the security section.
846
847   - Verify that the CGI script BackupPC_Admin runs correctly.  You might
848     need to change the permissions or group ownership of BackupPC_Admin.
849
850   - BackupPC should be ready to start.  Don't forget to run it
851     as user $Conf{BackupPCUser}!  The installation also contains an
852     init.d/backuppc script that can be copied to /etc/init.d
853     so that BackupPC can auto-start on boot.  This will also enable
854     administrative users to start the server from the CGI interface.
855     See init.d/README.
856
857 Enjoy!
858 EOF
859
860 if ( `$Conf{PerlPath} -V` =~ /uselargefiles=undef/ ) {
861     print <<EOF;
862
863 Warning: your perl, $Conf{PerlPath}, does not support large files.
864 This means BackupPC won't be able to backup files larger than 2GB.
865 To solve this problem you should build/install a new version of perl
866 with large file support enabled.  Use
867
868     $Conf{PerlPath} -V | egrep uselargefiles
869
870 to check if perl has large file support (undef means no support).
871 EOF
872 }
873
874 eval "use File::RsyncP;";
875 if ( !$@ && $File::RsyncP::VERSION < 0.68 ) {
876     print("\nWarning: you need to upgrade File::RsyncP;"
877         . " I found $File::RsyncP::VERSION and BackupPC needs 0.68\n");
878 }
879
880 exit(0);
881
882 ###########################################################################
883 # Subroutines
884 ###########################################################################
885
886 sub InstallFile
887 {
888     my($prog, $dest, $mode, $binary) = @_;
889     my $first = 1;
890     my($uid, $gid) = ($Uid, $Gid);
891
892     if ( -f $dest ) {
893         #
894         # preserve ownership and modes of files that already exist
895         #
896         my @stat = stat($dest);
897         $mode = $stat[2];
898         $uid  = $stat[4];
899         $gid  = $stat[5];
900     }
901     unlink($dest) if ( -f $dest );
902     if ( $binary ) {
903         die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
904     } else {
905         open(PROG, $prog)     || die("can't open $prog for reading\n");
906         open(OUT, ">", $dest) || die("can't open $dest for writing\n");
907         binmode(PROG);
908         binmode(OUT);
909         while ( <PROG> ) {
910             s/__INSTALLDIR__/$Conf{InstallDir}/g;
911             s/__LOGDIR__/$Conf{LogDir}/g;
912             s/__CONFDIR__/$Conf{ConfDir}/g;
913             s/__TOPDIR__/$Conf{TopDir}/g;
914             s/^(\s*my \$useFHS\s*=\s*)\d;/${1}$opts{fhs};/
915                                     if ( $prog =~ /Lib.pm/ );
916             s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
917             s/__CGIDIR__/$Conf{CgiDir}/g;
918             if ( $first && /^#.*bin\/perl/ ) {
919                 #
920                 # Fill in correct path to perl (no taint for >= 2.0.1).
921                 #
922                 print OUT "#!$Conf{PerlPath}\n";
923             } else {
924                 print OUT;
925             }
926             $first = 0;
927         }
928         close(PROG);
929         close(OUT);
930     }
931     die("can't chown $uid, $gid $dest") unless my_chown($uid, $gid, $dest);
932     die("can't chmod $mode $dest")      unless my_chmod($mode, $dest);
933 }
934
935 sub FindProgram
936 {
937     my($path, $prog) = @_;
938
939     if ( defined($opts{"bin-path"}{$prog}) ) {
940         return $opts{"bin-path"}{$prog};
941     }
942     foreach my $dir ( split(/:/, $path) ) {
943         my $file = File::Spec->catfile($dir, $prog);
944         return $file if ( -x $file );
945     }
946     return;
947 }
948
949 sub ConfigParse
950 {
951     my($file) = @_;
952     open(C, $file) || die("can't open $file");
953     binmode(C);
954     my($out, @conf, $var);
955     my $comment = 1;
956     my $allVars = {};
957     my $endLine = undef;
958     while ( <C> ) {
959         if ( /^#/ && !defined($endLine) ) {
960             if ( $comment ) {
961                 $out .= $_;
962             } else {
963                 if ( $out ne "" ) {
964                     $allVars->{$var} = @conf if ( defined($var) );
965                     push(@conf, {
966                         text => $out,
967                         var => $var,
968                     });
969                 }
970                 $var = undef;
971                 $comment = 1;
972                 $out = $_;
973             }
974         } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
975             $comment = 0;
976             if ( defined($var) ) {
977                 $allVars->{$var} = @conf if ( defined($var) );
978                 push(@conf, {
979                     text => $out,
980                     var => $var,
981                 });
982                 $out = $_;
983             } else {
984                 $out .= $_;
985             }
986             $var = $1;
987             $endLine = $1 if ( /^\s*\$Conf\{[^}]*} *= *<<(.*);/ );
988             $endLine = $1 if ( /^\s*\$Conf\{[^}]*} *= *<<'(.*)';/ );
989         } else {
990             $endLine = undef if ( defined($endLine) && /^\Q$endLine[\n\r]*$/ );
991             $out .= $_;
992         }
993     }
994     if ( $out ne "" ) {
995         $allVars->{$var} = @conf if ( defined($var) );
996         push(@conf, {
997             text => $out,
998             var  => $var,
999         });
1000     }
1001     close(C);
1002     return (\@conf, $allVars);
1003 }
1004
1005 sub ConfigMerge
1006 {
1007     my($old, $oldVars, $new, $newVars) = @_;
1008     my $posn = 0;
1009     my($res, $resVars);
1010
1011     #
1012     # Find which config parameters are not needed any longer
1013     #
1014     foreach my $var ( @$old ) {
1015         next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
1016         #print(STDERR "Deleting old config parameter $var->{var}\n");
1017         $var->{delete} = 1;
1018     }
1019     #
1020     # Find which config parameters are new
1021     #
1022     foreach my $var ( @$new ) {
1023         next if ( !defined($var->{var}) );
1024         if ( defined($oldVars->{$var->{var}}) ) {
1025             $posn = $oldVars->{$var->{var}};
1026         } else {
1027             #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
1028             push(@{$old->[$posn]{new}}, $var);
1029         }
1030     }
1031     #
1032     # Create merged config file
1033     #
1034     foreach my $var ( @$old ) {
1035         next if ( $var->{delete} );
1036         push(@$res, $var);
1037         foreach my $new ( @{$var->{new}} ) {
1038             push(@$res, $new);
1039         }
1040     }
1041     for ( my $i = 0 ; $i < @$res ; $i++ ) {
1042         $resVars->{$res->[$i]{var}} = $i;
1043     }
1044     return ($res, $resVars);
1045 }
1046
1047 sub my_chown
1048 {
1049     my($uid, $gid, $file) = @_;
1050
1051     return 1 if ( !$opts{"set-perms"} );
1052     return chown($uid, $gid, $file);
1053 }
1054
1055 sub my_chmod
1056 {
1057     my ($mode, $file) = @_;
1058
1059     return 1 if ( !$opts{"set-perms"} );
1060     return chmod($mode, $file);
1061 }
1062
1063 sub prompt
1064 {
1065     my($question, $default, $option) = @_;
1066
1067     $default = $opts{$option} if ( defined($opts{$option}) );
1068     if ( $opts{batch} ) {
1069         print("$question [$default]\n");
1070         return $default;
1071     }
1072     print("$question [$default]? ");
1073     my $reply = <STDIN>;
1074     $reply =~ s/[\n\r]*//g;
1075     return $reply if ( $reply !~ /^$/ );
1076     return $default;
1077 }
1078
1079 __END__
1080
1081 =head1 SYNOPSIS
1082
1083 configure.pl [options]
1084
1085 =head1 DESCRIPTION
1086
1087 configure.pl is a script that is used to install or upgrade a BackupPC
1088 installation.  It is usually run interactively without arguments.  It
1089 also supports a batch mode where all the options can be specified
1090 via the command-line.
1091
1092 For upgrading BackupPC you need to make sure that BackupPC is not
1093 running prior to running BackupPC.
1094
1095 Typically configure.pl needs to run as the super user (root).
1096
1097 =head1 OPTIONS
1098
1099 =over 8
1100
1101 =item B<--batch>
1102
1103 Run configure.pl in batch mode.  configure.pl will run without
1104 prompting the user.  The other command-line options are used
1105 to specify the settings that the user is usually prompted for.
1106
1107 =item B<--backuppc-user=USER>
1108
1109 Specify the BackupPC user name that owns all the BackupPC
1110 files and runs the BackupPC programs.  Default is backuppc.
1111
1112 =item B<--bin-path PROG=PATH>
1113
1114 Specify the path for various external programs that BackupPC
1115 uses.  Several --bin-path options may be specified.  configure.pl
1116 usually finds sensible defaults based on searching the PATH.
1117 The format is:
1118
1119     --bin-path PROG=PATH
1120
1121 where PROG is one of perl, tar, smbclient, nmblookup, rsync, ping,
1122 df, ssh, sendmail, hostname, split, par2, cat, gzip, bzip2 and
1123 PATH is that full path to that program.
1124
1125 Examples
1126
1127     --bin-path cat=/bin/cat --bin-path bzip2=/home/user/bzip2
1128
1129 =item B<--compress-level=N>
1130
1131 Set the configuration compression level to N.  Default is 3
1132 if Compress::Zlib is installed.
1133
1134 =item B<--config-dir CONFIG_DIR>
1135
1136 Configuration directory for new installations.  Defaults
1137 to /etc/BackupPC with FHS.  Automatically extracted
1138 from --config-path for existing installations.
1139
1140 =item B<--config-path CONFIG_PATH>
1141
1142 Path to the existing config.pl configuration file for BackupPC.
1143 This option should be specified for batch upgrades to an
1144 existing installation.  The option should be omitted when
1145 doing a batch new install.
1146
1147 =item B<--cgi-dir CGI_DIR>
1148
1149 Path to Apache's cgi-bin directory where the BackupPC_Admin
1150 script will be installed.  This option only needs to be
1151 specified for a batch new install.
1152
1153 =item B<--data-dir DATA_DIR>
1154
1155 Path to the BackupPC data directory.  This is where all the backup
1156 data is stored, and it should be on a large file system. This option
1157 only needs to be specified for a batch new install.
1158
1159 Example:
1160
1161     --data-dir /data/BackupPC
1162
1163 =item B<--dest-dir DEST_DIR>
1164
1165 An optional prefix to apply to all installation directories.
1166 Usually this is not needed, but certain auto-installers like
1167 to stage an install in a temporary directory, and then copy
1168 the files to their real destination.  This option can be used
1169 to specify the temporary directory prefix.  Note that if you
1170 specify this option, BackupPC won't run correctly if you try
1171 to run it from below the --dest-dir directory, since all the
1172 paths are set assuming BackupPC is installed in the intended
1173 final locations.
1174
1175 =item B<--fhs>
1176
1177 Use locations specified by the Filesystem Hierarchy Standard
1178 for installing BackupPC.  This is enabled by default for new
1179 installatios.  To use the pre-3.0 installation locations,
1180 specify --no-fhs.
1181
1182 =item B<--help|?>
1183
1184 Print a brief help message and exits.
1185
1186 =item B<--hostname HOSTNAME>
1187
1188 Host name (this machine's name) on which BackupPC is being installed.
1189 This option only needs to be specified for a batch new install.
1190
1191 =item B<--html-dir HTML_DIR>
1192
1193 Path to an Apache html directory where various BackupPC image files
1194 and the CSS files will be installed.  This is typically a directory
1195 below Apache's DocumentRoot directory.  This option only needs to be
1196 specified for a batch new install.
1197
1198 Example:
1199
1200     --html-dir /usr/local/apache/htdocs/BackupPC
1201
1202 =item B<--html-dir-url URL>
1203
1204 The URL (without http://hostname) required to access the BackupPC html
1205 directory specified with the --html-dir option.  This option only needs
1206 to be specified for a batch new install.
1207
1208 Example:
1209
1210     --html-dir-url /BackupPC
1211
1212 =item B<--install-dir INSTALL_DIR>
1213
1214 Installation directory for BackupPC scripts, libraries, and
1215 documentation.  This option only needs to be specified for a
1216 batch new install.
1217
1218 Example:
1219
1220     --install-dir /usr/local/BackupPC
1221
1222 =item B<--log-dir LOG_DIR>
1223
1224 Log directory.  Defaults to /var/log/BackupPC with FHS.
1225
1226 =item B<--man>
1227
1228 Prints the manual page and exits.
1229
1230 =item B<--set-perms>
1231
1232 When installing files and creating directories, chown them to
1233 the BackupPC user and chmod them too.  This is enabled by default.
1234 To disable (for example, if staging a destination directory)
1235 then specify --no-set-perms.
1236
1237 =item B<--uid-ignore>
1238
1239 configure.pl verifies that the script is being run as the super user
1240 (root).  Without the --uid-ignore option, in batch mode the script will
1241 exit with an error if not run as the super user, and in interactive mode
1242 the user will be prompted.  Specifying this option will cause the script
1243 to continue even if the user id is not root.
1244
1245 =head1 EXAMPLES
1246
1247 For a standard interactive install, run without arguments:
1248
1249     configure.pl
1250
1251 For a batch new install you need to specify answers to all the
1252 questions that are normally prompted:
1253
1254     configure.pl                                   \
1255         --batch                                    \
1256         --cgi-dir /var/www/cgi-bin/BackupPC        \
1257         --data-dir /data/BackupPC                  \
1258         --hostname myHost                          \
1259         --html-dir /var/www/html/BackupPC          \
1260         --html-dir-url /BackupPC                   \
1261         --install-dir /usr/local/BackupPC
1262
1263 For a batch upgrade, you only need to specify the path to the
1264 configuration file:
1265         
1266     configure.pl --batch --config-path /data/BackupPC/conf/config.pl
1267
1268 =head1 AUTHOR
1269
1270 Craig Barratt <cbarratt@users.sourceforge.net>
1271
1272 =head1 COPYRIGHT
1273
1274 Copyright (C) 2001-2006  Craig Barratt.
1275
1276 This program is free software; you can redistribute it and/or modify
1277 it under the terms of the GNU General Public License as published by
1278 the Free Software Foundation; either version 2 of the License, or
1279 (at your option) any later version.
1280
1281 =cut