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