This file is indexed.

/usr/share/perl5/EBox/OpenVPN/Model/ServerConfiguration.pm is in zentyal-openvpn 2.3.10+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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# 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
use strict;
use warnings;

package EBox::OpenVPN::Model::ServerConfiguration;
use base 'EBox::Model::DataForm';

use EBox::Global;
use EBox::Gettext;
use EBox::Validate qw(:all);
use EBox::Exceptions::External;
use EBox::Exceptions::DataExists;

use EBox::Types::Boolean;
use EBox::Types::HasMany;
use EBox::Types::Select;
use EBox::Types::Password;
use EBox::Types::DomainName;
use EBox::Types::Host;
use EBox::Types::IPNetwork;
use EBox::Types::IPAddr;

use EBox::OpenVPN::Server;
use EBox::OpenVPN::Types::PortAndProtocol;
use EBox::OpenVPN::Types::Certificate;
use EBox::OpenVPN::Types::TlsRemote;

use EBox::View::Customizer;

use constant ALL_INTERFACES => '_ALL';


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

    my $self = $class->SUPER::new(@_);
    bless($self, $class);

    return $self;
}



sub _table
{
    my @tableHead =
        (
         new EBox::OpenVPN::Types::PortAndProtocol(

             fieldName => 'portAndProtocol',
             printableName => __('Server port'),
             editable => 1,
             ),
         new EBox::Types::IPNetwork(
             fieldName => 'vpn',
             printableName => __('VPN address'),
             editable => 1,
             help => __('Use a network address which is not used by this ' .
                        'machine')
             ),

         new EBox::OpenVPN::Types::Certificate(
             fieldName => 'certificate',
             printableName => __('Server certificate'),

             editable       => 1,
             ),

         new EBox::OpenVPN::Types::TlsRemote(
                 fieldName => 'tlsRemote',
                 printableName => __('Client authorization by common name'),
                 editable => 1,
                 help => __('If disabled, any client with a certificate ' .
                            'generated by Zentyal will be able to connect. ' .
                            'If enabled, only certificates whose common ' .
                            'name begins with the selected value will be ' .
                            'able to connect.'
                           )
                 ),
         new EBox::Types::Boolean(
                 fieldName =>  'tunInterface',
                 printableName => __('TUN interface'),
                 editable => 1,
                 defaultValue => 0,
                 ),
         new EBox::Types::Boolean(
                 fieldName =>  'masquerade',
                 printableName => __('Network Address Translation'),
                 editable => 1,
                 defaultValue => 0,
                 help => __('Enable it if this VPN server is not the default gateway')
                 ),
         new EBox::Types::Boolean(
                 fieldName => 'clientToClient',
                 printableName => __('Allow client-to-client connections'),
                 editable => 1,
                 defaultValue => 0,
                 help => __('Enable it to allow client machines of this VPN ' .
                            'to see each other')
                 ),
         new EBox::Types::Boolean(
                 fieldName => 'pullRoutes',
                 printableName => __('Allow Zentyal-to-Zentyal tunnels'),
                 editable => 1,
                 defaultValue => 0,
                 help => __('Enable it if this VPN is used to connect to ' .
                            'another Zentyal')
                 ),
         new EBox::Types::Password(
                 fieldName => 'ripPasswd',
                 printableName => __('Zentyal-to-Zentyal tunnel password'),
                 minLength => 6,
                 editable => 1,
                 optional => 1,

                 ),
         new EBox::Types::Select(
                 fieldName  => 'local',
                 printableName => __('Interface to listen on'),
                 editable => 1,
                 populate      => \&_populateLocal,
                 defaultValue => ALL_INTERFACES,
                 ),
         new EBox::Types::Boolean(
             fieldName => 'redirectGw',
             printableName => __('Redirect gateway'),
             editable => 1,
             defaultValue => 0,
             help => __('Makes Zentyal the default gateway for the client'),
            ),
         new EBox::Types::Host(
             fieldName => 'dns1',
             printableName => __('First nameserver'),
             editable => 1,
             optional => 1,

            ),
         new EBox::Types::Host(
             fieldName => 'dns2',
             printableName => __('Secod nameserver'),
             editable => 1,
             optional => 1,
            ),
         new EBox::Types::DomainName(
             fieldName => 'searchDomain',
             printableName => __('Search domain'),
             editable => 1,
             optional => 1,
            ),
         new EBox::Types::Host(
             fieldName => 'wins',
             printableName => __('WINS server'),
             editable => 1,
             optional => 1,
            ),

         );

    my $dataTable =
    {
        'tableName'               => __PACKAGE__->nameFromClass(),
        'printableTableName' => __('Server configuration'),
        'automaticRemove' => 1,
        'defaultController' => '/OpenVPN/Controller/ServerConfiguration',
        'defaultActions' => ['add', 'del', 'editField',  'changeView' ],
        'tableDescription' => \@tableHead,
        'class' => 'dataTable',
        'printableRowName' => __('server'),
        'sortedBy' => 'name',
        'modelDomain' => 'OpenVPN',
    };

    return $dataTable;
}

# Method: viewCustomizer
#
#   Overrides <EBox::Model::DataTable::viewCustomizer> to implement
#   a custom behaviour to show and hide source and destination ports
#   depending on the protocol
#
#
sub viewCustomizer
{
    my ($self) = @_;
    my $customizer = new EBox::View::Customizer();
    $customizer->setModel($self);
    my $tunnelParams = [qw/ripPasswd/];
    my $noTunnelParams = [qw/redirectGw dns1 dns2 searchDomain wins/];

    $customizer->setOnChangeActions(
            { pullRoutes =>
                {
                on  => { enable  => $tunnelParams,
                         disable => $noTunnelParams,
                        },
                off => {
                        enable  => $noTunnelParams,
                        disable => $tunnelParams
                       },
                }
            });
    return $customizer;
}


sub name
{
    __PACKAGE__->nameFromClass(),
}




sub _populateLocal
{
    my @options;

    my $network = EBox::Global->modInstance('network');

    my @enabledIfaces = grep {
        $network->ifaceMethod($_) ne 'notset'
    } @{ $network->ifaces() };

    @options = map { { value => $_ } }  @enabledIfaces;


    push @options,  {
                     value => ALL_INTERFACES,
                      printableValue => __('All network interfaces'),
                    };

    return \@options;
}




sub validateTypedRow
{
    my ($self, $action, $params_r, $actual_r) = @_;

    $self->_uniqPortAndProtocol($action, $params_r, $actual_r);

    $self->_checkVPN($action, $params_r, $actual_r);

    $self->_checkServerCertificate($action, $params_r, $actual_r);

    $self->_checkRipPasswd($action, $params_r, $actual_r);

#    $self->_checkMasqueradeIsAvailable($action, $params_r, $actual_r);

    $self->_checkIface($action, $params_r, $actual_r);
#    $self->_checkIfaceAndMasquerade($action, $params_r, $actual_r);

    $self->_checkTlsRemote($action, $params_r, $actual_r);

    $self->_checkTunnelForbiddenParams($action, $params_r, $actual_r);

    $self->_checkPortIsAvailable($action, $params_r, $actual_r);
}


sub _checkRipPasswd
{
    my ($self, $action, $params_r, $actual_r) = @_;

    return unless (
                   (exists $params_r->{ripPasswd}) or
                   (exists $params_r->{pullRoutes})
                  );

    my $pullRoutes = exists $params_r->{pullRoutes} ?
                                    $params_r->{pullRoutes}->value() :
                                    $actual_r->{pullRoutes}->value();
    my $ripPasswd  = exists $params_r->{ripPasswd} ?
                                    $params_r->{ripPasswd}->value() :
                                    $actual_r->{ripPasswd}->value();

    return if (not $pullRoutes); # only ripPasswd is needed when pullRoutes
                                 #  is on

    $ripPasswd or
        throw EBox::Exceptions::External(
          __('Zentyal to Zentyal tunnel option requires a RIP password')
                                        );
}

sub _checkVPN
{
    my ($self, $action, $params_r, $actual_r) = @_;

    return unless ( exists $params_r->{vpn} );

    my $vpnAddress = $params_r->{vpn}->printableValue();
    # check other servers VPN networks
    $self->_uniqVPNAddress($vpnAddress);

    # check interfaces networks
    my $network = EBox::Global->getInstance()->modInstance('network');
    foreach my $iface (@{ $network->ifaces( )}) {
        my @addresses = @{  $network->ifaceAddresses($iface) };
        foreach my $addr_r (@addresses) {
            my $address = $addr_r->{address};
            my $netmask = $addr_r->{netmask};
            my $ipnetwork = EBox::NetWrappers::ip_network($address, $netmask);
            my $ipnetworkWithMask = EBox::NetWrappers::to_network_with_mask($ipnetwork, $netmask);

            if ($ipnetworkWithMask eq $vpnAddress) {
                throw EBox::Exceptions::External(
                    __x('The VPN address {addr} is already used by interface {iface}',
                        addr => $vpnAddress,
                        iface => $iface,));
            }
        }
    }
}

sub _uniqVPNAddress
{
    my ($self, $vpnAddress) = @_;
    my $olddir = $self->directory();

    my $parentId = $self->parentRow()->id();
    my $serverList = $self->parentModule()->model('Servers');
    foreach my $id ( @{ $serverList->ids()}) {
        if ($parentId eq $id) {
            next;
        }
        my $row = $serverList->row($id);
        my $serverConf = $row->subModel('configuration');
        my $other      = $serverConf->row()->elementByName('vpn');

        if ($vpnAddress eq $other->printableValue()) {
            throw EBox::Exceptions::External(
                    __('Other server is using the same VPN address, please choose another')
                    );
        }
    }

    $self->setDirectory($olddir);
}

sub _uniqPortAndProtocol
{
    my ($self, $action, $params_r) = @_;

    exists $params_r->{portAndProtocol}
        or return;
    my $olddir = $self->directory();

    my $portAndProtocol = $params_r->{portAndProtocol};

    my $parentId = $self->parentRow()->id();
    my $serverList = $self->parentModule()->model('Servers');
    foreach my $id ( @{ $serverList->ids()}) {
        if ($parentId eq $id) {
            next;
        }
        my $row = $serverList->row($id);
        my $serverConf = $row->subModel('configuration');
        my $other      = $serverConf->portAndProtocolType();

        if ($portAndProtocol->cmp($other) == 0) {
            throw EBox::Exceptions::External(
                    __('Other server is listening on the same port')
                    );
        }
    }

    $self->setDirectory($olddir);
}

sub _checkPortIsAvailable
{
    my ($self, $action, $params_r, $actual_r) = @_;
    my @ifacesToCheck;

    my $portAndProtocolNotChanged =  (not exists $params_r->{portAndProtocol} );
    my $localIfaceNotChanged      =    (not exists $params_r->{local} );
    if ( $portAndProtocolNotChanged and $localIfaceNotChanged ) {
        return;
    }

    my $local = exists $params_r->{local} ?
                    $params_r->{local}->value() :
                    $actual_r->{local}->value();
    if ($local eq ALL_INTERFACES) {
        $local = undef;
    }

    my $portAndProtocol = exists $params_r->{portAndProtocol} ?
                                   $params_r->{portAndProtocol} :
                                   $actual_r->{'portAndProtocol'};
    my $proto = $portAndProtocol->protocol();
    my $port  = $portAndProtocol->port();

    my $ownModuleName = $self->parentModule()->name();
    my @modules = grep {
                       ($_->can('usesPort')) and
                      ($_->name() ne $ownModuleName)
                  }  @{EBox::Global->getInstance()->modInstances()};
    foreach my $mod (@modules) {
        if ($mod->usesPort($proto, $port, $local)) {
            throw EBox::Exceptions::External(
                __x(
                    'Port {p}/{pr} is in use by {mod}',
                    p => $portAndProtocol->printableValue(),
                    pr => $proto,
                    mod => $mod->name()
                       )
               );
        }
    }
}


sub _alreadyCheckedAvailablity
{
    my ($self, $proto, $port, $local, $actual_r) = @_;

    # avoid falses positives
    my ($oldProto, $oldPort, $oldLocal) = (
            $actual_r->{portAndProtocol}->protocol(),
            $actual_r->{portAndProtocol}->port(),
            $actual_r->{local}->value(),
            );
    my $samePort  = $port eq $oldPort;
    my $sameProto = $proto eq $oldProto;
    my $sameLocal = $local eq $oldLocal;

    if ($local eq ALL_INTERFACES) {
        if ($sameProto and $samePort) {
            # we have already checked
            return 1;
        }
    }
    else {
        if ($sameProto and $samePort and $sameLocal) {
            # we have already checked,
            return 1;
        }
    }

    return 0;
}



#XXX this must be in a iface type...
sub _checkIface
{
    my ($self, $action, $params_r, $actual_r) = @_;

    $params_r->{'local'} or
        return;

    my $iface   = $params_r->{'local'}->value();
    if ($iface eq ALL_INTERFACES) {
        return;
    }


    my $network = EBox::Global->modInstance('network');

    if (not $network->ifaceExists($iface) ) {
        throw EBox::Exceptions::External(
            __x('The interface {iface} does not exist'), iface => $iface);
    }

    if ( $network->ifaceMethod($iface) eq 'notset') {
        throw EBox::Exceptions::External(
            __x('The interface {iface} is not configured'), iface => $iface);
    }
}



sub _checkMasqueradeIsAvailable
{
    my ($self, $action, $params_r, $actual_r) = @_;

    my $masquerade = exists $params_r->{masquerade} ?
                                 $params_r->{masquerade}->value() :
                                 $actual_r->{masquerade}->value();
    if (not $masquerade ) {
        return;
    }


    my $firewall = EBox::Global->modInstance('firewall');
    if (not $firewall) {
        throw EBox::Exceptions::External(
          __('Cannot use Network Address translation because it requires the ' .
             'firewall module. The module is neither installed or activated')
                                        );
    }

    if (not $firewall->isEnabled()) {
        throw EBox::Exceptions::External(
          __('Cannot use Network Address translation because it requires the ' .
              'firewall module enabled. Please activate it and try again')
                                        );
    }
}

sub _checkIfaceAndMasquerade
{
    my ($self, $action, $params_r, $actual_r) = @_;
    my $masquerade = exists $params_r->{masquerade} ?
                                 $params_r->{masquerade}->value() :
                                 $actual_r->{masquerade}->value();


    if ($masquerade) {
        # with masquerade either internal or external interfaces are correct
        return;
    }


    my $local   = exists $params_r->{local} ?
                                 $params_r->{local}->value() :
                                 $actual_r->{local}->value();

    my $network = EBox::Global->modInstance('network');

    if ($local eq ALL_INTERFACES) {
        # check that at least there is one external interface
        my $externalIfaces = @{ $network->ExternalIfaces() };
        if (not $externalIfaces) {
            throw EBox::Exceptions::External(
             __('At least one external interface is needed to connect to the ' .
                'server unless network address translation option is enabled')
                                            );
        }
    }
    else {
        my $external = $network->ifaceIsExternal($local);
        if (not $external) {
            throw EBox::Exceptions::External(
              __('The interface must be a external interface, unless ' .
              'Network Address Translation option is on')
                                            )
        }
    }

}


sub _checkServerCertificate
{
    my ($self, $action, $params_r, $actual_r) = @_;

    (exists $params_r->{certificate}) or
        return;

    my $cn = $params_r->{certificate}->value();
    EBox::OpenVPN::Server->checkCertificate($cn);
}


sub _checkTlsRemote
{
    my ($self, $action, $params_r, $actual_r) = @_;


    (exists $params_r->{tlsRemote}) or
        return;

    my $cn = $params_r->{tlsRemote}->value();

    if ($cn == 0) {
        # TLS rmeote option disabled, nothing to check
        return;
    }


    EBox::OpenVPN::Server->checkCertificate($cn);
}


sub _checkTunnelForbiddenParams
{
    my ($self, $action, $params_r, $all_r) = @_;
    if (not $all_r->{pullRoutes}->value()) {
        # no tunnel, no checks needed
        return;
    }

    my @forbidParams = qw(clientToClient dns1 dns2 searchDomain wins);
    foreach my $param (@forbidParams) {
        if ($all_r->{$param}->value()) {
            throw EBox::Exceptions::External(
                __x('{par} is not compatible with Zentyal-to-Zentyal tunnel',
                    par => $all_r->{$param}->printableName()
                   )
               )
        }
    }
}


# The interface type resides in the ServerModels so we must set it in the
# parentRow
sub updatedRowNotify
{
    my ($self, $row, $oldRow, $force) = @_;

    my $toSet = $row->valueByName('tunInterface') ? 'tun' : 'tap';
    my $parentRow = $self->parentRow();
    my $ifaceType = $parentRow->elementByName('interfaceType');
    if ($ifaceType->value() ne $toSet) {
        $ifaceType->setValue($toSet);
        $parentRow->store();
    }
}

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

    $self->portAndProtocolType()->port()     or return 0;
    $self->portAndProtocolType()->protocol() or return 0;

    $self->vpnType()->printableValue ne ''    or return 0;

    my $cn = $self->certificate();
    $cn
        or return 0;
    EBox::OpenVPN::Server->checkCertificate($cn);

    return 1;
}

# Method: pageTitle
#
#   Overrides <EBox::Model::DataTable::pageTitle>
#   to show the name of the domain
sub pageTitle
{
    my ($self) = @_;
    return $self->parentRow()->printableValueByName('name');
}

1;