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