File:  [LON-CAPA] / loncom / configuration / Checksumming.pm
Revision 1.7: download - view: text, annotated - select for diffs
Sat Feb 20 20:58:59 2016 UTC (8 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
- Use Digest::SHA for all distros.

    1: # The LearningOnline Network with CAPA
    2: # Checksum installed LON-CAPA modules and some configuration files
    3: #
    4: # $Id: Checksumming.pm,v 1.7 2016/02/20 20:58:59 raeburn Exp $
    5: #
    6: # The LearningOnline Network with CAPA
    7: #
    8: # Copyright Michigan State University Board of Trustees
    9: #
   10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   11: #
   12: # LON-CAPA is free software; you can redistribute it and/or modify
   13: # it under the terms of the GNU General Public License as published by
   14: # the Free Software Foundation; either version 2 of the License, or
   15: # (at your option) any later version.
   16: #
   17: # LON-CAPA is distributed in the hope that it will be useful,
   18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20: # GNU General Public License for more details.
   21: #
   22: # You should have received a copy of the GNU General Public License
   23: # along with LON-CAPA; if not, write to the Free Software
   24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25: #
   26: # /home/httpd/html/adm/gpl.txt
   27: #
   28: # http://www.lon-capa.org/
   29: #
   30: 
   31: package LONCAPA::Checksumming;
   32: 
   33: use strict;
   34: use lib '/home/httpd/lib/perl/';
   35: use Apache::lonlocal();
   36: use Apache::loncommon();
   37: use Digest::SHA;
   38: 
   39: sub get_checksums {
   40:     my ($distro,$londaemons,$lonlib,$lonincludes,$lontabdir) = @_;
   41:     my $output;
   42:     my (%versions,%chksums);
   43:     my $dirh;
   44:     my $revtag = '$Id:';
   45:     my @paths;
   46:     if ($londaemons) {
   47:         push(@paths,($londaemons.'/*',
   48:                      $londaemons.'/debug/*.pl'));
   49:     }
   50:     if ($lonlib) {
   51:         push(@paths,($lonlib.'/perl/Apache/*.pm',
   52:                      $lonlib.'/perl/Apache/localize/*.pm',
   53:                      $lonlib.'/perl/LONCAPA/*.pm',
   54:                      $lonlib.'/perl/AlgParser.pm',
   55:                      $lonlib.'/perl/LONCAPA.pm',
   56:                      $lonlib.'/perl/Safe.pm',
   57:                      $lonlib.'/perl/*-std.pm',
   58:                      $lonlib.'/perl/HTML/*.pm',
   59:                      $lonlib.'/perl/Safe.pm'));
   60:     }
   61:     if ($lonincludes) {
   62:         push(@paths,$lonincludes.'/*.lcpm');
   63:     }
   64:     push(@paths,('/home/httpd/cgi-bin/*.pl','/home/httpd/cgi-bin/*.png'));
   65:     my $confdir = '/etc/httpd/conf';
   66:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
   67:         $confdir = '/etc/apache2';
   68:     } elsif ($distro =~ /^sles(\d+)$/) {
   69:         if ($1 >= 10) {
   70:             $confdir = '/etc/apache2';
   71:         }
   72:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
   73:         if ($1 >= 10.0) {
   74:             $confdir = '/etc/apache2';
   75:         }
   76:     }
   77:     push(@paths,("$confdir/loncapa_apache.conf","$confdir/startup.pl"));
   78:     if (@paths) {
   79:         my $pathstr = join (' ',@paths);
   80:         if (open($dirh,"grep '$revtag' $pathstr 2>&1 |")) {
   81:             while (my $line=<$dirh>) {
   82:                 if ($line =~ m{^([^#]+):#\s\$Id:\s[\w.]+,v\s([\d.]+)\s}) {
   83:                     $versions{$1} = $2;
   84:                 }
   85:             }
   86:             close($dirh);
   87:         }
   88:         foreach my $key (sort(keys(%versions))) {
   89:             next if ($key =~ /\.lpmlsave$/);
   90:             my $sum;
   91:             if (open(my $fh,"<$key")) {
   92:                 binmode $fh;
   93:                 my $sha_obj = Digest::SHA->new();
   94:                 $sha_obj->addfile($fh);
   95:                 $sum = $sha_obj->hexdigest;
   96:                 close($fh);
   97:                 $chksums{$key} = $sum; 
   98:             }
   99:             $output .= "$key,$versions{$key},$sum\n";
  100:         }
  101:     }
  102:     if ($lontabdir ne '') {
  103:         if (open(my $tabfh,">$lontabdir/lonchksums.tab")) {
  104:             print $tabfh '# Last written: '.localtime(time)."\n";
  105:             print $tabfh "$output\n";
  106:             close($tabfh);
  107:         }
  108:     }
  109:     return (\%chksums,\%versions);
  110: }
  111: 
  112: sub compare_checksums {
  113:     my ($target,$lonhost,$version,$serversums,$serverversions) = @_;
  114:     my ($message,$numchg,$linefeed);
  115:     if ($target eq 'web') {
  116:         $linefeed = '<br />';
  117:     } else {
  118:         $linefeed = "\n";
  119:     }
  120:     if (!$Apache::lonlocal::lh) {
  121:         &Apache::lonlocal::get_language_handle();
  122:     }
  123:     if ((ref($serversums) eq 'HASH') && (keys(%{$serversums}))) {
  124:         my $checksums = &Apache::lonnet::fetch_dns_checksums();
  125:         my (%extra,%missing,%diffs,%stdsums,%stdversions);
  126:         if (ref($checksums) eq 'HASH') {
  127:             if (ref($checksums->{'sums'}) eq 'HASH') {
  128:                 %stdsums = %{$checksums->{'sums'}};
  129:             }
  130:             if (ref($checksums->{'versions'}) eq 'HASH') {
  131:                 %stdversions = %{$checksums->{'versions'}};
  132:             }
  133:             if (keys(%stdsums)) {
  134:                 foreach my $key (keys(%stdsums)) {
  135:                     if (exists($serversums->{$key})) {
  136:                         if ($serversums->{$key} ne $stdsums{$key}) {
  137:                             $diffs{$key} = 1;
  138:                             $numchg ++;
  139:                         }
  140:                     } else {
  141:                         unless ((-e $key) && (-B $key)) {
  142:                             $missing{$key} = 1;
  143:                             $numchg ++;
  144:                         }
  145:                     }
  146:                 }
  147:                 foreach my $key (keys(%{$serversums})) {
  148:                     unless (exists($stdsums{$key})) {
  149:                         $extra{$key} = 1;
  150:                         $numchg ++;
  151:                     }
  152:                 }
  153:             }
  154:             if ($numchg) {
  155:                 $message =
  156:                     &Apache::lonlocal::mt('[quant,_1,difference was,differences were] found'.
  157:                                           ' between LON-CAPA modules installed on your server [_2]'.
  158:                                           ' and those expected for the LON-CAPA version you are'.
  159:                                           ' currently running.',$numchg,"($lonhost)$linefeed");
  160:                 if ($target eq 'web') {
  161:                     $message = '<p>'.$message.'</p>';
  162:                 } else {
  163:                     $message .= "\n\n";
  164:                 }
  165:                 my (@diffversion,@modified);
  166:                 if (keys(%diffs) > 0) {
  167:                     foreach my $file (sort(keys(%diffs))) {
  168:                         if ($serverversions->{$file} ne $stdversions{$file}) {
  169:                             push(@diffversion,$file);
  170:                         } else {
  171:                             push(@modified,$file);
  172:                         }
  173:                     }
  174:                     if (@diffversion > 0) {
  175:                         my $text =
  176:                             &Apache::lonlocal::mt('The following [quant,_1,file is a,files are]'.
  177:                                                   ' different version(s) from that expected for LON-CAPA [_2]:',
  178:                                                   scalar(@diffversion),$version);
  179:                         if ($target eq 'web') {
  180:                             $message .= '<p>'.$text.'</p>'.
  181:                                         &Apache::loncommon::start_data_table().
  182:                                         &Apache::loncommon::start_data_table_header_row()."\n".
  183:                                         '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  184:                                         '<th>'.&Apache::lonlocal::mt('Server version').'</th>'."\n".
  185:                                         '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
  186:                                         &Apache::loncommon::end_data_table_header_row()."\n";
  187:                         } else {
  188:                             $message .= "$text\n";
  189:                         }
  190:                         foreach my $file (sort(@diffversion)) {
  191:                             my $revnum = $stdversions{$file};
  192:                             if ($target eq 'web') {
  193:                                 $message .=  &Apache::loncommon::start_data_table_row().
  194:                                              '<td>'.$file.'</td>'."\n".
  195:                                              '<td>'.$serverversions->{$file}.'</td>'."\n".
  196:                                              '<td>'.$revnum.'</td>'."\n".
  197:                                              &Apache::loncommon::end_data_table_row()."\n";
  198:                             } else {
  199:                                 $message .= $file.' '.
  200:                                             &Apache::lonlocal::mt('(local rev: [_1])',
  201:                                                                   $serverversions->{$file});
  202:                                 if ($revnum) {
  203:                                     $message .= '; '.
  204:                                                 &Apache::lonlocal::mt('(expected rev: [_1])',
  205:                                                                       $revnum);
  206:                                 }
  207:                                 $message .= "\n";
  208:                             }
  209:                         }
  210:                         if ($target eq 'web') {
  211:                             $message .= &Apache::loncommon::end_data_table().'<br />';
  212:                         } else {
  213:                             $message .= "\n";
  214:                         }
  215:                     }
  216:                     if (@modified > 0) {
  217:                         my $text =
  218:                             &Apache::lonlocal::mt('The following [quant,_1,file appears,files appear]'.
  219:                                                   ' to have been modified locally:',scalar(@modified));
  220:                         if ($target eq 'web') {
  221:                             $message .= '<p>'.$text.'</p>'.
  222:                                         &Apache::loncommon::start_data_table().
  223:                                         &Apache::loncommon::start_data_table_header_row()."\n".
  224:                                         '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  225:                                         '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
  226:                                         &Apache::loncommon::end_data_table_header_row()."\n";
  227:                         } else {
  228:                             $message .= "$text\n";
  229:                         }
  230:                         foreach my $file (sort(@modified)) {
  231:                             if ($target eq 'web') {
  232:                                 $message .= &Apache::loncommon::start_data_table_row()."\n".
  233:                                             '<td>'.$file.'</td>'."\n".
  234:                                             '<td>'.$serverversions->{$file}.'</td>'."\n".
  235:                                             &Apache::loncommon::end_data_table_row()."\n";
  236:                             } else {
  237:                                 $message .= $file.' '.
  238:                                             &Apache::lonlocal::mt('(local rev: [_1])',
  239:                                                                   $serverversions->{$file}).
  240:                                             "\n";
  241:                             }
  242:                         }
  243:                         if ($target eq 'web') {
  244:                             $message .= &Apache::loncommon::end_data_table().'<br />';
  245:                         } else {
  246:                             $message .= "\n";
  247:                         }
  248:                     }
  249:                 }
  250:                 if (keys(%missing) > 0) {
  251:                     my $text = 
  252:                         &Apache::lonlocal::mt('The following [quant,_1,local file appears,local files appear]'.
  253:                                               ' to be missing:',scalar(keys(%missing))); 
  254:                     if ($target eq 'web') {
  255:                         $message .= '<p>'.$text.'</p>'.
  256:                                     &Apache::loncommon::start_data_table().
  257:                                     &Apache::loncommon::start_data_table_header_row()."\n".
  258:                                     '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  259:                                     '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
  260:                                     &Apache::loncommon::end_data_table_header_row()."\n";
  261:                     } else {
  262:                         $message .= "$text\n";
  263:                     }
  264:                     foreach my $file (sort(keys(%missing))) {
  265:                         my $revnum = $stdversions{$file};
  266:                         if ($target eq 'web') {
  267:                             $message .= &Apache::loncommon::start_data_table_row()."\n".
  268:                                         '<td>'.$file.'</td>'."\n".
  269:                                         '<td>'.$revnum.'</td>'."\n".
  270:                                         &Apache::loncommon::end_data_table_row()."\n";
  271:                         } else {
  272:                             $message .= $file;
  273:                             if ($revnum) {
  274:                                 $message .= ' '.
  275:                                             &Apache::lonlocal::mt('(expected rev: [_1])',
  276:                                                                   $revnum);
  277:                             }
  278:                             $message .= "\n";
  279:                         }
  280:                     }
  281:                     if ($target eq 'web') {
  282:                         $message .= &Apache::loncommon::end_data_table();
  283:                     } else {
  284:                         $message .= "\n";
  285:                     }
  286:                 }
  287:                 if (keys(%extra) > 0) {
  288:                     my $text = 
  289:                         &Apache::lonlocal::mt('The following [quant,_1,file is,files are]'.
  290:                                               ' not required by the release you have installed:',
  291:                                               scalar(keys(%extra)));
  292:                     if ($target eq 'web') {
  293:                         $message .= '<p>'.$text.'</p>'.
  294:                                     &Apache::loncommon::start_data_table().
  295:                                     &Apache::loncommon::start_data_table_header_row()."\n".
  296:                                     '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  297:                                     '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
  298:                                     &Apache::loncommon::end_data_table_header_row()."\n";
  299:                     } else {
  300:                         $message .= "$text\n";
  301:                     }
  302:                     foreach my $file (sort(keys(%extra))) {
  303:                         if ($target eq 'web') {
  304:                             $message .= &Apache::loncommon::start_data_table_row()."\n".
  305:                                         '<td>'.$file.'</td>'."\n".
  306:                                         '<td>'.$serverversions->{$file}.'</td>'."\n".
  307:                                         &Apache::loncommon::end_data_table_row()."\n";
  308:                         } else {
  309:                             $message .= $file.' '.
  310:                                         &Apache::lonlocal::mt('(local rev: [_1])',
  311:                                                               $serverversions->{$file}).
  312:                                         "\n";
  313:                         }
  314:                     }
  315:                     if ($target eq 'web') {
  316:                         $message .= &Apache::loncommon::end_data_table().'<br />';
  317:                     } else {
  318:                         $message .= "\n";
  319:                     }
  320:                 }
  321:             } else {
  322:                 $message = &Apache::lonlocal::mt('No differences detected between installed files and files expected for LON-CAPA [_1]',$version);
  323:             }
  324:         } else {
  325:             $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
  326:         }
  327:     } else {
  328:         $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
  329:     }
  330:     unless ($message) {
  331:         $message = &Apache::lonlocal::mt('LON-CAPA module check found no file changes.')."\n";
  332:     }
  333:     return ($message,$numchg);
  334: }
  335: 
  336: 1;
  337: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>