This file is indexed.

/usr/share/perl5/EBox/Network/Model/MultiGwRulesDataTable.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
# 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::Network::Model::MultiGwRulesDataTable;

use EBox::Global;
use EBox::Gettext;
use EBox::Validate qw(:all);
use EBox::Exceptions::External;
use Perl6::Junction qw( any );
use EBox::Types::Int;
use EBox::Types::Text;
use EBox::Types::Boolean;
use EBox::Types::Select;
use EBox::Types::IPAddr;
use EBox::Types::Union;
use EBox::Types::Union::Text;

use strict;
use warnings;

use base 'EBox::Model::DataTable';

sub new
{
    my $class = shift;
    my %parms = @_;

    my $self = $class->SUPER::new(@_);
    bless($self, $class);
    $self->{'pageSize'} = 10;

    return $self;
}

sub ifacesSub
{
    my ($self) = @_;

    my $network = $self->parentModule();

    my @options;
    push (@options, {
        'value' => 'any',
        'printableValue' => __('any')
    });
    foreach my $iface (@{$network->InternalIfaces()}) {
        push (@options, {
           'value' => $iface,
           'printableValue' => $iface
        });
    }

    return sub {
        return \@options
    };
}

# Method: _table
#
# Overrides:
#
#     <EBox::Model::DataTable::_table>
#
sub _table
{
    my ($self) = @_;

    my @tableHead =
    (
        new EBox::Types::Select(
           'fieldName' => 'iface',
           'printableName' => __('Interface'),
           'editable' => 1,
           'populate' => $self->ifacesSub(),
           'help' => __('Incoming interface to match packets. If you '.
                        ' want to match a whole subnet you can ' .
                        ' select the interface of that subnet')
            ),
        new EBox::Types::Union(
            'fieldName' => 'source',
            'printableName' => __('Source'),
            'subtypes' =>
                [
                new EBox::Types::Union::Text(
                    'fieldName' => 'source_any',
                    'printableName' => __('Any')),
                new EBox::Types::IPAddr(
                    'fieldName' => 'source_ipaddr',
                    'printableName' => __('Source IP'),
                    'editable' => 1,
                    'optional' => 1),
                new EBox::Types::Select(
                    'fieldName' => 'source_object',
                    'printableName' => __('Source object'),
                    'foreignModel' => $self->modelGetter('objects', 'ObjectTable'),
                    'foreignField' => 'name',
                    'foreignNextPageField' => 'members',
                    'editable' => 1),
                new EBox::Types::Union::Text(
                    'fieldName' => 'source_ebox',
                    'printableName' => 'Zentyal')
                ],
            'size' => '16',
            'unique' => 1,
            'editable' => 1,
            ),
        new EBox::Types::Union(
            'fieldName' => 'destination',
            'printableName' => __('Destination'),
            'subtypes' =>
                [
                new EBox::Types::Union::Text(
                    'fieldName' => 'destination_any',
                    'printableName' => __('Any')),
                new EBox::Types::IPAddr(
                    'fieldName' => 'destination_ipaddr',
                    'printableName' => __('Destination IP'),
                    'editable' => 1,
                    'optional' => 1),
                new EBox::Types::Select(
                    'fieldName' => 'destination_object',
                    'printableName' => __('Destination object'),
                    'foreignModel' => $self->modelGetter('objects', 'ObjectTable'),
                    'foreignField' => 'name',
                    'foreignNextPageField' => 'members',
                    'editable' => 1)
                ],
            'size' => '16',
            'unique' => 1,
            'editable' => 1
            ),
        new EBox::Types::Select(
                'fieldName' => 'service',
                'printableName' => __('Service'),
                'foreignModel' => $self->modelGetter('services', 'ServiceTable'),
                'foreignField' => 'printableName',
                'foreignNextPageField' => 'configuration',
                'editable' => 1,
            ),
        new EBox::Types::Select(
            'fieldName' => 'gateway',
            'printableName' => 'Gateway',
            'foreignModel' => $self->modelGetter('network', 'GatewayTable'),
            'foreignField' => 'name',
            'editable' => 1,
            'help' => __('Gateway to route packets matching ' .
                        'this rule')
            )
    );

    my $dataTable =
    {
        'tableName' => 'MultiGwRulesDataTable',
        'printableTableName' => __('Multigateway rules'),
        'defaultController' => '/Network/Controller/MultiGwRulesDataTable',
        'defaultActions' =>
            [
                'add', 'del', 'clone',
                'move', 'editField',
                'changeView'
            ],
        'tableDescription' => \@tableHead,
        'class' => 'dataTable',
        'order' => 1,
        'enableProperty' => 1,
        'defaultEnabledValue' => 1,
        'help' => __('You can decide what kind of traffic goes out by each gateway. This way, you can force a subnet, service, destination and so forth  to use the router you choose. Please, bear in mind that rules will be applied in order, from top to bottom, you can reorder them once they are added. If you do not set a port or an IP address, then the rule will match all of them'),
        'rowUnique' => 0,
        'printableRowName' => __('rule'),
    };

    return $dataTable;
}

sub iptablesRules
{
    my $self = shift;

    my @rules;
    for my $id (@{$self->enabledRows()}) {
        my $row = $self->row($id);
        my @rule = $self->_buildIptablesRule($row);
        if (@rule) {
            push (@rules, @rule);
        }
    }

    return \@rules;
}


sub _ifacesForRule
{
    my ($self, $iface) = @_;

    unless ($iface eq 'any') {
        return [$iface];
    }

    my $network = $self->parentModule();
    my @ifaces;
    foreach my $iface (@{$network->InternalIfaces()}) {
        push (@ifaces, $iface)
    }

    return \@ifaces;
}


sub _objectMembers
{
    my ($self, $row, $elementName) = @_;
    my $oid = $row->elementByName($elementName)->subtype()->value();
    my $objects = $self->global()->modInstance('objects');
    return $objects->objectMembers($oid);
}


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

    my $network = $self->parentModule();
    my $services = $self->global()->modInstance('services');
    my $marks = $network->marksForRouters();

    my $iface = $row->valueByName('iface');
    my $srcType = $row->elementByName('source')->selectedType();
    my $dstType = $row->elementByName('destination')->selectedType();
    my $serviceConf = $services->serviceConfiguration($row->elementByName('service')->value());
    my $gw = $row->valueByName('gateway');

    # Return if the gateway for this rule is disabled
    my $gwRow = $self->parentModule()->model('GatewayTable')->row($gw);
    return unless (defined $gwRow);
    return unless ($gwRow->valueByName('enabled'));

    my @ifaces = @{$self->_ifacesForRule($iface)};

    # prepare srcs into @src array
    my @src;
    if ($srcType eq 'source_any') {
        @src = ('');
    } elsif  ($srcType eq 'source_ipaddr') {
        my $ipaddr = $row->elementByName('source')->subtype();
        my $ip;
        if ($ipaddr->ip()) {
            $ip = ' --source ' . $ipaddr->ip() . '/' . $ipaddr->mask();
        } else {
            $ip = '';
        }
        @src = ($ip);
    } elsif  ($srcType eq 'source_ebox') {
        @src = ('');
    } else {
        my $members = $self->_objectMembers($row, 'source');
        @src = @{$members->iptablesSrcParams()};
    }

    # prepare dsts into @dst array
    my @dst;
     if ($dstType eq 'destination_any') {
        @dst = ('');
    } elsif  ($dstType eq 'destination_ipaddr') {
        my $ipaddr = $row->elementByName('destination')->subtype();
        my $ip;
        if ($ipaddr->ip()) {
            $ip = ' --destination ' . $ipaddr->ip() . '/' . $ipaddr->mask();
        } else {
            $ip = '';
        }
        @dst = ($ip);
    } else {
        my $members = $self->_objectMembers($row, 'destination');
        @dst = @{$members->iptablesDstParams()};
    }

    my @chains;
    unless ($srcType eq 'source_ebox') {
        push(@chains, 'PREROUTING');
    }
    if ($srcType eq 'source_ebox' or ($srcType eq 'source_any' and $iface eq 'any')) {
        push(@chains, 'OUTPUT');
    }

    #rules for all prefixes
    my @all_rules;
    foreach my $chain (@chains) {
        my $prefix = "-t mangle -A $chain";

        # prepare rules determined by the service into @prerules array
        my @prerules;

        my $port;
        foreach my $ser (@{$serviceConf}) {
            $port = "";
            my $protocol = $ser->{'protocol'};
            my $srcPort = $ser->{'source'};
            my $dstPort = $ser->{'destination'};

            if ($protocol eq any ('tcp', 'udp', 'tcp/udp')) {

                if ($srcPort ne 'any') {
                    $port .= " --source-port $srcPort ";
                }

                if ($dstPort ne 'any') {
                    $port .= " --destination-port $dstPort ";
                }

                if ($protocol eq 'tcp/udp') {
                    push (@prerules, $prefix . " -p udp " . $port);
                    push (@prerules, $prefix . " -p tcp " . $port);
                } else {
                    push (@prerules, $prefix . " -p $protocol " . $port);
                }

            } elsif ($protocol eq any ('gre', 'icmp', 'esp')) {
                push (@prerules, $prefix . " -p $protocol " . $port);
            } elsif ($protocol eq 'any') {
                push (@prerules, $prefix . "" . $port);
            }
        }

        # generate final iptables rules from @prerules into @rules array
        my @rules;

        # src ebox (already with different prefix) we don't loop arround ifaces
        if ($srcType eq 'source_ebox' or $chain eq 'OUTPUT') {
            for my $rule (@prerules) {
                for my $rsrc (@src) {
                    for my $rdst (@dst) {
                        my $wrule = $rule . "$rsrc $rdst";
                        my $mrule  = "$wrule -m mark --mark 0/0xff "
                            . "-j MARK "
                            . "--set-mark $marks->{$gw}";
                        push (@rules, $mrule);
                    }
                }
            }
        } else {
            for my $rule (@prerules) {
                for my $riface (@ifaces) {
                    for my $rsrc (@src) {
                        for my $rdst (@dst) {
                            $riface = $network->realIface($riface);
                            my $wrule = $rule . " -i $riface";
                            $wrule .= " $rsrc $rdst";
                            my $mrule  = "$wrule -m mark --mark 0/0xff "
                                . "-j MARK "
                                . "--set-mark $marks->{$gw}";
                            push (@rules, $mrule);
                        }
                    }
                }
            }
        }

        push(@all_rules, @rules);
    }
    return @all_rules;
}

sub precondition
{
    my $network = EBox::Global->modInstance('network');
    my $nGateways = @{$network->gateways()};
    return $nGateways >= 2;
}

1;