first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / system / machine-update.pl
1 #!/usr/bin/perl -w
2 # For Emacs: -*- mode:cperl; mode:folding; -*-
3 #
4 # Get a machine's critical features, And mail/http them to the Linux Counter
5 #
6 # (c) 1999 - Harald Tveit Alvestrand, the Linux Counter Project
7 #     2003 - PetaMem Group (www.petamem.com)
8 # License:   GNU Copyleft - see bottom of file.
9 # Changelog: see even more bottom of the file
10 #
11 # As a matter of courtesy, if you change this file on your own,
12 # make sure it does NOT mail to the counter!
13 #
14 use strict;
15 use POSIX;
16
17 our $VERSION     = '0.32';
18 our $CVS_VERSION = '$Revision: 1.8 $ $Date: 2007/01/21 17:22:50 $ $Author: patrick $';
19 our $IsInTestHarness;
20 use vars qw(%values %oldvalues $errordata $debugdata %files); # data that is sent
21 use vars qw($progname %option $mailprogram);
22 use vars qw(%is_sys_account %is_user %is_account);
23
24 # stuff that controls defaults for passwdscan & accounts subroutines
25 my ($UID_MIN, $UID_MAX, $got_defs) = (100, 65533, '');
26
27 # Make sure nothing happens, so that the script's routines
28 # can be debugged from another file
29 return 1 if($IsInTestHarness);
30
31 &preparation;
32 &options;
33
34 &readfile;
35 &checkconfig;
36
37 if ($option{ask}) {
38   &askquestions;
39 }
40
41 &writefile;
42 &sendfile;
43
44 # {{{ preparation
45
46 #
47 sub preparation {
48   die "No HOME environment variable\n" if (!$ENV{HOME});
49   die "No home diretory\n" if ! -d $ENV{HOME};
50   # Kill some internationalization
51   $ENV{LANG} = 'C';
52   delete $ENV{LC_CTYPE};
53   delete $ENV{LC_NUMERIC};
54   delete $ENV{LC_NAME};
55   delete $ENV{LC_TIME};
56   delete $ENV{LC_MESSAGES};
57   delete $ENV{LC_COLLATE};
58   delete $ENV{LC_MONETARY};
59
60   my $infodir = "$ENV{HOME}/.linuxcounter";
61   if (! -d $infodir) {
62     mkdir($infodir, 0766) || die "Unable to make $infodir\n";
63   }
64   # Keep track of where I am; need it to install crontab entry
65   # progname is a global.
66   $progname = $0;
67   if ($progname !~ /^\//) {
68     my $progdir = `pwd`;
69     chop $progdir;
70     $progname = "$progdir/$progname";
71     $progname =~ s!/./!/!;
72   }
73   chdir($infodir) || die "Unable to change to $infodir\n";
74   my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
75   if (! -f $nodename) {
76     print STDERR "Machine-update $VERSION. Use $0 -l to display license.\n";
77     print STDERR "Creating the infofile for your computer.\n";
78     # Create the infodir
79     open(INFO, ">$nodename");
80     print INFO "uniqueid: ", randomnumber(), "\n";
81     close INFO;
82   }
83
84   srand time % $$;            # do some seed "randomization"
85
86   # Find out what mail program to use !! Terrible kludge !!
87   $mailprogram = "/usr/bin/mail";
88   if (! -x "$mailprogram") {
89     if (-x "/bin/mail") {
90       $mailprogram = "/bin/mail";
91     } else {
92       if (-x "/usr/sbin/sendmail") {
93         $mailprogram = "/usr/sbin/sendmail";
94       } else {
95         if (-x "/usr/lib/sendmail") {
96           $mailprogram = "/usr/lib/sendmail";
97         } else {
98           if (-x "/usr/bin/mutt") {
99             $mailprogram = "/usr/bin/mutt";
100           } else {
101             if (-x "/usr/sbin/exim4") {
102               $mailprogram = "/usr/sbin/exim4";
103             } else {
104               if (-x "/usr/bin/elm") {
105                 $mailprogram = "/usr/bin/elm";
106               } else {
107                 print "--------------------------------------------------------\n";
108                 print "!!!         Cannot find a mail program to use        !!!\n";
109                 print "Please copy the output into a new email and send that\n";
110                 print "email manually to machine-registration\@linuxcounter.net\n";
111                 print "--------------------------------------------------------\n";
112               }
113             }
114           }
115         }
116       }
117     }
118   }
119 }
120
121 # }}}
122 # {{{ options
123
124 #
125 sub options {
126   my $opt;
127
128   while (defined($ARGV[0]) && $ARGV[0] =~ /^-/) {
129     $opt = shift @ARGV;
130     $opt =~ /c/ && &installcrontab;
131     $opt =~ /d/ && $option{DEBUG}++ && print STDERR "Debug is $option{DEBUG}\n";
132     $opt =~ /h/ && &help;
133     $opt =~ /i/ && ($option{ask}  = 1);
134     $opt =~ /l/ && &license;
135     $opt =~ /m/ && ($option{mail} = 1);
136     $opt =~ /t/ && ($option{mail} = 0);
137     $opt =~ /u/ && &uninstallcrontab;
138     $opt =~ /v/ && die "\n\t Linux Counter machine-update version $VERSION\n"
139       . "\tCVS version $CVS_VERSION\n";
140     $opt =~ /x/ && ($option{info} = 1);
141   }
142 }
143
144 # }}}
145
146 # {{{ askquestions
147
148 #
149 sub askquestions {
150   return if ! -t STDIN || ! -t STDOUT;
151   $| = 1;
152   print "Here you can specify some info that the script can't know for itself\n";
153   $values{owner} = askone("Your Linux Counter reg#, if any", $values{owner});
154   $values{key}   = askone("Your machine's counter reg#, if any", $values{key});
155 }
156
157 # }}}
158 # {{{ askone
159 #
160 sub askone {
161   my $prompt  = shift;
162   my $default = shift;
163
164   print $prompt;
165   if (defined($default)) {
166     print " [$default]";
167   }
168   print ':';
169
170   my $ans = <STDIN>;
171
172   chop $ans;
173   &Debug("Answer was $ans\n");
174   $ans = $default if (!length($ans));
175
176   return $ans;
177 }
178 # }}}
179
180 # {{{ readfile
181
182 #
183 sub readfile {
184   my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
185   open(INFO, $nodename) || die "Did not find infofile $nodename\n";
186   while (<INFO>) {
187     chop;
188     s/#.*//;
189     if (/^(\S+): *(.+)/) {
190       my $key = $1;
191       my $value = $2;
192       if ($1 !~ /^(owner|key|uniqueid)$/) {
193         next;
194       }
195       &Debug("Read $key: $value\n");
196       $values{$key} = $value;
197     } else {
198       print STDERR "Unparsed info line: $_ - discarded\n";
199     }
200   }
201   close INFO;
202   %oldvalues = %values;
203 }
204
205 # }}}
206 # {{{ writefile
207
208 #
209 sub writefile {
210   my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
211
212   open(INFO, ">$nodename.new");
213   for my $val (sort keys(%values)) {
214     &Debug("Saving $val: $values{$val}\n");
215     print INFO "$val: $values{$val}\n";
216   }
217   close INFO;
218   rename("$nodename.new", $nodename) || die "Rename failed\n";
219 }
220
221 # }}}
222 # {{{ sendfile
223
224 #
225 sub sendfile {
226   if ($option{mail}) {
227     open(MAIL, "|$mailprogram machine-registration\@linuxcounter.net")
228       || die "Unable to open $mailprogram\n";
229   } else {
230     warn "--------------------------------------------------------\n";
231     warn "This is what will be sent to the Linux Counter if you\n";
232     warn "run the program with the -m switch. Now, NOTHING IS SENT\n";
233     warn "--------------------------------------------------------\n";
234     open(MAIL, ">&STDOUT");
235   }
236   # note that $ENV{USER} isn't (always) set in a cron job...
237   my $user = (getpwuid($<))[0];
238   $user = "unknown-id-$<" if !$user;
239   print MAIL <<EOF
240 From: $user
241 To: machine-registration\@linuxcounter.net
242 Subject: machine-update for $values{name}
243
244 Mail program that sent this email: $mailprogram
245 Perl version used to run machine-update: $]
246
247 //MACHINE
248 EOF
249     ;
250   for my $val (sort keys(%values)) {
251     print MAIL "$val: $values{$val}\n"
252       if length($values{$val}) > 0;
253   }
254   print MAIL "//END\n";
255   # Attach files
256   for my $file (keys(%files)) {
257     print MAIL "//FILE $file\n";
258     print MAIL $files{$file};
259     print MAIL "//EOF\n";
260   }
261
262
263   # Attach possible other info
264   if ($errordata) {
265     print MAIL "----- Problem info gathered during probing -----\n";
266     print MAIL $errordata;
267   }
268   $option{info} && do {
269     print MAIL "----- Debug data for the script maintainer's aid -----\n";
270     print MAIL $debugdata;
271   };
272   close MAIL;
273 }
274
275 # }}}
276
277 # {{{ randomnumber
278 #
279 sub randomnumber {
280   return int(rand(1_000_000_000));
281 }
282 # }}}
283
284 # {{{ checkconfig
285
286 #
287 sub checkconfig {
288   my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
289
290   warn "This is not Linux, but $sysname!\n" if($sysname ne 'Linux');
291   $values{method}    = "machine-update version $VERSION";
292   $values{os}        = $sysname;
293   $values{kernel}    = $release;
294   $values{cpu_uname} = $machine;
295   $values{name}      = $nodename;       # First order guess
296
297   # Credit for some of the code below goes to
298   # Denis Havlik: <havlik@ap.univie.ac.at>
299   # Blame is, of course, all mine - HTA -
300   # Note - there are numerous problems with df, including:
301   # - early versions don't support the -l option
302   # - at least some include SAMBA filesystems in the -l option
303   # 1: Snarf a df -T
304   my $dfbin = &xbin("df");
305   $files{"df -T"} = `$dfbin -T -x nfs`;
306
307   $values{accounts} = &accounts;
308   $values{users}    = &active_users;
309
310   my $uptime = &xbin('uptime');
311   if($uptime) {
312     $uptime = `$uptime`;
313     $values{uptime_1} = $uptime; # preserve raw version
314     $values{uptime_1} =~ s/\n.*//;
315   }
316   my $lastprog = xbin('last');
317   if ($lastprog && -r "/var/run/utmp") {
318     $values{uptime_2} = `$lastprog -xf /var/run/utmp runlevel`;
319     $values{uptime_2} =~ s/\n.*$//s;
320   } else {
321     DebugInfo("Can't do last to find uptime");
322   }
323   # Not sure this is a Right Thing...so not saving it for the moment
324   # This section based on a patch from Mark-Jason Dominus <mjd@plover.com>
325   # try to guess mailer based on content of /usr/lib/sendmail link
326   if (-l '/usr/lib/sendmail') {
327     my $realsendmail = readlink('/usr/lib/sendmail');
328     if ($realsendmail eq '../sbin/sendmail') {
329       $realsendmail = '/usr/sbin/sendmail';
330       if (-l $realsendmail) {
331         $realsendmail = readlink($realsendmail);
332       }
333     }
334     if ($realsendmail =~ m{^/var/qmail}) {
335       $values{mailer} = "qmail";
336     } else {
337       &DebugInfo("Found sendmail as a link to $realsendmail\n");
338     }
339   }
340   # Link method did not work. Try to guess based on presence of
341   # config files. (this is more susceptible to the old-junk problem)
342   if (!$values{mailer}) {
343     if ( -d '/var/qmail') {
344       $values{mailer} = 'qmail';
345     } elsif ( -f '/etc/sendmail.cf' || -f '/etc/mail/sendmail.cf') {
346       # TMDG claims recent Fedora Core has it in /etc/mail/sendmail.cf
347       $values{mailer} = 'sendmail';
348     } elsif ( -d '/etc/postfix') {
349       $values{mailer} = 'postfix';
350     }
351   }
352
353   $values{kcoresize} = -s "/proc/kcore" || 0;
354   addonefileforsending("/proc/meminfo");
355   addonefileforsending("/proc/cpuinfo");
356   addonefileforsending("/proc/version");
357   # info on what devices are in use on the system
358   addonefileforsending("/proc/pci");
359   addonefileforsending("/proc/bus/usb/devices");
360   # Both Mandrake and Red Hat use this file....
361   addonefileforsending("/etc/redhat-release");
362 }
363
364 # }}}
365
366 # {{{ accounts
367
368 #
369 sub accounts {
370   my $s;
371   my $niss;
372   my $ypcatbin;                # will hold path to the ypcat binary (if any)
373
374   open (TMP,"</etc/passwd");
375   $s += &passwdscan;
376   &DebugErr("Found $s accounts total\n");
377   &Debug("Switching to NIS passwords\n");
378
379   $ypcatbin = &xbin('ypcat');  # get path to ypcat binary (empty if none)
380   if($ypcatbin) {              # test whether ypcat was found
381     open TMP, "$ypcatbin passwd 2> /dev/null|"
382       || ($errordata .= "ypcat failed: $!\n");
383     $niss = &passwdscan;
384     $s   += $niss;
385     close TMP;
386     &Debug("Status of ypcat: $?\n");
387     &DebugErr("Found $niss accounts in ypcat passwd\n");
388   }
389
390   &DebugErr('Sysaccounts: ', join(' ', keys(%is_sys_account)), "\n");
391   &DebugErr("Found $s accounts total\n");
392
393   return $s;
394 }
395
396 # }}}
397 # {{{ passwdscan
398
399 #
400 sub passwdscan {
401   # Code for reading login.defs courtesy of Vassilii Khachaturov
402   # <vassilii@tarunz.org>
403   local (*DEFS);
404   # Try importing UID_MIN and UID_MAX from /etc/login.defs, if possible
405   # else just assume the above defaults for min and max non-system UID
406   if (!$got_defs && open (DEFS, '/etc/login.defs')) {
407     while (<DEFS>) {
408       if (/^\s*(UID_(?:MIN|MAX))\s+(\d+)/) {
409         # elegant, but not compatible with "strict refs":
410         #${ $1 } = $2;
411         if ($1 eq "UID_MIN") {
412           $UID_MIN = $2;
413         } else {
414           $UID_MAX = $2;
415         }
416         &Debug("DEFS match: $1 = $2\n");
417       }
418     }
419     close (DEFS);
420     $got_defs = 1;
421   }
422   &Debug("UID_MIN = $UID_MIN, UID_MAX = $UID_MAX\n");
423   # I suppose this is as good as it gets - 
424   #   Usually user accounts have UID > 100 and 
425   #   "system accounts" have UID < 100, but there is no guarantee   
426   # that 
427   #   this will hold for pseudo-users like "postgress" etc.
428   # Also nobody is usually 99 on linux, but -1 on "standard" unices.
429   # RedHat places the dividing line at 500. Others use 400...
430   my @line;
431   my $s = 0;
432
433   while (<TMP>) {
434     @line = split ':';
435     if (!($line[2] eq "")) {
436       if ($line[2] >= $UID_MIN && $line[2] <= $UID_MAX
437         && !($line[0] eq 'nobody')) {
438         $s++;
439         $is_account{$line[0]} = 1;
440       } else {
441         $is_sys_account{$line[0]} = 1;
442       }
443     }
444   }
445
446   return $s;
447 }
448
449 # }}}
450 # {{{ active_users
451
452 #
453 # This is kind of alpha, but please test it.
454 # It calculates the number of "active" users based on the "wtmp" entries
455 # unfortunately at least Mandrake 8 and 9 ship with non-world-read wtmp
456 # and non-set-uid last, so this does not work any more...
457 #
458 # RJ: Actually I think the best thing to do is to bury this code and be silent about it.
459 #
460 sub active_users {
461   my $userslisted;
462
463   for (qw(reboot wtmp runlevel)) {  # This sysaccounts shouldn't be counted. Who else?
464     $is_sys_account{$_} = 1;
465   }
466   open( TMP, "/usr/bin/last 2>&1|");
467   while (<TMP>) {
468     chop;
469     if (m!/var/log/wtmp: Permission denied!) { # RJ: ***Boom*** on every non-EN system
470       &ErrorInfo("/usr/bin/last failed because /var/log/wtmp isn't readable\n");
471       last;
472     }
473     last if(!$_);                            # RJ: quick hack to safe bad code from harm
474     my @tmp  = split;
475     my $name = $tmp[0];
476     if ($is_sys_account{$name}) {
477       # do nothing
478     } elsif (defined $is_account{$name}) {
479       $is_user{$name} = 1;
480     } elsif (/^\s*$/) {                      # blank line - do nothing
481     } elsif ($#tmp == 9) {                   # OK line, but unknown user
482       $option{DEBUG} && do {
483         if (!$userslisted) {
484           print STDERR 'Know users are: ',
485             join(' ', keys(%is_account)), "\n";
486           $userslisted = 1;
487         }
488         print STDERR "Unknown user: $name\n";
489       }
490     } else {
491       &DebugErr("Strange line: $_\n");
492     }
493   }
494   close TMP;
495   my $i = 0;
496   for (sort keys %is_user) {
497     $option{DEBUG} && printf "Active user %3d: %s\n", ++$i, $_;
498   }
499   &Debug("$i active users found.\n");
500
501   return $i;
502 }
503
504 # }}}
505
506 # {{{ installcrontab
507
508 #
509 sub installcrontab {
510   my $hour = int(rand(24));
511   my $min  = int(rand(60));
512   my $day  = int(rand(7));      # Weekday. This version runs once a week.
513   my $cron = "";
514
515   warn "Installing start of script into your crontab\n";
516   if (open(CRON, "crontab -l |")) {
517     &Debug("Checking crontab for machine-update\n");
518     &Debug("Want to install as $progname\n");
519     while (<CRON>) {
520       if (/machine-update/) {
521         if (/ $progname -m/) {
522           die "Crontab entry already installed: $_\n";
523         } else {
524           die "Another entry with machine-update: $_\n";
525         }
526       }
527       $cron .= $_;
528     }
529     close CRON;
530     &Debug("Result from crontab -l: ", $? / 256, "\n");
531     if ($? == 0) {
532       &Debug("Crontab successfully read\n");
533     } elsif ($? == 256) {
534       warn "You don't seem to have a crontab. I will create one.\n";
535     } else {
536       die "Failed to read your crontab. Please report this as a bug: $?\n";
537     }
538   } else {
539     &Debug("Result from crontab open(): $?\n");
540     die "Unable to execute crontab command. Please check your system\n";
541   }
542   open(CRON, "|crontab -");
543   print CRON $cron;
544   print CRON "$min $hour * * $day $progname -m\n";
545   close CRON;
546   &Debug("Result from crontab: $?\n");
547   if ($?) {
548     die(<<EoF);
549 Installing new crontab failed.
550 YOUR CRONTAB MAY BE DAMAGED - use crontab -l to check it.
551 Here's its former content (if any):
552
553 $cron
554
555 EoF
556   }
557   print "Crontab entry successfully installed.\nWill run on day $day of every week, at $hour:$min\n";
558
559   exit 0;
560 }
561
562 # }}}
563 # {{{ uninstallcrontab
564
565 #
566 sub uninstallcrontab {
567   my $found = 0;
568   my $cron;
569
570   print STDERR "Removing $progname from your crontab\n";
571   open(CRON, "crontab -l |");
572   &Debug("Checking crontab for machine-update\n");
573   &Debug("Want to uninstall as $progname\n");
574   while (<CRON>) {
575     if (/^#/ && $. <= 3) {      # initial comment
576       &Debug("Skipping comment: $_");
577       next;
578     }
579     if (/machine-update/) {
580       if (/ $progname -m/) {
581         print STDERR "Crontab entry found and removed\n";
582         $found = 1;
583         next;                   # skip stuff at end....
584       } else {
585         die "Another entry with machine-update: $_\nUninstall manually?\n";
586       }
587     }
588     $cron .= $_;
589   }
590   close CRON;
591   &Debug("Result from crontab -l: $?\n");
592   if ($?) {
593     die "Failed to read your crontab. You may not have one?\n";
594   }
595   if ($found) {
596     open(CRON, "|crontab -");
597     print CRON $cron;
598     close CRON;
599     &Debug("Result from crontab: $?\n");
600     if ($?) {
601       die(<<EoF);
602 Installing new crontab failed.
603 YOUR CRONTAB MAY BE DAMAGED - use crontab -l to check it.
604 Here's its former content (if any):
605
606 $cron
607
608 EoF
609     }
610   } else {
611     print STDERR "No instance of $progname found in your crontab\n";
612   }
613
614   exit 0;
615 }
616
617 # }}}
618
619 # {{{ xbin                       execute a linux binary
620
621 #
622 # This sub is to execute a linux binary robustly. i.e. testing
623 # whether it is present, where it is present, whether it is executable
624 #
625 sub xbin {
626   my $bin = shift;             # get name of binary to execute
627
628   $bin = `which $bin 2>/dev/null`;         # determine binarys full path
629   chomp $bin;
630   return $bin if(-x $bin);     # if there and executable: all is well - return it
631
632   if(!$bin) {                  # if not there
633     &Debug("No $bin found\n"); # state so
634   } else {                     # there but not executable
635     &Debug("$bin found, but not executable\n");
636   }
637
638   return '';                   # so return an empty string (binary will not exec)
639 }
640
641 # }}}
642 # {{{ getval_from_file           get value from system file @ row,col
643
644 #
645 sub getval_from_file {
646   my $file = shift;
647   my $row  = shift;
648   my $col  = shift;
649   my @file;
650   my @cols;
651
652   if (!(-r $file)) {
653     &DebugErr("File $file not readable\n");
654     return '';
655   }
656   sysopen(FH,$file, O_RDONLY);
657   @file = <FH>;                     # read whole file to array
658   close FH;
659
660   @cols = split /\s+/, $file[$row]; # get the right row
661
662   return $cols[$col];               # return the right column
663 }
664
665 # }}}
666
667 sub addonefileforsending {
668   my $file = shift;
669   my @file;
670
671   if (!(-r $file)) {
672     &DebugErr("File $file not readable\n");
673     return '';
674   }
675   sysopen(FH,$file, O_RDONLY);
676   @file = <FH>;                     # read whole file to array
677   close FH;
678   $files{$file} = join('', @file);
679
680 }
681
682 # {{{ Debug                      print debug information if flag is set
683
684 #
685 sub Debug {
686   $option{DEBUG} && print @_;
687 }
688
689 # }}}
690 # {{{ DebugErr                   print debug on STDERR if flag is set
691
692 #
693 sub DebugErr {
694   $option{DEBUG} && print STDERR @_;
695 }
696
697 # }}}
698 # {{{ ErrorInfo
699 sub ErrorInfo {
700   $errordata .= join('', @_);
701 }
702 # }}}
703 # {{{ DebugInfo
704
705 sub DebugInfo {
706   $option{info} && ($debugdata .= join('', @_));
707 }
708
709 # }}}
710
711 # {{{ help                       print help & exit
712
713 #
714 sub help {
715   my $host = `uname -n`;
716
717   print <<EoF;
718 machine-update version $VERSION
719 Send machine information to the Linux Counter
720
721 USE: machine-update [-i] [-(t|d|l|m|v|x|c|u|h)]
722
723 SWITCHES:
724  -i = interactive
725  -t = test (do not send e-mail, just print it ot STDOUT - default)
726  -d = debug (test, and print additional debug informations)
727  -l = display license
728  -m = mail results to linux-counter
729  -v = print version and exit
730  -x = send extra info to server (Debug)
731  -c = install crontab entry
732  -u = uninstall crontab entry
733  -h = print usage information and exit
734 If called with the "-i" option, will ask some questions and store the
735 answers in $ENV{HOME}/.linuxcounter/$host
736
737 EoF
738
739 exit 0;
740 }
741
742 # }}}
743 # {{{ license                    print license & exit
744
745 #
746 sub license {
747   print <<EoF;
748
749     Linux Counter Machine Update version $VERSION
750
751     Copyright (C) 1999-2005 Harald Tveit Alvestrand
752                   2003 PetaMem Group (www.petamem.com)
753
754     This program is free software; you can redistribute it and/or modify
755     it under the terms of the GNU General Public License as published by
756     the Free Software Foundation; either version 2 of the License, or
757     (at your option) any later version.
758
759     This program is distributed in the hope that it will be useful,
760     but WITHOUT ANY WARRANTY; without even the implied warranty of
761     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
762     GNU General Public License below for more details.
763
764
765                     GNU GENERAL PUBLIC LICENSE
766                        Version 2, June 1991
767
768  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
769                           675 Mass Ave, Cambridge, MA 02139, USA
770  Everyone is permitted to copy and distribute verbatim copies
771  of this license document, but changing it is not allowed.
772
773                             Preamble
774
775   The licenses for most software are designed to take away your
776 freedom to share and change it.  By contrast, the GNU General Public
777 License is intended to guarantee your freedom to share and change free
778 software--to make sure the software is free for all its users.  This
779 General Public License applies to most of the Free Software
780 Foundation's software and to any other program whose authors commit to
781 using it.  (Some other Free Software Foundation software is covered by
782 the GNU Library General Public License instead.)  You can apply it to
783 your programs, too.
784
785   When we speak of free software, we are referring to freedom, not
786 price.  Our General Public Licenses are designed to make sure that you
787 have the freedom to distribute copies of free software (and charge for
788 this service if you wish), that you receive source code or can get it
789 if you want it, that you can change the software or use pieces of it
790 in new free programs; and that you know you can do these things.
791
792   To protect your rights, we need to make restrictions that forbid
793 anyone to deny you these rights or to ask you to surrender the rights.
794 These restrictions translate to certain responsibilities for you if you
795 distribute copies of the software, or if you modify it.
796
797   For example, if you distribute copies of such a program, whether
798 gratis or for a fee, you must give the recipients all the rights that
799 you have.  You must make sure that they, too, receive or can get the
800 source code.  And you must show them these terms so they know their
801 rights.
802
803   We protect your rights with two steps: (1) copyright the software, and
804 (2) offer you this license which gives you legal permission to copy,
805 distribute and/or modify the software.
806
807   Also, for each author's protection and ours, we want to make certain
808 that everyone understands that there is no warranty for this free
809 software.  If the software is modified by someone else and passed on, we
810 want its recipients to know that what they have is not the original, so
811 that any problems introduced by others will not reflect on the original
812 authors' reputations.
813
814   Finally, any free program is threatened constantly by software
815 patents.  We wish to avoid the danger that redistributors of a free
816 program will individually obtain patent licenses, in effect making the
817 program proprietary.  To prevent this, we have made it clear that any
818 patent must be licensed for everyone's free use or not licensed at all.
819
820   The precise terms and conditions for copying, distribution and
821 modification follow.
822 \f
823                     GNU GENERAL PUBLIC LICENSE
824    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
825
826   0. This License applies to any program or other work which contains
827 a notice placed by the copyright holder saying it may be distributed
828 under the terms of this General Public License.  The "Program", below,
829 refers to any such program or work, and a "work based on the Program"
830 means either the Program or any derivative work under copyright law:
831 that is to say, a work containing the Program or a portion of it,
832 either verbatim or with modifications and/or translated into another
833 language.  (Hereinafter, translation is included without limitation in
834 the term "modification".)  Each licensee is addressed as "you".
835
836 Activities other than copying, distribution and modification are not
837 covered by this License; they are outside its scope.  The act of
838 running the Program is not restricted, and the output from the Program
839 is covered only if its contents constitute a work based on the
840 Program (independent of having been made by running the Program).
841 Whether that is true depends on what the Program does.
842
843   1. You may copy and distribute verbatim copies of the Program's
844 source code as you receive it, in any medium, provided that you
845 conspicuously and appropriately publish on each copy an appropriate
846 copyright notice and disclaimer of warranty; keep intact all the
847 notices that refer to this License and to the absence of any warranty;
848 and give any other recipients of the Program a copy of this License
849 along with the Program.
850
851 You may charge a fee for the physical act of transferring a copy, and
852 you may at your option offer warranty protection in exchange for a fee.
853
854   2. You may modify your copy or copies of the Program or any portion
855 of it, thus forming a work based on the Program, and copy and
856 distribute such modifications or work under the terms of Section 1
857 above, provided that you also meet all of these conditions:
858
859     a) You must cause the modified files to carry prominent notices
860     stating that you changed the files and the date of any change.
861
862     b) You must cause any work that you distribute or publish, that in
863     whole or in part contains or is derived from the Program or any
864     part thereof, to be licensed as a whole at no charge to all third
865     parties under the terms of this License.
866
867     c) If the modified program normally reads commands interactively
868     when run, you must cause it, when started running for such
869     interactive use in the most ordinary way, to print or display an
870     announcement including an appropriate copyright notice and a
871     notice that there is no warranty (or else, saying that you provide
872     a warranty) and that users may redistribute the program under
873     these conditions, and telling the user how to view a copy of this
874     License.  (Exception: if the Program itself is interactive but
875     does not normally print such an announcement, your work based on
876     the Program is not required to print an announcement.)
877 \f
878 These requirements apply to the modified work as a whole.  If
879 identifiable sections of that work are not derived from the Program,
880 and can be reasonably considered independent and separate works in
881 themselves, then this License, and its terms, do not apply to those
882 sections when you distribute them as separate works.  But when you
883 distribute the same sections as part of a whole which is a work based
884 on the Program, the distribution of the whole must be on the terms of
885 this License, whose permissions for other licensees extend to the
886 entire whole, and thus to each and every part regardless of who wrote it.
887
888 Thus, it is not the intent of this section to claim rights or contest
889 your rights to work written entirely by you; rather, the intent is to
890 exercise the right to control the distribution of derivative or
891 collective works based on the Program.
892
893 In addition, mere aggregation of another work not based on the Program
894 with the Program (or with a work based on the Program) on a volume of
895 a storage or distribution medium does not bring the other work under
896 the scope of this License.
897
898   3. You may copy and distribute the Program (or a work based on it,
899 under Section 2) in object code or executable form under the terms of
900 Sections 1 and 2 above provided that you also do one of the following:
901
902     a) Accompany it with the complete corresponding machine-readable
903     source code, which must be distributed under the terms of Sections
904     1 and 2 above on a medium customarily used for software interchange; or,
905
906     b) Accompany it with a written offer, valid for at least three
907     years, to give any third party, for a charge no more than your
908     cost of physically performing source distribution, a complete
909     machine-readable copy of the corresponding source code, to be
910     distributed under the terms of Sections 1 and 2 above on a medium
911     customarily used for software interchange; or,
912
913     c) Accompany it with the information you received as to the offer
914     to distribute corresponding source code.  (This alternative is
915     allowed only for noncommercial distribution and only if you
916     received the program in object code or executable form with such
917     an offer, in accord with Subsection b above.)
918
919 The source code for a work means the preferred form of the work for
920 making modifications to it.  For an executable work, complete source
921 code means all the source code for all modules it contains, plus any
922 associated interface definition files, plus the scripts used to
923 control compilation and installation of the executable.  However, as a
924 special exception, the source code distributed need not include
925 anything that is normally distributed (in either source or binary
926 form) with the major components (compiler, kernel, and so on) of the
927 operating system on which the executable runs, unless that component
928 itself accompanies the executable.
929
930 If distribution of executable or object code is made by offering
931 access to copy from a designated place, then offering equivalent
932 access to copy the source code from the same place counts as
933 distribution of the source code, even though third parties are not
934 compelled to copy the source along with the object code.
935 \f
936   4. You may not copy, modify, sublicense, or distribute the Program
937 except as expressly provided under this License.  Any attempt
938 otherwise to copy, modify, sublicense or distribute the Program is
939 void, and will automatically terminate your rights under this License.
940 However, parties who have received copies, or rights, from you under
941 this License will not have their licenses terminated so long as such
942 parties remain in full compliance.
943
944   5. You are not required to accept this License, since you have not
945 signed it.  However, nothing else grants you permission to modify or
946 distribute the Program or its derivative works.  These actions are
947 prohibited by law if you do not accept this License.  Therefore, by
948 modifying or distributing the Program (or any work based on the
949 Program), you indicate your acceptance of this License to do so, and
950 all its terms and conditions for copying, distributing or modifying
951 the Program or works based on it.
952
953   6. Each time you redistribute the Program (or any work based on the
954 Program), the recipient automatically receives a license from the
955 original licensor to copy, distribute or modify the Program subject to
956 these terms and conditions.  You may not impose any further
957 restrictions on the recipients' exercise of the rights granted herein.
958 You are not responsible for enforcing compliance by third parties to
959 this License.
960
961   7. If, as a consequence of a court judgment or allegation of patent
962 infringement or for any other reason (not limited to patent issues),
963 conditions are imposed on you (whether by court order, agreement or
964 otherwise) that contradict the conditions of this License, they do not
965 excuse you from the conditions of this License.  If you cannot
966 distribute so as to satisfy simultaneously your obligations under this
967 License and any other pertinent obligations, then as a consequence you
968 may not distribute the Program at all.  For example, if a patent
969 license would not permit royalty-free redistribution of the Program by
970 all those who receive copies directly or indirectly through you, then
971 the only way you could satisfy both it and this License would be to
972 refrain entirely from distribution of the Program.
973
974 If any portion of this section is held invalid or unenforceable under
975 any particular circumstance, the balance of the section is intended to
976 apply and the section as a whole is intended to apply in other
977 circumstances.
978
979 It is not the purpose of this section to induce you to infringe any
980 patents or other property right claims or to contest validity of any
981 such claims; this section has the sole purpose of protecting the
982 integrity of the free software distribution system, which is
983 implemented by public license practices.  Many people have made
984 generous contributions to the wide range of software distributed
985 through that system in reliance on consistent application of that
986 system; it is up to the author/donor to decide if he or she is willing
987 to distribute software through any other system and a licensee cannot
988 impose that choice.
989
990 This section is intended to make thoroughly clear what is believed to
991 be a consequence of the rest of this License.
992 \f
993   8. If the distribution and/or use of the Program is restricted in
994 certain countries either by patents or by copyrighted interfaces, the
995 original copyright holder who places the Program under this License
996 may add an explicit geographical distribution limitation excluding
997 those countries, so that distribution is permitted only in or among
998 countries not thus excluded.  In such case, this License incorporates
999 the limitation as if written in the body of this License.
1000
1001   9. The Free Software Foundation may publish revised and/or new versions
1002 of the General Public License from time to time.  Such new versions will
1003 be similar in spirit to the present version, but may differ in detail to
1004 address new problems or concerns.
1005
1006 Each version is given a distinguishing version number.  If the Program
1007 specifies a version number of this License which applies to it and "any
1008 later version", you have the option of following the terms and conditions
1009 either of that version or of any later version published by the Free
1010 Software Foundation.  If the Program does not specify a version number of
1011 this License, you may choose any version ever published by the Free Software
1012 Foundation.
1013
1014   10. If you wish to incorporate parts of the Program into other free
1015 programs whose distribution conditions are different, write to the author
1016 to ask for permission.  For software which is copyrighted by the Free
1017 Software Foundation, write to the Free Software Foundation; we sometimes
1018 make exceptions for this.  Our decision will be guided by the two goals
1019 of preserving the free status of all derivatives of our free software and
1020 of promoting the sharing and reuse of software generally.
1021
1022                             NO WARRANTY
1023
1024   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
1025 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
1026 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
1027 PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
1028 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1029 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
1030 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
1031 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
1032 REPAIR OR CORRECTION.
1033
1034   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
1035 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
1036 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
1037 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
1038 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
1039 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
1040 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
1041 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
1042 POSSIBILITY OF SUCH DAMAGES.
1043
1044                      END OF TERMS AND CONDITIONS
1045
1046 EoF
1047
1048   exit 0;
1049 }
1050
1051 # }}}
1052
1053 # Changelog for 0.2
1054 #  - indentation and folding marks
1055 #  - made script work with -w and use strict
1056 #  - removed some localization traps
1057 #  - marked some BUGS - but they`re still there (mostly localization)
1058 #  - more robust binary calls
1059 #  - getval_from_file data acquisition method
1060 #  - fixed df (shmfs) - but only temporarily (quick hack)
1061 #  - various code optimizations & cleanup (removed unneded vars)
1062 #  - Memory size detection now robst and >960MB capable
1063 #  - slightly better randomness
1064 #
1065
1066 # Changelog 0.21
1067 # - added attaching of files
1068 # - added fetching of uptime_1 and uptime_2
1069 #
1070 # Changelog 0.22
1071 # - removed "manual" copying of entries
1072 # - added suppressing error messages from "xbin" calling "which"
1073 # - suppressed NFS from "df -T" listing
1074 #
1075 # Changelog 0.23
1076 # - added sending /proc/pci
1077 # - removed client-side parsing of DF output and uptime
1078 #
1079 # Changelog 0.24
1080 # - added sending /proc/version (inspired by klive)
1081 # - changed fetching of old data from "all" to "needed"
1082 # - removed CPU-parsing code
1083 # - fixed warning (harmless) from crontab creation
1084 # - added sending /proc/bus/usb/devices
1085 #
1086 # Changelog 0.25
1087 # - added sending size of /proc/kcore
1088 # - removed computation of memory client-side
1089 #
1090 # Changelog 0.26
1091 # - adding cron entry no longer removes comments from crontab.
1092 #   Crontab is not ours, so hands off.
1093 #
1094 # Changelog 0.27
1095 # - When /proc/kcore does not exist on a system, its size is now zero
1096 #
1097 # Changelog 0.28
1098 # - Added a terrible way to find out what email program can be used to send out the mail
1099 #
1100 # Changelog 0.29
1101 # - Extended the hardcoded list of programs that can be used to send the
1102 #   machine-update email
1103 #
1104 # Changelog 0.30  
1105 # - The script now sends us the version of perl used to run the script
1106 #   We need this to solve some problems due to changes in between perl
1107 #   versions
1108 #
1109 # Changelog 0.31  
1110 # - The script now sends the email to machine-registration@linuxcounter.net
1111 #   instead of the old counter machine machine-registration@counter.li.org
1112 #
1113 # Changelog 0.32
1114 # - If no mail program was found, the script will not stop anymore. It prints
1115 #   the output anyways and you can send this manually to machine-registration@counter.li.org
1116 #
1117 #vim:ts=8:sw=4:sts=4