This file is indexed.

/usr/share/perl5/EBox/NetWrappers.pm is in zentyal-common 2.3.3.

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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

package EBox::NetWrappers;

use strict;
use warnings;

use EBox::Config;
use EBox::Gettext;
use EBox::Exceptions::DataNotFound;
use EBox::Exceptions::MissingArgument;
use Perl6::Junction qw(any);
use EBox::Validate;
use EBox::Sysfs;
use IO::Socket::INET;
use IO::Interface::Simple;

BEGIN {
    use Exporter ();
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);

    @ISA = qw(Exporter);
    @EXPORT = qw();
    %EXPORT_TAGS  = (all => [qw{    list_ifaces iface_exists iface_is_up
                    iface_netmask iface_addresses iface_addresses_with_netmask iface_by_address
                    iface_mac_address list_routes
                    list_local_addresses list_local_addresses_with_netmask
                    route_is_up ip_network ip_broadcast ip_mac
                    bits_from_mask mask_from_bits to_network_with_mask to_network_without_mask
                } ],
            );
    @EXPORT_OK = qw();
    Exporter::export_ok_tags('all');
    $VERSION = EBox::Config::version;
}

my @ifaceList;

# Function: iface_exists
#
#    Check if a given interface exists in the system
#
# Parameters:
#
#       iface - Interface's name
#
# Returns:
#
#       True if exists, otherwise undef
#
sub iface_exists
{
    my ($iface) = @_;
    my @ifaces = list_ifaces();
    return ($iface eq any(@ifaces));
}

# Function: list_ifaces
#
#       Return a list of all real interfaces in the machine
#
# Returns:
#
#       An array containg the interfaces
#
sub list_ifaces
{
    unless (@ifaceList) {
        @ifaceList = map { $_->{name} } IO::Interface::Simple->interfaces;
        @ifaceList = grep (!/:/, @ifaceList);
        @ifaceList = sort @ifaceList;
    }
    return @ifaceList;
}

# Function: iface_is_up
#
#   Checks if a given interface is up.
#
# Parameters:
#
#       iface - Interface's name
#
# Returns:
#
#       True if it's up, undef otherwise
#
# Exceptions:
#
#       DataNotFound - If interface does not exists
#
sub iface_is_up
{
    my $iface = shift;
    unless (iface_exists($iface)) {
        throw EBox::Exceptions::DataNotFound(
                data => __('Interface'),
                value => $iface);
    }
    my $state = EBox::Sysfs::read_value("/sys/class/net/$iface/operstate");
    return ($state eq 'up' or
            $state eq 'unknown'); # backward compatibility
}

# Function: iface_mac_address
#
#   Returns the mac address for a given interface
#
# Parameters:
#
#       iface - Interface's name
#
# Returns:
#
#       A string containing the mac address
#
# Exceptions:
#
#       DataNotFound - If interface does not exists
#
sub iface_mac_address
{
    my ($if) = @_;

    unless (iface_exists($if)) {
        throw EBox::Exceptions::DataNotFound(
                data => __('Interface'),
                value => $if);
    }
    my $mac = EBox::Sysfs::read_value("/sys/class/net/$if/address");
    return $mac;
}

# Function: iface_addresses
#
#   Return the addresses for a given interface (dot format)
#
# Parameters:
#
#       iface - Interface's name
#
# Returns:
#
#       A list of strings containing the addresses
#
# Exceptions:
#
#       DataNotFound - If interface does not exists
#
sub iface_addresses
{
    my ($if) = @_;

    my @addrs = map {  $_ =~ s{/.*$}{}; $_  }  _ifaceShowAddress($if);
    return @addrs;
}

#  Function: iface_by_address
#
#  Search a iface by his address
#
#  Assumption/Limitation: It assumes that we have not repeated addresses
#
#  Returns:
#
#     The iface or undef if there are not any iface with this address
sub iface_by_address
{
    my ($addr) = @_;

    foreach my $if (list_ifaces()) {
        my @addresses = iface_addresses($if);
        if ( $addr eq any(@addresses)  ) {
            return $if;
        }
    }

    return undef;
}

# Function: iface_addresses_with_netmask
#
#   Returns the  addresses for a given interface (dot format)
#
# Parameters:
#
#       iface - Interface's name
#
# Returns:
#
#       A hash reference wich keys are the ip addresses and the values the address' netmask
#
# Exceptions:
#
#       DataNotFound - If interface does not exists
#
sub iface_addresses_with_netmask
{
    my ($if) = @_;
    my %netmaskByAddr;

    my @addrs = _ifaceShowAddress($if);
    foreach my $addr (@addrs) {
        $addr =~ /^(.*)\/(.*)$/  ;
        my $ip = $1;
        my $netmask = mask_from_bits($2);
        $netmaskByAddr{$ip} = $netmask;
    }

    return \%netmaskByAddr;
}


sub _ifaceShowAddress
{
    my ($if) = @_;

    unless (iface_exists($if)) {
        throw EBox::Exceptions::DataNotFound(
                data => __('Interface'),
                value => $if);
    }

    my @output = `/bin/ip -f inet -o address show $if 2> /dev/null`;

    my @addrs = map {
        my ($number, $iface, $family,  $ip) =  split /\s+/, $_, 5;
        $ip;
    }  @output;

    return @addrs;
}

# Function: list_routes
#
#   Rertuns the list of current routes
#
#  Parameters:
#     viaGateway - returns  routes that uses a gateway (default: true)
#     localSource - returns routes that uses a local source (default: false)
#
# Returns:
#
#   An array containing hash references. Each hash contains a route
#   and consists of:
#
#   network -  network destination
#   router  -  router used to reach the above network if used
#       source  -  local ip used to reach the above network if used
#
sub list_routes
{
    my ($viaGateway, $localSource) = @_;
    defined $viaGateway  or $viaGateway = 1;
    defined $localSource or $localSource = 0;

    my @routes = ();
    my @ipOutput = `/bin/ip route show 2>/dev/null`;
    chomp(@ipOutput);

    if ($viaGateway) {
        my  @gwRoutes = grep { $_ =~ m{via}  } @ipOutput; # select routes with gateway
            foreach (@gwRoutes) {
                my ($net, $via, $router) = split(/ /,$_);
                my $route = {network => $net, router => $router};
                push(@routes, $route);
            }

    }

    # get no-gateway routes if instructed to do
    if ($localSource) {
        my @srcRoutes = grep { $_ =~ m{src}  } @ipOutput;
        foreach my $r (@srcRoutes) {
            $r =~ m/^(.*?)\sdev.*?src\s(.*?)$/;
            my $net = $1;
            my $source = $2;
            my $route = { network => $net, source => $source };
            push(@routes, $route);;
        }
    }

    return @routes;
}

# Function: route_is_up
#
#   Checks if a given route is already up.
#
# Parameters:
#
#   network - network destination
#   router -  router used to reach the network
#
# Returns:
#
#       True if it's up, undef otherwise
#
sub route_is_up
{
    my ($network, $router) = @_;

    my @routes = list_routes();
    foreach (@routes) {
        if (($_->{router} eq $router) and
                ($_->{network} eq $network)) {
            return 1;
        }
    }

    return undef;
}

# Function: ip_network
#
#   Returns the network for an address and netmask
#
# Parameters:
#
#   address - IPv4 address
#   netmask - network mask for the above ip
#
# Returns:
#
#       The network address
#
sub ip_network # (address, netmask)
{
    my ($address, $netmask) = @_;
    my $net_bits = pack("CCCC", split(/\./, $address));
    my $mask_bits = pack("CCCC", split(/\./, $netmask));
    return join(".", unpack("CCCC", $net_bits & $mask_bits));
}

# Function: ip_broadcast
#
#   Returns the broadcast address  for an address and netmask
#
# Parameters:
#
#   address - IPv4 address
#   netmask - network mask for the above ip
#
# Returns:
#
#       The broadcast address
#
sub ip_broadcast
{
    my ($address, $netmask) = @_;
    my $net_bits = pack("CCCC", split(/\./, $address));
    my $mask_bits = pack("CCCC", split(/\./, $netmask));
    return join(".", unpack("CCCC", $net_bits | (~$mask_bits)));
}

# Function: ip_mac
#
#   Returns the mac address for a given IP
#
# Parameters:
#
#   address - IPv4 address
#
# Returns:
#
#   The mac address if found or undef
#
sub ip_mac
{
    my ($address) = @_;
    my $output = `arp -an $address`;

    my ($mac) = ($output =~ /(([0-9a-f]{2}:){5}[0-9a-f]{2})/i);
    return $mac;
}

# Function: bits_from_mask
#
#   Given a network mask it returns it in binary format
#
# Parameters:
#
#   netmask - network mask
#
# Returns:
#
#      Network mask in binary format
#
sub bits_from_mask
{
    my $netmask = shift;
    return unpack("%B*", pack("CCCC", split(/\./, $netmask)));
}

# Function: mask_from_bits
#
#   Given a network mask in binary format it returns it in decimal dot notation
#
# Parameters:
#
#   bits - number of bits
#
# Returns:
#
#      Network mask in decimal dot notation
#
sub mask_from_bits
{
    my ($bits) = @_;

    unless($bits >= 0 and $bits <= 32) {
        return undef;
    }
    my $mask_binary = "1" x $bits . "0" x (32 - $bits);
    return join(".",unpack("CCCC", pack("B*",$mask_binary)));
}

# Function: to_network_with_mask
#
#   Given a network and a netmask rerurns the network with embeded mask (form x.x.x.x/n)
#
# Parameters:
#
#   network - network address
#   netmask - network mask
#
# Returns:
#
#      The network in format  x.x.x.x/m
#
sub to_network_with_mask
{
    my ($network, $netmask) = @_;

    my $bits = bits_from_mask($netmask);
    return "$network/$bits";
}

# Function: to_network_without_mask
#
#   Given a  network with embeded mask (form x.x.x.x/n) it returns the network and netmask
#
# Parameters:
#
#       networkWithMask - network address in format  x.x.x.x/m
#
# Returns:
#
#      (network, netmask)
#
sub to_network_without_mask
{
    my ($networkWithMask) = @_;

    my ($network, $bits) = split '/', $networkWithMask, 2;
    my $netmask = mask_from_bits($bits);
    return ($network, $netmask);
}

# Function: list_local_addresses
#
# Returns:
#    a list with all local ipv4 addresses
#
sub list_local_addresses
{
    my @ifaces = list_ifaces();
    my @localAddresses = map { iface_is_up($_) ?  iface_addresses($_) : () } @ifaces;
    @localAddresses    = map { s{/.*$}{}; $_  } @localAddresses;
    return @localAddresses;
}

# Function: list_local_addresses_with_netmask
#
# Returns:
#   a flat list with pairs of all local ipv4 addresses
#       and their netmask
sub list_local_addresses_with_netmask
{
    my @ifaces = list_ifaces();
    my @localAddresses = map { iface_is_up($_) ?  %{ iface_addresses_with_netmask($_) } : () } @ifaces;
    return @localAddresses;
}

# Method: getFreePort
#
#  Looks for a unused port
#
#  Parameters:
#    proto - protocol ('tcp' or 'udp')
#    localAddess - local address on which look for a free port
#
#   Returns:
#     port number or undef if it could not find a free port
sub getFreePort
{
    my ($proto, $localAddr) = @_;
    $proto or
        throw EBox::Exceptions::MissingArgument('proto');
    $localAddr or
        throw EBox::Exceptions::MissingArgument('localAddr');

    my @socketParams = (
                        Proto => $proto,
                        LocalAddr => $localAddr,
                        LocalPort => 0, # to select a unused port
                       );

    my $sock = IO::Socket::INET->new(@socketParams );
    defined $sock
        or return undef;

    my $port = $sock->sockport();
    $sock->close();

    return $port;
}

1;