This file is indexed.

/usr/share/perl5/EBox/OpenVPN/Daemon.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
661
662
663
664
665
666
667
668
669
670
671
# 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::OpenVPN::Daemon;

# package: Parent class for the distinct types of OpenVPN daemons
use strict;
use warnings;

use base qw(EBox::NetworkObserver);

use File::Slurp;
use Error qw(:try);

use EBox::NetWrappers;
use EBox::Service;

use constant UPSTART_DIR => '/etc/init';
use constant RUN_DIR     => '/var/run/';

sub new
{
    my ($class, $row) = @_;

    my $self = {};
    $self->{row}  = $row;

    bless $self, $class;

    return $self;
}

sub _openvpnModule
{
    my ($self) = @_;
    return EBox::Global->modInstance('openvpn');
}

#
# Method: init
#
#   Must be implimented by child to do initalisation stuff
#
#  Parametes:
#        $name        - the name of the daemon
#        @params      - initialization parameters, may be different for each daemon type
#
sub init
{
    throw EBox::Exceptions::NotImplemented('init method');
}

#
# Method: name
#
#  Returns:
#    the name of the daemon
sub name
{
    my ($self) = @_;
    return $self->_rowAttr('name');
}

sub type
{
    throw EBox::Exceptions::NotImplemented('type class method');
}

sub isEnabled
{
    my ($self) = @_;
    return $self->_rowAttr('service');
}

sub _rowAttr
{
    my ($self, $name) = @_;

    my $element = $self->{row}->elementByName($name);
    return $element->value();
}

sub _configAttr
{
    my ($self, $name) = @_;

    my $config =
      $self->{row}->elementByName('configuration')->foreignModelInstance();

    return $config->value($name);
}

#
# Method: upstartName
#
#  Returns:
#    the name of the upstart service that controls the daemon
sub upstartName
{
    my ($self)  = @_;
    return __PACKAGE__->upstartNameForDaemon($self->name(), $self->type());
}

#
# Method: upstartNameForDaemon
#
#  Parameters:
#
#    $type  - daemon's type
#    $name  - daemons's ma,e
#
#  Returns:
#    the name of the upstart service for the daemon type and name given
sub upstartNameForDaemon
{
    my ($class, $name, $type) = @_;
    $type
      or throw EBox::Exceptions::MissingArgument('type');
    $name
      or throw EBox::Exceptions::MissingArgument('name');

    return 'ebox.openvpn.' . $type . '.' . $name;
}

sub _upstartFile
{
    my ($self) = @_;
    return __PACKAGE__->_upstartFileForDaemon($self->name(), $self->type());
}

sub _upstartFileForDaemon
{
    my ($class, $name, $type) = @_;
    return  UPSTART_DIR . '/' . $class->upstartNameForDaemon($name, $type) .
        '.conf';
}

sub ifaceNumber
{
    my ($self) = @_;
    return $self->_rowAttr('interfaceNumber');
}

#
# Method: iface
#
#   get the iface device name used for this daemon
#
# Returns:
#    - the device name
sub iface
{
    my ($self) = @_;

    my $ifaceType = $self->ifaceType();
    my $number    = $self->ifaceNumber();
    return "$ifaceType$number";
}

#
# Method: ifaceType
#
#   get the iface device type used for this daemon
#
# Returns:
#    - the device type
sub ifaceType
{
    my ($self) = @_;
    return $self->_rowAttr('interfaceType');
}

#
# Method: ifaceAddress
#
#   get the vpn's iface address
#
# Returns:
#    - the address in CIDR notation or undef if the interface has not address
sub ifaceAddress
{
    my ($self) = @_;
    my $iface = $self->iface();

    if (not EBox::NetWrappers::iface_exists($iface)) {
        return undef;
    }

    if (not EBox::NetWrappers::iface_is_up($iface)) {
        return undef;
    }

    my %addresses =
      %{ EBox::NetWrappers::iface_addresses_with_netmask($iface) };
    my $nAddresses = keys %addresses;
    if ($nAddresses == 0) {
        EBox::error("No address found for interface $iface");
        return undef;
    }elsif ($nAddresses > 1) {
        EBox::warn(
'More than one address for $iface interface. Only one of them will be shown' );
    }

    my ($addr, $netmask) = each %addresses;
    my $cidrAddr = EBox::NetWrappers::to_network_with_mask($addr, $netmask);

    return $cidrAddr;
}

#
# Method: user
#
#    Return the user will be used to run the  daemon
#    after root drops privileges
#  Returns:
#    string - the user which will be run the daemon after the initialization
sub user
{
    my ($self) = @_;
    return $self->_openvpnModule->user();
}

# Method: group
#
#    Return the user will be used to run the  daemon
#    after root drops privileges
#
#  Returns:
#    string - the group which will be run the daemon after the initialization
sub group
{
    my ($self) = @_;
    return $self->_openvpnModule->group();
}

#
# Method: dh
#
#  Returns:
#    the diffie-hellman parameters file used by the daemon
sub dh
{
    my ($self) = @_;
    return $self->_openvpnModule->dh();
}

sub logFile
{
    my ($self) = @_;
    return $self->_logFile('');
}

sub statusLogFile
{
    my ($self) = @_;
    return $self->_logFile('status-');
}

sub _logFile
{
    my ($self, $prefix) = @_;
    my $logDir = $self->_openvpnModule->logDir();
    my $file = $logDir . "/$prefix"  . $self->name() . '.log';
    return $file;
}

# Method: internal
#
#   tell wether the client must been internal for users in the UI or nodaemon
#   is a internal daemon used and created by other EBox services
#
# Returns:
#  returns the daemon's internal state
sub internal
{
    throw EBox::Exceptions::NotImplemented(
                                          'Must be immplemented in subclasses');
}

# Method: createDirectories
#
#  Create the directory structure needed for the daemon if it does not exists.
#  Default empty implementation
sub createDirectories
{

}

# Method: confFile
#
#   get the configuration file for this daemon
#
#  Parameters:
#     confDir - directory where the configuration file will be stored
#
# Returns:
#    - the path of the configuration file
sub confFile
{
    my ($self, $confDir) = @_;
    my $name = $self->name();

    return __PACKAGE__->confFileForName($name, $confDir);
}

sub confFileForName
{
    my ($package, $name, $confDir) = @_;
    defined $confDir
      or throw EBox::Exceptions::MissingArgument('confDir');
    defined $name
      or throw EBox::Exceptions::MissingArgument('name');

    return "$confDir/$name.d/$name.conf";
}

#
# Method: writeConfFile
#
#   write the daemon's configuration file
#
#  Parameters:
#     confDir - directory where the configuration file will be stored
#
sub writeConfFile
{
    my ($self, $confDir) = @_;

    return if not $self->isEnabled();

    my $confFilePath   = $self->confFile($confDir);
    my $templatePath   = $self->confFileTemplate();
    my $templateParams = $self->confFileParams(confDir => $confDir);

    push @{$templateParams},
      (
        logFile       => $self->logFile(),
        statusLogFile => $self->statusLogFile(),
        pidFile       => $self->_pidFile(),
      );

    my $fileAttrs  = {
                      uid  => 0,
                      gid  => 0,
                      mode => '0400',
    };

    EBox::Module::Base::writeConfFileNoCheck(
        $confFilePath, $templatePath,
        $templateParams, $fileAttrs
    );
}

#
# Method: confFileTemplate
#
#   Abstract method. Must return the configuration file template
#
# Returns:
#   the mason path of the configuration file template
sub confFileTemplate
{
    throw EBox::Exceptions::NotImplemented();
}

#
# Method: daemonFiles
#
#    Get a list with the files and directories generated by the given
#    daemon. Paths must be absolute. Directories contents are not included
#
#    This is a default implementation, specifics daemon classes may want to
#    override this to include their additional files
#
#  Parameters:
#        $name - daemon name
#
# Returns:
#  a list with each path as string
sub daemonFiles
{
    my ($class, $name) = @_;

    my $confDir  = $class->_openvpnModule->confDir();
    my $confFile = $class->confFileForName($name, $confDir);

    return ($confFile);
}

#
# Method: confFileParams
#
#   Abstract method. Must return the configuration file template parameters
#
# Returns:
#   a reference to the parameter's list
sub confFileParams
{
    throw EBox::Exceptions::NotImplemented();
}

#
# Method: ripDaemon
#
#   Abstract method. Must return the configuration file template
#
#
# Returns:
#   undef if no ripDaemon is needed by the openvpn's daemon
#   if the ripDaemons is needed it must return a hash refrence with the
#   following keys:
#       iface        - a hash ref returned from the method ifaceWithRipPasswd
#       redistribute - wether the daemon wants to redistribute routes or not
sub ripDaemon
{
    throw EBox::Exceptions::NotImplemented();
}

#   Method: ifaceWithRipPasswd
#
#  return a reference to a hash with the interface information needed to
#  configure the ripd daemon
#
#   Returns:
#        hash reference with the following fields
#              ifaceName - name of the network interface
#              passwd    - rip password for this daemon
sub ifaceWithRipPasswd
{
    my ($self) = @_;
    my $iface = $self->iface;
    my $passwd = $self->ripPasswd;

    return {
            ifaceName => $iface,
            passwd    => $passwd,
    };
}

#  Method: ripPasswd
#
#     get the password used by this daemon to secure RIP transmissions
#
#     Returns:
#        the password as string (empty string if the password wasn't set)
sub ripPasswd
{
    my ($self) = @_;
    my $passwd = $self->_configAttr('ripPasswd');
    defined $passwd or $passwd = '';   # in some cases it may be optional it may
    # be undefined
    return $passwd;
}

#  Method: setRipPasswd
#
#     set the password used by this daemon to secure RIP transmissions
#
#     Parameters:
#        passwd - string
sub setRipPasswd
{
    my ($self, $passwd) = @_;
    throw EBox::Exceptions::NotImplemented('setRipPasswd');
}

# Method: start
#
#  Start the daemon
sub start
{
    my ($self) = @_;
    EBox::Service::manage($self->upstartName, 'start');
}

#
# Method: stop
#
#  Stop the daemon
sub stop
{
    my ($self) = @_;
    EBox::Service::manage($self->upstartName, 'stop');
}

sub stopDeletedDaemon
{
    my ($class, $name, $type) = @_;

    $class->isa('EBox::OpenVPN::Daemon')
      or throw EBox::Exceptions::Internal(
                                      "$class is not a openvpn's daemon class");

    # see if we have upstart service
    my $upstartFile = $class->_upstartFileForDaemon($name, $type);
    if (not -f $upstartFile) {
        return;
    }

    my $upstartService = $class->upstartNameForDaemon($name, $type);
    EBox::Service::manage($upstartService, 'stop');
}

sub deletedDaemonCleanup
{
    my ($class, $name) = @_;
    my $type = $class->type();

    try {
        $class->stopDeletedDaemon($name, $type);
        $class->removeUpstartFileForDaemon($name, $type);

        foreach my $file( $class->daemonFiles($name) ) {
            EBox::Sudo::root("rm -rf '$file'");
        }
    }
    otherwise {
        my $ex = shift;
        EBox::error(
"Failure when cleaning up the deleted openvpn daemon $name. Possibly you will need to do some manual cleanup"
        );
        $ex->throw();
    };
}

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

    my $name    = $self->name();
    my $bin     = $self->_openvpnModule->openvpnBin();
    my $confDir = $self->_openvpnModule->confDir();
    my $confFilePath =   $self->confFile($confDir);
    my $pidFile = $self->_pidFile();

    return "$bin --syslog '$name' --config '$confFilePath'";
}

# Method: limitRespawn
#
#      Return if the respawn for the openvpn is limited to 5 times
#      within 40 seconds
#
# Returns:
#
#      Boolean - indicating the respawning process is limited
#
sub limitRespawn
{
    return 0;
}

# Method: _pidFile
#
#    Get the PID file to write to check the status afterwards
#
# Returns:
#
#    String - the path to the PID file for this daemon
#
sub _pidFile
{
    my ($self) = @_;
    return RUN_DIR . 'openvpn.' . $self->name() . '.pid';
}

# Method: pid
#
# Returns:
#     the pid of the daemon or undef if isn't running
sub pid
{
    my ($self) = @_;
    my $pid;

    try {
        $pid = File::Slurp::read_file($self->_pidFile());

    }
    otherwise {
        $pid = undef;
    };

    return $pid;
}

#
# Method: isRunning
#
# Returns:
#    bool - whether the daemon is running
sub isRunning
{
    my ($self) = @_;

    if (not -f $self->_upstartFile()) {
        return undef;
    }

    return EBox::Service::running($self->upstartName);
}

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

    my $path    = $self->_upstartFile();
    my $cmd     = $self->_rootCommandForStartDaemon();
    my $limited = $self->limitRespawn();

    my $fileAttrs    = {
                        uid  => 0,
                        gid  => 0,
                        mode => '0644',
    };

    EBox::Module::Base::writeConfFileNoCheck(
        $path, '/openvpn/upstart.mas',
        [ cmd => $cmd, limited => $limited],
        $fileAttrs
       );

}

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

    my $path = $self->_upstartFile();
    EBox::Sudo::root("rm -f '$path'");
}

sub removeUpstartFileForDaemon
{
    my ($class, $name, $type) = @_;
    $type
      or throw EBox::Exceptions::MissingArgument('type');
    $name
      or throw EBox::Exceptions::MissingArgument('name');

    my $path = $class->_upstartFileForDaemon($name, $type);
    EBox::Sudo::root("rm -f '$path'");
}

#
# Method: summary
#
#   Abstract method. May be implemented by any daemon which wants his summary section
#
# Returns:
#     the summary data as list; the first element will be the title of the summary
#     section and the following pairs will be usd to build EBox::Summary::Value objects
sub summary
{
    my ($self) = @_;
    return ();
}

1;