summaryrefslogtreecommitdiffstats
path: root/lib/network/connection/ppp.pm
blob: 0d90d8a2152e98b802d916acaa95937e6260e5f7 (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
package network::connection::ppp;

use strict;
use common;

use base qw(network::connection);

my %authentication_methods = (
    script => N_("Script-based"),
    pap => N_("PAP"),
    terminal_ => N_("Terminal-based"),
    chap => N_("CHAP"),
    pap_chap => N_("PAP/CHAP"),
);

my @kppp_authentication_methods = qw(script pap terminal chap pap_chap);
my @secrets_files = qw(pap-secrets chap-secrets);

sub get_access_settings {
    my ($self) = @_;
    [
        { label => N("Account Login (user name)"), val => \$self->{access}{login} },
        { label => N("Account Password"),  val => \$self->{access}{password}, hidden => 1 },
    ];
}

sub get_secret {
    my ($_self, $login) = @_;
    foreach (@secrets_files) {
        my $file = "$::prefix/etc/ppp/$_";
	foreach (cat_($file)) {
	    my ($l, undef, $password) = split(' ');
	    if ($l && $password) {
		s/^(['"]?)(.*)\1$/$2/ foreach $l, $password;
                return $password if $l eq $login;
	    }
	}
    }
}

sub write_secrets {
    my ($self) = @_;
    foreach (@secrets_files) {
        my $file = "$::prefix/etc/ppp/$_";
	substInFile {
            s/^'$self->{access}{login}'.*\n//;
            $_ .= "\n'$self->{access}{login}' * '$self->{access}{password}' * \n" if eof;
        } $file;
	chmod 0600, $file;
    }
}

sub get_options {
    #- options: PAPNAME PEERDNS
}

sub get_tty_device { "/dev/modem" }

sub build_ifcfg_settings {
    my ($self) = @_;
    my $modemport = $self->get_tty_device;
    my $settings = put_in_hash($self->{settings}, {
        if_($modemport, MODEMPORT => $modemport),
        LINESPEED => "115200",
        PERSIST => "yes",
        DEBUG => "yes",
        DEFROUTE => "yes",
    });
    $self->SUPER::build_ifcfg_settings($settings);
}

sub build_chat {
    my ($self) = @_;
    #- optional:
    #-   auth_method: key of %authentication_methods
    #-   dial_number
    #-   login
    #-   password
    #-   at_commands: array ref of AT commands
    (map { "ABORT $_" } "BUSY", "ERROR", "'NO CARRIER'", "'NO DIALTONE'", "'Invalid Login'", "'Login incorrect'", "VOICE", "'NO ANSWER'", "DELAYED", "'SIM PIN'"),
    qq('' ATZ),
    if_(ref $self->{access}{at_commands}, map { qq(OK-AT-OK '$_') } @{$self->{access}{at_commands}}),
    if_($self->{access}{dial_number},
        qq(OK 'ATDT$self->{access}{dial_number}'),
        qq(TIMEOUT 120),
        qq(CONNECT ''),
        if_(member($self->{access}{auth_method}, qw(script terminal)),
            qq('ogin:--ogin:' '$self->{access}{login}'),
            qq('ord:' '$self->{access}{password}')),
        qq(TIMEOUT 5),
        qq('~--' ''),
    );
}

sub write_chat {
    my ($self) = @_;
    my $interface = $self->get_interface;
    my $chat_file = "/etc/sysconfig/network-scripts/chat-$interface";
    output_with_perm($::prefix . $chat_file, 0755, join("\n", $self->build_chat, ''));
}

sub get_peer_default_options {
    my ($_self) = @_;
    qw(noauth defaultroute usepeerdns);
}

sub build_peer {
    my ($self) = @_;
    #- options:
    #-   init
    #-   connect
    #-   pty
    #-   plugin
    if ($self->{access}{dial_number}) {
        my $interface = $self->get_interface;
        my $chat_file = "/etc/sysconfig/network-scripts/chat-$interface";
        $self->{access}{peer}{connect} ||= qq("/usr/sbin/chat -v -f $chat_file");
    }
    $self->get_peer_default_options,
    (map { if_($self->{access}{peer}{$_}, $_ . " " . $self->{access}{peer}{$_}) } qw(init connect pty plugin)),
    if_($self->{access}{login}, qq(user "$self->{access}{login}"));
}

sub write_peer {
    my ($self) = @_;
    my $interface = $self->get_interface;
    my $peer_file = "/etc/ppp/peers/$interface";
    output_with_perm($::prefix . $peer_file, 0755, join("\n", $self->build_peer, ''));
}

sub write_settings  {
    my ($self) = @_;
    $self->write_secrets if $self->{access}{login};
    $self->write_chat if $self->{access}{dial_number};
    $self->write_peer;
    $self->network::connection::write_settings;
}

1;