- fixed configure.pl and makeDist.
[BackupPC.git] / lib / BackupPC / CGI / Lib.pm
index 307e441..bec4ea8 100644 (file)
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 2.1.0beta1, released 9 Apr 2004.
+# Version 3.0.0alpha, released 23 Jan 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -44,7 +44,7 @@ require Exporter;
 
 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
 
-use vars qw($Cgi %In $MyURL $User %Conf $TopDir $BinDir $bpc);
+use vars qw($Cgi %In $MyURL $User %Conf $TopDir $LogDir $BinDir $bpc);
 use vars qw(%Status %Info %Jobs @BgQueue @UserQueue @CmdQueue
             %QueueLen %StatusHost);
 use vars qw($Hosts $HostsMTime $ConfigMTime $PrivAdmin);
@@ -76,7 +76,7 @@ use vars qw($Lang);
                    NavLink
                    h1
                    h2
-                   $Cgi %In $MyURL $User %Conf $TopDir $BinDir $bpc
+                   $Cgi %In $MyURL $User %Conf $TopDir $LogDir $BinDir $bpc
                    %Status %Info %Jobs @BgQueue @UserQueue @CmdQueue
                    %QueueLen %StatusHost
                    $Hosts $HostsMTime $ConfigMTime $PrivAdmin
@@ -95,15 +95,18 @@ sub NewRequest
 
     if ( !defined($bpc) ) {
        ErrorExit($Lang->{BackupPC__Lib__new_failed__check_apache_error_log})
-           if ( !($bpc = BackupPC::Lib->new(undef, undef, 1)) );
+           if ( !($bpc = BackupPC::Lib->new(undef, undef, undef, 1)) );
        $TopDir = $bpc->TopDir();
+       $LogDir = $bpc->LogDir();
        $BinDir = $bpc->BinDir();
        %Conf   = $bpc->Conf();
        $Lang   = $bpc->Lang();
        $ConfigMTime = $bpc->ConfigMTime();
     } elsif ( $bpc->ConfigMTime() != $ConfigMTime ) {
-        $bpc->ServerMesg("log Re-read config file because mtime changed");
-        $bpc->ServerMesg("server reload");
+        $bpc->ConfigRead();
+        %Conf   = $bpc->Conf();
+        $Lang   = $bpc->Lang();
+        $ConfigMTime = $bpc->ConfigMTime();
     }
 
     #
@@ -120,6 +123,10 @@ sub NewRequest
     #
     $MyURL  = $ENV{SCRIPT_NAME};
     $User   = $ENV{REMOTE_USER};
+    #
+    # Handle LDAP uid=user when using mod_authz_ldap
+    #
+    $User   = $1 if ( $User =~ /uid=([^,]+)/i );        
 
     #
     # Clean up %ENV for taint checking
@@ -154,6 +161,15 @@ EOF
               {map {$_, 1} split(",", $Hosts->{$host}{moreUsers}) }
        }
     }
+
+    #
+    # Untaint the host name
+    #
+    if ( $In{host} =~ /^([\w.\s-]+)$/ ) {
+       $In{host} = $1;
+    } else {
+       delete($In{host});
+    }
 }
 
 sub timeStamp2
@@ -205,7 +221,7 @@ sub EscHTML
     $s =~ s/\"/"/g;
     $s =~ s/>/>/g;
     $s =~ s/</&lt;/g;
-    $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
+    ### $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
     return \$s;
 }
 
@@ -278,6 +294,8 @@ sub GetStatusInfo
 {
     my($status) = @_;
     ServerConnect();
+    %Status = ()     if ( $status =~ /\bhosts\b/ );
+    %StatusHost = () if ( $status =~ /\bhost\(/ );
     my $reply = $bpc->ServerMesg("status $status");
     $reply = $1 if ( $reply =~ /(.*)/s );
     eval($reply);
@@ -292,9 +310,9 @@ sub GetStatusInfo
 
 sub ReadUserEmailInfo
 {
-    if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
-        do "$TopDir/log/UserEmailInfo.pl";
-        $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
+    if ( (stat("$LogDir/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
+        do "$LogDir/UserEmailInfo.pl";
+        $UserEmailInfoMTime = (stat("$LogDir/UserEmailInfo.pl"))[9];
     }
 }
 
@@ -392,27 +410,28 @@ sub Header
 {
     my($title, $content, $noBrowse, $contentSub, $contentPost) = @_;
     my @adminLinks = (
-        { link => "",                         name => $Lang->{Status}},
-        { link => "?action=adminOpts",        name => $Lang->{Admin_Options},
-                                              priv => 1},
-        { link => "?action=summary",          name => $Lang->{PC_Summary}},
-        { link => "?action=view&type=LOG",    name => $Lang->{LOG_file},
-                                              priv => 1},
-        { link => "?action=LOGlist",          name => $Lang->{Old_LOGs},
-                                              priv => 1},
-        { link => "?action=emailSummary",     name => $Lang->{Email_summary},
-                                              priv => 1},
-        { link => "?action=view&type=config", name => $Lang->{Config_file},
-                                              priv => 1},
-        { link => "?action=view&type=hosts",  name => $Lang->{Hosts_file},
-                                              priv => 1},
-        { link => "?action=queue",            name => $Lang->{Current_queues},
-                                              priv => 1},
-        @{$Conf{CgiNavBarLinks}},
+        { link => "",                      name => $Lang->{Status}},
+        { link => "?action=adminOpts",     name => $Lang->{Admin_Options},
+                                           priv => 1},
+        { link => "?action=editConfig",    name => $Lang->{CfgEdit_Edit_Config},
+                                           priv => 1},
+        { link => "?action=editConfig&newMenu=hosts",
+                                           name => $Lang->{CfgEdit_Edit_Hosts},
+                                           priv => 1},
+        { link => "?action=summary",       name => $Lang->{PC_Summary}},
+        { link => "?action=view&type=LOG", name => $Lang->{LOG_file},
+                                           priv => 1},
+        { link => "?action=LOGlist",       name => $Lang->{Old_LOGs},
+                                           priv => 1},
+        { link => "?action=emailSummary",  name => $Lang->{Email_summary},
+                                           priv => 1},
+        { link => "?action=queue",         name => $Lang->{Current_queues},
+                                           priv => 1},
+        @{$Conf{CgiNavBarLinks} || []},
     );
     my $host = $In{host};
 
-    print $Cgi->header();
+    print $Cgi->header(-charset => "utf-8");
     print <<EOF;
 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html><head>
@@ -448,10 +467,14 @@ EOF
                    $Lang->{Last_bad_XferLOG_errors_only},
                    " class=\"navbar\"");
        }
-       if ( -f "$TopDir/pc/$host/config.pl" ) {
-           NavLink("?action=view&type=config&host=${EscURI($host)}",
-                   $Lang->{Config_file}, " class=\"navbar\"");
-       }
+        if ( $Conf{CgiUserConfigEditEnable} || $PrivAdmin ) {
+            NavLink("?action=editConfig&host=${EscURI($host)}",
+                    $Lang->{CfgEdit_Edit_Config}, " class=\"navbar\"");
+        } elsif ( -f "$TopDir/pc/$host/config.pl"
+                    || ($host ne "config" && -f "$TopDir/conf/$host.pl") ) {
+            NavLink("?action=view&type=config&host=${EscURI($host)}",
+                    $Lang->{Config_file}, " class=\"navbar\"");
+        }
        print "</div>\n";
     }
     print("<div id=\"Content\">\n$content\n");
@@ -498,7 +521,7 @@ EOF
     NavSectionTitle($Lang->{NavSectionTitle_});
     foreach my $l ( @adminLinks ) {
         if ( $PrivAdmin || !$l->{priv} ) {
-            my $txt = defined($l->{lname}) ? $Lang->{$l->{lname}} : $l->{name};
+            my $txt = $l->{lname} ne "" ? $Lang->{$l->{lname}} : $l->{name};
             NavLink($l->{link}, $txt);
         }
     }