Handle hostnames with upper-case letters
[webmin.git] / pptp-client / conn.cgi
1 #!/usr/local/bin/perl
2 # conn.cgi
3 # Open a PPTP connection and add its routes
4
5 require './pptp-client-lib.pl';
6 &error_setup($text{'conn_err'});
7 &ReadParse();
8
9 # Get tunnel details
10 @tunnels = &list_tunnels();
11 ($tunnel) = grep { $_->{'name'} eq $in{'tunnel'} } @tunnels;
12 $tunnel || &error($text{'conn_egone'});
13 &parse_comments($tunnel);
14 $tunnel->{'server'} || &error($text{'conn_einvalid'});
15
16 # Check if it is already active
17 @conns = &list_connected();
18 ($conn) = grep { $_->[0] eq $in{'tunnel'} } @conns;
19 $conn && &error($text{'conn_ealready'});
20
21 $theme_no_table++;
22 $| = 1;
23 &ui_print_header(undef, $text{'conn_title'}, "");
24
25 print "<b>",&text('conn_cmd', "<tt>$config{'pptp'} $tunnel->{'server'} ".
26                                  "call $in{'tunnel'}</tt>"),"</b><p>\n";
27
28 # If a password is passed, use it to temprarily overwrite the one in
29 # the secrets file
30 if ($in{'cpass'}) {
31         $login = &find("name", $tunnel->{'opts'});
32         $sn = $login ? $login->{'value'} : &get_system_hostname(1);
33         &lock_file($config{'pap_file'});
34         @secs = &list_secrets();
35         ($sec) = grep { $_->{'client'} eq $sn } @secs;
36         $oldsecret = $sec->{'secret'};
37         $sec->{'secret'} = $in{'cpass'};
38         &change_secret($sec);
39         &flush_file_lines();
40         ($ok, @status) = &connect_tunnel($tunnel);
41         $sec->{'secret'} = $oldsecret;
42         &change_secret($sec);
43         &flush_file_lines();
44         &unlock_file($config{'pap_file'});
45         &lock_file($config{'pap_file'});
46         }
47 else {
48         ($ok, @status) = &connect_tunnel($tunnel);
49         }
50
51 if ($ok) {
52         # Worked! Tell user
53         print "<b>",&text('conn_ok', "<tt>$status[0]</tt>",
54                           "<tt>$status[1]</tt>",
55                           "<tt>$status[2]</tt>"),"</b><p>\n";
56         local @rcmds = @{$status[3]};
57         local @rout = @{$status[4]};
58         if (@rcmds) {
59                 print "<b>$text{'conn_routes'}</b><pre>\n";
60                 for($i=0; $i<@rcmds; $i++) {
61                         print $rcmds[$i],"\n";
62                         print "<i>$rout[$i]</i>";
63                         }
64                 print "</pre>\n";
65                 }
66         }
67 else {
68         # Failed! Say why
69         print "<b>$text{'conn_timeout'}</b><p>\n";
70         print "<pre>$status[0]</pre>\n";
71         if ($status[0] =~ /mppe/) {
72                 print "<b>",&text('conn_mppe', "edit_opts.cgi"),"</b><p>\n";
73                 }
74         }
75
76 # Save tunnel as default
77 &lock_file("$module_config_directory/config");
78 $config{'tunnel'} = $in{'tunnel'};
79 $config{'iface'} = $status[0] if ($ok);
80 &write_file("$module_config_directory/config", \%config);
81 &unlock_file("$module_config_directory/config");
82
83 &webmin_log("conn", undef, $in{'tunnel'}, $newiface);
84 &ui_print_footer("", $text{'index_return'});
85
86