This file is indexed.

/usr/share/perl5/Paranoid/Network.pm is in libparanoid-perl 0.31-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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# Paranoid::Network -- Network functions for paranoid programs
#
# (c) 2005, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: Network.pm,v 0.66 2011/12/08 07:53:07 acorliss Exp $
#
#    This software is licensed under the same terms as Perl, itself.
#    Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################

#####################################################################
#
# Environment definitions
#
#####################################################################

package Paranoid::Network;

use 5.006;

use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Paranoid::Debug qw(:all);
use Paranoid::Module;
use Socket;
use Carp;

($VERSION) = ( q$Revision: 0.66 $ =~ /(\d+(?:\.(\d+))+)/sm );

@EXPORT      = qw(ipInNetwork hostInDomain extractIPs);
@EXPORT_OK   = qw(ipInNetwork hostInDomain extractIPs);
%EXPORT_TAGS = ( all => [qw(ipInNetwork hostInDomain extractIPs)], );

use constant CHUNK       => 32;
use constant IPV6CHUNKS  => 4;
use constant MAXIPV4CIDR => 32;
use constant MAXIPV6CIDR => 128;
use constant MASK        => 0xffffffff;
use constant IP4REGEX    => qr/(?:\d{1,3}\.){3}\d{1,3}/sm;
use constant IP6REGEX    => qr/
                            :(?::[abcdef\d]{1,4}){1,7}                 | 
                            [abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} | 
                            (?:[abcdef\d]{1,4}:){1,7}:
                            /smix;

#####################################################################
#
# Module code follows
#
#####################################################################

sub ipInNetwork ($@) {

    # Purpose:  Checks to see if the IP occurs in the passed list of IPs and
    #           networks
    # Returns:  True (1) if the IP occurs, False (0) otherwise
    # Usage:    $rv = ipInNetwork($ip, @networks);

    my $ip       = shift;
    my @networks = @_;
    my $rv       = 0;
    my $oip      = $ip;
    my ( $bip, $bnet, $bmask, $family, @tmp, $irv );
    my ( $inet_pton, $af_inet6 );

    # Validate arguments
    if ( defined $ip ) {

        # Test for an IPv4 address
        if ( $ip =~ m/^@{[ IP4REGEX ]}$/smo and defined inet_aton($ip) ) {
            $family = AF_INET();
        } else {

            # If Socket6 is present or we have Perl 5.14 or higher we'll check
            # for IPv6 addresses
            if ( $] >= 5.014 or loadModule('Socket6') ) {

                # Choose Socket or Socket6 functions
                if ( $] >= 5.014 ) {
                    $inet_pton = \&Socket::inet_pton;
                    $af_inet6  = \&Socket::AF_INET6;
                } else {
                    $inet_pton = \&Socket6::inet_pton;
                    $af_inet6  = \&Socket6::AF_INET6;
                }

                if ( defined &$inet_pton( &$af_inet6(), $ip ) ) {

                    # Convert IPv6-encoded IPv4 addresses to pure IPv4
                    if ( $ip =~ m/^::ffff:(@{[ IP4REGEX ]})$/smio ) {
                        $ip     = $1;
                        $family = AF_INET();
                    } else {
                        $family = &$af_inet6();
                    }
                } else {
                    croak 'Mandatory first argument must be a '
                        . 'defined IPv4/IPv6 address';
                }
            } else {
                croak 'Mandatory first argument must be a valid IPv4 address';
            }
        }
    } else {
        croak 'Mandatory first argument must be a defined IP address';
    }

    pdebug( "entering w/($oip)(@networks)", PDLEVEL1 );
    pIn();

    # Filter out non-IP data from @networks
    @networks = grep {
        if ( defined $_
            && m#^([\d\.]+|[abcdef\d:]+)(?:/(?:\d+|@{[ IP4REGEX ]}))?$#smio )
        {
            defined(
                $family == AF_INET()
                ? inet_aton($1)
                : &$inet_pton( &$af_inet6(), $1 ) );
        }
    } @networks;

    # Start the comparisons
    if (@networks) {

        pdebug( "networks to compare: @{[ join ', ', @networks ]}",
            PDLEVEL2 );

        # Convert IP to binary
        $bip =
            $family == AF_INET()
            ? [ unpack 'N', inet_aton($ip) ]
            : [ unpack 'NNNN', &$inet_pton( &$af_inet6(), $ip ) ];

        # Compare against all networks
        foreach (@networks) {

            if ( $_ =~ m#^([^/]+)(?:/(.+))?$#sm ) {
                ( $bnet, $bmask ) = ( $1, $2 );
            }

            # See if it's a network address
            if ( defined $bmask and length $bmask ) {

                # Get the netmask
                if ( $family == AF_INET() ) {

                    # Convert IPv4/CIDR notation to a binary number
                    $bmask =
                          ( $bmask =~ m/^\d+$/sm and $bmask <= MAXIPV4CIDR )
                        ? [ MASK - ( ( 2**( CHUNK - $bmask ) ) - 1 ) ]
                        : ( $bmask =~ m/^@{[ IP4REGEX ]}$/smo
                            and defined inet_aton($ip) )
                        ? [ unpack 'N', inet_aton($bmask) ]
                        : undef;

                } else {

                    # Convert IPv6 CIDR notation to a binary number
                    if ( $bmask =~ m/^\d+$/sm and $bmask <= MAXIPV6CIDR ) {

                        # Add the mask in 32-bit chunks
                        @tmp = ();
                        while ( $bmask >= CHUNK ) {
                            push @tmp, MASK;
                            $bmask -= CHUNK;
                        }

                        # Push the final segment if there's a remainder
                        if ($bmask) {
                            push @tmp,
                                MASK - ( ( 2**( CHUNK - $bmask ) ) - 1 );
                        }

                        # Add zero'd chunks to fill it out
                        while ( @tmp < IPV6CHUNKS ) {
                            push @tmp, 0x0;
                        }

                        # Finally, save the chunks
                        $bmask = [@tmp];

                    } else {
                        $bmask = undef;
                    }
                }

                # Skip if the netmask was invalid
                next unless defined $bmask;

                # Convert network address to binary
                $bnet =
                    $family == AF_INET()
                    ? [ unpack 'N', inet_aton($bnet) ]
                    : [ unpack 'NNNN', &$inet_pton( &$af_inet6(), $bnet ) ];

                # Start comparing our chunks
                $irv = 1;
                @tmp = @$bip;
                while (@tmp) {
                    unless ( ( $tmp[0] & $$bmask[0] ) ==
                        ( $$bnet[0] & $$bmask[0] ) ) {
                        $irv = 0;
                        last;
                    }
                    shift @tmp;
                    shift @$bnet;
                    shift @$bmask;
                }
                if ($irv) {
                    pdebug( "matched against $_", PDLEVEL2 );
                    $rv = 1;
                    last;
                }

            } else {

                # Not a network address, so let's see if it's an exact match
                $bnet =
                    $family == AF_INET()
                    ? [ unpack 'N', inet_aton($_) ]
                    : [ unpack 'NNNN', &$inet_pton( &$af_inet6(), $_ ) ];

                # Do the comparison
                $irv = 1;
                @tmp = @$bip;
                while (@tmp) {
                    unless ( $tmp[0] == $$bnet[0] ) {
                        $irv = 0;
                        last;
                    }
                    shift @tmp;
                    shift @$bnet;
                }
                if ($irv) {
                    pdebug( "matched against $_", PDLEVEL2 );
                    $rv = 1;
                    last;
                }
            }
        }
    }

    pOut();
    pdebug( "leaving w/rv: $rv", PDLEVEL1 );

    return $rv;
}

sub hostInDomain ($@) {

    # Purpose:  Checks to see if the host occurs in the list of domains
    # Returns:  True (1) if the host occurs, False (0) otherwise
    # Usage:    $rv = hostInDomain($hostname, @domains);

    my $host    = shift;
    my @domains = @_;
    my $rv      = 0;
    my $domain;

    # Validate arguments
    croak 'Mandatory first argument must be a defined and valid hostname'
        unless defined $host && $host =~ /^(?:[\w\-]+\.)*[\w\-]+$/sm;

    pdebug( "entering w/($host)(@domains)", PDLEVEL1 );
    pIn();

    # Filter out non-domains
    @domains = grep { defined $_ && m/^(?:[\w\-]+\.)*[\w\-]+$/sm } @domains;

    # Start the comparison
    if (@domains) {
        foreach $domain (@domains) {
            if ( $host =~ /^(?:[\w\-]+\.)*\Q$domain\E$/smi ) {
                $rv = 1;
                last;
            }
        }
    }

    pOut();
    pdebug( "leaving w/rv: $rv", PDLEVEL1 );

    return $rv;
}

sub extractIPs (@) {

    # Purpose:  Extracts IPv4/IPv6 addresses from arbitrary text.
    # Returns:  List containing extracted IP addresses
    # Usage:    @ips = extractIP($string1, $string2);

    my @strings = grep {defined} @_;
    my ( $string, @ips, $ip, @tmp, @rv );
    my ($inet_pton, $af_inet6);

    pdebug( "entering w/(@strings)", PDLEVEL1 );
    pIn();

    foreach $string (@strings) {

        # Look for IPv4 addresses
        @ips = ( $string =~ /(@{[ IP4REGEX ]})/smog );

        # Validate them by filtering through inet_aton
        foreach $ip (@ips) {
            push @rv, $ip if defined inet_aton($ip);
        }

        # If Socket6 is present or we have Perl 5.14 or higher we'll check
        # for IPv6 addresses
        if ( $] >= 5.014 or loadModule('Socket6') ) {

                # Choose Socket or Socket6 functions
                if ( $] >= 5.014 ) {
                    $inet_pton = \&Socket::inet_pton;
                    $af_inet6  = \&Socket::AF_INET6;
                } else {
                    $inet_pton = \&Socket6::inet_pton;
                    $af_inet6  = \&Socket6::AF_INET6;
                }

            @ips = ( $string =~ m/(@{[ IP6REGEX ]})/smogix );

            # Filter out addresses with more than one ::
            @ips = grep { scalar(m/(::)/smg) <= 1 } @ips;

            # Validate remaining addresses with inet_pton
            foreach $ip (@ips) {
                push @rv, $ip
                    if
                    defined &$inet_pton( &$af_inet6(), $ip );
            }
        }
    }

    # Filter out IPv4 encoded as IPv6
    @rv = grep !/^::ffff:@{[ IP4REGEX ]}$/smo, @rv;

    pOut();
    pdebug( "leaving w/rv: @rv)", PDLEVEL1 );

    return @rv;
}

1;

__END__

=head1 NAME

Paranoid::Network - Network functions for paranoid programs

=head1 VERSION

$Id: Network.pm,v 0.66 2011/12/08 07:53:07 acorliss Exp $

=head1 SYNOPSIS

  use Paranoid::Network;

  $rv = ipInNetwork($ip, @networks);
  $rv = hostInDomain($host, @domains);

=head1 DESCRIPTION

This modules contains functions that may be useful for network operations.
IPv6 is supported out of the box starting with Perl 5.14.  Earlier versions of
Perl will require L<Socket6(3)> installed as well.  If it is available this
module will use it automatically.

=head1 SUBROUTINES/METHODS

=head2 ipInNetwork

  $rv = ipInNetwork($ip, @networks);

This function checks the passed IP against each of the networks 
or IPs in the list and returns true if there's a match.  The list of networks
can be either individual IP address or network addresses in CIDR notation or
with full netmasks:

  @networks = qw(127.0.0.1 
                 192.168.0.0/24 
                 172.16.12.0/255.255.240.0);

IPv6 is supported if the L<Socket6(3)> module is installed or you're running
Perl 5.14 or higher.  This routine will select the appropriate address family 
based on the IP you're testing and filter out the opposing address family in 
the list.

B<NOTE:>  IPv4 addresses encoded as IPv6 addresses, e.g.:

  ::ffff:192.168.0.5

are supported, however an IP address submitted in this format as the IP to
test for will be converted to a pure IPv4 address and compared only against
the IPv4 networks.  This is meant as a convenience to the developer supporting
dual-stack systems to avoid having to list IPv4 networks in the array twice
like so:

  ::ffff:192.168.0.0/120, 192.168.0.0/24

Just list IPv4 as IPv4, IPv6 as IPv6, and this routine will convert
IPv6-encoded IPv4 addresses automatically.  This would make the following test
return a true value:

  ipInNetwork( '::ffff:192.168.0.5', '192.168.0.0/24' );

but

  ipInNetwork( '::ffff:192.168.0.5', '::ffff:192.168.0.0/120' );

return a false value.  This may seem counter intuitive, but it simplifies
things in (my alternate) reality.

Please note that this automatic conversion only applies to the B<IP> argument,
not to any member of the network array.

=head2 hostInDomain

  $rv = hostInDomain($host, @domains);

This function checks the passed hostname (fully qualified) against each 
of the domains in the list and returns true if there's a match.  None of the
domains should have the preceding '.' (i.e., 'foo.com' rather than 
'.foo.com').

=head2 extractIPs

    @ips = extractIP($string1, $string2);

This function extracts IP addresses from arbitrary text.  If you have
L<Socket6(3)> installed or running Perl 5.14 or higher it will extract 
IPv6 addresses as well as IPv4 addresses.  This extracts only IP 
addresses, not network addresses in CIDR or dotted octet
notation.  In the case of the latter the netmask will be extracted as an
additional address.

B<NOTE:> in the interest of performance this function does only rough regex
extraction of IP-looking candidates, then runs them through B<inet_aton> (for
IPv4) and B<inet_pton> (for IPv6) to see if they successfully convert.  Even
with the overhead of B<Paranoid> (with debugging and I<loadModule> calls for
Socket6 and what-not) it seems that this is an order of a magnitude faster
than doing a pure regex extraction & validation of IPv6 addresses.

B<NOTE:> Like the B<ipInNetwork> function we filter out IPv4 addresses encoded
as IPv6 addresses since that address is already returned as a pure IPv4
address.

=head1 DEPENDENCIES

=over

=item o

L<Paranoid>

=item o

L<Socket>

=item o

L<Socket6> (optional)

=back

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Arthur Corliss (corliss@digitalmages.com)

=head1 LICENSE AND COPYRIGHT

This software is licensed under the same terms as Perl, itself. 
Please see http://dev.perl.org/licenses/ for more information.

(c) 2005, Arthur Corliss (corliss@digitalmages.com)