Imported Upstream version 3.2.0
[debian/amanda] / server-src / amserverconfig.pl
index ad0abc4f2a8af00a801ede1a705fe5678ee2275f..accd2164f378495dfeaa3e035ff04f8c9868709b 100755 (executable)
@@ -1,6 +1,6 @@
 #!@PERL@
 #
-# Copyright (c) 2007,2008,2009 Zmanda, Inc.  All Rights Reserved.
+# Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
 #
 # This program is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License version 2 as published
@@ -23,8 +23,11 @@ use lib '@amperldir@';
 use Getopt::Long;
 use Time::Local;
 use File::Copy;
+use File::Path;
 use Socket;   # for gethostbyname
 use Amanda::Paths;
+use Amanda::Util qw( :constants );
+use Amanda::Constants;
 
 my $confdir="$CONFIG_DIR";
 my $tmpdir="$AMANDA_DBGDIR";
@@ -32,8 +35,8 @@ my $amandahomedir="$localstatedir/lib/amanda";
 my $templatedir="$amdatadir/template.d"; #rpm install template files here
 my $def_tapedev="file:$amandahomedir/vtapes";
 
-my $amanda_user="@CLIENT_LOGIN@";
-my $def_config="@DEFAULT_CONFIG@";
+my $amanda_user="$Amanda::Constants::CLIENT_LOGIN";
+my $def_config="$Amanda::Constants::DEFAULT_CONFIG";
 my $def_dtimeout="1800";
 my $def_ctimeout="30";
 my $def_etimeout="300";
@@ -46,6 +49,8 @@ my $holding_err=0;
 my $template_only=0;
 my $parentdir;
 my $host;
+my @pw = getpwuid($<);
+my $dumpuser = $pw[0];
 
 
 #usage
@@ -104,38 +109,27 @@ sub log_and_die {
 }
 
 
-sub is_user_right {
-    my $user = `whoami`;
-    chomp($user);
-    ( $user eq $amanda_user ) ||
-       die ("ERROR: $0 must be run by $amanda_user\n", 0);
-}
-
-
 # rpm installation should have taken care of these. Create one if it's not there
 sub check_gnutarlist_dir {
     if ( -e "$amandahomedir/gnutar-lists" ) {
        &mprint ("$amandahomedir/gnutar-lists directory exists\n");
     }
     else {
-       mkdir ("$amandahomedir/gnutar-lists", $def_perm) ||
-           &log_and_die ("ERROR: mkdir:$amandahomedir/gnutar-lists failed: $!\n", 0);
+       mkpath ("$amandahomedir/gnutar-lists", $def_perm) ||
+           &log_and_die ("ERROR: mkpath:$amandahomedir/gnutar-lists failed: $!\n", 0);
     }
 }
 
 sub create_conf_dir {
-  unless ( -e $confdir ) {
-    &log_and_die ("ERROR: $confdir does not exist\n", 0);
-  }
   unless ( -e "$confdir/$config" ) {
-    mkdir ("$confdir/$config", $def_perm) ||
-      &log_and_die ("ERROR: mkdir: $confdir/$config failed: $!\n", 0); # $! = system error
+    mkpath ("$confdir/$config", $def_perm) ||
+      &log_and_die ("ERROR: mkpath: $confdir/$config failed: $!\n", 0);        # $! = system error
   } else {
     &log_and_die ("ERROR: Configuration $config exists\n", 0);
   }
   unless ( -e "$confdir/template.d" ) {
-    mkdir ("$confdir/template.d", $def_perm)  ||
-      &log_and_die ("ERROR: mkdir: $confdir/template.d failed: $!\n", 0);
+    mkpath ("$confdir/template.d", $def_perm)  ||
+      &log_and_die ("ERROR: mkpath: $confdir/template.d failed: $!\n", 0);
     &mprint ("$confdir/template.d directory created\n");
   }
 }
@@ -163,10 +157,10 @@ sub copy_template_file {
 
 
 sub create_curinfo_index_dir {
-    mkdir("$confdir/$config/curinfo", $def_perm) ||
-       &log_and_die ("ERROR: mkdir: $confdir/$config/curinfo failed: $!\n", 1);
-    mkdir("$confdir/$config/index", $def_perm) || 
-       &log_and_die ("ERROR: mkdir: $confdir/$config/index failed: $!\n", 1);
+    mkpath("$confdir/$config/curinfo", $def_perm) ||
+       &log_and_die ("ERROR: mkpath: $confdir/$config/curinfo failed: $!\n", 1);
+    mkpath("$confdir/$config/index", $def_perm) || 
+       &log_and_die ("ERROR: mkpath: $confdir/$config/index failed: $!\n", 1);
     &mprint ("curinfo and index directory created\n");
 }
 
@@ -208,23 +202,23 @@ sub create_holding {
     if (( $dfout[10] / $div )  > 1024000 ) { # holding disk is defined 1000 MB
        &mprint ("creating holding disk directory\n");
        unless ( -d "$amandahomedir/holdings" ) { 
-       mkdir ( "$amandahomedir/holdings", $def_perm) ||
-           (&mprint ("WARNING: mkdir $amandahomedir/holdings failed: $!\n"), $holding_err++, return );
+       mkpath ( "$amandahomedir/holdings", $def_perm) ||
+           (&mprint ("WARNING: mkpath $amandahomedir/holdings failed: $!\n"), $holding_err++, return );
     }
-       mkdir ( "$amandahomedir/holdings/$config", $def_perm) ||
-           (&mprint ("WARNING: mkdir $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
+       mkpath ( "$amandahomedir/holdings/$config", $def_perm) ||
+           (&mprint ("WARNING: mkpath $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
     }
 }
 
 #create default tape dir
 sub create_deftapedir{
     unless ( -e "$amandahomedir/vtapes" ) { 
-       mkdir ( "$amandahomedir/vtapes", $def_perm) ||
-           ( &mprint ("WARNING: mkdir $amandahomedir/$config/vtapes failed: $!\n"), return );
+       mkpath ( "$amandahomedir/vtapes", $def_perm) ||
+           ( &mprint ("WARNING: mkpath $amandahomedir/$config/vtapes failed: $!\n"), return );
     }
     unless ( -e "$amandahomedir/vtapes/$config" ) { 
-       mkdir ( "$amandahomedir/vtapes/$config", $def_perm) ||
-           ( &mprint ("WARNING: mkdir $amandahomedir/vtapes/$config failed: $!\n"), return );
+       mkpath ( "$amandahomedir/vtapes/$config", $def_perm) ||
+           ( &mprint ("WARNING: mkpath $amandahomedir/vtapes/$config failed: $!\n"), return );
     }
        $parentdir="$amandahomedir/vtapes/$config";
 }
@@ -279,8 +273,8 @@ sub create_vtape {
 
        for $i (1..$tp_cyclelimit) {
                unless ( -e "slot$i"){
-               mkdir ("slot$i", $def_perm) ||
-               ( &mprint ("WARNING: mkdir $parentdir/slot$i failed: $!\n"), $vtape_err++, return);
+               mkpath ("slot$i", $def_perm) ||
+               ( &mprint ("WARNING: mkpath $parentdir/slot$i failed: $!\n"), $vtape_err++, return);
                }
                ( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) ||
            ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
@@ -356,6 +350,7 @@ sub create_customconf{
            &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1);
 
        print CONF "org \"$config\"\t\t# your organization name for reports\n";
+       print CONF "dumpuser \"$dumpuser\"\t# the user to run dumps under\n";
        print CONF "mailto \"$mailto\"\t# space separated list of operators at your site\n";
        print CONF "dumpcycle $dumpcycle\t\t# the number of days in the normal dump cycle\n";
         print CONF "runspercycle $runspercycle\t\t# the number of amdump runs in dumpcycle days\n";
@@ -479,10 +474,12 @@ $date=`date +%Y%m%d%H%M%S`;
 chomp($date);
 my $logfile="$tmpdir/amserverconfig.$date.debug";
 
-&is_user_right;
+Amanda::Util::setup_application("amserverconfig", "server", $CONTEXT_CMDLINE);
+Amanda::Util::finish_setup($RUNNING_AS_ANY);
+
 unless ( -e "$tmpdir" ) {
-    mkdir ("$tmpdir", $def_perm) ||
-       die ("ERROR: mkdir: $tmpdir failed: $!\n");
+    mkpath ("$tmpdir", $def_perm) ||
+       die ("ERROR: mkpath: $tmpdir failed: $!\n");
 }
 
 open (LOG, ">$logfile") || die ("ERROR: Cannot create logfile: $!\n");
@@ -655,3 +652,4 @@ $ENV{'PATH'} = $oldPATH;
 
 
 # THE END
+Amanda::Util::finish_application();