home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / badrhosts next >
Encoding:
Text File  |  1990-01-14  |  1.4 KB  |  62 lines

  1. #!/usr/bin/perl
  2. #
  3. # badrhosts -- find any +'s in people's .rhosts file and
  4. #           send them mail asking them to replace it.
  5. #
  6. # good example of using perl for sysadmin scripts
  7.  
  8. ($program = $0) =~ s%/.*%%;
  9. chop($host = `hostname`);
  10.  
  11. do read_shells();
  12.  
  13. $passwd = '/etc/passwd';
  14. open passwd || die "$program: can't open $passwd: $!";
  15.  
  16. while (<passwd>) {
  17.     chop;
  18.     ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
  19.     next unless $shells{$shell};
  20.     $rhosts = $home . '/.rhosts';
  21.     open rhosts || next;
  22.     $found = 0;
  23.     while (<rhosts>) {
  24.     $found |= $_ eq "+\n";
  25.     last if $found;
  26.     } 
  27.     close rhosts;
  28.     next unless $found;
  29.     
  30.     ($name = $gcos) =~ s/[\s,].*//;
  31.     print "$login@$host\n";
  32.     open (mailer, "| Mail -s \"security hazard\" $login");
  33.     print mailer <<EO_MESSAGE;
  34. Dear $name,
  35.  
  36. On the machine $host, you have a + in your file "$rhosts".
  37. This is a security hazard.  Please replace this line with
  38. an explicit list of trusted hosts.
  39.  
  40. Thank you.
  41.  
  42. [This mail was automatically generated by the $program program]
  43. EO_MESSAGE
  44.     close mailer;
  45.     $bad++;
  46.  
  47. close passwd;
  48. print "$program: found $bad problem on $host\n" if $bad;
  49. exit ($bad > 0);
  50.  
  51. sub read_shells {
  52.     $shells = "/etc/shells";
  53.     open shells || die "$program: couldn't open $shells: $!\n";
  54.     while (<shells>) {
  55.     next if /^#/;
  56.     chop;
  57.     ++ $shells{$_};
  58.     } 
  59.     close shells;
  60.