[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 | |
---|
[2093] | 19 | sub zwrite($;$$\@) { |
---|
| 20 | my ($message, $class, $instance, $recipref) = @_; |
---|
| 21 | my @recipients = (); |
---|
| 22 | if (defined($recipref)) { |
---|
| 23 | if (@$recipref) { |
---|
| 24 | @recipients = @$recipref; |
---|
| 25 | } else { |
---|
| 26 | $message = '@b(Empty recipient list specified, message redacted)'; |
---|
| 27 | } |
---|
| 28 | } |
---|
[666] | 29 | $class ||= $ZCLASS; |
---|
[665] | 30 | $instance ||= 'root.'.hostname; |
---|
[784] | 31 | open(ZWRITE, "|-", qw|/usr/bin/zwrite -d -n -O log -c|, $class, '-i', $instance, '-s', hostname, @recipients) or die "Couldn't open zwrite"; |
---|
[645] | 32 | print ZWRITE $message; |
---|
| 33 | close(ZWRITE); |
---|
| 34 | } |
---|
| 35 | |
---|
[2093] | 36 | unless (@RECIPIENTS) { |
---|
| 37 | zwrite('@b(No .k5login found, sensitive logs will not be zephyred)', $ZCLASS); |
---|
| 38 | } |
---|
| 39 | |
---|
[666] | 40 | my %toclass; |
---|
[645] | 41 | |
---|
[770] | 42 | my %sshkeys; |
---|
| 43 | |
---|
| 44 | sub buildKeyMap($) { |
---|
| 45 | my ($file) = @_; |
---|
[2066] | 46 | open (KEYS, $file) or (warn "Couldn't open $file: $!\n" and return); |
---|
[770] | 47 | while (<KEYS>) { |
---|
| 48 | chomp; |
---|
| 49 | my ($fingerprint, $comment) = parseKey($_); |
---|
| 50 | $sshkeys{$fingerprint} = $comment; |
---|
| 51 | } |
---|
| 52 | close(KEYS); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | sub parseKey($) { |
---|
| 56 | my ($key) = @_; |
---|
| 57 | my $tmp = new File::Temp; |
---|
| 58 | print $tmp $key; |
---|
| 59 | close $tmp; |
---|
| 60 | open (KEYGEN, "-|", qw(/usr/bin/ssh-keygen -l -f), $tmp) or die "Couldn't call ssh-keygen: $!"; |
---|
| 61 | my ($line) = <KEYGEN>; |
---|
| 62 | close(KEYGEN); |
---|
| 63 | my (undef, $fingerprint, undef) = split(' ', $line, 3); |
---|
| 64 | my (undef, undef, $comment) = split(' ', $key, 3); |
---|
[784] | 65 | #print "$fingerprint $comment"; |
---|
[770] | 66 | return ($fingerprint, $comment); |
---|
| 67 | } |
---|
| 68 | |
---|
[938] | 69 | buildKeyMap("/root/.ssh/authorized_keys"); |
---|
[770] | 70 | buildKeyMap("/root/.ssh/authorized_keys2"); |
---|
| 71 | |
---|
[2066] | 72 | my @message; |
---|
| 73 | |
---|
| 74 | while (my $line = <>) { |
---|
| 75 | @message = $line; |
---|
[666] | 76 | eval { |
---|
| 77 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required |
---|
| 78 | ualarm(500*1000); |
---|
| 79 | while (<>) { push @message, $_; } |
---|
| 80 | }; |
---|
| 81 | chomp @message; |
---|
| 82 | map { s/^(.*?): // } @message; |
---|
| 83 | %toclass = (); |
---|
| 84 | foreach my $message (@message) { |
---|
| 85 | sub sendmsg ($;$) { |
---|
| 86 | my ($message, $class) = @_; |
---|
| 87 | $class ||= $ZCLASS; |
---|
| 88 | $toclass{$class} .= $message."\n"; |
---|
[645] | 89 | } |
---|
[666] | 90 | if ($message =~ m|Accepted (\S+) for (\S+)|) { |
---|
| 91 | sendmsg($message) if exists $USERS{$2} |
---|
| 92 | } elsif ($message =~ m|Authorized to (\S+),|) { |
---|
| 93 | sendmsg($message) if exists $USERS{$1}; |
---|
| 94 | } elsif ($message =~ m|Root (\S+) shell|) { |
---|
| 95 | sendmsg($message); |
---|
[2066] | 96 | } elsif ($message =~ m|pam_unix\(([^:]+):session\): session \S+ for user (\S+)|) { |
---|
| 97 | sendmsg($message) if $1 ne "cron" and exists $USERS{$2}; |
---|
[770] | 98 | } elsif ($message =~ m|^Found matching (\w+) key: (\S+)|) { |
---|
| 99 | if ($sshkeys{$2}) { |
---|
| 100 | sendmsg($message." (".$sshkeys{$2}.")"); |
---|
| 101 | } else { |
---|
| 102 | sendmsg($message." (UNKNOWN KEY)"); |
---|
| 103 | } |
---|
[817] | 104 | } elsif ($message =~ m|^Out of memory:|) { |
---|
| 105 | sendmsg($message); |
---|
[883] | 106 | } elsif ($message =~ m|^giving \S+ admin rights|) { |
---|
| 107 | sendmsg($message); |
---|
[666] | 108 | } elsif ($message =~ m|^Connection closed|) { |
---|
| 109 | # Do nothing |
---|
[770] | 110 | } elsif ($message =~ m|^Closing connection to |) { |
---|
| 111 | } elsif ($message =~ m|^Connection from (\S+) port (\S+)|) { |
---|
[666] | 112 | } elsif ($message =~ m|^Invalid user|) { |
---|
| 113 | } elsif ($message =~ m|^input_userauth_request: invalid user|) { |
---|
| 114 | } elsif ($message =~ m|^Received disconnect from|) { |
---|
[1331] | 115 | } elsif ($message =~ m|^Postponed keyboard-interactive|) { |
---|
[1584] | 116 | } elsif ($message =~ m|^Failed keyboard-interactive/pam|) { |
---|
[668] | 117 | } elsif ($message =~ m|^fatal: Read from socket failed: Connection reset by peer$|) { |
---|
[690] | 118 | } elsif ($message =~ m|^reverse mapping checking getaddrinfo|) { |
---|
| 119 | } elsif ($message =~ m|^pam_succeed_if\(sshd\:auth\)\:|) { |
---|
[749] | 120 | } elsif ($message =~ m|^error: PAM: Authentication failure|) { |
---|
| 121 | } elsif ($message =~ m|^pam_unix\(sshd:auth\): authentication failure|) { |
---|
[738] | 122 | } elsif ($message =~ m|^Postponed keyboard-interactive for invalid user |) { |
---|
| 123 | } elsif ($message =~ m|^Failed keyboard-interactive/pam for invalid user |) { |
---|
[770] | 124 | } elsif ($message =~ m|^Postponed gssapi-with-mic for |) { |
---|
[739] | 125 | } elsif ($message =~ m|^Address \S+ maps to \S+, but this does not map back to the address|) { |
---|
[1747] | 126 | } elsif ($message =~ m|^Nasty PTR record .* is set up for .*, ignoring|) { |
---|
[853] | 127 | } elsif ($message =~ m|^User child is on pid \d+$|) { |
---|
| 128 | } elsif ($message =~ m|^Transferred: sent \d+, received \d+ bytes$|) { |
---|
[939] | 129 | } elsif ($message =~ m|^Setting tty modes failed: Invalid argument$|) { |
---|
[1272] | 130 | } elsif ($message =~ m|^ *nrpe .* COMMAND=/etc/nagios/check_ldap_mmr.real$|) { |
---|
[1305] | 131 | } elsif ($message =~ m|^ *root : TTY=|) { |
---|
[1707] | 132 | } elsif ($message =~ m|^Set /proc/self/oom_adj to |) { |
---|
[2066] | 133 | } elsif ($message =~ m|^fatal: mm_request_receive: read: Connection reset by peer$|) { |
---|
[666] | 134 | } else { |
---|
[770] | 135 | sendmsg($message, "scripts-spew"); |
---|
[646] | 136 | } |
---|
[645] | 137 | } |
---|
[665] | 138 | |
---|
[666] | 139 | foreach my $class (keys %toclass) { |
---|
[2093] | 140 | if ($class eq $ZCLASS) { |
---|
[784] | 141 | zwrite($toclass{$class}, $class); |
---|
| 142 | } else { |
---|
| 143 | zwrite($toclass{$class}, $class, undef, @RECIPIENTS); |
---|
| 144 | } |
---|
[666] | 145 | } |
---|
[645] | 146 | } |
---|