This file is indexed.

/usr/share/perl5/EBox/Event/Watcher/Gateways.pm is in zentyal-network 2.3.13+quantal1.

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
# Copyright (C) 2009-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

use strict;
use warnings;

package EBox::Event::Watcher::Gateways;

# Class: EBox::Event::Watcher::Gateways;
#
# This class is a watcher which checks connection/disconnection of gateways
#
use base 'EBox::Event::Watcher::Base';

use EBox::Network;
use EBox::Event;
use EBox::Global;
use EBox::Gettext;
use EBox::Validate;
use EBox::Exceptions::Lock;

use Error qw(:try);

# Group: Public methods

# Constructor: new
#
#        The constructor for <EBox::Event::Watcher::Gateways>
#
# Overrides:
#
#        <EBox::Event::Watcher::Base::new>
#
# Parameters:
#
#        - non parameters
#
# Returns:
#
#        <EBox::Event::Watcher::Gateways> - the newly created object
#
sub new
{
    my ($class) = @_;

    my $network = EBox::Global->getInstance(1)->modInstance('network');
    my $options = $network->model('WANFailoverOptions')->row();
    my $period = $options->valueByName('period');

    my $self = $class->SUPER::new(period => $period);
    $self->{counter} = 0;

    bless ($self, $class);

    return $self;
}

# Method: ConfigurationMethod
#
# Overrides:
#
#       <EBox::Event::Component::ConfigurationMethod>
#
sub ConfigurationMethod
{
    return 'none';
}


# Method: run
#
#        Check state of the gateways.
#
# Overrides:
#
#        <EBox::Event::Watcher::Base::run>
#
# Returns:
#
#        array ref - <EBox::Event> an info event is sent if Zentyal is up and
#        running and a fatal event if Zentyal is down
#
sub run
{
    my ($self) = @_;

    EBox::debug('Entering failover event...');

    $self->{eventList} = [];
    $self->{failed} = {};

    # Getting readonly instance to test the gateways
    my $global = EBox::Global->getInstance(1);
    my $network = $global->modInstance('network');

    return [] unless $network->isEnabled();

    my $rules = $network->model('WANFailoverRules');
    my $gateways = $network->model('GatewayTable');
    $self->{gateways} = $gateways;
    $self->{marks} = $network->marksForRouters();

    my @enabledRules = @{$rules->enabledRows()};

    # If we don't have any enabled rule we finish here
    return [] unless @enabledRules;

    foreach my $id (@enabledRules) {
        EBox::debug("Testing rules for gateway with id $id...");
        my $row = $rules->row($id);
        $self->_testRule($row);
    }

    # We won't do anything if there are unsaved changes
    my $readonly = EBox::Global->getInstance()->modIsChanged('network');

    unless ($readonly) {
        # Getting read/write instance to apply the changes
        $network = EBox::Global->modInstance('network');
        $gateways = $network->model('GatewayTable');
    }

    EBox::debug('Applying changes in the gateways table...');

    my $needSave = 0;
    foreach my $id (@{$gateways->ids()}) {
        my $row = $gateways->row($id);
        my $gwName = $row->valueByName('name');
        my $enabled = $row->valueByName('enabled');

        # It must be enabled if all tests are passed
        my $enable = $enabled;
        if (defined($self->{failed}->{$id})) {
            $enable = not($self->{failed}->{$id})
        };

        EBox::debug("Properties for gateway $gwName ($id): enabled=$enabled, enable=$enable");

        # We don't do anything if the previous state is the same
        if ($enable xor $enabled) {
            unless ($readonly) {
                $row->elementByName('enabled')->setValue($enable);
                $row->store();
                $needSave = 1;
            }
            if ($enable) {
                my $event = new EBox::Event(message    => __x("Gateway {gw} connected again.", gw => $gwName),
                                            level      => 'info',
                                            source     => 'WAN Failover',
                                            additional => { 'up' => 1, gateway => $gwName });
                push (@{$self->{eventList}}, $event);
            }
        }
    }

    if ($readonly) {
        EBox::warn('Leaving failover event without doing anything due to unsaved changes on the Zentyal interface.');
        foreach my $event (@{$self->{eventList}}) {
            $event->{message} = __('WARNING: These changes are not applied due to unsaved changes on the administration interface.') . "\n\n" . $event->{message};
        }
        return $self->{eventList};
    }

    my ($setAsDefault, $unsetAsDefault);
    # Check if default gateway has been disabled and choose another
    my $default = $gateways->findValue('default' => 1);
    my $originalId = $network->selectedDefaultGateway();
    EBox::debug("The preferred default gateway is $originalId");
    unless ($default and $default->valueByName('enabled')) {
        # If the original default gateway is alive, restore it
        my $original;
        $original = $gateways->row($originalId) if $originalId;
        if ($original and $original->valueByName('enabled')) {
            $original = $gateways->row($originalId);
            $unsetAsDefault = $default;
            $setAsDefault   = $original;
            EBox::debug('The original default gateway will be restored');
            $needSave = 1;
        } else {
            EBox::debug('Checking if there is another enabled gateway to set as default');
            # Check if we can find another enabled to set it as default
            my $other = $gateways->findValue('enabled' => 1);
            if ($other) {
                $unsetAsDefault = $default;
                $setAsDefault   = $other;
            }
        }
    } else {
        # check if the gw enabled is the prefered one
        if ($originalId and ($default->id() ne $originalId)) {
            my $original = $gateways->row($originalId);
            if ($original and $original->valueByName('enabled')) {
                EBox::debug('The original default gateway will replace the current default');
                $unsetAsDefault = $default;
                $setAsDefault   = $original;
            }
        }
    }

    if ($unsetAsDefault) {
        $unsetAsDefault->elementByName('default')->setValue(0);
        $unsetAsDefault->store();
        EBox::debug("The gateway " .  $unsetAsDefault->valueByName('name').
                        " is not longer default");
        $needSave = 1;
    }

    if ($setAsDefault) {
        $setAsDefault->elementByName('default')->setValue(1);
        $setAsDefault->store();
        EBox::debug("The gateway " .  $setAsDefault->valueByName('name').
                        " is now the default");
        $needSave = 1;
    }

    if ($needSave) {
        EBox::debug('Regenerating rules for the gateways');
        $network->regenGateways();

        foreach my $module ($global->modInstancesOfType('EBox::NetworkObserver')) {
            my $timeout = 60;
            while ($timeout) {
                try {
                    $module->regenGatewaysFailover();
                    last;
                } catch EBox::Exceptions::Lock with {
                    sleep 5;
                    $timeout -= 5;
                };
            }
            if ($timeout <= 0) {
                EBox::error("WAN Failover: $module->{name} module has been locked for 60 seconds.");
            }
        }
    } else {
        EBox::debug('No need to regenerate the rules for the gateways');
    }

    EBox::debug('Leaving failover event...');

    return $self->{eventList};
}

sub _testRule # (row)
{
    my ($self, $row) = @_;

    my $gw = $row->valueByName('gateway');
    my $network = EBox::Global->modInstance('network');

    my $gwName = $self->{gateways}->row($gw)->valueByName('name');
    my $iface = $self->{gateways}->row($gw)->valueByName('interface');
    my $wasEnabled = $self->{gateways}->row($gw)->valueByName('enabled');

    EBox::debug("Entering _testRule for gateway $gwName...");

    # First test on this gateway, initialize its entry on the hash
    unless (exists $self->{failed}->{$gw}) {
        $self->{failed}->{$gw} = 0;
    }

    # If a test for this gw has already failed we don't test any other
    return if ($self->{failed}->{$gw});

    my ($ppp_iface, $iface_up);
    if ($network->ifaceMethod($iface) eq 'ppp') {
        EBox::debug("It is a PPPoE gateway");

        $ppp_iface = $network->realIface($iface);
        $iface_up = !($ppp_iface eq $iface);

        EBox::debug("Iface $ppp_iface up? = $iface_up");

        if (!$iface_up) {
            # PPP interface down, do not make test (not needed)
            $self->{failed}->{$gw} = 1;
            return;
        }
    }

    my $type = $row->valueByName('type');
    my $typeName = $row->printableValueByName('type');
    my $host = $row->valueByName('host');

    EBox::debug("Running $typeName tests for gateway $gwName...");

    if ($type eq 'gw_ping') {
        my $gwRow = $self->{gateways}->row($gw);
        $host = $gwRow->valueByName('ip');
        return unless $host;
    }

    my $probes = $row->valueByName('probes');
    my $ratio = $row->valueByName('ratio') / 100;
    my $neededSuccesses = $probes * $ratio;
    my $maxFailRatio = 1 - $ratio;
    my $maxFails = $probes * $maxFailRatio;

    my $usedProbes = 0;
    my $successes  = 0;
    my $fails      = 0;

    # Set rule for outgoing traffic through the gateway we are testing
    $self->_setIptablesRule($gw, 1);

    for (1..$probes) {
        $usedProbes++;
        if ($self->_runTest($type, $host)) {
            EBox::debug("Probe number $_ succeded.");
            $successes++;
            last if ($successes >= $neededSuccesses);
        } else {
            EBox::debug("Probe number $_ failed.");
            $fails++;
            last if ($fails >= $maxFails);
        }
    }

    # Clean rule
    $self->_setIptablesRule($gw, 0);

    my $failRatio = $fails / $usedProbes;
    if ($failRatio >= $maxFailRatio) {
        $self->{failed}->{$gw} = 1;

        # Only generate event if gateway was not already disabled
        return unless ($wasEnabled);

        my $disconnectMsg = __x('Gateway {gw} disconnected', gw => $gwName);
        my $reason =__x("'{type}' test to host '{host}' has failed {failRatio}%, max={maxFailRatio}%.",
                        failRatio => sprintf("%.2f", $failRatio*100),
                        type => $typeName, host => $host, maxFailRatio => $maxFailRatio*100);
        my $explanation = __('This gateway will be connected again if the test are passed.');
        my $event = new EBox::Event(message => "$disconnectMsg\n\n$reason\n$explanation",
                                    level   => 'error',
                                    source  => 'WAN Failover',
                                    additional => { down => 1,
                                                    gateway => $gwName,
                                                    type => $type,
                                                    host => $host,
                                                    fail_ratio => $failRatio,
                                                    max_fail_ratio => $maxFailRatio,
                                                }
                                   );
        push (@{$self->{eventList}}, $event);
    }
}

sub _runTest # (type, host)
{
    my ($self, $type, $host) = @_;

    my $result;

    if (($type eq 'gw_ping') or ($type eq 'host_ping')) {
        $result = system("ping -W5 -c1 $host");
    } elsif ($type eq 'dns') {
        $result = system("host -W 5 $host");
    } elsif ($type eq 'http') {
        my $command = "wget $host --tries=1 -T 5 -O /dev/null";
        $result = system($command);
    }

    return $result == 0;
}

sub _setIptablesRule # (gw, set)
{
    my ($self, $gw, $set) = @_;

    my $chain = EBox::Network::FAILOVER_CHAIN();
    # Flush previous rules from custom chain. It'll fail silently
    # if it doesn't exist
    EBox::Sudo::silentRoot("/sbin/iptables -t mangle -F $chain");

    if ($set) {
        # We need to add a rule to return and do nothing
        # when the traffic is generated by zentyal's apache
        my $apachePort = EBox::Global->modInstance('apache')->port();
        my $rule = "/sbin/iptables -t mangle -A $chain "
             . "-p tcp --source-port $apachePort -j RETURN";
        EBox::Sudo::root($rule);

        # Add rule to mark packets generated by zentyal, i.e: failover tests
        my $mark = $self->{marks}->{$gw};
        $rule = "/sbin/iptables -t mangle -A $chain "
             . "-m owner --gid-owner ebox -j MARK --set-mark $mark";
        EBox::Sudo::root($rule);
    }
}


# Group: Protected methods

# Method: _name
#
# Overrides:
#
#        <EBox::Event::Watcher::Base::_name>
#
# Returns:
#
#        String - the event watcher name
#
sub _name
{
    return __('WAN Failover');
}


# Method: _description
#
# Overrides:
#
#        <EBox::Event::Watcher::Base::_description>
#
# Returns:
#
#        String - the event watcher detailed description
#
sub _description
{
    return __('Check if gateways are connected or disconnected.');
}

1;