Annotation of loncom/configuration/Checksumming.pm, revision 1.4
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Checksum installed LON-CAPA modules and some configuration files
3: #
1.4 ! raeburn 4: # $Id: Checksumming.pm,v 1.3 2013/02/04 15:05:23 raeburn Exp $
1.1 raeburn 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:
38: sub get_checksums {
39: my ($distro,$londaemons,$lonlib,$lonincludes,$lontabdir) = @_;
40: my $output;
41: my (%versions,%chksums);
42: my $dirh;
43: my $revtag = '$Id:';
44: my @paths;
45: if ($londaemons) {
46: push(@paths,($londaemons.'/*',
47: $londaemons.'/debug/*.pl'));
48: }
49: if ($lonlib) {
50: push(@paths,($lonlib.'/perl/Apache/*.pm',
51: $lonlib.'/perl/Apache/localize/*.pm',
52: $lonlib.'/perl/LONCAPA/*.pm',
53: $lonlib.'/perl/AlgParser.pm',
54: $lonlib.'/perl/LONCAPA.pm',
55: $lonlib.'/perl/Safe.pm',
56: $lonlib.'/perl/*-std.pm',
57: $lonlib.'/perl/HTML/*.pm',
58: $lonlib.'/perl/Safe.pm'));
59: }
60: if ($lonincludes) {
61: push(@paths,$lonincludes.'/*.lcpm');
62: }
63: push(@paths,('/home/httpd/cgi-bin/*.pl','/home/httpd/cgi-bin/*.png'));
64: my $confdir = '/etc/httpd/conf';
65: my $sha = 'SHA1';
66: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
67: $confdir = '/etc/apache2';
68: if (($1 eq 'ubuntu') && ($2 >= 12)) {
69: $sha = 'SHA';
70: }
71: } elsif ($distro =~ /^sles(\d+)$/) {
72: if ($1 >= 10) {
73: $confdir = '/etc/apache2';
74: }
75: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
76: if ($1 >= 10.0) {
77: $confdir = '/etc/apache2';
78: }
79: }
80: push(@paths,("$confdir/loncapa_apache.conf","$confdir/startup.pl"));
81: if ($sha eq 'SHA1') {
82: require Digest::SHA1;
83: } else {
84: require Digest::SHA;
85: }
86: if (@paths) {
87: my $pathstr = join (' ',@paths);
88: if (open($dirh,"grep '$revtag' $pathstr |")) {
89: while (my $line=<$dirh>) {
90: if ($line =~ m{^([^#]+):#\s\$Id:\s[\w.]+,v\s([\d.]+)\s}) {
91: $versions{$1} = $2;
92: }
93: }
94: close($dirh);
95: }
96: foreach my $key (sort(keys(%versions))) {
97: next if ($key =~ /\.lpmlsave$/);
98: my $sum;
99: if (open(my $fh,"<$key")) {
100: binmode $fh;
101: my $sha_obj;
102: if ($sha eq 'SHA') {
103: $sha_obj = Digest::SHA->new();
104: } else {
105: $sha_obj = Digest::SHA1->new();
106: }
107: $sha_obj->addfile($fh);
108: $sum = $sha_obj->hexdigest;
109: close($fh);
110: $chksums{$key} = $sum;
111: }
112: $output .= "$key,$versions{$key},$sum\n";
113: }
114: }
115: if ($lontabdir ne '') {
116: if (open(my $tabfh,">$lontabdir/lonchksums.tab")) {
117: print $tabfh '# Last written: '.localtime(time)."\n";
118: print $tabfh "$output\n";
119: close($tabfh);
120: }
121: }
122: return (\%chksums,\%versions);
123: }
124:
125: sub compare_checksums {
126: my ($target,$lonhost,$version,$serversums,$serverversions) = @_;
1.2 raeburn 127: my ($message,$numchg,$linefeed);
1.3 raeburn 128: if ($target eq 'web') {
1.2 raeburn 129: $linefeed = '<br />';
130: } else {
131: $linefeed = "\n";
132: }
1.1 raeburn 133: if ((ref($serversums) eq 'HASH') && (keys(%{$serversums}))) {
134: my $checksums = &Apache::lonnet::fetch_dns_checksums();
135: my (%extra,%missing,%diffs,%stdsums,%stdversions);
136: if (ref($checksums) eq 'HASH') {
137: if (ref($checksums->{'sums'}) eq 'HASH') {
138: %stdsums = %{$checksums->{'sums'}};
139: }
140: if (ref($checksums->{'versions'}) eq 'HASH') {
141: %stdversions = %{$checksums->{'versions'}};
142: }
143: if (keys(%stdsums)) {
144: foreach my $key (keys(%stdsums)) {
145: if (exists($serversums->{$key})) {
146: if ($serversums->{$key} ne $stdsums{$key}) {
147: $diffs{$key} = 1;
148: $numchg ++;
149: }
150: } else {
1.4 ! raeburn 151: unless ((-e $key) && (-B $key)) {
! 152: $missing{$key} = 1;
! 153: $numchg ++;
! 154: }
1.1 raeburn 155: }
156: }
157: foreach my $key (keys(%{$serversums})) {
158: unless (exists($stdsums{$key})) {
159: $extra{$key} = 1;
160: $numchg ++;
161: }
162: }
163: }
164: if ($numchg) {
1.2 raeburn 165: $message =
1.1 raeburn 166: &Apache::lonlocal::mt('[quant,_1,difference was,differences were] found'.
1.2 raeburn 167: ' between LON-CAPA modules installed on your server [_2]'.
168: ' and those expected for the LON-CAPA version you are'.
169: ' currently running.',$numchg,"($lonhost)$linefeed");
1.1 raeburn 170: if ($target eq 'web') {
171: $message = '<p>'.$message.'</p>';
172: } else {
173: $message .= "\n\n";
174: }
175: my (@diffversion,@modified);
176: if (keys(%diffs) > 0) {
177: foreach my $file (sort(keys(%diffs))) {
178: if ($serverversions->{$file} ne $stdversions{$file}) {
179: push(@diffversion,$file);
180: } else {
181: push(@modified,$file);
182: }
183: }
184: if (@diffversion > 0) {
1.2 raeburn 185: my $text =
186: &Apache::lonlocal::mt('The following [quant,_1,file is a,files are]'.
1.1 raeburn 187: ' different version(s) from that expected for LON-CAPA [_2]:',
188: scalar(@diffversion),$version);
189: if ($target eq 'web') {
190: $message .= '<p>'.$text.'</p>'.
191: &Apache::loncommon::start_data_table().
192: &Apache::loncommon::start_data_table_header_row()."\n".
193: '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
194: '<th>'.&Apache::lonlocal::mt('Server version').'</th>'."\n".
195: '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
196: &Apache::loncommon::end_data_table_header_row()."\n";
197: } else {
198: $message .= "$text\n";
199: }
200: foreach my $file (sort(@diffversion)) {
201: my $revnum = $stdversions{$file};
202: if ($target eq 'web') {
203: $message .= &Apache::loncommon::start_data_table_row().
1.2 raeburn 204: '<td>'.$file.'</td>'."\n".
1.1 raeburn 205: '<td>'.$serverversions->{$file}.'</td>'."\n".
206: '<td>'.$revnum.'</td>'."\n".
207: &Apache::loncommon::end_data_table_row()."\n";
208: } else {
209: $message .= $file.' '.
210: &Apache::lonlocal::mt('(local rev: [_1])',
211: $serverversions->{$file});
212: if ($revnum) {
213: $message .= '; '.
214: &Apache::lonlocal::mt('(expected rev: [_1])',
215: $revnum);
216: }
217: $message .= "\n";
218: }
219: }
1.2 raeburn 220: if ($target eq 'web') {
221: $message .= &Apache::loncommon::end_data_table().'<br />';
222: } else {
223: $message .= "\n";
224: }
1.1 raeburn 225: }
226: if (@modified > 0) {
1.2 raeburn 227: my $text =
1.1 raeburn 228: &Apache::lonlocal::mt('The following [quant,_1,file appears,files appear]'.
229: ' to have been modified locally:',scalar(@modified));
230: if ($target eq 'web') {
231: $message .= '<p>'.$text.'</p>'.
232: &Apache::loncommon::start_data_table().
233: &Apache::loncommon::start_data_table_header_row()."\n".
234: '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
235: '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
236: &Apache::loncommon::end_data_table_header_row()."\n";
237: } else {
238: $message .= "$text\n";
239: }
240: foreach my $file (sort(@modified)) {
241: if ($target eq 'web') {
242: $message .= &Apache::loncommon::start_data_table_row()."\n".
243: '<td>'.$file.'</td>'."\n".
244: '<td>'.$serverversions->{$file}.'</td>'."\n".
245: &Apache::loncommon::end_data_table_row()."\n";
246: } else {
247: $message .= $file.' '.
248: &Apache::lonlocal::mt('(local rev: [_1])',
249: $serverversions->{$file}).
250: "\n";
251: }
252: }
1.2 raeburn 253: if ($target eq 'web') {
254: $message .= &Apache::loncommon::end_data_table().'<br />';
255: } else {
256: $message .= "\n";
257: }
1.1 raeburn 258: }
259: }
260: if (keys(%missing) > 0) {
261: my $text =
262: &Apache::lonlocal::mt('The following [quant,_1,local file appears,local files appear]'.
263: ' to be missing:',scalar(keys(%missing)));
264: if ($target eq 'web') {
265: $message .= '<p>'.$text.'</p>'.
266: &Apache::loncommon::start_data_table().
267: &Apache::loncommon::start_data_table_header_row()."\n".
268: '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
269: '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
270: &Apache::loncommon::end_data_table_header_row()."\n";
271: } else {
272: $message .= "$text\n";
273: }
274: foreach my $file (sort(keys(%missing))) {
275: my $revnum = $stdversions{$file};
276: if ($target eq 'web') {
1.2 raeburn 277: $message .= '<td>'.$file.'</td>'."\n".
1.1 raeburn 278: '<td>'.$revnum.'</td>'."\n".
279: &Apache::loncommon::end_data_table_row()."\n";
280: } else {
281: $message .= $file;
282: if ($revnum) {
283: $message .= ' '.
284: &Apache::lonlocal::mt('(expected rev: [_1])',
285: $revnum);
286: }
287: $message .= "\n";
288: }
289: }
1.2 raeburn 290: if ($target eq 'web') {
291: $message .= &Apache::loncommon::end_data_table();
292: } else {
293: $message .= "\n";
294: }
1.1 raeburn 295: }
296: if (keys(%extra) > 0) {
297: my $text =
298: &Apache::lonlocal::mt('The following [quant,_1,file is,files are]'.
299: ' not required by the release you have installed:',
300: scalar(keys(%extra)));
301: if ($target eq 'web') {
302: $message .= '<p>'.$text.'</p>'.
303: &Apache::loncommon::start_data_table().
304: &Apache::loncommon::start_data_table_header_row()."\n".
305: '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
306: '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
307: &Apache::loncommon::end_data_table_header_row()."\n";
308: } else {
309: $message .= "$text\n";
310: }
311: foreach my $file (sort(keys(%extra))) {
312: if ($target eq 'web') {
1.2 raeburn 313: $message .= '<td>'.$file.'</td>'."\n".
1.1 raeburn 314: '<td>'.$serverversions->{$file}.'</td>'."\n".
315: &Apache::loncommon::end_data_table_row()."\n";
316: } else {
317: $message .= $file.' '.
318: &Apache::lonlocal::mt('(local rev: [_1])',
319: $serverversions->{$file}).
320: "\n";
321: }
322: }
1.2 raeburn 323: if ($target eq 'web') {
324: $message .= &Apache::loncommon::end_data_table().'<br />';
325: } else {
326: $message .= "\n";
327: }
1.1 raeburn 328: }
329: } else {
330: $message = &Apache::lonlocal::mt('No differences detected between installed files and files expected for LON-CAPA [_1]',$version);
331: }
332: } else {
333: $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
334: }
335: } else {
336: $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
337: }
338: unless ($message) {
339: $message = &Apache::lonlocal::mt('LON-CAPA module check found no file changes.')."\n";
340: }
341: return ($message,$numchg);
342: }
343:
344: 1;
345:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>