| 1 | #!/usr/bin/perl | 
|---|
| 2 |  | 
|---|
| 3 | use strict; | 
|---|
| 4 | use warnings; | 
|---|
| 5 | use Sys::Hostname; | 
|---|
| 6 | use Time::HiRes qw(ualarm); | 
|---|
| 7 | use File::Temp; | 
|---|
| 8 |  | 
|---|
| 9 | our $ZCLASS = "scripts-auto"; | 
|---|
| 10 | our @USERS = qw/root logview/; | 
|---|
| 11 | my $k5login; | 
|---|
| 12 | open $k5login, '/root/.k5login'; | 
|---|
| 13 | our @RECIPIENTS = map {chomp; m|([^/@]*)| && $1} <$k5login>; | 
|---|
| 14 | close $k5login; | 
|---|
| 15 |  | 
|---|
| 16 | our %USERS; | 
|---|
| 17 | @USERS{@USERS} = undef; | 
|---|
| 18 |  | 
|---|
| 19 | sub zwrite($;$$@) { | 
|---|
| 20 | my ($message, $class, $instance, @recipients) = @_; | 
|---|
| 21 | $class ||= $ZCLASS; | 
|---|
| 22 | $instance ||= 'root.'.hostname; | 
|---|
| 23 | open(ZWRITE, "|-", qw|/usr/bin/zwrite -d -n -O log -c|, $class, '-i', $instance, '-s', hostname, @recipients) or die "Couldn't open zwrite"; | 
|---|
| 24 | print ZWRITE $message; | 
|---|
| 25 | close(ZWRITE); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | my %toclass; | 
|---|
| 29 |  | 
|---|
| 30 | my %sshkeys; | 
|---|
| 31 |  | 
|---|
| 32 | my %ips; | 
|---|
| 33 |  | 
|---|
| 34 | sub buildKeyMap($) { | 
|---|
| 35 | my ($file) = @_; | 
|---|
| 36 | open (KEYS, $file) or warn "Couldn't open $file: $!"; | 
|---|
| 37 | while (<KEYS>) { | 
|---|
| 38 | chomp; | 
|---|
| 39 | my ($fingerprint, $comment) = parseKey($_); | 
|---|
| 40 | $sshkeys{$fingerprint} = $comment; | 
|---|
| 41 | } | 
|---|
| 42 | close(KEYS); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | sub parseKey($) { | 
|---|
| 46 | my ($key) = @_; | 
|---|
| 47 | my $tmp = new File::Temp; | 
|---|
| 48 | print $tmp $key; | 
|---|
| 49 | close $tmp; | 
|---|
| 50 | open (KEYGEN, "-|", qw(/usr/bin/ssh-keygen -l -f), $tmp) or die "Couldn't call ssh-keygen: $!"; | 
|---|
| 51 | my ($line) = <KEYGEN>; | 
|---|
| 52 | close(KEYGEN); | 
|---|
| 53 | my (undef, $fingerprint, undef) = split(' ', $line, 3); | 
|---|
| 54 | my (undef, undef, $comment) = split(' ', $key, 3); | 
|---|
| 55 | #print "$fingerprint $comment"; | 
|---|
| 56 | return ($fingerprint, $comment); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | buildKeyMap("/root/.ssh/authorized_keys"); | 
|---|
| 60 | buildKeyMap("/root/.ssh/authorized_keys2"); | 
|---|
| 61 |  | 
|---|
| 62 | while (1) { | 
|---|
| 63 | my @message = scalar(<>); | 
|---|
| 64 | eval { | 
|---|
| 65 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required | 
|---|
| 66 | ualarm(500*1000); | 
|---|
| 67 | while (<>) { push @message, $_; } | 
|---|
| 68 | }; | 
|---|
| 69 | chomp @message; | 
|---|
| 70 | map { s/^(.*?): // } @message; | 
|---|
| 71 | %toclass = (); | 
|---|
| 72 | foreach my $message (@message) { | 
|---|
| 73 | sub sendmsg ($;$) { | 
|---|
| 74 | my ($message, $class) = @_; | 
|---|
| 75 | $class ||= $ZCLASS; | 
|---|
| 76 | $toclass{$class} .= $message."\n"; | 
|---|
| 77 | } | 
|---|
| 78 | if ($message =~ m|Accepted (\S+) for (\S+)|) { | 
|---|
| 79 | sendmsg($message) if exists $USERS{$2} | 
|---|
| 80 | } elsif ($message =~ m|Authorized to (\S+),|) { | 
|---|
| 81 | sendmsg($message) if exists $USERS{$1}; | 
|---|
| 82 | } elsif ($message =~ m|Root (\S+) shell|) { | 
|---|
| 83 | sendmsg($message); | 
|---|
| 84 | } elsif ($message =~ m|session \S+ for user (\S+)|) { | 
|---|
| 85 | sendmsg($message) if exists $USERS{$1}; | 
|---|
| 86 | } elsif ($message =~ m|^Found matching (\w+) key: (\S+)|) { | 
|---|
| 87 | if ($sshkeys{$2}) { | 
|---|
| 88 | sendmsg($message." (".$sshkeys{$2}.")"); | 
|---|
| 89 | } else { | 
|---|
| 90 | sendmsg($message." (UNKNOWN KEY)"); | 
|---|
| 91 | } | 
|---|
| 92 | } elsif ($message =~ m|^Failed keyboard-interactive/pam for root from ([^ ]*)|) { | 
|---|
| 93 | my $count = ++$ips{$1}; | 
|---|
| 94 | if ($count % 10 == 0 or $1 =~ /^18\./) { | 
|---|
| 95 | sendmsg($message." (repeated $count times)", "scripts-spew"); | 
|---|
| 96 | } | 
|---|
| 97 | } elsif ($message =~ m|^Out of memory:|) { | 
|---|
| 98 | sendmsg($message); | 
|---|
| 99 | } elsif ($message =~ m|^giving \S+ admin rights|) { | 
|---|
| 100 | sendmsg($message); | 
|---|
| 101 | } elsif ($message =~ m|^Connection closed|) { | 
|---|
| 102 | # Do nothing | 
|---|
| 103 | } elsif ($message =~ m|^Closing connection to |) { | 
|---|
| 104 | } elsif ($message =~ m|^Connection from (\S+) port (\S+)|) { | 
|---|
| 105 | } elsif ($message =~ m|^Invalid user|) { | 
|---|
| 106 | } elsif ($message =~ m|^input_userauth_request: invalid user|) { | 
|---|
| 107 | } elsif ($message =~ m|^Received disconnect from|) { | 
|---|
| 108 | } elsif ($message =~ m|^Postponed keyboard-interactive|) { | 
|---|
| 109 | } elsif ($message =~ m|^fatal: Read from socket failed: Connection reset by peer$|) { | 
|---|
| 110 | } elsif ($message =~ m|^reverse mapping checking getaddrinfo|) { | 
|---|
| 111 | } elsif ($message =~ m|^pam_succeed_if\(sshd\:auth\)\:|) { | 
|---|
| 112 | } elsif ($message =~ m|^error: PAM: Authentication failure|) { | 
|---|
| 113 | } elsif ($message =~ m|^pam_unix\(sshd:auth\): authentication failure|) { | 
|---|
| 114 | } elsif ($message =~ m|^Postponed keyboard-interactive for invalid user |) { | 
|---|
| 115 | } elsif ($message =~ m|^Failed keyboard-interactive/pam for invalid user |) { | 
|---|
| 116 | } elsif ($message =~ m|^Postponed gssapi-with-mic for |) { | 
|---|
| 117 | } elsif ($message =~ m|^Address \S+ maps to \S+, but this does not map back to the address|) { | 
|---|
| 118 | } elsif ($message =~ m|^User child is on pid \d+$|) { | 
|---|
| 119 | } elsif ($message =~ m|^Transferred: sent \d+, received \d+ bytes$|) { | 
|---|
| 120 | } elsif ($message =~ m|^Setting tty modes failed: Invalid argument$|) { | 
|---|
| 121 | } else { | 
|---|
| 122 | sendmsg($message, "scripts-spew"); | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | foreach my $class (keys %toclass) { | 
|---|
| 127 | if ($class eq "scripts-auto") { | 
|---|
| 128 | zwrite($toclass{$class}, $class); | 
|---|
| 129 | } else { | 
|---|
| 130 | zwrite($toclass{$class}, $class, undef, @RECIPIENTS); | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|