summaryrefslogtreecommitdiffstats
path: root/lib/network/vpn.pm
blob: 21142c8031c531d036fa668b469ef7a24ddee78a (plain)
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
package network::vpn;

=head1 NAME

network::vpn - VPN connection abstract class

=cut

use strict;
use lib qw(/usr/lib/libDrakX);   # helps perl_checker
use common;

my $vpn_d = "/etc/sysconfig/network-scripts/vpn.d";

=head1 CLASS METHODS

=head2 Generic class methods

=over

=item list_types

List supported VPN types

=cut

sub list_types {
    common::load_modules_from_base(__PACKAGE__);
}

=item get_configured_connections

Return list of configured connections for this class

=cut

sub get_configured_connections {
    my ($class) = @_;
    map { if_(/^(.*).conf$/, $class->new($1)) } all($::prefix . $vpn_d . '/' . $class->get_type);
}

=item new(NAME)

Create a new VPN connection object named NAME

=cut

sub new {
    my ($class, $name) = @_;
    bless {
        name => $name,
    }, $class;
}

=back

=head2 Pure virtual class methods

=over

=item get_type

Return VPN type (preferably one lowercase word)

=item get_description

Return description of the VPN type

=item get_packages

List package required for configuration

=back

=head1 INSTANCE METHODS

=head2 Generic instance methods

=over

=item get_name

Return name of the VPN connection

=cut

sub get_name {
    my ($connection) = @_;
    $connection->{name};
}

=item get_label

Return label of the VPN connection

=cut

sub get_label {
    my ($connection) = @_;
    sprintf("%s (%s)", $connection->get_name, $connection->get_type);
}

=item get_config_path

Get configuration file path

=cut

sub get_config_path {
    my ($connection) = @_;
    $::prefix . $vpn_d . '/' . $connection->get_type . '/' . $connection->get_name . '.conf';
}

sub _run {
    my ($connection, $action, @args) = @_;
    my @command = ('vpn-' . $action, $connection->get_type, $connection->get_name, @args);
    @command = common::wrap_command_for_root(@command) if $>;
    require run_program;
    run_program::rooted($::prefix, , @command);
}

=item start($o_in)

Start the VPN connection

$o_in is an interactive object used to interact with the user,
used if some interactive username/passwords are required.
If not specified, there is no user interaction.

=cut

sub start {
    my ($connection, $_o_in) = @_;
    $connection->_run('start');
}

=item stop

Stop the VPN connection

=cut

sub stop {
    my ($connection) = @_;
    $connection->_run('stop');
}

=item is_started

Returns true if the VPN connection is started

=cut

sub is_started {
    my ($connection) = @_;
    my $pid = chomp_(cat_($::prefix . '/var/run/' . $connection->get_type . '-' . $connection->get_name . '.pid'));
    $pid && -e '/proc/' . $pid;
}

=back

=head2 Pure virtual instance methods

=over

=item read_config

Read configuration from the file returned by get_config_path()

=item write_config

Write configuration to the file returned by get_config_path()

=item get_settings

Return an array ref of interactive settings

=back

=head2 Optional instance methods

=over

=item prepare

Run commands or services that are required for the VPN type

=back

=cut

1;