home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
- #
- # badrhosts -- find any +'s in people's .rhosts file and
- # send them mail asking them to replace it.
- #
- # good example of using perl for sysadmin scripts
-
- ($program = $0) =~ s%/.*%%;
- chop($host = `hostname`);
-
- do read_shells();
-
- $passwd = '/etc/passwd';
- open passwd || die "$program: can't open $passwd: $!";
-
- while (<passwd>) {
- chop;
- ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
- next unless $shells{$shell};
- $rhosts = $home . '/.rhosts';
- open rhosts || next;
- $found = 0;
- while (<rhosts>) {
- $found |= $_ eq "+\n";
- last if $found;
- }
- close rhosts;
- next unless $found;
-
- ($name = $gcos) =~ s/[\s,].*//;
- print "$login@$host\n";
- open (mailer, "| Mail -s \"security hazard\" $login");
- print mailer <<EO_MESSAGE;
- Dear $name,
-
- On the machine $host, you have a + in your file "$rhosts".
- This is a security hazard. Please replace this line with
- an explicit list of trusted hosts.
-
- Thank you.
-
- [This mail was automatically generated by the $program program]
- EO_MESSAGE
- close mailer;
- $bad++;
- }
-
- close passwd;
- print "$program: found $bad problem on $host\n" if $bad;
- exit ($bad > 0);
-
- sub read_shells {
- $shells = "/etc/shells";
- open shells || die "$program: couldn't open $shells: $!\n";
- while (<shells>) {
- next if /^#/;
- chop;
- ++ $shells{$_};
- }
- close shells;
- }
-