This file is indexed.

/usr/share/perl5/Kanla/Plugin/Banner.pm is in kanla 1.5-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# -*- Mode: CPerl;
# cperl-indent-level: 4;
# cperl-continued-statement-offset: 4;
# cperl-indent-parens-as-block: t;
# cperl-tabs-always-indent: t;
# cperl-indent-subs-specially: nil;
# -*-
# vim:ts=4:sw=4:expandtab
package Kanla::Plugin::Banner;

use strict;
use warnings;
use utf8;
use v5.10;

use Kanla::Plugin;

# libanyevent-perl
use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;

use Socket qw(SOCK_STREAM);
use Exporter qw(import);
our @EXPORT = qw(
    banner_connect
    banner_disconnect
);

# see http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
our $VERSION = "1.5";
$VERSION = eval $VERSION;

# Filled in banner_connect().
my $timeout = 0;

=head1 NAME

Kanla::Plugin::Banner - Useful functions for banner-based plugins

=head1 SYNOPSIS

    use Kanla::Plugin;
    use Kanla::Plugin::Banner;

    sub run {
        banner_connect(
            host            => 'irc.twice-irc.de',
            default_service => 'ircd',
            cb              => sub {
                my ($handle, $timeout) = @_;
                $handle->push_write("NICK kanla\r\n");
                $handle->push_write("USER kanla kanla kanla :kanla\r\n");
                my @read_line;
                @read_line = (
                    line => sub {
                        my ($handle, $line) = @_;
                        if ($line !~ /^:[^ ]+ 001 /) {
                            $handle->push_read(@read_line);
                            return;
                        }

                        # We successfully signed on.
                        undef $timeout;
                        $handle->push_write("QUIT\r\n");
                        banner_disconnect($handle);
                    });

                $handle->push_read(@read_line);
            });
    }

=head1 METHODS

=cut

sub _banner_connect {
    my ($ip, $service, $cb) = @_;

    tcp_connect $ip, $service, sub {
        my ($fh) = @_;
        if (!$fh) {
            signal_error(
                'critical',
                "Connecting to $ip on port $service failed: $!"
            );
            return;
        }

        my $t;
        $t = AnyEvent->timer(
            after => $timeout,
            cb    => sub {
                signal_error(
                    'critical',

                    # XXX: It is unfortunate that this
                    # error message is so sparse.
                    # we should refactor the code to
                    # allow for better errors here.
                    "Timeout ($timeout s) on [$ip]:$service"
                );
            });

        my $handle;    # avoid direct assignment so on_eof has it in scope.
        $handle = AnyEvent::Handle->new(
            fh       => $fh,
            on_error => sub {
                signal_error(
                    'critical',
                    "TCP read error on [$ip]:$service: " . $_[2]);
                undef $t;
                $_[0]->destroy;
            },
            on_eof => sub {
                $handle->destroy;    # destroy handle
                signal_error(
                    'critical',
                    "TCP EOF on [$ip]:$service"
                );
                undef $t;
            });

        $cb->($handle, $t, $ip, $service);
    };
}

=head2 banner_connect

Connects to the given address
(parsed by C<AnyEvent::Socket>'s parse_hostport)
using the configured address families.

The caller provides a callback,
which will be called
after the connection was established.
In case there was an error
(DNS name could not be resolved,
connection was refused/timed out,
etc.),
the callback will B<NOT> be called,
but an alert will be signaled.

The callback will be called
with an C<AnyEvent::Handle> and
an C<AnyEvent> timer (timeouts).

The timeout is initialized
to the configured value
(plugin configuration)
or 20s if left unconfigured.

This example connects to one of
Google's SMTP servers
and waits for the SMTP greeting.
It does not resolve MX records,
but that's not the point of the example:

    banner_connect(
        host => 'aspmx.l.google.com',
        default_service => 'smtp',
        cb => sub {
            my ($handle, $timeout) = @_;
            $handle->push_read(line => sub {
                my ($handle, $line) = @_;
                undef $timeout;
                if ($line !~ /^220 /) {
                    signal_error('critical', 'Invalid greeting');
                }
            });
        });

=cut
sub banner_connect {
    my %args = @_;

    $timeout = ($conf->exists('timeout') ? $conf->value('timeout') : 20);

    # Ensure timeout is an int and > 0.
    $timeout += 0;
    $timeout ||= 1;

    my ($host, $service) =
        parse_hostport($args{'host'}, $args{'default_service'});

    my $resolved_cb = sub {

        # family is either A or AAAA
        my $family = shift;
        if (@_ == 0) {
            signal_error(
                'critical',
                "Cannot resolve $args{'host'} ($family) DNS record"
            );
            return;
        }
        for my $record (@_) {
            my ($service, $host) =
                AnyEvent::Socket::unpack_sockaddr($record->[3]);
            _banner_connect(format_address($host), $service, $args{'cb'});
        }
    };

    if ($conf->obj('family')->value('ipv4')) {
        AnyEvent::Socket::resolve_sockaddr(
            $host, $service, "tcp", 4, SOCK_STREAM,
            sub { $resolved_cb->('A', @_) });
    }
    if ($conf->obj('family')->value('ipv6')) {
        AnyEvent::Socket::resolve_sockaddr(
            $host, $service, "tcp", 6, SOCK_STREAM,
            sub { $resolved_cb->('AAAA', @_) });
    }
}

=head2 banner_disconnect($handle)

Properly disconnects
the specified C<AnyEvent::Handle>.

=cut
sub banner_disconnect {
    my ($handle) = @_;

    $handle->on_drain(
        sub {
            shutdown $handle->{fh}, 1;
            $handle->destroy;
            undef $handle;
        });
}

1