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