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