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